Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
4.5 kB · 150 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151import { CustomEditor, type ExtensionAPI, type ExtensionContext, type KeybindingsManager,} from "@earendil-works/pi-coding-agent";import type { Component, EditorTheme, TUI } from "@earendil-works/pi-tui";import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
function fitBorder( left: string, right: string, width: number, border: (text: string) => string, fill: (text: string) => string = border,): string { if (width <= 0) return ""; if (width === 1) return border("─");
let leftText = left; let rightText = right; const fixedWidth = 2; const minimumGap = 3;
while ( fixedWidth + visibleWidth(leftText) + visibleWidth(rightText) + minimumGap > width && visibleWidth(rightText) > 0 ) { rightText = truncateToWidth(rightText, Math.max(0, visibleWidth(rightText) - 1), ""); } while ( fixedWidth + visibleWidth(leftText) + visibleWidth(rightText) + minimumGap > width && visibleWidth(leftText) > 0 ) { leftText = truncateToWidth(leftText, Math.max(0, visibleWidth(leftText) - 1), ""); }
const gapWidth = Math.max(0, width - fixedWidth - visibleWidth(leftText) - visibleWidth(rightText)); return `${border("─")}${leftText}${fill("─".repeat(gapWidth))}${rightText}${border("─")}`;}
function formatCwd(cwd: string): string { const home = process.env.HOME; if (home && cwd.startsWith(home)) { return `~${cwd.slice(home.length)}`; } return cwd;}
function formatContext(ctx: ExtensionContext): string { const usage = ctx.getContextUsage(); const contextWindow = usage?.contextWindow ?? ctx.model?.contextWindow; if (!contextWindow || !usage || usage.percent === null) { return "ctx ?"; } return `ctx ${Math.round(usage.percent)}%/${(contextWindow / 1000).toFixed(0)}k`;}
function formatThinking(level: string): string { return level === "off" ? "off" : level;}
class EmptyFooter implements Component { render(): string[] { return []; }
invalidate(): void {}}
export default function (pi: ExtensionAPI) { let isWorking = false; let spinnerIndex = 0; let spinnerTimer: ReturnType<typeof setInterval> | undefined; let activeTui: TUI | undefined; const spinnerFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
const stopSpinner = (ctx: ExtensionContext) => { if (spinnerTimer) { ctx.clearInterval(spinnerTimer); spinnerTimer = undefined; } };
pi.on("agent_start", (_event, ctx) => { isWorking = true; stopSpinner(ctx); spinnerTimer = ctx.setInterval(() => { spinnerIndex = (spinnerIndex + 1) % spinnerFrames.length; activeTui?.requestRender(); }, 80); activeTui?.requestRender(); });
pi.on("agent_end", (_event, ctx) => { isWorking = false; stopSpinner(ctx); activeTui?.requestRender(); });
pi.on("session_shutdown", (_event, ctx) => { stopSpinner(ctx); activeTui = undefined; });
pi.on("session_start", (_event, ctx) => { ctx.ui.setWorkingVisible(false); ctx.ui.setFooter(() => new EmptyFooter());
let branch: string | undefined;
const refreshBranch = async () => { const result = await pi.exec("git", ["branch", "--show-current"], { cwd: ctx.cwd }).catch(() => undefined); const stdout = result?.stdout.trim(); branch = stdout && stdout.length > 0 ? stdout : undefined; activeTui?.requestRender(); }; void refreshBranch();
class BorderStatusEditor extends CustomEditor { constructor(tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager) { super(tui, theme, keybindings, { paddingX: 0 }); activeTui = tui; }
render(width: number): string[] { const lines = super.render(width); if (lines.length < 2) return lines;
const thm = ctx.ui.theme; const model = ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : "no model"; const thinking = pi.getThinkingLevel(); const topLeft = isWorking ? thm.fg("accent", ` ${spinnerFrames[spinnerIndex]} `) : ""; const topRight = ""; const bottomLeft = thm.fg("muted", ` ${model} · ${formatThinking(thinking)} `); const bottomRight = thm.fg( "muted", ` ${formatContext(ctx)} · ${formatCwd(ctx.cwd)}${branch ? ` (${branch})` : ""} `, ); const borderColor = (text: string) => this.borderColor(text);
lines[0] = fitBorder(topLeft, topRight, width, borderColor); lines[lines.length - 1] = fitBorder(bottomLeft, bottomRight, width, borderColor); return lines; } }
ctx.ui.setEditorComponent((tui, theme, keybindings) => new BorderStatusEditor(tui, theme, keybindings)); });}