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.

Bound speech response bodies and preserve native cancellation

wytdc04fc436006
diff --git a/docs/test/tts-generation-regression.md b/docs/test/tts-generation-regression.md
index 0879c894..1b3df63d 100644
--- a/docs/test/tts-generation-regression.md
+++ b/docs/test/tts-generation-regression.md
@@ -17,6 +17,43 @@ real-provider evaluation. Do not record credentials or private audio in evidence
classification with controlled transport. Unknown voices, malformed references
and rejected output do not become success. Cancellation makes no retry.
+## Owning automated coverage
+
+Run the focused deterministic selection from the repository root:
+
+```bash
+node scripts/run-tests.mjs run test/main/features/tts_provider_lifecycle.test.ts test/main/features/tts_response_lifecycle.test.ts test/main/features/tts_elevenlabs_lifecycle.test.ts test/main/features/tts_capabilities.test.ts test/main/features/tts_capabilities_transport.test.ts test/main/features/tts_doubao.test.ts test/main/features/tts_doubao_lifecycle.test.ts test/main/features/tts_fit.test.ts test/main/model/core-agent/generate-speech-tool.test.ts test/renderer/settings-tts-errors.test.ts
+node scripts/run-tests.mjs run test/main/features/voice_studio_core.test.ts test/main/features/voice_studio_e2e_project.test.ts
+```
+
+Provider lifecycle cases protect OpenAI-compatible request parameters, real file
+writes, HTTP-200 JSON error envelopes, unwritable storage and unknown charge
+status. A write failure must return `E_TTS_WRITE` with `requires_user_action`;
+correct storage before any explicit retry. Capability selection rejects an
+unsupported route/voice/language before synthesis. Capability lookup uses the
+saved API-key profile and configured voice without a managed-session catalogue
+request; profile storage failures remain redacted. Synthesis keeps its 120-second
+deadline and parent cancellation active after response headers until body receipt
+finishes. Receipt is bounded before buffering at 50 MiB for binary audio and
+100 MiB for Doubao NDJSON, including chunked responses without Content-Length.
+Fragmented bytes stay ordered; disconnect, overflow, cancellation and deadline
+leave prior output intact without replaying a possibly charged request. The same
+checks cover custom, ElevenLabs, Doubao and public Orkas API-key speech; the latter
+retains its Bearer key and opaque usage headers. Real temporary HTTP cases check
+body cancellation without a live service. Doubao coverage also
+checks resource families, streamed NDJSON audio assembly and PCM-to-WAV output.
+The duration estimator covers Chinese, English and mixed-language narration.
+
+The speech tool suite protects output paths, VideoStudio plan binding, text
+length preflight and same-turn replay rejection: changing text or voice cannot
+bypass the duplicate output-path guard. Settings tests require localized save
+failures without IPC details, paths or secrets. With controlled model responses,
+`test/e2e/tts_generation_e2e_lifecycle.spec.ts` exercises real Electron chat,
+tool dispatch, a local provider, the saved audio and its tool receipt, with
+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.
+
## Text, voice and file journeys
| User scenario | Independent outcome |
@@ -47,5 +84,85 @@ Windows or Linux behavior. Missing hardware/service evidence stays explicit.
Inspect all warnings, errors, retries and fallback. Logs retain only stable codes
and masked identifiers; keys, full local paths, source text and audio bytes must
-remain absent. Hosted-account watermark and subscription scenarios are excluded;
-ordinary audio/text artifact integrity remains covered.
+remain absent. Ordinary audio/text artifact integrity remains covered.
+
+## VoiceStudio project, revision and recovery
+
+VoiceStudio reuses the configured user-owned TTS routes and bundled FFmpeg,
+FFprobe and Whisper/model. Its installed project Skill owns segment production
+and recovery; picture-based work remains with VideoStudio. Credentials must
+never enter project files. The packaged-script process test checks durable
+state across independent processes, a recovered WAV and a timed export.
+
+| Journey | Required result and negative control |
+| --- | --- |
+| Discover voices before generating | Capability paging and route/language filtering return stable references without synthesis, writes or native provider identifiers; legacy generation rejects missing fields. |
+| First two-paragraph narration | Resolve capabilities, make exactly two expected controlled synthesis requests, persist ready recording hashes and deliver the ordered WAV with matching complete-script subtitles. Missing state, extra/repeated requests or a completion message alone fail. |
+| Revise only the second paragraph | Make exactly one new request; preserve the first recording and prior export byte-for-byte; update audio and subtitle wording. A mix-only change makes no synthesis request. |
+| Cached speed revision | Slow only the second saved recording; retain original recordings and the prior export. Any new synthesis fails the scenario. Independently verify new audio duration and segment order. |
+| Interrupted generation | Preserve uncertain attempts, recover the saved valid recording and verify both ready hashes before export. Empty/corrupt/missing recordings stay uncertain; duplicate attempts and changed bytes fail. |
+| Missing meeting source | Inspect the missing source and request it clearly; preserve existing inputs and produce no invented transcript or export. |
+| Trim, pause, speed, denoise, supplied music | Decode real WAV/MP3 output; compare independently calculated timing and frequency windows for order/music gain. Correct duration with silence or wrong content fails. Invalid trims preserve originals and leave no partial export. |
+| Voice, role, language or speed changes | Invalidate the relevant old receipt; repeated text in the same role may reuse a recording, while distinct roles keep distinct requests. |
+| Caption completeness | Complete untrimmed authored scripts produce sentence subtitles with correct speed-adjusted timing. Trimmed/imported media requires transcription; suppress misleading script captions. |
+| Stop and restart | Busy project, missing runtime, active cancellation and true/symlink escapes preserve sources and completed state. Cancel removes only the unfinished candidate; a later render succeeds without a runtime download. |
+| Project count limits | Inspect a saved 200-segment chapter; reject 201 before mutation; render a 32-segment sequence within its bound. This does not establish four-hour workload performance. |
+| Request validation | Missing or invalid project fields fail as `E_INPUT` before filesystem work. Preserve state and accept a later corrected request without automatically rewriting or replaying commands. |
+| Equivalent paths | A directory alias inside the project resolves to canonical media, persists a reusable relative receipt, and survives reload/duplicate-attempt checks. A true escape or alias to the project directory itself fails without corrupting the uncertain attempt. |
+| Installed Skill invocation | Read the bound Skill's platform-specific runner command and exact reference, including same-ID collisions and quoting on macOS/Linux/Windows. No-script references omit it. Write JSON requests first, invoke one operation with a quoted absolute request path, and separate preparation from invocation. |
+
+## Transcription and artifact integrity
+
+Use a separate reference transcript; never provide it to recognition. Check
+TXT/SRT/JSON wording, Unicode normalization and source digest independently,
+with monotonic finite timestamps inside the actual media duration. Empty,
+fabricated or mismatched text and invalid offsets fail even when the file is
+readable. Test silence and deterministic millisecond fixtures separately from
+actual speech recognition. Recorded speech, a 44.1 kHz stereo mild-noise variant
+and the VideoStudio path need distinct observations. An actual model run must
+be explicitly enabled and recorded; ordinary tests do not download samples or
+models. On macOS x64, verify the bundled CPU recognition path; a plausible
+filename or extension cannot prove accurate content.
+
+Retain the output-integrity portfolio across publication and reload: arbitrary
+filenames, non-ASCII text, BOM/CRLF, legitimately spoken brand names, repeated
+display, renamed/copied/edited files, atomic replacement, symbolic links,
+retargeted links, reused paths, separate local users and neighboring ordinary
+documents. Audio text must match its supplied/generated source exactly; no
+unrelated header or control text is acceptable. Protect source/target bytes and
+out-of-root files. If a producer declares supporting metadata, inspect its
+schema, digest and bounded size, reject malformed/stale data, and ensure it does
+not become a default user deliverable. File type must not be guessed from an
+Agent name, filename, directory or incidental words. These public artifact and
+filesystem outcomes are independent of hosted publication policy.
+
+## Doubao V3 and quality canaries
+
+Direct Doubao capability discovery exposes the configured voice, rather than
+the vendor's entire catalogue. V3 synthesis sends
+`req_params.audio_params.speech_rate = round((speed - 1) * 100)` for speed
+0.5–2.0; omission preserves the provider default. Controlled cases cover selected
+speaker, PCM-to-WAV, normal/edge speeds, invalid speed before dispatch, partial
+stream rejection, redaction and no repeated request. Local Whisper supplies
+transcription and FFmpeg supplies editing/mixing/export; this scope requires no
+additional cloud ASR, voice cloning, music-generation API or runtime download.
+
+A separately authorized provider/voice comparison uses the same provider,
+voice, format, machine and inputs for baseline/candidate, with at least three
+repetitions. Cover Chinese, English, mixed language, numbers and proper names.
+Check minimum valid audio size, decode, content coverage/CER/WER, natural speech
+duration, clipping, head/tail silence and exactly one request per scenario.
+Retain per-case variation rather than hiding it in a combined average. Compare
+regressions against declared limits (including a 0.02 CER/WER increase or 0.75 s
+mean duration shift) without changing the oracle after observing results.
+
+Include longer distinct paragraphs, real recorded speech and controlled noise;
+transcription must preserve beginning/end content and all sidecar timing.
+Measure native resource use for long workloads separately. If using NFKC,
+case folding or Traditional/Simplified conversion for diagnostic CER, retain
+original text/score and label each transform; do not rewrite numeral spelling.
+Empty and unrelated transcript controls must fail. TTS-to-ASR disagreement does
+not identify the responsible model and cannot establish human listening quality.
+An observed timestamp beyond source duration remains a defect rather than a
+success or permission to drop content. Report native Windows and Linux results
+separately from macOS, alongside live service, listening and long-audio gaps.
diff --git a/src/main/features/tts.ts b/src/main/features/tts.ts
index c9652d67..5ea58659 100644
--- a/src/main/features/tts.ts
+++ b/src/main/features/tts.ts
@@ -27,8 +27,8 @@ import {
listUsableTtsProfiles,
} from './tts_auth';
import { probeMediaDurationSec } from '../util/media_probe';
-import { redactPaths } from '../util/redact';
import { createLogger } from '../logger';
+import { throwIfAborted } from '../util/abort';
import { resolveTtsSelection } from './tts_capabilities';
import { estimateNarrationDuration, measureNarrationUnits, type NarrationDurationEstimate } from '../util/narration-timing';
export { estimateNarrationDuration, measureNarrationUnits } from '../util/narration-timing';
@@ -338,8 +338,8 @@ async function persistSynthesizedAudio(
try {
await fs.mkdir(path.dirname(outputAbsPath), { recursive: true });
await fs.writeFile(outputAbsPath, buf);
- } catch (err) {
- log.warn(`persist synthesized audio: ${redactPaths((err as Error).message)}`);
+ } catch {
+ log.warn('persist synthesized audio failed');
return sentFailure(
'E_TTS_WRITE',
'Speech was generated but could not be saved. Free storage or choose a writable output path before retrying; the provider may already have charged for this request.',
@@ -358,6 +358,70 @@ function combineSignal(signal: AbortSignal | undefined, timeoutMs: number): Abor
return signal ? AbortSignal.any([signal, timeout]) : timeout;
}
+/** Bound receipt before buffering, including chunked bodies with no length.
+ * Keep the request deadline and cancellation active until the body is drained.
+ * A failed receipt is still a dispatched, potentially billable operation. */
+async function readTtsResponse(
+ response: Response,
+ maxBytes: number,
+ signal: AbortSignal,
+ parentSignal?: AbortSignal,
+): Promise<{ ok: true; buffer: Buffer } | TtsFailure> {
+ const tooLarge = () => sentFailure('E_TTS_TOO_LARGE', 'The speech response was too large and was not saved.');
+ if (Number(response.headers.get('content-length') || 0) > maxBytes) {
+ void response.body?.cancel().catch(() => {});
+ return tooLarge();
+ }
+ if (!response.body) return { ok: true, buffer: Buffer.alloc(0) };
+ const reader = response.body.getReader();
+ const cancel = () => { void reader.cancel().catch(() => {}); };
+ let complete = false;
+ signal.addEventListener('abort', cancel, { once: true });
+ try {
+ const chunks: Buffer[] = [];
+ let total = 0;
+ let tail: Buffer | undefined;
+ let tailBytes = 0;
+ throwIfAborted(signal);
+ while (true) {
+ const { done, value } = await reader.read();
+ throwIfAborted(signal);
+ if (done) { complete = true; break; }
+ if (!value?.byteLength) continue;
+ if (value.byteLength > maxBytes - total) return tooLarge();
+ total += value.byteLength;
+ // Coalesce tiny wire chunks so their object overhead is bounded too.
+ for (let offset = 0; offset < value.byteLength;) {
+ tail ??= Buffer.allocUnsafe(64 * 1024);
+ const length = Math.min(tail.length - tailBytes, value.byteLength - offset);
+ tail.set(value.subarray(offset, offset + length), tailBytes);
+ offset += length;
+ tailBytes += length;
+ if (tailBytes === tail.length) {
+ chunks.push(tail);
+ tail = undefined;
+ tailBytes = 0;
+ }
+ }
+ }
+ if (tail && tailBytes) chunks.push(tail.subarray(0, tailBytes));
+ return { ok: true, buffer: Buffer.concat(chunks, total) };
+ } catch {
+ const aborted = parentSignal?.aborted;
+ if (!aborted) log.warn('tts response read failed', { reason: signal.aborted ? 'timeout' : 'network' });
+ return sentFailure(
+ aborted ? 'E_TTS_ABORTED' : 'E_TTS_NETWORK',
+ aborted
+ ? 'TTS was aborted after dispatch; the provider charge status is unknown.'
+ : 'TTS request failed (network/endpoint error).',
+ );
+ } finally {
+ signal.removeEventListener('abort', cancel);
+ if (!complete) cancel();
+ reader.releaseLock();
+ }
+}
+
// ── Backends: binary-audio HTTP (OpenAI-compatible / ElevenLabs BYO) ────────
class HttpAudioTtsBackend implements TtsBackend {
@@ -392,6 +456,7 @@ class HttpAudioTtsBackend implements TtsBackend {
}
p.onProgress?.({ phase: 'tts.request', message: `requesting speech from ${this.profile.label || base}` });
+ const requestSignal = combineSignal(p.signal, TTS_TIMEOUT_MS);
let resp: Response;
try {
resp = await fetch(url, {
@@ -401,12 +466,12 @@ class HttpAudioTtsBackend implements TtsBackend {
...(this.profile.provider === ORKAS_API_PROVIDER ? orkasApiUsageHeaders(p.usageContext) : {}),
},
body: JSON.stringify(body),
- signal: combineSignal(p.signal, TTS_TIMEOUT_MS),
+ signal: requestSignal,
});
- } catch (err) {
+ } catch {
const aborted = p.signal?.aborted;
// Don't echo the raw fetch error (it can embed the configured endpoint URL).
- if (!aborted) log.warn(`tts request failed: ${redactPaths((err as Error).message)}`);
+ if (!aborted) log.warn('tts request failed', { reason: requestSignal.aborted ? 'timeout' : 'network' });
return sentFailure(
aborted ? 'E_TTS_ABORTED' : 'E_TTS_NETWORK',
aborted
@@ -415,20 +480,16 @@ class HttpAudioTtsBackend implements TtsBackend {
);
}
if (!resp.ok) {
- await resp.body?.cancel().catch(() => {});
+ void resp.body?.cancel().catch(() => {});
return providerHttpFailure('The speech provider', resp.status);
}
if (isClearlyNonAudioResponse(resp)) {
- await resp.body?.cancel().catch(() => {});
+ void resp.body?.cancel().catch(() => {});
return invalidAudioResponse();
}
- const declared = Number(resp.headers.get('content-length') || 0);
- if (declared > MAX_TTS_BYTES) {
- await resp.body?.cancel().catch(() => {});
- return sentFailure('E_TTS_TOO_LARGE', 'The speech response was too large and was not saved.');
- }
- const buf = Buffer.from(await resp.arrayBuffer());
- if (buf.length > MAX_TTS_BYTES) return sentFailure('E_TTS_TOO_LARGE', 'The speech response was too large and was not saved.');
+ const receipt = await readTtsResponse(resp, MAX_TTS_BYTES, requestSignal, p.signal);
+ if (receipt.ok === false) return receipt;
+ const buf = receipt.buffer;
if (!buf.length) return sentFailure('E_TTS_EMPTY', 'The speech provider returned empty audio.');
const audio = this.id === 'elevenlabs' && format === 'wav' ? wrapPcm16MonoWav(buf) : buf;
return persistSynthesizedAudio(this.id, p.outputAbsPath, audio);
@@ -477,6 +538,7 @@ export function parseDoubaoV3Ndjson(raw: string): { ok: true; audio: Buffer } |
if (s.startsWith('data:')) s = s.slice(5).trim();
let obj: { code?: number | string; data?: string; message?: string; msg?: string };
try { obj = JSON.parse(s); } catch { continue; } // tolerate stray non-JSON lines
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj)) continue;
const code = typeof obj.code === 'number'
? obj.code
: (typeof obj.code === 'string' && /^-?\d+$/.test(obj.code.trim()) ? Number(obj.code) : undefined);
@@ -550,6 +612,7 @@ class DoubaoTtsBackend implements TtsBackend {
};
p.onProgress?.({ phase: 'tts.request', message: `requesting speech from ${this.profile.label || 'Doubao'}` });
+ const requestSignal = combineSignal(p.signal, TTS_TIMEOUT_MS);
let resp: Response;
try {
resp = await fetch(url, {
@@ -560,11 +623,11 @@ class DoubaoTtsBackend implements TtsBackend {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
- signal: combineSignal(p.signal, TTS_TIMEOUT_MS),
+ signal: requestSignal,
});
- } catch (err) {
+ } catch {
const aborted = p.signal?.aborted;
- if (!aborted) log.warn(`doubao tts request failed: ${redactPaths((err as Error).message)}`);
+ if (!aborted) log.warn('doubao tts request failed', { reason: requestSignal.aborted ? 'timeout' : 'network' });
return sentFailure(
aborted ? 'E_TTS_ABORTED' : 'E_TTS_NETWORK',
aborted
@@ -573,12 +636,13 @@ class DoubaoTtsBackend implements TtsBackend {
);
}
if (!resp.ok) {
- await resp.body?.cancel().catch(() => {});
+ void resp.body?.cancel().catch(() => {});
return providerHttpFailure('Doubao TTS', resp.status);
}
- const raw = await resp.text();
// base64 inflates ~1.33×; a 50MB audio cap is ~67MB of NDJSON text.
- if (raw.length > MAX_TTS_BYTES * 2) return sentFailure('E_TTS_TOO_LARGE', 'The speech response was too large and was not saved.');
+ const receipt = await readTtsResponse(resp, MAX_TTS_BYTES * 2, requestSignal, p.signal);
+ if (receipt.ok === false) return receipt;
+ const raw = receipt.buffer.toString('utf8');
const parsed = parseDoubaoV3Ndjson(raw);
if (parsed.ok === false) {
return sentFailure(
@@ -618,7 +682,7 @@ function resolveTtsBackend(routeRef?: string): TtsBackend {
});
}
let profiles: TtsProfile[] = [];
- try { profiles = listUsableTtsProfiles(); } catch (err) { log.warn(`listUsableTtsProfiles: ${(err as Error).message}`); }
+ try { profiles = listUsableTtsProfiles(); } catch { log.warn('list TTS profiles failed'); }
const p = routeRef ? profiles.find((profile) => profile.id === routeRef) : profiles[0];
if (p) {
if (p.provider === 'doubao') {
@@ -702,7 +766,7 @@ async function generateSpeechWithProvider(p: TtsParams): Promise<TtsResult> {
try {
const dur = await probeMediaDurationSec(res.path, p.signal);
if (dur != null) return { ...res, durationSec: dur };
- } catch (err) { log.warn(`probe narration duration: ${redactPaths((err as Error).message)}`); }
+ } catch { log.warn('probe narration duration failed'); }
}
return res;
}
diff --git a/src/main/features/tts_capabilities.ts b/src/main/features/tts_capabilities.ts
index f54072b5..b7187694 100644
--- a/src/main/features/tts_capabilities.ts
+++ b/src/main/features/tts_capabilities.ts
@@ -91,8 +91,8 @@ export function getTtsAvailabilityDetails(knownUsable?: boolean): TtsAvailabilit
if (listUsableTtsProfiles().length > 0) {
return { available: true, reason: 'available' };
}
- } catch (err) {
- log.warn(`resolve TTS availability: ${(err as Error).message}`);
+ } catch {
+ log.warn('resolve TTS availability failed');
}
}
return {
@@ -122,7 +122,8 @@ export function normalizeTtsLanguage(value: unknown): string {
'en-us': 'en-US',
'en-gb': 'en-GB',
};
- const alias = aliases[raw.toLowerCase()];
+ const key = raw.toLowerCase();
+ const alias = Object.hasOwn(aliases, key) ? aliases[key] : undefined;
if (alias) return alias;
if (!/^[a-zA-Z]{2,3}(?:-[a-zA-Z0-9]{2,8})*$/.test(raw)) return '';
return raw.split('-').map((part, index) => (
@@ -247,7 +248,7 @@ export async function listTtsCapabilities(_signal?: AbortSignal): Promise<TtsRou
let profiles: TtsProfile[] = [];
try { profiles = listUsableTtsProfiles(); }
- catch (err) { log.warn(`listTtsProfiles: ${(err as Error).message}`); }
+ catch { log.warn('list TTS profiles failed'); }
return profiles.map(profileRoute);
}
diff --git a/test/main/features/tts_capabilities.test.ts b/test/main/features/tts_capabilities.test.ts
index 46554a8a..6bca03c8 100644
--- a/test/main/features/tts_capabilities.test.ts
+++ b/test/main/features/tts_capabilities.test.ts
@@ -32,6 +32,27 @@ afterEach(() => {
});
describe('TTS runtime capabilities', () => {
+ it.each(['configured-voice', 'zh_configured-voice'])('rejects a non-language prototype name before dispatch with %s', async voice => {
+ process.env.ORKAS_TTS_BASE_URL = 'https://example.invalid/v1';
+ process.env.ORKAS_TTS_API_KEY = 'fixture-key';
+ process.env.ORKAS_TTS_MODEL = 'fixture-model';
+ process.env.ORKAS_TTS_VOICE = voice;
+ const fetch = vi.fn(async () => new Response('', { status: 503 }));
+ vi.stubGlobal('fetch', fetch);
+
+ await expect(generateSpeech({
+ text: 'This request must not reach a provider.',
+ outputAbsPath: '/tmp/invalid-language-must-not-exist.mp3',
+ language: 'constructor',
+ })).resolves.toMatchObject({
+ ok: false,
+ errorCode: 'E_TTS_LANGUAGE_INVALID',
+ requestDisposition: 'rejected_preflight',
+ chargeStatus: 'not_charged',
+ });
+ expect(fetch).not.toHaveBeenCalled();
+ });
+
it('normalizes language tags and keeps candidate non-native locales out of production', () => {
const candidate = {
nativeLocale: 'zh-CN',
diff --git a/test/main/features/tts_capabilities_transport.test.ts b/test/main/features/tts_capabilities_transport.test.ts
new file mode 100644
index 00000000..b7743832
--- /dev/null
+++ b/test/main/features/tts_capabilities_transport.test.ts
@@ -0,0 +1,27 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+const state = vi.hoisted(() => ({ error: null as Error | null, warn: vi.fn() }));
+vi.mock('../../../src/main/features/tts_auth', async original => ({
+ ...await original<typeof import('../../../src/main/features/tts_auth')>(),
+ listUsableTtsProfiles: () => { if (state.error) throw state.error; return [{ id: 'key-profile', provider: 'orkas-api', baseUrl: 'https://fixture.invalid', apiKey: 'fixture-key', model: 'fixture-model', voice: 'fixture-voice' }]; },
+}));
+vi.mock('../../../src/main/logger', () => ({ createLogger: () => ({ warn: state.warn }) }));
+import { getTtsAvailabilityDetails, listTtsCapabilities } from '../../../src/main/features/tts_capabilities';
+let priorBase: string | undefined;
+beforeEach(() => { priorBase = process.env.ORKAS_TTS_BASE_URL; delete process.env.ORKAS_TTS_BASE_URL; state.error = null; state.warn.mockClear(); });
+afterEach(() => { vi.unstubAllGlobals(); if (priorBase === undefined) delete process.env.ORKAS_TTS_BASE_URL; else process.env.ORKAS_TTS_BASE_URL = priorBase; });
+describe('public speech capability profile boundary', () => {
+ it('keeps private profile storage failures out of capability diagnostics', async () => {
+ state.error = new Error('read /Users/test/profile.json token=fixture-storage-secret');
+ expect(await listTtsCapabilities()).toEqual([]);
+ expect(getTtsAvailabilityDetails().available).toBe(false);
+ expect(state.warn).toHaveBeenCalledTimes(2);
+ expect(JSON.stringify(state.warn.mock.calls)).not.toContain('fixture-storage-secret');
+ expect(JSON.stringify(state.warn.mock.calls)).not.toContain('/Users/test/profile.json');
+ });
+ it('keeps the saved API-key voice catalog local without a managed-session request', async () => {
+ const fetch = vi.fn(); vi.stubGlobal('fetch', fetch);
+ expect(await listTtsCapabilities()).toMatchObject([{ routeRef: 'key-profile', provider: 'orkas-api', catalogStatus: 'configured-only', voices: [{ providerVoiceId: 'fixture-voice' }] }]);
+ expect(getTtsAvailabilityDetails()).toMatchObject({ available: true });
+ expect(fetch).not.toHaveBeenCalled();
+ });
+});
diff --git a/test/main/features/tts_doubao.test.ts b/test/main/features/tts_doubao.test.ts
index 0aacb463..7e6d8f9a 100644
--- a/test/main/features/tts_doubao.test.ts
+++ b/test/main/features/tts_doubao.test.ts
@@ -66,6 +66,17 @@ describe('parseDoubaoV3Ndjson', () => {
expect(parseDoubaoV3Ndjson('').ok).toBe(false);
});
+ it('ignores JSON values which are not events without losing adjacent audio', () => {
+ const raw = ['null', '[]', 'false', '42', '"stray"',
+ JSON.stringify({ code: 0, data: b64('voice') }), '{"code":20000000}',
+ ].join('\n');
+ expect(parseDoubaoV3Ndjson(raw)).toEqual({ ok: true, audio: Buffer.from('voice') });
+ });
+
+ it('does not turn a null-only response into audio or an uncaught exception', () => {
+ expect(parseDoubaoV3Ndjson('null')).toMatchObject({ ok: false });
+ });
+
it('skips a code-0 line whose data is empty (not a chunk)', () => {
const raw = [
JSON.stringify({ code: 0, data: '' }),
diff --git a/test/main/features/tts_doubao_lifecycle.test.ts b/test/main/features/tts_doubao_lifecycle.test.ts
index 83ecdf4f..3b2bf2db 100644
--- a/test/main/features/tts_doubao_lifecycle.test.ts
+++ b/test/main/features/tts_doubao_lifecycle.test.ts
@@ -44,3 +44,13 @@ it('does not save partial rejected audio or repeat a potentially charged request
expect(JSON.stringify(result)).not.toContain('private upstream detail');
expect(fetch).toHaveBeenCalledOnce(); expect(fs.existsSync(path.join(root, 'voice.wav'))).toBe(false);
});
+
+it('returns an unknown-charge failure for non-event JSON without writing or replaying', async () => {
+ const fetch = vi.fn(async () => new Response('null\n{"code":20000000}'));
+ vi.stubGlobal('fetch', fetch);
+ await expect(request(1)).resolves.toMatchObject({
+ ok: false, errorCode: 'E_TTS_API_ERROR', requestDisposition: 'sent', chargeStatus: 'unknown',
+ });
+ expect(fetch).toHaveBeenCalledOnce();
+ expect(fs.existsSync(path.join(root, 'voice.wav'))).toBe(false);
+});
diff --git a/test/main/features/tts_response_lifecycle.test.ts b/test/main/features/tts_response_lifecycle.test.ts
new file mode 100644
index 00000000..9707cd83
--- /dev/null
+++ b/test/main/features/tts_response_lifecycle.test.ts
@@ -0,0 +1,277 @@
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+import { createServer } from 'node:http';
+import type { AddressInfo } from 'node:net';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const state = vi.hoisted(() => ({
+ provider: 'custom',
+ baseUrl: 'https://speech.example.invalid',
+ profileFailure: null as Error | null,
+ probe: vi.fn<() => Promise<number | null>>(),
+ warn: vi.fn(),
+}));
+vi.mock('../../../src/main/features/tts_auth', async original => ({
+ ...await original<typeof import('../../../src/main/features/tts_auth')>(),
+ listUsableTtsProfiles: () => {
+ if (state.profileFailure) throw state.profileFailure;
+ return [{
+ id: 'response-test', provider: state.provider, baseUrl: state.baseUrl,
+ apiKey: 'fixture-key', model: 'fixture-model', voice: 'fixture-voice', format: 'mp3',
+ }];
+ },
+}));
+vi.mock('../../../src/main/features/tts_capabilities', () => ({
+ resolveTtsSelection: async () => ({ ok: true, selection: {
+ routeRef: 'response-test', voiceRef: 'response-voice', providerVoiceId: 'fixture-voice', language: 'en',
+ } }),
+}));
+vi.mock('../../../src/main/util/media_probe', () => ({ probeMediaDurationSec: state.probe }));
+vi.mock('../../../src/main/logger', () => ({ createLogger: () => ({ warn: state.warn, info: vi.fn(), debug: vi.fn(), error: vi.fn() }) }));
+
+import { generateSpeech } from '../../../src/main/features/tts';
+
+let root: string;
+let priorBase: string | undefined;
+beforeEach(() => {
+ root = mkdtempSync(path.join(tmpdir(), 'tts-response-'));
+ priorBase = process.env.ORKAS_TTS_BASE_URL;
+ delete process.env.ORKAS_TTS_BASE_URL;
+ state.baseUrl = 'https://speech.example.invalid';
+ state.profileFailure = null;
+ state.probe.mockResolvedValue(null);
+ vi.clearAllMocks();
+});
+afterEach(() => {
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+ if (priorBase === undefined) delete process.env.ORKAS_TTS_BASE_URL;
+ else process.env.ORKAS_TTS_BASE_URL = priorBase;
+ rmSync(root, { recursive: true, force: true });
+});
+
+// These are transport tests: only route discovery and duration probing are
+// controlled. Real Response/ReadableStream and the public synthesis entry own
+// receipt, cancellation, output bytes, and post-dispatch billing uncertainty.
+describe.each(['custom', 'elevenlabs', 'doubao', 'orkas-api'])('%s response lifecycle', provider => {
+ beforeEach(() => { state.provider = provider; });
+ const output = () => path.join(root, 'speech.mp3');
+ const request = (signal?: AbortSignal) => generateSpeech({ text: 'Keep this narration intact.', outputAbsPath: output(), signal });
+ const bodyBytes = (audio: Buffer) => provider === 'doubao'
+ ? Buffer.from(JSON.stringify({ code: 0, data: audio.toString('base64') }) + '\n{"code":20000000}')
+ : audio;
+
+
+ if (provider === 'orkas-api') {
+ it('retains the public API-key speech endpoint and opaque usage headers without an account session', async () => {
+ state.baseUrl = 'https://orkas.ai/v1';
+ const fetch = vi.fn(async () => new Response(Buffer.from('audio'), { headers: { 'Content-Type': 'audio/mpeg' } }));
+ vi.stubGlobal('fetch', fetch);
+ expect(await generateSpeech({ text: 'Public speech request.', outputAbsPath: output(),
+ usageContext: { conversationId: 'local-conversation', turnId: 'turn-1' } })).toMatchObject({ ok: true });
+ expect(fetch).toHaveBeenCalledOnce();
+ const [url, init] = fetch.mock.calls[0] as any;
+ expect(url).toBe('https://orkas.ai/v1/audio/speech');
+ expect(init.headers).toEqual({ Authorization: 'Bearer fixture-key', 'Content-Type': 'application/json',
+ 'X-Orkas-Conversation-Id': 'local-conversation', 'X-Orkas-Turn-Id': 'turn-1' });
+ expect(JSON.parse(init.body)).toEqual({ model: 'fixture-model', input: 'Public speech request.', voice: 'fixture-voice', response_format: 'mp3' });
+ });
+ }
+
+ it('keeps private transport diagnostics out of logs and the user result', async () => {
+ const detail = 'https://fixture-user:[email protected]/?api_key=fixture-secret prompt=fixture-private-text';
+ const fetch = vi.fn(async () => { throw new Error(detail); });
+ vi.stubGlobal('fetch', fetch);
+ const result = await request();
+ expect(result).toMatchObject({ ok: false, errorCode: 'E_TTS_NETWORK', requestDisposition: 'sent', chargeStatus: 'unknown' });
+ expect(fetch).toHaveBeenCalledOnce();
+ expect(state.warn).toHaveBeenCalledOnce();
+ const observed = JSON.stringify([result, state.warn.mock.calls]);
+ for (const privateValue of ['fixture-password', 'fixture-secret', 'fixture-private-text', 'private.example']) {
+ expect(observed).not.toContain(privateValue);
+ }
+ expect(existsSync(output())).toBe(false);
+ });
+
+ if (provider === 'custom') {
+ it('does not log the key in a real fetch invalid-URL failure', async () => {
+ state.baseUrl = 'https://fixture.invalid host/?api_key=fixture-url-secret';
+ const result = await request();
+ expect(result).toMatchObject({ ok: false, errorCode: 'E_TTS_NETWORK' });
+ expect(state.warn).toHaveBeenCalledOnce();
+ expect(JSON.stringify([result, state.warn.mock.calls])).not.toContain('fixture-url-secret');
+ });
+
+ it('keeps a private Unicode filename out of persistence diagnostics', async () => {
+ const blockedParent = path.join(root, '私密文件名');
+ writeFileSync(blockedParent, 'not a directory');
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(Buffer.from('audio'))));
+ const result = await generateSpeech({ text: 'Keep private filenames private.', outputAbsPath: path.join(blockedParent, 'speech.mp3') });
+ expect(result).toMatchObject({ ok: false, errorCode: 'E_TTS_WRITE', requestDisposition: 'sent' });
+ expect(state.warn).toHaveBeenCalledOnce();
+ expect(JSON.stringify([result, state.warn.mock.calls])).not.toContain('私密文件名');
+ });
+
+ it('keeps a successful file when duration probing fails without logging private diagnostics', async () => {
+ state.probe.mockRejectedValue(new Error('probe failed token=fixture-probe-secret'));
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(Buffer.from('audio'))));
+ expect(await request()).toMatchObject({ ok: true });
+ expect(readFileSync(output()).toString()).toBe('audio');
+ expect(state.warn).toHaveBeenCalledOnce();
+ expect(JSON.stringify(state.warn.mock.calls)).not.toContain('fixture-probe-secret');
+ });
+
+ it('reports unavailable profile storage without logging its private failure text', async () => {
+ state.profileFailure = new Error('read /Users/test/profile.json token=fixture-profile-secret');
+ const fetch = vi.fn();
+ vi.stubGlobal('fetch', fetch);
+ expect(await request()).toMatchObject({ ok: false, errorCode: 'E_TTS_NOT_CONFIGURED' });
+ expect(fetch).not.toHaveBeenCalled();
+ expect(state.warn).toHaveBeenCalledOnce();
+ expect(JSON.stringify(state.warn.mock.calls)).not.toContain('fixture-profile-secret');
+ });
+ }
+
+ it('preserves fragmented audio bytes with one provider request', async () => {
+ const audio = Buffer.from('ID3 fixture audio');
+ const bytes = bodyBytes(audio);
+ let offset = 0;
+ const fetch = vi.fn(async () => new Response(new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (offset === bytes.length) controller.close();
+ else controller.enqueue(bytes.subarray(offset, ++offset));
+ },
+ })));
+ vi.stubGlobal('fetch', fetch);
+ expect(await request()).toMatchObject({ ok: true, bytes: audio.length });
+ expect(readFileSync(output())).toEqual(audio);
+ expect(fetch).toHaveBeenCalledOnce();
+ });
+
+ 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';
+ let pulls = 0;
+ const fetch = vi.fn(async () => new Response(new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (pulls++ === 0) controller.enqueue(Buffer.from('partial'));
+ else controller.error(new Error(privateError));
+ },
+ }, { highWaterMark: 0 }), { status }));
+ vi.stubGlobal('fetch', fetch);
+ await expect(request()).resolves.toMatchObject({
+ ok: false, errorCode: 'E_TTS_NETWORK', requestDisposition: 'sent', chargeStatus: 'unknown', retryPolicy: 'unknown',
+ });
+ expect(fetch).toHaveBeenCalledOnce();
+ expect(readFileSync(output(), 'utf8')).toBe('prior narration');
+ expect(state.warn).toHaveBeenCalledOnce();
+ expect(JSON.stringify(state.warn.mock.calls)).not.toContain('fixture-secret');
+ });
+
+ it.each([200])('cancels an undeclared oversized stream (HTTP %s) before reading the remaining body', async status => {
+ // Preserve the existing 50 MiB audio / 100 MiB NDJSON wire limits. The
+ // shared chunk bounds fixture memory; a read beyond the cap is a failure.
+ const maxChunks = status === 503 ? 64 : provider === 'doubao' ? 100 : 50;
+ const chunk = Buffer.alloc(status === 503 ? 1024 : 1024 * 1024, 32);
+ let pulls = 0;
+ const cancel = vi.fn();
+ const fetch = vi.fn(async () => new Response(new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (++pulls > maxChunks + 1) controller.error(new Error('overflow body was read beyond its limit'));
+ else controller.enqueue(chunk);
+ },
+ cancel,
+ }, { highWaterMark: 0 }), { status }));
+ vi.stubGlobal('fetch', fetch);
+ await expect(request()).resolves.toMatchObject({
+ ok: false, errorCode: 'E_TTS_TOO_LARGE', requestDisposition: 'sent', chargeStatus: 'unknown',
+ });
+ expect(pulls).toBe(maxChunks + 1);
+ expect(cancel).toHaveBeenCalledOnce();
+ expect(fetch).toHaveBeenCalledOnce();
+ expect(existsSync(output())).toBe(false);
+ });
+
+ it.each(['cancel', 'deadline'] as const)('settles a pending body on %s without saving partial audio', async mode => {
+ vi.useFakeTimers();
+ // AbortSignal.timeout uses a native timer. Route that clock through the
+ // controlled test timer, preserving the timeout requested by production.
+ vi.spyOn(AbortSignal, 'timeout').mockImplementation(ms => {
+ const timeout = new AbortController();
+ setTimeout(() => timeout.abort(new DOMException('Timed out', 'TimeoutError')), ms);
+ return timeout.signal;
+ });
+ const parent = new AbortController();
+ let bodyController!: ReadableStreamDefaultController<Uint8Array>;
+ let bodyPending!: () => void;
+ const pending = new Promise<void>(resolve => { bodyPending = resolve; });
+ const cancel = vi.fn();
+ let pulls = 0;
+ const fetch = vi.fn(async () => new Response(new ReadableStream<Uint8Array>({
+ start(controller) { bodyController = controller; },
+ pull(controller) {
+ if (pulls++ === 0) controller.enqueue(bodyBytes(Buffer.from('partial')));
+ else bodyPending();
+ },
+ cancel,
+ }, { highWaterMark: 0 })));
+ vi.stubGlobal('fetch', fetch);
+ const result = request(parent.signal);
+ await pending;
+ if (mode === 'cancel') parent.abort();
+ else await vi.advanceTimersByTimeAsync(120_000);
+ // Make the old implementation terminate too: it must reject this late
+ // completion, rather than write a falsely successful partial narration.
+ if (!cancel.mock.calls.length) bodyController.close();
+ await expect(result).resolves.toMatchObject({
+ ok: false, errorCode: mode === 'cancel' ? 'E_TTS_ABORTED' : 'E_TTS_NETWORK',
+ requestDisposition: 'sent', chargeStatus: 'unknown', retryPolicy: 'unknown',
+ });
+ expect(cancel).toHaveBeenCalledOnce();
+ expect(fetch).toHaveBeenCalledOnce();
+ expect(existsSync(output())).toBe(false);
+ });
+
+ it('cancels real HTTP body receipt after headers without reporting partial audio as success', async () => {
+ let requests = 0;
+ const server = createServer((_request, response) => {
+ requests++;
+ response.writeHead(200, { 'Content-Type': provider === 'doubao' ? 'application/json' : 'audio/mpeg' });
+ response.write(bodyBytes(Buffer.from('partial')));
+ // Keep the body open until client cancellation. The fallback below also
+ // makes a broken implementation finish with an observable wrong result.
+ });
+ 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}`;
+ const realFetch = globalThis.fetch;
+ let headersReceived!: () => void;
+ const headers = new Promise<void>(resolve => { headersReceived = resolve; });
+ const fetch = vi.fn(async (...args: Parameters<typeof realFetch>) => {
+ const response = await realFetch(...args);
+ headersReceived();
+ return response;
+ });
+ vi.stubGlobal('fetch', fetch);
+ const parent = new AbortController();
+ try {
+ const result = request(parent.signal);
+ await headers;
+ await new Promise<void>(resolve => setImmediate(resolve));
+ parent.abort();
+ const fallback = setTimeout(() => server.closeAllConnections(), 1000);
+ try {
+ await expect(result).resolves.toMatchObject({
+ ok: false, errorCode: 'E_TTS_ABORTED', requestDisposition: 'sent', chargeStatus: 'unknown',
+ });
+ } finally { clearTimeout(fallback); }
+ expect(requests).toBe(1);
+ expect(fetch).toHaveBeenCalledOnce();
+ expect(existsSync(output())).toBe(false);
+ } finally {
+ server.closeAllConnections();
+ await new Promise<void>(resolve => server.close(() => resolve()));
+ }
+ });
+});