import { CONTEXT_CATEGORIES, formatContextBreakdown, type ContextCategory, type ExtensionAPI, } from "@selesai/code"; import { ContextInspectorComponent } from "./component.ts"; export const CONTEXT_USAGE = "Usage: /context | /context --text | /context \nCategories: instructions, tool_definitions, conversation, tool_results, summaries, other"; export default function contextInspectorExtension(pi: ExtensionAPI): void { pi.registerCommand("context", { description: "Inspect main-session context allocation (estimated)", getArgumentCompletions: (prefix) => { const items = ["--text", ...CONTEXT_CATEGORIES] .filter((value) => value.startsWith(prefix)) .map((value) => ({ value, label: value })); return items.length ? items : null; }, handler: async (args, ctx) => { const selector = args.trim(); const category = CONTEXT_CATEGORIES.find((value) => value === selector) as ContextCategory | undefined; const valid = !selector || selector === "--text" || category !== undefined; if (valid && !selector && ctx.mode === "tui") { await ctx.ui.custom( (tui, theme, keybindings, done) => new ContextInspectorComponent({ readSnapshot: () => ctx.getContextBreakdown(), theme, keybindings, getHeight: () => Math.max(1, tui.terminal.rows), requestRender: () => tui.requestRender(), onClose: () => done(), }), { overlay: true, overlayOptions: { width: "100%", maxHeight: "100%", anchor: "center" } }, ); return; } const report = valid ? formatContextBreakdown(ctx.getContextBreakdown(category ? { category } : undefined), { category }) : CONTEXT_USAGE; // ACP's existing command-output forwarding needs a displayed custom message. // Unlike all other routes this persists and enters later model context, but starts no turn. if (ctx.mode === "rpc" && process.env.SELESAI_ACP_SESSION === "1") { pi.sendMessage({ customType: "context-inspector", display: true, content: report }, { triggerTurn: false }); } else { ctx.ui.notify(report, "info"); if (ctx.mode === "rpc") { ctx.ui.notify("Structured tree: get_context_breakdown version 1.", "info"); } } }, }); }