"use strict";const h=require("fs/promises"),I=require("path");function d(r){const s=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const e in r)if(e!=="default"){const t=Object.getOwnPropertyDescriptor(r,e);Object.defineProperty(s,e,t.get?t:{enumerable:!0,get:()=>r[e]})}}return s.default=r,Object.freeze(s)}const c=d(h),l=d(I);class f{basePath;constructor(s=process.cwd()){this.basePath=l.resolve(s)}resolvePath(s){return l.isAbsolute(s)?s:l.resolve(this.basePath,s)}async readTextFile(s){const e=this.resolvePath(s.path),t=await c.readFile(e,{encoding:"utf-8"});if(s.line!==void 0||s.limit!==void 0){const i=t.split(`
`),n=s.line??0,o=s.limit!==void 0?n+s.limit:i.length;return{content:i.slice(n,o).join(`
`)}}return{content:t}}async writeTextFile(s){const e=this.resolvePath(s.path);return await c.writeFile(e,s.content,"utf-8"),{}}async delete(s){const e=this.resolvePath(s);(await c.stat(e)).isDirectory()?await c.rm(e,{recursive:!0}):await c.unlink(e)}async exists(s){const e=this.resolvePath(s);try{return await c.access(e),!0}catch{return!1}}async stat(s){const e=this.resolvePath(s),t=await c.stat(e);let i;return t.isDirectory()?i="directory":t.isSymbolicLink()?i="symlink":i="file",{type:i}}async readDirectory(s){const e=this.resolvePath(s);try{return(await c.readdir(e,{withFileTypes:!0})).map(i=>{let n;return i.isDirectory()?n="directory":i.isSymbolicLink()?n="symlink":n="file",{name:i.name,type:n}})}catch(t){if(t.code==="ENOENT")return[];throw t}}}class w{constructor(s,e,t,i,n){this.id=s,this.terminalId=e,this.sessionId=t,this.client=i,this.cwd=n}_isBusy=!1;_isClosed=!1;_output="";get isBusy(){return this._isBusy}get isClosed(){return this._isClosed}async runCommand(s,e){this._isBusy=!0;try{const i=(await this.client.createTerminal({sessionId:this.sessionId,command:"/bin/bash",args:["-c",s],cwd:this.cwd})).terminalId,n=this.client.waitForTerminalExit({sessionId:this.sessionId,terminalId:i});let o;if(e){const u=new Promise((v,m)=>setTimeout(()=>m(new Error(`Command timed out after ${e}ms`)),e));o=await Promise.race([n,u])}else o=await n;const a=await this.client.terminalOutput({sessionId:this.sessionId,terminalId:i});return this._output=a.output,await this.client.releaseTerminal({sessionId:this.sessionId,terminalId:i}),{output:a.output,exitCode:o.exitCode??void 0,terminalId:this.id}}finally{this._isBusy=!1}}readOutputLines(s){return s?this._output.split(`
`).slice(-s).join(`
`):this._output}ensureCommandRunsInCwd(s,e){return!e||e===this.cwd?s:`cd "${e}" && ${s}`}close(){this._isClosed=!0,this.client.releaseTerminal({sessionId:this.sessionId,terminalId:this.terminalId})}focus(){}}class y{constructor(s,e){this.client=s,this.sessionId=e}terminals=new Map;nextId=1;async getOrCreateTerminal(s){const e=`${s.cwd}:${s.name??"default"}`,t=this.terminals.get(e);if(t&&!t.isClosed)return t;const i=await this.client.createTerminal({sessionId:this.sessionId,command:s.env?.SHELL??"/bin/bash",cwd:s.cwd,env:s.env?Object.entries(s.env).map(([o,a])=>({name:o,value:a})):void 0}),n=new w(this.nextId++,i.terminalId,this.sessionId,this.client,s.cwd);return this.terminals.set(e,n),n}}class p{constructor(s,e){this.client=s,this.sessionId=e}processes=new Map;async startProcess(s,e,t){for(const[o,a]of this.processes)if(a.command===e&&a.cwd===t&&a.status==="running")return{terminalId:o,isReused:!0,status:"running"};const i=await this.client.createTerminal({sessionId:this.sessionId,command:e,cwd:t}),n={terminalId:i.terminalId,command:e,cwd:t,status:"running"};return this.processes.set(i.terminalId,n),{terminalId:i.terminalId,isReused:!1,status:"running"}}async stopProcess(s,e){const t=this.processes.get(e);t&&(await this.client.killTerminalCommand({sessionId:this.sessionId,terminalId:e}),t.status="stopped")}async getProcessOutput(s,e,t){const i=await this.client.terminalOutput({sessionId:this.sessionId,terminalId:e});return t?i.output.split(`
`).slice(-t).join(`
`):i.output}getProcess(s){const e=this.processes.get(s);if(e)return{terminalId:e.terminalId,command:e.command,cwd:e.cwd,status:e.status}}listProcesses(){return Array.from(this.processes.values()).map(s=>({terminalId:s.terminalId,command:s.command,cwd:s.cwd,status:s.status}))}openTerminal(s){}}exports.NodeBackgroundProcessManager=p;exports.NodeFileSystem=f;exports.NodeTerminalManager=y;
//# sourceMappingURL=node-background-process-manager-CfopBwKp.cjs.map
