Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions apps/desktop/src/main/services/gbc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
type GbcJobPicture,
type GbcJobSnapshot,
type GbcLanguage,
type GbcPreset,
type GbcProgram,
type GbcParseProgress,
type GbcParseResult,
type GbcPicture,
Expand Down Expand Up @@ -284,6 +286,27 @@ export class GbcStopped extends Error {
}
}

/**
* Extra config layers after the generated machine: a shipped control preset,
* then GBC-Nest. They live in the installed version's clone; without one there
* is nothing to merge, and Nest says so instead of silently running the plain
* program.
*/
export function gbcConfigLayers(versionPath: string | null, options: { preset?: GbcPreset; program?: GbcProgram }): string[] {
if (!versionPath) return []
const layers: string[] = []
if (options.preset) {
const preset = join(versionPath, 'configs', 'controls', `${options.preset}.py`)
if (existsSync(preset)) layers.push(preset)
}
if (options.program === 'nest') {
const nest = join(versionPath, 'configs', 'programs', 'gbc_nest.py')
if (!existsSync(nest)) throw new Error('这个 KohakuGBC 版本没有 GBC-Nest(configs/programs/gbc_nest.py)')
layers.push(nest)
}
return layers
}

export interface GbcRunControl {
signal?: AbortSignal
/** Every output line, progress bars included (they redraw with a carriage return). */
Expand Down Expand Up @@ -322,6 +345,7 @@ export async function runGbc(input: GbcRunInput, control: GbcRunControl = {}): P
const configPath = join(out, 'machine.py')
await writeFile(configPath, configFor(input, provider, out, keyEnv, device, sourceSpec), 'utf8')
const version = input.versionId ? (await listGbcVersions(getSettings())).find((item) => item.id === input.versionId) : null
const layers = gbcConfigLayers(version?.path ?? null, input)
const bundled = version ? versionBinary(version) : ''
const command = bundled && existsSync(bundled) ? bundled : process.env['CLOVER_GBC_BIN'] || 'gbc'
const controller = ownedController()
Expand All @@ -332,7 +356,7 @@ export async function runGbc(input: GbcRunInput, control: GbcRunControl = {}): P
try {
result = await runStream(
command,
['run', configPath],
['run', configPath, ...layers],
{
cwd: version?.path ?? out,
env: { [keyEnv]: provider.apiKey, PYTHONUNBUFFERED: '1' },
Expand Down Expand Up @@ -710,6 +734,6 @@ export async function startGbcJob(input: GbcJobInput): Promise<{ jobId: string;
jobs.set(jobId, { snapshot, controller: new AbortController() })
trimJobs()
emit(Events.gbcJob, { ...snapshot })
void runJob(jobId, files, input, language)
void runJob(jobId, files, { ...input, versionId: input.versionId ?? getSettings().gbcVersionId ?? undefined }, language)
return { jobId, total: files.length }
}
6 changes: 6 additions & 0 deletions apps/desktop/src/renderer/src/lib/gbc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
type GbcDestination,
type GbcJobSnapshot,
type GbcLanguage,
type GbcPreset,
type GbcProgram,
type GbcSetupProgress,
type GbcStatus,
} from '@shared/gbc'
Expand Down Expand Up @@ -73,6 +75,8 @@ export interface GbcJobStart {
providerId: string
detector: 'owlv2' | 'sam3'
language?: GbcLanguage
preset?: GbcPreset
program?: GbcProgram
destination?: GbcDestination
fresh?: boolean
trainingId?: string
Expand Down Expand Up @@ -108,6 +112,8 @@ export function useGbcJob(): {
providerId: input.providerId,
detector: input.detector,
language: input.language,
preset: input.preset,
program: input.program,
destination: input.destination,
fresh: input.fresh,
trainingId: input.trainingId,
Expand Down
17 changes: 15 additions & 2 deletions apps/desktop/src/renderer/src/pages/studio/GbcPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ImagePlus, Square, X } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { Events, Ipc } from '@shared/channels'
import type { GbcLanguage, GbcPicture, GbcSetupProgress } from '@shared/gbc'
import type { GbcLanguage, GbcPicture, GbcPreset, GbcProgram, GbcSetupProgress } from '@shared/gbc'
import type { ProjectCanvas } from '@shared/types'
import { Button, FieldLabel, Segmented, Select, Spinner, Textarea, useToast } from '../../components/ui'
import { Notice } from '../../components/common/Notice'
Expand Down Expand Up @@ -47,6 +47,8 @@ export function GbcPanel({
const [providerId, setProviderId] = useState('')
const [detector, setDetector] = useState<'owlv2' | 'sam3'>('owlv2')
const [language, setLanguage] = useState<GbcLanguage>('auto')
const [preset, setPreset] = useState<GbcPreset>('mid')
const [program, setProgram] = useState<GbcProgram>('standard')
const [caption, setCaption] = useState('')
const [installing, setInstalling] = useState(false)
const [setupPhase, setSetupPhase] = useState<GbcSetupProgress['phase'] | null>(null)
Expand Down Expand Up @@ -93,7 +95,7 @@ export function GbcPanel({
if (!source || !providerId) return
setCaption('')
void job
.run({ path: source.path, providerId, detector, language, destination: 'studio', fresh })
.run({ path: source.path, providerId, detector, language, preset, program, destination: 'studio', fresh })
.catch((error: unknown) => toast.push(error instanceof Error ? error.message : String(error), 'error'))
}

Expand Down Expand Up @@ -259,6 +261,17 @@ export function GbcPanel({
]}
/>
</div>
<div className="flex gap-2">
<Select aria-label="解析规模" value={preset} onChange={(event) => setPreset(event.target.value as GbcPreset)}>
<option value="small">规模:小</option>
<option value="mid">规模:中</option>
<option value="large">规模:大</option>
</Select>
<Select aria-label="解析程序" value={program} onChange={(event) => setProgram(event.target.value as GbcProgram)}>
<option value="standard">程序:标准</option>
<option value="nest">程序:分格(GBC-Nest)</option>
</Select>
</div>
</section>

<div className="flex items-center gap-2">
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/renderer/src/pages/training/GbcLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export function GbcLabel({
providerId,
detector: settings?.gbcDetector ?? 'owlv2',
language: settings?.gbcLanguage ?? 'auto',
preset: 'mid',
program: 'standard',
destination: 'training',
trainingId,
})
Expand Down
8 changes: 8 additions & 0 deletions apps/desktop/src/shared/gbc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ export interface GbcGraph {
edges?: unknown[]
}

export type GbcPreset = 'small' | 'mid' | 'large'

export type GbcProgram = 'standard' | 'nest'

export interface GbcRunInput {
folder?: string
files?: string[]
providerId: string
detector: 'owlv2' | 'sam3'
versionId?: string
/** Shipped control preset merged after the machine: smaller or larger graphs. */
preset?: GbcPreset
/** `nest` adds GBC-Nest, which reads panels and people better than the plain program. */
program?: GbcProgram
sidecarJson?: boolean
sidecarTxt?: boolean
}
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/shared/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ export const schemas = {
versionId: z.string().max(160).nullish(),
language: z.enum(['auto', 'zh', 'en']).optional(),
destination: z.enum(['studio', 'training']).optional(),
preset: z.enum(['small', 'mid', 'large']).optional(),
program: z.enum(['standard', 'nest']).optional(),
trainingId: z.string().min(1).max(80).optional(),
fresh: z.boolean().optional(),
})
Expand Down
54 changes: 51 additions & 3 deletions apps/desktop/test/main/services/gbc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname } from 'node:path'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { RunOptions, RunResult } from '@main/lib/run'
import type { GbcGraph, GbcParseProgress } from '@shared/gbc'
Expand All @@ -25,6 +25,8 @@ const state = {
],
emitted: [] as Array<[string, unknown]>,
configs: [] as string[],
args: [] as string[][],
versions: [] as Array<{ id: string; path: string }>,
behaviour: (async () => ({})) as Behaviour,
}

Expand Down Expand Up @@ -59,6 +61,7 @@ vi.mock('@main/lib/run', async (importOriginal) => ({
// No NVIDIA here: GBC runs on the CPU.
run: async () => ({ code: 1, stdout: '', stderr: '', timedOut: false }),
runStream: async (_command: string, args: string[], options: RunOptions, onLine: (line: string) => void) => {
state.args.push(args)
state.configs.push(readFileSync(args[1]!, 'utf8'))
const result = await state.behaviour(args, options, onLine)
return { code: 0, stdout: '', stderr: '', timedOut: false, ...result }
Expand All @@ -74,7 +77,11 @@ vi.mock('@main/services/ai', () => ({
return provider ? { kind: provider.kind, baseUrl: provider.baseUrl, model: provider.model, apiKey: 'sk-test' } : null
},
}))
vi.mock('@main/services/gbcGit', () => ({ listGbcVersions: async () => [], listGbcBranches: vi.fn(), createGbcVersion: vi.fn() }))
vi.mock('@main/services/gbcGit', () => ({
listGbcVersions: async () => state.versions,
listGbcBranches: vi.fn(),
createGbcVersion: vi.fn(),
}))
vi.mock('@main/services/runtimes', () => ({ pickPython: vi.fn(), resolveTorchIndex: vi.fn() }))

const load = () => import('@main/services/gbc')
Expand All @@ -91,6 +98,8 @@ beforeEach(() => {
state.settings = settingsFixture({ gbcEnabled: true, gbcDetector: 'sam3' })
state.emitted = []
state.configs = []
state.args = []
state.versions = []
state.behaviour = writes(graph)
})

Expand Down Expand Up @@ -202,3 +211,42 @@ describe('parseProgressLine', () => {
expect(parseProgressLine('admitted 1 job')).toBeNull()
})
})

describe('gbcConfigLayers', () => {
const version = (options: { preset?: string; nest?: boolean } = {}): string => {
const base = join(root(), 'gbc-version')
mkdirSync(join(base, 'configs', 'controls'), { recursive: true })
if (options.preset) writeFileSync(join(base, 'configs', 'controls', `${options.preset}.py`), 'controls = {}\n')
if (options.nest) {
mkdirSync(join(base, 'configs', 'programs'), { recursive: true })
writeFileSync(join(base, 'configs', 'programs', 'gbc_nest.py'), 'program = "nest"\n')
}
return base
}

it('adds an existing control preset, skips a missing one, and refuses a version without GBC-Nest', async () => {
const { gbcConfigLayers } = await load()
const base = version({ preset: 'mid' })
expect(gbcConfigLayers(base, { preset: 'mid' })).toEqual([join(base, 'configs', 'controls', 'mid.py')])
expect(gbcConfigLayers(base, { preset: 'large' })).toEqual([])
expect(gbcConfigLayers(null, { preset: 'mid', program: 'nest' })).toEqual([])
expect(() => gbcConfigLayers(base, { program: 'nest' })).toThrow('GBC-Nest')
})

it('runs the installed version with the preset and GBC-Nest after the generated machine', async () => {
const base = version({ preset: 'mid', nest: true })
state.versions = [{ id: 'v1', path: base }]
state.behaviour = async (args) => {
writeFileSync(sinkOf(args[1]!), `${JSON.stringify(graph)}\n`)
return {}
}
const { runGbc } = await load()
await runGbc({ files: [picture()], providerId: 'claude', detector: 'owlv2', versionId: 'v1', preset: 'mid', program: 'nest' })
expect(state.args.at(-1)).toEqual([
'run',
expect.any(String),
join(base, 'configs', 'controls', 'mid.py'),
join(base, 'configs', 'programs', 'gbc_nest.py'),
])
})
})
2 changes: 1 addition & 1 deletion docs/reference/studio-node-board.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ The Studio does not read `preview`: a live preview shows on the card whenever th

The 助手 acts on the selected card through tools; [studio-assistant.md](/docs/reference/studio-assistant.md) has the registry, modes and cards. What the board itself does for it: the tool API lives in `studio/StudioPage.tsx` (`StudioToolApi`) and reads live state at call time; regions added by a tool go through the same `addRegion` and `editLayer` as the UI (`studio/canvases.ts`), so a tool cannot make a region the compiler rejects; a plan waiting for 审批 is previewed on the selected 画面 as dashed boxes tagged 方案 (`plannedRegions`, `lib/shell.ts` `planPreview`, `studio/RegionLayer.tsx`) until it is applied or dropped; `studio_create_scenes` places its new 画面 with `sceneCanvas` in free board spots.

解析 sits in the top bar next to 助手 (both open the right side; opening one closes the other) and changes nothing until the person acts. It reads a picture with KohakuGBC: a dropped file, a 作品 pick, or a picture already on the board; the run is one job in the main process (`main/services/gbc.ts` `startGbcJob`) with cache by picture hash, version, detector, provider and language, and progress on `Events.gbcJob`. The panel shows the caption and the boxes over the picture; 复制提示词 copies the caption; 变成画面 makes a 新 画面 with the description and the regions (the same `withParsedRegions` the card menu uses); 作为参考图 imports the picture and links it into the selected 画面 as a vision reference; 重新读 ignores the cache. The card menu 拆成区域 keeps its older quick path: it creates the 画面 next to the card and fills it, using the same parser. 自动 / 中文 / English controls the text: 自动 is English for a 画面 and Chinese for a training caption; 中文 translates through the same provider and falls back to the English with a warning. Without an install the panel offers 安装; 设置 › 功能 keeps the versions, detector and model. The e2e is `apps/desktop/e2e/specs/gbc.e2e.ts`.
解析 sits in the top bar next to 助手 (both open the right side; opening one closes the other) and changes nothing until the person acts. It reads a picture with KohakuGBC: a dropped file, a 作品 pick, or a picture already on the board; the run is one job in the main process (`main/services/gbc.ts` `startGbcJob`) with cache by picture hash, version, detector, provider and language, and progress on `Events.gbcJob`. The panel shows the caption and the boxes over the picture; 复制提示词 copies the caption; 变成画面 makes a 新 画面 with the description and the regions (the same `withParsedRegions` the card menu uses); 作为参考图 imports the picture and links it into the selected 画面 as a vision reference; 重新读 ignores the cache. 规模 picks one of the shipped control presets (小 / 中 / 大) and 程序 picks the plain program or GBC-Nest (分格), both passed as config layers after the generated machine (`gbcConfigLayers`): a missing preset is skipped, a version without `configs/programs/gbc_nest.py` is refused with a message. The job runs the installed version (`gbcVersionId`) unless the caller names another. The card menu 拆成区域 keeps its older quick path: it creates the 画面 next to the card and fills it, using the same parser. 自动 / 中文 / English controls the text: 自动 is English for a 画面 and Chinese for a training caption; 中文 translates through the same provider and falls back to the English with a warning. Without an install the panel offers 安装; 设置 › 功能 keeps the versions, detector and model. The e2e is `apps/desktop/e2e/specs/gbc.e2e.ts`.

## 12. Regions

Expand Down
Loading