File size: 11,473 Bytes
7cd5cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package openai

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	"github.com/QuantumNous/new-api/common"
	"github.com/QuantumNous/new-api/logger"
	relaycommon "github.com/QuantumNous/new-api/relay/common"
	"github.com/QuantumNous/new-api/service"

	"github.com/gin-gonic/gin"
)

const (
	maxImageTaskResponseBytes    int64 = 64 * 1024 * 1024
	defaultImageTaskPollTimeout        = 120 * time.Second
	defaultImageTaskPollDelay          = 2 * time.Second
	minImageTaskPollDelay              = 250 * time.Millisecond
	maxImageTaskPollDelay              = 5 * time.Second
	maxImageTaskPollErrors             = 3
)

type openAIImageTaskEnvelope struct {
	Object      string `json:"object"`
	Status      string `json:"status"`
	TaskID      string `json:"task_id"`
	ID          string `json:"id"`
	PollURL     string `json:"poll_url"`
	ResultURL   string `json:"result_url"`
	PollAfterMS int64  `json:"poll_after_ms"`
	Error       string `json:"error"`
	ErrorCode   string `json:"error_code"`
}

type imageTaskPollStats struct {
	TaskID   string
	Attempts int
	Duration time.Duration
}

type imageTaskWaitFunc func(context.Context, time.Duration) error

func resolveOpenAIImageTask(c *gin.Context, info *relaycommon.RelayInfo, response *http.Response) (*http.Response, error) {
	if !common.GetEnvOrDefaultBool("CODEX_IMAGE_TASK_POLL", false) || response == nil || response.StatusCode != http.StatusAccepted {
		return response, nil
	}
	if info == nil {
		return nil, fmt.Errorf("image task relay info is missing")
	}

	client, err := service.GetHttpClientWithProxy(info.ChannelSetting.Proxy)
	if err != nil {
		return nil, fmt.Errorf("create image task poll client: %w", err)
	}

	timeoutSeconds := common.GetEnvOrDefault("CODEX_IMAGE_TASK_POLL_TIMEOUT_SECONDS", int(defaultImageTaskPollTimeout/time.Second))
	if timeoutSeconds < 1 {
		timeoutSeconds = int(defaultImageTaskPollTimeout / time.Second)
	}
	if timeoutSeconds > 600 {
		timeoutSeconds = 600
	}

	startedAt := time.Now()
	resolved, stats, err := pollOpenAIImageTask(
		c.Request.Context(),
		response,
		client,
		time.Duration(timeoutSeconds)*time.Second,
		waitForOpenAIImageTask,
	)
	stats.Duration = time.Since(startedAt)
	if err != nil {
		return nil, err
	}
	if stats.Attempts > 0 {
		logger.LogInfo(c, fmt.Sprintf(
			"image task compatibility completed task_id=%s attempts=%d total_ms=%.3f",
			maskImageTaskID(stats.TaskID),
			stats.Attempts,
			float64(stats.Duration.Microseconds())/1000,
		))
		resolved.Header.Set("X-New-API-Image-Task-Compat", "polled")
	}
	return resolved, nil
}

func pollOpenAIImageTask(
	ctx context.Context,
	initial *http.Response,
	client *http.Client,
	timeout time.Duration,
	wait imageTaskWaitFunc,
) (*http.Response, imageTaskPollStats, error) {
	var stats imageTaskPollStats
	if initial == nil || initial.Body == nil {
		return nil, stats, fmt.Errorf("image task response is empty")
	}
	if initial.StatusCode != http.StatusAccepted {
		return initial, stats, nil
	}

	initialBody, err := readImageTaskResponseBody(initial)
	if err != nil {
		return nil, stats, err
	}
	initial.Body = io.NopCloser(bytes.NewReader(initialBody))
	initial.ContentLength = int64(len(initialBody))

	task, isTask, err := parseOpenAIImageTask(initialBody)
	if err != nil {
		return nil, stats, err
	}
	if !isTask {
		return initial, stats, nil
	}

	stats.TaskID = firstNonEmpty(task.TaskID, task.ID)
	baseURL, err := imageTaskBaseURL(initial)
	if err != nil {
		return nil, stats, err
	}
	pollClient := *client
	pollClient.CheckRedirect = func(request *http.Request, via []*http.Request) error {
		if len(via) >= 10 {
			return fmt.Errorf("image task polling stopped after 10 redirects")
		}
		if request == nil || request.URL == nil ||
			!strings.EqualFold(baseURL.Scheme, request.URL.Scheme) ||
			!strings.EqualFold(baseURL.Host, request.URL.Host) {
			return fmt.Errorf("image task redirect must use the original upstream origin")
		}
		return nil
	}
	client = &pollClient
	pollURL, err := resolveImageTaskPollURL(baseURL, firstNonEmpty(task.PollURL, task.ResultURL))
	if err != nil {
		return nil, stats, err
	}

	deadline := time.Now().Add(timeout)
	consecutiveErrors := 0
	for {
		if remaining := time.Until(deadline); remaining <= 0 {
			return nil, stats, fmt.Errorf("image task polling timed out after %s (task_id=%s)", timeout, maskImageTaskID(stats.TaskID))
		}

		delay := imageTaskPollDelay(task.PollAfterMS)
		if remaining := time.Until(deadline); delay > remaining {
			delay = remaining
		}
		if err := wait(ctx, delay); err != nil {
			return nil, stats, fmt.Errorf("image task polling stopped: %w", err)
		}

		request, err := http.NewRequestWithContext(ctx, http.MethodGet, pollURL.String(), nil)
		if err != nil {
			return nil, stats, fmt.Errorf("create image task poll request: %w", err)
		}
		copyImageTaskAuthHeaders(request.Header, initial.Request)
		request.Header.Set("Accept", "application/json")

		pollResponse, err := client.Do(request)
		stats.Attempts++
		if err != nil {
			consecutiveErrors++
			if consecutiveErrors >= maxImageTaskPollErrors {
				return nil, stats, fmt.Errorf("poll image task after %d consecutive errors: %w", consecutiveErrors, err)
			}
			continue
		}

		pollBody, readErr := readImageTaskResponseBody(pollResponse)
		if readErr != nil {
			return nil, stats, readErr
		}
		if pollResponse.StatusCode >= http.StatusInternalServerError {
			consecutiveErrors++
			if consecutiveErrors >= maxImageTaskPollErrors {
				return nil, stats, fmt.Errorf("poll image task returned HTTP %d after %d attempts", pollResponse.StatusCode, consecutiveErrors)
			}
			continue
		}
		consecutiveErrors = 0
		if pollResponse.StatusCode < http.StatusOK || pollResponse.StatusCode >= http.StatusMultipleChoices {
			return nil, stats, fmt.Errorf("poll image task returned HTTP %d", pollResponse.StatusCode)
		}

		if isFinalOpenAIImageResponse(pollBody) {
			pollResponse.StatusCode = http.StatusOK
			pollResponse.Status = fmt.Sprintf("%d %s", http.StatusOK, http.StatusText(http.StatusOK))
			pollResponse.Body = io.NopCloser(bytes.NewReader(pollBody))
			pollResponse.ContentLength = int64(len(pollBody))
			pollResponse.Header.Del("Content-Encoding")
			pollResponse.Header.Set("Content-Length", strconv.FormatInt(int64(len(pollBody)), 10))
			return pollResponse, stats, nil
		}

		nextTask, nextIsTask, err := parseOpenAIImageTask(pollBody)
		if err != nil {
			return nil, stats, err
		}
		if !nextIsTask {
			return nil, stats, fmt.Errorf("image task poll response is neither a task nor a final image response")
		}
		task = nextTask
		if nextID := firstNonEmpty(task.TaskID, task.ID); nextID != "" {
			stats.TaskID = nextID
		}

		switch strings.ToLower(strings.TrimSpace(task.Status)) {
		case "failed", "cancelled", "canceled", "expired":
			message := firstNonEmpty(task.Error, task.ErrorCode, task.Status)
			return nil, stats, fmt.Errorf("image task %s: %s", maskImageTaskID(stats.TaskID), message)
		case "completed", "succeeded", "success":
			return nil, stats, fmt.Errorf("image task %s completed without image data", maskImageTaskID(stats.TaskID))
		}

		if nextRawURL := firstNonEmpty(task.PollURL, task.ResultURL); nextRawURL != "" {
			pollURL, err = resolveImageTaskPollURL(baseURL, nextRawURL)
			if err != nil {
				return nil, stats, err
			}
		}
	}
}

func parseOpenAIImageTask(body []byte) (openAIImageTaskEnvelope, bool, error) {
	var task openAIImageTaskEnvelope
	if err := common.Unmarshal(body, &task); err != nil {
		return task, false, fmt.Errorf("decode image task response: %w", err)
	}
	if !strings.EqualFold(strings.TrimSpace(task.Object), "image.task") {
		return task, false, nil
	}
	if firstNonEmpty(task.PollURL, task.ResultURL) == "" {
		return task, false, fmt.Errorf("image task response is missing poll_url")
	}
	return task, true, nil
}

func isFinalOpenAIImageResponse(body []byte) bool {
	var payload map[string]json.RawMessage
	if err := common.Unmarshal(body, &payload); err != nil {
		return false
	}
	if _, ok := payload["created"]; !ok {
		return false
	}
	var images []json.RawMessage
	if err := common.Unmarshal(payload["data"], &images); err != nil {
		return false
	}
	return len(images) > 0
}

func imageTaskBaseURL(response *http.Response) (*url.URL, error) {
	if response.Request == nil || response.Request.URL == nil {
		return nil, fmt.Errorf("image task response is missing its upstream request URL")
	}
	base := response.Request.URL
	if base.Scheme != "http" && base.Scheme != "https" {
		return nil, fmt.Errorf("unsupported image task upstream scheme: %s", base.Scheme)
	}
	if base.User != nil || base.Hostname() == "" {
		return nil, fmt.Errorf("invalid image task upstream URL")
	}
	return base, nil
}

func resolveImageTaskPollURL(baseURL *url.URL, rawURL string) (*url.URL, error) {
	if baseURL == nil {
		return nil, fmt.Errorf("image task upstream URL is missing")
	}
	reference, err := url.Parse(strings.TrimSpace(rawURL))
	if err != nil {
		return nil, fmt.Errorf("parse image task poll URL: %w", err)
	}
	resolved := baseURL.ResolveReference(reference)
	if resolved.Scheme != "http" && resolved.Scheme != "https" {
		return nil, fmt.Errorf("unsupported image task poll scheme: %s", resolved.Scheme)
	}
	if resolved.User != nil || resolved.Hostname() == "" {
		return nil, fmt.Errorf("invalid image task poll URL")
	}
	if !strings.EqualFold(baseURL.Scheme, resolved.Scheme) || !strings.EqualFold(baseURL.Host, resolved.Host) {
		return nil, fmt.Errorf("image task poll URL must use the original upstream origin")
	}
	return resolved, nil
}

func copyImageTaskAuthHeaders(destination http.Header, source *http.Request) {
	if source == nil {
		return
	}
	for _, name := range []string{"Authorization", "Api-Key", "X-Api-Key"} {
		for _, value := range source.Header.Values(name) {
			destination.Add(name, value)
		}
	}
}

func readImageTaskResponseBody(response *http.Response) ([]byte, error) {
	if response == nil || response.Body == nil {
		return nil, fmt.Errorf("image task response is empty")
	}
	defer response.Body.Close()
	if response.ContentLength > maxImageTaskResponseBytes {
		return nil, fmt.Errorf("image task response exceeds %d bytes", maxImageTaskResponseBytes)
	}
	body, err := io.ReadAll(io.LimitReader(response.Body, maxImageTaskResponseBytes+1))
	if err != nil {
		return nil, fmt.Errorf("read image task response: %w", err)
	}
	if int64(len(body)) > maxImageTaskResponseBytes {
		return nil, fmt.Errorf("image task response exceeds %d bytes", maxImageTaskResponseBytes)
	}
	return body, nil
}

func imageTaskPollDelay(milliseconds int64) time.Duration {
	if milliseconds <= 0 {
		return defaultImageTaskPollDelay
	}
	delay := time.Duration(milliseconds) * time.Millisecond
	if delay < minImageTaskPollDelay {
		return minImageTaskPollDelay
	}
	if delay > maxImageTaskPollDelay {
		return maxImageTaskPollDelay
	}
	return delay
}

func waitForOpenAIImageTask(ctx context.Context, delay time.Duration) error {
	timer := time.NewTimer(delay)
	defer timer.Stop()
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-timer.C:
		return nil
	}
}

func firstNonEmpty(values ...string) string {
	for _, value := range values {
		if trimmed := strings.TrimSpace(value); trimmed != "" {
			return trimmed
		}
	}
	return ""
}

func maskImageTaskID(taskID string) string {
	taskID = strings.TrimSpace(taskID)
	if len(taskID) <= 12 {
		return taskID
	}
	return taskID[:8] + "..." + taskID[len(taskID)-4:]
}