import { RuntimeAgentService } from './runtime-agent.service.abstract.js';
import { RUNTIME_TYPES } from '../../constants.js';
/**
 * OpenAI Codex CLI specific runtime service implementation.
 * Handles Codex CLI initialization, detection, and interaction patterns.
 */
export class CodexRuntimeService extends RuntimeAgentService {
    constructor(sessionHelper, projectRoot) {
        super(sessionHelper, projectRoot);
    }
    getRuntimeType() {
        return RUNTIME_TYPES.CODEX_CLI;
    }
    /**
     * Codex CLI runtime detection.
     *
     * NOTE: Do not use active key probes (Ctrl+C, '/', etc.) here. Codex can
     * interpret those as shell input/cancel and drop back to zsh, which causes
     * false negatives and unintended exits during health checks.
     */
    async detectRuntimeSpecific(sessionName) {
        const output = this.sessionHelper.capturePane(sessionName, 120);
        const readyPatterns = this.getRuntimeReadyPatterns();
        const hasReadySignal = readyPatterns.some((pattern) => output.includes(pattern));
        this.logger.debug('Codex detection completed', {
            sessionName,
            hasReadySignal,
        });
        return hasReadySignal;
    }
    /**
     * Codex CLI specific ready patterns
     */
    getRuntimeReadyPatterns() {
        return [
            'Codex CLI',
            'codex>',
            'Ready for commands',
            'OpenAI Codex',
            'model:',
            'token:',
            'Connected to OpenAI',
            'Welcome to Codex',
            'Initialized successfully',
        ];
    }
    /**
     * Codex CLI specific exit patterns for runtime exit detection
     */
    getRuntimeExitPatterns() {
        return [
            /codex.*exited/i,
            /Session\s+ended/i,
            /Conversation interrupted/i,
        ];
    }
    /**
     * Codex CLI specific error patterns
     */
    getRuntimeErrorPatterns() {
        const commonErrors = ['Permission denied', 'No such file or directory'];
        return [
            ...commonErrors,
            'command not found: codex',
            'OpenAI API error',
            'Authentication failed',
            'Invalid API key',
            'Rate limit exceeded',
            'Token limit exceeded',
        ];
    }
    /**
     * Check if Codex CLI is installed and configured
     */
    async checkCodexInstallation() {
        try {
            // This would check if Codex CLI is available
            // Could run: codex --version or similar
            return {
                isInstalled: true,
                message: 'OpenAI Codex CLI is available',
            };
        }
        catch (error) {
            return {
                isInstalled: false,
                message: 'OpenAI Codex CLI not found or not configured',
            };
        }
    }
    /**
     * Initialize Codex in an existing session
     */
    async initializeCodexInSession(sessionName) {
        try {
            await this.executeRuntimeInitScript(sessionName);
            return {
                success: true,
                message: 'OpenAI Codex CLI initialized successfully',
            };
        }
        catch (error) {
            return {
                success: false,
                message: error instanceof Error
                    ? error.message
                    : 'Failed to initialize OpenAI Codex CLI',
            };
        }
    }
}
//# sourceMappingURL=codex-runtime.service.js.map