openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
108 lines (107 loc) • 3.85 kB
JavaScript
import { o as hasConfiguredSecretInput } from "../../types.secrets-BmNLLnHp.mjs";
import { n as findNormalizedProviderValue } from "../../provider-id-b5XGSf9C.mjs";
import { A as requireApiKey, k as isProviderAuthError } from "../../model-auth-provider-config-Bnc4upRS.mjs";
import { r as providerOperationRetryConfig } from "../../operation-retry-CNR4uE0x.mjs";
import { n as executeWithApiKeyRotation, t as collectProviderApiKeysForExecution } from "../../api-key-rotation-CSR2KXjS.mjs";
import { a as resolveApiKeyForProvider } from "../../provider-auth-runtime-DYG2Fn1g.mjs";
import "../../provider-http-LcawLneq.mjs";
import "../../provider-auth-D_oAZ3fr.mjs";
import { t as transcribeOpenAiCompatibleAudio } from "../../media-understanding-DB4QZhdc.mjs";
import { i as classifyOpenAIBaseUrl, t as OPENAI_API_BASE_URL } from "../../base-url-C__ME3gK.mjs";
import { n as OPENAI_DEFAULT_AUDIO_TRANSCRIPTION_MODEL } from "../../default-models-DkmAqILC.mjs";
//#region extensions/openai/audio-transcription.ts
async function transcribeOpenAiAudio(e) {
return await transcribeOpenAiCompatibleAudio({
...e,
provider: "openai",
defaultBaseUrl: OPENAI_API_BASE_URL,
defaultModel: OPENAI_DEFAULT_AUDIO_TRANSCRIPTION_MODEL
});
}
async function resolveOpenAiAudioAuth(e) {
const t = findNormalizedProviderValue(e.cfg.models?.providers, "openai");
const n = hasConfiguredSecretInput(t?.apiKey, e.cfg.secrets?.defaults);
const r = classifyOpenAIBaseUrl(e.baseUrl);
const i = r === "unresolved" || r === "platform" || r === "chatgpt";
const a = {
provider: "openai",
capability: "audio-transcription",
cfg: !e.profile && t && n && (!t.auth || t.auth === "api-key") ? {
...e.cfg,
models: {
...e.cfg.models,
providers: {
...e.cfg.models?.providers,
openai: {
...t,
auth: "api-key"
}
}
}
} : e.cfg,
agentDir: e.agentDir,
workspaceDir: e.workspaceDir,
profileId: e.profile,
preferredProfile: e.preferredProfile,
lockedProfile: Boolean(e.profile)
};
const o = t?.auth === "oauth" || t?.auth === "token";
let s;
let c;
try {
s = await resolveApiKeyForProvider({
...a,
modelApi: e.profile || o ? void 0 : "openai-audio-transcriptions"
}).catch((t) => {
if (e.profile || n || !i || !isProviderAuthError(t, "missing-provider-auth")) throw t;
return resolveApiKeyForProvider(a);
});
c = requireApiKey(s, "openai");
if (s.mode !== "api-key") {
if (s.mode !== "oauth" && s.mode !== "token") throw new Error("OpenAI audio transcription requires an API key or OAuth profile.");
if (!i || e.headers || e.request) throw new Error("OpenAI OAuth audio transcription requires the official endpoint without custom request overrides. Remove the overrides or select an OpenAI API-key profile.");
}
} catch (e) {
return {
ok: false,
error: e
};
}
return {
ok: true,
value: {
auth: s,
credential: c,
nativeEndpoint: i
}
};
}
const isOpenAiAudioConfigured = async (e) => (await resolveOpenAiAudioAuth(e)).ok;
const transcribeOpenAiAudioWithContext = async (e) => {
const t = await resolveOpenAiAudioAuth(e);
if (!t.ok) return t;
const { auth: n, credential: r, nativeEndpoint: i } = t.value;
const transcribe = (t) => transcribeOpenAiAudio({
...e,
baseUrl: i ? OPENAI_API_BASE_URL : e.baseUrl,
apiKey: t,
...n.mode === "api-key" ? { auth: {
kind: "api-key",
apiKey: t
} } : {}
});
return {
ok: true,
value: n.mode === "api-key" ? await executeWithApiKeyRotation({
provider: "openai",
apiKeys: collectProviderApiKeysForExecution({
provider: "openai",
primaryApiKey: r
}),
transientRetry: providerOperationRetryConfig("read"),
execute: transcribe
}) : await transcribe(r)
};
};
//#endregion
export { isOpenAiAudioConfigured, transcribeOpenAiAudio, transcribeOpenAiAudioWithContext };