import { randomUUID } from "node:crypto"; import { Type } from "@sinclair/typebox"; import { Text } from "@earendil-works/pi-tui"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { bbForms, type FormValue } from "../bb-forms.js"; import { detectHost, fitLine } from "../host.js"; import { askUser, isCancel } from "./ui.js"; import { NOT_SHOWN_AFTER_RUN, questionTitle, type Answer, type PendingQuestions } from "./deferred.js"; type Decision = "registered" | "yes" | "no" | "dismissed" | "unanswered" | "unattended"; interface ConfirmDetails { question: string; danger: boolean; decision: Decision; comment?: string; note?: string; /** A message the user sent in the chat after deciding in bb's form. */ alsoTyped?: string; } const DESCRIPTION = "Put one yes/no decision to the user at the end of this turn: permission before you change something, or any answer that is 'go ahead' or 'don't'. The call does not wait: it registers the question and returns at once. Then write your final message saying exactly what you are about to do and why, so the answer is informed, and end the turn without calling another tool; the question opens under that message. Write the question in the user's language. Set danger for anything destructive or hard to undo. The answer arrives as the next message: yes, no, or that the user dismissed it, plus any comment. For anything with more than two answers use ask_user."; const REGISTERED = "Registered: the question opens for the user when this turn ends, under your final message. Write that message now — exactly what you are about to do and why, in full markdown — and end the turn. Call no tool: they are blocked until the user answers, and the answer arrives as the next message. Text you wrote before this call may not have reached the user, so the final message must stand on its own."; const PARAMETERS = Type.Object({ question: Type.String({ description: "One line: exactly what you are about to do, as a yes/no question" }), detail: Type.Optional(Type.String({ description: "One line of consequence or scope, shown under the question" })), danger: Type.Optional(Type.Boolean({ description: "Destructive or hard to undo: No is the default, and a run without a user never proceeds" })), }); const text = (value: string) => [{ type: "text" as const, text: value }]; function resultFor(details: ConfirmDetails) { const comment = details.comment ? ` They added: "${details.comment}".` : ""; switch (details.decision) { case "yes": return `User confirmed: yes.${comment}`; case "no": return `User declined: no.${comment} Do not do it and do not ask again; propose a different approach or ask what to do instead.`; case "dismissed": return `User dismissed the question without deciding${details.note ? ` ("${details.note}")` : ""}. Treat it as not approved: do not do it, and do not ask again in the same form.`; case "unanswered": return `Not confirmed — bb took the question down before the user answered (${details.note}). Do not do it yet; ask in plain prose in your reply and end the turn.`; case "registered": return REGISTERED; case "unattended": return details.danger ? "Not confirmed — no user is available (non-interactive run), and the action is marked dangerous. Do not do it; report that it needs approval." : "Not confirmed — no user is available (non-interactive run). Proceed only if the action is reversible, and state that assumption; otherwise stop and report."; } } const YES = /^(y|yes|yeah|yep|sure|ok|okay|go|go ahead|do it|да|ага|давай|ок|го)[.!]*$/i; const NO = /^(n|no|nope|don'?t|stop|нет|не надо|не|стоп)[.!]*$/i; /** Under bb with pi-pi's plugin; undefined when no form could be shown. */ async function confirmViaBbForm(ctx: any, base: { question: string; danger: boolean }, detail: string | undefined, signal: AbortSignal, afterRun: boolean): Promise { const forms = detectHost(ctx.mode).id === "bb" ? bbForms() : null; if (!forms) return undefined; const outcome = await forms.ask( { version: 1, kind: "confirm", question: base.question, ...(detail?.trim() ? { detail: detail.trim().slice(0, 4000) } : {}), danger: base.danger }, { title: fitLine(base.question, 160), signal, // Inside the run, a plain yes or no typed in the chat decides it; anything else is not a decision. // After it, the reply is a prompt of its own and takes the form down. ...(afterRun ? {} : { onTyped: (typed: string): FormValue => (YES.test(typed) ? { kind: "confirm", decision: "yes" } : NO.test(typed) ? { kind: "confirm", decision: "no" } : { kind: "decline", note: typed }) }), }, ); if (outcome.outcome === "unavailable") return undefined; if (outcome.outcome === "cancelled" && outcome.reason !== "user" && outcome.reason !== "signal") { // Not the user's decision: bb or the connection took the form down. return { ...base, decision: "unanswered", note: outcome.reason }; } const alsoTyped = outcome.alsoTyped ? { alsoTyped: outcome.alsoTyped } : {}; if (outcome.outcome === "submitted" && outcome.value.kind === "confirm") { return { ...base, decision: outcome.value.decision, ...(outcome.value.comment?.trim() ? { comment: outcome.value.comment.trim() } : {}), ...alsoTyped }; } const note = outcome.outcome === "submitted" && outcome.value.kind === "decline" ? outcome.value.note : undefined; return { ...base, decision: "dismissed", ...(note ? { note } : {}), ...alsoTyped }; } export function registerConfirm(pi: ExtensionAPI, questions: PendingQuestions): void { pi.registerTool({ name: "confirm", label: "Confirm", description: DESCRIPTION, promptSnippet: "Ask the user a single yes/no question, such as permission to go ahead", // Registered before the turn's later calls run, so the guard holds them back until the user has answered. executionMode: "sequential", parameters: PARAMETERS, async execute(_id, raw, _signal, _onUpdate, ctx) { const params = raw as { question?: string; detail?: string; danger?: boolean }; const question = params.question?.trim(); if (!question) return { content: text("confirm requires a question"), isError: true, details: { error: "missing question" } }; const base = { question, danger: params.danger === true }; if (!ctx.hasUI || !ctx.ui) { const details: ConfirmDetails = { ...base, decision: "unattended" }; return { content: text(resultFor(details)), details }; } if (!questions.register({ tool: "confirm", title: questionTitle(question), form: "confirm", present: (target, signal, afterRun) => present(target, base, params.detail, signal, afterRun) })) { return { content: text("Not asked: another question is already waiting for the user. End the turn, and ask this one after their answer."), isError: true, details: { error: "a question is already registered" } }; } const details: ConfirmDetails = { ...base, decision: "registered" }; return { content: text(resultFor(details)), details }; }, renderCall(args: any, theme) { return new Text(theme.fg("toolTitle", theme.bold("confirm ")) + theme.fg("muted", fitLine(String(args.question ?? ""), 80)), 0, 0); }, renderResult(result, _options, theme) { const details = result.details as (ConfirmDetails & { error?: string }) | undefined; if (!details || details.error) return new Text(theme.fg("error", `✗ ${details?.error ?? "failed"}`), 0, 0); return renderConfirmOutcome(details, theme); }, }); } /** The decision on one line, with the user's comment. */ export function renderConfirmOutcome(details: ConfirmDetails, theme: any): Text { if (details.decision === "registered") return new Text(theme.fg("muted", "asked when the turn ends"), 0, 0); const mark = details.decision === "yes" ? theme.fg("success", "✓ yes") : details.decision === "no" ? theme.fg("warning", "✗ no") : theme.fg("warning", `✗ ${details.decision === "unattended" ? "no user" : details.decision === "unanswered" ? "not answered" : "dismissed"}`); return new Text(`${mark}${details.comment ? theme.fg("muted", ` “${details.comment}”`) : ""}`, 0, 0); } /** Puts a registered confirm to the user: the terminal's component, pi-pi's bb form, or the host's native approval. */ async function present(ctx: any, base: { question: string; danger: boolean }, detail: string | undefined, signal: AbortSignal, afterRun: boolean): Promise { let details: ConfirmDetails; if (ctx.mode === undefined || ctx.mode === "tui") { const options = base.danger ? ["No", "Yes"] : ["Yes", "No"]; const result = await askUser(ctx as any, { question: base.question, detail, options, allowFreeform: false, declineNote: true, signal, label: "confirm", hotkeys: { y: options.indexOf("Yes"), n: options.indexOf("No") }, }); if (result && !isCancel(result) && result.kind === "selection") { details = { ...base, decision: result.selections[0] === "Yes" ? "yes" : "no", ...(result.comment ? { comment: result.comment } : {}) }; } else { details = { ...base, decision: "dismissed", ...(isCancel(result) && result.note ? { note: result.note } : {}) }; } } else { const viaForm = await confirmViaBbForm(ctx, base, detail, signal, afterRun); if (viaForm) { details = viaForm; } else if (afterRun) { return { text: NOT_SHOWN_AFTER_RUN, details: { error: "not shown" }, wake: false }; } else { // Hosts render confirm as their native approval: Yes and No, with a // dismissal that comes back as No. The question goes in the title, // which every host shows. // T3 answers a confirm it has seen approved "always" by itself; a // dangerous one carries the time it was asked, so it is always new. const message = [detail?.trim().slice(0, 4000), base.danger ? `⚠ Destructive · request ${randomUUID().slice(0, 8)}` : ""].filter(Boolean).join("\n"); const yes = await ctx.ui.confirm(fitLine(base.question, 160), message, { signal }); details = { ...base, decision: signal.aborted ? "dismissed" : yes ? "yes" : "no" }; } } // After the run, bb takes a form down for the user's Stop, among others: nothing is to start on its own then. const takenDownAfterRun = afterRun && details.decision === "unanswered"; const result = takenDownAfterRun ? `Not confirmed — bb took the question down (${details.note}) after your turn had ended. Do not do it; wait for the user's next message.` : resultFor(details); // A bare dismissal has nothing to act on: it waits for the user's next message. const wake = !takenDownAfterRun && (details.decision !== "dismissed" || !!details.note || !!details.alsoTyped); return { text: details.alsoTyped ? `${result}\nAfter deciding, the user also wrote in the chat: "${details.alsoTyped}"` : result, details: details as unknown as Record, wake }; }