#!/usr/bin/env node
import {a}from'./chunk-EKZDFEJW.js';import'./chunk-5TAVYPOV.js';import*as x from'fs';import*as f from'path';import {spawn}from'child_process';import {EventEmitter}from'events';var b=class{name="claude-cli";claudePath=null;constructor(){}listModels(){return ["opus","sonnet","haiku"]}supportsParallel(){return  true}supportsMcp(){return  true}getTokenCost(r){return a[r]}async isAvailable(){try{let{execSync:r}=await import('child_process'),n=r("which claude",{encoding:"utf-8"}).trim();if(n)return this.claudePath=n,!0}catch{let r=["/usr/local/bin/claude",f.join(process.env.HOME||"",".local/bin/claude"),f.join(process.env.HOME||"",".claude/bin/claude")];for(let n of r)if(x.existsSync(n))return this.claudePath=n,true}return  false}async*spawn(r,n){if(!this.claudePath){yield {type:"error",content:"Claude CLI not found",timestamp:new Date().toISOString()};return}let e=this.buildPrompt(r,n);yield {type:"text",content:`Starting ${r.name} agent via CLI (streaming)...
`,timestamp:new Date().toISOString()};let p=["--print","--permission-mode","acceptEdits","--output-format","stream-json","--no-session-persistence"];n.model&&p.push("--model",n.model),n.workingDirectory&&p.push("--add-dir",n.workingDirectory),p.push("-p",e.slice(0,8e3));let o=new EventEmitter,h={input:0,output:0,total:0},a=null,c=false,u=spawn(this.claudePath,p,{cwd:n.workingDirectory||process.cwd(),env:{...process.env},stdio:["pipe","pipe","pipe"]}),g="";u.stdout?.on("data",t=>{g+=t.toString();let s=g.split(`
`);g=s.pop()||"";for(let d of s)if(d.trim())try{let k=JSON.parse(d);o.emit("message",k);}catch{o.emit("text",d);}}),u.stderr?.on("data",t=>{let s=t.toString();s.includes("Error")?a=s:o.emit("progress",s);}),u.on("close",t=>{c=true,o.emit("done",t);}),u.on("error",t=>{a=t.message,o.emit("error",t);});let y=n.timeout||180*1e3,S=setTimeout(()=>{u.kill(),a=`Claude CLI timed out after ${y/1e3}s`,o.emit("timeout");},y),l=[],m=null,i=t=>{m?(m(t),m=null):l.push(t);};for(o.on("message",t=>{if(t.type==="assistant"&&t.message?.content)for(let s of t.message.content)s.type==="text"&&s.text?i({type:"text",content:s.text,timestamp:new Date().toISOString()}):s.type==="tool_use"&&s.name&&i({type:"tool_use",content:`Using tool: ${s.name}`,toolName:s.name,toolInput:s.input,timestamp:new Date().toISOString()});else t.type==="result"&&t.usage&&(h={input:t.usage.input_tokens||0,output:t.usage.output_tokens||0,total:(t.usage.input_tokens||0)+(t.usage.output_tokens||0)});}),o.on("text",t=>{i({type:"text",content:t+`
`,timestamp:new Date().toISOString()});}),o.on("progress",t=>{i({type:"text",content:".",timestamp:new Date().toISOString()});}),o.on("done",()=>{clearTimeout(S),i({type:"done",content:"Agent completed",usage:h,timestamp:new Date().toISOString()});}),o.on("timeout",()=>{i({type:"error",content:a||"Timeout",timestamp:new Date().toISOString()});}),o.on("error",t=>{clearTimeout(S),i({type:"error",content:t.message,timestamp:new Date().toISOString()});});!c||l.length>0;)if(l.length>0){let t=l.shift();if(yield t,t.type==="done"||t.type==="error")break}else {let t=await new Promise(s=>{m=s,setTimeout(()=>s(null),100);});if(t&&(yield t,t.type==="done"||t.type==="error"))break}a&&!c&&(yield {type:"error",content:a,timestamp:new Date().toISOString()});}buildPrompt(r,n){let e=[];return e.push(`# ${r.name.toUpperCase()} Agent`),e.push(""),e.push("## Role"),e.push(r.role),e.push(""),e.push("## Task"),e.push(n.task),e.push(""),n.context.systemPrompt&&(e.push("## Context"),e.push(n.context.systemPrompt.slice(0,3e3)),e.push("")),n.context.symbols.length>0&&(e.push("## Symbols"),e.push(n.context.symbols.join(", ")),e.push("")),n.context.handoffContext&&(e.push("## From Previous Agent"),e.push(n.context.handoffContext),e.push("")),e.push("## Instructions"),e.push("Complete the task above. Be concise and focused. Keep response under 500 words."),e.join(`
`)}};export{b as ClaudeCliProvider};