Skip to content

Commit 31e03bc

Browse files
committed
merge: bound WebRTC Talk tool lifecycle (#117732)
* commit '1ffcfdd168c05d33e764ac111cf9f49be1f7fa31': test(talk): align browser E2E tool completion docs(changelog): note bounded WebRTC tool calls test(talk): split WebRTC tool lifecycle coverage test(talk): align WebRTC tool-call fixtures fix(talk): accept optional realtime item ids fix(talk): narrow completed response payload test(talk): cover WebRTC terminal tool calls fix(talk): bound WebRTC tool-call lifecycle
2 parents c879606 + 1ffcfdd commit 31e03bc

6 files changed

Lines changed: 415 additions & 98 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Docs: https://docs.openclaw.ai
7474
- **Control UI update reconciliation:** preserve an unresolved managed-update request across disconnects, accept the replacement Gateway version when it proves success, and otherwise show explicit recovery guidance instead of trusting an unrelated cached update result or failing silently. Fixes #116075. Thanks @shakkernerd.
7575
- **Control UI model readiness:** put AI setup first when no model is selectable, distinguish signed-in credentials from ready providers, and route accounts with no exposed models directly to provider recovery instead of leading with disabled default controls.
7676
- **Control UI Talk session isolation:** stop active realtime Talk media and retire its callbacks before chat session changes, Gateway disconnects, or pane disposal so previous-session audio, transcript, camera, and status updates cannot leak into the next view. Thanks @shakkernerd.
77+
- **Control UI Realtime tool calls:** execute OpenAI WebRTC tools only from completed responses, bound retained call identities and UTF-8 arguments, and ignore provisional or late duplicate events so long Talk sessions cannot grow tool state without limit.
7778
- **Gateway reconnect event ordering:** reset the shared TypeScript client's outer event-sequence baseline for each replacement WebSocket, preventing gap recovery from comparing unrelated connection generations across Control UI, TUI, SDK, and browser extension clients. Thanks @shakkernerd.
7879
- **Skill Workshop offline apply:** preserve configless local proposal apply after upgrades under exclusive Gateway startup ownership, while keeping running Gateway snapshot invalidation fail-closed when CLI credentials are unavailable.
7980
- **macOS and Control UI keyboard navigation:** let Tab traverse links and controls inside embedded Dashboard, browser, and Canvas web views, and keep shortcuts working on non-Latin keyboard layouts without firing during IME composition.

‎ui/src/e2e/browser-talk-start-stop.e2e.test.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,21 @@ describeControlUiE2e("Control UI browser Talk", () => {
509509
channel?.dispatchEvent(
510510
new MessageEvent("message", {
511511
data: JSON.stringify({
512-
type: "response.function_call_arguments.done",
513-
item_id: "item-camera",
514-
call_id: "call-camera",
515-
name: "describe_view",
516-
arguments: "{}",
512+
type: "response.done",
513+
response: {
514+
id: "response-camera",
515+
status: "completed",
516+
output: [
517+
{
518+
type: "function_call",
519+
id: "item-camera",
520+
status: "completed",
521+
call_id: "call-camera",
522+
name: "describe_view",
523+
arguments: "{}",
524+
},
525+
],
526+
},
517527
}),
518528
}),
519529
);

‎ui/src/pages/chat/realtime-talk-webrtc-control.test.ts‎

Lines changed: 268 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// @vitest-environment jsdom
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3+
import { REALTIME_VOICE_DESCRIBE_VIEW_TOOL_NAME } from "../../../../src/talk/describe-view-tool.js";
34
import { waitForFast } from "../../test-helpers/wait-for.ts";
4-
import { REALTIME_VOICE_AGENT_CONTROL_TOOL_NAME } from "./realtime-talk-shared.ts";
5+
import {
6+
REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME,
7+
REALTIME_VOICE_AGENT_CONTROL_TOOL_NAME,
8+
} from "./realtime-talk-shared.ts";
59
import { WebRtcSdpRealtimeTalkTransport } from "./realtime-talk-webrtc.ts";
610

711
class FakeDataChannel extends EventTarget {
@@ -69,19 +73,57 @@ function dispatchControlToolCall(
6973
peer: FakePeerConnection | undefined,
7074
args: { text: string; mode: "status" | "steer" },
7175
): void {
76+
dispatchCompletedToolCall(peer, {
77+
name: REALTIME_VOICE_AGENT_CONTROL_TOOL_NAME,
78+
arguments: JSON.stringify(args),
79+
});
80+
}
81+
82+
function dispatchCompletedToolCall(
83+
peer: FakePeerConnection | undefined,
84+
overrides: {
85+
responseId?: string | null;
86+
responseStatus?: string | null;
87+
itemId?: string | null;
88+
itemStatus?: string | null;
89+
callId?: string | null;
90+
name?: string | null;
91+
arguments?: string | null;
92+
} = {},
93+
): void {
94+
const field = (value: string | null | undefined, fallback: string): string | undefined =>
95+
value === undefined ? fallback : (value ?? undefined);
7296
peer?.channel.dispatchEvent(
7397
new MessageEvent("message", {
7498
data: JSON.stringify({
75-
type: "response.function_call_arguments.done",
76-
item_id: "item-control",
77-
call_id: "call-control",
78-
name: REALTIME_VOICE_AGENT_CONTROL_TOOL_NAME,
79-
arguments: JSON.stringify(args),
99+
type: "response.done",
100+
response: {
101+
id: field(overrides.responseId, "response-1"),
102+
status: field(overrides.responseStatus, "completed"),
103+
output: [
104+
{
105+
type: "function_call",
106+
id: field(overrides.itemId, "item-control"),
107+
status: field(overrides.itemStatus, "completed"),
108+
call_id: field(overrides.callId, "call-control"),
109+
name: field(overrides.name, REALTIME_VOICE_AGENT_CONTROL_TOOL_NAME),
110+
arguments: field(overrides.arguments, JSON.stringify({ text: "status" })),
111+
},
112+
],
113+
},
80114
}),
81115
}),
82116
);
83117
}
84118

119+
function sentRealtimeEvents(peer: FakePeerConnection | undefined): Array<Record<string, unknown>> {
120+
return (
121+
peer?.channel.send.mock.calls.map(
122+
([payload]) => JSON.parse(String(payload)) as Record<string, unknown>,
123+
) ?? []
124+
);
125+
}
126+
85127
describe("WebRtcSdpRealtimeTalkTransport control tool", () => {
86128
beforeEach(() => {
87129
FakePeerConnection.instances = [];
@@ -153,6 +195,226 @@ describe("WebRtcSdpRealtimeTalkTransport control tool", () => {
153195
transport.stop();
154196
});
155197

198+
it("executes completed calls once and ignores provisional events", async () => {
199+
const request = vi.fn(async (method: string) => {
200+
if (method === "talk.client.toolCall") {
201+
return { runId: "run-1" };
202+
}
203+
throw new Error(`unexpected request: ${method}`);
204+
});
205+
const transport = createOpenAiTransport({
206+
addEventListener: vi.fn(() => () => undefined),
207+
request,
208+
});
209+
210+
await transport.start();
211+
const peer = FakePeerConnection.instances[0];
212+
for (const type of [
213+
"response.function_call_arguments.delta",
214+
"response.function_call_arguments.done",
215+
]) {
216+
peer?.channel.dispatchEvent(
217+
new MessageEvent("message", {
218+
data: JSON.stringify({
219+
type,
220+
item_id: "item-1",
221+
call_id: "call-1",
222+
name: REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME,
223+
arguments: JSON.stringify({ question: "provisional" }),
224+
delta: JSON.stringify({ question: "provisional" }),
225+
}),
226+
}),
227+
);
228+
}
229+
expect(request).not.toHaveBeenCalled();
230+
231+
dispatchCompletedToolCall(peer, {
232+
itemId: "item-1",
233+
callId: "call-1",
234+
name: REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME,
235+
arguments: JSON.stringify({ question: "status?" }),
236+
});
237+
await waitForFast(() =>
238+
expect(request).toHaveBeenCalledWith("talk.client.toolCall", {
239+
sessionKey: "main",
240+
callId: "call-1",
241+
name: REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME,
242+
args: { question: "status?" },
243+
}),
244+
);
245+
246+
dispatchCompletedToolCall(peer, {
247+
responseId: "response-2",
248+
itemId: "item-2",
249+
callId: "call-1",
250+
name: REALTIME_VOICE_AGENT_CONSULT_TOOL_NAME,
251+
arguments: JSON.stringify({ question: "late" }),
252+
});
253+
await new Promise<void>((resolve) => {
254+
setTimeout(resolve, 0);
255+
});
256+
257+
expect(request).toHaveBeenCalledTimes(1);
258+
transport.stop();
259+
});
260+
261+
it.each([
262+
{ label: "cancelled response", responseStatus: "cancelled", itemStatus: "completed" },
263+
{ label: "failed response", responseStatus: "failed", itemStatus: "completed" },
264+
{ label: "incomplete response", responseStatus: "incomplete", itemStatus: "completed" },
265+
{ label: "incomplete item", responseStatus: "completed", itemStatus: "incomplete" },
266+
])("ignores function calls from a $label", async ({ responseStatus, itemStatus }) => {
267+
const request = vi.fn();
268+
const transport = createOpenAiTransport({ request });
269+
270+
await transport.start();
271+
dispatchCompletedToolCall(FakePeerConnection.instances[0], {
272+
responseStatus,
273+
itemStatus,
274+
});
275+
await new Promise<void>((resolve) => {
276+
setTimeout(resolve, 0);
277+
});
278+
279+
expect(request).not.toHaveBeenCalled();
280+
transport.stop();
281+
});
282+
283+
it("accepts completed calls without optional response and item ids", async () => {
284+
const request = vi.fn(async (method: string) => {
285+
if (method === "talk.client.steer") {
286+
return { ok: true, mode: "status" };
287+
}
288+
throw new Error(`unexpected request: ${method}`);
289+
});
290+
const transport = createOpenAiTransport({ request });
291+
292+
await transport.start();
293+
dispatchCompletedToolCall(FakePeerConnection.instances[0], {
294+
responseId: null,
295+
itemId: null,
296+
});
297+
await waitForFast(() =>
298+
expect(request).toHaveBeenCalledWith("talk.client.steer", {
299+
sessionKey: "main",
300+
text: "status",
301+
mode: "status",
302+
}),
303+
);
304+
305+
transport.stop();
306+
});
307+
308+
it("requires call, name, and arguments before executing tools", async () => {
309+
const request = vi.fn();
310+
const transport = createOpenAiTransport({ request });
311+
312+
await transport.start();
313+
const peer = FakePeerConnection.instances[0];
314+
for (const overrides of [
315+
{ callId: null, itemId: "missing-call" },
316+
{ name: null, callId: "missing-name", itemId: "missing-name" },
317+
{ arguments: null, callId: "missing-args", itemId: "missing-args" },
318+
]) {
319+
dispatchCompletedToolCall(peer, overrides);
320+
}
321+
await new Promise<void>((resolve) => {
322+
setTimeout(resolve, 0);
323+
});
324+
325+
expect(request).not.toHaveBeenCalled();
326+
transport.stop();
327+
});
328+
329+
it("enforces the authoritative 256000-byte UTF-8 argument limit", async () => {
330+
const request = vi.fn(async (method: string) => {
331+
if (method === "talk.client.steer") {
332+
return { ok: true, mode: "status" };
333+
}
334+
throw new Error(`unexpected request: ${method}`);
335+
});
336+
const onTalkEvent = vi.fn();
337+
const transport = createOpenAiTransport({ request }, { onTalkEvent });
338+
const baseArgs = JSON.stringify({ text: "status" });
339+
const argumentsAtLimit = baseArgs + " ".repeat(256_000 - baseArgs.length);
340+
const oversizedArguments = JSON.stringify({ text: "é".repeat(128_000) });
341+
342+
await transport.start();
343+
const peer = FakePeerConnection.instances[0];
344+
dispatchCompletedToolCall(peer, { arguments: argumentsAtLimit });
345+
await waitForFast(() => expect(request).toHaveBeenCalledOnce());
346+
dispatchCompletedToolCall(peer, {
347+
responseId: "response-2",
348+
itemId: "item-2",
349+
callId: "call-2",
350+
arguments: oversizedArguments,
351+
});
352+
dispatchCompletedToolCall(peer, {
353+
responseId: "response-3",
354+
itemId: "item-3",
355+
callId: "call-2",
356+
arguments: oversizedArguments,
357+
});
358+
359+
expect(new TextEncoder().encode(argumentsAtLimit)).toHaveLength(256_000);
360+
expect(new TextEncoder().encode(oversizedArguments).byteLength).toBeGreaterThan(256_000);
361+
const outputs = sentRealtimeEvents(peer).filter(
362+
(event) =>
363+
event.type === "conversation.item.create" &&
364+
(event.item as { type?: string } | undefined)?.type === "function_call_output",
365+
);
366+
expect(outputs).toHaveLength(2);
367+
expect(
368+
JSON.parse(String((outputs[1]?.item as { output?: string } | undefined)?.output)),
369+
).toEqual({
370+
error: "Realtime tool arguments exceed the 256000-byte UTF-8 limit",
371+
});
372+
expect(onTalkEvent).toHaveBeenCalledWith(
373+
expect.objectContaining({
374+
type: "tool.error",
375+
callId: "call-2",
376+
itemId: "item-2",
377+
final: true,
378+
}),
379+
);
380+
transport.stop();
381+
});
382+
383+
it("ends the session instead of evicting completed call identities", async () => {
384+
const onStatus = vi.fn();
385+
const transport = createOpenAiTransport({}, { onStatus });
386+
387+
await transport.start();
388+
const peer = FakePeerConnection.instances[0];
389+
for (let index = 0; index < 1_024; index += 1) {
390+
dispatchCompletedToolCall(peer, {
391+
responseId: `response-${index}`,
392+
itemId: `item-${index}`,
393+
callId: `call-${index}`,
394+
name: REALTIME_VOICE_DESCRIBE_VIEW_TOOL_NAME,
395+
arguments: "{}",
396+
});
397+
}
398+
dispatchCompletedToolCall(peer, {
399+
responseId: "response-overflow",
400+
itemId: "item-overflow",
401+
callId: "call-overflow",
402+
name: REALTIME_VOICE_DESCRIBE_VIEW_TOOL_NAME,
403+
arguments: "{}",
404+
});
405+
406+
expect(onStatus).toHaveBeenCalledWith("error", "Realtime tool-call session limit exceeded");
407+
expect(peer?.channel.close).toHaveBeenCalledOnce();
408+
dispatchCompletedToolCall(peer, {
409+
responseId: "response-late",
410+
itemId: "item-late",
411+
callId: "call-late",
412+
name: REALTIME_VOICE_DESCRIBE_VIEW_TOOL_NAME,
413+
arguments: "{}",
414+
});
415+
expect(peer?.channel.close).toHaveBeenCalledOnce();
416+
});
417+
156418
it("surfaces OpenAI tool-result send failures without an unhandled rejection", async () => {
157419
const onStatus = vi.fn();
158420
const onTalkEvent = vi.fn();

0 commit comments

Comments
 (0)