UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

219 lines (218 loc) • 9.61 kB
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs"; import { a as extractErrorCode } from "./error-coercion-hDrHOSGy.mjs"; import { i as isMissingPathError } from "./errno-DFcoqIT9.mjs"; import { i as resolveRealpathOrAbsolute } from "./boundary-path-C2gXEiXe.mjs"; import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs"; import "./utils-C8YECn2Q.mjs"; import { n as formatErrorMessageWithCode } from "./errors-BKzCeTYb.mjs"; import { a as resolveCompatibilityHostVersion } from "./version-GlxUMdxI.mjs"; import { o as pluginCacheStatSync, r as pluginCacheExistsSync, s as readPluginCacheDirectory } from "./plugin-cache-files-e1aGp2KM.mjs"; import { c as normalizePluginId } from "./config-state-B1JlHgba.mjs"; import { n as satisfiesPluginApiRange, t as resolvePackagePluginApiRange } from "./package-compat-BLExNVkv.mjs"; import path from "node:path"; //#region src/plugins/runtime-degraded-state.ts /** Boot-stable quarantine state for configured plugins whose payload failed verification. */ const PLUGIN_AVAILABILITY_POLICY = { state: "configured-unavailable", severity: "warning", repairCommand: "openclaw doctor --fix" }; /** Availability findings share one disposition across startup and Doctor lint. */ function describePluginAvailabilityFailure(e, t) { return { source: e, severity: PLUGIN_AVAILABILITY_POLICY.severity, message: `Plugin "${e}" is unavailable: ${t} Run \`${PLUGIN_AVAILABILITY_POLICY.repairCommand}\`.` }; } let activeDegradedPlugins = []; /** Converts verified ownership failures into the quarantine state used for this boot. */ function buildDegradedPluginsFromVerificationFailures(e) { const t = /* @__PURE__ */ new Map(); for (const n of e) { if (t.has(n.pluginId)) continue; t.set(n.pluginId, { pluginId: n.pluginId, state: PLUGIN_AVAILABILITY_POLICY.state, diagnostic: { kind: "plugin-verification", reason: n.reason, detail: n.detail, ...n.installPath ? { installPath: n.installPath } : {} } }); } return [...t.values()]; } /** Replaces the process-local quarantine snapshot established before Gateway plugin loading. */ function setActiveDegradedPlugins(e) { activeDegradedPlugins = [...e]; } function listActiveDegradedPlugins() { return activeDegradedPlugins; } function findActiveDegradedPlugin(e) { return activeDegradedPlugins.find((t) => t.pluginId === e); } /** Drops a verification failure that belongs to a different selected plugin root. */ function clearActiveDegradedPlugin(e) { activeDegradedPlugins = activeDegradedPlugins.filter((t) => t.pluginId !== e); } /** Matches an install-record path and discovered root across symlink/path aliases. */ function pluginInstallPathMatchesRoot(e, t) { if (!e) return false; return resolveRealpathOrAbsolute(e) === resolveRealpathOrAbsolute(t); } /** Matches install-record and discovered roots across symlink/path aliases. */ function degradedPluginMatchesRoot(e, t) { return pluginInstallPathMatchesRoot(e.diagnostic.installPath, t); } /** Removes the known private install root before diagnostics leave the Gateway process. */ function toPublicPluginVerificationDiagnostic(e) { const t = e.reason === "missing-openclaw-peer-link" ? "Plugin declares peerDependency \"openclaw\", but its host peer link is missing or invalid." : e.installPath ? e.detail.replaceAll(e.installPath, "<plugin-install>") : e.detail; return { kind: e.kind, reason: e.reason, detail: t }; } function formatPluginVerificationDiagnostic(e) { const t = toPublicPluginVerificationDiagnostic(e); return `configured plugin payload verification failed (${t.reason}): ${t.detail}`; } //#endregion //#region src/plugins/discovery-availability.ts const CONFIGURED_PLUGIN_PATH_UNAVAILABLE = "configured-plugin-path-unavailable"; const CONFIGURED_PLUGIN_PATH_INSPECTION_FAILED = "configured-plugin-path-inspection-failed"; function shouldSkipIncompatiblePackagePluginApi(e) { if (e.origin === "bundled") return false; const t = resolvePackagePluginApiRange(e.packageManifest); if (!t.ok) { e.diagnostics.push({ level: "warn", configDisposition: "preserve", source: path.join(e.packageDir, "package.json"), message: `invalid package plugin API metadata: ${t.error}; skipping discovery (check package.json openclaw.compat.pluginApi)`, pluginId: e.pluginId }); return true; } const n = t.range; if (!n) return false; const r = resolveCompatibilityHostVersion(e.env); if (satisfiesPluginApiRange(r, n)) return false; e.diagnostics.push({ level: "warn", configDisposition: "preserve", source: path.join(e.packageDir, "package.json"), message: `plugin requires plugin API ${n}, but this host is ${r}; skipping discovery (check "openclaw --version", OPENCLAW_COMPATIBILITY_HOST_VERSION, or run "openclaw doctor")`, pluginId: e.pluginId }); return true; } function isConfiguredPluginPathDiagnosticCode(e) { return e === CONFIGURED_PLUGIN_PATH_UNAVAILABLE || e === CONFIGURED_PLUGIN_PATH_INSPECTION_FAILED; } function pluginPathFailureDiagnostic(e, t, n) { if (t === "config" && !isMissingPathError(n)) { const t = extractErrorCode(n) ?? "UNKNOWN"; const r = `${t === "EACCES" || t === "EPERM" ? `Fix permissions on ${e}` : t === "ELOOP" ? `Fix the symbolic link loop at ${e}` : `Resolve the filesystem inspection error on ${e}`}, then run \`${PLUGIN_AVAILABILITY_POLICY.repairCommand}\`.`; return { level: "warn", code: CONFIGURED_PLUGIN_PATH_INSPECTION_FAILED, configDisposition: "preserve", errorCode: t, source: e, fixHint: r, message: `Configured plugin load path inspection failed: ${e} (${formatErrorMessageWithCode(n)}). Uninspected plugin configuration is preserved. ${r}` }; } return t === "config" ? { level: "warn", code: CONFIGURED_PLUGIN_PATH_UNAVAILABLE, configDisposition: "preserve", source: e, message: `Configured plugin load path is unavailable: ${e}. Uninspected plugin configuration is preserved. Restore access to the path, then run \`${PLUGIN_AVAILABILITY_POLICY.repairCommand}\`.` } : { level: "error", source: e, message: `plugin path not found: ${e}` }; } function inspectPluginLoadPath(e, t, n) { try { const r = pluginCacheStatSync(e, t === "config"); if (!r && !pluginCacheExistsSync(e)) n.push(pluginPathFailureDiagnostic(e, t, void 0)); if (t === "config" && r) { if (r.isDirectory()) readPluginCacheDirectory(e); else if (!r.isFile()) throw Object.assign(/* @__PURE__ */ new Error("Plugin load path is not a regular file or directory"), { code: "ERR_INVALID_FILE_TYPE" }); } return r; } catch (r) { n.push(pluginPathFailureDiagnostic(e, t, r)); return null; } } /** Consumers consult discovery's disposition without reclassifying availability. */ function findUninspectedPluginDiagnostic(e) { return e.find((e) => e.configDisposition === "preserve"); } /** Project the recorded fact onto a config surface whose owner could not be inspected. */ function pluginDiagnosticToConfigWarning(e, t) { return { path: t, message: e.message, code: e.code, source: e.source, ...e.errorCode ? { errorCode: e.errorCode } : {}, ...e.fixHint ? { fixHint: e.fixHint } : {} }; } /** Missing metadata cannot prove that authored plugin configuration is stale. */ function hasIncompletePluginDiscovery(e) { return e.some((e) => e.level === "error" || e.configDisposition === "preserve"); } /** Blocked candidates are present, not stale. Explicit identity excludes path-name guesses. */ function createBlockedPluginDiagnosticLookup(e) { const t = "blocked plugin candidate:"; const n = /* @__PURE__ */ new Map(); const r = []; const normalizeBlockedDiagnosticPath = (t) => { const n = t?.trim(); if (!n) return ""; try { return path.resolve(resolveUserPath(n, e.env ?? process.env)); } catch { return path.resolve(n); } }; for (const i of e.diagnostics) { if (!i.message.startsWith(t)) continue; if (!i.pluginId && i.source) r.push({ message: i.message, source: i.source }); if (i.pluginId) { const e = normalizePluginId(i.pluginId); for (const t of [i.pluginId, e]) if (t && !n.has(t)) n.set(t, { message: i.message, ...i.source ? { source: i.source } : {} }); } } const blockedDiagnosticSourceMatchesPluginId = (t, n) => { const r = normalizePluginId(n); if (!r) return false; const i = normalizeBlockedDiagnosticPath(t.source); if (!i) return false; if (normalizePluginId(path.basename(i)) === r || normalizePluginId(path.basename(path.dirname(i))) === r) return true; for (const t of e.config?.plugins?.load?.paths ?? []) { const e = normalizeBlockedDiagnosticPath(t); if (e && normalizePluginId(path.basename(e)) === r && (isPathInside(e, i) || isPathInside(i, e))) return true; } return false; }; return (e) => n.get(e) ?? n.get(normalizePluginId(e)) ?? r.find((t) => blockedDiagnosticSourceMatchesPluginId(t, e)); } //#endregion export { pluginInstallPathMatchesRoot as _, isConfiguredPluginPathDiagnosticCode as a, shouldSkipIncompatiblePackagePluginApi as c, clearActiveDegradedPlugin as d, degradedPluginMatchesRoot as f, listActiveDegradedPlugins as g, formatPluginVerificationDiagnostic as h, inspectPluginLoadPath as i, PLUGIN_AVAILABILITY_POLICY as l, findActiveDegradedPlugin as m, findUninspectedPluginDiagnostic as n, pluginDiagnosticToConfigWarning as o, describePluginAvailabilityFailure as p, hasIncompletePluginDiscovery as r, pluginPathFailureDiagnostic as s, createBlockedPluginDiagnosticLookup as t, buildDegradedPluginsFromVerificationFailures as u, setActiveDegradedPlugins as v, toPublicPluginVerificationDiagnostic as y };