UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

170 lines (169 loc) • 5.17 kB
import { o as runtimeProcessExternalPackageEntrypoints } from "./runtime-process-manifest-DmUAODRC.mjs"; import { a as getPluginCache } from "./plugin-cache-B_eRuqTM.mjs"; import openClawRootFsSync from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; import openClawRootFs from "node:fs/promises"; //#region src/infra/openclaw-root.ts const PNPM_VERSIONED_OPENCLAW_ENTRY_PATTERN = /^(.*?)([\\/])node_modules\2\.pnpm\2openclaw@[^\\/]+\2node_modules\2openclaw\2.+$/; /** Keeps replacement and respawn on pnpm's stable package link. */ function rewritePnpmVersionedOpenClawEntryPath(e) { return e.replace(PNPM_VERSIONED_OPENCLAW_ENTRY_PATTERN, `$1$2node_modules$2openclaw$2${runtimeProcessExternalPackageEntrypoints.cliLauncher}`); } /** Capture an installation path while its link still belongs to the running package. */ function resolveOpenClawInstallationRootSync(e, t) { const n = t ? findPackageRootSync(path.dirname(path.resolve(t))) : null; const r = path.dirname(rewritePnpmVersionedOpenClawEntryPath(path.join(e, runtimeProcessExternalPackageEntrypoints.cliLauncher))); for (const t of [n, r]) { if (!t || t === e) continue; try { if (openClawRootFsSync.realpathSync(t) === openClawRootFsSync.realpathSync(e)) return t; } catch {} } return e; } function parsePackageName(e) { const t = JSON.parse(e); return typeof t.name === "string" ? t.name : null; } async function readPackageName(e, t) { t?.throwIfAborted(); const n = getPluginCache().sdk.packageNames; const r = path.join(path.resolve(e), "package.json"); if (n.has(r)) return n.get(r) ?? null; try { const e = parsePackageName(await openClawRootFs.readFile(r, { encoding: "utf-8", signal: t })); t?.throwIfAborted(); n.set(r, e); return e; } catch { t?.throwIfAborted(); n.set(r, null); return null; } } function readPackageNameSync(e) { const t = getPluginCache().sdk.packageNames; const n = path.join(path.resolve(e), "package.json"); if (t.has(n)) return t.get(n) ?? null; try { const e = parsePackageName(openClawRootFsSync.readFileSync(n, "utf-8")); t.set(n, e); return e; } catch { t.set(n, null); return null; } } async function findPackageRoot(e, t) { for (const n of iterAncestorDirs(e)) if (await readPackageName(n, t) === "openclaw") return n; return null; } function findPackageRootSync(e) { for (const t of iterAncestorDirs(e)) if (readPackageNameSync(t) === "openclaw") return t; return null; } function* iterAncestorDirs(e) { let t = path.resolve(e); for (let e = 0; e < 12; e += 1) { yield t; if (path.basename(t) === "node_modules") break; const e = path.dirname(t); if (e === t) break; t = e; } } function candidateDirsFromArgv1(e) { const t = getPluginCache().sdk.argvDirectories; const n = path.resolve(e); const r = t.get(n); if (r) return [...r]; const i = n; const a = []; try { const e = openClawRootFsSync.realpathSync(i); if (e !== i) a.push(path.dirname(e)); } catch {} a.push(path.dirname(i)); const o = i.split(path.sep); const s = o.lastIndexOf(".bin"); if (s > 0 && o[s - 1] === "node_modules") { const e = path.basename(i); const t = o.slice(0, s).join(path.sep); a.push(path.join(t, e)); } const c = dedupeCandidates(a); t.set(n, c); return [...c]; } function preparePackageRootSearch(e, t) { const n = buildCandidates(e); const r = n.join("\0"); const i = getPluginCache().sdk.packageSearches; const a = i.get(r); return { candidates: n, first: a?.all ? a.all[0] ?? null : a?.first, complete(e) { i.set(r, t ? { ...i.get(r), first: e } : { first: e }); return e; } }; } async function resolveOpenClawPackageRoot(e) { e.signal?.throwIfAborted(); const t = preparePackageRootSearch(e, true); if (t.first !== void 0) return t.first; for (const n of t.candidates) { const r = await findPackageRoot(n, e.signal); if (r) return t.complete(r); } return t.complete(null); } function resolveOpenClawPackageRootsSync(e) { const t = buildCandidates(e); const n = t.join("\0"); const r = getPluginCache().sdk.packageSearches; const i = r.get(n)?.all; if (i) return [...i]; const a = /* @__PURE__ */ new Set(); const o = []; for (const e of t) { const t = findPackageRootSync(e); if (t && !a.has(t)) { a.add(t); o.push(t); } } r.set(n, { all: o }); return [...o]; } function resolveOpenClawPackageRootSync(e) { const t = preparePackageRootSearch(e, false); if (t.first !== void 0) return t.first; for (const e of t.candidates) { const n = findPackageRootSync(e); if (n) return t.complete(n); } return t.complete(null); } function buildCandidates(e) { const t = []; if (e.moduleUrl) try { t.push(path.dirname(fileURLToPath(e.moduleUrl))); } catch {} if (e.argv1) t.push(...candidateDirsFromArgv1(e.argv1)); if (e.cwd) t.push(e.cwd); return dedupeCandidates(t); } function dedupeCandidates(e) { return [...new Set(e.map((e) => path.resolve(e)))]; } //#endregion export { rewritePnpmVersionedOpenClawEntryPath as a, resolveOpenClawPackageRootsSync as i, resolveOpenClawPackageRoot as n, resolveOpenClawPackageRootSync as r, resolveOpenClawInstallationRootSync as t };