UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

214 lines (213 loc) • 7.67 kB
import { n as findNormalizedProviderValue, r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { F as resolveRegisteredAgentIdForDir } from "./agent-scope-config-DmXgFGKP.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { i as resolveProviderIdForAuth } from "./provider-auth-aliases-CgGlzANu.mjs"; import { r as matchesProviderPluginRef } from "./provider-registry-shared-C3T4TYXW.mjs"; import { f as isTrustedSecretSurfaceUnavailableError } from "./runtime-degraded-state-BSnwk5PQ.mjs"; import { o as buildOAuthRefreshFailureLoginCommand } from "./oauth-refresh-failure-DxrQiYzV.mjs"; import { i as withGuardedFetchRequestAuthority } from "./fetch-request-authority-nK0cxa_-.mjs"; import { r as PreparedModelRuntimePublicationSupersededError } from "./prepared-model-runtime-admission-errors-LxrcCg4d.mjs"; import { n as normalizePluginDiscoveryResult } from "./provider-discovery-C1BnGfVa.mjs"; import "./prepared-model-runtime.errors-BRsU9-dm.mjs"; import { t as captureProviderCatalogExpiries } from "./provider-catalog-expiry-NbtawN2q.mjs"; import { n as withCurrentReadAuthority } from "./current-read-authority-BoawkY8k.mjs"; //#region src/agents/models-config.providers.catalog-context.ts const log = createSubsystemLogger("agents/model-providers"); function buildPluginCatalogConfig(e, t) { const n = { ...e.config?.models?.providers, ...e.explicitProviders }; if (Object.keys(n).length === 0) return e.config ?? {}; for (const [r, i] of Object.entries(n)) { const a = findNormalizedProviderValue(e.discoveryAuthConfig?.models?.providers, r); if (a && matchesProviderPluginRef(t, r)) n[r] = { ...i, headers: a.headers, request: a.request }; } return { ...e.config, models: { ...e.config?.models, providers: n } }; } async function prepareProviderCatalogRun(e) { const { authStore: t, isActive: n, preparationAuthority: r, ...i } = e; if (!e.provider.auth.some((e) => e.kind === "oauth" || e.kind === "device_code") || e.providerIds !== void 0 && !e.providerIds.some((t) => matchesProviderPluginRef(e.provider, t))) return i; const a = resolveRegisteredAgentIdForDir(e.agentDir, e.env); const { prepareProviderCatalogOAuthAuth: o } = await import("./models-config.providers.discovery-auth.runtime.js"); const s = /* @__PURE__ */ new Set(); const c = []; const { resolveProviderAuth: l, failures: u } = await withCurrentReadAuthority(r, () => o({ agentDir: e.agentDir, authStore: t, env: e.env, provider: e.provider.id, resolveProviderAuth: e.resolveProviderAuth, isActive: n, onPreparationFailure: (e) => { for (const t of e) s.add(t); } }, e.config)); await withCurrentReadAuthority(r, () => {}); return { ...i, reportCatalogOutcome: (t) => { c.push({ ...t }); e.reportCatalogOutcome?.(t); }, resolveProviderAuth: l, finalizeCatalogResult: (t) => { if (s.size === 0 && u.length === 0) return t; const n = normalizePluginDiscoveryResult({ provider: e.provider, result: t }); const r = [...new Set(Object.values(n).flatMap(({ baseUrl: e }) => { const t = URL.parse(e)?.origin; return t ? [t] : []; }))]; const i = r.length ? `the live catalog returned ${r.join(", ")}` : "no live provider catalog was returned"; for (const t of u) { const n = buildOAuthRefreshFailureLoginCommand(e.provider.id, { profileId: t.profileId, agentId: a }); log.warn(`${e.provider.id}: OAuth profile ${JSON.stringify(t.profileId)} could not be resolved (${t.message}); ${i}. Re-authenticate with ${n}.`); } if (s.size > 0) { const t = new Set(c.map((e) => normalizeProviderId(e.provider))); const r = { config: e.config, env: e.env }; const i = resolveProviderIdForAuth(e.provider.id, r); for (const a of e.providerIds ?? [e.provider.id]) { const o = normalizeProviderId(a); if (resolveProviderIdForAuth(a, r) !== i || n[o] || t.has(o)) continue; for (const t of s) { const n = { provider: a, profileId: t, status: "unavailable" }; c.push(n); e.reportCatalogOutcome?.(n); } } } return t ? { providers: n, outcomes: c } : t; } }; } async function reportProviderCatalogSecretFailure(e, t) { if (!isTrustedSecretSurfaceUnavailableError(e)) return false; const { resolveUnavailableDiscoveryAuthProfileId: n } = await import("./models-config.providers.discovery-auth.runtime.js"); const r = n(e); for (const e of t.providerIds ?? [t.provider.id]) t.reportCatalogOutcome?.({ provider: e, ...r ? { profileId: r } : {}, status: "unavailable" }); return true; } /** An authorized selected profile reuses normal bounded provider discovery without failover. */ async function loadSelectedProviderAccountCatalog(e) { const { providerId: t, profileId: n, authStore: r } = e; const assertCurrent = () => { e.assertCurrent(); if (!e.isCurrent()) throw new PreparedModelRuntimePublicationSupersededError("Selected account catalog changed"); }; const i = { assertCurrent, withCurrent: e.withCurrent }; const a = r.profiles[n]; if (!a) return []; const [{ runProviderCatalogWithTimeout: o }, { createProviderAuthResolver: s }] = await Promise.all([import("./models-config.providers.implicit-BQ9tXXAi.mjs"), import("./models-config.providers.secrets-CgMEkr1d.mjs")]); await withCurrentReadAuthority(i, () => {}); const c = { ...r, profiles: { [n]: a } }; const l = resolveProviderIdForAuth(t, { config: e.config }); const u = Object.fromEntries(Object.entries(e.config.models?.providers ?? {}).map(([t, n]) => { if (resolveProviderIdForAuth(t, { config: e.config }) !== l) return [t, n]; const { apiKey: r, auth: i, ...a } = n; return [t, a]; })); const d = { ...e.config, models: { ...e.config.models, providers: u }, auth: { ...e.config.auth, order: { ...e.config.auth?.order, [t]: [n] } } }; const f = await withCurrentReadAuthority(i, () => s(process.env, c, d)(t)); const lockedAuth = (e, t) => { assertCurrent(); if (f.profileId !== n || t?.excludeProfileIds?.includes(n) || resolveProviderIdForAuth(e, { config: d }) !== l) return { apiKey: void 0, mode: "none", source: "none", preparationFailed: true }; return { ...f, ...f.mode === "oauth" ? { apiKey: t?.oauthMarker } : {} }; }; const p = []; const m = e.provider.catalog; const h = { provider: { ...e.provider, ...m ? { catalog: { ...m, run: async (e) => { const t = await withCurrentReadAuthority(i, () => m.run(e)); return withCurrentReadAuthority(i, () => t); } } } : {} }, preparationAuthority: i, providerIds: [t], config: e.config, agentDir: e.agentDir, workspaceDir: e.workspaceDir, env: process.env, authStore: c, timeoutMs: 5e3, resolveProviderAuth: (e, n) => lockedAuth(e ?? t, n), resolveProviderApiKey: (e) => { const { mode: n, ...r } = lockedAuth(e ?? t); return { ...r, ...n === "api_key" || n === "oauth" || n === "token" ? { mode: n } : {} }; }, isActive: e.isCurrent, reportCatalogOutcome: (e) => { assertCurrent(); if (normalizeProviderId(e.provider) === t && e.profileId === n) p.push(e); } }; await withCurrentReadAuthority(i, () => withGuardedFetchRequestAuthority(assertCurrent, async () => captureProviderCatalogExpiries(() => o(h), { refresh: e.refresh }), e.beforeRequest)); return withCurrentReadAuthority(i, () => p); } //#endregion export { reportProviderCatalogSecretFailure as i, loadSelectedProviderAccountCatalog as n, prepareProviderCatalogRun as r, buildPluginCatalogConfig as t };