"use strict";
/**
 * Common CLI tool patterns for response detection
 * Shared between response-poller.ts and API routes
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.GEMINI_PROMPT_PATTERN = exports.CODEX_SEPARATOR_PATTERN = exports.CODEX_PROMPT_PATTERN = exports.CLAUDE_SEPARATOR_PATTERN = exports.CLAUDE_PROMPT_PATTERN = exports.CODEX_THINKING_PATTERN = exports.CLAUDE_THINKING_PATTERN = exports.CLAUDE_SPINNER_CHARS = void 0;
exports.detectThinking = detectThinking;
exports.getCliToolPatterns = getCliToolPatterns;
exports.stripAnsi = stripAnsi;
const logger_1 = require("./logger");
const logger = (0, logger_1.createLogger)('cli-patterns');
/**
 * Claude CLI spinner characters (expanded set)
 * These are shown when Claude is thinking/processing
 */
exports.CLAUDE_SPINNER_CHARS = [
    '✻', '✽', '⏺', '·', '∴', '✢', '✳', '✶',
    '⦿', '◉', '●', '○', '◌', '◎', '⊙', '⊚',
    '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏', // Braille spinner
];
/**
 * Claude thinking pattern
 * Matches spinner character followed by activity text ending with …
 * The text can contain spaces (e.g., "Verifying implementation (dead code detection)…")
 */
exports.CLAUDE_THINKING_PATTERN = new RegExp(`[${exports.CLAUDE_SPINNER_CHARS.join('')}]\\s+.+…|to interrupt\\)`, 'm');
/**
 * Codex thinking pattern
 * Matches activity indicators like "• Planning", "• Searching", etc.
 */
exports.CODEX_THINKING_PATTERN = /•\s*(Planning|Searching|Exploring|Running|Thinking|Working|Reading|Writing|Analyzing)/m;
/**
 * Claude prompt pattern (waiting for input)
 * Supports both legacy '>' and new '❯' (U+276F) prompt characters
 * Issue #132: Also matches prompts with recommended commands (e.g., "❯ /work-plan")
 *
 * Matches:
 * - Empty prompt: "❯ " or "> "
 * - Prompt with command: "❯ /work-plan" or "> npm install"
 */
exports.CLAUDE_PROMPT_PATTERN = /^[>❯](\s*$|\s+\S)/m;
/**
 * Claude separator pattern
 */
exports.CLAUDE_SEPARATOR_PATTERN = /^─{10,}$/m;
/**
 * Codex prompt pattern
 */
exports.CODEX_PROMPT_PATTERN = /^›\s+.+/m;
/**
 * Codex separator pattern
 */
exports.CODEX_SEPARATOR_PATTERN = /^─.*Worked for.*─+$/m;
/**
 * Gemini shell prompt pattern
 */
exports.GEMINI_PROMPT_PATTERN = /^(%|\$|.*@.*[%$#])\s*$/m;
/**
 * Detect if CLI tool is showing "thinking" indicator
 */
function detectThinking(cliToolId, content) {
    const log = logger.withContext({ cliToolId });
    log.debug('detectThinking:check', { contentLength: content.length });
    let result;
    switch (cliToolId) {
        case 'claude':
            result = exports.CLAUDE_THINKING_PATTERN.test(content);
            break;
        case 'codex':
            result = exports.CODEX_THINKING_PATTERN.test(content);
            break;
        case 'gemini':
            // Gemini doesn't have a thinking indicator in one-shot mode
            result = false;
            break;
        default:
            result = exports.CLAUDE_THINKING_PATTERN.test(content);
    }
    log.debug('detectThinking:result', { isThinking: result });
    return result;
}
/**
 * Get CLI tool patterns for response extraction
 */
function getCliToolPatterns(cliToolId) {
    switch (cliToolId) {
        case 'claude':
            return {
                promptPattern: exports.CLAUDE_PROMPT_PATTERN,
                separatorPattern: exports.CLAUDE_SEPARATOR_PATTERN,
                thinkingPattern: exports.CLAUDE_THINKING_PATTERN,
                skipPatterns: [
                    /^─{10,}$/, // Separator lines
                    /^[>❯]\s*$/, // Prompt line (legacy '>' and new '❯')
                    exports.CLAUDE_THINKING_PATTERN, // Thinking indicators
                    /^\s*[⎿⏋]\s+Tip:/, // Tip lines
                    /^\s*Tip:/, // Tip lines
                    /^\s*\?\s*for shortcuts/, // Shortcuts hint
                    /to interrupt\)/, // Part of "esc to interrupt" message
                ],
            };
        case 'codex':
            return {
                promptPattern: exports.CODEX_PROMPT_PATTERN,
                separatorPattern: exports.CODEX_SEPARATOR_PATTERN,
                thinkingPattern: exports.CODEX_THINKING_PATTERN,
                skipPatterns: [
                    /^─.*─+$/, // Separator lines
                    /^›\s*$/, // Empty prompt line
                    /^›\s+(Implement|Find and fix|Type)/, // New prompt suggestions
                    exports.CODEX_THINKING_PATTERN, // Activity indicators
                    /^\s*\d+%\s+context left/, // Context indicator
                    /^\s*for shortcuts$/, // Shortcuts hint
                    /╭─+╮/, // Box drawing (top)
                    /╰─+╯/, // Box drawing (bottom)
                ],
            };
        case 'gemini':
            return {
                promptPattern: exports.GEMINI_PROMPT_PATTERN,
                separatorPattern: /^gemini\s+--\s+/m,
                thinkingPattern: /(?!)/m, // Never matches - one-shot execution
                skipPatterns: [
                    /^gemini\s+--\s+/, // Command line itself
                    exports.GEMINI_PROMPT_PATTERN, // Shell prompt lines
                    /^\s*$/, // Empty lines
                ],
            };
        default:
            // Default to Claude patterns
            return getCliToolPatterns('claude');
    }
}
/**
 * Strip ANSI escape codes from a string
 * Optimized version at module level for performance
 */
const ANSI_PATTERN = /\x1b\[[0-9;]*[a-zA-Z]|\x1b\][^\x07]*\x07|\[[0-9;]*m/g;
function stripAnsi(str) {
    return str.replace(ANSI_PATTERN, '');
}
