@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
57 lines • 2.25 kB
JavaScript
export const DELTA_WINDOW_MS = 2_000;
export const SURFACE_WINDOW_MS = 5_000;
const DELTA_KINDS = new Set(["message_update", "tool_execution_update", "bash_execution_update"]);
const SURFACE_KINDS = new Set(["loaded_surfaces_changed", "commands_changed"]);
const LAST_TEXT_LIMIT = 60;
export class RpcMonitorCoalescer {
#windows = new Map();
#flush;
constructor(flush) {
this.#flush = flush;
}
/** Takes a coalesced kind into its window; false for every other kind (the caller renders it). */
accept(sessionKey, label, record) {
const type = String(record.type);
const delta = DELTA_KINDS.has(type);
if (!delta && !SURFACE_KINDS.has(type))
return false;
const key = delta ? `${sessionKey}#deltas` : `${sessionKey}#${type}`;
const open = this.#windows.get(key);
const last = delta ? lastText(record) : "";
if (open !== undefined) {
open.count += 1;
if (last !== "")
open.last = last;
return true;
}
const window = {
count: 1,
last,
timer: setTimeout(() => {
this.#windows.delete(key);
this.#flush(delta
? `rpc ${label()} streaming +${window.count} deltas${window.last === "" ? "" : ` (last: "${window.last}")`}`
: `rpc ${label()} ${type} x${window.count}`);
}, delta ? DELTA_WINDOW_MS : SURFACE_WINDOW_MS),
};
this.#windows.set(key, window);
return true;
}
dispose() {
for (const window of this.#windows.values())
clearTimeout(window.timer);
this.#windows.clear();
}
}
function lastText(record) {
const event = record.assistantMessageEvent;
// Only answer text: a thinking delta is not what the session is saying.
const text = event?.type === "text_delta" && typeof event.delta === "string"
? event.delta
: typeof record.output === "string"
? record.output
: "";
const flat = text.replace(/\s+/g, " ").trim();
return flat.length > LAST_TEXT_LIMIT ? flat.slice(-LAST_TEXT_LIMIT) : flat;
}
//# sourceMappingURL=rpc-monitor-coalesce.js.map