( anthropicMessages: Anthropic.Messages.MessageParam[], )
| 29 | } |
| 30 | |
| 31 | export function convertToVsCodeLmMessages( |
| 32 | anthropicMessages: Anthropic.Messages.MessageParam[], |
| 33 | ): vscode.LanguageModelChatMessage[] { |
| 34 | const vsCodeLmMessages: vscode.LanguageModelChatMessage[] = [] |
| 35 | |
| 36 | for (const anthropicMessage of anthropicMessages) { |
| 37 | // Handle simple string messages |
| 38 | if (typeof anthropicMessage.content === "string") { |
| 39 | vsCodeLmMessages.push( |
| 40 | anthropicMessage.role === "assistant" |
| 41 | ? vscode.LanguageModelChatMessage.Assistant(anthropicMessage.content) |
| 42 | : vscode.LanguageModelChatMessage.User(anthropicMessage.content), |
| 43 | ) |
| 44 | continue |
| 45 | } |
| 46 | |
| 47 | // Handle complex message structures |
| 48 | switch (anthropicMessage.role) { |
| 49 | case "user": { |
| 50 | const { nonToolMessages, toolMessages } = anthropicMessage.content.reduce<{ |
| 51 | nonToolMessages: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[] |
| 52 | toolMessages: Anthropic.ToolResultBlockParam[] |
| 53 | }>( |
| 54 | (acc, part) => { |
| 55 | if (part.type === "tool_result") { |
| 56 | acc.toolMessages.push(part) |
| 57 | } else if (part.type === "text" || part.type === "image") { |
| 58 | acc.nonToolMessages.push(part) |
| 59 | } |
| 60 | return acc |
| 61 | }, |
| 62 | { nonToolMessages: [], toolMessages: [] }, |
| 63 | ) |
| 64 | |
| 65 | // Process tool messages first then non-tool messages |
| 66 | const contentParts = [ |
| 67 | // Convert tool messages to ToolResultParts |
| 68 | ...toolMessages.map((toolMessage) => { |
| 69 | // Process tool result content into TextParts |
| 70 | const toolContentParts: vscode.LanguageModelTextPart[] = |
| 71 | typeof toolMessage.content === "string" |
| 72 | ? [new vscode.LanguageModelTextPart(toolMessage.content)] |
| 73 | : (toolMessage.content?.map((part) => { |
| 74 | if (part.type === "image") { |
| 75 | if (part.source.type === "base64") { |
| 76 | return new vscode.LanguageModelTextPart( |
| 77 | `[Image (base64): ${part.source.media_type} not supported by VSCode LM API]`, |
| 78 | ) |
| 79 | } |
| 80 | return new vscode.LanguageModelTextPart( |
| 81 | `[Image (${part.source.type}): not supported by VSCode LM API]`, |
| 82 | ) |
| 83 | } |
| 84 | if (part.type === "text") { |
| 85 | return new vscode.LanguageModelTextPart(part.text) |
| 86 | } |
| 87 | return new vscode.LanguageModelTextPart("") |
| 88 | }) ?? [new vscode.LanguageModelTextPart("")]) |
no test coverage detected