UNPKG

@code-yeongyu/senpi

Version:

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

154 lines • 5.32 kB
import { parseStreamingJson } from "@earendil-works/pi-ai"; import { DEFAULT_SMOOTH_FPS, MAX_SMOOTH_FPS, MIN_SMOOTH_FPS, nextStep } from "./streaming-reveal.js"; export const MIN_TOOL_ARGS_PARSE_DELTA = 64; function isHighSurrogate(code) { return code >= 0xd800 && code <= 0xdbff; } function isLowSurrogate(code) { return code >= 0xdc00 && code <= 0xdfff; } function advancePastSurrogateBoundary(text, end) { if (end <= 0 || end >= text.length) return end; return isHighSurrogate(text.charCodeAt(end - 1)) && isLowSurrogate(text.charCodeAt(end)) ? end + 1 : end; } export class ToolArgsRevealController { #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, partialJson) { if (!this.#getSmoothStreaming() || partialJson.length === 0) { this.finish(id); return false; } const existing = this.#states.get(id); if (existing === undefined || existing.component !== component || !partialJson.startsWith(existing.target)) { component.updateArgs(parseStreamingJson(partialJson)); this.#states.set(id, { component, target: partialJson, revealed: partialJson.length, rendered: partialJson.length, lastTickAt: performance.now(), }); this.#syncTimer(); return true; } const wasCaughtUp = existing.revealed >= existing.target.length; existing.target = partialJson; if (wasCaughtUp && existing.revealed < partialJson.length) { existing.lastTickAt = performance.now(); } this.#syncTimer(); return true; } flush(id, exactArgs) { const state = this.#states.get(id); if (state === undefined) return false; state.component.updateArgs(exactArgs); this.#states.delete(id); this.#syncTimer(); return true; } flushAll() { for (const state of this.#states.values()) { state.component.updateArgs(parseStreamingJson(state.target)); } this.#states.clear(); this.#stopTimer(); } finish(id) { if (!this.#states.delete(id)) return; this.#syncTimer(); } refresh() { if (!this.#getSmoothStreaming()) { this.flushAll(); return; } if (!this.#hasBacklog()) { this.#stopTimer(); return; } this.#startTimer(true); } stop() { this.#stopTimer(); this.#states.clear(); } #syncTimer() { if (!this.#hasBacklog()) { this.#stopTimer(); return; } this.#startTimer(false); } #hasBacklog() { for (const state of this.#states.values()) { if (state.revealed < state.target.length) return true; } return 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.target.length) 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 backlog = state.target.length - state.revealed; if (backlog <= 0) continue; const step = Math.max(1, nextStep(backlog, now - state.lastTickAt)); state.revealed = Math.min(state.target.length, state.revealed + step); state.lastTickAt = now; if (state.revealed - state.rendered < MIN_TOOL_ARGS_PARSE_DELTA) continue; const renderEnd = advancePastSurrogateBoundary(state.target, Math.min(state.target.length, state.rendered + MIN_TOOL_ARGS_PARSE_DELTA)); state.component.updateArgs(parseStreamingJson(state.target.slice(0, renderEnd))); state.rendered = renderEnd; rendered = true; } if (rendered) this.#requestRender(); this.#syncTimer(); } } //# sourceMappingURL=tool-args-reveal.js.map