"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UsoAgent = void 0;
const state_1 = require("./state");
const agent_config_1 = require("./agent-config");
const llm_router_1 = require("./llm-router");
const brain_1 = require("./brain");
const fsm_1 = require("./fsm");
const tool_registry_1 = require("./tool-registry");
const terminal_1 = require("./tools/terminal");
const environment_1 = require("./tools/environment");
const filesystem_1 = require("./tools/filesystem");
const solana_1 = require("./tools/solana");
const discovery_1 = require("../runtime/discovery");
const toolchain_1 = require("../runtime/toolchain");
/**
 * UsoAgent — The public API for the USO AI Agent.
 *
 * Usage:
 * ```typescript
 * const agent = new UsoAgent();
 * const result = await agent.run({ goal: "Deploy my staking program" });
 * ```
 */
class UsoAgent {
    config;
    constructor(config) {
        this.config = config ?? (0, agent_config_1.loadAgentConfig)();
    }
    /**
     * Run the agent to accomplish a goal.
     */
    async run(options) {
        const startMs = Date.now();
        // 1. Resolve LLM
        const router = new llm_router_1.LlmRouter(this.config);
        const llm = await router.resolve(options.provider);
        // 2. Build tool registry
        const tools = this.buildToolRegistry();
        // 3. Initialize state
        const maxIterations = options.maxIterations ??
            this.config.agent?.maxIterations ??
            3;
        let state = (0, state_1.createInitialState)(options.goal, maxIterations);
        // 4. Discover runtime environment
        const runtime = (0, discovery_1.discoverRuntime)({
            projectRoot: process.cwd(),
            runtime: { preferWsl: false },
            healing: { enabled: true, maxAttempts: 2, backoffMs: 250 },
        });
        const toolchain = (0, toolchain_1.fingerprintToolchain)(true);
        state = (0, state_1.setEnvironment)(state, {
            os: runtime.os,
            route: runtime.route,
            toolchain,
        });
        // 5. Build brain + FSM
        const brain = new brain_1.AgentBrain(llm, tools);
        const fsm = new fsm_1.AgentFSM(brain, options.callbacks);
        // 6. Run the FSM
        const finalState = await fsm.run(state);
        // 7. Build result
        const durationMs = Date.now() - startMs;
        const success = finalState.currentPhase === "complete";
        const lastComplete = finalState.history
            .filter((h) => h.role === "system")
            .pop();
        return {
            success,
            summary: success
                ? lastComplete?.content ?? "Task completed successfully."
                : finalState.errors[finalState.errors.length - 1]?.message ??
                    "Task failed.",
            state: finalState,
            llmProvider: llm.name,
            totalIterations: finalState.iterationCount,
            durationMs,
        };
    }
    /**
     * Diagnose available LLM providers (for `uso doctor`).
     */
    async diagnoseLlm() {
        const router = new llm_router_1.LlmRouter(this.config);
        return router.diagnose();
    }
    /**
     * Build the tool registry with all built-in tools.
     */
    buildToolRegistry() {
        const registry = new tool_registry_1.ToolRegistry();
        // Core tools
        registry.register(terminal_1.runTerminalCommandTool);
        registry.register(environment_1.checkEnvironmentTool);
        registry.register(environment_1.checkWalletBalanceTool);
        registry.register(environment_1.checkClusterTool);
        // Filesystem tools
        registry.register(filesystem_1.readFileTool);
        registry.register(filesystem_1.writeFileTool);
        registry.register(filesystem_1.listDirectoryTool);
        // Solana tools
        registry.register(solana_1.anchorBuildTool);
        registry.register(solana_1.anchorTestTool);
        registry.register(solana_1.anchorDeployTool);
        registry.register(solana_1.solanaAirdropTool);
        return registry;
    }
}
exports.UsoAgent = UsoAgent;
//# sourceMappingURL=agent.js.map