UNPKG

@code-yeongyu/senpi

Version:

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

138 lines • 6.63 kB
const UNKNOWN_JSON_LIMIT = 160; export class RpcMonitorRenderer { #sessions = new Map(); #namedThisBatch = new Set(); /** Remembers the sessions a subscription answer listed; returns the arm-time RESULT rows. */ seed(socket, snapshot) { return snapshot.map((row) => { const view = this.#view(socket, row); view.status = String(row.status); const model = modelId(row.model); const attachments = typeof row.attachments === "number" ? row.attachments : 0; return `${view.label} ${view.status} (${attachments} attached${model === undefined ? "" : `, model ${model}`})`; }); } /** Drops every session an endpoint reported; returns how many. */ forgetSocket(socket) { let count = 0; for (const key of [...this.#sessions.keys()]) if (key.startsWith(`${socket}#`)) { this.#sessions.delete(key); count++; } return count; } /** Every session this renderer knows, with its last status: what a checkpoint records. */ statuses() { return new Map([...this.#sessions].map(([key, view]) => [key, view.status])); } label(socket, record) { const view = this.#view(socket, record); const key = sessionKey(socket, record); if (this.#namedThisBatch.has(key)) return view.label; if (this.#namedThisBatch.size === 0) queueMicrotask(() => this.#namedThisBatch.clear()); this.#namedThisBatch.add(key); return `${view.label} (${String(record.sessionId)})`; } /** One line for a non-coalesced record, or undefined when it changes nothing worth a line. */ render(socket, record) { switch (record.type) { case "session_status": return this.#status(socket, record); case "effect_status": { const detail = record.disposition ?? reasonOf(record.outcome); return `rpc ${this.label(socket, record)} effect ${String(record.kind)} ${tail(String(record.effectId))} ${String(record.state)}${detail === undefined ? "" : ` (${String(detail)})`}`; } case "agent_end": return `rpc ${this.label(socket, record)} agent_end${record.aborted === true ? ` aborted(${String(record.abortSource ?? "user")})` : ""}`; case "model_changed": return `rpc ${this.label(socket, record)} model_changed -> ${modelId(record.model) ?? "?"}${record.source === undefined ? "" : ` (${String(record.source)})`}`; case "queue_update": return `rpc ${this.label(socket, record)} queue_update steering=${count(record.steering)} followUp=${count(record.followUp)}`; case "session_replaced": return `rpc ${this.label(socket, record)} session_replaced -> ${String(record.durableSessionId ?? record.newSessionId ?? "?")}`; case "session_closed": return `rpc ${this.label(socket, record)} session_closed ${String(record.reason ?? "")}`.trimEnd(); case "extension_event": return `rpc ${this.label(socket, record)} extension_event ${String(record.name ?? record.event ?? "")}`.trimEnd(); case "entry_appended": return `rpc ${this.label(socket, record)} entry_appended ${entryType(record)}`; case "tool_execution_start": case "tool_execution_end": return `rpc ${this.label(socket, record)} tool ${String(record.toolName ?? "?")} ${record.type === "tool_execution_start" ? "started" : "ended"}`; case "turn_end": case "turn_start": case "agent_start": case "agent_idle": case "agent_settled": return `rpc ${this.label(socket, record)} ${record.type}`; default: { const { type: _type, sessionId: _sessionId, ...rest } = record; const json = JSON.stringify(rest); const body = json.length > UNKNOWN_JSON_LIMIT ? `${json.slice(0, UNKNOWN_JSON_LIMIT)}...` : json; return `rpc ${this.label(socket, record)} ${String(record.type)} ${body}`; } } } #status(socket, record) { const view = this.#view(socket, record); const to = String(record.status); const from = view.status; view.status = to; if (from === to) return undefined; const change = record.lastChange; const why = change === undefined ? "" : ` (${String(change.cause)}, by ${String(change.by)})`; return `rpc ${this.label(socket, record)} status ${from} -> ${to}${why} seq=${String(record.seq)}`; } #view(socket, record) { const key = sessionKey(socket, record); let view = this.#sessions.get(key); if (view === undefined) { view = { label: labelOf(record), status: "unknown" }; this.#sessions.set(key, view); } else if (typeof record.name === "string" || contextTask(record) !== undefined) view.label = labelOf(record); return view; } } export function sessionKey(socket, record) { return `${socket}#${String(record.sessionId)}`; } /** How a line about a session nobody watched names it: the first 8 characters of its key's session id. */ export function detachedLabel(key) { return key.slice(key.lastIndexOf("#") + 1).slice(0, 8); } function labelOf(record) { const task = contextTask(record); if (task !== undefined) return task; if (typeof record.name === "string" && record.name !== "") return record.name; const durable = typeof record.durableSessionId === "string" ? record.durableSessionId : String(record.sessionId); return durable.slice(0, 8); } function contextTask(record) { const context = record.context; return typeof context?.task_id === "string" ? context.task_id : undefined; } function modelId(model) { return typeof model === "object" && model !== null && "id" in model ? String(model.id) : undefined; } function reasonOf(outcome) { return typeof outcome === "object" && outcome !== null && "reason" in outcome ? String(outcome.reason) : undefined; } function count(value) { return Array.isArray(value) ? value.length : typeof value === "number" ? value : 0; } function tail(id) { return id.length > 8 ? id.slice(-8) : id; } function entryType(record) { const entry = record.entry; return String(entry?.type ?? record.entryType ?? "?"); } //# sourceMappingURL=rpc-monitor-render.js.map