UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

209 lines (208 loc) • 9.9 kB
import { p as isRecord } from "./utils-BclMknzT.mjs"; import { B as normalizeOptionalString } from "./path-guards-quCEQx1i.mjs"; import { p as uniqueStrings } from "./string-normalization-BMFMScZY.mjs"; //#region src/compat/legacy-names.ts /** Canonical metadata key shared by package manifests and plugin catalogs. */ const MANIFEST_KEY = "openclaw"; //#endregion //#region src/plugins/official-external-plugin-catalog-source.ts var HostedCatalogSignedFeedMonotonicityError = class extends Error { constructor(e) { super(e); this.name = "HostedCatalogSignedFeedMonotonicityError"; } }; const SUPPORTED_OFFICIAL_EXTERNAL_CATALOG_FEED_SCHEMA_VERSIONS = /* @__PURE__ */ new Set([1, 2]); const DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_URL = "https://clawhub.ai/v1/feeds/plugins"; const DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE = "clawhub-public"; const DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_ID = "clawhub-official"; const DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_CLAWHUB_SOURCE_REF = "public-clawhub"; const DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_NPM_SOURCE_REF = "public-npm"; const DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_PROFILE_CONFIG = { feeds: { [DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE]: { url: DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_URL, feedId: DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_ID } }, sources: { [DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_CLAWHUB_SOURCE_REF]: { type: "clawhub", baseUrl: "https://clawhub.ai" }, [DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_NPM_SOURCE_REF]: { type: "npm", registry: "https://registry.npmjs.org/" } } }; const ISO_CALENDAR_DATE_PREFIX_RE = /^(\d{4})-(\d{2})-(\d{2})/u; function parseOfficialExternalPluginCatalogTimestamp(e) { const t = e.trim(); const n = Date.parse(t); if (!Number.isFinite(n)) return; const r = ISO_CALENDAR_DATE_PREFIX_RE.exec(t); if (!r) return n; const i = Number(r[1]); const a = Number(r[2]); const o = Number(r[3]); const s = [ 31, i % 4 === 0 && (i % 100 !== 0 || i % 400 === 0) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; return a >= 1 && a <= 12 && o >= 1 && o <= s[a - 1] ? n : void 0; } function isOfficialExternalPluginCatalogSequence(e) { return typeof e === "number" && Number.isSafeInteger(e) && e >= 0; } /** Compare authenticated feed ordering in both remote admission and snapshot writes. */ function isOfficialExternalPluginCatalogRollback(e) { const { candidate: t, current: n } = e; return t.sequence !== n.sequence ? t.sequence < n.sequence : t.generatedAt !== void 0 && n.generatedAt !== void 0 && Date.parse(t.generatedAt) < Date.parse(n.generatedAt); } function isOfficialExternalPluginCatalogFeed(e) { if (!isRecord(e)) return false; const t = e.sequence; const n = e.generatedAt; const r = typeof n === "string" ? parseOfficialExternalPluginCatalogTimestamp(n) : void 0; const i = e.entries; return typeof e.schemaVersion === "number" && SUPPORTED_OFFICIAL_EXTERNAL_CATALOG_FEED_SCHEMA_VERSIONS.has(e.schemaVersion) && typeof e.id === "string" && e.id.trim().length > 0 && typeof n === "string" && n.trim().length > 0 && r !== void 0 && isOfficialExternalPluginCatalogSequence(t) && Array.isArray(i); } function parseOfficialExternalPluginCatalogEntries(e) { const t = Array.isArray(e) ? e : isOfficialExternalPluginCatalogFeed(e) ? e.entries : isRecord(e) && !("schemaVersion" in e) ? e.entries ?? e.packages ?? e.plugins : void 0; return Array.isArray(t) ? t.filter((e) => isRecord(e)) : []; } function resolveOfficialExternalPluginCatalogProfileConfig(e) { const t = e?.feeds?.[DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE]; const n = DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_PROFILE_CONFIG.feeds?.[DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE] ?? { url: DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_URL, feedId: DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_ID }; return { feeds: { ...e?.feeds, [DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE]: { ...n, ...t } }, sources: { ...DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_PROFILE_CONFIG.sources, ...e?.sources } }; } function getFeedEntryInstallCandidateRecords(e) { const t = (isRecord(e.install) ? e.install : void 0)?.candidates; if (!Array.isArray(t)) return []; return t.filter((e) => isRecord(e)); } function getManifestInstallSourceRefCandidate(e) { const t = getOfficialExternalPluginCatalogManifest(e)?.install; if (!t) return; if (!Boolean(normalizeOptionalString(t.clawhubSpec) || normalizeOptionalString(t.npmSpec) || normalizeOptionalString(t.localPath))) return; return { sourceRef: normalizeOptionalString(t.sourceRef), package: normalizeOptionalString(t.npmSpec) ?? normalizeOptionalString(t.clawhubSpec) }; } function hasKnownCatalogSourceRef(e, t) { const n = normalizeOptionalString(e.sourceRef); return Boolean(n && t.has(n)); } function filterOfficialExternalPluginCatalogEntriesBySourceRefs(e, t) { let n; return e.filter((e) => { const r = n ??= new Set(Object.keys(resolveOfficialExternalPluginCatalogProfileConfig(t?.catalogConfig).sources)); let i = getFeedEntryInstallCandidateRecords(e); if (t?.requireManifestInstallSourceRef) { const t = getManifestInstallSourceRefCandidate(e); if (t) i = [...i, t]; else if (i.length === 0) i = [{}]; } return i.every((e) => hasKnownCatalogSourceRef(e, r)); }); } function dedupeOfficialExternalPluginCatalogEntries(e) { const t = /* @__PURE__ */ new Map(); for (const n of e) { const e = resolveOfficialExternalPluginCatalogEntryKey(n); if (e && !t.has(e)) t.set(e, n); } return [...t.values()]; } function resolveOfficialExternalPluginCatalogEntryKey(e) { const t = resolveOfficialExternalPluginId(e); if (t) return `${normalizeOptionalString(e.kind) ?? "plugin"}:${t}`; return normalizeOptionalString(e.name); } function getOfficialExternalPluginCatalogManifest(e) { const t = e[MANIFEST_KEY]; return isRecord(t) ? t : void 0; } function resolveOfficialExternalPluginId(e) { const t = getOfficialExternalPluginCatalogManifest(e); return normalizeOptionalString(t?.plugin?.id) ?? normalizeOptionalString(t?.channel?.id) ?? normalizeOptionalString(t?.providers?.[0]?.id) ?? normalizeOptionalString(e.id); } const OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_HOSTNAME_ALLOWLIST = ["clawhub.ai"]; function resolveHostedCatalogFeedUrl(e) { const t = URL.parse(e.trim()); if (!t) throw new Error("hosted catalog feed URL is invalid"); if (t.protocol !== "https:") throw new Error("hosted catalog feed URL must use HTTPS"); if (t.username || t.password) throw new Error("hosted catalog feed URL must not include credentials"); if (t.search || t.hash) throw new Error("hosted catalog feed URL must not include query strings or fragments"); return t; } function resolveHostedCatalogFeedSource(e) { const t = normalizeOptionalString(e.feedUrl); const n = normalizeOptionalString(e.feedProfile); if (t) { const r = resolveHostedCatalogFeedUrl(t); if (!OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_HOSTNAME_ALLOWLIST.includes(r.hostname)) throw new Error("hosted catalog feed URL hostname is not allowed"); const i = n === void 0 ? resolveOfficialExternalPluginCatalogProfileConfig(e.catalogConfig).feeds[DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE] : void 0; const a = n ?? (i && resolveHostedCatalogFeedUrl(i.url).href === r.href ? DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE : void 0); const o = a === void 0 ? void 0 : resolveOfficialExternalPluginCatalogProfileConfig(e.catalogConfig); const s = a === void 0 ? void 0 : o?.feeds[a]; if (a !== void 0 && !s) throw new Error(`hosted catalog feed profile "${a}" is not configured`); return { url: r, hostnameAllowlist: OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_HOSTNAME_ALLOWLIST, ...s?.feedId ? { expectedFeedId: s.feedId } : {}, ...s?.verification ? { verification: s.verification } : {} }; } const r = n ?? DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE; const i = resolveOfficialExternalPluginCatalogProfileConfig(e.catalogConfig).feeds[r]; if (!i) throw new Error(`hosted catalog feed profile "${r}" is not configured`); const a = resolveHostedCatalogFeedUrl(i.url); return { url: a, hostnameAllowlist: uniqueStrings([...OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_HOSTNAME_ALLOWLIST, a.hostname]), ...i.feedId ? { expectedFeedId: i.feedId } : {}, verification: i.verification }; } function shouldRequireManifestInstallSourceRef(e) { let t = normalizeOptionalString(e.feedUrl); if (!t) { const n = normalizeOptionalString(e.feedProfile) ?? DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE; if (n !== DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_PROFILE) return true; const r = resolveOfficialExternalPluginCatalogProfileConfig(e.catalogConfig); t = normalizeOptionalString(r.feeds[n]?.url); } try { return resolveHostedCatalogFeedUrl(t ?? DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_URL).href !== resolveHostedCatalogFeedUrl(DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_FEED_URL).href; } catch { return true; } } //#endregion export { MANIFEST_KEY as _, getFeedEntryInstallCandidateRecords as a, isOfficialExternalPluginCatalogFeed as c, parseOfficialExternalPluginCatalogTimestamp as d, resolveHostedCatalogFeedSource as f, shouldRequireManifestInstallSourceRef as g, resolveOfficialExternalPluginId as h, filterOfficialExternalPluginCatalogEntriesBySourceRefs as i, isOfficialExternalPluginCatalogRollback as l, resolveOfficialExternalPluginCatalogProfileConfig as m, HostedCatalogSignedFeedMonotonicityError as n, getOfficialExternalPluginCatalogManifest as o, resolveOfficialExternalPluginCatalogEntryKey as p, dedupeOfficialExternalPluginCatalogEntries as r, hasKnownCatalogSourceRef as s, DEFAULT_OFFICIAL_EXTERNAL_PLUGIN_CATALOG_PROFILE_CONFIG as t, parseOfficialExternalPluginCatalogEntries as u };