/**
 * Knowledge Consumption Snapshot — 问题五十七核心机制之三。
 *
 * 追踪知识资产在 prompt 构建过程中的消费路径：
 * - 哪些资产被索引
 * - 哪些资产被注入 prompt
 * - 哪些资产被过滤及原因
 * - 哪些资产被降级及原因
 * - 冲突裁决结果
 */
// ── 核心函数 ──
/**
 * 创建知识消费快照。
 */
export function createKnowledgeConsumptionSnapshot(params) {
    return {
        build_id: params.build_id,
        route: params.route,
        indexed_assets: params.indexed_assets,
        injected_assets: params.injected_assets,
        filtered_assets: params.filtered_assets ?? [],
        degraded_assets: params.degraded_assets ?? [],
        conflict_resolutions: params.conflict_resolutions ?? [],
        generated_at: new Date().toISOString(),
        total_indexed: params.indexed_assets.length,
        total_injected: params.injected_assets.length,
        total_filtered: params.filtered_assets?.length ?? 0,
        total_degraded: params.degraded_assets?.length ?? 0,
    };
}
/**
 * 验证消费快照的完整性。
 * 必须包含 indexed/injected/filtered/degraded/conflict_resolutions 五个维度。
 */
export function validateConsumptionSnapshot(snapshot) {
    const issues = [];
    if (!snapshot.build_id)
        issues.push("快照缺少 build_id");
    if (!snapshot.route)
        issues.push("快照缺少 route");
    if (snapshot.indexed_assets.length === 0)
        issues.push("无索引资产 — 知识索引构建可能失败");
    if (snapshot.total_indexed === 0 && snapshot.total_injected === 0) {
        issues.push("索引和注入均为空 — 知识系统可能未正常工作");
    }
    // 检查 filtered 是否有合法原因
    for (const fa of snapshot.filtered_assets) {
        if (!fa.reason) {
            issues.push(`过滤资产 ${fa.asset_id} 缺少过滤原因`);
        }
    }
    // 检查 degraded 是否有合法原因
    for (const da of snapshot.degraded_assets) {
        if (!da.reason) {
            issues.push(`降级资产 ${da.asset_id} 缺少降级原因`);
        }
    }
    // 检查 conflict_resolutions
    for (const cr of snapshot.conflict_resolutions) {
        if (!cr.reason) {
            issues.push(`冲突裁决 winner=${cr.winner} 缺少原因`);
        }
    }
    return {
        passed: issues.length === 0,
        issues,
        snapshot,
    };
}
/**
 * 生成消费快照摘要文本。
 */
export function summarizeConsumptionSnapshot(snapshot) {
    const parts = [];
    parts.push(`知识消费快照: 索引 ${snapshot.total_indexed} 个, 注入 ${snapshot.total_injected} 个`);
    if (snapshot.total_filtered > 0) {
        parts.push(`过滤 ${snapshot.total_filtered} 个: ${snapshot.filtered_assets.map((f) => f.asset_id).join(", ")}`);
    }
    if (snapshot.total_degraded > 0) {
        parts.push(`降级 ${snapshot.total_degraded} 个: ${snapshot.degraded_assets.map((d) => d.asset_id).join(", ")}`);
    }
    if (snapshot.conflict_resolutions.length > 0) {
        parts.push(`冲突裁决 ${snapshot.conflict_resolutions.length} 项`);
    }
    return parts.join("; ");
}
/**
 * 检查消费链路是否可追踪 — route/expander/verifier/adapter prompt 是否消费了标准字段。
 */
export function verifyConsumptionPaths(params) {
    const requiredByComponent = {
        "intent router": ["routes", "primary_triggers", "negative_triggers", "specificity"],
        expander: ["authority", "priority", "required_evidence", "asset_kind"],
        verifier: ["hard_blocks", "required_evidence", "fallback", "lifecycle_status"],
        "adapter prompt": ["中文语义", "模板变量", "输出格式", "消费快照摘要"],
        "sync migration": ["version", "schema", "hash", "user_modified", "migration_note"],
    };
    const missing = [];
    for (const [component, required] of Object.entries(requiredByComponent)) {
        const consumed = component === "intent router" ? params.intent_router_consumes
            : component === "expander" ? params.expander_consumes
                : component === "verifier" ? params.verifier_consumes
                    : component === "adapter prompt" ? params.adapter_prompt_consumes
                        : params.sync_migration_consumes;
        for (const field of required) {
            if (!consumed || !consumed.includes(field)) {
                missing.push(`${component} 未消费必需字段: ${field}`);
            }
        }
    }
    return { passed: missing.length === 0, missing };
}
//# sourceMappingURL=knowledge_consumption_snapshot.js.map