#!/usr/bin/env node /** * Writes `src/plugins/bundle.generated.ts` from the first-party plugin folders in the * repository's `plugins/`: each plugin's description, tools, requirements, instruction sections, * skills and the SHA-256 of its files, so every consumer (the compiler, the projection, the * harness, the Runtime) reads the same bytes, offline. `--check` fails when the file is stale. */ import { createHash } from "node:crypto"; import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; import { dirname, join, relative } from "node:path"; import { fileURLToPath } from "node:url"; const core = join(dirname(fileURLToPath(import.meta.url)), ".."); const root = join(core, "..", "plugins"); const out = join(core, "src", "plugins", "bundle.generated.ts"); const ORDER = ["nylorun-memory", "nylorun-triggers", "nylorun-plan", "nylorun-interactions", "nylorun-artifacts", "nylorun-sandbox"]; const walk = (dir) => readdirSync(dir) .sort() .flatMap((name) => { const path = join(dir, name); return statSync(path).isDirectory() ? walk(path) : [path]; }); function frontmatter(text) { const match = /^---\n([\s\S]*?)\n---\n([\s\S]*)$/.exec(text); if (!match) throw new Error("A SKILL.md opens with frontmatter"); const fields = Object.fromEntries( match[1].split("\n").map((line) => { const at = line.indexOf(":"); return [line.slice(0, at).trim(), line.slice(at + 1).trim()]; }), ); return { name: fields.name, description: fields.description, body: match[2].trim() }; } const plugins = {}; for (const name of ORDER) { const dir = join(root, name); const files = walk(dir); const hash = createHash("sha256"); for (const file of files) hash.update(`${relative(dir, file)}\n`).update(readFileSync(file)).update("\n"); const manifest = JSON.parse(readFileSync(join(dir, "plugin.json"), "utf8")); const tools = JSON.parse(readFileSync(join(dir, "tools.json"), "utf8")).tools; const instructions = {}; for (const file of files.filter((file) => relative(dir, file).startsWith("instructions/"))) instructions[relative(join(dir, "instructions"), file).replace(/\.md$/, "")] = readFileSync(file, "utf8").trim(); const skills = {}; for (const file of files.filter((file) => /^skills\/[^/]+\/SKILL\.md$/.test(relative(dir, file)))) { const skill = frontmatter(readFileSync(file, "utf8")); skills[skill.name] = { ...skill, text: readFileSync(file, "utf8") }; } plugins[name] = { description: manifest.description, tools, requires: manifest.extensions.nylorun.requires, instructions, skills, sha256: `sha256:${hash.digest("hex")}`, }; } const body = `// Generated by core/scripts/build-plugins.mjs from plugins/: do not edit. /* eslint-disable */ export const PLUGIN_BUNDLE = ${JSON.stringify(plugins, null, 2)} as const; `; if (process.argv.includes("--check")) { let current = ""; try { current = readFileSync(out, "utf8"); } catch {} if (current !== body) { console.error("core/src/plugins/bundle.generated.ts is stale: run node core/scripts/build-plugins.mjs"); process.exit(1); } } else writeFileSync(out, body);