import { AnthropicVertex } from "@anthropic-ai/vertex-sdk" import { Anthropic } from "@anthropic-ai/sdk" import { ApiHandler, ApiHandlerMessageResponse, withoutImageData } from "." import { ApiHandlerOptions, ModelInfo, vertexDefaultModelId, VertexModelId, vertexModels } from "../shared/api" // https://docs.anthropic.com/en/api/claude-on-vertex-ai export class VertexHandler implements ApiHandler { private options: ApiHandlerOptions private client: AnthropicVertex constructor(options: ApiHandlerOptions) { this.options = options this.client = new AnthropicVertex({ projectId: this.options.vertexProjectId, // https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions region: this.options.vertexRegion, }) } async createMessage( systemPrompt: string, messages: Anthropic.Messages.MessageParam[], tools: Anthropic.Messages.Tool[] ): Promise { const message = await this.client.messages.create({ model: this.getModel().id, max_tokens: this.getModel().info.maxTokens, system: systemPrompt, messages, tools, tool_choice: { type: "auto" }, }) return { message } } createUserReadableRequest( userContent: Array< | Anthropic.TextBlockParam | Anthropic.ImageBlockParam | Anthropic.ToolUseBlockParam | Anthropic.ToolResultBlockParam > ): any { return { model: this.getModel().id, max_tokens: this.getModel().info.maxTokens, system: "(see SYSTEM_PROMPT in src/ClaudeDev.ts)", messages: [{ conversation_history: "..." }, { role: "user", content: withoutImageData(userContent) }], tools: "(see tools in src/ClaudeDev.ts)", tool_choice: { type: "auto" }, } } getModel(): { id: VertexModelId; info: ModelInfo } { const modelId = this.options.apiModelId if (modelId && modelId in vertexModels) { const id = modelId as VertexModelId return { id, info: vertexModels[id] } } return { id: vertexDefaultModelId, info: vertexModels[vertexDefaultModelId] } } }