@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
113 lines • 3.53 kB
JavaScript
export const LOOP_STATUS_KEY = "loop";
export const LOOP_STATUS_TICK_INTERVAL_MS = 1000;
function isArmed(entry) {
return entry.phase !== "ended";
}
function formatDuration(ms) {
const totalSeconds = Math.max(0, Math.floor(ms / 1000));
if (totalSeconds < 60) {
return `${totalSeconds}s`;
}
const minutes = Math.floor(totalSeconds / 60);
if (minutes < 60) {
const seconds = totalSeconds % 60;
return seconds > 0 ? `${minutes}m${seconds}s` : `${minutes}m`;
}
const hours = Math.floor(minutes / 60);
if (hours < 24) {
const mins = minutes % 60;
return mins > 0 ? `${hours}h${mins}m` : `${hours}h`;
}
const days = Math.floor(hours / 24);
const hrs = hours % 24;
return hrs > 0 ? `${days}d${hrs}h` : `${days}d`;
}
function nextDueAt(entry) {
if (entry.phase === "suspended" || entry.phase === "ended")
return undefined;
if (entry.kind === "fixed") {
return entry.nextFireAt;
}
return entry.pendingWakeup?.dueAt;
}
export function formatLoopStatus(state, nowMs) {
const armed = Object.values(state.entries).filter(isArmed);
if (armed.length === 0) {
return undefined;
}
const paused = armed.some((entry) => entry.phase === "suspended");
if (paused) {
return "Loop paused - /loop resume or /loop stop";
}
let nearest;
for (const entry of armed) {
if (entry.phase === "suspended")
continue;
const dueAt = nextDueAt(entry);
if (dueAt === undefined)
continue;
if (nearest === undefined || dueAt < nearest.dueAt) {
nearest = { entry, dueAt };
}
}
if (nearest === undefined) {
return "Loop active - /loop stop";
}
const remainingMs = Math.max(0, nearest.dueAt - nowMs);
const mode = nearest.entry.kind === "fixed" ? "fixed" : "dynamic";
return `Loop (${mode}): next in ${formatDuration(remainingMs)} - /loop stop`;
}
export function formatNoopFold(noopStreak) {
if (noopStreak < 2) {
return "";
}
return `↻ ${noopStreak} loop ticks with no actionable change`;
}
export class LoopStatusTicker {
constructor(options) {
this.render = options.render;
this.now = options.now ?? Date.now;
}
get running() {
return this.timeoutId !== undefined;
}
sync(state) {
this.state = state;
this.lastRendered = undefined;
this.tick();
this.scheduleNext();
}
dispose() {
if (this.timeoutId !== undefined) {
clearTimeout(this.timeoutId);
this.timeoutId = undefined;
}
this.state = undefined;
this.lastRendered = undefined;
this.render(LOOP_STATUS_KEY, undefined);
}
scheduleNext() {
if (this.timeoutId !== undefined) {
clearTimeout(this.timeoutId);
}
const handle = setTimeout(() => {
this.timeoutId = undefined;
this.tick();
if (this.state !== undefined) {
this.scheduleNext();
}
}, LOOP_STATUS_TICK_INTERVAL_MS);
handle.unref();
this.timeoutId = handle;
}
tick() {
if (this.state === undefined)
return;
const text = formatLoopStatus(this.state, this.now());
if (text === this.lastRendered)
return;
this.lastRendered = text;
this.render(LOOP_STATUS_KEY, text);
}
}
//# sourceMappingURL=status.js.map