转换器: 图片内容块跨协议互转保留(base64/url)
- 支持 OpenAI image_url ↔ Anthropic image.source(base64/url) 双向转换 - messages→chat / responses→chat 不再丢弃图片块, 保留图文原始顺序 - chat→messages 的 data URL 拆成 base64 source(media_type+data) - chat→responses 的 image_url 转 input_image - responses↔messages 经 chat 中转, 六方向全覆盖 - 新增多图/顺序回归测试 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -306,3 +306,246 @@ func TestStreamMessagesToResponses(t *testing.T) {
|
||||
t.Fatalf("missing response.completed: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 图片内容块转换(messages image / chat image_url / responses input_image 互转不丢图)
|
||||
|
||||
const testB64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
|
||||
|
||||
// userContentBlocks 断言 Chat 用户消息 content 为块数组并返回。
|
||||
func userContentBlocks(t *testing.T, out []byte) []any {
|
||||
t.Helper()
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(out, &m); err != nil {
|
||||
t.Fatalf("unmarshal: %v\n%s", err, out)
|
||||
}
|
||||
msgs := m["messages"].([]any)
|
||||
if len(msgs) != 1 {
|
||||
t.Fatalf("messages len = %d: %s", len(msgs), out)
|
||||
}
|
||||
content, ok := msgs[0].(map[string]any)["content"].([]any)
|
||||
if !ok {
|
||||
t.Fatalf("content not array: %s", out)
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
// blockURL 取 image_url 块的 url。
|
||||
func blockURL(t *testing.T, b any) string {
|
||||
t.Helper()
|
||||
bm := b.(map[string]any)
|
||||
if bm["type"] != "image_url" {
|
||||
t.Fatalf("block type = %v", bm["type"])
|
||||
}
|
||||
iu := bm["image_url"].(map[string]any)
|
||||
url, _ := iu["url"].(string)
|
||||
return url
|
||||
}
|
||||
|
||||
func TestMessagesToChatReqImage(t *testing.T) {
|
||||
in := mustJSON(t, map[string]any{
|
||||
"model": "gpt-4o-mini",
|
||||
"messages": []any{map[string]any{"role": "user", "content": []any{
|
||||
map[string]any{"type": "text", "text": "describe"},
|
||||
map[string]any{"type": "image", "source": map[string]any{"type": "base64", "media_type": "image/png", "data": testB64}},
|
||||
map[string]any{"type": "image", "source": map[string]any{"type": "url", "url": "https://example.com/a.png"}},
|
||||
}}},
|
||||
})
|
||||
out, err := ConvertRequest([]byte(in), ProtoMessages, ProtoChat)
|
||||
if err != nil {
|
||||
t.Fatalf("convert: %v", err)
|
||||
}
|
||||
blocks := userContentBlocks(t, out)
|
||||
if len(blocks) != 3 {
|
||||
t.Fatalf("blocks len = %d: %s", len(blocks), out)
|
||||
}
|
||||
if got := blockURL(t, blocks[1]); got != "data:image/png;base64,"+testB64 {
|
||||
t.Fatalf("base64 image url = %q", got)
|
||||
}
|
||||
if got := blockURL(t, blocks[2]); got != "https://example.com/a.png" {
|
||||
t.Fatalf("url image = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// 多图 + 图/文交错顺序:转换后块数量与顺序保持不变(文档推荐的 Image 1:/Image 2: 标签模式)。
|
||||
func TestMessagesToChatReqMultiImage(t *testing.T) {
|
||||
img := func(url string) any {
|
||||
return map[string]any{"type": "image", "source": map[string]any{"type": "url", "url": url}}
|
||||
}
|
||||
in := mustJSON(t, map[string]any{
|
||||
"model": "gpt-4o-mini",
|
||||
"messages": []any{map[string]any{"role": "user", "content": []any{
|
||||
map[string]any{"type": "text", "text": "Image 1:"},
|
||||
img("https://example.com/1.png"),
|
||||
map[string]any{"type": "text", "text": "Image 2:"},
|
||||
img("https://example.com/2.png"),
|
||||
map[string]any{"type": "text", "text": "How are these images different?"},
|
||||
}}},
|
||||
})
|
||||
out, err := ConvertRequest([]byte(in), ProtoMessages, ProtoChat)
|
||||
if err != nil {
|
||||
t.Fatalf("convert: %v", err)
|
||||
}
|
||||
blocks := userContentBlocks(t, out)
|
||||
if len(blocks) != 5 {
|
||||
t.Fatalf("blocks len = %d: %s", len(blocks), out)
|
||||
}
|
||||
types := []string{}
|
||||
for _, b := range blocks {
|
||||
types = append(types, b.(map[string]any)["type"].(string))
|
||||
}
|
||||
want := []string{"text", "image_url", "text", "image_url", "text"}
|
||||
for i := range want {
|
||||
if types[i] != want[i] {
|
||||
t.Fatalf("block[%d] type = %s, want %s: %v", i, types[i], want[i], types)
|
||||
}
|
||||
}
|
||||
// 第二张图仍是第二张
|
||||
if got := blockURL(t, blocks[3]); got != "https://example.com/2.png" {
|
||||
t.Fatalf("image2 url = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatToMessagesReqImage(t *testing.T) {
|
||||
in := mustJSON(t, map[string]any{
|
||||
"model": "claude-sonnet-5",
|
||||
"messages": []any{map[string]any{"role": "user", "content": []any{
|
||||
map[string]any{"type": "text", "text": "describe"},
|
||||
map[string]any{"type": "image_url", "image_url": map[string]any{"url": "data:image/jpeg;base64," + testB64}},
|
||||
map[string]any{"type": "image_url", "image_url": map[string]any{"url": "https://example.com/b.png"}},
|
||||
}}},
|
||||
})
|
||||
out, err := ConvertRequest([]byte(in), ProtoChat, ProtoMessages)
|
||||
if err != nil {
|
||||
t.Fatalf("convert: %v", err)
|
||||
}
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(out, &m)
|
||||
msgs := m["messages"].([]any)
|
||||
content := msgs[0].(map[string]any)["content"].([]any)
|
||||
if len(content) != 3 {
|
||||
t.Fatalf("content len = %d: %s", len(content), out)
|
||||
}
|
||||
src1 := content[1].(map[string]any)["source"].(map[string]any)
|
||||
if src1["type"] != "base64" || src1["media_type"] != "image/jpeg" || src1["data"] != testB64 {
|
||||
t.Fatalf("base64 source = %v", src1)
|
||||
}
|
||||
src2 := content[2].(map[string]any)["source"].(map[string]any)
|
||||
if src2["type"] != "url" || src2["url"] != "https://example.com/b.png" {
|
||||
t.Fatalf("url source = %v", src2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponsesToChatReqImage(t *testing.T) {
|
||||
in := mustJSON(t, map[string]any{
|
||||
"model": "claude-sonnet-5",
|
||||
"input": []any{map[string]any{"role": "user", "content": []any{
|
||||
map[string]any{"type": "input_text", "text": "describe"},
|
||||
map[string]any{"type": "input_image", "image_url": "data:image/png;base64," + testB64},
|
||||
}}},
|
||||
})
|
||||
out, err := ConvertRequest([]byte(in), ProtoResponses, ProtoChat)
|
||||
if err != nil {
|
||||
t.Fatalf("convert: %v", err)
|
||||
}
|
||||
blocks := userContentBlocks(t, out)
|
||||
if len(blocks) != 2 {
|
||||
t.Fatalf("blocks len = %d: %s", len(blocks), out)
|
||||
}
|
||||
if got := blockURL(t, blocks[1]); got != "data:image/png;base64,"+testB64 {
|
||||
t.Fatalf("image url = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatToResponsesReqImage(t *testing.T) {
|
||||
in := mustJSON(t, map[string]any{
|
||||
"model": "gpt-4o",
|
||||
"messages": []any{map[string]any{"role": "user", "content": []any{
|
||||
map[string]any{"type": "text", "text": "describe"},
|
||||
map[string]any{"type": "image_url", "image_url": map[string]any{"url": "https://example.com/c.png"}},
|
||||
}}},
|
||||
})
|
||||
out, err := ConvertRequest([]byte(in), ProtoChat, ProtoResponses)
|
||||
if err != nil {
|
||||
t.Fatalf("convert: %v", err)
|
||||
}
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(out, &m)
|
||||
var input []map[string]any
|
||||
// 单条 user 消息项
|
||||
switch v := m["input"].(type) {
|
||||
case map[string]any:
|
||||
input = []map[string]any{v}
|
||||
case []any:
|
||||
for _, it := range v {
|
||||
input = append(input, it.(map[string]any))
|
||||
}
|
||||
}
|
||||
content := input[0]["content"].([]any)
|
||||
if len(content) != 2 {
|
||||
t.Fatalf("content len = %d: %s", len(content), out)
|
||||
}
|
||||
img := content[1].(map[string]any)
|
||||
if img["type"] != "input_image" {
|
||||
t.Fatalf("block type = %v", img["type"])
|
||||
}
|
||||
if img["image_url"] != "https://example.com/c.png" {
|
||||
t.Fatalf("image_url = %v", img["image_url"])
|
||||
}
|
||||
}
|
||||
|
||||
// 链式转换:responses→messages、messages→responses(经 chat 中转)也不丢图。
|
||||
func TestResponsesToMessagesReqImage(t *testing.T) {
|
||||
in := mustJSON(t, map[string]any{
|
||||
"model": "claude-sonnet-5",
|
||||
"input": []any{map[string]any{"role": "user", "content": []any{
|
||||
map[string]any{"type": "input_text", "text": "describe"},
|
||||
map[string]any{"type": "input_image", "image_url": "data:image/png;base64," + testB64},
|
||||
}}},
|
||||
})
|
||||
out, err := ConvertRequest([]byte(in), ProtoResponses, ProtoMessages)
|
||||
if err != nil {
|
||||
t.Fatalf("convert: %v", err)
|
||||
}
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(out, &m)
|
||||
msgs := m["messages"].([]any)
|
||||
content := msgs[0].(map[string]any)["content"].([]any)
|
||||
if len(content) != 2 {
|
||||
t.Fatalf("content len = %d: %s", len(content), out)
|
||||
}
|
||||
src := content[1].(map[string]any)["source"].(map[string]any)
|
||||
if src["type"] != "base64" || src["data"] != testB64 {
|
||||
t.Fatalf("source = %v", src)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessagesToResponsesReqImage(t *testing.T) {
|
||||
in := mustJSON(t, map[string]any{
|
||||
"model": "gpt-4o",
|
||||
"messages": []any{map[string]any{"role": "user", "content": []any{
|
||||
map[string]any{"type": "image", "source": map[string]any{"type": "url", "url": "https://example.com/d.png"}},
|
||||
map[string]any{"type": "text", "text": "describe"},
|
||||
}}},
|
||||
})
|
||||
out, err := ConvertRequest([]byte(in), ProtoMessages, ProtoResponses)
|
||||
if err != nil {
|
||||
t.Fatalf("convert: %v", err)
|
||||
}
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(out, &m)
|
||||
var content []any
|
||||
switch v := m["input"].(type) {
|
||||
case map[string]any:
|
||||
content = v["content"].([]any)
|
||||
case []any:
|
||||
content = v[0].(map[string]any)["content"].([]any)
|
||||
}
|
||||
if len(content) != 2 {
|
||||
t.Fatalf("content len = %d: %s", len(content), out)
|
||||
}
|
||||
img := content[0].(map[string]any)
|
||||
if img["type"] != "input_image" || img["image_url"] != "https://example.com/d.png" {
|
||||
t.Fatalf("input_image = %v", img)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user