import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui"; import type { Component } from "@earendil-works/pi-tui"; import { previewText } from "./core"; import type { LlmiterateRun, RunStatus, UiState } from "./types"; const STATUS_KEY = "llmiterate"; const WIDGET_KEY = "llmiterate"; const WIDGET_RUN_LIMIT = 6; const PROMPT_PREVIEW_CHARS = 80; const DETAIL_PREVIEW_CHARS = 110; export function renderLlmiterateUi( ctx: ExtensionContext | undefined, panelVisible: boolean, runs: readonly LlmiterateRun[], state: UiState, ): void { if (!ctx?.hasUI) return; ctx.ui.setStatus(STATUS_KEY, statusLine(ctx, state)); if (!panelVisible) { ctx.ui.setWidget(WIDGET_KEY, undefined); return; } ctx.ui.setWidget(WIDGET_KEY, (_tui, theme) => new LlmiterateWidget(runs, theme), { placement: "belowEditor", }); } function statusLine(ctx: ExtensionContext, state: UiState): string { const theme = ctx.ui.theme; if (!state.config?.enabled) return theme.fg("dim", "llmiterate off"); if (state.lockError) return theme.fg("dim", "llmiterate standby"); if (state.watchErrors > 0) return theme.fg("warning", `llmiterate ${state.watchErrors} watch error(s)`); if (state.activeRun) return theme.fg("accent", `llmiterate ${state.activeRun.status} ${state.activeRun.block.file}:${state.activeRun.block.startLine}`); if (state.queuedRuns > 0) return theme.fg("warning", `llmiterate queued ${state.queuedRuns}`); return theme.fg("dim", "llmiterate idle"); } export class LlmiterateWidget implements Component { constructor( private readonly runs: readonly LlmiterateRun[], private readonly theme: ExtensionContext["ui"]["theme"], ) {} render(width: number): string[] { if (width <= 0) return []; const inner = Math.max(1, width - 2); const lines = [ this.border("╭", "╮", inner, ` ${this.theme.bold("llmiterate")} `), ...this.runLines(inner), this.border("╰", "╯", inner), ]; return lines.map((line) => truncateToWidth(line, width, "")); } invalidate(): void {} private runLines(inner: number): string[] { if (this.runs.length === 0) return [this.line(this.theme.fg("dim", "no prompts queued yet"), inner)]; const lines: string[] = []; for (const run of this.runs.slice(0, WIDGET_RUN_LIMIT)) { lines.push(this.line(this.runHeader(run), inner)); if (run.toolText) lines.push(this.line(this.theme.fg("dim", ` ${run.toolText}`), inner)); if (run.error) lines.push(this.line(this.theme.fg("error", ` ${previewText(run.error, DETAIL_PREVIEW_CHARS)}`), inner)); else if (run.assistantText) lines.push(this.line(this.theme.fg("dim", ` ${previewText(run.assistantText, DETAIL_PREVIEW_CHARS)}`), inner)); } return lines; } private runHeader(run: LlmiterateRun): string { return `${statusIcon(run.status)} ${run.block.file}:${run.block.startLine} ${this.theme.fg("muted", run.block.marker)} ${previewText(run.block.prompt, PROMPT_PREVIEW_CHARS)}`; } private border(left: string, right: string, inner: number, label = ""): string { const labelWidth = visibleWidth(label); return this.theme.fg("border", left) + this.theme.fg("accent", label) + this.theme.fg("border", "─".repeat(Math.max(0, inner - labelWidth)) + right); } private line(text: string, inner: number): string { const body = fit(text, inner); return this.theme.fg("border", "│") + body + this.theme.fg("border", "│"); } } function fit(text: string, width: number): string { const t = truncateToWidth(text, width, "…"); return t + " ".repeat(Math.max(0, width - visibleWidth(t))); } function statusIcon(status: RunStatus): string { if (status === "queued") return "◌"; if (status === "running") return "●"; if (status === "done") return "✓"; return "✗"; }