Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
5.5 KB
Raw Blame
#!/usr/bin/env bun
/**
 * Generate src/core/skills/bundled-skills.generated.ts: gbrain's bundled
 * skills (the plugin lane, skills/plugin-lanes.json) embedded as text imports,
 * so the spring-loaded skill index and `gbrain bundled-skill` work from a
 * compiled binary with no skills/ directory beside it.
 *
 *   bun run scripts/generate-bundled-skills.ts           write
 *   bun run scripts/generate-bundled-skills.ts --check   exit 1 when stale
 *
 * Per skill: name, the audience (`owner`: the plugin lane, for agents on the brain host;
 * `downstream`: also in openclaw.plugin.json#skills, whose curation keeps
 * host-only workflows out), and its assets: files in its own folder plus the
 * shared files (openclaw.plugin.json#shared_deps) they name, transitively. A
 * shared file is named by its path under skills/ (`conventions/quality.md`,
 * `../conventions/quality.md`) or, when no other shared file has the same
 * name, by its file name (`_brain-filing-rules.json`). Description and
 * triggers are read from the embedded SKILL.md at runtime (bundled.ts), so
 * editing a skill's text or frontmatter never changes this module; only
 * adding, removing or re-auditing a skill or one of its files does.
 */
import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';
import { basename, join, resolve } from 'node:path';
import { loadBundleManifest, pathSlug } from '../src/core/skillpack/bundle.ts';
import { computeLaneSet } from '../src/core/skillpack/personas.ts';

const ROOT = resolve(import.meta.dir, '..');
const OUT = join(ROOT, 'src/core/skills/bundled-skills.generated.ts');
const SKILLS = join(ROOT, 'skills');
const SKIP_OWN = new Set(['SKILL.md', 'routing-eval.jsonl']);

function walk(absDir: string, rel: string, out: string[]): void {
  for (const e of readdirSync(absDir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
    if (e.name.startsWith('.')) continue;
    if (e.isDirectory()) walk(join(absDir, e.name), `${rel}/${e.name}`, out);
    else if (e.isFile()) out.push(`${rel}/${e.name}`);
  }
}

/** The shared files the texts name, then the shared files those name, until nothing new is reached. */
function sharedClosure(texts: string[], shared: string[]): string[] {
  const names = new Map<string, number>();
  for (const p of shared) names.set(basename(p), (names.get(basename(p)) ?? 0) + 1);
  const named = (text: string, p: string) => text.includes(p) || (names.get(basename(p)) === 1 && text.includes(basename(p)));
  const reached = new Set<string>();
  const queue = [...texts];
  while (queue.length) {
    const text = queue.shift()!;
    for (const p of shared) {
      if (reached.has(p) || !named(text, p)) continue;
      reached.add(p);
      queue.push(readFileSync(join(SKILLS, p), 'utf8'));
    }
  }
  return [...reached];
}

export function renderBundledSkillsModule(): string {
  const manifest = loadBundleManifest(ROOT);
  const owner = [...computeLaneSet(ROOT)].sort();
  const lanes = JSON.parse(readFileSync(join(SKILLS, 'plugin-lanes.json'), 'utf8')) as { base_exclusions?: Record<string, string> };
  const downstream = new Set(manifest.skills.map(pathSlug).filter(s => !(s in (lanes.base_exclusions ?? {}))));
  const shared: string[] = [];
  for (const dep of manifest.shared_deps) {
    const abs = join(ROOT, dep);
    if (!existsSync(abs)) continue;
    const rel = dep.replace(/^skills\//, '');
    if (statSync(abs).isDirectory()) walk(abs, rel, shared); else shared.push(rel);
  }
  const files = new Set<string>();
  const skills = owner.filter(name => existsSync(join(SKILLS, name, 'SKILL.md'))).map(name => {
    const text = readFileSync(join(SKILLS, name, 'SKILL.md'), 'utf8');
    const own: string[] = [];
    walk(join(SKILLS, name), name, own);
    const ownAssets = own.filter(p => !SKIP_OWN.has(basename(p)));
    const assets = [...ownAssets, ...sharedClosure([text, ...ownAssets.map(p => readFileSync(join(SKILLS, p), 'utf8'))], shared)].sort();
    files.add(`${name}/SKILL.md`);
    for (const a of assets) files.add(a);
    return {
      name,
      audience: downstream.has(name) ? 'downstream' : 'owner',
      assets,
    };
  });
  const sorted = [...files].sort();
  const lines = [
    '// GENERATED by scripts/generate-bundled-skills.ts from skills/ — do not edit. Regenerate: bun run regen:all',
    '/* eslint-disable */',
    ...sorted.flatMap((f, i) => (f.endsWith('.json') ? [] : [`import f${i} from '../../../skills/${f}' with { type: 'text' };`])),
    '',
    'export interface BundledSkillMeta { name: string; audience: \'owner\' | \'downstream\'; assets: string[] }',
    '',
    `export const BUNDLED_SKILLS: readonly BundledSkillMeta[] = ${JSON.stringify(skills, null, 2)};`,
    '',
    'export const BUNDLED_SKILL_FILES: Readonly<Record<string, string>> = {',
    // A compiled binary hands a .json text import back parsed, so JSON files are inlined as string literals.
    ...sorted.map((f, i) => `  ${JSON.stringify(f)}: ${f.endsWith('.json') ? JSON.stringify(readFileSync(join(SKILLS, f), 'utf8')) : `f${i}`},`),
    '};',
    '',
  ];
  return lines.join('\n');
}

if (import.meta.main) {
  const next = renderBundledSkillsModule();
  const current = existsSync(OUT) ? readFileSync(OUT, 'utf8') : '';
  if (process.argv.includes('--check')) {
    if (current !== next) {
      console.error('src/core/skills/bundled-skills.generated.ts is stale: run bun run regen:all');
      process.exit(1);
    }
  } else if (current !== next) {
    writeFileSync(OUT, next);
    console.log('wrote src/core/skills/bundled-skills.generated.ts');
  }
}