UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.72 kB
import e from"node:path";import{readFileSync as t}from"node:fs";import{fileURLToPath as n}from"node:url";const r=1e3,i=Symbol.for(`openclaw.cliRespawnParent`),a=`OPENCLAW_CLI_RESPAWN_PARENT`,o=process.env[a];delete process.env[a],o===String(process.ppid)&&(globalThis[i]={pid:process.ppid});function getCliParentPid(){return globalThis[i]?.pid}let s=!1;function requestCliParentTermination(e=()=>{process.emit(`SIGTERM`)||process.kill(process.pid,`SIGTERM`)}){s||(s=!0,setTimeout(()=>process.kill(process.pid,`SIGKILL`),1e4).unref(),e())}function readStandaloneRuntimeManifest(e=process.execPath){return readStandaloneManifest({executable:e})}function projectStandaloneBuildInfo(e){let nonBlank=e=>typeof e==`string`&&e.trim().length>0,stringList=e=>Array.isArray(e)&&e.every(nonBlank),t={mode:e.mode,openclaw:{version:e.openclaw.version,...nonBlank(e.openclaw.commit)?{commit:e.openclaw.commit}:{}},bun:{version:e.bun.version}};for(let n of[`revision`,`commit`,`executableSha256`])nonBlank(e.bun[n])&&(t.bun[n]=e.bun[n]);if(e.build!==void 0){let{id:n,flavor:r,pluginSetSha256:i,moduleGraphSha256:a}=e.build;if(!nonBlank(n)||![`standard`,`support`].includes(r)||!/^[a-f0-9]{64}$/.test(i)||!/^[a-f0-9]{64}$/.test(a))throw Error(`Invalid standalone build provenance`);if(t.build={id:n,flavor:r,pluginSetSha256:i,moduleGraphSha256:a},e.build.debugEntry!==void 0){let n=e.build.debugEntry;if(typeof n!=`string`||!n.startsWith(`debug/`)||n.includes(`\\`)||n.split(`/`).some(e=>!e||e===`.`||e===`..`))throw Error(`Invalid standalone debugger entry`);t.build.debugEntry=n}}if(e.target!==void 0){if(!nonBlank(e.target.platform)||!nonBlank(e.target.arch))throw Error(`Invalid standalone build target`);t.target={platform:e.target.platform,arch:e.target.arch}}if(e.plugins!==void 0){if(!stringList(e.plugins.embedded)||!stringList(e.plugins.omitted))throw Error(`Invalid standalone plugin inventory`);t.plugins={embedded:[...e.plugins.embedded],omitted:[...e.plugins.omitted]}}if(e.nativeModules!==void 0){if(!Array.isArray(e.nativeModules)||e.nativeModules.some(e=>!e||!nonBlank(e.name)||!nonBlank(e.version)||!stringList(e.artifacts)||e.qualification!==`unverified`||!nonBlank(e.backend)))throw Error(`Invalid standalone native inventory`);t.nativeModules=e.nativeModules.map(({name:e,version:t,artifacts:n,qualification:r,backend:i})=>({name:e,version:t,artifacts:[...n],qualification:r,backend:i}))}return t}function readStandaloneManifest({executable:n,runtimeRoot:r,includeBuild:i=!1}){let a=n??r;if(!e.isAbsolute(a))return;let o=e.dirname(a),s=e.join(o,`manifest.json`),c;try{c=t(s,`utf8`)}catch(e){if(e?.code===`ENOENT`||e?.code===`ENOTDIR`)return;throw e}let l;try{l=JSON.parse(c)}catch(e){throw Error(`Invalid standalone runtime manifest: ${s}`,{cause:e})}let nonBlank=e=>typeof e==`string`&&e.trim().length>0;if(l?.schemaVersion!==1||![`jit`,`bytecode`,`aot`].includes(l.mode)||!nonBlank(l.executable?.path)||!nonBlank(l.runtime?.path)||!nonBlank(l.openclaw?.version)||!nonBlank(l.bun?.version))throw Error(`Invalid standalone runtime manifest: ${s}`);let u=e.relative(o,e.resolve(o,l.executable.path)),d=e.relative(o,e.resolve(o,l.runtime.path));if(e.isAbsolute(l.executable.path)||!u||u===`..`||u.startsWith(`..${e.sep}`)||n&&e.resolve(o,l.executable.path)!==e.resolve(n)||r&&e.resolve(o,l.runtime.path)!==e.resolve(r)||e.isAbsolute(l.runtime.path)||!d||d===`..`||d.startsWith(`..${e.sep}`))throw Error(`Invalid standalone runtime manifest: ${s}`);return{schemaVersion:1,mode:l.mode,executable:{path:l.executable.path,...nonBlank(l.executable.sha256)?{sha256:l.executable.sha256}:{}},runtime:{path:l.runtime.path,...nonBlank(l.runtime.sha256)?{sha256:l.runtime.sha256}:{}},openclaw:{version:l.openclaw.version,...nonBlank(l.openclaw.commit)?{commit:l.openclaw.commit}:{}},bun:{version:l.bun.version},...i?projectStandaloneBuildInfo(l):{}}}function getEmbeddedStandaloneRuntimeRoot(){let e=globalThis.__openclawStandaloneRootUrl;return typeof e==`string`?n(e):void 0}function isEmbeddedStandaloneModule(e){let t=e.startsWith(`file:`)?n(e):e;return globalThis.__openclawStandaloneIsEmbeddedModule?.(t)===!0}function resolveStandaloneSidecarRoot(t=process.execPath){if(t===process.execPath){let e=getEmbeddedStandaloneRuntimeRoot();if(e!==void 0)return e}let n=readStandaloneRuntimeManifest(t);return n?e.resolve(e.dirname(t),n.runtime.path):void 0}function isStandaloneRuntimeExecutable(e=process.execPath){return resolveStandaloneSidecarRoot(e)!==void 0}function resolveRuntimeScriptEnv(e){return isStandaloneRuntimeExecutable()?{...e,BUN_BE_BUN:`1`}:{...e}}export{requestCliParentTermination as a,getCliParentPid as i,resolveRuntimeScriptEnv as n,r as o,resolveStandaloneSidecarRoot as r,isEmbeddedStandaloneModule as t};