@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
47 lines • 1.53 kB
JavaScript
/**
* The cursor-based event feed a control client `subscribe`s to: state changes, assistant reports,
* question changes and turn completions, numbered by one sequence. A client that reconnects with the
* last `seq` it saw is replayed what the bounded ring still holds after it.
*/
const FEED_RING_SIZE = 256;
export class ControlFeed {
constructor() {
this.seq = 0;
this.stateVersionValue = 0;
this.ring = [];
this.subscribers = new Map();
}
get stateVersion() {
return this.stateVersionValue;
}
publish(kind, data = {}) {
if (kind === "state")
this.stateVersionValue += 1;
const event = {
type: "session_control_event",
seq: ++this.seq,
kind,
data: kind === "state" ? { ...data, state_version: this.stateVersionValue } : data,
};
this.ring.push(event);
if (this.ring.length > FEED_RING_SIZE)
this.ring.shift();
for (const send of this.subscribers.values())
send(event);
}
subscribe(connection, cursor, send) {
this.subscribers.set(connection, send);
if (cursor !== undefined)
for (const event of this.ring)
if (event.seq > cursor)
send(event);
return this.seq;
}
unsubscribe(connection) {
this.subscribers.delete(connection);
}
clear() {
this.subscribers.clear();
}
}
//# sourceMappingURL=session-control-feed.js.map