|
| 1 | +import { readdir, readFile } from "node:fs/promises"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import { join, relative } from "node:path"; |
| 4 | +import matter from "gray-matter"; |
| 5 | +import { skillsDir } from "../config.js"; |
| 6 | + |
| 7 | +export interface SkillGuideEntry { |
| 8 | + name: string; |
| 9 | + description: string; |
| 10 | +} |
| 11 | + |
| 12 | +export interface SkillGuideContent { |
| 13 | + name: string; |
| 14 | + description: string; |
| 15 | + content: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Resolve the skills directory. Checks the package-bundled location first, |
| 20 | + * then falls back to the monorepo root (for dev without build). |
| 21 | + */ |
| 22 | +function resolveSkillsDir(): string | null { |
| 23 | + if (existsSync(skillsDir)) return skillsDir; |
| 24 | + // Dev mode fallback: monorepo root |
| 25 | + const monorepoSkills = join(skillsDir, "..", "..", "skills"); |
| 26 | + if (existsSync(monorepoSkills)) return monorepoSkills; |
| 27 | + return null; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Recursively collect files from a directory. |
| 32 | + */ |
| 33 | +async function walkDir(dir: string): Promise<string[]> { |
| 34 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 35 | + const files: string[] = []; |
| 36 | + for (const entry of entries) { |
| 37 | + const full = join(dir, entry.name); |
| 38 | + if (entry.isDirectory()) { |
| 39 | + files.push(...(await walkDir(full))); |
| 40 | + } else { |
| 41 | + files.push(full); |
| 42 | + } |
| 43 | + } |
| 44 | + return files; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Derive a guide name from a file path relative to the skills directory. |
| 49 | + * |
| 50 | + * - `create-workflow/SKILL.md` → `"create-workflow"` |
| 51 | + * - `integrations/SKILL.md` → `"integrations"` |
| 52 | + * - `integrations/salesforce.md` → `"integrations/salesforce"` |
| 53 | + * - `integrations/scripts/fetch-schema.ts` → `"integrations/scripts/fetch-schema"` |
| 54 | + */ |
| 55 | +function deriveGuideName(relPath: string): string { |
| 56 | + // SKILL.md → use parent directory name |
| 57 | + if (relPath.endsWith("/SKILL.md") || relPath === "SKILL.md") { |
| 58 | + const dir = relPath.replace(/\/?SKILL\.md$/, ""); |
| 59 | + return dir || "index"; |
| 60 | + } |
| 61 | + // Strip extension |
| 62 | + return relPath.replace(/\.(md|ts)$/, ""); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * List all available skill guides with names and descriptions. |
| 67 | + */ |
| 68 | +export async function listSkillGuides(): Promise<SkillGuideEntry[]> { |
| 69 | + const dir = resolveSkillsDir(); |
| 70 | + if (!dir) return []; |
| 71 | + |
| 72 | + const allFiles = await walkDir(dir); |
| 73 | + const guides: SkillGuideEntry[] = []; |
| 74 | + |
| 75 | + for (const filePath of allFiles) { |
| 76 | + const rel = relative(dir, filePath); |
| 77 | + |
| 78 | + // Skip disabled files |
| 79 | + if (rel.includes(".disabled")) continue; |
| 80 | + |
| 81 | + // Only include .md and .ts files |
| 82 | + if (!rel.endsWith(".md") && !rel.endsWith(".ts")) continue; |
| 83 | + |
| 84 | + const name = deriveGuideName(rel); |
| 85 | + let description = ""; |
| 86 | + |
| 87 | + if (rel.endsWith(".md")) { |
| 88 | + try { |
| 89 | + const raw = await readFile(filePath, "utf-8"); |
| 90 | + const { data } = matter(raw); |
| 91 | + description = (data.description as string) || ""; |
| 92 | + } catch { |
| 93 | + // skip files we can't parse |
| 94 | + } |
| 95 | + } else { |
| 96 | + // For .ts files, use the filename as a description hint |
| 97 | + description = `Script template: ${rel}`; |
| 98 | + } |
| 99 | + |
| 100 | + guides.push({ name, description }); |
| 101 | + } |
| 102 | + |
| 103 | + // Sort: top-level skills first, then sub-guides |
| 104 | + guides.sort((a, b) => { |
| 105 | + const aDepth = a.name.split("/").length; |
| 106 | + const bDepth = b.name.split("/").length; |
| 107 | + if (aDepth !== bDepth) return aDepth - bDepth; |
| 108 | + return a.name.localeCompare(b.name); |
| 109 | + }); |
| 110 | + |
| 111 | + return guides; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Get the full content of a skill guide by name. |
| 116 | + */ |
| 117 | +export async function getSkillGuideContent( |
| 118 | + name: string, |
| 119 | +): Promise<SkillGuideContent | null> { |
| 120 | + const dir = resolveSkillsDir(); |
| 121 | + if (!dir) return null; |
| 122 | + |
| 123 | + // Try resolving the name to a file path |
| 124 | + const candidates = [ |
| 125 | + join(dir, name, "SKILL.md"), // "create-workflow" → create-workflow/SKILL.md |
| 126 | + join(dir, `${name}.md`), // "integrations/salesforce" → integrations/salesforce.md |
| 127 | + join(dir, `${name}.ts`), // "integrations/scripts/fetch-schema" → integrations/scripts/fetch-schema.ts |
| 128 | + ]; |
| 129 | + |
| 130 | + for (const filePath of candidates) { |
| 131 | + if (!existsSync(filePath)) continue; |
| 132 | + |
| 133 | + const raw = await readFile(filePath, "utf-8"); |
| 134 | + |
| 135 | + if (filePath.endsWith(".md")) { |
| 136 | + const { data, content } = matter(raw); |
| 137 | + return { |
| 138 | + name, |
| 139 | + description: (data.description as string) || "", |
| 140 | + content: content.trim(), |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + // .ts file — return raw content |
| 145 | + return { |
| 146 | + name, |
| 147 | + description: `Script template: ${relative(dir, filePath)}`, |
| 148 | + content: raw, |
| 149 | + }; |
| 150 | + } |
| 151 | + |
| 152 | + return null; |
| 153 | +} |
0 commit comments