UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

121 lines (120 loc) • 4.41 kB
import { f as listProfilesForProvider } from "./order-jOvee2J6.mjs"; import { n as fetchWithRuntimeDispatcher } from "./runtime-fetch-CQQovyS7.mjs"; import { A as requireApiKey } from "./model-auth-provider-config-Bnc4upRS.mjs"; import { i as ensureAuthProfileStoreAsync } from "./store-runtime-dW0pXLJI.mjs"; import "./runtime-fetch-C0XiZHRl.mjs"; import { a as resolveApiKeyForProvider } from "./provider-auth-runtime-DYG2Fn1g.mjs"; import "./provider-auth-D_oAZ3fr.mjs"; import { t as transcribeOpenAiCompatibleAudio } from "./media-understanding-DB4QZhdc.mjs"; import { n as normalizeClawRouterRootUrl } from "./base-url-N0YLRp3a.mjs"; //#region extensions/clawrouter/audio-transcription.ts const MODEL = "gpt-4o-transcribe"; async function prepare(e) { e.signal?.throwIfAborted(); const t = e.model ?? MODEL; if (t !== MODEL && t !== "openai/" + MODEL) throw new Error("ClawRouter transcription supports only gpt-4o-transcribe"); if (e.request?.auth && e.request.auth.mode !== "provider-default") throw new Error("ClawRouter transcription requires provider-default request auth"); for (const t of [...Object.keys(e.headers ?? {}), ...Object.keys(e.request?.headers ?? {})]) if ([ "authorization", "x-api-key", "cookie", "content-type", "content-length" ].includes(t.toLowerCase())) throw new Error("ClawRouter transcription does not accept credential or multipart framing header overrides"); const n = e.baseUrl ?? e.cfg.models?.providers?.clawrouter?.baseUrl; const r = normalizeClawRouterRootUrl(n); const i = r.endsWith("/v1/native/openai") ? `${r}/v1` : `${r}/v1/native/openai/v1`; let a; if (e.profile) { a = await ensureAuthProfileStoreAsync(e.agentDir, { config: e.cfg, readOnly: true, allowKeychainPrompt: false }); if (!listProfilesForProvider(a, "clawrouter").includes(e.profile)) throw new Error("Selected transcription auth profile is unavailable for ClawRouter"); } const o = await resolveApiKeyForProvider({ provider: "clawrouter", cfg: e.cfg, agentDir: e.agentDir, workspaceDir: e.workspaceDir, store: a, profileId: e.profile, preferredProfile: e.preferredProfile, lockedProfile: Boolean(e.profile), modelId: t, modelBaseUrl: i, capability: "audio-transcription", signal: e.signal }); e.signal?.throwIfAborted(); if (o.profileId) { a ??= await ensureAuthProfileStoreAsync(e.agentDir, { config: e.cfg, readOnly: true, allowKeychainPrompt: false }); if (!listProfilesForProvider(a, "clawrouter").includes(o.profileId)) throw new Error("Resolved transcription credential does not belong to ClawRouter"); } if (o.mode !== "api-key") throw new Error("ClawRouter transcription requires a ClawRouter API key"); return { model: t, baseUrl: i, apiKey: requireApiKey(o, "clawrouter"), source: o.source }; } const clawrouterMediaUnderstandingProvider = { id: "clawrouter", capabilities: ["audio"], inheritConfiguredImageCapability: true, sharedEntryCapabilities: ["image"], defaultModels: { audio: MODEL }, async isAudioConfigured(e) { try { await prepare(e); return true; } catch { return false; } }, async transcribeAudioWithContext(e) { let t = false; try { const n = await prepare(e); if (e.query && Object.keys(e.query).length > 0) throw new Error("ClawRouter transcription does not support providerOptions; use language and prompt only"); if (e.buffer.length === 0 || e.buffer.length > 25e6) throw new Error("ClawRouter transcription requires 1-25000000 file bytes"); if (!/^[A-Za-z0-9][A-Za-z0-9 ._()-]{0,126}$/.test(e.fileName) || e.fileName.includes("..") || e.fileName !== e.fileName.trim() || /\.aac$/i.test(e.fileName) || e.mime === "audio/aac") throw new Error("ClawRouter transcription requires a safe upload filename (AAC relabeling is unsupported)"); return { ok: true, value: await transcribeOpenAiCompatibleAudio({ ...e, ...n, provider: "clawrouter", defaultModel: MODEL, defaultBaseUrl: n.baseUrl, auth: { kind: "api-key", apiKey: n.apiKey, source: n.source }, fetchFn: (n, r) => { t = true; return (e.fetchFn ?? fetchWithRuntimeDispatcher)(n, { ...r, redirect: "error" }); } }) }; } catch (e) { if (t) throw e; return { ok: false, error: e }; } } }; //#endregion export { clawrouterMediaUnderstandingProvider as t };