UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

344 lines (343 loc) • 13.9 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { c as resolveUserPath, s as resolveUserFilesystemPath } from "./home-dir-EKmgmdPG.mjs"; import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs"; import "./utils-C8YECn2Q.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { n as isBundledPluginInsideDevSourceRoot } from "./dev-source-root-HNpp2lb0.mjs"; import { n as normalizeClawHubSha256Integrity } from "./clawhub-integrity-DTNmm7ZL.mjs"; import { M as parseClawHubPluginSpec, O as resolveDefaultNpmSpec, i as getOfficialExternalPluginCatalogEntryForPackage, l as listOfficialExternalPluginCatalogEntries, m as resolveOfficialExternalPluginInstallSources, p as resolveOfficialExternalPluginInstall, r as getOfficialExternalPluginCatalogEntry } from "./official-external-plugin-catalog-DtkWz1Xo.mjs"; import { s as parseRegistryNpmSpec } from "./npm-registry-spec-HKkAvl6N.mjs"; import { g as resolveOfficialExternalPluginId, o as getOfficialExternalPluginCatalogManifest } from "./official-external-plugin-catalog-source-4o5oPhVN.mjs"; import { t as ManagedPluginLifecycleError } from "./management-lifecycle-error-COJYzrtT.mjs"; import { d as resolveArchiveKind } from "./archive-xLn9WKtS.mjs"; import { o as isDefaultClawHubBaseUrl } from "./clawhub-client-BLg-dsn2.mjs"; import { n as looksLikeLocalInstallSpec } from "./install-spec-BA2WRNap.mjs"; import { n as findBundledPluginSourceInMap, r as getProcessBundledPluginSources, t as findBundledPluginSource } from "./bundled-sources-CIS6N5EC.mjs"; import { r as parseGitPluginSpec } from "./git-install-BejJds3T.mjs"; import { a as resolveFileNpmSpecToLocalPath, i as resolveBundledInstallPlanBeforeNpm, n as parseNpmPackPrefixPath, r as parseNpmPrefixSpec, t as isBareNpmPackageName } from "./install-source-spec-BYmQ0ff-.mjs"; import { g as resolveOfficialEntryById } from "./management-catalog-CIUzoNKS.mjs"; import fs from "node:fs"; import { pathToFileURL } from "node:url"; import path from "node:path"; //#region src/plugins/official-external-install-trust.ts function resolveCatalogOfficialExternalInstallPlan(e) { const t = resolveDefaultNpmSpec(e); if (!t) return null; const n = getOfficialExternalPluginCatalogEntry(t.name) ?? getOfficialExternalPluginCatalogEntryForPackage(t.name); const r = n && resolveOfficialExternalPluginId(n); const i = n ? resolveOfficialInstallSources(n, t.selector) : []; const a = i[0]; return r && a ? { pluginId: r, spec: a.spec, installSources: i } : null; } function resolveCatalogOfficialExternalNpmPackageTrust(e) { const t = parseRegistryNpmSpec(e); const n = t && getOfficialExternalPluginCatalogEntryForPackage(t.name); const r = n && resolveOfficialExternalPluginId(n); const i = n && resolveOfficialExternalPluginInstallSources(n).find((e) => e.source === "npm"); if (!t || !r || !i || parseRegistryNpmSpec(i.spec)?.name !== t.name) return null; return { pluginId: r, ...i.expectedIntegrity && i.spec === e.trim() ? { expectedIntegrity: i.expectedIntegrity } : {}, trustedSourceLinkedOfficialInstall: true }; } function resolveOfficialInstallSources(e, t) { return resolveOfficialExternalPluginInstallSources(e).map((e) => { if (!t) return e; const n = e.source === "npm" ? parseRegistryNpmSpec(e.spec)?.name : parseClawHubPluginSpec(e.spec)?.name; if (!n) return e; const r = `${e.source === "clawhub" ? "clawhub:" : ""}${n}@${t}`; return r === e.spec ? e : { source: e.source, spec: r }; }); } //#endregion //#region src/plugins/install-provenance.ts const NON_CLAWHUB_INSTALL_FORCE_FLAG = "--force"; function resolveOpenClawTrustedNpmPackageInstall(e, t = getProcessBundledPluginSources()) { const n = parseRegistryNpmSpec(e)?.name; if (!n) return null; const r = findBundledPluginSourceInMap({ bundled: t, lookup: { kind: "npmSpec", value: n } }); if (r) return { pluginId: r.pluginId }; return resolveCatalogOfficialExternalNpmPackageTrust(e); } const sourceClassLabels = { git: "Git repository", "local-archive": "local archive", "local-path": "local path", marketplace: "marketplace source", npm: "npm registry", "npm-pack": "local npm-pack archive" }; function formatNonClawHubInstallWarning(e) { return [`WARNING - Installing plugin from ${sourceClassLabels[e.sourceClass]}: ${sanitizeTerminalText(e.spec)}`, "This source is outside ClawHub review and trust metadata. Only continue if you trust the publisher, package contents, and install source."].join("\n"); } //#endregion //#region src/plugins/install-source-plan.ts function isLocalGitUrl(e) { return path.isAbsolute(e) || e.startsWith("file:") || !URL.canParse(e) && !/^[^@\s]+@[^:\s]+:.+/.test(e); } /** Local artifacts belong to the connecting host, not a remote Gateway's filesystem. */ function pluginInstallRequiresLocalHost(e) { if (e.source === "local" || e.source === "npm-pack" || e.source === "marketplace") return true; if (e.source === "git") { const t = parseGitPluginSpec(e.spec); return t !== null && isLocalGitUrl(t.url); } return false; } function sourcePlan(e, t, n, r = {}) { return { ok: true, request: e, ...r, ...n ? { acknowledgement: { sourceClass: n, spec: t } } : {} }; } function resolvePluginInstallSourcePlan(e) { const t = resolveFileNpmSpecToLocalPath(e.raw); if (t && !t.ok) return t; const n = t?.path ?? e.raw; let r; let i; try { r = resolveUserFilesystemPath(n); } catch (e) { i = e; } if (r && fs.existsSync(r)) { const t = resolveArchiveKind(r) ? "archive" : "path"; const n = t === "path" ? findBundledPluginSource({ lookup: { kind: "localPath", value: r } }) : void 0; return sourcePlan({ source: "local", path: r, mode: e.mode, ...e.link ? { link: true } : {} }, e.raw, n ? void 0 : t === "archive" ? "local-archive" : "local-path"); } const a = parseNpmPackPrefixPath(e.raw); if (a !== null) return a ? sourcePlan({ source: "npm-pack", archivePath: resolveUserFilesystemPath(a), mode: e.mode }, e.raw, "npm-pack") : { ok: false, error: "Unsupported npm-pack plugin spec: missing archive path." }; const o = e.raw.trim().toLowerCase().startsWith("git:"); const s = parseGitPluginSpec(e.raw); if (o) return s ? sourcePlan({ source: "git", spec: isLocalGitUrl(s.url) && !s.url.startsWith("file:") ? `git:${pathToFileURL(resolveUserPath(s.url)).href}${s.ref ? `@${s.ref}` : ""}` : e.raw, mode: e.mode }, e.raw, "git") : { ok: false, error: `unsupported git: plugin spec: ${e.raw}` }; function planClawHub(t) { return t ? sourcePlan({ source: "clawhub", packageName: t.name, version: t.version, mode: e.mode }, e.raw) : { ok: false, error: `Unsupported ClawHub plugin spec: ${e.raw}` }; } if (e.raw.trim().toLowerCase().startsWith("clawhub:")) return planClawHub(parseClawHubPluginSpec(e.raw)); const c = parseNpmPrefixSpec(e.raw); if (c !== null && !c) return { ok: false, error: "Unsupported npm plugin spec: missing package." }; if (c === null && (t !== null || looksLikeLocalInstallSpec(e.raw, e.raw.includes("/") ? [] : [ ".tgz", ".tar.gz", ".tar", ".zip" ]))) return { ok: false, error: i ? `Plugin path not found: ${n} (${formatErrorMessage(i)})` : `Plugin path not found: ${r}` }; const l = resolveBundledInstallPlanBeforeNpm({ rawSpec: e.raw, findBundledSource: (e) => findBundledPluginSource({ lookup: e }) }); if (l) return sourcePlan({ source: "bundled", pluginId: l.bundledSource.pluginId, spec: e.raw }, e.raw, void 0, { warning: l.warning, localPath: l.bundledSource.localPath }); const u = c === null && isBareNpmPackageName(e.raw) ? resolveCatalogOfficialExternalInstallPlan(e.raw) : null; if (u && u.pluginId === e.raw.trim()) return sourcePlan({ source: "official", pluginId: u.pluginId, mode: e.mode, ...e.pin ? { pin: true } : {} }, e.raw); if (c === null) return planClawHub(e.raw.includes(":") ? null : parseClawHubPluginSpec(`clawhub:${e.raw}`)); const d = resolveOpenClawTrustedNpmPackageInstall(c); return sourcePlan({ source: "npm", spec: c, mode: e.mode, ...e.pin ? { pin: true } : {} }, e.raw, d ? void 0 : "npm"); } function resolveBundledInstallPlanForCatalogEntry(e) { const t = e.pluginId.trim(); const n = e.npmSpec.trim(); if (!t || !n) return null; const r = e.findBundledSource({ kind: "npmSpec", value: n }); if (r?.pluginId === t) return { bundledSource: r }; const i = e.findBundledSource({ kind: "pluginId", value: t }); if (i?.pluginId !== t || i.npmSpec && i.npmSpec !== n) return null; return { bundledSource: i }; } /** Explicitly declared runtime id, ignoring the entry-id fallback used for display. */ function resolveDeclaredOfficialPluginId(e) { const t = getOfficialExternalPluginCatalogManifest(e); return normalizeOptionalString(t?.plugin?.id) ?? normalizeOptionalString(t?.channel?.id) ?? normalizeOptionalString(t?.providers?.[0]?.id); } function resolveOfficialEntryByClawHubPackage(e, t) { return e.find((e) => resolveOfficialExternalPluginInstallSources(e).some((e) => e.source === "clawhub" && parseClawHubPluginSpec(e.spec)?.name === t)); } /** Public requests carry intent and constraints; catalog provenance is resolved by this owner. */ function resolveManagedPluginInstallRequest(e, t, n) { const r = e.mode ?? "install"; switch (e.source) { case "official": { const i = resolveOfficialEntryById(t, e.pluginId) ?? resolveOfficialEntryById(listOfficialExternalPluginCatalogEntries(), e.pluginId); if (!i) throw new ManagedPluginLifecycleError(`unknown official plugin catalog entry: ${e.pluginId}`); const a = resolveOfficialExternalPluginId(i); const o = resolveOfficialExternalPluginInstall(i); if (!a || !o) throw new ManagedPluginLifecycleError(`official plugin catalog entry is not installable: ${e.pluginId}`); const s = !e.version && !e.pin ? findBundledPluginSource({ lookup: { kind: "pluginId", value: a } }) : void 0; if (s && isBundledPluginInsideDevSourceRoot({ rootDir: s.localPath, env: process.env })) return { source: "bundled", rawSpec: o.npmSpec ?? a, bundledSource: s }; const c = resolveOfficialInstallSources(i, e.version); const l = c[0]; if (!l) throw new ManagedPluginLifecycleError(`official plugin catalog entry has no supported install source: ${e.pluginId}`); return { source: "official", clawhubUrl: n, spec: l.spec, installSources: c, expectedPluginId: resolveDeclaredOfficialPluginId(i), mode: r, ...e.pin ? { pin: true } : {} }; } case "clawhub": { const i = e.packageName.trim(); const a = isDefaultClawHubBaseUrl(n); const o = a ? resolveOfficialEntryByClawHubPackage([...listOfficialExternalPluginCatalogEntries(), ...t], i) : void 0; const s = o ? resolveDeclaredOfficialPluginId(o) : void 0; const c = a ? resolveOfficialEntryByClawHubPackage(t, i) : void 0; const l = c ? resolveOfficialExternalPluginInstallSources(c).find((e) => e.source === "clawhub") : void 0; const u = parseClawHubPluginSpec(l?.spec ?? ""); const d = !e.version || e.version === u?.version; const f = e.version ?? u?.version; const p = d ? l?.expectedIntegrity : void 0; const m = parseClawHubPluginSpec(`clawhub:${i}`); if (!m || m.version) throw new ManagedPluginLifecycleError(`invalid ClawHub package name: ${i}`); if (s && e.expectedPluginId && s !== e.expectedPluginId) throw new ManagedPluginLifecycleError("Requested plugin identity differs from the official catalog."); if (p && e.expectedIntegrity && p !== normalizeClawHubSha256Integrity(e.expectedIntegrity)) throw new ManagedPluginLifecycleError("Requested artifact integrity differs from the official catalog."); return { source: "clawhub", clawhubUrl: n, spec: `clawhub:${i}${f ? `@${f}` : ""}`, ...!e.version && !e.expectedIntegrity ? { recordSpec: `clawhub:${i}` } : {}, mode: r, ...o ? { trustedSourceLinkedOfficialInstall: true } : {}, expectedPluginId: s ?? e.expectedPluginId, expectedIntegrity: p ?? e.expectedIntegrity }; } case "bundled": { const t = findBundledPluginSource({ lookup: { kind: "pluginId", value: e.pluginId } }); if (!t) throw new ManagedPluginLifecycleError(`Unknown bundled plugin: ${e.pluginId}`); return { source: "bundled", rawSpec: e.spec ?? e.pluginId, bundledSource: t }; } case "local": { const t = resolveUserPath(e.path); const n = findBundledPluginSource({ lookup: { kind: "localPath", value: t } }); return { source: "local", path: t, mode: r, recordSource: resolveArchiveKind(t) ? "archive" : "path", ...e.link ? { link: true } : {}, ...n ? { bundledOrigin: true } : {} }; } case "npm": { const t = resolveOpenClawTrustedNpmPackageInstall(e.spec); if (t && e.expectedPluginId && t.pluginId !== e.expectedPluginId) throw new ManagedPluginLifecycleError("Requested plugin identity differs from the official npm identity."); if (t?.expectedIntegrity && e.expectedIntegrity && t.expectedIntegrity !== e.expectedIntegrity.trim()) throw new ManagedPluginLifecycleError("Requested artifact integrity differs from the official npm artifact."); return { ...e, mode: r, ...t ? { trustedSourceLinkedOfficialInstall: true } : {}, expectedPluginId: t?.pluginId ?? e.expectedPluginId, expectedIntegrity: t?.expectedIntegrity ?? e.expectedIntegrity }; } case "npm-pack": case "git": case "marketplace": return { ...e, mode: r }; default: throw new ManagedPluginLifecycleError("Unsupported plugin install source."); } } //#endregion export { NON_CLAWHUB_INSTALL_FORCE_FLAG as a, resolvePluginInstallSourcePlan as i, resolveBundledInstallPlanForCatalogEntry as n, formatNonClawHubInstallWarning as o, resolveManagedPluginInstallRequest as r, pluginInstallRequiresLocalHost as t };