"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.SkillAnalysisAdapter = void 0;
exports.getSkillAnalysisAdapter = getSkillAnalysisAdapter;
exports.createSkillAnalysisAdapter = createSkillAnalysisAdapter;
const skillExecutor_1 = require("./skillExecutor");
const skillLoader_1 = require("./skillLoader");
const smartSummaryGenerator_1 = require("./smartSummaryGenerator");
const answerGenerator_1 = require("./answerGenerator");
const eventCollector_1 = require("./eventCollector");
// =============================================================================
// Skill Analysis Adapter
// =============================================================================
class SkillAnalysisAdapter {
    constructor(traceProcessor, eventHandler) {
        this.initialized = false;
        this.traceProcessor = traceProcessor;
        this.eventHandler = eventHandler;
        // 创建 executor，传入事件处理器
        this.executor = (0, skillExecutor_1.createSkillExecutor)(traceProcessor, undefined, // AI service 稍后注入
        eventHandler);
        // Wire fragment cache from the skill registry (if loaded)
        if (skillLoader_1.skillRegistry.isInitialized()) {
            this.executor.setFragmentRegistry(skillLoader_1.skillRegistry.getFragmentCache());
        }
    }
    /**
     * 设置 AI 服务（用于 ai_decision 和 ai_summary 步骤）
     */
    setAIService(aiService) {
        // SkillExecutor 需要支持 setAIService 方法
        this.executor.aiService = aiService;
    }
    /**
     * 设置事件处理器
     */
    setEventHandler(handler) {
        this.eventHandler = handler;
        this.executor.eventEmitter = handler;
    }
    /**
     * 确保 skill registry 已初始化
     */
    async ensureInitialized() {
        if (this.initialized)
            return;
        await (0, skillLoader_1.ensureSkillRegistryInitialized)();
        // 将所有 skills 注册到 executor
        const skills = skillLoader_1.skillRegistry.getAllSkills();
        this.executor.registerSkills(skills);
        this.initialized = true;
        console.log(`[SkillAnalysisAdapter] Initialized with ${skills.length} skills`);
    }
    /**
     * 从自然语言问题检测意图
     * 返回匹配的 skill ID 或 null
     */
    detectIntent(question) {
        const skill = skillLoader_1.skillRegistry.findMatchingSkill(question);
        return skill ? skill.name : null;
    }
    /**
     * 检测厂商（从 trace 数据）
     */
    async detectVendor(traceId) {
        try {
            // HarmonyOS 检测：通过 tracing_mark_write 中的 HarmonyOS 独有标签
            try {
                const harmonyCheck = await this.traceProcessor.query(traceId, `
          SELECT 1 FROM slice
          WHERE (LOWER(name) LIKE '%ace::%' OR LOWER(name) LIKE '%rsrender%'
             OR LOWER(name) LIKE '%ffrt%' OR LOWER(name) LIKE '%arkts%'
             OR LOWER(name) LIKE '%harmonyos%' OR LOWER(name) LIKE '%ohos.%')
          LIMIT 1`);
                if (harmonyCheck.rows && harmonyCheck.rows.length > 0) {
                    return { vendor: 'harmonyos', confidence: 0.85 };
                }
            }
            catch { /* content check failed */ }
            // Android vendor detection via slice table
            const result = await this.traceProcessor.query(traceId, `
        SELECT
          CASE
            WHEN EXISTS(
              SELECT 1 FROM slice
              WHERE name IS NOT NULL
                AND (LOWER(name) LIKE '%miui%' OR LOWER(name) LIKE '%hyperos%' OR LOWER(name) LIKE '%xiaomi%')
            ) THEN 'xiaomi'
            WHEN EXISTS(
              SELECT 1 FROM slice
              WHERE name IS NOT NULL
                AND (LOWER(name) LIKE '%oppo%' OR LOWER(name) LIKE '%coloros%' OR LOWER(name) LIKE '%oplus%')
            ) THEN 'oppo'
            WHEN EXISTS(
              SELECT 1 FROM slice
              WHERE name IS NOT NULL
                AND (LOWER(name) LIKE '%vivo%' OR LOWER(name) LIKE '%originos%' OR LOWER(name) LIKE '%funtouch%' OR LOWER(name) LIKE '%bbk%')
            ) THEN 'vivo'
            WHEN EXISTS(
              SELECT 1 FROM slice
              WHERE name IS NOT NULL
                AND (LOWER(name) LIKE '%honor%' OR LOWER(name) LIKE '%magicos%' OR LOWER(name) LIKE '%huawei%')
            ) THEN 'honor'
            WHEN EXISTS(
              SELECT 1 FROM slice
              WHERE name IS NOT NULL
                AND (LOWER(name) LIKE '%samsung%' OR LOWER(name) LIKE '%oneui%' OR LOWER(name) LIKE '%sec.%' OR LOWER(name) LIKE '%galaxy%')
            ) THEN 'samsung'
            WHEN EXISTS(
              SELECT 1 FROM slice
              WHERE name IS NOT NULL
                AND (LOWER(name) LIKE '%pixel%' OR LOWER(name) LIKE '%tensor%' OR LOWER(name) LIKE '%google%')
            ) THEN 'pixel'
            WHEN EXISTS(
              SELECT 1 FROM slice
              WHERE name IS NOT NULL
                AND (LOWER(name) LIKE '%qualcomm%' OR LOWER(name) LIKE '%qcom%' OR LOWER(name) LIKE '%snapdragon%' OR LOWER(name) LIKE '%adreno%')
            ) THEN 'qualcomm'
            WHEN EXISTS(
              SELECT 1 FROM slice
              WHERE name IS NOT NULL
                AND (LOWER(name) LIKE '%mediatek%' OR LOWER(name) LIKE '%mtk%' OR LOWER(name) LIKE '%dimensity%' OR LOWER(name) LIKE '%helio%')
            ) THEN 'mtk'
            ELSE 'aosp'
          END as vendor
      `);
            if (result.rows && result.rows.length > 0) {
                const detectedVendor = String(result.rows[0][0] || 'aosp');
                return {
                    vendor: detectedVendor,
                    confidence: detectedVendor === 'aosp' ? 0.5 : 0.8,
                };
            }
        }
        catch (error) {
            console.warn('[SkillAnalysisAdapter] Vendor detection failed:', error);
        }
        return { vendor: 'aosp', confidence: 0.5 };
    }
    /**
     * 检测 skill 是否使用分层输出（overview/list/session/deep）
     */
    hasLayeredOutput(skill) {
        if (!skill.steps || skill.steps.length === 0) {
            return false;
        }
        // 检查是否有任何 step 定义了 layer 属性
        return skill.steps.some(step => step.display && typeof step.display === 'object' && 'layer' in step.display);
    }
    collectLayeredSteps(layeredResult) {
        const byId = new Map();
        for (const step of layeredResult.stepResults || []) {
            byId.set(step.stepId, step);
        }
        for (const step of Object.values(layeredResult.layers.overview || {}))
            byId.set(step.stepId, step);
        for (const step of Object.values(layeredResult.layers.list || {}))
            byId.set(step.stepId, step);
        for (const step of Object.values(layeredResult.layers.diagnosis || {}))
            byId.set(step.stepId, step);
        for (const sessionData of Object.values(layeredResult.layers.session || {})) {
            for (const step of Object.values(sessionData))
                byId.set(step.stepId, step);
        }
        for (const sessionData of Object.values(layeredResult.layers.deep || {})) {
            for (const step of Object.values(sessionData))
                byId.set(step.stepId, step);
        }
        return [...byId.values()];
    }
    collectLayeredFailures(layeredResult) {
        return this.collectLayeredSteps(layeredResult).filter(step => !step.success);
    }
    /**
     * 执行 skill 分析
     * 这是主要的入口点
     */
    async analyze(request) {
        await this.ensureInitialized();
        const { traceId, skillId, question, packageName } = request;
        // 确定使用哪个 skill
        let targetSkillId = skillId;
        if (!targetSkillId && question) {
            targetSkillId = this.detectIntent(question) || undefined;
        }
        if (!targetSkillId) {
            return {
                skillId: 'unknown',
                skillName: 'Unknown',
                success: false,
                sections: {},
                diagnostics: [{
                        id: 'no_skill_match',
                        severity: 'warning',
                        message: '无法匹配到合适的分析技能',
                        suggestions: [
                            '尝试使用关键词：启动、滑动、卡顿、内存、CPU、Binder',
                            '使用 skillId 参数指定具体的技能',
                        ],
                    }],
                summary: '无法确定使用哪个分析技能',
                executionTimeMs: 0,
            };
        }
        // 获取 skill 信息
        const skill = skillLoader_1.skillRegistry.getSkill(targetSkillId);
        if (!skill) {
            return {
                skillId: targetSkillId,
                skillName: targetSkillId,
                success: false,
                sections: {},
                diagnostics: [{
                        id: 'skill_not_found',
                        severity: 'critical',
                        message: `技能未找到: ${targetSkillId}`,
                    }],
                summary: `技能未找到: ${targetSkillId}`,
                executionTimeMs: 0,
            };
        }
        // 检测厂商
        const vendorResult = await this.detectVendor(traceId);
        // 构建参数 - 合并 request.params 和 packageName
        const params = {
            ...request.params, // 先展开用户提供的参数
        };
        if (packageName) {
            params.package = packageName; // packageName 会覆盖 params.package (如果有)
        }
        // 创建事件收集器
        const eventCollector = (0, eventCollector_1.createEventCollector)();
        const totalSteps = skill.steps?.length || 1;
        eventCollector.start(targetSkillId, totalSteps);
        // 设置事件处理器（同时转发到外部处理器和收集器）
        const combinedHandler = (event) => {
            eventCollector.addEvent(event);
            if (this.eventHandler) {
                this.eventHandler(event);
            }
        };
        this.executor.eventEmitter = combinedHandler;
        // 检测是否使用分层输出
        const useLayeredOutput = this.hasLayeredOutput(skill);
        console.log(`[SkillAnalysisAdapter] Skill ${targetSkillId} has layered output:`, useLayeredOutput);
        let result;
        let layeredResult;
        if (useLayeredOutput) {
            // 使用分层输出模式（executeCompositeSkill）
            console.log('[SkillAnalysisAdapter] Using layered output mode for skill', targetSkillId);
            try {
                layeredResult = await this.executor.executeCompositeSkill(skill, params, { traceId, vendor: vendorResult.vendor });
                console.log('[SkillAnalysisAdapter] executeCompositeSkill completed. layeredResult:', JSON.stringify({
                    hasLayers: !!layeredResult?.layers,
                    layerKeys: layeredResult?.layers ? Object.keys(layeredResult.layers) : [],
                    overviewCount: layeredResult?.layers?.overview ? Object.keys(layeredResult.layers.overview).length : 0,
                    listCount: layeredResult?.layers?.list ? Object.keys(layeredResult.layers.list).length : 0,
                    sessionCount: layeredResult?.layers?.session ? Object.keys(layeredResult.layers.session).length : 0,
                    deepSessionCount: layeredResult?.layers?.deep ? Object.keys(layeredResult.layers.deep).length : 0,
                    hasMetadata: !!layeredResult?.metadata,
                }, null, 2));
                // 将 LayeredResult 转换为 displayResults 格式
                console.log('[SkillAnalysisAdapter] Calling convertLayeredResultToDisplayResults...');
                const displayResults = this.convertLayeredResultToDisplayResults(layeredResult);
                console.log('[SkillAnalysisAdapter] convertLayeredResultToDisplayResults completed with', displayResults.length, 'items');
                const failedSteps = this.collectLayeredFailures(layeredResult);
                result = {
                    success: failedSteps.length === 0,
                    displayResults,
                    diagnostics: failedSteps.map(step => ({
                        id: `step_failed_${step.stepId}`,
                        severity: 'critical',
                        diagnosis: `Step ${step.stepId} failed: ${step.error || 'unknown error'}`,
                        suggestions: ['检查输入参数、SQL 语义和 trace schema 兼容性'],
                    })),
                    executionTimeMs: 0,
                    error: failedSteps.length > 0
                        ? `Failed step(s): ${failedSteps.map(step => `${step.stepId}${step.error ? ` (${step.error})` : ''}`).join(', ')}`
                        : undefined,
                };
            }
            catch (error) {
                console.error('[SkillAnalysisAdapter] executeCompositeSkill failed:', error);
                result = {
                    success: false,
                    displayResults: [],
                    diagnostics: [{
                            id: 'skill_execution_failed',
                            severity: 'critical',
                            diagnosis: error?.message || 'Skill execution failed',
                            suggestions: ['检查输入参数、SQL 语义和 trace schema 兼容性'],
                        }],
                    executionTimeMs: 0,
                    error: error?.message || 'Skill execution failed',
                };
            }
        }
        else {
            // 使用传统输出模式（execute）
            console.log('[SkillAnalysisAdapter] Using traditional output mode for skill', targetSkillId);
            result = await this.executor.execute(targetSkillId, traceId, params, { vendor: vendorResult.vendor });
        }
        // 收集事件信息
        const executionEvents = eventCollector.getEvents();
        const eventSummary = eventCollector.getSummary();
        // 转换结果格式
        console.log('[SkillAnalysisAdapter] displayResults count:', result.displayResults.length);
        console.log('[SkillAnalysisAdapter] displayResults:', JSON.stringify(result.displayResults.map((dr) => ({
            stepId: dr.stepId,
            title: dr.title,
            hasData: !!dr.data,
            dataKeys: dr.data ? Object.keys(dr.data) : [],
        })), null, 2));
        const sections = this.convertDisplayResultsToSections(result.displayResults);
        console.log('[SkillAnalysisAdapter] sections count:', Object.keys(sections).length);
        console.log('[SkillAnalysisAdapter] sections keys:', Object.keys(sections));
        console.log('[SkillAnalysisAdapter] result.success:', result.success);
        const diagnostics = result.diagnostics.map((d) => ({
            id: d.id,
            severity: d.severity,
            message: d.diagnosis,
            suggestions: d.suggestions,
        }));
        // 生成智能摘要（优先使用 AI 摘要，否则使用规则生成）
        let summary;
        if (result.aiSummary) {
            summary = result.aiSummary;
        }
        else {
            const generatedSummary = smartSummaryGenerator_1.smartSummaryGenerator.generate({
                skillId: targetSkillId,
                skillName: skill.meta.display_name,
                displayResults: result.displayResults,
                diagnostics: result.diagnostics,
                executionTimeMs: result.executionTimeMs,
            });
            summary = generatedSummary.text;
        }
        // 生成直接回答
        const answer = answerGenerator_1.answerGenerator.generateAnswer({
            originalQuestion: question || '',
            skillId: targetSkillId,
            skillName: skill.meta.display_name,
            success: result.success,
            diagnostics,
            sections,
            executionTimeMs: result.executionTimeMs,
        });
        return {
            skillId: targetSkillId,
            skillName: skill.meta.display_name,
            success: result.success,
            sections,
            diagnostics,
            summary,
            executionTimeMs: result.executionTimeMs,
            vendor: vendorResult.vendor !== 'aosp' ? vendorResult.vendor : undefined,
            displayResults: result.displayResults,
            aiSummary: result.aiSummary,
            directAnswer: answer.answer,
            questionType: answer.questionType,
            answerConfidence: answer.confidence,
            // Include layeredResult for frontend display
            layeredResult,
            // 事件流
            executionEvents,
            eventSummary,
        };
    }
    /**
     * 将 LayeredResult 转换为 displayResults 格式
     * 用于处理分层输出模式（overview/list/session/deep）
     */
    convertLayeredResultToDisplayResults(layeredResult) {
        console.log('[convertLayeredResultToDisplayResults] Starting conversion. Input:', JSON.stringify({
            hasOverview: !!layeredResult?.layers?.overview,
            hasList: !!layeredResult?.layers?.list,
            hasSession: !!layeredResult?.layers?.session,
            hasDeep: !!layeredResult?.layers?.deep,
        }, null, 2));
        const displayResults = [];
        // 处理 overview 和 list 层（直接是 stepId -> StepResult 的映射）
        for (const layerKey of ['overview', 'list']) {
            const layer = layeredResult.layers[layerKey];
            if (!layer) {
                console.log(`[convertLayeredResultToDisplayResults] Layer ${layerKey} is empty, skipping`);
                continue;
            }
            console.log(`[convertLayeredResultToDisplayResults] Processing ${layerKey} with ${Object.keys(layer).length} steps`);
            for (const [stepId, stepResult] of Object.entries(layer)) {
                if (!stepResult.display?.show && stepResult.display?.show !== undefined) {
                    console.log(`[convertLayeredResultToDisplayResults] Skipping ${stepId} (show=false)`);
                    continue;
                }
                if (stepResult.display?.level === 'none') {
                    console.log(`[convertLayeredResultToDisplayResults] Skipping ${stepId} (level=none)`);
                    continue;
                }
                const normalizedData = this.extractLayerStepData(stepResult);
                const columnDefinitions = this.extractColumnDefinitions(stepResult);
                const dr = {
                    stepId,
                    title: stepResult.display?.title || stepId,
                    level: stepResult.display?.level || 'detail',
                    format: stepResult.display?.format || 'table',
                    data: normalizedData,
                    columnDefinitions,
                };
                console.log(`[convertLayeredResultToDisplayResults] Adding ${layerKey} item:`, JSON.stringify({
                    stepId,
                    hasData: !!dr.data,
                    dataType: Array.isArray(dr.data) ? 'array' : typeof dr.data,
                    dataLength: Array.isArray(dr.data) ? dr.data.length : 'N/A',
                    hasColumnDefinitions: Array.isArray(dr.columnDefinitions) && dr.columnDefinitions.length > 0,
                }));
                displayResults.push(dr);
            }
        }
        // 处理 session 层（按 session_id 组织）
        const sessionLayer = layeredResult.layers.session;
        if (sessionLayer) {
            console.log(`[convertLayeredResultToDisplayResults] Processing session layer with ${Object.keys(sessionLayer).length} sessions`);
            for (const [sessionId, sessionSteps] of Object.entries(sessionLayer)) {
                console.log(`[convertLayeredResultToDisplayResults] Processing session ${sessionId} with ${Object.keys(sessionSteps).length} steps`);
                for (const [stepId, stepResult] of Object.entries(sessionSteps)) {
                    if (!stepResult.display?.show && stepResult.display?.show !== undefined)
                        continue;
                    if (stepResult.display?.level === 'none')
                        continue;
                    const normalizedData = this.extractLayerStepData(stepResult);
                    const columnDefinitions = this.extractColumnDefinitions(stepResult);
                    displayResults.push({
                        stepId: `${sessionId}_${stepId}`,
                        title: stepResult.display?.title || `[${sessionId}] ${stepId}`,
                        level: stepResult.display?.level || 'detail',
                        format: stepResult.display?.format || 'table',
                        data: normalizedData,
                        columnDefinitions,
                    });
                }
            }
        }
        // 处理 deep 层（按 session_id -> frame_id 组织）
        const deepLayer = layeredResult.layers.deep;
        if (deepLayer) {
            console.log(`[convertLayeredResultToDisplayResults] Processing deep layer with ${Object.keys(deepLayer).length} sessions`);
            for (const [sessionId, frames] of Object.entries(deepLayer)) {
                console.log(`[convertLayeredResultToDisplayResults] Processing deep session ${sessionId} with ${Object.keys(frames).length} frames`);
                for (const [frameId, stepResult] of Object.entries(frames)) {
                    if (!stepResult.display?.show && stepResult.display?.show !== undefined)
                        continue;
                    if (stepResult.display?.level === 'none')
                        continue;
                    const normalizedData = this.extractLayerStepData(stepResult);
                    const columnDefinitions = this.extractColumnDefinitions(stepResult);
                    const dr = {
                        stepId: `${sessionId}_${frameId}`,
                        title: stepResult.display?.title || `[${sessionId}] ${frameId}`,
                        level: stepResult.display?.level || 'detail',
                        format: stepResult.display?.format || 'table',
                        data: normalizedData,
                        columnDefinitions,
                    };
                    console.log(`[convertLayeredResultToDisplayResults] Adding deep frame:`, JSON.stringify({
                        stepId: dr.stepId,
                        title: dr.title,
                        hasData: !!dr.data,
                        dataType: Array.isArray(dr.data) ? 'array' : typeof dr.data,
                        dataLength: Array.isArray(dr.data) ? dr.data.length : 'N/A',
                        hasColumnDefinitions: Array.isArray(dr.columnDefinitions) && dr.columnDefinitions.length > 0,
                    }));
                    displayResults.push(dr);
                }
            }
        }
        console.log(`[convertLayeredResultToDisplayResults] Completed. Converted to ${displayResults.length} displayResults`);
        return displayResults;
    }
    /**
     * 将 displayResults 转换为 sections 格式（兼容 v1）
     */
    convertDisplayResultsToSections(displayResults) {
        const sections = {};
        console.log('[convertDisplayResultsToSections] Input displayResults count:', displayResults.length);
        console.log('[convertDisplayResultsToSections] Input displayResults:', JSON.stringify(displayResults.map(dr => ({
            stepId: dr.stepId,
            title: dr.title,
            dataKeys: Object.keys(dr.data || {}),
            hasExpandableData: !!dr.data?.expandableData,
            expandableDataCount: dr.data?.expandableData?.length || 0,
        })), null, 2));
        for (const result of displayResults) {
            const data = result.data ?? {};
            console.log(`[convertDisplayResultsToSections] Processing ${result.stepId}:`, JSON.stringify({
                hasData: !!data,
                dataType: typeof data,
                dataIsArray: Array.isArray(data),
                dataHasRows: !!data?.rows,
                dataHasText: !!data?.text,
                dataLength: Array.isArray(data) ? data.length : 'N/A',
                dataSample: Array.isArray(data) && data.length > 0 ? data[0] : data,
            }, null, 2));
            // 处理不同类型的 data 格式
            let sectionData;
            let rowCount;
            let columns;
            const columnDefinitions = Array.isArray(result.columnDefinitions)
                ? result.columnDefinitions
                : undefined;
            // 0. 诊断数据格式 {diagnostics, inputs} - 保持原样
            if (data.diagnostics && Array.isArray(data.diagnostics)) {
                console.log(`[convertDisplayResultsToSections] ${result.stepId}: Using diagnostic format`);
                // 保持诊断结构，前端需要这个格式
                sectionData = data;
                rowCount = data.diagnostics.length;
            }
            // 1. 标准 {columns, rows} 格式
            else if (data.rows && Array.isArray(data.rows)) {
                console.log(`[convertDisplayResultsToSections] ${result.stepId}: Using {columns, rows} format`);
                const configuredColumns = columnDefinitions
                    ?.map((d) => d?.name)
                    .filter((name, idx, arr) => typeof name === 'string' &&
                    name.length > 0 &&
                    arr.indexOf(name) === idx) || [];
                const sourceColumns = Array.isArray(data.columns) ? data.columns : [];
                // Prefer configured display columns and project the row objects accordingly.
                // This keeps rendered columns aligned with skill author intent.
                if (configuredColumns.length > 0) {
                    if (sourceColumns.length > 0) {
                        const sourceRows = this.rowsToObjects(sourceColumns, data.rows);
                        sectionData = sourceRows.map((row) => {
                            const projected = {};
                            for (const col of configuredColumns) {
                                projected[col] = row[col];
                            }
                            return projected;
                        });
                    }
                    else {
                        sectionData = data.rows.map((row) => {
                            const projected = {};
                            configuredColumns.forEach((col, idx) => {
                                projected[col] = Array.isArray(row) ? row[idx] : undefined;
                            });
                            return projected;
                        });
                    }
                    columns = configuredColumns;
                }
                else {
                    sectionData = this.rowsToObjects(sourceColumns, data.rows);
                    columns = sourceColumns;
                }
                rowCount = data.rows.length;
            }
            // 2. 文本格式
            else if (data.text) {
                console.log(`[convertDisplayResultsToSections] ${result.stepId}: Using text format`);
                sectionData = [{ text: data.text }];
                rowCount = 1;
            }
            // 3. 对象数组格式（直接来自 SQL 查询）
            else if (Array.isArray(data)) {
                console.log(`[convertDisplayResultsToSections] ${result.stepId}: Using array format, length=${data.length}`);
                sectionData = data;
                rowCount = data.length;
                // 优先使用 display.columns 定义列顺序；否则从首行推导
                if (columnDefinitions && columnDefinitions.length > 0) {
                    columns = columnDefinitions
                        .map((d) => d?.name)
                        .filter((name) => typeof name === 'string' && name.length > 0);
                }
                else if (data.length > 0 && typeof data[0] === 'object') {
                    columns = Object.keys(data[0]);
                    console.log(`[convertDisplayResultsToSections] ${result.stepId}: Extracted columns:`, columns);
                }
            }
            // 4. 其他情况
            else {
                console.log(`[convertDisplayResultsToSections] ${result.stepId}: Unknown format, using empty array`);
                sectionData = [];
                rowCount = 0;
            }
            const section = {
                title: result.title,
                level: result.level,
                format: result.format,
                data: sectionData,
                rowCount,
                columns,
                columnDefinitions,
                sql: result.sql, // 保存 SQL
            };
            console.log(`[convertDisplayResultsToSections] ${result.stepId}: Final sectionData:`, JSON.stringify({
                dataType: typeof sectionData,
                dataLength: Array.isArray(sectionData) ? sectionData.length : 'N/A',
                hasColumns: !!columns,
                columnsCount: columns?.length || 0,
                sampleData: Array.isArray(sectionData) && sectionData.length > 0 ? sectionData[0] : sectionData,
            }, null, 2));
            // 包含可展开数据和汇总（用于 iterator 类型的结果）
            if (data.expandableData) {
                section.expandableData = data.expandableData;
                console.log(`[convertDisplayResultsToSections] Step ${result.stepId} has expandableData with ${data.expandableData.length} items`);
            }
            if (data.summary) {
                section.summary = data.summary;
            }
            sections[result.stepId] = section;
        }
        console.log('[convertDisplayResultsToSections] Output sections count:', Object.keys(sections).length);
        console.log('[convertDisplayResultsToSections] Output sections keys:', Object.keys(sections));
        return sections;
    }
    /**
     * 从分层 StepResult 提取可展示数据。
     * 对 skill 引用步骤进行解包，避免把嵌套 SkillExecutionResult 直接透传到展示层。
     */
    extractLayerStepData(stepResult) {
        if (!stepResult || typeof stepResult !== 'object') {
            return {};
        }
        if (stepResult.stepType !== 'skill') {
            return stepResult.data ?? {};
        }
        const nested = stepResult.data;
        if (!nested || typeof nested !== 'object') {
            return stepResult.data ?? {};
        }
        if (Object.prototype.hasOwnProperty.call(nested, 'data')) {
            return nested.data ?? {};
        }
        const rawResults = nested.rawResults;
        if (rawResults && typeof rawResults === 'object') {
            if (rawResults.root?.data !== undefined) {
                return rawResults.root.data ?? {};
            }
            for (const step of Object.values(rawResults)) {
                if (step && typeof step === 'object' && Object.prototype.hasOwnProperty.call(step, 'data')) {
                    return step.data ?? {};
                }
            }
        }
        const nestedDisplayResults = Array.isArray(nested.displayResults)
            ? nested.displayResults
            : [];
        const firstTabular = nestedDisplayResults.find((dr) => dr?.data && typeof dr.data === 'object' && Array.isArray(dr.data.rows));
        if (firstTabular?.data) {
            return firstTabular.data;
        }
        return stepResult.data ?? {};
    }
    /**
     * 从 StepResult.display.columns 中提取列定义（含 type/format/unit/clickAction）。
     */
    extractColumnDefinitions(stepResult) {
        const columns = stepResult?.display?.columns;
        if (!Array.isArray(columns)) {
            return undefined;
        }
        const columnDefinitions = columns
            .map((col) => {
            if (typeof col === 'string') {
                return { name: col };
            }
            if (col && typeof col === 'object' && typeof col.name === 'string') {
                return { ...col };
            }
            return null;
        })
            .filter((col) => !!col);
        return columnDefinitions.length > 0 ? columnDefinitions : undefined;
    }
    /**
     * 将行数组转换为对象数组
     */
    rowsToObjects(columns, rows) {
        return rows.map(row => {
            const obj = {};
            columns.forEach((col, idx) => {
                obj[col] = row[idx];
            });
            return obj;
        });
    }
    /**
     * 获取所有可用的 skills 列表
     */
    async listSkills() {
        await this.ensureInitialized();
        const skills = skillLoader_1.skillRegistry.getAllSkills();
        return skills.map((skill) => {
            const triggers = skill.triggers;
            let keywords = [];
            if (triggers?.keywords) {
                if (Array.isArray(triggers.keywords)) {
                    keywords = triggers.keywords;
                }
                else {
                    keywords = [...(triggers.keywords.zh || []), ...(triggers.keywords.en || [])];
                }
            }
            return {
                id: skill.name,
                name: skill.name,
                displayName: skill.meta?.display_name || skill.name,
                description: skill.meta?.description || '',
                type: skill.type,
                keywords,
                tags: skill.meta?.tags,
            };
        });
    }
    /**
     * 获取指定 skill 的详细信息
     */
    async getSkillDetail(skillId) {
        await this.ensureInitialized();
        return skillLoader_1.skillRegistry.getSkill(skillId) || null;
    }
    /**
     * Adapt skill result to layered format
     * This method converts LayeredResult to AdaptedResult for API responses
     */
    async adaptSkillResult(result) {
        // 处理新的分层格式
        return {
            format: 'layered',
            layers: result.layers,
            defaultExpanded: result.defaultExpanded,
            metadata: result.metadata
        };
    }
}
exports.SkillAnalysisAdapter = SkillAnalysisAdapter;
// =============================================================================
// Singleton Instance
// =============================================================================
let adapterInstance = null;
function getSkillAnalysisAdapter(traceProcessor, eventHandler) {
    if (!adapterInstance) {
        adapterInstance = new SkillAnalysisAdapter(traceProcessor, eventHandler);
    }
    return adapterInstance;
}
function createSkillAnalysisAdapter(traceProcessor, eventHandler) {
    return new SkillAnalysisAdapter(traceProcessor, eventHandler);
}
//# sourceMappingURL=skillAnalysisAdapter.js.map