openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 2.97 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{p as toErrorObject}from"./error-coercion-DlKnt6w-.mjs";import{g as normalizeUniqueStringEntries,v as normalizeUniqueTrimmedStringList}from"./string-normalization-D_oBUpO3.mjs";import{d as sleepWithAbort}from"./src-De06gkOi.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{t as getProviderEnvVarsCore}from"./provider-env-vars-CSXML2JF.mjs";import"./backoff-DRW5tqzp.mjs";import{n as classifyFailoverSignal}from"./classify-BQDdk1iN.mjs";import{a as resolveTransientProviderDelayMs,i as resolveTransientProviderAttempts,o as resolveTransientProviderRetryOptions,s as shouldRetrySameKeyProviderOperation}from"./operation-retry-BWpsksDm.mjs";
//!src/agents/live-auth-keys.ts
const KEY_SPLIT_RE=/[\s,;]+/g;const PROVIDER_PREFIX_OVERRIDES={google:`GEMINI`,"google-vertex":`GEMINI`};function collectProviderApiKeys(e,t={}){const n=t.env??process.env;const r=normalizeProviderId(e);const i=PROVIDER_PREFIX_OVERRIDES[r]??r.toUpperCase().replace(/-/g,`_`);const a=normalizeOptionalString(n[`OPENCLAW_LIVE_${i}_KEY`]);if(a)return[a];const o=r===`anthropic`?`OPENCLAW_LIVE_ANTHROPIC_KEYS`:`${i}_API_KEYS`;const s=t.providerEnvVars??getProviderEnvVarsCore(r);return normalizeUniqueTrimmedStringList([...n[o]?.split(KEY_SPLIT_RE)??[],n[`${i}_API_KEY`],...Object.entries(n).filter(([e])=>e.startsWith(`${i}_API_KEY_`)).map(([,e])=>e),...r===`google`||r===`google-vertex`?[n.GOOGLE_API_KEY]:[],...s.map(e=>n[e])])}function isApiKeyRateLimitError(e){const t=classifyFailoverSignal({message:e});return t?.kind===`reason`&&t.reason===`rate_limit`}
//!src/agents/api-key-rotation.ts
function collectProviderApiKeysForExecution(e){const{primaryApiKey:t,provider:n}=e;return normalizeUniqueStringEntries([t?.trim()??``,...collectProviderApiKeys(n)])}async function executeWithApiKeyRotation(e){const t=normalizeUniqueStringEntries(e.apiKeys);if(t.length===0)throw new Error(`No API keys configured for provider "${e.provider}".`);let n;const r=resolveTransientProviderRetryOptions(e.transientRetry);keyLoop:for(const[i,a]of t.entries()){const o=resolveTransientProviderAttempts(r);for(let s=1;s<=o;s+=1){r?.signal?.throwIfAborted();try{return await e.execute(a)}catch(c){r?.signal?.throwIfAborted();n=c;const l=formatErrorMessage(c);const u={apiKey:a,error:c,attempt:s,apiKeyIndex:i,message:l};if(e.shouldRetry?.(u)??isApiKeyRateLimitError(l)){if(i+1>=t.length)break;e.onRetry?.(u);break}if(!r||!shouldRetrySameKeyProviderOperation({options:r,error:c,message:l,provider:e.provider,apiKeyIndex:i,attemptNumber:s,maxAttempts:o}))break keyLoop;const d=resolveTransientProviderDelayMs(r,s);await(r.sleep??sleepWithAbort)(d,r.signal)}}}if(n===void 0)throw new Error(`Failed to run API request for ${e.provider}.`);throw toErrorObject(n,`Non-Error thrown`)}export{executeWithApiKeyRotation as n,collectProviderApiKeysForExecution as t};