grix-connector
Version:
Connect local AI coding agents (Claude, Codex, Gemini, Qwen, DeepSeek, Cursor, OpenCode, Pi, OpenHuman, Reasonix) to the Grix scheduling platform. Also serves as an OpenClaw plugin for Grix channel transport.
2 lines (1 loc) • 2.54 kB
JavaScript
import{execSync as P,spawn as g}from"node:child_process";import n from"node:process";import{setTimeout as k}from"node:timers/promises";let l;function R(){if(n.platform!=="win32")return!1;if(l!==void 0)return l;try{P("net session",{stdio:"ignore"}),l=!0}catch{l=!1}return l}function T(t,e){g(t,e,{detached:!0,stdio:"ignore",windowsHide:!0}).unref()}function p(t,e,o={}){const{cwd:r,env:s=n.env,allowFailure:f=!1}=o;return new Promise((a,i)=>{const c=g(t,e,{cwd:r||void 0,env:s,stdio:["ignore","pipe","pipe"],windowsHide:!0}),w=[],h=[];c.stdout&&c.stdout.on("data",u=>w.push(u)),c.stderr&&c.stderr.on("data",u=>h.push(u)),c.on("error",i),c.on("close",u=>{const d=u??0,x=Buffer.concat(w).toString("utf8"),y=Buffer.concat(h).toString("utf8");if(d!==0&&!f){const C=y.trim()||x.trim()||`${t} exited with code ${d}`;i(new Error(C));return}a({exitCode:d,stdout:x,stderr:y})})})}function m(t){if(t<=0)return!1;try{return n.kill(t,0),!0}catch(e){if(e instanceof Error&&"code"in e){if(e.code==="ESRCH")return!1;if(e.code==="EPERM")return!0}return!1}}async function E(t,e={}){const{platform:o=n.platform,runCommandImpl:r=p}=e;if(t<=0)return!1;if(o==="win32")return await r("taskkill",["/PID",String(t),"/T","/F"],{allowFailure:!0}),!0;try{return n.kill(-t,"SIGTERM"),!0}catch{}try{return n.kill(t,"SIGTERM"),!0}catch{return!1}}async function v(t,e={}){const{timeoutMs:o=5e3,intervalMs:r=100}=e;if(t<=0)return!0;const s=Date.now()+o;for(;Date.now()<=s;){if(!m(t))return!0;await k(r)}return!m(t)}async function I(t){const e=p;if(n.platform==="win32"){const r=await e("wmic",["process","where",`CommandLine Like '%${t}%'`,"get","ProcessId"],{allowFailure:!0});return r.exitCode!==0?[]:r.stdout.split(/\r?\n/).map(s=>parseInt(s.trim(),10)).filter(s=>s>0&&!isNaN(s))}const o=await e("pgrep",["-f",t],{allowFailure:!0});return o.exitCode!==0?[]:o.stdout.split(/\n/).map(r=>parseInt(r.trim(),10)).filter(r=>r>0&&!isNaN(r))}async function S(t,e={}){const{exceptPid:o=0,platform:r=n.platform}=e,s=await I(t),f=new Set([n.pid,n.ppid,o]),a=s.filter(i=>!f.has(i));if(a.length===0)return 0;if(r==="win32")for(const i of a)await E(i,{platform:"win32",runCommandImpl:p});else{for(const i of a)try{n.kill(i,"SIGTERM")}catch{}await k(500);for(const i of a)if(m(i))try{n.kill(i,"SIGKILL")}catch{}}return a.length}async function D(t,e={}){return S("dist/grix.js",e)}export{m as isProcessRunning,R as isWindowsElevated,D as killOrphanedDaemonProcesses,S as killProcessesByCommandLine,p as runCommand,T as spawnDetached,E as terminateProcessTree,v as waitForProcessExit};