import{ub as p}from"./cli-0.5.12-z2yzdy27.mjs";import{$z as y,AP as c,As as e,BP as X,Cs as jj,Ek as R,Iu as C,Jk as r,Ju as zj,SG as E,SN as Jj,TG as Hj,Ts as k,Us as w,Yj as $,Zj as P,Zs as qj,aA as Bj,cQ as Kj,ej as O,fj as o,gP as f,gr as W,hP as m,iP as S,ir as t,kQ as I,pi as T,qN as x,ri as u,rk as n,rs as h,tP as A,us as V,yQ as Qj}from"./cli-0.5.12-zc0qwdy2.mjs";import{CS as s,oS as v}from"./cli-0.5.12-2wf629mh.mjs";import{TT as D,WT as Gj}from"./cli-0.5.12-cw1qs4bg.mjs";import{p2 as b,w2 as i}from"./cli-0.5.12-pycnjaeg.mjs";import{pea as Z,qea as l}from"./cli-0.5.12-asbn3d19.mjs";import{fga as N,iga as g}from"./cli-0.5.12-p5ps8gf0.mjs";import{Aia as L,Dia as U,Iia as Y,Lja as _,Nja as M,Sla as K,rma as a}from"./cli-0.5.12-c8f4daey.mjs";a();Hj();qj();e();o();u();import{dirname as Vj}from"path";Qj();jj();Gj();l();g();t();Jj();i();r();Bj();Kj();s();zj();n();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===O);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(!v()&&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}y(j.worktreePath),Y(Z()),P(j),I(),E(),R.cache.clear?.()}function pj(){let j=$();if(!j)return;P(null),I(),E(),R.cache.clear?.();try{process.chdir(j.originalCwd)}catch{return}y(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 La,Zj as Ma,$j as Na,Rj as Oa,pj as Pa,dj as Qa};
