openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 5.91 kB
JavaScript
import path from"node:path";import{readFileSync}from"node:fs";import{fileURLToPath}from"node:url";const GATEWAY_SHUTDOWN_RESERVE_MS=1e4;const RESPAWN_SIGNAL_FORCE_KILL_GRACE_MS=1e3;const parentKey=Symbol.for(`openclaw.cliRespawnParent`);const CLI_RESPAWN_PARENT_ENV=`OPENCLAW_CLI_RESPAWN_PARENT`;const inheritedParent=process.env[CLI_RESPAWN_PARENT_ENV];delete process.env[CLI_RESPAWN_PARENT_ENV];if(inheritedParent===String(process.ppid))globalThis[parentKey]={pid:process.ppid};const CLI_PARENT_BOUND_PRELOAD=`data:text/javascript,${encodeURIComponent(`
globalThis[Symbol.for("openclaw.cliRespawnParent")] = { pid: process.ppid };
process.execArgv = process.execArgv.filter((arg, index, args) =>
arg !== import.meta.url && !(arg === "--import" && args[index + 1] === import.meta.url));
`)}`;function getCliParentPid(){return globalThis[parentKey]?.pid}let terminating=false;function requestCliParentTermination(e=()=>{if(!process.emit(`SIGTERM`))process.kill(process.pid,`SIGTERM`)}){if(terminating)return;terminating=true;setTimeout(()=>process.kill(process.pid,`SIGKILL`),GATEWAY_SHUTDOWN_RESERVE_MS).unref();e()}const windowsNativeRedirectStartupKey=Symbol.for(`openclaw.windowsNativeRedirectStartup`);const windowsNativeRedirectStartup=process.platform===`win32`&&!process.versions.bun?globalThis[windowsNativeRedirectStartupKey]??=Object.freeze({execArgv:Object.freeze([...process.execArgv]),appRoot:process.env.OPENCLAW_NATIVE_APP_ROOT,stagedRoot:process.env.OPENCLAW_NATIVE_STAGED_ROOT}):void 0;function readStandaloneRuntimeManifest(e=process.execPath){return readStandaloneManifest({executable:e})}function projectStandaloneBuildInfo(e){const nonBlank=e=>typeof e===`string`&&e.trim().length>0;const stringList=e=>Array.isArray(e)&&e.every(nonBlank);const t={mode:e.mode,openclaw:{version:e.openclaw.version,...nonBlank(e.openclaw.commit)?{commit:e.openclaw.commit}:{}},bun:{version:e.bun.version}};for(const n of[`revision`,`commit`,`executableSha256`])if(nonBlank(e.bun[n]))t.bun[n]=e.bun[n];if(e.build!==void 0){const{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 new Error(`Invalid standalone build provenance`);t.build={id:n,flavor:r,pluginSetSha256:i,moduleGraphSha256:a};if(e.build.debugEntry!==void 0){const n=e.build.debugEntry;if(typeof n!==`string`||!n.startsWith(`debug/`)||n.includes(`\\`)||n.split(`/`).some(e=>!e||e===`.`||e===`..`))throw new Error(`Invalid standalone debugger entry`);t.build.debugEntry=n}}if(e.target!==void 0){if(!nonBlank(e.target.platform)||!nonBlank(e.target.arch))throw new 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 new 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 new 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:e,runtimeRoot:t,includeBuild:n=false}){const r=e??t;if(!path.isAbsolute(r))return;const i=path.dirname(r);const a=path.join(i,`manifest.json`);let o;try{o=readFileSync(a,`utf8`)}catch(e){if(e?.code===`ENOENT`||e?.code===`ENOTDIR`)return;throw e}let s;try{s=JSON.parse(o)}catch(e){throw new Error(`Invalid standalone runtime manifest: ${a}`,{cause:e})}const nonBlank=e=>typeof e===`string`&&e.trim().length>0;if(s?.schemaVersion!==1||![`jit`,`bytecode`,`aot`].includes(s.mode)||!nonBlank(s.executable?.path)||!nonBlank(s.runtime?.path)||!nonBlank(s.openclaw?.version)||!nonBlank(s.bun?.version))throw new Error(`Invalid standalone runtime manifest: ${a}`);const c=path.relative(i,path.resolve(i,s.executable.path));const l=path.relative(i,path.resolve(i,s.runtime.path));if(path.isAbsolute(s.executable.path)||!c||c===`..`||c.startsWith(`..${path.sep}`)||e&&path.resolve(i,s.executable.path)!==path.resolve(e)||t&&path.resolve(i,s.runtime.path)!==path.resolve(t)||path.isAbsolute(s.runtime.path)||!l||l===`..`||l.startsWith(`..${path.sep}`))throw new Error(`Invalid standalone runtime manifest: ${a}`);return{schemaVersion:1,mode:s.mode,executable:{path:s.executable.path,...nonBlank(s.executable.sha256)?{sha256:s.executable.sha256}:{}},runtime:{path:s.runtime.path,...nonBlank(s.runtime.sha256)?{sha256:s.runtime.sha256}:{}},openclaw:{version:s.openclaw.version,...nonBlank(s.openclaw.commit)?{commit:s.openclaw.commit}:{}},bun:{version:s.bun.version},...n?projectStandaloneBuildInfo(s):{}}}function getEmbeddedStandaloneRuntimeRoot(){const e=globalThis.__openclawStandaloneRootUrl;return typeof e===`string`?fileURLToPath(e):void 0}function isEmbeddedStandaloneModule(e){const t=e.startsWith(`file:`)?fileURLToPath(e):e;return globalThis.__openclawStandaloneIsEmbeddedModule?.(t)===true}function resolveStandaloneSidecarRoot(e=process.execPath){if(e===process.execPath){const e=getEmbeddedStandaloneRuntimeRoot();if(e!==void 0)return e}const t=readStandaloneRuntimeManifest(e);return t?path.resolve(path.dirname(e),t.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,RESPAWN_SIGNAL_FORCE_KILL_GRACE_MS as o,resolveStandaloneSidecarRoot as r,isEmbeddedStandaloneModule as t};