UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 14.3 kB
import{K as e,N as t,U as n,V as r,y as i}from"./git-operation.worker-subsystem-DibpW_lB.mjs";import{n as a}from"./git-operation.worker-record-coerce-B9rMjEA7.mjs";import{i as o,o as s,t as c}from"./git-operation.worker-windows-install-roots-CRJXZ7cQ.mjs";import{AsyncLocalStorage as l}from"node:async_hooks";import u from"node:path";import"node:fs";import{spawnSync as d}from"node:child_process";const f=Symbol.for(`openclaw.command-cleanup-uncertain`),p=`Command cleanup could not confirm that owned work stopped`;function isCanonicalCommandProcessCleanupError(e){try{return Object.getOwnPropertyDescriptor(e,f)?.value===!0}catch{return!1}}var CommandProcessCleanupError=class extends Error{constructor(e){let t=p;for(let r of n(e?.cause))if(isCanonicalCommandProcessCleanupError(r))try{let e=Object.getOwnPropertyDescriptor(r,`message`)?.value;if(typeof e==`string`&&e.trim()&&e!==p){t=e;break}}catch{}super(t,e),this.code=`ERR_COMMAND_PROCESS_CLEANUP_UNCERTAIN`,this.cleanup=`uncertain`,this.name=`CommandProcessCleanupError`,Object.defineProperty(this,f,{value:!0})}};function hasCommandProcessCleanupError(e){return n(e).some(isCanonicalCommandProcessCleanupError)}const m=Symbol.for(`openclaw.command-process-cleanup`);function recordCommandProcessCleanup(e,t){return t?Object.defineProperty(e,m,{value:t}):e}function readCommandProcessCleanup(e){return Object.getOwnPropertyDescriptor(e,m)?.value}const h=Symbol.for(`openclaw.command-process-outcome`);function recordCommandProcessFailure(t,n){let r=e(t,`Command failed`),i=Object.isExtensible(r)?r:Error(r.message,{cause:r});return Object.defineProperty(i,h,{value:Object.freeze({...n}),configurable:!0})}function readCommandProcessFailure(e){for(let t of n(e))try{let e=a(t)?Object.getOwnPropertyDescriptor(t,h):void 0;if(e?.value)return e.value}catch{return}}function createSanitizedCommandError(e){let t=typeof e.code==`string`?e.code:void 0,n=typeof e.exitCode==`number`?e.exitCode:void 0,r=typeof e.signal==`string`?e.signal:void 0,i=e.timedOut?`Command timed out`:e.isMaxBuffer?`Command output exceeded its capture limit`:e.isCanceled?`Command was canceled`:e.isTerminated?`Command was terminated${r?` by ${r}`:``}`:n!==void 0&&n!==0?`Command exited with code ${n}`:`Command failed during launch or output capture${t?` (${t})`:``}`;return Object.assign(Error(i),{...t?{code:t}:{},...n===void 0?{}:{exitCode:n},...r?{signal:r}:{}})}function isPlainCommandExitFailure(e){return e.failed&&typeof e.exitCode==`number`&&e.exitCode!==0&&e.signal===void 0&&e.cause===void 0&&!e.timedOut&&!e.isCanceled&&!e.isMaxBuffer&&!e.isTerminated}function isPlainCommandSignalFailure(e){return e.failed&&e.exitCode===void 0&&typeof e.signal==`string`&&e.cause===void 0&&!e.timedOut&&!e.isCanceled&&!e.isMaxBuffer&&e.isTerminated===!0}function resolveProcessExitCode(e){return e.explicitCode??e.childExitCode??(e.usesWindowsExitCodeShim&&e.resolvedSignal==null&&!e.timedOut&&!e.noOutputTimedOut&&!e.killIssuedByTimeout&&!e.killIssuedByAbort?0:null)}const g=/* @__PURE__ */ new Set([`-lc`,`-c`,`--command`,`--commands`,`--cmdline`]);[...g];function expandPowerShellSwitchPrefixForms(e,t){let n=[];for(let r=t.length;r<=e.length;r+=1){let t=e.slice(0,r);n.push(`-${t}`,`--${t}`,`/${t}`)}return n}function expandPowerShellSwitchForms(e){return e.flatMap(e=>{let t=r(e);return[`-${t}`,`--${t}`,`/${t}`]})}const _=[...expandPowerShellSwitchPrefixForms(`command`,`c`),...expandPowerShellSwitchPrefixForms(`commandwithargs`,`cwa`),...expandPowerShellSwitchForms([`cwa`])],v=expandPowerShellSwitchPrefixForms(`file`,`f`),y=new Set(v);new Set(expandPowerShellSwitchPrefixForms(`noprofile`,`nop`)),[...expandPowerShellSwitchPrefixForms(`configurationfile`,`conf`),...expandPowerShellSwitchPrefixForms(`configurationname`,`config`),...expandPowerShellSwitchPrefixForms(`custompipename`,`cus`),...expandPowerShellSwitchPrefixForms(`encodedarguments`,`encodeda`),...expandPowerShellSwitchForms([`ea`]),...expandPowerShellSwitchPrefixForms(`interactive`,`i`),...expandPowerShellSwitchPrefixForms(`login`,`l`),...expandPowerShellSwitchPrefixForms(`namedpipeservermode`,`nam`),...expandPowerShellSwitchPrefixForms(`noexit`,`noe`),...expandPowerShellSwitchPrefixForms(`psconsolefile`,`pscf`),...expandPowerShellSwitchForms([`pscf`]),...expandPowerShellSwitchPrefixForms(`servermode`,`s`),...expandPowerShellSwitchPrefixForms(`settingsfile`,`settings`),...expandPowerShellSwitchPrefixForms(`socketservermode`,`so`),...expandPowerShellSwitchPrefixForms(`sshservermode`,`ssh`),...expandPowerShellSwitchPrefixForms(`v2socketservermode`,`v2so`)];const b=/* @__PURE__ */ new Set([...expandPowerShellSwitchPrefixForms(`encodedcommand`,`e`),...expandPowerShellSwitchPrefixForms(`ec`,`e`)]),x=/* @__PURE__ */ new Set([..._,...v,...b]),S=new Set(_),C=/* @__PURE__ */ new Set([...expandPowerShellSwitchPrefixForms(`configurationfile`,`conf`),...expandPowerShellSwitchPrefixForms(`configurationname`,`config`),...expandPowerShellSwitchPrefixForms(`custompipename`,`cus`),...expandPowerShellSwitchPrefixForms(`encodedarguments`,`encodeda`),...expandPowerShellSwitchPrefixForms(`executionpolicy`,`ex`),...expandPowerShellSwitchPrefixForms(`inputformat`,`inp`),...expandPowerShellSwitchPrefixForms(`outputformat`,`o`),...expandPowerShellSwitchPrefixForms(`psconsolefile`,`pscf`),...expandPowerShellSwitchPrefixForms(`settingsfile`,`settings`),...expandPowerShellSwitchPrefixForms(`token`,`to`),...expandPowerShellSwitchPrefixForms(`utctimestamp`,`utc`),...expandPowerShellSwitchPrefixForms(`version`,`v`),...expandPowerShellSwitchPrefixForms(`windowstyle`,`w`),...expandPowerShellSwitchPrefixForms(`workingdirectory`,`w`),...expandPowerShellSwitchForms([`ea`,`ep`,`if`,`of`,`pscf`,`wd`])]),w=/* @__PURE__ */ new Set([`--init-file`,`--rcfile`,`-O`,`-o`,`+O`,`+o`]);function countSeparateValueOptionChars(e){let t=0;for(let n=1;n<e.length;n+=1){let r=e[n];(r===`o`||r===`O`)&&(t+=1)}return t}function parseCombinedCommandFlag(e){if(e.length<2||e[0]!==`-`||e[1]===`-`)return null;let t=e.slice(1),n=t.indexOf(`c`);if(n===-1||t.includes(`-`))return null;let r=t.slice(n+1);return r&&!/^[A-Za-z]+$/.test(r)?{attachedCommand:r,separateValueCount:0}:{attachedCommand:null,separateValueCount:countSeparateValueOptionChars(e)}}function combinedSeparateValueOptionCount(e){return e.length<2||e[0]!==`-`&&e[0]!==`+`||e[1]===`-`||e.slice(1).includes(`-`)?0:countSeparateValueOptionChars(e)}function advancePosixInlineOptionScan(e){let t=combinedSeparateValueOptionCount(e);return t>0?1+t:w.has(e)?2:1}function isPowerShellOptionToken(e){return e.startsWith(`-`)||/^\/[A-Za-z][A-Za-z0-9]*$/.test(e)}function resolveInlineCommandMatch(e,t,n={}){for(let i=1;i<e.length;){let a=e[i]?.trim();if(!a){i+=1;continue}let o=r(a);if(o===`--`)break;let s=n.allowCombinedC?a:o,c=t.has(s),l=!c&&n.allowCombinedC?parseCombinedCommandFlag(a):null;if(c||l){if(l&&l.attachedCommand!==null)return{command:l.attachedCommand.trim()||null,valueTokenIndex:i};let t=i+1+(l?.separateValueCount??0),r=!l&&t>=e.length?null:t;return{command:(c&&n.restValueFlags?.has(s)?e.slice(i+1).map(e=>e.trim()).join(` `).trim():e[t]?.trim())||null,valueTokenIndex:r}}if(n.valueOptions?.has(o)){i+=2;continue}let u=n.isOptionToken?.(a)??(a.startsWith(`-`)||a.startsWith(`+`));if(n.stopAtFirstNonOption&&!u||n.allowCombinedC&&!a.startsWith(`-`)&&!a.startsWith(`+`))break;i+=n.allowCombinedC?advancePosixInlineOptionScan(a):1}return{command:null,valueTokenIndex:null}}function resolvePowerShellInlineCommandMatch(e){return resolveInlineCommandMatch(e,x,{isOptionToken:isPowerShellOptionToken,restValueFlags:S,stopAtFirstNonOption:!0,valueOptions:C})}function isPowerShellInlineFileCommandFlag(e){return y.has(r(e))}const T=['echo 1000 > /proc/self/oom_score_adj 2>/dev/null; if [ "${OC_INTERNAL_OOM_EXEC_BASH_ENV+x}" = x ]; then BASH_ENV="$OC_INTERNAL_OOM_EXEC_BASH_ENV"; export BASH_ENV; fi; unset OC_INTERNAL_OOM_EXEC_BASH_ENV','if [ "${OC_INTERNAL_OOM_EXEC_ENV+x}" = x ]; then ENV="$OC_INTERNAL_OOM_EXEC_ENV"; export ENV; fi; unset OC_INTERNAL_OOM_EXEC_ENV','if [ "${OC_INTERNAL_OOM_EXEC_CDPATH+x}" = x ]; then CDPATH="$OC_INTERNAL_OOM_EXEC_CDPATH"; export CDPATH; fi; unset OC_INTERNAL_OOM_EXEC_CDPATH','if [ "${OC_INTERNAL_OOM_EXEC_PS4+x}" = x ]; then PS4="$OC_INTERNAL_OOM_EXEC_PS4"; export PS4; fi; unset OC_INTERNAL_OOM_EXEC_PS4; exec "$0" "$@"'].join(`; `);function resolveOomScoreWrappedCommand(e,t){return e===`/bin/sh`&&t[0]===`-c`&&(t[1]===`echo 1000 > /proc/self/oom_score_adj 2>/dev/null; exec "$0" "$@"`||t[1]===T)&&t[2]?t[2]:void 0}const E=t(Symbol.for(`openclaw.childProcessSpawnCounts`),()=>({counts:/* @__PURE__ */ new Map,sampledAt:performance.now()})),D=/^(node|bun|git|ps|pgrep|lsof|sh|bash|zsh|cmd|powershell|pwsh|npm|pnpm|python|python3|uv|ssh|openclaw)$/,O=/* @__PURE__ */ new Set([`repository.identities`,`repository.branches`,`checkout.revision`,`checkout.context`,`checkout.diff`,`checkout.baseline`,`pull-request.branch-facts`,`worktree.snapshot`,`worktree.cleanup`,`worktree.provision`,`worktree.inspect`,`worktree.recovery`,`workspace.inventory`,`workspace.manifest`,`workspace.result-cleanup`,`project.clone`,`session.materialize`,`publication`,`github.identity`,`project.checkout`,`update.inspect`,`worktree.config`,`worktree.maintenance`,`workspace.sync`,`workspace.result`]),k=t(Symbol.for(`openclaw.gitSpawnOperation`),()=>new l),A=t(Symbol.for(`openclaw.childSpawnOperation`),()=>new l);function unscopedProcessOperation(e,t,n){if(n[0]===`--version`||n[0]===`-v`&&(e===`node`||e===`bun`||t===`gh`))return`unscoped.version`;if(e===`powershell`||e===`pwsh`){let e=[t,...n],{command:r,valueTokenIndex:i}=resolvePowerShellInlineCommandMatch(e);return r!==null&&i!==null&&!isPowerShellInlineFileCommandFlag(e[i-1]??``)?`unscoped.shell-command`:`unscoped.shell`}return/^(sh|bash|zsh)$/.test(e)?resolveInlineCommandMatch([t,...n],g,{allowCombinedC:!0}).command===null?`unscoped.shell`:`unscoped.shell-command`:e===`cmd`?n.some(e=>/^-[a-z]*c$/.test(e)||/^\/(?:c|k)$/i.test(e))?`unscoped.shell-command`:`unscoped.shell`:e===`node`||e===`bun`?`unscoped.runtime`:e===`other`&&/^(gh|docker|podman|which|uname|sw_vers|sysctl|curl|tar)$/.test(t)?`unscoped.${t}`:`unscoped.command`}function unscopedGitOperation(e){let t=0;for(;t<e.length;){let n=e[t];if([`-C`,`-c`,`--git-dir`,`--work-tree`,`--namespace`,`--config-env`].includes(n))t+=2;else if(/^--(?:git-dir|work-tree|namespace|config-env|attr-source)=/u.test(n))t++;else if([`--no-pager`,`--no-optional-locks`,`--no-lazy-fetch`,`--literal-pathspecs`,`--bare`].includes(n))t++;else break}let n=e[t]??``;return/^(config|rev-parse|status|diff|ls-files|rev-list|merge-base|cat-file|symbolic-ref|for-each-ref|show-ref|remote|log|show|hash-object|update-ref|worktree|branch|fetch|clone|init|checkout|read-tree|write-tree|commit-tree|pack-objects|index-pack|maintenance|multi-pack-index|gc|fast-import|add|commit|push|reset|clean|merge|tag|describe|switch|ls-tree|update-index|check-ref-format|diff-tree|diff-index|diff-files|sparse-checkout|checkout-index|check-attr|mktree|var|apply|fsck|ls-remote|rebase|reflog)$/u.test(n)?`unscoped.${n}`:n===`--version`||n===`version`?`unscoped.version`:`unscoped.other`}function withGitProcessOperation(e,t,n=`override`){return e===void 0||n==="default"&&k.getStore()!==void 0?t():k.run(O.has(e)?e:`unknown`,t)}function prepareChildProcessSpawnRecorder(e,t=[],n){if(!i())return;let r=resolveOomScoreWrappedCommand(e,t),a=r?t.slice(3):t,o=u.win32.basename(r??e).toLowerCase().replace(/\.(exe|cmd)$/,``),s=D.test(o)?o:`other`,c=s===`git`?k.getStore()??A.getStore()??unscopedGitOperation(a):n??A.getStore()??unscopedProcessOperation(s,o,a),l=`${s}:${c}`;return()=>{if(i()){let e=E.counts.get(l);E.counts.set(l,{family:s,operation:c,count:(e?.count??0)+1})}}}function recordChildProcessSpawn(e,t,n,r=[]){let i=prepareChildProcessSpawnRecorder(e,r,n);i&&t.once(`spawn`,i)}const j={65001:`utf-8`,54936:`gb18030`,874:`windows-874`,936:`gbk`,950:`big5`,932:`shift_jis`,949:`euc-kr`,1250:`windows-1250`,1251:`windows-1251`,1252:`windows-1252`,1253:`windows-1253`,1254:`windows-1254`,1255:`windows-1255`,1256:`windows-1256`,1257:`windows-1257`,1258:`windows-1258`};new Map(Object.entries({65001:`utf-8`,874:`windows-874`,932:`shift_jis`,936:`gbk`,949:`euc-kr`,950:`big5`,1258:`windows-1258`,437:`cp437`,720:`cp720`,737:`cp737`,775:`cp775`,850:`cp850`,852:`cp852`,855:`cp855`,857:`cp857`,858:`cp858`,860:`cp860`,861:`cp861`,862:`cp862`,863:`cp863`,865:`cp865`,866:`cp866`,869:`cp869`}).map(([e,t])=>[t,Number.parseInt(e,10)]));let M;function parseWindowsCodePage(e){let t=Number(e.match(/\b(\d{3,5})\b/)?.[1]);return t>0?t:null}function resolveWindowsConsoleEncoding(){return process.platform===`win32`?(M===void 0&&(M=probeWindowsEncoding(c,[`/d`,`/s`,`/c`,`chcp`])),M):null}function probeWindowsEncoding(e,t){o();try{let n=d(e(),t,{env:s(),windowsHide:!0,encoding:`utf8`,killSignal:`SIGKILL`,stdio:[`ignore`,`pipe`,`pipe`],timeout:5e3}),r=parseWindowsCodePage(`${n.stdout??``}\n${n.stderr??``}`);return r===null?null:j[r]??null}catch{return null}}function decodeWindowsOutputBuffer(e){return decodeWindowsBufferWithFallback({...e,resolveFallbackEncoding:()=>e.windowsEncoding??resolveWindowsConsoleEncoding()})}function decodeUtf16BomBuffer(e){let[t,n]=e;return t===255&&n===254||t===254&&n===255?new TextDecoder(t===255?`utf-16le`:`utf-16be`).decode(e):void 0}function decodeWindowsBufferWithFallback(e){if((e.platform??process.platform)!==`win32`)return e.buffer.toString(`utf8`);let t=decodeUtf16BomBuffer(e.buffer);if(t!==void 0)return t;let n=decodeStrictUtf8(e.buffer);if(n!==null)return n;let i=e.resolveFallbackEncoding();if(!i||r(i)===`utf-8`)return e.buffer.toString(`utf8`);try{return new TextDecoder(i).decode(e.buffer)}catch{return e.buffer.toString(`utf8`)}}function decodeStrictUtf8(e){try{return new TextDecoder(`utf-8`,{fatal:!0}).decode(e)}catch{return null}}export{CommandProcessCleanupError as a,isPlainCommandExitFailure as c,readCommandProcessFailure as d,recordCommandProcessCleanup as f,withGitProcessOperation as i,isPlainCommandSignalFailure as l,resolveProcessExitCode as m,resolveWindowsConsoleEncoding as n,createSanitizedCommandError as o,recordCommandProcessFailure as p,recordChildProcessSpawn as r,hasCommandProcessCleanupError as s,decodeWindowsOutputBuffer as t,readCommandProcessCleanup as u};