/** * Unified confirmation helper for destructive actions. * * Destructive actions (delete X, clear Y, uninstall Z, send public post, * sign transaction, etc.) should not fire on the first invocation. * Instead they should: * 1. Stash a pending-confirmation record in the runtime cache. * 2. Emit a callback message describing the operation and asking the * user to confirm. * 3. On a later turn in the same room, if the user's reply is a plain * "yes", proceed; if it says no, cancel; anything else asks again. * * This module centralizes that pattern so every destructive action * follows the same UX, the same TTL behavior, and the same cancel * semantics. * * Usage: * const decision = await requireConfirmation({ * runtime, * message, * actionName: "DELETE_LINEAR_ISSUE", * pendingKey: `delete:${issueId}`, * prompt: `Permanently delete issue ${humanId}? This cannot be undone.`, * callback, * }); * if (decision.status === "pending") { * return { success: true, data: { awaitingUserInput: true } }; * } * if (decision.status === "cancelled") { * return { success: true, text: "Cancelled." }; * } * // status === "confirmed" — proceed with the destructive op */ import type { HandlerCallback } from "../types/components.js"; import type { Memory } from "../types/memory.js"; import type { IAgentRuntime } from "../types/runtime.js"; export type ConfirmationStatus = "pending" | "confirmed" | "cancelled"; export interface RequireConfirmationArgs { runtime: IAgentRuntime; message: Memory; /** Action name doing the destructive op. Used in the cache key + emitted prompt. */ actionName: string; /** * Stable key identifying the specific pending operation, e.g. * `delete:${issueId}`. Combined with the room, user id and action name to * form the cache key, so only a reply in the room that showed the prompt * can settle it. Two simultaneous pending confirmations with the same * pendingKey for the same user in one room are not supported. */ pendingKey: string; /** Human-readable prompt the user sees. */ prompt: string; /** Optional callback for emitting the prompt; if omitted, the * caller is expected to deliver `prompt` via its own mechanism. */ callback?: HandlerCallback; /** TTL for the pending record. Default 5 minutes. */ ttlMs?: number; /** Optional structured metadata to stash on the pending record (passed back on confirm). */ metadata?: Record; } export interface ConfirmationDecision { status: ConfirmationStatus; /** When status is "confirmed" or "cancelled", this is the metadata * that was stashed when the confirmation was first requested. */ metadata?: Record; } /** * True when the user's own message is a plain yes, read the same way as * {@link requireConfirmation}. Callers that persist their pending preview * durably (instead of in the runtime cache) use this so the decision is still * bound to the actual user reply, never to model output. */ export declare function isAffirmativeConfirmationReply(message: Memory): boolean; /** * Two-phase destructive-action helper. * * Returns: * - `{ status: "pending" }` on the FIRST invocation (no record for this room * yet), and on a later one whose reply is neither a plain yes nor a no. The * helper has stashed the record and (if `callback` is provided) emitted the * prompt again. Caller should return early without performing the op. * * - `{ status: "confirmed", metadata }` on a later invocation in the same room * when the user replied with a plain yes. The pending record has been * cleared. Caller should perform the destructive op. * * - `{ status: "cancelled", metadata }` on a later invocation in the same room * when the reply says no. The pending record has been cleared. Caller should * not perform the op. * * Expired pending records (older than ttlMs) are treated as fresh first calls. */ export declare function requireConfirmation(args: RequireConfirmationArgs): Promise; export type DestructiveConfirmationGateResult = { readonly status: "confirmed"; readonly metadata?: Record; } | { readonly status: "pending"; } | { readonly status: "cancelled"; readonly metadata?: Record; }; /** * Thin wrapper around {@link requireConfirmation} for destructive action handlers. * Never consult LLM `confirmed` params — only user yes/no on a follow-up turn. */ export declare function gateDestructiveConfirmation(args: RequireConfirmationArgs): Promise;