"use strict";
// =============================================================================
// Pure Agent - Chat Completions Transport
// OpenAI-compatible API (passthrough, minimal conversion)
// =============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("./types");
const registry_1 = require("./registry");
class ChatCompletionsTransport {
    apiMode = 'chat_completions';
    convertMessages(messages) {
        // Chat Completions uses OpenAI format natively - no conversion needed
        return messages;
    }
    convertTools(tools) {
        // OpenAI format natively - no conversion needed
        return tools;
    }
    buildRequestBody(model, messages, tools, params) {
        const body = {
            model,
            messages: this.convertMessages(messages),
            stream: true,
            ...(params || {}),
        };
        if (tools && tools.length > 0) {
            body.tools = this.convertTools(tools);
            body.tool_choice = 'auto';
        }
        return body;
    }
    normalizeResponse(response) {
        const choice = response?.choices?.[0];
        if (!choice) {
            return {
                content: null,
                toolCalls: null,
                finishReason: 'stop',
                reasoning: null,
                usage: null,
            };
        }
        const message = choice.message;
        const toolCalls = message?.tool_calls?.map((tc) => (0, types_1.buildToolCall)(tc.id, tc.function.name, tc.function.arguments)) || null;
        let usage = null;
        if (response.usage) {
            usage = {
                promptTokens: response.usage.prompt_tokens || 0,
                completionTokens: response.usage.completion_tokens || 0,
                totalTokens: response.usage.total_tokens || 0,
                cachedTokens: response.usage.prompt_tokens_details?.cached_tokens || 0,
            };
        }
        return {
            content: message?.content || null,
            toolCalls: toolCalls?.length ? toolCalls : null,
            finishReason: (0, types_1.mapFinishReason)(choice.finish_reason),
            reasoning: message?.reasoning_content || null,
            usage,
        };
    }
    normalizeStreamChunk(chunk) {
        const choice = chunk?.choices?.[0];
        if (!choice) {
            // Might be a usage-only chunk at the end
            if (chunk?.usage) {
                return {
                    usage: {
                        promptTokens: chunk.usage.prompt_tokens,
                        completionTokens: chunk.usage.completion_tokens,
                        totalTokens: chunk.usage.total_tokens,
                    },
                };
            }
            return null;
        }
        const delta = choice.delta;
        if (!delta)
            return null;
        const result = {};
        if (delta.content !== undefined && delta.content !== null) {
            result.content = delta.content;
        }
        if (delta.reasoning_content) {
            result.reasoning = delta.reasoning_content;
        }
        if (delta.tool_calls) {
            result.toolCalls = delta.tool_calls.map((tc) => ({
                index: tc.index,
                id: tc.id,
                name: tc.function?.name,
                arguments: tc.function?.arguments,
            }));
        }
        if (choice.finish_reason) {
            result.finishReason = choice.finish_reason;
        }
        return Object.keys(result).length > 0 ? result : null;
    }
    getEndpoint() {
        return '/chat/completions';
    }
}
// Auto-register
(0, registry_1.registerTransport)('chat_completions', () => new ChatCompletionsTransport());
//# sourceMappingURL=chat-completions.js.map