UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.96 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{_ as secretRefKey}from"./ref-contract-CdpCUlb6.mjs";import{g as parseSecretRef}from"./types.secrets-UWdyCdE6.mjs";import{n as sanitizeForLog}from"./ansi-54CEWBZf.mjs";import{c as redactSensitiveText}from"./redact-BT59pdZ_.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{i as resolveProviderIdForAuth}from"./provider-auth-aliases-BRA1A5Vm.mjs";import{r as hasUsableOAuthCredential}from"./credential-state-C3XZhGc-.mjs";import{n as SecretSurfaceUnavailableError}from"./runtime-degraded-state-DZJvpZUM.mjs";import{n as resolveAuthProfileSecretOwnerId}from"./runtime-auth-profile-owner-B_kIPj1R.mjs"; //!src/agents/models-config.providers.discovery-auth.runtime.ts const unavailableDiscoveryAuthProfiles=/* @__PURE__ */ new WeakMap;function throwUnavailableDiscoveryAuthProfile(e,t){if(typeof t===`object`&&t!==null)unavailableDiscoveryAuthProfiles.set(t,e);throw t}function resolveUnavailableDiscoveryAuthProfileId(e){return typeof e===`object`&&e!==null?unavailableDiscoveryAuthProfiles.get(e):void 0}async function prepareProviderDiscoveryAuth({agentDir:e,authStore:t,env:n,resolveProviderApiKey:r,resolveProviderAuth:i},a){const o=/* @__PURE__ */ new Map;for(const[r,i]of Object.entries(t.profiles)){const s=i.type===`api_key`?`key`:`token`;const c=parseSecretRef(i.type===`api_key`?i.keyRef:i.type===`token`?i.tokenRef:void 0,a?.secrets?.defaults);if(!c)continue;const l=c.source===`env`?normalizeOptionalString(n[c.id.trim()]):void 0;if(l){o.set(r,()=>l);continue}const{resolveApiKeyForProfile:u}=await import(`./oauth-CuRxVugJ.mjs`);try{const n=await u({cfg:a,store:t,profileId:r,agentDir:e,allowProfileFallback:false});if(!n)throw new SecretSurfaceUnavailableError({ownerKind:`account`,ownerId:resolveAuthProfileSecretOwnerId({agentDir:e,profileId:r}),state:`unavailable`,paths:[`auth-profiles.${r}.${s}`],refKeys:[secretRefKey(c)],reason:`resolved secret value was invalid`});o.set(r,()=>n.apiKey)}catch(e){o.set(r,()=>throwUnavailableDiscoveryAuthProfile(r,e))}}const enrich=e=>{const t=e.profileId?o.get(e.profileId):void 0;return t?{...e,discoveryApiKey:t()}:e};return{resolveProviderApiKey:e=>enrich(r(e)),resolveProviderAuth:(e,t)=>enrich(i(e,t))}}function selectExpiringCatalogOAuthProfile(e,t,n,r){let i;try{i=t(n,{excludeProfileIds:r})}catch{return}const a=i.profileId?e.profiles[i.profileId]:void 0;return i.mode===`oauth`&&i.profileId&&a?.type===`oauth`&&!a.oauthRef&&!hasUsableOAuthCredential(a)?{profileId:i.profileId,authFlow:i.authFlow}:void 0}async function prepareProviderCatalogOAuthAuth({agentDir:e,authStore:t,env:n,provider:r,resolveProviderAuth:i,isActive:a,onPreparationFailure:o},s){const c=[];const l=[];let u;let d;while(a()){const n=selectExpiringCatalogOAuthProfile(t,i,r,c);if(!n)break;const{resolveApiKeyForProfile:o}=await import(`./oauth-CuRxVugJ.mjs`);if(!a())break;let f=`No OAuth credential was returned`;try{const r=await o({cfg:s,store:t,profileId:n.profileId,agentDir:e,allowProfileFallback:false});if(r?.apiKey){d={profileId:n.profileId,apiKey:r.apiKey,authFlow:r.credential?.type===`oauth`?r.credential.authFlow:void 0};break}}catch(e){f=sanitizeForLog(redactSensitiveText(formatErrorMessage(e),{mode:`tools`})).replace(/\s+/gu,` `).slice(0,500)}c.push(n.profileId);l.push({profileId:n.profileId,message:f});u=n.authFlow}const resolvePreparedProviderAuth=(e,t)=>{const a=e?.trim()||r;const l=i(a,{...t,excludeProfileIds:c});if(l.mode===`none`&&c.length>0&&resolveProviderIdForAuth(a,{config:s,env:n})===resolveProviderIdForAuth(r,{config:s,env:n})){o(c);return{...l,preparationFailed:true,...u?{authFlow:u}:{}}}return d&&l.profileId===d.profileId?{...l,discoveryApiKey:d.apiKey,authFlow:d.authFlow}:l};return{resolveProviderAuth:resolvePreparedProviderAuth,failures:l}}export{prepareProviderCatalogOAuthAuth,prepareProviderDiscoveryAuth,resolveUnavailableDiscoveryAuthProfileId,selectExpiringCatalogOAuthProfile};