UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

256 lines (255 loc) • 7.86 kB
import { r as isErrno } from "./errno-DFcoqIT9.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { n as sha256Hex } from "./node-crypto-B6yuTjmm.mjs"; import "./crypto-digest-B2m8wIBj.mjs"; import { n as resolveWorkspaceSkillInstallDir } from "./install-paths-DuXfcnHt.mjs"; import { n as digestClawHubSkillTree, t as checkClawHubSkillPlanAtPath } from "./skill-tree-digest-CO1NQnGe.mjs"; import { b as untrackClawHubSkill, i as formatClawHubSkillRef, l as parseRequestedClawHubSkillRef, r as describeClawHubSkillRefMismatch, y as resolveWorkspaceClawHubSkills } from "./clawhub-store-BhwTU-Sz.mjs"; import { i as resolveClawHubSkillStatusLinkSync } from "./clawhub-status-ctFKB1dD.mjs"; import { i as snapshotCommittedSkillArtifactBestEffort, n as hasCommittedSkillChangeHooks, t as dispatchCommittedSkillChangeBestEffort } from "./skill-change-hook-Cfaf7WZN.mjs"; import { lstatSync } from "node:fs"; import path from "node:path"; import { randomUUID } from "node:crypto"; import fs$1 from "node:fs/promises"; //#region src/skills/lifecycle/clawhub-uninstall.ts const log = createSubsystemLogger("skills"); async function refreshRemovedSkill(e) { try { const { refreshSkillsDiscovery: t } = await import("./refresh-CwpO2622.mjs"); await t({ workspaceDir: e }); } catch (e) { log.warn(`Skill removed, but discovery refresh failed: ${formatErrorMessage(e)}`); } } async function planClawHubSkillUninstall(e) { const t = resolveWorkspaceClawHubSkills(e.workspaceDir); if (t) return await t.planClawHubSkillUninstall(e); let n; try { n = parseRequestedClawHubSkillRef(e.slug); } catch (e) { return { ok: false, code: "ambiguous", error: String(e) }; } return await planTrackedClawHubSkillState({ workspaceDir: e.workspaceDir, requestedRef: n, expectedVersion: e.expectedVersion }); } async function planTrackedClawHubSkillState(e) { const t = e.requestedRef; const n = t.slug; const r = resolveWorkspaceSkillInstallDir(e.workspaceDir, n); const i = resolveClawHubSkillStatusLinkSync({ workspaceDir: e.workspaceDir, skillDir: r, skillKey: n }); if (!i) return { ok: false, code: "missing", error: `Skill ${JSON.stringify(n)} is not a tracked ClawHub install.` }; if (!i.valid || !i.skillFile || !i.fileTreeSha256) return { ok: false, code: "ambiguous", error: i.valid ? `Skill ${JSON.stringify(n)} was installed before OpenClaw recorded file fingerprints, so local changes cannot be detected.` : i.reason }; const a = describeClawHubSkillRefMismatch(t, i); if (a) return { ok: false, code: "ambiguous", error: a }; if (i.installedVersion !== e.expectedVersion) return { ok: false, code: "modified", error: `Skill ${JSON.stringify(n)} is at ${i.installedVersion}, expected ${e.expectedVersion}.` }; const o = path.join(r, i.skillFile.path); let s; try { const e = await fs$1.lstat(r); if (!e.isDirectory() || e.isSymbolicLink()) return { ok: false, code: "ambiguous", error: `Skill ${JSON.stringify(n)} is not a regular managed directory.` }; s = await fs$1.readFile(o); } catch (e) { return { ok: false, code: "missing", error: String(e) }; } if (sha256Hex(s) !== i.skillFile.sha256) return { ok: false, code: "modified", error: `Skill ${JSON.stringify(n)} has local SKILL.md changes.` }; let c; try { c = await digestClawHubSkillTree(r); } catch (e) { return { ok: false, code: "ambiguous", error: String(e) }; } if (c !== i.fileTreeSha256) return { ok: false, code: "modified", error: `Skill ${JSON.stringify(n)} has local file changes.` }; return { ok: true, plan: { workspaceDir: e.workspaceDir, requestedRef: t.requestedReference ?? formatClawHubSkillRef(t), slug: n, version: i.installedVersion, installedAt: i.installedAt, targetDir: r, skillFilePath: i.skillFile.path, skillFileSha256: i.skillFile.sha256, fileTreeSha256: c } }; } async function applyClawHubSkillUninstall(e, t = {}) { const n = resolveWorkspaceClawHubSkills(e.workspaceDir); if (n) { const r = await n.applyClawHubSkillUninstall(e, { beforePersistentApply: t.beforePersistentApply, beforeRollback: t.beforeRollback, onCommittedChange: t.onCommittedChange ?? (hasCommittedSkillChangeHooks() ? dispatchCommittedSkillChangeBestEffort : void 0) }); if (r.ok) await refreshRemovedSkill(e.workspaceDir); return r; } const r = await planClawHubSkillUninstall({ workspaceDir: e.workspaceDir, slug: e.requestedRef, expectedVersion: e.version }); if (!r.ok) return { ok: false, error: r.error }; const i = Boolean(t.onCommittedChange) || hasCommittedSkillChangeHooks(); const a = i ? await snapshotCommittedSkillArtifactBestEffort({ skillDir: e.targetDir, skillKey: e.slug, source: "clawhub", sourceVersion: e.version }) : void 0; const o = `${e.targetDir}.openclaw-skill-remove-${randomUUID()}`; let s = false; let c = false; let l; const u = await fs$1.lstat(e.targetDir, { bigint: true }); const assertRollbackCurrent = () => { const n = lstatSync(o, { bigint: true }); if (u.dev === 0n || u.ino === 0n || n.dev !== u.dev || n.ino !== u.ino || lstatSync(e.targetDir, { throwIfNoEntry: false }) !== void 0) throw new Error(`Skill ${JSON.stringify(e.slug)} staging or destination changed during rollback.`); t.beforeRollback?.(); }; const restoreStaged = async () => { if (t.authorizeMutation) await t.authorizeMutation("rollback"); assertRollbackCurrent(); await fs$1.rename(o, e.targetDir); s = false; }; try { if (t.authorizeMutation) await t.authorizeMutation("apply"); t.beforePersistentApply?.(); await fs$1.rename(e.targetDir, o); s = true; if (!(await checkClawHubSkillPlanAtPath(e, o)).ok) { await restoreStaged(); return { ok: false, error: `Skill ${JSON.stringify(e.slug)} changed during removal.` }; } if (t.authorizeMutation) await t.authorizeMutation("apply"); t.beforePersistentApply?.(); l = await untrackClawHubSkill(e.workspaceDir, e.slug, t.beforePersistentApply, assertRollbackCurrent, t.authorizeMutation); if (t.authorizeMutation) await t.authorizeMutation("apply"); t.beforePersistentApply?.(); await fs$1.rm(o, { recursive: true, force: false }); c = true; await refreshRemovedSkill(e.workspaceDir); if (i) { if (t.authorizeMutation) await t.authorizeMutation("apply"); t.beforePersistentApply?.(); try { await (t.onCommittedChange ?? dispatchCommittedSkillChangeBestEffort)({ action: "removed", source: "clawhub", workspaceDir: e.workspaceDir, before: a }); } catch {} } return { ok: true }; } catch (e) { const t = []; if (!c) { if (l) try { assertRollbackCurrent(); await l(); } catch (e) { t.push(`could not restore lockfile: ${String(e)}`); } if (s) try { await restoreStaged(); } catch (e) { t.push(`could not restore skill directory from ${o}: ${String(e)}`); } } return { ok: false, error: `${String(e)}${t.length > 0 ? `; rollback incomplete: ${t.join("; ")}` : ""}` }; } } async function guardTrackedSkillLocalState(e) { const t = resolveWorkspaceSkillInstallDir(e.workspaceDir, e.slug); try { await fs$1.lstat(t); } catch (e) { if (isErrno(e) && e.code === "ENOENT") return { ok: true, plan: void 0 }; return { ok: false, error: String(e) }; } const n = await planTrackedClawHubSkillState({ workspaceDir: e.workspaceDir, requestedRef: { slug: e.slug }, expectedVersion: e.previousVersion ?? "" }); if (n.ok) return { ok: true, plan: n.plan }; return { ok: false, error: n.error }; } //#endregion export { guardTrackedSkillLocalState as n, planClawHubSkillUninstall as r, applyClawHubSkillUninstall as t };