Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

Require the Doubao terminal completion event

wyt8adc3bde09ec
diff --git a/docs/test/tts-generation-regression.md b/docs/test/tts-generation-regression.md
index 1b3df63d..56d2ec9f 100644
--- a/docs/test/tts-generation-regression.md
+++ b/docs/test/tts-generation-regression.md
@@ -54,6 +54,17 @@ exactly one provider request. `test/e2e/professional_agents_e2e_smoke.spec.ts`
checks selection, prompt/installed Skill execution and a timed WAV export.
These controlled responses do not measure model quality or live service health.
+Direct Doubao V3 synthesis require a parsed `code: 20000000`
+completion event plus audio and no error event. Clean HTTP EOF without that
+event is an incomplete synthesis, including on previously tolerated gateways.
+`tts_doubao.test.ts` covers missing, truncated and look-alike completion frames,
+numeric/string codes, SSE/CRLF and final-line delimiters, and errors before or
+after completion. `tts_doubao_lifecycle.test.ts` and the real local HTTP case in
+`tts_response_lifecycle.test.ts` verify that incomplete responses do not create
+or overwrite audio, do not trigger duration probing or a second synthesis,
+and retain sent/unknown-charge/unknown-retry metadata. Existing cancellation,
+deadline and fragmented-completion cases remain required.
+
## Text, voice and file journeys
| User scenario | Independent outcome |
diff --git a/src/main/features/tts.ts b/src/main/features/tts.ts
index 5ea58659..51079962 100644
--- a/src/main/features/tts.ts
+++ b/src/main/features/tts.ts
@@ -528,10 +528,12 @@ export function deriveDoubaoResourceId(voice: string): string {
/** Concatenate the base64 audio chunks from a Doubao V3 `/unidirectional`
* NDJSON response. Data events are `{code:0,data:"<base64>"}`; the stream ends
- * with `{code:20000000}`; any other code is an error event. Pure → unit-tested. */
+ * with `{code:20000000}`; clean EOF alone does not prove synthesis completed.
+ * Any other code is an error event. Pure → unit-tested. */
export function parseDoubaoV3Ndjson(raw: string): { ok: true; audio: Buffer } | { ok: false; message: string } {
const chunks: Buffer[] = [];
let error = '';
+ let sawDone = false;
for (const line of raw.split('\n')) {
let s = line.trim();
if (!s) continue;
@@ -544,12 +546,15 @@ export function parseDoubaoV3Ndjson(raw: string): { ok: true; audio: Buffer } |
: (typeof obj.code === 'string' && /^-?\d+$/.test(obj.code.trim()) ? Number(obj.code) : undefined);
if (code === 0 && typeof obj.data === 'string' && obj.data) {
chunks.push(Buffer.from(obj.data, 'base64'));
+ } else if (code === DOUBAO_V3_DONE_CODE) {
+ sawDone = true;
} else if (code !== undefined && code !== 0 && code !== DOUBAO_V3_DONE_CODE) {
error = obj.message || obj.msg || `code ${code}`;
}
}
if (error) return { ok: false, message: `Doubao TTS failed: ${error}` };
if (!chunks.length) return { ok: false, message: 'Doubao TTS returned no audio.' };
+ if (!sawDone) return { ok: false, message: 'Doubao TTS stream ended before completion.' };
return { ok: true, audio: Buffer.concat(chunks) };
}
@@ -647,7 +652,7 @@ class DoubaoTtsBackend implements TtsBackend {
if (parsed.ok === false) {
return sentFailure(
'E_TTS_API_ERROR',
- 'Doubao TTS returned an invalid or rejected audio stream.',
+ 'Doubao speech generation failed or did not complete. No audio file was saved; the provider may already have charged for this request.',
);
}
const buf = format === 'wav' ? wrapPcm16MonoWav(parsed.audio) : parsed.audio;
diff --git a/test/main/features/tts_doubao.test.ts b/test/main/features/tts_doubao.test.ts
index 7e6d8f9a..7c262a26 100644
--- a/test/main/features/tts_doubao.test.ts
+++ b/test/main/features/tts_doubao.test.ts
@@ -21,10 +21,40 @@ describe('parseDoubaoV3Ndjson', () => {
if (r.ok) expect(r.audio.toString()).toBe('AAAABBBB');
});
- it('succeeds even without an explicit terminator line', () => {
+ it('rejects audio followed by clean EOF without an explicit terminator', () => {
const r = parseDoubaoV3Ndjson(JSON.stringify({ code: 0, data: b64('hello') }));
- expect(r.ok).toBe(true);
- if (r.ok) expect(r.audio.toString()).toBe('hello');
+ expect(r).toMatchObject({ ok: false, message: expect.stringMatching(/completion/i) });
+ expect(r).not.toHaveProperty('audio');
+ });
+
+ it.each([
+ '{"code":2000000}',
+ '{"code":20000001}',
+ '{"code":"20000000x"}',
+ '{"code":true}',
+ '{"code":[20000000]}',
+ '{"code":{"value":20000000}}',
+ '{"message":"20000000"}',
+ '[{"code":20000000}]',
+ '"{\\"code\\":20000000}"',
+ '{"code":20000000',
+ ])('does not accept a malformed or look-alike completion event: %s', tail => {
+ const r = parseDoubaoV3Ndjson(JSON.stringify({ code: 0, data: b64('partial') }) + '\n' + tail);
+ expect(r.ok).toBe(false);
+ expect(r).not.toHaveProperty('audio');
+ });
+
+ it.each(['', '\n', '\r\n'])('accepts complete CRLF/SSE input with final delimiter %j', delimiter => {
+ const raw = `data: {"code":"0","data":"${b64('voice')}"}\r\ndata: {"code":" 20000000 "}${delimiter}`;
+ expect(parseDoubaoV3Ndjson(raw)).toEqual({ ok: true, audio: Buffer.from('voice') });
+ });
+
+ it.each([false, true])('does not let completion hide a provider error (error follows completion: %s)', errorAfter => {
+ const error = '{"code":40000001,"message":"invalid speaker"}';
+ const done = '{"code":20000000}';
+ const raw = [JSON.stringify({ code: 0, data: b64('partial') }),
+ ...(errorAfter ? [done, error] : [error, done])].join('\n');
+ expect(parseDoubaoV3Ndjson(raw)).toEqual({ ok: false, message: 'Doubao TTS failed: invalid speaker' });
});
it('tolerates blank and stray non-JSON lines between data events', () => {
@@ -34,6 +64,7 @@ describe('parseDoubaoV3Ndjson', () => {
JSON.stringify({ code: 0, data: b64('X') }),
' ',
JSON.stringify({ code: 0, data: b64('Y') }),
+ JSON.stringify({ code: 20000000 }),
].join('\n');
const r = parseDoubaoV3Ndjson(raw);
expect(r.ok).toBe(true);
@@ -81,6 +112,7 @@ describe('parseDoubaoV3Ndjson', () => {
const raw = [
JSON.stringify({ code: 0, data: '' }),
JSON.stringify({ code: 0, data: b64('Z') }),
+ JSON.stringify({ code: 20000000 }),
].join('\n');
const r = parseDoubaoV3Ndjson(raw);
expect(r.ok).toBe(true);
diff --git a/test/main/features/tts_doubao_lifecycle.test.ts b/test/main/features/tts_doubao_lifecycle.test.ts
index 3b2bf2db..dcb6043b 100644
--- a/test/main/features/tts_doubao_lifecycle.test.ts
+++ b/test/main/features/tts_doubao_lifecycle.test.ts
@@ -54,3 +54,20 @@ it('returns an unknown-charge failure for non-event JSON without writing or repl
expect(fetch).toHaveBeenCalledOnce();
expect(fs.existsSync(path.join(root, 'voice.wav'))).toBe(false);
});
+
+it.each([false, true])('rejects clean EOF without completion and preserves the output (existing file: %s)', async existing => {
+ const output = path.join(root, 'voice.wav');
+ const priorAudio = Buffer.from('prior complete narration');
+ if (existing) fs.writeFileSync(output, priorAudio);
+ const fetch = vi.fn(async () => new Response(JSON.stringify({ code: 0, data: pcm.toString('base64') })));
+ vi.stubGlobal('fetch', fetch);
+ const result = await request(1);
+ expect(result).toMatchObject({
+ ok: false, errorCode: 'E_TTS_API_ERROR', requestDisposition: 'sent',
+ chargeStatus: 'unknown', retryPolicy: 'unknown',
+ });
+ expect(result).not.toHaveProperty('path');
+ expect(fetch).toHaveBeenCalledOnce();
+ if (existing) expect(fs.readFileSync(output)).toEqual(priorAudio);
+ else expect(fs.existsSync(output)).toBe(false);
+});
diff --git a/test/main/features/tts_response_lifecycle.test.ts b/test/main/features/tts_response_lifecycle.test.ts
index 9707cd83..995356f8 100644
--- a/test/main/features/tts_response_lifecycle.test.ts
+++ b/test/main/features/tts_response_lifecycle.test.ts
@@ -150,6 +150,32 @@ describe.each(['custom', 'elevenlabs', 'doubao', 'orkas-api'])('%s response life
expect(fetch).toHaveBeenCalledOnce();
});
+ if (provider === 'doubao') {
+ it('rejects a real HTTP 200 response closed before synthesis completion without overwriting or replaying', async () => {
+ writeFileSync(output(), 'prior complete narration');
+ let requests = 0;
+ const server = createServer((_request, response) => {
+ requests++;
+ response.writeHead(200, { 'Content-Type': 'application/json' });
+ response.end(JSON.stringify({ code: 0, data: Buffer.from('partial').toString('base64') }));
+ });
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
+ state.baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
+ try {
+ await expect(request()).resolves.toMatchObject({
+ ok: false, errorCode: 'E_TTS_API_ERROR', requestDisposition: 'sent',
+ chargeStatus: 'unknown', retryPolicy: 'unknown',
+ });
+ expect(requests).toBe(1);
+ expect(readFileSync(output(), 'utf8')).toBe('prior complete narration');
+ expect(state.probe).not.toHaveBeenCalled();
+ } finally {
+ server.closeAllConnections();
+ await new Promise<void>(resolve => server.close(() => resolve()));
+ }
+ });
+ }
+
it.each([200])('returns sent/unknown after a body disconnect (HTTP %s) without overwriting or replaying', async status => {
writeFileSync(output(), 'prior narration');
const privateError = 'connection failed https://private.example/voice?token=fixture-secret';