| 1 | package anthropic |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "reasonix/internal/provider" |
| 12 | ) |
| 13 | |
| 14 | // TestBuildRequestWebSearchServerTool covers the tools-array shape when the |
| 15 | // server-side web_search tool is enabled: it is prepended as a typed entry |
| 16 | // without an input_schema, and named tools keep their schema untouched. |
| 17 | func TestBuildRequestWebSearchServerTool(t *testing.T) { |
| 18 | c := &client{name: "deepseek", model: "deepseek-v4-flash", webSearch: true} |
| 19 | r := c.buildRequest(context.Background(), provider.Request{ |
| 20 | Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}, |
| 21 | Tools: []provider.ToolSchema{{Name: "read_file", Parameters: json.RawMessage(`{"type":"object"}`)}}, |
| 22 | }) |
| 23 | if len(r.Tools) != 2 { |
| 24 | t.Fatalf("want 2 tools (web_search + read_file), got %d: %+v", len(r.Tools), r.Tools) |
| 25 | } |
| 26 | if r.Tools[0].Type != "web_search_20250305" || r.Tools[0].Name != "web_search" { |
| 27 | t.Fatalf("tools[0] = %+v, want typed web_search server tool", r.Tools[0]) |
| 28 | } |
| 29 | if len(r.Tools[0].InputSchema) != 0 { |
| 30 | t.Fatalf("server tool must not carry input_schema, got %s", r.Tools[0].InputSchema) |
| 31 | } |
| 32 | if r.Tools[1].Type != "" || r.Tools[1].Name != "read_file" || len(r.Tools[1].InputSchema) == 0 { |
| 33 | t.Fatalf("tools[1] = %+v, want named tool with schema and no type", r.Tools[1]) |
| 34 | } |
| 35 | |
| 36 | // Disabled (default) ⇒ no server tool is injected. |
| 37 | off := &client{name: "deepseek", model: "deepseek-v4-flash"} |
| 38 | r = off.buildRequest(context.Background(), provider.Request{ |
| 39 | Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}, |
| 40 | Tools: []provider.ToolSchema{{Name: "read_file", Parameters: json.RawMessage(`{"type":"object"}`)}}, |
| 41 | }) |
| 42 | if len(r.Tools) != 1 || r.Tools[0].Name != "read_file" { |
| 43 | t.Fatalf("webSearch off: tools = %+v, want only read_file", r.Tools) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // TestAnthToolWireShape pins the JSON encoding both tool kinds put on the wire: |
| 48 | // the typed server tool omits input_schema entirely, and the omitempty on |
| 49 | // input_schema must not leak into named tools (every named tool keeps a schema |
| 50 | // because buildRequest substitutes a default for empty parameters). |
| 51 | func TestAnthToolWireShape(t *testing.T) { |
| 52 | server, err := json.Marshal(anthTool{Type: "web_search_20250305", Name: "web_search"}) |
| 53 | if err != nil { |
| 54 | t.Fatalf("marshal server tool: %v", err) |
| 55 | } |
| 56 | if got := string(server); got != `{"type":"web_search_20250305","name":"web_search"}` { |
| 57 | t.Fatalf("server tool wire = %s", got) |
| 58 | } |
| 59 | |
| 60 | named, err := json.Marshal(anthTool{Name: "read_file", InputSchema: json.RawMessage(`{"type":"object"}`)}) |
| 61 | if err != nil { |
| 62 | t.Fatalf("marshal named tool: %v", err) |
| 63 | } |
| 64 | if got := string(named); got != `{"name":"read_file","input_schema":{"type":"object"}}` { |
| 65 | t.Fatalf("named tool wire = %s", got) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestFormatWebSearchResults(t *testing.T) { |
| 70 | cases := []struct { |
| 71 | name string |
| 72 | raw string |
| 73 | want string |
| 74 | }{ |
| 75 | {"empty payload", "", ""}, |
| 76 | {"malformed json", `{"not":"an array"`, ""}, |
| 77 | {"non-array json", `{"title":"x"}`, ""}, |
| 78 | {"empty array", `[]`, ""}, |
| 79 | {"all entries blank", `[{"text":"body only"},{}]`, ""}, |
| 80 | { |
| 81 | // DeepSeek returns encrypted_content alongside title/url; unknown |
| 82 | // fields must be ignored rather than failing the whole block. |
| 83 | "titles and urls", |
| 84 | `[{"type":"web_search_result","title":"Change Log","url":"https://api-docs.deepseek.com/updates/","encrypted_content":"xxx"},{"title":"No URL"}]`, |
| 85 | "\n\n- **Change Log**\n <https://api-docs.deepseek.com/updates/>\n- **No URL**\n", |
| 86 | }, |
| 87 | } |
| 88 | for _, tc := range cases { |
| 89 | t.Run(tc.name, func(t *testing.T) { |
| 90 | if got := formatWebSearchResults(json.RawMessage(tc.raw)); got != tc.want { |
| 91 | t.Fatalf("formatWebSearchResults(%s) = %q, want %q", tc.raw, got, tc.want) |
| 92 | } |
| 93 | }) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // TestStreamSurfacesWebSearchResults drives a full SSE round-trip: a |
| 98 | // web_search_tool_result block must surface as readable text chunks, and the |
| 99 | // server_tool_use block (the model initiating the search) must not be mistaken |
| 100 | // for a client tool call. |
| 101 | func TestStreamSurfacesWebSearchResults(t *testing.T) { |
| 102 | sse := strings.Join([]string{ |
| 103 | `data: {"type":"message_start","message":{"usage":{"input_tokens":10}}}`, |
| 104 | ``, |
| 105 | `data: {"type":"content_block_start","index":0,"content_block":{"type":"server_tool_use","id":"s1","name":"web_search"}}`, |
| 106 | ``, |
| 107 | `data: {"type":"content_block_start","index":1,"content_block":{"type":"web_search_tool_result","tool_use_id":"s1","content":[{"title":"Change Log","url":"https://api-docs.deepseek.com/updates/"}]}}`, |
| 108 | ``, |
| 109 | `data: {"type":"content_block_start","index":2,"content_block":{"type":"text"}}`, |
| 110 | ``, |
| 111 | `data: {"type":"content_block_delta","index":2,"delta":{"type":"text_delta","text":"answer"}}`, |
| 112 | ``, |
| 113 | `data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5}}`, |
| 114 | ``, |
| 115 | `data: {"type":"message_stop"}`, |
| 116 | ``, |
| 117 | }, "\n") |
| 118 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 119 | w.Header().Set("Content-Type", "text/event-stream") |
| 120 | _, _ = w.Write([]byte(sse)) |
| 121 | })) |
| 122 | defer srv.Close() |
| 123 | |
| 124 | p, err := New(provider.Config{Name: "deepseek", BaseURL: srv.URL, Model: "deepseek-v4-flash", APIKey: "k", Extra: map[string]any{"web_search": true}}) |
| 125 | if err != nil { |
| 126 | t.Fatalf("New: %v", err) |
| 127 | } |
| 128 | ch, err := p.Stream(context.Background(), provider.Request{ |
| 129 | Messages: []provider.Message{{Role: provider.RoleUser, Content: "search something"}}, |
| 130 | }) |
| 131 | if err != nil { |
| 132 | t.Fatalf("Stream: %v", err) |
| 133 | } |
| 134 | |
| 135 | var text strings.Builder |
| 136 | for chunk := range ch { |
| 137 | switch chunk.Type { |
| 138 | case provider.ChunkText: |
| 139 | text.WriteString(chunk.Text) |
| 140 | case provider.ChunkToolCallStart, provider.ChunkToolCall: |
| 141 | t.Fatalf("server-side search must not surface as a client tool call, got %+v", chunk) |
| 142 | case provider.ChunkError: |
| 143 | t.Fatalf("stream error: %v", chunk.Err) |
| 144 | } |
| 145 | } |
| 146 | got := text.String() |
| 147 | if !strings.Contains(got, "**Change Log**") || !strings.Contains(got, "<https://api-docs.deepseek.com/updates/>") { |
| 148 | t.Fatalf("stream text missing formatted search results: %q", got) |
| 149 | } |
| 150 | if !strings.Contains(got, "answer") { |
| 151 | t.Fatalf("stream text missing model answer: %q", got) |
| 152 | } |
| 153 | } |
| 154 |