import{c as e}from"./home-dir-C44RaPME-B5iLWwCq.mjs";import{s as t}from"./bundled-dir-U738ay4K-BxbzrJOo.mjs";import{l as n}from"./bundled-compat-BpaDaAAu-DJtrIX6i.mjs";import{o as r}from"./manifest-registry-_hxKCS2K-OL8KK_o4.mjs";import{r as i}from"./runtime-state-IBOIXKOx-Cw1GA1td.mjs";import{R as a,a as o,i as s,v as c}from"./provider-runtime-CT50mNIX-DImw7ZZu.mjs";import{n as l,r as u,t as d}from"./dist-J5UB83Ws.mjs";import{g as f,l as p,t as m}from"./model-auth-env-oyg2Hexc-DJbt9J3o.mjs";import{n as h}from"./provider-model-compat-CFqcq0it-BUwZENrz.mjs";import g from"node:fs";import _ from"node:path";function v(e){let t=new Set,r=[];for(let i of e){let e=i.trim(),a=n(e);!e||t.has(a)||(t.add(a),r.push(e))}return r}function y(){return v(r({cache:!0}).plugins.flatMap(e=>e.syntheticAuthRefs??[]))}function b(){let e=i()?.activeRegistry;return e?v([...(e.providers??[]).filter(e=>`resolveSyntheticAuth`in e.provider&&typeof e.provider.resolveSyntheticAuth==`function`).map(e=>e.provider.id),...(e.cliBackends??[]).filter(e=>`resolveSyntheticAuth`in e.backend&&typeof e.backend.resolveSyntheticAuth==`function`).map(e=>e.backend.id)]):y()}function x(t){if(t.type===`api_key`){let n=e(t.key)??``;return n?{type:`api_key`,key:n}:null}if(t.type===`token`){let n=e(t.token)??``;return!n||typeof t.expires==`number`&&Number.isFinite(t.expires)&&Date.now()>=t.expires?null:{type:`api_key`,key:n}}if(t.type===`oauth`){let n=e(t.access)??``,r=e(t.refresh)??``;return!n||!r||!Number.isFinite(t.expires)||t.expires<=0?null:{type:`oauth`,access:n,refresh:r,expires:t.expires}}return null}function S(e){let t={};for(let r of Object.values(e.profiles)){let e=n(r.provider??``);if(!e||t[e])continue;let i=x(r);i&&(t[e]=i)}return t}const C=l,w=d;function T(e){let t=JSON.stringify(e,null,2);return{withLock(e){let{result:n,next:r}=e(t);return typeof r==`string`&&(t=r),n}}}function E(e,n){if(!t(e)||typeof e.id!=`string`||typeof e.name!=`string`||typeof e.provider!=`string`)return e;let r=e,i=c({provider:r.provider,context:{provider:r.provider,modelId:r.id,model:r,agentDir:n}})??r,a=s({provider:r.provider,context:{provider:r.provider,modelId:r.id,model:i,agentDir:n}})??i,l=o({provider:r.provider,context:{provider:r.provider,modelId:r.id,model:a,agentDir:n}})??a;return!t(l)||typeof l.id!=`string`||typeof l.name!=`string`||typeof l.provider!=`string`||typeof l.api!=`string`?e:h(l)}function D(e,t){let n=w;return typeof n.create==`function`?n.create(e,t):new n(e,t)}function O(e,t,r,i){let a=D(e,t),o=a.getAll.bind(a),s=a.getAvailable.bind(a),c=a.find.bind(a),l=i?.providerFilter?n(i.providerFilter):``,u=e=>!l||n(e.provider)===l;return a.getAll=()=>o().filter(e=>u(e)).map(e=>E(e,r)),a.getAvailable=()=>s().filter(e=>u(e)).map(e=>E(e,r)),a.find=(e,t)=>E(c(e,t),r),a}function k(e){if(process.env.OPENCLAW_AUTH_STORE_READONLY===`1`||!g.existsSync(e))return;let n;try{n=JSON.parse(g.readFileSync(e,`utf8`))}catch{return}if(!t(n))return;let r=!1;for(let[e,i]of Object.entries(n))t(i)&&i.type===`api_key`&&(delete n[e],r=!0);if(r){if(Object.keys(n).length===0){g.rmSync(e,{force:!0});return}g.writeFileSync(e,`${JSON.stringify(n,null,2)}\n`,`utf8`),g.chmodSync(e,384)}}function A(e,t,n){let r=e;if(typeof r.inMemory==`function`)return r.inMemory(n);let i=e;if(typeof i.fromStorage==`function`){let e=u,t=typeof e==`function`?new e:T(n);return t.withLock(()=>({result:void 0,next:JSON.stringify(n,null,2)})),i.fromStorage(t)}let a=e,o=typeof a.create==`function`?a.create(t):new e(t);if(typeof o.setRuntimeApiKey==`function`)for(let[e,t]of Object.entries(n)){if(t.type===`api_key`){o.setRuntimeApiKey(e,t.key);continue}o.setRuntimeApiKey(e,t.access)}return o}function j(e,t=process.env){let n={...e};for(let e of Object.keys(p({env:t}))){if(n[e])continue;let r=m(e,t);r?.apiKey&&(n[e]={type:`api_key`,key:r.apiKey})}return n}function M(e){let t=j(S(f(e,{allowKeychainPrompt:!1})));for(let e of b()){if(t[e])continue;let n=a({provider:e,context:{config:void 0,provider:e,providerConfig:void 0}})?.apiKey?.trim();n&&(t[e]={type:`api_key`,key:n})}return t}function N(e){let t=M(e),n=_.join(e,`auth.json`);return k(n),A(C,n,t)}function P(e,t,n){return O(e,_.join(t,`models.json`),t,n)}export{P as a,k as c,N as i,w as n,E as o,j as r,M as s,C as t};