Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(voice): 长段语音润色不再因返回数据超限而失败 (#5827)

Dashebded1721a5d
diff --git a/apps/desktop/src/main/voice-input/LiteLlmTextModelClient.ts b/apps/desktop/src/main/voice-input/LiteLlmTextModelClient.ts
index cc665f804f..487dd472d1 100644
--- a/apps/desktop/src/main/voice-input/LiteLlmTextModelClient.ts
+++ b/apps/desktop/src/main/voice-input/LiteLlmTextModelClient.ts
@@ -14,7 +14,13 @@ const log = createLogger('voice-input:refine-model:litellm');
const DISABLE_REFINER_PREWARM = process.env.XDT_VOICE_INPUT_DISABLE_REFINER_PREWARM === '1';
const REFINER_TIMING_ENABLED = process.env.XDT_VOICE_INPUT_REFINER_TIMING === '1';
+// Caps the model's output text and any single SSE event. The whole stream gets
+// a far larger cap: each event wraps one or two characters in ~180 chars of
+// JSON, so a 64K stream cap rejected every refinement over ~550 characters.
+// 2M still bounds endless framing-only events (each chunk rearms the idle
+// timeout) while staying ~10x above the managed server's 1,024-token output.
const MAX_RESPONSE_CHARS = 64_000;
+const MAX_STREAM_CHARS = 2_000_000;
// Own pool for the LiteLLM proxy host (tuning lives in refinerHttpDispatcher.ts).
const refinerDispatcher = createRefinerHttpDispatcher();
@@ -295,17 +301,13 @@ async function readStreamingChatCompletion(input: {
input.onChunk();
const chunk = decoder.decode(value, { stream: true });
rawChars += chunk.length;
- if (rawChars > MAX_RESPONSE_CHARS) {
- // Runaway generation: the channel works, the model is flooding.
- throw markRefinerModelOutputError(
- new Error(`LiteLLM refinement response exceeded ${MAX_RESPONSE_CHARS} characters`),
- );
- }
+ assertResponseWithinLimit(rawChars, MAX_STREAM_CHARS);
buf += normalizeSseLineEndings(chunk);
let idx = buf.indexOf('\n\n');
while (idx >= 0) {
const block = buf.slice(0, idx);
buf = buf.slice(idx + 2);
+ assertResponseWithinLimit(block.length);
const event = parseSseBlock(block);
if (event) {
if (!servedModel && isRecord(event.data) && typeof event.data.model === 'string' && event.data.model) {
@@ -319,6 +321,7 @@ async function readStreamingChatCompletion(input: {
const delta = extractDeltaContent(event.data);
if (delta) {
content += delta;
+ assertResponseWithinLimit(content.length);
input.onTextContent(content);
}
const chunkUsage = extractUsageSummary(event.data);
@@ -326,6 +329,7 @@ async function readStreamingChatCompletion(input: {
}
idx = buf.indexOf('\n\n');
}
+ assertResponseWithinLimit(buf.length);
}
if (buf.trim()) {
const event = parseSseBlock(buf);
@@ -336,6 +340,7 @@ async function readStreamingChatCompletion(input: {
const delta = extractDeltaContent(event.data);
if (delta) {
content += delta;
+ assertResponseWithinLimit(content.length);
input.onTextContent(content);
}
const chunkUsage = extractUsageSummary(event.data);
@@ -356,6 +361,14 @@ async function readStreamingChatCompletion(input: {
};
}
+function assertResponseWithinLimit(chars: number, max = MAX_RESPONSE_CHARS): void {
+ if (chars <= max) return;
+ // Runaway generation: the channel works, the model is flooding.
+ throw markRefinerModelOutputError(
+ new Error(`LiteLLM refinement response exceeded ${max} characters`),
+ );
+}
+
function parseSseBlock(block: string): ParsedSseBlock | null {
const dataLines: string[] = [];
for (const rawLine of block.split('\n')) {
diff --git a/apps/desktop/src/main/voice-input/__tests__/LiteLlmTextModelClient.test.ts b/apps/desktop/src/main/voice-input/__tests__/LiteLlmTextModelClient.test.ts
index 4a7588cbba..f6bac17fe1 100644
--- a/apps/desktop/src/main/voice-input/__tests__/LiteLlmTextModelClient.test.ts
+++ b/apps/desktop/src/main/voice-input/__tests__/LiteLlmTextModelClient.test.ts
@@ -122,6 +122,88 @@ describe('LiteLlmTextModelClient', () => {
expect(isRefinerModelOutputError(error)).toBe(true);
});
+ it('accepts a long refinement whose SSE framing exceeds the output limit', async () => {
+ // Real gateways send one or two characters per event wrapped in ~180 chars
+ // of JSON; a 1,000-character refinement streams over 100,000 bytes.
+ const refined = '长'.repeat(1_000);
+ const deltas = ['{"text":"', ...refined, '"}'];
+ undiciFetchMock.mockImplementation(async () => makeChatCompletionSseResponse(deltas.map((content) => ({
+ id: 'chatcmpl-EXxrC3CJkbLLnU1qOjh6Hn6s6iSdr',
+ object: 'chat.completion.chunk',
+ created: 1791763622,
+ model: 'gpt-5.4-mini',
+ choices: [{ index: 0, delta: { content } }],
+ }))));
+
+ const client = new LiteLlmTextModelClient({
+ proxyApiKey: 'proxy-key',
+ baseUrl: 'https://llm-proxy.example.com',
+ });
+
+ await expect(client.requestJson<{ text: string }>({
+ model: 'm',
+ schemaName: 's',
+ system: 'sys',
+ user: {},
+ })).resolves.toEqual({ text: refined });
+ });
+
+ it('rejects runaway model output as a model-output error', async () => {
+ undiciFetchMock.mockImplementation(async () => makeChatCompletionSseResponse([
+ { choices: [{ delta: { content: `{"text":"${'长'.repeat(64_001)}` } }] },
+ ]));
+
+ const client = new LiteLlmTextModelClient({
+ proxyApiKey: 'proxy-key',
+ baseUrl: 'https://llm-proxy.example.com',
+ });
+
+ const error = await client.requestJson<{ text: string }>({
+ model: 'm',
+ schemaName: 's',
+ system: 'sys',
+ user: {},
+ }).then(
+ () => null,
+ (caught: unknown) => caught,
+ );
+ expect(error).toBeInstanceOf(Error);
+ expect((error as Error).message).toContain('exceeded 64000 characters');
+ expect(isRefinerModelOutputError(error)).toBe(true);
+ });
+
+ it.each([
+ ['one oversized event with little output', [
+ { padding: 'x'.repeat(64_001), choices: [{ delta: { content: '{"text":"短"}' } }] },
+ ]],
+ ['endless framing-only events', Array.from({ length: 12_000 }, () => ({
+ id: 'chatcmpl-EXxrC3CJkbLLnU1qOjh6Hn6s6iSdr',
+ object: 'chat.completion.chunk',
+ created: 1791763622,
+ model: 'gpt-5.4-mini',
+ choices: [{ index: 0, delta: {} }],
+ }))],
+ ])('bounds the stream when the gateway sends %s', async (_case, events) => {
+ undiciFetchMock.mockImplementation(async () => makeChatCompletionSseResponse(events));
+
+ const client = new LiteLlmTextModelClient({
+ proxyApiKey: 'proxy-key',
+ baseUrl: 'https://llm-proxy.example.com',
+ });
+
+ const error = await client.requestJson<{ text: string }>({
+ model: 'm',
+ schemaName: 's',
+ system: 'sys',
+ user: {},
+ }).then(
+ () => null,
+ (caught: unknown) => caught,
+ );
+ expect((error as Error).message).toMatch(/exceeded \d+ characters/);
+ expect(isRefinerModelOutputError(error)).toBe(true);
+ });
+
it('does not mark HTTP transport failures as model-output errors', async () => {
undiciFetchMock.mockImplementation(async () => new Response('upstream exploded', { status: 502 }));
diff --git a/apps/mobile/src/__tests__/mobileVoiceInput.test.ts b/apps/mobile/src/__tests__/mobileVoiceInput.test.ts
index a6a7124281..63cbc18038 100644
--- a/apps/mobile/src/__tests__/mobileVoiceInput.test.ts
+++ b/apps/mobile/src/__tests__/mobileVoiceInput.test.ts
@@ -480,6 +480,84 @@ describe('mobileVoiceInput', () => {
expect(result).toEqual({ text: 'mock refined voice draft' });
});
+ // Real gateways send one or two characters per event wrapped in ~180 chars of
+ // JSON; a 1,000-character refinement streams over 100,000 bytes.
+ const longRefinement = '长'.repeat(1_000);
+ const longRefinementSse = ['{"text":"', ...longRefinement, '"}']
+ .map((content) => `data: ${JSON.stringify({
+ id: 'chatcmpl-EXxrC3CJkbLLnU1qOjh6Hn6s6iSdr',
+ object: 'chat.completion.chunk',
+ created: 1791763622,
+ model: 'gpt-5.4-mini',
+ choices: [{ index: 0, delta: { content } }],
+ })}\n\n`)
+ .join('') + 'data: [DONE]\n\n';
+
+ function longRefinementClient(response: Partial<Response>): MobileLiteLlmTextModelClient {
+ return new MobileLiteLlmTextModelClient({
+ deps: {
+ fetch: (async () => ({ ok: true, status: 200, statusText: 'OK', ...response })) as unknown as typeof fetch,
+ },
+ requestTargetProvider: async () => ({
+ url: 'https://voice.example.com/api/voice/sessions/session-1/refine?provider=auto',
+ authorization: 'Bearer refine-token',
+ }),
+ });
+ }
+
+ const refineRequest = {
+ model: 'auto',
+ system: 'Return JSON.',
+ user: { dictationText: 'raw' },
+ schemaName: 'dictation_refinement',
+ };
+
+ it('accepts a long streamed refinement whose SSE framing exceeds the output limit', async () => {
+ expect(longRefinementSse.length).toBeGreaterThan(64_000);
+ const client = longRefinementClient({ body: new Response(longRefinementSse).body });
+
+ await expect(client.requestJson<{ text: string }>(refineRequest))
+ .resolves.toEqual({ text: longRefinement });
+ });
+
+ it('accepts a long buffered refinement whose SSE framing exceeds the output limit', async () => {
+ const client = longRefinementClient({ body: undefined, text: async () => longRefinementSse });
+
+ await expect(client.requestJson<{ text: string }>(refineRequest))
+ .resolves.toEqual({ text: longRefinement });
+ });
+
+ it('rejects runaway refinement output', async () => {
+ const runaway = `data: ${JSON.stringify({
+ choices: [{ delta: { content: `{"text":"${'长'.repeat(64_001)}` } }],
+ })}\n\n`;
+ const streamed = longRefinementClient({ body: new Response(runaway).body });
+ const buffered = longRefinementClient({ body: undefined, text: async () => runaway });
+
+ await expect(streamed.requestJson(refineRequest)).rejects.toThrow('64000');
+ await expect(buffered.requestJson(refineRequest)).rejects.toThrow('64000');
+ });
+
+ it.each([
+ ['one oversized event with little output', `data: ${JSON.stringify({
+ padding: 'x'.repeat(64_001),
+ choices: [{ delta: { content: '{"text":"短"}' } }],
+ })}\n\n`],
+ ['endless framing-only events', `data: ${JSON.stringify({
+ id: 'chatcmpl-EXxrC3CJkbLLnU1qOjh6Hn6s6iSdr',
+ object: 'chat.completion.chunk',
+ created: 1791763622,
+ model: 'gpt-5.4-mini',
+ choices: [{ index: 0, delta: {} }],
+ })}\n\n`.repeat(12_000)],
+ ])('bounds the response when the gateway sends %s', async (_case, sse) => {
+ const streamed = longRefinementClient({ body: new Response(sse).body });
+ const buffered = longRefinementClient({ body: undefined, text: async () => sse });
+
+ await expect(streamed.requestJson(refineRequest)).rejects.toThrow(/\d{5,} 字符/);
+ await expect(buffered.requestJson(refineRequest)).rejects.toThrow(/\d{5,} 字符/);
+ });
+
it('surfaces managed refine HTTP error details', async () => {
const fetchCloud = vi.fn(async () => ({
ok: false,
diff --git a/apps/mobile/src/session/mobileVoiceInput.ts b/apps/mobile/src/session/mobileVoiceInput.ts
index a44b8ff63b..0a75497479 100644
--- a/apps/mobile/src/session/mobileVoiceInput.ts
+++ b/apps/mobile/src/session/mobileVoiceInput.ts
@@ -121,7 +121,13 @@ interface CloudVoiceDeps {
fetch?: typeof fetch;
}
+// Caps the model's output text and any single SSE event. The whole response
+// gets a far larger cap: each event wraps one or two characters in ~180 chars
+// of JSON, so a 64K response cap rejected every refinement over ~550
+// characters. 2M still bounds endless framing-only events while staying ~10x
+// above the managed server's 1,024-token output.
const MAX_REFINER_RESPONSE_CHARS = 64_000;
+const MAX_REFINER_STREAM_CHARS = 2_000_000;
const MAX_ERROR_DETAIL_CHARS = 1_000;
export function appendVoiceTranscriptDraft(current: string, transcript: string): string {
@@ -396,17 +402,17 @@ export class MobileLiteLlmTextModelClient implements TextModelClient {
}
const buffered = await readBufferedResponseText(response);
if (buffered.trim()) {
- if (buffered.length > MAX_REFINER_RESPONSE_CHARS) {
- throw new Error(i18n.t('composer.voice.refineResponseTooLarge', { max: MAX_REFINER_RESPONSE_CHARS }));
- }
+ assertRefinerResponseWithinLimit(buffered.length, MAX_REFINER_STREAM_CHARS);
const streamedContent = readBufferedChatCompletion(buffered, input.onTextSnapshot);
if (streamedContent) return parseJsonObject(streamedContent) as T;
const payload = parseJsonObject(buffered);
const content = extractChatCompletionContent(payload);
+ assertRefinerResponseWithinLimit(content.length);
return parseJsonObject(content) as T;
}
const payload = await response.json().catch(() => null);
const content = extractChatCompletionContent(payload);
+ assertRefinerResponseWithinLimit(content.length);
return parseJsonObject(content) as T;
}
}
@@ -469,14 +475,13 @@ async function readStreamingChatCompletion(
if (done) break;
const chunk = decoder.decode(value, { stream: true });
rawChars += chunk.length;
- if (rawChars > MAX_REFINER_RESPONSE_CHARS) {
- throw new Error(i18n.t('composer.voice.refineResponseTooLarge', { max: MAX_REFINER_RESPONSE_CHARS }));
- }
+ assertRefinerResponseWithinLimit(rawChars, MAX_REFINER_STREAM_CHARS);
buffer += normalizeSseLineEndings(chunk);
let splitAt = buffer.indexOf('\n\n');
while (splitAt >= 0) {
const block = buffer.slice(0, splitAt);
buffer = buffer.slice(splitAt + 2);
+ assertRefinerResponseWithinLimit(block.length);
const event = parseSseBlock(block);
if (event) {
if (isRecord(event.data) && isRecord(event.data.error)) {
@@ -487,6 +492,7 @@ async function readStreamingChatCompletion(
const delta = extractDeltaContent(event.data);
if (delta) {
content += delta;
+ assertRefinerResponseWithinLimit(content.length);
const snapshot = extractJsonStringFieldSnapshot(content, 'text');
if (snapshot && snapshot !== lastSnapshot) {
lastSnapshot = snapshot;
@@ -496,6 +502,7 @@ async function readStreamingChatCompletion(
}
splitAt = buffer.indexOf('\n\n');
}
+ assertRefinerResponseWithinLimit(buffer.length);
}
if (buffer.trim()) {
@@ -503,6 +510,7 @@ async function readStreamingChatCompletion(
const delta = event ? extractDeltaContent(event.data) : '';
if (delta) {
content += delta;
+ assertRefinerResponseWithinLimit(content.length);
const snapshot = extractJsonStringFieldSnapshot(content, 'text');
if (snapshot && snapshot !== lastSnapshot) onTextSnapshot?.(snapshot);
}
@@ -576,6 +584,7 @@ function readBufferedChatCompletion(raw: string, onTextSnapshot?: (text: string)
const normalized = normalizeSseLineEndings(raw);
for (const block of normalized.split(/\n\n+/)) {
if (!block.trim()) continue;
+ assertRefinerResponseWithinLimit(block.length);
const event = parseSseBlock(block);
if (event) applyChatCompletionEvent(event, state, onTextSnapshot);
}
@@ -612,6 +621,7 @@ function applyChatCompletionEvent(
const delta = extractDeltaContent(event.data);
if (!delta) return;
state.content += delta;
+ assertRefinerResponseWithinLimit(state.content.length);
const snapshot = extractJsonStringFieldSnapshot(state.content, 'text');
if (snapshot && snapshot !== state.lastSnapshot) {
state.lastSnapshot = snapshot;
@@ -619,6 +629,11 @@ function applyChatCompletionEvent(
}
}
+function assertRefinerResponseWithinLimit(chars: number, max = MAX_REFINER_RESPONSE_CHARS): void {
+ if (chars <= max) return;
+ throw new Error(i18n.t('composer.voice.refineResponseTooLarge', { max }));
+}
+
function parseSseBlock(block: string): ParsedSseBlock | null {
const dataLines: string[] = [];
for (const rawLine of block.split('\n')) {