openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
233 lines (232 loc) • 8.63 kB
JavaScript
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { d as pathExists } from "./fs-safe-BbWvmdVn.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { i as getAgentWorkspaceAccess, t as WorkspaceAccessUnavailableError } from "./workspace-access-CnjZBg1i.mjs";
import { a as resolvePackageDirInstallTransaction, i as requestDeferredPackageDirInstall, r as installPackageDir } from "./install-package-dir-_Rv9x8R0.mjs";
import { t as evaluateSkillInstallPolicy } from "./install-security-scan-BjcurHAj.mjs";
import { n as resolveWorkspaceSkillInstallDir } from "./install-paths-DuXfcnHt.mjs";
import { t as checkClawHubSkillPlanAtPath } from "./skill-tree-digest-CO1NQnGe.mjs";
import { i as snapshotCommittedSkillArtifactBestEffort, n as hasCommittedSkillChangeHooks, r as resolveCommittedSkillChangeSource, t as dispatchCommittedSkillChangeBestEffort } from "./skill-change-hook-Cfaf7WZN.mjs";
import { n as withExtractedArchiveRoot } from "./install-flow-C68L8OqQ.mjs";
import path from "node:path";
//#region src/skills/lifecycle/archive-install.ts
const DEFAULT_SKILL_ARCHIVE_ROOT_MARKERS = ["SKILL.md"];
const log = createSubsystemLogger("skills");
const CLAWHUB_SKILL_ARCHIVE_ROOT_MARKERS = [
"SKILL.md",
"skill.md",
"skills.md",
"SKILL.MD"
];
function installFailure(e, t) {
return {
ok: false,
error: e,
failureKind: t
};
}
async function hasSkillArchiveRoot(e, t) {
for (const n of t) if (await pathExists(path.join(e, n))) return true;
return false;
}
const TRANSIENT_ARCHIVE_ERROR_PATTERNS = [
"enoent",
"enospc",
"eio",
"eacces",
"eperm",
"ebusy",
"emfile",
"enfile",
"timeout",
"timed out"
];
function archiveFailureKind(e) {
const t = e.toLowerCase();
return t.startsWith("failed to install skill:") || TRANSIENT_ARCHIVE_ERROR_PATTERNS.some((e) => t.includes(e)) ? "unavailable" : "invalid-request";
}
async function installExtractedSkillRoot(e) {
try {
const t = resolveCommittedSkillChangeSource(e.policy?.origin.type);
const n = e.policy?.origin.version ?? e.policy?.origin.commit;
const r = typeof n === "string" || typeof n === "number" ? String(n) : void 0;
const i = hasCommittedSkillChangeHooks();
const a = getAgentWorkspaceAccess(e.workspaceDir, "loadSkills");
const o = a?.loadSkills ? a : void 0;
if (o && !o.applySkillRoot) throw new WorkspaceAccessUnavailableError("Remote workspace skill installation is unavailable");
if (e.deferCommit && o) throw new WorkspaceAccessUnavailableError("Transactional ClawHub skill replacement is unavailable for a remote workspace");
const s = o?.applySkillRoot ?? applyExtractedSkillRoot;
const { policy: c, beforePersistentEffect: l, ...u } = e;
e.beforePersistentApply?.();
const d = await s({
...u,
...l ? { authorizeMutation: async () => {
await l();
} } : {},
...i ? { changes: {
source: t,
sourceVersion: r
} } : {},
beforeInstall: async (t) => {
if (!e.policy) {
await e.beforePersistentEffect?.();
return;
}
const n = await evaluateSkillInstallPolicy({
config: e.policy.config,
onInstallPolicyWarning: e.policy.onInstallPolicyWarning,
installId: e.policy.installId ?? "archive",
logger: e.logger ?? {},
origin: e.policy.origin,
requestedSpecifier: e.policy.requestedSpecifier,
source: e.policy.source,
mode: t,
skillName: e.slug,
sourceDir: e.extractedRoot
});
await e.beforePersistentEffect?.();
e.beforePersistentApply?.();
return n?.blocked ? {
error: n.blocked.reason,
failureKind: n.blocked.code === "security_scan_failed" ? "unavailable" : "invalid-request"
} : void 0;
}
});
if (!d.ok) return d;
const publishChange = async () => {
try {
const { refreshSkillsDiscovery: t } = await import("./refresh-CwpO2622.mjs");
await t({
workspaceDir: e.workspaceDir,
config: e.policy?.config
});
} catch (e) {
log.warn(`Skill installed, but discovery refresh failed: ${formatErrorMessage(e)}`);
}
if (i) await dispatchCommittedSkillChangeBestEffort({
action: d.mode === "update" ? "updated" : "created",
source: t,
workspaceDir: e.workspaceDir,
before: d.before,
after: d.after,
logger: e.logger
});
};
const f = d.transaction;
if (!f) await publishChange();
return {
ok: true,
targetDir: d.targetDir,
...f ? { transaction: {
commit: async (e) => {
await f.commit(e);
await publishChange();
},
rollback: (e) => f.rollback(e)
} } : {}
};
} catch (e) {
return installFailure(formatErrorMessage(e), "unavailable");
}
}
/** Native file replacement on the workspace host; policy and hook dispatch stay with the caller. */
async function applyExtractedSkillRoot(e) {
try {
if (e.deferCommit && !e.assertOwned) return installFailure("Deferred skill replacement requires a live owner guard.", "invalid-request");
if (!await hasSkillArchiveRoot(e.extractedRoot, e.rootMarkers ?? DEFAULT_SKILL_ARCHIVE_ROOT_MARKERS)) return installFailure("archive is missing SKILL.md", "invalid-request");
let t;
try {
t = resolveWorkspaceSkillInstallDir(e.workspaceDir, e.slug);
} catch (e) {
return installFailure(formatErrorMessage(e), "invalid-request");
}
const n = await pathExists(t);
if (e.deferCommit && e.expectedClawHubState && !n) return installFailure("Guarded skill update target disappeared.", "invalid-request");
const r = e.mode === "update" && n ? "update" : "install";
if (e.mode === "install" && n) return installFailure(`Skill already exists at ${t}. Re-run with force/update.`, "invalid-request");
const snapshot = (n, r = false) => snapshotCommittedSkillArtifactBestEffort({
skillDir: t,
skillKey: e.slug,
source: n.source,
...r ? { sourceVersion: n.sourceVersion } : {},
logger: e.logger
});
const i = e.changes && r === "update" ? await snapshot(e.changes) : void 0;
const a = await e.beforeInstall?.(r);
if (a) return installFailure(a.error, a.failureKind);
const o = e.expectedClawHubState;
let s;
const c = {
sourceDir: e.extractedRoot,
targetDir: t,
mode: r,
timeoutMs: e.timeoutMs ?? 12e4,
logger: e.logger,
copyErrorPrefix: "failed to install skill",
beforePersistentApply: e.beforePersistentApply,
authorizeMutation: e.authorizeMutation,
requireExistingTarget: Boolean(e.deferCommit && o),
hasDeps: false,
depsLogMessage: "",
...o !== void 0 ? { afterBackup: async (t) => {
const n = o ? await checkClawHubSkillPlanAtPath(o, t) : {
ok: false,
error: `Skill ${JSON.stringify(e.slug)} appeared during update.`
};
s = n.ok ? void 0 : `${n.error} Updating replaces the installed skill directory.`;
return s ? {
ok: false,
error: s
} : { ok: true };
} } : {}
};
const l = await installPackageDir(e.deferCommit ? requestDeferredPackageDirInstall(c, e.assertOwned) : c);
if (!l.ok) return {
...installFailure(l.error, s ? "invalid-request" : "unavailable"),
...s ? { replacementBlocked: s } : {},
...l.recoveryIncomplete ? { recoveryIncomplete: true } : {}
};
const u = resolvePackageDirInstallTransaction(l);
if (e.deferCommit && !u) return {
...installFailure("Transactional skill replacement did not return a receipt.", "unavailable"),
recoveryIncomplete: true
};
const d = e.changes ? await snapshot(e.changes, true) : void 0;
return {
ok: true,
targetDir: t,
mode: r,
before: i,
after: d,
...u ? { transaction: u } : {}
};
} catch (e) {
return installFailure(formatErrorMessage(e), "unavailable");
}
}
async function installSkillArchiveFromPath(e) {
const t = await withExtractedArchiveRoot({
archivePath: e.archivePath,
tempDirPrefix: "openclaw-skill-archive-",
timeoutMs: e.timeoutMs ?? 12e4,
logger: e.logger,
rootMarkers: ["SKILL.md"],
onExtracted: async (t) => await installExtractedSkillRoot({
workspaceDir: e.workspaceDir,
slug: e.slug,
extractedRoot: t,
mode: e.force ? "update" : "install",
timeoutMs: e.timeoutMs,
logger: e.logger,
policy: e.policy,
beforePersistentApply: e.beforePersistentApply
})
});
if (!t.ok) {
const e = t.error.includes("unexpected archive layout") ? "archive is missing SKILL.md" : t.error;
return installFailure(e, "failureKind" in t && (t.failureKind === "invalid-request" || t.failureKind === "unavailable") ? t.failureKind : archiveFailureKind(e));
}
return t;
}
//#endregion
export { installSkillArchiveFromPath as i, applyExtractedSkillRoot as n, installExtractedSkillRoot as r, CLAWHUB_SKILL_ARCHIVE_ROOT_MARKERS as t };