|
| 1 | +package completions_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/felinics/twilight/provider/openai/completions" |
| 11 | + "github.com/felinics/twilight/sdk" |
| 12 | +) |
| 13 | + |
| 14 | +// captureMessages serves one canned completion and records the messages the |
| 15 | +// provider sent, so tests can assert on reasoning_content key presence. |
| 16 | +func captureMessages(t *testing.T) (*httptest.Server, *[]map[string]json.RawMessage) { |
| 17 | + t.Helper() |
| 18 | + var messages []map[string]json.RawMessage |
| 19 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | + var body struct { |
| 21 | + Messages []map[string]json.RawMessage `json:"messages"` |
| 22 | + } |
| 23 | + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { |
| 24 | + t.Fatalf("decode request body: %v", err) |
| 25 | + } |
| 26 | + messages = body.Messages |
| 27 | + w.Header().Set("Content-Type", "application/json") |
| 28 | + json.NewEncoder(w).Encode(map[string]any{ |
| 29 | + "id": "chatcmpl-replay", "model": "m", |
| 30 | + "choices": []map[string]any{{ |
| 31 | + "index": 0, "finish_reason": "stop", |
| 32 | + "message": map[string]any{"role": "assistant", "content": "ok"}, |
| 33 | + }}, |
| 34 | + "usage": map[string]any{"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, |
| 35 | + }) |
| 36 | + })) |
| 37 | + t.Cleanup(srv.Close) |
| 38 | + return srv, &messages |
| 39 | +} |
| 40 | + |
| 41 | +func toolCallMessage(id string, reasoning ...sdk.ReasoningPart) sdk.Message { |
| 42 | + parts := make([]sdk.MessagePart, 0, len(reasoning)+1) |
| 43 | + for _, r := range reasoning { |
| 44 | + parts = append(parts, r) |
| 45 | + } |
| 46 | + parts = append(parts, sdk.ToolCallPart{ |
| 47 | + ToolCallID: id, |
| 48 | + ToolName: "get_weather", |
| 49 | + Input: map[string]any{"city": "Paris"}, |
| 50 | + }) |
| 51 | + return sdk.Message{Role: sdk.MessageRoleAssistant, Content: parts} |
| 52 | +} |
| 53 | + |
| 54 | +func weatherTool() sdk.Tool { |
| 55 | + return sdk.Tool{ |
| 56 | + Name: "get_weather", |
| 57 | + Description: "Get weather", |
| 58 | + Parameters: map[string]any{ |
| 59 | + "type": "object", |
| 60 | + "properties": map[string]any{"city": map[string]any{"type": "string"}}, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// assertReasoningContent checks messages[index] against want: a nil want |
| 66 | +// means the key must be absent, otherwise it must equal *want. |
| 67 | +func assertReasoningContent(t *testing.T, msgs []map[string]json.RawMessage, index int, want *string) { |
| 68 | + t.Helper() |
| 69 | + raw, ok := msgs[index]["reasoning_content"] |
| 70 | + if !ok { |
| 71 | + if want != nil { |
| 72 | + t.Fatalf("messages[%d]: reasoning_content key missing, want %q", index, *want) |
| 73 | + } |
| 74 | + return |
| 75 | + } |
| 76 | + var got string |
| 77 | + if err := json.Unmarshal(raw, &got); err != nil { |
| 78 | + t.Fatalf("messages[%d]: reasoning_content is not a string: %s", index, raw) |
| 79 | + } |
| 80 | + if want == nil { |
| 81 | + t.Fatalf("messages[%d]: reasoning_content should be absent, got %q", index, got) |
| 82 | + } |
| 83 | + if got != *want { |
| 84 | + t.Fatalf("messages[%d]: reasoning_content = %q, want %q", index, got, *want) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func str(s string) *string { return &s } |
| 89 | + |
| 90 | +// TestDoGenerate_ThinkingReplayPadding drives padThinkingReplay through one |
| 91 | +// history under each provider configuration: a user turn, a tool call whose |
| 92 | +// reasoning varies per case, its result, a second tool call with no |
| 93 | +// reasoning, its result, a text answer and a follow-up user turn. Only |
| 94 | +// messages 1 and 3 are assistant tool calls; everything else must stay bare. |
| 95 | +func TestDoGenerate_ThinkingReplayPadding(t *testing.T) { |
| 96 | + empty := str("") |
| 97 | + cases := []struct { |
| 98 | + name string |
| 99 | + options []completions.Option |
| 100 | + step1 []sdk.ReasoningPart // reasoning on the first tool call |
| 101 | + wantStep1 *string // nil: key absent |
| 102 | + wantStep2 *string |
| 103 | + }{ |
| 104 | + { |
| 105 | + name: "deepseek compat pads bare tool calls", |
| 106 | + options: []completions.Option{completions.WithDeepSeekChatCompletionsCompat()}, |
| 107 | + wantStep1: empty, wantStep2: empty, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "plain endpoint pads once the conversation carries reasoning_content", |
| 111 | + step1: []sdk.ReasoningPart{{Text: "Paris first.", Format: sdk.ReasoningFormatOpenAIChat}}, |
| 112 | + wantStep1: str("Paris first."), wantStep2: empty, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "plain endpoint sends nothing without thinking evidence", |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "minimax compat never pads", |
| 119 | + options: []completions.Option{completions.WithMiniMaxChatCompletionsCompat()}, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tc := range cases { |
| 124 | + t.Run(tc.name, func(t *testing.T) { |
| 125 | + srv, sent := captureMessages(t) |
| 126 | + opts := append([]completions.Option{completions.WithAPIKey("k"), completions.WithBaseURL(srv.URL)}, tc.options...) |
| 127 | + p := completions.New(opts...) |
| 128 | + |
| 129 | + history := []sdk.Message{ |
| 130 | + sdk.UserMessage("Weather in Paris, then Tokyo."), |
| 131 | + toolCallMessage("c1", tc.step1...), |
| 132 | + sdk.ToolMessage(sdk.ToolResultPart{ToolCallID: "c1", ToolName: "get_weather", Result: "18C"}), |
| 133 | + toolCallMessage("c2"), |
| 134 | + sdk.ToolMessage(sdk.ToolResultPart{ToolCallID: "c2", ToolName: "get_weather", Result: "25C"}), |
| 135 | + sdk.AssistantMessage("Paris 18C, Tokyo 25C."), |
| 136 | + sdk.UserMessage("Thanks."), |
| 137 | + } |
| 138 | + _, err := p.DoGenerate(context.Background(), sdk.GenerateParams{ |
| 139 | + Model: &sdk.Model{ID: "m"}, |
| 140 | + Messages: history, |
| 141 | + Tools: []sdk.Tool{weatherTool()}, |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("DoGenerate: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + msgs := *sent |
| 148 | + if len(msgs) != len(history) { |
| 149 | + t.Fatalf("expected %d messages, got %d", len(history), len(msgs)) |
| 150 | + } |
| 151 | + for i := range msgs { |
| 152 | + var want *string |
| 153 | + switch i { |
| 154 | + case 1: |
| 155 | + want = tc.wantStep1 |
| 156 | + case 3: |
| 157 | + want = tc.wantStep2 |
| 158 | + } |
| 159 | + assertReasoningContent(t, msgs, i, want) |
| 160 | + } |
| 161 | + }) |
| 162 | + } |
| 163 | +} |
0 commit com 32FA ments