feat: add Anthropic LLM provider

This commit is contained in:
2001-01-01 00:00:00 +00:00
parent 19d1c37661
commit 52f2a7158d

View File

@@ -0,0 +1,34 @@
import Anthropic from "@anthropic-ai/sdk";
import type { LLMMessage, LLMOptions } from "@codeboard/shared";
import type { LLMProvider } from "./base.js";
export class AnthropicProvider implements LLMProvider {
name = "anthropic";
private client: Anthropic;
private defaultModel: string;
constructor(apiKey: string, model?: string) {
this.client = new Anthropic({ apiKey });
this.defaultModel = model ?? "claude-sonnet-4-20250514";
}
async chat(messages: LLMMessage[], options?: LLMOptions): Promise<string> {
const systemMessage = messages.find((m) => m.role === "system");
const nonSystemMessages = messages
.filter((m) => m.role !== "system")
.map((m) => ({
role: m.role as "user" | "assistant",
content: m.content,
}));
const response = await this.client.messages.create({
model: options?.model ?? this.defaultModel,
max_tokens: options?.maxTokens ?? 4096,
system: systemMessage?.content,
messages: nonSystemMessages,
});
const textBlock = response.content.find((b) => b.type === "text");
return textBlock?.type === "text" ? textBlock.text : "";
}
}