import { recommendModel } from '../token-tracker.js';
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/** Analyze session operations and produce optimization recommendations. */
export function analyzeSession(ops, status) {
    return {
        currentUsage: status,
        delegationHints: generateDelegationHints(ops),
        filesAlreadyRead: extractFilesRead(ops),
        topTokenConsumers: status.topConsumers,
    };
}
/** Generate delegation hints based on session operation patterns. */
export function generateDelegationHints(ops) {
    const hints = [];
    const toolCounts = countByTool(ops);
    // Repeated reads → suggest Explore subagent
    const readOps = toolCounts.consult_docs ?? 0;
    if (readOps >= 3) {
        hints.push({
            operation: 'consult_docs',
            reason: `Called ${String(readOps)} times — delegate batch lookups to an Explore subagent`,
            suggestedModel: 'haiku',
        });
    }
    // Heavy audit → suggest delegation
    const auditOps = toolCounts.audit ?? 0;
    if (auditOps >= 2) {
        hints.push({
            operation: 'audit',
            reason: `${String(auditOps)} audit calls consume significant context — delegate to a dedicated subagent`,
            suggestedModel: recommendModel('audit'),
        });
    }
    // Many generate_tests calls
    const testOps = toolCounts.generate_tests ?? 0;
    if (testOps >= 2) {
        hints.push({
            operation: 'generate_tests',
            reason: `${String(testOps)} test generation calls — consider batching in a subagent`,
            suggestedModel: 'sonnet',
        });
    }
    // Full-llm operations consuming too much
    const fullLlmOps = ops.filter((op) => op.tokens >= 8000);
    if (fullLlmOps.length >= 3) {
        hints.push({
            operation: 'full-llm-operations',
            reason: `${String(fullLlmOps.length)} heavy operations detected — reduce by delegating to subagents with appropriate models`,
            suggestedModel: 'sonnet',
        });
    }
    return hints;
}
/** Build a ready-to-use Explore subagent prompt for batch file reading. */
export function suggestExplorePrompt(files) {
    if (files.length === 0) {
        return '';
    }
    const fileList = files.map((f) => `- ${f}`).join('\n');
    return [
        'Read and summarize the following files. For each file, provide:',
        '1. Purpose (one sentence)',
        '2. Key exports (functions, types, constants)',
        '3. Dependencies (imports from other project modules)',
        '',
        'Files:',
        fileList,
    ].join('\n');
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
function countByTool(ops) {
    const counts = {};
    for (const op of ops) {
        counts[op.tool] = (counts[op.tool] ?? 0) + 1;
    }
    return counts;
}
function extractFilesRead(ops) {
    // Tools that typically involve reading files
    const readTools = new Set(['consult_docs', 'audit', 'reverse_engineer', 'detect_drift']);
    return ops.filter((op) => readTools.has(op.tool)).map((op) => op.tool);
}
//# sourceMappingURL=optimizer.js.map