import{t as e}from"./rolldown-runtime-CNxR59P3.js";import{Ao as t,Mc as n,No as r,al as i,eu as a,il as o,jo as s,uc as c}from"./auth-profiles-qXC1L8hs.js";import{u as l}from"./utils-7Gdj77QB.js";import u from"node:path";import d from"node:fs/promises";function f(e){return typeof e==`number`&&Number.isFinite(e)&&e>0}function p(e){return e.explicitPresent&&f(e.explicitValue)?e.explicitValue:f(e.implicitValue)?e.implicitValue:f(e.explicitValue)?e.explicitValue:void 0}function m(e){if(!e||typeof e!=`object`)return``;let t=e.id;return typeof t==`string`?t.trim():``}function h(e,t){let n=Array.isArray(e.models)?e.models:[],r=Array.isArray(t.models)?t.models:[];if(n.length===0)return{...e,...t};let i=new Map(n.map(e=>[m(e),e]).filter(([e])=>!!e)),a=new Set,o=r.map(e=>{let t=m(e);if(!t)return e;a.add(t);let n=i.get(t);if(!n)return e;let r=p({explicitPresent:`contextWindow`in e,explicitValue:e.contextWindow,implicitValue:n.contextWindow}),o=p({explicitPresent:`maxTokens`in e,explicitValue:e.maxTokens,implicitValue:n.maxTokens});return{...e,input:n.input,reasoning:`reasoning`in e?e.reasoning:n.reasoning,...r===void 0?{}:{contextWindow:r},...o===void 0?{}:{maxTokens:o}}});for(let e of n){let t=m(e);!t||a.has(t)||(a.add(t),o.push(e))}return{...e,...t,models:o}}function g(e){let t=e.implicit?{...e.implicit}:{};for(let[n,r]of Object.entries(e.explicit??{})){let e=n.trim();if(!e)continue;let i=t[e];t[e]=i?h(i,r):r}return t}function _(e){if(typeof e?.api==`string`)return e.api.trim()||void 0}function v(e){if(!Array.isArray(e?.models))return;let t=e.models.flatMap(e=>{if(!e||typeof e!=`object`)return[];let t=e.api;return typeof t==`string`&&t.trim()?[t.trim()]:[]}).toSorted();return t.length>0?JSON.stringify(t):void 0}function y(e){return _(e)??v(e)}function b(e){let{providerKey:t,existing:n,nextEntry:r,secretRefManagedProviders:i}=e,o=typeof r.apiKey==`string`?r.apiKey:``;return o&&a(o)?!1:!i.has(t)&&typeof n.apiKey==`string`&&n.apiKey.length>0&&!a(n.apiKey,{includeEnvVarName:!1})}function x(e){let{providerKey:t,existing:n,nextEntry:r,explicitBaseUrlProviders:i}=e;if(i.has(t)||typeof n.baseUrl!=`string`||n.baseUrl.length===0)return!1;let a=y(n),o=y(r);return!a||!o||a===o}function S(e){let{nextProviders:t,existingProviders:n,secretRefManagedProviders:r,explicitBaseUrlProviders:i}=e,a={};for(let[e,t]of Object.entries(n))a[e]=t;for(let[e,o]of Object.entries(t)){let t=n[e];if(!t){a[e]=o;continue}let s={};b({providerKey:e,existing:t,nextEntry:o,secretRefManagedProviders:r})&&(s.apiKey=t.apiKey),x({providerKey:e,existing:t,nextEntry:o,explicitBaseUrlProviders:i})&&(s.baseUrl=t.baseUrl),a[e]={...o,...s}}return a}async function C(e){let{cfg:t,agentDir:n,env:r}=e,a=t.models?.providers??{};return g({implicit:await i({agentDir:n,config:t,env:r,explicitProviders:a}),explicit:a})}function w(e){return new Set(Object.entries(e?.providers??{}).map(([e,t])=>[e.trim(),t]).filter(([e,t])=>!!e&&typeof t?.baseUrl==`string`&&t.baseUrl.trim()).map(([e])=>e))}async function T(e){if(e.mode!==`merge`)return e.providers;let t=e.existingParsed;if(!l(t)||!l(t.providers))return e.providers;let n=t.providers;return S({nextProviders:e.providers,existingProviders:n,secretRefManagedProviders:e.secretRefManagedProviders,explicitBaseUrlProviders:e.explicitBaseUrlProviders})}async function E(e){let{cfg:t,agentDir:n,env:r}=e,i=await C({cfg:t,agentDir:n,env:r});if(Object.keys(i).length===0)return{action:`skip`};let a=t.models?.mode??`merge`,s=new Set,c=o({providers:i,agentDir:n,env:r,secretDefaults:t.secrets?.defaults,secretRefManagedProviders:s})??i,l=await T({mode:a,existingParsed:e.existingParsed,providers:c,secretRefManagedProviders:s,explicitBaseUrlProviders:w(t.models)}),u=`${JSON.stringify({providers:l},null,2)}\n`;return e.existingRaw===u?{action:`noop`}:{action:`write`,contents:u}}var D=e({ensureOpenClawModelsJson:()=>P});const O=new Map;async function k(e){try{let t=await d.readFile(e,`utf8`);return{raw:t,parsed:JSON.parse(t)}}catch{return{raw:``,parsed:null}}}async function A(e){await d.chmod(e,384).catch(()=>{})}async function j(e,t){let n=`${e}.${process.pid}.${Date.now()}.tmp`;await d.writeFile(n,t,{mode:384}),await d.rename(n,e)}function M(e){let n=t();return e?n?r(e):e:n??s()}async function N(e,t){let n=O.get(e)??Promise.resolve(),r=()=>{},i=new Promise(e=>{r=e}),a=n.then(()=>i);O.set(e,a);try{return await n,await t()}finally{r(),O.get(e)===a&&O.delete(e)}}async function P(e,t){let r=M(e),i=t?.trim()?t.trim():n(),a=u.join(i,`models.json`);return await N(a,async()=>{let e=c(r),t=await k(a),n=await E({cfg:r,agentDir:i,env:e,existingRaw:t.raw,existingParsed:t.parsed});return n.action===`skip`?{agentDir:i,wrote:!1}:n.action===`noop`?(await A(a),{agentDir:i,wrote:!1}):(await d.mkdir(i,{recursive:!0,mode:448}),await j(a,n.contents),await A(a),{agentDir:i,wrote:!0})})}export{D as n,P as t};
