Skip to content

Commit 168397b

Browse files
committed
fix(google): consume interrupted voice turn completion
1 parent 279a12c commit 168397b

2 files changed

Lines changed: 68 additions & 32 deletions

File tree

‎ui/src/pages/chat/realtime-talk-google-live-transcripts.test.ts‎

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,65 @@ describe("Google Live browser transcript finality", () => {
6565
transport.stop();
6666
});
6767

68-
it("finalizes interrupted output on its original Talk turn without merging the next response", async () => {
69-
const onTranscript = vi.fn();
70-
const onTalkEvent = vi.fn();
71-
const transport = createTransport({ onTranscript, onTalkEvent });
72-
const ws = await startTransport(transport);
73-
onTalkEvent.mockClear();
74-
for (const serverContent of [
75-
{ outputTranscription: { text: "Interrupted " } },
76-
{ outputTranscription: { text: "response" } },
77-
{ interrupted: true },
78-
{ outputTranscription: { text: "Next response", finished: true } },
79-
{ turnComplete: true },
80-
]) {
81-
ws.emitMessage(encodeJsonFrame({ serverContent }));
82-
await flushMicrotasks();
83-
}
84-
expect(
85-
onTranscript.mock.calls.filter(([entry]) => entry.final).map(([entry]) => entry.text),
86-
).toEqual(["Interrupted response", "Next response"]);
87-
expect(onTalkEvent.mock.calls.map(([event]) => [event.type, event.turnId])).toEqual([
88-
["output.text.delta", "turn-1"],
89-
["output.text.delta", "turn-1"],
90-
["output.text.done", "turn-1"],
91-
["turn.cancelled", "turn-1"],
92-
["output.text.delta", "turn-2"],
93-
["output.text.done", "turn-2"],
94-
["turn.ended", "turn-2"],
95-
]);
96-
transport.stop();
97-
expect(onTranscript.mock.calls.filter(([entry]) => entry.final)).toHaveLength(2);
98-
});
68+
it.each([
69+
{
70+
name: "before the next input transcript",
71+
boundary: [
72+
{ interrupted: true },
73+
{ turnComplete: true },
74+
{ inputTranscription: { text: "New question" } },
75+
],
76+
},
77+
{
78+
name: "after an independently ordered input transcript",
79+
boundary: [
80+
{ interrupted: true },
81+
{ inputTranscription: { text: "New question" } },
82+
{ turnComplete: true },
83+
],
84+
},
85+
{
86+
name: "in the interruption frame",
87+
boundary: [
88+
{ interrupted: true, turnComplete: true },
89+
{ inputTranscription: { text: "New question" } },
90+
],
91+
},
92+
])(
93+
"does not end another Talk turn when interrupted completion arrives $name",
94+
async ({ boundary }) => {
95+
const onTranscript = vi.fn();
96+
const onTalkEvent = vi.fn();
97+
const transport = createTransport({ onTranscript, onTalkEvent });
98+
const ws = await startTransport(transport);
99+
onTalkEvent.mockClear();
100+
for (const serverContent of [
101+
{ outputTranscription: { text: "Interrupted " } },
102+
{ outputTranscription: { text: "response" } },
103+
...boundary,
104+
{ outputTranscription: { text: "Next response", finished: true } },
105+
{ turnComplete: true },
106+
]) {
107+
ws.emitMessage(encodeJsonFrame({ serverContent }));
108+
await flushMicrotasks();
109+
}
110+
expect(
111+
onTranscript.mock.calls.filter(([entry]) => entry.final).map(([entry]) => entry.text),
112+
).toEqual(["Interrupted response", "New question", "Next response"]);
113+
expect(onTalkEvent.mock.calls.map(([event]) => [event.type, event.turnId])).toEqual([
114+
["output.text.delta", "turn-1"],
115+
["output.text.delta", "turn-1"],
116+
["output.text.done", "turn-1"],
117+
["turn.cancelled", "turn-1"],
118+
["transcript.done", "turn-2"],
119+
["output.text.delta", "turn-2"],
120+
["output.text.done", "turn-2"],
121+
["turn.ended", "turn-2"],
122+
]);
123+
transport.stop();
124+
expect(onTranscript.mock.calls.filter(([entry]) => entry.final)).toHaveLength(3);
125+
},
126+
);
99127

100128
it("releases the UTF-8 transcript budget on finality and closes on overflow without persisting a partial", async () => {
101129
const onTranscript = vi.fn();

‎ui/src/pages/chat/realtime-talk-google-live.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export class GoogleLiveRealtimeTalkTransport implements RealtimeTalkTransport {
9292
private inputMeter: RealtimeTalkMediaStreamMeter | null = null;
9393
private readonly inputPump = new RealtimeTalkPcmInputPump();
9494
private closed = false;
95+
private interruptedTurn = false;
9596
private readonly camera: RealtimeTalkCameraController;
9697
private readonly lifecycle = new GoogleLiveConnectionLifecycle();
9798
private cameraPublished = false;
@@ -270,6 +271,7 @@ export class GoogleLiveRealtimeTalkTransport implements RealtimeTalkTransport {
270271

271272
private releaseResources(): void {
272273
this.clearSetupTimeout();
274+
this.interruptedTurn = false;
273275
this.pendingTranscripts.user = { text: "", byteCount: 0 };
274276
this.pendingTranscripts.assistant = { text: "", byteCount: 0 };
275277
const inputMeter = this.inputMeter;
@@ -377,6 +379,7 @@ export class GoogleLiveRealtimeTalkTransport implements RealtimeTalkTransport {
377379
}
378380
const content = message.serverContent;
379381
if (content?.interrupted) {
382+
this.interruptedTurn = true;
380383
this.stopOutput();
381384
}
382385
if (content?.inputTranscription && !this.appendTranscript("user", content.inputTranscription)) {
@@ -421,9 +424,14 @@ export class GoogleLiveRealtimeTalkTransport implements RealtimeTalkTransport {
421424
final: true,
422425
payload: { reason: "provider-interrupted" },
423426
});
424-
} else if (content?.turnComplete) {
427+
} else if (content?.turnComplete && !this.interruptedTurn) {
425428
this.emitTalkEvent({ type: "turn.ended", final: true });
426429
}
430+
// Google completes interrupted turns separately; input transcription can
431+
// arrive in between and must not have its new Talk turn closed by that frame.
432+
if (content?.turnComplete) {
433+
this.interruptedTurn = false;
434+
}
427435
if (this.closed) {
428436
return;
429437
}

0 commit comments

Comments
 (0)