openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
233 lines (232 loc) • 9.66 kB
JavaScript
import { g as normalizeUniqueStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { a as getRuntimeConfigSnapshot } from "./runtime-snapshot-WbwbY5b7.mjs";
import { a as resolveMergedModelProviderConfig } from "./model-provider-config-Bbah_2ON.mjs";
import { s as resolveProviderAuthLookupMaps } from "./provider-env-vars-DPvuIf4v.mjs";
import { a as isAuthModeAllowedForModel } from "./model-runtime-policy-Cfudvy7O.mjs";
import { c as mintSecretSentinel } from "./sentinel-BfPjneyJ.mjs";
import "./config-CMA-J084.mjs";
import { d as isSecretRefHeaderValueMarker, l as isNonSecretApiKeyMarker, n as CUSTOM_LOCAL_AUTH_MARKER, p as resolveExplicitProviderNoAuth } from "./model-auth-markers-4YSUG71a.mjs";
import { f as pluginProviderAuthUnavailable, g as resolveManagedSecretRefRuntimeProviderAuth, m as resolveProviderAuthScope } from "./loader-runtime-load-Ba16vW6K.mjs";
import { t as resolveEnvApiKey } from "./model-auth-env-kbpGo8fU.mjs";
import { b as resolveUsableCustomProviderApiKey, h as resolveProviderAuthOverride, i as hasSyntheticLocalProviderAuthConfig, m as resolveInlineProviderApiKeyCooldownUntil, o as isConfigBackedInlineProviderApiKey, r as hasSecretRefProviderApiKey, s as isManagedSecretRefApiKeyMarker } from "./model-auth-provider-config-Bnc4upRS.mjs";
import { C as resolveProviderSyntheticAuthWithPlugin, h as prepareProviderSyntheticAuthWithPlugin } from "./provider-runtime-BddjXkmv.mjs";
import { r as resolveRuntimeSyntheticAuthProviderRefState } from "./synthetic-auth.runtime.js";
//#region src/agents/model-auth-runtime.ts
function createRuntimeProviderAuthLookup(e) {
const t = e.env ?? process.env;
const n = {
config: e.cfg,
workspaceDir: e.workspaceDir,
env: t
};
const r = e.includePluginSyntheticAuth === false ? void 0 : resolveRuntimeSyntheticAuthProviderRefState(n);
const i = resolveProviderAuthLookupMaps(n);
return {
envApiKey: {
aliasMap: i.aliasMap,
candidateMap: i.envCandidateMap,
authEvidenceMap: i.authEvidenceMap,
skipSetupProviderFallback: true
},
setupProviderFallbackRefs: i.setupProviderFallbackRefs,
syntheticAuthProviderRefs: r?.complete ? r.refs : void 0
};
}
function runtimeLookupAllowsSetupProviderFallback(e) {
const t = e.runtimeLookup?.setupProviderFallbackRefs;
if (!t?.length) return false;
const n = normalizeProviderId(e.provider);
const r = e.runtimeLookup?.envApiKey.aliasMap?.[n];
return t.includes(n) || (r ? t.includes(r) : false);
}
function resolveRuntimeEnvApiKeyLookupOptions(e) {
const t = e.runtimeLookup?.envApiKey;
if (!t) return;
const n = t.skipSetupProviderFallback === true ? !runtimeLookupAllowsSetupProviderFallback(e) : t.skipSetupProviderFallback;
return {
...t,
...n !== void 0 ? { skipSetupProviderFallback: n } : {}
};
}
function listProviderSyntheticAuthRefs(e) {
const t = [e.provider];
const n = resolveMergedModelProviderConfig(e.cfg, e.provider);
if (n?.api) t.push(n.api);
return normalizeUniqueStringEntries(t.map((e) => normalizeProviderId(e)));
}
function shouldResolvePluginSyntheticAuth(e) {
const t = e.runtimeLookup?.syntheticAuthProviderRefs;
if (!t) return true;
const n = new Set(normalizeUniqueStringEntries(t.map((e) => normalizeProviderId(e))));
if (n.size === 0) return false;
return listProviderSyntheticAuthRefs(e).some((e) => n.has(e));
}
function resolveRuntimeAvailableProviderAuth(e, t) {
const n = normalizeProviderId(e.provider);
if (resolveProviderAuthScope({
...e,
config: e.cfg
}) === "plugin") return e.allowPluginSyntheticAuth === false ? false : t(n);
const r = resolveProviderAuthOverride(e.cfg, n);
if (r === "aws-sdk") return true;
if (r === "none") return hasSyntheticLocalProviderAuthConfig({
...e,
cfg: e.cfg,
provider: n
});
const i = e.store ? resolveInlineProviderApiKeyCooldownUntil(e.store, n) : null;
const a = i === null || i <= Date.now();
const isUsableSource = (t) => !isConfigBackedInlineProviderApiKey({
cfg: e.cfg,
provider: n,
source: t,
store: e.store
}) || a;
const o = resolveEnvApiKey(n, e.env, {
config: e.cfg,
workspaceDir: e.workspaceDir,
...resolveRuntimeEnvApiKeyLookupOptions({
provider: n,
runtimeLookup: e.runtimeLookup
})
});
if (o && isAuthModeAllowedForModel({
provider: n,
modelApi: e.modelApi,
capability: e.capability,
mode: o.source.includes("OAUTH_TOKEN") ? "oauth" : "api-key"
}) && isUsableSource(o.source)) return true;
if (resolveUsableCustomProviderApiKey({
cfg: e.cfg,
provider: n,
env: e.env
}) && a && (!e.capability || isAuthModeAllowedForModel({
provider: n,
capability: e.capability,
mode: "api-key"
}))) return true;
const s = resolveManagedSecretRefRuntimeProviderAuth({
cfg: e.cfg,
provider: n
});
if (s && (!e.capability || isAuthModeAllowedForModel({
provider: n,
capability: e.capability,
mode: s.mode,
authFlow: s.authFlow
})) && isUsableSource(s.source)) return true;
if (hasSyntheticLocalProviderAuthConfig({
cfg: e.cfg,
provider: n
})) return true;
if (e.allowPluginSyntheticAuth !== false && shouldResolvePluginSyntheticAuth({
cfg: e.cfg,
provider: n,
runtimeLookup: e.runtimeLookup
})) return t(n);
return false;
}
function hasRuntimeAvailableProviderAuth(e) {
return resolveRuntimeAvailableProviderAuth(e, (t) => Boolean(resolveSyntheticLocalProviderAuth({
cfg: e.cfg,
provider: t,
workspaceDir: e.workspaceDir,
env: e.env
})));
}
/** Prepare external auth only after immediate credentials and discovery scope permit it. */
async function prepareRuntimeAvailableProviderAuth(e) {
e.signal?.throwIfAborted();
return resolveRuntimeAvailableProviderAuth(e, async (t) => Boolean(await prepareSyntheticLocalProviderAuth({
...e,
cfg: e.cfg,
provider: t
})));
}
function syntheticAuthLookup(e, t) {
return {
provider: e.provider,
config: t,
workspaceDir: e.workspaceDir,
env: e.env,
modelApi: e.modelApi,
context: {
config: t,
provider: e.provider,
providerConfig: resolveMergedModelProviderConfig(t, e.provider)
}
};
}
function resolveNonSecretSyntheticProviderAuth(e, t = (t) => resolveProviderSyntheticAuthWithPlugin(syntheticAuthLookup(e, t))) {
const n = t(e.cfg);
return n?.mode === "api-key" && !n.nativeAuth && isNonSecretApiKeyMarker(n.apiKey, { includeEnvVarName: false }) && !isSecretRefHeaderValueMarker(n.apiKey) ? n : void 0;
}
/** A managed process owns auth only when its provider supplies a non-secret credential. */
function resolveManagedLocalProviderAuth(e, t) {
return resolveMergedModelProviderConfig(e.cfg, e.provider)?.localService ? resolveNonSecretSyntheticProviderAuth(e, t) : void 0;
}
function resolveProviderSyntheticRuntimeAuth(e, t = (t) => resolveProviderSyntheticAuthWithPlugin(syntheticAuthLookup(e, t))) {
if (resolveProviderAuthScope({
...e,
config: e.cfg
}) === "plugin") return { auth: t(e.cfg) };
const n = resolveManagedLocalProviderAuth(e, t);
if (n) return { auth: n };
const r = resolveManagedSecretRefRuntimeProviderAuth(e);
if (r) return { auth: r };
if (hasSecretRefProviderApiKey(e.cfg, e.provider)) return { blockedOnManagedSecretRef: true };
const i = t(e.cfg);
if (!i) return {};
if (!isManagedSecretRefApiKeyMarker(i.apiKey)) return { auth: i };
const a = getRuntimeConfigSnapshot();
if (!a || a === e.cfg) return { blockedOnManagedSecretRef: true };
const o = t(a);
const s = o?.apiKey;
if (!o || !s || isNonSecretApiKeyMarker(s)) return { blockedOnManagedSecretRef: true };
return { auth: {
...o,
apiKey: e.secretSentinels ? mintSecretSentinel(s, { label: `model-auth:${e.provider}` }) : s
} };
}
/** Prepare native readiness without widening explicit managed-credential authority. */
async function prepareSyntheticLocalProviderAuth(e) {
e.signal?.throwIfAborted();
if (resolveProviderAuthScope({
...e,
config: e.cfg
}) === "plugin") return resolveSyntheticLocalProviderAuth(e);
const t = resolveExplicitProviderNoAuth({
provider: e.provider,
providerConfig: resolveMergedModelProviderConfig(e.cfg, e.provider),
modelApi: e.modelApi
});
if (t) return t;
if (e.allowPluginSyntheticAuth === false || hasSecretRefProviderApiKey(e.cfg, e.provider)) return resolveSyntheticLocalProviderAuth(e);
const prepare = (t) => prepareProviderSyntheticAuthWithPlugin({
...syntheticAuthLookup(e, t),
signal: e.signal
});
const n = await prepare(e.cfg);
const r = getRuntimeConfigSnapshot();
const i = n && isManagedSecretRefApiKeyMarker(n.apiKey) && r && r !== e.cfg ? await prepare(r) : void 0;
return resolveSyntheticLocalProviderAuth(e, (t) => t === e.cfg ? n : t === r ? i : void 0);
}
function resolveSyntheticLocalProviderAuth(e, t) {
if (e.allowPluginSyntheticAuth === false && resolveProviderAuthScope({
...e,
config: e.cfg
}) === "plugin") throw pluginProviderAuthUnavailable(e.provider, "prepared route excludes plugin credentials");
if (e.allowPluginSyntheticAuth === false && hasSecretRefProviderApiKey(e.cfg, e.provider)) return null;
const n = e.allowPluginSyntheticAuth === false ? { auth: resolveNonSecretSyntheticProviderAuth(e, t) } : resolveProviderSyntheticRuntimeAuth(e, t);
if (n.auth) return n.auth;
if (n.blockedOnManagedSecretRef) return null;
if (hasSyntheticLocalProviderAuthConfig(e)) return {
apiKey: CUSTOM_LOCAL_AUTH_MARKER,
source: `models.providers.${e.provider} (synthetic local key)`,
mode: "api-key"
};
return null;
}
//#endregion
export { resolveManagedLocalProviderAuth as a, prepareSyntheticLocalProviderAuth as i, hasRuntimeAvailableProviderAuth as n, prepareRuntimeAvailableProviderAuth as r, createRuntimeProviderAuthLookup as t };