From 698ee25fea6a0fcffd5caec6a3822199b5e735c4 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Mon, 19 Oct 2026 22:20:40 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AF=20fix:=20Close=20Abandoned=20Downl?= =?UTF-8?q?oad=20and=20Speech=20Streams=20and=20Cap=20Speech=20Input=20(#1?= =?UTF-8?q?7270)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Contain Download and Speech Stream Failures and Cap Speech Input The code output download piped the Code API stream with .pipe, which adds no error listener, so a body that failed mid-stream (a bad gzip body, a reset) became an uncaught exception and the process exited. pipeToResponse uses stream.pipeline, ending the response and destroying the upstream when the client leaves. Text-to-speech piped provider streams the same way and waited only for 'end', so a provider error was uncaught and a client disconnect left the chunk loop waiting. pipeSegment resolves on end and rejects on an error or a disconnect, which ends the loop. A speech request had no length cap, so one request could fan out into thousands of serial paid provider calls. speech.tts.maxInputChars bounds it (default 20,000 characters). * docs: Document the Speech Input Cap in the Example Config * fix: Reject a Speech Segment When the Client Already Left * fix: Tear Down Both Ends of a Failed Speech Segment and Cap Streamed Speech A failed provider segment now destroys the response as well as the source, so a half-written answer cannot hang the client, and a response-side error rejects the segment instead of going unhandled. Automatic playback reads a message only up to speech.tts.maxInputChars, so long messages cannot fan out past the cap either. The download scenario now checks the behavior .pipe lacked: leaving a code download closes the upstream connection right away. * fix: Keep a Late Response Write Error From Going Unhandled * test: Key Slow Code Downloads by File and Send a Browser User Agent * fix: Read Speech Up to the Cap Instead of Refusing It Manual speech now synthesizes the text up to speech.tts.maxInputChars, the same as automatic playback, through capSpeechInput, so the CJS service only passes the capped text along and the request keeps its existing error contract. A streamed message that reaches the cap exactly now finishes, and a segment that fails before any audio went out leaves the response open for the caller's HTTP error. * fix: Count Only Text Against the Speech Cap --- api/server/routes/files/files.js | 5 +- api/server/services/Files/Audio/TTSService.js | 37 +++--- .../services/Files/Audio/TTSService.spec.js | 46 ++++++++ .../services/Files/Audio/streamAudio.js | 3 +- e2e/setup/fake-code-server.js | 41 +++++++ .../scenarios/code-download-stream.spec.ts | 80 +++++++++++++ librechat.example.yaml | 1 + packages/api/src/files/speech.spec.ts | 37 +++++- packages/api/src/files/speech.ts | 30 ++++- packages/api/src/utils/index.ts | 1 + packages/api/src/utils/streams.spec.ts | 109 ++++++++++++++++++ packages/api/src/utils/streams.ts | 83 +++++++++++++ packages/data-provider/src/config.ts | 2 + 13 files changed, 451 insertions(+), 24 deletions(-) create mode 100644 e2e/specs/mock/scenarios/code-download-stream.spec.ts create mode 100644 packages/api/src/utils/streams.spec.ts create mode 100644 packages/api/src/utils/streams.ts diff --git a/api/server/routes/files/files.js b/api/server/routes/files/files.js index 8e90bd15fc..07e402092b 100644 --- a/api/server/routes/files/files.js +++ b/api/server/routes/files/files.js @@ -23,6 +23,7 @@ const { checkToolResourceUploadPermission, resolveAssistantToolPermissions, resolveDownloadPath, + pipeToResponse, } = require('@librechat/api'); const { isUUID, @@ -438,7 +439,9 @@ router.get('/code/download/:session_id/:fileId', async (req, res) => { res.setHeader('Content-Type', 'application/octet-stream'); res.setHeader('X-Content-Type-Options', 'nosniff'); res.setHeader('Cache-Control', 'private, no-store'); - response.data.pipe(res); + pipeToResponse(response.data, res, (error) => + logger.error('[CODE DOWNLOAD] Stream failed', getSafeErrorMetadata(error)), + ); } catch (error) { /* `logAxiosError` redacts buffer/stream response bodies — without * it, a stream-typed axios failure dumps the entire `Readable`'s diff --git a/api/server/services/Files/Audio/TTSService.js b/api/server/services/Files/Audio/TTSService.js index 99b3cdb428..8f73e002a6 100644 --- a/api/server/services/Files/Audio/TTSService.js +++ b/api/server/services/Files/Audio/TTSService.js @@ -6,6 +6,9 @@ const { applyAxiosProxyConfig, resolveConfigSecret, applySSRFSafeAgentIfDirect, + pipeSegment, + pipeToResponse, + capSpeechInput, } = require('@librechat/api'); const { TTSProviders, @@ -295,10 +298,6 @@ class TTSService { async processTextToSpeech(req, res) { const { input, voice: requestVoice } = req.body; - if (!input) { - return res.status(400).send('Missing text in request body'); - } - const appConfig = req.config ?? (await getAppConfig({ @@ -306,6 +305,10 @@ class TTSService { userId: req.user?.id, tenantId: req.user?.tenantId, })); + const text = capSpeechInput(input, appConfig?.speech?.tts); + if (!text) { + return res.status(400).send('Missing text in request body'); + } try { res.setHeader('Content-Type', 'audio/mpeg'); const provider = this.getProvider(appConfig); @@ -313,18 +316,20 @@ class TTSService { const allowedAddresses = appConfig?.speech?.tts?.allowedAddresses; const voice = await this.getVoice(ttsSchema, requestVoice); - if (input.length < 4096) { + if (text.length < 4096) { const response = await this.ttsRequest( provider, ttsSchema, - { input, voice }, + { input: text, voice }, allowedAddresses, ); - response.data.pipe(res); + pipeToResponse(response.data, res, (error) => + logAxiosError({ message: '[TTS] Audio stream failed:', error }), + ); return; } - const textChunks = splitTextIntoChunks(input, 1000); + const textChunks = splitTextIntoChunks(text, 1000); for (const chunk of textChunks) { try { @@ -340,10 +345,7 @@ class TTSService { ); logger.debug(`[textToSpeech] user: ${req?.user?.id} | writing audio stream`); - await new Promise((resolve) => { - response.data.pipe(res, { end: chunk.isFinished }); - response.data.on('end', resolve); - }); + await pipeSegment(response.data, res, { end: chunk.isFinished }); if (chunk.isFinished) { break; @@ -399,7 +401,11 @@ class TTSService { shouldContinue = false; }); - const processChunks = createChunkProcessor(req.user.id, req.body.messageId); + const processChunks = createChunkProcessor( + req.user.id, + req.body.messageId, + appConfig?.speech?.tts?.maxInputChars, + ); try { while (shouldContinue) { @@ -432,10 +438,7 @@ class TTSService { } logger.debug(`[streamAudio] user: ${req?.user?.id} | writing audio stream`); - await new Promise((resolve) => { - response.data.pipe(res, { end: update.isFinished }); - response.data.on('end', resolve); - }); + await pipeSegment(response.data, res, { end: update.isFinished }); if (update.isFinished) { shouldContinue = false; diff --git a/api/server/services/Files/Audio/TTSService.spec.js b/api/server/services/Files/Audio/TTSService.spec.js index 8273d8b4e5..efe0d84591 100644 --- a/api/server/services/Files/Audio/TTSService.spec.js +++ b/api/server/services/Files/Audio/TTSService.spec.js @@ -5,8 +5,13 @@ jest.mock('@librechat/api', () => ({ logAxiosError: jest.fn(), applyAxiosProxyConfig: jest.fn(), resolveConfigSecret: jest.fn(), + capSpeechInput: jest.requireActual('@librechat/api').capSpeechInput, + pipeSegment: jest.fn(), + pipeToResponse: jest.fn(), })); jest.mock('librechat-data-provider', () => ({ + /** Real constants: `validateTTSInput` comes from the built `@librechat/api`, which reads them. */ + ...jest.requireActual('librechat-data-provider'), extractEnvVariable: jest.fn((value) => value), /* Real selection logic: these tests are about which provider a schema resolves to. */ listConfiguredSpeechProviders: @@ -105,3 +110,44 @@ describe('TTSService getProvider detection', () => { ).rejects.toThrow('Multiple providers are set'); }); }); + +describe('TTSService input cap', () => { + it('sends a provider only the text up to the configured cap', async () => { + const service = new TTSService(); + jest.spyOn(service, 'getVoice').mockResolvedValue('alloy'); + const ttsRequest = jest.spyOn(service, 'ttsRequest').mockResolvedValue({ data: {} }); + const res = { status: jest.fn().mockReturnThis(), send: jest.fn(), setHeader: jest.fn() }; + + await service.processTextToSpeech( + { + body: { input: 'a'.repeat(11) }, + config: { + speech: { tts: { maxInputChars: 10, openai: { apiKey: 'sk', model: 'tts-1' } } }, + }, + }, + res, + ); + + expect(ttsRequest).toHaveBeenCalledTimes(1); + expect(ttsRequest.mock.calls[0][2]).toEqual({ input: 'a'.repeat(10), voice: 'alloy' }); + expect(res.status).not.toHaveBeenCalled(); + }); + + it('refuses a non-text body before calling any provider', async () => { + const service = new TTSService(); + const ttsRequest = jest.spyOn(service, 'ttsRequest'); + const res = { status: jest.fn().mockReturnThis(), send: jest.fn(), setHeader: jest.fn() }; + + await service.processTextToSpeech( + { + body: { input: ['a'.repeat(30_000)] }, + config: { speech: { tts: { openai: { apiKey: 'sk', model: 'tts-1' } } } }, + }, + res, + ); + + expect(res.status).toHaveBeenCalledWith(400); + expect(res.send).toHaveBeenCalledWith('Missing text in request body'); + expect(ttsRequest).not.toHaveBeenCalled(); + }); +}); diff --git a/api/server/services/Files/Audio/streamAudio.js b/api/server/services/Files/Audio/streamAudio.js index 8f674261f8..8376e5817f 100644 --- a/api/server/services/Files/Audio/streamAudio.js +++ b/api/server/services/Files/Audio/streamAudio.js @@ -50,12 +50,13 @@ function getRandomVoiceId(voiceIds) { * @param {string} messageId * @returns {() => Promise<{ text: string, isFinished: boolean }[] | string>} */ -function createChunkProcessor(user, messageId) { +function createChunkProcessor(user, messageId, maxInputChars) { return createSpeechChunkProcessor({ user, messageId, cache: getLogStores(CacheKeys.MESSAGES), getMessage, + maxInputChars, }); } diff --git a/e2e/setup/fake-code-server.js b/e2e/setup/fake-code-server.js index d441ac6fe1..428aa859e2 100644 --- a/e2e/setup/fake-code-server.js +++ b/e2e/setup/fake-code-server.js @@ -226,6 +226,12 @@ async function handleExec(req, res) { sendJson(res, 200, { stdout: 'E2E code exec ok\n', stderr: '', files: [] }); } +/** File ids set through `POST /__debug/slow-downloads` download as a long trickle, so a spec can + * disconnect mid-stream. `GET /__debug/downloads?fileId=` reports how many of those bodies were + * cut off by LibreChat closing the upstream. Keyed by file id so parallel projects stay apart. */ +const slowDownloadFiles = new Set(); +const closedSlowDownloads = new Map(); + const server = http.createServer((req, res) => { const url = new URL(req.url, `http://${req.headers.host}`); const { pathname } = url; @@ -242,7 +248,23 @@ const server = http.createServer((req, res) => { return; } + if (pathname === '/__debug/slow-downloads' && req.method === 'POST') { + const { fileId, slow } = await readJson(req); + if (slow === true) slowDownloadFiles.add(fileId); + else slowDownloadFiles.delete(fileId); + sendJson(res, 200, { ok: true }); + return; + } + + if (pathname === '/__debug/downloads' && req.method === 'GET') { + const fileId = url.searchParams.get('fileId'); + sendJson(res, 200, { closed: closedSlowDownloads.get(fileId) ?? 0 }); + return; + } + if (pathname === '/__debug/reset' && req.method === 'POST') { + slowDownloadFiles.clear(); + closedSlowDownloads.clear(); sessions.clear(); generated.clear(); uploads.length = 0; @@ -267,6 +289,25 @@ const server = http.createServer((req, res) => { } const downloadMatch = pathname.match(/^\/v1\/download\/([^/]+)\/([^/]+)$/); + const slowFileId = downloadMatch ? decodeURIComponent(downloadMatch[2]) : ''; + if (downloadMatch && req.method === 'GET' && slowDownloadFiles.has(slowFileId)) { + res.writeHead(200, { 'Content-Type': 'text/csv' }); + let sent = 0; + const timer = setInterval(() => { + res.write(Buffer.alloc(16 * 1024, 'a')); + if (++sent >= 600) { + clearInterval(timer); + res.end(); + } + }, 50); + res.on('close', () => { + clearInterval(timer); + if (!res.writableFinished) { + closedSlowDownloads.set(slowFileId, (closedSlowDownloads.get(slowFileId) ?? 0) + 1); + } + }); + return; + } if (downloadMatch && req.method === 'GET') { const key = `${decodeURIComponent(downloadMatch[1])}/${decodeURIComponent(downloadMatch[2])}`; const file = generated.get(key); diff --git a/e2e/specs/mock/scenarios/code-download-stream.spec.ts b/e2e/specs/mock/scenarios/code-download-stream.spec.ts new file mode 100644 index 0000000000..b41e1192da --- /dev/null +++ b/e2e/specs/mock/scenarios/code-download-stream.spec.ts @@ -0,0 +1,80 @@ +import http from 'node:http'; +import { randomBytes } from 'node:crypto'; +import { expect, test } from '@playwright/test'; +import { getE2EUser } from '../../../setup/user'; +import { CODE_API_BASE } from '../helpers'; + +/** The route accepts 21-character ids; a fresh file id keeps parallel projects apart. */ +const SESSION_ID = 'e2esessionstream00001'; +const newFileId = (): string => randomBytes(16).toString('hex').slice(0, 21); +const BROWSER_UA = + 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'; + +/** Starts a download and drops the connection as soon as the first bytes arrive. */ +function downloadAndDisconnect(url: string, token: string): Promise { + return new Promise((resolve, reject) => { + /** The API rejects requests without a browser user agent before any route runs. */ + const headers = { + Authorization: `Bearer ${token}`, + 'User-Agent': BROWSER_UA, + }; + const req = http.get(url, { headers }, (res) => { + if (res.statusCode !== 200) { + reject(new Error(`download answered ${res.statusCode}`)); + return; + } + res.once('data', () => { + req.destroy(); + resolve(); + }); + }); + req.on('error', () => undefined); + }); +} + +test.describe('code output downloads', () => { + /* The fake code API trickles a long body. Once the browser side leaves, LibreChat must close + its upstream download too instead of holding the connection open, paused, until a timeout. */ + test('leaving a download closes the upstream code download @scenario:abandoned-code-download-closes-upstream', async ({ + playwright, + baseURL, + request, + }) => { + const client = await playwright.request.newContext({ baseURL }); + try { + const { email, password } = getE2EUser(); + const login = await client.post('/api/auth/login', { data: { email, password } }); + expect(login.ok()).toBeTruthy(); + const { token } = (await login.json()) as { token: string }; + + const fileId = newFileId(); + const slow = await request.post(`${CODE_API_BASE}/__debug/slow-downloads`, { + data: { fileId, slow: true }, + }); + expect(slow.ok()).toBeTruthy(); + + await downloadAndDisconnect( + new URL(`/api/files/code/download/${SESSION_ID}/${fileId}`, baseURL).toString(), + token, + ); + + await expect + .poll( + async () => { + const response = await request.get( + `${CODE_API_BASE}/__debug/downloads?fileId=${fileId}`, + ); + return ((await response.json()) as { closed: number }).closed; + }, + { timeout: 5_000, intervals: [250] }, + ) + .toBe(1); + expect((await client.get('/health')).status()).toBe(200); + await request.post(`${CODE_API_BASE}/__debug/slow-downloads`, { + data: { fileId, slow: false }, + }); + } finally { + await client.dispose(); + } + }); +}); diff --git a/librechat.example.yaml b/librechat.example.yaml index 0a9b555085..a0ca0e585c 100644 --- a/librechat.example.yaml +++ b/librechat.example.yaml @@ -503,6 +503,7 @@ registration: # connect-time guard only covers direct, non-proxied connections. # speech: # tts: +# maxInputChars: 20000 # longest text one request may synthesize (each 1,000 chars is a provider call) # openai: # url: '' # apiKey: '${TTS_API_KEY}' diff --git a/packages/api/src/files/speech.spec.ts b/packages/api/src/files/speech.spec.ts index b939bac4da..66c991b18a 100644 --- a/packages/api/src/files/speech.spec.ts +++ b/packages/api/src/files/speech.spec.ts @@ -1,11 +1,11 @@ import { ContentTypes } from 'librechat-data-provider'; import type { TMessage } from 'librechat-data-provider'; import type { SpeechMessageCache } from './speech'; -import { createSpeechChunkProcessor } from './speech'; +import { capSpeechInput, createSpeechChunkProcessor, DEFAULT_TTS_MAX_INPUT_CHARS } from './speech'; const messageId = 'message-id'; -function setup(stored: TMessage | null = null) { +function setup(stored: TMessage | null = null, maxInputChars?: number) { const entries = new Map[1]>(); const cache = { get: jest.fn(async (key: string) => entries.get(key)), @@ -19,6 +19,7 @@ function setup(stored: TMessage | null = null) { messageId, cache, getMessage, + maxInputChars, }); return { cache, getMessage, processChunks }; } @@ -64,6 +65,20 @@ describe('createSpeechChunkProcessor', () => { ]); }); + it('finishes a message that reaches the cap exactly', async () => { + const { cache, processChunks } = setup(null, 10); + cache.get.mockResolvedValueOnce({ text: 'Ten chars.', complete: false }); + + expect(await processChunks()).toEqual([{ text: 'Ten chars.', isFinished: true }]); + }); + + it('stops a message longer than the cap at the cap', async () => { + const { cache, processChunks } = setup(null, 10); + cache.get.mockResolvedValueOnce({ text: 'Twelve chars and then some more', complete: false }); + + expect(await processChunks()).toEqual([{ text: 'Twelve cha', isFinished: true }]); + }); + it('sends an incomplete message without separators whole', async () => { const text = 'This is a long message without separators hello there my friend'; const { cache, processChunks } = setup(); @@ -114,3 +129,21 @@ describe('createSpeechChunkProcessor', () => { expect(await processChunks()).toEqual([{ text: 'The answer is 4.', isFinished: true }]); }); }); + +describe('capSpeechInput', () => { + it('keeps text within the default cap whole', () => { + const text = 'a'.repeat(DEFAULT_TTS_MAX_INPUT_CHARS); + expect(capSpeechInput(text)).toBe(text); + }); + + it.each([['a', 'b'], { length: 5 }, 42, undefined])( + 'treats a non-text body (%j) as empty', + (input) => { + expect(capSpeechInput(input)).toBe(''); + }, + ); + + it('reads text over the configured cap only up to the cap', () => { + expect(capSpeechInput('a'.repeat(11), { maxInputChars: 10 })).toBe('a'.repeat(10)); + }); +}); diff --git a/packages/api/src/files/speech.ts b/packages/api/src/files/speech.ts index 4b414be1e9..9b215ea2d0 100644 --- a/packages/api/src/files/speech.ts +++ b/packages/api/src/files/speech.ts @@ -14,6 +14,9 @@ export interface SpeechMessageCache { set(key: string, value: CachedSpeech, ttl?: number): unknown; } +/** Default cap on the text one text-to-speech request may synthesize. */ +export const DEFAULT_TTS_MAX_INPUT_CHARS = 20_000; + /** * Polls a message for the next text to stream as speech. Each call returns the chunks * finalized since the previous one, or an error string once the message stays missing or @@ -25,11 +28,14 @@ export function createSpeechChunkProcessor({ messageId, cache, getMessage, + maxInputChars = DEFAULT_TTS_MAX_INPUT_CHARS, }: { user: string; messageId?: string; cache: SpeechMessageCache; getMessage: (filter: { user: string; messageId: string }) => Promise; + /** Speech stops here: text past the cap is never sent to the provider. */ + maxInputChars?: number; }): () => Promise { if (!messageId) { throw new Error('Message ID is required'); @@ -57,10 +63,14 @@ export function createSpeechChunkProcessor({ return []; } - const text = typeof message === 'string' ? message : getSpeechText(message); - cache.set(cacheKey, { text, complete: true }, Time.FIVE_MINUTES); + const fullText = typeof message === 'string' ? message : getSpeechText(message); + cache.set(cacheKey, { text: fullText, complete: true }, Time.FIVE_MINUTES); + /** A message longer than the cap is read up to the cap, as if it ended there. */ + const capped = fullText.length >= maxInputChars; + const text = capped ? fullText.slice(0, maxInputChars) : fullText; const complete = - typeof message !== 'string' && (!('complete' in message) || message.complete !== false); + capped || + (typeof message !== 'string' && (!('complete' in message) || message.complete !== false)); if (text === processedText) { noChangeCount++; @@ -87,3 +97,17 @@ export function createSpeechChunkProcessor({ return chunks; }; } + +/** + * The part of `input` one text-to-speech request may synthesize: the text up to the cap, so + * one request cannot fan out into an unbounded number of paid provider calls. Longer text is + * read up to the cap, the same as automatic playback of a long message. + */ +export function capSpeechInput(input: unknown, ttsConfig?: { maxInputChars?: number }): string { + /** A request body can carry any JSON; only text is spoken, so the cap always counts characters. */ + if (typeof input !== 'string') { + return ''; + } + const maxInputChars = ttsConfig?.maxInputChars ?? DEFAULT_TTS_MAX_INPUT_CHARS; + return input.length > maxInputChars ? input.slice(0, maxInputChars) : input; +} diff --git a/packages/api/src/utils/index.ts b/packages/api/src/utils/index.ts index f54f2efe18..12f1db8286 100644 --- a/packages/api/src/utils/index.ts +++ b/packages/api/src/utils/index.ts @@ -1,4 +1,5 @@ export * from './axios'; +export * from './streams'; export * from './azure'; export * from './code'; export * from './common'; diff --git a/packages/api/src/utils/streams.spec.ts b/packages/api/src/utils/streams.spec.ts new file mode 100644 index 0000000000..836b4e3b1b --- /dev/null +++ b/packages/api/src/utils/streams.spec.ts @@ -0,0 +1,109 @@ +import { PassThrough, Readable } from 'node:stream'; +import { pipeSegment, pipeToResponse } from './streams'; + +function failingSource(): Readable { + return new Readable({ + read() { + this.push(Buffer.from('partial')); + this.destroy(new Error('socket reset')); + }, + }); +} + +describe('pipeToResponse', () => { + it('reports a mid-stream failure and tears the response down', async () => { + const res = new PassThrough(); + res.resume(); + const onError = jest.fn(); + + pipeToResponse(failingSource(), res, onError); + await new Promise((resolve) => res.on('close', resolve)); + + expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'socket reset' })); + expect(res.destroyed).toBe(true); + }); + + it('destroys the upstream when the client disconnects', async () => { + const source = new PassThrough(); + const res = new PassThrough(); + + pipeToResponse(source, res); + res.destroy(); + await new Promise((resolve) => source.on('close', resolve)); + + expect(source.destroyed).toBe(true); + }); +}); + +describe('pipeSegment', () => { + it('resolves when the segment ends and leaves the response open', async () => { + const res = new PassThrough(); + res.resume(); + + await pipeSegment(Readable.from([Buffer.from('audio')]), res, { end: false }); + + expect(res.writableEnded).toBe(false); + }); + + it('rejects when the segment fails', async () => { + const res = new PassThrough(); + res.resume(); + + await expect(pipeSegment(failingSource(), res, { end: false })).rejects.toThrow('socket reset'); + }); + + it('rejects and stops the upstream when the client disconnects', async () => { + const source = new PassThrough(); + const res = new PassThrough(); + const pending = pipeSegment(source, res, { end: false }); + + res.destroy(); + + await expect(pending).rejects.toThrow('Client disconnected'); + expect(source.destroyed).toBe(true); + }); + + it('rejects at once when the client left before the segment started', async () => { + const source = new PassThrough(); + const res = new PassThrough(); + res.destroy(); + + await expect(pipeSegment(source, res, { end: false })).rejects.toThrow('Client disconnected'); + expect(source.destroyed).toBe(true); + }); + + it('tears the response down when the segment fails mid-write', async () => { + const res = new PassThrough(); + res.resume(); + + await expect(pipeSegment(failingSource(), res, { end: false })).rejects.toThrow('socket reset'); + expect(res.destroyed).toBe(true); + }); + + it('rejects on a response-side error instead of leaving it unhandled', async () => { + const source = new PassThrough(); + const res = new PassThrough(); + const pending = pipeSegment(source, res, { end: false }); + + res.emit('error', new Error('write EPIPE')); + + await expect(pending).rejects.toThrow('write EPIPE'); + expect(source.destroyed).toBe(true); + }); + + it('keeps a response write error after a segment from going unhandled', async () => { + const res = new PassThrough(); + res.resume(); + await pipeSegment(Readable.from([Buffer.from('audio')]), res, { end: false }); + + expect(() => res.emit('error', new Error('write EPIPE'))).not.toThrow(); + }); + + it('leaves a response with no audio sent yet open for an HTTP error', async () => { + const res = Object.assign(new PassThrough(), { headersSent: false }); + res.resume(); + + await expect(pipeSegment(failingSource(), res, { end: false })).rejects.toThrow('socket reset'); + expect(res.destroyed).toBe(false); + }); +}); diff --git a/packages/api/src/utils/streams.ts b/packages/api/src/utils/streams.ts new file mode 100644 index 0000000000..59365cfb68 --- /dev/null +++ b/packages/api/src/utils/streams.ts @@ -0,0 +1,83 @@ +import { pipeline } from 'node:stream'; +import type { Readable, Writable } from 'node:stream'; + +/** + * Streams `source` into `res` and ends it. An upstream failure tears the response down instead + * of surfacing as an uncaught stream error, and a client disconnect destroys the upstream. + */ +export function pipeToResponse( + source: Readable, + res: Writable, + onError?: (error: Error) => void, +): void { + pipeline(source, res, (error) => { + if (error) { + onError?.(error); + } + }); +} + +const guardedResponses = new WeakSet(); + +/** + * A response that outlives one segment can still fail a buffered write after that segment's + * listeners are gone. A lasting listener keeps that from becoming an uncaught error; the + * failure still destroys the response, which the next segment sees as a closed client. + */ +function guardResponse(res: Writable): void { + if (guardedResponses.has(res)) { + return; + } + guardedResponses.add(res); + res.on('error', () => undefined); +} + +/** + * Writes one `source` segment into `res`, ending `res` only when `end` is set. Resolves when the + * segment ends; rejects, destroying `source`, when it fails or the client disconnects first. + */ +export function pipeSegment( + source: Readable, + res: Writable, + { end }: { end: boolean }, +): Promise { + guardResponse(res); + return new Promise((resolve, reject) => { + /** A client that left while the previous segment was being fetched already emitted close. */ + if (res.destroyed || res.writableEnded) { + source.destroy(); + reject(new Error('Client disconnected')); + return; + } + const cleanup = (): void => { + source.off('end', onEnd); + source.off('error', onError); + res.off('close', onClose); + res.off('error', onError); + }; + const onEnd = (): void => { + cleanup(); + resolve(); + }; + /** Either side failing ends both: a half-written response would otherwise hang the client. */ + const onError = (error: Error): void => { + cleanup(); + source.destroy(); + /** Before any audio went out the caller can still answer with an HTTP error. */ + if (!('headersSent' in res) || res.headersSent) { + res.destroy(); + } + reject(error); + }; + const onClose = (): void => { + cleanup(); + source.destroy(); + reject(new Error('Client disconnected')); + }; + source.on('end', onEnd); + source.on('error', onError); + res.on('error', onError); + res.on('close', onClose); + source.pipe(res, { end }); + }); +} diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts index 19d8fd0cc7..d549ca30fb 100644 --- a/packages/data-provider/src/config.ts +++ b/packages/data-provider/src/config.ts @@ -2385,6 +2385,8 @@ const ttsLocalaiSchema = z.object({ const ttsSchema = z.object({ allowedAddresses: allowedAddressesSchema, + /** Longest text one request may synthesize; each 1,000 characters is a separate paid call. */ + maxInputChars: z.number().int().positive().optional(), openai: ttsOpenaiSchema.optional(), azureOpenAI: ttsAzureOpenAISchema.optional(), elevenlabs: ttsElevenLabsSchema.optional(),