"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.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DomainAgentRegistry = exports.createSystemAgent = exports.createANRAgent = exports.createInteractionAgent = exports.createStartupAgent = exports.SystemAgent = exports.ANRAgent = exports.InteractionAgent = exports.StartupAgent = exports.createMemoryAgent = exports.MemoryAgent = exports.createBinderAgent = exports.BinderAgent = exports.createCPUAgent = exports.CPUAgent = exports.createFrameAgent = exports.FrameAgent = exports.BaseAgent = void 0;
exports.createDomainAgentRegistry = createDomainAgentRegistry;
exports.createDomainAgentRegistryWithOptions = createDomainAgentRegistryWithOptions;
/**
 * SmartPerfetto Domain Agents
 *
 * Phase 2: All AI Agents that wrap Skills as tools
 *
 * Each agent specializes in a specific performance domain:
 * - FrameAgent: Frame timing, jank, scrolling
 * - CPUAgent: CPU scheduling, frequency, load
 * - BinderAgent: IPC and lock contention
 * - MemoryAgent: Memory, GC, LMK
 * - StartupAgent: App launch and startup
 * - InteractionAgent: Click response
 * - ANRAgent: ANR detection
 * - SystemAgent: Thermal, IO, suspend/wakeup
 */
// Base Agent
var baseAgent_1 = require("../base/baseAgent");
Object.defineProperty(exports, "BaseAgent", { enumerable: true, get: function () { return baseAgent_1.BaseAgent; } });
// Domain Agents
var frameAgent_1 = require("./frameAgent");
Object.defineProperty(exports, "FrameAgent", { enumerable: true, get: function () { return frameAgent_1.FrameAgent; } });
Object.defineProperty(exports, "createFrameAgent", { enumerable: true, get: function () { return frameAgent_1.createFrameAgent; } });
var cpuAgent_1 = require("./cpuAgent");
Object.defineProperty(exports, "CPUAgent", { enumerable: true, get: function () { return cpuAgent_1.CPUAgent; } });
Object.defineProperty(exports, "createCPUAgent", { enumerable: true, get: function () { return cpuAgent_1.createCPUAgent; } });
var binderAgent_1 = require("./binderAgent");
Object.defineProperty(exports, "BinderAgent", { enumerable: true, get: function () { return binderAgent_1.BinderAgent; } });
Object.defineProperty(exports, "createBinderAgent", { enumerable: true, get: function () { return binderAgent_1.createBinderAgent; } });
var memoryAgent_1 = require("./memoryAgent");
Object.defineProperty(exports, "MemoryAgent", { enumerable: true, get: function () { return memoryAgent_1.MemoryAgent; } });
Object.defineProperty(exports, "createMemoryAgent", { enumerable: true, get: function () { return memoryAgent_1.createMemoryAgent; } });
// Additional Agents
var additionalAgents_1 = require("./additionalAgents");
Object.defineProperty(exports, "StartupAgent", { enumerable: true, get: function () { return additionalAgents_1.StartupAgent; } });
Object.defineProperty(exports, "InteractionAgent", { enumerable: true, get: function () { return additionalAgents_1.InteractionAgent; } });
Object.defineProperty(exports, "ANRAgent", { enumerable: true, get: function () { return additionalAgents_1.ANRAgent; } });
Object.defineProperty(exports, "SystemAgent", { enumerable: true, get: function () { return additionalAgents_1.SystemAgent; } });
Object.defineProperty(exports, "createStartupAgent", { enumerable: true, get: function () { return additionalAgents_1.createStartupAgent; } });
Object.defineProperty(exports, "createInteractionAgent", { enumerable: true, get: function () { return additionalAgents_1.createInteractionAgent; } });
Object.defineProperty(exports, "createANRAgent", { enumerable: true, get: function () { return additionalAgents_1.createANRAgent; } });
Object.defineProperty(exports, "createSystemAgent", { enumerable: true, get: function () { return additionalAgents_1.createSystemAgent; } });
// Agent Protocol Types
__exportStar(require("../../types/agentProtocol"), exports);
const frameAgent_2 = require("./frameAgent");
const cpuAgent_2 = require("./cpuAgent");
const binderAgent_2 = require("./binderAgent");
const memoryAgent_2 = require("./memoryAgent");
const additionalAgents_2 = require("./additionalAgents");
const DEFAULT_AGENT_FACTORIES = {
    frame_agent: (modelRouter) => new frameAgent_2.FrameAgent(modelRouter),
    cpu_agent: (modelRouter) => new cpuAgent_2.CPUAgent(modelRouter),
    binder_agent: (modelRouter) => new binderAgent_2.BinderAgent(modelRouter),
    memory_agent: (modelRouter) => new memoryAgent_2.MemoryAgent(modelRouter),
    startup_agent: (modelRouter) => new additionalAgents_2.StartupAgent(modelRouter),
    interaction_agent: (modelRouter) => new additionalAgents_2.InteractionAgent(modelRouter),
    anr_agent: (modelRouter) => new additionalAgents_2.ANRAgent(modelRouter),
    system_agent: (modelRouter) => new additionalAgents_2.SystemAgent(modelRouter),
};
/**
 * Domain Agent Registry
 *
 * Manages instantiation and access to all domain agents
 */
class DomainAgentRegistry {
    constructor(modelRouter) {
        this.agents = new Map();
        this.agentFactories = new Map();
        this.modelRouter = modelRouter;
        this.registerDefaultFactories();
    }
    registerDefaultFactories() {
        for (const [agentId, factory] of Object.entries(DEFAULT_AGENT_FACTORIES)) {
            this.registerFactory(agentId, factory, { replace: true, initialize: false });
        }
    }
    registerFactory(agentId, factory, options = {}) {
        const replace = options.replace === true;
        const initialize = options.initialize !== false;
        if (!replace && this.agentFactories.has(agentId)) {
            throw new Error(`Domain agent factory already registered: ${agentId}`);
        }
        this.agentFactories.set(agentId, factory);
        if (initialize) {
            const agent = factory(this.modelRouter);
            this.registerAgent(agent, { replace });
        }
    }
    registerAgent(agent, options = {}) {
        const replace = options.replace === true;
        if (!replace && this.agents.has(agent.config.id)) {
            throw new Error(`Domain agent already registered: ${agent.config.id}`);
        }
        this.agents.set(agent.config.id, agent);
    }
    unregisterAgent(agentId) {
        this.agents.delete(agentId);
    }
    unregisterFactory(agentId) {
        this.agentFactories.delete(agentId);
        this.agents.delete(agentId);
    }
    getRegisteredFactoryIds() {
        return Array.from(this.agentFactories.keys());
    }
    /**
     * Initialize all domain agents
     */
    initialize(agentIds) {
        const idsToInitialize = Array.isArray(agentIds) && agentIds.length > 0
            ? agentIds
            : this.getRegisteredFactoryIds();
        for (const agentId of idsToInitialize) {
            const factory = this.agentFactories.get(agentId);
            if (!factory)
                continue;
            if (this.agents.has(agentId))
                continue;
            this.agents.set(agentId, factory(this.modelRouter));
        }
        console.log(`[DomainAgentRegistry] Initialized ${this.agents.size} domain agents`);
    }
    /**
     * Get agent by ID
     */
    get(agentId) {
        return this.agents.get(agentId);
    }
    /**
     * Get all agents
     */
    getAll() {
        return Array.from(this.agents.values());
    }
    /**
     * Get agent IDs
     */
    getAgentIds() {
        return Array.from(this.agents.keys());
    }
    /**
     * Get agent for a specific domain
     */
    getForDomain(domain) {
        for (const agent of this.agents.values()) {
            if (agent.config.domain === domain) {
                return agent;
            }
        }
        return undefined;
    }
    /**
     * Get agents that can handle a specific topic
     */
    getAgentsForTopic(topic) {
        const topicLower = topic.toLowerCase();
        const relevantAgents = [];
        const topicMapping = {
            'frame': ['frame_agent'],
            'jank': ['frame_agent'],
            'scroll': ['frame_agent'],
            '滑动': ['frame_agent'],
            '掉帧': ['frame_agent'],
            '卡顿': ['frame_agent', 'cpu_agent'],
            'cpu': ['cpu_agent'],
            '调度': ['cpu_agent'],
            'binder': ['binder_agent'],
            'ipc': ['binder_agent'],
            '锁': ['binder_agent'],
            'memory': ['memory_agent'],
            '内存': ['memory_agent'],
            'gc': ['memory_agent'],
            'lmk': ['memory_agent'],
            'startup': ['startup_agent'],
            '启动': ['startup_agent'],
            'launch': ['startup_agent'],
            'click': ['interaction_agent'],
            '点击': ['interaction_agent'],
            '响应': ['interaction_agent'],
            'anr': ['anr_agent'],
            '无响应': ['anr_agent'],
            'thermal': ['system_agent'],
            '热': ['system_agent'],
            'io': ['system_agent'],
        };
        for (const [keyword, agentIds] of Object.entries(topicMapping)) {
            if (topicLower.includes(keyword)) {
                for (const agentId of agentIds) {
                    const agent = this.agents.get(agentId);
                    if (agent && !relevantAgents.includes(agent)) {
                        relevantAgents.push(agent);
                    }
                }
            }
        }
        return relevantAgents;
    }
    /**
     * Get descriptions of all agents for LLM
     */
    getAgentDescriptionsForLLM() {
        return Array.from(this.agents.values())
            .map(a => `- ${a.config.id}: ${a.config.name} - ${a.config.description}`)
            .join('\n');
    }
}
exports.DomainAgentRegistry = DomainAgentRegistry;
/**
 * Create a domain agent registry
 */
function createDomainAgentRegistry(modelRouter) {
    return createDomainAgentRegistryWithOptions(modelRouter);
}
function createDomainAgentRegistryWithOptions(modelRouter, options = {}) {
    const registry = new DomainAgentRegistry(modelRouter);
    const { extraFactories = {}, disabledAgentIds = [], initialize = true, } = options;
    for (const [agentId, factory] of Object.entries(extraFactories)) {
        registry.registerFactory(agentId, factory, { replace: false, initialize: false });
    }
    if (initialize) {
        const disabled = new Set(disabledAgentIds);
        const enabledAgentIds = registry
            .getRegisteredFactoryIds()
            .filter(agentId => !disabled.has(agentId));
        registry.initialize(enabledAgentIds);
    }
    return registry;
}
//# sourceMappingURL=index.js.map