diff --git a/internal/gateway/tool_images_test.go b/internal/gateway/tool_images_test.go new file mode 100644 index 0000000..c5bc9f8 --- /dev/null +++ b/internal/gateway/tool_images_test.go @@ -0,0 +1,145 @@ +package gateway + +import ( + "bytes" + "encoding/json" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "opencode2api/internal/config" + wire "opencode2api/internal/protocol" + "opencode2api/internal/telemetry" +) + +// Exercise the real gateway handler and HTTP transport, not just converters. +func TestToolImagesForwardedHTTP(t *testing.T) { + for _, from := range []wire.Protocol{wire.Anthropic, wire.Responses} { + for _, to := range []wire.Protocol{wire.Chat, wire.Responses, wire.Anthropic} { + for _, stream := range []bool{false, true} { + t.Run(string(from)+"-"+string(to)+map[bool]string{false: "-json", true: "-stream"}[stream], func(t *testing.T) { + captured := make(chan map[string]any, 1) + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != wire.Path(to) { + t.Errorf("upstream path %s", r.URL.Path) + } + var body map[string]any + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + t.Error(err) + } + captured <- body + // Also prove a supplier rejection is surfaced, never retried without images. + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(400) + io.WriteString(w, `{"error":{"message":"fixture rejects image","type":"invalid_request_error"}}`) + })) + defer upstream.Close() + path := filepath.Join(t.TempDir(), "config.json") + cfgJSON := map[string]any{"server_keys": []string{"local-test"}, "go_keys": []string{"upstream-test"}, "upstream": map[string]string{"go": upstream.URL, "zen": upstream.URL}, "models": map[string]any{"protocols": map[string]string{"vision": string(to)}}} + b, _ := json.Marshal(cfgJSON) + if err := os.WriteFile(path, b, 0600); err != nil { + t.Fatal(err) + } + cfg, err := config.Load(path) + if err != nil { + t.Fatal(err) + } + g, err := New(cfg, slog.New(slog.NewTextHandler(io.Discard, nil)), telemetry.NewMonitor()) + if err != nil { + t.Fatal(err) + } + g.catalog.Replace(nil, []string{"vision"}) + server := httptest.NewServer(g.Handler()) + defer server.Close() + var input map[string]any + json.Unmarshal([]byte(`{"model":"vision","messages":[{"role":"assistant","content":[{"type":"tool_use","id":"A","name":"read","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"A","content":[{"type":"text","text":"read result"},{"type":"image","source":{"type":"base64","media_type":"image/png","data":"aW1hZ2U="}}]}]}]}`), &input) + if from == wire.Responses { + input, err = wire.ConvertRequest(wire.Anthropic, wire.Responses, input) + if err != nil { + t.Fatal(err) + } + } + input["stream"] = stream + b, _ = json.Marshal(input) + req, _ := http.NewRequest("POST", server.URL+wire.Path(from), bytes.NewReader(b)) + req.Header.Set("Authorization", "Bearer local-test") + resp, err := server.Client().Do(req) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + response, _ := io.ReadAll(resp.Body) + if resp.StatusCode != 400 || !strings.Contains(string(response), "fixture rejects image") { + t.Fatalf("status=%d body=%s", resp.StatusCode, response) + } + select { + case body := <-captured: + encoded, _ := json.Marshal(body) + if bytes.Contains(encoded, []byte(`\"source\"`)) || bytes.Contains(encoded, []byte(`\"input_image\"`)) { + t.Fatal("image stringified") + } + if strings.Count(string(encoded), "aW1hZ2U=") != 1 { + t.Fatalf("image lost or duplicated: %s", encoded) + } + if body["stream"] != stream { + t.Fatal("stream flag changed") + } + if to == wire.Chat { + messages := body["messages"].([]any) + tool := messages[1].(map[string]any) + if strings.Contains(tool["content"].(string), "aW1hZ2U=") { + t.Fatal("base64 in tool text") + } + } + default: + t.Fatal("no upstream request") + } + if from != to { + var removeSources func(any) + removeSources = func(v any) { + switch x := v.(type) { + case map[string]any: + if x["type"] == "image" { + delete(x, "source") + } + if x["type"] == "input_image" { + delete(x, "image_url") + } + for _, child := range x { + removeSources(child) + } + case []any: + for _, child := range x { + removeSources(child) + } + } + } + removeSources(input) + b, _ = json.Marshal(input) + req, _ = http.NewRequest("POST", server.URL+wire.Path(from), bytes.NewReader(b)) + req.Header.Set("Authorization", "Bearer local-test") + bad, err := server.Client().Do(req) + if err != nil { + t.Fatal(err) + } + io.Copy(io.Discard, bad.Body) + bad.Body.Close() + if bad.StatusCode != 400 { + t.Fatalf("invalid image status: %d", bad.StatusCode) + } + select { + case <-captured: + t.Fatal("invalid image reached upstream") + default: + } + } + }) + } + } + } +} diff --git a/internal/protocol/tool_images_test.go b/internal/protocol/tool_images_test.go new file mode 100644 index 0000000..9b7581a --- /dev/null +++ b/internal/protocol/tool_images_test.go @@ -0,0 +1,158 @@ +package protocol + +import ( + "encoding/json" + "reflect" + "strings" + "testing" +) + +func toolImageRequest(t *testing.T) map[string]any { + t.Helper() + var request map[string]any + err := json.Unmarshal([]byte(`{"model":"vision","messages":[ +{"role":"assistant","content":[{"type":"tool_use","id":"A","name":"read","input":{}},{"type":"tool_use","id":"B","name":"read","input":{}}]}, +{"role":"user","content":[{"type":"tool_result","tool_use_id":"B","content":[{"type":"image","source":{"type":"url","url":"https://example.test/b.png"}}]}]}, +{"role":"user","content":[{"type":"tool_result","tool_use_id":"A","content":[{"type":"text","text":"before"},{"type":"image","source":{"type":"base64","media_type":"image/png","data":"aW1hZ2U="}},{"type":"text","text":"after"},{"type":"image","source":{"type":"url","url":"https://example.test/a2.png"}}]},{"type":"text","text":"compare"},{"type":"image","source":{"type":"url","url":"https://example.test/user.png"}}]}]}`), &request) + if err != nil { + t.Fatal(err) + } + return request +} + +func TestToolImagesChatOrderAndPurity(t *testing.T) { + input := toolImageRequest(t) + before, _ := json.Marshal(input) + for _, from := range []Protocol{Anthropic, Responses} { + source := input + if from == Responses { + var err error + source, err = ConvertRequest(Anthropic, Responses, input) + if err != nil { + t.Fatal(err) + } + } + for run := 0; run < 2; run++ { + out, err := ConvertRequest(from, Chat, source) + if err != nil { + t.Fatal(err) + } + messages := out["messages"].([]any) + if len(messages) != 6 { + t.Fatalf("messages: %#v", messages) + } + for i, id := range []string{"A", "B"} { + tool := messages[i+1].(map[string]any) + if tool["role"] != "tool" || tool["tool_call_id"] != id { + t.Fatalf("tool order: %#v", tool) + } + text := tool["content"].(string) + if strings.Contains(text, "aW1hZ2U=") || !strings.Contains(text, `call_id="`+id+`"`) { + t.Fatalf("tool content: %s", text) + } + } + a := messages[3].(map[string]any)["content"].([]any) + if len(a) != 4 || a[1].(map[string]any)["image_url"].(map[string]any)["url"] != "data:image/png;base64,aW1hZ2U=" { + t.Fatalf("attachments: %#v", a) + } + if !strings.Contains(a[2].(map[string]any)["text"].(string), "index=2") { + t.Fatal("image index lost") + } + last, _ := json.Marshal(messages[5]) + if !strings.Contains(string(last), "user.png") { + t.Fatal("user image lost") + } + } + } + after, _ := json.Marshal(input) + if string(before) != string(after) { + t.Fatal("input mutated") + } +} + +func TestToolImagesNativeAndPassThrough(t *testing.T) { + input := toolImageRequest(t) + r, err := ConvertRequest(Anthropic, Responses, input) + if err != nil { + t.Fatal(err) + } + items := r["input"].([]any) + for _, raw := range items { + item := raw.(map[string]any) + if item["type"] == "function_call_output" { + if _, ok := item["output"].([]any); !ok { + t.Fatal("tool image stringified") + } + } + } + a, err := ConvertRequest(Responses, Anthropic, r) + if err != nil { + t.Fatal(err) + } + encoded, _ := json.Marshal(a) + if strings.Contains(string(encoded), "input_image") || !strings.Contains(string(encoded), `"type":"base64"`) { + t.Fatalf("native image conversion: %s", encoded) + } + for _, tc := range []struct { + p Protocol + input map[string]any + }{{Anthropic, input}, {Responses, r}, {Chat, map[string]any{"messages": []any{map[string]any{"role": "tool", "content": "unchanged"}}}}} { + out, err := ConvertRequest(tc.p, tc.p, tc.input) + if err != nil { + t.Fatal(err) + } + b1, _ := json.Marshal(out) + b2, _ := json.Marshal(tc.input) + if string(b1) != string(b2) { + t.Fatal("passthrough changed") + } + } +} + +func TestToolImagesRejectMalformedAndPreserveText(t *testing.T) { + for _, value := range []any{nil, "", "plain", []any{map[string]any{"type": "text", "text": "a"}, map[string]any{"type": "text", "text": "b"}}} { + out, err := decodeToolImages(Anthropic, value) + if err != nil || !reflect.DeepEqual(out, value) { + t.Fatal("legacy result changed") + } + } + for _, tail := range []any{nil, map[string]any{"type": "document"}, map[string]any{"type": "tool_result"}, map[string]any{"type": "text"}, map[string]any{"type": "image", "source": map[string]any{"type": "url", "url": "x", "data": "secret"}}} { + value := []any{map[string]any{"type": "image", "source": map[string]any{"type": "url", "url": "https://example.test/a"}}, tail} + if _, err := decodeToolImages(Anthropic, value); err == nil { + t.Fatalf("accepted malformed block: %#v", tail) + } + } + for _, image := range []map[string]any{{"type": "input_image"}, {"type": "input_image", "file_id": "secret"}, {"type": "input_image", "image_url": "data:bad"}} { + if _, err := decodeToolImages(Responses, []any{image}); err == nil { + t.Fatal("accepted unsupported image") + } + } + input := toolImageRequest(t) + messages := input["messages"].([]any) + input["messages"] = messages[:2] + if _, err := ConvertRequest(Anthropic, Chat, input); err == nil { + t.Fatal("accepted missing tool result") + } +} + +func TestToolImagesMultipleRounds(t *testing.T) { + input := toolImageRequest(t) + messages := input["messages"].([]any) + // Reusing call IDs in a later, completed round must not retain attachments. + input["messages"] = append(messages, messages...) + out, err := ConvertRequest(Anthropic, Chat, input) + if err != nil { + t.Fatal(err) + } + encoded, _ := json.Marshal(out) + if strings.Count(string(encoded), "aW1hZ2U=") != 2 || len(out["messages"].([]any)) != 12 { + t.Fatal("attachments leaked between rounds") + } + + input = toolImageRequest(t) + messages = input["messages"].([]any) + input["messages"] = append(messages[:2], messages[1], messages[2]) + if _, err := ConvertRequest(Anthropic, Chat, input); err == nil { + t.Fatal("duplicate result accepted") + } +}