UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

187 lines (186 loc) • 9.37 kB
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs"; import { l as isRecord, o as asRecord } from "./record-coerce-CVPuDqcc.mjs"; import "./utils-C8YECn2Q.mjs"; import { s as tracePluginLifecyclePhaseAsync } from "./discovery-DvJYrQFY.mjs"; import { r as loadPluginManifest } from "./manifest-Y5vPZPOf.mjs"; import { d as tryReadJsonSync } from "./json-files-Cb1h8pYs.mjs"; import { l as listOfficialExternalPluginCatalogEntries, p as resolveOfficialExternalPluginInstall } from "./official-external-plugin-catalog-DtkWz1Xo.mjs"; import { s as parseRegistryNpmSpec } from "./npm-registry-spec-HKkAvl6N.mjs"; import { r as loadInstalledPluginIndexInstallRecords } from "./installed-plugin-index-record-reader-B-7uGQZ9.mjs"; import { g as resolveOfficialExternalPluginId } from "./official-external-plugin-catalog-source-4o5oPhVN.mjs"; import { l as readConfigFileSnapshotForWrite } from "./io.runtime-DsJ2dLCQ.mjs"; import "./config-CMA-J084.mjs"; import "./installed-plugin-index-records-MNCNSlg8.mjs"; import { t as findBundledPluginSource } from "./bundled-sources-CIS6N5EC.mjs"; import { a as resolveFileNpmSpecToLocalPath, i as resolveBundledInstallPlanBeforeNpm, r as parseNpmPrefixSpec, t as isBareNpmPackageName } from "./install-source-spec-BYmQ0ff-.mjs"; import { n as listPersistedBundledPluginRecoveryLocations } from "./location-bridges-uCTU-M6c.mjs"; import { a as supportsInstallConfigSingleTopLevelIncludeShape, i as selectInstallMutationWriteOptions, r as resolveInstallConfigMutationPreflights } from "./install-config-mutation-BW96uF8m.mjs"; import fs from "node:fs"; import path from "node:path"; //#region src/plugins/install-config.ts function resolveFullyBlockedConfigMutationReason(e) { if (e.pluginMutation.mode !== "blocked" || e.hookMutation.mode !== "blocked") return null; if (e.pluginMutation.reason === e.hookMutation.reason) return e.pluginMutation.reason; return `Config plugin and hook mutations are both blocked. ${e.pluginMutation.reason} ${e.hookMutation.reason}`; } var PluginInstallConfigError = class extends Error { constructor(e, t) { super(e); this.blockedSnapshot = t; this.code = "INVALID_CONFIG"; this.name = "PluginInstallConfigError"; } }; function extractMissingPluginLoadPath(e) { if (e.path !== "plugins.load.paths") return null; const t = e.message.indexOf("plugin path not found:"); if (t < 0) return null; return e.message.slice(t + 22).trim() || null; } function isOwnedMissingPluginLoadPathIssue(e, t, n = process.env) { const r = extractMissingPluginLoadPath(e); return r !== null && t.has(resolveUserPath(r, n)); } function isAllowedPluginRecoveryIssue(e, t, n) { const r = t.bundledPluginId?.trim(); if (!r) return false; return e.path === `channels.${r}` && e.message === `unknown channel id: ${r}` || e.path.startsWith("channels.") && e.message.startsWith(`invalid config for plugin ${r}:`) || isOwnedMissingPluginLoadPathIssue(e, n) || e.path === `plugins.entries.${r}` && e.message.includes("requires compiled runtime output") || e.path === "tools.web.search.provider" && e.message.includes(`plugin "${r}"`); } function removeOwnedMissingPluginLoadPaths(e, t, n, r) { const i = /* @__PURE__ */ new Set(); for (const e of t) { const t = extractMissingPluginLoadPath(e); if (!t) continue; const a = resolveUserPath(t, r); if (n.has(a)) i.add(a); } const a = e.plugins?.load?.paths; if (i.size === 0 || !Array.isArray(a)) return e; const o = a.filter((e) => typeof e !== "string" || !i.has(resolveUserPath(e, r))); if (o.length === a.length) return e; return { ...e, plugins: { ...e.plugins, load: { ...e.plugins?.load, paths: o } } }; } async function resolveRequestedPluginInstallPaths(e, t, n, r) { if (!t.some((e) => extractMissingPluginLoadPath(e) !== null)) return /* @__PURE__ */ new Set(); const i = await loadInstalledPluginIndexInstallRecords(); const a = /* @__PURE__ */ new Set(); const o = n.bundledPluginId?.trim(); if (!o) return a; const s = i[o] ?? e.plugins?.installs?.[o]; for (const e of [s?.sourcePath, s?.installPath]) if (typeof e === "string" && e.trim()) a.add(resolveUserPath(e, r)); if (t.some((e) => extractMissingPluginLoadPath(e) !== null && !isOwnedMissingPluginLoadPathIssue(e, a, r))) { const e = (await listPersistedBundledPluginRecoveryLocations({ env: r })).filter((e) => e.pluginId === o).flatMap((e) => e.loadPaths); for (const t of e) a.add(resolveUserPath(t, r)); } return a; } async function recoverPluginInstallConfig(e, t) { if (resolvePluginInstallInvalidConfigPolicy(e) !== "allow-plugin-recovery") throw new PluginInstallConfigError("Config invalid; run `openclaw doctor --fix` before installing plugins."); const n = t.parsed ?? {}; if (!t.exists || Object.keys(n).length === 0) throw new PluginInstallConfigError("Config file could not be parsed; run `openclaw doctor` to repair it."); const r = await resolveRequestedPluginInstallPaths(t.config, t.issues, e, process.env); if (t.legacyIssues.length > 0 || t.issues.length === 0 || t.issues.some((t) => !isAllowedPluginRecoveryIssue(t, e, r))) { const t = e.bundledPluginId ?? "the requested plugin"; throw new PluginInstallConfigError(`Config invalid outside the plugin recovery path for ${t}; run \`openclaw doctor --fix\` before reinstalling it.`); } if (Object.hasOwn(n, "$include") || !supportsInstallConfigSingleTopLevelIncludeShape(isRecord(n) ? n.plugins : void 0)) throw new PluginInstallConfigError("Config plugin recovery uses an unsupported $include shape; use a single-file top-level plugins include or run `openclaw doctor --fix` before reinstalling it."); return removeOwnedMissingPluginLoadPaths(t.config, t.issues, r, process.env); } /** Read and authorize install configuration only after mutation-free request preflight. */ async function loadConfigForInstall(e) { const { snapshot: t, writeOptions: n } = await tracePluginLifecyclePhaseAsync("config read", () => readConfigFileSnapshotForWrite(), { command: "install" }); const r = selectInstallMutationWriteOptions(n); const i = t.valid ? t.sourceConfig : await recoverPluginInstallConfig(e, t); const a = asRecord(t.parsed); const { hookMutation: o, pluginMutation: s } = resolveInstallConfigMutationPreflights({ parsed: a, snapshotPath: t.path, writeOptions: r }); const c = { config: i, baseHash: t.hash, writeOptions: r, hookMutation: o, pluginMutation: s }; if ((!t.valid || e.installKind === "plugin") && s.mode === "blocked") throw new PluginInstallConfigError(s.reason, c); return c; } function readPluginInstallRecoveryMetadata(e) { const t = path.join(e, "package.json"); if (!fs.existsSync(t)) return { allowInvalidConfigRecovery: false }; const n = loadPluginManifest(e, false); const r = n.ok ? n.manifest.id : void 0; const i = tryReadJsonSync(t); return { ...r ? { pluginId: r } : {}, allowInvalidConfigRecovery: i?.openclaw?.install?.allowInvalidConfigRecovery === true }; } function resolvePluginInstallRecoveryMetadata(e, t, n) { if (t !== void 0) { const e = readPluginInstallRecoveryMetadata(t); return e.pluginId || e.allowInvalidConfigRecovery ? e : {}; } const r = parseNpmPrefixSpec(e); const i = n === "npm" ? r ?? e.trim() : n === void 0 ? r : null; const a = (n === void 0 || n === "official") && isBareNpmPackageName(e) ? e.trim() : void 0; if (!i && !a) return {}; const o = i ? parseRegistryNpmSpec(i)?.name : void 0; for (const e of listOfficialExternalPluginCatalogEntries()) { const t = resolveOfficialExternalPluginInstall(e); const n = resolveOfficialExternalPluginId(e); const r = t?.npmSpec?.trim(); if (!t || !(i ? r && (r === i || r === o) : n === a)) continue; return { ...n ? { pluginId: n } : {}, allowInvalidConfigRecovery: t?.allowInvalidConfigRecovery === true }; } return {}; } /** Resolve install metadata from the raw spec before Commander action handlers mutate config. */ function resolvePluginInstallRequestContext(e) { if (e.marketplace) return { ok: true, request: { rawSpec: e.rawSpec, installKind: "plugin", marketplace: e.marketplace } }; const t = resolveFileNpmSpecToLocalPath(e.rawSpec); if (t && !t.ok) return t; const n = t?.path ?? e.rawSpec; const r = resolveUserPath(e.localPath ?? n); const i = e.source ? e.source === "local" || e.source === "bundled" ? r : void 0 : t || fs.existsSync(r) ? r : resolveBundledInstallPlanBeforeNpm({ rawSpec: e.rawSpec, findBundledSource: (e) => findBundledPluginSource({ lookup: e }) })?.bundledSource.localPath; const a = resolvePluginInstallRecoveryMetadata(e.rawSpec, i, e.source); return { ok: true, request: { rawSpec: e.rawSpec, ...e.installKind === "plugin" || a.pluginId ? { installKind: "plugin" } : {}, ...a.pluginId ? { bundledPluginId: a.pluginId } : {}, ...a.allowInvalidConfigRecovery !== void 0 ? { allowInvalidConfigRecovery: a.allowInvalidConfigRecovery } : {} } }; } /** Decide whether invalid config should block a command before plugin recovery can run. */ function resolvePluginInstallInvalidConfigPolicy(e) { return e?.allowInvalidConfigRecovery === true ? "allow-plugin-recovery" : "deny"; } //#endregion export { resolvePluginInstallRequestContext as a, resolvePluginInstallInvalidConfigPolicy as i, loadConfigForInstall as n, resolveFullyBlockedConfigMutationReason as r, PluginInstallConfigError as t };