UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

447 lines (446 loc) • 19.6 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs"; import { a as redactSensitiveUrl } from "./redact-sensitive-url-D-E7DGoc.mjs"; import { n as validatePluginUiCapabilities } from "./plugin-ui-capabilities-CphHLqKu.mjs"; import { t as validatePluginCategories } from "./src-DFVOpZlE.mjs"; import { a as isClawHubTelemetryDisabled, d as readClawHubStringArrayField, f as readClawHubStringField, g as readRequiredClawHubStringField, h as readRequiredClawHubStringArrayField, i as fetchClawHubJson, m as readRequiredClawHubNumberField, o as isDefaultClawHubBaseUrl, p as readRequiredClawHubBooleanField, u as readClawHubNonEmptyStringFields, v as resolveClawHubBaseUrl, y as resolveClawHubImageUrl } from "./clawhub-client-BLg-dsn2.mjs"; import { o as parseClawHubPackageSecurityResponse } from "./clawhub-packages-BkEmBO-h.mjs"; //#region src/infra/clawhub-plugin-manifest.ts function parseClawHubPluginCapabilities(e) { const t = {}; const n = validatePluginUiCapabilities(e.uiCapabilities); if (n.ok && n.capabilities !== void 0) t.uiCapabilities = n.capabilities; for (const n of ["providers", "channels"]) { const r = readClawHubStringArrayField(e, n, "plugin manifest summary"); if (r) t[n] = r; } if (e.contracts != null) { if (!isRecord(e.contracts)) throw new Error("Malformed ClawHub plugin manifest summary: expected contracts to be an object."); const n = e.contracts; t.contracts = Object.fromEntries(Object.keys(n).toSorted().map((e) => [e, readRequiredClawHubStringArrayField(n, e, "plugin contracts")])); } return t; } function parseClawHubPluginCompatibility(e, t) { if (!e) return; const n = readClawHubNonEmptyStringFields(e, [ "pluginApiRange", "builtWithOpenClawVersion", "pluginSdkVersion", "minGatewayVersion" ], t); return Object.keys(n).length > 0 ? n : void 0; } function parseClawHubPluginMcpServer(e, t) { const n = `plugin MCP server ${t}`; if (!isRecord(e)) throw new Error(`Malformed ClawHub ${n}: expected an object.`); const r = readRequiredClawHubStringField(e, "name", n); const i = readClawHubStringField(e, "url", n); const a = readClawHubStringField(e, "scope", n); const o = readClawHubStringField(e, "setup", n); let s; let c = e.endpointRedacted === true; if (i && !c) { try { const e = new URL(i); const t = e.hostname.replace(/\.$/u, ""); if (i.length <= 2048 && e.protocol === "https:" && !e.username && !e.password && !e.hash && !e.port && t.includes(".") && !/^[\d.]+$/u.test(t) && !t.includes(":") && !/(?:^|\.)(?:localhost|local|internal|test|invalid)$/u.test(t) && redactSensitiveUrl(i) === i) s = i; } catch {} c = !s; } return { name: r, ...s ? { url: s } : {}, ...e.transport === "streamable-http" || e.transport === "sse" || e.transport === "stdio" ? { transport: e.transport } : {}, ...e.auth === "oauth" || e.auth === "api-key" || e.auth === "none" ? { auth: e.auth } : {}, ...a ? { scope: truncateUtf16Safe(a, 1e3) } : {}, ...o ? { setup: truncateUtf16Safe(o, 2e3) } : {}, ...c ? { endpointRedacted: c } : {} }; } //#endregion //#region src/infra/clawhub-plugin-release.ts /** Project exact release facts; listing and artifact URLs cannot prove stored download bytes. */ async function readClawHubPluginReleaseFacts(e) { const { value: t, versionRecord: n } = e; const r = n ? { version: readRequiredClawHubStringField(n, "version", "selected plugin release"), ...n.createdAt != null ? { createdAt: readRequiredClawHubNumberField(n, "createdAt", "selected plugin release") } : {}, ...n.changelog != null ? { changelog: readClawHubStringField(n, "changelog", "selected plugin release") ?? void 0 } : {}, tags: readClawHubStringArrayField(n, "distTags", "selected plugin release") ?? [] } : null; if (e.version && r?.version !== e.version) throw new Error("ClawHub did not return the requested plugin release."); const i = readClawHubStringField(t, "readme", "plugin detail response"); if (i && Buffer.byteLength(i, "utf8") > 524288) throw new Error("ClawHub plugin README exceeded 524288 bytes."); let a; let o; let s = r ? { status: "unknown", reason: "ClawHub does not expose artifact storage availability for this release." } : { status: "unavailable", reason: "The listing has no selected release." }; if (t.security != null) try { const n = parseClawHubPackageSecurityResponse(t.security); if (!r || n.package?.name && n.package.name !== e.packageName || n.release?.version && n.release.version !== r.version) throw new Error("ClawHub security metadata does not describe the selected release."); const { assessClawHubTrust: i } = await import("./clawhub-install-trust-CW8-Db_l.mjs"); const c = i(n); const l = n.trust; const u = l.moderationState && l.moderationState !== "approved" ? l.moderationState : void 0; const d = l.pending ? "pending" : l.stale ? "stale" : u ?? l.scanStatus ?? "unknown"; const f = d.trim().toLowerCase(); a = { status: c === "blocked" ? "blocked" : c !== "clean" && (f === "clean" || f === "benign") ? "review" : d, ...n.verdict ? { verdict: n.verdict } : {}, auditUrl: n.securityAuditUrl, summary: n.overview }; o = { disposition: c, reasons: n.trust.reasons, pending: n.trust.pending, stale: n.trust.stale }; if (n.trust.blockedFromDownload) s = { status: "unavailable", reason: n.trust.reasons.join("; ") || "ClawHub blocks downloads of this release." }; } catch {} return { selectedRelease: r, readme: i, security: a, trust: o, downloadability: s }; } //#endregion //#region src/infra/clawhub-plugin-catalog.ts const BARE_ICON_KEY = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/u; const PLUGIN_CATEGORY_ICON_KEYS = /* @__PURE__ */ new Set([ "activity", "book-open", "brain", "bot", "database", "git-branch", "globe", "message-circle", "message-square", "mic", "monitor", "package", "palette", "shield", "wrench", "plug", "code-xml", "server", "files", "inbox", "list-todo", "calendar-days", "wallet-cards", "megaphone", "chart-no-axes-combined", "workflow", "search" ]); function readOptionalNonNegativeNumber(e, t, n, r = "number") { const i = e[t]; if (i === void 0 || i === null) return; if (typeof i !== "number" || !Number.isFinite(i) || i < 0) throw new Error(`Malformed ClawHub ${n}: expected ${t} to be non-negative.`); if (r === "integer" && !Number.isInteger(i)) throw new Error(`Malformed ClawHub ${n}: expected ${t} to be an integer.`); return i; } function readOptionalBoolean(e, t, n) { return e[t] == null ? void 0 : readRequiredClawHubBooleanField(e, t, n); } function parseCatalogPackage(e, t, n) { if (!isRecord(e)) throw new Error(`Malformed ClawHub ${t}: expected package to be an object.`); const r = readRequiredClawHubStringField(e, "family", t); if (r !== "code-plugin" && r !== "bundle-plugin") throw new Error(`Malformed ClawHub ${t}: unsupported package family ${r}.`); const i = readOptionalRecord(e, "stats", t); const a = readClawHubNonEmptyStringFields(e, [ "summary", "ownerHandle", "latestVersion", "runtimeId" ], t); const o = readClawHubStringField(e, "icon", t) ?? readClawHubStringField(e, "ownerImage", t); const s = resolveClawHubImageUrl(o, n) ?? o; const c = readClawHubStringField(e, "verificationTier", t); const l = readOptionalBoolean(e, "featured", t); const u = readOptionalBoolean(e, "trending", t); const d = readOptionalNonNegativeNumber(e, "featuredRank", t, "integer"); const f = readOptionalNonNegativeNumber(e, "trendingRank", t, "integer"); const p = i ? readOptionalNonNegativeNumber(i, "downloads", `${t} stats`) : void 0; const m = i ? readOptionalNonNegativeNumber(i, "installs", `${t} stats`) : void 0; return { packageName: readRequiredClawHubStringField(e, "name", t), displayName: readRequiredClawHubStringField(e, "displayName", t), family: r, isOfficial: readOptionalBoolean(e, "isOfficial", t) === true, categories: readClawHubStringArrayField(e, "categories", t) ?? [], ...a, ...s ? { iconUrl: s } : {}, ...c ? { verificationTier: c } : {}, ...l !== void 0 ? { featured: l } : {}, ...u !== void 0 ? { trending: u } : {}, ...d !== void 0 ? { featuredRank: d } : {}, ...f !== void 0 ? { trendingRank: f } : {}, ...p !== void 0 ? { downloads: p } : {}, ...m !== void 0 ? { installs: m } : {} }; } function parsePluginCategories(e) { if (!isRecord(e) || !Array.isArray(e.categories)) throw new Error("Malformed ClawHub plugin categories response: expected categories to be an array."); const t = /* @__PURE__ */ new Set(); const n = /* @__PURE__ */ new Set(); return e.categories.map((e, r) => { if (!isRecord(e)) throw new Error(`Malformed ClawHub plugin category ${r}: expected an object.`); const i = readRequiredClawHubStringField(e, "slug", `plugin category ${r}`); const a = readRequiredClawHubStringField(e, "icon", `plugin category ${r}`); const o = readRequiredClawHubNumberField(e, "order", `plugin category ${r}`); if (!BARE_ICON_KEY.test(a)) throw new Error(`Malformed ClawHub plugin category ${i}: invalid icon key.`); if (!Number.isInteger(o) || o < 0 || t.has(i) || n.has(o)) throw new Error(`Malformed ClawHub plugin category ${i}: duplicate or invalid ordering.`); const s = readClawHubStringArrayField(e, "pinnedPackages", `plugin category ${i}`); if (s && (new Set(s).size !== s.length || s.some((e) => !e.trim() || e !== e.trim()))) throw new Error(`Malformed ClawHub plugin category ${i}: duplicate or invalid pinned package.`); t.add(i); n.add(o); return { slug: i, label: readRequiredClawHubStringField(e, "label", `plugin category ${i}`), description: readRequiredClawHubStringField(e, "description", `plugin category ${i}`), icon: PLUGIN_CATEGORY_ICON_KEYS.has(a) ? a : "package", order: o, ...s ? { pinnedPackages: s } : {} }; }).toSorted((e, t) => e.order - t.order); } function readOptionalRecord(e, t, n) { const r = e[t]; if (r === void 0 || r === null) return; if (!isRecord(r)) throw new Error(`Malformed ClawHub ${n}: expected ${t} to be an object.`); return r; } function parseManifest(e) { if (!e) return { configFields: [], mcpServers: [], skills: [] }; const { configFields: t, mcpServers: n, bundledSkills: r } = e; if (!Array.isArray(t) || !Array.isArray(n) || !Array.isArray(r)) throw new Error("Malformed ClawHub plugin manifest summary: expected capability arrays."); const i = parseClawHubPluginCompatibility(readOptionalRecord(e, "compatibility", "plugin manifest summary"), "plugin manifest compatibility"); const a = n.map(parseClawHubPluginMcpServer); return { ...i ? { compatibility: i } : {}, ...parseClawHubPluginCapabilities(e), configFields: t.map((e, t) => { if (!isRecord(e)) throw new Error(`Malformed ClawHub plugin config field ${t}: expected an object.`); const n = readClawHubStringField(e, "description", `plugin config field ${t}`); return Object.assign({ name: readRequiredClawHubStringField(e, "name", `plugin config field ${t}`), required: readRequiredClawHubBooleanField(e, "required", `plugin config field ${t}`), sensitive: readRequiredClawHubBooleanField(e, "sensitive", `plugin config field ${t}`) }, n ? { description: n } : {}); }), mcpServers: a.map(({ name: e }) => e), ...a.length ? { mcpServerDetails: a } : {}, skills: r.map((e, t) => { if (!isRecord(e)) throw new Error(`Malformed ClawHub bundled skill ${t}: expected an object.`); const n = readClawHubStringField(e, "description", `bundled skill ${t}`); return Object.assign({ name: readRequiredClawHubStringField(e, "name", `bundled skill ${t}`) }, n ? { description: n } : {}); }) }; } function parseVerification(e) { if (!e) return; const t = readClawHubNonEmptyStringFields(e, [ "summary", "sourceRepo", "sourceCommit", "sourcePath", "scanStatus" ], "plugin verification"); return { tier: readRequiredClawHubStringField(e, "tier", "plugin verification"), ...t }; } function parseVersions(e) { if (!isRecord(e) || !Array.isArray(e.items)) throw new Error("Malformed ClawHub plugin versions response: expected items to be an array."); return e.items.map((e, t) => { if (!isRecord(e)) throw new Error(`Malformed ClawHub plugin version ${t}: expected an object.`); const n = readClawHubStringField(e, "changelog", `plugin version ${t}`); return { version: readRequiredClawHubStringField(e, "version", `plugin version ${t}`), createdAt: readRequiredClawHubNumberField(e, "createdAt", `plugin version ${t}`), changelog: n ?? "", tags: readClawHubStringArrayField(e, "distTags", `plugin version ${t}`) ?? [] }; }); } async function fetchClawHubPluginCatalog(e) { const t = e.query?.trim(); const n = { baseUrl: e.baseUrl, token: e.token, skipAuth: e.skipAuth, timeoutMs: e.timeoutMs }; if (t) { const r = isClawHubTelemetryDisabled() ? void 0 : e.searchSource; const i = await fetchClawHubJson({ ...n, path: "/api/v1/plugins/search", retryTransientReads: r === void 0, search: { q: t, searchSource: r, category: e.category, isOfficial: e.officialOnly || e.intent === "official" ? "true" : void 0, limit: e.limit ? String(e.limit) : void 0 } }); const a = e.baseUrl; if (!isRecord(i) || !Array.isArray(i.results)) throw new Error("Malformed ClawHub plugin search response: expected results to be an array."); return { items: i.results.map((e, t) => { if (!isRecord(e)) throw new Error(`Malformed ClawHub plugin search result ${t}: expected an object.`); return parseCatalogPackage(e.package, `plugin search result ${t}`, a); }) }; } const r = await fetchClawHubJson({ ...n, path: "/api/v1/plugins", search: { category: e.category, cursor: e.cursor, featured: e.intent === "featured" ? "true" : void 0, isOfficial: e.officialOnly || e.intent === "official" ? "true" : void 0, curated: (e.intent ?? "all") === "all" && e.category ? "true" : void 0, sort: e.intent === "featured" ? void 0 : e.intent === "trending" ? "trending" : "downloads", limit: e.limit ? String(e.limit) : void 0 } }); const i = e.baseUrl; if (!isRecord(r) || !Array.isArray(r.items)) throw new Error("Malformed ClawHub plugin catalog response: expected items to be an array."); const a = readClawHubStringField(r, "nextCursor", "plugin catalog response"); return { items: r.items.map((e, t) => parseCatalogPackage(e, `plugin catalog item ${t}`, i)), ...a ? { nextCursor: a } : {}, ...r.categories !== void 0 ? { categories: parsePluginCategories(r) } : {} }; } async function fetchClawHubPluginOverview(e = {}) { const t = await fetchClawHubJson({ ...e, skipAuth: e.skipAuth ?? (!e.token && isDefaultClawHubBaseUrl(e.baseUrl)), path: "/api/v1/plugins/overview" }); if (!isRecord(t) || !Array.isArray(t.items)) throw new Error("Malformed ClawHub plugin overview response: expected items to be an array."); return { items: t.items.map((t, n) => parseCatalogPackage(t, `plugin overview item ${n}`, e.baseUrl)), categories: parsePluginCategories(t) }; } async function fetchClawHubPluginCategories(e = {}) { return parsePluginCategories(await fetchClawHubJson({ ...e, skipAuth: e.skipAuth ?? (!e.token && isDefaultClawHubBaseUrl(e.baseUrl)), path: "/api/v1/plugins/categories" })); } /** Read effective categories for exact installed ClawHub package versions in one request. */ async function fetchClawHubPluginVersionCategories(e) { if (e.packages.length === 0) return []; if (e.packages.length > 200) throw new Error("ClawHub plugin category batch cannot exceed 200 packages."); const t = await fetchClawHubJson({ ...e, method: "POST", path: "/api/v1/packages/categories:batch", json: { packages: e.packages } }); if (!isRecord(t) || !Array.isArray(t.packages)) throw new Error("Malformed ClawHub plugin category batch response: expected packages to be an array."); return t.packages.map((e, t) => { if (!isRecord(e)) throw new Error(`Malformed ClawHub plugin category batch item ${t}: expected an object.`); const n = e.categories; let r; if (n === null) r = null; else { const e = validatePluginCategories(n); if (!e.ok || !e.categories) throw new Error(`Malformed ClawHub plugin category batch item ${t}: expected categories to be a string array or null.`); r = e.categories; } return { name: readRequiredClawHubStringField(e, "name", `plugin category batch item ${t}`), version: readRequiredClawHubStringField(e, "version", `plugin category batch item ${t}`), categories: r }; }); } async function fetchClawHubPluginDetail(e) { const t = await fetchClawHubJson({ ...e, path: `/api/v1/packages/${encodeURIComponent(e.packageName)}/detail`, search: { version: e.version } }); if (!isRecord(t)) throw new Error("Malformed ClawHub plugin detail response: expected an object."); if (!isRecord(t.package)) throw new Error("Malformed ClawHub plugin detail response: expected package to be an object."); const n = parseCatalogPackage(t.package, "plugin detail", e.baseUrl); if (n.packageName !== e.packageName.trim().toLowerCase()) throw new Error("ClawHub returned a different plugin package identity."); const r = readOptionalRecord(t.package, "tags", "plugin detail") ?? {}; const i = Object.fromEntries(Object.keys(r).map((e) => [e, readRequiredClawHubStringField(r, e, "plugin tags")])); const a = readClawHubStringArrayField(t.package, "topics", "plugin detail") ?? []; const o = readOptionalNonNegativeNumber(t.package, "createdAt", "plugin detail"); const s = readOptionalNonNegativeNumber(t.package, "updatedAt", "plugin detail"); const c = parseClawHubPluginCompatibility(readOptionalRecord(t.package, "compatibility", "plugin detail"), "plugin compatibility"); const l = readOptionalRecord(t, "owner", "plugin detail response"); const u = readClawHubNonEmptyStringFields(l ?? {}, ["handle", "displayName"], "plugin owner"); const d = u.handle; const f = l ? readClawHubStringField(l, "image", "plugin owner") : void 0; const p = readOptionalRecord(t, "version", "plugin detail response"); const { selectedRelease: m, readme: h, security: g, trust: _, downloadability: v } = await readClawHubPluginReleaseFacts({ value: t, versionRecord: p, packageName: n.packageName, version: e.version }); const y = p ? readOptionalRecord(p, "pluginManifestSummary", "plugin version") : void 0; const b = parseManifest(y); const x = parseVerification(p ? readOptionalRecord(p, "verification", "plugin version") : void 0); const S = { ...u, ...f ? { imageUrl: f } : {}, ...typeof l?.official === "boolean" ? { official: l.official } : {} }; return { ...n, registry: resolveClawHubBaseUrl(e.baseUrl), tags: i, selectedRelease: m, downloadability: v, metadata: { manifest: y ? "available" : "missing", readme: h != null ? "available" : "missing", security: g ? "available" : "missing" }, ..._ ? { trust: _ } : {}, ...d && !n.ownerHandle ? { ownerHandle: d } : {}, ...Object.keys(S).length > 0 ? { owner: S } : {}, topics: a, ...o !== void 0 ? { createdAt: o } : {}, ...s !== void 0 ? { updatedAt: s } : {}, ...h ? { readme: h } : {}, ...x?.sourceRepo ? { repositoryUrl: x.sourceRepo } : {}, ...c ? { compatibility: c } : {}, ...b, versions: parseVersions(t.versions), ...x ? { verification: x } : {}, ...g ? { security: g } : {} }; } //#endregion export { fetchClawHubPluginVersionCategories as a, fetchClawHubPluginOverview as i, fetchClawHubPluginCategories as n, fetchClawHubPluginDetail as r, fetchClawHubPluginCatalog as t };