import{AN as d,mM as L,mN as N,nM as w,vw as B,ww as U}from"./cli-0.5.4-k5fz4zd3.mjs";import{b9 as z,f$ as P,i9 as F}from"./cli-0.5.4-cbq01zg3.mjs";import{$ca as H,fda as v}from"./cli-0.5.4-7grzwf6q.mjs";import{Zma as j}from"./cli-0.5.4-aacw1src.mjs";async function V(R){let{messages:I,getAppState:O,options:{mainLoopModel:J,tools:Y,agentDefinitions:Z,customSystemPrompt:W,appendSystemPrompt:X}}=R,Q=N(I),{messages:K}=await B(Q),h=O();return L(K,J,async()=>h.toolPermissionContext,Y,Z,void 0,{options:{customSystemPrompt:W,appendSystemPrompt:X}},void 0,Q)}async function g(R,I){let O=await V(I);return{type:"text",value:b(O)}}function b(R){let{categories:I,totalTokens:O,rawMaxTokens:J,percentage:Y,model:Z,memoryFiles:W,mcpTools:X,agents:Q,skills:K}=R,h=`## Context Usage

`;h+=`**Model:** ${Z}  
`,h+=`**Tokens:** ${H(O)} / ${H(J)} (${Y}%)
`,h+=`
`;let _=I.filter((q)=>q.tokens>0&&q.name!=="Free space"&&q.name!=="Autocompact buffer");if(_.length>0){h+=`### Estimated usage by category

`,h+=`| Category | Tokens | Percentage |
`,h+=`|----------|--------|------------|
`;for(let G of _){let $=(G.tokens/J*100).toFixed(1);h+=`| ${G.name} | ${H(G.tokens)} | ${$}% |
`}let q=I.find((G)=>G.name==="Free space");if(q&&q.tokens>0){let G=(q.tokens/J*100).toFixed(1);h+=`| Free space | ${H(q.tokens)} | ${G}% |
`}let E=I.find((G)=>G.name==="Autocompact buffer");if(E&&E.tokens>0){let G=(E.tokens/J*100).toFixed(1);h+=`| Autocompact buffer | ${H(E.tokens)} | ${G}% |
`}h+=`
`}if(X.length>0){h+=`### MCP Tools

`,h+=`| Tool | Server | Tokens |
`,h+=`|------|--------|--------|
`;for(let q of X)h+=`| ${q.name} | ${q.serverName} | ${H(q.tokens)} |
`;h+=`
`}if(Q.length>0){h+=`### Custom Agents

`,h+=`| Agent Type | Source | Tokens |
`,h+=`|------------|--------|--------|
`;for(let q of Q){let E;switch(q.source){case"projectSettings":E="Project";break;case"userSettings":E="User";break;case"localSettings":E="Local";break;case"flagSettings":E="Flag";break;case"policySettings":E="Policy";break;case"plugin":E="Plugin";break;case"built-in":E="Built-in";break;default:E=String(q.source)}h+=`| ${q.agentType} | ${E} | ${H(q.tokens)} |
`}h+=`
`}if(W.length>0){h+=`### Memory Files

`,h+=`| Type | Path | Tokens |
`,h+=`|------|------|--------|
`;for(let q of W)h+=`| ${q.type} | ${q.path} | ${H(q.tokens)} |
`;h+=`
`}if(K&&K.tokens>0&&K.skillFrontmatter.length>0){h+=`### Skills

`,h+=`| Skill | Source | Tokens |
`,h+=`|-------|--------|--------|
`;for(let q of K.skillFrontmatter)h+=`| ${q.name} | ${z(q.source)} | ${H(q.tokens)} |
`;h+=`
`}return h}var A=j(()=>{U();w();v();d();F();P()});
export{V as rg,g as sg,A as tg};
