UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

337 lines (336 loc) • 10.6 kB
import { t as appendConfigPathSegment } from "./dot-path-BJ4tyWKJ.mjs"; import { _ as secretRefKey } from "./ref-contract-b8L-9DrQ.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { p as resolveConfigSecretRef } from "./resolution-facts-xaIKuREs.mjs"; import { s as resolveProviderAuthLookupMaps } from "./provider-env-vars-DPvuIf4v.mjs"; import { i as isOAuthRefreshFence } from "./oauth-refresh-marker-Bm1Qb0ET.mjs"; import { n as SecretSurfaceUnavailableError } from "./runtime-degraded-state-BSnwk5PQ.mjs"; import { a as resolveAuthProfileOrder, i as resolveAuthProfileEligibility } from "./order-jOvee2J6.mjs"; import { r as resolveNonEnvSecretRefApiKeyMarker } from "./provider-credential-values-pLjdcwQh.mjs"; import { c as isKnownEnvApiKeyMarker, l as isNonSecretApiKeyMarker, m as resolveOAuthApiKeyMarker } from "./model-auth-markers-4YSUG71a.mjs"; import "./model-auth-env-kbpGo8fU.mjs"; import { M as resolveDirectProviderCredentialMode, S as shouldPreferExplicitConfigApiKeyAuth, g as resolveProviderConfigSecretInput, v as resolveProviderEntryApiKeyProfileReference } from "./model-auth-provider-config-Bnc4upRS.mjs"; import { C as resolveProviderSyntheticAuthWithPlugin } from "./provider-runtime-BddjXkmv.mjs"; import { a as resolveApiKeyFromCredential, l as toDiscoveryApiKey, o as resolveApiKeyFromProfiles, s as resolveEnvApiKeyVarName } from "./models-config.providers.secret-helpers-D-ksd1i8.mjs"; //#region src/agents/models-config.providers.secrets.ts /** * Provider auth resolution entry points used during model config generation. * The resolvers return env/profile/config marker values so discovery can prove * auth availability without writing secret material into generated config. */ function resolveCatalogProviderEntryAuth(e) { const t = resolveProviderConfigSecretInput(e.config, e.provider, e.sourceConfigForSecrets); if (t.ref || typeof t.providerConfig?.apiKey !== "string") return; const n = e.authStore; const r = resolveProviderEntryApiKeyProfileReference({ cfg: e.config, sourceConfig: e.sourceConfigForSecrets, provider: e.provider, store: n }); if (r.kind === "profile-incompatible") throw new Error(`Per-entry apiKey "${r.profileId}" for provider "${e.provider}" references an incompatible auth profile.`); if (r.kind !== "profile") return; if (!resolveAuthProfileEligibility({ cfg: e.config, store: n, provider: r.credential.provider, profileId: r.profileId, authAliasLookupParams: { config: e.config, env: e.env } }).eligible) throw new Error(`Per-entry apiKey "${r.profileId}" for provider "${e.provider}" matched an unusable stored profile.`); const i = resolveApiKeyFromCredential(r.credential, e.env); if (!i) throw new Error(`Per-entry apiKey "${r.profileId}" for provider "${e.provider}" matched a stored profile but failed to resolve.`); return { apiKey: i.apiKey, discoveryApiKey: i.discoveryApiKey, mode: r.credential.type === "token" ? "token" : "api_key", source: "profile", profileId: r.profileId }; } function resolveCatalogAuthProfileOrder(e) { return resolveAuthProfileOrder({ cfg: e.config, provider: e.provider, store: e.store, authAliasLookupParams: { config: e.config, env: e.env }, cooldownScope: "all-models", readinessMode: "read-only" }); } function resolveCatalogDirectAuthMode(e, t) { const n = resolveDirectProviderCredentialMode({ cfg: e, provider: t, inferredMode: "api-key" }); return n === "oauth" || n === "token" ? n : "api_key"; } /** Create a resolver over the credential map already selected for one lifecycle generation. */ function createProviderApiKeyResolverFromPreparedCredentials(e, t, n, r) { const i = createProviderApiKeyResolver(e, { version: 1, profiles: {} }, n, void 0, r); const a = createProviderAuthLookupCaches(e, n); return (e) => { const n = resolveProviderIdForAuthFromCaches(e, a()); const r = t[n]; if (!r) return i(e); if (r.type === "oauth") { if (isOAuthRefreshFence(r)) return i(e); return { apiKey: resolveOAuthApiKeyMarker(n), discoveryApiKey: toDiscoveryApiKey(r.access), mode: "oauth" }; } const o = r.type === "token" ? r.token : r.key; if (!o.trim() || r.type === "token" && r.expires !== void 0 && Date.now() >= r.expires) return i(e); return { apiKey: o, discoveryApiKey: toDiscoveryApiKey(o), mode: r.type }; }; } function createProviderAuthLookupCaches(e, t) { let n; return () => n ??= resolveProviderAuthLookupMaps({ config: t, env: e }); } function resolveProviderIdForAuthFromCaches(e, t) { const n = normalizeProviderId(e); if (!n) return n; return t.aliasMap[n] ?? n; } /** Create a resolver that returns redacted API-key markers for provider discovery. */ function createProviderApiKeyResolver(e, t, n, r, i, a = e) { const o = createProviderAuthLookupCaches(e, n); return (s) => { const c = o(); const l = resolveProviderIdForAuthFromCaches(s, c); const u = resolveDirectCatalogAuth({ provider: l, config: n, env: e, lookupCaches: c, sourceConfigForSecrets: r, workspaceDir: i, syntheticAuthEnv: a, authStore: t }); if (u?.apiKey) return { apiKey: u.apiKey, discoveryApiKey: u.discoveryApiKey, mode: u.mode, ...u.profileId ? { profileId: u.profileId } : {} }; const d = resolveApiKeyFromProfiles({ provider: l, store: t, env: e, profileIds: resolveCatalogAuthProfileOrder({ config: n, env: e, provider: s, store: t }) }); return d?.apiKey ? { apiKey: d.apiKey, discoveryApiKey: d.discoveryApiKey, profileId: d.profileId, mode: d.mode } : { apiKey: void 0, discoveryApiKey: void 0 }; }; } /** Create a resolver that reports provider auth mode and provenance. */ function createProviderAuthResolver(e, t, n, r, i, a = e) { const o = createProviderAuthLookupCaches(e, n); return (s, c) => { const l = o(); const u = resolveProviderIdForAuthFromCaches(s, l); const d = resolveCatalogProviderEntryAuth({ provider: u, config: n, sourceConfigForSecrets: r, authStore: t, env: e }); if (d) return c?.excludeProfileIds?.includes(d.profileId ?? "") ? { apiKey: void 0, mode: "none", source: "none" } : { ...d, source: "profile" }; const f = resolveProviderConfigSecretInput(n, u, r); if (f.ref || typeof f.providerConfig?.apiKey === "string" && isKnownEnvApiKeyMarker(f.providerConfig.apiKey) || shouldPreferExplicitConfigApiKeyAuth(n, u)) { const o = resolveConfigBackedProviderAuth({ provider: u, config: n, sourceConfigForSecrets: r, authStore: t, env: e, workspaceDir: i, syntheticAuthEnv: a }); return o ? { ...o, source: o.source ?? "none" } : { apiKey: void 0, mode: "none", source: "none" }; } const p = new Set(c?.excludeProfileIds); const m = resolveCatalogAuthProfileOrder({ config: n, env: e, provider: s, store: t }); for (const n of m) { if (p.has(n)) continue; const r = t.profiles[n]; if (!r) continue; if (r.type === "oauth") { if (isOAuthRefreshFence(r)) continue; return { apiKey: c?.oauthMarker, discoveryApiKey: toDiscoveryApiKey(r.access), mode: "oauth", ...r.authFlow ? { authFlow: r.authFlow } : {}, source: "profile", profileId: n }; } const i = resolveApiKeyFromCredential(r, e); if (!i) continue; return { apiKey: i.apiKey, discoveryApiKey: i.discoveryApiKey, mode: r.type, source: "profile", profileId: n }; } return resolveDirectCatalogAuth({ provider: u, config: n, env: e, lookupCaches: l, sourceConfigForSecrets: r, workspaceDir: i, syntheticAuthEnv: a, authStore: t }) ?? { apiKey: void 0, discoveryApiKey: void 0, mode: "none", source: "none" }; }; } function resolveDirectCatalogAuth(e) { const t = resolveEnvApiKeyVarName(e.provider, e.env, { aliasMap: e.lookupCaches.aliasMap, candidateMap: e.lookupCaches.envCandidateMap, authEvidenceMap: e.lookupCaches.authEvidenceMap }); const n = t ? { apiKey: t, discoveryApiKey: toDiscoveryApiKey(e.env[t]), mode: resolveCatalogDirectAuthMode(e.config, e.provider) } : resolveConfigBackedProviderAuth(e); return n ? { apiKey: n.apiKey, discoveryApiKey: n.discoveryApiKey, mode: n.mode, source: t ? "env" : n.source ?? "none", ...n.profileId ? { profileId: n.profileId } : {} } : void 0; } function resolveConfigBackedProviderAuth(e) { const t = e.provider; const n = resolveCatalogDirectAuthMode(e.config, t); const r = `${appendConfigPathSegment("models.providers", t)}.apiKey`; const i = resolveConfigSecretRef({ config: e.sourceConfigForSecrets, path: r, value: e.sourceConfigForSecrets?.models?.providers?.[t]?.apiKey, defaults: e.sourceConfigForSecrets?.secrets?.defaults }); if (i && i.source !== "env") { const a = e.config?.models?.providers?.[t]?.apiKey; if (typeof a !== "string" || !a.trim()) throw new SecretSurfaceUnavailableError({ ownerKind: "provider", ownerId: t, state: "unavailable", paths: [r], refKeys: [secretRefKey(i)], reason: "secret reference was not materialized by the active runtime" }); return { apiKey: resolveNonEnvSecretRefApiKeyMarker(i.source), discoveryApiKey: a, mode: n }; } const a = resolveCatalogProviderEntryAuth({ ...e, env: e.env ?? process.env }); if (a) return a; const o = resolveProviderSyntheticAuthWithPlugin({ provider: t, config: e.config, env: e.syntheticAuthEnv ?? e.env, workspaceDir: e.workspaceDir, context: { config: e.config, provider: t, providerConfig: e.config?.models?.providers?.[t] } })?.apiKey?.trim(); if (o) return { apiKey: isNonSecretApiKeyMarker(o) ? o : resolveNonEnvSecretRefApiKeyMarker("file"), discoveryApiKey: toDiscoveryApiKey(o), mode: n }; const s = (e.config?.models?.providers?.[t])?.apiKey; const c = resolveConfigSecretRef({ config: e.config, path: r, value: s, defaults: e.config?.secrets?.defaults }); if (c && c.source !== "env") return { apiKey: resolveNonEnvSecretRefApiKeyMarker(c.source), mode: n }; const l = c?.id.trim() ?? (typeof s === "string" ? s.trim() : void 0); if (l === void 0 || !c && !l) return; const u = c || isKnownEnvApiKeyMarker(l) ? e.env?.[l]?.trim() : l; if (!u) return; return { apiKey: l, discoveryApiKey: toDiscoveryApiKey(u), mode: n }; } //#endregion export { createProviderApiKeyResolverFromPreparedCredentials as n, createProviderAuthResolver as r, createProviderApiKeyResolver as t };