import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { isInitialized } from "../utils.js"; /** * Register event handlers for the docgraph extension. * * - session_start: notify user if docgraph is not initialized * - before_agent_start: inject docgraph context into the system prompt */ export function registerEvents(pi: ExtensionAPI): void { // Notify on session start if docgraph isn't initialized pi.on("session_start", async (_event, ctx) => { if (!isInitialized(ctx)) { ctx.ui.setStatus( "docgraph", "Docgraph: not initialized — ask agent to run docgraph_init", ); } else { ctx.ui.setStatus("docgraph", "Docgraph: initialized"); } }); // Inject docgraph context into the system prompt so the AI knows about it pi.on("before_agent_start", async (event, ctx) => { const initialized = isInitialized(ctx); if (!initialized) { // If not initialized, let the AI know it can scaffold docs return { systemPrompt: (event.systemPrompt ?? "") + [ "", "## Docgraph (AI-Native Documentation Graph)", "", "This project uses **pi-docgraph**, an AI-native documentation system.", "The documentation has NOT been initialized yet.", "", "Use the `docgraph_init` tool to scaffold the documentation structure:", "- AGENTS.md (AI entry point)", "- README.md (human entry point)", "- docs/SPEC.md, ARCHITECTURE.md, API.md, DESIGN.md", "- docs/ROADMAP.md, BACKLOG.md", "- docs/tickets/ (implementation tickets)", "", "After initialization:", `- Use \`docgraph_read\` to read a document's metadata and body`, `- Use \`docgraph_sync\` to validate cross-references after code changes`, `- Use \`docgraph_update\` to modify document fields`, `- Use \`docgraph_ticket_create\`, \`docgraph_ticket_update\`, \`docgraph_ticket_list\` for ticket-driven workflows`, "", "When implementing tickets, follow the Ticket Completion & Verification Rules section in AGENTS.md.", "Always treat the codebase as the source of truth.", ].join("\n"), }; } // If initialized, add a minimal context line return { systemPrompt: (event.systemPrompt ?? "") + [ "", "## Docgraph", "", "This project uses pi-docgraph for AI-native documentation.", `Use \`docgraph_read\` to read docs, \`docgraph_sync\` to validate links,`, `\`docgraph_ticket_list\` to view the work queue, and \`docgraph_ticket_update\` to move tickets.`, "The codebase is always the source of truth.", ].join("\n"), }; }); }