MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / convertToAiSdkMessages

Function convertToAiSdkMessages

src/api/transform/ai-sdk.ts:18–153  ·  view source on GitHub ↗
(messages: Anthropic.Messages.MessageParam[])

Source from the content-addressed store, hash-verified

16 * @returns Array of AI SDK ModelMessage objects
17 */
18export function convertToAiSdkMessages(messages: Anthropic.Messages.MessageParam[]): ModelMessage[] {
19 const modelMessages: ModelMessage[] = []
20
21 // First pass: build a map of tool call IDs to tool names from assistant messages
22 const toolCallIdToName = new Map<string, string>()
23 for (const message of messages) {
24 if (message.role === "assistant" && typeof message.content !== "string") {
25 for (const part of message.content) {
26 if (part.type === "tool_use") {
27 toolCallIdToName.set(part.id, part.name)
28 }
29 }
30 }
31 }
32
33 for (const message of messages) {
34 if (typeof message.content === "string") {
35 modelMessages.push({
36 role: message.role,
37 content: message.content,
38 })
39 } else {
40 if (message.role === "user") {
41 const parts: Array<
42 { type: "text"; text: string } | { type: "image"; image: string; mimeType?: string }
43 > = []
44 const toolResults: Array<{
45 type: "tool-result"
46 toolCallId: string
47 toolName: string
48 output: { type: "text"; value: string }
49 }> = []
50
51 for (const part of message.content) {
52 if (part.type === "text") {
53 parts.push({ type: "text", text: part.text })
54 } else if (part.type === "image") {
55 // Handle both base64 and URL source types
56 const source = part.source as { type: string; media_type?: string; data?: string; url?: string }
57 if (source.type === "base64" && source.media_type && source.data) {
58 parts.push({
59 type: "image",
60 image: `data:${source.media_type};base64,${source.data}`,
61 mimeType: source.media_type,
62 })
63 } else if (source.type === "url" && source.url) {
64 parts.push({
65 type: "image",
66 image: source.url,
67 })
68 }
69 } else if (part.type === "tool_result") {
70 // Convert tool results to string content
71 let content: string
72 if (typeof part.content === "string") {
73 content = part.content
74 } else {
75 content =

Callers 3

createMessageFunction · 0.90
createMessageMethod · 0.90
ai-sdk.spec.tsFile · 0.90

Calls 2

setMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected