(
messages: AnthropicMessage[],
options?: { mergeToolResultText?: boolean },
)
| 35 | * @returns Array of OpenAI messages optimized for Z.ai's thinking mode |
| 36 | */ |
| 37 | export function convertToZAiFormat( |
| 38 | messages: AnthropicMessage[], |
| 39 | options?: { mergeToolResultText?: boolean }, |
| 40 | ): Message[] { |
| 41 | const result: Message[] = [] |
| 42 | |
| 43 | for (const message of messages) { |
| 44 | // Check if the message has reasoning_content (for Z.ai interleaved thinking) |
| 45 | const messageWithReasoning = message as AnthropicMessage & { reasoning_content?: string } |
| 46 | const reasoningContent = messageWithReasoning.reasoning_content |
| 47 | |
| 48 | if (message.role === "user") { |
| 49 | // Handle user messages - may contain tool_result blocks |
| 50 | if (Array.isArray(message.content)) { |
| 51 | const textParts: string[] = [] |
| 52 | const imageParts: ContentPartImage[] = [] |
| 53 | const toolResults: { tool_use_id: string; content: string }[] = [] |
| 54 | |
| 55 | for (const part of message.content) { |
| 56 | if (part.type === "text") { |
| 57 | textParts.push(part.text) |
| 58 | } else if (part.type === "image") { |
| 59 | if (part.source.type === "base64") { |
| 60 | imageParts.push({ |
| 61 | type: "image_url", |
| 62 | image_url: { url: `data:${part.source.media_type};base64,${part.source.data}` }, |
| 63 | }) |
| 64 | } |
| 65 | } else if (part.type === "tool_result") { |
| 66 | // Convert tool_result to OpenAI tool message format |
| 67 | let content: string |
| 68 | if (typeof part.content === "string") { |
| 69 | content = part.content |
| 70 | } else if (Array.isArray(part.content)) { |
| 71 | content = |
| 72 | part.content |
| 73 | ?.map((c) => { |
| 74 | if (c.type === "text") return c.text |
| 75 | if (c.type === "image") return "(image)" |
| 76 | return "" |
| 77 | }) |
| 78 | .join("\n") ?? "" |
| 79 | } else { |
| 80 | content = "" |
| 81 | } |
| 82 | toolResults.push({ |
| 83 | tool_use_id: part.tool_use_id, |
| 84 | content, |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Add tool messages first (they must follow assistant tool_use) |
| 90 | for (const toolResult of toolResults) { |
| 91 | const toolMessage: ToolMessage = { |
| 92 | role: "tool", |
| 93 | tool_call_id: toolResult.tool_use_id, |
| 94 | content: toolResult.content, |
no outgoing calls
no test coverage detected