|
| 1 | +import { once } from "node:events"; |
| 2 | +import type { RealtimeVoiceBridge } from "openclaw/plugin-sdk/realtime-voice"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import WebSocket, { type RawData, WebSocketServer } from "ws"; |
| 5 | +import { OpenAIQuicksilverVoiceBridge } from "./realtime-quicksilver-bridge.js"; |
| 6 | +import { buildOpenAIRealtimeVoiceProvider } from "./realtime-voice-provider.js"; |
| 7 | + |
| 8 | +type RealtimeProviderKind = "native" | "gpt-live"; |
| 9 | + |
| 10 | +function parseWebSocketMessage(data: RawData): Record<string, unknown> { |
| 11 | + const bytes = Buffer.isBuffer(data) |
| 12 | + ? data |
| 13 | + : Array.isArray(data) |
| 14 | + ? Buffer.concat(data) |
| 15 | + : Buffer.from(data); |
| 16 | + return JSON.parse(bytes.toString("utf8")) as Record<string, unknown>; |
| 17 | +} |
| 18 | + |
| 19 | +async function withRealtimeProvider( |
| 20 | + kind: RealtimeProviderKind, |
| 21 | + prepareAudio: (bridge: RealtimeVoiceBridge) => void, |
| 22 | +): Promise<Array<Record<string, unknown>>> { |
| 23 | + const audioEventType = kind === "native" ? "input_audio_buffer.append" : "input_audio.append"; |
| 24 | + const server = new WebSocketServer({ host: "127.0.0.1", port: 0 }); |
| 25 | + await once(server, "listening"); |
| 26 | + const address = server.address(); |
| 27 | + if (!address || typeof address === "string") { |
| 28 | + throw new Error("expected an available local realtime WebSocket address"); |
| 29 | + } |
| 30 | + const received: Array<Record<string, unknown>> = []; |
| 31 | + server.once("connection", (socket) => { |
| 32 | + socket.on("message", (payload) => { |
| 33 | + const event = parseWebSocketMessage(payload); |
| 34 | + received.push(event); |
| 35 | + if (event.type === "session.update") { |
| 36 | + socket.send( |
| 37 | + JSON.stringify( |
| 38 | + kind === "native" |
| 39 | + ? { type: "session.updated" } |
| 40 | + : { |
| 41 | + type: "session.started", |
| 42 | + session: { id: "fixture-live", expires_at: Math.floor(Date.now() / 1000) + 60 }, |
| 43 | + }, |
| 44 | + ), |
| 45 | + ); |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + const endpoint = `http://127.0.0.1:${address.port}`; |
| 51 | + const bridge = |
| 52 | + kind === "native" |
| 53 | + ? buildOpenAIRealtimeVoiceProvider().createBridge({ |
| 54 | + providerConfig: { |
| 55 | + apiKey: "fixture-local", // pragma: allowlist secret |
| 56 | + azureEndpoint: endpoint, |
| 57 | + azureDeployment: "fixture-realtime", |
| 58 | + }, |
| 59 | + onAudio: vi.fn(), |
| 60 | + onClearAudio: vi.fn(), |
| 61 | + }) |
| 62 | + : new OpenAIQuicksilverVoiceBridge({ |
| 63 | + providerConfig: {}, |
| 64 | + model: "gpt-live-1-codex", |
| 65 | + audioFormat: { encoding: "pcm16", sampleRateHz: 24000, channels: 1 }, |
| 66 | + resolveAuth: async () => ({ type: "api-key", token: "fixture-local" }), |
| 67 | + webSocketFactory: (_url, options) => new WebSocket(endpoint, options), |
| 68 | + onAudio: vi.fn(), |
| 69 | + onClearAudio: vi.fn(), |
| 70 | + }); |
| 71 | + |
| 72 | + try { |
| 73 | + prepareAudio(bridge); |
| 74 | + await bridge.connect(); |
| 75 | + await vi.waitFor(() => { |
| 76 | + expect(received.some((event) => event.type === audioEventType)).toBe(true); |
| 77 | + }); |
| 78 | + return received; |
| 79 | + } finally { |
| 80 | + bridge.close(); |
| 81 | + for (const client of server.clients) { |
| 82 | + client.terminate(); |
| 83 | + } |
| 84 | + await new Promise<void>((resolve, reject) => { |
| 85 | + server.close((error) => (error ? reject(error) : resolve())); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +describe("OpenAI realtime queued audio buffer ownership", () => { |
| 91 | + it.each<RealtimeProviderKind>(["native", "gpt-live"])( |
| 92 | + "%s preserves each reusable producer frame until the real WebSocket is ready", |
| 93 | + async (kind) => { |
| 94 | + const audioEventType = kind === "native" ? "input_audio_buffer.append" : "input_audio.append"; |
| 95 | + const received = await withRealtimeProvider(kind, (bridge) => { |
| 96 | + const producerAllocation = Buffer.alloc(2 * 1024 * 1024, 0x7f); |
| 97 | + const producerView = producerAllocation.subarray(0, 1); |
| 98 | + bridge.sendAudio(producerView); |
| 99 | + producerAllocation[0] = 0x41; |
| 100 | + bridge.sendAudio(producerView); |
| 101 | + producerAllocation[0] = 0; |
| 102 | + }); |
| 103 | + |
| 104 | + expect(received.filter((event) => event.type === audioEventType)).toEqual([ |
| 105 | + { type: audioEventType, audio: "fw==" }, |
| 106 | + { type: audioEventType, audio: "QQ==" }, |
| 107 | + ]); |
| 108 | + }, |
| 109 | + ); |
| 110 | + |
| 111 | + it.each<RealtimeProviderKind>(["native", "gpt-live"])( |
| 112 | + "%s rejects oversized producer frames before allocating a queued copy", |
| 113 | + async (kind) => { |
| 114 | + const audioEventType = kind === "native" ? "input_audio_buffer.append" : "input_audio.append"; |
| 115 | + const received = await withRealtimeProvider(kind, (bridge) => { |
| 116 | + const oversized = Buffer.alloc(1024 * 1024 + 1); |
| 117 | + const copyBuffer = vi.spyOn(Buffer, "from"); |
| 118 | + try { |
| 119 | + bridge.sendAudio(oversized); |
| 120 | + expect(copyBuffer).not.toHaveBeenCalled(); |
| 121 | + } finally { |
| 122 | + copyBuffer.mockRestore(); |
| 123 | + } |
| 124 | + bridge.sendAudio(Buffer.from([0x7f])); |
| 125 | + }); |
| 126 | + |
| 127 | + expect(received.filter((event) => event.type === audioEventType)).toEqual([ |
| 128 | + { type: audioEventType, audio: "fw==" }, |
| 129 | + ]); |
| 130 | + }, |
| 131 | + ); |
| 132 | +}); |
0 commit comments