import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"; import * as fs from "node:fs"; import * as path from "node:path"; import { parseFeaturesMd } from "../lib/features-parser.js"; import { runAgent } from "../lib/agents.js"; import { TO_FEATURES_MD_TASK } from "../lib/prompts.js"; export function registerToFeaturesMdCommand(pi: ExtensionAPI) { pi.registerCommand("hive:to-features-md", { description: "Convert .hive/ analysis into features.md", handler: async (_args, ctx) => { const hiveDir = path.join(ctx.cwd, ".hive"); const mapPath = path.join(hiveDir, "codebase-map.json"); const summaryPath = path.join(hiveDir, "summary.md"); // Verify prerequisites if (!fs.existsSync(mapPath) || !fs.existsSync(summaryPath)) { const mapOk = fs.existsSync(mapPath); const sumOk = fs.existsSync(summaryPath); ctx.ui.notify( `.hive/ analysis not found or incomplete.\n\n codebase-map.json: ${mapOk ? "✓" : "✗"}\n summary.md: ${sumOk ? "✓" : "✗"}\n\nRun /hive:map-codebase first.`, "warning", ); return; } // Check if features.md already exists const featuresPath = path.join(ctx.cwd, "features.md"); if (fs.existsSync(featuresPath)) { const autoApprove = pi.getFlag("--auto-approve") as boolean; if (!autoApprove) { const ok = await ctx.ui.confirm( "features.md already exists", "Overwrite? (current will be backed up to features.md.backup)", ); if (!ok) return; } fs.copyFileSync(featuresPath, path.join(ctx.cwd, "features.md.backup")); } ctx.ui.notify("Generating features from .hive/ analysis...", "info"); ctx.ui.setStatus("hive", "Generating features..."); // Planner agent reads .hive/ and generates features.md const result = await runAgent("planner", TO_FEATURES_MD_TASK, ctx.cwd); ctx.ui.setStatus("hive", undefined); if (result.exitCode !== 0) { ctx.ui.notify(`Feature generation failed: ${result.stderr || result.output}`, "error"); return; } // Verify features.md was created/updated if (fs.existsSync(featuresPath)) { const content = fs.readFileSync(featuresPath, "utf-8"); const features = parseFeaturesMd(content); ctx.ui.notify( `features.md created with ${features.length} features.\n\nNext steps:\n /hive:worktree-split --count 3\n /hive:run`, "info", ); } else { ctx.ui.notify( "Planner completed but features.md was not written.\nTry /hive:spec instead (uses current session with write access).", "warning", ); } }, }); }