UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

167 lines (166 loc) • 6.43 kB
import { o as getPluginCache } from "./plugin-cache-Bn9I3v9Q.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(entryPath) { return entryPath.replace(PNPM_VERSIONED_OPENCLAW_ENTRY_PATTERN, "$1$2node_modules$2openclaw$2openclaw.mjs"); } /** Capture an installation path while its link still belongs to the running package. */ function resolveOpenClawInstallationRootSync(runningRoot, argv1) { const launcherRoot = argv1 ? findPackageRootSync(path.dirname(path.resolve(argv1))) : null; const pnpmRoot = path.dirname(rewritePnpmVersionedOpenClawEntryPath(path.join(runningRoot, "openclaw.mjs"))); for (const candidate of [launcherRoot, pnpmRoot]) { if (!candidate || candidate === runningRoot) continue; try { if (openClawRootFsSync.realpathSync(candidate) === openClawRootFsSync.realpathSync(runningRoot)) return candidate; } catch {} } return runningRoot; } function parsePackageName(raw) { const parsed = JSON.parse(raw); return typeof parsed.name === "string" ? parsed.name : null; } async function readPackageName(dir) { const packageNameCache = getPluginCache().sdk.packageNames; const packageJsonPath = path.join(path.resolve(dir), "package.json"); if (packageNameCache.has(packageJsonPath)) return packageNameCache.get(packageJsonPath) ?? null; try { const name = parsePackageName(await openClawRootFs.readFile(packageJsonPath, "utf-8")); packageNameCache.set(packageJsonPath, name); return name; } catch { packageNameCache.set(packageJsonPath, null); return null; } } function readPackageNameSync(dir) { const packageNameCache = getPluginCache().sdk.packageNames; const packageJsonPath = path.join(path.resolve(dir), "package.json"); if (packageNameCache.has(packageJsonPath)) return packageNameCache.get(packageJsonPath) ?? null; try { const name = parsePackageName(openClawRootFsSync.readFileSync(packageJsonPath, "utf-8")); packageNameCache.set(packageJsonPath, name); return name; } catch { packageNameCache.set(packageJsonPath, null); return null; } } async function findPackageRoot(startDir, maxDepth = 12) { for (const current of iterAncestorDirs(startDir, maxDepth)) if (await readPackageName(current) === "openclaw") return current; return null; } function findPackageRootSync(startDir, maxDepth = 12) { for (const current of iterAncestorDirs(startDir, maxDepth)) if (readPackageNameSync(current) === "openclaw") return current; return null; } function* iterAncestorDirs(startDir, maxDepth) { let current = path.resolve(startDir); for (let i = 0; i < maxDepth; i += 1) { yield current; if (path.basename(current) === "node_modules") break; const parent = path.dirname(current); if (parent === current) break; current = parent; } } function candidateDirsFromArgv1(argv1) { const argv1CandidateCache = getPluginCache().sdk.argvDirectories; const cacheKey = path.resolve(argv1); const cached = argv1CandidateCache.get(cacheKey); if (cached) return [...cached]; const normalized = cacheKey; const candidates = []; try { const resolved = openClawRootFsSync.realpathSync(normalized); if (resolved !== normalized) candidates.push(path.dirname(resolved)); } catch {} candidates.push(path.dirname(normalized)); const parts = normalized.split(path.sep); const binIndex = parts.lastIndexOf(".bin"); if (binIndex > 0 && parts[binIndex - 1] === "node_modules") { const binName = path.basename(normalized); const nodeModulesDir = parts.slice(0, binIndex).join(path.sep); candidates.push(path.join(nodeModulesDir, binName)); } const deduped = dedupeCandidates(candidates); argv1CandidateCache.set(cacheKey, deduped); return [...deduped]; } async function resolveOpenClawPackageRoot(opts) { const candidates = buildCandidates(opts); const cacheKey = candidates.join("\0"); const searches = getPluginCache().sdk.packageSearches; const cached = searches.get(cacheKey); if (cached?.all) return cached.all[0] ?? null; if (cached?.first !== void 0) return cached.first; for (const candidate of candidates) { const found = await findPackageRoot(candidate); if (found) { searches.set(cacheKey, { ...searches.get(cacheKey), first: found }); return found; } } searches.set(cacheKey, { ...searches.get(cacheKey), first: null }); return null; } function resolveOpenClawPackageRootsSync(opts) { const candidates = buildCandidates(opts); const cacheKey = candidates.join("\0"); const searches = getPluginCache().sdk.packageSearches; const cached = searches.get(cacheKey)?.all; if (cached) return [...cached]; const seen = /* @__PURE__ */ new Set(); const roots = []; for (const candidate of candidates) { const found = findPackageRootSync(candidate); if (found && !seen.has(found)) { seen.add(found); roots.push(found); } } searches.set(cacheKey, { all: roots }); return [...roots]; } function resolveOpenClawPackageRootSync(opts) { const candidates = buildCandidates(opts); const cacheKey = candidates.join("\0"); const searches = getPluginCache().sdk.packageSearches; const cached = searches.get(cacheKey); if (cached?.all) return cached.all[0] ?? null; if (cached?.first !== void 0) return cached.first; for (const candidate of candidates) { const found = findPackageRootSync(candidate); if (found) { searches.set(cacheKey, { first: found }); return found; } } searches.set(cacheKey, { first: null }); return null; } function buildCandidates(opts) { const candidates = []; if (opts.moduleUrl) try { candidates.push(path.dirname(fileURLToPath(opts.moduleUrl))); } catch {} if (opts.argv1) candidates.push(...candidateDirsFromArgv1(opts.argv1)); if (opts.cwd) candidates.push(opts.cwd); return dedupeCandidates(candidates); } function dedupeCandidates(candidates) { return [...new Set(candidates.map((candidate) => path.resolve(candidate)))]; } //#endregion export { rewritePnpmVersionedOpenClawEntryPath as a, resolveOpenClawPackageRootsSync as i, resolveOpenClawPackageRoot as n, resolveOpenClawPackageRootSync as r, resolveOpenClawInstallationRootSync as t };