Skip to content

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.

fix: harden startup, gateway recovery, and speech processing

wulien-centeref833abd5d32
diff --git a/apps/daemon/src/api/routes.creator-thumbnail.ts b/apps/daemon/src/api/routes.creator-thumbnail.ts
index 58a342df..9eeba7f3 100644
--- a/apps/daemon/src/api/routes.creator-thumbnail.ts
+++ b/apps/daemon/src/api/routes.creator-thumbnail.ts
@@ -1,6 +1,5 @@
import type { CreatorServicesConfig } from '@opencreator/protocol';
import type { FastifyInstance } from 'fastify';
-import sharp from 'sharp';
import { downloadThumbnail } from '../creator/cover/executor.js';
import type { CreatorService } from '../creator/service.js';
import { apiError } from './errors.js';
@@ -23,6 +22,7 @@ export async function registerCreatorThumbnailRoutes(
try {
const config = await readServicesConfig?.();
const thumbnail = await downloadThumbnail(url, config?.proxy ?? '', AbortSignal.timeout(30_000));
+ const { default: sharp } = await import('sharp');
const image = sharp(thumbnail.content);
const metadata = await image.metadata();
const content = metadata.format === 'jpeg' ? thumbnail.content
diff --git a/apps/daemon/src/creator/krillin/tts-service.ts b/apps/daemon/src/creator/krillin/tts-service.ts
index 1bbccd1c..6b52a06b 100644
--- a/apps/daemon/src/creator/krillin/tts-service.ts
+++ b/apps/daemon/src/creator/krillin/tts-service.ts
@@ -146,7 +146,7 @@ export function createKrillinTtsService(input: {
};
const executeSynthesis = input.executeSynthesis ?? (request => executeProviderSynthesis({
...request,
- timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS
+ timeoutMs: input.timeoutMs ?? (request.official ? 600_000 : DEFAULT_TIMEOUT_MS)
}));
async function listVoices(
diff --git a/apps/daemon/src/creator/presets/compiler.ts b/apps/daemon/src/creator/presets/compiler.ts
index a54471de..12675725 100644
--- a/apps/daemon/src/creator/presets/compiler.ts
+++ b/apps/daemon/src/creator/presets/compiler.ts
@@ -12,7 +12,6 @@ import {
writeFile
} from 'node:fs/promises';
import path from 'node:path';
-import sharp from 'sharp';
import { ZodError } from 'zod';
import { creatorPresetModuleDefinitions, getCreatorPresetModuleDefinition } from './module-schemas.js';
import { creatorPresetSourceManifestSchema, validatePresetPublicFields } from './schema.js';
@@ -570,6 +569,7 @@ async function validateImageResource(input: {
throw new Error(`${relativeFile}.${field}: unsupported image extension`);
}
const bytes = await readFile(resource.file);
+ const { default: sharp } = await import('sharp');
const dimensions = await sharp(bytes, { failOn: 'error' }).metadata();
if (dimensions.width === undefined || dimensions.height === undefined) {
throw new Error(`${relativeFile}.${field}: image dimensions are unavailable`);
diff --git a/apps/daemon/src/gateway/account-service.ts b/apps/daemon/src/gateway/account-service.ts
index cccce5b0..91b0b651 100644
--- a/apps/daemon/src/gateway/account-service.ts
+++ b/apps/daemon/src/gateway/account-service.ts
@@ -220,8 +220,8 @@ export function createGatewayAccountService(input: {
if (credentials.modelAccessError) { restoreModelAccessError(credentials.modelAccessError); return; }
if (input.deferStartupActivation) {
const cached = bootstrapSchema.safeParse(credentials);
- if (cached.success) state.models = cached.data.transport === 'gateway'
- ? withModelCatalog(cached.data, officialModelPolicy(cached.data.models)).models : (await visibleBootstrap(cached.data)).models;
+ // Catalog preparation may access the network; only restore the local snapshot before listen.
+ if (cached.success) state.models = withModelCatalog(cached.data, officialModelPolicy(cached.data.models)).models;
state.activationState = 'loading';
return;
}
@@ -283,12 +283,19 @@ export function createGatewayAccountService(input: {
stage = 'configuration';
await saveConfiguration();
stage = 'usage';
- await reportUsage();
+ let migration: GatewayError | undefined;
+ try { await reportUsage(); }
+ catch (error) {
+ if (!(error instanceof GatewayError) || error.status !== 409 || error.code !== 'gateway_migrated') throw error;
+ migration = error;
+ }
if (closing || accessPaused || generation !== expected || state.authState !== 'signed_in') return;
stage = 'bootstrap';
- const bootstrap = cached?.success ? cached.data : await input.client.bootstrap(original.accessToken, { rotate: rotate && !usesCachedModelKey() && !input.hasPendingTasks?.(), modelUsePolicy: cachedModelUsePolicy, ...(region === 'auto' ? {} : { region }) });
+ const bootstrap = cached?.success ? cached.data : await input.client.bootstrap(original.accessToken, { rotate: !migration && rotate && !usesCachedModelKey() && !input.hasPendingTasks?.(), modelUsePolicy: cachedModelUsePolicy, ...(region === 'auto' ? {} : { region }) });
if (closing || accessPaused || generation !== expected || state.authState !== 'signed_in') return;
if (original.account && bootstrap.account.id !== original.account.id) throw new GatewayError('account_mismatch');
+ // Only a confirmed gateway binding can replace obsolete direct-key coordination.
+ if (migration && (bootstrap.transport !== 'gateway' || bootstrap.keyRotation !== false)) { stage = 'usage'; throw migration; }
state.account = bootstrap.account;
state.supplier = bootstrap.supplier ?? 'openrouter'; supplierMigrating = false;
state.transport = bootstrap.transport;
diff --git a/apps/daemon/src/gateway/creator-service-source.ts b/apps/daemon/src/gateway/creator-service-source.ts
index 1f84f1d2..281b783d 100644
--- a/apps/daemon/src/gateway/creator-service-source.ts
+++ b/apps/daemon/src/gateway/creator-service-source.ts
@@ -14,6 +14,7 @@ import type { CreatorProviderRequestLedger, CreatorProviderLookupResult } from '
import { createDirectTransport, directRequestId } from './direct-transport.js';
import { createOfficialFetch } from './official-fetch.js';
import { chatAnywhereImage } from './chatanywhere-media.js';
+import { resolveGatewaySpeech } from './speech-response.js';
export type GatewayCreatorBinding = { accountId: string; bindingVersion: string; baseUrl: string; apiKey: string; models: Record<string, string>; catalog?: GatewayModel[]; capabilities?: Record<string, string[]>; speechVoices?: string[]; logicalId?: string; stageRunId?: string };
const bindings = new WeakMap<CreatorServicesConfig, GatewayCreatorBinding>();
@@ -250,7 +251,7 @@ export function createGatewayCreatorSource(input: {
const resource = (request.params as { '*': string })['*'];
if (!/^(chat\/completions|responses|images\/(generations|edits)|audio\/(speech|transcriptions)|video\/(quotes|assets|jobs(\/[A-Za-z0-9_-]{12,128}(\/result)?)?)|videos\/tasks|requests\/[A-Za-z0-9_-]{12,128}(\/result)?)$/.test(resource)) return reply.code(404).send({ error: { code: 'capability_unsupported', message: 'Operation is unavailable' } });
const controller = new AbortController();
- const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(180_000)]);
+ const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(resource === 'audio/speech' ? 600_000 : 180_000)]);
let complete!: () => void;
inFlight.set(controller, { stageRunId: original.stageRunId, done: new Promise<void>(resolve => { complete = resolve; }) });
let streaming = false;
@@ -258,6 +259,7 @@ export function createGatewayCreatorSource(input: {
const chatStream = request.method === 'POST' && resource === 'chat/completions' && (request.body as { stream?: unknown })?.stream === true;
let heartbeat: NodeJS.Timeout | undefined;
const disconnected = () => { if (!reply.raw.writableFinished) controller.abort(); };
+ reply.raw.once('close', disconnected);
const finish = () => {
clearInterval(heartbeat); reply.raw.removeListener('close', disconnected);
inFlight.delete(controller); void releaseUse?.(); releaseUse = undefined; complete();
@@ -286,7 +288,6 @@ export function createGatewayCreatorSource(input: {
const keepAlive = `data: ${JSON.stringify({ id: `chatcmpl-${randomUUID()}`, object: 'chat.completion.chunk',
created: Math.floor(Date.now() / 1000), model: (request.body as { model?: string })?.model ?? '', choices: [] })}\n\n`;
reply.raw.write(keepAlive);
- reply.raw.once('close', disconnected);
heartbeat = setInterval(() => { if (!reply.raw.destroyed && !reply.raw.writableEnded) reply.raw.write(keepAlive); }, 10_000);
heartbeat.unref();
}
@@ -347,15 +348,35 @@ export function createGatewayCreatorSource(input: {
headers['Content-Type'] = native.contentType;
body = Buffer.isBuffer(native.body) ? new Uint8Array(native.body) : JSON.stringify(native.body);
}
- response = await (input.fetcher ?? fetch)(`${credentials.baseUrl}/${resource}`, { method: request.method, headers, body, redirect: 'error', signal });
- if (credentials.transport === 'gateway' && response.status === 401 && input.account.recoverModelCredentials) {
- await response.body?.cancel();
- const updated = await input.account.recoverModelCredentials(credentials.keyVersion);
- if (updated.accountId !== credentials.accountId || updated.baseUrl !== credentials.baseUrl) throw new GatewayError('supplier_binding_changed', 409);
- if (updated.modelKey !== credentials.modelKey) {
- headers.Authorization = `Bearer ${updated.modelKey}`;
- response = await (input.fetcher ?? fetch)(`${credentials.baseUrl}/${resource}`, { method: request.method, headers, body, redirect: 'error', signal });
+ const gatewayRequest = async (target: string, method = 'GET', payload?: typeof body): Promise<Response> => {
+ let result = await (input.fetcher ?? fetch)(`${credentials.baseUrl}/${target}`, { method, headers, body: payload, redirect: 'error', signal });
+ if (credentials.transport === 'gateway' && result.status === 401 && input.account.recoverModelCredentials) {
+ await result.body?.cancel();
+ const updated = await input.account.recoverModelCredentials(credentials.keyVersion);
+ if (updated.accountId !== credentials.accountId || updated.baseUrl !== credentials.baseUrl) throw new GatewayError('supplier_binding_changed', 409);
+ if (updated.modelKey !== headers.Authorization?.replace(/^Bearer /, '')) {
+ headers.Authorization = `Bearer ${updated.modelKey}`;
+ result = await (input.fetcher ?? fetch)(`${credentials.baseUrl}/${target}`, { method, headers, body: payload, redirect: 'error', signal });
+ }
}
+ return result;
+ };
+ const resumeSpeech = credentials.transport === 'gateway' && resource === 'audio/speech' && request.method === 'POST' && receipt?.remoteTaskId;
+ const speechResponse = (accepted?: Response) => resolveGatewaySpeech({ response: accepted, requestId: receipt?.remoteTaskId ?? undefined,
+ signal, request: gatewayRequest, async onReceipt(value) {
+ if (!receipt || !input.ledger) return;
+ if (['submitting', 'waiting_remote', 'unknown_remote_acceptance'].includes(receipt.status)) receipt = input.ledger.markWaitingRemote(receipt.id, value.requestId);
+ receipt = await input.ledger.recover(receipt.id, { lookupByRequestKey: false, async lookup() {
+ if (value.state === 'succeeded') return { status: 'succeeded', remoteTaskId: value.requestId };
+ if (value.state === 'failed' || value.state === 'canceled') return { status: 'failed', remoteTaskId: value.requestId };
+ if (value.state === 'outcome_unknown') return { status: 'not_found' };
+ return { status: 'waiting_remote', remoteTaskId: value.requestId };
+ } });
+ } });
+ if (resumeSpeech) response = await speechResponse();
+ else {
+ response = await gatewayRequest(resource, request.method, body);
+ if (credentials.transport === 'gateway' && resource === 'audio/speech' && request.method === 'POST' && response.status === 202) response = await speechResponse(response);
}
}
signal.throwIfAborted();
@@ -409,8 +430,8 @@ export function createGatewayCreatorSource(input: {
}
return reply.send(stream);
} catch (error) {
- if (receipt && input.ledger && error instanceof GatewayError && [400, 401, 402, 403, 404, 422, 429].includes(error.status) && ['submitting', 'waiting_remote'].includes(receipt.status)) receipt = input.ledger.markFailed(receipt.id);
- else if (receipt?.remoteTaskId) await reconcile();
+ if (receipt?.remoteTaskId) await reconcile();
+ else if (receipt && input.ledger && error instanceof GatewayError && [400, 401, 402, 403, 404, 422, 429].includes(error.status) && ['submitting', 'waiting_remote'].includes(receipt.status)) receipt = input.ledger.markFailed(receipt.id);
else markUnknown();
if (chatStream) { sendStreamError(error); return reply; }
return reply.code(error instanceof GatewayError ? error.status : 503).send({ error: { code: error instanceof GatewayError ? error.code : 'gateway_unavailable', message: 'Official service request could not be completed' } });
diff --git a/apps/daemon/src/gateway/model-selection-probe.ts b/apps/daemon/src/gateway/model-selection-probe.ts
index 05e78e61..0f40670d 100644
--- a/apps/daemon/src/gateway/model-selection-probe.ts
+++ b/apps/daemon/src/gateway/model-selection-probe.ts
@@ -1,7 +1,6 @@
import { createHash, randomUUID } from 'node:crypto';
import { join } from 'node:path';
import { setTimeout as delay } from 'node:timers/promises';
-import sharp from 'sharp';
import { gatewayVideoSizes, type GatewayConnectionDiagnostics, type GatewayModel, type GatewayModelProbeOptions, type PublicErrorFacts } from '@opencreator/protocol';
import { GatewayError, validateGatewayOrigin } from './client.js';
import { chatAnywhereImage } from './chatanywhere-media.js';
@@ -89,6 +88,7 @@ export function createModelSelectionProbe(input: {
break;
}
case 'image': {
+ const { default: sharp } = await import('sharp');
const options = model.imageOptions;
let resource = 'images/generations'; let contentType = 'application/json';
let body: unknown = { model: model.id, prompt: 'A red circle on a white background.', n: 1,
diff --git a/apps/daemon/src/gateway/speech-response.ts b/apps/daemon/src/gateway/speech-response.ts
new file mode 100644
index 00000000..e0b56c7d
--- /dev/null
+++ b/apps/daemon/src/gateway/speech-response.ts
@@ -0,0 +1,52 @@
+import { setTimeout as delay } from 'node:timers/promises';
+import { GatewayError } from './client.js';
+
+type SpeechReceipt = { requestId: string; state: string };
+
+export async function resolveGatewaySpeech(input: {
+ response?: Response;
+ requestId?: string;
+ signal: AbortSignal;
+ request(resource: string): Promise<Response>;
+ onReceipt(receipt: SpeechReceipt): Promise<void>;
+}): Promise<Response> {
+ let requestId = input.requestId;
+ const readReceipt = async (response: Response): Promise<SpeechReceipt> => {
+ if (!response.ok) {
+ await response.body?.cancel();
+ throw new GatewayError(response.status === 401 ? 'auth_required' : 'request_outcome_unknown', response.status === 401 ? 401 : 409);
+ }
+ const value = await response.json() as Partial<SpeechReceipt>;
+ const header = response.headers.get('x-request-id');
+ if (typeof value.requestId !== 'string' || !/^[A-Za-z0-9_-]{12,128}$/.test(value.requestId)
+ || (requestId && value.requestId !== requestId) || (header && value.requestId !== header)
+ || !['submitting', 'waiting_upstream', 'succeeded', 'failed', 'canceled', 'outcome_unknown'].includes(value.state ?? '')) {
+ throw new GatewayError('invalid_gateway_response');
+ }
+ requestId = value.requestId;
+ const receipt = value as SpeechReceipt;
+ await input.onReceipt(receipt);
+ return receipt;
+ };
+ let receipt = input.response ? await readReceipt(input.response) : undefined;
+ for (;;) {
+ input.signal.throwIfAborted();
+ if (!requestId) throw new GatewayError('invalid_gateway_response');
+ if (!receipt) receipt = await readReceipt(await input.request(`requests/${requestId}`));
+ if (receipt.state === 'succeeded') {
+ const response = await input.request(`requests/${requestId}/result`);
+ const contentType = response.headers.get('content-type')?.split(';')[0]?.trim().toLowerCase();
+ if (response.status !== 200 || !(contentType?.startsWith('audio/') || contentType === 'application/octet-stream')) {
+ await response.body?.cancel();
+ throw new GatewayError('request_outcome_unknown', 409);
+ }
+ const headers = new Headers(response.headers);
+ headers.set('X-Request-ID', requestId);
+ return new Response(response.body, { status: 200, headers });
+ }
+ if (receipt.state === 'failed' || receipt.state === 'canceled') throw new GatewayError('official_service_unavailable', 502);
+ if (receipt.state === 'outcome_unknown') throw new GatewayError('request_outcome_unknown', 409);
+ await delay(1_000, undefined, { signal: input.signal });
+ receipt = undefined;
+ }
+}
diff --git a/apps/daemon/test/integration/creator-gateway-speech.test.ts b/apps/daemon/test/integration/creator-gateway-speech.test.ts
new file mode 100644
index 00000000..20c2ddf6
--- /dev/null
+++ b/apps/daemon/test/integration/creator-gateway-speech.test.ts
@@ -0,0 +1,118 @@
+import Fastify from 'fastify';
+import { createDefaultCreatorServicesConfig } from '@opencreator/protocol';
+import { afterEach, expect, it, vi } from 'vitest';
+import { mkdtemp, rm } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { openRuntimeDatabase } from '../../src/storage/database.js';
+import { createCreatorRepository } from '../../src/creator/repository.js';
+import { CreatorProviderRequestLedger } from '../../src/creator/provider-requests.js';
+import { createGatewayCreatorSource } from '../../src/gateway/creator-service-source.js';
+import { resolveGatewaySpeech } from '../../src/gateway/speech-response.js';
+
+const cleanup: Array<() => Promise<void>> = [];
+afterEach(async () => { for (const close of cleanup.splice(0).reverse()) await close(); });
+
+async function fixture(fetcher: typeof fetch) {
+ const root = await mkdtemp(join(tmpdir(), 'oc-gateway-speech-'));
+ const db = openRuntimeDatabase(join(root, 'runtime.sqlite'));
+ const repository = createCreatorRepository(db);
+ const job = repository.createJob({ projectId: 'project', templateId: 'video-translation', templateVersion: 1, status: 'running', state: {} });
+ const stage = repository.createStageRun({ jobId: job.id, stageId: 'tts', executor: 'krillin', status: 'running' });
+ const ledger = new CreatorProviderRequestLedger(repository);
+ const manual = createDefaultCreatorServicesConfig();
+ const source = createGatewayCreatorSource({ ledger, readStageRun: repository.getStageRun,
+ readJobBinding: () => ({ source: 'gateway', accountId: 'a', bindingVersion: 'a:1', models: { speech: 'speech' }, capabilities: { speech: ['tts'] } }),
+ account: {
+ peekState: () => ({ source: 'gateway', transport: 'gateway', authState: 'signed_in', activationState: 'ready', account: { id: 'a', email: '[email protected]', verified: true }, bindingVersion: 'a:1', models: [], activationError: null }),
+ modelCredentials: async () => ({ accountId: 'a', transport: 'gateway', modelKey: 'cloud-secret', keyVersion: '1', bindingVersion: 'a:1', baseUrl: 'https://gateway.example/v1', defaults: { speech: 'speech' }, models: [] })
+ },
+ manual: { read: async () => manual, write: async c => c, reset: async () => manual },
+ getLocalOrigin: () => 'http://127.0.0.1:39999', fetcher
+ });
+ const server = Fastify();
+ await source.register(server);
+ cleanup.push(async () => { await source.revoke(); await server.close(); db.close(); await rm(root, { recursive: true, force: true }); });
+ const synthesize = (stageId = stage.id) => source.runForJob(job.id, stageId, async () => {
+ const config = await source.store.read();
+ return server.inject({ method: 'POST', url: '/internal/krillin-llm/gateway/v1/audio/speech',
+ headers: { Authorization: `Bearer ${config.tts.openai.apiKey}` }, payload: { model: 'speech', voice: 'marin', input: 'Hello', response_format: 'wav' } });
+ });
+ return { repository, ledger, job, stage, synthesize };
+}
+
+it('waits for an accepted speech request and reuses its audio across stage retries', async () => {
+ const requestId = 'ocreq_accepted_speech';
+ let statusQueries = 0;
+ const fetcher = vi.fn<typeof fetch>(async (url, options) => {
+ expect(new Headers(options?.headers).get('authorization')).toBe('Bearer cloud-secret');
+ if (options?.method === 'POST') return Response.json({ requestId, state: 'submitting', billingState: 'reserved' }, { status: 202, headers: { 'X-Request-ID': requestId } });
+ if (String(url).endsWith('/result')) return new Response('RIFF-test-audio', { headers: { 'Content-Type': 'audio/wav' } });
+ return Response.json({ requestId, state: ++statusQueries === 1 ? 'waiting_upstream' : 'succeeded' });
+ });
+ const f = await fixture(fetcher);
+ const response = await f.synthesize();
+ expect(response.statusCode).toBe(200);
+ expect(response.headers['content-type']).toBe('audio/wav');
+ expect(response.headers['x-request-id']).toBe(requestId);
+ expect(response.body).toBe('RIFF-test-audio');
+ expect(f.repository.listProviderRequests(f.job.id)).toEqual([expect.objectContaining({ status: 'succeeded', remoteTaskId: requestId })]);
+ const retry = f.repository.createStageRun({ jobId: f.job.id, stageId: 'tts', executor: 'krillin', status: 'running', progress: { resumedFromStageRunId: f.stage.id } });
+ expect((await f.synthesize(retry.id)).body).toBe('RIFF-test-audio');
+ expect(fetcher.mock.calls.filter(([, options]) => options?.method === 'POST')).toHaveLength(1);
+ expect(fetcher.mock.calls.filter(([url]) => String(url).endsWith('/result'))).toHaveLength(2);
+});
+
+it.each(['failed', 'canceled', 'outcome_unknown'] as const)('preserves the speech receipt when the gateway reports %s', async state => {
+ const requestId = 'ocreq_terminal_speech';
+ const fetcher = vi.fn<typeof fetch>(async (_url, options) => options?.method === 'POST'
+ ? Response.json({ requestId, state }, { status: 202, headers: { 'X-Request-ID': requestId } })
+ : Response.json({ requestId, state }));
+ const f = await fixture(fetcher);
+ expect((await f.synthesize()).statusCode).toBe(state === 'outcome_unknown' ? 409 : 502);
+ expect(f.repository.listProviderRequests(f.job.id)).toEqual([expect.objectContaining({ remoteTaskId: requestId,
+ status: state === 'outcome_unknown' ? 'unknown_remote_acceptance' : 'failed' })]);
+ await f.synthesize();
+ expect(fetcher.mock.calls.filter(([, options]) => options?.method === 'POST')).toHaveLength(1);
+ expect(fetcher.mock.calls.some(([url]) => String(url).endsWith('/result'))).toBe(false);
+});
+
+it('cancels speech polling without another submission', async () => {
+ const controller = new AbortController();
+ const request = vi.fn();
+ await expect(resolveGatewaySpeech({ response: Response.json({ requestId: 'ocreq_canceled_speech', state: 'submitting' }, { status: 202 }),
+ signal: controller.signal, request, onReceipt: async () => { controller.abort(); }
+ })).rejects.toMatchObject({ name: 'AbortError' });
+ expect(request).not.toHaveBeenCalled();
+});
+
+it('recovers an uncertain speech outcome by querying the original request', async () => {
+ const requestId = 'ocreq_recovered_speech';
+ let state = 'outcome_unknown';
+ const fetcher = vi.fn<typeof fetch>(async (url, options) => {
+ if (String(url).endsWith('/result')) return new Response('RIFF-recovered-audio', { headers: { 'Content-Type': 'audio/wav' } });
+ return Response.json({ requestId, state }, { status: options?.method === 'POST' ? 202 : 200 });
+ });
+ const f = await fixture(fetcher);
+ expect((await f.synthesize()).statusCode).toBe(409);
+ state = 'succeeded';
+ const retry = f.repository.createStageRun({ jobId: f.job.id, stageId: 'tts', executor: 'krillin', status: 'running', progress: { resumedFromStageRunId: f.stage.id } });
+ const response = await f.synthesize(retry.id);
+ expect(response.statusCode).toBe(200);
+ expect(response.body).toBe('RIFF-recovered-audio');
+ expect(f.repository.listProviderRequests(f.job.id)[0]?.status).toBe('succeeded');
+ expect(fetcher.mock.calls.filter(([, options]) => options?.method === 'POST')).toHaveLength(1);
+});
+
+it('rejects mismatched receipts and JSON masquerading as speech audio', async () => {
+ const requestId = 'ocreq_valid_speech';
+ const request = vi.fn();
+ await expect(resolveGatewaySpeech({ response: Response.json({ requestId, state: 'succeeded' }, { status: 202, headers: { 'X-Request-ID': 'ocreq_unrelated_speech' } }),
+ signal: new AbortController().signal, request, onReceipt: async () => {}
+ })).rejects.toMatchObject({ code: 'invalid_gateway_response' });
+ expect(request).not.toHaveBeenCalled();
+ request.mockResolvedValue(Response.json({ requestId, state: 'submitting' }));
+ await expect(resolveGatewaySpeech({ response: Response.json({ requestId, state: 'succeeded' }, { status: 202 }),
+ signal: new AbortController().signal, request, onReceipt: async () => {}
+ })).rejects.toMatchObject({ code: 'request_outcome_unknown' });
+});
diff --git a/apps/daemon/test/unit/gateway-account-service.test.ts b/apps/daemon/test/unit/gateway-account-service.test.ts
index 0189f627..da17e354 100644
--- a/apps/daemon/test/unit/gateway-account-service.test.ts
+++ b/apps/daemon/test/unit/gateway-account-service.test.ts
@@ -565,6 +565,69 @@ it('本地账号初始化不联网,恢复期间状态可读且任务不会回
} finally { release(); await f.service.close(); }
});
+it.each(['openrouter', '88api', 'chatanywhere'] as const)('直连账号初始化只读缓存,慢模型目录在后台恢复(%s)', async supplier => {
+ let release!: () => void;
+ const waiting = new Promise<void>(resolve => { release = resolve; });
+ const fetcher = vi.fn<typeof fetch>(async (url, options) => {
+ if (String(url).endsWith('/cdn-cgi/trace')) return new Response('ip=203.0.113.1\nloc=JP\n');
+ await waiting;
+ options?.signal?.throwIfAborted();
+ return Response.json({ data: [{ id: 'gateway-text' }] });
+ });
+ const f = await setup(undefined, undefined, undefined, undefined, root => createOfficialModelAccess({ dataDir: root, fetcher, readProxy: async () => '' }), true);
+ const bootstrap: GatewayBootstrap = { ...await f.client.bootstrap('access-1'), schemaVersion: 2, supplier,
+ transport: supplier === 'openrouter' ? 'openrouter-direct' : 'supplier-direct',
+ baseUrl: supplier === 'openrouter' ? 'https://openrouter.ai/api/v1' : supplier === '88api' ? 'https://api.88api.ai/v1' : 'https://api.chatanywhere.tech/v1' };
+ f.client.bootstrap.mockResolvedValue(bootstrap);
+ await seedSavedAccount(f);
+ try {
+ // A pending fetch must not hold either initialization or account reads open.
+ await f.service.initialize();
+ expect(await f.service.readState()).toMatchObject({ source: 'gateway', authState: 'signed_in', activationState: 'loading', models: bootstrap.models });
+ expect(fetcher).not.toHaveBeenCalled();
+ expect(f.client.bootstrap).not.toHaveBeenCalled();
+ expect(f.client.refresh).not.toHaveBeenCalled();
+ const restoring = f.service.startBackgroundRestore();
+ void f.service.startBackgroundRestore();
+ await expect.poll(() => fetcher.mock.calls.some(([url]) => String(url).includes('/models'))).toBe(true);
+ expect(f.client.bootstrap).toHaveBeenCalledOnce();
+ expect(await f.service.readState()).toMatchObject({ source: 'gateway', activationState: 'loading' });
+ const manual = { startTurn: vi.fn() } as unknown as AppServerRuntimeManager;
+ const runtime = createRuntimeSource(manual, f.service.peekState, () => undefined);
+ expect(() => runtime.startTurn({} as Parameters<AppServerRuntimeManager['startTurn']>[0])).toThrow(GatewayError);
+ expect(manual.startTurn).not.toHaveBeenCalled();
+ expect(f.activate).not.toHaveBeenCalled();
+ release(); await restoring;
+ expect(f.service.peekState().activationState).toBe('ready');
+ expect(f.activate).toHaveBeenCalledOnce();
+ expect(f.probe).not.toHaveBeenCalled();
+ } finally { release(); await f.service.close(); }
+});
+
+it.each(['close', 'logout'] as const)('直连模型目录挂起时可以%s,不会激活迟到的恢复结果', async action => {
+ let catalogSignal: AbortSignal | undefined;
+ const fetcher = vi.fn<typeof fetch>(async (url, options) => {
+ if (String(url).endsWith('/cdn-cgi/trace')) return new Response('ip=203.0.113.1\nloc=JP\n');
+ catalogSignal = options!.signal!;
+ return new Promise<Response>((_resolve, reject) => {
+ catalogSignal!.addEventListener('abort', () => reject(catalogSignal!.reason), { once: true });
+ });
+ });
+ const f = await setup(undefined, undefined, undefined, undefined, root => createOfficialModelAccess({ dataDir: root, fetcher, readProxy: async () => '' }), true);
+ const bootstrap: GatewayBootstrap = { ...await f.client.bootstrap('access-1'), transport: 'openrouter-direct', baseUrl: 'https://openrouter.ai/api/v1' };
+ f.client.bootstrap.mockResolvedValue(bootstrap);
+ await seedSavedAccount(f);
+ try {
+ await f.service.initialize();
+ const restoring = f.service.startBackgroundRestore();
+ await expect.poll(() => !!catalogSignal).toBe(true);
+ await f.service[action](); await restoring;
+ expect(catalogSignal!.aborted).toBe(true);
+ expect(f.activate).not.toHaveBeenCalled();
+ if (action === 'logout') expect(f.service.peekState()).toMatchObject({ source: 'manual', authState: 'signed_out', activationState: 'inactive' });
+ } finally { await f.service.close(); }
+});
+
it('后台恢复失败时保留登录态并允许重试', async () => {
const f = await setup(undefined, undefined, undefined, undefined, undefined, true);
await seedSavedAccount(f);
@@ -919,6 +982,63 @@ it('automatically requests migration on restart and defers it for pending suppli
}
});
+it.each([false, true])('旧轮换凭据收到迁移提示后切换网关,无需重新登录(后台恢复=%s)', async deferred => {
+ const network = vi.fn<typeof fetch>(async () => { throw new Error('Migrated accounts must not access the old supplier'); });
+ const f = await setup(undefined, undefined, undefined, undefined,
+ root => createOfficialModelAccess({ dataDir: root, fetcher: network, readProxy: async () => '' }), deferred);
+ const legacy: GatewayBootstrap = { ...await f.client.bootstrap('access-1'), transport: 'openrouter-direct',
+ baseUrl: 'https://openrouter.ai/api/v1', keyRotation: true };
+ f.client.bootstrap.mockResolvedValue(legacy);
+ await seedSavedAccount(f);
+ const gateway: GatewayBootstrap = { ...legacy, schemaVersion: 3, transport: 'gateway', supplier: 'chatanywhere',
+ baseUrl: `${f.client.origin}/v1`, keyRotation: false, modelKey: undefined, keyVersion: 'gateway-3', bindingVersion: '1:gateway-3',
+ models: legacy.models.map(model => ({ ...model, capabilities: ['responses', 'chat'] })) };
+ f.client.bootstrap.mockResolvedValue(gateway);
+ const request = vi.spyOn(f.client, 'request').mockImplementation(async path => {
+ if (path === '/api/v1/client/usage') throw new GatewayError('gateway_migrated', 409, { path, reason: 'http', httpStatus: 409 });
+ return path === '/api/v1/client/models' ? { models: gateway.models } : { account: gateway.account };
+ });
+ try {
+ await f.service.initialize();
+ if (deferred) {
+ expect(f.service.peekState().activationState).toBe('loading');
+ expect(request).not.toHaveBeenCalled();
+ await f.service.startBackgroundRestore();
+ }
+ expect(f.service.peekState()).toMatchObject({ authState: 'signed_in', activationState: 'ready', transport: 'gateway', activationError: null });
+ expect(f.client.bootstrap).toHaveBeenCalledOnce();
+ expect(f.client.bootstrap).toHaveBeenCalledWith('access-1', { rotate: false, modelUsePolicy: 'cached-key-v1' });
+ expect(request.mock.calls.filter(([path]) => path === '/api/v1/client/usage')).toHaveLength(1);
+ expect(f.activate).toHaveBeenCalledWith(expect.objectContaining({ transport: 'gateway', keyRotation: false }));
+ expect(network).not.toHaveBeenCalled();
+ expect(f.probe).not.toHaveBeenCalled();
+ const saved = await createGatewayConfigStore(f.root).readCredentials();
+ expect(saved).toMatchObject({ schemaVersion: 3, transport: 'gateway', keyRotation: false, accessToken: 'access-1', refreshToken: 'refresh-1' });
+ expect(saved).not.toHaveProperty('modelKey');
+ } finally { await f.service.close(); }
+});
+
+it.each([
+ { code: 'gateway_unavailable', status: 503, migrated: false },
+ { code: 'account_disabled', status: 403, migrated: false },
+ { code: 'gateway_migrated', status: 403, migrated: false },
+ { code: 'gateway_migrated', status: 409, migrated: true }
+])('旧凭据同步失败时不能绕过直连协调($code/$status)', async ({ code, status, migrated }) => {
+ const f = await setup(undefined, undefined, undefined, undefined, undefined, true);
+ const legacy: GatewayBootstrap = { ...await f.client.bootstrap('access-1'), transport: 'openrouter-direct',
+ baseUrl: 'https://openrouter.ai/api/v1', keyRotation: true };
+ f.client.bootstrap.mockResolvedValue(legacy);
+ await seedSavedAccount(f);
+ vi.spyOn(f.client, 'request').mockRejectedValue(new GatewayError(code, status));
+ try {
+ await f.service.startBackgroundRestore();
+ expect(f.service.peekState()).toMatchObject({ activationState: 'blocked', activationError: { code, stage: 'usage' } });
+ expect(f.activate).not.toHaveBeenCalled();
+ expect(f.client.bootstrap).toHaveBeenCalledTimes(migrated ? 1 : 0);
+ expect((await createGatewayConfigStore(f.root).readCredentials())?.transport).toBe('openrouter-direct');
+ } finally { await f.service.close(); }
+});
+
it('does not admit direct use when control-plane coordination fails', async () => {
const f = await setup();
const binding = { ...await f.client.bootstrap('access-1'), keyRotation: true };
diff --git a/apps/desktop/e2e/desktop.spec.ts b/apps/desktop/e2e/desktop.spec.ts
index c29b47d3..f705e8d6 100644
--- a/apps/desktop/e2e/desktop.spec.ts
+++ b/apps/desktop/e2e/desktop.spec.ts
@@ -19,7 +19,7 @@ import {
import { execFileSync } from 'node:child_process';
import { DatabaseSync } from 'node:sqlite';
import { createServer } from 'node:http';
-import type { AddressInfo } from 'node:net';
+import type { AddressInfo, Socket } from 'node:net';
import { fileURLToPath } from 'node:url';
import {
expect,
@@ -28,6 +28,7 @@ import {
} from '@playwright/test';
import { packagedExecutable } from './package-artifact.js';
import type { GatewayCredentials } from '../../daemon/src/gateway/config-store.js';
+import { createOpenCreatorCreatorServicesConfigStore } from '../../daemon/src/creator-services/config-store.js';
import {
closePackagedApp,
launchPackagedApp,
@@ -47,6 +48,68 @@ const fakeCodexLauncherSource = join(e2eDir, 'fixtures', 'fake-codex-launcher.go
test.describe.configure({ mode: 'serial' });
+test('@package-smoke 官方配音等待异步回执后返回音频', async ({}, testInfo) => {
+ const account = { id: 'speech-test', email: '[email protected]', verified: true };
+ const models = [
+ { id: 'text-model', modality: 'text' as const, capabilities: ['chat'] },
+ { id: 'speech-model', modality: 'speech' as const, capabilities: [], voices: ['marin'] }
+ ];
+ const requestId = 'ocreq_packaged_speech';
+ let submissions = 0;
+ let statusQueries = 0;
+ const gateway = createServer((request, response) => {
+ response.setHeader('Content-Type', 'application/json');
+ const path = request.url?.split('?')[0];
+ if (path === '/api/v1/client/bootstrap') response.end(JSON.stringify(bootstrap));
+ else if (path === '/api/v1/me') response.end(JSON.stringify({ account }));
+ else if (path === '/api/v1/client/models') response.end(JSON.stringify({ models, defaults: bootstrap.defaults }));
+ else if (path === '/v1/audio/speech') {
+ submissions++;
+ response.writeHead(202, { 'X-Request-ID': requestId }).end(JSON.stringify({ requestId, state: 'submitting', billingState: 'reserved' }));
+ } else if (path === `/v1/requests/${requestId}`) {
+ statusQueries++;
+ response.end(JSON.stringify({ requestId, state: statusQueries === 1 ? 'waiting_upstream' : 'succeeded' }));
+ } else if (path === `/v1/requests/${requestId}/result`) {
+ response.setHeader('Content-Type', 'audio/wav');
+ response.end('RIFF-packaged-speech');
+ } else response.end(JSON.stringify({ items: [] }));
+ });
+ await new Promise<void>(resolve => gateway.listen(0, '127.0.0.1', resolve));
+ const origin = `http://127.0.0.1:${(gateway.address() as AddressInfo).port}`;
+ const bootstrap = {
+ schemaVersion: 3 as const, account, models, defaults: { text: 'text-model', speech: 'speech-model' },
+ keyVersion: 'speech-1', bindingVersion: 'speech:1', keyRotation: false,
+ transport: 'gateway' as const, supplier: 'chatanywhere' as const, baseUrl: `${origin}/v1`
+ };
+ let fixture: DesktopFixture | undefined;
+ try {
+ fixture = await launchPackagedDesktop('success', { runtimeMode: 'bundled', gatewayOrigin: origin,
+ gatewaySelectedModels: bootstrap.defaults,
+ gatewayCredentials: { ...bootstrap, accessToken: 'speech-private-access', refreshToken: 'speech-private-refresh',
+ expiresIn: 900, accessExpiresAt: new Date(Date.now() + 900_000).toISOString() }
+ });
+ await waitForWorkspace(fixture.page);
+ await expect.poll(async () => (await runtimeRequest<{ activationState: string }>(fixture!.page, 'GET', '/gateway/account')).body.activationState).toBe('ready');
+ expect(await fixture.page.evaluate(() => window.opencreatorDesktop?.readConnectionConfig())).toEqual({ baseUrl: '/.opencreator/runtime' });
+ expect((await runtimeRequest(fixture.page, 'GET', '/projects?status=all')).body)
+ .toMatchObject({ projects: expect.arrayContaining([expect.objectContaining({ name: 'Default project' })]) });
+ const audio = await fixture.page.evaluate(async () => {
+ const response = await fetch('/.opencreator/runtime/creator-services/tts/preview', { method: 'POST',
+ headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ provider: 'openai', voiceId: 'marin', text: 'Hello' }) });
+ return { status: response.status, contentType: response.headers.get('content-type'), content: await response.text() };
+ });
+ expect(audio).toMatchObject({ status: 200, contentType: 'audio/wav', content: 'RIFF-packaged-speech' });
+ expect(submissions).toBe(1);
+ expect(statusQueries).toBe(2);
+ await testInfo.attach('speech-receipt.json', { body: JSON.stringify({ submissions, statusQueries, audio }), contentType: 'application/json' });
+ await fixture.page.screenshot({ path: testInfo.outputPath('speech-receipt-workspace.png') });
+ } finally {
+ if (fixture) await closeFixture(fixture);
+ gateway.closeAllConnections();
+ await new Promise<void>(resolve => gateway.close(() => resolve()));
+ }
+});
+
test('@package-smoke 慢网关不阻塞缓存账号进入工作区,后台恢复完成后可输入任务', async ({}, testInfo) => {
const account = { id: 'startup-test', email: '[email protected]', verified: true };
const models = [{ id: 'official-model', modality: 'text' as const, capabilities: ['responses'] }];
@@ -99,7 +162,7 @@ test('@package-smoke 慢网关不阻塞缓存账号进入工作区,后台恢
expect((await runtimeRequest(fixture.page, 'GET', '/gateway/account')).body)
.toMatchObject({ source: 'gateway', authState: 'signed_in', activationState: 'loading', account });
expect((await runtimeRequest(fixture.page, 'GET', '/projects?status=all')).body)
- .toMatchObject({ projects: expect.arrayContaining([expect.objectContaining({ name: '默认项目' })]) });
+ .toMatchObject({ projects: expect.arrayContaining([expect.objectContaining({ id: expect.any(String), name: 'Default project' })]) });
await testInfo.attach('startup-timing.json', {
body: JSON.stringify({ workspaceReadyMs: Date.now() - startedAt, gatewayStillPending: true }),
contentType: 'application/json'
@@ -119,6 +182,147 @@ test('@package-smoke 慢网关不阻塞缓存账号进入工作区,后台恢
}
});
+test('@package-smoke 直连模型目录挂起时进入工作区,退出取消后台恢复', async ({}, testInfo) => {
+ const account = { id: 'direct-startup', email: '[email protected]', verified: true };
+ const models = [{ id: 'openai/gpt-4.1-mini', modality: 'text' as const, capabilities: ['responses'] }];
+ const tunnels = new Set<Socket>();
+ let connections = 0;
+ const proxy = createServer();
+ proxy.on('connect', (_request, socket) => {
+ const tunnel = socket as Socket;
+ tunnels.add(tunnel); connections++;
+ tunnel.on('error', () => undefined);
+ tunnel.on('close', () => tunnels.delete(tunnel));
+ tunnel.on('end', () => tunnel.end());
+ tunnel.on('data', () => undefined);
+ // Accept CONNECT but hold TLS open, keeping all supplier traffic local and pending.
+ tunnel.write('HTTP/1.1 200 Connection Established\r\n\r\n');
+ });
+ await new Promise<void>(resolve => proxy.listen(0, '127.0.0.1', resolve));
+ const gateway = createServer((request, response) => {
+ response.setHeader('Content-Type', 'application/json');
+ const path = request.url?.split('?')[0];
+ response.end(JSON.stringify(path === '/api/v1/client/bootstrap' ? bootstrap
+ : path === '/api/v1/client/models' ? { models }
+ : path === '/api/v1/me' ? { account } : { keyVersion: '1' }));
+ });
+ await new Promise<void>(resolve => gateway.listen(0, '127.0.0.1', resolve));
+ const origin = `http://127.0.0.1:${(gateway.address() as AddressInfo).port}`;
+ const bootstrap = {
+ schemaVersion: 1 as const, account, models, defaults: { text: models[0]!.id },
+ modelKey: 'private-direct-model-key', keyVersion: '1', bindingVersion: 'direct:1',
+ transport: 'openrouter-direct' as const, baseUrl: 'https://openrouter.ai/api/v1', keyRotation: true
+ };
+ let fixture: DesktopFixture | undefined;
+ try {
+ const startedAt = Date.now();
+ fixture = await launchPackagedDesktop('success', {
+ runtimeMode: 'bundled', gatewayOrigin: origin,
+ proxy: `http://127.0.0.1:${(proxy.address() as AddressInfo).port}`,
+ gatewayCredentials: { ...bootstrap, accessToken: 'private-access-token', refreshToken: 'private-refresh-token',
+ expiresIn: 900, accessExpiresAt: new Date(Date.now() + 900_000).toISOString() }
+ });
+ await fixture.page.waitForURL(url => url.protocol === 'opencreator-app:' && url.hostname === 'app');
+ await expect(fixture.page.locator('.opencreator-shell')).toBeVisible();
+ const workspaceReadyMs = Date.now() - startedAt;
+ const startupLog = readFileSync(join(fixture.root, '.opencreator', 'logs', 'desktop-main.log'), 'utf8')
+ .split('\n').filter(line => /Desktop starting|Daemon Utility Process (starting|ready)|workspace ready/.test(line)).join('\n');
+ const timingPath = testInfo.outputPath('direct-startup-timing.json');
+ writeFileSync(timingPath, JSON.stringify({ workspaceReadyMs, startupLog }, null, 2));
+ await testInfo.attach('direct-startup-timing.json', { path: timingPath, contentType: 'application/json' });
+ await expect.poll(() => fixture!.page.evaluate(async () => (await window.opencreatorDesktop?.readBootstrapState())?.phase)).toBe('ready');
+ // The first tunnel is exit detection; the second is the still-pending model catalog.
+ await expect.poll(() => connections).toBeGreaterThanOrEqual(2);
+ expect(tunnels.size).toBeGreaterThan(0);
+ expect((await runtimeRequest(fixture.page, 'GET', '/gateway/account')).body)
+ .toMatchObject({ source: 'gateway', authState: 'signed_in', activationState: 'loading', account });
+ expect((await runtimeRequest(fixture.page, 'GET', '/projects?status=all')).body)
+ .toMatchObject({ projects: expect.arrayContaining([expect.objectContaining({ id: expect.any(String), name: 'Default project' })]) });
+ await navigateToAppRoute(fixture.page, '#/chat');
+ await expect(fixture.page.getByRole('textbox', { name: '输入任务' })).toBeDisabled();
+ await fixture.page.screenshot({ path: testInfo.outputPath('direct-startup-models-loading.png') });
+ await requestPackagedAppQuit(fixture);
+ expect(await waitForProcessExit(fixture.process, 3_000)).toBe(true);
+ expect(fixture.process.exitCode).toBe(0);
+ await expect.poll(() => tunnels.size).toBe(0);
+ } finally {
+ if (fixture) await closeFixture(fixture);
+ for (const socket of tunnels) socket.destroy();
+ proxy.closeAllConnections(); gateway.closeAllConnections();
+ await Promise.all([
+ new Promise<void>(resolve => proxy.close(() => resolve())),
+ new Promise<void>(resolve => gateway.close(() => resolve()))
+ ]);
+ }
+});
+
+test('@package-smoke 旧账号收到 gateway_migrated 后恢复新网关,个人中心不再报错', async ({}, testInfo) => {
+ const account = { id: 'migrated-startup', email: '[email protected]', verified: true };
+ const models = [{ id: 'gpt-4.1-mini', modality: 'text' as const, capabilities: ['responses', 'chat'] }];
+ let usageRequests = 0;
+ let bootstrapRequests = 0;
+ let bootstrapRotate: string | null = null;
+ const gateway = createServer((request, response) => {
+ response.setHeader('Content-Type', 'application/json');
+ const path = request.url?.split('?')[0];
+ if (path === '/api/v1/client/usage') {
+ usageRequests++;
+ response.writeHead(409).end(JSON.stringify({ error: { code: 'gateway_migrated' } }));
+ } else if (path === '/api/v1/client/bootstrap') {
+ bootstrapRequests++;
+ bootstrapRotate = new URL(request.url!, origin).searchParams.get('rotate');
+ response.end(JSON.stringify(bootstrap));
+ } else if (path === '/api/v1/me') response.end(JSON.stringify({ account }));
+ else if (path === '/api/v1/client/models') response.end(JSON.stringify({ models, defaults: bootstrap.defaults }));
+ else if (path === '/api/v1/billing/summary') response.end(JSON.stringify({
+ balance: { availableUnits: '1000000000', reservedUnits: '0', periodEnd: null, asOf: new Date().toISOString() },
+ subscription: null, billingAvailable: false, plansUrl: `${origin}/pricing`
+ }));
+ else response.end(JSON.stringify({ items: [] }));
+ });
+ await new Promise<void>(resolve => gateway.listen(0, '127.0.0.1', resolve));
+ const origin = `http://127.0.0.1:${(gateway.address() as AddressInfo).port}`;
+ const bootstrap = {
+ schemaVersion: 3 as const, account, models, defaults: { text: models[0]!.id },
+ keyVersion: 'gateway-3', bindingVersion: 'migrated:gateway-3', keyRotation: false,
+ transport: 'gateway' as const, supplier: 'chatanywhere' as const, baseUrl: `${origin}/v1`
+ };
+ let fixture: DesktopFixture | undefined;
+ try {
+ fixture = await launchPackagedDesktop('success', {
+ runtimeMode: 'bundled', gatewayOrigin: origin,
+ gatewayCredentials: { schemaVersion: 1, account,
+ models: [{ id: 'openai/gpt-4.1-mini', modality: 'text', capabilities: ['responses'] }], defaults: { text: 'openai/gpt-4.1-mini' },
+ modelKey: 'obsolete-private-key', keyVersion: 'legacy-1', bindingVersion: 'migrated:legacy-1',
+ transport: 'openrouter-direct', baseUrl: 'https://openrouter.ai/api/v1', keyRotation: true,
+ accessToken: 'private-access-token', refreshToken: 'private-refresh-token', expiresIn: 900,
+ accessExpiresAt: new Date(Date.now() + 900_000).toISOString()
+ }
+ });
+ await waitForWorkspace(fixture.page);
+ await expect.poll(async () => (await runtimeRequest<{ activationState: string }>(fixture!.page, 'GET', '/gateway/account')).body.activationState).toBe('ready');
+ expect(usageRequests).toBe(1);
+ expect(bootstrapRequests).toBe(1);
+ expect(bootstrapRotate).toBe('0');
+ await navigateToAppRoute(fixture.page, '#/account');
+ await expect(fixture.page.getByRole('heading', { name: account.email, exact: true })).toBeVisible();
+ await expect(fixture.page.getByText('网关返回错误,请稍后重试。', { exact: true })).toHaveCount(0);
+ await expect(fixture.page.getByText('官方模型服务暂不可用', { exact: true })).toHaveCount(0);
+ await fixture.page.screenshot({ path: testInfo.outputPath('migrated-account-ready.png') });
+ const saved = JSON.parse(readFileSync(join(fixture.root, '.opencreator', 'data', 'config', 'gateway-credentials.json'), 'utf8')) as GatewayCredentials;
+ expect(saved).toMatchObject({ schemaVersion: 3, transport: 'gateway', keyRotation: false });
+ expect(saved).not.toHaveProperty('modelKey');
+ await navigateToAppRoute(fixture.page, '#/chat');
+ await expect(fixture.page.getByRole('textbox', { name: '输入任务' })).toBeEnabled();
+ await fixture.page.getByRole('textbox', { name: '输入任务' }).fill('迁移后可输入任务');
+ expect(usageRequests).toBe(1);
+ } finally {
+ if (fixture) await closeFixture(fixture);
+ gateway.closeAllConnections();
+ await new Promise<void>(resolve => gateway.close(() => resolve()));
+ }
+});
+
test('@package-smoke 充值回跳进入个人中心并以网关订单确认到账', async ({}, testInfo) => {
let paid = false;
let orderChecks = 0;
@@ -1580,6 +1784,7 @@ async function launchPackagedDesktop(
gatewayOrigin?: string;
gatewayCredentials?: GatewayCredentials;
gatewaySelectedModels?: Record<string, string>;
+ proxy?: string;
} = {}
): Promise<DesktopFixture> {
const root = mkdtempSync(join(tmpdir(), 'opencreator-desktop-e2e-'));
@@ -1630,6 +1835,13 @@ async function launchPackagedDesktop(
options.runtimeMode ?? 'external'
);
}
+ if (options.proxy) {
+ const store = createOpenCreatorCreatorServicesConfigStore({
+ configFile: join(root, '.opencreator', 'config.toml'),
+ credentialsFile: join(root, '.opencreator', 'credentials.json')
+ });
+ await store.write({ ...await store.read(), proxy: options.proxy });
+ }
if (options.misleadingCodexWrapper === true) {
writeMisleadingCodexWrapper(join(root, '.local', 'bin'));
}
diff --git a/runtime/krillinai/docs/paired-subtitle-translation.md b/runtime/krillinai/docs/paired-subtitle-translation.md
index 7e029297..dc3de3be 100644
--- a/runtime/krillinai/docs/paired-subtitle-translation.md
+++ b/runtime/krillinai/docs/paired-subtitle-translation.md
@@ -34,6 +34,8 @@ DeepSeek V4 的 `deepseek-flash`、`deepseek-v4-flash` 和 `deepseek-v4-pro` 使
普通字幕和短字幕共用位置映射。短字幕保留完整目标片段,再独立拆分源文;YouTube 使用 `short_subtitle_max_chars`,音频使用 `MaxWordOneLine`,同时保留原文单行显示上限。无词时间的块级字幕保留普通双语兼容输出。匹配不到精确词文本时使用已有 cue 时间,避免再次整词搜索把合法词内片段判为失败。音频转录全文与词列表标点不同的兼容情况仍通过既有匹配器生成 cue,再共用 cue 位置映射。
+短字幕字符参数是分组目标,普通字幕的 `max_sentence_length` 是所有原文行的硬上限。最后一组只有一个空白分隔词且不超过12显示单位时,允许回并前一组;无空格文字按同样的短尾长度规则处理。回并后仍必须满足单行硬上限和音频词数限制。直接保留原文中对应的连续片段,不给中文、组合字符或长词内部切分额外插入空格;时间由同一位置映射重新取得。此处理不调用模型。
+
原文和译文 SRT 的每条文本内部保持单行,双语 SRT 的两行分别对应目标语言和源语言。第三方播放器的视口、字体和自动折行由播放器控制,本功能仅负责翻译、配对、时间轴和字幕文件。
## 验证
diff --git a/runtime/krillinai/internal/service/subtitle_single_line_test.go b/runtime/krillinai/internal/service/subtitle_single_line_test.go
index 67d8a5b0..02accf60 100644
--- a/runtime/krillinai/internal/service/subtitle_single_line_test.go
+++ b/runtime/krillinai/internal/service/subtitle_single_line_test.go
@@ -94,6 +94,9 @@ func TestPairedTranslationProtectsUnpunctuatedInputAndBatchBudget(t *testing.T)
}
func TestShortSubtitleKeepsTrailingPunctuationWithText(t *testing.T) {
+ previous := config.Conf
+ t.Cleanup(func() { config.Conf = previous })
+ config.Conf.App.MaxSentenceLength = 50
text := "And you can do this with almost anything."
blocks := []*util.SrtBlock{{Index: 1, Timestamp: "00:00:00,000 --> 00:00:04,000", OriginLanguageSentence: text}}
spans, _, err := subtitleBlockTimingSpans(blocks)
@@ -106,7 +109,7 @@ func TestShortSubtitleKeepsTrailingPunctuationWithText(t *testing.T) {
}
var parts []string
for _, block := range short[1] {
- if !hasSubtitleContent(block.OriginLanguageSentence) || subtitleTargetLength(block.OriginLanguageSentence) > 20 {
+ if !hasSubtitleContent(block.OriginLanguageSentence) || subtitleTargetLength(block.OriginLanguageSentence) > 50 {
t.Fatalf("短字幕不能独立显示标点或超限: %+v", block)
}
parts = append(parts, block.OriginLanguageSentence)
@@ -116,6 +119,58 @@ func TestShortSubtitleKeepsTrailingPunctuationWithText(t *testing.T) {
}
}
+func TestShortSubtitleMergesTailWithinSingleLineLimits(t *testing.T) {
+ previous := config.Conf
+ t.Cleanup(func() { config.Conf = previous })
+ for _, tc := range []struct {
+ name, text string
+ groupLimit, lineLimit, maxWords int
+ want []string
+ }{
+ {"英文短尾回并", "And you can do this with almost anything.", 20, 50, 0, []string{"And you can do this", "with almost anything."}},
+ {"回并不得突破单行上限", "And you can do this with almost anything.", 20, 20, 0, []string{"And you can do this", "with almost", "anything."}},
+ {"词数限制仍生效", "one two three four five", 50, 50, 2, []string{"one two", "three four", "five"}},
+ {"无空格原文不插入空格", "甲乙丙丁戊己庚辛壬癸。", 8, 50, 0, []string{"甲乙丙丁", "戊己庚辛壬癸。"}},
+ {"长词内部回并不插入空格", strings.Repeat("a", 26) + ".", 20, 50, 0, []string{strings.Repeat("a", 26) + "."}},
+ {"短字幕配置不得放宽单行上限", "alpha beta gamma delta", 80, 16, 0, []string{"alpha beta gamma", "delta"}},
+ {"组合字符保持完整", "cafe\u0301cafe\u0301.", 6, 50, 0, []string{"cafe\u0301cafe\u0301."}},
+ {"Emoji连接序列保持完整", "👩🏽‍💻👨‍👩‍👧‍👦👩🏽‍💻", 4, 50, 0, []string{"👩🏽‍💻👨‍👩‍👧‍👦👩🏽‍💻"}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ config.Conf.App.MaxSentenceLength = tc.lineLimit
+ blocks := []*util.SrtBlock{{Index: 1, Timestamp: "00:00:00,000 --> 00:00:04,000", OriginLanguageSentence: tc.text}}
+ spans, _, err := subtitleBlockTimingSpans(blocks)
+ if err != nil {
+ t.Fatal(err)
+ }
+ short, err := splitShortSubtitleBlocks(blocks, spans, tc.groupLimit, tc.maxWords)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var parts []string
+ var previousEnd int64
+ for _, block := range short[1] {
+ part := block.OriginLanguageSentence
+ if subtitleTargetLength(part) > tc.lineLimit || tc.maxWords > 0 && len(strings.Fields(part)) > tc.maxWords {
+ t.Fatalf("合并后超过单行或词数限制: %q", part)
+ }
+ start, end, err := parseSRTTimeline(block.Timestamp)
+ if err != nil || start != previousEnd || end <= start {
+ t.Fatalf("短字幕时间不连续: %+v, %v", block, err)
+ }
+ previousEnd = end
+ parts = append(parts, part)
+ }
+ if strings.Join(parts, "\n") != strings.Join(tc.want, "\n") {
+ t.Fatalf("分组错误: got=%q want=%q", parts, tc.want)
+ }
+ if previousEnd != 4000 || compactSubtitleText(strings.Join(parts, "")) != compactSubtitleText(tc.text) || !subtitleSegmentsHaveSafeBoundaries(parts) {
+ t.Fatalf("合并破坏原文、字素或时间范围: %q", parts)
+ }
+ })
+ }
+}
+
func TestPairedTranslationRejectsSourceOverflowAndUnsafeBreaks(t *testing.T) {
translator := &Translator{}
for _, tc := range []struct {
diff --git a/runtime/krillinai/internal/service/subtitle_timing.go b/runtime/krillinai/internal/service/subtitle_timing.go
index 353962a1..374dffd7 100644
--- a/runtime/krillinai/internal/service/subtitle_timing.go
+++ b/runtime/krillinai/internal/service/subtitle_timing.go
@@ -69,13 +69,34 @@ func subtitleBlockTimingSpans(blocks []*util.SrtBlock) ([]subtitleTimingSpan, st
func splitShortSubtitleBlocks(blocks []*util.SrtBlock, spans []subtitleTimingSpan, maxChars, maxWords int) (map[int][]util.SrtBlock, error) {
result := make(map[int][]util.SrtBlock)
position := 0
+ hardLimit := subtitleTargetLimit()
+ if maxChars <= 0 || maxChars > hardLimit {
+ maxChars = hardLimit
+ }
+ fitsLine := func(text string) bool {
+ return subtitleTargetLength(text) <= hardLimit && (maxWords <= 0 || len(strings.Fields(text)) <= maxWords)
+ }
for _, block := range blocks {
parts, err := splitSubtitleText(block.OriginLanguageSentence, func(text string) bool {
- return (maxChars <= 0 || subtitleTargetLength(text) <= maxChars) && (maxWords <= 0 || len(strings.Fields(text)) <= maxWords)
+ return subtitleTargetLength(text) <= maxChars && fitsLine(text)
})
if err != nil {
return nil, err
}
+ // 保留短尾词回并,但不突破单行硬上限或音频短字幕词数限制。
+ if len(parts) > 1 {
+ last := parts[len(parts)-1]
+ if len(strings.Fields(last)) == 1 && subtitleTargetLength(last) <= 12 {
+ tail := strings.TrimSpace(block.OriginLanguageSentence)
+ for _, part := range parts[:len(parts)-2] {
+ tail = strings.TrimSpace(tail[len(part):])
+ }
+ if fitsLine(tail) {
+ parts[len(parts)-2] = tail
+ parts = parts[:len(parts)-1]
+ }
+ }
+ }
for _, part := range parts {
start := subtitlePositionTime(spans, position, false)
position += utf8.RuneCountInString(compactSubtitleText(part))
diff --git a/runtime/krillinai/pkg/openai/tts.go b/runtime/krillinai/pkg/openai/tts.go
index 5e973518..487712c2 100644
--- a/runtime/krillinai/pkg/openai/tts.go
+++ b/runtime/krillinai/pkg/openai/tts.go
@@ -20,6 +20,8 @@ const (
DefaultTTSBaseURL = "https://api.openai.com/v1"
DefaultTTSModel = "gpt-4o-mini-tts"
DefaultTTSVoice = "marin"
+ // Gateway receipts can remain pending while a speech segment is generated.
+ ttsTimeout = 10 * time.Minute
)
type TtsClient struct {
@@ -47,7 +49,7 @@ func NewTtsClient(baseURL, apiKey, model, proxy string) *TtsClient {
Model: strings.TrimSpace(model),
httpClient: &http.Client{
Transport: transport,
- Timeout: 90 * time.Second,
+ Timeout: ttsTimeout,
},
}
}
@@ -87,7 +89,7 @@ func (c *TtsClient) Synthesize(ctx context.Context, options types.TTSSpeechOptio
if err != nil {
return err
}
- ctx, cancel := context.WithTimeout(ctx, 90*time.Second)
+ ctx, cancel := context.WithTimeout(ctx, ttsTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL+"/audio/speech", bytes.NewReader(body))
if err != nil {