blob: 1bc0c95b3831a8a255df9dd55f09c59f0a1b5a1a [file] [log] [blame]
Philip Zeyliger72252cb2025-05-10 17:00:08 -07001package ant
2
3import (
4 "encoding/json"
5 "testing"
6
7 "sketch.dev/llm"
8)
9
10func TestAnthropicImageToolResult(t *testing.T) {
11 // Create a tool result with both text and image content
12 textContent := llm.Content{
13 Type: llm.ContentTypeText,
14 Text: "15 degrees",
15 }
16
17 imageContent := llm.Content{
18 Type: llm.ContentTypeText, // Will be mapped to "image" in Anthropic format
19 MediaType: "image/jpeg",
20 Data: "/9j/4AAQSkZJRg...", // Shortened base64 encoded image
21 }
22
23 toolResult := llm.Content{
24 Type: llm.ContentTypeToolResult,
25 ToolUseID: "toolu_01A09q90qw90lq917835lq9",
26 ToolResult: []llm.Content{textContent, imageContent},
27 }
28
29 // Convert to Anthropic format
30 anthropicContent := fromLLMContent(toolResult)
31
32 // Check the type
33 if anthropicContent.Type != "tool_result" {
34 t.Errorf("Expected type to be 'tool_result', got '%s'", anthropicContent.Type)
35 }
36
37 // Check the tool_use_id
38 if anthropicContent.ToolUseID != "toolu_01A09q90qw90lq917835lq9" {
39 t.Errorf("Expected tool_use_id to be 'toolu_01A09q90qw90lq917835lq9', got '%s'", anthropicContent.ToolUseID)
40 }
41
42 // Check that we have two content items in the tool result
43 if len(anthropicContent.ToolResult) != 2 {
44 t.Errorf("Expected 2 content items, got %d", len(anthropicContent.ToolResult))
45 }
46
47 // Check that the first item is text
48 if anthropicContent.ToolResult[0].Type != "text" {
49 t.Errorf("Expected first content type to be 'text', got '%s'", anthropicContent.ToolResult[0].Type)
50 }
51
52 if *anthropicContent.ToolResult[0].Text != "15 degrees" {
53 t.Errorf("Expected first content text to be '15 degrees', got '%s'", *anthropicContent.ToolResult[0].Text)
54 }
55
56 // Check that the second item is an image
57 if anthropicContent.ToolResult[1].Type != "image" {
58 t.Errorf("Expected second content type to be 'image', got '%s'", anthropicContent.ToolResult[1].Type)
59 }
60
61 // Check that the image source contains the expected format
62 var source map[string]any
63 if err := json.Unmarshal(anthropicContent.ToolResult[1].Source, &source); err != nil {
64 t.Errorf("Failed to unmarshal image source: %v", err)
65 }
66
67 if source["type"] != "base64" {
68 t.Errorf("Expected source type to be 'base64', got '%s'", source["type"])
69 }
70
71 if source["media_type"] != "image/jpeg" {
72 t.Errorf("Expected media_type to be 'image/jpeg', got '%s'", source["media_type"])
73 }
74
75 if source["data"] != "/9j/4AAQSkZJRg..." {
76 t.Errorf("Expected data to be '/9j/4AAQSkZJRg...', got '%s'", source["data"])
77 }
78}