UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

227 lines • 10.6 kB
import { settledQuestionStatus, unansweredQuestionIds } from "./extension-ui-response.js"; import { remoteOriginField } from "./remote-origin.js"; export const sessionQuestionBridges = new WeakMap(); export class ConnectionQuestionBridge { constructor(output, changed, raiser = () => undefined, origin = () => undefined) { this.pending = new Map(); this.resolved = new Set(); this.output = output; this.changed = changed; this.raiser = raiser; this.origin = origin; } pendingQuestions() { return [...this.pending.values()].map(({ frame }) => ({ ...frame, remainingMs: Math.max(0, frame.deadlineAtMs - Date.now()), })); } ask(request, opts) { const id = crypto.randomUUID(); const askedAtMs = Date.now(); const timeout = opts?.timeout ?? request.timeoutMs; const frame = { type: "extension_ui_request", method: "question", id, requestId: request.requestId, toolCallId: request.requestId, waitForAnswer: request.waitForAnswer, questions: request.questions, raisedBy: this.raiser() ?? "agent", ...remoteOriginField(this.origin()), timeout, askedAtMs, deadlineAtMs: askedAtMs + timeout, remainingMs: timeout, }; return new Promise((resolve) => { let answers = {}; let comment; let timer; let finished = false; let claimed = false; const unanswered = () => unansweredQuestionIds(request.questions, answers); const finish = (status, resolvedBy, cancelOrigin) => { if (finished) return; finished = true; clearTimeout(timer); opts?.signal?.removeEventListener("abort", cancel); this.pending.delete(id); this.changed?.({ type: "ui_request_settled", uiRequestId: id, disposition: status === "answered" || status === "comment-submitted" ? "answered" : "cancelled", }); this.resolved.add(id); const result = { status, ...(resolvedBy !== undefined ? { resolvedBy } : {}), answers, comment, unanswered: unanswered(), ...(status === "timed_out" ? { autoResolvedAfterMs: Date.now() - askedAtMs } : {}), ...(cancelOrigin !== undefined ? { cancelOrigin } : {}), }; this.output({ type: "question_resolved", id, requestId: frame.requestId, toolCallId: frame.toolCallId, outcome: status, ...(result.resolvedBy !== undefined ? { resolvedBy: result.resolvedBy } : {}), answers, comment, unanswered: result.unanswered, deadlineAtMs: frame.deadlineAtMs, }); resolve(result); }; // An aborted dialog is a cancellation unless the aborting side resolved a // terminal status of its own: the ask-user extension's idle timer settles // the question `timed_out` and aborts with that status, and connections // must see the outcome the extension resolved (docs/rpc.md question). const cancel = () => { const reason = opts?.signal?.reason; finish(reason === "timed_out" ? "timed_out" : "cancelled"); }; const arm = () => { clearTimeout(timer); frame.deadlineAtMs = Date.now() + timeout; timer = setTimeout(() => finish("timed_out"), timeout); }; this.pending.set(id, { frame, cancel, progress: (draft) => { // A claimed question already has its winner: a draft that lands while that answer is being admitted // must not overwrite the selections it settles with (senpi#5960). if (claimed) return "question_already_resolved"; if (draft.answers !== undefined) answers = draft.answers; if (draft.comment !== undefined) comment = draft.comment; arm(); opts?.onProgress?.({ answers, comment }); if (!finished) this.output({ type: "question_updated", id, deadlineAtMs: frame.deadlineAtMs, remainingMs: Math.max(0, frame.deadlineAtMs - Date.now()), }); return "applied"; }, respond: (response, beforeAnswer) => { // The first answer wins: one that arrives while the winner's `beforeAnswer` runs settles nothing. if (claimed) return "question_already_resolved"; // The client's cancel is the user's; a closed connection or an abort is a lifecycle cancel. if ("cancelled" in response) { finish("cancelled", undefined, "user"); return true; } if (!("answers" in response)) return false; answers = response.answers; comment = response.comment; const status = settledQuestionStatus(answers, comment); if (status === undefined) return false; // Only a tool-result answer is reported here. An async or resumed answer is delivered as a // user message, whose question-input release reports it; reporting both would count it twice. if (request.waitForAnswer && opts?.deliver !== "user-message") { claimed = true; try { beforeAnswer?.(); } finally { claimed = false; } // A cancel or abort that ran inside `beforeAnswer` settled the question first. if (finished) return "question_already_resolved"; } finish(status, "rpc_connection"); return true; }, }); arm(); this.changed?.({ type: "ui_request_pending", uiRequestId: id, method: "question", request: frame }); this.output(frame); opts?.signal?.addEventListener("abort", cancel, { once: true }); if (opts?.signal?.aborted) cancel(); }); } respond(response, beforeAnswer) { const pending = this.pending.get(response.id); if (pending) { const result = pending.respond(response, beforeAnswer); return result === false ? "question_incomplete" : result; } return this.resolved.has(response.id) ? "question_already_resolved" : false; } /** Hands a draft to its pending question; reports whether one took it, or why none did (senpi#5761). */ progress(draft) { const pending = this.pending.get(draft.id); if (pending) return pending.progress(draft); return this.resolved.has(draft.id) ? "question_already_resolved" : "unknown_extension_ui_request"; } cancelAll() { for (const pending of this.pending.values()) pending.cancel(); } } export async function degradeQuestion(ui, request, opts) { const askedAtMs = Date.now(); const answers = {}; const other = "Other (type an answer)"; for (const question of request.questions) { // An abort ends the fallback: no dialog opens after it. if (opts?.signal?.aborted) break; const selected = await ui.select(question.question, [...question.options.map((option) => option.label), other], opts); // A pick that lands after the abort is not an answer, whether a label or "Other". if (opts?.signal?.aborted) break; if (selected === other) { const text = await ui.input(question.question, undefined, opts); if (text?.trim()) answers[question.id] = { selected: [], text }; } else if (selected !== undefined) answers[question.id] = { selected: [selected] }; // Report the answers so far as the structured bridge does: an abort or the idle timer settles the // question from the extension's draft, before this fallback returns. if (answers[question.id] !== undefined) opts?.onProgress?.({ answers: { ...answers } }); } const unanswered = unansweredQuestionIds(request.questions, answers); // An abort is never an answer. Like the structured bridge it settles cancelled, or timed_out when // the aborting side resolved that status; what the user had answered is kept as partial answers. if (opts?.signal?.aborted) { const timedOut = opts.signal.reason === "timed_out"; return { status: timedOut ? "timed_out" : "cancelled", answers, unanswered, ...(timedOut ? { autoResolvedAfterMs: Date.now() - askedAtMs } : {}), }; } // No trailing "anything else?" prompt: a client without `question` shows every dialog as its own // blocking card, so an optional comment read as a new question from the agent // (sisyphuslabs/omo-desktop-app#4042). "Other" above still takes a typed answer per question. // A partial answer settles like the structured path: answered, with the rest listed unanswered. const status = settledQuestionStatus(answers, undefined); return { status: status ?? "cancelled", ...(status !== undefined ? { resolvedBy: "rpc_connection" } : {}), answers, unanswered, }; } //# sourceMappingURL=connection-question-bridge.js.map