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
279 lines
8.7 KiB
TypeScript
279 lines
8.7 KiB
TypeScript
// Openai provider module implements model/runtime integration.
|
|
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
|
|
import {
|
|
isProviderAuthProfileConfigured,
|
|
resolveProviderAuthProfileApiKey,
|
|
} from "openclaw/plugin-sdk/provider-auth";
|
|
import { resolveProviderRequestHeaders } from "openclaw/plugin-sdk/provider-http";
|
|
import {
|
|
createRealtimeTranscriptionWebSocketSession,
|
|
type RealtimeTranscriptionProviderConfig,
|
|
type RealtimeTranscriptionProviderPlugin,
|
|
type RealtimeTranscriptionSession,
|
|
type RealtimeTranscriptionSessionCreateRequest,
|
|
type RealtimeTranscriptionWebSocketTransport,
|
|
} from "openclaw/plugin-sdk/realtime-transcription";
|
|
import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-input";
|
|
import {
|
|
asFiniteNumber,
|
|
createOpenAIRealtimeTranscriptionClientSecret,
|
|
readRealtimeErrorDetail,
|
|
resolveOpenAIProviderConfigRecord,
|
|
trimToUndefined,
|
|
} from "./realtime-provider-shared.js";
|
|
|
|
type OpenAIRealtimeTranscriptionProviderConfig = {
|
|
apiKey?: string;
|
|
language?: string;
|
|
model?: string;
|
|
prompt?: string;
|
|
silenceDurationMs?: number;
|
|
vadThreshold?: number;
|
|
};
|
|
|
|
type OpenAIRealtimeTranscriptionSessionConfig = RealtimeTranscriptionSessionCreateRequest & {
|
|
apiKey?: string;
|
|
cfg?: OpenClawConfig;
|
|
language?: string;
|
|
model: string;
|
|
prompt?: string;
|
|
silenceDurationMs: number;
|
|
vadThreshold: number;
|
|
};
|
|
|
|
type RealtimeEvent = {
|
|
type: string;
|
|
delta?: string;
|
|
transcript?: string;
|
|
error?: unknown;
|
|
};
|
|
|
|
type OpenAIRealtimeTranscriptionSessionPayload = {
|
|
type: "transcription";
|
|
audio: {
|
|
input: {
|
|
format: { type: "audio/pcmu" };
|
|
transcription: {
|
|
model: string;
|
|
language?: string;
|
|
prompt?: string;
|
|
};
|
|
turn_detection: {
|
|
type: "server_vad";
|
|
threshold: number;
|
|
prefix_padding_ms: number;
|
|
silence_duration_ms: number;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
const OPENAI_REALTIME_TRANSCRIPTION_URL = "wss://api.openai.com/v1/realtime?intent=transcription";
|
|
const OPENAI_REALTIME_TRANSCRIPTION_CONNECT_TIMEOUT_MS = 10_000;
|
|
const OPENAI_REALTIME_TRANSCRIPTION_MAX_RECONNECT_ATTEMPTS = 5;
|
|
const OPENAI_REALTIME_TRANSCRIPTION_RECONNECT_DELAY_MS = 1000;
|
|
const OPENAI_REALTIME_TRANSCRIPTION_DEFAULT_MODEL = "gpt-4o-transcribe";
|
|
|
|
function normalizeProviderConfig(
|
|
config: RealtimeTranscriptionProviderConfig,
|
|
): OpenAIRealtimeTranscriptionProviderConfig {
|
|
const raw = resolveOpenAIProviderConfigRecord(config);
|
|
return {
|
|
apiKey:
|
|
normalizeResolvedSecretInputString({
|
|
value: raw?.apiKey,
|
|
path: "plugins.entries.voice-call.config.streaming.providers.openai.apiKey",
|
|
}) ??
|
|
normalizeResolvedSecretInputString({
|
|
value: raw?.openaiApiKey,
|
|
path: "plugins.entries.voice-call.config.streaming.openaiApiKey",
|
|
}),
|
|
language: trimToUndefined(raw?.language),
|
|
model: trimToUndefined(raw?.model) ?? trimToUndefined(raw?.sttModel),
|
|
prompt: trimToUndefined(raw?.prompt),
|
|
silenceDurationMs: normalizeNonNegativeInteger(raw?.silenceDurationMs),
|
|
vadThreshold: normalizeVadThreshold(raw?.vadThreshold),
|
|
};
|
|
}
|
|
|
|
function normalizeNonNegativeInteger(value: unknown): number | undefined {
|
|
const number = asFiniteNumber(value);
|
|
if (number === undefined || !Number.isSafeInteger(number) || number < 0) {
|
|
return undefined;
|
|
}
|
|
return number;
|
|
}
|
|
|
|
function normalizeVadThreshold(value: unknown): number | undefined {
|
|
const number = asFiniteNumber(value);
|
|
if (number === undefined || number < 0 || number > 1) {
|
|
return undefined;
|
|
}
|
|
return number;
|
|
}
|
|
|
|
function buildOpenAIRealtimeTranscriptionSessionPayload(
|
|
config: OpenAIRealtimeTranscriptionSessionConfig,
|
|
): OpenAIRealtimeTranscriptionSessionPayload {
|
|
return {
|
|
type: "transcription",
|
|
audio: {
|
|
input: {
|
|
format: { type: "audio/pcmu" },
|
|
transcription: {
|
|
model: config.model,
|
|
...(config.language ? { language: config.language } : {}),
|
|
...(config.prompt ? { prompt: config.prompt } : {}),
|
|
},
|
|
turn_detection: {
|
|
type: "server_vad",
|
|
threshold: config.vadThreshold,
|
|
prefix_padding_ms: 300,
|
|
silence_duration_ms: config.silenceDurationMs,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
async function resolveOpenAIRealtimeTranscriptionAuthorization(
|
|
config: OpenAIRealtimeTranscriptionSessionConfig,
|
|
): Promise<string> {
|
|
const apiKey = config.apiKey || process.env.OPENAI_API_KEY;
|
|
if (apiKey) {
|
|
return apiKey;
|
|
}
|
|
const authToken = await resolveProviderAuthProfileApiKey({
|
|
provider: "openai",
|
|
cfg: config.cfg,
|
|
});
|
|
if (!authToken) {
|
|
throw new Error("OpenAI API key or Codex OAuth missing");
|
|
}
|
|
const clientSecret = await createOpenAIRealtimeTranscriptionClientSecret({
|
|
authToken,
|
|
auditContext: "openai-realtime-transcription-session",
|
|
session: buildOpenAIRealtimeTranscriptionSessionPayload(config),
|
|
});
|
|
return clientSecret.value;
|
|
}
|
|
|
|
function createOpenAIRealtimeTranscriptionSession(
|
|
config: OpenAIRealtimeTranscriptionSessionConfig,
|
|
): RealtimeTranscriptionSession {
|
|
let pendingTranscript = "";
|
|
|
|
const handleEvent = (
|
|
event: RealtimeEvent,
|
|
transport: RealtimeTranscriptionWebSocketTransport,
|
|
) => {
|
|
switch (event.type) {
|
|
case "session.updated":
|
|
case "transcription_session.updated":
|
|
transport.markReady();
|
|
return;
|
|
|
|
case "conversation.item.input_audio_transcription.delta":
|
|
if (event.delta) {
|
|
pendingTranscript += event.delta;
|
|
config.onPartial?.(pendingTranscript);
|
|
}
|
|
return;
|
|
|
|
case "conversation.item.input_audio_transcription.completed":
|
|
if (event.transcript) {
|
|
config.onTranscript?.(event.transcript);
|
|
}
|
|
pendingTranscript = "";
|
|
return;
|
|
|
|
case "input_audio_buffer.speech_started":
|
|
pendingTranscript = "";
|
|
config.onSpeechStart?.();
|
|
return;
|
|
|
|
case "error": {
|
|
const detail = readRealtimeErrorDetail(event.error);
|
|
const error = new Error(detail);
|
|
if (!transport.isReady()) {
|
|
transport.failConnect(error);
|
|
} else {
|
|
config.onError?.(error);
|
|
}
|
|
}
|
|
|
|
default:
|
|
}
|
|
};
|
|
|
|
return createRealtimeTranscriptionWebSocketSession<RealtimeEvent>({
|
|
providerId: "openai",
|
|
callbacks: config,
|
|
url: OPENAI_REALTIME_TRANSCRIPTION_URL,
|
|
headers: async () => {
|
|
const bearer = await resolveOpenAIRealtimeTranscriptionAuthorization(config);
|
|
return (
|
|
resolveProviderRequestHeaders({
|
|
provider: "openai",
|
|
baseUrl: OPENAI_REALTIME_TRANSCRIPTION_URL,
|
|
capability: "audio",
|
|
transport: "websocket",
|
|
defaultHeaders: {
|
|
Authorization: `Bearer ${bearer}`,
|
|
},
|
|
}) ?? {
|
|
Authorization: `Bearer ${bearer}`,
|
|
}
|
|
);
|
|
},
|
|
connectTimeoutMs: OPENAI_REALTIME_TRANSCRIPTION_CONNECT_TIMEOUT_MS,
|
|
maxReconnectAttempts: OPENAI_REALTIME_TRANSCRIPTION_MAX_RECONNECT_ATTEMPTS,
|
|
reconnectDelayMs: OPENAI_REALTIME_TRANSCRIPTION_RECONNECT_DELAY_MS,
|
|
connectTimeoutMessage: "OpenAI realtime transcription connection timeout",
|
|
connectClosedBeforeReadyMessage: "OpenAI realtime transcription connection closed before ready",
|
|
reconnectLimitMessage: "OpenAI realtime transcription reconnect limit reached",
|
|
sendAudio: (audio, transport) => {
|
|
transport.sendJson({
|
|
type: "input_audio_buffer.append",
|
|
audio: audio.toString("base64"),
|
|
});
|
|
},
|
|
onOpen: (transport: RealtimeTranscriptionWebSocketTransport) => {
|
|
transport.sendJson({
|
|
type: "session.update",
|
|
session: buildOpenAIRealtimeTranscriptionSessionPayload(config),
|
|
});
|
|
},
|
|
onMessage: handleEvent,
|
|
});
|
|
}
|
|
|
|
export function buildOpenAIRealtimeTranscriptionProvider(): RealtimeTranscriptionProviderPlugin {
|
|
return {
|
|
id: "openai",
|
|
label: "OpenAI Realtime Transcription",
|
|
aliases: ["openai-realtime"],
|
|
defaultModel: OPENAI_REALTIME_TRANSCRIPTION_DEFAULT_MODEL,
|
|
autoSelectOrder: 10,
|
|
resolveConfig: ({ rawConfig }) => normalizeProviderConfig(rawConfig),
|
|
isConfigured: ({ cfg, providerConfig }) =>
|
|
Boolean(
|
|
normalizeProviderConfig(providerConfig).apiKey ||
|
|
process.env.OPENAI_API_KEY ||
|
|
isProviderAuthProfileConfigured({ provider: "openai", cfg }),
|
|
),
|
|
createSession: (req) => {
|
|
const config = normalizeProviderConfig(req.providerConfig);
|
|
return createOpenAIRealtimeTranscriptionSession({
|
|
...req,
|
|
apiKey: config.apiKey,
|
|
language: config.language,
|
|
model: config.model ?? OPENAI_REALTIME_TRANSCRIPTION_DEFAULT_MODEL,
|
|
prompt: config.prompt,
|
|
silenceDurationMs: config.silenceDurationMs ?? 800,
|
|
vadThreshold: config.vadThreshold ?? 0.5,
|
|
});
|
|
},
|
|
};
|
|
}
|