@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
95 lines • 3.69 kB
JavaScript
import { JobWidgetRegistry } from "./registry.js";
const sessions = new Map();
const sessionListeners = new Set();
let hostResolver;
/**
* Hosts that keep jobs across a reload hear when a session's widgets start or stop: a stop leaves
* their handles inert (drop them), a start after one is when to offer their running jobs again.
*/
export function onJobWidgetSessionChange(listener) {
sessionListeners.add(listener);
return () => {
sessionListeners.delete(listener);
};
}
function notifySessionChange(sessionId, open) {
for (const listener of [...sessionListeners])
listener(sessionId, open);
}
/** Starts (or re-binds after a reload, with the settings read again) the job widgets of one session. */
export function openJobWidgetSession(sessionId, options) {
const existing = sessions.get(sessionId);
if (existing) {
existing.author = options.author;
existing.publish = options.publish;
existing.registry.configure({
limits: { authorAfterMs: options.authorAfterMs },
...(options.obfuscate === undefined ? {} : { obfuscate: options.obfuscate }),
});
existing.registry.republish();
return;
}
const slot = {
author: options.author,
publish: options.publish,
registry: new JobWidgetRegistry({
sessionId,
author: (request, signal) => slot.author(request, signal),
publish: (state) => slot.publish?.(state),
limits: { authorAfterMs: options.authorAfterMs },
...(options.obfuscate === undefined ? {} : { obfuscate: options.obfuscate }),
}),
};
sessions.set(sessionId, slot);
notifySessionChange(sessionId, true);
}
/**
* Sends the session's current state again for a listener that just subscribed (an RPC client that
* declared `extension_events`), when there is a job to show; an empty state tells it nothing.
*/
export function resendJobWidgetState(sessionId) {
const registry = sessions.get(sessionId)?.registry;
if (registry && registry.snapshot().jobs.length > 0)
registry.republish();
}
/** Holds a session's state across a reload: jobs keep their taps, nothing is sent until re-bound. */
export function parkJobWidgetSession(sessionId) {
const slot = sessions.get(sessionId);
if (slot)
slot.publish = undefined;
}
export function closeJobWidgetSession(sessionId) {
const slot = sessions.get(sessionId);
if (!slot)
return;
slot.registry.dispose();
sessions.delete(sessionId);
notifySessionChange(sessionId, false);
}
/**
* Offers a job to the session's widgets. Returns `undefined` when the session has no widgets (the
* setting is off, or the job-widgets extension is not loaded): the host then taps nothing.
*/
export function registerJobWidgetSource(registration) {
const { sessionId, ...input } = registration;
return sessions.get(sessionId)?.registry.add(input);
}
/** The session's current widget state (views redacted), or `undefined` when the session has no widgets. */
export function getJobWidgetState(sessionId) {
return sessions.get(sessionId)?.registry.state();
}
/**
* Installs the host's model route for every session, ahead of the session's own model and behind an
* explicit `jobWidgets.model` setting. Returns a function that removes it again (when still current).
*/
export function setJobWidgetAuthorResolver(resolver) {
hostResolver = resolver;
return () => {
if (hostResolver === resolver)
hostResolver = undefined;
};
}
export function jobWidgetHostResolver() {
return hostResolver;
}
//# sourceMappingURL=session-host.js.map