"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenAiMetrics = void 0;
const genAiAttributes_1 = require("./genAiAttributes");
/**
 * Pre-configured OTel GenAI metric instruments.
 * All methods are static to avoid per-call allocations (aligned with gemini-cli pattern).
 */
class GenAiMetrics {
    // ── GenAI Convention Metrics ──
    static recordOperationDuration(otel, durationSec, attrs) {
        otel.recordMetric('gen_ai.client.operation.duration', durationSec, {
            [genAiAttributes_1.GenAiAttr.OPERATION_NAME]: attrs.operationName,
            [genAiAttributes_1.GenAiAttr.PROVIDER_NAME]: attrs.providerName,
            [genAiAttributes_1.GenAiAttr.REQUEST_MODEL]: attrs.requestModel,
            ...(attrs.responseModel ? { [genAiAttributes_1.GenAiAttr.RESPONSE_MODEL]: attrs.responseModel } : {}),
            ...(attrs.serverAddress ? { [genAiAttributes_1.StdAttr.SERVER_ADDRESS]: attrs.serverAddress } : {}),
            ...(attrs.serverPort ? { [genAiAttributes_1.StdAttr.SERVER_PORT]: attrs.serverPort } : {}),
            ...(attrs.errorType ? { [genAiAttributes_1.StdAttr.ERROR_TYPE]: attrs.errorType } : {}),
        });
    }
    static recordTokenUsage(otel, tokenCount, tokenType, attrs) {
        otel.recordMetric('gen_ai.client.token.usage', tokenCount, {
            [genAiAttributes_1.GenAiAttr.OPERATION_NAME]: attrs.operationName,
            [genAiAttributes_1.GenAiAttr.PROVIDER_NAME]: attrs.providerName,
            [genAiAttributes_1.GenAiAttr.TOKEN_TYPE]: tokenType,
            [genAiAttributes_1.GenAiAttr.REQUEST_MODEL]: attrs.requestModel,
            ...(attrs.responseModel ? { [genAiAttributes_1.GenAiAttr.RESPONSE_MODEL]: attrs.responseModel } : {}),
            ...(attrs.serverAddress ? { [genAiAttributes_1.StdAttr.SERVER_ADDRESS]: attrs.serverAddress } : {}),
        });
    }
    // ── Extension-Specific Metrics ──
    static recordToolCallCount(otel, toolName, success) {
        otel.incrementCounter('copilot_chat.tool.call.count', 1, {
            [genAiAttributes_1.GenAiAttr.TOOL_NAME]: toolName,
            success,
        });
    }
    static recordToolCallDuration(otel, toolName, durationMs) {
        otel.recordMetric('copilot_chat.tool.call.duration', durationMs, {
            [genAiAttributes_1.GenAiAttr.TOOL_NAME]: toolName,
        });
    }
    static recordAgentDuration(otel, agentName, durationSec) {
        otel.recordMetric('copilot_chat.agent.invocation.duration', durationSec, {
            [genAiAttributes_1.GenAiAttr.AGENT_NAME]: agentName,
        });
    }
    static recordAgentTurnCount(otel, agentName, turnCount) {
        otel.recordMetric('copilot_chat.agent.turn.count', turnCount, {
            [genAiAttributes_1.GenAiAttr.AGENT_NAME]: agentName,
        });
    }
    static recordTimeToFirstToken(otel, model, ttftSec) {
        otel.recordMetric('copilot_chat.time_to_first_token', ttftSec, {
            [genAiAttributes_1.GenAiAttr.REQUEST_MODEL]: model,
        });
    }
    static incrementSessionCount(otel) {
        otel.incrementCounter('copilot_chat.session.count');
    }
    // ── Agent Activity & Outcome Metrics ──
    /** Accept/reject counter for inline chat and chat editing edits */
    static recordEditAcceptance(otel, source, outcome, languageId) {
        otel.incrementCounter('copilot_chat.edit.acceptance.count', 1, {
            [genAiAttributes_1.CopilotChatAttr.EDIT_SOURCE]: source,
            [genAiAttributes_1.CopilotChatAttr.EDIT_OUTCOME]: outcome,
            ...(languageId ? { [genAiAttributes_1.CopilotChatAttr.LANGUAGE_ID]: languageId } : {}),
        });
    }
    /** File-level chat editing session outcome (accepted/rejected/saved) */
    static recordChatEditOutcome(otel, source, outcome, languageId, hasRemainingEdits) {
        otel.incrementCounter('copilot_chat.chat_edit.outcome.count', 1, {
            [genAiAttributes_1.CopilotChatAttr.EDIT_SOURCE]: source,
            [genAiAttributes_1.CopilotChatAttr.EDIT_OUTCOME]: outcome,
            ...(languageId ? { [genAiAttributes_1.CopilotChatAttr.LANGUAGE_ID]: languageId } : {}),
            ...(hasRemainingEdits !== undefined ? { [genAiAttributes_1.CopilotChatAttr.HAS_REMAINING_EDITS]: hasRemainingEdits } : {}),
        });
    }
    /** 4-gram text similarity survival score */
    static recordEditSurvivalFourGram(otel, source, score, timeDelayMs) {
        otel.recordMetric('copilot_chat.edit.survival.four_gram', score, {
            [genAiAttributes_1.CopilotChatAttr.EDIT_SOURCE]: source,
            [genAiAttributes_1.CopilotChatAttr.TIME_DELAY_MS]: timeDelayMs,
        });
    }
    /** No-revert survival score */
    static recordEditSurvivalNoRevert(otel, source, score, timeDelayMs) {
        otel.recordMetric('copilot_chat.edit.survival.no_revert', score, {
            [genAiAttributes_1.CopilotChatAttr.EDIT_SOURCE]: source,
            [genAiAttributes_1.CopilotChatAttr.TIME_DELAY_MS]: timeDelayMs,
        });
    }
    /** Lines of code added/removed by accepted agent edits */
    static incrementLinesOfCode(otel, type, languageId, count) {
        otel.incrementCounter('copilot_chat.lines_of_code.count', count, {
            'type': type,
            ...(languageId ? { [genAiAttributes_1.CopilotChatAttr.LANGUAGE_ID]: languageId } : {}),
        });
    }
    // ── User Engagement Metrics ──
    static incrementUserActionCount(otel, action) {
        otel.incrementCounter('copilot_chat.user.action.count', 1, {
            'action': action,
        });
    }
    static incrementUserFeedbackCount(otel, rating) {
        otel.incrementCounter('copilot_chat.user.feedback.count', 1, {
            'rating': rating,
        });
    }
    // ── Agent Internals Metrics ──
    static incrementAgentEditResponseCount(otel, outcome) {
        otel.incrementCounter('copilot_chat.agent.edit_response.count', 1, {
            'outcome': outcome,
        });
    }
    static incrementAgentSummarizationCount(otel, outcome) {
        otel.incrementCounter('copilot_chat.agent.summarization.count', 1, {
            'outcome': outcome,
        });
    }
    // ── Background/Cloud Metrics ──
    static incrementPullRequestCount(otel) {
        otel.incrementCounter('copilot_chat.pull_request.count');
    }
    static incrementCloudSessionCount(otel, partnerAgent) {
        otel.incrementCounter('copilot_chat.cloud.session.count', 1, {
            'partner_agent': partnerAgent,
        });
    }
    static incrementCloudPrReadyCount(otel) {
        otel.incrementCounter('copilot_chat.cloud.pr_ready.count');
    }
}
exports.GenAiMetrics = GenAiMetrics;
//# sourceMappingURL=genAiMetrics.js.map