import { type AgentCompletionOptions, type AgentLlmAdapter, type AgentMessage, type AgentToolCall, type AgentToolDefinition, type FrontMcpLogger } from '../common'; /** * Configuration for the agent execution loop. */ export interface AgentExecutionLoopConfig { /** * LLM adapter for making completions. */ adapter: AgentLlmAdapter; /** * System instructions for the agent. */ systemInstructions: string; /** * Available tools for the agent to use. */ tools: AgentToolDefinition[]; /** * Maximum number of iterations (tool call rounds). * @default 10 */ maxIterations?: number; /** * Timeout for the entire execution in milliseconds. * @default 120000 (2 minutes) */ timeout?: number; /** * Completion options (temperature, maxTokens, etc.). */ completionOptions?: AgentCompletionOptions; /** * Logger for debugging. */ logger?: FrontMcpLogger; /** * Callback when a tool call is made. */ onToolCall?: (toolCall: AgentToolCall) => void; /** * Callback when a tool result is received. */ onToolResult?: (toolCall: AgentToolCall, result: unknown, error?: Error) => void; /** * Callback for streaming content. */ onContent?: (content: string) => void; /** * Callback for each iteration. */ onIteration?: (iteration: number, message: AgentMessage) => void; /** * Callback when LLM request starts. */ onLlmStart?: (iteration: number, maxIterations: number) => void; /** * Callback when LLM response is received (with usage stats). */ onLlmComplete?: (iteration: number, usage?: { promptTokens?: number; completionTokens?: number; }) => void; /** * Callback when tool calls are extracted from LLM response. */ onToolsIdentified?: (count: number, names: string[]) => void; /** * Callback before a tool starts execution. */ onToolStart?: (toolCall: AgentToolCall, index: number, total: number) => void; /** * Callback when agent execution is complete. */ onComplete?: (content: string | null, error?: Error) => void; } /** * Result of an agent execution. */ export interface AgentExecutionResult { /** * Final response content from the agent. */ content: string | null; /** * All messages in the conversation. */ messages: AgentMessage[]; /** * Number of iterations (tool call rounds) performed. */ iterations: number; /** * Total token usage. */ usage?: { promptTokens: number; completionTokens: number; totalTokens?: number; }; /** * Whether the execution completed successfully. */ success: boolean; /** * Error if execution failed. */ error?: Error; /** * Duration in milliseconds. */ durationMs: number; } /** * Handler function for executing tools. */ export type ToolExecutor = (name: string, args: Record) => Promise; /** * Agent execution loop for processing LLM interactions. * * The loop: * 1. Sends the current prompt to the LLM * 2. Processes tool calls if any * 3. Adds tool results to the conversation * 4. Repeats until the LLM responds with text or max iterations * * @example * ```typescript * const loop = new AgentExecutionLoop({ * adapter: myLlmAdapter, * systemInstructions: 'You are a helpful assistant.', * tools: [searchTool, calculateTool], * }); * * const result = await loop.run( * 'What is the weather in Paris?', * async (name, args) => { * // Execute the tool and return result * return toolRegistry.execute(name, args); * }, * ); * * console.log(result.content); * ``` */ export declare class AgentExecutionLoop { private readonly config; constructor(config: AgentExecutionLoopConfig); /** * Run the execution loop with a user message. * * @param userMessage - The user's input message * @param toolExecutor - Function to execute tools * @param existingMessages - Optional existing conversation history * @returns Execution result */ run(userMessage: string, toolExecutor: ToolExecutor, existingMessages?: AgentMessage[]): Promise; /** * Run the execution loop with streaming. * * @param userMessage - The user's input message * @param toolExecutor - Function to execute tools * @param existingMessages - Optional existing conversation history * @returns AsyncGenerator yielding chunks and final result */ runStreaming(userMessage: string, toolExecutor: ToolExecutor, existingMessages?: AgentMessage[]): AsyncGenerator; private executeLoop; private executeLoopStreaming; } /** * Events emitted during streaming execution. */ export type AgentStreamEvent = { type: 'iteration'; iteration: number; } | { type: 'content'; content: string; } | { type: 'tool_call'; toolCall: Partial & { id: string; }; } | { type: 'tool_start'; toolCall: AgentToolCall; } | { type: 'tool_end'; toolCall: AgentToolCall; result: unknown; error?: Error; } | { type: 'usage'; promptTokens: number; completionTokens: number; } | { type: 'error'; error: Error; } | { type: 'done'; result: AgentExecutionResult; }; /** * Error thrown when agent reaches maximum iterations. */ export declare class AgentMaxIterationsError extends Error { readonly maxIterations: number; constructor(message: string, maxIterations: number); } //# sourceMappingURL=agent-execution-loop.d.ts.map