@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
135 lines (134 loc) • 4.75 kB
TypeScript
/**
* HITL (Human-in-the-Loop) Manager
*
* Central orchestrator for all HITL confirmation workflows.
* Manages user confirmation requests, timeouts, and argument modifications
* for enterprise-grade AI safety.
*/
import { EventEmitter } from "events";
import type { HITLConfig, ConfirmationResult, HITLConfirmationContext, HITLStatistics } from "../types/index.js";
export declare const DEFAULT_ALLOW_MODIFICATION = false;
/** The abort reason as text for `hitl:cancelled`, whatever the caller passed. */
export declare function describeAbortReason(signal: AbortSignal): string;
/**
* HITLManager - Central orchestrator for Human-in-the-Loop safety mechanisms
*
* Features:
* - Real-time user confirmation via events
* - Configurable dangerous action detection
* - Custom rule engine for complex scenarios
* - Argument modification support
* - Comprehensive audit logging
* - Timeout handling with cleanup
*/
export declare class HITLManager extends EventEmitter {
private config;
private pendingConfirmations;
/** hitl:cancelled deliveries held back while that ID's request emit runs. */
private requestsEmitting;
private statistics;
constructor(config: HITLConfig);
private readonly warnedUnknownKeys;
/**
* Validate HITL configuration and apply defaults
*/
private validateConfig;
/**
* Check if a tool requires confirmation based on configuration
*/
requiresConfirmation(toolName: string, args?: unknown): boolean;
/**
* Request confirmation for a tool execution.
*
* Aborting `context.signal` withdraws the request (see `cancelConfirmation`);
* a signal that is already aborted raises no card at all.
*/
requestConfirmation(toolName: string, arguments_: unknown, context?: HITLConfirmationContext): Promise<ConfirmationResult>;
/**
* Process user response to confirmation request
*/
processUserResponse(confirmationId: string, response: {
approved: boolean;
reason?: string;
modifiedArguments?: unknown;
responseTime?: number;
userId?: string;
}): void;
/**
* Handle confirmation timeout
*/
private handleTimeout;
/**
* Withdraw a pending confirmation because the call that asked was aborted.
*
* Clears the timer, forgets the entry (so a later answer for this ID runs
* nothing), rejects the waiting call with `HITLCancelledError` and emits
* `hitl:cancelled` so the approval UI can retire the card.
*
* @returns false when nothing was pending under this ID
*/
cancelConfirmation(confirmationId: string, reason?: string): boolean;
/**
* Emit `hitl:cancelled`, or hold it until the request emit for this ID has
* finished so no listener sees the cancel before the card.
*/
private deliverCancelled;
/**
* Set up event handlers for processing responses
*/
private setupEventHandlers;
/**
* Generate unique confirmation ID
*/
private generateConfirmationId;
/**
* Generate human-readable action description
*/
private generateActionDescription;
/**
* Get keywords that triggered HITL
*/
private getTriggeredKeywords;
/**
* Log audit events for compliance and debugging
*/
private logAuditEvent;
/**
* Get current HITL usage statistics
*/
getStatistics(): HITLStatistics;
/**
* Get current configuration
*/
getConfig(): HITLConfig;
/**
* Update configuration (for dynamic reconfiguration)
*/
updateConfig(newConfig: Partial<HITLConfig>): void;
/**
* Clean up resources and reject pending confirmations
*/
cleanup(): void;
/**
* Check if manager is currently enabled
*/
isEnabled(): boolean;
/**
* Get count of pending confirmations
*/
getPendingCount(): number;
/**
* Whether a specific confirmation is still awaiting a response on this manager.
*
* A pending entry holds the `resolve`/`reject` of the suspended tool call, so it
* exists only in the memory of the manager that issued it. A manager constructed
* after the confirmation was issued — a session rebuilt from persisted state, for
* example — has an empty set, and `processUserResponse` for such an id logs a
* warning and returns without resolving anything.
*
* Callers that report an outcome back to a user should check this before treating
* a delivered `hitl:confirmation-response` as acted upon: the event being received
* says a listener existed, not that anything was waiting for it.
*/
hasPendingConfirmation(confirmationId: string): boolean;
}