UNPKG

@code-yeongyu/senpi

Version:

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

117 lines • 4.84 kB
import { CompactComponentRoots } from "./compact-component-roots.js"; import { CompactValues } from "./compact-values.js"; /** The host-origin records that change lifecycle, run state or dialogs; each folds at its own lane sequence. */ export const COMPONENT_RECORDS = new Set([ "agent_start", "agent_end", "session_lifecycle", "extension_ui_request", "question_updated", "question_resolved", "ui_request_settled", ]); const TARGETED = new Set(["select", "confirm", "input", "editor"]); const field = (record, key) => Reflect.get(record, key); const deadline = (record) => { const value = field(record, "deadlineAtMs"); return typeof value === "number" ? { deadlineAtMs: value } : {}; }; /** * Run state, the latest lifecycle description and the open dialogs of one epoch (S03b-3b, RULING-26 Q4): current * values only, never a log of notifications. Each change replaces a persistent root; a cut pins the roots it froze. */ export class CompactCurrentComponents { constructor(store) { this.runState = "idle"; this.dialogs = { header: undefined, ref: undefined }; this.open = new Map(); this.opened = 0; this.store = store; this.values = new CompactValues(store); this.dialogRoots = new CompactComponentRoots(store, () => []); } get openBroadcast() { let count = 0; for (const { dialog } of this.open.values()) if (dialog.audience === "broadcast") count++; return count; } /** `record` is JSON-normal (the acceptance unit's producer boundary). */ apply(record) { switch (field(record, "type")) { case "agent_start": case "agent_end": this.runState = field(record, "type") === "agent_start" ? "running" : "idle"; return; case "session_lifecycle": { const next = this.values.own(record); if (this.lifecycle) this.store.adjust(this.lifecycle.owner, -1, 0); this.lifecycle = next; return; } case "extension_ui_request": { const id = field(record, "id"); const method = String(field(record, "method")); if (typeof id !== "string" || (method !== "question" && !TARGETED.has(method)) || this.open.has(id)) return; const broadcast = method === "question"; const dialog = { id, audience: broadcast ? "broadcast" : "targeted", method, ...deadline(record), ...(broadcast ? { request: record } : {}), }; return this.put(id, String(this.opened++).padStart(16, "0"), dialog); } case "question_updated": { const id = String(field(record, "id")); const open = this.open.get(id); if (open) this.put(id, open.order, { ...open.dialog, ...deadline(record) }); return; } default: { const id = String(field(record, "id") ?? field(record, "uiRequestId")); const open = this.open.get(id); if (!open) return; this.dialogs = this.dialogRoots.set(this.dialogs, open.order, undefined); this.open.delete(id); } } } freeze() { for (const ref of [this.lifecycle, this.dialogs.ref]) if (ref) this.store.adjust(ref.owner, 0, 1); const broadcast = this.openBroadcast; return { runState: this.runState, lifecycle: this.lifecycle, dialogs: this.dialogs.ref, dialogCounts: { broadcast, targeted: this.open.size - broadcast }, }; } release(frozen) { for (const ref of [frozen.lifecycle, frozen.dialogs]) if (ref) this.store.adjust(ref.owner, 0, -1); } /** The cut's components; dialogs in the order they opened. Called under the cut's operation lease. */ async read(frozen, valid) { const lifecycle = frozen.lifecycle ? await this.values.read(frozen.lifecycle, valid) : null; valid(); const dialogs = []; for await (const [, dialog] of this.dialogRoots.entries(frozen.dialogs, valid)) dialogs.push(dialog); return { runState: frozen.runState, lifecycle, dialogs, dialogCounts: frozen.dialogCounts }; } put(id, order, dialog) { this.dialogs = this.dialogRoots.set(this.dialogs, order, dialog); this.open.set(id, { order, dialog }); } } //# sourceMappingURL=compact-current-components.js.map