惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
博客园 - Franky
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
爱范儿
爱范儿
罗磊的独立博客
博客园_首页
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
V
Visual Studio Blog
T
Tailwind CSS Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(openai): wait for realtime transcription session upda...
vincentkoc · 2026-05-04 · via Recent Commits to openclaw:main

@@ -1,7 +1,88 @@

1-

import { describe, expect, it } from "vitest";

1+

import { beforeEach, describe, expect, it, vi } from "vitest";

22

import { buildOpenAIRealtimeTranscriptionProvider } from "./realtime-transcription-provider.js";

334+

const { FakeWebSocket } = vi.hoisted(() => {

5+

type Listener = (...args: unknown[]) => void;

6+7+

class MockWebSocket {

8+

static readonly OPEN = 1;

9+

static readonly CLOSED = 3;

10+

static instances: MockWebSocket[] = [];

11+12+

readonly listeners = new Map<string, Listener[]>();

13+

readyState = 0;

14+

sent: string[] = [];

15+

closed = false;

16+17+

constructor() {

18+

MockWebSocket.instances.push(this);

19+

}

20+21+

on(event: string, listener: Listener): this {

22+

const listeners = this.listeners.get(event) ?? [];

23+

listeners.push(listener);

24+

this.listeners.set(event, listeners);

25+

return this;

26+

}

27+28+

emit(event: string, ...args: unknown[]): void {

29+

for (const listener of this.listeners.get(event) ?? []) {

30+

listener(...args);

31+

}

32+

}

33+34+

send(payload: string): void {

35+

this.sent.push(payload);

36+

}

37+38+

close(code?: number, reason?: string): void {

39+

this.closed = true;

40+

this.readyState = MockWebSocket.CLOSED;

41+

this.emit("close", code ?? 1000, Buffer.from(reason ?? ""));

42+

}

43+

}

44+45+

return { FakeWebSocket: MockWebSocket };

46+

});

47+48+

vi.mock("ws", () => ({

49+

default: FakeWebSocket,

50+

}));

51+52+

type FakeWebSocketInstance = InstanceType<typeof FakeWebSocket>;

53+

type SentRealtimeEvent = {

54+

type: string;

55+

audio?: string;

56+

session?: {

57+

type?: string;

58+

audio?: {

59+

input?: {

60+

format?: { type?: string };

61+

transcription?: {

62+

model?: string;

63+

language?: string;

64+

prompt?: string;

65+

};

66+

turn_detection?: {

67+

type?: string;

68+

threshold?: number;

69+

prefix_padding_ms?: number;

70+

silence_duration_ms?: number;

71+

};

72+

};

73+

};

74+

};

75+

};

76+77+

function parseSent(socket: FakeWebSocketInstance): SentRealtimeEvent[] {

78+

return socket.sent.map((payload) => JSON.parse(payload) as SentRealtimeEvent);

79+

}

80+481

describe("buildOpenAIRealtimeTranscriptionProvider", () => {

82+

beforeEach(() => {

83+

FakeWebSocket.instances = [];

84+

});

85+586

it("normalizes OpenAI config defaults", () => {

687

const provider = buildOpenAIRealtimeTranscriptionProvider();

788

const resolved = provider.resolveConfig?.({

@@ -70,4 +151,88 @@ describe("buildOpenAIRealtimeTranscriptionProvider", () => {

70151

const provider = buildOpenAIRealtimeTranscriptionProvider();

71152

expect(provider.aliases).toContain("openai-realtime");

72153

});

154+155+

it("waits for the OpenAI session update before draining audio", async () => {

156+

const provider = buildOpenAIRealtimeTranscriptionProvider();

157+

const session = provider.createSession({

158+

providerConfig: {

159+

apiKey: "sk-test", // pragma: allowlist secret

160+

language: "en",

161+

model: "gpt-4o-transcribe",

162+

prompt: "expect OpenClaw product names",

163+

silenceDurationMs: 900,

164+

vadThreshold: 0.45,

165+

},

166+

});

167+168+

const connecting = session.connect();

169+

const socket = FakeWebSocket.instances[0];

170+

if (!socket) {

171+

throw new Error("expected session to create a websocket");

172+

}

173+174+

socket.readyState = FakeWebSocket.OPEN;

175+

socket.emit("open");

176+

session.sendAudio(Buffer.from("before-ready"));

177+178+

expect(session.isConnected()).toBe(false);

179+

expect(parseSent(socket)).toEqual([

180+

{

181+

type: "transcription_session.update",

182+

session: {

183+

type: "transcription",

184+

audio: {

185+

input: {

186+

format: { type: "audio/pcmu" },

187+

transcription: {

188+

model: "gpt-4o-transcribe",

189+

language: "en",

190+

prompt: "expect OpenClaw product names",

191+

},

192+

turn_detection: {

193+

type: "server_vad",

194+

threshold: 0.45,

195+

prefix_padding_ms: 300,

196+

silence_duration_ms: 900,

197+

},

198+

},

199+

},

200+

},

201+

},

202+

]);

203+204+

socket.emit("message", Buffer.from(JSON.stringify({ type: "session.updated" })));

205+

await connecting;

206+207+

expect(session.isConnected()).toBe(true);

208+

expect(parseSent(socket)).toEqual([

209+

{

210+

type: "transcription_session.update",

211+

session: {

212+

type: "transcription",

213+

audio: {

214+

input: {

215+

format: { type: "audio/pcmu" },

216+

transcription: {

217+

model: "gpt-4o-transcribe",

218+

language: "en",

219+

prompt: "expect OpenClaw product names",

220+

},

221+

turn_detection: {

222+

type: "server_vad",

223+

threshold: 0.45,

224+

prefix_padding_ms: 300,

225+

silence_duration_ms: 900,

226+

},

227+

},

228+

},

229+

},

230+

},

231+

{

232+

type: "input_audio_buffer.append",

233+

audio: Buffer.from("before-ready").toString("base64"),

234+

},

235+

]);

236+

session.close();

237+

});

73238

});