openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.07 kB
JavaScript
import{t as e}from"./fs-safe-copy.worker-errno-CsW5NI8i.mjs";import{i as t}from"./fs-safe-copy.worker-global-singleton-DIJgobpU.mjs";import{a as n}from"./fs-safe-copy.worker-utils-C58aD3xL.mjs";import{a as r}from"./fs-safe-copy.worker-worker-diagnostic-script-C6p7spUr.mjs";import{t as i}from"./fs-safe-copy.worker-subsystem-CSYDX9_J.mjs";import{i as a}from"./fs-safe-copy.worker-kill-tree-l7i2t07V.mjs";import{s as o}from"./fs-safe-copy.worker-child-DIh38sJV.mjs";import{accessSync as s,constants as c,statSync as l}from"node:fs";import u from"node:fs/promises";import d from"node:path";import{execFile as f,spawnSync as p}from"node:child_process";const m=t(Symbol.for(`openclaw.childRuntimeViability`),()=>({initialPath:process.execPath,initialIdentity:executableIdentity(process.execPath)})),h=/(?:dyld[^\n]*:\s*Library not loaded:|error while loading shared libraries:)/i,g=/(?:dyld[^\n]*:\s*Symbol not found:|symbol lookup error:|Error loading shared library|version [`'"][^`'"\r\n]+[`'"] not found)/i;function executableIdentity(e){try{return formatExecutableIdentity(l(e))}catch{return}}function formatExecutableIdentity({dev:e,ino:t,size:n,mtimeMs:r,mode:i}){return`${e}:${t}:${n}:${r}:${i}`}function publishRuntimeViability(t,n){return(e(n,`ENOENT`)||e(n,`ENOTDIR`))&&recordUnavailableRuntime(t,n),{execPath:t,available:!(m.execPath===t&&m.failure)}}function readChildRuntimeViability(e){let t=e?.execPath??process.execPath;if(m.execPath===t&&m.failure)return{execPath:t,available:!1};try{s(t,c.X_OK)}catch(e){return publishRuntimeViability(t,e)}return publishRuntimeViability(t)}async function prepareChildRuntimeViability(e){let t=e?.execPath??process.execPath,n=!(m.execPath===t&&m.failure);if(n)try{let e=await probeChildRuntimeExecutable(t);n=!classifyRuntimeProbe(t,e,await u.stat(t).then(formatExecutableIdentity,()=>void 0),e.error)}catch(e){n=publishRuntimeViability(t,await u.stat(t).then(()=>e,e=>e)).available}return{publish:()=>({execPath:t,available:n&&publishRuntimeViability(t).available})}}function recoverUnavailableRuntime(){if(m.execPath!==process.execPath||!m.failure||!m.recover||m.recoveryStarted)return;m.recoveryStarted=!0;let e=m.recover;r(()=>Promise.resolve().then(e).catch(()=>({status:`report-only`,reason:`Automatic runtime recovery is report-only: service or restart preparation failed. Inspect the service runtime and restart through its owner.`})).then(e=>{i(`gateway/runtime`).warn(`${e.status}: ${e.reason}`)}))}function runtimeProbeOptions(e={}){return{encoding:`utf8`,cwd:e.cwd??d.parse(process.execPath).root,env:e.env??{...process.env,NODE_OPTIONS:void 0},timeout:1e3,maxBuffer:8192}}async function probeChildRuntimeExecutable(e,t={}){let n=a();if(n){let r=n.spawnExeca([e,`--version`],{...runtimeProbeOptions(t),extendEnv:!1,stripFinalNewline:!1,stdin:`ignore`});r.child.once(`spawn`,()=>{r.child.stdout?.resume(),r.child.stderr?.resume()});let i=o(await r.result);return await r.child.waitForClose(),{stdout:typeof i.stdout==`string`?i.stdout:``,stderr:typeof i.stderr==`string`?i.stderr:``,error:i instanceof Error?i:void 0,status:i.exitCode??null,signal:i.signal??null}}return new Promise(n=>{f(e,[`--version`],runtimeProbeOptions(t),(e,t,r)=>n({stdout:t,stderr:r,error:e??void 0,status:e?typeof e.code==`number`?e.code:null:0,signal:e?.signal??null}))})}function formatMissingChildRuntimeWarning(e){return e.available?void 0:`The Node.js runtime this Gateway started with (${n(e.execPath)}) can no longer start worker processes, likely because it was upgraded or removed. Restart the Gateway.`}function assertChildRuntimeAvailable(){if(m.execPath===process.execPath&&m.failure)throw m.failure}function recordUnavailableRuntime(e,t,n){if(m.execPath===e&&m.failure)return m.failure;m.execPath=e;let r=formatMissingChildRuntimeWarning({execPath:e,available:!1}),a=n?.trim();return m.failure=Object.assign(Error(a?`${r}\n${a}`:r,{cause:t}),{code:`NODE_RUNTIME_UNAVAILABLE`}),i(`gateway/runtime`).error(m.failure.message),recoverUnavailableRuntime(),m.failure}function needsRuntimeProbe(e){return!!(e.error||e.signal===`SIGABRT`||g.test(e.stderr??``))}function classifyRuntimeProbe(t,n,r,i){if(m.execPath===t&&m.failure)return m.failure;let a=t!==m.initialPath||r!==m.initialIdentity;return h.test(n.stderr)||g.test(n.stderr)||e(n.error,`ENOENT`)||e(n.error,`ENOTDIR`)||a&&(n.signal===`SIGABRT`||e(n.error,`ENOEXEC`)||e(n.error,`EACCES`)||e(n.error,`EPERM`))?recordUnavailableRuntime(t,i,n.stderr):void 0}function classifyChildRuntimeFailure(e){if(e.started)return;let t=e.execPath??process.execPath;if(m.execPath===t&&m.failure)return m.failure;if(h.test(e.stderr??``)||e.error&&!readChildRuntimeViability({execPath:t}).available)return recordUnavailableRuntime(t,e.error,e.stderr);if(needsRuntimeProbe(e))return classifyRuntimeProbe(t,p(t,[`--version`],runtimeProbeOptions()),t===m.initialPath?executableIdentity(t):void 0,e.error)}async function prepareChildRuntimeFailure(e){if(e.started)return;let t=e.execPath??process.execPath;if(m.execPath===t&&m.failure)return m.failure;if(h.test(e.stderr??``)||e.error&&!(await prepareChildRuntimeViability({execPath:t})).publish().available)return recordUnavailableRuntime(t,e.error,e.stderr);if(!e.error&&needsRuntimeProbe(e))return classifyRuntimeProbe(t,await probeChildRuntimeExecutable(t),t===m.initialPath?await u.stat(t).then(formatExecutableIdentity,()=>void 0):void 0,e.error)}function observeChildRuntime(e,t=process.execPath){let n=!1,r,i=``;return e.once(`message`,()=>{n=!0,i=``}),e.on(`error`,e=>{r??=e}),e.stderr?.on(`data`,e=>{n||(i=(i+e.toString()).slice(-8192))}),async()=>e.exitCode===0&&!r?void 0:prepareChildRuntimeFailure({execPath:t,error:r,stderr:i,signal:e.signalCode,started:n})}async function formatChildRuntimeSpawnWarning(e){if(e instanceof Error&&`path`in e&&e.path===process.execPath)return(await prepareChildRuntimeFailure({error:e}))?.message}export{prepareChildRuntimeFailure as a,observeChildRuntime as i,classifyChildRuntimeFailure as n,readChildRuntimeViability as o,formatChildRuntimeSpawnWarning as r,assertChildRuntimeAvailable as t};