import Anthropic from '@anthropic-ai/sdk';
import { DEFAULT_ANTHROPIC_MODEL } from '../../../../core/config.js';
import { AnthropicCodec } from './anthropic-codec.js';
/**
 * Anthropic implementation of the LLM port. It owns Claude message/tool
 * conversion while exposing the provider-neutral LlmAdapter contract.
 */
export class AnthropicAdapter {
    static capabilities = {
        toolCalls: true,
        systemMessages: true,
        reasoningSummaries: false,
        parallelToolCalls: false,
    };
    info;
    client;
    model;
    constructor(options = {}) {
        this.client = new Anthropic({
            apiKey: AnthropicAdapter.firstDefinedNonEmpty(options.credentials?.apiKey, process.env.ANTHROPIC_API_KEY, process.env.PERSONAL_ANTHROPIC_API_KEY),
        });
        this.model = options.model ?? DEFAULT_ANTHROPIC_MODEL;
        this.info = {
            provider: 'anthropic',
            model: this.model,
            capabilities: AnthropicAdapter.capabilities,
        };
    }
    async chat(messages, tools, signal) {
        const system = messages
            .filter((message) => message.role === 'system')
            .map((message) => message.content)
            .join('\n\n');
        const anthropicMessages = AnthropicCodec.toMessages(messages);
        const response = await this.client.messages.create({
            model: this.model,
            system: system || undefined,
            messages: anthropicMessages,
            tools: tools.length > 0 ? tools.map((tool) => AnthropicCodec.toTool(tool)) : undefined,
            max_tokens: 4096,
        }, { signal });
        const text = response.content
            .flatMap((block) => (block.type === 'text' ? [block.text] : []))
            .join('')
            .trim();
        const toolCalls = response.content.flatMap((block) => {
            if (block.type !== 'tool_use') {
                return [];
            }
            return [{
                    id: block.id,
                    tool: block.name,
                    input: block.input,
                }];
        });
        return {
            content: text || undefined,
            toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
            usage: AnthropicCodec.extractUsage(response.usage),
        };
    }
    static firstDefinedNonEmpty(...values) {
        return values.find((value) => typeof value === 'string' && value.trim().length > 0);
    }
}
//# sourceMappingURL=anthropic-adapter.js.map