UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

135 lines (134 loc) • 6.57 kB
import { n as ignoreMissingPath } from "./errno-DFcoqIT9.mjs"; import { d as pathExists, w as root } from "./fs-safe-BbWvmdVn.mjs"; import { t as parseSkillFrontmatter } from "./frontmatter-CetkUJdW.mjs"; import { n as resolveWorkshopSkillsDir, t as resolveWorkshopDraftsDir } from "./skills-root-DFNOcDAR.mjs"; import { r as removePathWithinRoot } from "./fs-safe-remove-mw1k_RwN.mjs"; import { t as resolveSkillWorkshopConfig } from "./config-DO1phBEt.mjs"; import { n as assertInsideSkillsRoot, t as MAX_WORKSPACE_SKILL_SUPPORT_FILE_BYTES } from "./workspace-skill-write-DINtvFgf.mjs"; import { d as normalizeSkillFilePath, h as readSkillFile, i as WorkshopWriteError, m as publishSkillOverlay, n as NEW_SKILL_NAME_PATTERN, r as SKILL_FILE, s as listSkillFiles, t as EXISTING_SKILL_NAME_PATTERN, u as mutateSkill, v as validateSkillFile } from "./library-D3QR-mF3.mjs"; import path from "node:path"; import { randomUUID } from "node:crypto"; import fs from "node:fs/promises"; //#region src/skills/workshop/drafts.ts const REASON_FILE = ".reason"; const DRAFT_ID_PATTERN = /^[a-z0-9][a-z0-9-]{5,120}$/u; const MAX_DRAFTS = 50; function resolveDraftDir(e, t, n) { if (!DRAFT_ID_PATTERN.test(n)) throw new WorkshopWriteError(`Invalid draft id "${n}". Call action=list to see drafts.`); return path.join(resolveWorkshopDraftsDir(e, t), n); } async function readDraft(e, t) { const n = path.join(e, t); const r = await readSkillFile(n, SKILL_FILE).catch(() => null); if (r === null) return; let i = {}; try { i = parseSkillFrontmatter(r); } catch {} const a = (await fs.readFile(path.join(n, REASON_FILE), "utf8").catch(() => "")).trim(); const { files: o, updatedAtMs: s } = await listSkillFiles(n); return { id: t, skillName: i.name?.trim() || t, description: i.description?.trim() ?? "", ...a ? { reason: a } : {}, createdAtMs: s, files: o }; } /** Drafts waiting for review, oldest first. */ async function listWorkshopDrafts(e, t) { const n = resolveWorkshopDraftsDir(e, t); const r = await fs.readdir(n, { withFileTypes: true }).catch((e) => ignoreMissingPath(e) ?? []); const i = []; for (const e of r) if (e.isDirectory() && DRAFT_ID_PATTERN.test(e.name)) { const t = await readDraft(n, e.name); if (t) i.push(t); } return i.toSorted((e, t) => e.createdAtMs - t.createdAtMs || e.id.localeCompare(t.id)); } /** Saves a proposed SKILL.md for later review and returns its id; live skills stay unchanged. */ async function saveWorkshopDraft(e, t, n) { if (!EXISTING_SKILL_NAME_PATTERN.test(n.name)) throw new WorkshopWriteError(`Invalid skill name "${n.name}": use lowercase letters, digits, or hyphens (e.g. "deploy-staging").`); validateSkillFile(e, n.name, SKILL_FILE, n.content); if ((await listWorkshopDrafts(e, t)).length >= MAX_DRAFTS) throw new WorkshopWriteError(`${MAX_DRAFTS} drafts are already waiting for review. Save nothing; the user reviews drafts with /learn drafts.`); const r = resolveWorkshopDraftsDir(e, t); const i = `${n.name.slice(0, 60).replace(/-+$/u, "")}-${randomUUID().slice(0, 8)}`; await fs.mkdir(r, { recursive: true }); assertInsideSkillsRoot(resolveWorkshopSkillsDir(e, t), r, "drafts directory"); const a = await root(r); const write = async (e, t) => await a.write(`${i}/${e}`, t, { encoding: "utf8", mkdir: true, overwrite: false, ...n.assertLive ? { assertBeforeMutation: n.assertLive } : {} }); const o = n.reason?.trim(); if (o) await write(REASON_FILE, `${o}\n`); await write(SKILL_FILE, n.content); return i; } async function viewWorkshopDraft(e, t, n, r) { const i = resolveDraftDir(e, t, n); const a = await readDraft(path.dirname(i), n); if (!a) throw new WorkshopWriteError(`No draft "${n}". Call action=list to see drafts.`); const o = normalizeSkillFilePath(r); const s = await readSkillFile(i, o); if (s === null) throw new WorkshopWriteError(`${o} does not exist in draft "${n}". Files: ${a.files.join(", ")}.`); return { draft: a, filePath: o, content: s }; } /** Deletes a reviewed draft, whether its lesson was saved or rejected. */ async function discardWorkshopDraft(e, t, n, r) { const i = resolveDraftDir(e, t, n); if (!await pathExists(i)) throw new WorkshopWriteError(`No draft "${n}". Call action=list to see drafts.`); await removePathWithinRoot({ rootDir: path.dirname(i), relativePath: n, recursive: true, force: false, ...r ? { assertBeforeMutation: r } : {} }); } /** * Makes a draft the live skill in one versioned change, then deletes the draft. Every file is * validated first and the whole next skill is published by one staged swap, so a refused file or * failed write leaves the skill untouched, and one undo restores it as it was before the draft. */ async function applyWorkshopDraft(e, t, n) { const r = resolveDraftDir(e.config, e.agentId, t); const i = await readDraft(path.dirname(r), t); if (!i) throw new WorkshopWriteError(`No draft "${t}".`); if (n !== void 0 && n !== i.skillName) throw new WorkshopWriteError(`Draft "${t}" is for skill "${i.skillName}", not "${n}". Reload the drafts.`); const a = i.skillName; const o = path.join(resolveWorkshopSkillsDir(e.config, e.agentId), a); const s = EXISTING_SKILL_NAME_PATTERN.test(a) && await pathExists(path.join(o, "SKILL.md")); if (!(s ? EXISTING_SKILL_NAME_PATTERN : NEW_SKILL_NAME_PATTERN).test(a)) throw new WorkshopWriteError(`Draft "${t}" names no valid skill; its SKILL.md frontmatter needs a name like "deploy-staging".`); const c = await root(r); const l = []; for (const t of i.files) { const n = (await c.read(t, { hardlinks: "reject", symlinks: "reject", maxBytes: t === "SKILL.md" ? resolveSkillWorkshopConfig(e.config).maxSkillBytes : MAX_WORKSPACE_SKILL_SUPPORT_FILE_BYTES })).buffer; validateSkillFile(e.config, a, t, n.toString("utf8")); if (!(await fs.readFile(path.join(o, t)).catch(() => null))?.equals(n)) l.push([t, n]); } const u = i.reason ?? "applied a draft"; const d = l.length === 0 ? [] : [await mutateSkill(e, a, s ? "write_file" : "create", async (e) => { if (await pathExists(s ? path.join(e.skillDir, "SKILL.md") : e.skillDir) !== s) throw new WorkshopWriteError(`Skill "${a}" changed while the draft was being applied; open the draft again.`); const t = await publishSkillOverlay(e, l); return { summary: u, ...t ? { versionId: t } : {} }; })]; await discardWorkshopDraft(e.config, e.agentId, t, e.assertLive); return d; } //#endregion export { viewWorkshopDraft as a, saveWorkshopDraft as i, discardWorkshopDraft as n, listWorkshopDrafts as r, applyWorkshopDraft as t };