"use strict";
/**
 * OpenAI provider — thin wrapper since our canonical format matches OpenAI's.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAIProvider = void 0;
const openai_1 = __importDefault(require("openai"));
class OpenAIProvider {
    constructor(apiKey, baseURL) {
        this.client = new openai_1.default({
            apiKey: apiKey || process.env.OPENAI_API_KEY,
            ...(baseURL ? { baseURL } : {}),
        });
    }
    async call(options) {
        const response = await this.client.chat.completions.create({
            model: options.model,
            messages: options.messages,
            ...(options.tools?.length ? { tools: options.tools } : {}),
            ...(options.max_tokens !== undefined ? { max_tokens: options.max_tokens } : {}),
            ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),
            ...(options.response_format ? { response_format: options.response_format } : {}),
        });
        const choice = response.choices[0]?.message;
        if (!choice) {
            return { content: null, tool_calls: undefined, usage: undefined };
        }
        return {
            content: choice.content ?? null,
            tool_calls: choice.tool_calls?.map((tc) => ({
                id: tc.id,
                type: 'function',
                function: { name: tc.function.name, arguments: tc.function.arguments },
            })),
            usage: response.usage ? {
                prompt_tokens: response.usage.prompt_tokens,
                completion_tokens: response.usage.completion_tokens,
                total_tokens: response.usage.total_tokens,
            } : undefined,
        };
    }
}
exports.OpenAIProvider = OpenAIProvider;
