"use strict";
/**
 * Agent Executor
 * Executes agents using NVIDIA models
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentExecutor = void 0;
exports.quickExecute = quickExecute;
class AgentExecutor {
    nvidia;
    loader;
    executedCount = 0;
    constructor(nvidia, loader) {
        this.nvidia = nvidia;
        this.loader = loader;
    }
    /**
     * Execute agent synchronously
     */
    async execute(config) {
        try {
            // Load agent
            const agent = this.loader.loadAgent(config.agent);
            if (!agent) {
                throw new Error(`Agent not found: ${config.agent}`);
            }
            // Build messages
            const messages = this.buildMessages(agent, config.task);
            // Execute with NVIDIA
            const response = await this.nvidia.chat(messages, {
                model: config.model,
                temperature: config.temperature,
                maxTokens: config.maxTokens,
                stream: false
            });
            this.executedCount++;
            return {
                success: true,
                content: response.content,
                agent: agent.id,
                model: response.model,
                usage: response.usage
            };
        }
        catch (error) {
            return {
                success: false,
                content: '',
                agent: config.agent,
                model: config.model || this.nvidia.getDefaultModel(),
                error: error instanceof Error ? error.message : String(error)
            };
        }
    }
    /**
     * Execute agent with streaming
     */
    async executeStreaming(config) {
        try {
            // Load agent
            const agent = this.loader.loadAgent(config.agent);
            if (!agent) {
                throw new Error(`Agent not found: ${config.agent}`);
            }
            // Build messages
            const messages = this.buildMessages(agent, config.task);
            // Execute with streaming
            const stream = this.nvidia.stream(messages, {
                model: config.model,
                temperature: config.temperature,
                maxTokens: config.maxTokens
            });
            this.executedCount++;
            return {
                success: true,
                content: stream,
                agent: agent.id,
                model: config.model || this.nvidia.getDefaultModel()
            };
        }
        catch (error) {
            // Return error as single-item async generator
            const errorMessage = error instanceof Error ? error.message : String(error);
            async function* errorGen() {
                yield `Error: ${errorMessage}`;
            }
            return {
                success: false,
                content: errorGen(),
                agent: config.agent,
                model: config.model || this.nvidia.getDefaultModel()
            };
        }
    }
    /**
     * Build chat messages from agent
     */
    buildMessages(agent, task) {
        const systemPrompt = this.loader.getAgentSystemPrompt(agent);
        return [
            {
                role: 'system',
                content: systemPrompt
            },
            {
                role: 'user',
                content: task
            }
        ];
    }
    /**
     * Get execution count
     */
    getExecutedCount() {
        return this.executedCount;
    }
    /**
     * Reset execution count
     */
    resetCount() {
        this.executedCount = 0;
    }
    /**
     * Get available models
     */
    getAvailableModels() {
        return this.nvidia.getAvailableModels();
    }
    /**
     * Get default model
     */
    getDefaultModel() {
        return this.nvidia.getDefaultModel();
    }
}
exports.AgentExecutor = AgentExecutor;
/**
 * Quick execute function
 */
async function quickExecute(nvidia, loader, agentId, task, options) {
    const executor = new AgentExecutor(nvidia, loader);
    return executor.execute({
        agent: agentId,
        task,
        ...options
    });
}
//# sourceMappingURL=agent-executor.js.map