|
| 1 | +/** |
| 2 | + * Shared TUI indicator updates for the agenticoding extension. |
| 3 | + * |
| 4 | + * Extracted from index.ts so that tool execute handlers can push live |
| 5 | + * updates to the TUI during tool execution — not just at turn boundaries. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; |
| 9 | +import type { AgenticodingState } from "./state.js"; |
| 10 | + |
| 11 | +// ── TUI status / widget keys ───────────────────────────────────────── |
| 12 | + |
| 13 | +/** Status bar key for the handoff-in-progress indicator. */ |
| 14 | +export const STATUS_KEY_HANDOFF = "agenticoding-handoff"; |
| 15 | + |
| 16 | +/** Widget key for the high-context warning banner above the editor. */ |
| 17 | +export const WIDGET_KEY_WARNING = "agenticoding-warning"; |
| 18 | + |
| 19 | +/** Status bar key for context usage percentage. */ |
| 20 | +export const STATUS_KEY_CTX = "agenticoding-ctx"; |
| 21 | + |
| 22 | +/** Status bar key for ledger entry count. */ |
| 23 | +export const STATUS_KEY_LEDGER = "agenticoding-ledger"; |
| 24 | + |
| 25 | +/** Update TUI indicators: context usage, ledger count, warning widget. */ |
| 26 | +export function updateIndicators(ctx: ExtensionContext, state: AgenticodingState): void { |
| 27 | + if (!ctx.hasUI) return; |
| 28 | + |
| 29 | + const theme = ctx.ui.theme; |
| 30 | + |
| 31 | + // Context usage |
| 32 | + const usage = ctx.getContextUsage(); |
| 33 | + if (usage && usage.percent !== null) { |
| 34 | + const pct = Math.round(usage.percent); |
| 35 | + const tone = pct >= 70 ? "error" : pct >= 50 ? "warning" : pct >= 30 ? "accent" : "dim"; |
| 36 | + ctx.ui.setStatus(STATUS_KEY_CTX, theme.fg("dim", "ctx ") + theme.fg(tone, `${pct}%`)); |
| 37 | + } else { |
| 38 | + ctx.ui.setStatus(STATUS_KEY_CTX, theme.fg("dim", "ctx --%")); |
| 39 | + } |
| 40 | + |
| 41 | + // Ledger count — show 📒 0 in dim tone when empty so the feature is discoverable |
| 42 | + const count = state.ledger.size; |
| 43 | + ctx.ui.setStatus(STATUS_KEY_LEDGER, count > 0 |
| 44 | + ? `\u{1F4D2} ${count}` |
| 45 | + : theme.fg("dim", "\u{1F4D2} 0"), |
| 46 | + ); |
| 47 | + |
| 48 | + // High-context warning widget (above editor) |
| 49 | + if (usage && usage.percent !== null && usage.percent >= 70) { |
| 50 | + ctx.ui.setWidget(WIDGET_KEY_WARNING, [ |
| 51 | + theme.fg("error", "\u26A0 ") + |
| 52 | + theme.fg("warning", `Context at ${Math.round(usage.percent)}% — consider handoff`), |
| 53 | + ]); |
| 54 | + } else { |
| 55 | + ctx.ui.setWidget(WIDGET_KEY_WARNING, undefined); |
| 56 | + } |
| 57 | +} |
0 commit comments