UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 10.8 kB
import{i as e}from"./fs-safe-copy.worker-global-singleton-DIJgobpU.mjs";import{m as t}from"./fs-safe-copy.worker-number-coercion-1FVMw2ic.mjs";import{i as n,t as r,u as i}from"./fs-safe-copy.worker-diagnostic-events-CJsrCJVq.mjs";import a from"node:fs";import o from"node:path";import{AsyncLocalStorage as s}from"node:async_hooks";import"node:child_process";function normalizeExecutableToken(e){let n=o.win32.basename(e),r=o.posix.basename(e),i=n.length<r.length?n:r;return t(i).replace(/\.(?:exe|cmd|bat|com)$/,``)}const c=/* @__PURE__ */ new Set([`-lc`,`-c`,`--command`,`--commands`,`--cmdline`]);[...c];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 n=t(e);return[`-${n}`,`--${n}`,`/${n}`]})}const l=[...expandPowerShellSwitchPrefixForms(`command`,`c`),...expandPowerShellSwitchPrefixForms(`commandwithargs`,`cwa`),...expandPowerShellSwitchForms([`cwa`])],u=expandPowerShellSwitchPrefixForms(`file`,`f`),d=new Set(u);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 f=/* @__PURE__ */ new Set([...expandPowerShellSwitchPrefixForms(`encodedcommand`,`e`),...expandPowerShellSwitchPrefixForms(`ec`,`e`)]),p=/* @__PURE__ */ new Set([...l,...u,...f]),m=new Set(l),h=/* @__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`])]),g=/* @__PURE__ */ new Set([`--init-file`,`--rcfile`]);function countSeparateValueOptionChars(e,t){let n=0;for(let r=1;r<e.length;r+=1){let i=e[r];(i===`o`||i===`O`&&t!==`zsh`)&&(n+=1)}return n}function parseCombinedCommandFlag(e,t){if(e.length<2||e[0]!==`-`||e[1]===`-`)return null;let n=e.slice(1),r=n.indexOf(`c`);if(r===-1||n.includes(`-`))return null;let i=n.slice(r+1);return i&&!/^[A-Za-z]+$/.test(i)?{attachedCommand:i,separateValueCount:0}:{attachedCommand:null,separateValueCount:countSeparateValueOptionChars(e,t)}}function combinedSeparateValueOptionCount(e,t){return e.length<2||e[0]!==`-`&&e[0]!==`+`||e[1]===`-`||e.slice(1).includes(`-`)?0:countSeparateValueOptionChars(e,t)}function advancePosixInlineOptionScan(e,t){let n=combinedSeparateValueOptionCount(e,t);return n>0?1+n:g.has(e)?2:1}function isPowerShellOptionToken(e){return e.startsWith(`-`)||/^\/[A-Za-z][A-Za-z0-9]*$/.test(e)}function resolveInlineCommandMatch(e,n,r={}){let i=normalizeExecutableToken(e[0]??``);for(let a=1;a<e.length;){let o=e[a]?.trim();if(!o){a+=1;continue}let s=t(o);if(s===`--`)break;let c=r.allowCombinedC?o:s,l=n.has(c),u=!l&&r.allowCombinedC?parseCombinedCommandFlag(o,i):null;if(l||u){if(u&&u.attachedCommand!==null)return{command:u.attachedCommand.trim()||null,valueTokenIndex:a};let t=a+1+(u?.separateValueCount??0),n=!u&&t>=e.length?null:t;return{command:(l&&r.restValueFlags?.has(c)?e.slice(a+1).map(e=>e.trim()).join(` `).trim():e[t]?.trim())||null,valueTokenIndex:n}}if(r.valueOptions?.has(s)){a+=2;continue}let d=r.isOptionToken?.(o)??(o.startsWith(`-`)||o.startsWith(`+`));if(r.stopAtFirstNonOption&&!d||r.allowCombinedC&&!o.startsWith(`-`)&&!o.startsWith(`+`))break;a+=r.allowCombinedC?advancePosixInlineOptionScan(o,i):1}return{command:null,valueTokenIndex:null}}function resolvePowerShellInlineCommandMatch(e){return resolveInlineCommandMatch(e,p,{isOptionToken:isPowerShellOptionToken,restValueFlags:m,stopAtFirstNonOption:!0,valueOptions:h})}function isPowerShellInlineFileCommandFlag(e){return d.has(t(e))}const _=`OPENCLAW_CHILD_OOM_SCORE_ADJ`,v=`/bin/sh`,y=`echo 1000 > /proc/self/oom_score_adj 2>/dev/null; exec "$0" "$@"`,b='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" "$@"';function raiseChildOomScore(e){if(e!==void 0)try{a.writeFileSync(`/proc/${e}/oom_score_adj`,`1000`)}catch{}}function shouldAdjustChildOomScore(e){if((e?.platform??process.platform)!==`linux`)return!1;let t=e?.env?.[_]??process.env[_];return i(t)!==!1}function isWrapped(e,t){return e===`/bin/sh`&&t[0]===`-c`&&t[1]===`echo 1000 > /proc/self/oom_score_adj 2>/dev/null; exec "$0" "$@"`}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]===b)&&t[2]?t[2]:void 0}const x=e(Symbol.for(`openclaw.childProcessSpawnCounts`),()=>({counts:/* @__PURE__ */ new Map,sampledAt:performance.now()})),S=/^(node|bun|git|ps|pgrep|lsof|sh|bash|zsh|cmd|powershell|pwsh|npm|pnpm|python|python3|uv|ssh|openclaw)$/,C=/* @__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`]),w=e(Symbol.for(`openclaw.gitSpawnOperation`),()=>new s),T=/* @__PURE__ */ new Set([`agent.exec`,`agent.cli`,`agent.file-read`,`shell.snapshot`,`service.relay`,`service.manager`,`plugin.capture-reclamation`,`worktree.filesystem`,`worktree.cleanup`,`worktree.maintenance`]),E=e(Symbol.for(`openclaw.childSpawnOperation`),()=>new s);function withChildProcessOperation(e,t,n=`override`){return n==="default"&&E.getStore()!==void 0?t():E.run(T.has(e)?e:`unknown`,t)}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],c,{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"&&w.getStore()!==void 0?t():w.run(C.has(e)?e:`unknown`,t)}function prepareChildProcessSpawnRecorder(e,t=[],n){if(!r())return;let i=resolveOomScoreWrappedCommand(e,t),a=i?t.slice(3):t,s=o.win32.basename(i??e).toLowerCase().replace(/\.(exe|cmd)$/,``),c=S.test(s)?s:`other`,l=c===`git`?w.getStore()??E.getStore()??unscopedGitOperation(a):n??E.getStore()??unscopedProcessOperation(c,s,a),u=`${c}:${l}`;return()=>{if(r()){let e=x.counts.get(u);x.counts.set(u,{family:c,operation:l,count:(e?.count??0)+1})}}}function recordChildProcessSpawn(e,t,n,r=[]){let i=prepareChildProcessSpawnRecorder(e,r,n);i&&t.once(`spawn`,i)}function emitChildProcessSpawnSample(){let e=performance.now();if(!r()){x.counts.clear(),x.sampledAt=e;return}let t=e-x.sampledAt;if(!(t<6e4)){for(let{family:e,operation:r,count:i}of x.counts.values())n({type:`diagnostic.child_process.spawn`,family:e,operation:r,count:i,intervalMs:t});x.counts.clear(),x.sampledAt=e}}export{withGitProcessOperation as a,v as c,shouldAdjustChildOomScore as d,withChildProcessOperation as i,isWrapped as l,prepareChildProcessSpawnRecorder as n,b as o,recordChildProcessSpawn as r,y as s,emitChildProcessSpawnSample as t,raiseChildOomScore as u};