diff --git a/src/client/http.ts b/src/client/http.ts index 7e327d6..c77ba79 100644 --- a/src/client/http.ts +++ b/src/client/http.ts @@ -62,7 +62,7 @@ export async function request(config: Config, opts: RequestOpts): Promise { + process.stdout.on('error', (err: NodeJS.ErrnoException) => { + if (err.code === 'EPIPE') process.exit(0); + throw err; + }); + + for await (const event of parseSSE(response)) { + if (!event.data || event.data === '[DONE]') break; + + let parsed: SseAudioPayload; + try { parsed = JSON.parse(event.data); } catch { continue; } + + if (parsed.data?.status === 2) continue; + + const hex = parsed.data?.audio; + if (!hex) continue; + + const chunk = Buffer.from(hex, 'hex'); + if (!process.stdout.write(chunk)) { + await new Promise(r => process.stdout.once('drain', r)); + } + } +}