-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathcodex_openai_images_test.go
More file actions
349 lines (328 loc) · 13.6 KB
/
Copy pathcodex_openai_images_test.go
File metadata and controls
349 lines (328 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package executor
import (
"bytes"
"context"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator"
"github.com/tidwall/gjson"
)
func newCodexOpenAIImageTestAuth(serverURL string) *cliproxyauth.Auth {
return &cliproxyauth.Auth{
Provider: "codex",
Attributes: map[string]string{
"base_url": serverURL,
"api_key": "codex-token",
},
}
}
func codexOpenAIImageTestOptions(path string, stream bool) cliproxyexecutor.Options {
return cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString(codexOpenAIImageSourceFormat),
Stream: stream,
Metadata: map[string]any{
cliproxyexecutor.RequestPathMetadataKey: path,
},
}
}
func TestCodexExecutorDirectOpenAIImageGenerationUsesImagesEndpoint(t *testing.T) {
var gotPath string
var gotAuth string
var gotAccept string
var gotUA string
var gotVersion string
var gotTurnMetadata string
var gotClientRequestID string
var gotOriginator string
var gotBody []byte
upstreamBody := []byte(`{"created":1713833628,"data":[{"b64_json":"AA=="}],"usage":{"total_tokens":100,"input_tokens":50,"output_tokens":50}}`)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotAuth = r.Header.Get("Authorization")
gotAccept = r.Header.Get("Accept")
gotUA = r.Header.Get("User-Agent")
gotVersion = r.Header.Get("Version")
gotTurnMetadata = r.Header.Get("X-Codex-Turn-Metadata")
gotClientRequestID = r.Header.Get("X-Client-Request-Id")
gotOriginator = r.Header.Get("Originator")
var errRead error
gotBody, errRead = io.ReadAll(r.Body)
if errRead != nil {
t.Fatalf("read body: %v", errRead)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(upstreamBody)
}))
defer server.Close()
ctx := contextWithGinHeaders(map[string]string{
"User-Agent": "downstream-client/9.9",
"Version": "0.135.0",
"X-Codex-Turn-Metadata": `{"turn_id":"turn-1"}`,
"X-Client-Request-Id": "client-request-1",
"Originator": "Codex Desktop",
})
executor := NewCodexExecutor(&config.Config{})
resp, errExecute := executor.Execute(ctx, newCodexOpenAIImageTestAuth(server.URL), cliproxyexecutor.Request{
Model: "codex/gpt-image-1.5",
Payload: []byte(`{"model":"codex/gpt-image-1.5","prompt":"A cute baby sea otter","n":1,"size":"1024x1024","quality":"high","background":"opaque","output_format":"jpeg","output_compression":70,"moderation":"low","extra":{"preserve":true},"stream":false}`),
}, codexOpenAIImageTestOptions(codexImagesGenerationsPath, false))
if errExecute != nil {
t.Fatalf("Execute() error = %v", errExecute)
}
if gotPath != "/images/generations" {
t.Fatalf("path = %q, want /images/generations", gotPath)
}
if gotAuth != "Bearer codex-token" {
t.Fatalf("Authorization = %q, want Bearer codex-token", gotAuth)
}
if gotAccept != "application/json" {
t.Fatalf("Accept = %q, want application/json", gotAccept)
}
if gotUA != codexUserAgent {
t.Fatalf("User-Agent = %q, want codex default %q", gotUA, codexUserAgent)
}
if gotVersion != "0.135.0" {
t.Fatalf("Version = %q, want %q", gotVersion, "0.135.0")
}
if gotTurnMetadata != `{"turn_id":"turn-1"}` {
t.Fatalf("X-Codex-Turn-Metadata = %q, want %q", gotTurnMetadata, `{"turn_id":"turn-1"}`)
}
if gotClientRequestID != "client-request-1" {
t.Fatalf("X-Client-Request-Id = %q, want %q", gotClientRequestID, "client-request-1")
}
if gotOriginator != "Codex Desktop" {
t.Fatalf("Originator = %q, want %q", gotOriginator, "Codex Desktop")
}
if got := gjson.GetBytes(gotBody, "model").String(); got != "gpt-image-1.5" {
t.Fatalf("model = %q, want gpt-image-1.5; body=%s", got, string(gotBody))
}
if got := gjson.GetBytes(gotBody, "extra.preserve").Bool(); !got {
t.Fatalf("extra.preserve missing from body: %s", string(gotBody))
}
if got := gjson.GetBytes(gotBody, "output_compression").Int(); got != 70 {
t.Fatalf("output_compression = %d, want 70; body=%s", got, string(gotBody))
}
if gjson.GetBytes(gotBody, "stream").Exists() {
t.Fatalf("stream should be removed for non-stream execution: %s", string(gotBody))
}
if !bytes.Equal(resp.Payload, upstreamBody) {
t.Fatalf("payload = %s, want %s", string(resp.Payload), string(upstreamBody))
}
}
func TestCodexExecutorDirectOpenAIImageGenerationStreamsImagesEndpoint(t *testing.T) {
var gotPath string
var gotAccept string
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotAccept = r.Header.Get("Accept")
var errRead error
gotBody, errRead = io.ReadAll(r.Body)
if errRead != nil {
t.Fatalf("read body: %v", errRead)
}
w.Header().Set("Content-Type", "text/event-stream")
_, _ = w.Write([]byte("event: image_generation.partial_image\ndata: {\"type\":\"image_generation.partial_image\",\"b64_json\":\"AA==\",\"partial_image_index\":0}\n\n"))
_, _ = w.Write([]byte("event: image_generation.completed\ndata: {\"type\":\"image_generation.completed\",\"b64_json\":\"BB==\",\"usage\":{\"total_tokens\":10,\"input_tokens\":4,\"output_tokens\":6}}\n\n"))
}))
defer server.Close()
executor := NewCodexExecutor(&config.Config{})
stream, errStream := executor.ExecuteStream(context.Background(), newCodexOpenAIImageTestAuth(server.URL), cliproxyexecutor.Request{
Model: "gpt-image-2",
Payload: []byte(`{"model":"gpt-image-2","prompt":"A cute baby sea otter","partial_images":2}`),
}, codexOpenAIImageTestOptions(codexImagesGenerationsPath, true))
if errStream != nil {
t.Fatalf("ExecuteStream() error = %v", errStream)
}
var combined bytes.Buffer
for chunk := range stream.Chunks {
if chunk.Err != nil {
t.Fatalf("stream chunk error = %v", chunk.Err)
}
combined.Write(chunk.Payload)
}
if gotPath != "/images/generations" {
t.Fatalf("path = %q, want /images/generations", gotPath)
}
if gotAccept != "text/event-stream" {
t.Fatalf("Accept = %q, want text/event-stream", gotAccept)
}
if !gjson.GetBytes(gotBody, "stream").Bool() {
t.Fatalf("stream flag missing from upstream body: %s", string(gotBody))
}
if got := gjson.GetBytes(gotBody, "partial_images").Int(); got != 2 {
t.Fatalf("partial_images = %d, want 2; body=%s", got, string(gotBody))
}
out := combined.String()
if !strings.Contains(out, "event: image_generation.partial_image") || !strings.Contains(out, "event: image_generation.completed") {
t.Fatalf("stream output missing image events: %q", out)
}
}
func TestCodexExecutorDirectOpenAIImageEditUsesImagesEditEndpointForJSON(t *testing.T) {
var gotPath string
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
var errRead error
gotBody, errRead = io.ReadAll(r.Body)
if errRead != nil {
t.Fatalf("read body: %v", errRead)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"created":1713833628,"data":[{"b64_json":"AA=="}],"usage":{"total_tokens":10}}`))
}))
defer server.Close()
executor := NewCodexExecutor(&config.Config{})
_, errExecute := executor.Execute(context.Background(), newCodexOpenAIImageTestAuth(server.URL), cliproxyexecutor.Request{
Model: "gpt-image-2",
Payload: []byte(`{"model":"gpt-image-2","prompt":"Replace the background","images":[{"file_id":"file-abc123"}],"mask":{"file_id":"file-mask123"},"size":"1024x1024","quality":"high","output_format":"png","output_compression":100,"stream":false}`),
}, codexOpenAIImageTestOptions(codexImagesEditsPath, false))
if errExecute != nil {
t.Fatalf("Execute() error = %v", errExecute)
}
if gotPath != "/images/edits" {
t.Fatalf("path = %q, want /images/edits", gotPath)
}
if got := gjson.GetBytes(gotBody, "model").String(); got != "gpt-image-2" {
t.Fatalf("model = %q, want gpt-image-2; body=%s", got, string(gotBody))
}
if got := gjson.GetBytes(gotBody, "images.0.file_id").String(); got != "file-abc123" {
t.Fatalf("images.0.file_id = %q, want file-abc123; body=%s", got, string(gotBody))
}
if got := gjson.GetBytes(gotBody, "mask.file_id").String(); got != "file-mask123" {
t.Fatalf("mask.file_id = %q, want file-mask123; body=%s", got, string(gotBody))
}
if gjson.GetBytes(gotBody, "stream").Exists() {
t.Fatalf("stream should be removed for non-stream execution: %s", string(gotBody))
}
}
func TestCodexExecutorDirectOpenAIImageEditUsesImagesEditEndpointForMultipart(t *testing.T) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
if errWrite := writer.WriteField("model", "codex/gpt-image-1.5"); errWrite != nil {
t.Fatalf("write model field: %v", errWrite)
}
if errWrite := writer.WriteField("prompt", "Create a lovely gift basket"); errWrite != nil {
t.Fatalf("write prompt field: %v", errWrite)
}
if errWrite := writer.WriteField("output_format", "webp"); errWrite != nil {
t.Fatalf("write output_format field: %v", errWrite)
}
if errWrite := writer.WriteField("n", "2"); errWrite != nil {
t.Fatalf("write n field: %v", errWrite)
}
if errWrite := writer.WriteField("stream", "false"); errWrite != nil {
t.Fatalf("write stream field: %v", errWrite)
}
imagePart, errCreate := writer.CreateFormFile("image[]", "source.png")
if errCreate != nil {
t.Fatalf("create image field: %v", errCreate)
}
if _, errWrite := imagePart.Write([]byte("png-data")); errWrite != nil {
t.Fatalf("write image data: %v", errWrite)
}
maskPart, errCreateMask := writer.CreateFormFile("mask", "mask.png")
if errCreateMask != nil {
t.Fatalf("create mask field: %v", errCreateMask)
}
if _, errWrite := maskPart.Write([]byte("mask-data")); errWrite != nil {
t.Fatalf("write mask data: %v", errWrite)
}
if errClose := writer.Close(); errClose != nil {
t.Fatalf("close multipart writer: %v", errClose)
}
var gotPath string
var gotContentType string
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotContentType = r.Header.Get("Content-Type")
var errRead error
gotBody, errRead = io.ReadAll(r.Body)
if errRead != nil {
t.Fatalf("read body: %v", errRead)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"created":1713833628,"data":[{"b64_json":"AA=="}]}`))
}))
defer server.Close()
opts := codexOpenAIImageTestOptions(codexImagesEditsPath, false)
opts.Headers = http.Header{"Content-Type": []string{writer.FormDataContentType()}}
executor := NewCodexExecutor(&config.Config{})
_, errExecute := executor.Execute(context.Background(), newCodexOpenAIImageTestAuth(server.URL), cliproxyexecutor.Request{
Model: "codex/gpt-image-1.5",
Payload: body.Bytes(),
}, opts)
if errExecute != nil {
t.Fatalf("Execute() error = %v", errExecute)
}
if gotPath != "/images/edits" {
t.Fatalf("path = %q, want /images/edits", gotPath)
}
if !strings.HasPrefix(gotContentType, "application/json") {
t.Fatalf("Content-Type = %q, want application/json", gotContentType)
}
if !json.Valid(gotBody) {
t.Fatalf("body is not valid JSON: %s", string(gotBody))
}
if got := gjson.GetBytes(gotBody, "model").String(); got != "gpt-image-1.5" {
t.Fatalf("model = %q, want gpt-image-1.5; body=%s", got, string(gotBody))
}
if got := gjson.GetBytes(gotBody, "prompt").String(); got != "Create a lovely gift basket" {
t.Fatalf("prompt = %q", got)
}
if got := gjson.GetBytes(gotBody, "output_format").String(); got != "webp" {
t.Fatalf("output_format = %q, want webp; body=%s", got, string(gotBody))
}
if got := gjson.GetBytes(gotBody, "n").Int(); got != 2 {
t.Fatalf("n = %d, want 2; body=%s", got, string(gotBody))
}
if gjson.GetBytes(gotBody, "stream").Exists() {
t.Fatalf("stream should be removed for non-stream execution: %s", string(gotBody))
}
imageURL := gjson.GetBytes(gotBody, "images.0.image_url").String()
if !strings.Contains(imageURL, ";base64,cG5nLWRhdGE=") {
t.Fatalf("images.0.image_url = %q, want png-data data URL; body=%s", imageURL, string(gotBody))
}
maskURL := gjson.GetBytes(gotBody, "mask.image_url").String()
if !strings.Contains(maskURL, ";base64,bWFzay1kYXRh") {
t.Fatalf("mask.image_url = %q, want mask-data data URL; body=%s", maskURL, string(gotBody))
}
}
func TestCodexExecutorWrappedOpenAIImageDoesNotForwardDownstreamUA(t *testing.T) {
var gotUA string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotUA = r.Header.Get("User-Agent")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"id":"resp_1","output":[{"type":"image_generation_call","result":"AA=="}],"usage":{"input_tokens":1,"output_tokens":1}}`))
}))
defer server.Close()
ctx := contextWithGinHeaders(map[string]string{
"User-Agent": "Python-urllib/3.9",
})
executor := NewCodexExecutor(&config.Config{})
// Use a model name that is NOT gpt-image-1.5 / gpt-image-2 so the request
// takes the wrapped (/responses) path instead of the direct (/images/*) path.
_, _ = executor.Execute(ctx, newCodexOpenAIImageTestAuth(server.URL), cliproxyexecutor.Request{
Model: "codex/dall-e-3",
Payload: []byte(`{"model":"codex/dall-e-3","prompt":"A cute otter","stream":false}`),
}, codexOpenAIImageTestOptions(codexImagesGenerationsPath, false))
if gotUA == "" {
t.Fatal("upstream request was never made (User-Agent not captured)")
}
if gotUA == "Python-urllib/3.9" {
t.Fatalf("downstream User-Agent leaked to upstream: %q", gotUA)
}
if gotUA != codexUserAgent {
t.Fatalf("User-Agent = %q, want codex default %q", gotUA, codexUserAgent)
}
}