"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.BinderAgent = void 0;
exports.createBinderAgent = createBinderAgent;
/**
 * SmartPerfetto Binder Agent
 *
 * Phase 2.4: AI Agent for Binder IPC analysis
 *
 * Skills wrapped as tools (lazy-loaded at executeTask time):
 * - binder_analysis
 * - binder_detail
 * - binder_in_range
 * - lock_contention_analysis
 * - lock_contention_in_range
 */
const baseAgent_1 = require("../base/baseAgent");
const adbTools_1 = require("../tools/adbTools");
const skillCatalog_1 = require("./skillCatalog");
// =============================================================================
// Binder Agent Implementation
// =============================================================================
class BinderAgent extends baseAgent_1.BaseAgent {
    constructor(modelRouter) {
        super({
            id: 'binder_agent',
            name: 'Binder Analysis Agent',
            domain: 'binder',
            description: 'AI agent specialized in Binder IPC and lock contention analysis',
            tools: [...(0, adbTools_1.getAdbAgentTools)()],
            maxIterations: 3,
            confidenceThreshold: 0.7,
            canDelegate: true,
            delegateTo: ['cpu_agent', 'frame_agent'],
        }, modelRouter, skillCatalog_1.BINDER_SKILLS);
    }
    buildUnderstandingPrompt(task) {
        return `你是一个 Binder IPC 分析专家 Agent，负责分析 Android 系统的进程间通信问题。

## 任务
${task.description}

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

${this.getToolSectionForPrompt()}

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

${this.getToolSectionForPrompt()}

请以 JSON 返回：{"steps":[{"toolName":"","params":{},"purpose":""}],"expectedOutcomes":[],"estimatedTimeMs":30000,"confidence":0.7}`;
    }
    buildReflectionPrompt(result, task) {
        return `反思 Binder 分析结果：
发现: ${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 binderFindings = findings.filter(f => f.title.includes('Binder') || f.title.includes('锁'));
        if (binderFindings.length > 0) {
            hypotheses.push(this.createHypothesis('Binder 调用或锁竞争导致阻塞', 0.6, binderFindings.map(f => ({ id: f.id, description: f.title, source: 'binder_agent', type: 'finding', strength: 0.7 }))));
        }
        return hypotheses;
    }
    getRecommendedTools(context) {
        const query = context.query?.toLowerCase() || '';
        const hasTimeRange = !!context.timeRange;
        const tools = [];
        // Prefer in-range binder analysis when time range is provided (e.g. jank interval).
        tools.push(hasTimeRange ? 'analyze_binder_range' : 'analyze_binder_overview');
        const evidenceText = (context.evidenceNeeded || []).join(' ').toLowerCase();
        if (query.includes('锁') ||
            query.includes('lock') ||
            query.includes('contention') ||
            evidenceText.includes('lock') ||
            evidenceText.includes('contention') ||
            evidenceText.includes('锁')) {
            tools.push(hasTimeRange ? 'analyze_lock_range' : 'analyze_lock_contention');
        }
        return [...new Set(tools)];
    }
}
exports.BinderAgent = BinderAgent;
function createBinderAgent(modelRouter) {
    return new BinderAgent(modelRouter);
}
exports.default = BinderAgent;
//# sourceMappingURL=binderAgent.js.map