Skip to content

Commit 278e3ea

Browse files
committed
fix(realtime): wrap malformed transcription frames
1 parent f3fecb7 commit 278e3ea

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Docs: https://docs.openclaw.ai
5252
- CLI/export-trajectory: report malformed encoded request JSON with a stable CLI error instead of leaking raw parser output.
5353
- Twilio voice-call: report malformed successful API JSON responses with provider-owned errors instead of leaking raw parser failures.
5454
- Voice-call provider APIs: report malformed successful guarded JSON responses with provider-prefixed errors instead of leaking raw parser failures.
55+
- Realtime transcription: report malformed provider websocket JSON frames with owned parser errors instead of leaking raw `SyntaxError` objects.
5556
- Models config/auth: stop inferring provider env-var markers from broad `^[A-Z_][A-Z0-9_]*$` strings, and resolve config-backed provider `apiKey` values only through structured env SecretRefs (`secrets.providers[id]` / `secrets.defaults`), so unrelated env vars cannot accidentally become provider credentials. Thanks @sallyom.
5657
- Media fetch: skip allocating and buffering the response body for bodyless media responses (HEAD probes and 204-style empty bodies), avoiding wasted heap on streams that carry no payload. Thanks @shakkernerd.
5758
- CLI/onboarding: forward provider-specific auth flags (e.g. `--openai-api-key`) through the onboarding wizard so they reach provider auth methods via `ctx.opts`, letting `--openai-api-key "$OPENAI_API_KEY"` skip the redundant "use existing env var?" prompt in non-interactive harnesses. (#81669) Thanks @sjf.

src/realtime-transcription/websocket-session.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ afterEach(async () => {
1515
async function createRealtimeServer(params?: {
1616
closeOnConnection?: boolean;
1717
initialEvent?: unknown;
18+
initialText?: string;
1819
onUpgrade?: (headers: Record<string, string | string[] | undefined>) => void;
1920
onBinary?: (payload: Buffer) => void;
2021
onText?: (payload: unknown) => void;
@@ -35,6 +36,9 @@ async function createRealtimeServer(params?: {
3536
if (params?.initialEvent) {
3637
ws.send(JSON.stringify(params.initialEvent));
3738
}
39+
if (params?.initialText) {
40+
ws.send(params.initialText);
41+
}
3842
ws.on("message", (data, isBinary) => {
3943
const buffer = Buffer.isBuffer(data)
4044
? data
@@ -257,6 +261,32 @@ describe("createRealtimeTranscriptionWebSocketSession", () => {
257261
expect(setupError.message).toBe("nope");
258262
});
259263

264+
it("reports malformed websocket JSON with an owned parser error", async () => {
265+
const server = await createRealtimeServer({ initialText: "{not json" });
266+
const onError = vi.fn();
267+
const session = createRealtimeTranscriptionWebSocketSession({
268+
providerId: "test",
269+
callbacks: { onError },
270+
url: server.url,
271+
readyOnOpen: true,
272+
onMessage: () => {
273+
throw new Error("malformed payload should not reach provider handler");
274+
},
275+
sendAudio: (audio, transport) => {
276+
transport.sendBinary(audio);
277+
},
278+
});
279+
280+
await session.connect();
281+
await vi.waitFor(() => {
282+
expect(onError).toHaveBeenCalledTimes(1);
283+
});
284+
const parseError = requireFirstMockArg(onError, "malformed websocket json error");
285+
expect(parseError).toBeInstanceOf(Error);
286+
expect(parseError.message).toBe("Realtime transcription websocket received malformed JSON.");
287+
session.close();
288+
});
289+
260290
it("reports pre-ready closes separately from connection timeouts", async () => {
261291
const server = await createRealtimeServer({ closeOnConnection: true });
262292
const onError = vi.fn();

src/realtime-transcription/websocket-session.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ function rawWsDataToBuffer(data: RawData): Buffer {
5858
}
5959

6060
function defaultParseMessage(payload: Buffer): unknown {
61-
return JSON.parse(payload.toString());
61+
try {
62+
return JSON.parse(payload.toString()) as unknown;
63+
} catch {
64+
throw new Error("Realtime transcription websocket received malformed JSON.");
65+
}
6266
}
6367

6468
class WebSocketRealtimeTranscriptionSession<Event> implements RealtimeTranscriptionSession {

0 commit comments

Comments
 (0)