UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

149 lines (148 loc) • 6.47 kB
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs"; import { n as coerceErrorMessage } from "./error-coercion-hDrHOSGy.mjs"; import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs"; import { p as shortenHomePath } from "./utils-C8YECn2Q.mjs"; import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs"; import { a as resolveDefaultPluginExtensionsDir, n as isPluginNpmManagedPath, r as isPluginNpmProjectDir, s as resolveDefaultPluginNpmDir } from "./install-paths-Bwm0P5EG.mjs"; import { c as relinkOpenClawPeerDependenciesInManagedNpmRoot, n as auditOpenClawPeerDependenciesInManagedNpmRoot, s as reconcileRegisteredOpenClawHostLinks } from "./plugin-peer-link-KsX3OiLh.mjs"; import { s as resolveRetainedManagedNpmInstallPackageInfo, u as listManagedPluginNpmRootsSync } from "./managed-npm-retention-1zEp6KL9.mjs"; import { a as readPersistedInstalledPluginIndexInstallRecordsAsync, r as loadInstalledPluginIndexInstallRecords } from "./installed-plugin-index-record-reader-B-7uGQZ9.mjs"; import "./installed-plugin-index-records-MNCNSlg8.mjs"; import { t as note } from "./note-CnLgHvug.mjs"; import path from "node:path"; //#region src/plugins/installed-plugin-npm-roots.ts /** Selects active managed projects without discovering unrelated generations. */ function listManagedPluginNpmRootsForInstallRecords(e) { if (e.installRecords === null) { process.emitWarning("Plugin install records could not be read; inspecting all managed npm roots for host-link repair.", { code: "OPENCLAW_PLUGIN_NPM_ROOTS_FALLBACK" }); return listManagedPluginNpmRootsSync(e.npmRoot); } const t = path.resolve(e.npmRoot); const n = /* @__PURE__ */ new Set(); for (const r of Object.values(e.installRecords)) { if (r.source !== "npm" || !r.installPath) continue; const i = resolveUserPath(r.installPath, e.env); const a = resolveRetainedManagedNpmInstallPackageInfo(i); if (a && (path.relative(t, a.projectRoot) === "" && isPluginNpmManagedPath({ managedPath: i, npmDir: t }) || isPluginNpmProjectDir({ packageName: a.packageName, projectDir: a.projectRoot, npmDir: t }))) n.add(a.projectRoot); } return [...n].toSorted((e, t) => e.localeCompare(t)); } //#endregion //#region src/commands/doctor-plugin-host-links.ts function resolveRegisteredPluginExtensionsRoot(e) { return e.stateDir ? path.join(e.stateDir, "extensions") : resolveDefaultPluginExtensionsDir(e.env); } function resolveDoctorPluginNpmRoots(e) { const t = e.stateDir ? path.join(e.stateDir, "npm") : resolveDefaultPluginNpmDir(e.env); return listManagedPluginNpmRootsSync(t); } async function loadHostLinkInstallRecords(e) { let t; try { t = await readPersistedInstalledPluginIndexInstallRecordsAsync(e); } catch (e) { if (![ "EACCES", "EPERM", "EIO", "SQLITE_CANTOPEN", "SQLITE_IOERR" ].some((t) => hasErrnoCode(e, t))) throw e; return null; } return t ?? await loadInstalledPluginIndexInstallRecords(e); } function resolveHostLinkNpmRoots(e, t) { return listManagedPluginNpmRootsForInstallRecords({ npmRoot: e.stateDir ? path.join(e.stateDir, "npm") : resolveDefaultPluginNpmDir(e.env), installRecords: t, env: e.env }); } /** Audits managed npm and registered plugin host links without mutating either root. */ async function listPluginOpenClawHostLinkIssues(e) { const t = await loadHostLinkInstallRecords(e); const n = []; const r = []; const recordReadFailure = (e) => (t, n) => { e.push({ packageDir: n, reason: coerceErrorMessage(t) }); }; const i = await Promise.all(resolveHostLinkNpmRoots(e, t).map((e) => auditOpenClawPeerDependenciesInManagedNpmRoot({ npmRoot: e, onPackageReadError: recordReadFailure(n) }))); const a = await reconcileRegisteredOpenClawHostLinks({ installRecords: t ?? {}, extensionsDir: resolveRegisteredPluginExtensionsRoot(e), env: e.env, mode: "audit", onPackageReadError: recordReadFailure(r) }); return { peerLinkIssues: i.flatMap((e) => e.issues), packageReadFailures: n, registeredPeerLinkIssues: a.issues, registeredPackageReadFailures: r }; } async function maybeRepairPluginOpenClawHostLinks(e) { if (!e.prompter.shouldRepair) { const t = await listPluginOpenClawHostLinkIssues(e); const reportLinks = (e, t, n) => { if (e.length > 0) note([ `${t} need repair:`, ...e.map((e) => `- ${e.packageName}: ${e.reason}`), `Repair with ${formatCliCommand("openclaw doctor --fix")} to relink ${n} plugin packages.` ].join("\n"), "Plugin registry"); }; reportLinks(t.peerLinkIssues, "Managed npm OpenClaw host peer links", "managed npm"); for (const [e, n] of [["Managed npm plugin", t.packageReadFailures], ["Registered plugin", t.registeredPackageReadFailures]]) if (n.length > 0) note([`${e} packages could not be inspected:`, ...n.map((e) => `- ${shortenHomePath(e.packageDir)}: ${e.reason}`)].join("\n"), "Plugin registry"); reportLinks(t.registeredPeerLinkIssues, "Registered plugin OpenClaw host links", "registered"); return false; } const t = e.installRecords ?? await loadHostLinkInstallRecords(e); const n = resolveHostLinkNpmRoots(e, t); const r = []; const i = { info() {}, warn: (e) => r.push(`- ${e}`) }; const warnReadFailure = (e) => (t, n) => { i.warn(`Could not inspect ${e} package ${shortenHomePath(n)}: ${coerceErrorMessage(t)}`); }; const a = await Promise.allSettled(n.map((t) => relinkOpenClawPeerDependenciesInManagedNpmRoot({ npmRoot: t, logger: i, onPackageReadError: warnReadFailure("managed npm"), beforePersistentEffect: e.beforePersistentEffect }))); let o = 0; for (const e of a) { if (e.status === "rejected") throw e.reason; o += e.value.repaired; } const s = await reconcileRegisteredOpenClawHostLinks({ installRecords: t ?? {}, extensionsDir: resolveRegisteredPluginExtensionsRoot(e), env: e.env, mode: "repair", logger: i, onPackageReadError: warnReadFailure("registered"), beforePersistentEffect: e.beforePersistentEffect }); for (const [e, t] of [[o, "managed npm"], [s.repaired, "registered"]]) if (e > 0) note(`Repaired OpenClaw host peer link(s) for ${e} ${t} plugin package(s).`, "Plugin registry"); if (r.length > 0) note(["OpenClaw host peer link warnings:", ...r].join("\n"), "Plugin registry"); return o > 0 || s.repaired > 0; } //#endregion export { listManagedPluginNpmRootsForInstallRecords as i, maybeRepairPluginOpenClawHostLinks as n, resolveDoctorPluginNpmRoots as r, listPluginOpenClawHostLinkIssues as t };