Skip to content

Commit 065d390

Browse files
committed
Merge pull request #117179 from openclaw/fix/google-live-transcript-bound
fix(google): stop runaway realtime transcript growth
2 parents 865a728 + 5ab2471 commit 065d390

2 files changed

Lines changed: 116 additions & 12 deletions

File tree

‎extensions/google/realtime-voice-provider.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,85 @@ describe("buildGoogleRealtimeVoiceProvider", () => {
13441344
]);
13451345
});
13461346

1347+
it("allows each role's UTF-8 transcript limit and releases it on finished", async () => {
1348+
const provider = buildGoogleRealtimeVoiceProvider();
1349+
const onTranscript = vi.fn();
1350+
const bridge = provider.createBridge({
1351+
providerConfig: { apiKey: "gemini-key" },
1352+
onAudio: vi.fn(),
1353+
onClearAudio: vi.fn(),
1354+
onTranscript,
1355+
});
1356+
1357+
await bridge.connect();
1358+
const onmessage = lastConnectParams().callbacks.onmessage;
1359+
const halfLimit = "é".repeat(64 * 1024);
1360+
onmessage({
1361+
serverContent: {
1362+
inputTranscription: { text: halfLimit },
1363+
outputTranscription: { text: halfLimit },
1364+
},
1365+
});
1366+
onmessage({
1367+
serverContent: {
1368+
inputTranscription: { text: halfLimit },
1369+
outputTranscription: { text: halfLimit },
1370+
},
1371+
});
1372+
onmessage({ serverContent: { outputTranscription: { finished: true } } });
1373+
onmessage({ serverContent: { inputTranscription: { finished: true } } });
1374+
1375+
expect(onTranscript.mock.calls.filter((call) => call[2] === true)).toEqual([
1376+
["assistant", `${halfLimit}${halfLimit}`, true],
1377+
["user", `${halfLimit}${halfLimit}`, true],
1378+
]);
1379+
expect(session.close).not.toHaveBeenCalled();
1380+
});
1381+
1382+
it("terminates and clears a runaway transcript stream at the UTF-8 byte limit", async () => {
1383+
const provider = buildGoogleRealtimeVoiceProvider();
1384+
const onError = vi.fn();
1385+
const onClose = vi.fn();
1386+
const onTranscript = vi.fn();
1387+
const bridge = provider.createBridge({
1388+
providerConfig: { apiKey: "gemini-key" },
1389+
onAudio: vi.fn(),
1390+
onClearAudio: vi.fn(),
1391+
onError,
1392+
onClose,
1393+
onTranscript,
1394+
});
1395+
1396+
await bridge.connect();
1397+
const callbacks = lastConnectParams().callbacks;
1398+
const transcriptChunk = "€".repeat(16);
1399+
const acceptedChunks = Math.floor((256 * 1024) / Buffer.byteLength(transcriptChunk, "utf8"));
1400+
for (let index = 0; index < 10_000; index += 1) {
1401+
callbacks.onmessage({
1402+
serverContent: {
1403+
inputTranscription: { text: transcriptChunk },
1404+
},
1405+
});
1406+
}
1407+
callbacks.onclose({ code: 1000, reason: "late clean close", wasClean: true });
1408+
1409+
expect(onTranscript).toHaveBeenCalledTimes(acceptedChunks);
1410+
expect(onTranscript.mock.calls.at(-1)).toEqual(["user", transcriptChunk, false]);
1411+
expect(onError).toHaveBeenCalledTimes(1);
1412+
expect(onError).toHaveBeenCalledWith(
1413+
expect.objectContaining({
1414+
message: "Google Live transcript exceeded the 256 KiB UTF-8 pending buffer limit",
1415+
}),
1416+
);
1417+
expect(onTranscript.mock.calls.filter((call) => call[2] === true)).toEqual([]);
1418+
expect(onClose).toHaveBeenCalledTimes(1);
1419+
expect(onClose).toHaveBeenCalledWith("error");
1420+
expect(session.close).toHaveBeenCalledTimes(1);
1421+
await expect(bridge.connect()).rejects.toThrow(
1422+
"Google Live transcript exceeded the 256 KiB UTF-8 pending buffer limit",
1423+
);
1424+
});
1425+
13471426
it("retains unordered transcript chunks until a protocol terminal or close", async () => {
13481427
const provider = buildGoogleRealtimeVoiceProvider();
13491428
const onTranscript = vi.fn();

‎extensions/google/realtime-voice-provider.ts‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ const GOOGLE_REALTIME_BROWSER_NEW_SESSION_TTL_MS = 60 * 1000;
6868
const GOOGLE_REALTIME_RECONNECT_MAX_ATTEMPTS = 3;
6969
const GOOGLE_REALTIME_RECONNECT_BASE_DELAY_MS = 250;
7070
const GOOGLE_REALTIME_RECONNECT_MAX_DELAY_MS = 2_000;
71+
const GOOGLE_REALTIME_MAX_PENDING_TRANSCRIPT_BYTES = 256 * 1024;
72+
const GOOGLE_REALTIME_TRANSCRIPT_OVERFLOW_MESSAGE =
73+
"Google Live transcript exceeded the 256 KiB UTF-8 pending buffer limit";
7174
// Google Live requires a leading letter/underscore and caps function names at 128 characters.
7275
const GOOGLE_REALTIME_TOOL_NAME_RE = /^[A-Za-z_][A-Za-z0-9_.:-]{0,127}$/;
7376
const MULAW_LINEAR_SAMPLES = new Int16Array(256);
@@ -143,6 +146,10 @@ type GoogleRealtimeLiveConfig = {
143146

144147
type GoogleRealtimeVoiceBridgeConfig = RealtimeVoiceBridgeCreateRequest & GoogleRealtimeLiveConfig;
145148
type GoogleLiveTranscription = NonNullable<LiveServerContent["inputTranscription"]>;
149+
type GoogleLiveTranscriptAccumulator = {
150+
text: string;
151+
byteCount: number;
152+
};
146153

147154
function trimToUndefined(value: unknown): string | undefined {
148155
return normalizeOptionalString(value);
@@ -478,10 +485,13 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
478485
private closeNotified = false;
479486
private connectionOwner: GoogleLiveConnectionAttempt | undefined;
480487
private connectAttempt: GoogleLiveConnectionAttempt | undefined;
481-
private readonly pendingTranscripts: Record<RealtimeVoiceRole, string> = {
482-
user: "",
483-
assistant: "",
484-
};
488+
// Google can interleave independent input/output transcripts, so each role
489+
// owns its own in-progress byte budget until `finished` or terminal cleanup.
490+
private readonly pendingTranscripts: Record<RealtimeVoiceRole, GoogleLiveTranscriptAccumulator> =
491+
{
492+
user: { text: "", byteCount: 0 },
493+
assistant: { text: "", byteCount: 0 },
494+
};
485495

486496
constructor(private readonly config: GoogleRealtimeVoiceBridgeConfig) {
487497
this.audioFormat = config.audioFormat ?? REALTIME_VOICE_AUDIO_FORMAT_G711_ULAW_8KHZ;
@@ -858,13 +868,17 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
858868
}
859869

860870
if (content.inputTranscription) {
861-
this.appendTranscript("user", content.inputTranscription);
871+
if (!this.appendTranscript("user", content.inputTranscription)) {
872+
return;
873+
}
862874
}
863875

864876
if (content.outputTranscription) {
865877
// outputAudioTranscription is requested in the session config. Keep that
866878
// official stream canonical; modelTurn text has no transcript turn identity.
867-
this.appendTranscript("assistant", content.outputTranscription);
879+
if (!this.appendTranscript("assistant", content.outputTranscription)) {
880+
return;
881+
}
868882
}
869883

870884
for (const part of content.modelTurn?.parts ?? []) {
@@ -886,22 +900,33 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
886900
}
887901
}
888902

889-
private appendTranscript(role: RealtimeVoiceRole, transcript: GoogleLiveTranscription): void {
903+
private appendTranscript(role: RealtimeVoiceRole, transcript: GoogleLiveTranscription): boolean {
890904
const text = transcript.text;
891905
if (text) {
892-
this.pendingTranscripts[role] += text;
906+
const pending = this.pendingTranscripts[role];
907+
const textBytes = Buffer.byteLength(text, "utf8");
908+
if (pending.byteCount + textBytes > GOOGLE_REALTIME_MAX_PENDING_TRANSCRIPT_BYTES) {
909+
this.resetPendingTranscripts();
910+
this.failConnection(new Error(GOOGLE_REALTIME_TRANSCRIPT_OVERFLOW_MESSAGE));
911+
return false;
912+
}
913+
pending.text += text;
914+
pending.byteCount += textBytes;
893915
this.emitTranscript(role, text, false);
894916
}
895917
// turnComplete belongs to model generation and is unordered with transcription.
896918
// Finalize only on the protocol terminal or when the bridge permanently closes.
897919
if (transcript.finished) {
898920
this.flushPendingTranscript(role);
899921
}
922+
return true;
900923
}
901924

902925
private flushPendingTranscript(role: RealtimeVoiceRole): void {
903-
const completeText = this.pendingTranscripts[role].trim();
904-
this.pendingTranscripts[role] = "";
926+
const pending = this.pendingTranscripts[role];
927+
const completeText = pending.text.trim();
928+
pending.text = "";
929+
pending.byteCount = 0;
905930
if (completeText) {
906931
this.emitTranscript(role, completeText, true);
907932
}
@@ -927,8 +952,8 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
927952
}
928953

929954
private resetPendingTranscripts(): void {
930-
this.pendingTranscripts.user = "";
931-
this.pendingTranscripts.assistant = "";
955+
this.pendingTranscripts.user = { text: "", byteCount: 0 };
956+
this.pendingTranscripts.assistant = { text: "", byteCount: 0 };
932957
}
933958

934959
private failConnection(error: Error): void {

0 commit comments

Comments
 (0)