|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +// Copyright (C) 2025-2026 lin-snow |
| 3 | + |
| 4 | +package agent |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + anthropic "github.com/anthropics/anthropic-sdk-go" |
| 10 | + openai "github.com/sashabaranov/go-openai" |
| 11 | +) |
| 12 | + |
| 13 | +// 累积器把跨 chunk 的 arguments 分片按 index 拼回完整 JSON。 |
| 14 | +func TestToolCallAccumulator_CrossChunk(t *testing.T) { |
| 15 | + a := newToolCallAccumulator() |
| 16 | + a.add([]openai.ToolCall{{Index: new(0), ID: "c1", Function: openai.FunctionCall{Name: "search"}}}) |
| 17 | + a.add([]openai.ToolCall{{Index: new(0), Function: openai.FunctionCall{Arguments: `{"q"`}}}) |
| 18 | + a.add([]openai.ToolCall{{Index: new(0), Function: openai.FunctionCall{Arguments: `:"x"}`}}}) |
| 19 | + |
| 20 | + got := a.finish() |
| 21 | + if len(got) != 1 { |
| 22 | + t.Fatalf("got %d tool calls, want 1", len(got)) |
| 23 | + } |
| 24 | + if got[0].ID != "c1" || got[0].Name != "search" { |
| 25 | + t.Fatalf("id/name = %q/%q, want c1/search", got[0].ID, got[0].Name) |
| 26 | + } |
| 27 | + if string(got[0].Args) != `{"q":"x"}` { |
| 28 | + t.Fatalf("args = %s, want {\"q\":\"x\"}", got[0].Args) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// 多个 index 的调用按出现顺序保留。 |
| 33 | +func TestToolCallAccumulator_MultipleIndicesPreserveOrder(t *testing.T) { |
| 34 | + a := newToolCallAccumulator() |
| 35 | + a.add([]openai.ToolCall{{Index: new(0), ID: "a", Function: openai.FunctionCall{Name: "first", Arguments: "{}"}}}) |
| 36 | + a.add([]openai.ToolCall{{Index: new(1), ID: "b", Function: openai.FunctionCall{Name: "second", Arguments: "{}"}}}) |
| 37 | + |
| 38 | + got := a.finish() |
| 39 | + if len(got) != 2 || got[0].Name != "first" || got[1].Name != "second" { |
| 40 | + t.Fatalf("order not preserved: %+v", got) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// 无 arguments 分片时兜底成 "{}"。 |
| 45 | +func TestToolCallAccumulator_EmptyArgsFallback(t *testing.T) { |
| 46 | + a := newToolCallAccumulator() |
| 47 | + a.add([]openai.ToolCall{{Index: new(0), ID: "a", Function: openai.FunctionCall{Name: "noargs"}}}) |
| 48 | + |
| 49 | + got := a.finish() |
| 50 | + if len(got) != 1 || string(got[0].Args) != "{}" { |
| 51 | + t.Fatalf("empty args should fall back to {}, got %+v", got) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// OpenAI 角色映射。 |
| 56 | +func TestToOpenAIRole(t *testing.T) { |
| 57 | + cases := map[Role]string{ |
| 58 | + RoleSystem: openai.ChatMessageRoleSystem, |
| 59 | + RoleAssistant: openai.ChatMessageRoleAssistant, |
| 60 | + RoleTool: openai.ChatMessageRoleTool, |
| 61 | + RoleUser: openai.ChatMessageRoleUser, |
| 62 | + Role("weird"): openai.ChatMessageRoleUser, // 未知角色兜底 user |
| 63 | + } |
| 64 | + for in, want := range cases { |
| 65 | + if got := toOpenAIRole(in); got != want { |
| 66 | + t.Fatalf("toOpenAIRole(%q) = %q, want %q", in, got, want) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// OpenAI buildMessages:RoleTool 带 ToolCallID;RoleAssistant 的 ToolCalls 映射为 openai.ToolCall; |
| 72 | +// 带图消息走 MultiContent 而非 Content。 |
| 73 | +func TestOpenAIBuildMessages(t *testing.T) { |
| 74 | + p := &openaiProvider{} |
| 75 | + in := []Message{ |
| 76 | + {Role: RoleAssistant, Content: "calling", ToolCalls: []ToolCall{{ID: "c1", Name: "search", Args: []byte(`{"q":"x"}`)}}}, |
| 77 | + {Role: RoleTool, ToolCallID: "c1", Content: "result"}, |
| 78 | + {Role: RoleUser, Content: "see image", Images: []ImagePart{{MediaType: "image/png", Base64: "abc"}}}, |
| 79 | + } |
| 80 | + msgs := p.buildMessages(in) |
| 81 | + if len(msgs) != 3 { |
| 82 | + t.Fatalf("got %d messages, want 3", len(msgs)) |
| 83 | + } |
| 84 | + if len(msgs[0].ToolCalls) != 1 || msgs[0].ToolCalls[0].Function.Arguments != `{"q":"x"}` { |
| 85 | + t.Fatalf("assistant tool_calls not mapped: %+v", msgs[0].ToolCalls) |
| 86 | + } |
| 87 | + if msgs[1].ToolCallID != "c1" { |
| 88 | + t.Fatalf("tool message ToolCallID = %q, want c1", msgs[1].ToolCallID) |
| 89 | + } |
| 90 | + if msgs[2].Content != "" || len(msgs[2].MultiContent) == 0 { |
| 91 | + t.Fatalf("image message should use MultiContent, got Content=%q MultiContent=%+v", msgs[2].Content, msgs[2].MultiContent) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// openAIImageParts:Base64 转 data URL,纯 URL 直链透传,空文本不产文本块,空图跳过。 |
| 96 | +func TestOpenAIImageParts(t *testing.T) { |
| 97 | + parts := openAIImageParts("hello", []ImagePart{ |
| 98 | + {MediaType: "image/png", Base64: "abc"}, |
| 99 | + {URL: "https://example.com/x.jpg"}, |
| 100 | + {}, // 既无 Base64 也无 URL → 跳过 |
| 101 | + }) |
| 102 | + // 1 文本块 + 2 图片块 |
| 103 | + if len(parts) != 3 { |
| 104 | + t.Fatalf("got %d parts, want 3 (1 text + 2 image)", len(parts)) |
| 105 | + } |
| 106 | + if parts[0].Type != openai.ChatMessagePartTypeText || parts[0].Text != "hello" { |
| 107 | + t.Fatalf("first part should be text 'hello', got %+v", parts[0]) |
| 108 | + } |
| 109 | + if parts[1].ImageURL == nil || parts[1].ImageURL.URL != "data:image/png;base64,abc" { |
| 110 | + t.Fatalf("base64 image should become data URL, got %+v", parts[1].ImageURL) |
| 111 | + } |
| 112 | + if parts[2].ImageURL == nil || parts[2].ImageURL.URL != "https://example.com/x.jpg" { |
| 113 | + t.Fatalf("url image should pass through, got %+v", parts[2].ImageURL) |
| 114 | + } |
| 115 | + |
| 116 | + // 空文本不产文本块。 |
| 117 | + noText := openAIImageParts("", []ImagePart{{Base64: "abc", MediaType: "image/png"}}) |
| 118 | + if len(noText) != 1 || noText[0].Type != openai.ChatMessagePartTypeImageURL { |
| 119 | + t.Fatalf("empty text should yield only the image part, got %+v", noText) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// Anthropic buildMessages:连续的 RoleTool 合并进单条 user 消息(满足 tool_result 同处一条 user 的约束)。 |
| 124 | +func TestAnthropicBuildMessages_ConsecutiveToolsMerge(t *testing.T) { |
| 125 | + p := &anthropicProvider{} |
| 126 | + in := []Message{ |
| 127 | + {Role: RoleSystem, Content: "you are a bot"}, |
| 128 | + {Role: RoleUser, Content: "q"}, |
| 129 | + {Role: RoleAssistant, ToolCalls: []ToolCall{{ID: "c1", Name: "search", Args: []byte(`{}`)}}}, |
| 130 | + {Role: RoleTool, ToolCallID: "c1", Content: "r1"}, |
| 131 | + {Role: RoleTool, ToolCallID: "c2", Content: "r2"}, |
| 132 | + {Role: RoleUser, Content: "follow up"}, |
| 133 | + } |
| 134 | + |
| 135 | + systemBlocks, msgs := p.buildMessages(in) |
| 136 | + |
| 137 | + if len(systemBlocks) != 1 || systemBlocks[0].Text != "you are a bot" { |
| 138 | + t.Fatalf("system blocks = %+v, want single 'you are a bot'", systemBlocks) |
| 139 | + } |
| 140 | + // 期望序列:user(q) / assistant(tool_use) / user(2×tool_result 合并) / user(follow up) |
| 141 | + if len(msgs) != 4 { |
| 142 | + t.Fatalf("got %d messages, want 4: %+v", len(msgs), msgs) |
| 143 | + } |
| 144 | + if msgs[0].Role != anthropic.MessageParamRoleUser || msgs[1].Role != anthropic.MessageParamRoleAssistant { |
| 145 | + t.Fatalf("unexpected leading roles: %v / %v", msgs[0].Role, msgs[1].Role) |
| 146 | + } |
| 147 | + // 第三条是合并后的 tool_result,应含 2 个 tool_result block。 |
| 148 | + merged := msgs[2] |
| 149 | + if merged.Role != anthropic.MessageParamRoleUser { |
| 150 | + t.Fatalf("merged tool results should be a user message, got role %v", merged.Role) |
| 151 | + } |
| 152 | + if len(merged.Content) != 2 { |
| 153 | + t.Fatalf("merged message should have 2 tool_result blocks, got %d", len(merged.Content)) |
| 154 | + } |
| 155 | + for i, b := range merged.Content { |
| 156 | + if b.OfToolResult == nil { |
| 157 | + t.Fatalf("merged block %d is not a tool_result: %+v", i, b) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// userBlocks:无内容无图时兜底成一个(空)文本块,避免空消息;Base64 与 URL 各走对应 image source。 |
| 163 | +func TestAnthropicUserBlocks(t *testing.T) { |
| 164 | + // 空消息兜底。 |
| 165 | + empty := userBlocks(Message{Role: RoleUser}) |
| 166 | + if len(empty) != 1 || empty[0].OfText == nil { |
| 167 | + t.Fatalf("empty user message should fall back to a single text block, got %+v", empty) |
| 168 | + } |
| 169 | + |
| 170 | + // 文本 + base64 图 + url 图。 |
| 171 | + blocks := userBlocks(Message{ |
| 172 | + Role: RoleUser, |
| 173 | + Content: "txt", |
| 174 | + Images: []ImagePart{ |
| 175 | + {MediaType: "image/png", Base64: "abc"}, |
| 176 | + {URL: "https://example.com/y.png"}, |
| 177 | + }, |
| 178 | + }) |
| 179 | + if len(blocks) != 3 { |
| 180 | + t.Fatalf("got %d blocks, want 3 (text + 2 images)", len(blocks)) |
| 181 | + } |
| 182 | + if blocks[0].OfText == nil || blocks[0].OfText.Text != "txt" { |
| 183 | + t.Fatalf("first block should be text 'txt', got %+v", blocks[0]) |
| 184 | + } |
| 185 | + if blocks[1].OfImage == nil || blocks[2].OfImage == nil { |
| 186 | + t.Fatalf("image blocks not produced: %+v", blocks) |
| 187 | + } |
| 188 | +} |
0 commit comments