import { homedir } from "node:os"; import { isAbsolute, relative, resolve, sep } from "node:path"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui"; function formatTokens(count: number): string { if (count < 1_000) return count.toString(); if (count < 10_000) return `${(count / 1_000).toFixed(1)}k`; if (count < 1_000_000) return `${Math.round(count / 1_000)}k`; if (count < 10_000_000) return `${(count / 1_000_000).toFixed(1)}M`; return `${Math.round(count / 1_000_000)}M`; } function formatCwd(cwd: string): string { const home = homedir(); const relativeToHome = relative(resolve(home), resolve(cwd)); const isInsideHome = relativeToHome === "" || (relativeToHome !== ".." && !relativeToHome.startsWith(`..${sep}`) && !isAbsolute(relativeToHome)); if (!isInsideHome) return cwd; return relativeToHome === "" ? "~" : `~${sep}${relativeToHome}`; } function sanitizeStatus(text: string): string { return text.replace(/[\r\n\t]/g, " ").replace(/ +/g, " ").trim(); } function alignRight(text: string, width: number): string { const clipped = truncateToWidth(text, width, ""); return " ".repeat(Math.max(0, width - visibleWidth(clipped))) + clipped; } function joinSides(left: string, right: string, width: number): string { if (!left) return alignRight(right, width); if (!right) return truncateToWidth(left, width, ""); const rightClipped = truncateToWidth(right, width, ""); const rightWidth = visibleWidth(rightClipped); const leftWidth = Math.max(0, width - rightWidth - 2); if (leftWidth === 0) return alignRight(rightClipped, width); const leftClipped = truncateToWidth(left, leftWidth, ""); const padding = " ".repeat(Math.max(2, width - visibleWidth(leftClipped) - rightWidth)); return leftClipped + padding + rightClipped; } export default function (pi: ExtensionAPI) { let installed = false; pi.on("session_start", (_event, ctx) => { if (installed || ctx.mode !== "tui") return; installed = true; ctx.ui.setFooter((tui, theme, footerData) => { const unsubscribe = footerData.onBranchChange(() => tui.requestRender()); return { render(width: number): string[] { let location = formatCwd(ctx.sessionManager.getCwd()); const branch = footerData.getGitBranch(); if (branch) location += ` (${branch})`; const sessionName = ctx.sessionManager.getSessionName(); if (sessionName) location += ` • ${sessionName}`; const session = `session ${ctx.sessionManager.getSessionId()}`; const locationLine = joinSides(theme.fg("dim", location), theme.fg("dim", session), width); const model = ctx.model; let modelText = model?.name || model?.id || "no-model"; if (model?.reasoning) { const thinkingLevel = pi.getThinkingLevel(); modelText += thinkingLevel === "off" ? " • thinking off" : ` • ${thinkingLevel}`; } if (model && footerData.getAvailableProviderCount() > 1) { modelText = `(${model.provider}) ${modelText}`; } const modelLine = alignRight(theme.fg("dim", modelText), width); let totalCost = 0; for (const entry of ctx.sessionManager.getEntries()) { if (entry.type === "message" && entry.message.role === "assistant") { totalCost += entry.message.usage.cost.total; } } const usage = ctx.getContextUsage(); const contextWindow = usage?.contextWindow ?? model?.contextWindow ?? 0; const contextPercent = usage?.percent == null ? "?" : `${usage.percent.toFixed(1)}%`; const usageText = `${contextPercent}/${formatTokens(contextWindow)} • $${totalCost.toFixed(3)}`; const hiddenStatuses = new Set(["mcp", "mcp-auth", "perf-metrics"]); const statuses = Array.from(footerData.getExtensionStatuses().entries()) .filter(([key]) => !hiddenStatuses.has(key)) .sort(([a], [b]) => a.localeCompare(b)) .map(([, text]) => sanitizeStatus(text)) .join(" "); const usageLine = joinSides(theme.fg("dim", usageText), statuses, width); return [locationLine, modelLine, usageLine]; }, invalidate() {}, dispose: unsubscribe, }; }); }); }