Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
fix(creator): improve media workflows and runtime reliability
Leggett3a44609cd669
diff --git a/apps/daemon/src/api/routes.creator-runtime.ts b/apps/daemon/src/api/routes.creator-runtime.tsindex d321d0c2..e22a3917 100644--- a/apps/daemon/src/api/routes.creator-runtime.ts+++ b/apps/daemon/src/api/routes.creator-runtime.ts@@ -2,13 +2,17 @@ import type { FastifyInstance, FastifyReply } from 'fastify';import type { YtDlpUpdateManager } from '../creator/yt-dlp/update-manager.js';import { YtDlpUpdateError } from '../creator/yt-dlp/update-manager.js';import { apiError } from './errors.js';+import type { CreatorServicesConfig } from '@opencreator/protocol';+import type { createKrillinDependencyLoader } from '../creator/krillin/dependency-loader.js';export async function registerCreatorRuntimeRoutes(server: FastifyInstance,- ytDlp: YtDlpUpdateManager | undefined+ ytDlp: YtDlpUpdateManager | undefined,+ local?: { loader: ReturnType<typeof createKrillinDependencyLoader>; readConfig(): Promise<CreatorServicesConfig> }): Promise<void> {server.addHook('preClose', () => {ytDlp?.close();+ local?.loader.close();});server.get('/creator/yt-dlp/status', async (_request, reply) => {@@ -34,6 +38,19 @@ export async function registerCreatorRuntimeRoutes(return sendUpdateError(reply, error);}});++ server.get('/creator/components/status', async (_request, reply) => {+ if (local === undefined) return reply.code(503).send(apiError('creator_components_unavailable', 'Local components are unavailable'));+ return local.loader.status(await local.readConfig());+ });+ server.post('/creator/components/download', async (_request, reply) => {+ if (local === undefined) return reply.code(503).send(apiError('creator_components_unavailable', 'Local components are unavailable'));+ try {+ return await local.loader.download(await local.readConfig());+ } catch (error) {+ return reply.code(400).send(apiError('creator_component_download_unavailable', error instanceof Error ? error.message : 'Component download unavailable'));+ }+ });}function readForce(value: unknown): boolean {diff --git a/apps/daemon/src/api/routes.creator-services.ts b/apps/daemon/src/api/routes.creator-services.tsindex 8aaa6e5e..da53ebb8 100644--- a/apps/daemon/src/api/routes.creator-services.ts+++ b/apps/daemon/src/api/routes.creator-services.ts@@ -1,4 +1,5 @@import type {+ CodexImageStatus,CreatorServicesCapabilitiesResponse,CreatorServicesConfig,CreatorTtsPreviewRequest,@@ -26,9 +27,17 @@ export async function registerCreatorServicesRoutes(readCapabilities: () => CreatorServicesCapabilitiesResponse =createKrillinCreatorServicesCapabilities,ttsService?: Pick<KrillinTtsService, 'listVoices' | 'preview'>,- onConfigurationChanged?: () => Promise<void> | void+ onConfigurationChanged?: () => Promise<void> | void,+ readCodexImageStatus?: () => Promise<CodexImageStatus>): Promise<void> {server.get('/creator-services/capabilities', async () => readCapabilities());+ server.get('/creator-services/image/codex/status', async (_request, reply) => {+ reply.header('Cache-Control', 'no-store');+ return readCodexImageStatus ? await readCodexImageStatus() : {+ authentication: 'none', ready: false, executionMode: null,+ message: '当前 Runtime 未配置 Codex 生图能力检查,请更新或检查 Runtime'+ } satisfies CodexImageStatus;+ });server.get('/creator-services/config', async (_request, reply) => {try {diff --git a/apps/daemon/src/api/routes.video-metadata.ts b/apps/daemon/src/api/routes.video-metadata.tsindex e364b3b5..7662c2be 100644--- a/apps/daemon/src/api/routes.video-metadata.ts+++ b/apps/daemon/src/api/routes.video-metadata.ts@@ -19,7 +19,7 @@ export async function registerVideoMetadataRoutes(return await service.get(url);} catch (error) {if (error instanceof VideoMetadataError) {- if (error.code === 'UNSUPPORTED_SOURCE') {+ if (error.code === 'UNSUPPORTED_SOURCE' || error.code === 'INVALID_PART') {return reply.code(400).send(apiError('VALIDATION_FAILED', error.message));}return reply.code(502).send(apiError('VIDEO_METADATA_UNAVAILABLE', error.message));diff --git a/apps/daemon/src/api/server.ts b/apps/daemon/src/api/server.tsindex e663b24b..0c4d99bc 100644--- a/apps/daemon/src/api/server.ts+++ b/apps/daemon/src/api/server.ts@@ -91,6 +91,7 @@ import { CreatorProviderRequestLedger } from '../creator/provider-requests.js';import { createCreatorProjectCoverService } from '../creator/project-cover.js';import { createVideoGenerationService } from '../video-generation/service.js';import { createImageGenerationService } from '../image-generation/service.js';+import { inspectCodexImageRuntime } from '../image-generation/codex-runtime.js';import {createCreatorReferenceImageUploadService,type CreatorReferenceImageUploadService@@ -290,6 +291,7 @@ export type BuildServerInput = {getCodexAvailabilityProbe?(): CodexAvailabilityProbe | undefined;memoryHistoryReader?(threadId: string): { items: import('@opencreator/protocol').ThreadHistoryItem[] } | undefined;creatorServicesConfigStore?: CreatorServicesConfigStore;+ videoMetadataService?: VideoMetadataService;codexProviderCredentialStore?: CodexProviderCredentialStore;creatorService?: CreatorService;creatorPresetRegistry?: CreatorPresetRegistry;@@ -620,6 +622,9 @@ export async function buildServer(input: BuildServerInput) {}});+ const videoMetadataService = input.videoMetadataService ?? createVideoMetadataService({+ getProxy: async () => (await creatorServicesConfigStore.read()).proxy.trim()+ });const creatorService = input.creatorService ?? createCreatorService({jobsRoot: creatorJobsRoot,repository: creatorRepository,@@ -660,10 +665,12 @@ export async function buildServer(input: BuildServerInput) {dataDir,configStore: creatorServicesConfigStore});+ const codexImageRuntime = { codexHome, codexBin };+ const readCodexImageStatus = () => inspectCodexImageRuntime(codexImageRuntime);const imageGenerationService = createImageGenerationService({dataDir,configStore: creatorServicesConfigStore,- codexNative: { codexHome }+ codexNative: codexImageRuntime});const developmentStickmanRuntimeRoot = resolve(dirname(fileURLToPath(import.meta.url)),@@ -714,7 +721,7 @@ export async function buildServer(input: BuildServerInput) {creatorExecutors.push(createStickmanImageExecutor({configStore: creatorServicesConfigStore,ledger: creatorProviderRequestLedger,- codexNative: { codexHome },+ codexNative: codexImageRuntime,...(creatorTesseractPath === undefined ? {} : { tesseractPath: creatorTesseractPath })}));}@@ -871,7 +878,7 @@ export async function buildServer(input: BuildServerInput) {}));creatorExecutors.push(createImageExecutor({configStore: creatorServicesConfigStore,- codexNative: { codexHome },+ codexNative: codexImageRuntime,...(creatorFfmpegPath === undefined? {}: {@@ -894,11 +901,13 @@ export async function buildServer(input: BuildServerInput) {model: createWechatArticleModel({ configStore: creatorServicesConfigStore }),imageGenerator: createArticleImageGenerator({configStore: creatorServicesConfigStore,- codexNative: { codexHome }+ codexNative: codexImageRuntime})}));}const creatorPreflight = createCreatorPreflight({+ readCodexImageStatus,+ videoMetadataService,configStore: creatorServicesConfigStore,readCapabilities: () => krillinDependencyLoader.capabilities(),resourceRoot: creatorRuntimeRoot,@@ -1082,7 +1091,8 @@ export async function buildServer(input: BuildServerInput) {: createVideoTranslationWorkflow({creator: creatorService,dispatcher: creatorCommandDispatcher,- configStore: creatorServicesConfigStore+ configStore: creatorServicesConfigStore,+ videoMetadataService});coverWorkflow = creatorStageRunner === undefined? undefined@@ -1365,6 +1375,7 @@ export async function buildServer(input: BuildServerInput) {};}});+ await registerVideoMetadataRoutes(server, videoMetadataService);await registerProjectRoutes(server, projectManager, runManager);await registerSkillRoutes(server, { skillManager });await registerSkillMarketRoutes(server, { skillMarketManager });@@ -1409,10 +1420,14 @@ export async function buildServer(input: BuildServerInput) {await videoTranslationWorkflow?.resumeConfiguredJobs();await coverWorkflow?.resumeConfiguredJobs();await stickmanVideoWorkflow?.resumeConfiguredJobs();- }+ },+ readCodexImageStatus);await registerSmartDubbingRoutes(server, smartDubbingService);- await registerCreatorRuntimeRoutes(server, creatorYtDlpUpdateManager);+ await registerCreatorRuntimeRoutes(server, creatorYtDlpUpdateManager, {+ loader: krillinDependencyLoader,+ readConfig: () => creatorServicesConfigStore.read()+ });await registerImageGenerationRoutes(server, imageGenerationService);await registerCreatorRoutes(server, creatorService, creatorEvents, {sseHeartbeatMs: input.sseHeartbeatMs,diff --git a/apps/daemon/src/codex/argv.ts b/apps/daemon/src/codex/argv.tsindex c5f30237..6ab26124 100644--- a/apps/daemon/src/codex/argv.ts+++ b/apps/daemon/src/codex/argv.ts@@ -9,6 +9,7 @@ export type BuildCodexExecArgsInput = {imagePaths?: string[];mcpServers?: CodexMcpServerConfig[];builtInTools?: BuiltInToolPolicy;+ imageGenerationOnly?: boolean;};export type BuildCodexResumeArgsInput = {@@ -69,7 +70,14 @@ export type CodexMcpServerConfig =export function buildCodexExecArgs(input: BuildCodexExecArgsInput): string[] {const args = ['exec', '--json', '--skip-git-repo-check'];- if (input.builtInTools !== undefined) {+ if (input.imageGenerationOnly) {+ args.push(+ '--ignore-user-config',+ '-c', 'cli_auth_credentials_store="file"',+ '-c', 'forced_login_method="chatgpt"',+ ...codexToolIsolationArgs({ shell: false, fileRead: false, fileWrite: false, applyPatch: false, webSearch: false }, { imageGeneration: true })+ );+ } else if (input.builtInTools !== undefined) {args.push('--ignore-user-config', ...codexToolIsolationArgs(input.builtInTools));}@@ -105,7 +113,7 @@ export function buildCodexResumeArgs(input: BuildCodexResumeArgsInput): string[]return args;}-export function codexToolIsolationArgs(policy: BuiltInToolPolicy): string[] {+export function codexToolIsolationArgs(policy: BuiltInToolPolicy, options: { imageGeneration?: boolean } = {}): string[] {if (Object.values(policy).some(enabled => enabled)) {throw new Error('Restricted built-in tool policy must disable every tool');}@@ -128,7 +136,9 @@ export function codexToolIsolationArgs(policy: BuiltInToolPolicy): string[] {'-c', 'plugins={}','-c', 'web_search="disabled"','-c', 'notify=[]',- ...disabled.flatMap(feature => ['--disable', feature])+ ...disabled.filter(feature => !options.imageGeneration || feature !== 'image_generation')+ .flatMap(feature => ['--disable', feature]),+ ...(options.imageGeneration ? ['--enable', 'image_generation', '--disable', 'view_image'] : [])];}diff --git a/apps/daemon/src/codex/local-provider.ts b/apps/daemon/src/codex/local-provider.tsindex d4ae3944..a0336d6d 100644--- a/apps/daemon/src/codex/local-provider.ts+++ b/apps/daemon/src/codex/local-provider.ts@@ -10,6 +10,10 @@ export type LocalCodexProvider = {model: string;};+export type LocalCodexImageConfiguration =+ | { authentication: 'api_key'; provider: LocalCodexProvider }+ | { authentication: 'chatgpt' };+export class LocalCodexProviderError extends Error {constructor(message: string) {super(message);@@ -17,6 +21,21 @@ export class LocalCodexProviderError extends Error {}}+export async function readLocalCodexImageConfiguration(input: {+ codexHome: string;+ env?: NodeJS.ProcessEnv;+}): Promise<LocalCodexImageConfiguration> {+ const auth = await readJson(join(input.codexHome, 'auth.json'), true);+ const tokens = readRecord(auth?.tokens);+ if (auth?.auth_mode !== 'apikey' && (auth?.auth_mode === 'chatgpt' || readString(tokens?.access_token))) {+ if (!readString(tokens?.access_token)) {+ throw new LocalCodexProviderError('ChatGPT 登录凭据不完整,请在 Agent 设置中重新登录');+ }+ return { authentication: 'chatgpt' };+ }+ return { authentication: 'api_key', provider: await readLocalCodexProvider(input) };+}+export async function readLocalCodexProvider(input: {codexHome: string;env?: NodeJS.ProcessEnv;@@ -42,7 +61,7 @@ export async function readLocalCodexProvider(input: {}if (!apiKey) {throw new LocalCodexProviderError(- '本机 Codex 当前只有 ChatGPT 登录态,未配置图像接口可用的 API Key'+ '本机 Codex 未配置图像接口可用的 API Key,请检查 Agent 配置');}return {@@ -63,12 +82,15 @@ async function readToml(path: string): Promise<Record<string, TomlValue>> {}}-async function readJson(path: string): Promise<Record<string, unknown> | undefined> {+async function readJson(path: string, strict = false): Promise<Record<string, unknown> | undefined> {try {const value = JSON.parse(await readFile(path, 'utf8')) as unknown;- return readRecord(value);+ const record = readRecord(value);+ if (strict && record === undefined) throw new Error('Invalid auth document');+ return record;} catch (error) {if (isNotFound(error)) return undefined;+ if (strict) throw new LocalCodexProviderError('无法读取本机 Codex 登录凭据,请在 Agent 设置中重新登录或配置 API Key');return undefined;}}diff --git a/apps/daemon/src/codex/runner.ts b/apps/daemon/src/codex/runner.tsindex 9f9347a7..a5efa8c3 100644--- a/apps/daemon/src/codex/runner.ts+++ b/apps/daemon/src/codex/runner.ts@@ -25,7 +25,7 @@ export type RunCodexExecInput = {inactivityTimeoutMs?: number;forceKillGraceMs?: number;finalKillSettleMs?: number;- env?: Record<string, string>;+ env?: NodeJS.ProcessEnv;beforeSpawn?: () => Promise<void>;onStdoutLine?: (line: string) => Promise<void> | void;onStderrChunk?: (chunk: string) => Promise<void> | void;diff --git a/apps/daemon/src/creator/article/executor.ts b/apps/daemon/src/creator/article/executor.tsindex dc8caa3a..796a9dd3 100644--- a/apps/daemon/src/creator/article/executor.ts+++ b/apps/daemon/src/creator/article/executor.ts@@ -22,6 +22,7 @@ import { CreatorExecutorError } from '../executor.js';import { validateImageFile } from '../validators/image.js';import { writeArticleHtml, writeArticlePdf } from './document-generator.js';import type { ArticleImageGenerationResult } from './image-generator.js';+import type { NativeImageProgress } from '../../image-generation/codex-native.js';import type { ExtractedArticleSource } from './source-extractor.js';export function createWechatArticleExecutor(input: {@@ -67,7 +68,7 @@ export function createWechatArticleExecutor(input: {}): Promise<WechatArticleImagePlanItem[]>;};imageGenerator?: {- generate(request: { prompt: string; signal: AbortSignal; cwd?: string }): Promise<ArticleImageGenerationResult>;+ generate(request: { prompt: string; signal: AbortSignal; cwd?: string; onProgress?(progress: NativeImageProgress): void }): Promise<ArticleImageGenerationResult>;};}): CreatorExecutor {return {@@ -297,7 +298,10 @@ export function createWechatArticleExecutor(input: {'Do not render titles, paragraphs, logos, watermarks, UI, or illegible decorative text.'].join('\n\n'),signal: stage.signal,- cwd: stage.workdir+ cwd: stage.workdir,+ onProgress: progress => stage.reportProgress({+ ...progress, completed: outputs.length, failed: failures.length, total: count+ })});const extension = extensionForMime(generated.mime);const fileName = `article-image-${String(index + 1).padStart(2, '0')}.${extension}`;diff --git a/apps/daemon/src/creator/article/image-generator.ts b/apps/daemon/src/creator/article/image-generator.tsindex f53afb76..c63b5892 100644--- a/apps/daemon/src/creator/article/image-generator.ts+++ b/apps/daemon/src/creator/article/image-generator.ts@@ -8,6 +8,7 @@ import {} from '../../image-generation/provider.js';import { CreatorExecutorError } from '../executor.js';import { resolveCreatorImageSettings } from '../image-settings.js';+import type { NativeImageProgress } from '../../image-generation/codex-native.js';export type ArticleImageGenerationResult = GeneratedImageContent & {provider: ImageGenerationProvider;@@ -25,6 +26,7 @@ export function createArticleImageGenerator(input: {prompt: string;signal: AbortSignal;cwd?: string;+ onProgress?(progress: NativeImageProgress): void;}): Promise<ArticleImageGenerationResult> {const config = await input.configStore.read();const { provider } = resolveCreatorImageSettings({@@ -41,6 +43,7 @@ export function createArticleImageGenerator(input: {count: 1}, config, {signal: request.signal,+ onProgress: request.onProgress,...(input.codexNative === undefined? {}: { codexNative: input.codexNative })diff --git a/apps/daemon/src/creator/image/executor.ts b/apps/daemon/src/creator/image/executor.tsindex 7b28d722..e8dc1f69 100644--- a/apps/daemon/src/creator/image/executor.ts+++ b/apps/daemon/src/creator/image/executor.ts@@ -111,6 +111,9 @@ export function createImageExecutor(input: {config,{signal: stage.signal,+ onProgress: progress => stage.reportProgress({+ ...progress, completed, failed, total: request.count+ }),...(referenceImage === undefined? {}: {diff --git a/apps/daemon/src/creator/krillin/adapter.ts b/apps/daemon/src/creator/krillin/adapter.tsindex d0de8eff..18011576 100644--- a/apps/daemon/src/creator/krillin/adapter.ts+++ b/apps/daemon/src/creator/krillin/adapter.ts@@ -40,7 +40,8 @@ export function createKrillinExecutor(input: {}): CreatorExecutor {return {id: 'krillinai',- async run(stage): Promise<CreatorExecutorResult> {+ async run(stageInput): Promise<CreatorExecutorResult> {+ const stage = filterObsoleteSourceVideo(stageInput);await input.ensureRuntimeReady?.();const configured = await input.configStore.read();const preflight = preflightKrillinDependencies(input.resourceRoot, configured, {@@ -67,7 +68,8 @@ export function createKrillinExecutor(input: {await ensureKrillinTranscriptionDependency(input.dependencyLoader,preflight.config,- stage+ stage,+ deferredFailure !== undefined);}completed = await runKrillinCli({@@ -126,7 +128,10 @@ export function createKrillinExecutor(input: {ffprobe});return {- outputs,+ outputs: stage === stageInput ? outputs : outputs.map(output => ({+ ...output,+ sourceArtifactIds: output.sourceArtifactIds ?? stage.inputArtifacts.map(artifact => artifact.id)+ })),progress: {...stage.stageRun.progress,krillinMode: 'cli',@@ -139,6 +144,20 @@ export function createKrillinExecutor(input: {};}+function filterObsoleteSourceVideo(input: CreatorExecutorInput): CreatorExecutorInput {+ const sourceUrl = stringValue(input.job.state.sourceUrl);+ if (input.job.templateId !== 'video-translation' || input.job.state.sourceType === 'file' || !sourceUrl) {+ return input;+ }+ const inputArtifacts = input.inputArtifacts.filter(artifact => {+ if (artifact.kind !== 'source_video') return true;+ const snapshot = artifact.metadata?.settingsSnapshot;+ return snapshot !== null && typeof snapshot === 'object' && !Array.isArray(snapshot)+ && snapshot.sourceType !== 'file' && stringValue(snapshot.sourceUrl) === sourceUrl;+ });+ return inputArtifacts.length === input.inputArtifacts.length ? input : { ...input, inputArtifacts };+}+function requiresTranscriptionDependency(stageId: string,options: Record<string, unknown>@@ -149,7 +168,8 @@ function requiresTranscriptionDependency(async function ensureKrillinTranscriptionDependency(loader: KrillinDependencyLoader,config: CreatorServicesConfig,- stage: CreatorExecutorInput+ stage: CreatorExecutorInput,+ platformFallback = false): Promise<void> {if (stage.job.templateId === 'video-translation' && (stage.stageRun.stageId === 'subtitle'@@ -160,7 +180,11 @@ async function ensureKrillinTranscriptionDependency(config,signal: stage.signal,reportProgress(progress) {- stage.reportProgress({ ...stage.stageRun.progress, ...progress });+ stage.reportProgress({ ...stage.stageRun.progress, ...progress,+ ...(platformFallback && typeof progress.message === 'string'+ ? { message: `未找到可用的原始字幕,已切换到本地语音转录。${progress.message}` }+ : {})+ });}});}@@ -204,6 +228,8 @@ export function buildKrillinStageOptions(input: CreatorExecutorInput): Record<stcaptionSource: state.preferPlatformCaptions === false ? 'whisper' : 'any',sourceOnly: input.job.templateId === 'stickman-video'&& input.stageRun.stageId === 'source-transcript',+ prepareVideo: input.job.templateId !== 'video-translation',+ audioOnly: input.job.templateId === 'video-translation' && state.composeVideo !== true,bilingual: input.stageRun.stageId === 'subtitles' || state.bilingual === true,bilingualTop: state.subtitlePosition === 'top',ttsProvider: typeof state.ttsProvider === 'string' ? state.ttsProvider : undefined,@@ -436,7 +462,9 @@ export async function validateResultArtifacts(input: {} else {metadata = { ...metadata, ...(await validateMediaFile(path, input.ffprobe)) };}- outputs.push({ kind: outputKind, status: 'completed', path, metadata });+ outputs.push({ kind: outputKind, status: 'completed', path, metadata,+ ...(outputKind === 'source_video' && input.stage.stageRun.stageId !== 'subtitle' ? { sourceArtifactIds: [] } : {})+ });}for (const required of contract.requiredOutputKinds) {if (required === 'source_subtitle' && input.stage.inputArtifacts.some(artifact => artifact.kind === 'target_subtitle')) continue;@@ -533,9 +561,9 @@ function expectedOutputKinds(stageId: string): Set<string> {'bilingual_subtitle','vertical_subtitle']);- if (stageId === 'tts') return new Set(['dubbed_audio', 'dubbed_video']);- if (stageId === 'render-horizontal') return new Set(['horizontal_video']);- if (stageId === 'render-vertical') return new Set(['vertical_video']);+ if (stageId === 'tts') return new Set(['source_video', 'dubbed_audio', 'dubbed_video']);+ if (stageId === 'render-horizontal') return new Set(['source_video', 'horizontal_video']);+ if (stageId === 'render-vertical') return new Set(['source_video', 'vertical_video']);return new Set();}@@ -544,7 +572,7 @@ function requiredOutputKinds(stageId: string, templateId?: string): string[] {return ['target_subtitle'];}if (stageId === 'subtitle') {- return ['source_video', 'source_subtitle', 'target_subtitle', 'vertical_subtitle'];+ return [...(templateId === 'video-translation' ? [] : ['source_video']), 'source_subtitle', 'target_subtitle', 'vertical_subtitle'];}if (stageId === 'tts') return ['dubbed_audio'];if (stageId === 'render-horizontal') return ['horizontal_video'];diff --git a/apps/daemon/src/creator/krillin/cli-runner.ts b/apps/daemon/src/creator/krillin/cli-runner.tsindex abdead24..9625b6aa 100644--- a/apps/daemon/src/creator/krillin/cli-runner.ts+++ b/apps/daemon/src/creator/krillin/cli-runner.ts@@ -225,6 +225,8 @@ export function buildKrillinCliCommandArguments(): string[] {const command = krillinCliStageId(stage.stageRun.stageId);const common = ['--workdir', stage.workdir, '--task-id', stage.stageRun.id];+ const sourceUrl = stringOption(options, 'sourceUrl');+ const mediaCache = sourceUrl ? ['--media-workdir', join(dirname(stage.workdir), 'source-media', createHash('sha256').update(sourceUrl).digest('hex').slice(0, 16))] : [];if (command === 'subtitle') {const source = sourceOverride ?? resolveKrillinCliSource(artifacts, options);if (!source) throw new CreatorExecutorError('creator_stage_input_missing', 'Subtitle input video or URL is required');@@ -235,7 +237,7 @@ export function buildKrillinCliCommandArguments(return ['subtitle',source,- ...(sourceOnly ? [] : ['--prepare-video']),+ ...(!sourceOnly && booleanOption(options, 'prepareVideo', true) ? ['--prepare-video'] : []),'--origin-lang', requiredOption(options, 'originLanguage'),'--target-lang', requiredOption(options, 'targetLanguage'),'--caption-source', stringOption(options, 'captionSource') ?? 'any',@@ -263,6 +265,9 @@ export function buildKrillinCliCommandArguments(...common,'--input-srt', subtitle,'--line-mode', lineMode,+ ...mediaCache,+ ...(booleanOption(options, 'audioOnly', false) ? ['--audio-only'] : []),+ ...(!video && !booleanOption(options, 'audioOnly', false) ? optionalArgument('--source-url', stringOption(options, 'sourceUrl')) : []),...(video ? ['--video', video] : []),...(voice ? ['--voice', voice] : [])];@@ -270,14 +275,15 @@ export function buildKrillinCliCommandArguments(if (command === 'render-horizontal' || command === 'render-vertical') {const sourceVideo = artifactPath(artifacts, 'source_video');const dubbedVideo = artifactPath(artifacts, 'dubbed_video');+ const dubbedAudio = artifactPath(artifacts, 'dubbed_audio');const dubbed = booleanOption(options, 'dubbed', false);- if (dubbed && !dubbedVideo) {+ if (dubbed && !dubbedVideo && !dubbedAudio) {throw new CreatorExecutorError('krillin_dubbed_video_missing','Generate the dubbed video before rendering a dubbed output');}- const video = dubbed ? dubbedVideo : sourceVideo;+ const video = dubbed ? dubbedVideo ?? sourceVideo : sourceVideo;const vertical = artifactPath(artifacts, 'vertical_subtitle');const target = artifactPath(artifacts, 'target_subtitle');const bilingual = artifactPath(artifacts, 'bilingual_subtitle');@@ -286,14 +292,16 @@ export function buildKrillinCliCommandArguments(: booleanOption(options, 'bilingual', false) && bilingual? bilingual: target ?? bilingual;- if (!video || !subtitle) {+ if ((!video && !sourceUrl) || !subtitle) {throw new CreatorExecutorError('creator_stage_input_missing', 'Render video and subtitle inputs are required');}return [command,...common,- '--video', video,+ ...mediaCache,+ ...(video ? ['--video', video] : ['--source-url', sourceUrl!]),'--subtitle', subtitle,+ ...(dubbed && !dubbedVideo && dubbedAudio ? ['--audio', dubbedAudio] : []),...(dubbed ? ['--dubbed'] : []),...(command === 'render-vertical'? [@@ -394,6 +402,7 @@ async function collectArtifacts(const result: KrillinResultArtifact[] = [];const jobRoot = await realpath(resolve(input.jobsRoot, input.stage.job.id));for (const [field, kind] of mappings) {+ if (kind === 'source_video' && input.stage.stageRun.stageId !== 'subtitle' && input.artifacts.some(artifact => artifact.kind === 'source_video')) continue;const reported = response.outputs?.[field];if (!reported) continue;const candidate = isAbsolute(reported) ? reported : resolve(input.stage.workdir, reported);@@ -434,9 +443,9 @@ export function outputMappings(stageId: string): Array<[string, string]> {['short_origin_mixed_srt', 'vertical_subtitle']];}- if (stageId === 'tts') return [['tts_audio', 'dubbed_audio'], ['video_with_tts', 'dubbed_video']];- if (stageId === 'render-horizontal') return [['horizontal_video', 'horizontal_video']];- if (stageId === 'render-vertical') return [['vertical_video', 'vertical_video']];+ if (stageId === 'tts') return [['origin_video', 'source_video'], ['tts_audio', 'dubbed_audio'], ['video_with_tts', 'dubbed_video']];+ if (stageId === 'render-horizontal') return [['origin_video', 'source_video'], ['horizontal_video', 'horizontal_video']];+ if (stageId === 'render-vertical') return [['origin_video', 'source_video'], ['vertical_video', 'vertical_video']];return [];}diff --git a/apps/daemon/src/creator/krillin/dependency-loader.ts b/apps/daemon/src/creator/krillin/dependency-loader.tsindex ca7fedb0..0d3a3746 100644--- a/apps/daemon/src/creator/krillin/dependency-loader.ts+++ b/apps/daemon/src/creator/krillin/dependency-loader.ts@@ -14,6 +14,7 @@ import { dirname, join, resolve } from 'node:path';import { pipeline } from 'node:stream/promises';import type {CreatorJson,+ CreatorLocalComponent,CreatorServicesCapabilitiesResponse,CreatorServicesConfig} from '@opencreator/protocol';@@ -21,6 +22,10 @@ import { CreatorExecutorError } from '../executor.js';import { spawnCreatorProcess } from '../process-tree.js';import { createKrillinCreatorServicesCapabilities } from './capabilities.js';import type { ZipFile } from 'yauzl';+import { manageLocalComponents } from './local-components.js';++type DownloadProgress = { state: 'downloading' | 'verifying' | 'extracting'; item: string; downloadedBytes?: number; totalBytes?: number | null; bytesPerSecond?: number; remainingSeconds?: number | null; statusMessage?: string };+type DownloadReporter = { onProgress?(progress: DownloadProgress): void };const whisperKitRelease = {executable: {@@ -99,6 +104,7 @@ type WhisperKitInstaller = {proxy: string;signal: AbortSignal;onPhase(phase: 'cli' | 'model'): void;+ onProgress?(progress: DownloadProgress): void;}): Promise<void>;};@@ -110,6 +116,7 @@ type WhisperCppInstaller = {model: WhisperCppModel;signal: AbortSignal;onPhase(phase: 'cli' | 'model'): void;+ onProgress?(progress: DownloadProgress): void;}): Promise<void>;};@@ -129,18 +136,16 @@ export function createKrillinDependencyLoader(input: {arch?: string;whisperKitInstaller?: WhisperKitInstaller;whisperCppInstaller?: WhisperCppInstaller;-}): KrillinDependencyLoader {+}) {const root = resolve(input.root);const platform = input.platform ?? process.platform;const arch = input.arch ?? process.arch;const whisperKitInstaller = input.whisperKitInstaller ?? defaultWhisperKitInstaller;const whisperCppInstaller = input.whisperCppInstaller ?? defaultWhisperCppInstaller;- let whisperKitReady = false;let whisperKitPending: Promise<void> | undefined;- const whisperCppReady = new Set<WhisperCppModel>();const whisperCppPending = new Map<WhisperCppModel, Promise<void>>();- return {+ const loader: KrillinDependencyLoader = {root,capabilities() {return createKrillinCreatorServicesCapabilities(platform, arch);@@ -155,14 +160,15 @@ export function createKrillinDependencyLoader(input: {);}const model = config.transcription.whisperCpp.model;- if (whisperCppReady.has(model) || await whisperCppInstaller.isInstalled(root, model)) {- whisperCppReady.add(model);+ if (await whisperCppInstaller.isInstalled(root, model)) {return;}if (signal.aborted) {throw new CreatorExecutorError('creator_stage_canceled', 'Creator stage was canceled');}reportProgress({+ phase: 'downloading_dependencies',+ message: '当前本地转录组件尚未安装,正在自动下载。模型文件较大,可能需要较长时间;下载并校验完成后将自动继续,无需重新开始。',krillinMode: 'cli',providerStatus: 'preparing',dependency: 'whispercpp',@@ -176,8 +182,11 @@ export function createKrillinDependencyLoader(input: {proxy: config.proxy.trim(),model,signal,+ onProgress: progress => reportProgress(componentDownloadProgress('whispercpp', model, progress)),onPhase(phase) {reportProgress({+ phase: 'downloading_dependencies',+ message: `正在准备 Whisper.cpp ${phase === 'cli' ? '转录引擎' : model + ' 模型'};下载并校验完成后将自动继续`,krillinMode: 'cli',providerStatus: 'preparing',dependency: 'whispercpp',@@ -189,7 +198,6 @@ export function createKrillinDependencyLoader(input: {if (!await whisperCppInstaller.isInstalled(root, model)) {throw new Error('Whisper.cpp dependency verification failed after installation');}- whisperCppReady.add(model);}).finally(() => {whisperCppPending.delete(model);});@@ -215,14 +223,15 @@ export function createKrillinDependencyLoader(input: {`WhisperKit is unavailable on ${platform}/${arch}`);}- if (whisperKitReady || await whisperKitInstaller.isInstalled(root)) {- whisperKitReady = true;+ if (await whisperKitInstaller.isInstalled(root)) {return;}if (signal.aborted) {throw new CreatorExecutorError('creator_stage_canceled', 'Creator stage was canceled');}reportProgress({+ phase: 'downloading_dependencies',+ message: '当前本地转录组件尚未安装,正在自动下载。模型文件较大,可能需要较长时间;下载并校验完成后将自动继续,无需重新开始。',krillinMode: 'cli',providerStatus: 'preparing',dependency: 'whisperkit',@@ -233,8 +242,11 @@ export function createKrillinDependencyLoader(input: {root,proxy: config.proxy.trim(),signal,+ onProgress: progress => reportProgress(componentDownloadProgress('whisperkit', 'large-v2', progress)),onPhase(phase) {reportProgress({+ phase: 'downloading_dependencies',+ message: `正在准备 WhisperKit ${phase === 'cli' ? '转录引擎' : 'large-v2 模型'};下载并校验完成后将自动继续`,krillinMode: 'cli',providerStatus: 'preparing',dependency: 'whisperkit',@@ -246,7 +258,6 @@ export function createKrillinDependencyLoader(input: {if (!await whisperKitInstaller.isInstalled(root)) {throw new Error('WhisperKit dependency verification failed after installation');}- whisperKitReady = true;}).finally(() => {whisperKitPending = undefined;});@@ -263,6 +274,43 @@ export function createKrillinDependencyLoader(input: {}}};+ return manageLocalComponents({ loader, async inspect() {+ const capabilities = loader.capabilities();+ const components = await Promise.all(capabilities.transcription.providers.filter(candidate => candidate.kind === 'local').map(async candidate => {+ const provider = candidate.provider as CreatorLocalComponent['id'];+ const executableInstalled = !candidate.available ? false : provider === 'whisperkit'+ ? await isWhisperKitExecutableInstalled(join(root, 'bin', 'whisperkit-cli'))+ : await isWhisperCppExecutableInstalled(join(root, 'bin', 'whispercpp'));+ const models = await Promise.all(candidate.models.map(async model => ({ id: model,+ installed: candidate.available && (provider === 'whisperkit'+ ? input.whisperKitInstaller ? await whisperKitInstaller.isInstalled(root) : await isWhisperKitModelInstalled(join(root, 'models', 'whisperkit', 'openai_whisper-large-v2'), join(root, 'models', 'whisperkit', '.opencreator-large-v2.json'))+ : input.whisperCppInstaller ? await whisperCppInstaller.isInstalled(root, model as WhisperCppModel) : await isWhisperCppModelInstalled(join(root, 'models', 'whispercpp'), model as WhisperCppModel)),+ bytes: candidate.modelDetails?.[model]?.diskBytes ?? null+ })));+ const version = provider === 'whisperkit' ? whisperKitRelease.executable.version : provider === 'whisper.cpp' ? whisperCppRelease.executable.version : null;+ const installedInfo = await stat(provider === 'whisperkit' ? join(root, 'bin', 'whisperkit-cli') : join(root, 'bin', 'whispercpp', '.opencreator-runtime.json')).catch(() => undefined);+ const component: CreatorLocalComponent = {+ id: provider, name: provider === 'whisperkit' ? 'WhisperKit' : provider === 'whisper.cpp' ? 'whisper.cpp' : 'Faster Whisper',+ available: candidate.available, version: executableInstalled || ((input.whisperKitInstaller || input.whisperCppInstaller) && models.some(model => model.installed)) ? version : null,+ supportedVersion: version, installedAt: installedInfo?.mtime.toISOString() ?? null, path: root,+ source: provider === 'whisperkit' ? 'Homebrew / ModelScope · SHA-256 verified' : 'GitHub / Hugging Face · SHA-256 verified',+ models, state: !candidate.available ? 'unsupported' : models.some(model => model.installed) ? 'ready' : executableInstalled ? 'partial' : 'not_installed',+ model: null, item: null, downloadedBytes: 0, totalBytes: null, percent: null, bytesPerSecond: null, remainingSeconds: null, error: null+ };+ return component;+ }));+ return { platform, arch, components };+ } });+}++function componentDownloadProgress(dependency: string, model: string, progress: DownloadProgress): Record<string, CreatorJson> {+ const percent = progress.totalBytes && progress.downloadedBytes !== undefined ? Math.min(100, progress.downloadedBytes / progress.totalBytes * 100) : null;+ return {+ phase: `${progress.state}_dependencies`, percent: 2, dependency, dependencyModel: model, dependencyItem: progress.item,+ dependencyPercent: percent, downloadedBytes: progress.downloadedBytes ?? 0, totalBytes: progress.totalBytes ?? null,+ bytesPerSecond: progress.bytesPerSecond ?? null, remainingSeconds: progress.remainingSeconds ?? null,+ message: `正在${progress.state === 'verifying' ? '校验' : progress.state === 'extracting' ? '解压安装' : '下载'} ${progress.item}。${progress.statusMessage ?? ''}模型较大,准备完成后会自动继续转录,无需重新开始。`+ };}const defaultWhisperKitInstaller: WhisperKitInstaller = {@@ -281,6 +329,7 @@ async function installWhisperCpp(input: {model: WhisperCppModel;signal: AbortSignal;onPhase(phase: 'cli' | 'model'): void;+ onProgress?(progress: DownloadProgress): void;}): Promise<void> {await mkdir(input.root, { recursive: true, mode: 0o700 });const staging = join(input.root, `.whispercpp-${randomUUID()}`);@@ -296,7 +345,7 @@ async function installWhisperCpp(input: {}async function installWhisperCppExecutable(- input: { root: string; proxy: string; signal: AbortSignal },+ input: { root: string; proxy: string; signal: AbortSignal } & DownloadReporter,staging: string): Promise<void> {const executableRoot = join(input.root, 'bin', 'whispercpp');@@ -308,9 +357,11 @@ async function installWhisperCppExecutable(sha256: whisperCppRelease.executable.archiveSha256,path: join(staging, 'whisper-bin-x64.zip'),proxy: input.proxy,- signal: input.signal+ signal: input.signal,+ onProgress: input.onProgress});const extracted = join(staging, 'cli');+ input.onProgress?.({ state: 'extracting', item: 'Whisper.cpp CLI' });await extractWhisperCppExecutableArchive(archive, extracted, input.signal);await verifyWhisperCppExecutableFiles(extracted);const version = await runCommand(join(extracted, 'whisper-cli.exe'), ['--version'], {@@ -337,7 +388,7 @@ async function installWhisperCppModel(proxy: string;model: WhisperCppModel;signal: AbortSignal;- },+ } & DownloadReporter,staging: string): Promise<void> {const modelRoot = join(input.root, 'models', 'whispercpp');@@ -351,7 +402,8 @@ async function installWhisperCppModel(sha256: release.sha256,path: stagedModel,proxy: input.proxy,- signal: input.signal+ signal: input.signal,+ onProgress: input.onProgress});const source = downloaded === stagedModel? stagedModel@@ -505,6 +557,7 @@ async function installWhisperKit(input: {proxy: string;signal: AbortSignal;onPhase(phase: 'cli' | 'model'): void;+ onProgress?(progress: DownloadProgress): void;}): Promise<void> {await mkdir(input.root, { recursive: true, mode: 0o700 });await chmod(input.root, 0o700);@@ -521,7 +574,7 @@ async function installWhisperKit(input: {}async function installWhisperKitExecutable(- input: { root: string; proxy: string; signal: AbortSignal },+ input: { root: string; proxy: string; signal: AbortSignal } & DownloadReporter,staging: string): Promise<void> {const executable = join(input.root, 'bin', 'whisperkit-cli');@@ -535,9 +588,11 @@ async function installWhisperKitExecutable(path: join(staging, 'whisperkit-cli.tar.gz'),proxy: input.proxy,ghcrScope: whisperKitRelease.executable.ghcrScope,- signal: input.signal+ signal: input.signal,+ onProgress: input.onProgress});const extracted = join(staging, 'cli');+ input.onProgress?.({ state: 'extracting', item: 'WhisperKit CLI' });await mkdir(extracted, { recursive: true });await runCommand('tar', ['-xzf', archive, '-C', extracted], {cwd: staging,@@ -568,7 +623,7 @@ async function installWhisperKitExecutable(}async function installWhisperKitModel(- input: { root: string; proxy: string; signal: AbortSignal },+ input: { root: string; proxy: string; signal: AbortSignal } & DownloadReporter,staging: string): Promise<void> {const modelRoot = join(input.root, 'models', 'whisperkit');@@ -583,9 +638,11 @@ async function installWhisperKitModel(sha256: whisperKitRelease.model.archiveSha256,path: join(staging, 'whisperkit-model.zip'),proxy: input.proxy,- signal: input.signal+ signal: input.signal,+ onProgress: input.onProgress});const extracted = join(staging, 'model');+ input.onProgress?.({ state: 'extracting', item: 'WhisperKit large-v2 model' });await mkdir(extracted, { recursive: true });await runCommand('ditto', ['-x', '-k', archive, extracted], {cwd: staging,@@ -657,7 +714,7 @@ async function verifyWhisperKitModel(root: string): Promise<void> {}}-async function configuredOrDownloadedFile(input: {+export async function configuredOrDownloadedFile(input: {configured: string | undefined;name: string;url: string;@@ -666,12 +723,13 @@ async function configuredOrDownloadedFile(input: {proxy: string;ghcrScope?: string;signal: AbortSignal;-}): Promise<string> {+} & DownloadReporter): Promise<string> {if (input.configured !== undefined && input.configured.trim() !== '') {const configured = resolve(input.configured);if (!(await stat(configured)).isFile()) {throw new Error(`${input.name} archive is unavailable`);}+ input.onProgress?.({ state: 'verifying', item: input.name });await verifyHash(input.name, configured, input.sha256, input.signal);return configured;}@@ -680,7 +738,8 @@ async function configuredOrDownloadedFile(input: {'--location','--retry', '3','--connect-timeout', '20',- '--output', input.path+ '--output', input.path,+ '--dump-header', `${input.path}.headers`];if (input.proxy) args.push('--proxy', input.proxy);if (input.ghcrScope !== undefined) {@@ -701,10 +760,41 @@ async function configuredOrDownloadedFile(input: {args.push('--header', `Authorization: Bearer ${token}`);}args.push(input.url);- await runCommand('curl', args, {- cwd: dirname(input.path),- signal: input.signal- });+ let lastBytes = 0;+ let lastTime = Date.now();+ let lastDataAt = lastTime;+ let retrying = false;+ let polling = false;+ let transferring = true;+ const report = async () => {+ if (polling) return;+ polling = true;+ try {+ const downloadedBytes = await stat(input.path).then(info => info.size, () => 0);+ const headers = await readFile(`${input.path}.headers`, 'utf8').catch(() => '');+ if (!transferring) return;+ const responseHeaders = headers.trim().split(/\r?\n\r?\n/).at(-1) ?? '';+ const length = /(?:^|\n)content-length:\s*(\d+)/i.exec(responseHeaders)?.[1];+ const totalBytes = length === undefined ? null : Number(length);+ const now = Date.now();+ if (downloadedBytes > lastBytes) { lastDataAt = now; retrying = false; }+ const bytesPerSecond = Math.max(0, downloadedBytes - lastBytes) * 1000 / Math.max(1, now - lastTime);+ lastBytes = downloadedBytes;+ lastTime = now;+ input.onProgress?.({ state: 'downloading', item: input.name, downloadedBytes, totalBytes, bytesPerSecond,+ statusMessage: retrying ? '下载连接暂时失败,正在自动重试。' : now - lastDataAt > 10_000 ? '下载暂无新数据,正在等待服务器响应;任务仍在运行。' : downloadedBytes === 0 ? '正在连接下载服务器,尚未开始传输。' : undefined,+ remainingSeconds: totalBytes !== null && bytesPerSecond > 0 ? Math.max(0, totalBytes - downloadedBytes) / bytesPerSecond : null });+ } finally { polling = false; }+ };+ input.onProgress?.({ state: 'downloading', item: input.name, downloadedBytes: 0 });+ const timer = setInterval(() => { void report().catch(() => {}); }, 500);+ try {+ await runCommand('curl', args, { cwd: dirname(input.path), signal: input.signal, onStderr(text) { if (/retry/i.test(text)) retrying = true; } });+ } finally {+ transferring = false;+ clearInterval(timer);+ }+ input.onProgress?.({ state: 'verifying', item: input.name });await verifyHash(input.name, input.path, input.sha256, input.signal);return input.path;}@@ -737,6 +827,7 @@ function runCommand(cwd: string;signal: AbortSignal;captureStdout?: boolean;+ onStderr?(text: string): void;}): Promise<{ stdout: string; stderr: string }> {return new Promise((resolvePromise, reject) => {@@ -751,6 +842,7 @@ function runCommand(if (input.captureStdout) stdout = boundedAppend(stdout, String(chunk));});child.stderr?.on('data', chunk => {+ input.onStderr?.(String(chunk));stderr = boundedAppend(stderr, String(chunk));});child.once('error', reject);diff --git a/apps/daemon/src/creator/krillin/local-components.ts b/apps/daemon/src/creator/krillin/local-components.tsnew file mode 100644index 00000000..e5612c5b--- /dev/null+++ b/apps/daemon/src/creator/krillin/local-components.ts@@ -0,0 +1,125 @@+import type { CreatorJson, CreatorLocalComponent, CreatorRuntimeComponentsResponse, CreatorServicesConfig } from '@opencreator/protocol';+import type { KrillinDependencyLoader } from './dependency-loader.js';++type Progress = Record<string, CreatorJson>;+type Operation = { controller: AbortController; promise: Promise<void>; listeners: Set<(progress: Progress) => void>; progress: Progress };++export function manageLocalComponents(input: {+ loader: KrillinDependencyLoader;+ inspect(): Promise<Omit<CreatorRuntimeComponentsResponse, 'selectedProvider' | 'selectedModel'>>;+}) {+ const operations = new Map<string, Operation>();+ const states = new Map<string, Partial<CreatorLocalComponent>>();+ let inspection: Awaited<ReturnType<typeof input.inspect>> | undefined;+ let inspectionPending: ReturnType<typeof input.inspect> | undefined;+ let inspectedAt = 0;++ async function status(config: CreatorServicesConfig): Promise<CreatorRuntimeComponentsResponse> {+ if (inspection === undefined || Date.now() - inspectedAt > 10_000) {+ inspectionPending ??= input.inspect().finally(() => { inspectionPending = undefined; });+ inspection = await inspectionPending;+ inspectedAt = Date.now();+ }+ const selectedModel = localModel(config);+ return {+ ...inspection,+ selectedProvider: config.transcription.provider,+ selectedModel,+ components: inspection.components.map(component => {+ const model = component.id === config.transcription.provider ? selectedModel : component.models[0]?.id ?? null;+ const state = states.get(`${component.id}:${model}`);+ const active = [...operations.keys()].find(key => key.startsWith(`${component.id}:`));+ const activeState = active === undefined ? undefined : states.get(active);+ const installed = component.models.find(candidate => candidate.id === model)?.installed === true;+ const actualState = !component.available ? 'unsupported' : installed && component.version ? 'ready' : installed || component.version ? 'partial' : 'not_installed';+ return { ...component, model, state: actualState, ...state,+ ...(state?.state === 'ready' ? { state: actualState } : {}), ...activeState,+ ...(!component.available ? { state: 'unsupported' as const } : {})+ };+ })+ };+ }++ async function ensure(request: Parameters<KrillinDependencyLoader['ensure']>[0]): Promise<void> {+ const provider = request.config.transcription.provider;+ const model = localModel(request.config);+ if (model === null) return input.loader.ensure(request);+ if (request.signal.aborted) throw Object.assign(new Error('Creator stage was canceled'), { code: 'creator_stage_canceled' });+ const key = `${provider}:${model}`;+ let operation = operations.get(key);+ if (operation === undefined) {+ const preceding = [...operations.entries()].filter(([operationKey]) => operationKey.startsWith(`${provider}:`)).map(([, operation]) => operation.promise.catch(() => {}));+ const listeners = new Set<(progress: Progress) => void>();+ const controller = new AbortController();+ operation = { controller, listeners, promise: Promise.resolve(), progress: {} };+ operations.set(key, operation);+ const current = operation;+ const publish = (progress: Progress) => {+ const phase = typeof progress.phase === 'string' ? progress.phase : 'downloading_dependencies';+ states.set(key, {+ state: phase === 'verifying_dependencies' ? 'verifying' : phase === 'extracting_dependencies' ? 'extracting' : 'downloading',+ model, error: null, message: typeof progress.message === 'string' ? progress.message : undefined,+ item: typeof progress.dependencyItem === 'string' ? progress.dependencyItem : null,+ downloadedBytes: numberValue(progress.downloadedBytes) ?? 0, totalBytes: numberValue(progress.totalBytes),+ percent: numberValue(progress.dependencyPercent), bytesPerSecond: numberValue(progress.bytesPerSecond), remainingSeconds: numberValue(progress.remainingSeconds)+ });+ current.progress = progress;+ for (const listener of listeners) listener(progress);+ };+ publish({ phase: preceding.length > 0 ? 'downloading_dependencies' : 'verifying_dependencies', percent: 2, dependency: provider, dependencyModel: model,+ message: preceding.length > 0 ? '正在等待本地转录引擎的共享组件准备完成,随后会自动准备当前模型并继续转录,无需重新开始。' : '正在检查本地转录引擎和模型是否已安装;缺失时会自动下载,准备完成后会自动继续转录。'+ });+ current.promise = Promise.all(preceding).then(() => input.loader.ensure({+ config: structuredClone(request.config),+ signal: controller.signal,+ reportProgress: publish+ })).then(() => {+ states.set(key, { state: 'ready', model, error: null, percent: 100 });+ inspectedAt = 0;+ const progress: Progress = { phase: 'dependencies_ready', percent: 5, message: '本地转录组件已准备完成,正在开始语音转录', dependency: provider, dependencyModel: model };+ for (const listener of listeners) listener(progress);+ }).catch(error => {+ states.set(key, { ...states.get(key), state: 'failed', error: error instanceof Error ? error.message : String(error) });+ throw error;+ }).finally(() => { operations.delete(key); });+ }+ operation.listeners.add(request.reportProgress);+ if (Object.keys(operation.progress).length > 0) request.reportProgress(operation.progress);+ try {+ await new Promise<void>((resolve, reject) => {+ const canceled = () => reject(Object.assign(new Error('Creator stage was canceled'), { code: 'creator_stage_canceled' }));+ request.signal.addEventListener('abort', canceled, { once: true });+ operation!.promise.then(resolve, reject).finally(() => request.signal.removeEventListener('abort', canceled));+ });+ } finally {+ operation.listeners.delete(request.reportProgress);+ }+ }++ return {+ ...input.loader,+ ensure,+ status,+ async download(config: CreatorServicesConfig): Promise<CreatorRuntimeComponentsResponse> {+ if (localModel(config) === null) throw new Error('A local transcription provider must be selected');+ const current = await status(config);+ if (!current.components.find(component => component.id === config.transcription.provider)?.available) {+ throw new Error('The selected local transcription component is unavailable on this platform');+ }+ void ensure({ config, signal: new AbortController().signal, reportProgress() {} }).catch(() => {});+ return status(config);+ },+ close() { for (const operation of operations.values()) operation.controller.abort(); }+ };+}++function localModel(config: CreatorServicesConfig): string | null {+ if (config.transcription.provider === 'whisperkit') return config.transcription.whisperKit.model;+ if (config.transcription.provider === 'whisper.cpp') return config.transcription.whisperCpp.model;+ if (config.transcription.provider === 'faster-whisper') return config.transcription.fasterWhisper.model;+ return null;+}++function numberValue(value: CreatorJson | undefined): number | null {+ return typeof value === 'number' && Number.isFinite(value) ? value : null;+}diff --git a/apps/daemon/src/creator/preflight.ts b/apps/daemon/src/creator/preflight.tsindex 175d5fcd..0c7ffb85 100644--- a/apps/daemon/src/creator/preflight.ts+++ b/apps/daemon/src/creator/preflight.ts@@ -1,4 +1,5 @@import type {+ CodexImageStatus,CreatorJob,CreatorPreflightCheck,CreatorPreflightExecutionMode,@@ -20,6 +21,8 @@ import {import { creatorResultSnapshotForVersion } from './result-snapshots.js';import { resolveCreatorStageInputs } from './stage-runner.js';import { readStickmanRemotionRuntime } from './stickman/remotion-runtime.js';+import { validateBilibiliSource } from './templates/video-translation-actions.js';+import type { VideoMetadataService } from '../video-metadata/service.js';export type CreatorPreflight = ReturnType<typeof createCreatorPreflight>;@@ -63,6 +66,8 @@ export function createCreatorPreflight(input: {ensureRuntimeReady?(): Promise<void>;executorIds?: Iterable<string>;validateRuntimeAssets?: boolean;+ readCodexImageStatus?(): Promise<CodexImageStatus>;+ videoMetadataService?: VideoMetadataService;}) {const executorIds = new Set(input.executorIds ?? []);@@ -94,7 +99,7 @@ export function createCreatorPreflight(input: {}const config = await input.configStore.read();- checkProviderConfig(job, stage, config, input.readCapabilities(), add);+ await checkProviderConfig(job, stage, config, input.readCapabilities(), add, input.readCodexImageStatus);const inputSnapshot = options.inputResultVersion === undefined? undefined: creatorResultSnapshotForVersion(job, options.inputResultVersion);@@ -107,6 +112,17 @@ export function createCreatorPreflight(input: {}, { label: '打开诊断', deepLink: '#/settings?tab=diagnostics' });}const inputState = inputSnapshot?.state ?? job.state;+ if (job.templateId === 'video-translation' && stage.id === 'subtitle') {+ try {+ await validateBilibiliSource({ ...job, state: inputState }, input.videoMetadataService);+ } catch (error) {+ add('blocked', {+ id: 'bilibili-part', title: '请确认 B 站分集',+ message: error instanceof Error ? error.message : '无法确认 B 站分集,请重新选择视频来源。',+ executionMode: 'remote'+ }, { label: '选择视频分集', deepLink: `#/workbench?tool=video-translation&jobId=${encodeURIComponent(job.id)}` });+ }+ }if (inputState.sourceType === 'file' && stage.executor === 'krillinai' && typeof inputState.sourceArtifactId !== 'string') {add('blocked', {id: 'input-file',@@ -263,13 +279,14 @@ function runtimeBackedExecutor(executorId: string): boolean {].includes(executorId);}-function checkProviderConfig(+async function checkProviderConfig(job: CreatorJob,stage: CreatorTemplateStage,config: CreatorServicesConfig,capabilities: CreatorServicesCapabilitiesResponse,- add: (status: 'ready' | 'warning' | 'blocked', item: Omit<CreatorPreflightCheck, 'executionMode'> & { executionMode?: CreatorPreflightExecutionMode }, repair?: CreatorPreflightCheck['repair']) => void-): void {+ add: (status: 'ready' | 'warning' | 'blocked', item: Omit<CreatorPreflightCheck, 'executionMode'> & { executionMode?: CreatorPreflightExecutionMode }, repair?: CreatorPreflightCheck['repair']) => void,+ readCodexImageStatus?: () => Promise<CodexImageStatus>+): Promise<void> {const needs = new Set<string>();if (stage.executor === 'krillinai') {if (@@ -293,6 +310,7 @@ function checkProviderConfig(if (stage.executor === 'smart-dubbing') needs.add('tts');if (stage.executor === 'stickman-audio' && stage.id === 'narration') needs.add('tts');if (stage.executor === 'stickman-image') needs.add('image');+ if (stage.executor === 'wechat-article' && stage.id === 'images') needs.add('image');if (needs.has('llm')) {if (config.llm.source === 'codex' && stage.executor === 'krillinai') {@@ -320,13 +338,19 @@ function checkProviderConfig(maxCandidateCount: job.templateId === 'image-generation' ? 4 : 8});const { provider } = settings;- if (imageProviderConfigured(config, provider)) {+ if (provider === 'codex-native') {+ let status: CodexImageStatus | undefined;+ try { status = await readCodexImageStatus?.(); } catch { status = undefined; }+ add(status?.ready ? 'ready' : 'blocked', {+ id: 'image-provider', title: '本机 Codex 生图',+ message: status?.message ?? '无法检查 Codex 生图认证和 Runtime 能力,请检查 Agent 配置。',+ executionMode: status?.executionMode === 'api' ? 'remote' : 'local'+ }, { label: '检查生图设置', deepLink: '#/settings?tab=ai-services§ion=image' });+ } else if (imageProviderConfigured(config, provider)) {add('ready', {id: 'image-provider',title: '图像服务',- message: provider === 'codex-native'- ? '本机 Codex 生图已启用。'- : `${provider} / ${settings.model} 已配置。`,+ message: `${provider} / ${settings.model} 已配置。`,executionMode: settings.executionMode});} else {diff --git a/apps/daemon/src/creator/stickman/image-executor.ts b/apps/daemon/src/creator/stickman/image-executor.tsindex d4910363..041cdf59 100644--- a/apps/daemon/src/creator/stickman/image-executor.ts+++ b/apps/daemon/src/creator/stickman/image-executor.ts@@ -331,6 +331,9 @@ export function createStickmanImageExecutor(input: {try {result = await generate(candidateRequest, config, {signal: stage.signal,+ onProgress: progress => stage.reportProgress({+ ...progress, completed: shotIndex, failed: candidateAttempt - 1, total: shotCount+ }),referenceImages: referenceImages.map(reference => ({content: reference.content,mime: reference.mimediff --git a/apps/daemon/src/creator/templates/video-translation-actions.ts b/apps/daemon/src/creator/templates/video-translation-actions.tsindex 75102868..c87737e8 100644--- a/apps/daemon/src/creator/templates/video-translation-actions.ts+++ b/apps/daemon/src/creator/templates/video-translation-actions.ts@@ -1,11 +1,13 @@-import type {- CreatorJob,- CreatorStageRun,- CreatorTtsProvider+import {+ parseBilibiliVideoSource,+ type CreatorJob,+ type CreatorStageRun,+ type CreatorTtsProvider} from '@opencreator/protocol';import type { CreatorServicesConfigStore } from '../../creator-services/config-store.js';import type { CreatorCommandDispatcher } from '../command-dispatcher.js';import type { CreatorService } from '../service.js';+import { VideoMetadataError, type VideoMetadataService } from '../../video-metadata/service.js';export type VideoTranslationWorkflow = ReturnType<typeof createVideoTranslationWorkflow>;@@ -13,11 +15,13 @@ export function createVideoTranslationWorkflow(input: {creator: CreatorService;dispatcher: Pick<CreatorCommandDispatcher, 'dispatch'>;configStore: Pick<CreatorServicesConfigStore, 'read'>;+ videoMetadataService?: VideoMetadataService;}) {async function validateStage(job: CreatorJob, stageId: string): Promise<void> {if (job.templateId !== 'video-translation') return;if (stageId === 'subtitle') {validateSource(job);+ await validateBilibiliSource(job, input.videoMetadataService);if (job.artifacts.some(artifact => artifact.id === job.state.importedTargetSubtitleId && artifact.kind === 'target_subtitle' && artifact.status === 'completed')) return;const config = await input.configStore.read();if (@@ -120,6 +124,38 @@ export class VideoTranslationWorkflowError extends Error {}}+export async function validateBilibiliSource(job: CreatorJob, service?: VideoMetadataService): Promise<void> {+ if (job.state.sourceType === 'file' || typeof job.state.sourceUrl !== 'string') return;+ let host: string;+ try {+ host = new URL(job.state.sourceUrl).hostname.toLowerCase();+ } catch {+ return;+ }+ if (host !== 'bilibili.com' && !host.endsWith('.bilibili.com')) return;+ const source = parseBilibiliVideoSource(job.state.sourceUrl);+ if (source === null) {+ throw new VideoTranslationWorkflowError('creator_source_invalid_part', 'B 站链接无效,请检查视频地址和分 P 参数(p 必须为正整数)');+ }+ try {+ if (service === undefined) throw new Error('Video metadata service unavailable');+ const metadata = await service.get(source.url);+ if ((metadata.parts?.length ?? 0) > 1 && source.partIndex === undefined) {+ throw new VideoTranslationWorkflowError('creator_source_part_required', `该视频包含 ${metadata.parts!.length} 个分 P,请先在视频来源中选择要翻译的分集`);+ }+ if (source.partIndex !== undefined && metadata.parts !== undefined+ && !metadata.parts.some(part => part.index === source.partIndex)) {+ throw new VideoTranslationWorkflowError('creator_source_invalid_part', '指定的分 P 不存在,请重新选择视频分集');+ }+ } catch (error) {+ if (error instanceof VideoTranslationWorkflowError) throw error;+ if (error instanceof VideoMetadataError && error.code === 'INVALID_PART') {+ throw new VideoTranslationWorkflowError('creator_source_invalid_part', error.message);+ }+ throw new VideoTranslationWorkflowError('creator_source_metadata_unavailable', '无法读取 B 站分集信息,请重试;未启动下载或翻译');+ }+}+function validateSource(job: CreatorJob): void {if (job.state.sourceType === 'file') {if (!job.artifacts.some(artifact => artifact.kind === 'source_video' && artifact.status === 'completed')) {@@ -135,8 +171,12 @@ function validateSource(job: CreatorJob): void {function isSupportedVideoUrl(value: string): boolean {try {- const host = new URL(value).hostname.toLowerCase();- return host === 'youtu.be' || host.endsWith('youtube.com') || host === 'b23.tv' || host.endsWith('bilibili.com');+ const source = new URL(value);+ const host = source.hostname.toLowerCase();+ const protocol = source.protocol;+ return (protocol === 'http:' || protocol === 'https:')+ && (host === 'youtu.be' || host === 'youtube.com' || host.endsWith('.youtube.com')+ || host === 'b23.tv' || host === 'bilibili.com' || host.endsWith('.bilibili.com'));} catch {return false;}diff --git a/apps/daemon/src/creator/templates/video-translation.ts b/apps/daemon/src/creator/templates/video-translation.tsindex 93c55bd4..ccf01946 100644--- a/apps/daemon/src/creator/templates/video-translation.ts+++ b/apps/daemon/src/creator/templates/video-translation.ts@@ -85,6 +85,7 @@ function createVideoTranslationTemplateDefinition(version: 1 | 2): CreatorTemplaoptional: true}],outputArtifacts: [+ { kind: 'source_video', status: 'completed' },{ kind: 'dubbed_audio', status: 'completed' },{ kind: 'dubbed_video', status: 'completed' }]@@ -96,13 +97,13 @@ function createVideoTranslationTemplateDefinition(version: 1 | 2): CreatorTemplaoptional: true,allowedJobStatuses: ['draft', 'running', 'needs_input', 'failed'],inputArtifacts: [- { kind: 'source_video', selector: 'latest-completed' },+ { kind: 'source_video', selector: 'latest-completed', optional: true },{ kind: 'target_subtitle', selector: 'latest-completed' },{ kind: 'bilingual_subtitle', selector: 'latest-completed', optional: true },{ kind: 'dubbed_audio', selector: 'latest-completed', optional: true },{ kind: 'dubbed_video', selector: 'latest-completed', optional: true }],- outputArtifacts: [{ kind: 'horizontal_video', status: 'completed' }]+ outputArtifacts: [{ kind: 'source_video', status: 'completed' }, { kind: 'horizontal_video', status: 'completed' }]},{id: 'render-vertical',@@ -111,14 +112,14 @@ function createVideoTranslationTemplateDefinition(version: 1 | 2): CreatorTemplaoptional: true,allowedJobStatuses: ['draft', 'running', 'needs_input', 'failed'],inputArtifacts: [- { kind: 'source_video', selector: 'latest-completed' },+ { kind: 'source_video', selector: 'latest-completed', optional: true },{ kind: 'vertical_subtitle', selector: 'latest-completed', optional: true },{ kind: 'target_subtitle', selector: 'latest-completed' },{ kind: 'bilingual_subtitle', selector: 'latest-completed', optional: true },{ kind: 'dubbed_audio', selector: 'latest-completed', optional: true },{ kind: 'dubbed_video', selector: 'latest-completed', optional: true }],- outputArtifacts: [{ kind: 'vertical_video', status: 'completed' }]+ outputArtifacts: [{ kind: 'source_video', status: 'completed' }, { kind: 'vertical_video', status: 'completed' }]}],actions: [diff --git a/apps/daemon/src/image-generation/codex-native.ts b/apps/daemon/src/image-generation/codex-native.tsnew file mode 100644index 00000000..a7935175--- /dev/null+++ b/apps/daemon/src/image-generation/codex-native.ts@@ -0,0 +1,179 @@+import type { CreateImageGenerationRequest } from '@opencreator/protocol';+import { mkdtemp, mkdir, readFile, readdir, realpath, rm, stat, writeFile } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { isAbsolute, join, relative, sep } from 'node:path';+import { buildCodexExecArgs } from '../codex/argv.js';+import { CodexExecError, startCodexExec, type CodexExecProcess } from '../codex/runner.js';+import type { CodexNativeImageRuntime, GeneratedImageContent } from './provider.js';+import { isRecord } from '../creator-services/upstream-fetch.js';++const MAX_IMAGE_BYTES = 30 * 1024 * 1024;+const TIMEOUT_MS = 180_000;+export type NativeImageProgress = { phase: string; message: string };++export async function generateCodexNativeImage(input: {+ runtime: CodexNativeImageRuntime;+ request: CreateImageGenerationRequest;+ referenceImages: GeneratedImageContent[];+ signal: AbortSignal;+ onProgress?(progress: NativeImageProgress): void;+}): Promise<GeneratedImageContent> {+ input.signal.throwIfAborted();+ if (!input.runtime.codexBin) throw new Error('当前 Runtime 缺少 Codex 可执行程序');+ const workdir = await mkdtemp(join(tmpdir(), 'opencreator-codex-image-'));+ const outputDir = join(workdir, 'output');+ let process: CodexExecProcess | undefined;+ const abort = () => process?.cancel();+ let threadId: string | undefined;+ let savedPath: string | undefined;+ let failure: string | undefined;+ let streamError: string | undefined;+ let turnFailed = false;+ const report = (phase: string, message: string) => {+ if (!input.signal.aborted) input.onProgress?.({ phase, message });+ };+ try {+ await mkdir(outputDir, { mode: 0o700 });+ const imagePaths: string[] = [];+ for (const [index, image] of input.referenceImages.entries()) {+ if (!image.content.length || image.content.length > MAX_IMAGE_BYTES) throw new Error('参考图片超过大小限制或内容为空');+ const path = join(workdir, `reference-${index}.${image.mime === 'image/jpeg' ? 'jpg' : image.mime === 'image/webp' ? 'webp' : 'png'}`);+ await writeFile(path, image.content, { mode: 0o600 });+ imagePaths.push(path);+ }+ input.signal.throwIfAborted();+ report('preparing_native_image', '正在准备 ChatGPT 登录态生图;将使用 Codex 原生工具,不会调用图片 API Key 接口。');+ const startedAt = Date.now();+ process = (input.runtime.startExec ?? startCodexExec)({+ codexBin: input.runtime.codexBin,+ codexHome: input.runtime.codexHome,+ cwd: workdir,+ args: [...buildCodexExecArgs({ cwd: workdir, sandbox: 'workspace-write', imagePaths, imageGenerationOnly: true }), '--ephemeral'],+ prompt: [+ 'Generate exactly one image using the native image_generation tool.',+ 'Do not use a shell, scripts, an external image API, or API keys. Text alone is not a successful image result.',+ `Requested canvas: ${input.request.size}. Requested quality: ${input.request.quality}.`,+ imagePaths.length ? 'Use all attached images as references, preserving the requested subject and identity.' : '',+ `Save the generated image under ${outputDir} if the tool allows choosing its output location.`,+ 'Otherwise retain the native generated image artifact. Do not use an existing image as the output.',+ `User image brief:\n${input.request.prompt.trim()}`+ ].filter(Boolean).join('\n'),+ timeoutMs: TIMEOUT_MS,+ inactivityTimeoutMs: TIMEOUT_MS,+ env: { OPENAI_API_KEY: undefined, OPENAI_BASE_URL: undefined, CODEX_API_KEY: undefined, CODEX_ACCESS_TOKEN: undefined },+ onStdoutLine(line) {+ let event: Record<string, unknown>;+ try { event = JSON.parse(line); } catch { return; }+ if (!isRecord(event)) return;+ if (event.type === 'thread.started' && typeof event.thread_id === 'string') threadId = event.thread_id;+ if (event.type === 'turn.started') report('generating_image', '正在通过 ChatGPT 登录态生成图片,请稍候。');+ if (event.type === 'turn.failed' || event.type === 'error') {+ const error = isRecord(event.error) ? event.error : event;+ if (typeof error.message === 'string') streamError = nativeFailureMessage(error.message);+ if (event.type === 'turn.failed') turnFailed = true;+ }+ const item = isRecord(event.item) ? event.item : undefined;+ if (item && (item.type === 'image_generation' || item.type === 'imageGeneration')) {+ report('generating_image', 'Codex 原生生图工具正在处理图片,请稍候。');+ const path = item.saved_path ?? item.savedPath;+ if (typeof path === 'string') savedPath = path;+ if (item.status === 'failed' || item.failure) {+ const error = isRecord(item.failure) ? item.failure : item;+ failure = typeof error.message === 'string' ? nativeFailureMessage(error.message)+ : typeof error.type === 'string' ? nativeFailureMessage(error.type) : 'Codex 原生生图工具执行失败';+ } else if (item.status === 'completed') failure = undefined;+ }+ }+ });+ input.signal.addEventListener('abort', abort, { once: true });+ if (input.signal.aborted) abort();+ const result = await process.result.catch(error => {+ input.signal.throwIfAborted();+ if (error instanceof CodexExecError) {+ if (['timeout', 'inactivity_timeout', 'spawn_timeout'].includes(error.terminationReason)) {+ throw new Error('Codex 原生生图超时,请稍后重试');+ }+ throw new Error(nativeFailureMessage(error.message));+ }+ throw new Error('无法启动或执行 Codex 原生生图,请检查 Runtime');+ });+ input.signal.throwIfAborted();+ if (result.terminationReason !== 'completed' || result.exitCode !== 0 || turnFailed || failure) {+ throw new Error(failure ?? streamError ?? nativeFailureMessage(result.stderr));+ }+ report('collecting_outputs', '图片已生成,正在检查并保存本地产物。');+ const roots = [await realpath(workdir)];+ const references = new Set(await Promise.all(imagePaths.map(path => realpath(path))));+ if (threadId && /^[A-Za-z0-9_-]+$/.test(threadId)) {+ const home = await realpath(input.runtime.codexHome);+ const generated = await realpath(join(home, 'generated_images', threadId)).catch(() => undefined);+ if (generated === join(home, 'generated_images', threadId)) roots.push(generated);+ }+ const candidates = savedPath === undefined+ ? (await Promise.all(roots.map(root => imageFiles(root)))).flat()+ : [savedPath];+ const images: GeneratedImageContent[] = [];+ for (const candidate of candidates) {+ if (!isAbsolute(candidate) || candidate.includes('\0')) throw new Error('Codex 返回的图片路径无效');+ const path = await realpath(candidate);+ if (references.has(path)) continue;+ if (!roots.some(root => inside(root, path))) throw new Error('Codex 图片产物不属于当前生图任务,已拒绝读取');+ const info = await stat(path);+ if (!info.isFile() || info.size <= 0 || info.size > MAX_IMAGE_BYTES) throw new Error('Codex 图片产物大小无效');+ if (info.mtimeMs < startedAt - 1000) {+ if (savedPath !== undefined) throw new Error('Codex 返回了已有图片,未产生新的生图结果');+ continue;+ }+ const content = await readFile(path);+ if (content.length > MAX_IMAGE_BYTES) throw new Error('Codex 图片产物超过大小限制');+ const mime = imageMime(content);+ if (!mime) throw new Error('Codex 图片产物不是受支持的 PNG、JPEG 或 WebP 图片');+ images.push({ content, mime });+ }+ if (images.length !== 1) throw new Error(`Codex 没有返回唯一的新图片产物(${images.length} 张),不能将文字回复视为生图成功`);+ return images[0]!;+ } finally {+ input.signal.removeEventListener('abort', abort);+ await rm(workdir, { recursive: true, force: true });+ }+}++function nativeFailureMessage(message: string): string {+ if (/unauthorized|401|token.*expired|refresh.*token|not.*logged|authentication|login|sign.?in/i.test(message)) {+ return 'ChatGPT 登录已失效或无法验证,请在 Agent 设置中重新登录后重试';+ }+ if (/usage.?limit|rate.?limit|quota|429|limit.?exceeded/i.test(message)) {+ return 'ChatGPT 生图额度或请求频率受限,请稍后重试';+ }+ if (/unsupported|not.*support|not.*available|403|access.*denied/i.test(message)) {+ return '当前 ChatGPT 账号或 Codex Runtime 无法使用原生生图,请检查账号权限或更新 Runtime';+ }+ if (/policy|moderation|safety|content_filter/i.test(message)) {+ return '图片请求被安全策略拒绝,请调整描述后重试';+ }+ return 'Codex 原生生图失败,请检查登录状态、网络或 Runtime 后重试';+}++async function imageFiles(root: string, depth = 0): Promise<string[]> {+ const files: string[] = [];+ for (const entry of await readdir(root, { withFileTypes: true })) {+ if (entry.isSymbolicLink()) continue;+ const path = join(root, entry.name);+ if (entry.isDirectory() && depth < 3) files.push(...await imageFiles(path, depth + 1));+ else if (entry.isFile() && /\.(png|jpe?g|webp)$/i.test(entry.name)) files.push(path);+ if (files.length > 32) throw new Error('Codex 当前任务返回了过多图片产物');+ }+ return files;+}++function inside(root: string, path: string): boolean {+ const child = relative(root, path);+ return child === '' || (child !== '..' && !child.startsWith(`..${sep}`) && !isAbsolute(child));+}++function imageMime(content: Buffer): GeneratedImageContent['mime'] | undefined {+ if (content.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return 'image/png';+ if (content[0] === 0xff && content[1] === 0xd8 && content[2] === 0xff) return 'image/jpeg';+ if (content.subarray(0, 4).toString() === 'RIFF' && content.subarray(8, 12).toString() === 'WEBP') return 'image/webp';+ return undefined;+}diff --git a/apps/daemon/src/image-generation/codex-runtime.ts b/apps/daemon/src/image-generation/codex-runtime.tsnew file mode 100644index 00000000..3a887c79--- /dev/null+++ b/apps/daemon/src/image-generation/codex-runtime.ts@@ -0,0 +1,43 @@+import { execFile } from 'node:child_process';+import type { CodexImageStatus } from '@opencreator/protocol';+import { readLocalCodexImageConfiguration, type LocalCodexImageConfiguration } from '../codex/local-provider.js';+import type { CodexNativeImageRuntime } from './provider.js';++export async function readCodexImageConfiguration(runtime: CodexNativeImageRuntime): Promise<LocalCodexImageConfiguration> {+ if (runtime.readProvider) return { authentication: 'api_key', provider: await runtime.readProvider() };+ return readLocalCodexImageConfiguration({ codexHome: runtime.codexHome });+}++export async function inspectCodexImageRuntime(runtime: CodexNativeImageRuntime | undefined): Promise<CodexImageStatus> {+ let authentication: CodexImageStatus['authentication'] = 'none';+ try {+ if (!runtime) throw new Error('当前 Runtime 未配置 Codex 生图,请检查 Agent 设置');+ const configuration = await readCodexImageConfiguration(runtime);+ authentication = configuration.authentication;+ if (configuration.authentication === 'api_key') return {+ authentication, ready: true, executionMode: 'api', model: configuration.provider.model,+ message: '已检测到 Codex API 配置;将沿用当前接口生图,接口和模型需支持图片生成。'+ };+ if (!runtime.codexBin) throw new Error('当前 Runtime 缺少 Codex 可执行程序,无法使用 ChatGPT 登录态生图');+ const capability = runtime.checkNativeCapability+ ? await runtime.checkNativeCapability()+ : await probeNativeImageCapability(runtime.codexBin, runtime.codexHome);+ if (!capability.supported) throw new Error(`当前 Codex Runtime ${capability.version ?? ''} 不支持原生生图,请更新 Runtime`);+ return { authentication, ready: true, executionMode: 'native', version: capability.version,+ message: '已检测到本地 ChatGPT 登录凭据和原生生图工具;将通过 Codex 生成图片,无需额外配置图片 API Key。'+ };+ } catch (error) {+ return { authentication, ready: false, executionMode: null, message: error instanceof Error ? error.message : '无法检查 Codex 生图能力' };+ }+}++async function probeNativeImageCapability(codexBin: string, codexHome: string): Promise<{ supported: boolean; version?: string }> {+ const run = (args: string[]) => new Promise<string>((resolve, reject) => {+ execFile(codexBin, args, { timeout: 8_000, maxBuffer: 1024 * 1024, encoding: 'utf8', env: { ...process.env, CODEX_HOME: codexHome } }, (error, stdout) => {+ if (error) reject(new Error('无法启动 Codex Runtime 或读取原生生图能力,请检查可执行程序'));+ else resolve(stdout);+ });+ });+ const [features, version] = await Promise.all([run(['features', 'list']), run(['--version'])]);+ return { supported: /^image_generation\s+(?!removed\b)\S.*$/m.test(features), version: version.trim() };+}diff --git a/apps/daemon/src/image-generation/provider.ts b/apps/daemon/src/image-generation/provider.tsindex 406e54d2..efcb710a 100644--- a/apps/daemon/src/image-generation/provider.ts+++ b/apps/daemon/src/image-generation/provider.ts@@ -6,9 +6,11 @@ import type {import type { PublicErrorFacts } from '@opencreator/protocol';import {LocalCodexProviderError,- readLocalCodexProvider,type LocalCodexProvider} from '../codex/local-provider.js';+import type { startCodexExec } from '../codex/runner.js';+import { generateCodexNativeImage, type NativeImageProgress } from './codex-native.js';+import { inspectCodexImageRuntime, readCodexImageConfiguration } from './codex-runtime.js';import { createKlingAuthorization } from '../creator-services/kling-auth.js';import {appendEndpointPath,@@ -31,7 +33,10 @@ export type GeneratedImageContent = {export type CodexNativeImageRuntime = {codexHome: string;+ codexBin?: string;readProvider?: () => Promise<LocalCodexProvider>;+ checkNativeCapability?: () => Promise<{ supported: boolean; version?: string }>;+ startExec?: typeof startCodexExec;};export type ImageGenerationCapabilities = {@@ -69,18 +74,21 @@ export async function generateImageContents(referenceImage?: GeneratedImageContent;referenceImages?: GeneratedImageContent[];codexNative?: CodexNativeImageRuntime;+ onProgress?(progress: NativeImageProgress): void;} = {}): Promise<{ model: string; contents: GeneratedImageContent[] }> {const referenceImages = options.referenceImages?? (options.referenceImage === undefined ? [] : [options.referenceImage]);const capabilities = imageGenerationCapabilities(request.provider);const controller = new AbortController();- const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);+ const timeout = setTimeout(() => controller.abort(new DOMException('图片生成超时,请重试', 'TimeoutError')), REQUEST_TIMEOUT_MS);const abort = () => controller.abort(options.signal?.reason);timeout.unref();if (options.signal?.aborted) abort();else options.signal?.addEventListener('abort', abort, { once: true });+ let nativeExecution = false;try {+ controller.signal.throwIfAborted();if (referenceImages.length > capabilities.maxReferenceImages) {@@ -105,17 +113,29 @@ export async function generateImageContents();}try {- const provider = await (- options.codexNative.readProvider?.()- ?? readLocalCodexProvider({ codexHome: options.codexNative.codexHome })- );+ const configuration = await readCodexImageConfiguration(options.codexNative);+ if (configuration.authentication === 'chatgpt') {+ nativeExecution = true;+ options.onProgress?.({ phase: 'preparing_native_image', message: '正在检查 ChatGPT 登录态和 Codex 原生生图能力…' });+ const status = await inspectCodexImageRuntime(options.codexNative);+ if (!status.ready) throw new ImageGenerationProviderError('config_missing', status.message);+ if (status.executionMode !== 'native') throw new ImageGenerationProviderError('config_missing', 'Codex 认证模式已变化,请刷新状态后重试');+ const image = await generateCodexNativeImage({+ runtime: options.codexNative,+ request,+ referenceImages,+ signal: controller.signal,+ onProgress: options.onProgress+ });+ return { model: 'codex-native', contents: [image] };+ }return await generateOpenAiImages(request,config,controller.signal,referenceImages,options.fetchImpl,- provider+ configuration.provider);} catch (error) {if (error instanceof LocalCodexProviderError) {@@ -146,6 +166,12 @@ export async function generateImageContents(} catch (error) {if (error instanceof ImageGenerationProviderError) throw error;if (options.signal?.aborted) throw error;+ if (controller.signal.reason instanceof DOMException && controller.signal.reason.name === 'TimeoutError') {+ throw new ImageGenerationProviderError('upstream_error', '图片生成超时,请稍后重试');+ }+ if (nativeExecution && error instanceof Error) {+ throw new ImageGenerationProviderError('upstream_error', error.message);+ }throw new ImageGenerationProviderError('upstream_error','The image generation provider could not be reached',diff --git a/apps/daemon/src/video-metadata/service.ts b/apps/daemon/src/video-metadata/service.tsindex a3a31771..1ba30389 100644--- a/apps/daemon/src/video-metadata/service.ts+++ b/apps/daemon/src/video-metadata/service.ts@@ -1,4 +1,4 @@-import type { VideoMetadataResponse } from '@opencreator/protocol';+import { parseBilibiliVideoSource, type VideoMetadataResponse, type VideoSourcePart } from '@opencreator/protocol';import { request as httpsRequest } from 'node:https';import { HttpsProxyAgent } from 'https-proxy-agent';@@ -6,7 +6,7 @@ const METADATA_TIMEOUT_MS = 5_000;export class VideoMetadataError extends Error {constructor(- readonly code: 'UNSUPPORTED_SOURCE' | 'UPSTREAM_ERROR',+ readonly code: 'UNSUPPORTED_SOURCE' | 'INVALID_PART' | 'UPSTREAM_ERROR',message: string) {super(message);@@ -21,10 +21,15 @@ export function createVideoMetadataService(input: {fetchImpl?: typeof fetch;getProxy?(): string | Promise<string>;} = {}): VideoMetadataService {+ const cache = new Map<string, { payload: unknown; expiresAt: number }>();return {async get(value: string): Promise<VideoMetadataResponse> {const source = parseVideoSource(value);const endpoint = createMetadataEndpoint(source);+ const cached = cache.get(endpoint.href);+ if (source.platform === 'bilibili' && cached !== undefined && cached.expiresAt > Date.now()) {+ return parseMetadataPayload(source, cached.payload);+ }const controller = new AbortController();const timeout = setTimeout(() => controller.abort(), METADATA_TIMEOUT_MS);@@ -41,7 +46,15 @@ export function createVideoMetadataService(input: {throw new VideoMetadataError('UPSTREAM_ERROR', 'Video metadata request failed');}const payload = await response.json() as unknown;- return parseMetadataPayload(source, payload);+ const metadata = parseMetadataPayload(source, payload);+ if (source.platform === 'bilibili') {+ for (const [key, entry] of cache) {+ if (entry.expiresAt <= Date.now()) cache.delete(key);+ }+ if (cache.size >= 64) cache.delete(cache.keys().next().value!);+ cache.set(endpoint.href, { payload, expiresAt: Date.now() + 30_000 });+ }+ return metadata;} catch (error) {if (error instanceof VideoMetadataError) throw error;throw new VideoMetadataError('UPSTREAM_ERROR', 'Video metadata request failed');@@ -109,7 +122,7 @@ async function fetchMetadata(type ParsedVideoSource =| { platform: 'youtube'; videoId: string }- | { platform: 'bilibili'; videoId: string };+ | { platform: 'bilibili'; videoId: string; partIndex?: number };function parseVideoSource(value: string): ParsedVideoSource {let url: URL;@@ -134,10 +147,13 @@ function parseVideoSource(value: string): ParsedVideoSource {}if (!videoId || videoId.length > 128 || !/^[A-Za-z0-9_-]+$/.test(videoId)) {- const bilibiliId = (hostname === 'bilibili.com' || hostname.endsWith('.bilibili.com'))- ? url.pathname.match(/\/(BV[A-Za-z0-9]+|av\d+)(?:[/?#]|$)/i)?.[1]- : undefined;- if (bilibiliId) return { platform: 'bilibili', videoId: bilibiliId };+ if (hostname === 'bilibili.com' || hostname.endsWith('.bilibili.com')) {+ const bilibili = parseBilibiliVideoSource(value);+ if (bilibili === null) {+ throw new VideoMetadataError('UNSUPPORTED_SOURCE', 'B 站链接无效,请检查视频地址和分 P 参数(p 必须为正整数)');+ }+ return { platform: 'bilibili', videoId: bilibili.videoId, ...(bilibili.partIndex === undefined ? {} : { partIndex: bilibili.partIndex }) };+ }throw new VideoMetadataError('UNSUPPORTED_SOURCE', 'Only YouTube and Bilibili video metadata is supported');}return { platform: 'youtube', videoId };@@ -183,22 +199,54 @@ function parseMetadataPayload(source: ParsedVideoSource, payload: unknown): Videconst owner = isRecord(payload.data.owner) ? payload.data.owner : undefined;const authorName = boundedText(owner?.name, 160);const thumbnailUrl = boundedText(payload.data.pic, 2048);- const dimension = isRecord(payload.data.dimension) ? payload.data.dimension : undefined;- let width = boundedDimension(dimension?.width);- let height = boundedDimension(dimension?.height);- const rotation = boundedRotation(dimension?.rotate);- if (width !== undefined && height !== undefined && (rotation === 90 || rotation === 270)) {- [width, height] = [height, width];+ const parts = Array.isArray(payload.data.pages) ? payload.data.pages.map(parseBilibiliPart) : undefined;+ if (parts?.length === 0) {+ throw new VideoMetadataError('UPSTREAM_ERROR', '无法读取 B 站视频分集信息,请重试');}+ const partIndex = source.partIndex ?? (parts?.length === 1 ? parts[0]?.index : undefined);+ const selectedPart = parts?.find(part => part.index === partIndex);+ if (source.partIndex !== undefined && (parts === undefined ? source.partIndex !== 1 : selectedPart === undefined)) {+ throw new VideoMetadataError('INVALID_PART', `分 P ${source.partIndex} 不存在,请重新选择视频分集`);+ }+ const dimension = selectedPart ?? (parts !== undefined && parts.length > 1 ? undefined : payload.data.dimension);+ const dimensions = parseDimensions(dimension);return {platform: 'bilibili',title,...(authorName === undefined ? {} : { authorName }),...(thumbnailUrl === undefined ? {} : { thumbnailUrl }),- ...(width === undefined || height === undefined ? {} : { width, height })+ ...(parts === undefined ? {} : { parts }),+ ...(selectedPart === undefined ? {} : { selectedPart }),+ ...dimensions};}+function parseBilibiliPart(value: unknown): VideoSourcePart {+ if (!isRecord(value) || typeof value.page !== 'number' || !Number.isSafeInteger(value.page) || value.page < 1) {+ throw new VideoMetadataError('UPSTREAM_ERROR', '无法读取 B 站视频分集信息,请重试');+ }+ const cid = typeof value.cid === 'number' && Number.isSafeInteger(value.cid) && value.cid > 0 ? value.cid : undefined;+ const durationSeconds = typeof value.duration === 'number' && value.duration >= 0 ? value.duration : undefined;+ return {+ index: value.page,+ title: boundedText(value.part, 300) ?? `P${value.page}`,+ ...(cid === undefined ? {} : { cid }),+ ...(durationSeconds === undefined ? {} : { durationSeconds }),+ ...parseDimensions(value.dimension)+ };+}++function parseDimensions(value: unknown): { width?: number; height?: number } {+ const dimension = isRecord(value) ? value : undefined;+ let width = boundedDimension(dimension?.width);+ let height = boundedDimension(dimension?.height);+ const rotation = boundedRotation(dimension?.rotate);+ if (width !== undefined && height !== undefined && (rotation === 90 || rotation === 270)) {+ [width, height] = [height, width];+ }+ return width === undefined || height === undefined ? {} : { width, height };+}+function boundedText(value: unknown, maximum: number): string | undefined {return typeof value === 'string' && value.trim().length > 0? value.trim().slice(0, maximum)diff --git a/apps/daemon/test/fixtures/fake-codex-image.mjs b/apps/daemon/test/fixtures/fake-codex-image.mjsnew file mode 100644index 00000000..5ac27e08--- /dev/null+++ b/apps/daemon/test/fixtures/fake-codex-image.mjs@@ -0,0 +1,58 @@+import { mkdir, symlink, utimes, writeFile } from 'node:fs/promises';+import { join } from 'node:path';++const args = process.argv.slice(2);+const mode = process.env.TEST_CODEX_IMAGE_MODE ?? 'success';+const threadId = process.env.TEST_CODEX_IMAGE_THREAD ?? 'image-test-thread';+const emit = event => process.stdout.write(`${JSON.stringify(event)}\n`);+const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/l9sAAAAASUVORK5CYII=', 'base64');++if (args[0] === 'features') {+ process.stdout.write('image_generation stable true\n');+ process.exit(0);+}+if (args.includes('--version')) {+ process.stdout.write('codex-test 0.149.0\n');+ process.exit(0);+}+if (args[0] === 'login' && args[1] === 'status') {+ process.stderr.write('Logged in using ChatGPT\n');+ process.exit(0);+}++if (!args.includes('--ignore-user-config') || !args.includes('image_generation') || process.env.OPENAI_API_KEY || process.env.OPENAI_BASE_URL || process.env.CODEX_API_KEY || process.env.CODEX_ACCESS_TOKEN) {+ emit({ type: 'error', message: 'Authentication or tool isolation failed' });+ process.exitCode = 1;+} else {+ for await (const chunk of process.stdin) {+ if (!chunk.length) throw new Error('missing prompt');+ }+ emit({ type: 'thread.started', thread_id: threadId });+ emit({ type: 'turn.started' });+ if (mode === 'recovered') emit({ type: 'error', message: 'Reconnecting... 1/5' });+ if (process.env.TEST_CODEX_IMAGE_DELAY_MS) await new Promise(resolve => setTimeout(resolve, Number(process.env.TEST_CODEX_IMAGE_DELAY_MS)));+ if (mode === 'waiting') {+ setInterval(() => {}, 1000);+ } else if (mode === 'failure') {+ emit({ type: 'turn.failed', error: { message: '401 expired access_token=private-oauth-secret' } });+ process.exitCode = 1;+ } else if (mode === 'text-only') {+ emit({ type: 'item.completed', item: { type: 'agent_message', text: 'An image has been generated' } });+ emit({ type: 'turn.completed' });+ } else if (mode === 'quota') {+ emit({ type: 'item.completed', item: { type: 'image_generation', status: 'failed', failure: { type: 'usageLimitExceeded', limitId: 'test-limit', resetsAt: null } } });+ emit({ type: 'turn.completed' });+ } else {+ const directory = mode === 'home' || mode === 'other-thread'+ ? join(process.env.CODEX_HOME, 'generated_images', mode === 'other-thread' ? 'another-thread' : threadId)+ : mode === 'outside' || mode === 'symlink' ? process.env.TEST_CODEX_IMAGE_OUTSIDE : join(process.cwd(), 'output');+ await mkdir(directory, { recursive: true });+ const path = join(directory, 'image.png');+ await writeFile(path, mode === 'invalid' ? Buffer.from('not an image') : png);+ if (mode === 'old') await utimes(path, new Date(0), new Date(0));+ const outputPath = mode === 'symlink' ? join(process.cwd(), 'output', 'linked.png') : path;+ if (mode === 'symlink') await symlink(path, outputPath);+ emit({ type: 'item.completed', item: { type: 'image_generation', status: 'completed', ...(mode === 'home' || mode === 'other-thread' ? {} : { saved_path: outputPath }) } });+ emit({ type: 'turn.completed' });+ }+}diff --git a/apps/daemon/test/integration/creator-image-generation.test.ts b/apps/daemon/test/integration/creator-image-generation.test.tsindex 39aae320..4a7f54e0 100644--- a/apps/daemon/test/integration/creator-image-generation.test.ts+++ b/apps/daemon/test/integration/creator-image-generation.test.ts@@ -1,8 +1,9 @@import { createDefaultCreatorServicesConfig } from '@opencreator/protocol';import type { FastifyInstance } from 'fastify';-import { mkdtemp, rm } from 'node:fs/promises';+import { chmod, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';import { tmpdir } from 'node:os';import { join } from 'node:path';+import { fileURLToPath } from 'node:url';import { afterEach, describe, expect, it, vi } from 'vitest';import { buildServer } from '../../src/api/server.js';@@ -15,9 +16,48 @@ afterEach(async () => {if (tempDir) await rm(tempDir, { recursive: true, force: true });tempDir = '';vi.unstubAllGlobals();+ vi.unstubAllEnvs();});describe('creator image generation', () => {+ it('runs native ChatGPT generation with checked capability, visible progress and saved Creator artifacts', async () => {+ tempDir = await mkdtemp(join(tmpdir(), 'creator-native-image-api-'));+ const home = join(tempDir, 'codex-home');+ await mkdir(home);+ await writeFile(join(home, 'auth.json'), JSON.stringify({ auth_mode: 'chatgpt', tokens: { access_token: 'private-oauth-secret' } }));+ const fixture = fileURLToPath(new URL('../fixtures/fake-codex-image.mjs', import.meta.url));+ const bin = join(tempDir, 'fake-codex-image.mjs');+ await writeFile(bin, `#!/usr/bin/env node\nawait import(${JSON.stringify(fixture)});\n`);+ await chmod(bin, 0o755);+ const fetchImpl = vi.fn();+ vi.stubGlobal('fetch', fetchImpl);+ vi.stubEnv('TEST_CODEX_IMAGE_DELAY_MS', '250');+ const config = createDefaultCreatorServicesConfig();+ server = await buildServer({ token: 'secret', dataDir: tempDir, codexHome: home, codexBin: bin, creatorServicesConfigStore: {+ async read() { return structuredClone(config); }, async write(next) { return structuredClone(next); }, async reset() { return structuredClone(config); }+ } });+ const status = await request('GET', '/creator-services/image/codex/status');+ expect(status.json()).toMatchObject({ authentication: 'chatgpt', ready: true, executionMode: 'native', version: 'codex-test 0.149.0' });+ const created = await request('POST', '/creator/jobs', { projectId: 'project_image', templateId: 'image-generation', creationKey: 'native-generation-flow' });+ const initial = created.json().job;+ const updated = await request('POST', `/creator/jobs/${initial.id}/actions`, { action: 'update-settings', expectedRevision: initial.revision, input: { patch: { prompt: 'One orange cat', provider: 'codex-native', candidateCount: 1 } } });+ const started = await request('POST', `/creator/jobs/${initial.id}/actions`, { action: 'run-stage', expectedRevision: updated.json().job.revision, input: { stageId: 'generate' } });+ expect(started.statusCode).toBe(200);+ await vi.waitFor(async () => {+ const running = (await request('GET', `/creator/jobs/${initial.id}`)).json().job;+ expect(running.stages).toContainEqual(expect.objectContaining({ status: 'running', progress: expect.objectContaining({ phase: 'generating_image', message: expect.stringContaining('ChatGPT'), completed: 0, total: 1 }) }));+ });+ const completed = await waitForCompletedJob(initial.id);+ expect(completed.artifacts).toContainEqual(expect.objectContaining({ kind: 'generated_image', status: 'completed', metadata: expect.objectContaining({ provider: 'codex-native', model: 'codex-native' }) }));+ expect(completed.state.latestResultVersion).toBe(1);+ const artifact = completed.artifacts.find((item: { kind: string }) => item.kind === 'generated_image');+ const content = await request('GET', `/creator/jobs/${initial.id}/artifacts/${artifact.id}/content`);+ expect(content.statusCode).toBe(200);+ expect(content.rawPayload.subarray(0, 4).toString('hex')).toBe('89504e47');+ expect(fetchImpl).not.toHaveBeenCalled();+ expect(status.rawPayload.toString()).not.toContain('private-oauth-secret');+ });+it('runs image generation through Creator Job, Stage, Artifact and ResultSnapshot', async () => {tempDir = await mkdtemp(join(tmpdir(), 'creator-image-api-'));const config = createDefaultCreatorServicesConfig();diff --git a/apps/daemon/test/integration/creator-services-api.test.ts b/apps/daemon/test/integration/creator-services-api.test.tsindex 5815d709..20cde5ee 100644--- a/apps/daemon/test/integration/creator-services-api.test.ts+++ b/apps/daemon/test/integration/creator-services-api.test.ts@@ -57,6 +57,28 @@ describe('creator services API', () => {await server.close();});+ it('reports unavailable capability instead of assuming native image generation is enabled', async () => {+ const response = await server.inject({ method: 'GET', url: '/creator-services/image/codex/status' });+ expect(response.statusCode).toBe(200);+ expect(response.json()).toMatchObject({ ready: false, authentication: 'none', executionMode: null });+ expect(response.headers['cache-control']).toBe('no-store');+ expect(store.write).not.toHaveBeenCalled();+ });++ it('exposes the checked authentication mode without mutating configuration', async () => {+ await server.close();+ server = Fastify({ logger: false });+ const readStatus = vi.fn(async () => ({ authentication: 'chatgpt' as const, ready: true, executionMode: 'native' as const, version: 'codex-test', message: '原生工具已就绪' }));+ await registerCreatorServicesRoutes(server, store, undefined, ttsService, undefined, readStatus);+ const response = await server.inject({ method: 'GET', url: '/creator-services/image/codex/status' });+ expect(response.statusCode).toBe(200);+ expect(response.json()).toMatchObject({ authentication: 'chatgpt', ready: true, executionMode: 'native', version: 'codex-test' });+ expect(response.body).not.toContain('initial-secret');+ expect(readStatus).toHaveBeenCalledOnce();+ expect(store.write).not.toHaveBeenCalled();+ expect(store.reset).not.toHaveBeenCalled();+ });+it('supports legacy independent text-model fields during migration', async () => {const read = await server.inject({ method: 'GET', url: '/creator-services/config' });expect(read.statusCode).toBe(200);diff --git a/apps/daemon/test/integration/image-generation-api.test.ts b/apps/daemon/test/integration/image-generation-api.test.tsindex 23c428dc..d5099a6e 100644--- a/apps/daemon/test/integration/image-generation-api.test.ts+++ b/apps/daemon/test/integration/image-generation-api.test.ts@@ -3,11 +3,13 @@ import Fastify, { type FastifyInstance } from 'fastify';import { mkdir, mkdtemp, readdir, rm, writeFile } from 'node:fs/promises';import { tmpdir } from 'node:os';import { join } from 'node:path';+import { fileURLToPath } from 'node:url';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { registerImageGenerationRoutes } from '../../src/api/routes.image-generation.js';import type { CreatorServicesConfigStore } from '../../src/creator-services/config-store.js';import { generateImageContents } from '../../src/image-generation/provider.js';import { createImageGenerationService } from '../../src/image-generation/service.js';+import { startCodexExec } from '../../src/codex/runner.js';describe('image generation API', () => {let server: FastifyInstance;@@ -23,6 +25,31 @@ describe('image generation API', () => {await rm(dataDir, { recursive: true, force: true });});+ it('generates and serves native ChatGPT artifacts through the existing image API without HTTP fallback', async () => {+ const home = join(dataDir, 'codex');+ await mkdir(home);+ await writeFile(join(home, 'auth.json'), JSON.stringify({ auth_mode: 'chatgpt', tokens: { access_token: 'private-oauth-secret' } }));+ const fixture = fileURLToPath(new URL('../fixtures/fake-codex-image.mjs', import.meta.url));+ const fetchImpl = vi.fn();+ await registerImageGenerationRoutes(server, createImageGenerationService({+ dataDir, configStore: createConfigStore(createDefaultCreatorServicesConfig()), fetchImpl,+ codexNative: {+ codexHome: home, codexBin: process.execPath,+ checkNativeCapability: async () => ({ supported: true }),+ startExec: input => startCodexExec({ ...input, args: [fixture, ...input.args] })+ }+ }));+ const response = await server.inject({ method: 'POST', url: '/image-generation/results', payload: { prompt: 'An orange cat', provider: 'codex-native', size: '1024x1024', quality: 'medium', count: 1 } });+ expect(response.statusCode).toBe(201);+ expect(response.json().result).toMatchObject({ provider: 'codex-native', model: 'codex-native', count: 1 });+ const content = await server.inject({ method: 'GET', url: `/image-generation/results/${response.json().result.id}/content/0` });+ expect(content.statusCode).toBe(200);+ expect(content.headers['content-type']).toBe('image/png');+ expect(content.rawPayload.subarray(0, 4).toString('hex')).toBe('89504e47');+ expect(fetchImpl).not.toHaveBeenCalled();+ expect(response.body).not.toContain('private-oauth-secret');+ });+it('returns a safe upstream code and HTTP status without exposing provider text', async () => {const config = createDefaultCreatorServicesConfig();config.image.openai.apiKey = 'sk-image-test';diff --git a/apps/daemon/test/integration/video-metadata-api.test.ts b/apps/daemon/test/integration/video-metadata-api.test.tsindex 0d1903d0..87c9b12b 100644--- a/apps/daemon/test/integration/video-metadata-api.test.ts+++ b/apps/daemon/test/integration/video-metadata-api.test.ts@@ -1,4 +1,9 @@import Fastify, { type FastifyInstance } from 'fastify';+import { mkdtemp, rm } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { buildServer } from '../../src/api/server.js';+import { createDefaultCreatorServicesConfig } from '@opencreator/protocol';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { registerVideoMetadataRoutes } from '../../src/api/routes.video-metadata.js';import {@@ -114,6 +119,89 @@ describe('video metadata API', () => {expect(response.json()).toMatchObject({ width: 1080, height: 1920 });});+ it.each([undefined, 1, 2, 3])('returns Bilibili parts and the requested selection: %s', async partIndex => {+ const pages = [1, 2, 3].map(index => ({+ page: index, part: `Lesson ${index}`, cid: 1000 + index, duration: 60 * index,+ dimension: index === 3 ? { width: 1920, height: 1080, rotate: 90 } : { width: 1920, height: 1080, rotate: 0 }+ }));+ const fetchImpl = vi.fn(async () => new Response(JSON.stringify({ code: 0, data: { title: 'Course', pages } })));+ await registerVideoMetadataRoutes(server, createVideoMetadataService({ fetchImpl }));+ const url = `https://www.bilibili.com/video/BV18E421w7bf/${partIndex === undefined ? '' : `?p=${partIndex}`}`;+ const response = await server.inject({ method: 'GET', url: `/video-metadata?${new URLSearchParams({ url })}` });+ expect(response.statusCode).toBe(200);+ const metadata = response.json();+ expect(metadata.parts).toHaveLength(3);+ if (partIndex === undefined) {+ expect(metadata.selectedPart).toBeUndefined();+ expect(metadata.width).toBeUndefined();+ } else {+ expect(metadata.selectedPart).toMatchObject({ index: partIndex, title: `Lesson ${partIndex}`, cid: 1000 + partIndex });+ expect(metadata.width).toBe(partIndex === 3 ? 1080 : 1920);+ }+ });++ it('rejects an out-of-range Bilibili part instead of falling back to P1', async () => {+ const fetchImpl = vi.fn(async () => new Response(JSON.stringify({ code: 0, data: {+ title: 'Course', pages: [{ page: 1, part: 'Lesson 1' }]+ } })));+ await registerVideoMetadataRoutes(server, createVideoMetadataService({ fetchImpl }));+ const url = 'https://www.bilibili.com/video/BV18E421w7bf?p=3';+ const response = await server.inject({ method: 'GET', url: `/video-metadata?${new URLSearchParams({ url })}` });+ expect(response.statusCode).toBe(400);+ expect(response.json().error.message).toContain('分 P 3 不存在');+ });++ it('registers production metadata and prevents a multipart task from bypassing selection', async () => {+ const dataDir = await mkdtemp(join(tmpdir(), 'video-metadata-server-'));+ const get = vi.fn(async () => ({ platform: 'bilibili' as const, title: 'Course',+ parts: [1, 2, 3].map(index => ({ index, title: `Lesson ${index}` })) }));+ const run = vi.fn(async () => ({ outputs: [] }));+ const config = createDefaultCreatorServicesConfig();+ config.llm.source = 'codex';+ await server.close();+ try {+ server = await buildServer({ token: 'secret', dataDir, codexHome: join(dataDir, 'codex-home'),+ creatorExecutors: [{ id: 'krillinai', run }], videoMetadataService: { get },+ creatorServicesConfigStore: { read: async () => config, write: async value => value, reset: async () => config } });+ const url = 'https://www.bilibili.com/video/BV18E421w7bf?p=3';+ const response = await server.inject({ method: 'GET', url: `/video-metadata?${new URLSearchParams({ url })}`,+ headers: { authorization: 'Bearer secret' } });+ expect(response.statusCode).toBe(200);+ expect(get).toHaveBeenCalledWith(url);+ expect(response.json()).toMatchObject({ platform: 'bilibili', title: 'Course' });+ const created = await server.inject({ method: 'POST', url: '/creator/jobs',+ headers: { authorization: 'Bearer secret' }, payload: {+ projectId: 'bilibili-parts', templateId: 'video-translation',+ state: { sourceType: 'url', sourceUrl: 'https://www.bilibili.com/video/BV18E421w7bf' }+ } });+ expect(created.statusCode).toBe(201);+ const job = created.json().job;+ const started = await server.inject({ method: 'POST', url: `/creator/jobs/${job.id}/actions`,+ headers: { authorization: 'Bearer secret' }, payload: {+ action: 'run-stage', expectedRevision: job.revision, input: { stageId: 'subtitle', workflow: true }+ } });+ expect(started.statusCode).toBe(400);+ expect(started.json().error.code).toBe('creator_source_part_required');+ expect(run).not.toHaveBeenCalled();+ const snapshot = await server.inject({ method: 'GET', url: `/creator/jobs/${job.id}`, headers: { authorization: 'Bearer secret' } });+ expect(snapshot.json().job.stages).toHaveLength(0);+ } finally {+ await server.close();+ await rm(dataDir, { recursive: true, force: true });+ }+ });++ it('reuses the collection metadata while validating different parts', async () => {+ const fetchImpl = vi.fn(async () => new Response(JSON.stringify({ code: 0, data: {+ title: 'Course', pages: [1, 2, 3].map(index => ({ page: index, part: `Lesson ${index}` }))+ } })));+ const service = createVideoMetadataService({ fetchImpl });+ await service.get('https://www.bilibili.com/video/BV18E421w7bf');+ expect((await service.get('https://www.bilibili.com/video/BV18E421w7bf?p=3')).selectedPart?.index).toBe(3);+ await expect(service.get('https://www.bilibili.com/video/BV18E421w7bf?p=4')).rejects.toMatchObject({ code: 'INVALID_PART' });+ expect(fetchImpl).toHaveBeenCalledTimes(1);+ });+it('maps unavailable upstream metadata to a recoverable API error', async () => {await registerVideoMetadataRoutes(server, {async get() {diff --git a/apps/daemon/test/unit/codex-argv.test.ts b/apps/daemon/test/unit/codex-argv.test.tsindex fa3e2fc8..6143b024 100644--- a/apps/daemon/test/unit/codex-argv.test.ts+++ b/apps/daemon/test/unit/codex-argv.test.ts@@ -6,6 +6,18 @@ import {} from '../../src/codex/argv.js';describe('codex argv', () => {+ it('isolates native image generation from user providers, shells and MCP', () => {+ const args = buildCodexExecArgs({ cwd: '/tmp/image-task', sandbox: 'workspace-write', imageGenerationOnly: true, imagePaths: ['/tmp/reference.png'] });+ expect(args).toContain('--ignore-user-config');+ expect(args.join(' ')).toContain('--enable image_generation');+ expect(args.join(' ')).not.toContain('--disable image_generation');+ for (const feature of ['shell_tool', 'plugins', 'apps', 'unified_exec', 'view_image']) {+ expect(args.join(' ')).toContain(`--disable ${feature}`);+ }+ expect(args).toContain('mcp_servers={}');+ expect(args).toContain('web_search="disabled"');+ expect(args.slice(-2)).toEqual(['--image', '/tmp/reference.png']);+ });it('builds exec args without prompt in argv', () => {const args = buildCodexExecArgs({profile: 'default',diff --git a/apps/daemon/test/unit/codex-local-provider.test.ts b/apps/daemon/test/unit/codex-local-provider.test.tsindex c601b625..13182968 100644--- a/apps/daemon/test/unit/codex-local-provider.test.ts+++ b/apps/daemon/test/unit/codex-local-provider.test.ts@@ -4,7 +4,8 @@ import { join } from 'node:path';import { afterEach, describe, expect, it } from 'vitest';import {LocalCodexProviderError,- readLocalCodexProvider+ readLocalCodexProvider,+ readLocalCodexImageConfiguration} from '../../src/codex/local-provider.js';describe('local Codex provider', () => {@@ -15,6 +16,40 @@ describe('local Codex provider', () => {root = '';});+ it('preserves API key image configuration', async () => {+ root = await codexHome(['openai_base_url = "https://api.example.test/v1"']);+ await writeFile(join(root, 'auth.json'), JSON.stringify({ auth_mode: 'apikey', OPENAI_API_KEY: 'api-secret' }));+ await expect(readLocalCodexImageConfiguration({ codexHome: root })).resolves.toMatchObject({+ authentication: 'api_key', provider: { apiKey: 'api-secret', model: 'gpt-image-1' }+ });+ });++ it('honors an explicitly selected API mode even if an old OAuth token remains', async () => {+ root = await codexHome(['openai_base_url = "https://api.example.test/v1"']);+ await writeFile(join(root, 'auth.json'), JSON.stringify({ auth_mode: 'apikey', OPENAI_API_KEY: 'api-secret', tokens: { access_token: 'old-oauth-secret' } }));+ await expect(readLocalCodexImageConfiguration({ codexHome: root })).resolves.toMatchObject({ authentication: 'api_key', provider: { apiKey: 'api-secret' } });+ });++ it('does not silently route corrupted authentication to a configured API provider', async () => {+ root = await codexHome(['openai_base_url = "https://api.example.test/v1"']);+ await writeFile(join(root, 'auth.json'), '{corrupt');+ await expect(readLocalCodexImageConfiguration({ codexHome: root })).rejects.toThrow('无法读取本机 Codex 登录凭据');+ });++ it.each(['chatgpt', undefined])('recognizes OAuth credentials without treating them as API keys (%s)', async authMode => {+ root = await codexHome([]);+ await writeFile(join(root, 'auth.json'), JSON.stringify({ auth_mode: authMode, tokens: { access_token: 'oauth-secret' }, OPENAI_API_KEY: 'old-secret' }));+ const configuration = await readLocalCodexImageConfiguration({ codexHome: root });+ expect(configuration).toEqual({ authentication: 'chatgpt' });+ expect(JSON.stringify(configuration)).not.toContain('secret');+ });++ it('does not fall back to an old key when the selected ChatGPT login is incomplete', async () => {+ root = await codexHome(['openai_base_url = "https://api.example.test/v1"']);+ await writeFile(join(root, 'auth.json'), JSON.stringify({ auth_mode: 'chatgpt', OPENAI_API_KEY: 'old-secret' }));+ await expect(readLocalCodexImageConfiguration({ codexHome: root })).rejects.toThrow('重新登录');+ });+it('reads the selected provider base URL and bearer token', async () => {root = await codexHome(['model = "gpt-5.6-sol"',diff --git a/apps/daemon/test/unit/codex-native-image.test.ts b/apps/daemon/test/unit/codex-native-image.test.tsnew file mode 100644index 00000000..91562a63--- /dev/null+++ b/apps/daemon/test/unit/codex-native-image.test.ts@@ -0,0 +1,160 @@+import type { CreateImageGenerationRequest } from '@opencreator/protocol';+import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { fileURLToPath } from 'node:url';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { CodexExecError, startCodexExec, type RunCodexExecInput } from '../../src/codex/runner.js';+import { generateCodexNativeImage } from '../../src/image-generation/codex-native.js';+import { generateImageContents } from '../../src/image-generation/provider.js';+import { inspectCodexImageRuntime } from '../../src/image-generation/codex-runtime.js';+import { createDefaultCreatorServicesConfig } from '@opencreator/protocol';++const fixture = fileURLToPath(new URL('../fixtures/fake-codex-image.mjs', import.meta.url));+const request: CreateImageGenerationRequest = { provider: 'codex-native', prompt: 'one orange cat', count: 1, size: '1024x1024', quality: 'medium' };+let home = '';++beforeEach(async () => {+ home = await mkdtemp(join(tmpdir(), 'codex-native-image-test-'));+ await writeFile(join(home, 'auth.json'), JSON.stringify({ auth_mode: 'chatgpt', tokens: { access_token: 'private-oauth-secret' }, OPENAI_API_KEY: 'old-api-secret' }));+});++afterEach(async () => {+ vi.useRealTimers();+ await rm(home, { recursive: true, force: true });+});++function runtime(mode = 'success', thread = 'image-test-thread') {+ return {+ codexHome: home, codexBin: process.execPath,+ checkNativeCapability: async () => ({ supported: true, version: 'codex-test' }),+ startExec: (input: RunCodexExecInput) => startCodexExec({ ...input, args: [fixture, ...input.args], env: { ...input.env, TEST_CODEX_IMAGE_MODE: mode, TEST_CODEX_IMAGE_THREAD: thread, TEST_CODEX_IMAGE_OUTSIDE: home } })+ };+}++describe('ChatGPT native image execution', () => {+ it('uses a real child process, returns a new artifact and reports standard progress', async () => {+ const onProgress = vi.fn();+ const image = await generateCodexNativeImage({ runtime: runtime(), request, referenceImages: [], signal: new AbortController().signal, onProgress });+ expect(image.mime).toBe('image/png');+ expect(image.content.subarray(0, 4).toString('hex')).toBe('89504e47');+ expect(onProgress.mock.calls.map(([progress]) => progress.phase)).toEqual(expect.arrayContaining(['preparing_native_image', 'generating_image', 'collecting_outputs']));+ expect(onProgress.mock.calls.every(([progress]) => typeof progress.message === 'string' && !('percent' in progress))).toBe(true);+ });++ it('accepts generation that recovers from a transient stream reconnect', async () => {+ await expect(generateCodexNativeImage({ runtime: runtime('recovered'), request, referenceImages: [], signal: new AbortController().signal })).resolves.toMatchObject({ mime: 'image/png' });+ });++ it('keeps reference images attached and does not mistake them for generated images', async () => {+ const base = runtime('text-only');+ const reference = Buffer.from('reference-image');+ let workspace = '';+ const startExec = vi.fn((input: RunCodexExecInput) => {+ workspace = input.cwd;+ expect(input.args).toContain('--image');+ expect(input.prompt).toContain('attached images');+ return { cancel() {}, result: readFile(input.args[input.args.indexOf('--image') + 1]!).then(content => {+ expect(content).toEqual(reference);+ return base.startExec(input).result;+ }) };+ });+ await expect(generateCodexNativeImage({ runtime: { ...base, startExec }, request, referenceImages: [{ content: reference, mime: 'image/png' }], signal: new AbortController().signal })).rejects.toThrow('唯一的新图片');+ await expect(readFile(join(workspace, 'reference-0.png'))).rejects.toMatchObject({ code: 'ENOENT' });+ });++ it('only discovers artifacts for the current thread and isolates concurrent requests', async () => {+ const [first, second] = await Promise.all(['thread-first', 'thread-second'].map(thread => generateCodexNativeImage({ runtime: runtime('home', thread), request, referenceImages: [], signal: new AbortController().signal })));+ expect(first!.content).toEqual(second!.content);+ await expect(generateCodexNativeImage({ runtime: runtime('other-thread'), request, referenceImages: [], signal: new AbortController().signal })).rejects.toThrow('唯一的新图片');+ });++ it('rejects a thread directory symlink pointing to another thread', async () => {+ const generated = join(home, 'generated_images');+ await mkdir(join(generated, 'different-thread'), { recursive: true });+ await symlink(join(generated, 'different-thread'), join(generated, 'image-test-thread'));+ await expect(generateCodexNativeImage({ runtime: runtime('home'), request, referenceImages: [], signal: new AbortController().signal })).rejects.toThrow('唯一的新图片');+ });++ it.each([+ ['text-only', '唯一的新图片'], ['old', '已有图片'], ['outside', '不属于当前生图任务'],+ ['symlink', '不属于当前生图任务'], ['invalid', '不是受支持'], ['failure', '重新登录'], ['quota', '额度']+ ])('rejects unsafe or unsuccessful results (%s)', async (mode, message) => {+ await expect(generateCodexNativeImage({ runtime: runtime(mode), request, referenceImages: [], signal: new AbortController().signal })).rejects.toThrow(message);+ });++ it('cancels a running child when the user aborts', async () => {+ const controller = new AbortController();+ let started!: () => void;+ const generating = new Promise<void>(resolve => { started = resolve; });+ const task = generateCodexNativeImage({ runtime: runtime('waiting'), request, referenceImages: [], signal: controller.signal, onProgress: progress => { if (progress.phase === 'generating_image') started(); } });+ const rejected = expect(task).rejects.toMatchObject({ name: 'AbortError' });+ await generating;+ controller.abort();+ await rejected;+ });++ it('reports execution timeouts explicitly instead of a user cancellation', async () => {+ const base = runtime();+ await expect(generateCodexNativeImage({+ runtime: { ...base, startExec: () => ({ cancel() {}, result: Promise.reject(new CodexExecError({ message: 'timeout token=private-oauth-secret', terminationReason: 'timeout', stdoutLines: [], stderr: '' })) }) },+ request, referenceImages: [], signal: new AbortController().signal+ })).rejects.toThrow('原生生图超时');+ });+});++describe('Codex authentication dispatch and readiness', () => {+ it('routes OAuth only through native execution without sending HTTP or secrets', async () => {+ const fetchImpl = vi.fn();+ const base = runtime();+ const startExec = vi.fn(base.startExec);+ const result = await generateImageContents(request, createDefaultCreatorServicesConfig(), { codexNative: { ...base, startExec }, fetchImpl });+ expect(result.model).toBe('codex-native');+ expect(fetchImpl).not.toHaveBeenCalled();+ expect(JSON.stringify(startExec.mock.calls)).not.toContain('private-oauth-secret');+ expect(JSON.stringify(startExec.mock.calls)).not.toContain('old-api-secret');+ });++ it('removes inherited API credentials from the native child environment', async () => {+ vi.stubEnv('OPENAI_API_KEY', 'inherited-key');+ vi.stubEnv('OPENAI_BASE_URL', 'https://wrong-provider.example.test/v1');+ vi.stubEnv('CODEX_API_KEY', 'inherited-codex-key');+ vi.stubEnv('CODEX_ACCESS_TOKEN', 'inherited-agent-token');+ try {+ await expect(generateCodexNativeImage({ runtime: runtime(), request, referenceImages: [], signal: new AbortController().signal })).resolves.toMatchObject({ mime: 'image/png' });+ } finally {+ vi.unstubAllEnvs();+ }+ });++ it('does not fall back to HTTP after a native failure', async () => {+ const fetchImpl = vi.fn();+ const task = generateImageContents(request, createDefaultCreatorServicesConfig(), { codexNative: runtime('failure'), fetchImpl });+ await expect(task).rejects.toMatchObject({ code: 'upstream_error', message: expect.stringContaining('重新登录') });+ expect(fetchImpl).not.toHaveBeenCalled();+ });++ it('blocks native execution when Runtime capability is missing', async () => {+ const startExec = vi.fn(runtime().startExec);+ const codexNative = { ...runtime(), startExec, checkNativeCapability: async () => ({ supported: false, version: 'old-runtime' }) };+ await expect(generateImageContents(request, createDefaultCreatorServicesConfig(), { codexNative })).rejects.toMatchObject({ code: 'config_missing', message: expect.stringContaining('不支持原生生图') });+ expect(startExec).not.toHaveBeenCalled();+ await expect(inspectCodexImageRuntime(codexNative)).resolves.toMatchObject({ authentication: 'chatgpt', ready: false, executionMode: null });+ });++ it('reports native capability without exposing OAuth credentials', async () => {+ const status = await inspectCodexImageRuntime(runtime());+ expect(status).toMatchObject({ authentication: 'chatgpt', ready: true, executionMode: 'native', version: 'codex-test' });+ expect(JSON.stringify(status)).not.toContain('secret');+ });++ it('does not require a CLI or probe for the existing API path', async () => {+ const checkNativeCapability = vi.fn();+ await expect(inspectCodexImageRuntime({ codexHome: home, readProvider: async () => ({ baseUrl: 'https://images.example.test/v1', apiKey: 'api-secret', model: 'gpt-image-1' }), checkNativeCapability })).resolves.toMatchObject({ authentication: 'api_key', ready: true, executionMode: 'api' });+ expect(checkNativeCapability).not.toHaveBeenCalled();+ });++ it('blocks ChatGPT mode without a Codex executable', async () => {+ await expect(inspectCodexImageRuntime({ codexHome: home })).resolves.toMatchObject({ authentication: 'chatgpt', ready: false, message: expect.stringContaining('缺少 Codex') });+ });+});diff --git a/apps/daemon/test/unit/creator-krillin-adapter-dependency.test.ts b/apps/daemon/test/unit/creator-krillin-adapter-dependency.test.tsindex 3985bd5c..47505e60 100644--- a/apps/daemon/test/unit/creator-krillin-adapter-dependency.test.ts+++ b/apps/daemon/test/unit/creator-krillin-adapter-dependency.test.ts@@ -1,4 +1,5 @@import { createDefaultCreatorServicesConfig } from '@opencreator/protocol';+import type { CreatorExecutorInput } from '../../src/creator/executor.js';import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join, relative } from 'node:path';@@ -11,6 +12,10 @@ vi.mock('../../src/creator/krillin/dependency-preflight.js', () => ({preflightKrillinDependencies}));+vi.mock('../../src/creator/validators/media.js', () => ({+ validateMediaFile: vi.fn(async () => ({ duration: 1, hasVideo: true, hasAudio: true }))+}));+vi.mock('../../src/creator/krillin/cli-runner.js', async importOriginal => {const actual = await importOriginal<typeof import('../../src/creator/krillin/cli-runner.js')>();return { ...actual, runKrillinCli };@@ -93,6 +98,78 @@ describe('KrillinAI configured transcription dependency', () => {message: expect.stringMatching(/No original YouTube captions.*whisperkit-cli rejected/)});});++ it.each(['platform_caption_access_failed', 'platform_caption_processing_failed'])('does not download transcription components after %s', async code => {+ const fixture = setup();+ runKrillinCli.mockRejectedValueOnce(new KrillinCliError(code, 'Platform request failed'));++ await expect(fixture.executor.run(fixture.stage)).rejects.toMatchObject({ code });++ expect(runKrillinCli).toHaveBeenCalledTimes(1);+ expect(fixture.ensure).not.toHaveBeenCalled();+ });++ it.each(['different-url', 'unknown'])('does not reuse a source video with %s provenance when rendering a URL', async provenance => {+ const fixture = setup();+ const stage = fixture.stage as CreatorExecutorInput;+ stage.job.templateId = 'video-translation';+ stage.stageRun.stageId = 'render-horizontal';+ const previousVideo = stage.inputArtifacts[0]!;+ previousVideo.metadata = provenance === 'different-url'+ ? { settingsSnapshot: { sourceUrl: 'https://www.youtube.com/watch?v=previous' } }+ : {};+ const subtitle = writeTargetSubtitle(tempDir);+ stage.inputArtifacts.push({ id: 'target-subtitle', kind: 'target_subtitle', path: join(tempDir, subtitle.relativePath) } as never);+ runKrillinCli.mockImplementation(async input => {+ const outputDir = join(input.jobsRoot, stage.job.id, 'outputs');+ mkdirSync(outputDir, { recursive: true });+ const sourcePath = join(outputDir, 'new-source.mp4');+ const renderPath = join(outputDir, 'horizontal.mp4');+ writeFileSync(sourcePath, 'new source');+ writeFileSync(renderPath, 'render');+ return [+ { kind: 'source_video', relativePath: relative(input.jobsRoot, sourcePath), size: 10 },+ { kind: 'horizontal_video', relativePath: relative(input.jobsRoot, renderPath), size: 6 }+ ];+ });++ const result = await fixture.executor.run(stage);++ expect(runKrillinCli.mock.calls[0]?.[0].artifacts).toEqual([+ expect.objectContaining({ id: 'target-subtitle', kind: 'target_subtitle' })+ ]);+ expect(runKrillinCli.mock.calls[0]?.[0].options.sourceUrl).toBe(stage.job.state.sourceUrl);+ expect(result.outputs).toEqual([+ expect.objectContaining({ kind: 'source_video', sourceArtifactIds: [] }),+ expect.objectContaining({ kind: 'horizontal_video', sourceArtifactIds: ['target-subtitle'] })+ ]);+ expect(fixture.ensure).not.toHaveBeenCalled();+ });++ it.each(['matching-url', 'local-file'])('retains a source video with %s provenance', async provenance => {+ const fixture = setup();+ const stage = fixture.stage as CreatorExecutorInput;+ stage.job.templateId = 'video-translation';+ stage.stageRun.stageId = 'tts';+ stage.job.state.sourceType = provenance === 'local-file' ? 'file' : 'url';+ stage.inputArtifacts[0]!.metadata = provenance === 'matching-url'+ ? { settingsSnapshot: { sourceUrl: stage.job.state.sourceUrl! } }+ : {};+ runKrillinCli.mockImplementation(async input => {+ const outputDir = join(input.jobsRoot, stage.job.id, 'outputs');+ mkdirSync(outputDir, { recursive: true });+ const outputPath = join(outputDir, 'dubbed.wav');+ writeFileSync(outputPath, 'audio');+ return [{ kind: 'dubbed_audio', relativePath: relative(input.jobsRoot, outputPath), size: 5 }];+ });++ await fixture.executor.run(stage);++ expect(runKrillinCli.mock.calls[0]?.[0].artifacts).toEqual([+ expect.objectContaining({ id: 'source-video', kind: 'source_video' })+ ]);+ expect(fixture.ensure).not.toHaveBeenCalled();+ });});function setup() {diff --git a/apps/daemon/test/unit/creator-krillin-cli-protocol.test.ts b/apps/daemon/test/unit/creator-krillin-cli-protocol.test.tsindex 8bd745f9..1900dc63 100644--- a/apps/daemon/test/unit/creator-krillin-cli-protocol.test.ts+++ b/apps/daemon/test/unit/creator-krillin-cli-protocol.test.ts@@ -13,6 +13,25 @@ import { createKrillinConfigToml } from '../../src/creator/krillin/config-bridgeimport { buildKrillinStageOptions } from '../../src/creator/krillin/adapter.js';describe('KrillinAI CLI protocol', () => {+ it.each([true, false])('never prepares video in the translation subtitle stage, even when composeVideo=%s', composeVideo => {+ const stage = { job: { templateId: 'video-translation', state: { sourceUrl: 'https://youtu.be/demo', sourceLanguage: 'en', targetLanguage: 'zh_cn', composeVideo } }, stageRun: { stageId: 'subtitle', id: 'stage' }, workdir: '/job/stage' };+ const options = buildKrillinStageOptions(stage as never);+ expect(buildKrillinCliCommandArguments(stage as never, [], options, undefined)).not.toContain('--prepare-video');+ });++ it('generates audio-only dubbing without a video, and lazily composes that audio later', () => {+ const state = { sourceUrl: 'https://youtu.be/demo', dubbing: true, composeVideo: false };+ const stage = { job: { templateId: 'video-translation', state }, stageRun: { stageId: 'tts', id: 'tts' }, workdir: '/job/tts' };+ const subtitle = { id: 'subtitle', kind: 'target_subtitle', path: '/job/target.srt' };+ const tts = buildKrillinCliCommandArguments(stage as never, [subtitle], buildKrillinStageOptions(stage as never), undefined);+ expect(tts).toContain('--audio-only');+ expect(tts).not.toContain('--source-url');+ const render = { ...stage, job: { ...stage.job, state: { ...state, composeVideo: true } }, stageRun: { stageId: 'render-horizontal', id: 'render' } };+ const args = buildKrillinCliCommandArguments(render as never, [subtitle, { id: 'audio', kind: 'dubbed_audio', path: '/job/dubbed.wav' }], buildKrillinStageOptions(render as never), undefined);+ expect(args).toContain('--source-url');+ expect(args).toContain('--media-workdir');+ expect(args.slice(args.indexOf('--audio'), args.indexOf('--audio') + 2)).toEqual(['--audio', '/job/dubbed.wav']);+ });it.each([true, false])('preserves French-to-Chinese settings through the adapter and CLI with platform captions=%s', preferPlatformCaptions => {const stage = {job: { templateId: 'video-translation', state: {diff --git a/apps/daemon/test/unit/creator-krillin-dependency-loader.test.ts b/apps/daemon/test/unit/creator-krillin-dependency-loader.test.tsindex 97cafd30..d332cb62 100644--- a/apps/daemon/test/unit/creator-krillin-dependency-loader.test.ts+++ b/apps/daemon/test/unit/creator-krillin-dependency-loader.test.ts@@ -1,11 +1,42 @@import { createDefaultCreatorServicesConfig } from '@opencreator/protocol';import { describe, expect, it } from 'vitest';+import { createHash } from 'node:crypto';+import { createServer } from 'node:http';+import { mkdtemp, readFile, rm } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';import {+ configuredOrDownloadedFile,createKrillinDependencyLoader,promoteDependencyPath} from '../../src/creator/krillin/dependency-loader.js';describe('KrillinAI on-demand dependency loader', () => {+ it('reports actual downloaded bytes and HTTP content length before verifying the file', async () => {+ const root = await mkdtemp(join(tmpdir(), 'opencreator-download-progress-'));+ const content = Buffer.alloc(128 * 1024, 'model');+ const server = createServer((_request, response) => {+ response.writeHead(200, { 'Content-Length': content.length });+ response.write(content.subarray(0, content.length / 2));+ setTimeout(() => response.end(content.subarray(content.length / 2)), 1200);+ });+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));+ const address = server.address();+ if (address === null || typeof address === 'string') throw new Error('Expected TCP address');+ const updates: Array<{ state: string; downloadedBytes?: number; totalBytes?: number | null }> = [];+ try {+ const file = await configuredOrDownloadedFile({ configured: undefined, name: 'test model', url: `http://127.0.0.1:${address.port}/model`, sha256: createHash('sha256').update(content).digest('hex'), path: join(root, 'model.bin'), proxy: '', signal: new AbortController().signal, onProgress: progress => updates.push(progress) });+ expect(await readFile(file)).toEqual(content);+ expect(updates.some(update => update.state === 'downloading'+ && (update.downloadedBytes ?? 0) > 0+ && (update.downloadedBytes ?? 0) < content.length+ && update.totalBytes === content.length)).toBe(true);+ expect(updates.at(-1)?.state).toBe('verifying');+ } finally {+ await new Promise<void>(resolve => server.close(() => resolve()));+ await rm(root, { recursive: true, force: true });+ }+ });it('waits and retries when Windows temporarily locks a downloaded dependency', async () => {let attempts = 0;const delays: number[] = [];diff --git a/apps/daemon/test/unit/creator-local-components.test.ts b/apps/daemon/test/unit/creator-local-components.test.tsnew file mode 100644index 00000000..98288dec--- /dev/null+++ b/apps/daemon/test/unit/creator-local-components.test.ts@@ -0,0 +1,74 @@+import { createDefaultCreatorServicesConfig } from '@opencreator/protocol';+import { describe, expect, it, vi } from 'vitest';+import { createKrillinDependencyLoader } from '../../src/creator/krillin/dependency-loader.js';++function fixture() {+ let installed = false;+ let finish!: () => void;+ const install = vi.fn(async (input: { onPhase(phase: 'cli' | 'model'): void; onProgress?(progress: { state: 'downloading'; item: string; downloadedBytes: number; totalBytes: number; bytesPerSecond: number; remainingSeconds: number }): void }) => {+ input.onPhase('model');+ input.onProgress?.({ state: 'downloading', item: 'WhisperKit large-v2 model', downloadedBytes: 50, totalBytes: 100, bytesPerSecond: 10, remainingSeconds: 5 });+ await new Promise<void>(resolve => { finish = resolve; });+ installed = true;+ });+ const loader = createKrillinDependencyLoader({ root: '/tmp/opencreator-components-unit', platform: 'darwin', arch: 'arm64', whisperKitInstaller: { isInstalled: async () => installed, install } });+ const config = createDefaultCreatorServicesConfig();+ config.transcription.provider = 'whisperkit';+ return { loader, config, install, finish: () => finish() };+}++describe('shared local transcription components', () => {+ it('exposes a failed installation and lets users retry the same component', async () => {+ let installed = false;+ const install = vi.fn().mockRejectedValueOnce(new Error('download connection failed')).mockImplementationOnce(async () => { installed = true; });+ const loader = createKrillinDependencyLoader({ root: '/tmp/opencreator-component-retry', platform: 'darwin', arch: 'arm64', whisperKitInstaller: { isInstalled: async () => installed, install } });+ const config = createDefaultCreatorServicesConfig();+ config.transcription.provider = 'whisperkit';+ await expect(loader.ensure({ config, signal: new AbortController().signal, reportProgress() {} })).rejects.toThrow('download connection failed');+ expect((await loader.status(config)).components.find(component => component.id === 'whisperkit')).toMatchObject({ state: 'failed', error: expect.stringContaining('download connection failed') });+ await loader.ensure({ config, signal: new AbortController().signal, reportProgress() {} });+ expect((await loader.status(config)).components.find(component => component.id === 'whisperkit')?.state).toBe('ready');+ expect(install).toHaveBeenCalledTimes(2);+ loader.close();+ });+ it('shares a manual download with task subscribers and exposes real progress', async () => {+ const test = fixture();+ expect((await test.loader.status(test.config)).components.find(component => component.id === 'whisperkit')?.state).toBe('not_installed');+ await test.loader.download(test.config);+ await vi.waitFor(() => expect(test.install).toHaveBeenCalledOnce());+ const listener = vi.fn();+ const task = test.loader.ensure({ config: test.config, signal: new AbortController().signal, reportProgress: listener });+ await vi.waitFor(() => expect(listener).toHaveBeenCalledWith(expect.objectContaining({ phase: 'downloading_dependencies', dependencyPercent: 50, downloadedBytes: 50, totalBytes: 100 })));+ const status = await test.loader.status(test.config);+ expect(status.components.find(component => component.id === 'whisperkit')).toMatchObject({ state: 'downloading', percent: 50, model: 'large-v2', item: 'WhisperKit large-v2 model' });+ test.finish();+ await task;+ expect(test.install).toHaveBeenCalledOnce();+ expect(listener).toHaveBeenCalledWith(expect.objectContaining({ phase: 'dependencies_ready', message: expect.stringContaining('开始语音转录') }));+ expect((await test.loader.status(test.config)).components.find(component => component.id === 'whisperkit')).toMatchObject({ state: 'ready', version: '1.1.0' });+ test.loader.close();+ });++ it('canceling one task does not cancel the shared download or another task', async () => {+ const test = fixture();+ const canceled = new AbortController();+ const first = test.loader.ensure({ config: test.config, signal: canceled.signal, reportProgress() {} });+ const second = test.loader.ensure({ config: test.config, signal: new AbortController().signal, reportProgress() {} });+ await vi.waitFor(() => expect(test.install).toHaveBeenCalledOnce());+ canceled.abort();+ await expect(first).rejects.toMatchObject({ code: 'creator_stage_canceled' });+ test.finish();+ await second;+ expect(test.install).toHaveBeenCalledOnce();+ test.loader.close();+ });++ it('does not offer installation on an unsupported Runtime', async () => {+ const loader = createKrillinDependencyLoader({ root: '/tmp/opencreator-components-unit', platform: 'linux', arch: 'x64' });+ const config = createDefaultCreatorServicesConfig();+ config.transcription.provider = 'whisperkit';+ expect((await loader.status(config)).components.find(component => component.id === 'whisperkit')?.state).toBe('unsupported');+ await expect(loader.download(config)).rejects.toThrow('unavailable');+ loader.close();+ });+});diff --git a/apps/daemon/test/unit/creator-preflight.test.ts b/apps/daemon/test/unit/creator-preflight.test.tsindex 0d02a29e..041ad886 100644--- a/apps/daemon/test/unit/creator-preflight.test.ts+++ b/apps/daemon/test/unit/creator-preflight.test.ts@@ -20,6 +20,28 @@ afterEach(async () => {});describe('creator preflight', () => {+ it.each([undefined, 3])('checks Bilibili selection for Agent stage requests: %s', async part => {+ root = await mkdtemp(join(tmpdir(), 'creator-preflight-'));+ const config = createDefaultCreatorServicesConfig();+ config.llm.source = 'codex';+ const job = fakeJob('video-translation', { sourceType: 'url',+ sourceUrl: `https://www.bilibili.com/video/BV18E421w7bf${part === undefined ? '' : `?p=${part}`}` });+ const result = await createCreatorPreflight({+ configStore: { read: async () => config },+ readCapabilities: () => createKrillinCreatorServicesCapabilities('darwin', 'arm64'),+ resourceRoot: join(root, 'runtime'), jobsRoot: join(root, 'jobs'),+ executorIds: ['krillinai'], validateRuntimeAssets: false,+ videoMetadataService: { get: async () => ({ platform: 'bilibili', title: 'Course',+ parts: [1, 2, 3].map(index => ({ index, title: `Lesson ${index}` })) }) }+ }).check(job, createVideoTranslationTemplate().stages.find(stage => stage.id === 'subtitle')!);+ if (part === undefined) {+ expect(result.canStart).toBe(false);+ expect(result.blocked).toContainEqual(expect.objectContaining({ id: 'bilibili-part', message: expect.stringContaining('选择要翻译的分集') }));+ } else {+ expect(result.blocked.map(item => item.id)).not.toContain('bilibili-part');+ }+ });+it('blocks a stage with missing provider credentials and provides a settings repair', async () => {root = await mkdtemp(join(tmpdir(), 'creator-preflight-'));const config = createDefaultCreatorServicesConfig();@@ -127,7 +149,7 @@ describe('creator preflight', () => {]));});- it('accepts Codex subscription image generation without API credentials', async () => {+ it('accepts Codex subscription image generation only with checked native capability', async () => {root = await mkdtemp(join(tmpdir(), 'creator-preflight-'));const config = createDefaultCreatorServicesConfig();config.image.provider = 'codex-native';@@ -138,7 +160,8 @@ describe('creator preflight', () => {resourceRoot: join(root, 'runtime'),jobsRoot: join(root, 'jobs'),executorIds: ['stickman-image'],- validateRuntimeAssets: false+ validateRuntimeAssets: false,+ readCodexImageStatus: async () => ({ authentication: 'chatgpt', ready: true, executionMode: 'native', message: '原生工具已就绪' })}).check(fakeJob('stickman-video', { provider: 'codex-native' }), stage);expect(result.blocked.map(item => item.id)).not.toContain('image-provider');@@ -147,6 +170,32 @@ describe('creator preflight', () => {]));});+ it.each([undefined, false])('blocks unchecked or unsupported Codex image capability (%s)', async available => {+ root = await mkdtemp(join(tmpdir(), 'creator-preflight-'));+ const config = createDefaultCreatorServicesConfig();+ config.image.provider = 'codex-native';+ const result = await createCreatorPreflight({+ configStore: { read: async () => config },+ readCapabilities: () => createKrillinCreatorServicesCapabilities('darwin', 'arm64'),+ resourceRoot: root, jobsRoot: join(root, 'jobs'), validateRuntimeAssets: false,+ ...(available === undefined ? {} : { readCodexImageStatus: async () => ({ authentication: 'chatgpt' as const, ready: false, executionMode: null, message: 'Runtime 不支持原生生图' }) })+ }).check(fakeJob('image-generation', { provider: 'codex-native', prompt: 'test' }), createImageGenerationTemplate().stages[0]!);+ expect(result.canStart).toBe(false);+ expect(result.blocked).toContainEqual(expect.objectContaining({ id: 'image-provider' }));+ });++ it('reports Codex API mode separately from local native generation', async () => {+ root = await mkdtemp(join(tmpdir(), 'creator-preflight-'));+ const config = createDefaultCreatorServicesConfig();+ const result = await createCreatorPreflight({+ configStore: { read: async () => config },+ readCapabilities: () => createKrillinCreatorServicesCapabilities('darwin', 'arm64'),+ resourceRoot: root, jobsRoot: join(root, 'jobs'), validateRuntimeAssets: false,+ readCodexImageStatus: async () => ({ authentication: 'api_key', ready: true, executionMode: 'api', message: '接口需支持图片生成' })+ }).check(fakeJob('image-generation', { provider: 'codex-native', prompt: 'test' }), createImageGenerationTemplate().stages[0]!);+ expect(result.ready).toContainEqual(expect.objectContaining({ id: 'image-provider', executionMode: 'remote', message: '接口需支持图片生成' }));+ });+it.each([['source-brief', 'stickman-content', 'llm'],['narration', 'stickman-audio', 'tts'],diff --git a/apps/daemon/test/unit/creator-runtime-routes.test.ts b/apps/daemon/test/unit/creator-runtime-routes.test.tsindex 996ace22..7fc15d5b 100644--- a/apps/daemon/test/unit/creator-runtime-routes.test.ts+++ b/apps/daemon/test/unit/creator-runtime-routes.test.ts@@ -1,5 +1,6 @@import Fastify, { type FastifyInstance } from 'fastify';-import type { CreatorYtDlpStatus } from '@opencreator/protocol';+import { createDefaultCreatorServicesConfig, type CreatorYtDlpStatus } from '@opencreator/protocol';+import { createKrillinDependencyLoader } from '../../src/creator/krillin/dependency-loader.js';import { afterEach, describe, expect, it, vi } from 'vitest';import { registerCreatorRuntimeRoutes } from '../../src/api/routes.creator-runtime.js';import {@@ -15,6 +16,23 @@ afterEach(async () => {});describe('creator yt-dlp runtime routes', () => {+ it('reports local engine inventory and starts downloads independently of yt-dlp', async () => {+ const config = createDefaultCreatorServicesConfig();+ config.transcription.provider = 'whisperkit';+ let installed = false;+ const install = vi.fn(async () => { installed = true; });+ const loader = createKrillinDependencyLoader({ root: '/tmp/opencreator-components-route', platform: 'darwin', arch: 'arm64', whisperKitInstaller: { isInstalled: async () => installed, install } });+ server = Fastify();+ await registerCreatorRuntimeRoutes(server, undefined, { loader, readConfig: async () => config });+ const before = await server.inject({ method: 'GET', url: '/creator/components/status' });+ expect(before.statusCode).toBe(200);+ expect(before.json()).toMatchObject({ selectedProvider: 'whisperkit', selectedModel: 'large-v2', components: expect.arrayContaining([expect.objectContaining({ id: 'whisperkit', state: 'not_installed' })]) });+ const download = await server.inject({ method: 'POST', url: '/creator/components/download' });+ expect(download.statusCode).toBe(200);+ await vi.waitFor(() => expect(install).toHaveBeenCalledOnce());+ const after = await server.inject({ method: 'GET', url: '/creator/components/status' });+ expect(after.json().components.find((component: { id: string }) => component.id === 'whisperkit').state).toBe('ready');+ });it('exposes status, periodic checks, and manual updates', async () => {const current = status();const checked = status({diff --git a/apps/daemon/test/unit/creator-video-translation-workflow.test.ts b/apps/daemon/test/unit/creator-video-translation-workflow.test.tsindex 13d5e668..f2a103ad 100644--- a/apps/daemon/test/unit/creator-video-translation-workflow.test.ts+++ b/apps/daemon/test/unit/creator-video-translation-workflow.test.ts@@ -20,6 +20,42 @@ afterEach(() => {});describe('video translation workflow', () => {+ it.each([+ { suffix: '', error: 'creator_source_part_required' },+ { suffix: '?p=0', error: 'creator_source_invalid_part' },+ { suffix: '?p=4', error: 'creator_source_invalid_part' },+ { suffix: '?p=3', error: undefined }+ ])('validates Bilibili selection before starting: $suffix', async ({ suffix, error }) => {+ const fixture = setup({ sourceUrl: `https://www.bilibili.com/video/BV18E421w7bf${suffix}` });+ const config = createDefaultCreatorServicesConfig();+ config.llm.source = 'codex';+ const get = vi.fn(async () => ({ platform: 'bilibili' as const, title: 'Course',+ parts: [1, 2, 3].map(index => ({ index, title: `Lesson ${index}` })) }));+ const workflow = createVideoTranslationWorkflow({ creator: fixture.service, dispatcher: fixture.dispatcher,+ configStore: { read: async () => config }, videoMetadataService: { get } });+ try {+ const validated = workflow.validateStage(fixture.service.getJob(fixture.jobId)!, 'subtitle');+ if (error) await expect(validated).rejects.toMatchObject({ code: error });+ else {+ await expect(validated).resolves.toBeUndefined();+ expect(get).toHaveBeenCalledWith('https://www.bilibili.com/video/BV18E421w7bf?p=3');+ }+ expect(fixture.service.getJob(fixture.jobId)!.stages).toHaveLength(0);+ } finally { fixture.db.close(); }+ });++ it('does not download a Bilibili collection when part lookup fails', async () => {+ const fixture = setup({ sourceUrl: 'https://www.bilibili.com/video/BV18E421w7bf' });+ const workflow = createVideoTranslationWorkflow({ creator: fixture.service, dispatcher: fixture.dispatcher,+ configStore: { read: async () => createDefaultCreatorServicesConfig() },+ videoMetadataService: { get: async () => { throw new Error('network unavailable'); } } });+ try {+ await expect(workflow.validateStage(fixture.service.getJob(fixture.jobId)!, 'subtitle'))+ .rejects.toMatchObject({ code: 'creator_source_metadata_unavailable', message: expect.stringContaining('未启动下载或翻译') });+ expect(fixture.service.getJob(fixture.jobId)!.stages).toHaveLength(0);+ } finally { fixture.db.close(); }+ });+it('accepts Codex runtime translation without a creator API key', async () => {const fixture = setup({});const config = createDefaultCreatorServicesConfig();diff --git a/apps/desktop/e2e/desktop.spec.ts b/apps/desktop/e2e/desktop.spec.tsindex 89b8f60b..499f9e0f 100644--- a/apps/desktop/e2e/desktop.spec.ts+++ b/apps/desktop/e2e/desktop.spec.ts@@ -981,12 +981,17 @@ test('Dashboard 握手超时后显示本地错误页并可无 Probe 重载', asy}});-test('@package-smoke Daemon 异常退出后自动恢复且不重复 Probe', async () => {+test('@package-smoke Daemon 连续退出保留 Creator 草稿,自动恢复耗尽后可手动重启', async () => {const fixture = await launchPackagedDesktop('success');try {await waitForWorkspace(fixture.page);await expect.poll(() => readCounter(fixture.stateDir, 'probe-count.txt')).toBe(1);const mainPid = requiredPid(fixture.process.pid);+ await fixture.page.getByRole('button', { name: '工作台', exact: true }).click();+ await fixture.page.getByRole('button', { name: /^视频翻译/ }).click();+ const composer = fixture.page.getByRole('textbox', { name: '告诉 Agent 你的要求' });+ await composer.fill('保留这段尚未提交的翻译要求');+ await fixture.page.evaluate(() => { document.body.dataset.runtimeRecoveryMarker = 'retain-renderer'; });const firstDaemonPid = await waitForDaemonUtilityPid(mainPid);process.kill(firstDaemonPid, 'SIGKILL');@@ -1004,8 +1009,15 @@ test('@package-smoke Daemon 异常退出后自动恢复且不重复 Probe', asynhealth: undefined})), { timeout: 30_000 }).toEqual({ phase: 'ready', health: 200 });await expect.poll(() => readCounter(fixture.stateDir, 'probe-count.txt')).toBe(1);-- process.kill(replacementPid, 'SIGKILL');+ await expect(composer).toHaveValue('保留这段尚未提交的翻译要求');+ let currentPid = replacementPid;+ for (let attempt = 0; attempt < 2; attempt += 1) {+ process.kill(currentPid, 'SIGKILL');+ currentPid = await waitForDaemonUtilityPid(mainPid, currentPid);+ await expect.poll(async () => (await fixture.page.evaluate(() => window.opencreatorDesktop?.readBootstrapState()))?.phase).toBe('ready');+ await expect(composer).toHaveValue('保留这段尚未提交的翻译要求');+ }+ process.kill(currentPid, 'SIGKILL');await expect.poll(async () => await fixture.page.evaluate(async () => {const state = await window.opencreatorDesktop?.readBootstrapState();return {@@ -1020,6 +1032,13 @@ test('@package-smoke Daemon 异常退出后自动恢复且不重复 Probe', asyncode: 'DAEMON_RESTART_EXHAUSTED'});expect(readCounter(fixture.stateDir, 'probe-count.txt')).toBe(1);+ await expect(fixture.page.getByRole('button', { name: '重启本地服务', exact: true })).toBeVisible();+ fixture.page.once('dialog', dialog => void dialog.accept());+ await fixture.page.getByRole('button', { name: '重启本地服务', exact: true }).click();+ await expect.poll(async () => (await fixture.page.evaluate(() => window.opencreatorDesktop?.readBootstrapState()))?.phase).toBe('ready');+ await expect(composer).toHaveValue('保留这段尚未提交的翻译要求');+ expect(await fixture.page.evaluate(() => document.body.dataset.runtimeRecoveryMarker)).toBe('retain-renderer');+ expect(readCounter(fixture.stateDir, 'probe-count.txt')).toBe(1);} finally {await closeFixture(fixture);}diff --git a/apps/desktop/src/main/bootstrap-controller.ts b/apps/desktop/src/main/bootstrap-controller.tsindex 643eb88f..11a9966a 100644--- a/apps/desktop/src/main/bootstrap-controller.ts+++ b/apps/desktop/src/main/bootstrap-controller.ts@@ -34,6 +34,9 @@ export class BootstrapController extends EventEmitter<BootstrapControllerEvents>private verifiedFingerprint: string | undefined;private automaticRestarts = 0;private stableTimer: NodeJS.Timeout | undefined;+ private recoveryWork: Promise<void> | undefined;+ private recoveryController: AbortController | undefined;+ private stopped = false;constructor(private readonly input: {daemon: DaemonManager;@@ -47,6 +50,7 @@ export class BootstrapController extends EventEmitter<BootstrapControllerEvents>stickmanRuntimeRoot: string;defaultProjectRoot: string;development: boolean;+ automaticRestartDelays?: readonly number[];}) {super();input.daemon.on('bootstrap', event => {@@ -76,8 +80,11 @@ export class BootstrapController extends EventEmitter<BootstrapControllerEvents>this.emit('connection', connection === null ? null : this.rendererConnection());});input.daemon.on('exit', event => {- if (event.reason !== 'unexpected' || this.state.phase === 'failed') return;- void this.recoverUnexpectedExit();+ if (event.reason !== 'unexpected' || this.state.phase === 'failed' || this.stopped) return;+ if (this.recoveryWork !== undefined || this.runWork !== undefined) return;+ this.recoveryWork = this.recoverUnexpectedExit().finally(() => {+ this.recoveryWork = undefined;+ });});}@@ -95,6 +102,7 @@ export class BootstrapController extends EventEmitter<BootstrapControllerEvents>selectedCodexBin?: string,loginShellTask?: LoginShellEnvironmentTask): Promise<void> {+ this.stopped = false;if (this.runWork !== undefined) return this.runWork;this.runWork = this.startInternal(selectedCodexBin, loginShellTask).finally(() => {this.runWork = undefined;@@ -123,17 +131,32 @@ export class BootstrapController extends EventEmitter<BootstrapControllerEvents>}async restartRuntime(): Promise<void> {+ if (this.runWork !== undefined) return this.runWork;+ this.recoveryController?.abort();+ if (this.recoveryWork !== undefined) await this.input.daemon.stop();+ await this.recoveryWork;+ if (this.runWork !== undefined) return this.runWork;+ this.stopped = false;if (this.resolvedEnvironment === undefined) {await this.start();+ if (this.state.phase === 'failed') throw new Error(this.state.error?.message ?? '本地服务启动失败');return;}- await this.input.daemon.restart(this.daemonStartInput(- this.resolvedEnvironment,- false,- true- ));- this.updateState({ phase: 'starting_runtime', error: undefined });- this.emit('ready');+ const environment = this.resolvedEnvironment;+ this.updateState({ phase: 'starting_daemon', error: undefined });+ this.runWork = (async () => {+ try {+ await this.input.daemon.restart(this.daemonStartInput(environment, false, true));+ this.automaticRestarts = 0;+ this.updateState({ phase: 'starting_runtime', error: undefined });+ this.emit('ready');+ this.armStableTimer();+ } catch (error) {+ this.updateState({ phase: 'failed', error: parseBootstrapError(error) });+ throw error;+ }+ })().finally(() => { this.runWork = undefined; });+ await this.runWork;}markWorkspaceReady(): void {@@ -161,8 +184,11 @@ export class BootstrapController extends EventEmitter<BootstrapControllerEvents>}async stop(): Promise<void> {+ this.stopped = true;+ this.recoveryController?.abort();if (this.stableTimer !== undefined) clearTimeout(this.stableTimer);await this.input.daemon.stop();+ await this.recoveryWork;}private async startInternal(@@ -277,32 +303,34 @@ export class BootstrapController extends EventEmitter<BootstrapControllerEvents>private async recoverUnexpectedExit(): Promise<void> {const environment = this.resolvedEnvironment;- if (environment === undefined || this.automaticRestarts >= 1) {- this.updateState({- phase: 'failed',- error: {- code: 'DAEMON_RESTART_EXHAUSTED',- message: '本地运行服务连续退出,请导出诊断后重新检测'- }- });- return;+ const delays = this.input.automaticRestartDelays ?? [500, 1_500, 3_000];+ const controller = new AbortController();+ this.recoveryController = controller;+ if (this.stableTimer !== undefined) clearTimeout(this.stableTimer);+ while (environment !== undefined && this.automaticRestarts < delays.length && !this.stopped) {+ const delay = delays[this.automaticRestarts] ?? 3_000;+ this.automaticRestarts += 1;+ this.updateState({ phase: 'starting_daemon', error: undefined });+ this.input.logger.info('Recovering local Runtime', { attempt: this.automaticRestarts, delayMs: delay });+ await waitForRecovery(delay, controller.signal);+ if (controller.signal.aborted || this.stopped) return;+ try {+ await this.input.daemon.start(this.daemonStartInput(environment, false, true));+ if (controller.signal.aborted || this.stopped) return;+ this.updateState({ phase: 'starting_runtime', error: undefined });+ this.emit('ready');+ this.armStableTimer();+ return;+ } catch (error) {+ if (controller.signal.aborted || this.stopped) return;+ this.input.logger.error('Local Runtime recovery failed', { message: error instanceof Error ? error.message : String(error) });+ }}- this.automaticRestarts += 1;+ if (controller.signal.aborted || this.stopped) return;this.updateState({- phase: 'starting_daemon',- error: undefined+ phase: 'failed',+ error: { code: 'DAEMON_RESTART_EXHAUSTED', message: '本地服务多次恢复失败,请在连接提示或设置 → 诊断中重启本地服务。' }});- try {- await this.input.daemon.start(this.daemonStartInput(environment, false, true));- this.updateState({ phase: 'starting_runtime' });- this.emit('ready');- this.armStableTimer();- } catch (error) {- this.updateState({- phase: 'failed',- error: parseBootstrapError(error)- });- }}private daemonStartInput(@@ -350,6 +378,18 @@ export class BootstrapController extends EventEmitter<BootstrapControllerEvents>}}+function waitForRecovery(delay: number, signal: AbortSignal): Promise<void> {+ return new Promise(resolve => {+ const finish = () => {+ clearTimeout(timer);+ signal.removeEventListener('abort', finish);+ resolve();+ };+ const timer = setTimeout(finish, delay);+ signal.addEventListener('abort', finish, { once: true });+ });+}+function isWorkspaceError(error: unknown): error is Error & { code: string } {diff --git a/apps/desktop/src/main/main.ts b/apps/desktop/src/main/main.tsindex 92dd7294..5da639a3 100644--- a/apps/desktop/src/main/main.ts+++ b/apps/desktop/src/main/main.ts@@ -257,7 +257,8 @@ async function launchDesktop(): Promise<void> {windowManager?.send(desktopIpc.connectionChanged, connection);});bootstrap.on('ready', () => {- void loadWorkspace();+ if (workspaceLoaded) bootstrap?.markWorkspaceReady();+ else void loadWorkspace();});bootstrapSurfaceReady = windowManager.loadBootstrap();diff --git a/apps/desktop/test/bootstrap-controller.test.ts b/apps/desktop/test/bootstrap-controller.test.tsnew file mode 100644index 00000000..3623c728--- /dev/null+++ b/apps/desktop/test/bootstrap-controller.test.ts@@ -0,0 +1,111 @@+import { EventEmitter } from 'node:events';+import { afterEach, describe, expect, it, vi } from 'vitest';+import { BootstrapController } from '../src/main/bootstrap-controller.js';+import type { DaemonManager } from '../src/main/daemon-manager.js';+import type { DesktopLogger } from '../src/main/logger.js';+import type { SettingsStore } from '../src/main/settings-store.js';++vi.mock('../src/main/codex-resolver.js', async importOriginal => ({+ ...await importOriginal<typeof import('../src/main/codex-resolver.js')>(),+ resolveCodexEnvironment: vi.fn(async () => ({+ codexBin: '/test/codex', codexHome: '/test/home', defaultCwd: '/test/project',+ env: {}, source: 'bundled', version: '1.0', commit: null, runtimeRoot: '/test/runtime'+ }))+}));++afterEach(() => vi.useRealTimers());++function fixture() {+ const daemon = new EventEmitter() as EventEmitter & {+ start: ReturnType<typeof vi.fn>;+ restart: ReturnType<typeof vi.fn>;+ stop: ReturnType<typeof vi.fn>;+ };+ daemon.start = vi.fn(async () => undefined);+ daemon.restart = vi.fn(async () => undefined);+ daemon.stop = vi.fn(async () => undefined);+ const controller = new BootstrapController({+ daemon: daemon as unknown as DaemonManager,+ logger: { info: vi.fn(), error: vi.fn() } as unknown as DesktopLogger,+ settings: { read: () => ({}), update: vi.fn() } as unknown as SettingsStore,+ daemonEntryPath: '/test/daemon', appHome: '/test', dataDir: '/test/data',+ codexRuntimeRoot: '/test/codex', creatorRuntimeRoot: '/test/creator', stickmanRuntimeRoot: '/test/stickman',+ defaultProjectRoot: '/test/projects', development: false, automaticRestartDelays: [10, 20, 30]+ });+ controller.on('ready', () => controller.markWorkspaceReady());+ const crash = () => daemon.emit('exit', { reason: 'unexpected', code: 1 });+ return { daemon, controller, crash };+}++describe('Desktop Runtime recovery', () => {+ it('bounds automatic recovery, backs off, and resets the budget after manual restart', async () => {+ vi.useFakeTimers();+ const input = fixture();+ await input.controller.start();+ for (const delay of [10, 20, 30]) {+ input.crash();+ await vi.advanceTimersByTimeAsync(delay - 1);+ expect(input.controller.currentState.phase).toBe('starting_daemon');+ await vi.advanceTimersByTimeAsync(1);+ expect(input.controller.currentState.phase).toBe('ready');+ }+ input.crash();+ await vi.advanceTimersByTimeAsync(0);+ expect(input.controller.currentState).toMatchObject({ phase: 'failed', error: { code: 'DAEMON_RESTART_EXHAUSTED' } });+ expect(input.daemon.start).toHaveBeenCalledTimes(4);+ await input.controller.restartRuntime();+ input.crash();+ await vi.advanceTimersByTimeAsync(10);+ expect(input.controller.currentState.phase).toBe('ready');+ expect(input.daemon.start).toHaveBeenCalledTimes(5);+ expect(input.daemon.start.mock.calls.slice(1).every(call => call[0].requireProbe === false)).toBe(true);+ await input.controller.stop();+ });++ it('retries failed recovery starts and cancels queued recovery on shutdown', async () => {+ vi.useFakeTimers();+ const input = fixture();+ await input.controller.start();+ input.daemon.start.mockRejectedValueOnce(new Error('temporary start failure'));+ input.crash();+ await vi.advanceTimersByTimeAsync(10);+ expect(input.controller.currentState.phase).toBe('starting_daemon');+ await vi.advanceTimersByTimeAsync(20);+ expect(input.controller.currentState.phase).toBe('ready');+ input.crash();+ await input.controller.stop();+ await vi.advanceTimersByTimeAsync(1_000);+ expect(input.daemon.start).toHaveBeenCalledTimes(3);+ });++ it('cancels a pending automatic restart before manually restarting and reports a real failure', async () => {+ vi.useFakeTimers();+ const input = fixture();+ await input.controller.start();+ input.crash();+ await input.controller.restartRuntime();+ await vi.advanceTimersByTimeAsync(100);+ expect(input.daemon.start).toHaveBeenCalledTimes(1);+ expect(input.daemon.restart).toHaveBeenCalledTimes(1);+ input.daemon.restart.mockRejectedValueOnce(new Error('cannot start'));+ await expect(input.controller.restartRuntime()).rejects.toThrow('cannot start');+ expect(input.controller.currentState.phase).toBe('failed');+ await input.controller.stop();+ });++ it.each(['stop', 'restartRuntime'] as const)('cancels an in-flight recovery start before waiting for %s', async operation => {+ vi.useFakeTimers();+ const input = fixture();+ await input.controller.start();+ let rejectStart: ((error: Error) => void) | undefined;+ input.daemon.start.mockImplementationOnce(() => new Promise((_resolve, reject) => { rejectStart = reject; }));+ input.daemon.stop.mockImplementation(async () => { rejectStart?.(new Error('stopped')); });+ input.crash();+ await vi.advanceTimersByTimeAsync(10);+ expect(rejectStart).toBeDefined();+ await input.controller[operation]();+ expect(input.daemon.stop).toHaveBeenCalled();+ if (operation === 'restartRuntime') expect(input.controller.currentState.phase).toBe('ready');+ await input.controller.stop();+ });+});diff --git a/apps/web/e2e/video-translation-components.spec.ts b/apps/web/e2e/video-translation-components.spec.tsnew file mode 100644index 00000000..3ff3aee4--- /dev/null+++ b/apps/web/e2e/video-translation-components.spec.ts@@ -0,0 +1,70 @@+import type { CreatorJob, CreatorRuntimeComponentsResponse } from '@opencreator/protocol';+import { expect, test } from './fixtures/runtime.js';++test('本地转录提示可跳转下载、显示真实进度并返回原视频翻译草稿', async ({ page, runtime }) => {+ const status: CreatorRuntimeComponentsResponse = {+ platform: 'darwin', arch: 'arm64', selectedProvider: 'whisperkit', selectedModel: 'large-v2',+ components: [{ id: 'whisperkit', name: 'WhisperKit', available: true, version: null, supportedVersion: '1.1.0', installedAt: null,+ path: '/runtime/dependencies', source: 'Homebrew / ModelScope', models: [{ id: 'large-v2', installed: false, bytes: null }],+ state: 'not_installed', model: 'large-v2', item: null, downloadedBytes: 0, totalBytes: null, percent: null, bytesPerSecond: null, remainingSeconds: null, error: null }]+ };+ let downloadRequests = 0;+ await page.route('**/creator/components/status', route => route.fulfill({ json: status }));+ await page.route('**/creator/components/download', route => {+ downloadRequests += 1;+ Object.assign(status.components[0]!, { state: 'downloading', item: 'WhisperKit large-v2 model', downloadedBytes: 1024 ** 3, totalBytes: 2 * 1024 ** 3, percent: 50, bytesPerSecond: 1024 ** 2 });+ return route.fulfill({ json: status });+ });+ await page.route('**/creator/yt-dlp/check', route => route.fulfill({ json: { ytDlp: { channel: 'nightly', source: 'bundled', currentVersion: 'test', bundledVersion: 'test', latestVersion: 'next', updateAvailable: true, checkDue: false, lastCheckedAt: null, lastCheckAttemptAt: null, installedAt: null } } }));+ await page.route('**/video-metadata?*', route => route.fulfill({ json: { platform: 'youtube', title: 'Component test', width: 1920, height: 1080 } }));+ const job = await runtime.api<{ job: CreatorJob }>('POST', '/creator/jobs', {+ projectId: runtime.projectId, templateId: 'video-translation', templateVersion: 2,+ state: { sourceType: 'url', sourceUrl: 'https://www.youtube.com/watch?v=component-test', sourceLanguage: 'en', targetLanguage: 'zh_cn', currentStep: 1, furthestStep: 1, workspacePhase: 'configure' },+ creationKey: 'component-notice'+ }).then(response => response.job);+ await runtime.openApp(page);+ await page.goto(`${runtime.origin}/#/workbench?tool=video-translation&jobId=${encodeURIComponent(job.id)}`);+ await expect(page.getByText('本地转录组件尚未就绪')).toBeVisible();+ await expect(page.getByText(/没有可用字幕时才需要本地转录/)).toBeVisible();+ await page.getByRole('link', { name: '前往组件下载' }).click();+ await expect(page.getByRole('heading', { name: '第三方组件' })).toBeVisible();+ await page.getByRole('button', { name: '下载组件' }).click();+ await expect(page.getByRole('progressbar', { name: '组件下载进度' })).toHaveAttribute('value', '50');+ await expect(page.getByText(/当前尚未开始转录/)).toBeVisible();+ Object.assign(status.components[0]!, { state: 'ready', version: '1.1.0', percent: 100 });+ status.components[0]!.models[0]!.installed = true;+ const component = page.locator('#component-whisperkit');+ const checkStatus = component.getByRole('button', { name: '检查状态' });+ await expect(checkStatus).toBeVisible();+ await expect(component.locator('.settings-primary-button')).toHaveCount(0);+ await expect(component.getByText('/runtime/dependencies')).toBeHidden();+ await checkStatus.click();+ await expect(component.getByText('检查完成,本地组件已就绪。')).toBeVisible();+ expect(downloadRequests).toBe(1);+ await component.getByText('组件详情', { exact: true }).click();+ await expect(component.getByText('/runtime/dependencies')).toBeVisible();+ await component.getByText('组件详情', { exact: true }).click();+ const ytDlp = page.locator('.runtime-component-item').filter({ has: page.getByRole('heading', { name: 'yt-dlp nightly' }) });+ await expect(ytDlp.getByRole('button', { name: '更新到 next' })).toBeVisible();+ const controls = await page.evaluate(() => {+ const localCard = document.querySelector('#component-whisperkit')!;+ const localButton = localCard.querySelector('button')!.getBoundingClientRect();+ const ytDlpButtons = document.querySelectorAll('.runtime-component-item:not(.local-component-item) .runtime-component-actions button');+ const check = ytDlpButtons[0]!.getBoundingClientRect();+ const update = ytDlpButtons[1]!.getBoundingClientRect();+ return { localWidth: localButton.width, localHeight: localButton.height, cardWidth: localCard.getBoundingClientRect().width,+ checkWidth: check.width, checkHeight: check.height, checkTop: check.top, updateLeft: update.left, checkRight: check.right, updateTop: update.top };+ });+ expect(controls.localWidth).toBeLessThan(200);+ expect(controls.localWidth).toBeLessThan(controls.cardWidth / 2);+ expect(controls.localHeight).toBeGreaterThanOrEqual(32);+ expect(controls.localHeight).toBeLessThanOrEqual(36);+ expect(controls.checkHeight).toBe(controls.localHeight);+ expect(Math.abs(controls.checkTop - controls.updateTop)).toBeLessThan(1);+ expect(controls.updateLeft - controls.checkRight).toBeGreaterThanOrEqual(7);+ await page.screenshot({ path: 'test-results/video-translation-components-compact.png', fullPage: true });+ await page.getByRole('button', { name: '返回视频翻译' }).click();+ await expect(page).toHaveURL(new RegExp(`jobId=${job.id}`));+ await expect(page.getByText('本地转录已就绪')).toBeVisible();+ await expect(page.getByRole('navigation', { name: '翻译流程' }).getByRole('button', { name: /翻译设置$/ })).toHaveAttribute('aria-current', 'step');+});diff --git a/apps/web/src/app/App.test.tsx b/apps/web/src/app/App.test.tsxindex 43cfae3b..9adf0c82 100644--- a/apps/web/src/app/App.test.tsx+++ b/apps/web/src/app/App.test.tsx@@ -423,6 +423,50 @@ describe('App', () => {);});+ it.each(['browser', 'desktop'] as const)('retains Creator and composer drafts across Runtime recovery in the %s host', async hostKind => {+ window.location.hash = '#/workbench?tool=cover-generator';+ const hostBridge = createHostBridge();+ hostBridge.kind = hostKind;+ let online = true;+ let announce: ((config: { baseUrl: string; token?: string } | null) => void) | undefined;+ const config = { baseUrl: 'http://127.0.0.1:60764', token: 'runtime-token' };+ hostBridge.readConnectionConfig = async () => online ? { ...config } : null;+ hostBridge.subscribeConnectionConfig = listener => {+ announce = listener;+ return () => { announce = undefined; };+ };+ let taskRequests = 0;+ const runtimeFetch = async (input: RequestInfo | URL, init?: RequestInit) => {+ const url = String(input);+ if (!online) throw new TypeError('offline');+ if (init?.method === 'POST' && /\/agent-(turns|turn)|\/stages\//.test(url)) taskRequests += 1;+ const projectApiResponse = handleDefaultProjectApiRequest(url, init);+ if (projectApiResponse !== undefined) return projectApiResponse;+ if (url.endsWith('/healthz')) return jsonResponse({ ok: true });+ if (url.endsWith('/codex/status')) return jsonResponse(createCodexStatusResponse());+ if (url.includes('/threads?')) return jsonResponse({ threads: [] });+ throw new Error(`Unexpected request ${url}`);+ };+ render(<App fileService={createFileService()} hostBridge={hostBridge} runtimeFetch={runtimeFetch} subscribeRunEvents={async () => undefined} />);+ const draft = await screen.findByRole('textbox', { name: '内容与补充要求' });+ fireEvent.change(draft, { target: { value: '封面业务草稿' } });+ await waitFor(() => expect(testCreatorJobs).toHaveLength(1));+ const composer = screen.getByRole('textbox', { name: '告诉 Agent 你的要求' });+ fireEvent.change(composer, { target: { value: '尚未提交的 Agent 要求' } });+ online = false;+ act(() => announce?.(null));+ expect(await screen.findByText(/本地服务暂未连接,正在自动重连/)).toBeVisible();+ expect(draft).toHaveValue('封面业务草稿');+ expect(composer).toHaveValue('尚未提交的 Agent 要求');+ online = true;+ act(() => announce?.(config));+ await waitFor(() => expect(screen.queryByText(/本地服务暂未连接,正在自动重连/)).not.toBeInTheDocument());+ expect(screen.getByRole('textbox', { name: '内容与补充要求' })).toHaveValue('封面业务草稿');+ expect(screen.getByRole('textbox', { name: '告诉 Agent 你的要求' })).toHaveValue('尚未提交的 Agent 要求');+ expect(testCreatorJobs).toHaveLength(1);+ expect(taskRequests).toBe(0);+ });+it('restores the project library directly from its URL', async () => {window.location.hash = '#/projects';render(<App projectNavigationMode="library" />);diff --git a/apps/web/src/app/AppController.tsx b/apps/web/src/app/AppController.tsxindex e76f6e53..9f516c99 100644--- a/apps/web/src/app/AppController.tsx+++ b/apps/web/src/app/AppController.tsx@@ -127,6 +127,8 @@ import { browserBridge } from '../host/browser-bridge.js';import type { HostBridge } from '../host/bridge.js';import { useAppLanguage } from '../i18n/LanguageProvider.js';import { ApiClientError, RuntimeClient } from '../runtime/client.js';+import { useRuntimeConnection } from '../runtime/use-runtime-connection.js';+import { RuntimeRecoveryContext } from '../runtime/runtime-recovery.js';import { createFrameBatcher, type FrameBatcher } from '../runtime/frame-batcher.js';import { subscribeRunEvents as defaultSubscribeRunEvents, type SubscribeRunEventsInput } from '../runtime/sse.js';import type { ConnectionConfig } from '../runtime/types.js';@@ -179,7 +181,7 @@ import {writeColorModePreference} from '../styles/color-mode.js';import { initialAppState, reduceAppState, type ActiveView, type AppState } from './app-state.js';-import { formatRoute, type AppRoute } from './routes.js';+import { formatRoute, parseRoute, type AppRoute } from './routes.js';import { useRuntimeDependencies } from './use-runtime-dependencies.js';type AppFileService = {@@ -336,7 +338,7 @@ export function AppController(props: AppControllerProps) {() => new Set());const [approvalErrors, setApprovalErrors] = useState<Record<string, string | undefined>>({});- const [connectionConfig, setConnectionConfig] = useState<ConnectionConfig | null>(null);+ const { config: connectionConfig, state: connectionState, recovery: runtimeRecovery } = useRuntimeConnection(hostBridge, runtimeFetch);const [runtimeThreads, setRuntimeThreads] = useState<ThreadResponse[]>([]);const [runtimeSchedules, setRuntimeSchedules] = useState<ScheduleResponse[]>([]);const [runtimeTasks, setRuntimeTasks] = useState<TaskItem[]>([]);@@ -353,10 +355,6 @@ export function AppController(props: AppControllerProps) {const [historyLoadingThreadId, setHistoryLoadingThreadId] = useState<string>();const [historyLoadedThreadId, setHistoryLoadedThreadId] = useState<string>();const [threadConfigUpdateError, setThreadConfigUpdateError] = useState<string>();- const [connectionState, setConnectionState] = useState<ConnectionState>({- status: 'disconnected',- message: '正在等待本地服务'- });const [agentSetup, setAgentSetup] = useState<'checking' | 'ready' | 'needed' | 'skipped'>('checking');const [agentSetupSnapshot, setAgentSetupSnapshot] = useState<AgentSetupSnapshot>();const [runDiagnosticsById, setRunDiagnosticsById] = useState<Record<string, RunDiagnosticsResponse | undefined>>({});@@ -473,7 +471,6 @@ export function AppController(props: AppControllerProps) {const fileRevisionByPathRef = useRef<Record<string, number>>({});const savingFilePathsRef = useRef(new Set<string>());const connectionConfigRef = useRef<ConnectionConfig | null>(null);- const connectionConfigVersionRef = useRef(0);const runEventControllersRef = useRef(new Map<string, ActiveRunEventController>());const replayedTargetRunKeyRef = useRef<string>();const timelineEventBatchersByThreadIdRef = useRef(new Map<string, FrameBatcher<TimelineItem>>());@@ -1227,26 +1224,6 @@ export function AppController(props: AppControllerProps) {};}, [connectionState.status, modelService]);- useEffect(() => {- let canceled = false;- const loadVersion = connectionConfigVersionRef.current;-- readHostRuntimeConfig(loadVersion, () => canceled);- const unsubscribe = hostBridge.subscribeConnectionConfig?.(config => {- if (canceled) return;- connectionConfigVersionRef.current += 1;- setConnectionConfig(config);- if (config === null) {- setConnectionState({ status: 'disconnected', message: '正在恢复本地服务连接' });- }- });-- return () => {- canceled = true;- unsubscribe?.();- };- }, [hostBridge]);-useEffect(() => {let canceled = false;@@ -1267,32 +1244,6 @@ export function AppController(props: AppControllerProps) {};}, [fileService]);- useEffect(() => {- let canceled = false;-- if (connectionService === null) {- setConnectionState({ status: 'disconnected', message: '正在等待本地服务' });- return () => {- canceled = true;- };- }-- connectionService- .check()- .then(nextState => {- if (canceled) return;- setConnectionState(nextState);- })- .catch(() => {- if (canceled) return;- setConnectionState({ status: 'disconnected', message: '本地服务连接失败' });- });-- return () => {- canceled = true;- };- }, [connectionService]);-useEffect(() => {if (connectionState.status !== 'connected' || connectionService === null || agentSetup !== 'checking') return;let canceled = false;@@ -1309,37 +1260,6 @@ export function AppController(props: AppControllerProps) {return () => { canceled = true; };}, [agentSetup, connectionService, connectionState.status]);- const availabilityProbeStatus = connectionState.status === 'connected'- ? connectionState.codexStatus.availabilityProbe?.status- : undefined;-- useEffect(() => {- if (- connectionService === null- || connectionState.status !== 'connected'- || availabilityProbeStatus !== 'pending'- ) {- return;- }- let canceled = false;- const refresh = () => {- void connectionService.check()- .then(nextState => {- if (!canceled) setConnectionState(nextState);- })- .catch(() => undefined);- };- const timer = window.setInterval(refresh, 1_500);- return () => {- canceled = true;- window.clearInterval(timer);- };- }, [- availabilityProbeStatus,- connectionService,- connectionState.status- ]);-useEffect(() => {let canceled = false;@@ -1349,11 +1269,6 @@ export function AppController(props: AppControllerProps) {|| threadService === null) {setRuntimeWorkspaceReady(false);- if (connectionState.status !== 'connected') {- setProjects([]);- setArchivedProjects([]);- setRuntimeThreads([]);- }return () => {canceled = true;};@@ -1441,10 +1356,6 @@ export function AppController(props: AppControllerProps) {}} catch {if (canceled) return;- setProjects([]);- setArchivedProjects([]);- setRuntimeThreads([]);- dispatch({ type: 'set_current_project', projectId: undefined });setProjectLoadError('无法迁移或加载项目,请稍后重试');setThreadLoadError(undefined);} finally {@@ -2326,25 +2237,6 @@ export function AppController(props: AppControllerProps) {if (mountedRef.current) setPendingRunStartsById(updated);}- function readHostRuntimeConfig(loadVersion: number, isCanceled: () => boolean) {- hostBridge- .readConnectionConfig()- .then(config => {- if (!isCanceled() && connectionConfigVersionRef.current === loadVersion) setConnectionConfig(config);- })- .catch(() => {- if (!isCanceled() && connectionConfigVersionRef.current === loadVersion) setConnectionConfig(null);- });- }-- function retryRuntimeConnection() {- connectionConfigVersionRef.current += 1;- const loadVersion = connectionConfigVersionRef.current;- setConnectionConfig(null);- setConnectionState({ status: 'disconnected', message: '正在等待本地服务' });- readHostRuntimeConfig(loadVersion, () => false);- }-function handleColorModeChange(mode: ColorMode) {setColorMode(mode);applyColorMode(mode);@@ -4794,8 +4686,14 @@ export function AppController(props: AppControllerProps) {codexStatus={connectionState.status === 'connected' ? connectionState.codexStatus : undefined}initialTab={props.route.view === 'settings' ? props.route.tab : undefined}initialSection={props.route.view === 'settings' ? props.route.section : undefined}+ componentId={props.route.view === 'settings' ? props.route.component : undefined}+ returnToTranslation={props.route.view === 'settings' && props.route.from === 'video-translation'}onBack={() => {- const returnRoute = settingsReturnRouteRef.current;+ const returnRoute = props.route.view === 'settings' && props.route.returnPath+ ? parseRoute(props.route.returnPath)+ : props.route.view === 'settings' && props.route.from === 'video-translation'+ ? { view: 'workbench' as const, tool: 'video-translation' as const }+ : settingsReturnRouteRef.current;applyRouteFromLocation(returnRoute);navigateToRoute(returnRoute);}}@@ -4837,7 +4735,7 @@ export function AppController(props: AppControllerProps) {);return (- <PageIssueRoutingProvider><div+ <RuntimeRecoveryContext.Provider value={runtimeRecovery}><PageIssueRoutingProvider><divclassName="app-drop-shell"data-integrated-title-bar={integratedTitleBar?.integratedTitleBar === true ? 'true' : undefined@@ -5005,7 +4903,7 @@ export function AppController(props: AppControllerProps) {hiddenBackgroundRuntimeIssues={immersiveWorkspace && props.route.view === 'workbench'}onAskIssue={askAgentAboutIssue}/>- </div></PageIssueRoutingProvider>+ </div></PageIssueRoutingProvider></RuntimeRecoveryContext.Provider>);function createDetailPanel() {@@ -5577,7 +5475,7 @@ function readImageInputSupported(thread?: ThreadResponse): boolean {if (connectionState.status !== 'connected') return false;- const capabilities = connectionState.codexStatus.capabilities;+ const capabilities = connectionState.codexStatus?.capabilities;if (typeof capabilities !== 'object' || capabilities === null || Array.isArray(capabilities)) {return false;}@@ -5591,7 +5489,7 @@ function readImageInputSupported(function readMcpCapabilities(connectionState: ConnectionState): McpCapabilities | undefined {if (connectionState.status !== 'connected') return undefined;- const capabilities = connectionState.codexStatus.capabilities;+ const capabilities = connectionState.codexStatus?.capabilities;if (typeof capabilities !== 'object' || capabilities === null || Array.isArray(capabilities)) {return undefined;}@@ -5867,9 +5765,9 @@ function mapRuntimeStatus(connected: true,...(appVersion === undefined ? {} : { appVersion }),runtimeVersion: '0.1.0',- codexVersion: connectionState.codexStatus.codexVersion,- codexPath: connectionState.codexStatus.codexBin,- codexHome: connectionState.codexStatus.codexHome,+ codexVersion: connectionState.codexStatus?.codexVersion,+ codexPath: connectionState.codexStatus?.codexBin,+ codexHome: connectionState.codexStatus?.codexHome,lastCheckedAt: '2026-07-07 10:00'};}diff --git a/apps/web/src/app/routes.test.ts b/apps/web/src/app/routes.test.tsindex e3887e4f..5937a4e9 100644--- a/apps/web/src/app/routes.test.ts+++ b/apps/web/src/app/routes.test.ts@@ -2,6 +2,11 @@ import { describe, expect, it } from 'vitest';import { formatRoute, parseRoute } from './routes.js';describe('app routes', () => {+ it('round-trips a component download target and a safe video-translation return route', () => {+ const route = { view: 'settings' as const, tab: 'local-components' as const, component: 'whisperkit' as const, from: 'video-translation' as const, returnPath: '#/workbench?tool=video-translation&jobId=job_1' };+ expect(parseRoute(formatRoute(route))).toEqual(route);+ expect(parseRoute('#/settings?tab=local-components&returnPath=https%3A%2F%2Fexample.com')).toEqual({ view: 'settings', tab: 'local-components' });+ });it('decodes thread route ids', () => {expect(parseRoute('#/thread/thread%201')).toEqual({ view: 'thread', threadId: 'thread 1' });});diff --git a/apps/web/src/app/routes.ts b/apps/web/src/app/routes.tsindex 919ff277..983538d7 100644--- a/apps/web/src/app/routes.ts+++ b/apps/web/src/app/routes.ts@@ -27,6 +27,9 @@ export type AppRoute =view: 'settings';tab?: SettingsRouteTab;section?: AiServicesSection;+ component?: 'whisperkit' | 'whisper.cpp' | 'faster-whisper';+ from?: 'video-translation';+ returnPath?: string;}| { view: 'files'; threadId?: string; path?: string };@@ -87,6 +90,8 @@ export function parseRoute(hash: string): AppRoute {const params = new URLSearchParams(query);const tab = params.get('tab');const section = parseAiServicesSection(params.get('section'));+ const component = params.get('component');+ const returnPath = params.get('returnPath');if (tab === 'codex-agent') {return { view: 'settings', tab: 'ai-services', section: 'text' };}@@ -97,7 +102,10 @@ export function parseRoute(hash: string): AppRoute {: tab === 'ai-services' || section !== undefined? { tab: 'ai-services' as const }: {}),- ...(section === undefined ? {} : { section })+ ...(section === undefined ? {} : { section }),+ ...(component === 'whisperkit' || component === 'whisper.cpp' || component === 'faster-whisper' ? { component } : {}),+ ...(params.get('from') === 'video-translation' ? { from: 'video-translation' as const } : {}),+ ...(returnPath && /^#\/(?:workbench|dashboard)(?:\?|$)/.test(returnPath) && returnPath.length < 2000 ? { returnPath } : {})};}if (path === '#/files') {@@ -158,6 +166,9 @@ export function formatRoute(route: AppRoute): string {if (route.tab === undefined) return '#/settings';const query = new URLSearchParams({ tab: route.tab });if (route.section !== undefined) query.set('section', route.section);+ if (route.component !== undefined) query.set('component', route.component);+ if (route.from !== undefined) query.set('from', route.from);+ if (route.returnPath !== undefined) query.set('returnPath', route.returnPath);return `#/settings?${query.toString()}`;}case 'files': {diff --git a/apps/web/src/app/use-runtime-dependencies.test.tsx b/apps/web/src/app/use-runtime-dependencies.test.tsxindex f3340ed3..51eae938 100644--- a/apps/web/src/app/use-runtime-dependencies.test.tsx+++ b/apps/web/src/app/use-runtime-dependencies.test.tsx@@ -19,6 +19,7 @@ describe('useRuntimeDependencies', () => {})}));const service = {+ getComponentsStatus: vi.fn(), downloadComponents: vi.fn(),getYtDlpStatus,checkYtDlpUpdate,updateYtDlp: vi.fn()@@ -44,6 +45,7 @@ describe('useRuntimeDependencies', () => {})}));const service = {+ getComponentsStatus: vi.fn(), downloadComponents: vi.fn(),getYtDlpStatus: vi.fn(async () => ({ ytDlp: status() })),checkYtDlpUpdate: vi.fn(),updateYtDlp@@ -72,6 +74,7 @@ describe('useRuntimeDependencies', () => {});});const service = {+ getComponentsStatus: vi.fn(), downloadComponents: vi.fn(),getYtDlpStatus: vi.fn(async () => ({ ytDlp: status() })),checkYtDlpUpdate: vi.fn(),updateYtDlpdiff --git a/apps/web/src/app/use-runtime-dependencies.ts b/apps/web/src/app/use-runtime-dependencies.tsindex 872dd0e2..4a15f94b 100644--- a/apps/web/src/app/use-runtime-dependencies.ts+++ b/apps/web/src/app/use-runtime-dependencies.ts@@ -1,5 +1,5 @@-import type { CreatorYtDlpStatus, OpenCreatorIssue } from '@opencreator/protocol';-import { useCallback, useEffect, useState } from 'react';+import type { CreatorRuntimeComponentsResponse, CreatorYtDlpStatus, OpenCreatorIssue } from '@opencreator/protocol';+import { useCallback, useEffect, useRef, useState } from 'react';import { usePageIssueState } from '../features/issues/page-issue-state.js';import type { RuntimeDependencyService } from '../services/runtime-dependency-service.js';@@ -10,6 +10,10 @@ export type RuntimeDependencyPhase =| 'updating';export type RuntimeDependenciesController = {+ componentsStatus?: CreatorRuntimeComponentsResponse;+ componentError?: string;+ refreshComponents?(): Promise<void>;+ downloadComponents?(): Promise<void>;ytDlpStatus?: CreatorYtDlpStatus;phase: RuntimeDependencyPhase;/** @deprecated Runtime failures are exposed through issues. */@@ -25,8 +29,46 @@ export function useRuntimeDependencies(input: {service: RuntimeDependencyService | null;}): RuntimeDependenciesController {const [ytDlpStatus, setYtDlpStatus] = useState<CreatorYtDlpStatus>();+ const [componentsStatus, setComponentsStatus] = useState<CreatorRuntimeComponentsResponse>();+ const [componentError, setComponentError] = useState<string>();+ const componentsRef = useRef<CreatorRuntimeComponentsResponse>();+ const componentDownloadActive = componentsStatus?.components.some(component => ['downloading', 'verifying', 'extracting'].includes(component.state)) ?? false;const [phase, setPhase] = useState<RuntimeDependencyPhase>('idle');const pageIssues = usePageIssueState('runtime');+ const refreshComponents = useCallback(async () => {+ if (!input.connected || !input.service?.getComponentsStatus) return;+ try {+ const status = await input.service.getComponentsStatus();+ componentsRef.current = status;+ setComponentsStatus(status);+ setComponentError(undefined);+ } catch (error) {+ setComponentError(error instanceof Error ? error.message : 'Unable to read local components');+ }+ }, [input.connected, input.service]);++ useEffect(() => {+ if (!input.connected) { setComponentsStatus(undefined); return; }+ let stopped = false;+ let timer: ReturnType<typeof setTimeout>;+ const poll = async () => {+ await refreshComponents();+ const busy = componentsRef.current?.components.some(component => ['downloading', 'verifying', 'extracting'].includes(component.state));+ if (!stopped) timer = setTimeout(() => { void poll(); }, busy ? 1000 : 15_000);+ };+ void poll();+ return () => { stopped = true; clearTimeout(timer); };+ }, [input.connected, refreshComponents, componentDownloadActive]);++ const downloadComponents = useCallback(async () => {+ if (!input.connected || !input.service?.downloadComponents) return;+ try {+ setComponentsStatus(await input.service.downloadComponents());+ setComponentError(undefined);+ } catch (error) {+ setComponentError(error instanceof Error ? error.message : 'Unable to download local components');+ }+ }, [input.connected, input.service]);useEffect(() => {let active = true;@@ -129,6 +171,7 @@ export function useRuntimeDependencies(input: {}, [input.connected, input.service, pageIssues.captureOperationFailure, pageIssues.resolveOperation]);return {+ componentsStatus, componentError, refreshComponents, downloadComponents,ytDlpStatus,phase,issues: pageIssues.issues,diff --git a/apps/web/src/features/dashboard/BilibiliPartSelector.tsx b/apps/web/src/features/dashboard/BilibiliPartSelector.tsxnew file mode 100644index 00000000..69f46ce3--- /dev/null+++ b/apps/web/src/features/dashboard/BilibiliPartSelector.tsx@@ -0,0 +1,38 @@+import type { VideoMetadataResponse } from '@opencreator/protocol';+import { useLocalizedCopy } from '../../i18n/useLocalizedCopy.js';++export function BilibiliPartSelector(props: {+ metadata?: VideoMetadataResponse;+ error?: string;+ onRetry?(): void;+ onSelect(index: number): void;+}) {+ const l = useLocalizedCopy();+ if (props.error) {+ return (+ <div className="video-translation-run-notice is-error" role="alert">+ <p>{props.error}</p>+ {props.onRetry ? <button className="video-translation-secondary-action" type="button" onClick={props.onRetry}>+ {l('重新读取分集', 'Retry loading parts')}+ </button> : <a href="#/settings?tab=diagnostics">{l('查看连接诊断', 'Open connection diagnostics')}</a>}+ </div>+ );+ }+ if (props.metadata === undefined) {+ return <p className="video-translation-run-notice" role="status">{l('正在读取 B 站分集信息,请稍候…', 'Loading Bilibili parts, please wait…')}</p>;+ }+ const parts = props.metadata.parts ?? [];+ if (parts.length < 2) return null;+ return (+ <div className="video-translation-bilibili-parts">+ <label className="video-translation-field">+ <span>{l('选择要翻译的分集', 'Choose a part to translate')}</span>+ <select value={props.metadata.selectedPart?.index ?? ''} onChange={event => props.onSelect(Number(event.target.value))}>+ <option value="" disabled>{l('请选择分集', 'Select a part')}</option>+ {parts.map(part => <option key={part.index} value={part.index}>P{part.index} · {part.title}</option>)}+ </select>+ </label>+ <p role="status">{l(`共 ${parts.length} 个分 P,只翻译选中的分集,不会下载整个合集。`, `${parts.length} parts. Only the selected part is translated; the entire collection will not be downloaded.`)}</p>+ </div>+ );+}diff --git a/apps/web/src/features/dashboard/CreatorCollaborationPanel.tsx b/apps/web/src/features/dashboard/CreatorCollaborationPanel.tsxindex 67efe0f9..57172ad1 100644--- a/apps/web/src/features/dashboard/CreatorCollaborationPanel.tsx+++ b/apps/web/src/features/dashboard/CreatorCollaborationPanel.tsx@@ -32,6 +32,7 @@ import {type CreatorStageProgressView} from './creator-panel-adapters.js';import { useOptionalCreatorSession } from './creator-session-store.js';+import { RuntimeRecoveryNotice, useRuntimeRecovery } from '../../runtime/runtime-recovery.js';export type CreatorPanelQuickAction = {id: string;@@ -102,6 +103,9 @@ export default function CreatorCollaborationPanel(props: {}) {const l = useLocalizedCopy();const session = useOptionalCreatorSession();+ const runtimeRecovery = useRuntimeRecovery();+ const connectionUnavailable = runtimeRecovery !== null && runtimeRecovery.state.status !== 'connected'+ || session?.connection !== undefined && session.connection.status !== 'connected';const [input, setInput] = useState('');const [sending, setSending] = useState(false);const [permission, setPermission] = useState<ToolAgentPermission>('full-access');@@ -175,7 +179,7 @@ export default function CreatorCollaborationPanel(props: {async function send(message: string) {const content = message.trim();- if (!content || session === null || sendingRef.current) return;+ if (!content || session === null || sendingRef.current || connectionUnavailable) return;sendingRef.current = true;session.clearError();setInput('');@@ -262,10 +266,11 @@ export default function CreatorCollaborationPanel(props: {</section>) : null}+ <RuntimeRecoveryNotice session={session?.connection} onRetrySession={session?.reconnect} />{session === null ? (<div className="creator-collaboration-unavailable" role="alert"><ServerOff size={16} aria-hidden="true" />- <p>{l('Creator Runtime 未连接,Agent 不会生成替代回复。', 'Creator Runtime is disconnected. The Agent will not generate substitute responses.')}</p>+ <p>{l('项目会话尚未初始化,请重新打开项目;这不代表本地服务已断开。', 'The project session is not initialized. Reopen the project; this does not mean the local Runtime is disconnected.')}</p></div>) : (<>@@ -383,6 +388,7 @@ export default function CreatorCollaborationPanel(props: {permissionDisabled={session.agentBusy}onPermissionChange={setPermission}submitting={sending}+ disabled={connectionUnavailable}ariaLabel={l('告诉 Agent 你的要求', 'Tell the Agent your requirements')}placeholder={session.agentBusy? l('补充当前任务的要求', 'Add guidance to the active task')@@ -552,6 +558,8 @@ function CollaborationStageView(props: {<span style={percent === null ? undefined : { width: `${percent}%` }} /></div>) : null}+ {stage.status === 'running' && progress.detailsHref && progress.message ? <p className="creator-collaboration-stage-message" role="status">{progress.message}</p> : null}+ {(stage.status === 'running' || stage.status === 'failed') && progress.detailsHref ? <a href={progress.detailsHref}>{l('查看组件下载详情', 'View component download details')}</a> : null}</article>);}diff --git a/apps/web/src/features/dashboard/DashboardPage.test.tsx b/apps/web/src/features/dashboard/DashboardPage.test.tsxindex d7bcc05d..98b45403 100644--- a/apps/web/src/features/dashboard/DashboardPage.test.tsx+++ b/apps/web/src/features/dashboard/DashboardPage.test.tsx@@ -34,6 +34,23 @@ function openBlankWorkspace(moduleName: string, _blankName: string) {}));}+describe('Creator initial session recovery', () => {+ it('automatically opens an existing project after the first snapshot fails', async () => {+ const service = createInMemoryCreatorService();+ const { job } = await service.createJob({ projectId: 'project_1', templateId: 'video-translation' });+ const getJob = vi.spyOn(service, 'getJob');+ getJob.mockRejectedValueOnce(new TypeError('offline'));+ const applyAction = vi.spyOn(service, 'applyAction');+ const runAgentTurn = vi.spyOn(service, 'runAgentTurn');+ render(<DashboardPage onSelectPrompt={vi.fn()} workspace="video-translation" jobId={job.id} creatorService={service} />);+ expect(await screen.findByText('正在恢复项目会话与任务状态…')).toBeVisible();+ expect(await screen.findByRole('heading', { name: '视频翻译配音' })).toBeVisible();+ expect(getJob).toHaveBeenCalledTimes(2);+ expect(applyAction).not.toHaveBeenCalled();+ expect(runAgentTurn).not.toHaveBeenCalled();+ });+});+function createInMemoryCreatorService(): CreatorWebService {const jobs = new Map<string, CreatorJob>();let sequence = 0;@@ -876,6 +893,61 @@ describe('DashboardPage', () => {expect(container.querySelector('.wechat-layout-list')).not.toBeInTheDocument();});+ it('requires a Bilibili part selection and persists it without duplicate metadata requests', async () => {+ const creatorService = createInMemoryCreatorService();+ const applyAction = vi.spyOn(creatorService, 'applyAction');+ const parts = [1, 2, 3].map(index => ({ index, title: `课程 ${index}`, cid: 1000 + index, width: 1920, height: 1080 }));+ const getVideoMetadata = vi.fn(async () => ({ platform: 'bilibili' as const, title: '课程合集', parts }));+ render(<DashboardPage onSelectPrompt={vi.fn()} workspace="video-translation" creatorService={creatorService}+ videoMetadataService={{ getVideoMetadata }} />);+ fireEvent.change(screen.getByRole('textbox', { name: '视频链接' }), {+ target: { value: 'https://www.bilibili.com/video/BV18E421w7bf/?spm_id_from=share' }+ });+ expect(screen.getByText('正在读取 B 站分集信息,请稍候…')).toBeInTheDocument();+ const selector = await screen.findByRole('combobox', { name: '选择要翻译的分集' });+ expect(selector).toHaveValue('');+ expect(screen.queryByTitle('Bilibili 视频预览')).not.toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: '继续' }));+ expect(screen.getByText('请确认链接有效,并选择要翻译的分集后继续')).toBeInTheDocument();+ fireEvent.change(selector, { target: { value: '3' } });+ expect(new URL(screen.getByTitle('Bilibili 视频预览').getAttribute('src')!).searchParams.get('p')).toBe('3');+ expect(screen.getByText('课程合集 · P3 课程 3')).toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: '继续' }));+ await waitFor(() => expect(applyAction).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({+ action: 'update-settings', input: expect.objectContaining({ patch: expect.objectContaining({ sourceUrl: 'https://www.bilibili.com/video/BV18E421w7bf?p=3' }) })+ })));+ expect(getVideoMetadata).toHaveBeenCalledTimes(1);+ });++ it.each([1, 2, 3])('preselects an explicit Bilibili P%s link', async index => {+ const parts = [1, 2, 3].map(index => ({ index, title: `课程 ${index}` }));+ render(<DashboardPage onSelectPrompt={vi.fn()} workspace="video-translation"+ videoMetadataService={{ getVideoMetadata: vi.fn(async () => ({ platform: 'bilibili' as const, title: '课程合集', parts, selectedPart: parts[index - 1]! })) }} />);+ fireEvent.change(screen.getByRole('textbox', { name: '视频链接' }), {+ target: { value: `https://www.bilibili.com/video/BV18E421w7bf?p=${index}` }+ });+ expect(await screen.findByRole('combobox', { name: '选择要翻译的分集' })).toHaveValue(String(index));+ expect(new URL(screen.getByTitle('Bilibili 视频预览').getAttribute('src')!).searchParams.get('p')).toBe(String(index));+ fireEvent.click(screen.getByRole('button', { name: '继续' }));+ expect(screen.queryByRole('combobox', { name: '选择要翻译的分集' })).not.toBeInTheDocument();+ });++ it('shows a recoverable Bilibili lookup error rather than silently translating P1', async () => {+ const getVideoMetadata = vi.fn()+ .mockRejectedValueOnce(new Error('无法读取 B 站分集信息,请重试'))+ .mockResolvedValueOnce({ platform: 'bilibili', title: '课程合集', parts: [{ index: 1, title: '课程 1' }, { index: 2, title: '课程 2' }] });+ render(<DashboardPage onSelectPrompt={vi.fn()} workspace="video-translation" videoMetadataService={{ getVideoMetadata }} />);+ fireEvent.change(screen.getByRole('textbox', { name: '视频链接' }), {+ target: { value: 'https://www.bilibili.com/video/BV18E421w7bf' }+ });+ expect(await screen.findByText('无法读取 B 站分集信息,请重试')).toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: '继续' }));+ expect(screen.getByText('请确认链接有效,并选择要翻译的分集后继续')).toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: '重新读取分集' }));+ expect(await screen.findByRole('combobox', { name: '选择要翻译的分集' })).toHaveValue('');+ expect(getVideoMetadata).toHaveBeenCalledTimes(2);+ });+it('opens the video translation workspace and keeps its result in place', async () => {const onSelectPrompt = vi.fn();render(diff --git a/apps/web/src/features/dashboard/DashboardPage.tsx b/apps/web/src/features/dashboard/DashboardPage.tsxindex 12acddc6..9f2964d2 100644--- a/apps/web/src/features/dashboard/DashboardPage.tsx+++ b/apps/web/src/features/dashboard/DashboardPage.tsx@@ -35,6 +35,8 @@ import VideoTranslationWorkspace from './VideoTranslationWorkspace.js';import VideoGenerationWorkspace from './VideoGenerationWorkspace.js';import type { CreatorWebService } from '../../services/creator-service.js';import { ApiClientError } from '../../runtime/errors.js';+import { createCreatorSnapshotSubscription, type CreatorConnectionState } from '../../runtime/creator-sse.js';+import { RuntimeRecoveryNotice } from '../../runtime/runtime-recovery.js';import type { RuntimeDependenciesController } from '../../app/use-runtime-dependencies.js';import { CreatorSessionProvider } from './creator-session-store.js';import type {@@ -202,7 +204,6 @@ const creatorTools: DashboardEntry[] = [const categories = ['全部', '视频创作', '视频编辑', '图像创作', '文案创作', '音频处理'] as const;type CategoryFilter = typeof categories[number];-const CREATOR_JOB_LOAD_TIMEOUT_MS = 15_000;const CREATOR_JOB_CREATE_ATTEMPT_TIMEOUT_MS = 4_000;const CREATOR_JOB_CREATE_ATTEMPTS = 3;const CREATOR_JOB_CREATE_RECOVERY_TIMEOUT_MS = 30_000;@@ -347,6 +348,7 @@ export default function DashboardPage(props: {if (activeWorkspace === 'video-translation') {return renderCreatorWorkspace('video-translation', (<VideoTranslationWorkspace+ runtimeDependencies={props.runtimeDependencies}promptHint={activePromptHint}videoMetadataService={props.videoMetadataService}creatorServicesService={props.creatorServicesService}@@ -618,6 +620,7 @@ function CreatorWorkspaceSession(props: {? createPendingCreatorJob(props.projectId, props.templateId, props.templateVersion): undefined);const [loadAttempt, setLoadAttempt] = useState(0);+ const [restoreConnection, setRestoreConnection] = useState<CreatorConnectionState>({ status: 'connecting', attempt: 0 });const pageIssues = usePageIssueState('creator-launch');const jobRef = useRef(job);const mountedRef = useRef(false);@@ -748,41 +751,40 @@ function CreatorWorkspaceSession(props: {}if (props.jobId !== createdJobIdRef.current) createdJobIdRef.current = undefined;- const request = props.service.getJob(props.jobId).then(response => response.job);+ if (jobRef.current?.id === props.jobId) return;+ const jobId = props.jobId;setJob(current => current?.id === props.jobId ? current : undefined);- const timeout = window.setTimeout(() => {- if (canceled) return;- pageIssues.captureOperationFailure(- 'creator-launch.restore-job',- new Error('creator_job_restore_timeout'),- l('恢复创作项目超时,请重试。', 'Restoring the creator project timed out. Try again.'),- { retryable: true }- );- }, CREATOR_JOB_LOAD_TIMEOUT_MS);- void request.then(next => {- if (next.projectId !== props.projectId) {- throw new Error('该创作项目不属于当前工作目录');- }- if (next.templateId !== props.templateId) {- throw new Error('该创作项目与当前模板不匹配');+ const subscription = createCreatorSnapshotSubscription<CreatorJob>({+ loadSnapshot: async options => {+ const next = (await props.service.getJob(jobId, options)).job;+ if (next.projectId !== props.projectId || next.templateId !== props.templateId) {+ throw new ApiClientError({ status: 409, code: 'CREATOR_SESSION_MISMATCH', message: '该创作项目与当前工作目录或模板不匹配' });+ }+ return next;+ },+ subscribe: () => ({ close() {} }),+ onSnapshot(next) {+ if (canceled) return;+ pageIssues.resolveOperation('creator-launch.restore-job');+ setJob(next);+ subscription.close();+ },+ onState(next) {+ if (canceled) return;+ setRestoreConnection(next);+ if (next.status === 'failed') {+ pageIssues.captureOperationFailure(+ 'creator-launch.restore-job', next.error,+ l('无法恢复创作项目,请重试。', 'Could not restore the creator project. Try again.'),+ { retryable: true }+ );+ }}- if (canceled) return;- window.clearTimeout(timeout);- pageIssues.resolveOperation('creator-launch.restore-job');- setJob(next);- }).catch(reason => {- if (canceled) return;- window.clearTimeout(timeout);- pageIssues.captureOperationFailure(- 'creator-launch.restore-job',- reason,- l('无法恢复创作项目,请重试。', 'Could not restore the creator project. Try again.'),- { retryable: true }- );});+ void subscription.start();return () => {canceled = true;- window.clearTimeout(timeout);+ subscription.close();};}, [l,@@ -800,6 +802,7 @@ function CreatorWorkspaceSession(props: {if (restoreIssue) {return (<main className="creator-workspace-loading">+ <RuntimeRecoveryNotice session={restoreConnection} onRetrySession={async () => setLoadAttempt(attempt => attempt + 1)} /><IssueListissues={pageIssues.issues}actions={{ retryOperations: {@@ -817,6 +820,7 @@ function CreatorWorkspaceSession(props: {return (<main className="creator-workspace-loading" aria-busy="true">{l('正在恢复创作项目', 'Restoring creator project')}+ <RuntimeRecoveryNotice session={restoreConnection} onRetrySession={async () => setLoadAttempt(attempt => attempt + 1)} /></main>);}diff --git a/apps/web/src/features/dashboard/LocalTranscriptionNotice.tsx b/apps/web/src/features/dashboard/LocalTranscriptionNotice.tsxnew file mode 100644index 00000000..41b9d47f--- /dev/null+++ b/apps/web/src/features/dashboard/LocalTranscriptionNotice.tsx@@ -0,0 +1,38 @@+import { useEffect } from 'react';+import type { RuntimeDependenciesController } from '../../app/use-runtime-dependencies.js';+import { useLocalizedCopy } from '../../i18n/useLocalizedCopy.js';+import { componentProgressText } from '../settings/LocalTranscriptionComponents.js';++export function LocalTranscriptionNotice({ controller, platformCaptions, importedSubtitle, beforeNavigate }: {+ controller?: RuntimeDependenciesController;+ platformCaptions: boolean;+ importedSubtitle: boolean;+ beforeNavigate(): void;+}) {+ const localize = useLocalizedCopy();+ useEffect(() => { void controller?.refreshComponents?.(); }, [controller?.refreshComponents]);+ if (!controller) return null;+ const status = controller.componentsStatus;+ if (!status) return controller.componentError ? <aside className="local-transcription-notice" role="status">{localize('无法读取本地转录组件状态', 'Cannot read local transcription component status')} <button type="button" onClick={() => void controller.refreshComponents?.()}>{localize('重试', 'Retry')}</button></aside> : null;+ const component = status.components.find(candidate => candidate.id === status.selectedProvider);+ if (!component) return null;+ const busy = ['downloading', 'verifying', 'extracting'].includes(component.state);+ const ready = component.state === 'ready';+ const query = new URLSearchParams({ tab: 'local-components', component: component.id, from: 'video-translation', returnPath: window.location.hash });+ const settingsQuery = new URLSearchParams(query);+ settingsQuery.set('tab', 'ai-services');+ settingsQuery.set('section', 'transcription');+ return <aside className="local-transcription-notice" data-state={component.state} role="status">+ <div>+ <strong>{!component.available ? localize('当前平台不支持此本地转录组件', 'This local transcription component is unavailable on this platform') : busy ? localize('正在准备本地转录组件', 'Preparing local transcription components') : ready ? localize('本地转录已就绪', 'Local transcription ready') : component.state === 'failed' ? localize('本地转录组件下载失败', 'Local transcription component download failed') : localize('本地转录组件尚未就绪', 'Local transcription components are not ready')}</strong>+ <small>{component.name} · {status.selectedModel}</small>+ {!ready || importedSubtitle ? <p>{importedSubtitle ? localize('当前任务使用导入字幕,无需本地语音转录。', 'This task uses imported subtitles and does not need transcription.') : platformCaptions ? localize('将优先使用视频原始字幕;没有可用字幕时才需要本地转录。可提前下载组件,避免任务中等待。', 'Original video captions are used first. Local transcription is needed only when captions are unavailable; prepare components now to avoid waiting during the task.') : localize('此任务需要本地语音转录。模型文件较大,首次执行会自动下载,也可提前准备;下载并校验完成后任务会自动继续。', 'This task requires local transcription. Large models are downloaded automatically when needed, or can be prepared in advance. The task continues automatically after verification.')}</p> : null}+ {busy ? <p>{component.item} {component.state === 'downloading' ? componentProgressText(component) : localize('正在校验或安装,请稍候', 'Verifying or installing; please wait')}</p> : null}+ {component.error ? <p>{component.error}</p> : null}+ </div>+ <div className="local-transcription-notice-actions">+ {component.available ? <a href={`#/settings?${query}`} onClick={beforeNavigate}>{ready ? localize('管理组件', 'Manage components') : busy ? localize('查看下载进度', 'View download progress') : component.state === 'failed' ? localize('前往重试', 'Retry download') : localize('前往组件下载', 'Go to component downloads')}</a> : null}+ <a href={`#/settings?${settingsQuery}`} onClick={beforeNavigate}>{localize('调整转录设置', 'Change transcription settings')}</a>+ </div>+ </aside>;+}diff --git a/apps/web/src/features/dashboard/ToolAgentComposer.tsx b/apps/web/src/features/dashboard/ToolAgentComposer.tsxindex e567a070..77628be4 100644--- a/apps/web/src/features/dashboard/ToolAgentComposer.tsx+++ b/apps/web/src/features/dashboard/ToolAgentComposer.tsx@@ -26,6 +26,7 @@ export default function ToolAgentComposer(props: {showPermission?: boolean;showModel?: boolean;submitting?: boolean;+ disabled?: boolean;permission?: ToolAgentPermission;permissionDisabled?: boolean;onChange(value: string): void;@@ -61,7 +62,7 @@ export default function ToolAgentComposer(props: {function submit(event: FormEvent) {event.preventDefault();- if (!props.value.trim()) return;+ if (props.disabled || props.submitting || !props.value.trim()) return;props.onSubmit();setAttachments([]);setOpenMenu(null);@@ -228,7 +229,7 @@ export default function ToolAgentComposer(props: {<buttonclassName="tool-agent-composer-send"type="submit"- disabled={props.submitting || !props.value.trim()}+ disabled={props.disabled || props.submitting || !props.value.trim()}aria-busy={props.submitting || undefined}aria-label={l('发送给 Agent', 'Send to Agent')}>diff --git a/apps/web/src/features/dashboard/VideoSourceInput.tsx b/apps/web/src/features/dashboard/VideoSourceInput.tsxindex 75b23ecc..aa1614a3 100644--- a/apps/web/src/features/dashboard/VideoSourceInput.tsx+++ b/apps/web/src/features/dashboard/VideoSourceInput.tsx@@ -13,6 +13,8 @@ export default function VideoSourceInput(props: {hasSource: boolean;invalid?: boolean;metadataService?: VideoMetadataService;+ metadata?: VideoMetadataResponse;+ previewEnabled?: boolean;onFileChange(file: File | null): void;onUrlChange(url: string): void;onClear(): void;@@ -65,6 +67,8 @@ export default function VideoSourceInput(props: {sourceType={props.sourceType}url={props.url}metadataService={props.metadataService}+ metadata={props.metadata}+ previewEnabled={props.previewEnabled}onChooseFile={openFilePicker}onClear={props.onClear}onDimensions={props.onDimensions}diff --git a/apps/web/src/features/dashboard/VideoSourcePreview.test.tsx b/apps/web/src/features/dashboard/VideoSourcePreview.test.tsxindex 287cb0f5..9ff96e0c 100644--- a/apps/web/src/features/dashboard/VideoSourcePreview.test.tsx+++ b/apps/web/src/features/dashboard/VideoSourcePreview.test.tsx@@ -167,6 +167,23 @@ describe('VideoSourcePreview', () => {);});+ it('previews the selected Bilibili part instead of always P1', () => {+ render(<VideoSourcePreview file={null} sourceType="url"+ url="https://www.bilibili.com/video/BV18E421w7bf/?spm_id_from=share&p=3"+ onChooseFile={vi.fn()} onClear={vi.fn()} />);+ const player = new URL(screen.getByTitle('Bilibili 视频预览').getAttribute('src')!);+ expect(player.searchParams.get('p')).toBe('3');+ expect(player.searchParams.has('page')).toBe(false);+ });++ it('does not show a misleading P1 preview when a multipart source needs a selection', () => {+ render(<VideoSourcePreview file={null} sourceType="url"+ url="https://www.bilibili.com/video/BV18E421w7bf" onChooseFile={vi.fn()} onClear={vi.fn()}+ metadata={{ platform: 'bilibili', title: '课程', parts: [{ index: 1, title: '第一课' }, { index: 2, title: '第二课' }] }} />);+ expect(screen.queryByTitle('Bilibili 视频预览')).not.toBeInTheDocument();+ expect(screen.getByText('选择分集后预览视频')).toBeInTheDocument();+ });+it('keeps the original platform link available when embedded playback fails', () => {render(<VideoSourcePreviewdiff --git a/apps/web/src/features/dashboard/VideoSourcePreview.tsx b/apps/web/src/features/dashboard/VideoSourcePreview.tsxindex 8a8e1433..597ddbbc 100644--- a/apps/web/src/features/dashboard/VideoSourcePreview.tsx+++ b/apps/web/src/features/dashboard/VideoSourcePreview.tsx@@ -1,12 +1,12 @@import { useEffect, useMemo, useState } from 'react';import { ExternalLink, FileVideo, Link2, Play, RotateCcw, Trash2 } from 'lucide-react';import { useLocalizedCopy } from '../../i18n/useLocalizedCopy.js';-import type { VideoMetadataResponse } from '@opencreator/protocol';+import { parseBilibiliVideoSource, type VideoMetadataResponse } from '@opencreator/protocol';import type { VideoMetadataService } from '../../services/video-metadata-service.js';type VideoSource =| { kind: 'youtube'; embedUrl: string; thumbnailUrl: string; url: string; label: string }- | { kind: 'bilibili'; embedUrl: string; url: string; label: string }+ | { kind: 'bilibili'; embedUrl: string; url: string; label: string; partIndex?: number }| { kind: 'direct'; url: string; label: string }| { kind: 'link'; url: string; hostname: string; label: string }| { kind: 'invalid'; label: string };@@ -43,18 +43,21 @@ export function parseVideoSource(value: string): VideoSource {}if (hostname === 'bilibili.com' || hostname.endsWith('.bilibili.com')) {- const videoId = url.pathname.match(/\/(BV[\w]+|av\d+)(?:[/?#]|$)/i)?.[1];- if (videoId) {+ const bilibili = parseBilibiliVideoSource(trimmed);+ if (bilibili) {+ const { videoId, partIndex } = bilibili;const idParam = videoId.toLowerCase().startsWith('av')? `aid=${encodeURIComponent(videoId.slice(2))}`: `bvid=${encodeURIComponent(videoId)}`;return {kind: 'bilibili',- embedUrl: `https://player.bilibili.com/player.html?${idParam}&page=1&high_quality=1&danmaku=0`,- url: url.toString(),- label: 'Bilibili 视频'+ embedUrl: `https://player.bilibili.com/player.html?${idParam}&p=${partIndex ?? 1}&high_quality=1&danmaku=0`,+ url: bilibili.url,+ label: 'Bilibili 视频',+ ...(partIndex === undefined ? {} : { partIndex })};}+ return { kind: 'invalid', label: trimmed };}if (/\.(?:mp4|webm|mov|m4v)(?:$|[?#])/i.test(`${url.pathname}${url.search}${url.hash}`)) {@@ -122,12 +125,15 @@ export default function VideoSourcePreview(props: {displayLabel?: string;displayDetail?: string;metadataService?: VideoMetadataService;+ metadata?: VideoMetadataResponse;+ previewEnabled?: boolean;onDimensions?(width: number, height: number): void;onMetadata?(url: string, metadata: VideoMetadataResponse): void;}) {const l = useLocalizedCopy();const [showYouTubePlayer, setShowYouTubePlayer] = useState(false);- const [metadata, setMetadata] = useState<VideoMetadataResponse>();+ const [fetchedMetadata, setFetchedMetadata] = useState<{ url: string; value: VideoMetadataResponse }>();+ const metadata = props.metadata ?? (fetchedMetadata?.url === props.url ? fetchedMetadata.value : undefined);const [mediaDimensions, setMediaDimensions] = useState<{ width: number; height: number }>();const source = useMemo(() => parseVideoSource(props.url), [props.url]);const localFile = props.sourceType === 'file' ? props.file : null;@@ -143,7 +149,9 @@ export default function VideoSourcePreview(props: {? l('视频链接', 'Video link'): source.label;const sourceLabel = props.displayLabel ?? (- localFile?.name ?? registeredFile?.name ?? metadata?.title ?? localizedLabel+ localFile?.name ?? registeredFile?.name ?? (metadata?.selectedPart && (metadata.parts?.length ?? 0) > 1+ ? `${metadata.title} · P${metadata.selectedPart.index} ${metadata.selectedPart.title}`+ : metadata?.title) ?? localizedLabel);const sourceDetail = props.displayDetail ?? (localFile? `${localFile.type.startsWith('audio/') ? l('本地音频', 'Local audio') : l('本地视频', 'Local video')} · ${formatFileSize(localFile.size)}`@@ -173,11 +181,10 @@ export default function VideoSourcePreview(props: {useEffect(() => {let active = true;- setMetadata(undefined);if ((source.kind !== 'youtube' && source.kind !== 'bilibili') || props.metadataService === undefined) return undefined;void props.metadataService.getVideoMetadata(props.url).then(result => {if (!active) return;- setMetadata(result);+ setFetchedMetadata({ url: props.url, value: result });props.onMetadata?.(props.url, result);if (result.width !== undefined && result.height !== undefined) {updateMediaDimensions(result.width, result.height);@@ -188,6 +195,15 @@ export default function VideoSourcePreview(props: {};}, [props.metadataService, props.url, source.kind]);+ useEffect(() => {+ if (props.metadata?.width !== undefined && props.metadata.height !== undefined) {+ updateMediaDimensions(props.metadata.width, props.metadata.height);+ }+ }, [props.metadata]);++ const needsPartSelection = source.kind === 'bilibili'+ && (props.previewEnabled === false || ((metadata?.parts?.length ?? 0) > 1 && metadata?.selectedPart === undefined));+return (<divclassName="video-source-preview"@@ -218,7 +234,14 @@ export default function VideoSourcePreview(props: {<span aria-hidden="true"><Play size={24} fill="currentColor" /></span></button>) : null}- {!isLocal && ((source.kind === 'youtube' && showYouTubePlayer) || source.kind === 'bilibili') ? (+ {!isLocal && needsPartSelection ? (+ <div className="video-source-link-preview">+ <strong>{metadata === undefined+ ? l('正在确认视频分集', 'Checking the selected video part')+ : l('选择分集后预览视频', 'Select a part to preview the video')}</strong>+ </div>+ ) : null}+ {!isLocal && ((source.kind === 'youtube' && showYouTubePlayer) || (source.kind === 'bilibili' && !needsPartSelection)) ? (<iframesrc={source.embedUrl}title={l(`${localizedLabel}预览`, `${localizedLabel} preview`)}diff --git a/apps/web/src/features/dashboard/VideoTranslationAgentPanel.test.tsx b/apps/web/src/features/dashboard/VideoTranslationAgentPanel.test.tsxindex 54d7535d..ae6cb9b5 100644--- a/apps/web/src/features/dashboard/VideoTranslationAgentPanel.test.tsx+++ b/apps/web/src/features/dashboard/VideoTranslationAgentPanel.test.tsx@@ -6,6 +6,23 @@ import { CreatorSessionProvider } from './creator-session-store.js';import VideoTranslationAgentPanel from './VideoTranslationAgentPanel.js';describe('VideoTranslationAgentPanel', () => {+ it('明确显示长时间模型下载的原因、真实进度和自动继续提示', async () => {+ const current = job();+ current.status = 'running';+ current.stages.push({+ id: 'component-download', jobId: current.id, stageId: 'subtitle', executor: 'krillinai', status: 'running', dispatchStatus: 'claimed',+ claimOwner: 'scheduler', claimExpiresAt: null, attempt: 1, idempotencyKey: 'component-download', scopeKey: null, inputFingerprint: null,+ progress: { phase: 'downloading_dependencies', percent: 2, dependencyPercent: 25, downloadedBytes: 512 * 1024 ** 2, totalBytes: 2 * 1024 ** 3,+ message: '未找到可用的原始字幕,正在下载本地模型。模型文件较大,下载并校验完成后将自动继续,无需重新开始。' },+ errorCode: null, errorMessage: null, startedAt: '2026-10-01T00:00:00.000Z', finishedAt: null+ });+ renderPanel({ initialJob: current, getAgentTimeline: vi.fn(async () => emptyTimeline()) });+ expect(await screen.findByText('下载本地转录组件(尚未开始转录)')).toBeInTheDocument();+ expect(screen.getByText(/下载并校验完成后将自动继续/)).toBeInTheDocument();+ expect(screen.getByText(/512.0 MiB \/ 2.00 GiB/)).toBeInTheDocument();+ expect(screen.getByRole('progressbar')).toHaveAttribute('aria-valuenow', '25');+ expect(screen.getByRole('link', { name: '查看组件下载详情' })).toHaveAttribute('href', expect.stringContaining('returnPath='));+ });it('只展示标准对话和协作事件,不泄露内部工具上下文', async () => {const { container } = renderPanel({getAgentTimeline: vi.fn(async () => timeline())diff --git a/apps/web/src/features/dashboard/VideoTranslationWorkspace.tsx b/apps/web/src/features/dashboard/VideoTranslationWorkspace.tsxindex 5b30fbfe..9752f3b1 100644--- a/apps/web/src/features/dashboard/VideoTranslationWorkspace.tsx+++ b/apps/web/src/features/dashboard/VideoTranslationWorkspace.tsx@@ -21,6 +21,8 @@ import {Square} from 'lucide-react';import { TtsVoicePicker } from '../../components/tts/TtsVoicePicker.js';+import type { RuntimeDependenciesController } from '../../app/use-runtime-dependencies.js';+import { LocalTranscriptionNotice } from './LocalTranscriptionNotice.js';import { useLocalizedCopy, type LocalizeCopy } from '../../i18n/useLocalizedCopy.js';import type { CreatorServicesSettingsService } from '../../services/creator-services-service.js';import type { VideoMetadataService } from '../../services/video-metadata-service.js';@@ -28,6 +30,7 @@ import VideoTranslationAgentPanel from './VideoTranslationAgentPanel.js';import CreatorResizableLayout from './CreatorResizableLayout.js';import CreatorTaskSummary from './CreatorTaskSummary.js';import VideoSourceInput from './VideoSourceInput.js';+import { BilibiliPartSelector } from './BilibiliPartSelector.js';import { parseVideoSource } from './VideoSourcePreview.js';import { VideoTranslationSubtitleImport } from './VideoTranslationSubtitleImport.js';import VideoTranslationResultWorkspace, {@@ -46,6 +49,7 @@ import {} from './creator-session-store.js';import {readCreatorResultSnapshots,+ parseBilibiliVideoSource,type CreatorArtifact,type CreatorJson,type CreatorResultSnapshot,@@ -146,6 +150,7 @@ type LanguageOption = {};const steps = ['添加视频', '翻译设置', '字幕样式', '配音与输出'] as const;+let componentNavigationDraft: { jobId: string | null; settings: TranslationSettingsSnapshot; source: TranslationSourceSnapshot; currentStep: WizardStep; furthestStep: WizardStep; workspacePhase: WorkspacePhase } | undefined;const subtitleColors = ['#FFFFFF', '#FFE45C', '#7EE7FF', '#A7F3D0'] as const;const defaultSubtitleStyle: SubtitleStyleSettings = {subtitleFont: 'sans',@@ -1483,6 +1488,7 @@ export default function VideoTranslationWorkspace(props: {promptHint?: string;videoMetadataService?: VideoMetadataService;creatorServicesService?: CreatorServicesSettingsService | null;+ runtimeDependencies?: RuntimeDependenciesController;}) {const l = useLocalizedCopy();const creatorSession = useOptionalCreatorSession();@@ -1502,6 +1508,8 @@ export default function VideoTranslationWorkspace(props: {const [videoFile, setVideoFile] = useState<File | null>(null);const [sourceDimensions, setSourceDimensions] = useState<{ width: number; height: number }>();const [urlMetadata, setUrlMetadata] = useState<{ url: string; value: VideoMetadataResponse }>();+ const [bilibiliMetadataError, setBilibiliMetadataError] = useState<{ url: string; message: string }>();+ const [metadataRetry, setMetadataRetry] = useState(0);const [localPreviewUrl, setLocalPreviewUrl] = useState<{ file: File; url: string }>();const [videoFileName, setVideoFileName] = useState<string | null>(null);const [videoFileSize, setVideoFileSize] = useState<number | null>(null);@@ -1553,7 +1561,10 @@ export default function VideoTranslationWorkspace(props: {const [agentFocus, setAgentFocus] = useState<AgentFocus>();const cancelConfirmRef = useRef<HTMLButtonElement>(null);const parsedVideoSource = useMemo(() => parseVideoSource(videoUrl), [videoUrl]);- const currentUrlMetadata = urlMetadata?.url === videoUrl ? urlMetadata.value : undefined;+ const currentUrlMetadata = sourceType === 'url' && urlMetadata?.url === videoUrl ? urlMetadata.value : undefined;+ const sourceSelectionReady = sourceType !== 'url' || (parsedVideoSource.kind !== 'invalid'+ && (parsedVideoSource.kind !== 'bilibili' || (currentUrlMetadata !== undefined+ && ((currentUrlMetadata.parts?.length ?? 0) < 2 || currentUrlMetadata.selectedPart !== undefined))));useEffect(() => {if (workspacePhase !== 'configure' || currentStep !== 2 || sourceType !== 'file'@@ -1568,19 +1579,29 @@ export default function VideoTranslationWorkspace(props: {}, [workspacePhase, currentStep, sourceType, videoFile]);useEffect(() => {- if (currentStep === 0 || sourceType !== 'url' || !isValidVideoUrl(videoUrl)+ if (sourceType !== 'url' || !isValidVideoUrl(videoUrl)|| props.videoMetadataService === undefined || currentUrlMetadata !== undefined+ || (currentStep === 0 && parsedVideoSource.kind !== 'bilibili')|| (parsedVideoSource.kind !== 'youtube' && parsedVideoSource.kind !== 'bilibili')) return;let canceled = false;- void props.videoMetadataService.getVideoMetadata(videoUrl).then(metadata => {- if (canceled) return;- setUrlMetadata({ url: videoUrl, value: metadata });- if (metadata.width !== undefined && metadata.height !== undefined) {- updateSourceOrientation(metadata.width, metadata.height);- }- }).catch(() => undefined);- return () => { canceled = true; };- }, [currentStep, sourceType, videoUrl, props.videoMetadataService, currentUrlMetadata, parsedVideoSource.kind]);+ if (parsedVideoSource.kind === 'bilibili') setBilibiliMetadataError(undefined);+ const timeout = setTimeout(() => {+ void props.videoMetadataService!.getVideoMetadata(videoUrl).then(metadata => {+ if (canceled) return;+ setUrlMetadata({ url: videoUrl, value: metadata });+ setBilibiliMetadataError(undefined);+ if (metadata.width !== undefined && metadata.height !== undefined) {+ updateSourceOrientation(metadata.width, metadata.height);+ }+ }).catch(error => {+ if (!canceled && parsedVideoSource.kind === 'bilibili') {+ setBilibiliMetadataError({ url: videoUrl, message: error instanceof Error+ ? error.message : l('无法读取 B 站分集信息,请重试', 'Could not load Bilibili parts. Please retry.') });+ }+ });+ }, parsedVideoSource.kind === 'bilibili' ? 350 : 0);+ return () => { canceled = true; clearTimeout(timeout); };+ }, [currentStep, sourceType, videoUrl, props.videoMetadataService, currentUrlMetadata, parsedVideoSource.kind, metadataRetry]);useEffect(() => {if (creatorSession === null) return;@@ -2305,6 +2326,11 @@ export default function VideoTranslationWorkspace(props: {? subtitleStyleLabel: `${dubbing ? l('配音开启', 'Dubbing on') : l('无配音', 'No dubbing')}, ${outputLabel}`;function openWizardStep(step: WizardStep) {+ if (step > 0 && !sourceSelectionReady) {+ setCurrentStep(0);+ setAttemptedContinue(true);+ return;+ }setCurrentStep(step);setFurthestStep(previous => Math.max(previous, step) as WizardStep);}@@ -2421,6 +2447,21 @@ export default function VideoTranslationWorkspace(props: {const draftSettingsSnapshot = currentDraftSettings();const draftSourceSnapshot = currentDraftSource();+ const navigationDraftRef = useRef(componentNavigationDraft);+ useEffect(() => {+ const draft = navigationDraftRef.current;+ if (!draft || draft.jobId !== (creatorSession?.job.id ?? null)) return;+ navigationDraftRef.current = undefined;+ componentNavigationDraft = undefined;+ applySettingsSnapshot(draft.settings);+ applySourceSnapshot(draft.source);+ setCurrentStep(draft.currentStep);+ setFurthestStep(draft.furthestStep);+ setWorkspacePhase(draft.workspacePhase);+ }, [creatorSession?.job.id]);+ function preserveComponentNavigationDraft() {+ componentNavigationDraft = { jobId: creatorSession?.job.id ?? null, settings: currentDraftSettings(), source: currentDraftSource(), currentStep, furthestStep, workspacePhase };+ }const draftAppliesToSelectedResult = selectedResult !== undefined&& draftBaseVersion === selectedResult.value;const hasConfigDraftChanges = draftAppliesToSelectedResult@@ -2462,6 +2503,22 @@ export default function VideoTranslationWorkspace(props: {setAttemptedContinue(false);}+ function selectBilibiliPart(index: number) {+ const source = parseBilibiliVideoSource(videoUrl);+ const part = currentUrlMetadata?.parts?.find(candidate => candidate.index === index);+ if (source === null || part === undefined || currentUrlMetadata === undefined) return;+ const url = `https://www.bilibili.com/video/${source.videoId}?p=${index}`;+ setUrlMetadata({+ url,+ value: { ...currentUrlMetadata, selectedPart: part, width: part.width, height: part.height }+ });+ setVideoUrl(url);+ setSourceDimensions(undefined);+ setSourceOrientation('landscape');+ if (part.width !== undefined && part.height !== undefined) updateSourceOrientation(part.width, part.height);+ setAttemptedContinue(false);+ }+function clearCurrentSource() {if (sourceType === 'file') {chooseVideo(null);@@ -2559,7 +2616,7 @@ export default function VideoTranslationWorkspace(props: {function continueToSettings() {setAttemptedContinue(true);- if (!hasSource) return;+ if (!hasSource || !sourceSelectionReady) return;openWizardStep(1);}@@ -2602,7 +2659,7 @@ export default function VideoTranslationWorkspace(props: {}async function submit() {- if (!hasSource) {+ if (!hasSource || !sourceSelectionReady) {setCurrentStep(0);setAttemptedContinue(true);return;@@ -2917,6 +2974,10 @@ export default function VideoTranslationWorkspace(props: {{workspacePhase === 'configure' ? (<div className="video-translation-configure-top">+ <LocalTranscriptionNotice controller={props.runtimeDependencies}+ platformCaptions={sourceType === 'url' && (videoUrl.trim() === '' || parseVideoSource(videoUrl).kind === 'youtube') && preferPlatformCaptions}+ importedSubtitle={typeof creatorSession?.state.importedSourceSubtitleId === 'string' || typeof creatorSession?.state.importedTargetSubtitleId === 'string'}+ beforeNavigate={preserveComponentNavigationDraft} />{draftBaseVersion !== undefined ? (<div className="video-translation-draft-bar" role="status"><History size={16} strokeWidth={1.8} aria-hidden="true" />@@ -2997,6 +3058,7 @@ export default function VideoTranslationWorkspace(props: {) : null}{workspacePhase === 'configure' && currentStep === 0 ? (+ <><VideoSourceInputfile={videoFile}registeredFile={registeredSourceFile}@@ -3004,7 +3066,9 @@ export default function VideoTranslationWorkspace(props: {url={videoUrl}hasSource={hasSource}invalid={attemptedContinue && !hasSource}- metadataService={props.videoMetadataService}+ metadataService={parsedVideoSource.kind === 'bilibili' ? undefined : props.videoMetadataService}+ metadata={currentUrlMetadata}+ previewEnabled={sourceSelectionReady}onFileChange={chooseVideo}onUrlChange={url => {setVideoUrl(url);@@ -3020,6 +3084,19 @@ export default function VideoTranslationWorkspace(props: {setUrlMetadata({ url, value: metadata });}}/>+ {sourceType === 'url' && parsedVideoSource.kind === 'bilibili' ? (+ <BilibiliPartSelector+ metadata={currentUrlMetadata}+ error={bilibiliMetadataError?.url === videoUrl ? bilibiliMetadataError.message : props.videoMetadataService === undefined+ ? l('分集信息服务不可用,请确认 Creator Runtime 已连接', 'The parts service is unavailable. Check the Creator Runtime connection.') : undefined}+ onRetry={props.videoMetadataService === undefined ? undefined : () => {+ setBilibiliMetadataError(undefined);+ setMetadataRetry(value => value + 1);+ }}+ onSelect={selectBilibiliPart}+ />+ ) : null}+ </>) : null}{workspacePhase === 'configure' && currentStep === 1 ? (@@ -3428,6 +3505,9 @@ export default function VideoTranslationWorkspace(props: {{attemptedContinue && !hasSource ? (<p className="video-translation-error" role="alert">{l('请先添加需要翻译的视频', 'Add a video to translate first')}</p>) : null}+ {attemptedContinue && hasSource && !sourceSelectionReady ? (+ <p className="video-translation-error" role="alert">{l('请确认链接有效,并选择要翻译的分集后继续', 'Check the link and choose a part before continuing')}</p>+ ) : null}<button className="video-translation-primary-action" type="button" onClick={continueToSettings}>{l('继续', 'Continue')}<ArrowRight size={16} strokeWidth={1.8} aria-hidden="true" />diff --git a/apps/web/src/features/dashboard/creator-panel-adapters.test.ts b/apps/web/src/features/dashboard/creator-panel-adapters.test.tsindex c8479a5d..335d35ff 100644--- a/apps/web/src/features/dashboard/creator-panel-adapters.test.ts+++ b/apps/web/src/features/dashboard/creator-panel-adapters.test.ts@@ -1,6 +1,7 @@import type { CreatorActivity, CreatorStageRun } from '@opencreator/protocol';import { describe, expect, it } from 'vitest';import {+ videoTranslationPanelAdapter,creatorPanelAdapterFor,stickmanVideoPanelAdapter,type CreatorPanelLocalize@@ -9,6 +10,25 @@ import {const zh: CreatorPanelLocalize = value => value;const en: CreatorPanelLocalize = (_zh, value) => value;+describe('shared native image progress', () => {+ it.each(['image-generation', 'cover', 'stickman-video', 'wechat-article'])('shows native preparation and generation phases without fake percentages (%s)', template => {+ const adapter = creatorPanelAdapterFor(template);+ expect(adapter.phaseLabel('preparing_native_image', zh)).toContain('ChatGPT');+ expect(adapter.phaseLabel('generating_image', en)).toBe('Generating image');+ const running = { ...stage('native', '', 'running', null, 'generate'), progress: { phase: 'generating_image', message: '正在生成图片,请稍候', completed: 0, failed: 0, total: 1 } };+ expect(adapter.readStageProgress(running)).toMatchObject({ phase: 'generating_image', message: '正在生成图片,请稍候', percent: null });+ });+});++describe('video translation component progress', () => {+ it('shows download progress separately from task progress, without inventing unknown percentages', () => {+ const running = { ...stage('download', '', 'running', null, 'subtitle', 2, 'downloading_dependencies'), progress: { phase: 'downloading_dependencies', percent: 2, dependencyPercent: 50, downloadedBytes: 1024 ** 3, totalBytes: 2 * 1024 ** 3, message: '模型文件较大,下载完成后自动继续' } };+ expect(videoTranslationPanelAdapter.readStageProgress(running)).toMatchObject({ percent: 50, message: expect.stringContaining('1.00 GiB / 2.00 GiB'), detailsHref: expect.stringContaining('returnPath=') });+ expect(videoTranslationPanelAdapter.phaseLabel('downloading_dependencies', zh)).toContain('尚未开始转录');+ expect(videoTranslationPanelAdapter.readStageProgress({ ...running, progress: { phase: 'downloading_dependencies', percent: 2 } })).toMatchObject({ percent: null, indeterminate: true });+ });+});+const stages = ['ingest-text', 'source-transcript', 'source-brief', 'content-plan', 'script','narration', 'audio-timing', 'storyboard', 'style-assets', 'prompt-pack',diff --git a/apps/web/src/features/dashboard/creator-panel-adapters.ts b/apps/web/src/features/dashboard/creator-panel-adapters.tsindex 512e9cb6..f6bc1edc 100644--- a/apps/web/src/features/dashboard/creator-panel-adapters.ts+++ b/apps/web/src/features/dashboard/creator-panel-adapters.ts@@ -12,6 +12,7 @@ export type NormalizedCreatorActivity = {};export type CreatorStageProgressView = {+ detailsHref?: string;percent: number | null;indeterminate?: boolean;phase: string | null;@@ -68,6 +69,11 @@ const genericAdapter: CreatorPanelAdapter = {export const videoTranslationPanelAdapter: CreatorPanelAdapter = {id: 'video-translation',+ failedProgressText(stage, l) {+ return stage.errorCode === 'creator_dependency_prepare_failed'+ ? l('本地转录组件准备失败,可前往组件页查看原因并重试', 'Local transcription preparation failed. View the component page and retry.')+ : null;+ },composerPlaceholder: l => l('询问状态,或描述要调整的语言、字幕、配音和成片要求','Ask about status or describe language, subtitle, dubbing, and video changes'@@ -89,6 +95,10 @@ export const videoTranslationPanelAdapter: CreatorPanelAdapter = {collecting_subtitles: l('生成双语字幕', 'Generating bilingual subtitles'),preparing_original_media: l('准备原始视频', 'Preparing the original video'),preparing_audio: l('准备音频转录', 'Preparing audio transcription'),+ downloading_dependencies: l('下载本地转录组件(尚未开始转录)', 'Downloading local components (transcription has not started)'),+ verifying_dependencies: l('校验本地转录组件', 'Verifying local transcription components'),+ extracting_dependencies: l('安装本地转录组件', 'Installing local transcription components'),+ dependencies_ready: l('组件已就绪,开始转录', 'Components ready; starting transcription'),transcribing_audio: l('转录并翻译音频', 'Transcribing and translating audio'),collecting_outputs: l('整理输出文件', 'Collecting outputs'),generating_voice: l('生成配音', 'Generating dubbing'),@@ -116,6 +126,20 @@ export const videoTranslationPanelAdapter: CreatorPanelAdapter = {},readStageProgress(stage) {const standard = readStandardProgress(stage);+ if (standard.phase?.endsWith('_dependencies')) {+ const downloaded = readFiniteNumber(stage.progress.downloadedBytes) ?? 0;+ const total = readFiniteNumber(stage.progress.totalBytes);+ const speed = readFiniteNumber(stage.progress.bytesPerSecond);+ const remaining = readFiniteNumber(stage.progress.remainingSeconds);+ const format = (bytes: number) => bytes >= 1024 ** 3 ? `${(bytes / 1024 ** 3).toFixed(2)} GiB` : `${(bytes / 1024 ** 2).toFixed(1)} MiB`;+ const bytes = `${format(downloaded)}${total === null ? '' : ` / ${format(total)}`}${speed ? ` · ${format(speed)}/s` : ''}${remaining === null ? '' : ` · ~${Math.ceil(remaining / 60)} min`}`;+ return { ...standard,+ percent: standard.phase === 'downloading_dependencies' ? readFiniteNumber(stage.progress.dependencyPercent) : null,+ indeterminate: standard.phase !== 'downloading_dependencies' || readFiniteNumber(stage.progress.dependencyPercent) === null,+ message: `${standard.message ?? ''}${standard.phase === 'downloading_dependencies' ? ` ${bytes}` : ''}`,+ detailsHref: `#/settings?tab=local-components&from=video-translation&returnPath=${encodeURIComponent(`#/workbench?tool=video-translation&jobId=${encodeURIComponent(stage.jobId)}`)}`+ };+ }const legacy = readRecord(stage.progress.krillinEventPayload);return {percent: standard.percent ?? readFiniteNumber(legacy?.percent),@@ -1168,6 +1192,8 @@ function genericPhaseLabel(validating: l('检查任务设置', 'Checking task settings'),preparing_source: l('准备任务素材', 'Preparing source material'),requesting_provider: l('提交生成服务', 'Submitting to the provider'),+ preparing_native_image: l('准备 ChatGPT 登录态生图', 'Preparing ChatGPT image generation'),+ generating_image: l('生成图片', 'Generating image'),collecting_outputs: l('整理输出文件', 'Collecting outputs'),finalizing_outputs: l('整理创作结果', 'Finalizing outputs'),completed: l('任务已完成', 'Task completed')diff --git a/apps/web/src/features/dashboard/creator-session-context.ts b/apps/web/src/features/dashboard/creator-session-context.tsnew file mode 100644index 00000000..0a25f87d--- /dev/null+++ b/apps/web/src/features/dashboard/creator-session-context.ts@@ -0,0 +1,8 @@+import { createContext, type Context } from 'react';+import type { CreatorSessionContextValue } from './creator-session-store.js';++type SessionContext = Context<CreatorSessionContextValue | null>;+const hot = (import.meta as ImportMeta & { hot?: { data?: Record<string, unknown> } }).hot;+export const CreatorSessionContext: SessionContext = hot?.data?.creatorSessionContext as SessionContext | undefined+ ?? createContext<CreatorSessionContextValue | null>(null);+if (hot?.data !== undefined) hot.data.creatorSessionContext = CreatorSessionContext;diff --git a/apps/web/src/features/dashboard/creator-session-store.test.tsx b/apps/web/src/features/dashboard/creator-session-store.test.tsxindex b9da8483..1ffc9f6c 100644--- a/apps/web/src/features/dashboard/creator-session-store.test.tsx+++ b/apps/web/src/features/dashboard/creator-session-store.test.tsx@@ -43,6 +43,8 @@ function Harness() {<output aria-label="dubbing">{String(session.state.dubbing)}</output><output aria-label="conflicts">{session.conflictedFields.join(',')}</output><output aria-label="error">{session.error?.code ?? ''}</output>+ <output aria-label="connection">{session.connection?.status ?? ''}</output>+ <button type="button" onClick={() => session.updateDraft({ targetLanguage: 'ja' }, { persist: false })}>change-local</button><output aria-label="turns">{session.turns.map(turn => turn.content).join('|')}</output><output aria-label="busy">{String(session.agentBusy)}</output><output aria-label="stages">{session.job.stages.map(stage => `${stage.stageId}:${stage.status}`).join('|')}</output>@@ -84,6 +86,23 @@ function PendingHarness() {}describe('CreatorSessionStore', () => {+ it('recovers an initial snapshot failure while retaining local state without submitting tasks', async () => {+ const getJob = vi.fn().mockRejectedValueOnce(new TypeError('offline'))+ .mockResolvedValue({ job: job(2, { targetLanguage: 'en' }) });+ const applyAction = vi.fn();+ const runAgentTurn = vi.fn();+ const subscribeJobEvents = vi.fn(() => ({ close: vi.fn() }));+ const view = render(<CreatorSessionProvider initialJob={job(1, { targetLanguage: 'en' })}+ service={{ getJob, subscribeJobEvents, applyAction, runAgentTurn }}><Harness /></CreatorSessionProvider>);+ await waitFor(() => expect(screen.getByLabelText('connection')).toHaveTextContent('reconnecting'));+ fireEvent.click(screen.getByRole('button', { name: 'change-local' }));+ await waitFor(() => expect(screen.getByLabelText('connection')).toHaveTextContent('connected'));+ expect(screen.getByLabelText('language')).toHaveTextContent('ja');+ expect(subscribeJobEvents).toHaveBeenCalledTimes(1);+ expect(applyAction).not.toHaveBeenCalled();+ expect(runAgentTurn).not.toHaveBeenCalled();+ view.unmount();+ });it('updates the confirmed job after canceling and resuming a task', async () => {let session: ReturnType<typeof useCreatorSession> | undefined;function ControlHarness() {diff --git a/apps/web/src/features/dashboard/creator-session-store.tsx b/apps/web/src/features/dashboard/creator-session-store.tsxindex 83d9dbc9..f31b1a21 100644--- a/apps/web/src/features/dashboard/creator-session-store.tsx+++ b/apps/web/src/features/dashboard/creator-session-store.tsx@@ -17,7 +17,6 @@ import {CreatorStageRun} from '@opencreator/protocol';import {- createContext,useCallback,useContext,useEffect,@@ -27,11 +26,15 @@ import {type ReactNode} from 'react';import type { CreatorWebService } from '../../services/creator-service.js';-import { createCreatorSnapshotSubscription } from '../../runtime/creator-sse.js';+import { createCreatorSnapshotSubscription, type CreatorConnectionState } from '../../runtime/creator-sse.js';+import { useRuntimeRecovery } from '../../runtime/runtime-recovery.js';+import { CreatorSessionContext } from './creator-session-context.js';import { normalizePageIssue } from '../issues/page-issue-state.js';import { presentIssue } from '../issues/issue-catalog.js';export type CreatorSessionContextValue = {+ connection?: CreatorConnectionState;+ reconnect?(): Promise<void>;job: CreatorJob;state: Record<string, CreatorJson>;conflictedFields: string[];@@ -137,8 +140,6 @@ export class CreatorPreflightBlockedError extends Error {}}-const CreatorSessionContext = createContext<CreatorSessionContextValue | null>(null);-export function CreatorSessionProvider(props: {initialJob: CreatorJob;ensureJob?: (state: Record<string, CreatorJson>) => Promise<CreatorJob>;@@ -167,6 +168,10 @@ export function CreatorSessionProvider(props: {children: ReactNode;}) {const [confirmedJob, setConfirmedJob] = useState(props.initialJob);+ const [connection, setConnection] = useState<CreatorConnectionState>({ status: 'connecting', attempt: 0 });+ const subscriptionRef = useRef<ReturnType<typeof createCreatorSnapshotSubscription<CreatorJob, CreatorEventEnvelope>>>();+ const runtimeEpoch = useRuntimeRecovery()?.epoch ?? 0;+ const reconnect = useCallback(async () => { await subscriptionRef.current?.retry(); }, []);const [draft, setDraft] = useState<Record<string, CreatorJson>>({});const [dirtyFields, setDirtyFields] = useState<Set<string>>(() => new Set());const [conflictedFields, setConflictedFields] = useState<string[]>([]);@@ -522,11 +527,17 @@ export function CreatorSessionProvider(props: {}, [acceptAuthoritativeIssue]);useEffect(() => {- if (props.service.getJob === undefined || props.service.subscribeJobEvents === undefined) return;- if (isPendingCreatorJob(confirmedJob)) return;+ if (props.service.getJob === undefined || props.service.subscribeJobEvents === undefined) {+ setConnection({ status: 'connected', attempt: 0 });+ return;+ }+ if (isPendingCreatorJob(confirmedJob)) {+ setConnection({ status: 'connected', attempt: 0 });+ return;+ }const jobId = confirmedJob.id;const subscription = createCreatorSnapshotSubscription<CreatorJob, CreatorEventEnvelope>({- loadSnapshot: async () => (await props.service.getJob!(jobId)).job,+ loadSnapshot: async options => (await props.service.getJob!(jobId, options)).job,subscribe: (onEvent, onDisconnect) => (props.service.subscribeJobEvents!(jobId, onEvent, onDisconnect)),@@ -548,15 +559,18 @@ export function CreatorSessionProvider(props: {void reloadAgentTimeline().catch(() => undefined);}},+ onState: setConnection,shouldReloadSnapshot(event) {return event.kind === 'snapshot_changed';}});- void subscription.start().catch(() => undefined);+ subscriptionRef.current = subscription;+ void subscription.start();return () => {subscription.close();+ if (subscriptionRef.current === subscription) subscriptionRef.current = undefined;};- }, [applyLiveEvent, applyRemoteSnapshot, confirmedJob.id, props.service, reloadAgentTimeline, reportLocalIssue]);+ }, [applyLiveEvent, applyRemoteSnapshot, confirmedJob.id, props.service, reloadAgentTimeline, reportLocalIssue, runtimeEpoch]);useEffect(() => {if (@@ -925,6 +939,8 @@ export function CreatorSessionProvider(props: {: issues.find(issue => issue.id === focusedIssueId) ?? null;const value = useMemo<CreatorSessionContextValue>(() => ({+ connection,+ reconnect,job: confirmedJob,state: { ...confirmedJob.state, ...draft },conflictedFields,@@ -959,7 +975,7 @@ export function CreatorSessionProvider(props: {steerAgentTurn,interruptAgentTurn,respondAgentApproval- }), [agentBusy, agentSession, askPendingIssue, applyAction, applyRemoteSnapshot, approvals, cancelJob, captureCreatorFailure, clearError, confirmedJob, conflictedFields, draft, error, flush, focusIssue, focusedIssue, interruptAgentTurn, issues, items, openArtifact, openArtifactJson, preflight, repairIssue, respondAgentApproval, resumeJob, runAgentTurn, runPreflight, steerAgentTurn, turns, updateDraft, uploadArticleImage, uploadReferenceImage, uploadSourceDocument, uploadSourceVideo]);+ }), [connection, reconnect, agentBusy, agentSession, askPendingIssue, applyAction, applyRemoteSnapshot, approvals, cancelJob, captureCreatorFailure, clearError, confirmedJob, conflictedFields, draft, error, flush, focusIssue, focusedIssue, interruptAgentTurn, issues, items, openArtifact, openArtifactJson, preflight, repairIssue, respondAgentApproval, resumeJob, runAgentTurn, runPreflight, steerAgentTurn, turns, updateDraft, uploadArticleImage, uploadReferenceImage, uploadSourceDocument, uploadSourceVideo]);return (<CreatorSessionContext.Provider value={value}>diff --git a/apps/web/src/features/dashboard/dashboard.css b/apps/web/src/features/dashboard/dashboard.cssindex 662fff3d..7c7efead 100644--- a/apps/web/src/features/dashboard/dashboard.css+++ b/apps/web/src/features/dashboard/dashboard.css@@ -11303,6 +11303,23 @@box-sizing: border-box;}+.video-translation-bilibili-parts {+ width: min(960px, 100%);+ margin: 0 auto;+ padding: 0 24px 24px;+ box-sizing: border-box;+}++.video-translation-bilibili-parts select {+ padding: 0 12px;+}++.video-translation-bilibili-parts p {+ color: var(--muted);+ font-size: 13px;+ line-height: 1.5;+}+.video-translation-dropzone {position: relative;width: min(592px, 100%);diff --git a/apps/web/src/features/settings/CodexImageStatusNotice.test.tsx b/apps/web/src/features/settings/CodexImageStatusNotice.test.tsxnew file mode 100644index 00000000..46b84d7f--- /dev/null+++ b/apps/web/src/features/settings/CodexImageStatusNotice.test.tsx@@ -0,0 +1,51 @@+import type { CodexImageStatus } from '@opencreator/protocol';+import { fireEvent, render, screen, waitFor } from '@testing-library/react';+import { userEvent } from '@testing-library/user-event';+import { describe, expect, it, vi } from 'vitest';+import type { CreatorServicesSettingsService } from '../../services/creator-services-service.js';+import { CodexImageStatusNotice } from './CodexImageStatusNotice.js';++const native: CodexImageStatus = { authentication: 'chatgpt', ready: true, executionMode: 'native', version: 'codex 0.149.0', message: '本地 ChatGPT 凭据和工具已就绪' };+const api: CodexImageStatus = { authentication: 'api_key', ready: true, executionMode: 'api', message: '接口需支持图片生成' };+function service(getCodexImageStatus: () => Promise<CodexImageStatus>): CreatorServicesSettingsService {+ return { getCodexImageStatus } as CreatorServicesSettingsService;+}++describe('Codex image status notice', () => {+ it('distinguishes native login from API mode and never claims real generation is verified', async () => {+ const getStatus = vi.fn().mockResolvedValueOnce(native).mockResolvedValue(api);+ render(<CodexImageStatusNotice service={service(getStatus)} />);+ expect(await screen.findByText('ChatGPT 登录态 · 原生生图')).toBeInTheDocument();+ expect(screen.getByText('Runtime 版本: codex 0.149.0')).toBeInTheDocument();+ expect(screen.getByText(/以实际生成结果为准/)).toBeInTheDocument();+ await userEvent.click(screen.getByRole('button', { name: '刷新状态' }));+ expect(await screen.findByText('API Key · 图片接口')).toBeInTheDocument();+ expect(screen.getByText('接口需支持图片生成')).toBeInTheDocument();+ });++ it('offers the real Agent setup callback when login or capability is unavailable', async () => {+ const onOpenAgentSetup = vi.fn();+ render(<CodexImageStatusNotice service={service(async () => ({ authentication: 'chatgpt', ready: false, executionMode: null, message: '请重新登录' }))} onOpenAgentSetup={onOpenAgentSetup} />);+ expect(await screen.findByText('请重新登录')).toBeInTheDocument();+ await userEvent.click(screen.getByRole('button', { name: '配置 Agent' }));+ expect(onOpenAgentSetup).toHaveBeenCalledOnce();+ expect(screen.queryByText(/以实际生成结果为准/)).not.toBeInTheDocument();+ });++ it('does not render inactive buttons when callbacks or status support are absent', () => {+ render(<CodexImageStatusNotice service={null} />);+ expect(screen.getByText(/当前 Runtime 无法检查生图能力/)).toBeInTheDocument();+ expect(screen.queryByRole('button')).not.toBeInTheDocument();+ });++ it('supports retry after status errors and refreshes on window focus', async () => {+ const getStatus = vi.fn().mockRejectedValueOnce(new Error('private runtime details')).mockResolvedValue(native);+ render(<CodexImageStatusNotice service={service(getStatus)} />);+ expect(await screen.findByText(/无法读取生图状态/)).toBeInTheDocument();+ expect(screen.queryByText('private runtime details')).not.toBeInTheDocument();+ await userEvent.click(screen.getByRole('button', { name: '刷新状态' }));+ expect(await screen.findByText(native.message)).toBeInTheDocument();+ fireEvent.focus(window);+ await waitFor(() => expect(getStatus).toHaveBeenCalledTimes(3));+ });+});diff --git a/apps/web/src/features/settings/CodexImageStatusNotice.tsx b/apps/web/src/features/settings/CodexImageStatusNotice.tsxnew file mode 100644index 00000000..0a55870d--- /dev/null+++ b/apps/web/src/features/settings/CodexImageStatusNotice.tsx@@ -0,0 +1,50 @@+import type { CodexImageStatus } from '@opencreator/protocol';+import { useEffect, useState } from 'react';+import { useLocalizedCopy } from '../../i18n/useLocalizedCopy.js';+import type { CreatorServicesSettingsService } from '../../services/creator-services-service.js';++export function CodexImageStatusNotice(props: {+ service: CreatorServicesSettingsService | null;+ onOpenAgentSetup?(): void;+}) {+ const localize = useLocalizedCopy();+ const [status, setStatus] = useState<CodexImageStatus>();+ const [loading, setLoading] = useState(false);+ const [failed, setFailed] = useState(false);+ const [refreshToken, setRefreshToken] = useState(0);+ const readStatus = props.service?.getCodexImageStatus;+ useEffect(() => {+ let active = true;+ setStatus(undefined);+ setFailed(false);+ setLoading(Boolean(readStatus));+ if (!readStatus) return;+ void readStatus().then(value => {+ if (active) setStatus(value);+ }).catch(() => {+ if (active) setFailed(true);+ }).finally(() => {+ if (active) setLoading(false);+ });+ return () => { active = false; };+ }, [readStatus, refreshToken]);+ useEffect(() => {+ const refresh = () => setRefreshToken(value => value + 1);+ window.addEventListener('focus', refresh);+ return () => window.removeEventListener('focus', refresh);+ }, []);+ return <div className={`creator-services-inline-note${status?.ready ? '' : ' is-warning'}`} role="status" aria-live="polite">+ <strong>{loading ? localize('正在检查 Codex 生图配置…', 'Checking Codex image configuration…')+ : status?.authentication === 'chatgpt' ? localize('ChatGPT 登录态 · 原生生图', 'ChatGPT sign-in · Native image generation')+ : status?.authentication === 'api_key' ? localize('API Key · 图片接口', 'API key · Image API')+ : localize('Codex 生图尚未就绪', 'Codex image generation is not ready')}</strong>+ <p>{failed ? localize('无法读取生图状态,请检查 Runtime 连接后重试。', 'Cannot read image status. Check the Runtime connection and retry.')+ : status?.message ?? (!readStatus ? localize('当前 Runtime 无法检查生图能力,请更新 Runtime。', 'This Runtime cannot check image capabilities. Update the Runtime.') : '')}</p>+ {status?.version ? <p>{localize('Runtime 版本', 'Runtime version')}: {status.version}</p> : null}+ {status?.ready ? <p>{localize('这里只检查本地配置和工具能力,不会执行生图请求;账号权限、额度及接口可用性以实际生成结果为准。', 'This checks local configuration and tool support without generating an image. Account access, quota, and API availability are verified during generation.')}</p> : null}+ <div className="creator-services-codex-image-actions">+ {readStatus ? <button type="button" className="settings-secondary-button" disabled={loading} onClick={() => setRefreshToken(value => value + 1)}>{localize('刷新状态', 'Refresh status')}</button> : null}+ {props.onOpenAgentSetup ? <button type="button" className="settings-secondary-button" onClick={props.onOpenAgentSetup}>{localize('配置 Agent', 'Set up Agent')}</button> : null}+ </div>+ </div>;+}diff --git a/apps/web/src/features/settings/CreatorServicesSettingsView.test.tsx b/apps/web/src/features/settings/CreatorServicesSettingsView.test.tsxindex a59ec973..ab2ad868 100644--- a/apps/web/src/features/settings/CreatorServicesSettingsView.test.tsx+++ b/apps/web/src/features/settings/CreatorServicesSettingsView.test.tsx@@ -14,6 +14,17 @@ import type { CreatorServicesSettingsService } from '../../services/creator-servimport { CreatorServicesSettingsView } from './CreatorServicesSettingsView.js';describe('CreatorServicesSettingsView', () => {+ it('loads the real Codex image status only in the native image settings section', async () => {+ const service = createService();+ service.getCodexImageStatus = vi.fn(async () => ({ authentication: 'chatgpt' as const, ready: true, executionMode: 'native' as const, message: '本地登录凭据已就绪' }));+ const onOpenAgentSetup = vi.fn();+ render(<CreatorServicesSettingsView connected service={service} modelService={createModelService()} onOpenAgentSetup={onOpenAgentSetup} />);+ expect(service.getCodexImageStatus).not.toHaveBeenCalled();+ await userEvent.click(await screen.findByRole('tab', { name: '图像生成' }));+ expect(await screen.findByText('本地登录凭据已就绪')).toBeInTheDocument();+ await userEvent.click(screen.getByRole('button', { name: '配置 Agent' }));+ expect(onOpenAgentSetup).toHaveBeenCalledOnce();+ });it('saves an OSS region and optional endpoint in the shared settings form', async () => {const user = userEvent.setup();const service = createService([diff --git a/apps/web/src/features/settings/CreatorServicesSettingsView.tsx b/apps/web/src/features/settings/CreatorServicesSettingsView.tsxindex 5c73249b..73531e81 100644--- a/apps/web/src/features/settings/CreatorServicesSettingsView.tsx+++ b/apps/web/src/features/settings/CreatorServicesSettingsView.tsx@@ -44,6 +44,7 @@ import type { ConnectionService } from '../../services/connection-service.js';import { IssueList } from '../issues/IssuePresenter.js';import { usePageIssueState } from '../issues/page-issue-state.js';import { inferLlmProviderId, llmProviderOptions } from './llm-provider-selection.js';+import { CodexImageStatusNotice } from './CodexImageStatusNotice.js';import './creator-services-settings.css';export type CreatorServicesSection = 'text' | 'transcription' | 'tts' | 'image' | 'video';@@ -58,6 +59,7 @@ export function CreatorServicesSettingsView(props: {service: CreatorServicesSettingsService | null;modelService?: ModelSettingsService | null;initialSection?: CreatorServicesSection;+ onOpenAgentSetup?(): void;}) {const l = useLocalizedCopy();const confirm = useConfirmDialog();@@ -439,7 +441,7 @@ export function CreatorServicesSettingsView(props: {/>) : null}{activeSection === 'image' ? (- <ImageSettings config={config} update={updateConfig} configuredCredentials={configuredCredentials} />+ <ImageSettings config={config} update={updateConfig} configuredCredentials={configuredCredentials} service={props.service} onOpenAgentSetup={props.onOpenAgentSetup} />) : null}{activeSection === 'video' ? (<VideoSettings config={config} update={updateConfig} configuredCredentials={configuredCredentials} />@@ -1024,7 +1026,10 @@ function TtsSettings(props: SettingsGroupProps) {);}-function ImageSettings(props: SettingsGroupProps) {+function ImageSettings(props: SettingsGroupProps & {+ service: CreatorServicesSettingsService | null;+ onOpenAgentSetup?(): void;+}) {const l = useLocalizedCopy();const provider = props.config.image.provider;return (@@ -1053,6 +1058,7 @@ function ImageSettings(props: SettingsGroupProps) {if (!target.model && preset?.models[0] !== undefined) target.model = preset.models[0].id;})}/>+ {provider === 'codex-native' ? <CodexImageStatusNotice service={props.service} onOpenAgentSetup={props.onOpenAgentSetup} /> : null}{provider === 'openai' ? <OpenAiFields id="image-openai" credential="image.openai.apiKey" configuredCredentials={props.configuredCredentials} value={props.config.image.openai} modelPlaceholder="gpt-image-1" modelSuggestions={creatorProviderOfKind('image', 'openai')?.models.map(model => model.id)} onChange={value => props.update(config => { config.image.openai = value; })} /> : null}{provider === 'jimeng' ? <OpenAiFields id="image-jimeng" credential="image.jimeng.apiKey" configuredCredentials={props.configuredCredentials} value={props.config.image.jimeng} modelPlaceholder="doubao-seedream-4-0-250828" modelSuggestions={creatorProviderOfKind('image', 'jimeng')?.models.map(model => model.id)} baseUrlPlaceholder="https://ark.cn-beijing.volces.com/api/v3" onChange={value => props.update(config => { config.image.jimeng = value; })} /> : null}{provider === 'kling' ? <KlingFields id="image-kling" accessKeyCredential="image.kling.accessKey" secretKeyCredential="image.kling.secretKey" configuredCredentials={props.configuredCredentials} value={props.config.image.kling} modelPlaceholder="kling-v2-1" modelSuggestions={creatorProviderOfKind('image', 'kling')?.models.map(model => model.id)} onChange={value => props.update(config => { config.image.kling = value; })} /> : null}diff --git a/apps/web/src/features/settings/DiagnosticsSettingsView.tsx b/apps/web/src/features/settings/DiagnosticsSettingsView.tsxindex cfffbcb9..a07b58cc 100644--- a/apps/web/src/features/settings/DiagnosticsSettingsView.tsx+++ b/apps/web/src/features/settings/DiagnosticsSettingsView.tsx@@ -3,6 +3,7 @@ import type {CodexStatusResponse} from '@opencreator/protocol';import { AlertTriangle, CheckCircle2, HardDrive, SquareTerminal } from 'lucide-react';+import { RuntimeRecoveryNotice } from '../../runtime/runtime-recovery.js';export type DiagnosticsSettingsViewProps = {connected: boolean;@@ -26,6 +27,7 @@ export function DiagnosticsSettingsView(props: DiagnosticsSettingsViewProps) {{!props.connected ? (<p className="settings-notice">本地服务未连接,诊断状态可能已过期。</p>) : null}+ <RuntimeRecoveryNotice diagnostics /><div className="diagnostics-overview"><section>diff --git a/apps/web/src/features/settings/LocalTranscriptionComponents.test.tsx b/apps/web/src/features/settings/LocalTranscriptionComponents.test.tsxnew file mode 100644index 00000000..b83b23d4--- /dev/null+++ b/apps/web/src/features/settings/LocalTranscriptionComponents.test.tsx@@ -0,0 +1,195 @@+import type { CreatorLocalComponent, CreatorRuntimeComponentsResponse } from '@opencreator/protocol';+import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';+import { describe, expect, it, vi } from 'vitest';+import { LanguageProvider } from '../../i18n/LanguageProvider.js';+import type { RuntimeDependenciesController } from '../../app/use-runtime-dependencies.js';+import { LocalTranscriptionComponents } from './LocalTranscriptionComponents.js';+import { LocalTranscriptionNotice } from '../dashboard/LocalTranscriptionNotice.js';++function componentFixture(patch: Partial<CreatorLocalComponent> = {}): CreatorLocalComponent {+ return { id: 'whisperkit', name: 'WhisperKit', available: true, version: null, supportedVersion: '1.1.0', installedAt: null,+ path: '/runtime/dependencies', source: 'Homebrew / ModelScope', models: [{ id: 'large-v2', installed: false, bytes: null }],+ state: 'not_installed', model: 'large-v2', item: null, downloadedBytes: 0, totalBytes: null, percent: null, bytesPerSecond: null, remainingSeconds: null, error: null, ...patch };+}++function controllerFixture(patch: Partial<CreatorLocalComponent> = {}, provider = 'whisperkit') {+ const componentsStatus: CreatorRuntimeComponentsResponse = { platform: 'darwin', arch: 'arm64', selectedProvider: provider, selectedModel: 'large-v2', components: [componentFixture(patch)] };+ const controller: RuntimeDependenciesController = { phase: 'idle', componentsStatus, downloadComponents: vi.fn(async () => {}), refreshComponents: vi.fn(async () => {}), checkYtDlpUpdate: vi.fn(), updateYtDlp: vi.fn() };+ return controller;+}++describe('local transcription component management', () => {+ it('shows engine and model information and starts a manual download', async () => {+ const controller = controllerFixture();+ render(<LanguageProvider><LocalTranscriptionComponents controller={controller} /></LanguageProvider>);+ expect(screen.getByRole('heading', { name: /WhisperKit/ })).toBeInTheDocument();+ expect(screen.getByText('当前模型')).toBeVisible();+ expect(screen.getByText('受支持版本')).not.toBeVisible();+ expect(screen.getByText('/runtime/dependencies')).not.toBeVisible();+ fireEvent.click(screen.getByRole('button', { name: '下载组件' }));+ expect(controller.downloadComponents).toHaveBeenCalledOnce();+ await waitFor(() => expect(screen.getByRole('button', { name: '下载组件' })).not.toBeDisabled());+ });++ it('checks a ready component without downloading or showing a primary maintenance action', async () => {+ const controller = controllerFixture({ state: 'ready', version: '1.1.0' });+ render(<LanguageProvider><LocalTranscriptionComponents controller={controller} /></LanguageProvider>);+ expect(controller.refreshComponents).toHaveBeenCalledOnce();+ const button = screen.getByRole('button', { name: '检查状态' });+ expect(button).toHaveClass('settings-secondary-button');+ expect(button.closest('.runtime-component-actions')).not.toBeNull();+ expect(screen.queryByRole('button', { name: /下载|修复|更新/ })).not.toBeInTheDocument();++ fireEvent.click(button);++ await waitFor(() => expect(screen.getByText('检查完成,本地组件已就绪。')).toBeVisible());+ expect(controller.refreshComponents).toHaveBeenCalledTimes(2);+ expect(controller.downloadComponents).not.toHaveBeenCalled();+ });++ it('keeps checks read-only even when components are missing', async () => {+ const controller = controllerFixture();+ render(<LanguageProvider><LocalTranscriptionComponents controller={controller} /></LanguageProvider>);++ fireEvent.click(screen.getByRole('button', { name: '检查状态' }));++ await waitFor(() => expect(screen.getByText('组件状态已刷新,尚未开始下载。')).toBeVisible());+ expect(controller.downloadComponents).not.toHaveBeenCalled();+ expect(screen.getByRole('button', { name: '下载组件' })).toHaveClass('settings-primary-button');+ });++ it('disables duplicate actions until a status check completes', async () => {+ const controller = controllerFixture();+ let finish!: () => void;+ render(<LanguageProvider><LocalTranscriptionComponents controller={controller} /></LanguageProvider>);+ vi.mocked(controller.refreshComponents!).mockImplementationOnce(() => new Promise(resolve => { finish = resolve; }));++ fireEvent.click(screen.getByRole('button', { name: '检查状态' }));++ expect(screen.getByRole('button', { name: '正在检查' })).toBeDisabled();+ expect(screen.getByRole('button', { name: '下载组件' })).toBeDisabled();+ await act(async () => { finish(); });+ expect(screen.getByRole('button', { name: '检查状态' })).not.toBeDisabled();+ });++ it('shows a failed status check without claiming success or starting a download', async () => {+ const controller = controllerFixture({ state: 'ready', version: '1.1.0' });+ render(<LanguageProvider><LocalTranscriptionComponents controller={controller} /></LanguageProvider>);+ vi.mocked(controller.refreshComponents!).mockRejectedValueOnce(new Error('status unavailable'));++ fireEvent.click(screen.getByRole('button', { name: '检查状态' }));++ await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent('status unavailable'));+ expect(screen.queryByText('检查完成,本地组件已就绪。')).not.toBeInTheDocument();+ expect(controller.downloadComponents).not.toHaveBeenCalled();+ expect(screen.getByRole('button', { name: '检查状态' })).not.toBeDisabled();+ });++ it('keeps detailed component information available in a collapsed disclosure', () => {+ render(<LanguageProvider><LocalTranscriptionComponents controller={controllerFixture({ state: 'ready', version: '1.1.0' })} /></LanguageProvider>);+ const path = screen.getByText('/runtime/dependencies');+ expect(path).not.toBeVisible();+ expect(path.closest('details')).not.toHaveAttribute('open');++ fireEvent.click(screen.getByText('组件详情'));++ expect(path).toBeVisible();+ expect(screen.getByText('受支持版本')).toBeVisible();+ });++ it.each([+ { state: 'partial' as const, name: '补齐组件' },+ { state: 'failed' as const, name: '重试下载' }+ ])('uses a specific primary action for $state components', async ({ state, name }) => {+ const controller = controllerFixture({ state, version: '1.1.0' });+ render(<LanguageProvider><LocalTranscriptionComponents controller={controller} /></LanguageProvider>);+ const button = screen.getByRole('button', { name });+ expect(button).toHaveClass('settings-primary-button');+ expect(screen.getByText(/将准备所需资源:large-v2 模型/)).toBeVisible();++ fireEvent.click(button);++ expect(controller.downloadComponents).toHaveBeenCalledOnce();+ await waitFor(() => expect(screen.getByRole('button', { name })).not.toBeDisabled());+ });++ it('shows real byte progress and explains the long preparation before transcription', () => {+ render(<LanguageProvider><LocalTranscriptionComponents controller={controllerFixture({ state: 'downloading', item: 'WhisperKit large-v2 model', downloadedBytes: 1024 ** 3, totalBytes: 2 * 1024 ** 3, percent: 50, bytesPerSecond: 1024 ** 2, remainingSeconds: 1024 })} /></LanguageProvider>);+ expect(screen.getByRole('progressbar')).toHaveAttribute('value', '50');+ expect(screen.getByText(/1.00 GiB \/ 2.00 GiB/)).toBeInTheDocument();+ expect(screen.getByText(/当前尚未开始转录/)).toBeInTheDocument();+ expect(screen.queryByRole('button')).not.toBeInTheDocument();+ });++ it.each([+ { state: 'verifying' as const, label: '正在校验' },+ { state: 'extracting' as const, label: '正在解压安装' }+ ])('keeps $state progress visible outside the folded details', ({ state, label }) => {+ render(<LanguageProvider><LocalTranscriptionComponents controller={controllerFixture({ state })} /></LanguageProvider>);+ expect(screen.getByText(label)).toBeVisible();+ expect(screen.getByText(/当前尚未开始转录/)).toBeVisible();+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();+ expect(screen.queryByRole('button')).not.toBeInTheDocument();+ });++ it('keeps unknown-size progress indeterminate and hides unsupported components', () => {+ const { rerender } = render(<LanguageProvider><LocalTranscriptionComponents controller={controllerFixture({ state: 'downloading' })} /></LanguageProvider>);+ expect(screen.getByRole('progressbar')).not.toHaveAttribute('value');+ rerender(<LanguageProvider><LocalTranscriptionComponents controller={controllerFixture({ state: 'unsupported', available: false })} /></LanguageProvider>);+ expect(screen.queryByRole('button', { name: '下载组件' })).not.toBeInTheDocument();+ expect(screen.queryByRole('heading', { name: /WhisperKit/ })).not.toBeInTheDocument();+ expect(screen.queryByRole('link', { name: '调整转录设置' })).not.toBeInTheDocument();+ expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();+ });++ it.each([+ { platform: 'darwin', arch: 'arm64', visibleProvider: 'whisperkit' },+ { platform: 'win32', arch: 'x64', visibleProvider: 'whisper.cpp' },+ { platform: 'darwin', arch: 'x64', visibleProvider: null },+ { platform: 'win32', arch: 'arm64', visibleProvider: null },+ { platform: 'linux', arch: 'x64', visibleProvider: null }+ ])('shows only available components on Runtime $platform / $arch', ({ platform, arch, visibleProvider }) => {+ const controller = controllerFixture();+ const providers = [+ { id: 'whisperkit', name: 'WhisperKit' },+ { id: 'whisper.cpp', name: 'whisper.cpp' },+ { id: 'faster-whisper', name: 'Faster Whisper' }+ ] as const;+ controller.componentsStatus = {+ ...controller.componentsStatus!, platform, arch,+ components: providers.map(provider => componentFixture({+ ...provider, available: provider.id === visibleProvider,+ state: provider.id === visibleProvider ? 'not_installed' : 'unsupported'+ }))+ };++ render(<LanguageProvider><LocalTranscriptionComponents controller={controller} /></LanguageProvider>);++ for (const provider of providers) {+ const heading = screen.queryByRole('heading', { name: new RegExp(provider.name) });+ if (provider.id === visibleProvider) expect(heading).toBeInTheDocument();+ else expect(heading).not.toBeInTheDocument();+ }+ });+});++describe('video translation component notice', () => {+ it('explains conditional transcription and preserves context before navigating', () => {+ const beforeNavigate = vi.fn();+ window.location.hash = '#/workbench?tool=video-translation&jobId=job_1';+ render(<LanguageProvider><LocalTranscriptionNotice controller={controllerFixture()} platformCaptions importedSubtitle={false} beforeNavigate={beforeNavigate} /></LanguageProvider>);+ expect(screen.getByText(/没有可用字幕时才需要本地转录/)).toBeInTheDocument();+ const link = screen.getByRole('link', { name: '前往组件下载' });+ expect(link.getAttribute('href')).toContain('component=whisperkit');+ expect(link.getAttribute('href')).toContain('returnPath=');+ fireEvent.click(link);+ expect(beforeNavigate).toHaveBeenCalledOnce();+ });++ it('does not require downloading when subtitles were imported, or when using cloud transcription', () => {+ const { rerender } = render(<LanguageProvider><LocalTranscriptionNotice controller={controllerFixture()} platformCaptions={false} importedSubtitle beforeNavigate={() => {}} /></LanguageProvider>);+ expect(screen.getByText('当前任务使用导入字幕,无需本地语音转录。')).toBeInTheDocument();+ rerender(<LanguageProvider><LocalTranscriptionNotice controller={controllerFixture({}, 'openai')} platformCaptions={false} importedSubtitle={false} beforeNavigate={() => {}} /></LanguageProvider>);+ expect(screen.queryByText('本地转录组件尚未就绪')).not.toBeInTheDocument();+ });+});diff --git a/apps/web/src/features/settings/LocalTranscriptionComponents.tsx b/apps/web/src/features/settings/LocalTranscriptionComponents.tsxnew file mode 100644index 00000000..cc7012da--- /dev/null+++ b/apps/web/src/features/settings/LocalTranscriptionComponents.tsx@@ -0,0 +1,122 @@+import type { CreatorLocalComponent } from '@opencreator/protocol';+import { ChevronDown, Download, LoaderCircle, RefreshCw } from 'lucide-react';+import { useEffect, useRef, useState } from 'react';+import type { RuntimeDependenciesController } from '../../app/use-runtime-dependencies.js';+import { useLocalizedCopy } from '../../i18n/useLocalizedCopy.js';+import './local-transcription.css';++export function componentProgressText(component: Pick<CreatorLocalComponent, 'downloadedBytes' | 'totalBytes' | 'bytesPerSecond' | 'remainingSeconds'>): string {+ const size = (bytes: number) => bytes >= 1024 ** 3 ? `${(bytes / 1024 ** 3).toFixed(2)} GiB` : `${(bytes / 1024 ** 2).toFixed(1)} MiB`;+ return `${size(component.downloadedBytes)}${component.totalBytes === null ? '' : ` / ${size(component.totalBytes)}`}${component.bytesPerSecond ? ` · ${size(component.bytesPerSecond)}/s` : ''}${component.remainingSeconds === null ? '' : ` · ~${Math.ceil(component.remainingSeconds / 60)} min`}`;+}++export function LocalTranscriptionComponents({ controller, componentId, onReturn }: {+ controller: RuntimeDependenciesController;+ componentId?: string;+ onReturn?(): void;+}) {+ const localize = useLocalizedCopy();+ const [action, setAction] = useState<{ componentId: string; kind: 'check' | 'download' }>();+ const [checkedComponentId, setCheckedComponentId] = useState<string>();+ const [actionError, setActionError] = useState<string>();+ const status = controller.componentsStatus;+ const refreshComponents = controller.refreshComponents;+ const downloadComponents = controller.downloadComponents;+ const focused = useRef<string>();+ useEffect(() => { void controller.refreshComponents?.(); }, [controller.refreshComponents]);+ useEffect(() => {+ if (!status || !componentId || focused.current === componentId) return;+ document.getElementById(`component-${componentId}`)?.scrollIntoView?.({ block: 'nearest' });+ focused.current = componentId;+ }, [status, componentId]);++ async function performAction(id: string, kind: 'check' | 'download', run: () => Promise<void>) {+ setAction({ componentId: id, kind });+ setCheckedComponentId(undefined);+ setActionError(undefined);+ try {+ await run();+ if (kind === 'check') setCheckedComponentId(id);+ } catch (error) {+ setActionError(error instanceof Error ? error.message : String(error));+ } finally {+ setAction(undefined);+ }+ }++ return <div className="local-transcription-components">+ {onReturn ? <button type="button" className="settings-secondary-button" onClick={onReturn}>{localize('返回视频翻译', 'Return to video translation')}</button> : null}+ {controller.componentError ? <p role="alert">{localize('无法读取或下载本地转录组件:', 'Cannot read or download local components: ')}{controller.componentError} <button type="button" onClick={() => void controller.refreshComponents?.()}>{localize('重试', 'Retry')}</button></p> : null}+ {actionError ? <p role="alert">{actionError}</p> : null}+ {!status && !controller.componentError && controller.refreshComponents ? <p role="status">{localize('正在检查本地转录组件', 'Checking local transcription components')}</p> : null}+ {status?.components.filter(component => component.available).map(component => {+ const selected = component.id === status.selectedProvider;+ const busy = ['downloading', 'verifying', 'extracting'].includes(component.state);+ const checking = action?.componentId === component.id && action.kind === 'check';+ const startingDownload = action?.componentId === component.id && action.kind === 'download';+ const canDownload = selected && downloadComponents && component.state !== 'ready';+ const requiredResources = [+ component.version === null ? localize('转录引擎', 'transcription engine') : undefined,+ component.models.find(model => model.id === component.model)?.installed !== true+ ? `${component.model ?? ''} ${localize('模型', 'model')}`.trim() : undefined+ ].filter(Boolean).join(localize('、', ', '));+ const labels = {+ not_installed: localize('尚未安装', 'Not installed'), partial: localize('模型尚未就绪', 'Model not ready'),+ downloading: localize('正在下载', 'Downloading'), verifying: localize('正在校验', 'Verifying'), extracting: localize('正在解压安装', 'Extracting'),+ ready: localize('已就绪', 'Ready'), failed: localize('下载失败', 'Download failed'), unsupported: localize('当前平台不支持受控安装', 'Managed installation unavailable on this platform')+ };+ return <article key={component.id} id={`component-${component.id}`} className="runtime-component-item local-component-item" data-highlight={component.id === componentId || undefined}>+ <div className="runtime-component-heading">+ <div>+ <div className="runtime-component-title">+ <h2>{component.name}</h2>+ <span role="status" data-status={component.state === 'ready' ? 'current' : component.state === 'failed' ? 'update' : 'checking'}>{labels[component.state]}</span>+ {selected ? <span>{localize('当前使用', 'Selected')}</span> : null}+ </div>+ <p>{localize('用于本地语音转录,仅在实际需要转录时使用。', 'Used for local speech transcription only when transcription is needed.')}</p>+ </div>+ </div>+ <dl className="runtime-component-details">+ <div><dt>{localize('已安装引擎版本', 'Installed engine version')}</dt><dd>{component.version ?? localize('未安装', 'Not installed')}</dd></div>+ <div><dt>{localize('当前模型', 'Current model')}</dt><dd>{component.model ?? '—'}</dd></div>+ </dl>+ <details className="local-component-details">+ <summary><ChevronDown size={15} aria-hidden="true" />{localize('组件详情', 'Component details')}</summary>+ <dl className="runtime-component-details">+ <div><dt>{localize('受支持版本', 'Supported version')}</dt><dd>{component.supportedVersion ?? '—'}</dd></div>+ <div><dt>{localize('Runtime 平台', 'Runtime platform')}</dt><dd>{status.platform} / {status.arch}</dd></div>+ <div><dt>{localize('安装位置', 'Install location')}</dt><dd>{component.path}</dd></div>+ <div><dt>{localize('资源来源与校验', 'Source and verification')}</dt><dd>{component.source}</dd></div>+ <div><dt>{localize('安装时间', 'Installed at')}</dt><dd>{component.installedAt ? new Date(component.installedAt).toLocaleString() : '—'}</dd></div>+ <div><dt>{localize('模型列表', 'Models')}</dt><dd><ul className="local-component-models">{component.models.map(model => <li key={model.id}>{model.id} · {model.installed ? localize('已安装并通过检查', 'Installed and checked') : localize('未安装', 'Not installed')}{model.bytes === null ? '' : ` · ${(model.bytes / 1024 ** 3).toFixed(2)} GiB`}</li>)}</ul></dd></div>+ </dl>+ </details>+ {busy ? <div className="local-component-progress" role="status">+ <strong>{component.item ?? (component.state === 'verifying' ? localize('正在检查本地组件', 'Checking local components') : localize('正在连接下载服务器', 'Connecting to download server'))}</strong>+ {component.message ? <p>{component.message}</p> : null}+ {component.state === 'downloading' ? <>+ <progress max={100} value={component.percent ?? undefined} aria-label={localize('组件下载进度', 'Component download progress')} />+ <span>{component.percent === null ? '' : `${Math.floor(component.percent)}% · `}{componentProgressText(component)}</span>+ </> : null}+ <p>{localize('模型文件较大,可能需要较长时间。当前尚未开始转录;准备完成后将自动继续,无需重新开始任务。', 'Models are large and may take time to prepare. Transcription has not started yet; the task continues automatically after preparation.')}</p>+ </div> : null}+ {component.error ? <p role="alert">{component.error}</p> : null}+ {checkedComponentId === component.id && !controller.componentError && !actionError && !busy ? <p className="local-component-check-notice" role="status">{component.state === 'ready' ? localize('检查完成,本地组件已就绪。', 'Check complete. Local components are ready.') : localize('组件状态已刷新,尚未开始下载。', 'Component status refreshed. No downloads have started.')}</p> : null}+ {!busy && canDownload ? <p>{localize('将准备所需资源:', 'Resources to prepare: ')}{requiredResources || localize('当前转录引擎和模型', 'the current transcription engine and model')}{localize('。', '.')}</p> : null}+ {!busy ? <footer className="runtime-component-footer">+ <div className="runtime-component-actions">+ {refreshComponents ? <button type="button" className="settings-secondary-button" disabled={action !== undefined} onClick={() => void performAction(component.id, 'check', refreshComponents)}>+ {checking ? <LoaderCircle className="settings-spin" size={15} aria-hidden="true" /> : <RefreshCw size={15} aria-hidden="true" />}+ {checking ? localize('正在检查', 'Checking') : localize('检查状态', 'Check status')}+ </button> : null}+ {canDownload ? <button type="button" className="settings-primary-button" disabled={action !== undefined} onClick={() => void performAction(component.id, 'download', downloadComponents)}>+ {startingDownload ? <LoaderCircle className="settings-spin" size={15} aria-hidden="true" /> : <Download size={15} aria-hidden="true" />}+ {startingDownload ? localize('正在准备', 'Preparing') : component.state === 'failed' ? localize('重试下载', 'Retry download') : component.state === 'partial' ? localize('补齐组件', 'Complete installation') : localize('下载组件', 'Download components')}+ </button> : null}+ </div>+ {!selected ? <a href="#/settings?tab=ai-services§ion=transcription">{localize('调整转录设置', 'Change transcription settings')}</a> : null}+ </footer> : null}+ </article>;+ })}+ </div>;+}diff --git a/apps/web/src/features/settings/OpenCreatorSettingsView.tsx b/apps/web/src/features/settings/OpenCreatorSettingsView.tsxindex 2efc97c6..1a328e1b 100644--- a/apps/web/src/features/settings/OpenCreatorSettingsView.tsx+++ b/apps/web/src/features/settings/OpenCreatorSettingsView.tsx@@ -84,6 +84,8 @@ export type OpenCreatorSettingsViewProps = {codexStatus?: CodexStatusResponse;initialTab?: 'general' | 'ai-services' | 'local-components';initialSection?: CreatorServicesSection;+ componentId?: string;+ returnToTranslation?: boolean;onBack(): void;};@@ -188,12 +190,15 @@ export function OpenCreatorSettingsView(props: OpenCreatorSettingsViewProps) {service={props.creatorServicesService ?? null}modelService={props.codexRuntimeService ?? null}initialSection={props.initialSection}+ onOpenAgentSetup={props.onOpenAgentSetup}/></Suspense></>) : null}{activeTab === 'local-components' && props.runtimeDependencies !== undefined ? (<RuntimeComponentsSettingsView+ componentId={props.componentId}+ onReturn={props.returnToTranslation ? props.onBack : undefined}connected={props.runtimeStatus.connected}controller={props.runtimeDependencies}/>diff --git a/apps/web/src/features/settings/RuntimeComponentsSettingsView.tsx b/apps/web/src/features/settings/RuntimeComponentsSettingsView.tsxindex 800e889f..11a2455e 100644--- a/apps/web/src/features/settings/RuntimeComponentsSettingsView.tsx+++ b/apps/web/src/features/settings/RuntimeComponentsSettingsView.tsx@@ -9,12 +9,15 @@ import type { RuntimeDependenciesController } from '../../app/use-runtime-dependimport { IssueList } from '../issues/IssuePresenter.js';import { useAppLanguage } from '../../i18n/LanguageProvider.js';import { useEffect, useRef, useState } from 'react';+import { LocalTranscriptionComponents } from './LocalTranscriptionComponents.js';type LiveCheckState = 'pending' | 'checking' | 'ready' | 'failed';export function RuntimeComponentsSettingsView(props: {connected: boolean;controller: RuntimeDependenciesController;+ componentId?: string;+ onReturn?(): void;}) {const { language, t } = useAppLanguage();const status = props.controller.ytDlpStatus;@@ -61,6 +64,7 @@ export function RuntimeComponentsSettingsView(props: {return (<section className="settings-section settings-management" aria-labelledby="runtime-components-title"><SettingsHeader />+ <LocalTranscriptionComponents controller={props.controller} componentId={props.componentId} onReturn={props.onReturn} />{!hasIssue ? (<div className="settings-state"><LoaderCircle className="settings-spin" size={17} aria-hidden="true" />@@ -98,6 +102,7 @@ export function RuntimeComponentsSettingsView(props: {return (<section className="settings-section settings-management" aria-labelledby="runtime-components-title"><SettingsHeader />+ <LocalTranscriptionComponents controller={props.controller} componentId={props.componentId} onReturn={props.onReturn} /><article className="runtime-component-item"><div className="runtime-component-heading"><div>diff --git a/apps/web/src/features/settings/creator-services-settings.css b/apps/web/src/features/settings/creator-services-settings.cssindex ca594a15..ca686e91 100644--- a/apps/web/src/features/settings/creator-services-settings.css+++ b/apps/web/src/features/settings/creator-services-settings.css@@ -469,6 +469,13 @@background: var(--surface-2);}+.creator-services-codex-image-actions {+ display: flex;+ flex-wrap: wrap;+ gap: 8px;+ margin-top: 8px;+}+.creator-services-inline-note.is-warning {border-color: color-mix(in srgb, var(--warning) 45%, var(--border));background: color-mix(in srgb, var(--warning) 8%, var(--surface));diff --git a/apps/web/src/features/settings/local-transcription.css b/apps/web/src/features/settings/local-transcription.cssnew file mode 100644index 00000000..a0a5b8ce--- /dev/null+++ b/apps/web/src/features/settings/local-transcription.css@@ -0,0 +1,129 @@+.local-transcription-notice {+ display: flex;+ align-items: flex-start;+ justify-content: space-between;+ gap: 16px;+ padding: 14px 16px;+ border: 1px solid var(--border);+ border-radius: 12px;+ background: var(--surface);+ font-size: 13px;+}++.local-transcription-notice strong,+.local-transcription-notice small {+ display: block;+}++.local-transcription-notice small {+ margin-top: 4px;+ color: var(--muted);+}++.local-transcription-notice p {+ margin: 6px 0 0;+ line-height: 1.6;+}++.local-transcription-notice-actions {+ display: flex;+ flex-shrink: 0;+ flex-direction: column;+ gap: 8px;+}++.local-transcription-components {+ display: grid;+ gap: 16px;+ margin-bottom: 20px;+}++.local-transcription-components [data-highlight] {+ outline: 2px solid var(--accent, #6366f1);+ outline-offset: 2px;+}++.local-transcription-components dd {+ overflow-wrap: anywhere;+}++.local-transcription-components > button {+ justify-self: start;+}++.local-component-item {+ gap: 12px;+}++.local-component-details summary {+ display: flex;+ align-items: center;+ gap: 6px;+ width: fit-content;+ color: var(--muted);+ font-size: 13px;+ list-style: none;+ cursor: pointer;+}++.local-component-details summary::-webkit-details-marker {+ display: none;+}++.local-component-details summary:hover {+ color: var(--text);+}++.local-component-details summary:focus-visible {+ outline: 2px solid var(--accent);+ outline-offset: 4px;+ border-radius: 4px;+}++.local-component-details summary svg {+ transform: rotate(-90deg);+}++.local-component-details[open] summary svg {+ transform: none;+}++.local-component-details > dl {+ margin-top: 12px;+}++.local-component-models {+ margin: 0;+ padding-left: 16px;+}++.local-component-check-notice {+ color: var(--success);+}++.local-component-progress {+ display: grid;+ gap: 8px;+ margin: 12px 0;+}++.local-component-progress progress {+ width: 100%;+}++.creator-collaboration-stage-message {+ margin: 10px 0;+ line-height: 1.6;+ overflow-wrap: anywhere;+}++@media (max-width: 640px) {+ .local-transcription-notice {+ flex-direction: column;+ }++ .local-transcription-notice-actions {+ flex-direction: row;+ flex-wrap: wrap;+ }+}diff --git a/apps/web/src/features/settings/settings-management.css b/apps/web/src/features/settings/settings-management.cssindex d828f99b..bf1cde1d 100644--- a/apps/web/src/features/settings/settings-management.css+++ b/apps/web/src/features/settings/settings-management.css@@ -336,7 +336,7 @@}.runtime-component-heading > div,-.runtime-component-footer > div:first-child {+.runtime-component-footer > div:first-child:not(.runtime-component-actions) {min-width: 0;display: grid;gap: 5px;diff --git a/apps/web/src/runtime/client.ts b/apps/web/src/runtime/client.tsindex 52b7af4c..d2066be0 100644--- a/apps/web/src/runtime/client.ts+++ b/apps/web/src/runtime/client.ts@@ -25,8 +25,8 @@ export class RuntimeClient {this.fetchImpl = input.fetchImpl ?? fetch;}- async get<T = unknown>(path: string): Promise<T> {- return this.request<T>(path, { method: 'GET' });+ async get<T = unknown>(path: string, options: RuntimeRequestOptions = {}): Promise<T> {+ return this.request<T>(path, { method: 'GET', signal: options.signal });}async rawGet(path: string, options: RuntimeRequestOptions = {}): Promise<Response> {@@ -58,7 +58,7 @@ export class RuntimeClient {return this.request<T>(path, { method: 'DELETE' });}- async request<T>(path: string, input: { method: string; body?: unknown }): Promise<T> {+ async request<T>(path: string, input: { method: string; body?: unknown; signal?: AbortSignal }): Promise<T> {const response = await this.rawRequest(path, input);const payload = await readSuccessfulJson(response, path);return payload as T;diff --git a/apps/web/src/runtime/creator-sse.test.ts b/apps/web/src/runtime/creator-sse.test.tsindex 579d07a2..65dc7dd7 100644--- a/apps/web/src/runtime/creator-sse.test.ts+++ b/apps/web/src/runtime/creator-sse.test.ts@@ -1,7 +1,58 @@import { afterEach, describe, expect, it, vi } from 'vitest';import { createCreatorSnapshotSubscription } from './creator-sse.js';+import { ApiClientError } from './client.js';describe('creator snapshot subscription', () => {+ it('recovers automatically after the first snapshot fails', async () => {+ vi.useFakeTimers();+ const loadSnapshot = vi.fn().mockRejectedValueOnce(new TypeError('offline'))+ .mockResolvedValue({ revision: 2 });+ const subscribe = vi.fn(() => ({ close: vi.fn() }));+ const onState = vi.fn();+ const subscription = createCreatorSnapshotSubscription({+ loadSnapshot, subscribe, onSnapshot: vi.fn(), onState, reconnectDelays: [10]+ });+ await subscription.start();+ expect(onState).toHaveBeenLastCalledWith(expect.objectContaining({ status: 'reconnecting', attempt: 1 }));+ await vi.advanceTimersByTimeAsync(10);+ await subscription.whenIdle();+ expect(subscribe).toHaveBeenCalledTimes(1);+ expect(onState).toHaveBeenLastCalledWith(expect.objectContaining({ status: 'connected' }));+ subscription.close();+ });++ it.each([401, 403, 404])('does not retry terminal HTTP %s until explicitly requested', async status => {+ vi.useFakeTimers();+ const loadSnapshot = vi.fn().mockRejectedValueOnce(new ApiClientError({ status, code: 'FAILED', message: 'unavailable' }))+ .mockResolvedValue({ revision: 2 });+ const onState = vi.fn();+ const subscription = createCreatorSnapshotSubscription({+ loadSnapshot, subscribe: () => ({ close: vi.fn() }), onSnapshot: vi.fn(), onState+ });+ await subscription.start();+ await vi.advanceTimersByTimeAsync(10_000);+ expect(loadSnapshot).toHaveBeenCalledTimes(1);+ expect(onState).toHaveBeenLastCalledWith(expect.objectContaining({ status: 'failed' }));+ await subscription.retry();+ expect(loadSnapshot).toHaveBeenCalledTimes(2);+ subscription.close();+ });++ it('aborts an initial snapshot and cancels recovery when closed', async () => {+ vi.useFakeTimers();+ let signal: AbortSignal | undefined;+ const loadSnapshot = vi.fn((options: { signal: AbortSignal }) => new Promise<never>((_resolve, reject) => {+ signal = options.signal;+ signal.addEventListener('abort', () => reject(signal?.reason));+ }));+ const subscription = createCreatorSnapshotSubscription({ loadSnapshot, subscribe: vi.fn(), onSnapshot: vi.fn() });+ const started = subscription.start();+ subscription.close();+ await started;+ await vi.advanceTimersByTimeAsync(10_000);+ expect(signal?.aborted).toBe(true);+ expect(loadSnapshot).toHaveBeenCalledTimes(1);+ });afterEach(() => {vi.useRealTimers();});diff --git a/apps/web/src/runtime/creator-sse.ts b/apps/web/src/runtime/creator-sse.tsindex 1bf8613b..cb18fe8a 100644--- a/apps/web/src/runtime/creator-sse.ts+++ b/apps/web/src/runtime/creator-sse.ts@@ -1,10 +1,20 @@+import { ApiClientError } from './errors.js';+import { createRequestDeadline } from './request-deadline.js';++export type CreatorConnectionState = {+ status: 'connecting' | 'connected' | 'reconnecting' | 'failed';+ attempt: number;+ error?: unknown;+};+export function createCreatorSnapshotSubscription<T, Event = unknown>(input: {- loadSnapshot(): Promise<T>;- subscribe(onEvent: (event: Event) => void, onDisconnect: () => void): { close(): void };+ loadSnapshot(options: { signal: AbortSignal }): Promise<T>;+ subscribe(onEvent: (event: Event) => void, onDisconnect: (error?: unknown) => void): { close(): void };onSnapshot(snapshot: T): void;onEvent?(event: Event): void;shouldReloadSnapshot?(event: Event): boolean;onError?(error: unknown): void;+ onState?(state: CreatorConnectionState): void;reconnectDelays?: readonly number[];}) {const reconnectDelays = input.reconnectDelays ?? [250, 500, 1_000, 2_000, 5_000];@@ -15,15 +25,25 @@ export function createCreatorSnapshotSubscription<T, Event = unknown>(input: {let reloadScheduled = false;let reconnectAttempt = 0;let reconnectTimer: ReturnType<typeof setTimeout> | undefined;+ const controller = new AbortController();+ let lastError: unknown;const reloadSnapshot = async () => {if (closed) return;- const snapshot = await input.loadSnapshot();+ const deadline = createRequestDeadline(15_000, controller.signal);+ let snapshot: T;+ try {+ snapshot = await input.loadSnapshot({ signal: deadline.signal });+ } finally {+ deadline.dispose();+ }if (closed) return;input.onSnapshot(snapshot);};const connect = async () => {+ if (reconnectTimer !== undefined) clearTimeout(reconnectTimer);+ reconnectTimer = undefined;await reloadSnapshot();if (closed) return;connection?.close();@@ -33,8 +53,9 @@ export function createCreatorSnapshotSubscription<T, Event = unknown>(input: {input.onEvent?.(event);if (input.shouldReloadSnapshot?.(event) ?? true) scheduleReload();},- () => scheduleReconnect()+ error => error === undefined ? scheduleReconnect() : handleFailure(error));+ input.onState?.({ status: 'connected', attempt: 0 });};const scheduleReload = () => {@@ -54,11 +75,27 @@ export function createCreatorSnapshotSubscription<T, Event = unknown>(input: {}}).catch(error => {- input.onError?.(error);- if (reloadRequested) scheduleReload();+ handleFailure(error);});};+ const handleFailure = (error: unknown) => {+ if (closed) return;+ lastError = error;+ input.onError?.(error);+ if (error instanceof ApiClientError && error.status >= 400 && error.status < 500+ && ![408, 429].includes(error.status)) {+ if (reconnectTimer !== undefined) clearTimeout(reconnectTimer);+ reconnectTimer = undefined;+ reloadRequested = false;+ connection?.close();+ connection = undefined;+ input.onState?.({ status: 'failed', attempt: reconnectAttempt, error });+ return;+ }+ scheduleReconnect();+ };+const scheduleReconnect = () => {if (closed || reconnectTimer !== undefined) return;connection?.close();@@ -66,21 +103,29 @@ export function createCreatorSnapshotSubscription<T, Event = unknown>(input: {const delayIndex = Math.min(reconnectAttempt, Math.max(0, reconnectDelays.length - 1));const delay = reconnectDelays[delayIndex] ?? 5_000;reconnectAttempt += 1;+ input.onState?.({ status: 'reconnecting', attempt: reconnectAttempt, error: lastError });reconnectTimer = setTimeout(() => {reconnectTimer = undefined;if (closed) return;work = work.then(connect)- .catch(error => {- input.onError?.(error);- scheduleReconnect();- });+ .catch(handleFailure);}, delay);};return {async start(): Promise<void> {- work = connect();+ input.onState?.({ status: 'connecting', attempt: 0 });+ work = connect().catch(handleFailure);+ await work;+ },+ async retry(): Promise<void> {+ if (closed) return;+ if (reconnectTimer !== undefined) clearTimeout(reconnectTimer);+ reconnectTimer = undefined;+ reconnectAttempt = 0;+ lastError = undefined;+ work = work.then(connect).catch(handleFailure);await work;},async whenIdle(): Promise<void> {@@ -88,6 +133,7 @@ export function createCreatorSnapshotSubscription<T, Event = unknown>(input: {},close(): void {closed = true;+ controller.abort();if (reconnectTimer !== undefined) {clearTimeout(reconnectTimer);reconnectTimer = undefined;diff --git a/apps/web/src/runtime/request-deadline.test.ts b/apps/web/src/runtime/request-deadline.test.tsnew file mode 100644index 00000000..4e1a3742--- /dev/null+++ b/apps/web/src/runtime/request-deadline.test.ts@@ -0,0 +1,31 @@+import { afterEach, describe, expect, it, vi } from 'vitest';+import { createRequestDeadline } from './request-deadline.js';++afterEach(() => vi.useRealTimers());++describe('read-only Runtime request deadlines', () => {+ it('aborts with a timeout reason and releases timers after the request finishes', () => {+ vi.useFakeTimers();+ const deadline = createRequestDeadline(100);+ vi.advanceTimersByTime(100);+ expect(deadline.signal.aborted).toBe(true);+ expect(deadline.signal.reason.name).toBe('TimeoutError');+ deadline.dispose();+ const completed = createRequestDeadline(100);+ completed.dispose();+ vi.advanceTimersByTime(100);+ expect(completed.signal.aborted).toBe(false);+ expect(vi.getTimerCount()).toBe(0);+ });++ it('propagates cancellation, including an already canceled parent', () => {+ const parent = new AbortController();+ const deadline = createRequestDeadline(1_000, parent.signal);+ parent.abort();+ expect(deadline.signal.aborted).toBe(true);+ deadline.dispose();+ const canceled = createRequestDeadline(1_000, parent.signal);+ expect(canceled.signal.aborted).toBe(true);+ canceled.dispose();+ });+});diff --git a/apps/web/src/runtime/request-deadline.ts b/apps/web/src/runtime/request-deadline.tsnew file mode 100644index 00000000..884d342b--- /dev/null+++ b/apps/web/src/runtime/request-deadline.ts@@ -0,0 +1,14 @@+export function createRequestDeadline(timeoutMs: number, parent?: AbortSignal) {+ const controller = new AbortController();+ const onAbort = () => controller.abort(parent?.reason);+ if (parent?.aborted) onAbort();+ else parent?.addEventListener('abort', onAbort, { once: true });+ const timer = setTimeout(() => controller.abort(new DOMException('Runtime request timed out', 'TimeoutError')), timeoutMs);+ return {+ signal: controller.signal,+ dispose() {+ clearTimeout(timer);+ parent?.removeEventListener('abort', onAbort);+ }+ };+}diff --git a/apps/web/src/runtime/runtime-recovery.css b/apps/web/src/runtime/runtime-recovery.cssnew file mode 100644index 00000000..c1b0d88d--- /dev/null+++ b/apps/web/src/runtime/runtime-recovery.css@@ -0,0 +1,32 @@+.runtime-recovery-notice {+ margin: 12px;+ padding: 12px;+ border: 1px solid var(--border);+ border-radius: 8px;+ font-size: 13px;+}++.runtime-recovery-notice p {+ margin: 0 0 6px;+}++.runtime-recovery-notice small {+ display: block;+ color: var(--muted);+ overflow-wrap: anywhere;+}++.runtime-recovery-notice > div {+ display: flex;+ gap: 8px;+ margin-top: 8px;+}++.runtime-recovery-notice button {+ padding: 5px 10px;+ border: 1px solid var(--border);+ border-radius: 6px;+ background: var(--surface-2);+ color: inherit;+ cursor: pointer;+}diff --git a/apps/web/src/runtime/runtime-recovery.test.tsx b/apps/web/src/runtime/runtime-recovery.test.tsxnew file mode 100644index 00000000..d5c071c2--- /dev/null+++ b/apps/web/src/runtime/runtime-recovery.test.tsx@@ -0,0 +1,31 @@+import { fireEvent, render, screen, waitFor } from '@testing-library/react';+import { describe, expect, it, vi } from 'vitest';+import { RuntimeRecoveryContext, RuntimeRecoveryNotice } from './runtime-recovery.js';+import type { RuntimeRecovery } from './use-runtime-connection.js';++describe('Runtime recovery controls', () => {+ it('shows automatic recovery and invokes the real supported restart after confirmation', async () => {+ const retry = vi.fn(async () => undefined);+ const restart = vi.fn(async () => undefined);+ const recovery: RuntimeRecovery = {+ state: { status: 'disconnected', message: 'offline' }, phase: 'reconnecting', attempt: 2, epoch: 1, retry, restart+ };+ const view = render(<RuntimeRecoveryContext.Provider value={recovery}><RuntimeRecoveryNotice /></RuntimeRecoveryContext.Provider>);+ expect(screen.getByText(/正在自动重连/)).toBeVisible();+ expect(screen.getByText(/不会重复启动任务/)).toBeVisible();+ fireEvent.click(screen.getByRole('button', { name: '重新连接' }));+ await waitFor(() => expect(retry).toHaveBeenCalledTimes(1));+ fireEvent.click(screen.getByRole('button', { name: '重启本地服务' }));+ expect(restart).toHaveBeenCalledTimes(1);+ view.rerender(<RuntimeRecoveryContext.Provider value={{ ...recovery, restart: undefined }}><RuntimeRecoveryNotice /></RuntimeRecoveryContext.Provider>);+ expect(screen.queryByRole('button', { name: '重启本地服务' })).not.toBeInTheDocument();+ });++ it('distinguishes a failed session from a healthy Runtime', () => {+ render(<RuntimeRecoveryContext.Provider value={{ state: { status: 'connected' }, phase: 'connected', attempt: 0, epoch: 1, retry: vi.fn() }}>+ <RuntimeRecoveryNotice session={{ status: 'failed', attempt: 1 }} onRetrySession={vi.fn()} />+ </RuntimeRecoveryContext.Provider>);+ expect(screen.getByText('服务已连接,但项目会话恢复失败。')).toBeVisible();+ expect(screen.queryByText(/正在自动重连/)).not.toBeInTheDocument();+ });+});diff --git a/apps/web/src/runtime/runtime-recovery.tsx b/apps/web/src/runtime/runtime-recovery.tsxnew file mode 100644index 00000000..0c8d7470--- /dev/null+++ b/apps/web/src/runtime/runtime-recovery.tsx@@ -0,0 +1,64 @@+import { createContext, useContext } from 'react';+import { useLocalizedCopy } from '../i18n/useLocalizedCopy.js';+import type { CreatorConnectionState } from './creator-sse.js';+import type { RuntimeRecovery } from './use-runtime-connection.js';+import './runtime-recovery.css';++export const RuntimeRecoveryContext = createContext<RuntimeRecovery | null>(null);++export function useRuntimeRecovery() {+ return useContext(RuntimeRecoveryContext);+}++export function RuntimeRecoveryNotice(props: {+ session?: CreatorConnectionState;+ onRetrySession?: () => Promise<void>;+ diagnostics?: boolean;+}) {+ const recovery = useRuntimeRecovery();+ const l = useLocalizedCopy();+ const runtimeUnavailable = recovery !== null && recovery.phase !== 'connected';+ const sessionUnavailable = props.session !== undefined && props.session.status !== 'connected';+ const codexError = recovery?.state.status === 'connected' ? recovery.state.codexStatusError : undefined;+ if (!runtimeUnavailable && !sessionUnavailable && !props.diagnostics && codexError === undefined) return null;+ const message = runtimeUnavailable+ ? recovery.phase === 'failed'+ ? l('本地服务恢复失败,请重连或查看诊断。', 'Runtime recovery failed. Reconnect or check diagnostics.')+ : recovery.phase === 'restarting'+ ? l('正在重启本地服务…', 'Restarting the local Runtime…')+ : l('本地服务暂未连接,正在自动重连…', 'The local Runtime is unavailable. Reconnecting automatically…')+ : sessionUnavailable+ ? props.session?.status === 'failed'+ ? l('服务已连接,但项目会话恢复失败。', 'Runtime is connected, but the project session could not be restored.')+ : l('正在恢复项目会话与任务状态…', 'Restoring the project session and task status…')+ : codexError !== undefined+ ? l('本地服务已连接,Codex 状态暂时不可用,正在重新检查。', 'Runtime is connected. Codex status is temporarily unavailable and will be checked again.')+ : l('本地服务已连接。', 'The local Runtime is connected.');+ const attempt = runtimeUnavailable ? recovery?.attempt : props.session?.attempt;+ const retry = async () => {+ await recovery?.retry();+ await props.onRetrySession?.();+ };+ return (+ <section className="runtime-recovery-notice" role="status" aria-label={l('连接恢复', 'Connection recovery')}>+ <p>{message}</p>+ {runtimeUnavailable || sessionUnavailable ? <small>{l('保留当前草稿;恢复只同步状态,不会重复启动任务。', 'Your draft is retained. Recovery only synchronizes state; it does not restart tasks.')}{attempt ? ` · ${l('重试', 'Attempt')} ${attempt}` : ''}</small> : null}+ {props.diagnostics && recovery?.message ? <small>{recovery.message}</small> : null}+ {props.diagnostics && props.session?.error instanceof Error ? <small>{props.session.error.message}</small> : null}+ <div>+ {recovery !== null || props.onRetrySession !== undefined ? (+ <button type="button" disabled={recovery?.phase === 'restarting'} onClick={() => void retry().catch(() => undefined)}>+ {l('重新连接', 'Reconnect')}+ </button>+ ) : null}+ {recovery?.restart !== undefined && (runtimeUnavailable || sessionUnavailable || props.diagnostics) ? (+ <button type="button" disabled={recovery.phase === 'restarting'} onClick={() => {+ if (window.confirm(l('重启本地服务可能中断运行中的任务。草稿会保留,但任务不会自动重新提交。是否继续?', 'Restarting the Runtime may interrupt active tasks. Drafts are retained, but tasks will not be resubmitted. Continue?'))) {+ void recovery.restart!();+ }+ }}>{l('重启本地服务', 'Restart local Runtime')}</button>+ ) : null}+ </div>+ </section>+ );+}diff --git a/apps/web/src/runtime/use-runtime-connection.test.tsx b/apps/web/src/runtime/use-runtime-connection.test.tsxnew file mode 100644index 00000000..ad72e9b7--- /dev/null+++ b/apps/web/src/runtime/use-runtime-connection.test.tsx@@ -0,0 +1,96 @@+import { act, renderHook } from '@testing-library/react';+import { afterEach, describe, expect, it, vi } from 'vitest';+import type { HostBridge } from '../host/bridge.js';+import type { ConnectionConfig } from './types.js';+import { useRuntimeConnection } from './use-runtime-connection.js';++afterEach(() => vi.useRealTimers());++function fixture() {+ let online = true;+ let codexAvailable = true;+ let listener: ((config: ConnectionConfig | null) => void) | undefined;+ const config = { baseUrl: 'http://runtime.test', token: 'test-token' };+ const fetchImpl = vi.fn(async (url: string | URL | Request) => {+ if (!online) throw new TypeError('offline');+ if (String(url).endsWith('/codex/status') && !codexAvailable) throw new Error('Codex unavailable');+ return new Response(JSON.stringify({ ok: true }), { status: 200 });+ });+ const host = {+ kind: 'browser',+ readConnectionConfig: vi.fn(async () => online ? { ...config } : null),+ subscribeConnectionConfig(callback: typeof listener) {+ listener = callback;+ return () => { listener = undefined; };+ },+ openExternal: vi.fn(), revealPath: vi.fn(), notify: vi.fn(), configureBackgroundNotifications: vi.fn()+ } satisfies HostBridge;+ return { host, fetchImpl, setOnline(value: boolean) { online = value; }, setCodex(value: boolean) { codexAvailable = value; }, emit: () => listener?.(online ? config : null) };+}++async function settle() {+ await act(async () => { await vi.advanceTimersByTimeAsync(0); });+}++describe('Runtime connection recovery', () => {+ it('retries an initial failure and recovers without recreating the connection config', async () => {+ vi.useFakeTimers();+ const input = fixture();+ input.setOnline(false);+ const { result, unmount } = renderHook(() => useRuntimeConnection(input.host, input.fetchImpl));+ await settle();+ expect(result.current.recovery.phase).toBe('reconnecting');+ input.setOnline(true);+ await act(async () => { await vi.advanceTimersByTimeAsync(500); });+ expect(result.current.state.status).toBe('connected');+ const config = result.current.config;+ await act(async () => { await result.current.recovery.retry(); });+ expect(result.current.config).toBe(config);+ unmount();+ });++ it('retains services during null host config and periodically recovers after a later outage', async () => {+ vi.useFakeTimers();+ const input = fixture();+ const { result, unmount } = renderHook(() => useRuntimeConnection(input.host, input.fetchImpl));+ await settle();+ const config = result.current.config;+ const epoch = result.current.recovery.epoch;+ input.setOnline(false);+ await act(async () => { input.emit(); await vi.advanceTimersByTimeAsync(0); });+ expect(result.current.config).toBe(config);+ expect(result.current.recovery.phase).toBe('reconnecting');+ input.setOnline(true);+ await act(async () => { await vi.advanceTimersByTimeAsync(500); });+ expect(result.current.recovery.epoch).toBe(epoch + 1);+ expect(result.current.config).toBe(config);+ input.setOnline(false);+ await act(async () => { await vi.advanceTimersByTimeAsync(15_000); });+ expect(result.current.state.status).toBe('disconnected');+ input.setOnline(true);+ await act(async () => { await vi.advanceTimersByTimeAsync(500); });+ expect(result.current.state.status).toBe('connected');+ const calls = input.fetchImpl.mock.calls.length;+ unmount();+ await vi.advanceTimersByTimeAsync(30_000);+ expect(input.fetchImpl).toHaveBeenCalledTimes(calls);+ });++ it('keeps Codex errors separate and exposes restart only when supported', async () => {+ vi.useFakeTimers();+ const input = fixture();+ input.setCodex(false);+ const restartRuntime = vi.fn(async () => ({ ok: true as const }));+ const host = { ...input.host, restartRuntime };+ const { result, unmount } = renderHook(() => useRuntimeConnection(host, input.fetchImpl));+ await settle();+ expect(result.current.state.status).toBe('connected');+ expect(result.current.recovery.message).toBeDefined();+ await act(async () => { await result.current.recovery.restart?.(); });+ expect(restartRuntime).toHaveBeenCalledTimes(1);+ unmount();+ const browser = renderHook(() => useRuntimeConnection(input.host, input.fetchImpl));+ expect(browser.result.current.recovery.restart).toBeUndefined();+ browser.unmount();+ });+});diff --git a/apps/web/src/runtime/use-runtime-connection.ts b/apps/web/src/runtime/use-runtime-connection.tsnew file mode 100644index 00000000..8d50e2e7--- /dev/null+++ b/apps/web/src/runtime/use-runtime-connection.ts@@ -0,0 +1,148 @@+import { useCallback, useEffect, useRef, useState } from 'react';+import type { HostBridge } from '../host/bridge.js';+import { createConnectionService, type ConnectionState } from '../services/connection-service.js';+import { RuntimeClient } from './client.js';+import type { ConnectionConfig } from './types.js';++export type RuntimeRecovery = {+ state: ConnectionState;+ phase: 'connecting' | 'connected' | 'reconnecting' | 'failed' | 'restarting';+ attempt: number;+ epoch: number;+ message?: string;+ retry(): Promise<void>;+ restart?: () => Promise<void>;+};++export function useRuntimeConnection(host: HostBridge, fetchImpl: typeof fetch) {+ const [config, setConfig] = useState<ConnectionConfig | null>(null);+ const [state, setState] = useState<ConnectionState>({ status: 'disconnected', message: '正在连接本地服务' });+ const [progress, setProgress] = useState<Omit<RuntimeRecovery, 'state' | 'retry' | 'restart'>>({+ phase: 'connecting', attempt: 0, epoch: 0+ });+ const refreshRef = useRef<() => Promise<void>>(async () => undefined);+ const restartWorkRef = useRef<Promise<void>>();++ useEffect(() => {+ let closed = false;+ let timer: ReturnType<typeof setTimeout> | undefined;+ let controller: AbortController | undefined;+ let currentConfig: ConnectionConfig | null = null;+ let connected = false;+ let attempt = 0;+ let epoch = 0;+ let version = 0;+ const delays = [500, 1_000, 2_000, 5_000];++ const refresh = async (announcedConfig?: ConnectionConfig | null) => {+ if (closed) return;+ if (timer !== undefined) clearTimeout(timer);+ controller?.abort();+ const activeController = new AbortController();+ controller = activeController;+ const activeVersion = ++version;+ const active = () => !closed && activeVersion === version;+ let next: ConnectionState;+ try {+ const nextConfig = announcedConfig === undefined ? await readConfig(host, activeController.signal) : announcedConfig;+ if (!active()) return;+ if (nextConfig !== null) {+ currentConfig = nextConfig;+ setConfig(previous => sameConfig(previous, nextConfig) ? previous : nextConfig);+ }+ next = currentConfig === null+ ? { status: 'disconnected', message: '正在等待本地服务启动' }+ : await createConnectionService(new RuntimeClient({ ...currentConfig, fetchImpl }))+ .check({ signal: activeController.signal });+ } catch (error) {+ next = { status: 'disconnected', message: error instanceof Error ? error.message : '本地服务连接失败' };+ }+ if (!active()) return;+ setState(next);+ if (next.status === 'connected') {+ if (!connected) epoch += 1;+ connected = true;+ attempt = 0;+ setProgress({ phase: 'connected', attempt, epoch, message: next.codexStatusError });+ const delay = next.codexStatusError !== undefined || next.codexStatus?.availabilityProbe?.status === 'pending'+ ? 1_500 : 15_000;+ timer = setTimeout(() => void refresh(), delay);+ } else {+ connected = false;+ attempt += 1;+ setProgress({ phase: next.status === 'invalid_token' ? 'failed' : 'reconnecting', attempt, epoch, message: next.message });+ if (next.status !== 'invalid_token') {+ timer = setTimeout(() => void refresh(), delays[Math.min(attempt - 1, delays.length - 1)]);+ }+ }+ };+ refreshRef.current = refresh;+ const unsubscribe = host.subscribeConnectionConfig?.(nextConfig => {+ if (closed) return;+ if (nextConfig === null) {+ connected = false;+ setState({ status: 'disconnected', message: '本地服务暂时断开,正在重连' });+ setProgress(previous => ({ ...previous, phase: 'reconnecting' }));+ } else {+ currentConfig = nextConfig;+ setConfig(previous => sameConfig(previous, nextConfig) ? previous : nextConfig);+ }+ void refresh(nextConfig);+ });+ void refresh();+ return () => {+ closed = true;+ version += 1;+ controller?.abort();+ if (timer !== undefined) clearTimeout(timer);+ unsubscribe?.();+ };+ }, [host, fetchImpl]);++ const retry = useCallback(() => refreshRef.current(), []);+ const restart = useCallback(() => {+ if (restartWorkRef.current !== undefined) return restartWorkRef.current;+ if (host.restartRuntime === undefined) return Promise.resolve();+ setProgress(previous => ({ ...previous, phase: 'restarting', message: undefined }));+ const work = (async () => {+ try {+ const result = await host.restartRuntime!();+ if (!result.ok) throw new Error(result.message);+ await refreshRef.current();+ } catch (error) {+ setProgress(previous => ({ ...previous, phase: 'failed', message: error instanceof Error ? error.message : '重启本地服务失败' }));+ } finally {+ restartWorkRef.current = undefined;+ }+ })();+ restartWorkRef.current = work;+ return work;+ }, [host]);++ return {+ config,+ state,+ recovery: { ...progress, state, retry, ...(host.restartRuntime === undefined ? {} : { restart }) } satisfies RuntimeRecovery+ };+}++function sameConfig(previous: ConnectionConfig | null, next: ConnectionConfig): boolean {+ return previous?.baseUrl === next.baseUrl && previous?.token === next.token;+}++function readConfig(host: HostBridge, signal: AbortSignal): Promise<ConnectionConfig | null> {+ return new Promise((resolve, reject) => {+ const timer = setTimeout(() => finish(() => reject(new Error('读取本地服务配置超时'))), 4_000);+ const onAbort = () => finish(() => reject(signal.reason));+ function finish(callback: () => void) {+ clearTimeout(timer);+ signal.removeEventListener('abort', onAbort);+ callback();+ }+ signal.addEventListener('abort', onAbort, { once: true });+ void host.readConnectionConfig().then(+ value => finish(() => resolve(value)),+ error => finish(() => reject(error))+ );+ });+}diff --git a/apps/web/src/services/connection-service.test.ts b/apps/web/src/services/connection-service.test.tsindex 35241b88..b4307281 100644--- a/apps/web/src/services/connection-service.test.ts+++ b/apps/web/src/services/connection-service.test.ts@@ -1,8 +1,38 @@-import { describe, expect, it } from 'vitest';-import { ApiClientError, type RuntimeClient } from '../runtime/client.js';+import { afterEach, describe, expect, it, vi } from 'vitest';+import { ApiClientError, RuntimeClient } from '../runtime/client.js';import { createConnectionService } from './connection-service.js';describe('ConnectionService', () => {+ afterEach(() => vi.useRealTimers());++ it.each([true, false])('bounds stalled health/Codex reads without timing out business requests (health=%s)', async healthy => {+ vi.useFakeTimers();+ const fetchImpl = vi.fn((url: string | URL | Request, options?: RequestInit) => {+ if (healthy && String(url).endsWith('/healthz')) return Promise.resolve(new Response('{}'));+ return new Promise<Response>((_resolve, reject) => {+ options?.signal?.addEventListener('abort', () => reject(options.signal?.reason));+ });+ });+ const client = new RuntimeClient({ baseUrl: 'http://runtime.test', fetchImpl });+ const result = createConnectionService(client).check();+ await vi.advanceTimersByTimeAsync(4_000);+ expect((await result).status).toBe(healthy ? 'connected' : 'disconnected');+ expect(vi.getTimerCount()).toBe(0);+ });+ it('keeps a healthy Runtime connected when Codex status is unavailable', async () => {+ const get = vi.fn().mockResolvedValueOnce({ ok: true }).mockRejectedValueOnce(new Error('Codex unavailable'));+ const service = createConnectionService({ get, post: vi.fn(), patch: vi.fn() });+ await expect(service.check()).resolves.toEqual({ status: 'connected', codexStatusError: 'Codex unavailable' });+ expect(get.mock.calls[0]?.[1]?.signal).toBeInstanceOf(AbortSignal);+ });++ it('keeps authentication errors distinct even when health is successful', async () => {+ const get = vi.fn().mockResolvedValueOnce({ ok: true }).mockRejectedValueOnce(+ new ApiClientError({ status: 401, code: 'UNAUTHORIZED', message: 'Unauthorized' })+ );+ const service = createConnectionService({ get, post: vi.fn(), patch: vi.fn() });+ await expect(service.check()).resolves.toEqual({ status: 'invalid_token', message: 'Unauthorized' });+ });it('returns invalid_token for unauthorized Runtime responses', async () => {const client = {async get<T>(): Promise<T> {diff --git a/apps/web/src/services/connection-service.ts b/apps/web/src/services/connection-service.tsindex f5059daf..1ee91e6d 100644--- a/apps/web/src/services/connection-service.ts+++ b/apps/web/src/services/connection-service.ts@@ -11,21 +11,28 @@ import type {CodexStatusResponse} from '@opencreator/protocol';import { ApiClientError, type RuntimeClient } from '../runtime/client.js';+import { createRequestDeadline } from '../runtime/request-deadline.js';export type ConnectionState =| { status: 'disconnected'; message: string }- | { status: 'connected'; codexStatus: CodexStatusResponse }+ | { status: 'connected'; codexStatus?: CodexStatusResponse; codexStatusError?: string }| { status: 'invalid_token'; message: string };type ClientLike = Pick<RuntimeClient, 'get' | 'post' | 'patch'>;export function createConnectionService(client: ClientLike) {return {- async check(): Promise<ConnectionState> {+ async check(options: { signal?: AbortSignal } = {}): Promise<ConnectionState> {+ const read = async <T,>(path: string): Promise<T> => {+ const deadline = createRequestDeadline(4_000, options.signal);+ try {+ return await client.get<T>(path, { signal: deadline.signal });+ } finally {+ deadline.dispose();+ }+ };try {- await client.get('/healthz');- const codexStatus = await client.get<CodexStatusResponse>('/codex/status');- return { status: 'connected', codexStatus };+ await read('/healthz');} catch (error) {const message = errorMessage(error);if (error instanceof ApiClientError && (error.status === 401 || error.status === 403)) {@@ -33,6 +40,16 @@ export function createConnectionService(client: ClientLike) {}return { status: 'disconnected', message };}+ try {+ const codexStatus = await read<CodexStatusResponse>('/codex/status');+ return { status: 'connected', codexStatus };+ } catch (error) {+ const message = errorMessage(error);+ if (error instanceof ApiClientError && (error.status === 401 || error.status === 403)) {+ return { status: 'invalid_token', message };+ }+ return { status: 'connected', codexStatusError: message };+ }},getCodexReadiness(): Promise<CodexRuntimeReadiness> {return client.get<CodexRuntimeReadiness>('/codex/readiness');diff --git a/apps/web/src/services/creator-service.test.ts b/apps/web/src/services/creator-service.test.tsindex 9dad3acc..0077ab50 100644--- a/apps/web/src/services/creator-service.test.ts+++ b/apps/web/src/services/creator-service.test.ts@@ -214,6 +214,6 @@ function subscribeUntilDisconnect(onEvent: Parameters<ReturnType<typeof createCreatorService>['subscribeJobEvents']>[1]): Promise<void> {return new Promise(resolve => {- service.subscribeJobEvents(jobId, onEvent, resolve);+ service.subscribeJobEvents(jobId, onEvent, () => resolve());});}diff --git a/apps/web/src/services/creator-service.ts b/apps/web/src/services/creator-service.tsindex 0f24a977..d49e9eb9 100644--- a/apps/web/src/services/creator-service.ts+++ b/apps/web/src/services/creator-service.ts@@ -27,7 +27,7 @@ import type {} from '@opencreator/protocol';type ClientLike = {- get(path: string): Promise<unknown>;+ get(path: string, options?: { signal?: AbortSignal }): Promise<unknown>;post(path: string, body?: unknown): Promise<unknown>;delete(path: string): Promise<unknown>;postBinary?(path: string, body: BodyInit, contentType?: string): Promise<unknown>;@@ -86,8 +86,8 @@ export function createCreatorService(client: ClientLike) {? '/creator/jobs': `/creator/jobs?projectId=${encodeURIComponent(projectId)}`) as Promise<CreatorJobListResponse>;},- getJob(jobId: string): Promise<{ job: CreatorJob }> {- return client.get(`/creator/jobs/${encodeURIComponent(jobId)}`) as Promise<{ job: CreatorJob }>;+ getJob(jobId: string, options?: { signal?: AbortSignal }): Promise<{ job: CreatorJob }> {+ return client.get(`/creator/jobs/${encodeURIComponent(jobId)}`, options) as Promise<{ job: CreatorJob }>;},preflight(jobId: string, stageId: string): Promise<CreatorPreflightResponse> {return client.get(`/creator/jobs/${encodeURIComponent(jobId)}/preflight?stageId=${encodeURIComponent(stageId)}`) as Promise<CreatorPreflightResponse>;@@ -254,7 +254,7 @@ export function createCreatorService(client: ClientLike) {subscribeJobEvents(jobId: string,onEvent: (event: CreatorEventEnvelope) => void,- onDisconnect: () => void+ onDisconnect: (error?: unknown) => void): { close(): void } {const controller = new AbortController();void (async () => {@@ -267,7 +267,7 @@ export function createCreatorService(client: ClientLike) {{ signal: controller.signal });const reader = response.body?.getReader();- if (reader === undefined) return;+ if (reader === undefined) throw new Error('Creator event stream is unavailable');const decoder = new TextDecoder();let buffered = '';while (!controller.signal.aborted) {@@ -292,8 +292,8 @@ export function createCreatorService(client: ClientLike) {}}if (!controller.signal.aborted) onDisconnect();- })().catch(() => {- if (!controller.signal.aborted) onDisconnect();+ })().catch(error => {+ if (!controller.signal.aborted) onDisconnect(error);});return { close: () => controller.abort() };}diff --git a/apps/web/src/services/creator-services-service.test.ts b/apps/web/src/services/creator-services-service.test.tsindex fe9332b3..f8cd25c0 100644--- a/apps/web/src/services/creator-services-service.test.ts+++ b/apps/web/src/services/creator-services-service.test.ts@@ -49,6 +49,7 @@ describe('creator services service', () => {await service.saveConfig(config);await service.resetConfig();await service.getTtsVoices('aliyun', 'qwen3-tts-flash');+ await service.getCodexImageStatus();await service.previewTtsVoice({provider: 'aliyun',model: 'qwen3-tts-flash',@@ -63,6 +64,7 @@ describe('creator services service', () => {3,'/creator-services/tts/voices?provider=aliyun&model=qwen3-tts-flash');+ expect(get).toHaveBeenNthCalledWith(4, '/creator-services/image/codex/status');expect(rawRequest).toHaveBeenCalledWith('/creator-services/tts/preview', {method: 'POST',body: {diff --git a/apps/web/src/services/creator-services-service.ts b/apps/web/src/services/creator-services-service.tsindex 4cbb4b6f..f8e2e9d0 100644--- a/apps/web/src/services/creator-services-service.ts+++ b/apps/web/src/services/creator-services-service.ts@@ -1,4 +1,5 @@import type {+ CodexImageStatus,CreatorServicesCapabilitiesResponse,CreatorServicesConfig,CreatorServicesConfigResponse,@@ -10,10 +11,15 @@ import type { RuntimeClient } from '../runtime/client.js';type ClientLike = Pick<RuntimeClient, 'get' | 'patch' | 'delete' | 'rawRequest'>;-export type CreatorServicesSettingsService = ReturnType<typeof createCreatorServicesService>;+export type CreatorServicesSettingsService = Omit<ReturnType<typeof createCreatorServicesService>, 'getCodexImageStatus'> & {+ getCodexImageStatus?: () => Promise<CodexImageStatus>;+};export function createCreatorServicesService(client: ClientLike) {return {+ getCodexImageStatus(): Promise<CodexImageStatus> {+ return client.get('/creator-services/image/codex/status');+ },getCapabilities(): Promise<CreatorServicesCapabilitiesResponse> {return client.get('/creator-services/capabilities');},diff --git a/apps/web/src/services/runtime-dependency-service.ts b/apps/web/src/services/runtime-dependency-service.tsindex cc8b6aa2..462f8c29 100644--- a/apps/web/src/services/runtime-dependency-service.ts+++ b/apps/web/src/services/runtime-dependency-service.ts@@ -1,4 +1,4 @@-import type { CreatorYtDlpStatusResponse } from '@opencreator/protocol';+import type { CreatorRuntimeComponentsResponse, CreatorYtDlpStatusResponse } from '@opencreator/protocol';type ClientLike = {get(path: string): Promise<unknown>;@@ -7,6 +7,12 @@ type ClientLike = {export function createRuntimeDependencyService(client: ClientLike) {return {+ getComponentsStatus(): Promise<CreatorRuntimeComponentsResponse> {+ return client.get('/creator/components/status') as Promise<CreatorRuntimeComponentsResponse>;+ },+ downloadComponents(): Promise<CreatorRuntimeComponentsResponse> {+ return client.post('/creator/components/download') as Promise<CreatorRuntimeComponentsResponse>;+ },getYtDlpStatus(): Promise<CreatorYtDlpStatusResponse> {return client.get('/creator/yt-dlp/status') as Promise<CreatorYtDlpStatusResponse>;},diff --git a/docs/development/codex-image-generation.md b/docs/development/codex-image-generation.mdnew file mode 100644index 00000000..3642451a--- /dev/null+++ b/docs/development/codex-image-generation.md@@ -0,0 +1,36 @@+# 本机 Codex 生图++图像设置中的“本机 Codex 生图”根据当前 Runtime 的认证模式分流,所有图像、封面、文章配图和火柴人镜头共用同一实现。++## API Key 模式++- 保留现有配置的 Base URL、API Key 和图片模型,调用 `/images/generations` 或带参考图的 `/images/edits`。+- 文本模型不是图片模型时,保持原有 `gpt-image-1` 默认值。+- 接口必须支持图片生成。设置状态检查只检查本地配置,不会发送收费的生成请求。++## ChatGPT 登录模式++- 通过当前 Runtime 的 Codex CLI 原生生图工具执行,不把 OAuth 凭据当作图片 API Key。+- 使用原始 `CODEX_HOME` 的文件认证,由 Codex 负责登录凭据刷新;不复制或改写用户认证。+- 忽略用户自定义接口和模型配置,移除子进程继承的 `OPENAI_API_KEY`、`OPENAI_BASE_URL`、`CODEX_API_KEY`、`CODEX_ACCESS_TOKEN`,避免误走其他认证通道。+- 使用独立的临时工作区,关闭 shell、MCP、插件、浏览器等无关工具;参考图片通过附件传入。+- 只接受当前请求工作区或当前线程专属目录中的新图片,校验路径、大小与格式。不会扫描其他线程图片或把文字回复视作生图成功。+- 原生工具失败时不回退到 API 通道。登录失效、额度限制和超时有明确提示,取消任务会终止子进程。++## 状态与进度++- `GET /creator-services/image/codex/status` 只读检查认证模式、Runtime 版本和原生工具能力,不返回凭据。+- 设置页展示检查结果,并通过现有“配置 Agent”入口处理登录;刷新状态会重新检查当前认证。+- Creator 阶段预检查会阻止缺少登录配置或不支持原生生图的 Runtime。+- 共用协作面板展示“准备 ChatGPT 登录态生图”“生成图片”“整理输出文件”及说明;未获得真实百分比时不编造进度。+- 本地凭据和工具检查通过不等于账号权限、额度或上游接口已在线验证,这些以真实生成结果为准。++## 定向验证++```sh+pnpm --filter @opencreator/protocol build+pnpm --filter @opencreator/daemon exec vitest run test/unit/codex-local-provider.test.ts test/unit/codex-argv.test.ts test/unit/codex-native-image.test.ts test/unit/image-generation-codex-native.test.ts test/unit/creator-preflight.test.ts test/integration/creator-services-api.test.ts test/integration/image-generation-api.test.ts test/integration/creator-image-generation.test.ts+pnpm --filter @opencreator/web exec vitest run src/features/settings/CodexImageStatusNotice.test.tsx src/features/settings/CreatorServicesSettingsView.test.tsx src/features/dashboard/creator-panel-adapters.test.ts src/services/creator-services-service.test.ts+```++自动测试使用模拟 Codex 子进程,覆盖认证分流、进度、产物、错误与取消,不构成真实 ChatGPT 账号生图验收。真实验收应在用户已经登录的 Runtime 上执行;不要替换用户当前的 API Key 配置来制造登录态测试。diff --git a/docs/development/runtime-connection-recovery.md b/docs/development/runtime-connection-recovery.mdnew file mode 100644index 00000000..55eccd82--- /dev/null+++ b/docs/development/runtime-connection-recovery.md@@ -0,0 +1,32 @@+# Creator Runtime 连接恢复++## 问题与边界++- 旧面板把没有 Creator Context 显示为 Runtime 未连接。缺少项目上下文与服务断线是两类问题;截图中具体触发上下文丢失的原因并未实锤。+- 首次 Creator 快照请求失败时,以前不会进入 SSE 重连;错误被调用方吞掉,会话一直停在未订阅状态。+- Runtime 健康检查与 Codex 状态读取以前共用失败分支,Codex 暂时不可用会误报整个服务断开。+- Desktop 以前仅自动重启一次,并在 Runtime 恢复时重新加载整个工作区。正式工作区没有接通已有的宿主重启能力。++## 现在的恢复流程++1. Web 与 Desktop 共用 `useRuntimeConnection`,定期检查健康状态。断线按 0.5、1、2、5 秒退避,延迟上限为 5 秒;健康连接每 15 秒检查一次。+2. 健康请求和 Codex 状态请求分别限制为 4 秒。健康成功但 Codex 状态失败时,保留 Runtime 已连接状态,并单独显示 Codex 告警。此超时不用于生成、下载、Whisper 准备或其他长任务。+3. 初次 Creator 快照和后续订阅断开共用恢复逻辑。快照读取限制为 15 秒;临时网络错误自动恢复,认证、项目不存在等不可自动恢复的 4xx 错误停止重试,等待用户操作。+4. 连接恢复只读取权威快照和 Agent 时间线、重新订阅事件。不得自动提交 `run-stage`、Agent turn 或其他可能产生重复费用的业务操作。+5. 暂时没有宿主连接配置时保留已有 Service 和项目数据,避免卸载 Workspace。Creator Context 放在独立模块中,并在开发热更新时保留身份。+6. Desktop 异常退出最多自动重启 3 次,分别等待 0.5、1.5、3 秒;稳定运行 5 分钟或成功手动重启后重置预算。退出应用时取消尚未执行的恢复。+7. 已进入工作区的 Desktop 恢复只通知连接变更,不整页刷新,保留表单和 Composer 草稿。被进程退出中断的任务仍需用户主动继续或重试。++## 用户入口++- 通用 Creator 协作面板显示连接中、自动重连、项目恢复失败及重试次数,并提供“重新连接”。+- 支持宿主重启能力的 Desktop 在连接提示和“设置 → 诊断”提供“重启本地服务”;浏览器不展示无效重启按钮。+- 重启前明确提醒可能中断运行中的任务,不会自动重新提交任务。+- Context 缺失时提示“项目会话尚未初始化”,不再声称 Runtime 已断开。++## 验证方式++- Web 定向测试覆盖初始失败、断线恢复、终止错误、超时和取消、两种 Host 下的业务草稿与 Composer 草稿保留及不重发任务。+- Desktop 单元测试覆盖有限恢复预算、启动失败重试、手动重启重置预算和退出时取消恢复。+- 实际打包 App E2E 反复终止隔离 Daemon,验证自动恢复、预算耗尽后的真实手动重启、草稿保留、Renderer 未重新加载以及不重复执行 Codex Probe。+- `desktop:package` 重建 Web,并校验 App 内 Web 文件列表和哈希与本次 `apps/web/dist` 一致。此验证不意味着其他平台的安装包已验收,也不替换用户已安装的 App。diff --git a/docs/development/video-translation-components.md b/docs/development/video-translation-components.mdnew file mode 100644index 00000000..96c3a1f9--- /dev/null+++ b/docs/development/video-translation-components.md@@ -0,0 +1,30 @@+# Video translation: media and local transcription components++Video translation first uses imported SRT or available original-language platform captions. Successful caption translation does not download audio, video, or local transcription dependencies. Caption-processing/translation failures do not trigger transcription. When captions are unavailable, transcription downloads the audio track; the configured local engine and model are installed on demand if necessary.++Original video preparation is deferred until composition. Audio-only dubbing does not require video. Composition can reuse audio-only dubbing and original media cached within the same job; retries and the second aspect ratio do not repeat a completed source download.++For URL inputs, reusable source-video artifacts must have a matching source URL in their settings snapshot. Videos from a different URL or legacy artifacts without provenance are ignored rather than applied to a new source. Local-file inputs are unchanged.++Local component installation is owned by the Daemon, not a page or individual task. Manual downloads and task preparation share installation and progress. Canceling a waiting task detaches that subscriber without aborting another task's shared download. Daemon shutdown cancels active component downloads.++## Bilibili multipart inputs++Bilibili `?p=N` selects a single part. The shared URL parser retains that selection while removing tracking parameters; the embedded player uses `p=N`, not `page=1`. Metadata includes the part list, selected part title, CID, duration, and that part's dimensions. An explicit part is preselected. A multipart link without `p` asks the user to choose a part before continuing; a single-part video can continue without that extra selection.++Part lookup has a visible loading state and retry action. Invalid or out-of-range part parameters do not fall back to P1. Failed lookup does not start translation. The workflow API and shared Agent preflight both validate the selection, so Agent requests cannot bypass the UI. Collection metadata is cached for 30 seconds to avoid repeating the same upstream request for preview and validation.++Audio preparation, deferred video composition, and title/description lookup use `--no-playlist`. KrillinAI URL normalization preserves an explicit part for both audio and video. Direct CLI calls without a part are defensively limited to P1; the product workflow requires an explicit selection for multipart videos and never starts a whole-collection download.++## API++- `GET /creator/components/status`: Runtime platform/architecture, configured transcription provider/model, installed engine version, supported version, model inventory, installation status, and live byte progress.+- `POST /creator/components/download`: asynchronously prepare the currently configured local provider/model. Unsupported providers are rejected. Existing verified resources are reused.++Download percentage is based on actual transferred bytes and the final HTTP response's content length. Unknown totals remain indeterminate. Verification and extraction are separate phases; only verified installation is ready. Installed component inventory is reconstructed from disk on startup; unfinished temporary downloads are not advertised as installed.++The video translation notice links to `#/settings?tab=local-components`, preserving its draft, selected file, and an internal return route. The shared collaboration panel explains why component preparation is needed, that large model downloads may take time, and that transcription continues automatically. Component percentages are explicitly distinguished from translation progress.++Managed engine versions are supported, pinned versions, not claims about the latest upstream releases. Component management does not offer arbitrary upstream engine upgrades or platform-specific UI forks.++Component cards show only engines available on the current Runtime platform. The compact `Check status` action is read-only and uses the status endpoint; it never starts an installation. Download, complete-installation, and retry actions are available only when the selected component is not ready. Engine version and current model remain visible, while platform, install location, resource source, and model inventory are folded into component details. Download progress and preparation warnings stay outside the disclosure.diff --git a/packages/protocol/src/creator-services.ts b/packages/protocol/src/creator-services.tsindex c0d8c278..35298415 100644--- a/packages/protocol/src/creator-services.ts+++ b/packages/protocol/src/creator-services.ts@@ -168,6 +168,15 @@ export type CreatorServicesCapabilitiesResponse = {};};+export type CodexImageStatus = {+ authentication: 'api_key' | 'chatgpt' | 'none';+ ready: boolean;+ executionMode: 'api' | 'native' | null;+ message: string;+ model?: string;+ version?: string;+};+export type CreatorPreflightExecutionMode = 'local' | 'remote' | 'mixed';export type CreatorPreflightRepair = {diff --git a/packages/protocol/src/creator.ts b/packages/protocol/src/creator.tsindex 58d30a4c..d2909a89 100644--- a/packages/protocol/src/creator.ts+++ b/packages/protocol/src/creator.ts@@ -379,6 +379,36 @@ export type CreatorYtDlpStatusResponse = {ytDlp: CreatorYtDlpStatus;};+export type CreatorLocalComponent = {+ id: 'whisperkit' | 'whisper.cpp' | 'faster-whisper';+ name: string;+ available: boolean;+ version: string | null;+ supportedVersion: string | null;+ installedAt: string | null;+ path: string;+ source: string;+ models: Array<{ id: string; installed: boolean; bytes: number | null }>;+ state: 'not_installed' | 'partial' | 'downloading' | 'verifying' | 'extracting' | 'ready' | 'failed' | 'unsupported';+ model: string | null;+ item: string | null;+ downloadedBytes: number;+ totalBytes: number | null;+ percent: number | null;+ bytesPerSecond: number | null;+ remainingSeconds: number | null;+ error: string | null;+ message?: string;+};++export type CreatorRuntimeComponentsResponse = {+ platform: string;+ arch: string;+ selectedProvider: string;+ selectedModel: string | null;+ components: CreatorLocalComponent[];+};+export type CreatorSelection = {kind: string;id?: string;diff --git a/packages/protocol/src/errors.ts b/packages/protocol/src/errors.tsindex 67fa8c6d..01cc23ca 100644--- a/packages/protocol/src/errors.ts+++ b/packages/protocol/src/errors.ts@@ -107,6 +107,9 @@ export type RuntimeErrorCode =| 'VIDEO_GENERATION_NOT_READY'| 'VIDEO_GENERATION_STORAGE_FAILED'| 'VIDEO_METADATA_UNAVAILABLE'+ | 'creator_source_part_required'+ | 'creator_source_invalid_part'+ | 'creator_source_metadata_unavailable'| 'creator_job_not_found'| 'creator_job_has_active_run'| 'creator_job_not_running'@@ -132,6 +135,8 @@ export type RuntimeErrorCode =| 'creator_tts_upstream_error'| 'creator_dependency_prepare_failed'| 'creator_yt_dlp_update_unavailable'+ | 'creator_components_unavailable'+ | 'creator_component_download_unavailable'| 'creator_yt_dlp_update_check_failed'| 'creator_yt_dlp_update_download_failed'| 'creator_yt_dlp_update_verification_failed'diff --git a/packages/protocol/src/video-metadata.ts b/packages/protocol/src/video-metadata.tsindex ca9c488f..d61c2e73 100644--- a/packages/protocol/src/video-metadata.ts+++ b/packages/protocol/src/video-metadata.ts@@ -1,5 +1,14 @@export type VideoMetadataPlatform = 'youtube' | 'bilibili';+export type VideoSourcePart = {+ index: number;+ title: string;+ cid?: number;+ durationSeconds?: number;+ width?: number;+ height?: number;+};+export type VideoMetadataResponse = {platform: VideoMetadataPlatform;title: string;@@ -7,4 +16,33 @@ export type VideoMetadataResponse = {thumbnailUrl?: string;width?: number;height?: number;+ parts?: VideoSourcePart[];+ selectedPart?: VideoSourcePart;};++export function parseBilibiliVideoSource(value: string): {+ videoId: string;+ partIndex?: number;+ url: string;+} | null {+ try {+ const source = new URL(value.trim());+ const host = source.hostname.toLowerCase();+ if ((source.protocol !== 'https:' && source.protocol !== 'http:')+ || (host !== 'bilibili.com' && !host.endsWith('.bilibili.com'))) return null;+ const videoId = source.pathname.match(/^\/video\/(BV[A-Za-z0-9]+|av\d+)\/?$/)?.[1];+ if (!videoId) return null;+ const parts = source.searchParams.getAll('p');+ if (parts.length > 1) return null;+ const partIndex = parts.length === 0 ? undefined : Number(parts[0]);+ if (partIndex !== undefined && (!/^[1-9]\d*$/.test(parts[0]!)+ || !Number.isSafeInteger(partIndex))) return null;+ return {+ videoId,+ ...(partIndex === undefined ? {} : { partIndex }),+ url: `https://www.bilibili.com/video/${videoId}${partIndex === undefined ? '' : `?p=${partIndex}`}`+ };+ } catch {+ return null;+ }+}diff --git a/packages/protocol/test/video-metadata.test.ts b/packages/protocol/test/video-metadata.test.tsnew file mode 100644index 00000000..b9438d27--- /dev/null+++ b/packages/protocol/test/video-metadata.test.ts@@ -0,0 +1,24 @@+import { describe, expect, it } from 'vitest';+import { parseBilibiliVideoSource } from '../src/video-metadata.js';++describe('Bilibili video sources', () => {+ it('preserves the selected part while removing tracking parameters', () => {+ expect(parseBilibiliVideoSource('https://www.bilibili.com/video/BV18E421w7bf/?spm_id_from=share&vd_source=tracking&p=3'))+ .toEqual({ videoId: 'BV18E421w7bf', partIndex: 3, url: 'https://www.bilibili.com/video/BV18E421w7bf?p=3' });+ });++ it('does not silently select P1 when the link has no part', () => {+ expect(parseBilibiliVideoSource('https://www.bilibili.com/video/av123/'))+ .toEqual({ videoId: 'av123', url: 'https://www.bilibili.com/video/av123' });+ });++ it.each(['0', '-1', '1.5', '', 'abc', '9007199254740992', '2&p=3'])(+ 'rejects an invalid or ambiguous part: %s', part => {+ expect(parseBilibiliVideoSource(`https://www.bilibili.com/video/BV18E421w7bf?p=${part}`)).toBeNull();+ }+ );++ it('rejects lookalike hosts', () => {+ expect(parseBilibiliVideoSource('https://notbilibili.com/video/BV18E421w7bf?p=3')).toBeNull();+ });+});diff --git a/runtime/krillinai/cmd/cli/main.go b/runtime/krillinai/cmd/cli/main.goindex 7f16de11..f3dcd92d 100644--- a/runtime/krillinai/cmd/cli/main.go+++ b/runtime/krillinai/cmd/cli/main.go@@ -131,6 +131,8 @@ func configureOpenCreatorProgress(cmd *cli.Command, output *jsonLineWriter) {cmd.Subtitle.ReportProgress = writeProgress(cmd.Subtitle.ReportProgress)case "tts":cmd.TTS.ReportProgress = writeProgress(cmd.TTS.ReportProgress)+ case "render-horizontal", "render-vertical":+ cmd.Render.ReportProgress = writeProgress(cmd.Render.ReportProgress)}}diff --git a/runtime/krillinai/internal/cli/commands.go b/runtime/krillinai/internal/cli/commands.goindex 85b4e6c6..27c02416 100644--- a/runtime/krillinai/internal/cli/commands.go+++ b/runtime/krillinai/internal/cli/commands.go@@ -143,6 +143,8 @@ Flags:--video <file> Optional source video for dubbed output--voice <voice> Provider-specific voice--voice-clone-source <source> Optional voice clone source+ --audio-only Generate audio without downloading or muxing video+ --source-url <url> Download video only when needed for composition--dry-run Validate and write manifest without external calls-h, --help Show this help`@@ -546,6 +548,9 @@ func parseTTS(name string, args []string) (Command, error) {inputSRT := fs.String("input-srt", "", "input srt")lineMode := fs.String("line-mode", string(pipeline.LineModeTargetOnly), "line mode")video := fs.String("video", "", "input video")+ sourceURL := fs.String("source-url", "", "download source video only when required")+ mediaWorkdir := fs.String("media-workdir", "", "shared source video cache")+ audioOnly := fs.Bool("audio-only", false, "generate audio without a video")voice := fs.String("voice", "", "voice")voiceCloneSource := fs.String("voice-clone-source", "", "voice clone source")dryRun := fs.Bool("dry-run", false, "validate command without running external services")@@ -564,6 +569,9 @@ func parseTTS(name string, args []string) (Command, error) {InputSRT: *inputSRT,LineMode: pipeline.LineMode(*lineMode),Video: *video,+ SourceURL: *sourceURL,+ MediaWorkdir: *mediaWorkdir,+ AudioOnly: *audioOnly,Voice: *voice,VoiceCloneSource: *voiceCloneSource,},@@ -578,6 +586,8 @@ func parseRender(name string, args []string, horizontal bool) (Command, error) {workdir := fs.String("workdir", "", "workdir")taskID := fs.String("task-id", "", "task id")video := fs.String("video", "", "input video")+ sourceURL := fs.String("source-url", "", "download source video for rendering")+ mediaWorkdir := fs.String("media-workdir", "", "shared source video cache")audio := fs.String("audio", "", "input audio")subtitle := fs.String("subtitle", "", "subtitle")dubbed := fs.Bool("dubbed", false, "render dubbed video")@@ -593,15 +603,17 @@ func parseRender(name string, args []string, horizontal bool) (Command, error) {DryRun: *dryRun,SubtitleStyleFile: *subtitleStyleFile,Render: pipeline.RenderRequest{- Workdir: *workdir,- TaskID: *taskID,- Video: *video,- Audio: *audio,- Subtitle: *subtitle,- Horizontal: horizontal,- Dubbed: *dubbed,- MajorTitle: *majorTitle,- MinorTitle: *minorTitle,+ Workdir: *workdir,+ TaskID: *taskID,+ Video: *video,+ SourceURL: *sourceURL,+ MediaWorkdir: *mediaWorkdir,+ Audio: *audio,+ Subtitle: *subtitle,+ Horizontal: horizontal,+ Dubbed: *dubbed,+ MajorTitle: *majorTitle,+ MinorTitle: *minorTitle,},}, nil}diff --git a/runtime/krillinai/internal/cli/commands_test.go b/runtime/krillinai/internal/cli/commands_test.goindex 1a929f7b..5daba4d0 100644--- a/runtime/krillinai/internal/cli/commands_test.go+++ b/runtime/krillinai/internal/cli/commands_test.go@@ -37,6 +37,17 @@ func TestParseSubtitleCommand(t *testing.T) {}}+func TestParseDeferredMediaAndAudioOnlyOptions(t *testing.T) {+ cmd, err := Parse([]string{"tts", "--input-srt", "target.srt", "--audio-only", "--source-url", "https://www.youtube.com/watch?v=demo", "--media-workdir", "source-cache"})+ if err != nil || !cmd.TTS.AudioOnly || cmd.TTS.SourceURL == "" || cmd.TTS.MediaWorkdir != "source-cache" {+ t.Fatalf("TTS command = %+v, error = %v", cmd, err)+ }+ cmd, err = Parse([]string{"render-horizontal", "--source-url", "https://www.youtube.com/watch?v=demo", "--media-workdir", "source-cache", "--subtitle", "target.srt", "--audio", "dubbed.wav"})+ if err != nil || cmd.Render.SourceURL == "" || cmd.Render.MediaWorkdir != "source-cache" || cmd.Render.Audio != "dubbed.wav" {+ t.Fatalf("render command = %+v, error = %v", cmd, err)+ }+}+func TestParseSubtitlePrepareVideo(t *testing.T) {for _, enabled := range []bool{false, true} {args := []string{"subtitle", "https://www.youtube.com/watch?v=abc"}diff --git a/runtime/krillinai/internal/pipeline/media.go b/runtime/krillinai/internal/pipeline/media.gonew file mode 100644index 00000000..6d5fa740--- /dev/null+++ b/runtime/krillinai/internal/pipeline/media.go@@ -0,0 +1,55 @@+package pipeline++import (+ "context"+ "errors"+ "krillin-ai/internal/types"+ "os"+ "path/filepath"+)++func ensureSourceVideo(ctx context.Context, svc StageService, manifest *Manifest, sourceURL, mediaWorkdir string, report func(string, int, string)) (string, error) {+ if info, err := os.Stat(manifest.Outputs.OriginVideo); err == nil && info.Mode().IsRegular() && info.Size() > 0 {+ return manifest.Outputs.OriginVideo, nil+ }+ if mediaWorkdir == "" {+ mediaWorkdir = manifest.Workdir+ }+ if err := os.MkdirAll(mediaWorkdir, 0700); err != nil {+ return "", err+ }+ cached := filepath.Join(mediaWorkdir, types.SubtitleTaskVideoFileName)+ marker := filepath.Join(mediaWorkdir, ".source-ready")+ if input, err := os.ReadFile(marker); err == nil && string(input) == sourceURL {+ if info, err := os.Stat(cached); err == nil && info.Mode().IsRegular() && info.Size() > 0 {+ manifest.Outputs.OriginVideo = cached+ return cached, manifest.Save()+ }+ }+ step := &types.SubtitleTaskStepParam{+ TaskId: manifest.TaskID,+ TaskPtr: &types.SubtitleTask{TaskId: manifest.TaskID, Status: types.SubtitleTaskStatusProcessing},+ TaskBasePath: mediaWorkdir,+ Link: sourceURL,+ SkipAudio: true,+ EmbedSubtitleVideoType: "all",+ }+ if report != nil {+ report("preparing_original_media", 5, "字幕已保留,正在为视频合成下载原始视频")+ step.TaskPtr.SetProgressReporter(func(percent uint8) {+ report("preparing_original_media", int(percent), "字幕已保留,正在为视频合成下载原始视频")+ })+ }+ if err := svc.PrepareMedia(ctx, step); err != nil {+ return "", err+ }+ info, err := os.Stat(step.InputVideoPath)+ if err != nil || !info.Mode().IsRegular() || info.Size() == 0 {+ return "", errors.New("source video was not produced")+ }+ manifest.Outputs.OriginVideo = step.InputVideoPath+ if err := os.WriteFile(marker, []byte(sourceURL), 0600); err != nil {+ return "", err+ }+ return step.InputVideoPath, manifest.Save()+}diff --git a/runtime/krillinai/internal/pipeline/media_test.go b/runtime/krillinai/internal/pipeline/media_test.gonew file mode 100644index 00000000..6a68ecde--- /dev/null+++ b/runtime/krillinai/internal/pipeline/media_test.go@@ -0,0 +1,52 @@+package pipeline++import (+ "context"+ "os"+ "path/filepath"+ "testing"+)++func TestPrepareVideoOnDemandAndReuseAcrossStages(t *testing.T) {+ root := t.TempDir()+ cache := filepath.Join(root, "source-media")+ firstWorkdir := filepath.Join(root, "first")+ if err := os.MkdirAll(firstWorkdir, 0700); err != nil {+ t.Fatal(err)+ }+ first := NewManifest("first", firstWorkdir)+ fake := &fakeStageService{}+ video, err := ensureSourceVideo(context.Background(), fake, first, "https://www.youtube.com/watch?v=demo", cache, nil)+ if err != nil {+ t.Fatal(err)+ }+ if len(fake.calls) != 1 || fake.lastPrepare == nil || !fake.lastPrepare.SkipAudio {+ t.Fatalf("media preparation = %+v, calls = %v", fake.lastPrepare, fake.calls)+ }+ second := NewManifest("second", filepath.Join(root, "second"))+ reused, err := ensureSourceVideo(context.Background(), fake, second, "https://www.youtube.com/watch?v=demo", cache, nil)+ if err != nil || reused != video || len(fake.calls) != 1 {+ t.Fatalf("video = %s, error = %v, calls = %v", reused, err, fake.calls)+ }+}++func TestGenerateAudioOnlyTTSDoesNotPrepareVideo(t *testing.T) {+ root := t.TempDir()+ input := filepath.Join(root, "target.srt")+ if err := os.WriteFile(input, []byte("1\n00:00:00,000 --> 00:00:01,000\nhello\n\n"), 0600); err != nil {+ t.Fatal(err)+ }+ fake := &fakeStageService{}+ response, err := GenerateTTS(context.Background(), fake, TTSRequest{Workdir: root, TaskID: "audio-only", InputSRT: input, AudioOnly: true, SourceURL: "https://www.youtube.com/watch?v=demo"})+ if err != nil || !response.OK {+ t.Fatalf("response = %+v, error = %v", response, err)+ }+ if fake.lastSpeech == nil || !fake.lastSpeech.TtsAudioOnly || fake.lastSpeech.InputVideoPath != "" || response.Outputs.VideoWithTTS != "" {+ t.Fatalf("audio-only output = %+v, step = %+v", response.Outputs, fake.lastSpeech)+ }+ for _, call := range fake.calls {+ if call == "prepare" {+ t.Fatal("audio-only TTS prepared a video")+ }+ }+}diff --git a/runtime/krillinai/internal/pipeline/render.go b/runtime/krillinai/internal/pipeline/render.goindex 6e6dc938..4663c808 100644--- a/runtime/krillinai/internal/pipeline/render.go+++ b/runtime/krillinai/internal/pipeline/render.go@@ -11,16 +11,19 @@ import ()type RenderRequest struct {- Workdir string- TaskID string- Video string- Audio string- Subtitle string- Horizontal bool- Dubbed bool- MajorTitle string- MinorTitle string- SubtitleStyle *subtitlestyle.StyleSet+ Workdir string+ TaskID string+ Video string+ SourceURL string+ MediaWorkdir string+ ReportProgress func(string, int, string)+ Audio string+ Subtitle string+ Horizontal bool+ Dubbed bool+ MajorTitle string+ MinorTitle string+ SubtitleStyle *subtitlestyle.StyleSet}func Render(ctx context.Context, svc StageService, req RenderRequest) (Response, error) {@@ -37,6 +40,12 @@ func Render(ctx context.Context, svc StageService, req RenderRequest) (Response,restoreOutputs(manifest, existingOutputs)inputVideo := renderInputVideo(req, manifest)+ if req.Video == "" && req.SourceURL != "" {+ inputVideo, err = ensureSourceVideo(ctx, svc, manifest, req.SourceURL, req.MediaWorkdir, req.ReportProgress)+ if err != nil {+ return failRenderStage(req, manifest, renderStage(req), "prepare_media_for_render_failed", err)+ }+ }subtitle := renderSubtitle(req, manifest)output := renderOutput(req)stepParam := &types.SubtitleTaskStepParam{@@ -53,6 +62,7 @@ func Render(ctx context.Context, svc StageService, req RenderRequest) (Response,rendered, err := svc.RenderVideo(ctx, service.RenderVideoRequest{Workdir: req.Workdir,InputVideo: inputVideo,+ AudioFile: req.Audio,SubtitleFile: subtitle,OutputFile: output,Horizontal: req.Horizontal,diff --git a/runtime/krillinai/internal/pipeline/subtitle.go b/runtime/krillinai/internal/pipeline/subtitle.goindex b57e93bd..ac3c4f63 100644--- a/runtime/krillinai/internal/pipeline/subtitle.go+++ b/runtime/krillinai/internal/pipeline/subtitle.go@@ -56,11 +56,19 @@ func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleRequesstepParam.TaskPtr.SetProgressReporter(func(percent uint8) {reportSubtitleProgress(req, "preparing_source", 10+minInt(int(percent), 10), "正在准备视频来源")})- if err := svc.PrepareMedia(ctx, stepParam); err != nil {- return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_media_failed", err)+ localInput := strings.TrimPrefix(req.Input, "local:")+ if info, err := os.Stat(localInput); err == nil && info.Mode().IsRegular() {+ stepParam.InputVideoPath = localInput}syncPreparedMediaOutputs(manifest, stepParam)if req.InputSRT != "" {+ if req.PrepareVideo {+ stepParam.SkipAudio = true+ if err := svc.PrepareMedia(ctx, stepParam); err != nil {+ return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_media_failed", err)+ }+ syncPreparedMediaOutputs(manifest, stepParam)+ }reportSubtitleProgress(req, "importing_subtitles", 30, "正在使用本地字幕")if err := importSubtitleFile(ctx, svc, req, stepParam, manifest); err != nil {return failSubtitleStage(req, manifest, ErrorKindRetryable, "subtitle_import_failed", err)@@ -85,11 +93,14 @@ func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleRequesreportSubtitleProgress(req, "processing_platform_captions", 25, "正在解析平台字幕")youtubeReq.VttFile = vttFile_, err = svc.ProcessYouTubeSubtitle(ctx, youtubeReq)+ if err != nil {+ return failSubtitleStage(req, manifest, ErrorKindRetryable, "platform_caption_processing_failed", err)+ }}if err == nil {manifest.OriginLanguage = youtubeReq.OriginLanguagemanifest.CaptionSource = "youtube_vtt"- if !req.SourceOnly || req.PrepareVideo {+ if req.PrepareVideo {reportSubtitleProgress(req, "preparing_original_media", 76, "正在补齐原始视频")stepParam.TaskPtr.SetProgressReporter(func(percent uint8) {overall := 76 + minInt(int(percent), 10)*14/10@@ -103,6 +114,9 @@ func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleRequesreportSubtitleProgress(req, "collecting_outputs", 95, "正在整理字幕和视频产物")return saveSubtitleSuccess(manifest, req, CaptionSource("youtube_vtt"))}+ if errors.Is(err, service.ErrYouTubeCaptionAccess) || ctx.Err() != nil {+ return failSubtitleStage(req, manifest, ErrorKindRetryable, "platform_caption_access_failed", err)+ }if req.CaptionSource != CaptionSourceAny {return failSubtitleStage(req, manifest, ErrorKindRetryable, "platform_caption_failed", err)}@@ -123,6 +137,13 @@ func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleReques}syncPreparedMediaOutputs(manifest, stepParam)}+ if platformCaptionErr == nil {+ stepParam.TaskPtr.SetProgressReporter(audioSubtitleProgressReporter(req))+ if err := svc.PrepareMedia(ctx, stepParam); err != nil {+ return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_media_failed", err)+ }+ syncPreparedMediaOutputs(manifest, stepParam)+ }stepParam.TaskPtr.SetProgressReporter(audioSubtitleProgressReporter(req))reportSubtitleProgress(req, "transcribing_audio", 30, "正在转录并翻译音频字幕")@@ -263,6 +284,7 @@ func subtitleStepParam(req SubtitleRequest) *types.SubtitleTaskStepParam {func prepareOriginalMediaForRendering(ctx context.Context, svc StageService, stepParam *types.SubtitleTaskStepParam) error {stepParam.VttSwitch = falsestepParam.EmbedSubtitleVideoType = "all"+ stepParam.SkipAudio = truereturn svc.PrepareMedia(ctx, stepParam)}diff --git a/runtime/krillinai/internal/pipeline/subtitle_test.go b/runtime/krillinai/internal/pipeline/subtitle_test.goindex 9d320c71..a1325f2b 100644--- a/runtime/krillinai/internal/pipeline/subtitle_test.go+++ b/runtime/krillinai/internal/pipeline/subtitle_test.go@@ -176,11 +176,11 @@ func TestGenerateSubtitlesFallsBackToAudioWhenAnySourceFails(t *testing.T) {if !resp.OK {t.Fatalf("OK = false, want true")}- if got := fake.calls; len(got) != 4 || got[0] != "prepare" || got[1] != "download-youtube" || got[2] != "prepare" || got[3] != "audio" {+ if got := fake.calls; len(got) != 3 || got[0] != "download-youtube" || got[1] != "prepare" || got[2] != "audio" {t.Fatalf("calls = %v", got)}- if got := fake.prepareVTT; len(got) != 2 || got[0] != true || got[1] != false {- t.Fatalf("prepare VttSwitch values = %v, want [true false]", got)+ if got := fake.prepareVTT; len(got) != 1 || got[0] != false {+ t.Fatalf("prepare VttSwitch values = %v, want [false]", got)}}@@ -323,7 +323,7 @@ func TestGenerateSubtitlesManualDoesNotFallback(t *testing.T) {if resp.OK {t.Fatalf("OK = true, want false")}- if got := fake.calls; len(got) != 2 || got[1] != "download-youtube" {+ if got := fake.calls; len(got) != 1 || got[0] != "download-youtube" {t.Fatalf("calls = %v", got)}}@@ -349,7 +349,7 @@ func TestGenerateSubtitlesYouTubeCaptionsDoNotUseAudio(t *testing.T) {if resp.CaptionSource == "" {t.Fatalf("CaptionSource is empty")}- if got := fake.calls; len(got) != 4 || got[0] != "prepare" || got[1] != "download-youtube" || got[2] != "process-youtube" || got[3] != "prepare" {+ if got := fake.calls; len(got) != 2 || got[0] != "download-youtube" || got[1] != "process-youtube" {t.Fatalf("calls = %v", got)}for _, call := range fake.calls {@@ -376,11 +376,11 @@ func TestGenerateSubtitlesSourceOnlySkipsTranslationAndVideoPreparation(t *testiif err != nil || !resp.OK {t.Fatalf("response = %+v, error = %v", resp, err)}- if got := fake.calls; len(got) != 3 || got[0] != "prepare" || got[1] != "download-youtube" || got[2] != "process-youtube" {- t.Fatalf("calls = %v, want source preparation and platform captions only", got)+ if got := fake.calls; len(got) != 2 || got[0] != "download-youtube" || got[1] != "process-youtube" {+ t.Fatalf("calls = %v, want platform captions only", got)}- if fake.lastPrepare == nil || fake.lastPrepare.SubtitleResultType != types.SubtitleResultTypeOriginOnly {- t.Fatalf("SubtitleResultType = %v, want origin only", fake.lastPrepare.SubtitleResultType)+ if fake.lastPrepare != nil {+ t.Fatal("source-only captions unexpectedly prepared media")}if fake.lastYouTube == nil || !fake.lastYouTube.SourceOnly {t.Fatal("YouTube request did not preserve SourceOnly")@@ -403,6 +403,7 @@ func TestGenerateSubtitlesYouTubeCaptionsPrepareOriginalMediaForRendering(t *tesOriginLang: "en",TargetLang: "zh_cn",CaptionSource: CaptionSourceAny,+ PrepareVideo: true,}resp, err := GenerateSubtitles(context.Background(), fake, req)if err != nil {@@ -411,14 +412,14 @@ func TestGenerateSubtitlesYouTubeCaptionsPrepareOriginalMediaForRendering(t *tesif !resp.OK {t.Fatalf("OK = false, want true")}- if got := fake.calls; len(got) != 4 || got[0] != "prepare" || got[1] != "download-youtube" || got[2] != "process-youtube" || got[3] != "prepare" {+ if got := fake.calls; len(got) != 3 || got[0] != "download-youtube" || got[1] != "process-youtube" || got[2] != "prepare" {t.Fatalf("calls = %v", got)}- if got := fake.prepareVTT; len(got) != 2 || got[0] != true || got[1] != false {- t.Fatalf("prepare VttSwitch values = %v, want [true false]", got)+ if got := fake.prepareVTT; len(got) != 1 || got[0] != false {+ t.Fatalf("prepare VttSwitch values = %v, want [false]", got)}- if got := fake.prepareEmbedTypes; len(got) != 2 || got[1] != "all" {- t.Fatalf("prepare EmbedSubtitleVideoType values = %v, want second value all", got)+ if got := fake.prepareEmbedTypes; len(got) != 1 || got[0] != "all" {+ t.Fatalf("prepare EmbedSubtitleVideoType values = %v, want [all]", got)}}@@ -562,8 +563,31 @@ func TestGenerateSubtitlesFallbackPreparesVideoWhenRequested(t *testing.T) {if !resp.OK {t.Fatalf("OK = false, want true")}- if got := fake.prepareEmbedTypes; len(got) != 2 || got[0] != "none" || got[1] != "all" {- t.Fatalf("prepare EmbedSubtitleVideoType values = %v, want [none all]", got)+ if got := fake.prepareEmbedTypes; len(got) != 1 || got[0] != "all" {+ t.Fatalf("prepare EmbedSubtitleVideoType values = %v, want [all]", got)+ }+}++func TestPlatformTranslationFailureDoesNotDownloadOrTranscribe(t *testing.T) {+ fake := &fakeStageService{processErr: errors.New("translation service unavailable")}+ response, err := GenerateSubtitles(context.Background(), fake, SubtitleRequest{Input: "https://www.youtube.com/watch?v=abc", Workdir: t.TempDir(), TaskID: "translation-error", OriginLang: "en", TargetLang: "zh_cn", CaptionSource: CaptionSourceAny})+ if err == nil || response.Error.Code != "platform_caption_processing_failed" {+ t.Fatalf("response = %+v, error = %v", response, err)+ }+ if len(fake.calls) != 2 || fake.calls[0] != "download-youtube" || fake.calls[1] != "process-youtube" {+ t.Fatalf("unexpected fallback calls: %v", fake.calls)+ }+}++func TestPlatformAccessFailureDoesNotTriggerWhisperDownload(t *testing.T) {+ for _, captionSource := range []CaptionSource{CaptionSourceAny, CaptionSourcePlatform} {+ t.Run(string(captionSource), func(testCase *testing.T) {+ fake := &fakeStageService{downloadErr: service.ErrYouTubeCaptionAccess}+ response, err := GenerateSubtitles(context.Background(), fake, SubtitleRequest{Input: "https://www.youtube.com/watch?v=abc", Workdir: testCase.TempDir(), TaskID: "access-error", CaptionSource: captionSource})+ if err == nil || response.Error.Code != "platform_caption_access_failed" || len(fake.calls) != 1 {+ testCase.Fatalf("response = %+v, error = %v, calls = %v", response, err, fake.calls)+ }+ })}}diff --git a/runtime/krillinai/internal/pipeline/tts.go b/runtime/krillinai/internal/pipeline/tts.goindex 71d0c019..035f922c 100644--- a/runtime/krillinai/internal/pipeline/tts.go+++ b/runtime/krillinai/internal/pipeline/tts.go@@ -16,6 +16,9 @@ type TTSRequest struct {InputSRT stringLineMode LineModeVideo string+ SourceURL string+ MediaWorkdir string+ AudioOnly boolVoice stringVoiceCloneSource stringReportProgress func(phase string, percent int, message string)@@ -54,11 +57,21 @@ func GenerateTTS(ctx context.Context, svc StageService, req TTSRequest) (Responsif inputVideo == "" {inputVideo = manifest.Outputs.OriginVideo}+ if req.AudioOnly {+ inputVideo = ""+ manifest.Outputs.VideoWithTTS = ""+ } else if req.Video == "" && req.SourceURL != "" {+ inputVideo, err = ensureSourceVideo(ctx, svc, manifest, req.SourceURL, req.MediaWorkdir, req.ReportProgress)+ if err != nil {+ return failTTSStage(req, manifest, "prepare_media_for_render_failed", err)+ }+ }stepParam := &types.SubtitleTaskStepParam{TaskId: req.TaskID,TaskPtr: &types.SubtitleTask{TaskId: req.TaskID, Status: types.SubtitleTaskStatusProcessing},TaskBasePath: req.Workdir,EnableTts: true,+ TtsAudioOnly: req.AudioOnly,TtsSourceFilePath: ttsSource,TtsResultFilePath: manifest.Outputs.TTSAudio,InputVideoPath: inputVideo,@@ -81,7 +94,7 @@ func GenerateTTS(ctx context.Context, svc StageService, req TTSRequest) (Responsif stepParam.TtsResultFilePath != "" {manifest.Outputs.TTSAudio = stepParam.TtsResultFilePath}- if stepParam.VideoWithTtsFilePath != "" {+ if !req.AudioOnly && stepParam.VideoWithTtsFilePath != "" {manifest.Outputs.VideoWithTTS = stepParam.VideoWithTtsFilePath}manifest.MarkStage(StageTTS, true, "")diff --git a/runtime/krillinai/internal/service/dubbing/runner.go b/runtime/krillinai/internal/service/dubbing/runner.goindex b5fbb358..f42a3cd4 100644--- a/runtime/krillinai/internal/service/dubbing/runner.go+++ b/runtime/krillinai/internal/service/dubbing/runner.go@@ -117,14 +117,18 @@ func (r *Runner) Run(ctx context.Context) (Result, error) {return Result{}, err}- if err := ensureParentDir(r.deps.OutputVideo); err != nil {- return Result{}, err- }- if err := r.deps.FFmpeg(buildMuxArgs(r.deps.InputVideo, r.deps.OutputAudio, r.deps.OutputVideo)); err != nil {- return Result{}, err- }- if err := ensureNonEmptyFile(r.deps.OutputVideo, "output video"); err != nil {- return Result{}, err+ if !r.deps.AudioOnly {+ if err := ensureParentDir(r.deps.OutputVideo); err != nil {+ return Result{}, err+ }+ if err := r.deps.FFmpeg(buildMuxArgs(r.deps.InputVideo, r.deps.OutputAudio, r.deps.OutputVideo)); err != nil {+ return Result{}, err+ }+ if err := ensureNonEmptyFile(r.deps.OutputVideo, "output video"); err != nil {+ return Result{}, err+ }+ } else {+ r.deps.OutputVideo = ""}return Result{@@ -147,6 +151,9 @@ func (r *Runner) validate() error {if r.deps.TTS == nil {return errors.New("tts is required")}+ if r.deps.AudioOnly {+ return nil+ }if r.deps.InputVideo == "" {return errors.New("input video is required")}diff --git a/runtime/krillinai/internal/service/dubbing/runner_test.go b/runtime/krillinai/internal/service/dubbing/runner_test.goindex 0200b8ef..62c1ac24 100644--- a/runtime/krillinai/internal/service/dubbing/runner_test.go+++ b/runtime/krillinai/internal/service/dubbing/runner_test.go@@ -29,6 +29,28 @@ func fakeRunnerWritingOutputsWithoutMkdir() CommandRunner {}}+func TestAudioOnlyRunnerDoesNotRequireOrMuxVideo(t *testing.T) {+ root := t.TempDir()+ input := filepath.Join(root, "input.srt")+ if err := os.WriteFile(input, []byte("1\n00:00:00,000 --> 00:00:01,000\n你好\n\n"), 0600); err != nil {+ t.Fatal(err)+ }+ result, err := NewRunner(Dependencies{+ TTS: &fakeTTS{writeOnReturn: true}, Language: "zh_cn", Voice: "voice", Workdir: root, InputSRT: input, AudioOnly: true,+ OutputAudio: filepath.Join(root, "output.wav"), Config: DefaultConfig(),+ FFmpeg: func(args []string) error {+ if strings.HasSuffix(args[len(args)-1], ".mp4") {+ t.Fatal("audio-only runner attempted video muxing")+ }+ return fakeRunnerWritingOutputs(root)(args)+ },+ Duration: func(string) (float64, error) { return 0.8, nil },+ }).Run(context.Background())+ if err != nil || result.Audio == "" || result.Video != "" {+ t.Fatalf("result = %+v, error = %v", result, err)+ }+}+func TestRunWritesDubbingArtifactsWithFakeTTS(t *testing.T) {dir := t.TempDir()input := filepath.Join(dir, "input.srt")diff --git a/runtime/krillinai/internal/service/dubbing/types.go b/runtime/krillinai/internal/service/dubbing/types.goindex 1987a165..f0d22640 100644--- a/runtime/krillinai/internal/service/dubbing/types.go+++ b/runtime/krillinai/internal/service/dubbing/types.go@@ -95,6 +95,7 @@ type Dependencies struct {Workdir stringInputSRT stringInputVideo string+ AudioOnly boolOutputAudio stringOutputVideo stringConfig Configdiff --git a/runtime/krillinai/internal/service/get_video_info.go b/runtime/krillinai/internal/service/get_video_info.goindex 7ee45a3e..c7328da9 100644--- a/runtime/krillinai/internal/service/get_video_info.go+++ b/runtime/krillinai/internal/service/get_video_info.go@@ -19,8 +19,8 @@ func (s Service) getVideoInfo(ctx context.Context, stepParam *types.SubtitleTasktitle, description string)// 获取标题- titleCmdArgs := []string{"--skip-download", "--encoding", "utf-8", "--get-title", stepParam.Link}- descriptionCmdArgs := []string{"--skip-download", "--encoding", "utf-8", "--get-description", stepParam.Link}+ titleCmdArgs := []string{"--no-playlist", "--skip-download", "--encoding", "utf-8", "--get-title", stepParam.Link}+ descriptionCmdArgs := []string{"--no-playlist", "--skip-download", "--encoding", "utf-8", "--get-description", stepParam.Link}titleCmdArgs = appendCookiesArgs(titleCmdArgs, youtubeCookiesPath)descriptionCmdArgs = appendCookiesArgs(descriptionCmdArgs, youtubeCookiesPath)if config.Conf.App.Proxy != "" {diff --git a/runtime/krillinai/internal/service/link2file.go b/runtime/krillinai/internal/service/link2file.goindex 76be681a..5d4f3755 100644--- a/runtime/krillinai/internal/service/link2file.go+++ b/runtime/krillinai/internal/service/link2file.go@@ -29,6 +29,10 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskStif isLocal {// 本地文件videoPath = localVideoPath+ if stepParam.SkipAudio {+ stepParam.InputVideoPath = videoPath+ return nil+ }cmd := exec.Command(storage.FfmpegPath, "-i", videoPath, "-vn", "-ar", "44100", "-ac", "2", "-ab", "192k", "-f", "mp3", audioPath)output, err = cmd.CombinedOutput()if err != nil {@@ -43,7 +47,7 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskStreturn fmt.Errorf("linkToFile.GetYouTubeID error: %w", err)}stepParam.Link = "https://www.youtube.com/watch?v=" + videoId- if !stepParam.VttSwitch {+ if !stepParam.VttSwitch && !stepParam.SkipAudio {// 使用更灵活的音频格式选择器,避免 HTTP 403 错误。cmdArgs := []string{"-f", "bestaudio[ext=m4a]/bestaudio[ext=mp3]/bestaudio/worst",@@ -68,34 +72,39 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskSt}}} else if strings.Contains(link, "bilibili.com") {- videoId := util.GetBilibiliVideoId(link)- if videoId == "" {- return errors.New("linkToFile error: invalid link")- }- stepParam.Link = "https://www.bilibili.com/video/" + videoId- cmdArgs := []string{"-f", "bestaudio[ext=m4a]", "-x", "--audio-format", "mp3", "-o", audioPath, stepParam.Link}- if config.Conf.App.Proxy != "" {- cmdArgs = append(cmdArgs, "--proxy", config.Conf.App.Proxy)- }- if storage.FfmpegPath != "ffmpeg" {- cmdArgs = append(cmdArgs, "--ffmpeg-location", storage.FfmpegPath)+ normalizedLink, normalizeErr := util.NormalizeBilibiliVideoURL(link)+ if normalizeErr != nil {+ return fmt.Errorf("linkToFile error: %w", normalizeErr)}- cmd := storage.YtdlpCommand(cmdArgs...)- output, err = cmd.CombinedOutput()- if err != nil {- log.GetLogger().Error("linkToFile download audio yt-dlp error", zap.Any("step param", stepParam), zap.String("output", string(output)), zap.Error(err))- return fmt.Errorf("linkToFile download audio yt-dlp error: %w: %s", err, compactCommandOutput(output))+ stepParam.Link = normalizedLink+ if !stepParam.SkipAudio {+ cmdArgs := []string{"--no-playlist", "-f", "bestaudio[ext=m4a]", "-x", "--audio-format", "mp3", "-o", audioPath, stepParam.Link}+ if config.Conf.App.Proxy != "" {+ cmdArgs = append(cmdArgs, "--proxy", config.Conf.App.Proxy)+ }+ if storage.FfmpegPath != "ffmpeg" {+ cmdArgs = append(cmdArgs, "--ffmpeg-location", storage.FfmpegPath)+ }+ cmd := storage.YtdlpCommand(cmdArgs...)+ output, err = cmd.CombinedOutput()+ if err != nil {+ log.GetLogger().Error("linkToFile download audio yt-dlp error", zap.Any("step param", stepParam), zap.String("output", string(output)), zap.Error(err))+ return fmt.Errorf("linkToFile download audio yt-dlp error: %w: %s", err, compactCommandOutput(output))+ }}} else {log.GetLogger().Info("linkToFile.unsupported link type", zap.Any("step param", stepParam))return errors.New("linkToFile error: unsupported link, only support youtube, bilibili and local file")}stepParam.TaskPtr.SetProgress(6)- stepParam.AudioFilePath = audioPath+ if !stepParam.SkipAudio {+ stepParam.AudioFilePath = audioPath+ }if !isLocal && stepParam.EmbedSubtitleVideoType != "none" {// 需要下载原视频- cmdArgs := []string{"-f", "bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]", "-o", videoPath, stepParam.Link}+ cmdArgs := []string{"--no-playlist", "-f", "bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]", "-o", videoPath, stepParam.Link}+ cmdArgs = appendCookiesArgs(cmdArgs, youtubeCookiesPath)if config.Conf.App.Proxy != "" {cmdArgs = append(cmdArgs, "--proxy", config.Conf.App.Proxy)}diff --git a/runtime/krillinai/internal/service/link2file_test.go b/runtime/krillinai/internal/service/link2file_test.goindex 04bbf5bd..a7a296d9 100644--- a/runtime/krillinai/internal/service/link2file_test.go+++ b/runtime/krillinai/internal/service/link2file_test.go@@ -1,11 +1,64 @@package serviceimport (+ "context"+ "krillin-ai/config"+ "krillin-ai/internal/storage"+ "krillin-ai/internal/types""os""path/filepath"+ "runtime"+ "strings""testing")+func TestBilibiliDownloadsOnlySelectedPart(testRunner *testing.T) {+ if runtime.GOOS == "windows" {+ testRunner.Skip("shell downloader fixture requires POSIX")+ }+ originalYtdlp, originalPrefix, originalFfmpeg, originalProxy := storage.YtdlpPath, storage.YtdlpPrefixArgs, storage.FfmpegPath, config.Conf.App.Proxy+ testRunner.Cleanup(func() {+ storage.YtdlpPath, storage.YtdlpPrefixArgs, storage.FfmpegPath, config.Conf.App.Proxy = originalYtdlp, originalPrefix, originalFfmpeg, originalProxy+ })+ dir := testRunner.TempDir()+ argsPath := filepath.Join(dir, "arguments")+ downloader := filepath.Join(dir, "yt-dlp")+ testRunner.Setenv("BILIBILI_TEST_ARGS", argsPath)+ if err := os.WriteFile(downloader, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" >> \"$BILIBILI_TEST_ARGS\"\nprintf '\\n' >> \"$BILIBILI_TEST_ARGS\"\n"), 0700); err != nil {+ testRunner.Fatal(err)+ }+ storage.YtdlpPath, storage.YtdlpPrefixArgs, storage.FfmpegPath, config.Conf.App.Proxy = downloader, nil, "ffmpeg", ""+ for _, test := range []struct {+ compose string+ skipAudio bool+ commands int+ }{+ {"none", false, 1},+ {"horizontal", false, 2},+ {"horizontal", true, 1},+ } {+ if err := os.WriteFile(argsPath, nil, 0600); err != nil {+ testRunner.Fatal(err)+ }+ step := &types.SubtitleTaskStepParam{Link: "https://www.bilibili.com/video/BV18E421w7bf/?spm_id_from=share&p=3",+ TaskBasePath: dir, TaskPtr: &types.SubtitleTask{}, EmbedSubtitleVideoType: test.compose, SkipAudio: test.skipAudio}+ if err := (Service{}).linkToFile(context.Background(), step); err != nil {+ testRunner.Fatal(err)+ }+ arguments, err := os.ReadFile(argsPath)+ if err != nil {+ testRunner.Fatal(err)+ }+ wantCommands := test.commands+ if strings.Count(string(arguments), "--no-playlist\n") != wantCommands || strings.Count(string(arguments), "https://www.bilibili.com/video/BV18E421w7bf?p=3\n") != wantCommands {+ testRunner.Fatalf("expected %d single-part downloads with preserved P3, got %s", wantCommands, arguments)+ }+ if step.Link != "https://www.bilibili.com/video/BV18E421w7bf?p=3" {+ testRunner.Fatalf("unexpected normalized link: %s", step.Link)+ }+ }+}+func TestResolveLocalMediaInput(t *testing.T) {dir := t.TempDir()path := filepath.Join(dir, "source.webm")diff --git a/runtime/krillinai/internal/service/render_stage.go b/runtime/krillinai/internal/service/render_stage.goindex 914c6f8d..c120d243 100644--- a/runtime/krillinai/internal/service/render_stage.go+++ b/runtime/krillinai/internal/service/render_stage.go@@ -18,6 +18,7 @@ import (type RenderVideoRequest struct {Workdir stringInputVideo string+ AudioFile stringSubtitleFile stringOutputFile stringHorizontal bool@@ -54,16 +55,21 @@ func escapeAssFilterPath(path string) string {func buildEmbedSubtitleArgs(req RenderVideoRequest) ([]string, string) {assPath := renderAssPath(req)filter := buildAssFilterExpression(assPath, packagedSubtitleFontsDir())- return []string{+ args := []string{"-y","-i", req.InputVideo,+ }+ if req.AudioFile != "" {+ args = append(args, "-i", req.AudioFile, "-map", "0:v:0", "-map", "1:a:0", "-shortest")+ }+ return append(args,"-vf", filter,"-c:v", "libx264","-preset", "fast","-c:a", "aac","-b:a", "192k",req.OutputFile,- }, assPath+ ), assPath}func buildAssFilterExpression(assPath, fontsDir string) string {diff --git a/runtime/krillinai/internal/service/render_stage_test.go b/runtime/krillinai/internal/service/render_stage_test.goindex 32897206..7de87d46 100644--- a/runtime/krillinai/internal/service/render_stage_test.go+++ b/runtime/krillinai/internal/service/render_stage_test.go@@ -33,6 +33,14 @@ func TestBuildEmbedSubtitleArgsUsesRequestedSubtitleAndOutput(t *testing.T) {}}+func TestRenderCanComposePreviouslyGeneratedAudioWithoutRepeatingTTS(t *testing.T) {+ args, _ := buildEmbedSubtitleArgs(RenderVideoRequest{InputVideo: "origin.mp4", AudioFile: "dubbed.wav", SubtitleFile: "target.srt", OutputFile: "dubbed.mp4"})+ command := strings.Join(args, " ")+ if !strings.Contains(command, "-i origin.mp4 -i dubbed.wav -map 0:v:0 -map 1:a:0") {+ t.Fatalf("audio mapping missing: %v", args)+ }+}+func TestRenderAssPathDerivesFromOutputFile(t *testing.T) {req := RenderVideoRequest{Workdir: "tasks/demo",diff --git a/runtime/krillinai/internal/service/srt2speech.go b/runtime/krillinai/internal/service/srt2speech.goindex 58db4530..b5d59554 100644--- a/runtime/krillinai/internal/service/srt2speech.go+++ b/runtime/krillinai/internal/service/srt2speech.go@@ -67,6 +67,7 @@ func (s Service) srtFileToSpeech(ctx context.Context, stepParam *types.SubtitleTWorkdir: stepParam.TaskBasePath,InputSRT: stepParam.TtsSourceFilePath,InputVideo: stepParam.InputVideoPath,+ AudioOnly: stepParam.TtsAudioOnly,OutputAudio: outputAudio,OutputVideo: outputVideo,ReportProgress: func(completed, total int) {diff --git a/runtime/krillinai/internal/service/youtube_subtitle.go b/runtime/krillinai/internal/service/youtube_subtitle.goindex 1a588b02..28b60132 100644--- a/runtime/krillinai/internal/service/youtube_subtitle.go+++ b/runtime/krillinai/internal/service/youtube_subtitle.go@@ -4,6 +4,7 @@ import ("bufio""context""encoding/json"+ "errors""fmt""net/url""os"@@ -24,6 +25,8 @@ import ("go.uber.org/zap")+var ErrYouTubeCaptionAccess = errors.New("platform caption access failed")+// VttWord 表示VTT文件中的一个单词及其时间戳信息type VttWord struct {Text string // 单词文本,包含标点符号@@ -220,7 +223,8 @@ func (s *YouTubeSubtitleService) downloadYouTubeSubtitle(ctx context.Context, relog.GetLogger().Error("downloadYouTubeSubtitle failed after all attempts", zap.Any("req", req), zap.Error(lastErr))return "", fmt.Errorf(- "downloadYouTubeSubtitle yt-dlp error after %d attempts: %w: %s",+ "%w: downloadYouTubeSubtitle yt-dlp error after %d attempts: %w: %s",+ ErrYouTubeCaptionAccess,maxAttempts,lastErr,compactCommandOutput(lastOutput),@@ -244,14 +248,14 @@ func (s *YouTubeSubtitleService) resolveYouTubeSubtitleTrack(ctx context.Contextdetails = compactCommandOutput(exitErr.Stderr)}if details != "" {- return youtubeSubtitleTrack{}, fmt.Errorf("yt-dlp subtitle metadata error: %w: %s", err, details)+ return youtubeSubtitleTrack{}, fmt.Errorf("%w: yt-dlp subtitle metadata error: %w: %s", ErrYouTubeCaptionAccess, err, details)}- return youtubeSubtitleTrack{}, fmt.Errorf("yt-dlp subtitle metadata error: %w", err)+ return youtubeSubtitleTrack{}, fmt.Errorf("%w: yt-dlp subtitle metadata error: %w", ErrYouTubeCaptionAccess, err)}var metadata youtubeSubtitleMetadataif err := json.Unmarshal(output, &metadata); err != nil {- return youtubeSubtitleTrack{}, fmt.Errorf("parse yt-dlp subtitle metadata: %w", err)+ return youtubeSubtitleTrack{}, fmt.Errorf("%w: parse yt-dlp subtitle metadata: %w", ErrYouTubeCaptionAccess, err)}manualLanguages := availableLanguageKeys(metadata.Subtitles)diff --git a/runtime/krillinai/internal/types/subtitle_task.go b/runtime/krillinai/internal/types/subtitle_task.goindex 3371bfe6..675a304e 100644--- a/runtime/krillinai/internal/types/subtitle_task.go+++ b/runtime/krillinai/internal/types/subtitle_task.go@@ -335,6 +335,8 @@ type SubtitleTaskStepParam struct {SubtitleResultType SubtitleResultTypeEnableModalFilter boolEnableTts bool+ TtsAudioOnly bool+ SkipAudio boolTtsVoiceCode string // 人声语音编码VoiceCloneAudioUrl string // 音色克隆的源音频oss地址ReplaceWordsMap map[string]stringdiff --git a/runtime/krillinai/pkg/util/bilibili.go b/runtime/krillinai/pkg/util/bilibili.gonew file mode 100644index 00000000..833f833e--- /dev/null+++ b/runtime/krillinai/pkg/util/bilibili.go@@ -0,0 +1,44 @@+package util++import (+ "fmt"+ "net/url"+ "regexp"+ "strconv"+ "strings"+)++var bilibiliVideoPath = regexp.MustCompile(`^/video/(BV[A-Za-z0-9]+|av[0-9]+)/?$`)+var bilibiliPartNumber = regexp.MustCompile(`^[1-9][0-9]*$`)++func NormalizeBilibiliVideoURL(value string) (string, error) {+ source, err := url.Parse(strings.TrimSpace(value))+ if err != nil {+ return "", fmt.Errorf("invalid Bilibili video URL: %w", err)+ }+ host := strings.ToLower(source.Hostname())+ if (source.Scheme != "http" && source.Scheme != "https") || (host != "bilibili.com" && !strings.HasSuffix(host, ".bilibili.com")) {+ return "", fmt.Errorf("invalid Bilibili video URL")+ }+ match := bilibiliVideoPath.FindStringSubmatch(source.Path)+ if len(match) != 2 {+ return "", fmt.Errorf("invalid Bilibili video ID")+ }+ query, err := url.ParseQuery(source.RawQuery)+ if err != nil {+ return "", fmt.Errorf("invalid Bilibili video query: %w", err)+ }+ parts := query["p"]+ part := "1"+ if len(parts) > 0 {+ if len(parts) != 1 || !bilibiliPartNumber.MatchString(parts[0]) {+ return "", fmt.Errorf("invalid Bilibili part: select a positive part number")+ }+ index, parseErr := strconv.ParseUint(parts[0], 10, 53)+ if parseErr != nil || index == 0 {+ return "", fmt.Errorf("invalid Bilibili part number")+ }+ part = strconv.FormatUint(index, 10)+ }+ return "https://www.bilibili.com/video/" + match[1] + "?p=" + part, nil+}diff --git a/runtime/krillinai/pkg/util/bilibili_test.go b/runtime/krillinai/pkg/util/bilibili_test.gonew file mode 100644index 00000000..119e3d7d--- /dev/null+++ b/runtime/krillinai/pkg/util/bilibili_test.go@@ -0,0 +1,30 @@+package util++import "testing"++func TestNormalizeBilibiliVideoURL(testRunner *testing.T) {+ for _, test := range []struct {+ name string+ input string+ want string+ }{+ {"selected part", "https://www.bilibili.com/video/BV18E421w7bf/?spm_id_from=share&p=3&vd_source=tracking", "https://www.bilibili.com/video/BV18E421w7bf?p=3"},+ {"safe single part fallback", "https://www.bilibili.com/video/BV18E421w7bf", "https://www.bilibili.com/video/BV18E421w7bf?p=1"},+ {"av video", "https://www.bilibili.com/video/av123/?p=2", "https://www.bilibili.com/video/av123?p=2"},+ } {+ testRunner.Run(test.name, func(subtest *testing.T) {+ got, err := NormalizeBilibiliVideoURL(test.input)+ if err != nil || got != test.want {+ subtest.Fatalf("got %q, %v; want %q", got, err, test.want)+ }+ })+ }+ for _, query := range []string{"p=0", "p=-1", "p=1.5", "p=", "p=2&p=3", "p=9007199254740992"} {+ if _, err := NormalizeBilibiliVideoURL("https://www.bilibili.com/video/BV18E421w7bf?" + query); err == nil {+ testRunner.Fatalf("expected invalid query %q to fail", query)+ }+ }+ if _, err := NormalizeBilibiliVideoURL("https://notbilibili.com/video/BV18E421w7bf?p=3"); err == nil {+ testRunner.Fatal("expected lookalike host to fail")+ }+}