| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import { Anthropic } from "@anthropic-ai/sdk"
- import { ApiConfiguration, ApiModelId, ModelInfo } from "../shared/api"
- import { AnthropicHandler } from "./anthropic"
- import { AwsBedrockHandler } from "./bedrock"
- import { OpenRouterHandler } from "./openrouter"
- export interface ApiHandler {
- createMessage(
- systemPrompt: string,
- messages: Anthropic.Messages.MessageParam[],
- tools: Anthropic.Messages.Tool[]
- ): Promise<Anthropic.Messages.Message>
- createUserReadableRequest(
- userContent: Array<
- | Anthropic.TextBlockParam
- | Anthropic.ImageBlockParam
- | Anthropic.ToolUseBlockParam
- | Anthropic.ToolResultBlockParam
- >
- ): any
- getModel(): { id: ApiModelId; info: ModelInfo }
- }
- export function buildApiHandler(configuration: ApiConfiguration): ApiHandler {
- const { apiProvider, ...options } = configuration
- switch (apiProvider) {
- case "anthropic":
- return new AnthropicHandler(options)
- case "openrouter":
- return new OpenRouterHandler(options)
- case "bedrock":
- return new AwsBedrockHandler(options)
- default:
- return new AnthropicHandler(options)
- }
- }
- export function withoutImageData(
- userContent: Array<
- | Anthropic.TextBlockParam
- | Anthropic.ImageBlockParam
- | Anthropic.ToolUseBlockParam
- | Anthropic.ToolResultBlockParam
- >
- ): Array<
- Anthropic.TextBlockParam | Anthropic.ImageBlockParam | Anthropic.ToolUseBlockParam | Anthropic.ToolResultBlockParam
- > {
- return userContent.map((part) => {
- if (part.type === "image") {
- return { ...part, source: { ...part.source, data: "..." } }
- } else if (part.type === "tool_result" && typeof part.content !== "string") {
- return {
- ...part,
- content: part.content?.map((contentPart) => {
- if (contentPart.type === "image") {
- return { ...contentPart, source: { ...contentPart.source, data: "..." } }
- }
- return contentPart
- }),
- }
- }
- return part
- })
- }
|