const e=require(`./paths-Bcdg3ma0.cjs`),t=require(`./model-auth-Do7m_TFG.cjs`);let n=require(`node:path`);n=e.p(n);let r=require(`node:fs`);r=e.p(r);let i=require(`@mariozechner/pi-coding-agent`);i=e.p(i);function a(e){if(e.type===`api_key`){let t=typeof e.key==`string`?e.key.trim():``;return t?{type:`api_key`,key:t}:null}if(e.type===`token`){let t=typeof e.token==`string`?e.token.trim():``;return!t||typeof e.expires==`number`&&Number.isFinite(e.expires)&&Date.now()>=e.expires?null:{type:`api_key`,key:t}}if(e.type===`oauth`){let t=typeof e.access==`string`?e.access.trim():``,n=typeof e.refresh==`string`?e.refresh.trim():``;return!t||!n||!Number.isFinite(e.expires)||e.expires<=0?null:{type:`oauth`,access:t,refresh:n,expires:e.expires}}return null}function o(e){let n={};for(let r of Object.values(e.profiles)){let e=t.Cs(String(r.provider??``)).trim();if(!e||n[e])continue;let i=a(r);i&&(n[e]=i)}return n}const s=i.AuthStorage,c=i.ModelRegistry;function l(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 u(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function d(e){if(process.env.OPENCLAW_AUTH_STORE_READONLY===`1`||!r.default.existsSync(e))return;let t;try{t=JSON.parse(r.default.readFileSync(e,`utf8`))}catch{return}if(!u(t))return;let n=!1;for(let[e,r]of Object.entries(t))u(r)&&r.type===`api_key`&&(delete t[e],n=!0);if(n){if(Object.keys(t).length===0){r.default.rmSync(e,{force:!0});return}r.default.writeFileSync(e,`${JSON.stringify(t,null,2)}\n`,`utf8`),r.default.chmodSync(e,384)}}function f(e,t,n){let r=e;if(typeof r.inMemory==`function`)return r.inMemory(n);let a=e;if(typeof a.fromStorage==`function`){let e=i.InMemoryAuthStorageBackend,t=typeof e==`function`?new e:l(n);return t.withLock(()=>({result:void 0,next:JSON.stringify(n,null,2)})),a.fromStorage(t)}let o=e,s=typeof o.create==`function`?o.create(t):new e(t);if(typeof s.setRuntimeApiKey==`function`)for(let[e,t]of Object.entries(n)){if(t.type===`api_key`){s.setRuntimeApiKey(e,t.key);continue}s.setRuntimeApiKey(e,t.access)}return s}function p(e){return o(t.ds(e,{allowKeychainPrompt:!1}))}function m(e){let t=p(e),r=n.default.join(e,`auth.json`);return d(r),f(s,r,t)}function h(e,t){return new c(e,n.default.join(t,`models.json`))}Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return h}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return c}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return m}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return s}});
