117 lines
3.8 KiB
TypeScript
117 lines
3.8 KiB
TypeScript
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { homedir } from "node:os";
|
|
import { dirname, join } from "node:path";
|
|
|
|
import {
|
|
EMBEDDED_SKILL_ASSET_PATHS,
|
|
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;
|
|
// Runtime self-healing imports this module even for `version`. Read asset bytes
|
|
// only when installation actually asks for them, preserving local startup latency.
|
|
export const EMBEDDED_SKILL_ASSETS: Record<string, Uint8Array> = {};
|
|
for (const [relPath, path] of Object.entries(EMBEDDED_SKILL_ASSET_PATHS)) {
|
|
let bytes: Uint8Array | undefined;
|
|
Object.defineProperty(EMBEDDED_SKILL_ASSETS, relPath, {
|
|
enumerable: true,
|
|
get: () => bytes ??= readFileSync(path)
|
|
});
|
|
}
|
|
|
|
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 };
|
|
}
|
|
|
|
// Re-export agent host skill bridging capabilities
|
|
export * from "./agent-hosts/index.js";
|