@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
90 lines • 4.11 kB
JavaScript
/**
* The job-widgets builtin: per session it opens the widget registry hosts feed (the terminal's
* background sessions, omo's task jobs), binds the model author, and sends `job_widget_state` to
* in-process listeners and to RPC clients that advertise `extension_events`. Default on, no setup;
* `jobWidgets.enabled: false` opens nothing, so hosts tap nothing and no author is ever called.
* In the interactive TUI the same in-process event drives the widget area above the editor. An RPC
* client that starts receiving extension events gets the current state once (when a job shows).
*/
import { EXTENSION_RPC_SUBSCRIBED_CHANNEL } from "../../../event-bus.js";
import { SettingsManager } from "../../../settings-manager.js";
import { isStaleExtensionContextError } from "../goal/stale-context.js";
import { JOB_WIDGET_STATE_EVENT } from "../monitor-state-event.js";
import { secretOutputObfuscator } from "./author-redact.js";
import { createSessionJobWidgetAuthor } from "./session-author.js";
import { closeJobWidgetSession, openJobWidgetSession, parkJobWidgetSession, resendJobWidgetState, } from "./session-host.js";
import { JobWidgetArea } from "./tui-widget.js";
function publishState(pi, state) {
try {
pi.events?.emit(JOB_WIDGET_STATE_EVENT, state);
pi.rpc?.emit(JOB_WIDGET_STATE_EVENT, state);
}
catch (error) {
// A retired generation cannot reach a listener; the next one re-sends the state when it binds.
if (!isStaleExtensionContextError(error))
throw error;
}
}
export default function jobWidgetsExtension(pi) {
let ctx;
let sessionId;
let stopArea;
let stopResend;
const showInTui = (startCtx, id) => {
stopArea?.();
stopArea = undefined;
if (startCtx.mode !== "tui" || !startCtx.hasUI || !pi.events)
return;
const area = new JobWidgetArea({ setWidget: (key, content) => startCtx.ui.setWidget(key, content) });
const off = pi.events.on(JOB_WIDGET_STATE_EVENT, (data) => {
const state = data;
if (state.sessionId === id)
area.update(state);
});
stopArea = () => {
off();
area.dispose();
};
};
pi.on("session_start", (_event, startCtx) => {
ctx = startCtx;
sessionId = startCtx.sessionManager?.getSessionId?.();
if (sessionId === undefined)
return;
const settingsManager = SettingsManager.create(startCtx.cwd, startCtx.agentDir);
const settings = settingsManager.getJobWidgetsSettings();
if (!settings.enabled) {
closeJobWidgetSession(sessionId);
return;
}
showInTui(startCtx, sessionId);
const id = sessionId;
stopResend?.();
stopResend = pi.events?.on(EXTENSION_RPC_SUBSCRIBED_CHANNEL, () => resendJobWidgetState(id));
openJobWidgetSession(sessionId, {
authorAfterMs: settings.authorAfterMs,
author: createSessionJobWidgetAuthor(() => ctx, settings.model),
publish: (state) => publishState(pi, state),
...(settingsManager.getBashRedactSecretsInOutput() ? { obfuscate: secretOutputObfuscator } : {}),
});
});
pi.on("model_select", (event, selectCtx) => {
// Copy the descriptors, not the values: a spread reads every guarded getter now and freezes
// them past the stale-instance checks (see ExtensionRunner.createCommandContext).
const next = Object.defineProperties({}, Object.getOwnPropertyDescriptors(selectCtx));
ctx = Object.defineProperty(next, "model", { value: event.model, enumerable: true, configurable: true });
});
pi.on("session_shutdown", (event) => {
stopArea?.();
stopArea = undefined;
stopResend?.();
stopResend = undefined;
if (sessionId === undefined)
return;
if (event.reason === "reload")
parkJobWidgetSession(sessionId);
else
closeJobWidgetSession(sessionId);
});
}
//# sourceMappingURL=extension.js.map