import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"; import * as fs from "node:fs"; import * as path from "node:path"; import { runAgent } from "../lib/agents.js"; import { MAP_CODEBASE_TASK } from "../lib/prompts.js"; export function registerMapCodebaseCommand(pi: ExtensionAPI) { pi.registerCommand("hive:map-codebase", { description: "Analyze codebase structure → .hive/codebase-map.json + summary.md", handler: async (_args, ctx) => { // Ensure .hive/ directory exists const hiveDir = path.join(ctx.cwd, ".hive"); if (!fs.existsSync(hiveDir)) { fs.mkdirSync(hiveDir, { recursive: true }); } ctx.ui.notify("Mapping codebase with scout agent...", "info"); ctx.ui.setStatus("hive", "Mapping codebase..."); const result = await runAgent("scout", MAP_CODEBASE_TASK, ctx.cwd); ctx.ui.setStatus("hive", undefined); if (result.exitCode !== 0) { ctx.ui.notify(`Codebase mapping failed: ${result.stderr || result.output}`, "error"); return; } // Verify outputs were created const mapExists = fs.existsSync(path.join(hiveDir, "codebase-map.json")); const summaryExists = fs.existsSync(path.join(hiveDir, "summary.md")); if (mapExists && summaryExists) { ctx.ui.notify( `Codebase mapped successfully!\n\n .hive/codebase-map.json ✓\n .hive/summary.md ✓\n\nNext: /hive:to-features-md or /hive:spec`, "info", ); } else { // The scout agent output the analysis but didn't write files — // this can happen if tools were restricted. Write them ourselves. ctx.ui.notify( `Scout analysis complete but output files missing.\n codebase-map.json: ${mapExists ? "✓" : "✗"}\n summary.md: ${summaryExists ? "✓" : "✗"}\n\nThe scout may not have had write access. Run /hive:spec instead (uses current session).`, "warning", ); } }, }); }