@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
68 lines • 3.56 kB
JavaScript
/**
* `pi.session` as one session binds it: admission and the ledger come from the session's own
* `ExternalAdmission`, the header write from its `SessionManager`, and the control endpoint from
* whichever host the running mode installed. Only the interactive TUI installs one; every other
* mode answers `unsupported` and registers nothing.
*/
import { RELOCATION_NOT_OFFERED } from "./session-relocation.js";
export function createSessionControlActions(deps) {
const owned = new Set();
let retired = false;
return {
registerControlEndpoint: async (options) => {
const host = deps.host();
if (host === undefined)
return { status: "unsupported", reason: "unsupported_mode" };
// The endpoint may run a pass before this registration returns (its first `inbox` wake): a retired
// generation's `drain` is never called, whatever the timing.
const registration = await host.register({
...options,
drain: async (event) => (retired ? undefined : options.drain(event)),
});
if (registration.status !== "registered")
return registration;
// Registered while this generation was being retired: it is released at once, never left serving.
if (retired) {
const teardown = await registration.dispose().then(() => "", (error) => `; its teardown failed: ${error instanceof Error ? error.message : String(error)}`);
return { status: "failed", reason: `stale extension generation after reload${teardown}` };
}
owned.add(registration);
return {
...registration,
dispose: async () => {
owned.delete(registration);
await registration.dispose();
},
};
},
retire: async () => {
retired = true;
const registrations = [...owned];
owned.clear();
await Promise.all(registrations.map((registration) => registration.dispose()));
},
admissionGate: () => deps.admission.gate(),
admitExternalMessage: (input) => deps.admission.admit(input),
listAdmittedDeliveries: () => deps.admission.list(),
persistHeaderNow: () => deps.sessionManager.persistHeaderNow(),
relocationOffered: () => deps.relocationHost()?.offered() ?? false,
relocationTargets: async (signal) => (await deps.relocationHost()?.targets(signal)) ?? RELOCATION_NOT_OFFERED,
requestRelocation: async (request, signal) => (await deps.relocationHost()?.request(request, signal)) ?? RELOCATION_NOT_OFFERED,
};
}
export function unboundSessionControlActions() {
const notBound = () => {
throw new Error("Extension runtime not initialized. pi.session cannot be used during extension loading.");
};
return {
registerControlEndpoint: () => Promise.reject(new Error("Extension runtime not initialized")),
admissionGate: notBound,
admitExternalMessage: notBound,
listAdmittedDeliveries: notBound,
persistHeaderNow: () => Promise.reject(new Error("Extension runtime not initialized")),
relocationOffered: notBound,
relocationTargets: () => Promise.reject(new Error("Extension runtime not initialized")),
requestRelocation: () => Promise.reject(new Error("Extension runtime not initialized")),
};
}
//# sourceMappingURL=session-control-actions.js.map