|
| 1 | +import { readFile, readdir } from "fs/promises"; |
| 2 | +import { homedir } from "os"; |
| 3 | +import { resolve, join } from "path"; |
| 4 | + |
| 5 | +const MONO_REPO = "Factory-AI/factory-mono"; |
| 6 | +const MONO_BRANCH = "dev"; |
| 7 | + |
| 8 | +const SHARED_BEGIN = "<!-- BEGIN_SHARED_METHODOLOGY -->"; |
| 9 | +const SHARED_END = "<!-- END_SHARED_METHODOLOGY -->"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Format skill content for inclusion in a CI prompt. |
| 13 | + * Extracts only the shared methodology (between markers) so CI-specific |
| 14 | + * instructions in the template remain authoritative for execution behavior. |
| 15 | + */ |
| 16 | +export function formatSkillSection(skillContent: string | undefined): string { |
| 17 | + if (!skillContent) return ""; |
| 18 | + const methodology = extractSharedMethodology(skillContent); |
| 19 | + return ` |
| 20 | +<code_review_methodology> |
| 21 | +${methodology} |
| 22 | +</code_review_methodology> |
| 23 | +`; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Extract the shared methodology section from a skill's content. |
| 28 | + * Looks for BEGIN_SHARED_METHODOLOGY / END_SHARED_METHODOLOGY markers. |
| 29 | + * Returns the full content if markers are not found. |
| 30 | + */ |
| 31 | +export function extractSharedMethodology(content: string): string { |
| 32 | + const beginIdx = content.indexOf(SHARED_BEGIN); |
| 33 | + const endIdx = content.indexOf(SHARED_END); |
| 34 | + if (beginIdx === -1 || endIdx === -1 || endIdx <= beginIdx) { |
| 35 | + return content; |
| 36 | + } |
| 37 | + return content.slice(beginIdx + SHARED_BEGIN.length, endIdx).trim(); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Load a skill from the local core plugin cache. |
| 42 | + * The Droid CLI installs the core plugin to: |
| 43 | + * ~/.factory/plugins/cache/factory-plugins/core/<hash>/skills/<name>/SKILL.md |
| 44 | + */ |
| 45 | +async function loadSkillFromCache( |
| 46 | + skillName: string, |
| 47 | +): Promise<string | undefined> { |
| 48 | + const home = process.env.HOME || homedir(); |
| 49 | + const cacheDir = resolve(home, ".factory/plugins/cache/factory-plugins/core"); |
| 50 | + |
| 51 | + let entries: string[]; |
| 52 | + try { |
| 53 | + entries = await readdir(cacheDir); |
| 54 | + } catch { |
| 55 | + return undefined; |
| 56 | + } |
| 57 | + |
| 58 | + for (const hash of entries) { |
| 59 | + const skillPath = join(cacheDir, hash, "skills", skillName, "SKILL.md"); |
| 60 | + try { |
| 61 | + const content = await readFile(skillPath, "utf8"); |
| 62 | + const trimmed = content.trim(); |
| 63 | + if (!trimmed) continue; |
| 64 | + console.log( |
| 65 | + `Loaded skill ${skillName} from ${skillPath} (${trimmed.length} bytes)`, |
| 66 | + ); |
| 67 | + return trimmed; |
| 68 | + } catch { |
| 69 | + continue; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return undefined; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Fetch a skill from the factory-mono GitHub repo. |
| 78 | + * Used as fallback when the local plugin cache is not available (e.g. CI). |
| 79 | + */ |
| 80 | +async function loadSkillFromGitHub( |
| 81 | + skillName: string, |
| 82 | +): Promise<string | undefined> { |
| 83 | + const url = `https://raw.githubusercontent.com/${MONO_REPO}/${MONO_BRANCH}/apps/cli/builtin-skills/${skillName}/SKILL.md`; |
| 84 | + try { |
| 85 | + const response = await fetch(url); |
| 86 | + if (!response.ok) return undefined; |
| 87 | + const content = await response.text(); |
| 88 | + const trimmed = content.trim(); |
| 89 | + if (!trimmed) return undefined; |
| 90 | + console.log( |
| 91 | + `Loaded skill ${skillName} from GitHub (${trimmed.length} bytes)`, |
| 92 | + ); |
| 93 | + return trimmed; |
| 94 | + } catch { |
| 95 | + return undefined; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Load a skill by name. Tries the local plugin cache first, |
| 101 | + * then falls back to fetching from the factory-mono GitHub repo. |
| 102 | + * Throws if the skill cannot be loaded from either source. |
| 103 | + */ |
| 104 | +export async function loadSkill(skillName: string): Promise<string> { |
| 105 | + const cached = await loadSkillFromCache(skillName); |
| 106 | + if (cached) return cached; |
| 107 | + |
| 108 | + const remote = await loadSkillFromGitHub(skillName); |
| 109 | + if (remote) return remote; |
| 110 | + |
| 111 | + throw new Error( |
| 112 | + `Required skill "${skillName}" not found in local plugin cache or on GitHub (${MONO_REPO}).`, |
| 113 | + ); |
| 114 | +} |
0 commit comments