import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { EMBEDDED_SKILL_ASSET_DATA, EMBEDDED_SKILL_TEXT_FILES } from "./embedded-skill.generated.js"; export { EMBEDDED_SKILL_ALL_PATHS } from "./embedded-skill.generated.js"; export const EMBEDDED_SKILL_FILES = EMBEDDED_SKILL_TEXT_FILES; export const EMBEDDED_SKILL_ASSETS: Record = Object.fromEntries( Object.entries(EMBEDDED_SKILL_ASSET_DATA).map(([relPath, base64]) => [relPath, Buffer.from(base64, "base64")]) ); export function getSkillContent(): string { return EMBEDDED_SKILL_FILES["SKILL.md"] || ""; } /** * Reads the `version:` field out of a SKILL.md front matter block. * * The skill is versioned separately from the code that reads it, and that separation is exactly * how `seal-use update` shipped a stale skill across several releases: writing the file reported * success either way, so the version is the only signal that says *which generation* of the * document actually landed on disk. */ export function parseSkillVersion(markdown: string): string | undefined { return markdown.match(/^\s*version:\s*(\S+)\s*$/m)?.[1]; } /** Reads the version of an installed SKILL.md, or `undefined` when it is missing or unreadable. */ export function readInstalledSkillVersion( skillFile: string = join(getDefaultGlobalSkillDir(), "SKILL.md") ): string | undefined { try { return parseSkillVersion(readFileSync(skillFile, "utf8")); } catch { return undefined; } } /** The version of the skill compiled into this build, used to assert what an install wrote. */ export const EMBEDDED_SKILL_VERSION = parseSkillVersion(getSkillContent()); export function getDefaultGlobalSkillDir(): string { return join(homedir(), ".agents", "skills", "seal-use"); } function getLegacyGlobalSkillDir(): string { return join(homedir(), ".agents", "skills", "seal-home"); } function cleanLegacySkillDir(): boolean { const legacyDir = getLegacyGlobalSkillDir(); if (existsSync(legacyDir)) { try { rmSync(legacyDir, { recursive: true, force: true }); return true; } catch { return false; } } return false; } export function installSkillFiles(targetDir: string = getDefaultGlobalSkillDir()): { targetDir: string; files: string[] } { cleanLegacySkillDir(); mkdirSync(targetDir, { recursive: true }); const installed: string[] = []; for (const [relPath, content] of Object.entries(EMBEDDED_SKILL_FILES)) { const fullPath = join(targetDir, relPath); mkdirSync(dirname(fullPath), { recursive: true }); writeFileSync(fullPath, content, "utf8"); installed.push(relPath); } for (const [relPath, content] of Object.entries(EMBEDDED_SKILL_ASSETS)) { const fullPath = join(targetDir, relPath); mkdirSync(dirname(fullPath), { recursive: true }); writeFileSync(fullPath, content); installed.push(relPath); } return { targetDir, files: installed }; } export function syncGlobalSkillIfPresent(): { synced: boolean; targetDir?: string } { const legacyDir = getLegacyGlobalSkillDir(); const globalDir = getDefaultGlobalSkillDir(); const hadLegacy = existsSync(legacyDir); const hadCurrent = existsSync(globalDir); if (hadLegacy || hadCurrent) { cleanLegacySkillDir(); installSkillFiles(globalDir); return { synced: true, targetDir: globalDir }; } return { synced: false }; }