#!/usr/bin/env node
import {a}from'./chunk-EKZDFEJW.js';import'./chunk-5TAVYPOV.js';import*as v from'fs';import*as m from'path';import {execSync,spawn}from'child_process';import {EventEmitter}from'events';var C=class{name="cursor-cli";agentPath=null;constructor(){}listModels(){return ["opus","sonnet","haiku"]}supportsParallel(){return  true}supportsMcp(){return  true}getTokenCost(s){return a[s]}async isAvailable(){if(!this.isCursorEnvironment())return  false;try{let s=execSync("which agent",{encoding:"utf-8"}).trim();if(s)return this.agentPath=s,!0}catch{let s=["/usr/local/bin/agent",m.join(process.env.HOME||"",".local/bin/agent"),m.join(process.env.HOME||"",".cursor/bin/agent"),"/Applications/Cursor.app/Contents/Resources/app/bin/agent"];for(let n of s)if(v.existsSync(n))return this.agentPath=n,true}return  false}isCursorEnvironment(){return process.env.TERM_PROGRAM==="cursor"||!!process.env.CURSOR_SESSION||!!process.env.CURSOR_TRACE_ID||(process.env.VSCODE_CWD?.toLowerCase().includes("cursor")??false)||(process.env.VSCODE_NLS_CONFIG?.toLowerCase().includes("cursor")??false)||process.env.TERM_PROGRAM==="vscode"&&(process.env.VSCODE_GIT_ASKPASS_NODE?.toLowerCase().includes("cursor")??false)}async*spawn(s,n){if(!this.agentPath){yield {type:"error",content:"Cursor agent CLI not found",timestamp:new Date().toISOString()};return}let t=this.buildPrompt(s,n);yield {type:"text",content:`Starting ${s.name} agent via Cursor CLI...
`,timestamp:new Date().toISOString()};let g=["-p",t.slice(0,8e3)];if(n.model){let e=this.mapToCursorModel(n.model);e&&g.push("--model",e);}let r=new EventEmitter,h="",d={input:0,output:0,total:0},i=null,c=false,a=spawn(this.agentPath,g,{cwd:n.workingDirectory||process.cwd(),env:{...process.env},stdio:["pipe","pipe","pipe"]});a.stdout?.on("data",e=>{let o=e.toString();h+=o,r.emit("text",o);}),a.stderr?.on("data",e=>{let o=e.toString();o.toLowerCase().includes("error")?(i=o,r.emit("error",new Error(o))):r.emit("progress",o);}),a.on("close",e=>{c=true;let o=Math.ceil(h.length/4),y=Math.ceil(t.length/4);d={input:y,output:o,total:y+o},r.emit("done",e);}),a.on("error",e=>{i=e.message,r.emit("error",e);});let f=n.timeout||300*1e3,S=setTimeout(()=>{a.kill(),i=`Cursor agent timed out after ${f/1e3}s`,r.emit("timeout");},f),p=[],l=null,u=e=>{l?(l(e),l=null):p.push(e);};for(r.on("text",e=>{u({type:"text",content:e,timestamp:new Date().toISOString()});}),r.on("progress",e=>{u({type:"text",content:".",timestamp:new Date().toISOString()});}),r.on("done",()=>{clearTimeout(S),u({type:"done",content:"Agent completed",usage:d,timestamp:new Date().toISOString()});}),r.on("timeout",()=>{u({type:"error",content:i||"Timeout",timestamp:new Date().toISOString()});}),r.on("error",e=>{clearTimeout(S),u({type:"error",content:e.message,timestamp:new Date().toISOString()});});!c||p.length>0;)if(p.length>0){let e=p.shift();if(yield e,e.type==="done"||e.type==="error")break}else {let e=await new Promise(o=>{l=o,setTimeout(()=>o(null),100);});if(e&&(yield e,e.type==="done"||e.type==="error"))break}i&&!c&&(yield {type:"error",content:i,timestamp:new Date().toISOString()});}buildPrompt(s,n){let t=[];return t.push(`# ${s.name.toUpperCase()} Agent`),t.push(""),t.push("## Role"),t.push(s.role),t.push(""),t.push("## Task"),t.push(n.task),t.push(""),n.context.systemPrompt&&(t.push("## Context"),t.push(n.context.systemPrompt.slice(0,3e3)),t.push("")),n.context.symbols.length>0&&(t.push("## Symbols"),t.push(n.context.symbols.join(", ")),t.push("")),n.context.handoffContext&&(t.push("## From Previous Agent"),t.push(n.context.handoffContext),t.push("")),t.push("## Instructions"),t.push("Complete the task above. Be concise and focused. Keep response under 500 words."),t.join(`
`)}mapToCursorModel(s){switch(s){case "opus":return "claude-opus-4-6";case "sonnet":return "claude-sonnet-4-5-20250929";case "haiku":return "claude-haiku-4-5-20251001";default:return null}}};export{C as CursorCliProvider};