import { z } from 'zod';
import { GeminiRunner } from '../services/GeminiRunner.js';
import { storeRun } from '../memory/MemoryFactory.js';
import { getWorkspaceDir } from '../lib/config.js';
import { deleteSessionId } from '../lib/sessions.js';
export const runGeminiSchema = z.object({
    prompt: z.string().describe("Le prompt à envoyer à l'agent Gemini (Antigravity)"),
    sessionId: z.string().optional(),
    agentName: z.string().optional(),
    autoResume: z.boolean().optional().default(false),
    path: z.string().optional(),
    config: z.string().optional(),
    silent: z.boolean().optional().default(false),
    /** Mode Antigravity (défaut: GENERAL) */
    mode: z.enum(['GENERAL', 'CONTEXT_CHECK', 'PLAN', 'COMMAND', 'CASCADE', 'EVAL', 'ANTIGRAVITY_REVIEW', 'MQUERY', 'COMMIT_MESSAGE', 'CHECKPOINT', 'FAST_APPLY']).optional().default('GENERAL'),
});
export async function runGeminiAgent(args) {
    const runner = new GeminiRunner();
    const { prompt, agentName, autoResume, sessionId, path: argPath, config: argConfig, silent, mode, } = args;
    const finalPath = argPath || getWorkspaceDir();
    const finalConfig = argConfig || getWorkspaceDir();
    const start = Date.now();
    let result = await runner.runAgent({
        prompt,
        agentName,
        autoResume,
        sessionId,
        cwd: finalPath,
        configPath: finalConfig,
        silent,
        mode,
    });
    // Retry if session invalid
    if (result.error?.includes('session') || result.error?.includes('EXIT_CODE_1')) {
        if (agentName)
            await deleteSessionId(agentName, finalConfig, 'gemini');
        result = await runner.runAgent({
            prompt,
            agentName,
            autoResume: false,
            sessionId: undefined,
            cwd: finalPath,
            configPath: finalConfig,
            silent,
            mode,
        });
    }
    const durationMs = Date.now() - start;
    try {
        await storeRun({
            runner: 'gemini',
            agentName,
            prompt,
            result: result.result,
            error: result.error,
            durationMs,
            success: !result.error,
            sessionId: result.sessionId,
        });
    }
    catch (e) {
        console.error(`[run_gemini] ⚠️ Memory store failed: ${e instanceof Error ? e.message : String(e)}`);
    }
    if (result.error === 'INVALID_AGENT') {
        return {
            content: [{ type: 'text', text: `❌ Agent Gemini '${agentName}' introuvable.` }],
            isError: true,
        };
    }
    if (result.error) {
        return {
            content: [{ type: 'text', text: `❌ Erreur Gemini: ${result.error}` }],
            isError: true,
        };
    }
    return {
        content: [
            { type: 'text', text: result.result },
            ...(result.sessionId
                ? [{ type: 'text', text: `SESSION_ID: ${result.sessionId}` }]
                : []),
        ],
    };
}
//# sourceMappingURL=run_gemini.js.map