UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

365 lines (364 loc) • 14 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { b as withClawHubResponse, c as readClawHubBooleanField, f as readClawHubStringField, g as readRequiredClawHubStringField, i as fetchClawHubJson, l as readClawHubBytes, m as readRequiredClawHubNumberField, n as createClawHubError, r as decodeClawHubResponseBody, s as parseClawHubJsonBody, t as ClawHubRequestError, v as resolveClawHubBaseUrl, y as resolveClawHubImageUrl } from "./clawhub-client-BLg-dsn2.mjs"; import { t as reportClawHubInstallTelemetry } from "./clawhub-telemetry-BWeV91QR.mjs"; //#region src/infra/clawhub-skills.ts const SKILL_CARD_MAX_BYTES = 262144; const SKILL_VERIFICATION_MAX_BYTES = 67108864; const CLAWHUB_SKILLS_SH_TRUST_STATE = "not-scanned-by-clawhub"; const CLAWHUB_SKILLS_SH_TRUST_LABEL = "Not scanned by ClawHub"; /** Marks a reference ClawHub resolves from an external source it never scanned. */ const CLAWHUB_SKILLS_SH_REF_PREFIX = "skills-sh:"; /** Source variants ClawHub resolves search results from. Anything else is unidentifiable. */ const CLAWHUB_NATIVE_SOURCE_KIND = "clawhub"; const CLAWHUB_SKILLS_SH_SOURCE_KIND = "skills-sh"; const CLAWHUB_SUPPORTED_INSTALL_KINDS = /* @__PURE__ */ new Set([ "clawhub", "github", "skills-sh" ]); function buildVersionOrTagSearch(e) { const t = normalizeOptionalString(e.version); const n = normalizeOptionalString(e.ownerHandle); const r = t ? void 0 : normalizeOptionalString(e.tag); const i = t ? { version: t } : r ? { tag: r } : void 0; return i || n ? { ...i, ...n ? { ownerHandle: n } : {} } : void 0; } async function searchClawHubSkills(e) { const t = resolveClawHubBaseUrl(e.baseUrl); const n = e.query.trim(); const r = { ...e, baseUrl: t }; let i; if (n) i = (await fetchClawHubJson({ ...r, path: "/api/v1/search", search: { q: n, limit: e.limit ? String(e.limit) : void 0 } })).results ?? []; else i = ((await fetchClawHubJson({ ...r, path: "/api/v1/trending", search: { kind: "skills", limit: String(Math.min(e.limit ?? 20, 100)) } })).items ?? []).map((e) => ({ score: 0, slug: e.slug, displayName: e.displayName, summary: e.summary ?? void 0, source: e.source, install: e.install, official: e.official, ownerHandle: e.publisher?.handle, updatedAt: e.metrics?.updatedAt ?? void 0 })); return i.flatMap((e) => mapClawHubSkillSearchEntry(e, t)); } async function fetchClawHubSkillCatalog(e) { const t = e.query?.trim(); if (t && e.cursor) throw new Error("ClawHub skill search does not support a cursor."); const n = resolveClawHubBaseUrl(e.baseUrl); const r = !t && e.feed === "trending"; const i = await fetchClawHubJson({ ...e, baseUrl: n, path: t ? "/api/v1/packages/search" : r ? "/api/v1/trending" : "/api/v1/packages", search: { ...r ? { kind: "skills" } : { family: "skill", ...t ? { q: t } : { sort: "downloads" }, isOfficial: e.officialOnly ? "true" : void 0 }, limit: String(Math.min(e.limit ?? 100, 100)), cursor: e.cursor } }); const a = isRecord(i) ? i[t ? "results" : "items"] : void 0; if (!isRecord(i) || !Array.isArray(a)) throw new Error("Malformed ClawHub skill catalog: expected a result array."); const o = readClawHubStringField(i, "nextCursor", "skill catalog"); const s = a.map((e, n) => { const i = `skill catalog item ${n}`; if (!isRecord(e)) throw new Error(`Malformed ClawHub ${i}: expected an object.`); const a = t ? e.package : e; if (!isRecord(a)) throw new Error(`Malformed ClawHub ${i}: expected a package object.`); const o = { score: 0, slug: readRequiredClawHubStringField(a, r ? "slug" : "name", i), displayName: readRequiredClawHubStringField(a, "displayName", i), official: readClawHubBooleanField(a, r ? "official" : "isOfficial", i), summary: readClawHubStringField(a, "summary", i) ?? void 0, icon: readClawHubStringField(a, "icon", i) }; if (r) { const e = a.publisher; const t = a.metrics; const n = a.install; if (e !== null && e !== void 0 && !isRecord(e) || t !== void 0 && !isRecord(t) || !isRecord(n)) throw new Error(`Malformed ClawHub ${i}: invalid trending identity or metrics.`); return Object.assign(o, { source: readRequiredClawHubStringField(a, "source", i), install: { kind: readRequiredClawHubStringField(n, "kind", i), reference: readRequiredClawHubStringField(n, "reference", i) }, ownerHandle: e ? readClawHubStringField(e, "handle", i) : void 0, updatedAt: t ? readRequiredClawHubNumberField(t, "updatedAt", i) : void 0 }); } if (a.family !== "skill") throw new Error(`Malformed ClawHub ${i}: expected skill family.`); o.score = t ? readRequiredClawHubNumberField(e, "score", i) : 0; return Object.assign(o, { source: CLAWHUB_NATIVE_SOURCE_KIND, ownerHandle: readClawHubStringField(a, "ownerHandle", i), version: readClawHubStringField(a, "latestVersion", i) ?? void 0, updatedAt: readRequiredClawHubNumberField(a, "updatedAt", i) }); }).flatMap((e) => mapClawHubSkillSearchEntry(e, n)); if (r) for (let t = 0; t < s.length; t += 4) await Promise.all(s.slice(t, t + 4).map(async (t) => { if (t.installOnly) { t.official = void 0; return; } const r = await fetchClawHubJson({ ...e, baseUrl: n, path: `/api/v1/packages/${encodeURIComponent(t.slug)}`, search: { family: "skill", ownerHandle: t.ownerHandle ?? void 0 } }); if (!isRecord(r) || !isRecord(r.package)) throw new Error("Malformed ClawHub skill listing: expected a package object."); const i = r.package; t.official = i.family === "skill" && i.name === t.slug && i.ownerHandle === t.ownerHandle ? readClawHubBooleanField(i, "isOfficial", "skill listing") : void 0; })); return { items: s, ...o ? { nextCursor: o } : {} }; } /** * Records each result's own source once, here, so no consumer rebuilds it. A row whose source is * unknown, or whose external reference is missing, is dropped rather than published under * `@owner/slug`: that spelling would point install at a different publisher's skill. */ function mapClawHubSkillSearchEntry(e, t) { const { install: n, source: r, ...i } = e; const a = { ...i, registry: t, icon: resolveClawHubImageUrl(e.icon, t) }; const o = normalizeOptionalString(e.source); const s = normalizeOptionalString(e.install?.kind); const c = normalizeOptionalString(e.install?.reference); if (s && !CLAWHUB_SUPPORTED_INSTALL_KINDS.has(s)) return []; switch (o) { case CLAWHUB_SKILLS_SH_SOURCE_KIND: if (!c?.startsWith("skills-sh:")) return []; return { ...a, installRef: c, installOnly: true, trustState: CLAWHUB_SKILLS_SH_TRUST_STATE }; case CLAWHUB_NATIVE_SOURCE_KIND: { const t = normalizeOptionalString(e.ownerHandle); return t ? { ...a, installRef: `@${t}/${e.slug}` } : []; } default: return []; } } async function fetchClawHubSkillDetail(e) { const t = resolveClawHubBaseUrl(e.baseUrl); const n = await fetchClawHubJson({ ...e, baseUrl: t, path: `/api/v1/skills/${encodeURIComponent(e.slug)}`, search: e.ownerHandle ? { ownerHandle: e.ownerHandle } : void 0 }); if (n.skill && n.skill.slug !== e.slug) throw new Error("ClawHub returned details for a different skill."); if (e.ownerHandle && n.owner?.handle && n.owner.handle !== e.ownerHandle) throw new Error("ClawHub returned details for a different publisher."); if (!e.includeInspection && e.version === void 0) return { ...n, skill: n.skill ? { ...n.skill, icon: resolveClawHubImageUrl(n.skill.icon, t) } : null }; const r = e.ownerHandle ?? n.owner?.handle ?? void 0; const i = normalizeOptionalString(e.version) ?? n.latestVersion?.version; const a = { ...e, baseUrl: t, ownerHandle: r, version: i }; const o = []; const [s, c] = i ? await Promise.allSettled([fetchClawHubJson({ ...a, path: `/api/v1/skills/${encodeURIComponent(e.slug)}/versions/${encodeURIComponent(i)}`, search: r ? { ownerHandle: r } : void 0 }), fetchClawHubSkillCard(a)]) : []; if (s?.status === "rejected") o.push(`Selected release details unavailable: ${String(s.reason)}`); const l = s?.status === "fulfilled" ? s.value.version : void 0; const u = l?.version === i ? l : void 0; const d = s?.status === "fulfilled" && u === void 0; if (d) o.push("ClawHub returned details for a different release."); const f = s?.status === "rejected" && s.reason instanceof ClawHubRequestError && [404, 410].includes(s.reason.status); const p = u?.security; const m = p && typeof p.status === "string" && p.status.trim().length > 0 && typeof p.hasWarnings === "boolean" && typeof p.hasScanResult === "boolean"; const h = i !== void 0 && i === n.latestVersion?.version; const g = h && !f && !d; const _ = u ?? (g ? n.latestVersion : void 0); return { ...n, registry: t, source: "clawhub", installRef: r ? `@${r}/${e.slug}` : e.slug, selectedRelease: _ ? { version: _.version, createdAt: _.createdAt, changelog: _.changelog, ...u ? { tags: Object.entries(n.skill?.tags ?? {}).filter(([, e]) => e === u.version).map(([e]) => e) } : {} } : null, downloadability: !i ? { status: "unknown", reason: "The listing has no hosted release; source-backed availability is not reported." } : f ? { status: "unavailable", reason: "The selected release is not available from ClawHub." } : { status: "unknown", reason: s?.status === "rejected" ? `Selected release details unavailable: ${String(s.reason)}` : d ? "ClawHub returned details for a different release." : "ClawHub does not report downloadability for a selected skill release." }, card: c?.status === "fulfilled" ? { status: "available", content: c.value } : { status: "unavailable", reason: c?.status === "rejected" ? String(c.reason) : "No published release is available for a skill card." }, requirements: g && n.metadata?.setup ? { status: "available", setup: n.metadata.setup, os: n.metadata.os, systems: n.metadata.systems, scope: "registry-setup", note: "Registry setup keys combine environment and configuration requirements; binary requirements are not reported." } : { status: "unavailable", reason: h ? "ClawHub does not report setup requirements for this release." : "ClawHub reports structured setup requirements only for the latest release." }, security: m ? { status: "available", scanStatus: p.status, hasWarnings: p.hasWarnings, hasScanResult: p.hasScanResult, checkedAt: typeof p.checkedAt === "number" ? p.checkedAt : null, summary: typeof p.scanners?.llm?.summary === "string" ? p.scanners.llm.summary : null, virustotalUrl: typeof p.virustotalUrl === "string" ? p.virustotalUrl : null } : { status: "unavailable", reason: "ClawHub has no security scan snapshot for this release." }, warnings: o, skill: n.skill ? { ...n.skill, icon: resolveClawHubImageUrl(n.skill.icon, t) } : null, owner: n.owner ? { ...n.owner, image: resolveClawHubImageUrl(n.owner.image, t) } : n.owner }; } async function fetchClawHubSkillInstallResolution(e) { return await withClawHubResponse({ ...e, path: `/api/v1/skills/${encodeURIComponent(e.slug)}/install`, search: { ownerHandle: e.ownerHandle, reference: e.requestedReference, forceInstall: e.forceInstall ? "1" : void 0 } }, async ({ response: t, url: n, hasToken: r }) => { const i = [ 403, 409, 410, 423 ].includes(t.status); if (!t.ok && !i) throw await createClawHubError(t, n, r, e.timeoutMs); return parseClawHubJsonBody(t, n, e.timeoutMs); }); } async function fetchClawHubSkillVerification(e) { return await fetchClawHubJson({ ...e, path: `/api/v1/skills/${encodeURIComponent(e.slug)}/verify`, maxResponseBytes: SKILL_VERIFICATION_MAX_BYTES, search: { ...buildVersionOrTagSearch(e), reference: e.requestedReference } }); } async function fetchClawHubSkillSecurityVerdicts(e) { return await fetchClawHubJson({ ...e, path: "/api/v1/skills/-/security-verdicts", method: "POST", json: { items: e.items } }); } async function fetchClawHubSkillCard(e) { const t = normalizeOptionalString(e.url); const n = normalizeOptionalString(e.slug); if (!t && !n) throw new Error("ClawHub skill card fetch requires a slug or card URL"); const r = normalizeOptionalString(e.token); const i = t != null && r == null && new URL(t, `${resolveClawHubBaseUrl(e.baseUrl)}/`).origin !== new URL(`${resolveClawHubBaseUrl(e.baseUrl)}/`).origin; return await withClawHubResponse({ ...e, url: t, path: n ? `/api/v1/skills/${encodeURIComponent(n)}/card` : void 0, token: r, search: t ? void 0 : buildVersionOrTagSearch(e), skipAuth: i }, async ({ response: t, url: r, hasToken: i }) => { if (!t.ok) throw await createClawHubError(t, r, i, e.timeoutMs); const a = await readClawHubBytes({ response: t, maxBytes: SKILL_CARD_MAX_BYTES, timeoutMs: e.timeoutMs, resourceLabel: n ? `skill card for ${n}` : `skill card at ${r.pathname}` }); return decodeClawHubResponseBody(a); }); } async function reportClawHubSkillInstallTelemetry(e) { return await reportClawHubInstallTelemetry(e, () => { const t = e.slug.trim(); return t ? { event: "install", slug: t, ...e.ownerHandle ? { ownerHandle: e.ownerHandle } : {}, ...e.requestedReference ? { reference: e.requestedReference } : {}, ...e.trustState ? { trustState: e.trustState } : {}, version: e.version ?? void 0 } : void 0; }); } //#endregion export { fetchClawHubSkillCatalog as a, fetchClawHubSkillSecurityVerdicts as c, searchClawHubSkills as d, fetchClawHubSkillCard as i, fetchClawHubSkillVerification as l, CLAWHUB_SKILLS_SH_TRUST_LABEL as n, fetchClawHubSkillDetail as o, CLAWHUB_SKILLS_SH_TRUST_STATE as r, fetchClawHubSkillInstallResolution as s, CLAWHUB_SKILLS_SH_REF_PREFIX as t, reportClawHubSkillInstallTelemetry as u };