import { jsx as _jsx } from "react/jsx-runtime";
import { ErrorMessage } from '../../../components/message-box.js';
import { formatError } from '../../../utils/error-formatter.js';
import { parseToolArguments } from '../../../utils/tool-args-parser.js';
import { displayToolResult } from '../../../utils/tool-result-display.js';
/**
 * Executes tools directly without confirmation.
 * Handles validation, execution, and error display.
 *
 * @returns Array of tool results from executed tools
 */
export const executeToolsDirectly = async (toolsToExecuteDirectly, toolManager, conversationStateManager, addToChatQueue, getNextComponentKey) => {
    // Import processToolUse here to avoid circular dependencies
    const { processToolUse } = await import('../../../message-handler.js');
    const directResults = [];
    for (const toolCall of toolsToExecuteDirectly) {
        try {
            // Run validator if available
            const validator = toolManager?.getToolValidator(toolCall.function.name);
            if (validator) {
                const parsedArgs = parseToolArguments(toolCall.function.arguments);
                const validationResult = await validator(parsedArgs);
                if (!validationResult.valid) {
                    // Validation failed - create error result and skip execution
                    const errorResult = {
                        tool_call_id: toolCall.id,
                        role: 'tool',
                        name: toolCall.function.name,
                        content: validationResult.error,
                    };
                    directResults.push(errorResult);
                    // Update conversation state with error
                    conversationStateManager.current.updateAfterToolExecution(toolCall, errorResult.content);
                    // Display the validation error to the user
                    addToChatQueue(_jsx(ErrorMessage, { message: validationResult.error, hideBox: true }, `validation-error-${toolCall.id}-${Date.now()}`));
                    continue; // Skip to next tool
                }
            }
            const result = await processToolUse(toolCall);
            directResults.push(result);
            // Update conversation state with tool execution
            conversationStateManager.current.updateAfterToolExecution(toolCall, result.content);
            // Display the tool result immediately
            await displayToolResult(toolCall, result, toolManager, addToChatQueue, getNextComponentKey);
        }
        catch (error) {
            // Handle tool execution errors
            const errorResult = {
                tool_call_id: toolCall.id,
                role: 'tool',
                name: toolCall.function.name,
                content: `Error: ${formatError(error)}`,
            };
            directResults.push(errorResult);
            // Update conversation state with error
            conversationStateManager.current.updateAfterToolExecution(toolCall, errorResult.content);
            // Display the error result
            await displayToolResult(toolCall, errorResult, toolManager, addToChatQueue, getNextComponentKey);
        }
    }
    return directResults;
};
//# sourceMappingURL=tool-executor.js.map