@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
126 lines • 4.89 kB
JavaScript
export class TuiSessionControlHost {
constructor(context) {
this.editorRevision = 0;
this.hadDraft = false;
this.submitting = false;
this.openTickets = 0;
this.bufferedElsewhere = false;
this.registering = Promise.resolve();
this.context = context;
}
/**
* One registration at a time: one still opening when another starts (a retiring extension
* generation's, still in flight across a reload, and the new generation's) would otherwise finish
* last and replace `active`, leaving the other endpoint serving with no host tracking it (senpi#3246).
*/
register(options) {
const next = this.registering.then(() => this.registerNow(options));
this.registering = next.catch(() => undefined);
return next;
}
async registerNow(options) {
// A previous endpoint whose teardown failed is gone from this host either way: the failure is
// reported, and the new registration still gets its endpoint.
await this.disposeActive().catch((error) => this.context().surface.notice(`control endpoint teardown failed: ${error instanceof Error ? error.message : String(error)}`));
const { registerSessionControlEndpoint } = await import("./session-control-endpoint.js");
const outcome = await registerSessionControlEndpoint({ ...this.context(), editorRevision: () => this.editorRevision }, options);
if (!("endpoint" in outcome))
return outcome.registration;
const endpoint = outcome.endpoint;
this.active = endpoint;
return {
...outcome.registration,
// Its owner disposing it (a retired extension generation, senpi#3246) also takes it off this host,
// but only once its teardown settled: until then the next registration's `disposeActive` awaits
// it, so the teardown's header-only discard never removes the file the next endpoint serves.
dispose: async () => {
await endpoint.dispose();
if (this.active === endpoint)
this.active = undefined;
},
};
}
/** Wraps the editor's callbacks; call after both are assigned. `hasDraft` reads the composer now. */
attachEditor(editor, hasDraft) {
const change = editor.onChange;
editor.onChange = (text) => {
change?.(text);
this.editorChanged(hasDraft());
};
const submit = editor.onSubmit;
editor.onSubmit = (text, ...details) => {
this.submitting = true;
queueMicrotask(() => {
this.submitting = false;
});
const ticket = this.openTicket();
this.handoff = ticket;
try {
submit?.(text, ...details);
}
finally {
const claimed = this.handoff !== ticket;
this.handoff = undefined;
if (!claimed)
ticket.release();
}
};
}
/** Called synchronously by a submit branch whose input still has to reach the runtime. */
claimHandoff() {
const ticket = this.handoff;
this.handoff = undefined;
return ticket;
}
submissionInFlight() {
return this.openTickets > 0 || this.bufferedElsewhere;
}
/** Input the TUI buffers outside the main loop (the compaction queue) holds admission the same way. */
noteBufferedElsewhere(held) {
const released = this.bufferedElsewhere && !held;
this.bufferedElsewhere = held;
if (released)
this.wakeIfUnheld();
}
questionsChanged() {
this.active?.noteQuestions();
}
async disposeActive() {
const active = this.active;
this.active = undefined;
await active?.dispose();
}
openTicket() {
this.openTickets += 1;
let released = false;
return {
release: () => {
if (released)
return;
released = true;
this.openTickets -= 1;
this.wakeIfUnheld();
},
};
}
wakeIfUnheld() {
// Deferred: a prompt reports its disposition just before the run marks itself active.
queueMicrotask(() => {
if (!this.submissionInFlight())
this.active?.wake("submission");
});
}
editorChanged(hasDraft) {
this.editorRevision += 1;
const cleared = this.hadDraft && !hasDraft;
this.hadDraft = hasDraft;
this.active?.noteState();
if (!cleared)
return;
queueMicrotask(() => {
if (!this.submitting)
this.active?.wake("draft_cleared");
});
}
}
//# sourceMappingURL=session-control-host.js.map