import{rb as p}from"./cli-0.5.3-4aj4qph6.mjs";import{AO as c,Ak as y,An as t,BO as X,GG as I,HG as Jj,Lu as C,Mj as R,Mu as Bj,Ok as r,Rj as n,Wh as T,Ws as k,Xs as w,Xz as E,YM as x,Yh as u,Yz as Gj,at as zj,cP as Qj,cr as W,dj as $,ej as P,er as e,gO as f,hO as m,iO as S,ns as h,qs as V,tO as A,ws as jj,xj as o,yN as Kj,ys as qj,zn as v}from"./cli-0.5.3-jbzr8k4s.mjs";import{LQ as O,ZQ as s}from"./cli-0.5.3-42e2paz8.mjs";import{SR as D,VR as Hj}from"./cli-0.5.3-gr8nsa2k.mjs";import{I1 as b,P1 as i}from"./cli-0.5.3-1vt6twff.mjs";import{Xda as Z,Yda as g}from"./cli-0.5.3-6j2jk6dt.mjs";import{Nfa as N,Qfa as l}from"./cli-0.5.3-n3cxyxpg.mjs";import{Aja as _,Cja as M,Hla as K,gma as a,pia as L,sia as U,xia as Y}from"./cli-0.5.3-qrvd6cmy.mjs";a();Jj();zj();jj();t();u();import{dirname as Vj}from"path";r();qj();Hj();g();l();e();Kj();i();n();Gj();Qj();s();Bj();o();function Xj(j){for(let q=j.length-1;q>=0;q--){let B=j[q];if(B?.type!=="assistant")continue;let z=B.message.content.find((J)=>J.type==="tool_use"&&J.name===v);if(!z||z.type!=="tool_use")continue;let G=z.input;if(G===null||typeof G!=="object")return[];let H=C().safeParse(G.todos);return H.success?H.data:[]}return[]}function cj(j,q){if(j.fileHistorySnapshots&&j.fileHistorySnapshots.length>0)W(j.fileHistorySnapshots,(B)=>{q((z)=>({...z,fileHistory:B}))});if(!O()&&j.messages&&j.messages.length>0){let B=Xj(j.messages);if(B.length>0){let z=L();q((G)=>({...G,todos:{...G.todos,[z]:B}}))}}}function Yj(j){return}function Zj(j,q){if(!j&&!q)return;return{name:j??"",color:q==="default"?void 0:q}}function $j(j,q,B){if(q)return{agentDefinition:q,agentType:void 0};if(!j)return K(void 0),{agentDefinition:void 0,agentType:void 0};let z=B.activeAgents.find((G)=>G.agentType===j);if(!z)return N(`Resumed session had agent "${j}" but it is no longer available. Using default behavior.`),K(void 0),{agentDefinition:void 0,agentType:void 0};if(K(z.agentType),!_()&&z.model&&z.model!=="inherit")M(b(z.model));return{agentDefinition:z,agentType:z.agentType}}async function Pj(j,q,B,z){if(!j)return z;V.cache.clear?.();let G=await V(q),H=[...G.allAgents,...B];return{...G,allAgents:H,activeAgents:h(H)}}function Rj(j){let q=$();if(q){X(q);return}if(!j)return;try{process.chdir(j.worktreePath)}catch{X(null);return}E(j.worktreePath),Y(Z()),P(j),y(),I(),R.cache.clear?.()}function pj(){let j=$();if(!j)return;P(null),y(),I(),R.cache.clear?.();try{process.chdir(j.originalCwd)}catch{return}E(j.originalCwd),Y(Z())}async function dj(j,q,B){let z;if(z=B.modeApi?.matchSessionMode(j.mode),z)j.messages.push(x(z,"warning"));if(!q.forkSession){let Q=q.sessionIdOverride??j.sessionId;if(Q){if(U(T(Q),q.transcriptPath?Vj(q.transcriptPath):null),await p(),await m(),!k(Q))w(j.messages)}}else if(j.contentReplacements?.length)await f(j.contentReplacements);if(A(q.forkSession?{...j,worktreeSession:void 0}:j),!q.forkSession)Rj(j.worktreeSession),S();let{agentDefinition:G,agentType:H}=$j(j.agentSetting,B.mainThreadAgentDefinition,B.agentDefinitions);c(B.modeApi?.isCoordinatorMode()?"coordinator":"normal");let J=q.includeAttribution?Yj(j):void 0,F=Zj(j.agentName,j.agentColor);D(j.agentName);let d=await Pj(!!z,B.currentCwd,B.cliAgents,B.agentDefinitions);return{messages:j.messages,fileHistorySnapshots:j.fileHistorySnapshots,contentReplacements:j.contentReplacements,agentName:j.agentName,agentColor:j.agentColor==="default"?void 0:j.agentColor,restoredAgentDef:G,initialState:{...B.initialState,...H&&{agent:H},...J&&{attribution:J},...F&&{standaloneAgentContext:F},agentDefinitions:d}}}
export{cj as Ka,Zj as La,$j as Ma,Rj as Na,pj as Oa,dj as Pa};
