|
1 | 1 | // @vitest-environment jsdom |
2 | 2 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { REALTIME_VOICE_DESCRIBE_VIEW_TOOL_NAME } from "../../../../src/talk/describe-view-tool.js"; |
3 | 4 | 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"; |
5 | 9 | import { WebRtcSdpRealtimeTalkTransport } from "./realtime-talk-webrtc.ts"; |
6 | 10 |
|
7 | 11 | class FakeDataChannel extends EventTarget { |
@@ -69,19 +73,57 @@ function dispatchControlToolCall( |
69 | 73 | peer: FakePeerConnection | undefined, |
70 | 74 | args: { text: string; mode: "status" | "steer" }, |
71 | 75 | ): 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); |
72 | 96 | peer?.channel.dispatchEvent( |
73 | 97 | new MessageEvent("message", { |
74 | 98 | 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 | + }, |
80 | 114 | }), |
81 | 115 | }), |
82 | 116 | ); |
83 | 117 | } |
84 | 118 |
|
| 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 | + |
85 | 127 | describe("WebRtcSdpRealtimeTalkTransport control tool", () => { |
86 | 128 | beforeEach(() => { |
87 | 129 | FakePeerConnection.instances = []; |
@@ -153,6 +195,226 @@ describe("WebRtcSdpRealtimeTalkTransport control tool", () => { |
153 | 195 | transport.stop(); |
154 | 196 | }); |
155 | 197 |
|
| 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 | + |
156 | 418 | it("surfaces OpenAI tool-result send failures without an unhandled rejection", async () => { |
157 | 419 | const onStatus = vi.fn(); |
158 | 420 | const onTalkEvent = vi.fn(); |
|
0 commit comments