import { sanitizeLegacyPlannerContext } from "./legacyContextSanitizer.js";
export function buildSessionContext(records, options = {}) {
    const keepTailMessages = options.keepTailMessages ?? 10;
    const maxSummaryChars = options.maxSummaryChars ?? 5000;
    const maxSelectedRecords = options.maxSelectedRecords ?? 28;
    const chatRecords = records.filter(isChatRecord);
    const tailRecords = chatRecords.slice(-keepTailMessages);
    const tailIds = new Set(tailRecords.map((record) => record.id));
    const olderRecords = records.filter((record) => !tailIds.has(record.id));
    const selectedRecords = olderRecords
        .map((record, index) => ({
        record,
        index,
        score: scoreTranscriptRecord(record, index, records.length),
    }))
        .filter((entry) => entry.score > 0)
        .sort((a, b) => b.score - a.score || a.index - b.index)
        .slice(0, maxSelectedRecords)
        .sort((a, b) => a.index - b.index)
        .map((entry) => entry.record);
    const summary = dedupeSummaryRecords(selectedRecords)
        .map(formatSummaryRecord)
        .filter(Boolean)
        .join("\n")
        .slice(-maxSummaryChars)
        .trim();
    return {
        history: tailRecords.map(recordToChatMessage),
        summary,
        selectedRecords,
        totalRecords: records.length,
    };
}
function dedupeSummaryRecords(records) {
    const seen = new Set();
    return records.filter((record) => {
        const key = summaryDedupeKey(record);
        if (!key)
            return true;
        if (seen.has(key))
            return false;
        seen.add(key);
        return true;
    });
}
function summaryDedupeKey(record) {
    const text = record.text ?? "";
    if (/tdai_memory_search\s+succeeded/i.test(text))
        return "tool:tdai_memory_search:succeeded";
    if (/tdai_conversation_search\s+succeeded/i.test(text))
        return "tool:tdai_conversation_search:succeeded";
    return undefined;
}
export function scoreTranscriptRecord(record, index = 0, total = 1) {
    const text = record.text ?? "";
    const recency = Math.max(0, Math.min(20, index === total - 1 ? 20 : 20 - Math.floor((total - index) / 4)));
    let score = recency;
    if (record.role === "error")
        score += 120;
    if (record.role === "tool")
        score += 45;
    if (record.role === "user")
        score += 70;
    if (record.role === "assistant")
        score += 35;
    if (record.role === "system")
        score += /resume|compact|validation|approval|cache|session/i.test(text) ? 30 : -20;
    if (/failed|error|exception|traceback|stderr|失败|错误|异常|未通过/i.test(text))
        score += 90;
    if (/\b[\w./\\-]+\.(ts|tsx|js|mjs|json|md|html|docx|pptx|pdf|py|css)\b/i.test(text))
        score += 45;
    if (/write_file|apply_patch|run_command|validate_artifact|create_docx|create_pptx|artifact|产物|验证/i.test(text)) {
        score += 45;
    }
    if (/todo|remaining|continue|下一步|继续|未完成/i.test(text))
        score += 35;
    return Math.max(0, score);
}
function isChatRecord(record) {
    return record.role === "user" || record.role === "assistant" || record.role === "system";
}
function recordToChatMessage(record) {
    return {
        role: record.role === "assistant" ? "assistant" : record.role === "system" ? "system" : "user",
        content: sanitizeLegacyPlannerContext(record.text),
    };
}
function formatSummaryRecord(record) {
    const text = compact(sanitizeLegacyPlannerContext(record.text), record.role === "tool" || record.role === "error" ? 900 : 520);
    if (!text)
        return "";
    return `- ${record.role}${record.runId ? ` run=${record.runId}` : ""}: ${text}`;
}
function compact(value, max) {
    const normalized = value.replace(/\s+/g, " ").trim();
    if (normalized.length <= max)
        return normalized;
    return `${normalized.slice(0, Math.max(0, max - 28)).trimEnd()} ... [truncated]`;
}
