Overview
Concrete singleton manager that queues human-in-the-loop intervention requests, tracks state, handles timeouts and page-monitoring cancellation, and dispatches lifecycle events.
Description
The InterventionManager class is the central coordinator for the intervention system. It is a singleton (`getInstance`) that manages a single active intervention at a time with a FIFO queue. The `requestIntervention` method creates a request with type, params, timeout (default 300s), and reason. It checks the conversation mode (disabled rejects), validates availability via the registry, queues if an intervention is active, and executes immediately otherwise. Page monitoring cancels interventions on tab switch or URL navigation. Timeout handling fires after the configured seconds. An event system (`emitEvent`, `addEventListener`) notifies UI components of request, start, complete, cancel, timeout, and error events.
Usage
- Request intervention: `await InterventionManager.getInstance().requestIntervention("voice-input", {}, 120, "Need voice confirmation")`
- Set mode: `InterventionManager.getInstance().setConversationMode("passive")`
- Subscribe to events: subscribe via the event listener API.
Code Reference
Source Location
- Repository: AIPex
- File: packages/browser-runtime/src/intervention/intervention-manager.ts
- Lines: L54-518 (class), L68-73 (getInstance), L117-188 (requestIntervention), L315-360 (cancel), L362-408 (timeout), L420-454 (page monitoring), L482-507 (emitEvent)
- Support: packages/browser-runtime/src/intervention/types.ts (InterventionMode, InterventionResult, etc.)
Signature
export class InterventionManager {
static getInstance(): InterventionManager
async initialize(): Promise<void>
setConversationMode(mode: InterventionMode): void
getConversationMode(): InterventionMode
async requestIntervention(
type: InterventionType,
params?: unknown,
timeout?: number,
reason?: string
): Promise<InterventionResult>
cancelIntervention(id: string, reason: CancelReason): boolean
emitEvent(type: InterventionEventType, interventionId: string, data?: unknown): void
}
type InterventionMode = "disabled" | "passive"
type InterventionType = "monitor-operation" | "voice-input" | "user-selection"
type InterventionStatus = "pending" | "active" | "completed" | "cancelled" | "timeout" | "error"
interface InterventionResult {
success: boolean;
data?: unknown;
error?: string;
status: InterventionStatus;
timestamp: number;
duration?: number;
}
function getCancelMessage(reason: CancelReason): string
Import
import { InterventionManager } from "@aipexstudio/browser-runtime";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| type |
InterventionType |
Yes |
"monitor-operation", "voice-input", or "user-selection"
|
| params |
unknown |
No |
Type-specific parameters
|
| timeout |
number |
No |
Timeout in seconds (default 300)
|
| reason |
string |
No |
AI explanation for why intervention is needed
|
Outputs
| Name |
Type |
Description
|
| returns |
InterventionResult |
Structured result with success, data, status, timestamp, duration
|
Usage Examples
Request Voice Input
import { InterventionManager } from "@aipexstudio/browser-runtime";
const manager = InterventionManager.getInstance();
await manager.initialize();
const result = await manager.requestIntervention(
"voice-input",
{ prompt: "What would you like me to do?" },
120, // 2-minute timeout
"Need user confirmation before proceeding with form submission"
);
if (result.success) {
console.log("User said:", result.data); // transcribed text
} else {
console.log("Intervention failed:", result.error);
}
Cancel on Tab Switch
// The manager internally monitors chrome.tabs.onActivated:
// if user switches tabs during an active intervention:
// cancelIntervention(currentIntervention.request.id, "tab_switched")
// → getCancelMessage("tab_switched") → "Cancelled: browser tab switched"
Related Pages
Implements Principle
Uses Heuristics
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.