@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
104 lines • 4.7 kB
JavaScript
import { SessionExtensionUiRequests } from "../../modes/rpc/session-extension-ui-requests.js";
import { noOpUIContext } from "../extensions/runner.js";
import { SecretInputUnavailableError } from "../extensions/secret-input.js";
/**
* The UI of a runtime that has no session (senpi#5228). One presenter at a time answers the runtime's extension
* UI requests; with none attached, every request settles as an unbound UI does.
*/
export class RuntimeUiBroker {
#presenter;
get presenter() {
return this.#presenter;
}
/** Attach the presenter that answers this runtime's UI requests; the returned function detaches it. */
attach(presenter) {
if (this.#presenter !== undefined)
throw new Error("A presenter is already attached to this runtime's UI broker");
this.#presenter = presenter;
return () => {
if (this.#presenter === presenter)
this.#presenter = undefined;
};
}
}
function dialogCall(opts) {
if (opts?.toolCallId === undefined)
return {};
return {
toolCallId: opts.toolCallId,
...(opts.parentToolCallId === undefined ? {} : { parentToolCallId: opts.parentToolCallId }),
};
}
function answeredValue(response) {
return "cancelled" in response && response.cancelled ? undefined : "value" in response ? response.value : undefined;
}
/**
* A presenter that asks over the RPC wire with the same `extension_ui_request` frames, timeouts and abort
* handling as a session's RPC UI, for a connection that serves a runtime with no session.
*/
export function createRpcUiPresenter(output) {
const pending = new SessionExtensionUiRequests();
function dialog(opts, defaultValue, request, parse) {
if (opts?.signal?.aborted)
return Promise.resolve(defaultValue);
const id = crypto.randomUUID();
return new Promise((resolve, reject) => {
let timeoutId;
const cleanup = () => {
if (timeoutId)
clearTimeout(timeoutId);
opts?.signal?.removeEventListener("abort", onAbort);
pending.delete(id);
};
const onAbort = () => {
cleanup();
resolve(defaultValue);
};
opts?.signal?.addEventListener("abort", onAbort, { once: true });
if (opts?.timeout) {
timeoutId = setTimeout(() => {
cleanup();
resolve(defaultValue);
}, opts.timeout);
}
const frame = { type: "extension_ui_request", id, ...request };
pending.set(id, {
frame,
resolve: (response) => {
cleanup();
resolve(parse(response));
},
reject,
});
output(frame);
});
}
const fireAndForget = (request) => output({ type: "extension_ui_request", id: crypto.randomUUID(), ...request });
const ui = {
...noOpUIContext,
select: (title, options, opts) => dialog(opts, undefined, { method: "select", title, options, timeout: opts?.timeout, ...dialogCall(opts) }, answeredValue),
confirm: (title, message, opts) => dialog(opts, false, { method: "confirm", title, message, timeout: opts?.timeout }, (response) => "cancelled" in response && response.cancelled
? false
: "confirmed" in response
? response.confirmed
: false),
// This presenter does not speak the secret-input protocol, so a secret is refused, never asked as plain text.
input: (title, placeholder, opts) => {
if (opts?.secret)
return Promise.reject(new SecretInputUnavailableError("client_unsupported"));
return dialog(opts, undefined, { method: "input", title, placeholder, timeout: opts?.timeout, ...dialogCall(opts) }, answeredValue);
},
editor: (title, prefill) => dialog(undefined, undefined, { method: "editor", title, prefill }, answeredValue),
notify: (message, type) => fireAndForget({ method: "notify", message, notifyType: type }),
setStatus: (key, text) => fireAndForget({ method: "setStatus", statusKey: key, statusText: text }),
setTitle: (title) => fireAndForget({ method: "setTitle", title }),
// OSC 7501 is a terminal report and this connection owns no terminal.
setProgramStatus: () => { },
};
return {
ui,
respond: (response) => pending.resolve(response),
dispose: () => pending.cancelAll(),
};
}
//# sourceMappingURL=ui-broker.js.map