perf(bridge): stream TTS per sentence to cut voice reply latency

The /converse turn synthesised the entire reply before any audio played, so
time-to-first-audio grew with reply length. Add a streaming /converse_stream
endpoint that emits the transcript/reply first, then one audio clip per
sentence as each finishes synthesising. The Discord voice layer enqueues each
clip on arrival via the existing FIFO playQueue, so the first sentence starts
speaking while the rest are still being synthesised.

STT and the reply engine still run to completion before the first clip; only
TTS is pipelined. The non-streaming /converse and /text endpoints are
unchanged.

- bridge: language-agnostic sentence splitter (bridge/text_utils.py) + NDJSON
  streaming route
- bot: ndjson() reader + converseStream() client; voice.ts plays clips
  progressively
- tests: splitter unit tests + bot ndjson/converseStream tests

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-06-13 01:09:39 +09:00
parent 989a4f3e98
commit 5c295420ea
6 changed files with 363 additions and 18 deletions

View File

@@ -38,6 +38,69 @@ export async function converse(wav: Buffer, broadcasting?: boolean): Promise<Con
return (await res.json()) as ConverseResult;
}
/** Metadata for a streamed turn: everything except the audio clips. */
export interface ConverseMeta {
transcript: string;
language?: string | null;
reply: string;
error?: string | null;
broadcast_action?: BroadcastAction | null;
}
export interface ConverseStreamHandlers {
/** Fired once, before any audio, with the transcript/reply/broadcast directive. */
onMeta?: (meta: ConverseMeta) => void | Promise<void>;
/** Fired per sentence as its audio finishes synthesising (in order). */
onAudio?: (wav: Buffer) => void | Promise<void>;
}
/** Parse a byte stream of newline-delimited JSON into objects, one per line. */
export async function* ndjson(
stream: AsyncIterable<Uint8Array>,
): AsyncGenerator<any> {
const decoder = new TextDecoder();
let buf = "";
for await (const chunk of stream) {
buf += decoder.decode(chunk, { stream: true });
let nl: number;
while ((nl = buf.indexOf("\n")) >= 0) {
const line = buf.slice(0, nl).trim();
buf = buf.slice(nl + 1);
if (line) yield JSON.parse(line);
}
}
const last = buf.trim();
if (last) yield JSON.parse(last);
}
/** Streaming voice turn: the bridge emits the transcript/reply first, then one
* audio clip per sentence as it is synthesised. Handlers run in arrival order,
* so playing each clip on arrival starts the first sentence while the rest are
* still being spoken. Mirrors {@link converse} but pipelines TTS. */
export async function converseStream(
wav: Buffer,
broadcasting: boolean | undefined,
handlers: ConverseStreamHandlers,
): Promise<void> {
const qs = broadcasting === undefined ? "" : `?broadcasting=${broadcasting ? "1" : "0"}`;
const res = await fetch(`${config.bridgeUrl}/converse_stream${qs}`, {
method: "POST",
headers: { "content-type": "audio/wav" },
body: wav,
});
if (!res.ok || !res.body) {
throw new Error(`bridge /converse_stream ${res.status}: ${await res.text().catch(() => "")}`);
}
for await (const ev of ndjson(res.body as AsyncIterable<Uint8Array>)) {
if (ev.type === "meta") {
await handlers.onMeta?.(ev as ConverseMeta);
} else if (ev.type === "audio" && ev.audio_b64) {
const clip = decodeWav(ev.audio_b64);
if (clip) await handlers.onAudio?.(clip);
}
}
}
/** Text-only turn (used by /자비스 ask). */
export async function ask(text: string): Promise<TextResult> {
const res = await fetch(`${config.bridgeUrl}/text`, {