/** * HooksManager - Manages lifecycle hooks for agent customization * * Provides: * - Hook registration with priorities * - Sequential execution in priority order * - Type-safe hook invocation * - Error handling and recovery */ import type { HooksConfig, IterationHookContext, LLMHookContext, AfterLLMHookContext, ToolHookContext, AfterToolHookContext, ErrorHookContext, BeforeIterationHook, AfterIterationHook, BeforeLLMHook, AfterLLMHook, BeforeToolHook, AfterToolHook, OnErrorHook, ErrorHookResult, HookRegistrationOptions, HookEventHandler } from './types.js'; import type { Message } from '../providers/types.js'; import type { ToolDefinition, ToolExecutionResult } from '../tools/types.js'; /** * Options for creating a HooksManager */ export interface HooksManagerOptions { /** * Initial hooks configuration */ hooks?: HooksConfig; /** * Event handler for hook lifecycle events */ onEvent?: HookEventHandler; } /** * Manages lifecycle hooks for agent customization */ export declare class HooksManager { private beforeIterationHooks; private afterIterationHooks; private beforeLLMHooks; private afterLLMHooks; private beforeToolHooks; private afterToolHooks; private onErrorHooks; private readonly onEvent?; private hookIdCounter; constructor(options?: HooksManagerOptions); /** * Register hooks from a configuration object */ registerFromConfig(config: HooksConfig): void; /** * Register a before:iteration hook */ registerBeforeIteration(hook: BeforeIterationHook, options?: HookRegistrationOptions): string; /** * Register an after:iteration hook */ registerAfterIteration(hook: AfterIterationHook, options?: HookRegistrationOptions): string; /** * Register a before:llm hook */ registerBeforeLLM(hook: BeforeLLMHook, options?: HookRegistrationOptions): string; /** * Register an after:llm hook */ registerAfterLLM(hook: AfterLLMHook, options?: HookRegistrationOptions): string; /** * Register a before:tool hook */ registerBeforeTool(hook: BeforeToolHook, options?: HookRegistrationOptions): string; /** * Register an after:tool hook */ registerAfterTool(hook: AfterToolHook, options?: HookRegistrationOptions): string; /** * Register an onError hook */ registerOnError(hook: OnErrorHook, options?: HookRegistrationOptions): string; /** * Unregister a hook by ID */ unregister(hookId: string): boolean; /** * Clear all hooks */ clear(): void; /** * Run before:iteration hooks * * @returns true to continue, false to skip iteration */ runBeforeIteration(context: IterationHookContext): Promise; /** * Run after:iteration hooks */ runAfterIteration(context: IterationHookContext & { toolCalls: Array<{ name: string; input: Record; result: ToolExecutionResult; }>; completedWithText: boolean; }): Promise; /** * Run before:llm hooks * * @returns potentially modified messages and tools */ runBeforeLLM(context: LLMHookContext): Promise<{ messages: Message[]; tools: ToolDefinition[]; systemPrompt: string; }>; /** * Run after:llm hooks */ runAfterLLM(context: AfterLLMHookContext): Promise; /** * Run before:tool hooks * * @returns whether to proceed and potentially modified input or skip result */ runBeforeTool(context: ToolHookContext): Promise<{ proceed: boolean; input: Record; skipResult?: ToolExecutionResult; }>; /** * Run after:tool hooks * * @returns potentially modified result */ runAfterTool(context: AfterToolHookContext): Promise; /** * Run onError hooks * * @returns error handling result */ runOnError(context: ErrorHookContext): Promise; /** * Check if any hooks are registered */ hasHooks(): boolean; /** * Get hook counts by type */ getHookCounts(): Record; /** * Get all registered hook IDs */ getHookIds(): string[]; private generateId; private sortByPriority; private emitEvent; }