| 1 | import { |
| 2 | Base64ImageSource, |
| 3 | ErrorResponse, |
| 4 | MessageParam, |
| 5 | Tool, |
| 6 | ToolChoice, |
| 7 | } from "@anthropic-ai/sdk/resources"; |
| 8 | import { |
| 9 | ChatCompletionTool, |
| 10 | ChatCompletionToolChoiceOption, |
| 11 | } from "openai/resources"; |
| 12 | |
| 13 | export function getAnthropicErrorMessage(response: ErrorResponse): string { |
| 14 | switch (response.error.type) { |
| 15 | case "api_error": |
| 16 | return "An unexpected error has occurred internal to Anthropic's systems."; |
| 17 | case "invalid_request_error": |
| 18 | return "There was an issue with the format or content of your request."; |
| 19 | case "authentication_error": |
| 20 | return "There's an issue with your API key."; |
| 21 | case "permission_error": |
| 22 | return "Your API key does not have permission to use the specified resource."; |
| 23 | case "not_found_error": |
| 24 | return "The requested resource was not found."; |
| 25 | case "rate_limit_error": |
| 26 | return "Your account has hit a rate limit."; |
| 27 | case "overloaded_error": |
| 28 | return "Anthropic's API is temporarily overloaded. Please check their status page: https://status.anthropic.com/#past-incidents"; |
| 29 | case "timeout_error": |
| 30 | return "Anthropic API timed out. Please check their status page: https://status.anthropic.com/#past-incidents"; |
| 31 | case "billing_error": |
| 32 | default: |
| 33 | return response.error.message; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | export function getAnthropicHeaders( |
| 38 | apiKey: string, |
| 39 | enableCaching: boolean, |
| 40 | ): Record<string, string> { |
| 41 | const headers: Record<string, string> = { |
| 42 | "Content-Type": "application/json", |
| 43 | Accept: "application/json", |
| 44 | "anthropic-version": "2023-06-01", |
| 45 | "x-api-key": apiKey, |
| 46 | }; |
| 47 | |
| 48 | if (enableCaching) { |
| 49 | headers["anthropic-beta"] = "prompt-caching-2024-07-31"; |
| 50 | } |
| 51 | return headers; |
| 52 | } |
| 53 | |
| 54 | export function addCacheControlToLastTwoUserMessages(messages: MessageParam[]) { |
| 55 | let userMessages = 0; |
| 56 | for (let i = messages.length - 1; i >= 0; i--) { |
| 57 | const msg = messages[i]; |
| 58 | if (msg.role === "user") { |
| 59 | userMessages++; |
| 60 | if (typeof msg.content === "string") { |
| 61 | continue; |
| 62 | } |
| 63 | for (let j = msg.content.length - 1; j >= 0; j--) { |
| 64 | const part = msg.content[j]; |
| 65 | if (part.type === "text") { |
| 66 | part.cache_control = { type: "ephemeral" }; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | if (userMessages >= 2) { |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | export function openAiToolChoiceToAnthropicToolChoice( |
| 78 | toolChoice: ChatCompletionToolChoiceOption | undefined, |
| 79 | ): ToolChoice | undefined { |
| 80 | if (!toolChoice) { |
| 81 | return undefined; |
| 82 | } |
| 83 | switch (toolChoice) { |
| 84 | case "none": |
| 85 | // "none" is handled by excluding tools |
| 86 | return undefined; |
| 87 | case "required": |
| 88 | return { |
| 89 | type: "any", |
| 90 | }; |
| 91 | case "auto": |
| 92 | return { |
| 93 | type: "auto", |
| 94 | }; |
| 95 | default: |
| 96 | switch (toolChoice.type) { |
| 97 | case "allowed_tools": |
| 98 | // Filtering of tools is handled elsewhere |
| 99 | return { |
| 100 | type: toolChoice.allowed_tools.mode === "auto" ? "auto" : "any", |
| 101 | }; |
| 102 | case "custom": |
| 103 | return undefined; // TODO not supported yet |
| 104 | case "function": |
| 105 | return { |
| 106 | type: "tool", |
| 107 | name: toolChoice.function.name, |
| 108 | }; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | export function openaiToolToAnthropicTool(tool: ChatCompletionTool): Tool { |
| 114 | if (tool.type === "function" && "function" in tool) { |
| 115 | return { |
| 116 | name: tool.function.name, |
| 117 | description: tool.function.description, |
| 118 | input_schema: tool.function.parameters as Tool.InputSchema, // TODO unsafe cast, may be differences between openai tool schema and anthropic tool schema, |
| 119 | }; |
| 120 | } else { |
| 121 | throw new Error(`Unsupported tool type in Anthropic: ${tool.type}`); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Extract media type from data URL (ex. "data:image/png;base64,..." -> "image/png") |
| 126 | export function getAnthropicMediaTypeFromDataUrl( |
| 127 | dataUrl: string, |
| 128 | ): Base64ImageSource["media_type"] { |
| 129 | const match = dataUrl.match(/^data:([^;]+);base64,/); |
| 130 | if (match) { |
| 131 | switch (match[1]) { |
| 132 | case "image/png": |
| 133 | case "image/gif": |
| 134 | case "image/webp": |
| 135 | return match[1]; |
| 136 | } |
| 137 | } |
| 138 | return "image/jpeg"; |
| 139 | } |