@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
140 lines • 7.11 kB
JavaScript
import { Loader, sliceByColumn, truncateToWidth, visibleWidth, } from "@earendil-works/pi-tui";
import { theme } from "../theme/theme.js";
import { CountdownTimer } from "./countdown-timer.js";
import { keyText } from "./keybinding-hints.js";
export class StatusIndicator extends Loader {
constructor(kind, ui, spinnerColorFn, messageColorFn, message, indicator) {
super(ui, spinnerColorFn, messageColorFn, message, indicator);
this.kind = kind;
}
renderInBorder(width) {
const line = super.render(width + 2)[1] ?? "";
return truncateToWidth(line.startsWith(" ") ? line.slice(1).trimEnd() : line.trimEnd(), width, "");
}
renderSpinnerInBorder(width) {
return truncateToWidth(this.getRenderedIndicator(), width, "");
}
dispose() {
this.stop();
}
setProgressText(_progressText) {
// Only compaction status renders progress.
}
}
export class WorkingStatusIndicator extends StatusIndicator {
constructor(ui, message, indicator, colorFn) {
super("working", ui, colorFn ?? ((text) => theme.fg("accent", text)), colorFn ?? ((text) => theme.fg("muted", text)), message, indicator);
}
}
export class RetryStatusIndicator extends StatusIndicator {
constructor(ui, attempt, maxAttempts, delayMs, indicator, providerTrouble = false, reason) {
const cause = reason === undefined ? "" : ` (${reason})`;
const retryMessage = (seconds) => providerTrouble
? `The model provider may be having trouble${cause}. Retrying... (${attempt}/${maxAttempts}, ${seconds > 0 ? `in ${seconds}s` : "now"}; ${keyText("app.interrupt")} to cancel)`
: `Retrying (${attempt}/${maxAttempts}) in ${seconds}s... (${keyText("app.interrupt")} to cancel)`;
const compactMessage = (seconds) => `Retrying ${attempt}/${maxAttempts} ${seconds > 0 ? `in ${seconds}s` : "now"} (${keyText("app.interrupt")} cancel)${reason === undefined ? "" : `: ${reason}`}`;
const retryIndicator = indicator === undefined
? undefined
: {
...indicator,
indicatorFormatter: indicator.indicatorFormatter ?? ((spinner) => theme.fg("warning", spinner)),
};
super("retry", ui, (spinner) => theme.fg("warning", spinner), (text) => theme.fg("muted", text), retryMessage(Math.ceil(delayMs / 1000)), retryIndicator);
if (providerTrouble)
this.compactMessage = compactMessage(Math.ceil(delayMs / 1000));
this.countdown = new CountdownTimer(delayMs, ui, (seconds) => {
if (providerTrouble)
this.compactMessage = compactMessage(seconds);
this.setMessage(retryMessage(seconds));
}, () => {
this.countdown = undefined;
});
}
renderInBorder(width) {
if (this.compactMessage && super.render(width + 2).length > 2) {
return truncateToWidth(`${this.getRenderedIndicator()} ${theme.fg("muted", this.compactMessage)}`, width, "");
}
return super.renderInBorder(width);
}
dispose() {
this.countdown?.dispose();
this.countdown = undefined;
super.dispose();
}
}
export class CompactionStatusIndicator extends StatusIndicator {
constructor(ui, reason) {
const cancelHint = `(${keyText("app.interrupt")} to cancel)`;
const label = reason === "manual"
? `Compacting context... ${cancelHint}`
: reason === "overflow"
? `Context overflow detected, compacting... ${cancelHint}`
: reason === "pre_prompt"
? `Compacting before next prompt... ${cancelHint}`
: reason === "threshold"
? `Auto-compacting... ${cancelHint}`
: `Compacting context... ${cancelHint}`;
super("compaction", ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), label);
this.progressText = "";
// Once a streamed preview needs room, the reason-specific label collapses to the
// shortest form that still keeps the cancellation hint fully visible.
this.progressLabel = `Compacting... ${cancelHint}`;
}
setProgressText(progressText) {
if (!this.progressText && progressText)
this.setMessage(this.progressLabel);
this.progressText = progressText;
}
renderInBorder(width) {
// The embedded editor border goes through this hook, not render(), so route it through the
// single-row render below to keep the cancellation hint and the streamed preview.
return truncateToWidth((this.render(width)[0] ?? "").trim(), width, "");
}
render(width) {
// Loader.render() prepends a spacer row; joining keeps this indicator a single row
// whether or not a preview is present, so the composer never shifts between states.
// Text pads rows to the full width, so trailing padding is stripped before measuring.
let status = super.render(width).join(" ").trimEnd();
// On narrow terminals the reason-specific label can wrap even before any preview
// arrives, and head-truncating the joined rows would drop the cancellation hint.
// Collapse to the compact label whenever the full status cannot fit on one row.
if (visibleWidth(status) > width) {
this.setMessage(this.progressLabel);
status = super.render(width).join(" ").trimEnd();
}
if (!this.progressText)
return [truncateToWidth(status, width)];
// The status label (with its cancellation hint) is allocated first; the preview only
// receives whatever width is left over instead of starving the hint out of the row.
const progressWidth = Math.min(visibleWidth(this.progressText), width - visibleWidth(status) - 1);
if (progressWidth <= 0)
return [truncateToWidth(status, width)];
// The preview is accumulated summary text, so keep the newest trailing columns.
const totalProgressWidth = visibleWidth(this.progressText);
const progressTail = totalProgressWidth > progressWidth
? sliceByColumn(this.progressText, totalProgressWidth - progressWidth, progressWidth)
: this.progressText;
return [truncateToWidth(`${status} ${theme.fg("muted", progressTail)}`, width)];
}
}
export class BranchSummaryStatusIndicator extends StatusIndicator {
constructor(ui) {
super("branchSummary", ui, (spinner) => theme.fg("accent", spinner), (text) => theme.fg("muted", text), `Summarizing branch... (${keyText("app.interrupt")} to cancel)`);
}
}
export class IdleStatus {
constructor(height = 2) {
this.height = height;
}
setHeight(height) {
this.height = height;
}
invalidate() {
// No cached state to invalidate.
}
render(width) {
const emptyLine = " ".repeat(width);
return Array.from({ length: this.height }, () => emptyLine);
}
}
//# sourceMappingURL=status-indicator.js.map