"use strict";
/**
 * Anthropic provider — translates OpenAI canonical format to Anthropic's API.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnthropicProvider = void 0;
const translate_1 = require("../translate");
const DEFAULT_MAX_TOKENS = 4096;
class AnthropicProvider {
    constructor(apiKey) {
        this.clientPromise = null;
        this.apiKey = apiKey || process.env.ANTHROPIC_API_KEY || '';
        if (!this.apiKey) {
            throw new Error('ANTHROPIC_API_KEY is required for Claude models. ' +
                'Set it in your environment or .env file.');
        }
    }
    async getClient() {
        if (!this.clientPromise) {
            this.clientPromise = Promise.resolve().then(() => __importStar(require('@anthropic-ai/sdk'))).then((mod) => new mod.default({ apiKey: this.apiKey }));
        }
        return this.clientPromise;
    }
    async call(options) {
        const client = await this.getClient();
        // Translate messages from OpenAI canonical to Anthropic format
        const { system, messages } = (0, translate_1.translateMessagesToAnthropic)(options.messages);
        // Build system prompt — append JSON mode instruction if needed
        let systemPrompt = system;
        if (options.response_format?.type === 'json_object') {
            const jsonInstruction = '\n\nYou must respond with valid JSON only. No markdown fences, no explanation, just the JSON object.';
            systemPrompt = systemPrompt ? systemPrompt + jsonInstruction : jsonInstruction.trim();
        }
        // Anthropic requires max_tokens
        const maxTokens = options.max_tokens || DEFAULT_MAX_TOKENS;
        // Build request
        const request = {
            model: options.model,
            max_tokens: maxTokens,
            messages,
            ...(systemPrompt ? { system: systemPrompt } : {}),
            ...(options.temperature !== undefined ? { temperature: options.temperature } : {}),
        };
        // Translate tools
        if (options.tools?.length) {
            request.tools = (0, translate_1.translateToolsToAnthropic)(options.tools);
        }
        const response = await client.messages.create(request);
        return (0, translate_1.translateAnthropicResponse)(response);
    }
}
exports.AnthropicProvider = AnthropicProvider;
