UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.88 kB
import{o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{n as isNodeRuntime,r as resolveRuntimeScriptPosition,t as isBunRuntime}from"./runtime-binary-DNstBs1K.mjs";import{s as getRootOptionAwareCommandPath}from"./cli-root-options-eez2TgAl.mjs";import{a as extractErrorCode}from"./error-coercion-DlKnt6w-.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{f as normalizeStringEntries}from"./string-normalization-D_oBUpO3.mjs";import{r as resolveDiagnosticProcessEnv}from"./process-env-BMQKxoSL.mjs";import{i as isLegacyPluginSourceCaptureName}from"./plugin-source-capture-path-BpBJjNIB.mjs";import fs from"node:fs";import path from"node:path";import{spawnSync}from"node:child_process"; //!src/infra/gateway-process-argv.ts function normalizeProcArg(e){return normalizeLowercaseStringOrEmpty(e.replaceAll(`\\`,`/`))}const ENTRY_CANDIDATES=[`openclaw.mjs`,`dist/index.js`,`dist/entry.js`,`scripts/run-node.mjs`,`src/entry.ts`,`src/index.ts`];function referencesRetainedArtifact(e){return e.split(/[\\/=]/u).some(e=>isLegacyPluginSourceCaptureName(e)||/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(e))}function readProcessPackageIdentity(e,t=false){for(let n=e;;){let e;try{e=JSON.parse(fs.readFileSync(path.join(n,`package.json`),`utf8`))}catch(e){const r=path.dirname(n);if(t&&extractErrorCode(e)===`ENOENT`){if(r===n)return{kind:`absent`};if(path.basename(n)===`node_modules`)return{kind:`unknown`,reason:`could not read package identity`};n=r;continue}return{kind:`unknown`,reason:`could not read package identity`}}if(!isRecord(e)||typeof e.name!==`string`||!e.name.trim()||e.name!==e.name.trim())return{kind:`unknown`,reason:`package identity has no valid name`};return{kind:`package`,name:e.name,scripts:isRecord(e.scripts)?e.scripts:{}}}}function readProcessWorkingDirectories(e){const t=new Set(e.filter(e=>Number.isSafeInteger(e)&&e>0));const n=/* @__PURE__ */ new Map;if(t.size===0)return n;if(process.platform===`linux`){for(const e of t)try{n.set(e,fs.readlinkSync(`/proc/${e}/cwd`))}catch{}return n}try{if(process.platform===`darwin`){const e=spawnSync(`/usr/sbin/lsof`,[`-a`,`-p`,[...t].join(`,`),`-d`,`cwd`,`-F0pn`],{encoding:`utf8`,timeout:5e3,maxBuffer:4194304,env:resolveDiagnosticProcessEnv()});if(e.error||e.status!==0&&e.status!==1)return n;const r=e.stdout.split(`\0`);r.pop();const i=/* @__PURE__ */ new Set;let a;for(const e of r){const r=e.replace(/^\n/,``);if(r.startsWith(`p`)){const e=/^p(\d+)$/.exec(r);a=e&&t.has(Number(e[1]))?Number(e[1]):void 0}else if(a!==void 0&&r.startsWith(`n`)&&path.isAbsolute(r.slice(1))){if(n.delete(a))i.add(a);else if(!i.has(a))n.set(a,r.slice(1))}}}}catch{}return n}function classifyEntrypoint(e,t={}){const n=normalizeProcArg(e[0]??``).replace(/\.(bat|cmd|exe)$/i,``);if(!t.requirePackageIdentity&&(n.endsWith(`/openclaw`)||n===`openclaw`))return{kind:`openclaw`,entryIndex:0};const r=/(?:^|\/)openclaw\.mjs$/.test(n)?0:resolveRuntimeScriptPosition(e).position;if(typeof r!==`number`)return r.kind===`unclassified`?{kind:`unclassified`,cause:`runtime-syntax`,reason:r.reason}:{kind:`other`};const i=e[r];if(t.inspectPackage&&!t.requirePackageIdentity&&t.cwd&&path.isAbsolute(t.cwd)&&isBunRuntime(e[0]??``)&&!/[\\/]/u.test(i)&&!path.extname(i)){const e=readProcessPackageIdentity(t.cwd,true);if(e.kind===`unknown`)return{kind:`unclassified`,cause:`package-identity`,reason:e.reason};const n=e.kind===`package`?e.scripts[i]:void 0;if(typeof n===`string`&&n.trim())return e.kind===`package`&&e.name===`openclaw`?{kind:`openclaw`}:{kind:`other`}}const a=classifyOpenClawEntrypointPath(i,t);return a.kind===`openclaw`?{...a,entryIndex:r}:a}function classifyOpenClawEntrypointPath(e,t={}){const n=normalizeProcArg(e);if(!t.requirePackageIdentity&&!t.inspectPackage&&/(?:^|\/)openclaw\.mjs$/.test(n))return{kind:`openclaw`};const r=[...ENTRY_CANDIDATES,...t.additionalEntrypoints??[]];let i=e;if(!path.isAbsolute(e)){const n=t.cwd??(t.pid===void 0?void 0:readProcessWorkingDirectories([t.pid]).get(t.pid));if(!n||!path.isAbsolute(n))return{kind:`unclassified`,cause:`cwd`,reason:`working directory is unavailable`};i=path.resolve(n,e)}let a;let o;try{a=fs.realpathSync(i);if(t.inspectPackage&&!t.requirePackageIdentity&&referencesRetainedArtifact(a))return{kind:`openclaw`,artifactPath:a};const n=t.requirePackageIdentity||t.inspectPackage?fs.statSync(a):void 0;if(t.requirePackageIdentity&&!n?.isFile())return{kind:`unclassified`,cause:`script`,reason:`entrypoint is not a regular file: ${e}`};o=n?.isDirectory()??false}catch{return{kind:`unclassified`,cause:`script`,reason:`could not resolve script`}}const s=normalizeProcArg(a);if(!t.requirePackageIdentity&&/(?:^|\/)openclaw\.mjs$/.test(n))return{kind:`openclaw`};const c=o?void 0:r.find(e=>s.endsWith(`/${e}`));if(!c&&(!t.inspectPackage||t.requirePackageIdentity))return{kind:`other`};const l=c?a.slice(0,-c.length):o?a:path.dirname(a);const u=readProcessPackageIdentity(l,t.inspectPackage&&!t.requirePackageIdentity||!c&&!o);if(u.kind===`unknown`)return{kind:`unclassified`,cause:`package-identity`,reason:u.reason};return u.kind===`package`&&u.name===`openclaw`?{kind:`openclaw`,...t.requirePackageIdentity?{packageIdentity:{root:path.resolve(l),entrypoint:a}}:{}}:{kind:`other`}}function parseProcCmdline(e){return normalizeStringEntries(e.split(`\0`))}function classifyOpenClawArgv(e,t={}){const{command:n,owner:r,pid:i,port:a}=t;if(!t.requirePackageIdentity&&n===`gateway`&&r?.pid===i&&r?.state===`live`&&(a===void 0||r.port===a))return{kind:`openclaw`};const o=normalizeProcArg(e[0]??``).split(`/`).at(-1)?.replace(/\.(exe|cmd|bat)$/,``)??``;if(!t.requirePackageIdentity&&/^openclaw-[a-z0-9-]+$/.test(o))return!n||o===`openclaw-${n}`?{kind:`openclaw`}:{kind:`other`};const s=classifyEntrypoint(e,t);if(n)return s.kind===`openclaw`&&normalizeProcArg(getRootOptionAwareCommandPath([`node`,...e.slice(s.entryIndex)],1)[0]??``)!==n?{kind:`other`}:s;if(s.kind===`openclaw`||!t.requirePackageIdentity&&e.some(e=>e.replaceAll(`\\`,`/`).split(`/`).some(isLegacyPluginSourceCaptureName)))return s.kind===`openclaw`?s:{kind:`openclaw`};if(t.serviceMarkerUnavailable&&!t.requirePackageIdentity)return{kind:`unclassified`,cause:`service-marker`,reason:`process environment is unreadable`};let c=t.serviceMarker;if(i!==void 0&&process.platform===`linux`&&(isNodeRuntime(o)||isBunRuntime(o)||o===`tsx`))try{c=fs.readFileSync(`/proc/${i}/environ`,`utf8`).split(`\0`).find(e=>e.startsWith(`OPENCLAW_SERVICE_MARKER=`))?.slice(24)}catch(e){return{kind:`unclassified`,cause:`service-marker`,reason:`process identity inspection failed (${extractErrorCode(e)??`unavailable`})`}}return!t.requirePackageIdentity&&c===`openclaw`?{kind:`openclaw`}:s}export{readProcessWorkingDirectories as a,readProcessPackageIdentity as i,classifyOpenClawEntrypointPath as n,referencesRetainedArtifact as o,parseProcCmdline as r,classifyOpenClawArgv as t};