@aria-cli/tools 1.0.15 → 1.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/dist/.aria-build-stamp.json +1 -1
- package/dist/{dns-pinning-vc0r0vpx.js → dns-pinning-7szqhtmq.js} +1 -1
- package/dist/{index-8xap0se3.js → index-b3sdwzh5.js} +1 -1
- package/dist/index-tvryzx00.js +2 -0
- package/dist/{index-x52t2mbj.js → index-v49pd2a0.js} +2 -2
- package/dist/index.js +99 -99
- package/dist/{lsp-client-msnj763y.js → lsp-client-3qxptwyw.js} +1 -1
- package/dist/network-runtime/index.js +1 -1
- package/dist/pty-cf6pmvx6.js +1 -0
- package/dist/session-history-gk75e3ta.js +1 -0
- package/dist-cjs/index.js +98 -98
- package/dist-cjs/network-runtime/index.js +1 -1
- package/package.json +21 -20
- package/dist/index-a2fr1b3x.js +0 -2
- package/dist/pty-ff59hy75.js +0 -1
- package/dist/session-history-6gn52xhp.js +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @aria-cli/tools
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
Part of [ARIA](https://www.npmjs.com/package/@aria-cli/cli) — Adaptive Reasoning Intelligence Agent.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i @aria-cli/tools
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
See the main [`@aria-cli/cli`](https://www.npmjs.com/package/@aria-cli/cli) package for full documentation.
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
MIT
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{kc as V,lc as W,mc as X}from"./index-syg9gyvy.js";import{sc as J}from"./index-6qew5tp1.js";import"./index-
|
|
1
|
+
import{kc as V,lc as W,mc as X}from"./index-syg9gyvy.js";import{sc as J}from"./index-6qew5tp1.js";import"./index-tvryzx00.js";import*as Y from"node:dns";import{Agent as _}from"undici";async function $(q){let w;try{let B=await Y.promises.lookup(q,{all:!0,verbatim:!0});w=V(B)}catch(B){throw Error(`DNS resolution failed for ${q}: ${J(B)}`)}if(w.length===0)throw Error(`DNS resolution failed for ${q}: no addresses returned`);let G=w.find((B)=>X(B.address));if(G)throw Error(`SSRF protection: ${q} resolves to private network address ${G.address}`);return w}function x(q){return q instanceof Error&&q.name==="AbortError"}function C(q){if(typeof q==="object"&&q!==null&&"code"in q&&typeof q.code==="string")return`${q.code}: ${J(q)}`;if(q instanceof Error&&q.cause){let w=q.cause;if(typeof w.code==="string")return`${w.code}: ${J(q.cause)}`}return J(q)}function L(q,w){return new _({connect:{lookup:(G,B,K)=>{K(null,[{address:q,family:w}])}}})}async function R(q,w){let G=W(q);if(G)throw Error(G);let B=new URL(q),K=await $(B.hostname),N=[],O;for(let{address:Q,family:T}of K){let H=L(Q,T);try{let D=globalThis.fetch;if(typeof D!=="function")throw Error("Global fetch is unavailable");return await D(q,{...w,dispatcher:H})}catch(D){if(x(D))throw D;O=D,N.push(`${Q}/${T}: ${C(D)}`)}finally{if(H&&"close"in H&&typeof H.close==="function")await H.close()}}let Z=N.length>0?` Attempted addresses: ${N.join("; ")}`:"";throw Error(`Fetch failed for ${B.hostname}.${Z}`,{cause:O})}export{R as fetchWithDnsPinning,L as createPinnedAgent};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{pc as U,qc as Q,sc as C}from"./index-6qew5tp1.js";import{
|
|
1
|
+
import{pc as U,qc as Q,sc as C}from"./index-6qew5tp1.js";import{tc as F}from"./index-tvryzx00.js";import*as G from"node:os";import*as X from"node:path";var T=2000,j,O=null;async function M(){if(j!==void 0)return j;if(O)return O;return O=(async()=>{try{let B=(await import("@aria-cli/cli")).SessionHistory,J=process.env.ARIA_HOME??X.join(G.homedir(),".aria"),V="ARIA";try{let{readFileSync:W,existsSync:Y}=await import("node:fs"),K=X.join(J,"config.json");if(Y(K)){let D=JSON.parse(W(K,"utf-8"));if(D.activeArion)V=D.activeArion}}catch{}let R=B.resolvePerArionPath(J,V);return j=new B(R),j}catch{return j=null,null}})(),O}async function N(q){if(q.sessionHistory)return q.sessionHistory;return M()}function E(q){return q.map((z)=>({id:z.id,title:z.title,arion:z.arion,model:z.model,messageCount:z.messageCount,updatedAt:z.updatedAt.toISOString(),completedAt:z.completedAt?.toISOString(),preview:z.preview}))}function L(q,z){if(q.length<=z)return q;return q.slice(0,z-3)+"..."}function A(q,z){if(z.length===36)return q.getSession(z)?z:null;return q.findSessionByPrefix(z)}async function v(q,z){if(z.abortSignal?.aborted)return Q("Operation cancelled");if(!q.action)return Q("action is required. Valid actions: list, search, get, current, stats, delete, set_title");let B=await N(z);if(!B)return Q("Session history is not available in this context");try{switch(q.action){case"list":return P(q,B);case"search":return g(q,B);case"get":return k(q,B);case"current":return b(z,B);case"stats":return w(B);case"delete":return H(q,B);case"set_title":return _(q,B);default:return Q(`Unknown action "${q.action}". Valid actions: list, search, get, current, stats, delete, set_title`)}}catch(J){return Q(C(J))}}function P(q,z){let B=q.limit??20,J=q.offset??0,V=z.listSessions(B,J),R=E(V),W={sessions:R,total:R.length,limit:B,offset:J};if(R.length===0)return U("No sessions found",W);let Y=R.map((K)=>`• ${K.id.slice(0,8)}… | ${K.title||"(untitled)"} | ${K.messageCount} msgs | ${K.arion} | ${K.updatedAt.slice(0,10)}`);return U(`Found ${R.length} session(s):
|
|
2
2
|
${Y.join(`
|
|
3
3
|
`)}`,W)}function g(q,z){if(!q.query||q.query.trim()==="")return Q("query is required for 'search' action");let B=q.limit??20,J=q.offset??0,V=z.searchSessionsFts(q.query,B,J),R=E(V),W={sessions:R,total:R.length,limit:B,offset:J};if(R.length===0)return U(`No sessions found matching "${q.query}"`,W);let Y=R.map((K)=>`• ${K.id.slice(0,8)}… | ${K.title||"(untitled)"} | ${K.messageCount} msgs | ${K.arion} | ${K.updatedAt.slice(0,10)}`);return U(`Found ${R.length} session(s) matching "${q.query}":
|
|
4
4
|
${Y.join(`
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{pc as _,qc as X,sc as M}from"./index-6qew5tp1.js";import{
|
|
1
|
+
import{pc as _,qc as X,sc as M}from"./index-6qew5tp1.js";import{tc as Wq}from"./index-tvryzx00.js";import*as a from"node-pty";import{spawn as m}from"node:child_process";import{constants as l}from"node:os";var r=new Set(["ls","cat","head","tail","wc","file","stat","grep","rg","find","which","whereis","echo","date","whoami","pwd","printenv","uname","hostname"]),n=["git stash list","git status","git log","git diff","git show","git blame","git branch","git remote","git tag","node --version","npm --version","pnpm --version","python --version","pnpm list","npm list"],s=[/rm\s+(?:--?[A-Za-z0-9-]+\s+)*\/(?:\s|$)/,/rm\s+(?:--?[A-Za-z0-9-]+\s+)*\/\*(?:\s|$)/,/rm\s+(?:--?[A-Za-z0-9-]+\s+)*\.(?:\s|$)/,/rm\s+(?:--?[A-Za-z0-9-]+\s+)*~(?:[a-zA-Z]\w*)?(?:\/\*)?(?:\s|$)/,/rm\s+(?:--?[A-Za-z0-9-]+\s+)*\$HOME\b/,/rm\s+(?:--?[A-Za-z0-9-]+\s+)*\*(?:\s|$)/,/>\s*\/dev\/(?:sd[a-z]|nvme\d+|vd[a-z])\b/,/mkfs/,/dd\s+.*(?:if=|of=)/,/chmod\s+(?:-R\s+)?777\b/,/curl[\s\S]*\|\s*(ba)?sh/,/wget[\s\S]*\|\s*(ba)?sh/,/(?:^|[;&|]\s*|\$\(|`|\()\s*(?:(?:env|command)\s+)*eval\b/,/(?:^|[;&|]\s*|\$\(|`|\()\s*(?:(?:env|command)\s+)*shutdown\b/,/(?:^|[;&|]\s*|\$\(|`|\()\s*(?:(?:env|command)\s+)*reboot\b/,/(?:^|[;&|]\s*|\$\(|`|\()\s*(?:(?:env|command)\s+)*halt\b/,/(?:^|[;&|]\s*|\$\(|`|\()\s*(?:(?:env|command)\s+)*init\s+0\b/,/(?:^|[;&|]\s*|\$\(|`|\()\s*(?:(?:env|command)\s+)*systemctl\s+(?:poweroff|halt|reboot)\b/,/(?:^|[;&|]\s*|\$\(|`|\()\s*(?:(?:env|command)\s+)*kill\s+(?:-\d+\s+|-[A-Z]+\s+)*\b1\b/,/:\(\)\{\s*:\|:&\s*\};:/,/\bsudo\b/,/git\s+push\s+.*--force(?!-with-lease)\b/,/git\s+push(?:\s+-[A-Za-z]*f[A-Za-z]*\b|\s+.*\s-[A-Za-z]*f[A-Za-z]*\b)/,/git\s+reset\s+--hard/];function y(q){let J=v(q),Z=i(J);return s.some(($)=>$.test(Z))}function v(q){let J=/<<-?\s*(?:'([^']+)'|"([^"]+)"|(\w+))/g,Z=q,$,U=[];while(($=J.exec(q))!==null){let W=$[1]??$[2]??$[3]??"";if(!W)continue;let Q=q.indexOf(`
|
|
2
2
|
`,$.index);if(Q===-1)continue;let F=Q+1,H=new RegExp(`^\\s*${W.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}\\s*$`,"m"),K=q.slice(F),j=H.exec(K);if(!j)continue;let L=F+j.index;U.push({delimiter:W,bodyStart:F,bodyEnd:L})}for(let W=U.length-1;W>=0;W--){let{bodyStart:Q,bodyEnd:F}=U[W],H=Z.slice(Q,F);Z=Z.slice(0,Q)+H.replace(/[^\n]/g," ")+Z.slice(F)}return Z}function i(q){let J="",Z=!1,$=!1,U=!1;for(let W=0;W<q.length;W++){let Q=q[W];if(Z){if(Q==="'")Z=!1,J+=" ";else J+=" ";continue}if($){if(U){U=!1,J+=" ";continue}if(Q==="\\"){U=!0,J+=" ";continue}if(Q==='"')$=!1,J+=" ";else J+=" ";continue}if(Q==="'"){Z=!0,J+=" ";continue}if(Q==='"'){$=!0,J+=" ";continue}J+=Q}return J}function x(q){let J=q.lastIndexOf("/");return J===-1?q:q.slice(J+1)}function t(q){let J=!1,Z=!1,$=!1;for(let U=0;U<q.length;U++){let W=q[U],Q=q[U+1];if(J){if(W==="'")J=!1;continue}if(Z){if($){$=!1;continue}if(W==="\\"){$=!0;continue}if(W==='"'){Z=!1;continue}if(W==="`"||W==="$"&&Q==="(")return!0;continue}if($){$=!1;continue}if(W==="\\"){$=!0;continue}if(W==="'"){J=!0;continue}if(W==='"'){Z=!0;continue}if(W==="`"||W==="$"&&Q==="(")return!0}return!1}function e(q){let J=[],Z="",$=!1,U=!1,W=!1;for(let Q=0;Q<q.length;Q++){let F=q[Q],H=q[Q+1];if($){if(Z+=F,F==="'")$=!1;continue}if(U){if(Z+=F,W){W=!1;continue}if(F==="\\"){W=!0;continue}if(F==='"')U=!1;continue}if(W){Z+=F,W=!1;continue}if(F==="\\"){Z+=F,W=!0;continue}if(F==="'"){Z+=F,$=!0;continue}if(F==='"'){Z+=F,U=!0;continue}if(F===";"||F===`
|
|
3
|
-
`||F==="\r"||F==="&"&&H==="&"||F==="|"&&H==="|"){if(J.push(Z.trim()),Z="",(F==="&"||F==="|")&&H===F)Q+=1;continue}Z+=F}return J.push(Z.trim()),J}function qq(q){let J=[],Z="",$=!1,U=!1,W=!1;for(let Q=0;Q<q.length;Q++){let F=q[Q],H=q[Q+1],K=Q>0?q[Q-1]:"";if($){if(Z+=F,F==="'")$=!1;continue}if(U){if(Z+=F,W){W=!1;continue}if(F==="\\"){W=!0;continue}if(F==='"')U=!1;continue}if(W){Z+=F,W=!1;continue}if(F==="\\"){Z+=F,W=!0;continue}if(F==="'"){Z+=F,$=!0;continue}if(F==='"'){Z+=F,U=!0;continue}if(F==="|"&&H!=="|"&&K!=="|"){J.push(Z.trim()),Z="";continue}Z+=F}return J.push(Z.trim()),J}var Jq=/(?:>>|(?:^|[^2])>(?!&)|2>(?!&)|&>)/;function Zq(q){let J=q.trim();if(J==="")return!1;if(Jq.test(J))return!1;for(let $ of n)if(J===$||J.startsWith($+" "))return!0;let Z=J.split(/\s+/)[0]??"";return r.has(x(Z))}function C(q){let J=q.trim();if(J==="")return"moderate";let Z=v(J);if(y(Z))return"blocked";if(t(Z))return"moderate";let $=e(Z);for(let Q of $)if(y(Q))return"blocked";let U=[];for(let Q of $){let F=qq(Q);for(let H of F){if(y(H))return"blocked";U.push(H)}}if(/^[A-Za-z_]\w*=/.test(J))return"moderate";let W=U.filter((Q)=>Q.trim()!=="");if(W.length>0&&W.every(Zq))return"safe";return"moderate"}var $q=new Set(["sh","bash","zsh","ksh","dash","ash","fish"]);function S(q,J=[]){let Z=x(q).toLowerCase();if($q.has(Z)&&J[0]==="-c"&&typeof J[1]==="string")return C(J[1]);return C([q,...J].join(" "))}var u=new Set(["LD_PRELOAD","LD_LIBRARY_PATH","DYLD_INSERT_LIBRARIES","DYLD_FRAMEWORK_PATH","DYLD_LIBRARY_PATH","BASH_ENV","ENV","CDPATH","GLOBIGNORE","PROMPT_COMMAND","SHELLOPTS","BASHOPTS","GIT_DIR","GIT_WORK_TREE","GIT_INDEX_FILE","GIT_OBJECT_DIRECTORY","GIT_ALTERNATE_OBJECT_DIRECTORIES","GIT_COMMON_DIR","GIT_PREFIX","GIT_INTERNAL_SUPER_PREFIX","GIT_CONFIG","GIT_CONFIG_GLOBAL","GIT_CONFIG_SYSTEM","GIT_CONFIG_COUNT","GIT_CEILING_DIRECTORIES"]),Fq=new Set(["GIT_DIR","GIT_WORK_TREE","GIT_INDEX_FILE","GIT_INDEX_VERSION","GIT_COMMON_DIR","GIT_OBJECT_DIRECTORY","GIT_ALTERNATE_OBJECT_DIRECTORIES","GIT_CEILING_DIRECTORIES","GIT_DISCOVERY_ACROSS_FILESYSTEM","GIT_NAMESPACE","GIT_CONFIG","GIT_CONFIG_GLOBAL","GIT_CONFIG_SYSTEM","GIT_CONFIG_NOSYSTEM","GIT_CONFIG_COUNT","GIT_CONFIG_PARAMETERS"]),Qq=["GIT_CONFIG_KEY_","GIT_CONFIG_VALUE_","GIT_AUTHOR_","GIT_COMMITTER_"];function f(q){let J=q.toUpperCase();if(Fq.has(J))return!0;return Qq.some((Z)=>J.startsWith(Z))}function Cq(q=process.env){let J={};for(let[Z,$]of Object.entries(q)){if($===void 0)continue;if(f(Z))continue;J[Z]=$}return J}var Uq=new Set([...u,"PATH","NODE_OPTIONS","NODE_PATH","PYTHONPATH","IFS","HOME","SHELL","EDITOR","VISUAL","JAVA_TOOL_OPTIONS","_JAVA_OPTIONS","CLASSPATH","RUBYOPT","GEM_HOME","GEM_PATH","PERL5OPT","PERL5LIB","PYTHONSTARTUP"]);function d(q){return u.has(q.toUpperCase())||f(q)}function w(q){return Uq.has(q.toUpperCase())||f(q)}function R(q,J){let Z=J;if(J){Z={};for(let[W,Q]of Object.entries(J)){if(w(W))continue;Z[W]=Q}}let $=q.env;if(q.env){$={};for(let[W,Q]of Object.entries(q.env)){if(w(W))continue;$[W]=Q}}let U={};for(let[W,Q]of Object.entries(process.env)){if(Q===void 0)continue;if(d(W))continue;U[W]=Q}return{...U,...$,...Z}}function p(q){let J={};for(let[$,U]of Object.entries(process.env)){if(U===void 0)continue;if(d($))continue;J[$]=U}let Z;if(q){Z={};for(let[$,U]of Object.entries(q)){if(w($))continue;Z[$]=U}}return{...J,...Z}}async function Rq(q,J){if(C(q.command)==="blocked")return X(`Command blocked by shell safety policy: ${q.command}`);if(J.abortSignal?.aborted)return X("Command cancelled");let $=q.cwd??J.workingDir,U=R(J,q.env),W=q.timeout??120000;return Hq({program:"/bin/bash",args:["-lc",q.command],cwd:$,env:q.env,timeout:W},{...J,workingDir:$,env:U})}function G(q,J,Z){if(!q)return;if(Z)try{process.kill(-q,J);return}catch{}try{process.kill(q,J)}catch{}}function I(q,J){if(!q)return!1;if(J)try{return process.kill(-q,0),!0}catch{}try{return process.kill(q,0),!0}catch{return!1}}async function Xq(q,J,Z=1000){if(!q)return;let $=Date.now()+Z;while(Date.now()<$){if(!I(q,J))return;await new Promise((U)=>setTimeout(U,25))}}async function Hq(q,J){let Z=q.args??[],$=[q.program,...Z].join(" ");if(S(q.program,Z)==="blocked")return X(`Command blocked by shell safety policy: ${$}`,{stdout:"",stderr:"",exitCode:1});if(J.abortSignal?.aborted)return X("Command cancelled",{stdout:"",stderr:"",exitCode:1});let W=q.cwd??J.workingDir,Q=R(J,q.env),F=q.timeout??120000;return new Promise((H)=>{let K=process.platform!=="win32",j=m(q.program,Z,{cwd:W,env:Q,detached:K});if(j.pid&&J.processRegistry)J.processRegistry.add(j.pid,{command:q.program,args:Z,cwd:W,interactive:!1});let L="",z="",B=!1,Y=!1,h=!1,k,D,A=(O,V)=>{if(h)return;if(h=!0,k)clearTimeout(k);if(D)clearTimeout(D);if(J.abortSignal&&b)J.abortSignal.removeEventListener("abort",b);if(J.processRegistry?.recordExitMetadata?.(j.pid??0,V),j.pid&&J.processRegistry&&!(K&&I(j.pid,K)))J.processRegistry.remove(j.pid,V);H(O)},b;if(J.abortSignal)b=()=>{Y=!0,G(j.pid,"SIGTERM",K),D=setTimeout(()=>{G(j.pid,"SIGKILL",K)},1000)},J.abortSignal.addEventListener("abort",b,{once:!0});if(F)k=setTimeout(()=>{B=!0,G(j.pid,"SIGTERM",K),D=setTimeout(()=>{G(j.pid,"SIGKILL",K)},1000)},F);j.stdout?.on("data",(O)=>{L+=O.toString()}),j.stderr?.on("data",(O)=>{z+=O.toString()}),j.on("error",(O)=>{let V=typeof O==="object"&&O&&"code"in O&&O.code==="ENOENT"?127:1;A(X(M(O),{stdout:"",stderr:z,exitCode:V}))}),j.on("close",(O,V)=>{(async()=>{if(Y||B){if(D)clearTimeout(D),D=void 0;G(j.pid,"SIGKILL",K),await Xq(j.pid,K)}if(Y){A(X("Command cancelled",{stdout:L,stderr:z,exitCode:O??1}),{exitCode:O,signal:V});return}if(B){A(X("Command timed out",{stdout:L,stderr:z,exitCode:O??1}),{exitCode:O,signal:V});return}if(V){let c=l.signals[V]??1;A(X(`Command exited due to signal ${V}`,{stdout:L,stderr:z,exitCode:O??c}),{exitCode:O,signal:V});return}let N=O??0;if(N===0)A(_("Command executed successfully",{stdout:L,stderr:z,exitCode:N}),{exitCode:N,signal:V});else A(X(`Command exited with code ${N}`,{stdout:L,stderr:z,exitCode:N}),{exitCode:N,signal:V})})()})})}async function Tq(q,J){let Z=q.args??[],$=[q.program,...Z].join(" ");if(S(q.program,Z)==="blocked")return X(`Command blocked by shell safety policy: ${$}`);if(J.abortSignal?.aborted)return X("Command cancelled");if(q.interactive===!0){if(!J.ptySessionStore)return X("PTY session store not available — interactive mode requires runner wiring");let{createPTYSession:F}=await import("./pty-ff59hy75.js"),H=await F({command:q.program,args:q.args,cwd:q.cwd??J.workingDir,env:R(J,q.env)}),K=H.pid;if(!K)return H.close(),X("Failed to start interactive session: no PID");if(J.ptySessionStore.add(K,H),J.processRegistry)J.processRegistry.add(K,{command:q.program,args:q.args??[],cwd:q.cwd??J.workingDir,interactive:!0});let j=()=>{if(J.ptySessionStore?.remove(K),J.processRegistry?.has(K))J.processRegistry.remove(K,{exitCode:H.exitCode??null})};if(!H.isRunning)j();else{let L=setInterval(()=>{if(!H.isRunning)clearInterval(L),j()},50);if(L.unref?.(),J.abortSignal){let z=()=>{clearInterval(L),H.close(),j()};J.abortSignal.addEventListener("abort",z,{once:!0})}}return _(`Started interactive session with PID ${K}`,{pid:K})}let W=q.cwd??J.workingDir,Q=R(J,q.env);return new Promise((F)=>{let H=!1,K,j=(z)=>{if(H)return;if(H=!0,K)clearTimeout(K);F(z)},L;if(J.abortSignal)J.abortSignal.addEventListener("abort",()=>{if(L)try{process.kill(-L,"SIGTERM")}catch{try{process.kill(L,"SIGTERM")}catch{}}},{once:!0});try{let z=m(q.program,Z,{cwd:W,env:Q,detached:!0,stdio:"ignore"});if(z.on("error",(B)=>{j(X(M(B)))}),z.pid){if(L=z.pid,z.on("exit",(B,Y)=>{if(J.processRegistry?.recordExitMetadata?.(z.pid,{exitCode:B,signal:Y}),J.processRegistry&&!I(z.pid,!0))J.processRegistry.remove(z.pid,{exitCode:B,signal:Y})}),J.processRegistry)J.processRegistry.add(z.pid,{command:q.program,args:Z,cwd:W,interactive:!1});z.unref(),j(_(`Spawned process with PID ${z.pid}`,{pid:z.pid}))}else K=setTimeout(()=>{j(X("Failed to spawn process: no PID returned"))},100)}catch(z){j(X(M(z)))}})}async function Pq(q,J){let Z=q.signal??"SIGTERM";try{let $=zq(Z);if($===void 0)return X(`Invalid signal: ${Z}`);if(J.processRegistry?.has(q.pid))Oq(q.pid,$);else process.kill(q.pid,$);let U=J.processRegistry;if(U?.waitForExit&&Lq($))await U.waitForExit(q.pid,2000);let W=typeof $==="number"?Z:$;return _(`Sent ${W} to process ${q.pid}`,{pid:q.pid,signal:W})}catch($){if($ instanceof Error&&"code"in $){let U=$;if(U.code==="ESRCH")return X(`Process not found: ${q.pid}`);if(U.code==="EPERM")return X(`Permission denied to kill process: ${q.pid}`)}return X(M($))}}var Kq=["SIGHUP","SIGINT","SIGQUIT","SIGILL","SIGTRAP","SIGABRT","SIGBUS","SIGFPE","SIGKILL","SIGUSR1","SIGSEGV","SIGUSR2","SIGPIPE","SIGALRM","SIGTERM","SIGSTKFLT","SIGCHLD","SIGCONT","SIGSTOP","SIGTSTP","SIGTTIN","SIGTTOU","SIGURG","SIGXCPU","SIGXFSZ","SIGVTALRM","SIGPROF","SIGWINCH","SIGIO","SIGPWR","SIGSYS"],g=new Set(["SIGCHLD","SIGCONT","SIGSTOP","SIGTSTP","SIGTTIN","SIGTTOU","SIGURG","SIGWINCH"]);function jq(q){return Kq.includes(q.toUpperCase())}function zq(q){if(/^\d+$/.test(q)){let Z=parseInt(q,10);if(Z>=1&&Z<=31)return Z;return}let J=q.toUpperCase();if(jq(J))return J;return}function Lq(q){if(typeof q==="number"){let J=Vq(q);if(!J)return!1;return!g.has(J)}return!g.has(q)}function Oq(q,J){try{if(q>0){process.kill(-q,J);return}}catch{}process.kill(q,J)}function Vq(q){for(let[J,Z]of Object.entries(l.signals))if(Z===q)return J;return}function Bq(q){return typeof q.getAll==="function"}async function kq(q,J){let Z=o(q),$=typeof Z?.includeExited==="boolean"?Z.includeExited:!1;if(!J.processRegistry)return X("Process registry not available");let U=J.processRegistry,W=typeof U.listProcesses==="function"?U.listProcesses({includeExited:$}):Bq(U)?U.getAll().map((Q)=>({pid:Q,command:null,args:[],cwd:null,interactive:!1,startedAt:new Date().toISOString(),runtimeMs:0,status:"running",exitCode:null,signal:null,endedAt:null})):[];return _(`Found ${W.length} tracked process${W.length===1?"":"es"}`,{processes:W,count:W.length})}async function yq(q,J){let Z=o(q);if(!Z)return X("Invalid input: expected an object");let $=typeof Z.pid==="number"?Z.pid:NaN;if(!$||$<=0||!Number.isInteger($))return X("Invalid PID: must be a positive integer");if($===process.pid)return X("Cannot wait on own process");let U=Z.timeoutMs;if(U!==void 0&&(typeof U!=="number"||Number.isNaN(U)))return X("Invalid timeoutMs: must be a number");let W=Z.timeout;if(W!==void 0&&(typeof W!=="number"||Number.isNaN(W)))return X("Invalid timeout: must be a number");let Q=Math.min(Math.max(0,U??W??30000),300000);if(!J.processRegistry)return X("Process registry not available");if(typeof J.processRegistry.waitForExit!=="function")return X("Process registry does not support waiting");let F=Date.now(),H=await J.processRegistry.waitForExit($,Q),K=Math.max(0,Date.now()-F);if(H.status==="not_found"||!H.process)return X(`Process not tracked: ${$}`,{pid:$,exited:!1,status:"not_found",timedOut:!1,waitedMs:K});let j=Yq(H,K);if(H.status==="running"||H.timedOut)return X(`Timed out waiting for process ${$}`,j);return _(`Process ${$} exited`,j)}function Yq(q,J){let Z=q.process,$=q.status==="exited"?"exited":"running";return{pid:q.pid,status:$,exited:$==="exited",timedOut:q.timedOut,waitedMs:J,command:Z.command,args:Z.args,cwd:Z.cwd,interactive:Z.interactive,startedAt:Z.startedAt,endedAt:Z.endedAt,runtimeMs:Z.runtimeMs,exitCode:Z.exitCode,signal:Z.signal}}function o(q){if(!q||typeof q!=="object")return;return q}async function Sq(q,J){if(!q||typeof q!=="object")return X("Invalid input: expected an object");let Z=q,$=typeof Z.pid==="number"?Z.pid:NaN,U=typeof Z.input==="string"?Z.input:void 0,W=typeof Z.timeout==="number"?Z.timeout:void 0;if(!$||$<=0||!Number.isInteger($))return X("Invalid PID: must be a positive integer");if($===process.pid)return X("Cannot write to own process");if(U===void 0)return X("Input must be a string");if(!J.ptySessionStore)return X("PTY session store not available");if(!J.ptySessionStore.has($))return X(`No interactive session found for PID ${$}. Use spawn with interactive=true first.`);let Q=J.ptySessionStore.get($);if(!Q)return X(`No interactive session found for PID ${$}. Use spawn with interactive=true first.`);if(!Q.isRunning)return X(`Process ${$} has exited (code: ${Q.exitCode??"unknown"})`);try{let F=Q.output.length;if(Q.write(U),W&&W>0){let L=Math.min(W,30000),z=Date.now()+L,B=50;while(Date.now()<z){if(await new Promise((Y)=>setTimeout(Y,50)),Q.output.length>F)break;if(!Q.isRunning)break}}let K=Q.output.slice(F),j=K.length>1e4?K.slice(-1e4):K;return _(`Wrote ${U.length} bytes to PID ${$}`,{output:j})}catch(F){let H=F instanceof Error?F.message:String(F);return X(`Failed to write to PID ${$}: ${H}`)}}class E extends Error{output;constructor(q,J){super(q);this.output=J;this.name="PTYTimeoutError"}}class T extends Error{output;constructor(q,J){super(q);this.output=J;this.name="PTYError"}}class P{static MAX_OUTPUT=10485760;_output="";_isRunning=!0;_exitCode;_exitPromise;_exitResolve;_pty;_dataDisposable;_exitDisposable;constructor(q){this._pty=q,this._exitPromise=new Promise((J)=>{this._exitResolve=J}),this._dataDisposable=q.onData((J)=>{if(this._output+=J,this._output.length>P.MAX_OUTPUT)this._output=this._output.slice(-P.MAX_OUTPUT)}),this._exitDisposable=q.onExit(({exitCode:J})=>{this._isRunning=!1,this._exitCode=J,this._exitResolve(J)})}write(q){if(!this._isRunning)throw new T("Cannot write to a closed PTY session");this._pty.write(q)}resize(q,J){if(!this._isRunning)throw new T("Cannot resize a closed PTY session");this._pty.resize(q,J)}async waitFor(q,J=5000){let Z=Date.now(),$=typeof q==="string"?new RegExp(Dq(q)):q;if($.test(this._output))return this._output;return new Promise((U,W)=>{let Q,F,H=()=>{if(Q)clearInterval(Q);if(F)clearTimeout(F)},K=()=>{if($.test(this._output))return H(),U(this._output),!0;if(!this._isRunning)return H(),W(new T(`Process exited before pattern was found: ${q}`,this._output)),!0;return!1};if(K())return;Q=setInterval(()=>{K()},10),F=setTimeout(()=>{H();let j=Date.now()-Z;W(new E(`Timeout (${j}ms) waiting for pattern: ${q}`,this._output))},J)})}async waitForExit(q=30000){if(!this._isRunning)return this._output;return new Promise((J,Z)=>{let $;$=setTimeout(()=>{Z(new E(`Timeout (${q}ms) waiting for process to exit`,this._output))},q),this._exitPromise.then(()=>{if($)clearTimeout($);J(this._output)}).catch(Z)})}kill(q){if(!this._isRunning)return;try{this._pty.kill(q)}catch{}}close(){if(this._isRunning)this.kill("SIGKILL"),this._isRunning=!1,this._exitResolve(-1);this._dataDisposable.dispose(),this._exitDisposable.dispose()}get output(){return this._output}get isRunning(){return this._isRunning}get pid(){return this._pty.pid}get exitCode(){return this._exitCode}get cols(){return this._pty.cols}get rows(){return this._pty.rows}}async function _q(q){let $=a.spawn(q.command,q.args??[],{name:"xterm-256color",cols:q.cols??80,rows:q.rows??24,cwd:q.cwd??process.cwd(),env:{...p(q.env),TERM:"xterm-256color"}});return new P($)}async function Iq(q){let{timeoutMs:J=30000,...Z}=q,$=await _q(Z);try{return{output:await $.waitForExit(J),exitCode:$.exitCode??0}}finally{$.close()}}function Dq(q){return q.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}
|
|
3
|
+
`||F==="\r"||F==="&"&&H==="&"||F==="|"&&H==="|"){if(J.push(Z.trim()),Z="",(F==="&"||F==="|")&&H===F)Q+=1;continue}Z+=F}return J.push(Z.trim()),J}function qq(q){let J=[],Z="",$=!1,U=!1,W=!1;for(let Q=0;Q<q.length;Q++){let F=q[Q],H=q[Q+1],K=Q>0?q[Q-1]:"";if($){if(Z+=F,F==="'")$=!1;continue}if(U){if(Z+=F,W){W=!1;continue}if(F==="\\"){W=!0;continue}if(F==='"')U=!1;continue}if(W){Z+=F,W=!1;continue}if(F==="\\"){Z+=F,W=!0;continue}if(F==="'"){Z+=F,$=!0;continue}if(F==='"'){Z+=F,U=!0;continue}if(F==="|"&&H!=="|"&&K!=="|"){J.push(Z.trim()),Z="";continue}Z+=F}return J.push(Z.trim()),J}var Jq=/(?:>>|(?:^|[^2])>(?!&)|2>(?!&)|&>)/;function Zq(q){let J=q.trim();if(J==="")return!1;if(Jq.test(J))return!1;for(let $ of n)if(J===$||J.startsWith($+" "))return!0;let Z=J.split(/\s+/)[0]??"";return r.has(x(Z))}function C(q){let J=q.trim();if(J==="")return"moderate";let Z=v(J);if(y(Z))return"blocked";if(t(Z))return"moderate";let $=e(Z);for(let Q of $)if(y(Q))return"blocked";let U=[];for(let Q of $){let F=qq(Q);for(let H of F){if(y(H))return"blocked";U.push(H)}}if(/^[A-Za-z_]\w*=/.test(J))return"moderate";let W=U.filter((Q)=>Q.trim()!=="");if(W.length>0&&W.every(Zq))return"safe";return"moderate"}var $q=new Set(["sh","bash","zsh","ksh","dash","ash","fish"]);function S(q,J=[]){let Z=x(q).toLowerCase();if($q.has(Z)&&J[0]==="-c"&&typeof J[1]==="string")return C(J[1]);return C([q,...J].join(" "))}var u=new Set(["LD_PRELOAD","LD_LIBRARY_PATH","DYLD_INSERT_LIBRARIES","DYLD_FRAMEWORK_PATH","DYLD_LIBRARY_PATH","BASH_ENV","ENV","CDPATH","GLOBIGNORE","PROMPT_COMMAND","SHELLOPTS","BASHOPTS","GIT_DIR","GIT_WORK_TREE","GIT_INDEX_FILE","GIT_OBJECT_DIRECTORY","GIT_ALTERNATE_OBJECT_DIRECTORIES","GIT_COMMON_DIR","GIT_PREFIX","GIT_INTERNAL_SUPER_PREFIX","GIT_CONFIG","GIT_CONFIG_GLOBAL","GIT_CONFIG_SYSTEM","GIT_CONFIG_COUNT","GIT_CEILING_DIRECTORIES"]),Fq=new Set(["GIT_DIR","GIT_WORK_TREE","GIT_INDEX_FILE","GIT_INDEX_VERSION","GIT_COMMON_DIR","GIT_OBJECT_DIRECTORY","GIT_ALTERNATE_OBJECT_DIRECTORIES","GIT_CEILING_DIRECTORIES","GIT_DISCOVERY_ACROSS_FILESYSTEM","GIT_NAMESPACE","GIT_CONFIG","GIT_CONFIG_GLOBAL","GIT_CONFIG_SYSTEM","GIT_CONFIG_NOSYSTEM","GIT_CONFIG_COUNT","GIT_CONFIG_PARAMETERS"]),Qq=["GIT_CONFIG_KEY_","GIT_CONFIG_VALUE_","GIT_AUTHOR_","GIT_COMMITTER_"];function f(q){let J=q.toUpperCase();if(Fq.has(J))return!0;return Qq.some((Z)=>J.startsWith(Z))}function Cq(q=process.env){let J={};for(let[Z,$]of Object.entries(q)){if($===void 0)continue;if(f(Z))continue;J[Z]=$}return J}var Uq=new Set([...u,"PATH","NODE_OPTIONS","NODE_PATH","PYTHONPATH","IFS","HOME","SHELL","EDITOR","VISUAL","JAVA_TOOL_OPTIONS","_JAVA_OPTIONS","CLASSPATH","RUBYOPT","GEM_HOME","GEM_PATH","PERL5OPT","PERL5LIB","PYTHONSTARTUP"]);function d(q){return u.has(q.toUpperCase())||f(q)}function w(q){return Uq.has(q.toUpperCase())||f(q)}function R(q,J){let Z=J;if(J){Z={};for(let[W,Q]of Object.entries(J)){if(w(W))continue;Z[W]=Q}}let $=q.env;if(q.env){$={};for(let[W,Q]of Object.entries(q.env)){if(w(W))continue;$[W]=Q}}let U={};for(let[W,Q]of Object.entries(process.env)){if(Q===void 0)continue;if(d(W))continue;U[W]=Q}return{...U,...$,...Z}}function p(q){let J={};for(let[$,U]of Object.entries(process.env)){if(U===void 0)continue;if(d($))continue;J[$]=U}let Z;if(q){Z={};for(let[$,U]of Object.entries(q)){if(w($))continue;Z[$]=U}}return{...J,...Z}}async function Rq(q,J){if(C(q.command)==="blocked")return X(`Command blocked by shell safety policy: ${q.command}`);if(J.abortSignal?.aborted)return X("Command cancelled");let $=q.cwd??J.workingDir,U=R(J,q.env),W=q.timeout??120000;return Hq({program:"/bin/bash",args:["-lc",q.command],cwd:$,env:q.env,timeout:W},{...J,workingDir:$,env:U})}function G(q,J,Z){if(!q)return;if(Z)try{process.kill(-q,J);return}catch{}try{process.kill(q,J)}catch{}}function I(q,J){if(!q)return!1;if(J)try{return process.kill(-q,0),!0}catch{}try{return process.kill(q,0),!0}catch{return!1}}async function Xq(q,J,Z=1000){if(!q)return;let $=Date.now()+Z;while(Date.now()<$){if(!I(q,J))return;await new Promise((U)=>setTimeout(U,25))}}async function Hq(q,J){let Z=q.args??[],$=[q.program,...Z].join(" ");if(S(q.program,Z)==="blocked")return X(`Command blocked by shell safety policy: ${$}`,{stdout:"",stderr:"",exitCode:1});if(J.abortSignal?.aborted)return X("Command cancelled",{stdout:"",stderr:"",exitCode:1});let W=q.cwd??J.workingDir,Q=R(J,q.env),F=q.timeout??120000;return new Promise((H)=>{let K=process.platform!=="win32",j=m(q.program,Z,{cwd:W,env:Q,detached:K});if(j.pid&&J.processRegistry)J.processRegistry.add(j.pid,{command:q.program,args:Z,cwd:W,interactive:!1});let L="",z="",B=!1,Y=!1,h=!1,k,D,A=(O,V)=>{if(h)return;if(h=!0,k)clearTimeout(k);if(D)clearTimeout(D);if(J.abortSignal&&b)J.abortSignal.removeEventListener("abort",b);if(J.processRegistry?.recordExitMetadata?.(j.pid??0,V),j.pid&&J.processRegistry&&!(K&&I(j.pid,K)))J.processRegistry.remove(j.pid,V);H(O)},b;if(J.abortSignal)b=()=>{Y=!0,G(j.pid,"SIGTERM",K),D=setTimeout(()=>{G(j.pid,"SIGKILL",K)},1000)},J.abortSignal.addEventListener("abort",b,{once:!0});if(F)k=setTimeout(()=>{B=!0,G(j.pid,"SIGTERM",K),D=setTimeout(()=>{G(j.pid,"SIGKILL",K)},1000)},F);j.stdout?.on("data",(O)=>{L+=O.toString()}),j.stderr?.on("data",(O)=>{z+=O.toString()}),j.on("error",(O)=>{let V=typeof O==="object"&&O&&"code"in O&&O.code==="ENOENT"?127:1;A(X(M(O),{stdout:"",stderr:z,exitCode:V}))}),j.on("close",(O,V)=>{(async()=>{if(Y||B){if(D)clearTimeout(D),D=void 0;G(j.pid,"SIGKILL",K),await Xq(j.pid,K)}if(Y){A(X("Command cancelled",{stdout:L,stderr:z,exitCode:O??1}),{exitCode:O,signal:V});return}if(B){A(X("Command timed out",{stdout:L,stderr:z,exitCode:O??1}),{exitCode:O,signal:V});return}if(V){let c=l.signals[V]??1;A(X(`Command exited due to signal ${V}`,{stdout:L,stderr:z,exitCode:O??c}),{exitCode:O,signal:V});return}let N=O??0;if(N===0)A(_("Command executed successfully",{stdout:L,stderr:z,exitCode:N}),{exitCode:N,signal:V});else A(X(`Command exited with code ${N}`,{stdout:L,stderr:z,exitCode:N}),{exitCode:N,signal:V})})()})})}async function Tq(q,J){let Z=q.args??[],$=[q.program,...Z].join(" ");if(S(q.program,Z)==="blocked")return X(`Command blocked by shell safety policy: ${$}`);if(J.abortSignal?.aborted)return X("Command cancelled");if(q.interactive===!0){if(!J.ptySessionStore)return X("PTY session store not available — interactive mode requires runner wiring");let{createPTYSession:F}=await import("./pty-cf6pmvx6.js"),H=await F({command:q.program,args:q.args,cwd:q.cwd??J.workingDir,env:R(J,q.env)}),K=H.pid;if(!K)return H.close(),X("Failed to start interactive session: no PID");if(J.ptySessionStore.add(K,H),J.processRegistry)J.processRegistry.add(K,{command:q.program,args:q.args??[],cwd:q.cwd??J.workingDir,interactive:!0});let j=()=>{if(J.ptySessionStore?.remove(K),J.processRegistry?.has(K))J.processRegistry.remove(K,{exitCode:H.exitCode??null})};if(!H.isRunning)j();else{let L=setInterval(()=>{if(!H.isRunning)clearInterval(L),j()},50);if(L.unref?.(),J.abortSignal){let z=()=>{clearInterval(L),H.close(),j()};J.abortSignal.addEventListener("abort",z,{once:!0})}}return _(`Started interactive session with PID ${K}`,{pid:K})}let W=q.cwd??J.workingDir,Q=R(J,q.env);return new Promise((F)=>{let H=!1,K,j=(z)=>{if(H)return;if(H=!0,K)clearTimeout(K);F(z)},L;if(J.abortSignal)J.abortSignal.addEventListener("abort",()=>{if(L)try{process.kill(-L,"SIGTERM")}catch{try{process.kill(L,"SIGTERM")}catch{}}},{once:!0});try{let z=m(q.program,Z,{cwd:W,env:Q,detached:!0,stdio:"ignore"});if(z.on("error",(B)=>{j(X(M(B)))}),z.pid){if(L=z.pid,z.on("exit",(B,Y)=>{if(J.processRegistry?.recordExitMetadata?.(z.pid,{exitCode:B,signal:Y}),J.processRegistry&&!I(z.pid,!0))J.processRegistry.remove(z.pid,{exitCode:B,signal:Y})}),J.processRegistry)J.processRegistry.add(z.pid,{command:q.program,args:Z,cwd:W,interactive:!1});z.unref(),j(_(`Spawned process with PID ${z.pid}`,{pid:z.pid}))}else K=setTimeout(()=>{j(X("Failed to spawn process: no PID returned"))},100)}catch(z){j(X(M(z)))}})}async function Pq(q,J){let Z=q.signal??"SIGTERM";try{let $=zq(Z);if($===void 0)return X(`Invalid signal: ${Z}`);if(J.processRegistry?.has(q.pid))Oq(q.pid,$);else process.kill(q.pid,$);let U=J.processRegistry;if(U?.waitForExit&&Lq($))await U.waitForExit(q.pid,2000);let W=typeof $==="number"?Z:$;return _(`Sent ${W} to process ${q.pid}`,{pid:q.pid,signal:W})}catch($){if($ instanceof Error&&"code"in $){let U=$;if(U.code==="ESRCH")return X(`Process not found: ${q.pid}`);if(U.code==="EPERM")return X(`Permission denied to kill process: ${q.pid}`)}return X(M($))}}var Kq=["SIGHUP","SIGINT","SIGQUIT","SIGILL","SIGTRAP","SIGABRT","SIGBUS","SIGFPE","SIGKILL","SIGUSR1","SIGSEGV","SIGUSR2","SIGPIPE","SIGALRM","SIGTERM","SIGSTKFLT","SIGCHLD","SIGCONT","SIGSTOP","SIGTSTP","SIGTTIN","SIGTTOU","SIGURG","SIGXCPU","SIGXFSZ","SIGVTALRM","SIGPROF","SIGWINCH","SIGIO","SIGPWR","SIGSYS"],g=new Set(["SIGCHLD","SIGCONT","SIGSTOP","SIGTSTP","SIGTTIN","SIGTTOU","SIGURG","SIGWINCH"]);function jq(q){return Kq.includes(q.toUpperCase())}function zq(q){if(/^\d+$/.test(q)){let Z=parseInt(q,10);if(Z>=1&&Z<=31)return Z;return}let J=q.toUpperCase();if(jq(J))return J;return}function Lq(q){if(typeof q==="number"){let J=Vq(q);if(!J)return!1;return!g.has(J)}return!g.has(q)}function Oq(q,J){try{if(q>0){process.kill(-q,J);return}}catch{}process.kill(q,J)}function Vq(q){for(let[J,Z]of Object.entries(l.signals))if(Z===q)return J;return}function Bq(q){return typeof q.getAll==="function"}async function kq(q,J){let Z=o(q),$=typeof Z?.includeExited==="boolean"?Z.includeExited:!1;if(!J.processRegistry)return X("Process registry not available");let U=J.processRegistry,W=typeof U.listProcesses==="function"?U.listProcesses({includeExited:$}):Bq(U)?U.getAll().map((Q)=>({pid:Q,command:null,args:[],cwd:null,interactive:!1,startedAt:new Date().toISOString(),runtimeMs:0,status:"running",exitCode:null,signal:null,endedAt:null})):[];return _(`Found ${W.length} tracked process${W.length===1?"":"es"}`,{processes:W,count:W.length})}async function yq(q,J){let Z=o(q);if(!Z)return X("Invalid input: expected an object");let $=typeof Z.pid==="number"?Z.pid:NaN;if(!$||$<=0||!Number.isInteger($))return X("Invalid PID: must be a positive integer");if($===process.pid)return X("Cannot wait on own process");let U=Z.timeoutMs;if(U!==void 0&&(typeof U!=="number"||Number.isNaN(U)))return X("Invalid timeoutMs: must be a number");let W=Z.timeout;if(W!==void 0&&(typeof W!=="number"||Number.isNaN(W)))return X("Invalid timeout: must be a number");let Q=Math.min(Math.max(0,U??W??30000),300000);if(!J.processRegistry)return X("Process registry not available");if(typeof J.processRegistry.waitForExit!=="function")return X("Process registry does not support waiting");let F=Date.now(),H=await J.processRegistry.waitForExit($,Q),K=Math.max(0,Date.now()-F);if(H.status==="not_found"||!H.process)return X(`Process not tracked: ${$}`,{pid:$,exited:!1,status:"not_found",timedOut:!1,waitedMs:K});let j=Yq(H,K);if(H.status==="running"||H.timedOut)return X(`Timed out waiting for process ${$}`,j);return _(`Process ${$} exited`,j)}function Yq(q,J){let Z=q.process,$=q.status==="exited"?"exited":"running";return{pid:q.pid,status:$,exited:$==="exited",timedOut:q.timedOut,waitedMs:J,command:Z.command,args:Z.args,cwd:Z.cwd,interactive:Z.interactive,startedAt:Z.startedAt,endedAt:Z.endedAt,runtimeMs:Z.runtimeMs,exitCode:Z.exitCode,signal:Z.signal}}function o(q){if(!q||typeof q!=="object")return;return q}async function Sq(q,J){if(!q||typeof q!=="object")return X("Invalid input: expected an object");let Z=q,$=typeof Z.pid==="number"?Z.pid:NaN,U=typeof Z.input==="string"?Z.input:void 0,W=typeof Z.timeout==="number"?Z.timeout:void 0;if(!$||$<=0||!Number.isInteger($))return X("Invalid PID: must be a positive integer");if($===process.pid)return X("Cannot write to own process");if(U===void 0)return X("Input must be a string");if(!J.ptySessionStore)return X("PTY session store not available");if(!J.ptySessionStore.has($))return X(`No interactive session found for PID ${$}. Use spawn with interactive=true first.`);let Q=J.ptySessionStore.get($);if(!Q)return X(`No interactive session found for PID ${$}. Use spawn with interactive=true first.`);if(!Q.isRunning)return X(`Process ${$} has exited (code: ${Q.exitCode??"unknown"})`);try{let F=Q.output.length;if(Q.write(U),W&&W>0){let L=Math.min(W,30000),z=Date.now()+L,B=50;while(Date.now()<z){if(await new Promise((Y)=>setTimeout(Y,50)),Q.output.length>F)break;if(!Q.isRunning)break}}let K=Q.output.slice(F),j=K.length>1e4?K.slice(-1e4):K;return _(`Wrote ${U.length} bytes to PID ${$}`,{output:j})}catch(F){let H=F instanceof Error?F.message:String(F);return X(`Failed to write to PID ${$}: ${H}`)}}class E extends Error{output;constructor(q,J){super(q);this.output=J;this.name="PTYTimeoutError"}}class T extends Error{output;constructor(q,J){super(q);this.output=J;this.name="PTYError"}}class P{static MAX_OUTPUT=10485760;_output="";_isRunning=!0;_exitCode;_exitPromise;_exitResolve;_pty;_dataDisposable;_exitDisposable;constructor(q){this._pty=q,this._exitPromise=new Promise((J)=>{this._exitResolve=J}),this._dataDisposable=q.onData((J)=>{if(this._output+=J,this._output.length>P.MAX_OUTPUT)this._output=this._output.slice(-P.MAX_OUTPUT)}),this._exitDisposable=q.onExit(({exitCode:J})=>{this._isRunning=!1,this._exitCode=J,this._exitResolve(J)})}write(q){if(!this._isRunning)throw new T("Cannot write to a closed PTY session");this._pty.write(q)}resize(q,J){if(!this._isRunning)throw new T("Cannot resize a closed PTY session");this._pty.resize(q,J)}async waitFor(q,J=5000){let Z=Date.now(),$=typeof q==="string"?new RegExp(Dq(q)):q;if($.test(this._output))return this._output;return new Promise((U,W)=>{let Q,F,H=()=>{if(Q)clearInterval(Q);if(F)clearTimeout(F)},K=()=>{if($.test(this._output))return H(),U(this._output),!0;if(!this._isRunning)return H(),W(new T(`Process exited before pattern was found: ${q}`,this._output)),!0;return!1};if(K())return;Q=setInterval(()=>{K()},10),F=setTimeout(()=>{H();let j=Date.now()-Z;W(new E(`Timeout (${j}ms) waiting for pattern: ${q}`,this._output))},J)})}async waitForExit(q=30000){if(!this._isRunning)return this._output;return new Promise((J,Z)=>{let $;$=setTimeout(()=>{Z(new E(`Timeout (${q}ms) waiting for process to exit`,this._output))},q),this._exitPromise.then(()=>{if($)clearTimeout($);J(this._output)}).catch(Z)})}kill(q){if(!this._isRunning)return;try{this._pty.kill(q)}catch{}}close(){if(this._isRunning)this.kill("SIGKILL"),this._isRunning=!1,this._exitResolve(-1);this._dataDisposable.dispose(),this._exitDisposable.dispose()}get output(){return this._output}get isRunning(){return this._isRunning}get pid(){return this._pty.pid}get exitCode(){return this._exitCode}get cols(){return this._pty.cols}get rows(){return this._pty.rows}}async function _q(q){let $=a.spawn(q.command,q.args??[],{name:"xterm-256color",cols:q.cols??80,rows:q.rows??24,cwd:q.cwd??process.cwd(),env:{...p(q.env),TERM:"xterm-256color"}});return new P($)}async function Iq(q){let{timeoutMs:J=30000,...Z}=q,$=await _q(Z);try{return{output:await $.waitForExit(J),exitCode:$.exitCode??0}}finally{$.close()}}function Dq(q){return q.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}
|
|
4
4
|
export{s as Ub,C as Vb,E as Wb,T as Xb,P as Yb,_q as Zb,Iq as _b,Cq as $b,p as ac,Rq as bc,Hq as cc,Tq as dc,Pq as ec,kq as fc,yq as gc,Sq as hc};
|