"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.createToolFromSkill = createToolFromSkill;
exports.createTaskId = createTaskId;
exports.createHypothesisId = createHypothesisId;
exports.createMessageId = createMessageId;
const skillEngine_1 = require("../../services/skillEngine");
// =============================================================================
// Factory Functions
// =============================================================================
/**
 * Create an AgentTool from a SkillDefinition
 */
function createToolFromSkill(skill, category, executor) {
    return {
        name: `analyze_${skill.name.replace(/_/g, '_')}`,
        description: skill.meta?.description || `Execute ${skill.name} analysis`,
        skillId: skill.name,
        category,
        parameters: skill.inputs?.map((input) => ({
            name: input.name,
            type: input.type,
            required: input.required,
            description: input.description || `Parameter ${input.name}`,
            default: input.default,
        })),
        execute: async (params, context) => {
            const startTime = Date.now();
            try {
                const result = await executor(skill.name, params, context);
                // Extract data from displayResults or rawResults
                let data = null;
                if (result.displayResults && result.displayResults.length > 0) {
                    data = {};
                    for (const dr of result.displayResults) {
                        data[dr.stepId] = dr.data;
                    }
                }
                else if (result.rawResults) {
                    data = {};
                    for (const [stepId, stepResult] of Object.entries(result.rawResults)) {
                        data[stepId] = stepResult.data;
                    }
                }
                // Extract findings from diagnostics
                const findings = [];
                if (result.diagnostics && result.diagnostics.length > 0) {
                    for (const diag of result.diagnostics) {
                        findings.push({
                            id: `${skill.name}_${Date.now()}_${findings.length}`,
                            category: category,
                            severity: diag.severity,
                            title: diag.diagnosis,
                            description: diag.suggestions?.join('; ') || diag.diagnosis,
                            source: skill.name,
                            confidence: typeof diag.confidence === 'number' ? diag.confidence : 0.8,
                            details: diag.evidence,
                        });
                    }
                }
                const dataEnvelopes = skillEngine_1.SkillExecutor.toDataEnvelopes(result, undefined, {
                    traceId: context.traceId,
                    traceSide: 'current',
                });
                return {
                    success: result.success,
                    data,
                    findings,
                    dataEnvelopes: dataEnvelopes.length > 0 ? dataEnvelopes : undefined,
                    error: result.error,
                    executionTimeMs: Date.now() - startTime,
                };
            }
            catch (error) {
                return {
                    success: false,
                    error: error.message,
                    executionTimeMs: Date.now() - startTime,
                };
            }
        },
    };
}
/**
 * Create a unique task ID
 */
function createTaskId() {
    return `task_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
 * Create a unique hypothesis ID
 */
function createHypothesisId() {
    return `hypo_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
 * Create a unique message ID
 */
function createMessageId() {
    return `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
//# sourceMappingURL=agentProtocol.js.map