/** * Human-in-the-Loop (HITL) Runtime * * Purpose: * - Provide a world-scoped request/response flow for option-based HITL prompts. * * Key Features: * - Emits structured tool-progress events carrying HITL prompt payloads. * - Resolves pending requests when renderer/API submits an option response. * - Exposes unresolved option prompts through a deterministic read model. * - Maintains pending request map for validation and lifecycle cleanup. * * Implementation Notes: * - Requests are keyed by `(worldId, requestId)` to avoid cross-world collisions. * - Prompt read model is deterministic and scoped by `(worldId, chatId)`. * - Runtime is in-memory and process-local by design. * * Recent Changes: * - 2026-03-12: Relaxed the hard `requestId === toolCallId` invariant so durable approval prompts can keep distinct request and owning-tool identities. * - 2026-03-06: Removed `world.currentChatId` fallback from HITL option requests; interactive requests now require explicit `chatId`. * - 2026-02-26: Replaced direct `[hitl]` console traces with categorized structured logger events (`hitl`) for env-controlled filtering. * - 2026-02-25: Added HITL runtime trace logs for request emission, replay, and resolution lifecycle diagnostics. * - 2026-02-24: Removed HITL `system` event emission/replay and switched to tool-progress prompt payloads. * - 2026-02-24: Added `listPendingHitlPromptEvents` to expose scoped pending HITL prompt payloads for web chat-switch replay. * - 2026-02-24: Removed timeout auto-resolution and added deterministic replay helpers for unresolved HITL requests. * - 2026-02-20: Enforced global options-only HITL runtime by removing input-mode request/response paths. * - 2026-02-14: Added initial generic HITL option request/response runtime. */ import { type AgentMessage, type World } from './types.js'; export interface HitlOption { id: string; label: string; description?: string; } export interface HitlOptionRequest { requestId?: string; title: string; message: string; options: HitlOption[]; chatId?: string | null; timeoutMs?: number; defaultOptionId?: string; metadata?: Record; agentName?: string | null; } export interface HitlOptionResolution { requestId: string; worldId: string; chatId: string | null; optionId: string; source: 'user' | 'timeout'; } export type HitlResponseResolution = { requestId: string; worldId: string; chatId: string | null; optionId: string; source: 'user' | 'timeout'; }; interface PendingHitlOptionRequest { worldId: string; requestId: string; chatId: string | null; optionIds: Set; sequence: number; prompt: { requestId: string; title: string; message: string; options: HitlOption[]; defaultOptionId: string; metadata: Record | null; agentName: string | null; toolName: string; toolCallId: string; }; metadata: Record | null; resolve: (resolution: HitlResponseResolution) => void; } /** * Remove all pending HITL requests scoped to a specific chat. * Called during edit+resubmit so orphaned requests from aborted processing * do not block the activity tracker or leak into subscription replays. */ export declare function clearPendingHitlRequestsForChat(worldId: string, chatId: string | null): void; export declare function requestWorldOption(world: World, request: HitlOptionRequest): Promise; export declare function listPendingHitlPromptEventsFromMessages(messages: AgentMessage[], chatId?: string | null): Array<{ chatId: string | null; prompt: { requestId: string; title: string; message: string; options: Array<{ id: string; label: string; }>; defaultOptionId: string; metadata: Record | null; agentName: string | null; toolName: string; toolCallId: string; }; }>; export declare function replayPendingHitlRequests(world: World, chatId?: string | null): number; export declare function listPendingHitlPromptEvents(world: World, chatId?: string | null): Array<{ chatId: string | null; prompt: PendingHitlOptionRequest['prompt']; }>; export declare function submitWorldOptionResponse(params: { worldId: string; requestId: string; optionId: string; chatId?: string | null; }): { accepted: boolean; reason?: string; metadata?: Record | null; }; export declare function submitWorldHitlResponse(params: { worldId: string; requestId: string; optionId: string; chatId?: string | null; }): { accepted: boolean; reason?: string; metadata?: Record | null; }; export declare function clearHitlStateForTests(): void; export {};