|
| 1 | +package gateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/yetone/magpie/internal/provider" |
| 9 | +) |
| 10 | + |
| 11 | +// notice is the item Codex writes between one batch's tool results when the |
| 12 | +// image a tool returned was resized for the model. |
| 13 | +const resizeNotice = `<image_resize_notice> |
| 14 | +Image 1 of 1 in the preceding tool output was resized from 1024x9000 to 233x2048 pixels. |
| 15 | +</image_resize_notice>` |
| 16 | + |
| 17 | +// orderOf is a Responses request's input items as "type:call_id" strings. |
| 18 | +func orderOf(t *testing.T, body []byte) []string { |
| 19 | + t.Helper() |
| 20 | + var order []string |
| 21 | + for _, it := range inputOf(t, body) { |
| 22 | + typ, _ := it["type"].(string) |
| 23 | + id, _ := it["call_id"].(string) |
| 24 | + order = append(order, typ+":"+id) |
| 25 | + } |
| 26 | + return order |
| 27 | +} |
| 28 | + |
| 29 | +// A batch of two calls whose results have a message between them: the |
| 30 | +// message goes after both results, where the run a strict upstream checks |
| 31 | +// is whole. Fixtures are the reporter's own bytes — two parallel calls |
| 32 | +// (view_image and exec_command), an output holding an image, Codex's resize |
| 33 | +// notice, and the second output. |
| 34 | +func TestJoinToolRunsMovesAnItemOutOfARun(t *testing.T) { |
| 35 | + body := []byte(`{"model":"m","input":[` + |
| 36 | + `{"type":"message","role":"user","content":[{"type":"input_text","text":"look"}]},` + |
| 37 | + `{"type":"function_call","call_id":"call_00_hC0DonBjeKHawzABcyLL1642","name":"view_image","arguments":"{\"path\":\"wide.png\"}"},` + |
| 38 | + `{"type":"function_call","call_id":"call_01_mNySew0rARWuQYQXPeIY2319","name":"exec_command","arguments":"{\"cmd\":\"python -c ...\"}"},` + |
| 39 | + `{"type":"function_call_output","call_id":"call_00_hC0DonBjeKHawzABcyLL1642","output":[{"type":"input_image","image_url":"data:image/png;base64,AQID"}]},` + |
| 40 | + `{"type":"message","role":"developer","content":[{"type":"input_text","text":` + jsonStr(resizeNotice) + `}]},` + |
| 41 | + `{"type":"function_call_output","call_id":"call_01_mNySew0rARWuQYQXPeIY2319","output":"PIL 11.3.0\r\n"}]}`) |
| 42 | + original := bytes.Clone(body) |
| 43 | + got := joinToolRuns(body) |
| 44 | + if !bytes.Equal(body, original) { |
| 45 | + t.Fatal("mutated the caller's request buffer") |
| 46 | + } |
| 47 | + want := []string{ |
| 48 | + "message:", |
| 49 | + "function_call:call_00_hC0DonBjeKHawzABcyLL1642", |
| 50 | + "function_call:call_01_mNySew0rARWuQYQXPeIY2319", |
| 51 | + "function_call_output:call_00_hC0DonBjeKHawzABcyLL1642", |
| 52 | + "function_call_output:call_01_mNySew0rARWuQYQXPeIY2319", |
| 53 | + "message:", |
| 54 | + } |
| 55 | + order := orderOf(t, got) |
| 56 | + if len(order) != len(want) { |
| 57 | + t.Fatalf("items %v, want %v", order, want) |
| 58 | + } |
| 59 | + for i := range want { |
| 60 | + if order[i] != want[i] { |
| 61 | + t.Fatalf("items %v, want %v", order, want) |
| 62 | + } |
| 63 | + } |
| 64 | + // the notice keeps its text, and the results keep theirs |
| 65 | + if !bytes.Contains(got, []byte("image_resize_notice")) || |
| 66 | + !bytes.Contains(got, []byte("PIL 11.3.0")) || |
| 67 | + !bytes.Contains(got, []byte("data:image/png;base64,AQID")) { |
| 68 | + t.Fatalf("lost an item's content: %s", got) |
| 69 | + } |
| 70 | + // the same request again is left as it is |
| 71 | + if again := joinToolRuns(got); !bytes.Equal(again, got) { |
| 72 | + t.Fatalf("not idempotent:\n%s\n%s", got, again) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Only an item that breaks a run moves: one between two whole exchanges, a |
| 77 | +// request with no tool items, and anything that isn't a Responses body go |
| 78 | +// byte for byte. |
| 79 | +func TestJoinToolRunsLeavesWholeExchangesAlone(t *testing.T) { |
| 80 | + for _, keep := range []string{ |
| 81 | + // each exchange is whole: the call before the notice is answered, |
| 82 | + // and so is the one after it |
| 83 | + `{"model":"m","input":[` + |
| 84 | + `{"type":"function_call","call_id":"call_a","name":"echo","arguments":"{}"},` + |
| 85 | + `{"type":"function_call_output","call_id":"call_a","output":"a"},` + |
| 86 | + `{"type":"message","role":"developer","content":[{"type":"input_text","text":"a note"}]},` + |
| 87 | + `{"type":"function_call","call_id":"call_b","name":"echo","arguments":"{}"},` + |
| 88 | + `{"type":"function_call_output","call_id":"call_b","output":"b"}]}`, |
| 89 | + // a call nothing answers is not a run to keep together |
| 90 | + `{"model":"m","input":[` + |
| 91 | + `{"type":"function_call","call_id":"call_a","name":"echo","arguments":"{}"},` + |
| 92 | + `{"type":"message","role":"user","content":[{"type":"input_text","text":"never mind"}]}]}`, |
| 93 | + // parallel results already contiguous, and out of the calls' order |
| 94 | + `{"model":"m","input":[` + |
| 95 | + `{"type":"function_call","call_id":"call_a","name":"echo","arguments":"{}"},` + |
| 96 | + `{"type":"function_call","call_id":"call_b","name":"echo","arguments":"{}"},` + |
| 97 | + `{"type":"function_call_output","call_id":"call_b","output":"b"},` + |
| 98 | + `{"type":"function_call_output","call_id":"call_a","output":"a"}]}`, |
| 99 | + `{"model":"m","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}]}`, |
| 100 | + `not json`, `{}`, `{"input":"hello"}`, |
| 101 | + } { |
| 102 | + if got := joinToolRuns([]byte(keep)); string(got) != keep { |
| 103 | + t.Fatalf("rewritten:\n%s\nto\n%s", keep, got) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// The relay keeps the run whole for the upstream: a request whose batch has |
| 109 | +// an item between its results comes out with the results together, and an |
| 110 | +// upstream that checks the exchange answers it. |
| 111 | +func TestResponsesRelayKeepsAToolRunTogether(t *testing.T) { |
| 112 | + f := &fake{t: t, reply: sse(`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[]}}`)} |
| 113 | + f.refuse = func(body []byte) (int, string) { |
| 114 | + // The upstream's own check, on the chat form it builds: every result |
| 115 | + // of a run must follow the calls without an item between them. |
| 116 | + open := 0 |
| 117 | + var sent struct { |
| 118 | + Input []struct { |
| 119 | + Type string `json:"type"` |
| 120 | + CallID string `json:"call_id"` |
| 121 | + } `json:"input"` |
| 122 | + } |
| 123 | + if err := json.Unmarshal(body, &sent); err != nil { |
| 124 | + t.Fatal(err) |
| 125 | + } |
| 126 | + for _, it := range sent.Input { |
| 127 | + switch it.Type { |
| 128 | + case "function_call": |
| 129 | + open++ |
| 130 | + case "function_call_output": |
| 131 | + open-- |
| 132 | + default: |
| 133 | + if open > 0 { |
| 134 | + return 400, `{"error":{"message":"An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'."}}` |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return 0, "" |
| 139 | + } |
| 140 | + setup(t, provider.Responses, f) |
| 141 | + body := `{"model":"fake/m1","stream":true,"input":[` + |
| 142 | + `{"type":"message","role":"user","content":[{"type":"input_text","text":"look"}]},` + |
| 143 | + `{"type":"function_call","call_id":"call_a","name":"view_image","arguments":"{}"},` + |
| 144 | + `{"type":"function_call","call_id":"call_b","name":"exec_command","arguments":"{}"},` + |
| 145 | + `{"type":"function_call_output","call_id":"call_a","output":"looked"},` + |
| 146 | + `{"type":"message","role":"developer","content":[{"type":"input_text","text":` + jsonStr(resizeNotice) + `}]},` + |
| 147 | + `{"type":"function_call_output","call_id":"call_b","output":"ran"}]}` |
| 148 | + if code, out := post(t, "/v1/responses", body); code != 200 { |
| 149 | + t.Fatalf("status %d: %s", code, out) |
| 150 | + } |
| 151 | + want := []string{"message:", "function_call:call_a", "function_call:call_b", |
| 152 | + "function_call_output:call_a", "function_call_output:call_b", "message:"} |
| 153 | + order := orderOf(t, f.got) |
| 154 | + if len(order) != len(want) { |
| 155 | + t.Fatalf("sent %v, want %v", order, want) |
| 156 | + } |
| 157 | + for i := range want { |
| 158 | + if order[i] != want[i] { |
| 159 | + t.Fatalf("sent %v, want %v", order, want) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments