const e=require(`./model-auth-BEMdAUrd.cjs`);function t(e){if(e.startsWith(`{`))try{let t=JSON.parse(e);if(typeof t.token==`string`&&t.token)return{headers:{Authorization:`Bearer ${t.token}`,"Content-Type":`application/json`}}}catch{}return{headers:{"x-goog-api-key":e,"Content-Type":`application/json`}}}const n=/[\s,;]+/g,r=`OPENCLAW_LIVE_GEMINI_KEY`,i={google:`GEMINI`,"google-vertex":`GEMINI`},a={anthropic:{liveSingle:`OPENCLAW_LIVE_ANTHROPIC_KEY`,listVar:`OPENCLAW_LIVE_ANTHROPIC_KEYS`,primaryVar:`ANTHROPIC_API_KEY`,prefixedVar:`ANTHROPIC_API_KEY_`},google:{liveSingle:r,listVar:`GEMINI_API_KEYS`,primaryVar:`GEMINI_API_KEY`,prefixedVar:`GEMINI_API_KEY_`},"google-vertex":{liveSingle:r,listVar:`GEMINI_API_KEYS`,primaryVar:`GEMINI_API_KEY`,prefixedVar:`GEMINI_API_KEY_`},openai:{liveSingle:`OPENCLAW_LIVE_OPENAI_KEY`,listVar:`OPENAI_API_KEYS`,primaryVar:`OPENAI_API_KEY`,prefixedVar:`OPENAI_API_KEY_`}};function o(e){return e?e.split(n).map(e=>e.trim()).filter(Boolean):[]}function s(e){let t=[];for(let[n,r]of Object.entries(process.env)){if(!n.startsWith(e))continue;let i=r?.trim();i&&t.push(i)}return t}function c(t){let n=e.Ds(t),r=a[n],o=i[n]??n.toUpperCase().replace(/-/g,`_`),s=r?.liveSingle??`OPENCLAW_LIVE_${o}_KEY`,c=r?.listVar??`${o}_API_KEYS`,l=r?.primaryVar??`${o}_API_KEY`,u=r?.prefixedVar??`${o}_API_KEY_`;return n===`google`||n===`google-vertex`?{liveSingle:s,listVar:c,primaryVar:l,prefixedVar:u,fallbackVars:[`GOOGLE_API_KEY`]}:{liveSingle:s,listVar:c,primaryVar:l,prefixedVar:u,fallbackVars:[]}}function l(e){let t=c(e),n=t.liveSingle?process.env[t.liveSingle]?.trim():void 0;if(n)return[n];let r=o(t.listVar?process.env[t.listVar]:void 0),i=t.primaryVar?process.env[t.primaryVar]?.trim():void 0,a=t.prefixedVar?s(t.prefixedVar):[],l=t.fallbackVars.map(e=>process.env[e]?.trim()).filter(Boolean),u=new Set,d=e=>{e&&(u.has(e)||u.add(e))};for(let e of r)d(e);d(i);for(let e of a)d(e);for(let e of l)d(e);return Array.from(u)}function u(e){let t=e.toLowerCase();return!!(t.includes(`rate_limit`)||t.includes(`rate limit`)||t.includes(`429`)||t.includes(`quota exceeded`)||t.includes(`quota_exceeded`)||t.includes(`resource exhausted`)||t.includes(`resource_exhausted`)||t.includes(`too many requests`))}function d(e){let t=new Set,n=[];for(let r of e){let e=r.trim();!e||t.has(e)||(t.add(e),n.push(e))}return n}function f(e){let{primaryApiKey:t,provider:n}=e;return d([t?.trim()??``,...l(n)])}async function p(t){let n=d(t.apiKeys);if(n.length===0)throw Error(`No API keys configured for provider "${t.provider}".`);let r;for(let i=0;i<n.length;i+=1){let a=n[i];try{return await t.execute(a)}catch(o){r=o;let s=e.hi(o);if(!(t.shouldRetry?t.shouldRetry({apiKey:a,error:o,attempt:i,message:s}):u(s))||i+1>=n.length)break;t.onRetry?.({apiKey:a,error:o,attempt:i,message:s})}}throw r===void 0?Error(`Failed to run API request for ${t.provider}.`):r}Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return p}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return t}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return f}});
