fix(provider): 延长媒体超时并终止超时轮转
将图像和视频的默认 HTTP/轮询超时分别提高到 20 分钟和 30 分钟。媒体任务超时后直接失败,不再重试、切换客户端或标记为 upstream_submission_unknown。OpenAI 图像端点遇到上游明确拒绝 response_format 时自动移除并缓存兼容结论。 验证:go test ./... -count=1;go vet ./...;gofmt;相关 Shell bash -n、ShellCheck 与发布脚本回归测试。
This commit is contained in:
@@ -61,7 +61,7 @@ func (c GeminiClient) Run(ctx context.Context, request Request) (Response, error
|
||||
}
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return Response{}, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return Response{}, transportClientError(err)
|
||||
}
|
||||
if err := notifyResponseReceived(request); err != nil {
|
||||
return Response{}, err
|
||||
@@ -294,7 +294,7 @@ func uploadOfficialGeminiImageFile(ctx context.Context, client *http.Client, can
|
||||
startRequest.Header.Set("Content-Type", "application/json")
|
||||
startResponse, err := client.Do(startRequest)
|
||||
if err != nil {
|
||||
return nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, transportClientError(err)
|
||||
}
|
||||
if startResponse.StatusCode < http.StatusOK || startResponse.StatusCode >= http.StatusMultipleChoices {
|
||||
_, responseErr := decodeHTTPResponse(startResponse)
|
||||
@@ -314,7 +314,7 @@ func uploadOfficialGeminiImageFile(ctx context.Context, client *http.Client, can
|
||||
uploadRequest.Header.Set("Content-Type", mimeType)
|
||||
uploadResponse, err := client.Do(uploadRequest)
|
||||
if err != nil {
|
||||
return nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, transportClientError(err)
|
||||
}
|
||||
result, err := decodeHTTPResponse(uploadResponse)
|
||||
if err != nil {
|
||||
@@ -392,7 +392,9 @@ func decodeGeminiStreamResponse(resp *http.Response, onDelta StreamDelta, wire *
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true, Wire: wire}
|
||||
clientErr := transportClientError(err)
|
||||
clientErr.Wire = wire
|
||||
return nil, clientErr
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil, &ClientError{Code: "invalid_response", Message: "gemini stream returned no events", StatusCode: resp.StatusCode, Retryable: false, Wire: wire}
|
||||
|
||||
@@ -55,7 +55,7 @@ func (c GeminiClient) runVeo(ctx context.Context, request Request, apiKey string
|
||||
}
|
||||
|
||||
interval := durationFromConfig(request.Candidate.PlatformConfig, 10*time.Second, "pollIntervalMs", "poll_interval_ms")
|
||||
timeout := durationFromConfig(request.Candidate.PlatformConfig, 10*time.Minute, "pollTimeoutMs", "poll_timeout_ms", "timeoutMs")
|
||||
timeout := durationFromConfig(request.Candidate.PlatformConfig, ProviderRequestTimeout(request.Kind), "pollTimeoutMs", "poll_timeout_ms", "timeoutMs")
|
||||
deadline := time.NewTimer(timeout)
|
||||
defer deadline.Stop()
|
||||
nextPoll := time.NewTimer(0)
|
||||
@@ -400,7 +400,7 @@ func (c GeminiClient) geminiVeoPost(ctx context.Context, request Request, apiKey
|
||||
}
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return nil, "", nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", nil, transportClientError(err)
|
||||
}
|
||||
if err := notifyResponseReceived(request); err != nil {
|
||||
resp.Body.Close()
|
||||
@@ -426,7 +426,7 @@ func (c GeminiClient) geminiVeoGetOperation(ctx context.Context, request Request
|
||||
req.Header.Set("x-goog-api-key", apiKey)
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return nil, "", nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", nil, transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, wire, err := decodeHTTPResponseForProtocol(resp, ProtocolGeminiVeo)
|
||||
@@ -546,7 +546,7 @@ func (c GeminiClient) geminiVeoDownload(ctx context.Context, request Request, ap
|
||||
}
|
||||
resp, err := redirectClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", transportClientError(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
|
||||
@@ -613,7 +613,7 @@ func (c KelingClient) postJSONAt(ctx context.Context, request Request, baseURL s
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return nil, "", nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", nil, transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, wire, err := decodeHTTPResponseForProtocol(resp, protocol)
|
||||
@@ -638,7 +638,7 @@ func (c KelingClient) getJSONAt(ctx context.Context, request Request, baseURL st
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return nil, "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, err := decodeHTTPResponse(resp)
|
||||
@@ -661,7 +661,7 @@ func (c KelingClient) createKelingElement(ctx context.Context, request Request,
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return "", transportClientError(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 16*1024*1024))
|
||||
@@ -745,7 +745,7 @@ func kelingImageToBase64(ctx context.Context, request Request, value string) (st
|
||||
}
|
||||
resp, err := httpClient(request.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return "", transportClientError(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
@@ -754,7 +754,7 @@ func kelingImageToBase64(ctx context.Context, request Request, value string) (st
|
||||
}
|
||||
raw, err := io.ReadAll(io.LimitReader(resp.Body, 16*1024*1024))
|
||||
if err != nil {
|
||||
return "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return "", transportClientError(err)
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(raw), nil
|
||||
}
|
||||
@@ -1373,9 +1373,10 @@ func kelingPollInterval(request Request) time.Duration {
|
||||
}
|
||||
|
||||
func kelingPollTimeout(request Request) time.Duration {
|
||||
seconds := numericValue(firstPresent(request.Candidate.PlatformConfig["kelingPollTimeoutSeconds"], request.Candidate.PlatformConfig["klingPollTimeoutSeconds"], request.Body["pollTimeoutSeconds"], request.Body["poll_timeout_seconds"]), 600)
|
||||
fallbackSeconds := ProviderRequestTimeout(request.Kind).Seconds()
|
||||
seconds := numericValue(firstPresent(request.Candidate.PlatformConfig["kelingPollTimeoutSeconds"], request.Candidate.PlatformConfig["klingPollTimeoutSeconds"], request.Body["pollTimeoutSeconds"], request.Body["poll_timeout_seconds"]), fallbackSeconds)
|
||||
if seconds < 1 {
|
||||
seconds = 600
|
||||
seconds = fallbackSeconds
|
||||
}
|
||||
return time.Duration(seconds) * time.Second
|
||||
}
|
||||
|
||||
@@ -646,7 +646,7 @@ func providerPostMultipartFile(ctx context.Context, client *http.Client, url str
|
||||
applyProviderAuth(req, credentials, auth)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, err := decodeHTTPResponse(resp)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestProviderRequestTimeoutUsesMediaDefaults(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
kind string
|
||||
want time.Duration
|
||||
}{
|
||||
{kind: "images.generations", want: 20 * time.Minute},
|
||||
{kind: "images.edits", want: 20 * time.Minute},
|
||||
{kind: "videos.generations", want: 30 * time.Minute},
|
||||
{kind: "chat.completions", want: 10 * time.Minute},
|
||||
} {
|
||||
if got := ProviderRequestTimeout(test.kind); got != test.want {
|
||||
t.Fatalf("timeout for %s: got %s want %s", test.kind, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportTimeoutUsesTerminalTimeoutCode(t *testing.T) {
|
||||
err := transportClientError(context.DeadlineExceeded)
|
||||
if err.Code != "timeout" || err.Retryable {
|
||||
t.Fatalf("transport timeout classification = %+v, want terminal timeout", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMediaPollTimeoutsUseTaskDefaultsWithoutPlatformOverride(t *testing.T) {
|
||||
image := Request{Kind: "images.generations", Candidate: storeCandidateWithConfig("", "", nil, nil)}
|
||||
video := Request{Kind: "videos.generations", Candidate: storeCandidateWithConfig("", "", nil, nil)}
|
||||
if got := providerPollTimeout(image); got != 20*time.Minute {
|
||||
t.Fatalf("image provider poll timeout: got %s want %s", got, 20*time.Minute)
|
||||
}
|
||||
if got := providerPollTimeout(video); got != 30*time.Minute {
|
||||
t.Fatalf("video provider poll timeout: got %s want %s", got, 30*time.Minute)
|
||||
}
|
||||
if got := kelingPollTimeout(video); got != 30*time.Minute {
|
||||
t.Fatalf("Keling video poll timeout: got %s want %s", got, 30*time.Minute)
|
||||
}
|
||||
if got := volcesPollTimeout(video); got != 30*time.Minute {
|
||||
t.Fatalf("Volces video poll timeout: got %s want %s", got, 30*time.Minute)
|
||||
}
|
||||
}
|
||||
@@ -82,7 +82,8 @@ func (c OpenAIClient) Run(ctx context.Context, request Request) (Response, error
|
||||
upstreamEndpoint := joinURL(openAIBaseURL(endpointKind, request.Candidate), endpoint)
|
||||
responseStartedAt := time.Now()
|
||||
correctionScope := newParameterCorrectionScope(request, endpointKind)
|
||||
correctionEnabled := endpointKind == "chat.completions" || endpointKind == "responses"
|
||||
correctionEnabled := endpointKind == "chat.completions" || endpointKind == "responses" ||
|
||||
endpointKind == "images.generations" || endpointKind == "images.edits"
|
||||
protectedCorrections := callerProtectedCorrectionParameters(request, endpointKind)
|
||||
if correctionEnabled {
|
||||
c.Corrections.apply(correctionScope, body, protectedCorrections)
|
||||
@@ -108,7 +109,7 @@ func (c OpenAIClient) Run(ctx context.Context, request Request) (Response, error
|
||||
}
|
||||
resp, requestErr = requestClient.Do(req)
|
||||
if requestErr != nil {
|
||||
return Response{}, &ClientError{Code: "network", Message: requestErr.Error(), Retryable: true}
|
||||
return Response{}, transportClientError(requestErr)
|
||||
}
|
||||
if err := notifyResponseReceived(request); err != nil {
|
||||
_ = resp.Body.Close()
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
|
||||
)
|
||||
|
||||
func TestOpenAIImageEndpointsRemoveRejectedResponseFormatAndCacheCorrection(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
kind string
|
||||
body map[string]any
|
||||
}{
|
||||
{
|
||||
name: "generation JSON",
|
||||
kind: "images.generations",
|
||||
body: map[string]any{"prompt": "test", "response_format": "url"},
|
||||
},
|
||||
{
|
||||
name: "edit multipart",
|
||||
kind: "images.edits",
|
||||
body: map[string]any{"prompt": "test", "image": "aW1hZ2U=", "response_format": "url"},
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestNo := requests.Add(1)
|
||||
responseFormat := ""
|
||||
if test.kind == "images.edits" {
|
||||
if err := r.ParseMultipartForm(1 << 20); err != nil {
|
||||
t.Fatalf("parse multipart image request: %v", err)
|
||||
}
|
||||
responseFormat = r.FormValue("response_format")
|
||||
} else {
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode image request: %v", err)
|
||||
}
|
||||
responseFormat, _ = body["response_format"].(string)
|
||||
}
|
||||
if requestNo == 1 {
|
||||
if responseFormat != "url" {
|
||||
t.Fatalf("first request lost caller response_format: %q", responseFormat)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{
|
||||
"message": "Unknown parameter: 'response_format'.",
|
||||
"type": "invalid_request_error", "param": "response_format", "code": "unknown_parameter",
|
||||
}})
|
||||
return
|
||||
}
|
||||
if responseFormat != "" {
|
||||
t.Fatalf("corrected request still contains response_format: %q", responseFormat)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": []any{map[string]any{"b64_json": "aW1hZ2U="}}})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cache := NewParameterCorrectionCache()
|
||||
request := Request{
|
||||
Kind: test.kind, Model: "gpt-image-2", Body: test.body, OriginalBody: test.body,
|
||||
UpstreamIdempotencyKey: "gateway-task-id",
|
||||
Candidate: store.RuntimeModelCandidate{
|
||||
Provider: "openai", BaseURL: server.URL, ProviderModelName: "gpt-image-2",
|
||||
Credentials: map[string]any{"apiKey": "test-key"},
|
||||
},
|
||||
}
|
||||
client := OpenAIClient{HTTPClient: server.Client(), Corrections: cache}
|
||||
if _, err := client.Run(context.Background(), request); err != nil {
|
||||
t.Fatalf("correct rejected response_format: %v", err)
|
||||
}
|
||||
if requests.Load() != 2 {
|
||||
t.Fatalf("first corrected call requests=%d, want 2", requests.Load())
|
||||
}
|
||||
if _, err := client.Run(context.Background(), request); err != nil {
|
||||
t.Fatalf("reuse cached response_format correction: %v", err)
|
||||
}
|
||||
if requests.Load() != 3 {
|
||||
t.Fatalf("cached correction should avoid another 400, requests=%d", requests.Load())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ var safeUpstreamCorrectionParameters = stringSet(
|
||||
"temperature", "top_p", "frequency_penalty", "presence_penalty", "seed", "stop", "n",
|
||||
"max_tokens", "max_completion_tokens", "max_output_tokens",
|
||||
"logprobs", "top_logprobs", "service_tier", "verbosity",
|
||||
"response_format",
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -286,6 +287,12 @@ func callerProtectedCorrectionParameters(request Request, endpointKind string) m
|
||||
}
|
||||
}
|
||||
}
|
||||
if endpointKind == "images.generations" || endpointKind == "images.edits" {
|
||||
// The Gateway canonicalizes image output independently of the provider's
|
||||
// URL/Base64 preference. Allow an explicitly supplied response_format to
|
||||
// be removed only after the selected upstream rejects it as unsupported.
|
||||
delete(protected, "response_format")
|
||||
}
|
||||
return protected
|
||||
}
|
||||
|
||||
|
||||
@@ -190,7 +190,7 @@ func providerPostJSON(ctx context.Context, client *http.Client, url string, body
|
||||
applyProviderAuth(req, credentials, auth)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, err := decodeHTTPResponse(resp)
|
||||
@@ -205,7 +205,7 @@ func providerGetJSON(ctx context.Context, client *http.Client, url string, crede
|
||||
applyProviderAuth(req, credentials, auth)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, err := decodeHTTPResponse(resp)
|
||||
@@ -460,7 +460,7 @@ func providerPollInterval(request Request) time.Duration {
|
||||
}
|
||||
|
||||
func providerPollTimeout(request Request) time.Duration {
|
||||
return durationFromConfig(request.Candidate.PlatformConfig, 10*time.Minute, "pollTimeoutMs", "poll_timeout_ms", "timeoutMs")
|
||||
return durationFromConfig(request.Candidate.PlatformConfig, ProviderRequestTimeout(request.Kind), "pollTimeoutMs", "poll_timeout_ms", "timeoutMs")
|
||||
}
|
||||
|
||||
func durationFromConfig(config map[string]any, fallback time.Duration, keys ...string) time.Duration {
|
||||
|
||||
@@ -163,7 +163,7 @@ func (c TopazClient) prepareSource(ctx context.Context, request Request, rawURL
|
||||
downloadClient := topazSourceHTTPClient(httpClient(request.HTTPClient, c.HTTPClient), boolishDefault(request.Candidate.PlatformConfig["allowPrivateSourceDownloads"], false))
|
||||
resp, err := downloadClient.Do(req)
|
||||
if err != nil {
|
||||
return topazSource{}, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return topazSource{}, transportClientError(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
@@ -287,7 +287,7 @@ func (c TopazClient) uploadSource(ctx context.Context, request Request, apiKey,
|
||||
req.Header.Set("Content-Type", topazContainerContentType(source.Container))
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return transportClientError(err)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<20))
|
||||
resp.Body.Close()
|
||||
@@ -353,7 +353,7 @@ func (c TopazClient) topazJSON(ctx context.Context, request Request, apiKey, met
|
||||
}
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, transportClientError(err)
|
||||
}
|
||||
result, decodeErr := decodeHTTPResponse(resp)
|
||||
if decodeErr != nil {
|
||||
|
||||
@@ -3,6 +3,7 @@ package clients
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -10,6 +11,41 @@ import (
|
||||
"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultProviderRequestTimeout = 10 * time.Minute
|
||||
imageProviderRequestTimeout = 20 * time.Minute
|
||||
videoProviderRequestTimeout = 30 * time.Minute
|
||||
)
|
||||
|
||||
// ProviderRequestTimeout returns the default end-to-end provider request or
|
||||
// polling budget for a task kind. Explicit platform polling configuration may
|
||||
// still override these defaults for providers with a documented requirement.
|
||||
func ProviderRequestTimeout(kind string) time.Duration {
|
||||
switch {
|
||||
case strings.HasPrefix(strings.TrimSpace(kind), "images."):
|
||||
return imageProviderRequestTimeout
|
||||
case strings.HasPrefix(strings.TrimSpace(kind), "videos."):
|
||||
return videoProviderRequestTimeout
|
||||
default:
|
||||
return defaultProviderRequestTimeout
|
||||
}
|
||||
}
|
||||
|
||||
func transportClientError(err error) *ClientError {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var netErr net.Error
|
||||
if errors.Is(err, context.DeadlineExceeded) || (errors.As(err, &netErr) && netErr.Timeout()) {
|
||||
return &ClientError{
|
||||
Code: "timeout",
|
||||
Message: "upstream request timed out: " + err.Error(),
|
||||
Retryable: false,
|
||||
}
|
||||
}
|
||||
return &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Kind string
|
||||
ModelType string
|
||||
|
||||
@@ -153,7 +153,7 @@ func (c UniversalClient) universalSubmit(ctx context.Context, executor *scripten
|
||||
|
||||
func (c UniversalClient) universalPollUntilDone(ctx context.Context, executor *scriptengine.Executor, request Request, modelType string, upstreamTaskID string, payload map[string]any, requestID string, startedAt time.Time) (map[string]any, string, error) {
|
||||
interval := universalDurationConfig(request.Candidate.PlatformConfig, 2*time.Second, "pollIntervalMs", "poll_interval_ms")
|
||||
timeout := universalDurationConfig(request.Candidate.PlatformConfig, 10*time.Minute, "pollTimeoutMs", "poll_timeout_ms", "timeoutMs")
|
||||
timeout := universalDurationConfig(request.Candidate.PlatformConfig, ProviderRequestTimeout(request.Kind), "pollTimeoutMs", "poll_timeout_ms", "timeoutMs")
|
||||
deadline := time.NewTimer(timeout)
|
||||
defer deadline.Stop()
|
||||
ticker := time.NewTicker(interval)
|
||||
@@ -234,7 +234,7 @@ func universalScriptContext(request Request, modelType string, payload map[strin
|
||||
"platformModelId": request.Candidate.PlatformModelID,
|
||||
"canonicalModelKey": request.Candidate.CanonicalModelKey,
|
||||
"modelType": modelType,
|
||||
"timeout": universalDurationConfig(request.Candidate.PlatformConfig, 10*time.Minute, "pollTimeoutMs", "poll_timeout_ms").Milliseconds(),
|
||||
"timeout": universalDurationConfig(request.Candidate.PlatformConfig, ProviderRequestTimeout(request.Kind), "pollTimeoutMs", "poll_timeout_ms").Milliseconds(),
|
||||
},
|
||||
"env": cloneMapAny(request.Candidate.PlatformConfig),
|
||||
"candidate": universalCandidateSnapshot(request),
|
||||
@@ -318,7 +318,7 @@ func universalPostJSON(ctx context.Context, client *http.Client, baseURL string,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, err := decodeHTTPResponse(resp)
|
||||
@@ -335,7 +335,7 @@ func universalGetJSON(ctx context.Context, client *http.Client, url string, cred
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, err := decodeHTTPResponse(resp)
|
||||
|
||||
@@ -78,7 +78,7 @@ func (c VectorizerClient) Run(ctx context.Context, request Request) (Response, e
|
||||
client := httpClient(request.HTTPClient, c.HTTPClient)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return Response{}, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return Response{}, transportClientError(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
|
||||
@@ -52,7 +52,7 @@ func (c VolcesClient) runImage(ctx context.Context, request Request, apiKey stri
|
||||
}
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return Response{}, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return Response{}, transportClientError(err)
|
||||
}
|
||||
if err := notifyResponseReceived(request); err != nil {
|
||||
return Response{}, err
|
||||
@@ -199,7 +199,7 @@ func (c VolcesClient) DeleteVideoTask(ctx context.Context, request Request) (map
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
response, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return nil, "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(response)
|
||||
result, err := decodeHTTPResponse(response)
|
||||
@@ -239,7 +239,7 @@ func (c VolcesClient) postJSON(ctx context.Context, request Request, baseURL str
|
||||
}
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return nil, "", nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", nil, transportClientError(err)
|
||||
}
|
||||
if err := notifyResponseReceived(request); err != nil {
|
||||
return nil, "", nil, err
|
||||
@@ -271,7 +271,7 @@ func (c VolcesClient) getJSON(ctx context.Context, request Request, baseURL stri
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
resp, err := httpClient(request.HTTPClient, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return nil, "", nil, &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return nil, "", nil, transportClientError(err)
|
||||
}
|
||||
requestID := requestIDFromHTTPResponse(resp)
|
||||
result, wire, err := decodeHTTPResponseForProtocol(resp, ProtocolVolcesContents)
|
||||
@@ -1184,9 +1184,10 @@ func volcesPollInterval(request Request) time.Duration {
|
||||
}
|
||||
|
||||
func volcesPollTimeout(request Request) time.Duration {
|
||||
seconds := numericValue(firstPresent(request.Candidate.PlatformConfig["volcesPollTimeoutSeconds"], request.Body["pollTimeoutSeconds"], request.Body["poll_timeout_seconds"]), 600)
|
||||
fallbackSeconds := ProviderRequestTimeout(request.Kind).Seconds()
|
||||
seconds := numericValue(firstPresent(request.Candidate.PlatformConfig["volcesPollTimeoutSeconds"], request.Body["pollTimeoutSeconds"], request.Body["poll_timeout_seconds"]), fallbackSeconds)
|
||||
if seconds < 1 {
|
||||
seconds = 600
|
||||
seconds = fallbackSeconds
|
||||
}
|
||||
return time.Duration(seconds) * time.Second
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ func (c VolcesAssetClient) call(ctx context.Context, credentials VolcesAssetCred
|
||||
|
||||
response, err := httpClient(nil, c.HTTPClient).Do(req)
|
||||
if err != nil {
|
||||
return "", &ClientError{Code: "network", Message: err.Error(), Retryable: true}
|
||||
return "", transportClientError(err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
var envelope struct {
|
||||
|
||||
Reference in New Issue
Block a user