@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
168 lines • 7.73 kB
JavaScript
import { Container, Text } from "@earendil-works/pi-tui";
import { AnswerRefusedError } from "./attach-client.js";
import { AskUserQuestionComponent } from "./components/ask-user-question.js";
import { ExtensionEditorComponent } from "./components/extension-editor.js";
import { ExtensionInputComponent } from "./components/extension-input.js";
import { ExtensionSelectorComponent } from "./components/extension-selector.js";
import { theme } from "./theme/theme.js";
/**
* "Requested from <device>" for a dialog raised by a remote device's turn (senpi#5480), read from the
* frame's `remoteOrigin`. Absent or malformed means a local turn: no label. The name is wire data, so
* control characters are dropped before it reaches the terminal.
*/
function remoteOriginLabel(request) {
const origin = "remoteOrigin" in request ? request.remoteOrigin : undefined;
if (typeof origin !== "object" || origin === null || !("deviceId" in origin))
return undefined;
const name = "deviceName" in origin && typeof origin.deviceName === "string" ? clean(origin.deviceName) : "";
const id = typeof origin.deviceId === "string" ? clean(origin.deviceId) : "";
const device = name || id;
return device ? `Requested from ${device}` : undefined;
}
const clean = (value) => value.replace(/[\u0000-\u001f\u007f-\u009f]/g, "").trim();
/** Owns only the visible dialog; the endpoint owns its resolution and lifetime. */
export class AttachDialogs extends Container {
constructor(ui, keys, answer, report, focusEditor) {
super();
this.queue = [];
this.seen = new Set();
this.settled = new Set();
this.closed = false;
this.ui = ui;
this.keys = keys;
this.answer = answer;
this.report = report;
this.focusEditor = focusEditor;
}
get active() {
return this.current !== undefined;
}
accept(request) {
if (this.seen.has(request.id))
return;
if (!["select", "confirm", "input", "editor", "question"].includes(request.method))
return;
this.seen.add(request.id);
this.queue.push(request);
this.showNext();
}
/** The endpoint settled the request (another client answered it): it never comes back. */
resolve(id) {
this.settled.add(id);
const index = this.queue.findIndex((request) => request.id === id);
if (index >= 0)
this.queue.splice(index, 1);
if (this.current?.request.id === id)
this.dismiss();
}
handleInput(data) {
this.current?.component.handleInput?.(data);
}
dispose() {
this.closed = true;
this.queue.length = 0;
this.current?.component.dispose?.();
this.current = undefined;
this.clear();
}
dismiss() {
this.current?.component.dispose?.();
this.current = undefined;
this.clear();
this.focusEditor();
this.showNext();
this.ui.requestRender();
}
/**
* The dialog leaves while its answer is in flight and comes back unless the endpoint accepted it.
* A request the endpoint no longer waits on (settled elsewhere, or gone) stays dismissed.
*/
submit(request, response) {
this.dismiss();
this.answer(response).catch((error) => {
const code = error instanceof AnswerRefusedError ? error.code : undefined;
if (code === "question_already_resolved" || code === "unknown_extension_ui_request") {
this.report(`Not accepted: ${code} (already settled)`);
return;
}
if (this.closed || this.settled.has(request.id))
return;
this.report(request.method === "input" && request.secret
? "Secret answer was not acknowledged. The outcome is unknown; enter a new answer only after checking."
: `${error instanceof Error ? error.message : String(error)}. Answer again.`);
if (request.method === "input" &&
request.secret &&
(!(error instanceof AnswerRefusedError) || Date.now() >= (request.deadlineAtMs ?? 0)))
return;
this.queue.unshift(request);
this.showNext();
this.ui.requestRender();
});
}
showNext() {
if (this.current)
return;
const request = this.queue.shift();
if (!request)
return;
const finish = (response) => {
if (this.current?.request.id !== request.id)
return;
this.submit(request, request.method === "input" && request.secret
? {
...response,
secret: true,
uiRequestId: request.id,
id: crypto.randomUUID(),
answerId: crypto.randomUUID(),
}
: response);
};
const cancel = () => finish({ type: "extension_ui_response", id: request.id, cancelled: true });
let component;
switch (request.method) {
case "select":
component = new ExtensionSelectorComponent(request.title, request.options, (value) => finish({ type: "extension_ui_response", id: request.id, value }), cancel, { tui: this.ui, timeout: request.timeout });
break;
case "confirm":
component = new ExtensionSelectorComponent(request.title, ["Yes", "No"], (value) => finish({ type: "extension_ui_response", id: request.id, confirmed: value === "Yes" }), cancel, { tui: this.ui, timeout: request.timeout, description: request.message });
break;
case "input":
if (request.secret && (request.deadlineAtMs === undefined || request.deadlineAtMs <= Date.now())) {
this.settled.add(request.id);
this.showNext();
return;
}
component = new ExtensionInputComponent(request.title, request.placeholder, (value) => finish({ type: "extension_ui_response", id: request.id, value }), cancel, { tui: this.ui, timeout: request.timeout, secret: request.secret, deadlineAtMs: request.deadlineAtMs });
break;
case "editor":
component = new ExtensionEditorComponent(this.ui, this.keys, request.title, request.prefill, (value) => finish({ type: "extension_ui_response", id: request.id, value }), cancel);
break;
case "question":
component = new AskUserQuestionComponent({
requestId: request.requestId,
questions: request.questions,
waitForAnswer: request.waitForAnswer,
timeoutMs: request.remainingMs,
}, (response) => response.status === "answered" || response.status === "comment-submitted"
? finish({
type: "extension_ui_response",
id: request.id,
answers: response.answers,
comment: response.comment,
})
: cancel(), { tui: this.ui, getDeadlineAtMs: () => request.deadlineAtMs });
break;
default:
return;
}
this.current = { request, component };
const origin = remoteOriginLabel(request);
if (origin)
this.addChild(new Text(theme.fg("warning", origin), 1, 0));
this.addChild(component);
this.ui.setFocus(component);
this.ui.requestRender();
}
}
//# sourceMappingURL=attach-dialogs.js.map