UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

155 lines • 5.39 kB
import { BlockUnitCounter, DEFAULT_SMOOTH_FPS, MAX_SMOOTH_FPS, MIN_SMOOTH_FPS, nextStep } from "./streaming-reveal.js"; function firstText(result) { const block = result.content[0]; return block?.type === "text" ? block.text : undefined; } function displayResult(state, revealed = state.revealed) { const first = state.target.content[0]; if (first?.type !== "text") throw new Error("Expected a text tool result"); return { content: [{ ...first, text: state.unitCounter.slice(0, state.text, revealed) }, ...state.target.content.slice(1)], details: state.target.details, isError: false, }; } export class ToolResultRevealController { #getSmoothStreaming; #getSmoothStreamingFps; #requestRender; #states = new Map(); #timer; #timerFps; constructor(options) { this.#getSmoothStreaming = options.getSmoothStreaming; this.#getSmoothStreamingFps = options.getSmoothStreamingFps; this.#requestRender = options.requestRender; } update(id, component, partialResult) { const text = firstText(partialResult); if (!this.#getSmoothStreaming() || text === undefined) { this.#discard(id); return false; } const existing = this.#states.get(id); if (existing === undefined || existing.component !== component || !text.startsWith(existing.text)) { const state = { component, target: partialResult, text, revealed: 0, lastTickAt: performance.now(), unitCounter: new BlockUnitCounter(), }; state.revealed = state.unitCounter.count(0, text); component.updateResult(displayResult(state), true); this.#states.set(id, state); this.#syncTimer(); return true; } const wasCaughtUp = existing.revealed >= existing.unitCounter.count(0, existing.text); existing.target = partialResult; existing.text = text; const total = existing.unitCounter.count(0, text); if (wasCaughtUp && existing.revealed < total) existing.lastTickAt = performance.now(); this.#syncTimer(); return true; } finish(id) { const state = this.#states.get(id); if (state === undefined) return; state.component.updateResult(displayResult(state, state.unitCounter.count(0, state.text)), true); this.#states.delete(id); this.#syncTimer(); } refresh() { if (!this.#getSmoothStreaming()) { this.#flushAll(); return; } if (!this.#hasBacklog()) { this.#stopTimer(); return; } this.#startTimer(true); } stop() { this.#stopTimer(); this.#states.clear(); } #discard(id) { if (!this.#states.delete(id)) return; this.#syncTimer(); } #flushAll() { for (const state of this.#states.values()) { state.component.updateResult(displayResult(state, state.unitCounter.count(0, state.text)), true); } this.#states.clear(); this.#stopTimer(); } #hasBacklog() { for (const state of this.#states.values()) { if (state.revealed < state.unitCounter.count(0, state.text)) return true; } return false; } #syncTimer() { if (!this.#hasBacklog()) { this.#stopTimer(); return; } this.#startTimer(false); } #startTimer(restart) { const configuredFps = this.#getSmoothStreamingFps(); const fps = Number.isFinite(configuredFps) ? Math.min(MAX_SMOOTH_FPS, Math.max(MIN_SMOOTH_FPS, configuredFps)) : DEFAULT_SMOOTH_FPS; if (!restart && this.#timer !== undefined && this.#timerFps === fps) return; this.#stopTimer(); const now = performance.now(); for (const state of this.#states.values()) { if (state.revealed < state.unitCounter.count(0, state.text)) state.lastTickAt = now; } const timer = setInterval(() => this.#tick(), 1000 / fps); timer.unref(); this.#timer = timer; this.#timerFps = fps; } #stopTimer() { if (this.#timer !== undefined) clearInterval(this.#timer); this.#timer = undefined; this.#timerFps = undefined; } #tick() { if (!this.#getSmoothStreaming()) { this.#flushAll(); this.#requestRender(); return; } const now = performance.now(); let rendered = false; for (const state of this.#states.values()) { const total = state.unitCounter.count(0, state.text); const backlog = total - state.revealed; if (backlog <= 0) continue; state.revealed = Math.min(total, state.revealed + nextStep(backlog, now - state.lastTickAt)); state.lastTickAt = now; state.component.updateResult(displayResult(state), true); rendered = true; } if (rendered) this.#requestRender(); this.#syncTimer(); } } //# sourceMappingURL=tool-result-reveal.js.map