UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

310 lines (309 loc) • 11.9 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { c as readFileDescriptorBoundedSync, o as openRootFileSync } from "./boundary-file-read-BfaFLDqt.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { d as pathExists } from "./fs-safe-BbWvmdVn.mjs"; import { v as resolveClawHubBaseUrl } from "./clawhub-client-BLg-dsn2.mjs"; import { d as searchClawHubSkills, r as CLAWHUB_SKILLS_SH_TRUST_STATE, t as CLAWHUB_SKILLS_SH_REF_PREFIX } from "./clawhub-skills-C1KFinHC.mjs"; import { n as resolveWorkspaceSkillInstallDir, r as validateRequestedSkillSlug, t as normalizeTrackedSkillSlug } from "./install-paths-DuXfcnHt.mjs"; import { a as normalizeDownloadedArtifactLock, c as normalizeStoredRegistry, d as readClawHubSkillOriginStatusSync, f as readClawHubSkillOriginStrict, l as parseRequestedClawHubSkillRef, m as readClawHubSkillsLockfileStatusSync, p as readClawHubSkillsLockfile, r as describeClawHubSkillRefMismatch, s as normalizeSkillFileLock, u as readClawHubSkillOrigin, y as resolveWorkspaceClawHubSkills } from "./clawhub-store-BhwTU-Sz.mjs"; import fs from "node:fs"; import path from "node:path"; //#region src/skills/lifecycle/clawhub-status.ts const LOCAL_SKILL_CARD_FILENAME = "skill-card.md"; const LOCAL_SKILL_CARD_MAX_BYTES = 262144; function readRealPathSync(e) { try { return fs.realpathSync.native(e); } catch { return; } } function invalidLink(e, t) { return { status: "invalid", valid: false, reason: e, ...t }; } function resolveClawHubSkillStatusLinkSync(e) { const t = readClawHubSkillOriginStatusSync(e.skillDir); const n = e.lockRead ?? readClawHubSkillsLockfileStatusSync(e.workspaceDir); const r = `${e.lockfileScope ?? "workspace"} ClawHub lockfile`; if (t.kind === "missing") { let t; try { t = normalizeTrackedSkillSlug(e.skillKey); } catch { return; } const i = n.kind === "found" ? n.lock.skills[t] : void 0; if (!i) return; return invalidLink(`Skill "${t}" is tracked by the ${r} but is missing local ClawHub origin metadata.`, { slug: t, installedVersion: i.version, installedAt: i.installedAt, registry: normalizeStoredRegistry(i.registry ?? resolveClawHubBaseUrl()), lockPath: n.kind === "found" ? n.path : void 0 }); } if (t.kind === "malformed") return invalidLink(`Malformed ClawHub origin metadata at ${t.path}: ${t.error}`, { originPath: t.path, lockPath: n.kind === "found" ? n.path : void 0 }); const i = { registry: t.origin.registry, installedVersion: t.origin.installedVersion, installedAt: t.origin.installedAt, originPath: t.path }; let a; try { a = normalizeTrackedSkillSlug(t.origin.slug); } catch (e) { return invalidLink(`Invalid ClawHub origin slug "${t.origin.slug}": ${formatErrorMessage(e)}`, { ...i, slug: t.origin.slug, lockPath: n.kind === "found" ? n.path : void 0 }); } const invalid = (e, t = i.registry) => invalidLink(e, { ...i, registry: t, slug: a, ...n.kind === "missing" ? {} : { lockPath: n.path } }); if (n.kind === "missing") return invalid(`Skill "${a}" has ClawHub origin metadata but is not tracked by the ${r}.`); if (n.kind === "malformed") return invalid(`Malformed ${r} at ${n.path}: ${n.error}`); const o = n.lock.skills[a]; if (!o) return invalid(`Skill "${a}" has ClawHub origin metadata but is not tracked by the ${r}.`); const s = readRealPathSync(resolveWorkspaceSkillInstallDir(e.workspaceDir, a)); if (!s || readRealPathSync(e.skillDir) !== s) return invalid(`Skill "${a}" ClawHub origin metadata is not in the expected ClawHub install directory.`); const c = t.origin.registry; const l = o.registry === void 0 ? c : normalizeStoredRegistry(o.registry); const u = normalizeOptionalString(o.sourceUrl); const d = normalizeOptionalString(o.ownerHandle); const f = normalizeOptionalString(o.requestedReference); const p = o.trustState === "not-scanned-by-clawhub" ? CLAWHUB_SKILLS_SH_TRUST_STATE : void 0; const m = normalizeDownloadedArtifactLock(o.artifact); const h = normalizeSkillFileLock(o.skillFile); const g = normalizeOptionalString(o.fileTreeSha256); const _ = t.origin.ownerHandle === d && t.origin.requestedReference === f && t.origin.trustState === p && t.origin.sourceUrl === u && t.origin.artifact?.kind === m?.kind && t.origin.artifact?.sha256 === m?.sha256 && t.origin.artifact?.integrity === m?.integrity && t.origin.skillFile?.path === h?.path && t.origin.skillFile?.sha256 === h?.sha256 && t.origin.fileTreeSha256 === g; if (o.version !== t.origin.installedVersion || o.installedAt !== t.origin.installedAt || l !== c || !_) return invalid(`Skill "${a}" ClawHub origin metadata does not match the ${r}.`, l); const { version: v, ...y } = t.origin; return { ...y, status: "linked", valid: true, registry: l, slug: a, originPath: t.path, lockPath: n.path }; } function readLocalSkillCardSync(e, t = false) { const n = path.join(e, LOCAL_SKILL_CARD_FILENAME); let r; try { const i = openRootFileSync({ absolutePath: n, rootPath: e, boundaryLabel: "skill directory", maxBytes: LOCAL_SKILL_CARD_MAX_BYTES, rejectHardlinks: false }); if (!i.ok) return; r = i.fd; const a = { present: true, path: n, sizeBytes: i.stat.size }; if (t) a.content = readFileDescriptorBoundedSync(r, LOCAL_SKILL_CARD_MAX_BYTES).toString("utf8"); return a; } catch { return; } finally { if (r !== void 0) try { fs.closeSync(r); } catch {} } } function readLocalSkillCardContentSync(e) { return readLocalSkillCardSync(e, true)?.content; } async function searchSkillsFromClawHub(e) { return await searchClawHubSkills({ query: e.query?.trim() ?? "", limit: e.limit, signal: e.signal }); } async function resolveClawHubSkillVerificationTarget(e) { try { const t = resolveWorkspaceClawHubSkills(e.workspaceDir); if (t) return await t.resolveClawHubSkillVerificationTarget({ ...e, baseUrl: resolveClawHubBaseUrl(e.baseUrl) }); const n = normalizeOptionalString(e.version); const r = normalizeOptionalString(e.tag); if (n && r) return { ok: false, error: "Use either --version or --tag." }; const i = parseRequestedClawHubSkillRef(e.slug); if (i.requestedReference && (n || r)) return { ok: false, error: "--version and --tag are not supported for skills-sh references." }; const a = i.slug; const o = resolveWorkspaceSkillInstallDir(e.workspaceDir, a); const s = await readClawHubSkillOriginStrict(o); if (s.kind === "malformed") return { ok: false, error: `Malformed ClawHub origin metadata at ${s.path}: ${s.error}` }; if (s.kind === "found") { const t = (await readClawHubSkillsLockfile(e.workspaceDir)).skills[a]; if (!t) return { ok: false, error: `Skill "${a}" has ClawHub origin metadata but is not tracked by the workspace ClawHub lockfile. Reinstall it from ClawHub before verifying it as an installed ClawHub skill.` }; if (normalizeTrackedSkillSlug(s.origin.slug) !== a) return { ok: false, error: `Skill "${a}" has ClawHub origin metadata for "${s.origin.slug}". Reinstall it from ClawHub before verifying it as an installed ClawHub skill.` }; const c = s.origin.registry; const l = t.registry === void 0 ? c : normalizeStoredRegistry(t.registry); const u = normalizeOptionalString(t.ownerHandle); const d = normalizeOptionalString(t.requestedReference); const f = t.trustState === "not-scanned-by-clawhub" ? CLAWHUB_SKILLS_SH_TRUST_STATE : void 0; if (t.version !== s.origin.installedVersion || t.installedAt !== s.origin.installedAt || l !== c || s.origin.ownerHandle !== u || s.origin.requestedReference !== d || s.origin.trustState !== f) return { ok: false, error: `Skill "${a}" ClawHub origin metadata does not match the workspace ClawHub lockfile. Reinstall it from ClawHub before verifying it as an installed ClawHub skill.` }; if (d && (n || r)) return { ok: false, error: "--version and --tag are not supported for skills-sh references." }; const p = describeClawHubSkillRefMismatch(i, { ownerHandle: u, requestedReference: d }); if (p) return { ok: false, error: p }; const m = n ? "version" : r ? "tag" : "installed-version"; const h = d ? void 0 : n ?? (r ? void 0 : t.version); return { ok: true, slug: a, ...u ? { ownerHandle: u } : {}, ...d ? { requestedReference: d } : {}, ...f ? { trustState: f } : {}, baseUrl: l, version: h, tag: d ? void 0 : r, resolution: { source: "installed", selector: m, registry: l, skillDir: o, installedVersion: t.version } }; } const c = readClawHubSkillsLockfileStatusSync(e.workspaceDir); if (c.kind === "malformed") return { ok: false, error: `Malformed workspace ClawHub lockfile at ${c.path}: ${c.error}` }; if (c.kind === "found" && c.lock.skills[a]) return { ok: false, error: `Skill "${a}" is tracked by the workspace ClawHub lockfile but is missing ClawHub origin metadata. Reinstall it from ClawHub before verifying it as an installed ClawHub skill.` }; const l = resolveClawHubBaseUrl(e.baseUrl); const u = n ? "version" : r ? "tag" : "latest"; return { ok: true, ...i, baseUrl: l, version: n, tag: r, resolution: { source: "registry", selector: u, registry: l, skillDir: void 0, installedVersion: void 0 } }; } catch (e) { return { ok: false, error: formatErrorMessage(e) }; } } async function preflightSkillOwnerState(e) { const t = resolveWorkspaceSkillInstallDir(e.workspaceDir, e.requested.slug); if (!await pathExists(t)) return { ok: true, action: "install", integrity: e.integrity }; const n = resolveClawHubSkillStatusLinkSync({ workspaceDir: e.workspaceDir, skillDir: t, skillKey: e.requested.slug }); if (n?.status === "linked" && n.installedVersion === e.version && n.ownerHandle === e.requested.ownerHandle && n.artifact?.integrity === e.integrity) return { ok: true, action: "reuse", integrity: e.integrity }; return { ok: false, code: "skill_version_conflict", error: `Skill ${e.requestedLabel}@${e.version} conflicts with the existing workspace skill at ${t}.` }; } async function resolveRequestedUpdateSlug(e) { const t = e.requestedSlug.trim(); const n = t.startsWith("@") || t.startsWith("skills-sh:") ? parseRequestedClawHubSkillRef(t) : { slug: normalizeTrackedSkillSlug(t) }; const r = n.slug; const i = await readClawHubSkillOrigin(resolveWorkspaceSkillInstallDir(e.workspaceDir, r)); const a = e.lock.skills[r]; if (!i && !a) return validateRequestedSkillSlug(n.slug); const o = describeClawHubSkillRefMismatch(n, { ownerHandle: i?.ownerHandle ?? a?.ownerHandle, requestedReference: i?.requestedReference ?? a?.requestedReference }); if (o) throw new Error(o); return r; } async function resolveTrackedUpdateTarget(e) { const t = await readClawHubSkillOrigin(resolveWorkspaceSkillInstallDir(e.workspaceDir, e.slug)); const n = e.lock.skills[e.slug]; if (!t && !n) return { ok: false, slug: e.slug, error: `Skill "${e.slug}" is not tracked as a ClawHub install.` }; const r = t?.ownerHandle ?? n?.ownerHandle; const i = t?.requestedReference ?? n?.requestedReference; const a = t?.trustState ?? n?.trustState; return { ok: true, slug: e.slug, ...r ? { ownerHandle: r } : {}, ...i ? { requestedReference: i } : {}, ...a ? { trustState: a } : {}, baseUrl: t?.registry ?? e.baseUrl, previousVersion: t?.installedVersion ?? n?.version ?? null }; } //#endregion export { resolveClawHubSkillVerificationTarget as a, searchSkillsFromClawHub as c, resolveClawHubSkillStatusLinkSync as i, readLocalSkillCardContentSync as n, resolveRequestedUpdateSlug as o, readLocalSkillCardSync as r, resolveTrackedUpdateTarget as s, preflightSkillOwnerState as t };