@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
100 lines • 4.46 kB
JavaScript
import { isSessionArchived } from "../../core/session-archived.js";
import { readSessionSettled, settledFields } from "../../core/session-settled.js";
import { ConnectionRoles } from "../rpc/connection-roles.js";
import { SessionEventWriter } from "../rpc/session-event-writer.js";
import { subscribeSessionRecords } from "../rpc/session-record-stream.js";
import { sessionRuntimeView } from "../rpc/session-status-source.js";
export class ControlAttachments {
constructor(session, pendingQuestions, presenters) {
this.writer = new SessionEventWriter(() => { });
this.roles = new ConnectionRoles();
this.receipts = new Map();
this.attached = new Set();
this.session = session;
this.presenters = presenters;
this.writer.status.setSource({
sessionIds: () => [this.handle],
entryView: (id) => (id === this.handle ? this.entryView() : undefined),
view: (id) => id === this.handle
? { entry: this.entryView(), runtime: sessionRuntimeView(session, pendingQuestions()) }
: undefined,
});
// A permanent, never-attached connection keeps the writer off its stdio lane: records reach attached
// connections and the replay snapshot only, and nothing queues for a sink that writes nowhere.
this.writer.registerConnection(NULL_CONNECTION, { writeRaw: () => { }, waitForBackpressure: async () => { } });
this.stopRecords = subscribeSessionRecords(session, (record) => this.writer.enqueue(this.handle, record));
}
/** The routing handle records carry: this terminal's session id. */
get handle() {
return this.session.sessionId;
}
opened(connection) {
this.writer.registerConnection(keyOf(connection), connection.transport);
this.presenters?.opened(keyOf(connection), connection.transport);
}
closed(connection) {
const key = keyOf(connection);
if (this.attached.delete(key))
this.writer.status.publish(this.handle, "detach", "client");
this.roles.release(key);
this.presenters?.closed(key);
this.writer.unregisterConnection(key);
}
/** Drops this connection's attachment (`release_attachment`); false when it was not attached. */
detach(connection) {
const key = keyOf(connection);
if (!this.attached.delete(key))
return false;
this.presenters?.detach(key);
this.writer.detachConnectionFromSession(key, this.handle);
this.writer.status.publish(this.handle, "detach", "client");
return true;
}
/** Attaches the connection: it is replayed the in-flight snapshot, then receives the session's records. */
attach(connection) {
const key = keyOf(connection);
if (this.attached.has(key))
return false;
this.attached.add(key);
this.presenters?.attach(key);
this.writer.attachConnectionToSession(key, this.handle);
this.writer.status.publish(this.handle, "attach", "client");
return true;
}
isAttached(connection) {
return this.attached.has(keyOf(connection));
}
/** Runs a command with this connection as the writer's current one (its targeted records reach it). */
run(connection, task) {
return this.writer.withConnection(keyOf(connection), task);
}
respond(connection, answer) {
this.writer.enqueueControl(answer, keyOf(connection)).catch(() => connection.transport.close?.());
}
dispose() {
this.stopRecords();
}
entryView() {
const manager = this.session.sessionManager;
const path = this.session.sessionFile;
const name = manager.getSessionName();
return {
state: "open",
kind: "interactive",
cwd: manager.getCwd(),
attachments: 1 + this.attached.size,
durableSessionId: this.session.sessionId,
...(path !== undefined ? { sessionPath: path } : {}),
...(name !== undefined ? { name } : {}),
archived: isSessionArchived(manager),
settled: settledFields(readSessionSettled(manager)),
context: {},
};
}
}
const NULL_CONNECTION = "tui-null";
/** The writer's id for one control connection. */
export function keyOf(connection) {
return `tui-${connection.id}`;
}
//# sourceMappingURL=session-control-attach.js.map