Files
openclaw/extensions/openai/realtime-provider-shared.ts
Peter Steinberger fa03d9b913 refactor: consolidate coercion helpers (#121366)
* refactor: consolidate coercion helpers

* fix: remove duplicate coercion imports

* fix: preserve serialized coercion guard

* chore: ratchet coercion helper carve-outs

* fix(test): keep gauntlet subprocess startup lean

* fix: preserve imported session timestamp semantics

* fix: preserve catalog timestamp string semantics

* chore: align plugin SDK surface ratchet

* fix: preserve trajectory and SDK string contracts

* fix(test): preserve QA record assertion semantics

* fix: complete standalone record guard rename

* refactor(cron): use canonical string coercion

* fix(acpx): preserve Pi timestamp parsing

* test(channels): adapt custody test harnesses

* test(telegram): classify media harness as test support

* test(acpx): split timestamp contract coverage

* test(channels): support generated custody contracts

* chore: ban the full coercion helper name set

Extends the declaration guard to all eleven consolidated helper names and
renames the cron schedule-identity readNumber wrapper to readScheduleInteger
so the banned generic name cannot regrow.

* fix(scripts): repair release-validation guard drift and lint cause

Restores the renamed isJsonRecord guard in assertTrustedWorkflowHarness after
main added isRecord call sites in parallel, and attaches the caught YAML error
as the thrown error cause (preserve-caught-error was red on main).

* fix: preserve Claude timestamp string semantics

* fix: preserve persisted timestamp string semantics

* fix: preserve date-first timestamp contracts

* fix(openai): harden delegation failure formatting

* chore: close coercion helper guard gaps

* test(openai): model non-error delegation rejection

* chore: refresh plugin SDK API contract

* fix(tasks): use canonical string field reader

* fix(ai): use canonical provider error field coercion

* fix(browser): migrate native bootstrap coercion

* docs(plugin-sdk): clarify text record export compatibility

* fix(gateway): normalize approval execution identity

* test(outbound): isolate message action poll harness
2026-08-11 00:02:18 -07:00

182 lines
5.8 KiB
TypeScript

// Openai provider module implements model/runtime integration.
import { resolveExpiresAtMsFromEpochSeconds } from "openclaw/plugin-sdk/number-runtime";
import {
createProviderHttpError,
readProviderJsonResponse,
resolveProviderRequestHeaders,
} from "openclaw/plugin-sdk/provider-http";
import { captureWsEvent } from "openclaw/plugin-sdk/proxy-capture";
import { fetchWithSsrFGuard, type SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
import {
asOptionalRecord,
normalizeOptionalString,
} from "openclaw/plugin-sdk/string-coerce-runtime";
const OPENAI_REALTIME_API_BASE_URL = "https://api.openai.com/v1";
const OPENAI_REALTIME_SSRF_POLICY = {
allowRfc2544BenchmarkRange: true,
allowIpv6UniqueLocalRange: true,
hostnameAllowlist: [new URL(OPENAI_REALTIME_API_BASE_URL).hostname],
} satisfies SsrFPolicy;
// Secret minting blocks interactive Talk setup; keep this absolute budget aligned
// with the maintained realtime Talk live smoke.
const OPENAI_REALTIME_CLIENT_SECRET_REQUEST_TIMEOUT_MS = 30_000;
export function readRealtimeErrorDetail(error: unknown): string {
if (typeof error === "string" && error) {
return error;
}
const message = asOptionalRecord(error)?.message;
if (typeof message === "string" && message) {
return message;
}
return "Unknown error";
}
export function resolveOpenAIProviderConfigRecord(
config: Record<string, unknown>,
): Record<string, unknown> | undefined {
const providers = asOptionalRecord(config.providers);
return (
asOptionalRecord(providers?.openai) ??
asOptionalRecord(config.openai) ??
asOptionalRecord(config)
);
}
export function captureOpenAIRealtimeWsClose(params: {
url: string;
flowId: string;
capability: "realtime-transcription" | "realtime-voice";
code: unknown;
reasonBuffer: unknown;
}): void {
captureWsEvent({
url: params.url,
direction: "local",
kind: "ws-close",
flowId: params.flowId,
closeCode: typeof params.code === "number" ? params.code : undefined,
meta: {
provider: "openai",
capability: params.capability,
reason:
Buffer.isBuffer(params.reasonBuffer) && params.reasonBuffer.length > 0
? params.reasonBuffer.toString("utf8")
: undefined,
},
});
}
type OpenAIRealtimeClientSecretResult = {
value: string;
expiresAt?: number;
};
type OpenAIRealtimeSecretRequest = {
authToken: string;
auditContext: string;
url: string;
body: unknown;
errorMessage: string;
authRejectedMessage?: string;
missingValueMessage: string;
};
async function createOpenAIRealtimeSecret(
params: OpenAIRealtimeSecretRequest,
): Promise<OpenAIRealtimeClientSecretResult> {
const { response, release } = await fetchWithSsrFGuard({
url: params.url,
init: {
method: "POST",
headers: resolveProviderRequestHeaders({
provider: "openai",
baseUrl: params.url,
capability: "audio",
transport: "http",
defaultHeaders: {
Authorization: `Bearer ${params.authToken}`,
"Content-Type": "application/json",
},
}) ?? {
Authorization: `Bearer ${params.authToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(params.body),
},
policy: OPENAI_REALTIME_SSRF_POLICY,
timeoutMs: OPENAI_REALTIME_CLIENT_SECRET_REQUEST_TIMEOUT_MS,
auditContext: params.auditContext,
});
const payload = await (async () => {
try {
if (!response.ok) {
const error = await createProviderHttpError(response, params.errorMessage);
// Provider details can echo a masked credential while hiding which
// OpenClaw auth source won. Keep the status metadata, but give callers
// a bounded remediation for an explicitly configured key.
if (response.status === 401 && params.authRejectedMessage) {
error.message = params.authRejectedMessage;
}
throw error;
}
return await readProviderJsonResponse<unknown>(response, "openai.realtime-session");
} finally {
await release();
}
})();
const nestedSecret =
payload && typeof payload === "object"
? (payload as Record<string, unknown>).client_secret
: undefined;
const clientSecret =
normalizeOptionalString(asOptionalRecord(payload)?.value) ??
normalizeOptionalString(asOptionalRecord(nestedSecret)?.value);
if (!clientSecret) {
throw new Error(params.missingValueMessage);
}
const expiresAt =
payload && typeof payload === "object"
? (payload as Record<string, unknown>).expires_at
: undefined;
const expiresAtMs = resolveExpiresAtMsFromEpochSeconds(expiresAt);
return {
value: clientSecret,
...(expiresAtMs === undefined ? {} : { expiresAt: expiresAtMs }),
};
}
export async function createOpenAIRealtimeClientSecret(params: {
authToken: string;
auditContext: string;
session: Record<string, unknown>;
authRejectedMessage?: string;
}): Promise<OpenAIRealtimeClientSecretResult> {
const url = `${OPENAI_REALTIME_API_BASE_URL}/realtime/client_secrets`;
return createOpenAIRealtimeSecret({
...params,
url,
body: { session: params.session },
errorMessage: "OpenAI Realtime client secret failed",
missingValueMessage: "OpenAI Realtime client secret response did not include a value",
});
}
export async function createOpenAIRealtimeTranscriptionClientSecret(params: {
authToken: string;
auditContext: string;
session: Record<string, unknown>;
authRejectedMessage?: string;
}): Promise<OpenAIRealtimeClientSecretResult> {
const url = `${OPENAI_REALTIME_API_BASE_URL}/realtime/client_secrets`;
return createOpenAIRealtimeSecret({
...params,
url,
body: { session: params.session },
errorMessage: "OpenAI Realtime transcription client secret failed",
missingValueMessage:
"OpenAI Realtime transcription client secret response did not include a value",
});
}