/** * Pure TUI frame renderer for the devflow agents view. * * CLI-layer view module; zero fs/tty imports. * Pure function, no process.exit(), no I/O. * * Layout (fixed lines = 9, viewport = dims.rows - 9): * 1 Title " Devflow Agents" + right "proxy: enabled|disabled" * 2 (blank) * 3 Column header " AGENT MODEL EFFORT STATE" * 4 Scroll-up indicator " ↑ N more" (blank if none) * 5+ Viewport rows * -3 Scroll-down indicator " ↓ N more" (blank if none) * -2 (blank) * -1 Unsaved count " N unsaved changes" (blank if 0) * 0 Keybinding footer * * Columns (chars) — total 80 ≤ 80: * PREFIX : 2 (cursor mark "❯ " or " ") * AGENT : 12 * MODEL : 30 * EFFORT : 22 * STATE : 14 * * EFFORT is 22 wide so the longest unconfigured cell, "default (medium)" (16), * fits whole inside the cursor's "‹ … ›" wrapper with the dirty marker * ("‹ default (medium) ● ›", 22) — the shipped effort (D-SHIPPED-EFFORT) and the * unsaved mark are never clipped on the cursor row. The agent names the registry * holds are at most 10 characters ("Synthesize"); longer orphan keys are * truncated by truncateVisible. * * MODEL has no such slack for a worker row, whose shipped default is a full * model identifier: the focused cell narrows its own wrapper to fit * (D-FOCUSED-CELL-FITS, renderFocusedCell) rather than taking a column from * the 80-column budget. */ import { bold, dim, green, yellow, cyan, gray, stripAnsi, truncate, } from '../../core/ansi.js'; import { padToVisible, truncateVisible, sanitizeCell } from '../tui/cells.js'; import { isDirtyModel, isDirtyEffort, unsavedCount, isOffCycle, rowState, type AgentRow, type AgentsViewState, } from './state.js'; import { AGENT_STATE_LABELS, formatEffortDisplay } from '../../core/agent-state.js'; // --------------------------------------------------------------------------- // Layout constants // --------------------------------------------------------------------------- /** Non-viewport fixed lines in a rendered frame (see layout comment above). */ export const FIXED_ROWS = 9; const MIN_VIEWPORT = 1; // --------------------------------------------------------------------------- // Viewport height (exported so terminal.ts and agents.ts share one definition) // --------------------------------------------------------------------------- /** Return the number of data rows the terminal can display given its height. */ export function computeViewportHeight(termRows: number): number { return Math.max(MIN_VIEWPORT, termRows - FIXED_ROWS); } const COL_AGENT = 12; const COL_MODEL = 30; const COL_EFFORT = 22; const COL_STATE = 14; // --------------------------------------------------------------------------- // Name formatter — TUI only // --------------------------------------------------------------------------- /** * Title-case each hyphen-separated segment of the agent name for TUI display. * * Examples: 'code' → 'Code', 'my-custom-agent' → 'My-Custom-Agent' * * TUI-only — `--list` output uses the raw lowercase name from the registry. * Exactly ONE call site: the name cell in the row renderer below. * * Pure function, no I/O. */ export function formatAgentName(name: string): string { if (name.length === 0) return name; return name .split('-') .map(seg => (seg.length === 0 ? seg : seg[0].toUpperCase() + seg.slice(1))) .join('-'); } // --------------------------------------------------------------------------- // Cell renderers (pure, return styled string) // --------------------------------------------------------------------------- /** * Wrap the focused field's value in the cursor arrows, with the unsaved mark * after the value when the field is dirty. * * D-FOCUSED-CELL-FITS: the arrows and the unsaved mark are never clipped. A * column is sized for the common value, but a worker row ships a full model * identifier ("default (claude-sonnet-5-5)" is 27 characters), so the spaced * wrapper "‹ default (claude-sonnet-5-5) ● ›" is 33 in a 30-wide MODEL cell and * the closing arrow and the mark would fall off the end. The layout already * spends all 80 columns and no other column has slack (EFFORT needs 22 for * "‹ default (medium) ● ›", STATE needs 14 for "saved-inactive"), so the cell * narrows itself instead. The widest form that fits wins: * spaced "‹ value ● ›" * tight "‹value●›" — the padding spaces go, the value stays whole * clipped "‹valu…●›" — only a value wider than the column; the * value gives way, never the wrapper * * Pure function, no I/O. */ function renderFocusedCell(value: string, dirty: boolean, maxWidth: number): string { const valueWidth = stripAnsi(value).length; const mark = dirty ? '●' : ''; const spacedWidth = valueWidth + 4 + (dirty ? 2 : 0); if (spacedWidth <= maxWidth) { return cyan(`‹ ${value}${dirty ? ` ${mark}` : ''} ›`); } const tightBudget = Math.max(1, maxWidth - 2 - mark.length); if (valueWidth <= tightBudget) { return cyan(`‹${value}${mark}›`); } return cyan(`‹${truncate(stripAnsi(value), tightBudget)}${mark}›`); } /** Options for renderModelCell — named to prevent silent argument transposition. */ interface RenderModelCellOptions { readonly row: AgentRow; readonly isCursor: boolean; /** Whether the model field is the currently active field on the cursor row. */ readonly isActive: boolean; readonly maxWidth: number; readonly modelCycle: readonly string[]; } /** * Render the model cell for a given row, considering cursor/active/dirty state. * * Three branches: * 1. configuredModel === 'default' → "default (shippedDefault)" [+ dormant hint] * 2. off-cycle pin → "model (unavailable)" * 3. in-cycle model → bare name (aliases stored as picker names by buildRow) * * Off-cycle pin (AC-F4): when configuredModel is absent from modelCycle * (retired/unavailable model), show "model (unavailable)". * * Dormant model: proxy off, saved external model → "default (hint) model saved". */ function renderModelCell({ row, isCursor, isActive, maxWidth, modelCycle, }: RenderModelCellOptions): string { const dirty = isDirtyModel(row); // Model names come from user-controlled config and the third-party external // model catalog. Strip ANSI escapes at the display boundary so hostile // sequences cannot reach the terminal. Original values are used for cycle // checks; safe copies for display strings only. const safeConfiguredModel = stripAnsi(row.configuredModel); const safeShippedDefault = stripAnsi(row.shippedDefault); const safeDormantModel = row.dormantModel !== null ? stripAnsi(row.dormantModel) : null; let valueStr: string; if (row.configuredModel === 'default') { const hint = dim(`(${safeShippedDefault})`); valueStr = `default ${hint}`; if (safeDormantModel !== null) { // Dormant: show saved model name as dim annotation valueStr += ` ${dim(`${safeDormantModel} saved`)}`; } } else if (!row.worker && isOffCycle(modelCycle, row.configuredModel)) { // A worker row is exempt: its model is never judged against the catalog, and // a full claude- identifier, which no cycle lists, is in the worker domain // (D-WORKER-AGENTS) — readAgentMapping has already dropped anything outside it. // Off-cycle pin: model was saved but is no longer in the discovered catalog. // The per-row effective cycle (state.ts cycleField) includes it for reachability, // but it renders as unavailable to signal the user should update it. valueStr = `${safeConfiguredModel} (unavailable)`; } else { // In-cycle model: render bare. Aliases are already stored as picker names // (normalized by buildRow via buildPickerNameMap), so no annotation needed. valueStr = safeConfiguredModel; } let cell: string; if (isCursor && isActive) { // Active field on cursor row: wrap in ‹ ›, put ● after value if dirty cell = renderFocusedCell(valueStr, dirty, maxWidth); } else if (isCursor && dirty) { cell = `● ${valueStr}`; } else { cell = valueStr; } return truncateVisible(cell, maxWidth); } /** * Render the effort cell for a given row, considering cursor/active/dirty state. * * D-SHIPPED-EFFORT: an unconfigured row shows `default ()` when * the shipped source carries an effort, through the formatter --list shares. */ function renderEffortCell( row: AgentRow, isCursor: boolean, isActive: boolean, maxWidth: number, ): string { const dirty = isDirtyEffort(row); const value = formatEffortDisplay(row.configuredEffort, row.shippedEffort); let cell: string; if (isCursor && isActive) { cell = renderFocusedCell(value, dirty, maxWidth); } else if (isCursor && dirty) { cell = `● ${value}`; } else { cell = value; } return truncateVisible(cell, maxWidth); } /** * Render the state cell (installed/active/dormant/orphan) for a row. * Pure function — derives state via rowState(row, proxyEnabled). */ function renderStateCell(row: AgentRow, proxyEnabled: boolean, maxWidth: number): string { const state = rowState(row, proxyEnabled); let cell: string; switch (state) { case 'active': cell = green(AGENT_STATE_LABELS['active']); break; case 'saved-inactive': cell = yellow(AGENT_STATE_LABELS['saved-inactive']); break; case 'not-installed': cell = dim(AGENT_STATE_LABELS['not-installed']); break; case 'unknown': cell = dim(AGENT_STATE_LABELS['unknown']); break; case 'worker': cell = dim(AGENT_STATE_LABELS['worker']); break; default: { const _: never = state; void _; cell = ''; } } return truncateVisible(cell, maxWidth); } // --------------------------------------------------------------------------- // renderFrame // --------------------------------------------------------------------------- export interface RenderDims { readonly rows: number; readonly cols: number; } /** * Render a complete TUI frame as an array of strings (one per terminal line). * No newlines within strings. Safe at any dims (narrows gracefully). */ export function renderFrame( state: AgentsViewState, dims: RenderDims, ): string[] { const { rows, cursor, activeField, viewportOffset, proxyEnabled, modelCycle, } = state; // state.viewportHeight is the single owner — clamped to a MIN so tests that // set viewportHeight explicitly render exactly that many data rows. const viewportHeight = Math.max(MIN_VIEWPORT, state.viewportHeight); // Column widths — shrink gracefully at narrow terminals. const totalContent = 2 + COL_AGENT + COL_MODEL + COL_EFFORT + COL_STATE; // prefix + 4 cols const scale = Math.min(1, dims.cols / Math.max(totalContent, 1)); const agentW = Math.max(6, Math.floor(COL_AGENT * scale)); const modelW = Math.max(8, Math.floor(COL_MODEL * scale)); const effortW = Math.max(7, Math.floor(COL_EFFORT * scale)); const stateW = Math.max(5, Math.floor(COL_STATE * scale)); // --------------------------------------------------------------------------- // 1. Title line // --------------------------------------------------------------------------- const proxyLabel = proxyEnabled ? `proxy: ${green('enabled')}` : `proxy: ${yellow('disabled')}`; const title = bold(' Devflow Agents'); const titleVisible = stripAnsi(title); const proxyVisible = stripAnsi(proxyLabel); const gap = Math.max(1, dims.cols - titleVisible.length - proxyVisible.length); const titleLine = `${title}${' '.repeat(gap)}${proxyLabel}`; // --------------------------------------------------------------------------- // 2. Column header // --------------------------------------------------------------------------- const colHeader = ` ` + padToVisible(gray('AGENT'), agentW) + padToVisible(gray('MODEL'), modelW) + padToVisible(gray('EFFORT'), effortW) + gray('STATE'); // --------------------------------------------------------------------------- // 3. Determine visible row range // --------------------------------------------------------------------------- const totalRows = rows.length; const lastVisible = Math.min(totalRows - 1, viewportOffset + viewportHeight - 1); const visibleRows = rows.slice(viewportOffset, lastVisible + 1); const rowsAbove = viewportOffset; const rowsBelow = Math.max(0, totalRows - (lastVisible + 1)); // --------------------------------------------------------------------------- // 4. Render visible rows // --------------------------------------------------------------------------- const renderedRows: string[] = visibleRows.map((row, relIdx) => { const absIdx = viewportOffset + relIdx; const isCursor = absIdx === cursor; const prefix = isCursor ? '❯ ' : ' '; // Sanitize the name — mandatory for orphan rows (arbitrary JSON keys from // agent-models.json may contain escape sequences, newlines or tabs injected // by a hostile file). // formatAgentName is TUI-only; --list uses the raw lowercase name. const safeName = formatAgentName(sanitizeCell(row.name)); const nameCell = padToVisible( isCursor ? bold(truncateVisible(safeName, agentW)) : truncateVisible(safeName, agentW), agentW, ); const modelCell = padToVisible( renderModelCell({ row, isCursor, isActive: isCursor && activeField === 'model', maxWidth: modelW, modelCycle, }), modelW, ); const effortCell = padToVisible( renderEffortCell( row, isCursor, isCursor && activeField === 'effort', effortW, ), effortW, ); const stateCell = renderStateCell(row, proxyEnabled, stateW); return `${prefix}${nameCell}${modelCell}${effortCell}${stateCell}`; }); // --------------------------------------------------------------------------- // 5. Scroll indicators // --------------------------------------------------------------------------- const upIndicator = rowsAbove > 0 ? dim(` ↑ ${rowsAbove} more`) : ''; const downIndicator = rowsBelow > 0 ? dim(` ↓ ${rowsBelow} more`) : ''; // --------------------------------------------------------------------------- // 6. Footer // --------------------------------------------------------------------------- const count = unsavedCount(rows); const unsavedLine = count > 0 ? ` ${yellow(`${count} unsaved change${count === 1 ? '' : 's'}`)}` : ''; // Truncate to cols before applying dim so narrow terminals stay within bounds. // Uses the same primitive as every cell — one truncation rule per renderer. const keybindingsText = ' ↑↓ agent tab field ←→/space cycle d default enter save esc cancel'; const keybindingsLine = dim(truncateVisible(keybindingsText, dims.cols)); const proxyHintLine = !proxyEnabled ? dim(' devflow proxy --enable to activate GPT models') : ''; // --------------------------------------------------------------------------- // Assemble // --------------------------------------------------------------------------- const out: string[] = [ titleLine, '', colHeader, upIndicator, ...renderedRows, downIndicator, '', unsavedLine, keybindingsLine, proxyHintLine, ]; return out; }