import{Dc as e,Do as t,Mc as n,Oc as r,Ro as i,do as a,lo as o,wc as s,zo as c}from"./auth-profiles-qXC1L8hs.js";import{a as l,r as u}from"./agent-scope-BKE4M04k.js";import{v as d}from"./utils-7Gdj77QB.js";import{d as f}from"./types.secrets-D4tbc3Wq.js";import{a as p,c as m,n as h,o as g,s as _,t as v,u as y}from"./runtime-web-tools-CXyVBqxi.js";function b(e){let{explicitRef:t,inlineRef:n,ref:r}=f({value:e.profile.key,refValue:e.profile.keyRef,defaults:e.defaults});r&&(!t&&n&&(e.profile.keyRef=n),t&&a(e.profile.key)&&m(e.context,{code:`SECRETS_REF_OVERRIDES_PLAINTEXT`,path:`${e.agentDir}.auth-profiles.${e.profileId}.key`,message:`auth-profiles ${e.profileId}: keyRef is set; runtime will ignore plaintext key.`}),_(e.context,{ref:r,path:`${e.agentDir}.auth-profiles.${e.profileId}.key`,expected:`string`,apply:t=>{e.profile.key=String(t)}}))}function x(e){let{explicitRef:t,inlineRef:n,ref:r}=f({value:e.profile.token,refValue:e.profile.tokenRef,defaults:e.defaults});r&&(!t&&n&&(e.profile.tokenRef=n),t&&a(e.profile.token)&&m(e.context,{code:`SECRETS_REF_OVERRIDES_PLAINTEXT`,path:`${e.agentDir}.auth-profiles.${e.profileId}.token`,message:`auth-profiles ${e.profileId}: tokenRef is set; runtime will ignore plaintext token.`}),_(e.context,{ref:r,path:`${e.agentDir}.auth-profiles.${e.profileId}.token`,expected:`string`,apply:t=>{e.profile.token=String(t)}}))}function S(e){let t=e.context.sourceConfig.secrets?.defaults;for(let[n,r]of Object.entries(e.store.profiles)){if(r.type===`api_key`){b({profile:r,profileId:n,agentDir:e.agentDir,defaults:t,context:e.context});continue}r.type===`token`&&x({profile:r,profileId:n,agentDir:e.agentDir,defaults:t,context:e.context})}}let C=null,w=null;const T=new WeakMap;function E(e){return{sourceConfig:structuredClone(e.sourceConfig),config:structuredClone(e.config),authStores:e.authStores.map(e=>({agentDir:e.agentDir,store:structuredClone(e.store)})),warnings:e.warnings.map(e=>({...e})),webTools:structuredClone(e.webTools)}}function D(e){return{env:{...e.env},explicitAgentDirs:e.explicitAgentDirs?[...e.explicitAgentDirs]:null,loadAuthStore:e.loadAuthStore}}function O(){C=null,w=null,c(null),t(),s()}function k(e){let t=new Set;t.add(d(n()));for(let n of u(e))t.add(d(l(e,n)));return[...t]}function A(e,t){let n=k(e);return!t.explicitAgentDirs||t.explicitAgentDirs.length===0?n:[...new Set([...t.explicitAgentDirs,...n])]}async function j(t){let n=structuredClone(t.config),r=structuredClone(t.config),i=g({sourceConfig:n,env:t.env??process.env});h({config:r,context:i});let a=t.loadAuthStore??e,s=t.agentDirs?.length?[...new Set(t.agentDirs.map(e=>d(e)))]:k(r),c=[];for(let e of s){let t=structuredClone(a(e));S({store:t,context:i,agentDir:e}),c.push({agentDir:e,store:t})}if(i.assignments.length>0){let e=await o(i.assignments.map(e=>e.ref),{config:n,env:i.env,cache:i.cache});p({assignments:i.assignments,resolved:e})}let l={sourceConfig:n,config:r,authStores:c,warnings:i.warnings,webTools:await v({sourceConfig:n,resolvedConfig:r,context:i})};return T.set(l,{env:{...t.env??process.env},explicitAgentDirs:t.agentDirs?.length?[...s]:null,loadAuthStore:a}),l}function M(t){let n=E(t),a=T.get(t)??w??{env:{...process.env},explicitAgentDirs:null,loadAuthStore:e};i(n.config,n.sourceConfig),r(n.authStores),C=n,w=D(a),c({refresh:async({sourceConfig:e})=>!C||!w?!1:(M(await j({config:e,env:w.env,agentDirs:A(e,w),loadAuthStore:w.loadAuthStore})),!0)})}function N(){if(!C)return null;let e=E(C);return w&&T.set(e,D(w)),e}function P(){return C?structuredClone(C.webTools):null}function F(e){if(!C)throw Error(`Secrets runtime snapshot is not active.`);if(e.targetIds.size===0)return{assignments:[],diagnostics:[],inactiveRefPaths:[]};let t=[...new Set(C.warnings.filter(e=>e.code===`SECRETS_REF_IGNORED_INACTIVE_SURFACE`).map(e=>e.path))],n=y({sourceConfig:C.sourceConfig,resolvedConfig:C.config,commandName:e.commandName,targetIds:e.targetIds,inactiveRefPaths:new Set(t)});return{assignments:n.assignments,diagnostics:n.diagnostics,inactiveRefPaths:t}}function I(){O()}export{j as a,N as i,I as n,F as o,P as r,M as t};
