openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 7.19 kB
JavaScript
import{n as findNormalizedProviderValue,r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{F as resolveRegisteredAgentIdForDir}from"./agent-scope-config-CG9CFpGX.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{i as resolveProviderIdForAuth}from"./provider-auth-aliases-BRA1A5Vm.mjs";import{i as resolveConfiguredProviderCatalogBaseUrl,r as isProviderCatalogSourceAllowed}from"./provider-config-owner-CkJs2SUk.mjs";import{i as matchesProviderPluginRef}from"./provider-registry-shared-WqkaV5jB.mjs";import{f as isTrustedSecretSurfaceUnavailableError}from"./runtime-degraded-state-DZJvpZUM.mjs";import{o as buildOAuthRefreshFailureLoginCommand}from"./oauth-refresh-failure-BdZMhNvV.mjs";import{r as PreparedModelRuntimePublicationSupersededError}from"./prepared-model-runtime-admission-errors-DZto8oll.mjs";import{n as normalizePluginDiscoveryResult}from"./provider-discovery-Bd_7hFWy.mjs";import{i as withGuardedFetchRequestAuthority}from"./fetch-request-authority-BTQz0YES.mjs";import"./prepared-model-runtime.errors-CJIB8kC0.mjs";import{t as captureProviderCatalogExpiries}from"./provider-catalog-expiry-DGHaV1tR.mjs";import{n as withCurrentReadAuthority}from"./current-read-authority-J_xgisvq.mjs";
//!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}}}function catalogPreparesOAuth(e,t){return e.auth.some(e=>e.kind===`oauth`||e.kind===`device_code`)&&(t===void 0||t.some(t=>matchesProviderPluginRef(e,t)))}async function listCatalogOAuthRuntimeOwners(e){const{selectExpiringCatalogOAuthProfile:t}=await import(`./models-config.providers.discovery-auth.runtime-B2bBvTgt.mjs`);return e.providers.flatMap(n=>{if(!n.pluginId||e.registered.has(n.pluginId)||!n.catalog)return[];const r=e.manifests.get(n.pluginId);const i=e.discoveryScope.get(n.pluginId);return catalogPreparesOAuth(n,e.providerIds.filter(t=>i?.includes(normalizeProviderId(t))&&isProviderCatalogSourceAllowed({provider:t,config:e.config,plugin:r,sourceBaseUrl:resolveConfiguredProviderCatalogBaseUrl(e.config,t)})))&&t(e.authStore,e.resolveProviderAuth,n.id)?[n.pluginId]:[]})}async function prepareProviderCatalogRun(e){const{authStore:t,isActive:n,preparationAuthority:r,...i}=e;if(!catalogPreparesOAuth(e.provider,e.providerIds))return i;const a=resolveRegisteredAgentIdForDir(e.agentDir,e.env);const{prepareProviderCatalogOAuthAuth:o}=await import(`./models-config.providers.discovery-auth.runtime-B2bBvTgt.mjs`);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-B2bBvTgt.mjs`);const r=n(e);for(const e of t.providerIds??[t.provider.id])t.reportCatalogOutcome?.({provider:e,...r?{profileId:r}:{},status:`unavailable`});return true}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-zwRT3hA8.mjs`),import(`./models-config.providers.secrets-BKUdvAf6.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)}export{reportProviderCatalogSecretFailure as a,prepareProviderCatalogRun as i,listCatalogOAuthRuntimeOwners as n,loadSelectedProviderAccountCatalog as r,buildPluginCatalogConfig as t};