@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
96 lines • 3.21 kB
JavaScript
import { readToolProgress } from "../tool-progress.js";
import { hasCompletedTodoTasks, TODO_STRIKE_FRAME_INTERVAL_MS, TODO_STRIKE_TOTAL_FRAMES } from "./todo-strike.js";
const PENDING_RENDER_FRAME_INTERVAL_MS = 80;
/** A card spins while edit-like arguments stream in, or while a partial task or progress result is showing. */
export function toolCardSpins(toolName, card) {
const isStreamingArgs = !card.argsComplete && ["edit", "write", "apply_patch"].includes(toolName);
const isPartialTask = card.isPartial && toolName === "task" && card.result !== undefined;
const isPartialProgress = card.isPartial && card.result !== undefined && readToolProgress(card.result.details) !== undefined;
return isStreamingArgs || isPartialTask || isPartialProgress;
}
/** A finished, successful todo card that completed tasks strikes them through once. */
export function toolCardStrikes(toolName, card) {
return (toolName === "todo" &&
card.executionStarted &&
!card.isPartial &&
card.result !== undefined &&
!card.result.isError &&
hasCompletedTodoTasks(card.result.details));
}
/** A tool card's spinner and todo-strike timers and the frame they advance. */
export class ToolExecutionAnimation {
#host;
#spinner;
#strike;
#frame;
constructor(host) {
this.#host = host;
}
get frame() {
return this.#frame;
}
get running() {
return this.#spinner !== undefined || this.#strike !== undefined;
}
spin(on) {
if (on)
this.#startSpinner();
else
this.#stopSpinner();
}
strike(on) {
if (!on) {
this.#stopStrike();
return;
}
if (this.#strike)
return;
this.#frame = 0;
this.#strike = setInterval(() => {
const next = (this.#frame ?? 0) + 1;
if (next > TODO_STRIKE_TOTAL_FRAMES) {
this.#stopStrike();
return;
}
this.#frame = next;
this.#host.invalidate();
this.#host.redraw();
}, TODO_STRIKE_FRAME_INTERVAL_MS);
this.#strike.unref?.();
}
stop() {
this.#stopSpinner();
this.#stopStrike();
}
#startSpinner() {
if (this.#spinner)
return;
this.#spinner = setInterval(() => {
this.#frame = ((this.#frame ?? -1) + 1) % 10;
this.#host.invalidate();
this.#host.redraw();
}, PENDING_RENDER_FRAME_INTERVAL_MS);
this.#spinner.unref?.();
}
#stopSpinner() {
if (!this.#spinner)
return;
clearInterval(this.#spinner);
this.#spinner = undefined;
if (!this.#strike)
this.#frame = undefined;
this.#host.invalidate();
}
#stopStrike() {
if (this.#strike) {
clearInterval(this.#strike);
this.#strike = undefined;
}
if (!this.#spinner && this.#frame !== undefined) {
this.#frame = undefined;
this.#host.invalidate();
this.#host.redraw();
}
}
}
//# sourceMappingURL=tool-execution-animation.js.map