diff --git a/apps/desktop/src/main/services/gbc.ts b/apps/desktop/src/main/services/gbc.ts index 25d8475..ee9b21f 100644 --- a/apps/desktop/src/main/services/gbc.ts +++ b/apps/desktop/src/main/services/gbc.ts @@ -14,6 +14,8 @@ import { type GbcJobPicture, type GbcJobSnapshot, type GbcLanguage, + type GbcPreset, + type GbcProgram, type GbcParseProgress, type GbcParseResult, type GbcPicture, @@ -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). */ @@ -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() @@ -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' }, @@ -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 } } diff --git a/apps/desktop/src/renderer/src/lib/gbc.ts b/apps/desktop/src/renderer/src/lib/gbc.ts index fb89e60..0bf665c 100644 --- a/apps/desktop/src/renderer/src/lib/gbc.ts +++ b/apps/desktop/src/renderer/src/lib/gbc.ts @@ -7,6 +7,8 @@ import { type GbcDestination, type GbcJobSnapshot, type GbcLanguage, + type GbcPreset, + type GbcProgram, type GbcSetupProgress, type GbcStatus, } from '@shared/gbc' @@ -73,6 +75,8 @@ export interface GbcJobStart { providerId: string detector: 'owlv2' | 'sam3' language?: GbcLanguage + preset?: GbcPreset + program?: GbcProgram destination?: GbcDestination fresh?: boolean trainingId?: string @@ -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, diff --git a/apps/desktop/src/renderer/src/pages/studio/GbcPanel.tsx b/apps/desktop/src/renderer/src/pages/studio/GbcPanel.tsx index 62ec6eb..00264ef 100644 --- a/apps/desktop/src/renderer/src/pages/studio/GbcPanel.tsx +++ b/apps/desktop/src/renderer/src/pages/studio/GbcPanel.tsx @@ -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' @@ -47,6 +47,8 @@ export function GbcPanel({ const [providerId, setProviderId] = useState('') const [detector, setDetector] = useState<'owlv2' | 'sam3'>('owlv2') const [language, setLanguage] = useState('auto') + const [preset, setPreset] = useState('mid') + const [program, setProgram] = useState('standard') const [caption, setCaption] = useState('') const [installing, setInstalling] = useState(false) const [setupPhase, setSetupPhase] = useState(null) @@ -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')) } @@ -259,6 +261,17 @@ export function GbcPanel({ ]} /> +
+ + +
diff --git a/apps/desktop/src/renderer/src/pages/training/GbcLabel.tsx b/apps/desktop/src/renderer/src/pages/training/GbcLabel.tsx index f379924..b4e6988 100644 --- a/apps/desktop/src/renderer/src/pages/training/GbcLabel.tsx +++ b/apps/desktop/src/renderer/src/pages/training/GbcLabel.tsx @@ -75,6 +75,8 @@ export function GbcLabel({ providerId, detector: settings?.gbcDetector ?? 'owlv2', language: settings?.gbcLanguage ?? 'auto', + preset: 'mid', + program: 'standard', destination: 'training', trainingId, }) diff --git a/apps/desktop/src/shared/gbc.ts b/apps/desktop/src/shared/gbc.ts index 6ba11cd..5b24eec 100644 --- a/apps/desktop/src/shared/gbc.ts +++ b/apps/desktop/src/shared/gbc.ts @@ -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 } diff --git a/apps/desktop/src/shared/schemas.ts b/apps/desktop/src/shared/schemas.ts index 8c5e3c8..6638cd4 100644 --- a/apps/desktop/src/shared/schemas.ts +++ b/apps/desktop/src/shared/schemas.ts @@ -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(), }) diff --git a/apps/desktop/test/main/services/gbc.test.ts b/apps/desktop/test/main/services/gbc.test.ts index 965b30e..50fe751 100644 --- a/apps/desktop/test/main/services/gbc.test.ts +++ b/apps/desktop/test/main/services/gbc.test.ts @@ -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' @@ -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, } @@ -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 } @@ -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') @@ -91,6 +98,8 @@ beforeEach(() => { state.settings = settingsFixture({ gbcEnabled: true, gbcDetector: 'sam3' }) state.emitted = [] state.configs = [] + state.args = [] + state.versions = [] state.behaviour = writes(graph) }) @@ -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'), + ]) + }) +}) diff --git a/docs/reference/studio-node-board.md b/docs/reference/studio-node-board.md index 51ff630..936ba13 100644 --- a/docs/reference/studio-node-board.md +++ b/docs/reference/studio-node-board.md @@ -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