"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_RAW_TOOL_PRESERVE = exports.DEFAULT_MAX_NOTE_CHARS = void 0;
exports.buildRecoveryNote = buildRecoveryNote;
exports.DEFAULT_MAX_NOTE_CHARS = 800;
exports.DEFAULT_RAW_TOOL_PRESERVE = 5;
function summariseToolCall(call) {
    const short = call.toolName.replace('mcp__smartperfetto__', '');
    const skill = call.skillId ? `(${call.skillId})` : '';
    const input = call.inputSummary ? ` — ${call.inputSummary}` : '';
    const phase = call.matchedPhaseId ? ` [phase:${call.matchedPhaseId}]` : '';
    return `· ${short}${skill}${input}${phase}`.slice(0, 160);
}
/**
 * Build the recovery note as a single string plus structured metadata.
 * Pure function — does not touch sessionNotes or the SDK.
 */
function buildRecoveryNote(input) {
    const maxChars = input.maxChars ?? exports.DEFAULT_MAX_NOTE_CHARS;
    const rawToolPreserve = Math.max(0, input.rawToolPreserve ?? exports.DEFAULT_RAW_TOOL_PRESERVE);
    const sections = [];
    const sectionsIncluded = [];
    let usedChars = 0;
    const tryAdd = (label, section) => {
        if (usedChars + section.length + 1 > maxChars)
            return false;
        sections.push(section);
        sectionsIncluded.push(label);
        usedChars += section.length + 1;
        return true;
    };
    tryAdd('header', '[上下文压缩恢复] SDK 自动压缩已触发。');
    // Section 1: plan progress
    if (input.plan) {
        const planLines = input.plan.phases.map(p => {
            const icon = p.status === 'completed'
                ? '✓'
                : p.status === 'skipped'
                    ? '⊘'
                    : p.status === 'in_progress'
                        ? '→'
                        : '○';
            const summary = p.summary ? ` — ${p.summary.substring(0, 60)}` : '';
            return `${icon} ${p.id}: ${p.name}${summary}`;
        });
        if (planLines.length > 0) {
            tryAdd('plan_progress', `分析进度:\n${planLines.join('\n')}`);
        }
        // Section 2: next/in-progress phase pointer
        const nextPhase = input.plan.phases.find(p => p.status === 'pending' || p.status === 'in_progress');
        if (nextPhase) {
            const tools = nextPhase.expectedTools.length > 0
                ? ` (工具: ${nextPhase.expectedTools.join(', ')})`
                : '';
            tryAdd('next_phase', `当前/下一阶段: ${nextPhase.name} — ${nextPhase.goal}${tools}`);
        }
    }
    // Section 3: recent raw tool origins (Phase 3-2 addition)
    if (rawToolPreserve > 0 && input.recentToolCalls && input.recentToolCalls.length > 0) {
        const recent = input.recentToolCalls.slice(-rawToolPreserve);
        const toolLines = recent.map(summariseToolCall).join('\n');
        tryAdd('recent_tool_calls', `近 ${recent.length} 次工具调用（结构化摘要）:\n${toolLines}`);
    }
    // Section 4: key findings (confident first)
    if (input.findings && input.findings.length > 0) {
        const confident = input.findings.filter(f => (f.confidence ?? 0.5) >= 0.5);
        const list = confident.length > 0 ? confident.slice(0, 5) : input.findings.slice(0, 3);
        const summary = list.map(f => `- [${f.severity}] ${f.title}`).join('\n');
        tryAdd('findings', `关键发现:\n${summary}`);
    }
    // Section 5: entity context (lowest priority)
    if (input.entitySnapshot && input.entitySnapshot.length < 200) {
        tryAdd('entity_context', `已知实体:\n${input.entitySnapshot}`);
    }
    return {
        text: sections.join('\n'),
        sectionsIncluded,
        usedChars,
    };
}
//# sourceMappingURL=recoveryNoteBuilder.js.map