Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
376 lines
11 KiB
TypeScript
376 lines
11 KiB
TypeScript
// Openai tests cover realtime transcription provider plugin behavior.
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { buildOpenAIRealtimeTranscriptionProvider } from "./realtime-transcription-provider.js";
|
|
|
|
const { FakeWebSocket, providerAuthMocks, ssrfMocks } = vi.hoisted(() => {
|
|
type Listener = (...args: unknown[]) => void;
|
|
|
|
class MockWebSocket {
|
|
static readonly OPEN = 1;
|
|
static readonly CLOSED = 3;
|
|
static instances: MockWebSocket[] = [];
|
|
|
|
readonly listeners = new Map<string, Listener[]>();
|
|
readonly headers?: Record<string, string>;
|
|
readonly url?: string;
|
|
readyState = 0;
|
|
sent: string[] = [];
|
|
closed = false;
|
|
|
|
constructor(url?: string, options?: { headers?: Record<string, string> }) {
|
|
this.url = url;
|
|
this.headers = options?.headers;
|
|
MockWebSocket.instances.push(this);
|
|
}
|
|
|
|
on(event: string, listener: Listener): this {
|
|
const listeners = this.listeners.get(event) ?? [];
|
|
listeners.push(listener);
|
|
this.listeners.set(event, listeners);
|
|
return this;
|
|
}
|
|
|
|
emit(event: string, ...args: unknown[]): void {
|
|
for (const listener of this.listeners.get(event) ?? []) {
|
|
listener(...args);
|
|
}
|
|
}
|
|
|
|
send(payload: string): void {
|
|
this.sent.push(payload);
|
|
}
|
|
|
|
close(code?: number, reason?: string): void {
|
|
this.closed = true;
|
|
this.readyState = MockWebSocket.CLOSED;
|
|
this.emit("close", code ?? 1000, Buffer.from(reason ?? ""));
|
|
}
|
|
}
|
|
|
|
return {
|
|
FakeWebSocket: MockWebSocket,
|
|
providerAuthMocks: {
|
|
isProviderAuthProfileConfigured: vi.fn(),
|
|
resolveProviderAuthProfileApiKey: vi.fn(),
|
|
},
|
|
ssrfMocks: {
|
|
fetchWithSsrFGuard: vi.fn(),
|
|
},
|
|
};
|
|
});
|
|
|
|
vi.mock("ws", () => ({
|
|
default: FakeWebSocket,
|
|
}));
|
|
|
|
vi.mock("openclaw/plugin-sdk/provider-auth", () => ({
|
|
isProviderAuthProfileConfigured: providerAuthMocks.isProviderAuthProfileConfigured,
|
|
resolveProviderAuthProfileApiKey: providerAuthMocks.resolveProviderAuthProfileApiKey,
|
|
}));
|
|
|
|
vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ({
|
|
fetchWithSsrFGuard: ssrfMocks.fetchWithSsrFGuard,
|
|
}));
|
|
|
|
type FakeWebSocketInstance = InstanceType<typeof FakeWebSocket>;
|
|
type SentRealtimeEvent = {
|
|
type: string;
|
|
audio?: string;
|
|
session?: unknown;
|
|
};
|
|
|
|
function parseSent(socket: FakeWebSocketInstance): SentRealtimeEvent[] {
|
|
return socket.sent.map((payload) => JSON.parse(payload) as SentRealtimeEvent);
|
|
}
|
|
|
|
async function waitForFakeSocket(): Promise<FakeWebSocketInstance> {
|
|
let socket: FakeWebSocketInstance | undefined;
|
|
await vi.waitFor(() => {
|
|
socket = FakeWebSocket.instances[0];
|
|
if (!socket) {
|
|
throw new Error("expected session to create a websocket");
|
|
}
|
|
});
|
|
if (!socket) {
|
|
throw new Error("expected session to create a websocket");
|
|
}
|
|
return socket;
|
|
}
|
|
|
|
function mockCallArg(mock: { mock: { calls: unknown[][] } }, index = 0): Record<string, unknown> {
|
|
const call = mock.mock.calls[index];
|
|
if (!call) {
|
|
throw new Error(`expected mock call ${index}`);
|
|
}
|
|
return call[0] as Record<string, unknown>;
|
|
}
|
|
|
|
describe("buildOpenAIRealtimeTranscriptionProvider", () => {
|
|
beforeEach(() => {
|
|
FakeWebSocket.instances = [];
|
|
providerAuthMocks.isProviderAuthProfileConfigured.mockReset();
|
|
providerAuthMocks.resolveProviderAuthProfileApiKey.mockReset();
|
|
ssrfMocks.fetchWithSsrFGuard.mockReset();
|
|
vi.stubEnv("OPENAI_API_KEY", "");
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
it("normalizes OpenAI config defaults", () => {
|
|
const provider = buildOpenAIRealtimeTranscriptionProvider();
|
|
const resolved = provider.resolveConfig?.({
|
|
cfg: {} as never,
|
|
rawConfig: {
|
|
providers: {
|
|
openai: {
|
|
apiKey: "sk-test", // pragma: allowlist secret
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(resolved).toEqual({
|
|
apiKey: "sk-test",
|
|
});
|
|
});
|
|
|
|
it("keeps provider-owned transcription settings configurable via raw provider config", () => {
|
|
const provider = buildOpenAIRealtimeTranscriptionProvider();
|
|
const resolved = provider.resolveConfig?.({
|
|
cfg: {} as never,
|
|
rawConfig: {
|
|
providers: {
|
|
openai: {
|
|
language: "en",
|
|
model: "gpt-4o-transcribe",
|
|
prompt: "expect OpenClaw product names",
|
|
silenceDurationMs: 900,
|
|
vadThreshold: 0.45,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(resolved).toEqual({
|
|
language: "en",
|
|
model: "gpt-4o-transcribe",
|
|
prompt: "expect OpenClaw product names",
|
|
silenceDurationMs: 900,
|
|
vadThreshold: 0.45,
|
|
});
|
|
});
|
|
|
|
it("preserves explicit zero-valued VAD settings", () => {
|
|
const provider = buildOpenAIRealtimeTranscriptionProvider();
|
|
const resolved = provider.resolveConfig?.({
|
|
cfg: {} as never,
|
|
rawConfig: {
|
|
providers: {
|
|
openai: {
|
|
silenceDurationMs: 0,
|
|
vadThreshold: 0,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(resolved?.silenceDurationMs).toBe(0);
|
|
expect(resolved?.vadThreshold).toBe(0);
|
|
});
|
|
|
|
it("drops malformed VAD timing settings", () => {
|
|
const provider = buildOpenAIRealtimeTranscriptionProvider();
|
|
const resolved = provider.resolveConfig?.({
|
|
cfg: {} as never,
|
|
rawConfig: {
|
|
providers: {
|
|
openai: {
|
|
silenceDurationMs: -1,
|
|
vadThreshold: 1.5,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(resolved?.silenceDurationMs).toBeUndefined();
|
|
expect(resolved?.vadThreshold).toBeUndefined();
|
|
});
|
|
|
|
it("accepts the legacy openai-realtime alias", () => {
|
|
const provider = buildOpenAIRealtimeTranscriptionProvider();
|
|
expect(provider.aliases).toContain("openai-realtime");
|
|
});
|
|
|
|
it("treats a Codex OAuth profile as configured when no API key is present", () => {
|
|
const provider = buildOpenAIRealtimeTranscriptionProvider();
|
|
const cfg = { auth: { order: { openai: ["openai:default"] } } };
|
|
providerAuthMocks.isProviderAuthProfileConfigured.mockReturnValue(true);
|
|
|
|
expect(provider.isConfigured({ cfg: cfg as never, providerConfig: {} })).toBe(true);
|
|
expect(providerAuthMocks.isProviderAuthProfileConfigured).toHaveBeenCalledWith({
|
|
provider: "openai",
|
|
cfg,
|
|
});
|
|
});
|
|
|
|
it("mints a Codex OAuth client secret for realtime transcription sockets", async () => {
|
|
const provider = buildOpenAIRealtimeTranscriptionProvider();
|
|
const release = vi.fn();
|
|
providerAuthMocks.resolveProviderAuthProfileApiKey.mockResolvedValue("oauth-token");
|
|
ssrfMocks.fetchWithSsrFGuard.mockResolvedValue({
|
|
response: new Response(JSON.stringify({ value: "ek-test" }), { status: 200 }),
|
|
release,
|
|
});
|
|
const cfg = { auth: { order: { openai: ["openai:default"] } } };
|
|
const session = provider.createSession({
|
|
cfg: cfg as never,
|
|
providerConfig: {},
|
|
});
|
|
|
|
const connecting = session.connect();
|
|
const socket = await waitForFakeSocket();
|
|
|
|
expect(socket.headers?.Authorization).toBe("Bearer ek-test");
|
|
expect(providerAuthMocks.resolveProviderAuthProfileApiKey).toHaveBeenCalledWith({
|
|
provider: "openai",
|
|
cfg,
|
|
});
|
|
const request = mockCallArg(ssrfMocks.fetchWithSsrFGuard);
|
|
expect(request.auditContext).toBe("openai-realtime-transcription-session");
|
|
expect(request.url).toBe("https://api.openai.com/v1/realtime/transcription_sessions");
|
|
const init = request.init as {
|
|
method?: string;
|
|
headers?: Record<string, string>;
|
|
body?: unknown;
|
|
};
|
|
expect(init.method).toBe("POST");
|
|
expect(init.headers?.Authorization).toBe("Bearer oauth-token");
|
|
expect(init.headers?.["Content-Type"]).toBe("application/json");
|
|
expect(typeof init.body).toBe("string");
|
|
expect(JSON.parse(init.body as string)).toEqual({
|
|
type: "transcription",
|
|
audio: {
|
|
input: {
|
|
format: { type: "audio/pcmu" },
|
|
transcription: { model: "gpt-4o-transcribe" },
|
|
turn_detection: {
|
|
type: "server_vad",
|
|
threshold: 0.5,
|
|
prefix_padding_ms: 300,
|
|
silence_duration_ms: 800,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
socket.readyState = FakeWebSocket.OPEN;
|
|
socket.emit("open");
|
|
socket.emit("message", Buffer.from(JSON.stringify({ type: "transcription_session.updated" })));
|
|
await connecting;
|
|
|
|
expect(release).toHaveBeenCalled();
|
|
expect(parseSent(socket)[0]).toEqual({
|
|
type: "session.update",
|
|
session: {
|
|
type: "transcription",
|
|
audio: {
|
|
input: {
|
|
format: { type: "audio/pcmu" },
|
|
transcription: { model: "gpt-4o-transcribe" },
|
|
turn_detection: {
|
|
type: "server_vad",
|
|
threshold: 0.5,
|
|
prefix_padding_ms: 300,
|
|
silence_duration_ms: 800,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
session.close();
|
|
});
|
|
|
|
it("waits for the OpenAI session update before draining audio", async () => {
|
|
const provider = buildOpenAIRealtimeTranscriptionProvider();
|
|
const session = provider.createSession({
|
|
providerConfig: {
|
|
apiKey: "sk-test", // pragma: allowlist secret
|
|
language: "en",
|
|
model: "gpt-4o-transcribe",
|
|
prompt: "expect OpenClaw product names",
|
|
silenceDurationMs: 900,
|
|
vadThreshold: 0.45,
|
|
},
|
|
});
|
|
|
|
const connecting = session.connect();
|
|
const socket = await waitForFakeSocket();
|
|
|
|
socket.readyState = FakeWebSocket.OPEN;
|
|
socket.emit("open");
|
|
session.sendAudio(Buffer.from("before-ready"));
|
|
|
|
expect(session.isConnected()).toBe(false);
|
|
expect(parseSent(socket)).toEqual([
|
|
{
|
|
type: "session.update",
|
|
session: {
|
|
type: "transcription",
|
|
audio: {
|
|
input: {
|
|
format: { type: "audio/pcmu" },
|
|
transcription: {
|
|
model: "gpt-4o-transcribe",
|
|
language: "en",
|
|
prompt: "expect OpenClaw product names",
|
|
},
|
|
turn_detection: {
|
|
type: "server_vad",
|
|
threshold: 0.45,
|
|
prefix_padding_ms: 300,
|
|
silence_duration_ms: 900,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
]);
|
|
|
|
socket.emit("message", Buffer.from(JSON.stringify({ type: "session.updated" })));
|
|
await connecting;
|
|
|
|
expect(session.isConnected()).toBe(true);
|
|
expect(parseSent(socket)).toEqual([
|
|
{
|
|
type: "session.update",
|
|
session: {
|
|
type: "transcription",
|
|
audio: {
|
|
input: {
|
|
format: { type: "audio/pcmu" },
|
|
transcription: {
|
|
model: "gpt-4o-transcribe",
|
|
language: "en",
|
|
prompt: "expect OpenClaw product names",
|
|
},
|
|
turn_detection: {
|
|
type: "server_vad",
|
|
threshold: 0.45,
|
|
prefix_padding_ms: 300,
|
|
silence_duration_ms: 900,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
type: "input_audio_buffer.append",
|
|
audio: Buffer.from("before-ready").toString("base64"),
|
|
},
|
|
]);
|
|
session.close();
|
|
});
|
|
});
|