"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.emitInferenceDetailsEvent = emitInferenceDetailsEvent;
exports.emitSessionStartEvent = emitSessionStartEvent;
exports.emitToolCallEvent = emitToolCallEvent;
exports.emitAgentTurnEvent = emitAgentTurnEvent;
exports.emitEditFeedbackEvent = emitEditFeedbackEvent;
exports.emitEditHunkActionEvent = emitEditHunkActionEvent;
exports.emitInlineDoneEvent = emitInlineDoneEvent;
exports.emitEditSurvivalEvent = emitEditSurvivalEvent;
exports.emitUserFeedbackEvent = emitUserFeedbackEvent;
exports.emitCloudSessionInvokeEvent = emitCloudSessionInvokeEvent;
const genAiAttributes_1 = require("./genAiAttributes");
const messageFormatters_1 = require("./messageFormatters");
const workspaceOTelMetadata_1 = require("./workspaceOTelMetadata");
/**
 * Emit OTel GenAI standard events via the IOTelService abstraction.
 */
function emitInferenceDetailsEvent(otel, request, response, error) {
    const attributes = {
        'event.name': 'gen_ai.client.inference.operation.details',
        [genAiAttributes_1.GenAiAttr.OPERATION_NAME]: genAiAttributes_1.GenAiOperationName.CHAT,
        [genAiAttributes_1.GenAiAttr.REQUEST_MODEL]: request.model,
    };
    if (response) {
        if (response.model) {
            attributes[genAiAttributes_1.GenAiAttr.RESPONSE_MODEL] = response.model;
        }
        if (response.id) {
            attributes[genAiAttributes_1.GenAiAttr.RESPONSE_ID] = response.id;
        }
        if (response.finishReasons) {
            attributes[genAiAttributes_1.GenAiAttr.RESPONSE_FINISH_REASONS] = response.finishReasons;
        }
        if (response.inputTokens !== undefined) {
            attributes[genAiAttributes_1.GenAiAttr.USAGE_INPUT_TOKENS] = response.inputTokens;
        }
        if (response.outputTokens !== undefined) {
            attributes[genAiAttributes_1.GenAiAttr.USAGE_OUTPUT_TOKENS] = response.outputTokens;
        }
    }
    if (request.temperature !== undefined) {
        attributes[genAiAttributes_1.GenAiAttr.REQUEST_TEMPERATURE] = request.temperature;
    }
    if (request.maxTokens !== undefined) {
        attributes[genAiAttributes_1.GenAiAttr.REQUEST_MAX_TOKENS] = request.maxTokens;
    }
    if (error) {
        attributes[genAiAttributes_1.StdAttr.ERROR_TYPE] = error.type;
    }
    // Full content capture (optionally truncated per OTelConfig.maxAttributeSizeChars).
    // Normalize to OTel GenAI semantic convention format.
    if (otel.config.captureContent) {
        const maxLen = otel.config.maxAttributeSizeChars;
        if (request.messages !== undefined) {
            const msgs = Array.isArray(request.messages) ? request.messages : undefined;
            attributes[genAiAttributes_1.GenAiAttr.INPUT_MESSAGES] = (0, messageFormatters_1.truncateForOTel)(JSON.stringify(msgs ? (0, messageFormatters_1.normalizeProviderMessages)(msgs) : request.messages), maxLen);
        }
        if (request.systemMessage !== undefined) {
            const systemText = typeof request.systemMessage === 'string'
                ? request.systemMessage
                : JSON.stringify(request.systemMessage);
            const systemInstructions = (0, messageFormatters_1.toSystemInstructions)(systemText);
            if (systemInstructions !== undefined) {
                attributes[genAiAttributes_1.GenAiAttr.SYSTEM_INSTRUCTIONS] = (0, messageFormatters_1.truncateForOTel)(JSON.stringify(systemInstructions), maxLen);
            }
        }
        if (request.tools !== undefined) {
            attributes[genAiAttributes_1.GenAiAttr.TOOL_DEFINITIONS] = (0, messageFormatters_1.truncateForOTel)(JSON.stringify(request.tools), maxLen);
        }
    }
    otel.emitLogRecord(`GenAI inference: ${request.model}`, attributes);
}
/**
 * Emit extension-specific events.
 */
function emitSessionStartEvent(otel, sessionId, model, participant) {
    otel.emitLogRecord('copilot_chat.session.start', {
        'event.name': 'copilot_chat.session.start',
        'session.id': sessionId,
        [genAiAttributes_1.GenAiAttr.REQUEST_MODEL]: model,
        [genAiAttributes_1.GenAiAttr.AGENT_NAME]: participant,
    });
}
function emitToolCallEvent(otel, toolName, durationMs, success, error) {
    otel.emitLogRecord(`copilot_chat.tool.call: ${toolName}`, {
        'event.name': 'copilot_chat.tool.call',
        [genAiAttributes_1.GenAiAttr.TOOL_NAME]: toolName,
        'duration_ms': durationMs,
        'success': success,
        ...(error ? { [genAiAttributes_1.StdAttr.ERROR_TYPE]: error } : {}),
    });
}
function emitAgentTurnEvent(otel, turnIndex, inputTokens, outputTokens, toolCallCount) {
    otel.emitLogRecord(`copilot_chat.agent.turn: ${turnIndex}`, {
        'event.name': 'copilot_chat.agent.turn',
        'turn.index': turnIndex,
        [genAiAttributes_1.GenAiAttr.USAGE_INPUT_TOKENS]: inputTokens,
        [genAiAttributes_1.GenAiAttr.USAGE_OUTPUT_TOKENS]: outputTokens,
        'tool_call_count': toolCallCount,
    });
}
// ── Agent Activity & Outcome Events ──
function emitEditFeedbackEvent(otel, outcome, languageId, participant, requestId, editSurface, hasRemainingEdits, isNotebook, workspace) {
    otel.emitLogRecord(`copilot_chat.edit.feedback: ${outcome}`, {
        'event.name': 'copilot_chat.edit.feedback',
        'outcome': outcome,
        'language_id': languageId,
        'participant': participant,
        'request_id': requestId,
        'edit_surface': editSurface,
        'has_remaining_edits': hasRemainingEdits,
        'is_notebook': isNotebook,
        ...(0, workspaceOTelMetadata_1.workspaceMetadataToOTelAttributes)(workspace),
    });
}
function emitEditHunkActionEvent(otel, outcome, languageId, requestId, lineCount, linesAdded, linesRemoved, workspace) {
    otel.emitLogRecord(`copilot_chat.edit.hunk.action: ${outcome}`, {
        'event.name': 'copilot_chat.edit.hunk.action',
        'outcome': outcome,
        'language_id': languageId,
        'request_id': requestId,
        'line_count': lineCount,
        'lines_added': linesAdded,
        'lines_removed': linesRemoved,
        ...(0, workspaceOTelMetadata_1.workspaceMetadataToOTelAttributes)(workspace),
    });
}
function emitInlineDoneEvent(otel, accepted, languageId, editCount, editLineCount, replyType, isNotebook, workspace) {
    otel.emitLogRecord(`copilot_chat.inline.done: ${accepted ? 'accepted' : 'rejected'}`, {
        'event.name': 'copilot_chat.inline.done',
        'accepted': accepted,
        'language_id': languageId,
        'edit_count': editCount,
        'edit_line_count': editLineCount,
        'reply_type': replyType,
        'is_notebook': isNotebook,
        ...(0, workspaceOTelMetadata_1.workspaceMetadataToOTelAttributes)(workspace),
    });
}
function emitEditSurvivalEvent(otel, editSource, survivalRateFourGram, survivalRateNoRevert, timeDelayMs, didBranchChange, requestId, workspace) {
    otel.emitLogRecord(`copilot_chat.edit.survival: ${editSource}`, {
        'event.name': 'copilot_chat.edit.survival',
        'edit_source': editSource,
        'survival_rate_four_gram': survivalRateFourGram,
        'survival_rate_no_revert': survivalRateNoRevert,
        'time_delay_ms': timeDelayMs,
        'did_branch_change': didBranchChange,
        'request_id': requestId,
        ...(0, workspaceOTelMetadata_1.workspaceMetadataToOTelAttributes)(workspace),
    });
}
function emitUserFeedbackEvent(otel, rating, participant, conversationId, requestId) {
    otel.emitLogRecord(`copilot_chat.user.feedback: ${rating}`, {
        'event.name': 'copilot_chat.user.feedback',
        'rating': rating,
        'participant': participant,
        'conversation_id': conversationId,
        'request_id': requestId,
    });
}
function emitCloudSessionInvokeEvent(otel, partnerAgent, model, requestId) {
    otel.emitLogRecord(`copilot_chat.cloud.session.invoke: ${partnerAgent}`, {
        'event.name': 'copilot_chat.cloud.session.invoke',
        'partner_agent': partnerAgent,
        'model': model,
        'request_id': requestId,
    });
}
//# sourceMappingURL=genAiEvents.js.map