|
| 1 | +import { createServer } from "node:http"; |
| 2 | +import type { AddressInfo } from "node:net"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import type WebSocket from "ws"; |
| 5 | +import { WebSocketServer } from "ws"; |
| 6 | +import { buildXaiRealtimeVoiceProvider } from "./realtime-voice-provider.js"; |
| 7 | + |
| 8 | +type RealtimeOutcome = { |
| 9 | + errors: string[]; |
| 10 | + transcripts: Array<{ speaker: string; text: string; final: boolean }>; |
| 11 | + tools: Array<{ itemId: string; callId: string; name: string; args: unknown }>; |
| 12 | +}; |
| 13 | + |
| 14 | +async function captureRealtimeOutcome( |
| 15 | + eventInput: Record<string, unknown> | Record<string, unknown>[], |
| 16 | +): Promise<RealtimeOutcome> { |
| 17 | + const events = Array.isArray(eventInput) ? eventInput : [eventInput]; |
| 18 | + const outcome: RealtimeOutcome = { errors: [], transcripts: [], tools: [] }; |
| 19 | + let markServerEventHandled: () => void = () => {}; |
| 20 | + const serverEventHandled = new Promise<void>((resolve) => { |
| 21 | + markServerEventHandled = resolve; |
| 22 | + }); |
| 23 | + const server = createServer(); |
| 24 | + const sockets = new Set<WebSocket>(); |
| 25 | + const wss = new WebSocketServer({ noServer: true, maxPayload: 1024 * 1024 }); |
| 26 | + server.on("upgrade", (request, socket, head) => { |
| 27 | + wss.handleUpgrade(request, socket, head, (ws) => { |
| 28 | + sockets.add(ws); |
| 29 | + ws.on("message", (message) => { |
| 30 | + if (JSON.parse(Buffer.from(message as Buffer).toString("utf8")).type !== "session.update") { |
| 31 | + return; |
| 32 | + } |
| 33 | + ws.send(JSON.stringify({ type: "session.updated" })); |
| 34 | + ws.send(JSON.stringify({ type: "response.created", response: { id: "response_1" } })); |
| 35 | + for (const event of events) { |
| 36 | + ws.send(JSON.stringify(event)); |
| 37 | + } |
| 38 | + }); |
| 39 | + }); |
| 40 | + }); |
| 41 | + await new Promise<void>((resolve) => { |
| 42 | + server.listen(0, "127.0.0.1", resolve); |
| 43 | + }); |
| 44 | + const port = (server.address() as AddressInfo).port; |
| 45 | + const bridge = buildXaiRealtimeVoiceProvider().createBridge({ |
| 46 | + providerConfig: { apiKey: "fixture-value", baseUrl: `http://127.0.0.1:${port}/v1` }, |
| 47 | + onAudio() {}, |
| 48 | + onClearAudio() {}, |
| 49 | + onError: (error) => outcome.errors.push(error.message), |
| 50 | + onTranscript: (speaker, text, final) => outcome.transcripts.push({ speaker, text, final }), |
| 51 | + onToolCall: (tool) => outcome.tools.push(tool), |
| 52 | + onEvent: (observed) => { |
| 53 | + if (observed.direction === "server" && observed.type === events.at(-1)?.type) { |
| 54 | + markServerEventHandled(); |
| 55 | + } |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + try { |
| 60 | + await bridge.connect(); |
| 61 | + await serverEventHandled; |
| 62 | + return outcome; |
| 63 | + } finally { |
| 64 | + bridge.close(); |
| 65 | + for (const socket of sockets) { |
| 66 | + socket.terminate(); |
| 67 | + } |
| 68 | + await new Promise<void>((resolve) => { |
| 69 | + wss.close(() => resolve()); |
| 70 | + }); |
| 71 | + await new Promise<void>((resolve) => { |
| 72 | + server.close(() => resolve()); |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +const completedTool = { |
| 78 | + id: "item_tool", |
| 79 | + type: "function_call", |
| 80 | + call_id: "call_tool", |
| 81 | + name: "lookup_weather", |
| 82 | + arguments: JSON.stringify({ city: "Paris" }), |
| 83 | +}; |
| 84 | +const expectedTool = { |
| 85 | + itemId: "item_tool", |
| 86 | + callId: "call_tool", |
| 87 | + name: "lookup_weather", |
| 88 | + args: { city: "Paris" }, |
| 89 | +}; |
| 90 | + |
| 91 | +describe("xAI realtime terminal event ownership", () => { |
| 92 | + it.each([ |
| 93 | + { |
| 94 | + name: "preserves assistant transcripts carried only by terminal output", |
| 95 | + event: { |
| 96 | + type: "response.done", |
| 97 | + response: { |
| 98 | + status: "completed", |
| 99 | + output: [ |
| 100 | + { |
| 101 | + type: "message", |
| 102 | + role: "assistant", |
| 103 | + content: [{ type: "output_audio", transcript: "terminal answer" }], |
| 104 | + }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + }, |
| 108 | + expected: { |
| 109 | + errors: [], |
| 110 | + transcripts: [{ speaker: "assistant", text: "terminal answer", final: true }], |
| 111 | + tools: [], |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "surfaces failed responses with provider error details", |
| 116 | + event: { |
| 117 | + type: "response.done", |
| 118 | + response: { status: "failed", status_details: { error: { code: "rate_limit_exceeded" } } }, |
| 119 | + }, |
| 120 | + expected: { errors: ["rate_limit_exceeded"], transcripts: [], tools: [] }, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "surfaces incomplete responses with their authoritative reason", |
| 124 | + event: { |
| 125 | + type: "response.done", |
| 126 | + response: { status: "incomplete", status_details: { reason: "max_output_tokens" } }, |
| 127 | + }, |
| 128 | + expected: { |
| 129 | + errors: ["xAI realtime voice response incomplete: max_output_tokens"], |
| 130 | + transcripts: [], |
| 131 | + tools: [], |
| 132 | + }, |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "does not mistake intentional cancellation for a provider failure", |
| 136 | + event: { |
| 137 | + type: "response.done", |
| 138 | + response: { status: "cancelled", status_details: { reason: "client_cancelled" } }, |
| 139 | + }, |
| 140 | + expected: { errors: [], transcripts: [], tools: [] }, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "ignores output-item events and waits for canonical terminal output", |
| 144 | + event: { type: "response.output_item.done", item: completedTool }, |
| 145 | + expected: { errors: [], transcripts: [], tools: [] }, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "retains immediate completed replay delivery for resumed conversations", |
| 149 | + event: { type: "conversation.item.created", item: completedTool }, |
| 150 | + expected: { errors: [], transcripts: [], tools: [expectedTool] }, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "retains immediate authoritative function-call argument completion", |
| 154 | + event: { |
| 155 | + type: "response.function_call_arguments.done", |
| 156 | + item_id: completedTool.id, |
| 157 | + call_id: completedTool.call_id, |
| 158 | + name: completedTool.name, |
| 159 | + arguments: completedTool.arguments, |
| 160 | + }, |
| 161 | + expected: { errors: [], transcripts: [], tools: [expectedTool] }, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "preserves required streamed-call timing when the response later fails", |
| 165 | + event: [ |
| 166 | + { |
| 167 | + type: "response.function_call_arguments.done", |
| 168 | + item_id: completedTool.id, |
| 169 | + call_id: completedTool.call_id, |
| 170 | + name: completedTool.name, |
| 171 | + arguments: completedTool.arguments, |
| 172 | + }, |
| 173 | + { type: "response.done", response: { status: "failed", output: [] } }, |
| 174 | + ], |
| 175 | + expected: { |
| 176 | + errors: ["xAI realtime voice response failed"], |
| 177 | + transcripts: [], |
| 178 | + tools: [expectedTool], |
| 179 | + }, |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "does not dispatch incomplete replayed function-call items", |
| 183 | + event: { |
| 184 | + type: "conversation.item.created", |
| 185 | + item: { ...completedTool, status: "incomplete" }, |
| 186 | + }, |
| 187 | + expected: { errors: [], transcripts: [], tools: [] }, |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "recovers completed tool calls from terminal response output", |
| 191 | + event: { |
| 192 | + type: "response.done", |
| 193 | + response: { status: "completed", output: [completedTool] }, |
| 194 | + }, |
| 195 | + expected: { errors: [], transcripts: [], tools: [expectedTool] }, |
| 196 | + }, |
| 197 | + { |
| 198 | + name: "deduplicates immediate tool delivery against terminal output", |
| 199 | + event: [ |
| 200 | + { |
| 201 | + type: "response.function_call_arguments.done", |
| 202 | + item_id: completedTool.id, |
| 203 | + call_id: completedTool.call_id, |
| 204 | + name: completedTool.name, |
| 205 | + arguments: completedTool.arguments, |
| 206 | + }, |
| 207 | + { type: "response.done", response: { status: "completed", output: [completedTool] } }, |
| 208 | + ], |
| 209 | + expected: { errors: [], transcripts: [], tools: [expectedTool] }, |
| 210 | + }, |
| 211 | + ...["failed", "incomplete", "cancelled"].map((status) => ({ |
| 212 | + name: `does not recover terminal-output tool calls from a ${status} response`, |
| 213 | + event: { |
| 214 | + type: "response.done", |
| 215 | + response: { status, output: [completedTool] }, |
| 216 | + }, |
| 217 | + expected: { |
| 218 | + errors: status === "cancelled" ? [] : [`xAI realtime voice response ${status}`], |
| 219 | + transcripts: [], |
| 220 | + tools: [], |
| 221 | + }, |
| 222 | + })), |
| 223 | + { |
| 224 | + name: "does not dispatch incomplete items from a completed terminal response", |
| 225 | + event: { |
| 226 | + type: "response.done", |
| 227 | + response: { status: "completed", output: [{ ...completedTool, status: "incomplete" }] }, |
| 228 | + }, |
| 229 | + expected: { errors: [], transcripts: [], tools: [] }, |
| 230 | + }, |
| 231 | + ])("$name", async ({ event, expected }) => { |
| 232 | + expect(await captureRealtimeOutcome(event)).toEqual(expected); |
| 233 | + }); |
| 234 | +}); |
0 commit comments