/**
 * Tool Execution Orchestrator Module
 *
 * Manages the agentic tool execution loop, handling:
 * - Tool call batching and execution ordering
 * - Parallel vs sequential execution decisions
 * - Tool result collection and formatting
 * - Execution metrics and cost tracking
 *
 * @module agent/execution
 */
import { EventEmitter } from "events";
import { CodeBuddyToolCall } from "../../codebuddy/client.js";
import { ToolResult } from "../../types/index.js";
/**
 * Configuration for the ToolExecutionOrchestrator
 */
export interface OrchestratorConfig {
    /** Maximum number of tool execution rounds. Default: 50 */
    maxToolRounds: number;
    /** Enable parallel execution of safe tools. Default: true */
    parallelExecution: boolean;
    /** Timeout for individual tool execution in milliseconds. Default: 120000 */
    toolTimeout: number;
    /** Maximum concurrent tool executions. Default: 5 */
    maxConcurrent: number;
}
/**
 * Default orchestrator configuration
 */
export declare const DEFAULT_ORCHESTRATOR_CONFIG: OrchestratorConfig;
/**
 * Result of executing a batch of tool calls
 */
export interface BatchExecutionResult {
    /** Map of tool call ID to result */
    results: Map<string, ToolResult>;
    /** Total execution time in milliseconds */
    duration: number;
    /** Number of successful executions */
    successCount: number;
    /** Number of failed executions */
    failureCount: number;
    /** Whether tools were executed in parallel */
    parallel: boolean;
}
/**
 * Tool execution metrics
 */
export interface ExecutionMetrics {
    /** Total tool rounds executed */
    toolRounds: number;
    /** Total individual tool calls */
    totalCalls: number;
    /** Successful tool calls */
    successfulCalls: number;
    /** Failed tool calls */
    failedCalls: number;
    /** Total execution time across all tools */
    totalExecutionTime: number;
    /** Per-tool execution statistics */
    toolStats: Map<string, {
        calls: number;
        successes: number;
        failures: number;
        totalTime: number;
        avgTime: number;
    }>;
}
/**
 * Events emitted by the orchestrator
 */
export interface OrchestratorEvents {
    "round:start": {
        round: number;
        toolCount: number;
    };
    "round:complete": {
        round: number;
        results: BatchExecutionResult;
    };
    "tool:start": {
        toolCall: CodeBuddyToolCall;
    };
    "tool:complete": {
        toolCall: CodeBuddyToolCall;
        result: ToolResult;
        duration: number;
    };
    "tool:error": {
        toolCall: CodeBuddyToolCall;
        error: string;
    };
    "max_rounds_reached": {
        rounds: number;
    };
}
/**
 * Type for tool executor function
 */
export type ToolExecutor = (toolCall: CodeBuddyToolCall) => Promise<ToolResult>;
/**
 * ToolExecutionOrchestrator - Manages the agentic tool execution loop
 *
 * This class handles the orchestration of tool execution during an
 * agent conversation, including:
 * - Deciding when to execute tools in parallel vs sequentially
 * - Managing execution rounds and preventing infinite loops
 * - Collecting and aggregating tool results
 * - Tracking execution metrics
 *
 * @example
 * ```typescript
 * const orchestrator = new ToolExecutionOrchestrator({
 *   maxToolRounds: 50,
 *   parallelExecution: true
 * });
 *
 * orchestrator.setExecutor(async (toolCall) => {
 *   // Execute the tool and return result
 *   return { success: true, output: "done" };
 * });
 *
 * const results = await orchestrator.executeBatch(toolCalls);
 * ```
 */
export declare class ToolExecutionOrchestrator extends EventEmitter {
    private config;
    private executor;
    private currentRound;
    private metrics;
    private aborted;
    constructor(config?: Partial<OrchestratorConfig>);
    /**
     * Set the tool executor function
     */
    setExecutor(executor: ToolExecutor): void;
    /**
     * Check if tool calls can be safely executed in parallel
     *
     * Tools that modify the same files or have side effects should not be parallelized.
     * Read-only operations (view_file, search, web_search) are safe to parallelize.
     */
    canParallelizeToolCalls(toolCalls: CodeBuddyToolCall[]): boolean;
    /**
     * Execute a batch of tool calls
     *
     * @param toolCalls - Array of tool calls to execute
     * @returns BatchExecutionResult with all results
     */
    executeBatch(toolCalls: CodeBuddyToolCall[]): Promise<BatchExecutionResult>;
    /**
     * Execute a single tool call with full tracking
     */
    private executeSingleTool;
    /**
     * Execute a single tool call with timeout
     */
    private executeWithTimeout;
    /**
     * Check if maximum tool rounds has been reached
     */
    hasReachedMaxRounds(): boolean;
    /**
     * Get current round number
     */
    getCurrentRound(): number;
    /**
     * Get remaining rounds
     */
    getRemainingRounds(): number;
    /**
     * Reset round counter for a new conversation
     */
    resetRounds(): void;
    /**
     * Abort current execution
     */
    abort(): void;
    /**
     * Check if execution was aborted
     */
    isAborted(): boolean;
    /**
     * Get execution metrics
     */
    getMetrics(): ExecutionMetrics;
    /**
     * Reset metrics
     */
    resetMetrics(): void;
    /**
     * Get configuration
     */
    getConfig(): OrchestratorConfig;
    /**
     * Update configuration
     */
    updateConfig(config: Partial<OrchestratorConfig>): void;
    /**
     * Get optimization hints based on tool execution patterns
     */
    getOptimizationHints(): string[];
    /**
     * Check if a tool is typically fast
     */
    isToolFast(toolName: string): boolean;
    /**
     * Check if a tool is typically slow
     */
    isToolSlow(toolName: string): boolean;
    /**
     * Get recommended timeout for a tool
     */
    getRecommendedTimeout(toolName: string): number;
    /**
     * Format metrics for display
     */
    formatMetrics(): string;
    /**
     * Create empty metrics object
     */
    private createEmptyMetrics;
    /**
     * Update per-tool statistics
     */
    private updateToolStats;
    /**
     * Dispose and cleanup
     */
    dispose(): void;
}
/**
 * Create a ToolExecutionOrchestrator instance
 */
export declare function createToolOrchestrator(config?: Partial<OrchestratorConfig>): ToolExecutionOrchestrator;
/**
 * Get global orchestrator instance
 */
export declare function getToolOrchestrator(config?: Partial<OrchestratorConfig>): ToolExecutionOrchestrator;
/**
 * Reset global orchestrator
 */
export declare function resetToolOrchestrator(): void;
