"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.SystemAgent = exports.ANRAgent = exports.InteractionAgent = exports.StartupAgent = void 0;
exports.createStartupAgent = createStartupAgent;
exports.createInteractionAgent = createInteractionAgent;
exports.createANRAgent = createANRAgent;
exports.createSystemAgent = createSystemAgent;
/**
 * SmartPerfetto Additional Domain Agents
 *
 * Phase 2.6: Additional AI Agents for specific analysis domains
 *
 * This file contains:
 * - StartupAgent: App launch and startup analysis
 * - InteractionAgent: Click response and user interaction analysis
 * - ANRAgent: ANR detection and analysis
 * - SystemAgent: System-level analysis (thermal, IO, suspend/wakeup)
 *
 * All agents use lazy tool initialization via BaseAgent.ensureToolsLoaded()
 */
const baseAgent_1 = require("../base/baseAgent");
const adbTools_1 = require("../tools/adbTools");
const skillCatalog_1 = require("./skillCatalog");
// =============================================================================
// Startup Agent
// =============================================================================
class StartupAgent extends baseAgent_1.BaseAgent {
    constructor(modelRouter) {
        super({
            id: 'startup_agent',
            name: 'Startup Analysis Agent',
            domain: 'startup',
            description: 'AI agent specialized in app startup and launch performance analysis',
            tools: [...(0, adbTools_1.getAdbAgentTools)()],
            maxIterations: 3,
            confidenceThreshold: 0.7,
            canDelegate: true,
            delegateTo: ['cpu_agent', 'binder_agent', 'memory_agent'],
        }, modelRouter, skillCatalog_1.STARTUP_SKILLS);
    }
    buildUnderstandingPrompt(task) {
        return `你是一个启动性能分析专家 Agent，负责分析 Android 应用的启动性能问题。

## 任务
${task.description}

## 上下文
- 用户查询: ${task.context.query}
${task.context.hypothesis ? `- 当前假设: ${task.context.hypothesis.description}` : ''}
${this.formatTaskContext(task)}

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"objective":"","questions":[],"relevantAreas":["startup"],"recommendedTools":["analyze_startup"],"constraints":[],"confidence":0.7}`;
    }
    buildPlanningPrompt(understanding, task) {
        return `规划启动分析：目标 ${understanding.objective}

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"steps":[{"toolName":"analyze_startup","params":{},"purpose":"分析启动性能"}],"expectedOutcomes":["启动时间分析"],"estimatedTimeMs":30000,"confidence":0.7}`;
    }
    buildReflectionPrompt(result, task) {
        return `反思启动分析：发现 ${result.findings.map(f => f.title).join(', ') || '无'}
请以 JSON 返回：{"insights":[],"objectivesMet":${result.success},"findingsConfidence":0.7,"gaps":[],"nextSteps":[],"hypothesisUpdates":[],"questionsForOthers":[]}`;
    }
    async generateHypotheses(findings) {
        const hypotheses = [];
        const startupFindings = findings.filter(f => f.title.includes('启动') || f.title.includes('launch'));
        if (startupFindings.length > 0) {
            hypotheses.push(this.createHypothesis('启动过程存在性能瓶颈', 0.6, startupFindings.map(f => ({ id: f.id, description: f.title, source: 'startup_agent', type: 'finding', strength: 0.7 }))));
        }
        return hypotheses;
    }
    getRecommendedTools(context) {
        return ['analyze_startup', 'get_startup_detail'];
    }
}
exports.StartupAgent = StartupAgent;
// =============================================================================
// Interaction Agent
// =============================================================================
class InteractionAgent extends baseAgent_1.BaseAgent {
    constructor(modelRouter) {
        super({
            id: 'interaction_agent',
            name: 'Interaction Analysis Agent',
            domain: 'interaction',
            description: 'AI agent specialized in click response and user interaction analysis',
            tools: [...(0, adbTools_1.getAdbAgentTools)()],
            maxIterations: 3,
            confidenceThreshold: 0.7,
            canDelegate: true,
            delegateTo: ['frame_agent', 'cpu_agent', 'binder_agent'],
        }, modelRouter, skillCatalog_1.INTERACTION_SKILLS);
    }
    buildUnderstandingPrompt(task) {
        return `你是一个交互响应分析专家 Agent，负责分析用户点击响应延迟问题。

## 任务
${task.description}

## 上下文
- 用户查询: ${task.context.query}
${task.context.hypothesis ? `- 当前假设: ${task.context.hypothesis.description}` : ''}
${this.formatTaskContext(task)}

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"objective":"","questions":[],"relevantAreas":["interaction"],"recommendedTools":["analyze_click_response"],"constraints":[],"confidence":0.7}`;
    }
    buildPlanningPrompt(understanding, task) {
        return `规划交互分析：目标 ${understanding.objective}

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"steps":[{"toolName":"analyze_click_response","params":{},"purpose":"分析点击响应"}],"expectedOutcomes":[],"estimatedTimeMs":30000,"confidence":0.7}`;
    }
    buildReflectionPrompt(result, task) {
        return `反思交互分析：发现 ${result.findings.map(f => f.title).join(', ') || '无'}
请以 JSON 返回：{"insights":[],"objectivesMet":${result.success},"findingsConfidence":0.7,"gaps":[],"nextSteps":[],"hypothesisUpdates":[],"questionsForOthers":[]}`;
    }
    async generateHypotheses(findings) {
        const hypotheses = [];
        const interactionFindings = findings.filter(f => f.title.includes('点击') || f.title.includes('响应'));
        if (interactionFindings.length > 0) {
            hypotheses.push(this.createHypothesis('点击响应存在延迟问题', 0.6, interactionFindings.map(f => ({ id: f.id, description: f.title, source: 'interaction_agent', type: 'finding', strength: 0.7 }))));
        }
        return hypotheses;
    }
    getRecommendedTools(context) {
        const query = context.query?.toLowerCase() || '';
        const tools = [];
        if (query.includes('点击') || query.includes('click') || query.includes('响应') || query.includes('response')) {
            tools.push('analyze_click_response');
        }
        if (query.includes('导航') || query.includes('navigation') || query.includes('页面') || query.includes('activity') || query.includes('跳转')) {
            tools.push('analyze_navigation');
        }
        if (tools.length === 0)
            tools.push('analyze_click_response');
        return [...new Set(tools)];
    }
}
exports.InteractionAgent = InteractionAgent;
// =============================================================================
// ANR Agent
// =============================================================================
class ANRAgent extends baseAgent_1.BaseAgent {
    constructor(modelRouter) {
        super({
            id: 'anr_agent',
            name: 'ANR Analysis Agent',
            domain: 'anr',
            description: 'AI agent specialized in ANR detection and root cause analysis',
            tools: [...(0, adbTools_1.getAdbAgentTools)()],
            maxIterations: 3,
            confidenceThreshold: 0.7,
            canDelegate: true,
            delegateTo: ['cpu_agent', 'binder_agent', 'memory_agent'],
        }, modelRouter, skillCatalog_1.ANR_SKILLS);
    }
    buildUnderstandingPrompt(task) {
        return `你是一个 ANR 分析专家 Agent，负责分析 Android 应用的 ANR 问题。

## 任务
${task.description}

## 上下文
- 用户查询: ${task.context.query}
${task.context.hypothesis ? `- 当前假设: ${task.context.hypothesis.description}` : ''}
${this.formatTaskContext(task)}

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"objective":"","questions":[],"relevantAreas":["anr"],"recommendedTools":["analyze_anr"],"constraints":[],"confidence":0.7}`;
    }
    buildPlanningPrompt(understanding, task) {
        return `规划 ANR 分析：目标 ${understanding.objective}

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"steps":[{"toolName":"analyze_anr","params":{},"purpose":"分析 ANR"}],"expectedOutcomes":[],"estimatedTimeMs":30000,"confidence":0.7}`;
    }
    buildReflectionPrompt(result, task) {
        return `反思 ANR 分析：发现 ${result.findings.map(f => f.title).join(', ') || '无'}
请以 JSON 返回：{"insights":[],"objectivesMet":${result.success},"findingsConfidence":0.7,"gaps":[],"nextSteps":[],"hypothesisUpdates":[],"questionsForOthers":[]}`;
    }
    async generateHypotheses(findings) {
        const hypotheses = [];
        const anrFindings = findings.filter(f => f.title.includes('ANR') || f.title.includes('无响应'));
        if (anrFindings.length > 0) {
            hypotheses.push(this.createHypothesis('主线程阻塞导致 ANR', 0.8, anrFindings.map(f => ({ id: f.id, description: f.title, source: 'anr_agent', type: 'finding', strength: 0.9 }))));
        }
        return hypotheses;
    }
    getRecommendedTools(context) {
        return ['analyze_anr', 'get_anr_detail'];
    }
}
exports.ANRAgent = ANRAgent;
// =============================================================================
// System Agent
// =============================================================================
class SystemAgent extends baseAgent_1.BaseAgent {
    constructor(modelRouter) {
        super({
            id: 'system_agent',
            name: 'System Analysis Agent',
            domain: 'system',
            description: 'AI agent specialized in system-level analysis: thermal, IO, suspend/wakeup',
            tools: [...(0, adbTools_1.getAdbAgentTools)({ includeRecorder: true })],
            maxIterations: 3,
            confidenceThreshold: 0.7,
            canDelegate: true,
            delegateTo: ['cpu_agent', 'memory_agent'],
        }, modelRouter, skillCatalog_1.SYSTEM_SKILLS);
    }
    buildUnderstandingPrompt(task) {
        return `你是一个系统级分析专家 Agent，负责分析热节流、IO 压力等系统问题。

## 任务
${task.description}

## 上下文
- 用户查询: ${task.context.query}
${task.context.hypothesis ? `- 当前假设: ${task.context.hypothesis.description}` : ''}
${this.formatTaskContext(task)}

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"objective":"","questions":[],"relevantAreas":["system"],"recommendedTools":[],"constraints":[],"confidence":0.7}`;
    }
    buildPlanningPrompt(understanding, task) {
        return `规划系统分析：目标 ${understanding.objective}

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"steps":[{"toolName":"","params":{},"purpose":""}],"expectedOutcomes":[],"estimatedTimeMs":30000,"confidence":0.7}`;
    }
    buildReflectionPrompt(result, task) {
        return `反思系统分析：发现 ${result.findings.map(f => f.title).join(', ') || '无'}
请以 JSON 返回：{"insights":[],"objectivesMet":${result.success},"findingsConfidence":0.7,"gaps":[],"nextSteps":[],"hypothesisUpdates":[],"questionsForOthers":[]}`;
    }
    async generateHypotheses(findings) {
        const hypotheses = [];
        const systemFindings = findings.filter(f => f.title.includes('热') || f.title.includes('IO') || f.title.includes('thermal'));
        if (systemFindings.length > 0) {
            hypotheses.push(this.createHypothesis('系统级问题影响性能', 0.6, systemFindings.map(f => ({ id: f.id, description: f.title, source: 'system_agent', type: 'finding', strength: 0.7 }))));
        }
        return hypotheses;
    }
    getRecommendedTools(context) {
        const query = context.query?.toLowerCase() || '';
        const tools = [];
        if (query.includes('热') || query.includes('thermal') || query.includes('温度') || query.includes('降频'))
            tools.push('analyze_thermal');
        if (query.includes('io') || query.includes('磁盘') || query.includes('存储') || query.includes('iowait'))
            tools.push('analyze_io_pressure');
        if (query.includes('休眠') || query.includes('唤醒') || query.includes('suspend') || query.includes('wakelock'))
            tools.push('analyze_suspend_wakeup');
        if (query.includes('块io') || query.includes('block') || query.includes('读写') || query.includes('数据库'))
            tools.push('analyze_block_io');
        if (query.includes('irq') || query.includes('中断') || query.includes('interrupt'))
            tools.push('analyze_irq');
        if (query.includes('网络') || query.includes('network') || query.includes('dns') || query.includes('http'))
            tools.push('analyze_network');
        if (tools.length === 0)
            tools.push('analyze_thermal');
        return tools;
    }
}
exports.SystemAgent = SystemAgent;
// =============================================================================
// Factory Functions
// =============================================================================
function createStartupAgent(modelRouter) {
    return new StartupAgent(modelRouter);
}
function createInteractionAgent(modelRouter) {
    return new InteractionAgent(modelRouter);
}
function createANRAgent(modelRouter) {
    return new ANRAgent(modelRouter);
}
function createSystemAgent(modelRouter) {
    return new SystemAgent(modelRouter);
}
//# sourceMappingURL=additionalAgents.js.map