UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

212 lines (211 loc) • 7.44 kB
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs"; import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs"; import "./utils-C8YECn2Q.mjs"; import { r as resolveInstalledPluginIndexInstallOwner, t as isInstalledPluginIndexInstallOwnerAmbiguous } from "./installed-plugin-index-install-owner-DoEpptXw.mjs"; import { a as safeRealpathSync } from "./path-safety-CTChYC4Z.mjs"; import { s as parseRegistryNpmSpec } from "./npm-registry-spec-HKkAvl6N.mjs"; import path from "node:path"; //#region src/plugins/installed-plugin-package-ownership.ts function collectDuplicateInstallRecordOwners(e, t, n) { const r = /* @__PURE__ */ new Map(); const i = /* @__PURE__ */ new Set(); for (const [a, o] of Object.entries(e.installRecords)) { const e = o.installPath?.trim() || o.sourcePath?.trim(); if (!e) continue; const s = path.resolve(resolveUserPath(e, t)); const c = safeRealpathSync(s, n) ?? s; const l = r.get(c); if (l && l !== a) { i.add(l); i.add(a); } r.set(c, a); } return i; } function ownershipError(e, t) { return { ok: false, error: `Plugin "${e}" ${t}. Package maintenance requires an unambiguous install record and its discovered package owner. Refresh the plugin registry and run openclaw plugins doctor to inspect the install record before retrying.` }; } function createInstalledPluginOwnershipResolver(e, t = process.env) { const n = /* @__PURE__ */ new Map(); const r = /* @__PURE__ */ new Map(); for (const t of e.plugins) { if (!n.has(t.pluginId)) n.set(t.pluginId, t); const e = resolveInstalledPluginIndexInstallOwner(t); if (e) { const n = r.get(e) ?? []; n.push(t.pluginId); r.set(e, n); } } for (const e of r.values()) e.sort(); function resolveShadowedInstallOwner(t) { if (Object.hasOwn(e.installRecords, t.pluginId)) return t.pluginId; const n = t.pluginId.lastIndexOf("/"); if (n <= 0) return; const r = t.pluginId.slice(0, n); const i = e.installRecords[r]; const a = i?.resolvedSpec ?? i?.spec; const o = i?.resolvedName ?? (a ? parseRegistryNpmSpec(a)?.name : void 0) ?? i?.clawhubPackage; return t.packageName && o === t.packageName ? r : void 0; } const i = new Map(n); for (const e of n.values()) if (e.origin === "config" && !resolveInstalledPluginIndexInstallOwner(e)) { const t = resolveShadowedInstallOwner(e); if (t && !i.has(t)) i.set(t, e); } const a = /* @__PURE__ */ new Map(); let o; const duplicates = () => o ??= collectDuplicateInstallRecordOwners(e, t, a); const s = /* @__PURE__ */ new Map(); const c = createInstallRecordPathMatcher(t, a); function resolvePackage(t) { const i = n.get(t); if (i && isInstalledPluginIndexInstallOwnerAmbiguous(i)) return ownershipError(t, "has ambiguous package ownership"); const a = i ? resolveInstalledPluginIndexInstallOwner(i) : void 0; if (i && !a) return ownershipError(t, "has no authoritative package-owner metadata"); const o = Object.hasOwn(e.installRecords, t) ? t : void 0; const l = a ?? o; if (!l) return ownershipError(t, "is not associated with a tracked package install"); if (a && o && a !== o) return ownershipError(t, "matches conflicting package owners"); const u = e.installRecords[l]; if (!u) return ownershipError(t, `references missing package owner "${l}"`); if (duplicates().has(l)) return ownershipError(t, `shares package path ownership with "${l}"`); const [d, ...f] = r.get(l) ?? []; if (!d) return ownershipError(t, `package owner "${l}" has no authoritative runtime child list`); const p = [d, ...f]; const m = s.get(l) ?? e.plugins.some((e) => c(u, e.rootDir) && resolveInstalledPluginIndexInstallOwner(e) !== l); s.set(l, m); if (m) return ownershipError(t, `package owner "${l}" has conflicting child rows`); return { ok: true, value: { installOwner: l, installRecord: u, pluginIds: p } }; } function resolveLifecycle(t) { const n = resolvePackage(t); if (n.ok) return { ok: true, value: { kind: "package", ...n.value } }; const r = e.installRecords[t]; if (!Object.hasOwn(e.installRecords, t) || !r || duplicates().has(t)) return n; if (e.plugins.some((e) => e.pluginId === t || c(r, e.rootDir))) return n; return { ok: true, value: { kind: "orphan", installOwner: t, installRecord: r, pluginIds: [] } }; } function resolveUpdate(t) { const n = i.get(t); const r = n ? resolveShadowedInstallOwner(n) : void 0; if (n?.origin === "config" && !isInstalledPluginIndexInstallOwnerAmbiguous(n) && !resolveInstalledPluginIndexInstallOwner(n) && (!r || !duplicates().has(r))) { const t = r ? e.installRecords[r] : void 0; return { ok: true, value: { kind: "operator-managed", pluginIds: [n.pluginId], source: n.source, rootDir: n.rootDir, shadowedInstallOwner: r, ...t ? { shadowedInstallRecord: { source: t.source, spec: t.spec, installPath: t.installPath, sourcePath: t.sourcePath } } : {} } }; } return resolveLifecycle(t); } function resolveReload(t) { const r = n.get(t); if (r && !isInstalledPluginIndexInstallOwnerAmbiguous(r) && !resolveInstalledPluginIndexInstallOwner(r) && (r.origin === "bundled" || !Object.hasOwn(e.installRecords, t)) && !Object.values(e.installRecords).some((e) => c(e, r.rootDir))) return { ok: true, value: { kind: "discovered", pluginIds: [t] } }; return resolveLifecycle(t); } function isSourceInUse(n, r) { return isPluginSourcePathInUse(n, [ ...r, ...Object.values(e.installRecords).flatMap((e) => [e.installPath, e.sourcePath]), ...e.plugins.flatMap((e) => [ e.rootDir, e.source, e.manifestPath ]) ], t); } return { resolvePackage, resolveLifecycle, resolveUpdate, resolveReload, isSourceInUse }; } function isPluginSourcePathInUse(e, t, n = process.env) { const r = /* @__PURE__ */ new Map(); const i = safeRealpathSync(e, r) ?? path.resolve(e); return t.some((e) => { if (!e?.trim()) return false; const t = path.resolve(resolveUserPath(e, n)); const a = safeRealpathSync(t, r) ?? t; return isPathInside(i, a) || isPathInside(a, i); }); } function createInstallRecordPathMatcher(e, t) { const n = /* @__PURE__ */ new Map(); const r = /* @__PURE__ */ new Map(); return (i, a) => { let o = n.get(a); if (o === void 0) { const e = path.resolve(a); o = safeRealpathSync(e, t) ?? e; n.set(a, o); } let s = r.get(i); if (s === void 0) { s = [i.installPath, i.sourcePath].filter((e) => Boolean(e?.trim())).map((n) => { const r = path.resolve(resolveUserPath(n, e)); return safeRealpathSync(r, t) ?? r; }); r.set(i, s); } for (const e of s) { if (process.platform !== "win32" && !o.startsWith(e)) continue; if (isPathInside(e, o)) return true; } return false; }; } function hasMissingInstalledPluginOwnerMetadata(e, t = process.env) { const n = /* @__PURE__ */ new Map(); if (collectDuplicateInstallRecordOwners(e, t, n).size > 0) return true; const r = Object.entries(e.installRecords); const i = createInstallRecordPathMatcher(t, n); return e.plugins.some((e) => isInstalledPluginIndexInstallOwnerAmbiguous(e) || !resolveInstalledPluginIndexInstallOwner(e) && r.some(([, t]) => i(t, e.rootDir))); } //#endregion export { hasMissingInstalledPluginOwnerMetadata as n, isPluginSourcePathInUse as r, createInstalledPluginOwnershipResolver as t };