|
| 1 | +/** |
| 2 | + * Fetch all memory-* skills from basicmachines-co/basic-memory-skills. |
| 3 | + * |
| 4 | + * Auto-discovers skill directories via GitHub Contents API, downloads each |
| 5 | + * SKILL.md, writes to skills/<dir>/SKILL.md, and generates skills/manifest.json. |
| 6 | + * |
| 7 | + * Env vars: |
| 8 | + * GITHUB_TOKEN — optional, avoids 60 req/hr unauthenticated rate limit |
| 9 | + * SKILLS_BRANCH — branch to fetch from (default: "main") |
| 10 | + */ |
| 11 | + |
| 12 | +import { mkdirSync, writeFileSync } from "node:fs" |
| 13 | +import { dirname, resolve } from "node:path" |
| 14 | +import { fileURLToPath } from "node:url" |
| 15 | + |
| 16 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 17 | +const SKILLS_DIR = resolve(__dirname, "..", "skills") |
| 18 | + |
| 19 | +const REPO = "basicmachines-co/basic-memory-skills" |
| 20 | +const BRANCH = process.env.SKILLS_BRANCH ?? "main" |
| 21 | +const TOKEN = process.env.GITHUB_TOKEN |
| 22 | + |
| 23 | +interface GitHubEntry { |
| 24 | + name: string |
| 25 | + type: "file" | "dir" |
| 26 | +} |
| 27 | + |
| 28 | +interface SkillManifestEntry { |
| 29 | + dir: string |
| 30 | + name: string |
| 31 | + description: string |
| 32 | +} |
| 33 | + |
| 34 | +function headers(): Record<string, string> { |
| 35 | + const h: Record<string, string> = { |
| 36 | + Accept: "application/vnd.github.v3+json", |
| 37 | + "User-Agent": "openclaw-basic-memory/fetch-skills", |
| 38 | + } |
| 39 | + if (TOKEN) h.Authorization = `Bearer ${TOKEN}` |
| 40 | + return h |
| 41 | +} |
| 42 | + |
| 43 | +async function fetchJSON<T>(url: string): Promise<T> { |
| 44 | + const res = await fetch(url, { headers: headers() }) |
| 45 | + if (!res.ok) { |
| 46 | + throw new Error(`GET ${url} → ${res.status} ${res.statusText}`) |
| 47 | + } |
| 48 | + return res.json() as Promise<T> |
| 49 | +} |
| 50 | + |
| 51 | +async function fetchText(url: string): Promise<string> { |
| 52 | + const res = await fetch(url, { headers: headers() }) |
| 53 | + if (!res.ok) { |
| 54 | + throw new Error(`GET ${url} → ${res.status} ${res.statusText}`) |
| 55 | + } |
| 56 | + return res.text() |
| 57 | +} |
| 58 | + |
| 59 | +function parseFrontmatter(md: string): { name: string; description: string } { |
| 60 | + const match = md.match(/^---\r?\n([\s\S]*?)\r?\n---/) |
| 61 | + if (!match) throw new Error("SKILL.md missing YAML frontmatter") |
| 62 | + |
| 63 | + const yaml = match[1] |
| 64 | + const name = yaml |
| 65 | + .match(/^name:\s*(.+)$/m)?.[1] |
| 66 | + ?.trim() |
| 67 | + .replace(/^["']|["']$/g, "") |
| 68 | + const description = yaml |
| 69 | + .match(/^description:\s*(.+)$/m)?.[1] |
| 70 | + ?.trim() |
| 71 | + .replace(/^["']|["']$/g, "") |
| 72 | + |
| 73 | + if (!name) throw new Error("Frontmatter missing 'name'") |
| 74 | + if (!description) throw new Error("Frontmatter missing 'description'") |
| 75 | + |
| 76 | + return { name, description } |
| 77 | +} |
| 78 | + |
| 79 | +async function main() { |
| 80 | + console.log(`Fetching skills from ${REPO}@${BRANCH}`) |
| 81 | + |
| 82 | + // 1. Discover all memory-* directories |
| 83 | + const contentsUrl = `https://api.github.com/repos/${REPO}/contents?ref=${BRANCH}` |
| 84 | + const entries = await fetchJSON<GitHubEntry[]>(contentsUrl) |
| 85 | + const skillDirs = entries |
| 86 | + .filter((e) => e.type === "dir" && e.name.startsWith("memory-")) |
| 87 | + .map((e) => e.name) |
| 88 | + .sort() |
| 89 | + |
| 90 | + if (skillDirs.length === 0) { |
| 91 | + throw new Error("No memory-* directories found in repo") |
| 92 | + } |
| 93 | + |
| 94 | + console.log(`Found ${skillDirs.length} skills: ${skillDirs.join(", ")}`) |
| 95 | + |
| 96 | + // 2. Download each SKILL.md and parse frontmatter |
| 97 | + const manifest: SkillManifestEntry[] = [] |
| 98 | + |
| 99 | + const results = await Promise.all( |
| 100 | + skillDirs.map(async (dir) => { |
| 101 | + const rawUrl = `https://raw.githubusercontent.com/${REPO}/${BRANCH}/${dir}/SKILL.md` |
| 102 | + const content = await fetchText(rawUrl) |
| 103 | + const meta = parseFrontmatter(content) |
| 104 | + return { dir, content, meta } |
| 105 | + }), |
| 106 | + ) |
| 107 | + |
| 108 | + // 3. Write files and build manifest |
| 109 | + for (const { dir, content, meta } of results) { |
| 110 | + const outDir = resolve(SKILLS_DIR, dir) |
| 111 | + mkdirSync(outDir, { recursive: true }) |
| 112 | + writeFileSync(resolve(outDir, "SKILL.md"), content) |
| 113 | + manifest.push({ dir, name: meta.name, description: meta.description }) |
| 114 | + console.log(` ✓ ${dir}`) |
| 115 | + } |
| 116 | + |
| 117 | + // Sort manifest by dir name for deterministic output |
| 118 | + manifest.sort((a, b) => a.dir.localeCompare(b.dir)) |
| 119 | + |
| 120 | + // 4. Write manifest |
| 121 | + mkdirSync(SKILLS_DIR, { recursive: true }) |
| 122 | + writeFileSync( |
| 123 | + resolve(SKILLS_DIR, "manifest.json"), |
| 124 | + `${JSON.stringify(manifest, null, 2)}\n`, |
| 125 | + ) |
| 126 | + |
| 127 | + console.log(`\nWrote ${manifest.length} skills + manifest.json to skills/`) |
| 128 | +} |
| 129 | + |
| 130 | +main().catch((err) => { |
| 131 | + console.error("Fatal:", err.message) |
| 132 | + process.exit(1) |
| 133 | +}) |
0 commit comments