Skip to content

Commit 9575209

Browse files
committed
Fix tests
1 parent 57f2a4e commit 9575209

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

src/api/transform/gemini-format.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function convertAnthropicContentToGemini(content: string | Anthropic.Cont
66
return [{ text: content }]
77
}
88

9-
return content.flatMap((block): Part => {
9+
return content.flatMap((block): Part | Part[] => {
1010
switch (block.type) {
1111
case "text":
1212
return { text: block.text }
@@ -16,6 +16,79 @@ export function convertAnthropicContentToGemini(content: string | Anthropic.Cont
1616
}
1717

1818
return { inlineData: { data: block.source.data, mimeType: block.source.media_type } }
19+
case "tool_use":
20+
return {
21+
functionCall: {
22+
name: block.name,
23+
args: block.input as Record<string, unknown>,
24+
},
25+
}
26+
case "tool_result": {
27+
// Skip empty tool results
28+
if (!block.content) {
29+
return []
30+
}
31+
32+
// Extract tool name from tool_use_id (e.g., "calculator-123" -> "calculator")
33+
const toolName = block.tool_use_id.split("-")[0]
34+
35+
// Handle string content
36+
if (typeof block.content === "string") {
37+
return {
38+
functionResponse: {
39+
name: toolName,
40+
response: {
41+
name: toolName,
42+
content: block.content,
43+
},
44+
},
45+
}
46+
}
47+
48+
// Handle array content
49+
if (Array.isArray(block.content)) {
50+
const textParts: string[] = []
51+
const imageParts: Part[] = []
52+
53+
block.content.forEach((item) => {
54+
if (item.type === "text") {
55+
textParts.push(item.text)
56+
} else if (item.type === "image") {
57+
if (item.source.type === "base64") {
58+
imageParts.push({
59+
inlineData: {
60+
data: item.source.data,
61+
mimeType: item.source.media_type,
62+
},
63+
})
64+
}
65+
}
66+
})
67+
68+
// Create content text with a note about images if present
69+
const contentText =
70+
textParts.join("\n\n") +
71+
(imageParts.length > 0
72+
? (textParts.length > 0 ? "\n\n" : "\n\n") + "(See next part for image)"
73+
: "")
74+
75+
// Return function response followed by any images
76+
return [
77+
{
78+
functionResponse: {
79+
name: toolName,
80+
response: {
81+
name: toolName,
82+
content: contentText,
83+
},
84+
},
85+
},
86+
...imageParts,
87+
]
88+
}
89+
90+
return []
91+
}
1992
default:
2093
throw new Error(`Unsupported content block type: ${block.type}`)
2194
}

0 commit comments

Comments
 (0)