import{n as e}from"./chunk-DR8-3Aex.js";import{In as t,Ln as n}from"./src-Di342QoJ.js";import{cn as r,dn as i}from"./state-sIHsFpDu.js";import{H as a,U as o}from"./debug-otxXY3j-.js";import{o as s,s as c}from"./log-DSV4Ht5l.js";import{AT as l,Al as u,Cl as d,Cn as f,El as p,Hk as m,Ol as h,Po as g,Tk as _,Tl as v,Uk as y,Uo as b,Wk as x,Zt as S,__ as C,_l as w,cA as T,dA as E,dt as D,fA as O,ft as k,g_ as A,hl as j,iA as M,jT as N,jk as P,kl as F,md as I,pd as L,rA as R,sA as z,wl as B,xl as V}from"./loadAgentsDir-C_wFhytF.js";async function H(e,t,n,r){t.onCompactProgress?.({type:`hooks_start`,hookType:`pre_compact`}),t.setSDKStatus?.(`compacting`);try{let[i,a]=await Promise.all([S({trigger:`manual`,customInstructions:n||null},t.abortController.signal),W(t,e)]),o=u(n,i.newCustomInstructions);t.setStreamMode?.(`requesting`),t.setResponseLength?.(()=>0),t.onCompactProgress?.({type:`compact_start`});let s=await r.reactiveCompactOnPromptTooLong(e,a,{customInstructions:o,trigger:`manual`});if(!s.ok)switch(s.reason){case`too_few_groups`:throw Error(v);case`aborted`:throw Error(p);case`exhausted`:case`error`:case`media_unstrippable`:throw Error(B)}P(void 0),d(),T(),y.cache.clear?.();let c=[i.userDisplayMessage,s.result.userDisplayMessage].filter(Boolean).join(`
`)||void 0;return{type:`compact`,compactionResult:{...s.result,userDisplayMessage:c},displayText:U(t,c)}}finally{t.setStreamMode?.(`requesting`),t.setResponseLength?.(()=>0),t.onCompactProgress?.({type:`compact_end`}),t.setSDKStatus?.(``)}}function U(e,n){let r=A(`tip`),i=l(`app:toggleTranscript`,`Global`,`ctrl+o`),a=[...e.options.verbose?[]:[`(${i} to see full summary)`],...n?[n]:[],...r?[r]:[]];return t.dim(`Compacted `+a.join(`
`))}async function W(e,t){let n=e.getAppState(),r=await D(e.options.tools,e.options.mainLoopModel,Array.from(n.toolPermissionContext.additionalWorkingDirectories.keys()),e.options.mcpClients),i=L({mainThreadAgentDefinition:void 0,toolUseContext:e,customSystemPrompt:e.options.customSystemPrompt,defaultSystemPrompt:r,appendSystemPrompt:e.options.appendSystemPrompt}),[a,o]=await Promise.all([y(),m()]);return{systemPrompt:i,userContext:a,systemContext:o,toolUseContext:e,forkContextMessages:t}}var G,K;e((()=>{n(),r(),k(),x(),N(),E(),F(),z(),R(),V(),j(),_(),o(),f(),s(),b(),C(),I(),G=null,K=async(e,t)=>{let{abortController:n}=t,{messages:r}=t;if(r=g(r),r.length===0)throw Error(`No messages to compact`);let o=e.trim();try{if(!o){let e=await w(r,t.agentId);if(e)return y.cache.clear?.(),d(),O(t.options.querySource??`compact`,t.agentId),i(),T(),{type:`compact`,compactionResult:e,displayText:U(t)}}if(G?.isReactiveOnlyMode())return await H(r,t,o,G);let e=(await M(r,t)).messages,n=await h(e,t,await W(t,e),!1,o,!1);return P(void 0),T(),y.cache.clear?.(),d(),{type:`compact`,compactionResult:n,displayText:U(t,n.userDisplayMessage)}}catch(e){throw n.signal.aborted?Error(`Compaction canceled.`):a(e,`Not enough messages to compact. Send a few more messages first, then try again.`)?Error(v):a(e,`Compaction interrupted · This may be due to network issues — please try again.`)?Error(B):(c(e),Error(`Error during compaction: ${e}`))}}}))();export{K as call};