Files
CLIProxyAPI/internal/runtime/executor/openai_compat_executor.go
Luis Pater f5d017afa5 feat(antigravity): complete Hub 2.36.0 v2 parity and origin/dev audit fixes
- Batch A: fix ObserveAntigravitySafetySignals for results[]/embeddingVerdicts/numeric 2/snake_case with structured logrus fields, align used_claude_conservative OR-latch and subagent_type underscore sanitization, unify Anthropic model detection (claude/fable-/opus-/sonnet-/haiku-) across all 11 core sites with C4 linear adjacent-user merge and unified signature provider/cache group resolution, add legObservedPayload continuation retry guard, strip request.stream/snake_case/duplicate toolConfig, and cap non-2xx/unary response reads
- Batch B: enforce 1 MiB OAuth refresh body cap and pre-RTT expiry, add compare-and-delete invalidateAICodeAccessToken on AICode 401 and broad refresh, harden sdk/cliproxy AICode token minter (Antigravity User-Agent, pre-RTT, non-cancelable singleflight, 400 invalid_grant AuthError, 401 retry-once, stale metadata suppression isolated from broad-bearer suppression), tighten status-code-aware NEW-06 scope gating, and pool/cache /v1internal:rewriteUri grounding URL resolution
- Batch C: complete 4 hard-quota 429 substrings, 240s retry ceiling, numeric gRPC 8/14 and missing error.status handling, retry-after-ms header precedence, PayGo X-Goog-User-Project omission helper, 5 snake_case GenerationConfig aliases, BlockReason 5->6 normalization, read-only post-FinalizePayloadDoc GOOGLE_ONE_HELIUM rejection, remaining Mendel cutover flags and rate-limited sign-out-missing-aicode-scope warnings, tieredModelIds + M-04 disabled model filtering, telemetry JSON label scrubbing across all request types, shared bare-token/plt redaction, and allowlisted OAuth error parsing preserving typed 403 TOS/validation metadata
- Batch D: reject empty access_token and non-positive expires_in with proxy/epoch-scoped OAuth singleflight, wire AdaptiveThinking/ThoughtCirculation/DeferredToolLoading/TieredModelIDs through sdk/cliproxy, harden credits quota admission guards, map FormatAntigravity to "antigravity" with mixed-pool LCP mirroring and dual rollback generations, upgrade signature ownership to Get() LRU recency + Home KV SSOT with millisecond PX backfill and NX lease protection, and add bounded 32-leg unary Execute continuation loop
2026-11-30 23:29:47 +08:00

1568 lines
59 KiB
Go

package executor
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"math"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"strconv"
"strings"
"time"
"github.com/MoeForge/moego/json"
"github.com/MoeForge/moego/ojson"
"github.com/google/uuid"
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
"github.com/router-for-me/CLIProxyAPI/v8/internal/misc"
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
"github.com/router-for-me/CLIProxyAPI/v8/internal/thinking"
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
log "github.com/sirupsen/logrus"
)
const (
openAICompatImageHandlerType = "openai-image"
openAICompatImagesGenerationsPath = "/images/generations"
openAICompatImagesEditsPath = "/images/edits"
openAICompatDefaultImageEndpoint = openAICompatImagesGenerationsPath
openAICompatMultipartMemory int64 = 32 << 20
openAICompatDefaultUserAgent = "cli-proxy-openai-compat"
)
func ensureOpenAICompatUserAgent(r *http.Request, clientHeaders ...http.Header) {
if r == nil {
return
}
misc.EnsureHeader(r.Header, util.ResolveClientHeaders(r, clientHeaders...), "User-Agent", openAICompatDefaultUserAgent)
}
// OpenAICompatExecutor implements a stateless executor for OpenAI-compatible providers.
// It performs request/response translation and executes against the provider base URL
// using per-auth credentials (API key) and per-auth HTTP transport (proxy) from context.
type OpenAICompatExecutor struct {
provider string
cfg *config.Config
}
// NewOpenAICompatExecutor creates an executor bound to a provider key (e.g., "openrouter").
func NewOpenAICompatExecutor(provider string, cfg *config.Config) *OpenAICompatExecutor {
return &OpenAICompatExecutor{provider: provider, cfg: cfg}
}
// Identifier implements cliproxyauth.ProviderExecutor.
func (e *OpenAICompatExecutor) Identifier() string { return e.provider }
// UsesConfig reports whether the executor is bound to the provided config snapshot.
func (e *OpenAICompatExecutor) UsesConfig(cfg *config.Config) bool {
return e != nil && e.cfg == cfg
}
// PrepareRequest injects OpenAI-compatible credentials into the outgoing HTTP request.
func (e *OpenAICompatExecutor) PrepareRequest(req *http.Request, auth *cliproxyauth.Auth) error {
if req == nil {
return nil
}
_, apiKey := e.resolveCredentials(auth)
if strings.TrimSpace(apiKey) != "" {
req.Header.Set("Authorization", "Bearer "+apiKey)
}
ensureOpenAICompatUserAgent(req)
var attrs map[string]string
if auth != nil {
attrs = auth.Attributes
}
util.ApplyCustomHeadersFromAttrs(req, attrs)
return nil
}
// HttpRequest injects OpenAI-compatible credentials into the request and executes it.
func (e *OpenAICompatExecutor) HttpRequest(ctx context.Context, auth *cliproxyauth.Auth, req *http.Request) (*http.Response, error) {
if req == nil {
return nil, fmt.Errorf("openai compat executor: request is nil")
}
if ctx == nil {
ctx = req.Context()
}
httpReq := req.WithContext(ctx)
if err := e.PrepareRequest(httpReq, auth); err != nil {
return nil, err
}
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
return httpClient.Do(httpReq)
}
func (e *OpenAICompatExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) {
ctx = helps.EnsureSessionContext(ctx, opts, req.Payload)
if endpointPath := openAICompatImageEndpointPath(opts); endpointPath != "" {
return e.executeImages(ctx, auth, req, opts, endpointPath)
}
baseURL, apiKey := e.resolveCredentials(auth)
compat := e.resolveCompatConfig(auth, req)
return e.executeWithCredentials(ctx, auth, req, opts, baseURL, apiKey, compat)
}
func (e *OpenAICompatExecutor) executeWithCredentials(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, baseURL, apiKey string, compat *config.OpenAICompatibility) (resp cliproxyexecutor.Response, err error) {
ctx = helps.EnsureSessionContext(ctx, opts, req.Payload)
if opts.SourceFormat == sdktranslator.FormatOpenAIResponse {
if opts.Alt == "responses/compact" {
if errExpand := helps.ExpandRecognizedResponsesCompactionCapsules(&req, &opts); errExpand != nil {
return resp, statusErr{code: http.StatusBadRequest, msg: errExpand.Error()}
}
} else {
if errExpand := helps.ExpandResponsesCompactionCapsules(&req, &opts, e.Identifier()); errExpand != nil {
return resp, statusErr{code: http.StatusBadRequest, msg: errExpand.Error()}
}
if helps.HasResponsesCompactionTriggerRequest(req, opts) {
return e.executeOpenAICompatResponsesCompaction(ctx, auth, req, opts, baseURL, apiKey, compat)
}
}
}
baseModel := thinking.ParseSuffix(req.Model).ModelName
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
defer reporter.TrackFailure(ctx, &err)
if baseURL == "" {
err = statusErr{code: http.StatusUnauthorized, msg: "missing provider baseURL"}
return
}
from := opts.SourceFormat
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
to := sdktranslator.FromString("openai")
endpoint := "/chat/completions"
if opts.Alt == "responses/compact" {
to = sdktranslator.FromString("openai-response")
endpoint = "/responses/compact"
}
originalPayloadSource := req.Payload
if len(opts.OriginalRequest) > 0 {
originalPayloadSource = opts.OriginalRequest
}
originalPayload := originalPayloadSource
isCompat := helps.APIKeyModelIsCompat(req)
targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts)
originalTranslated, translated, updatesChanged, err := helps.TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent(ctx, opts.Headers, e.cfg, from, to, targetModel, originalPayload, req.Payload, opts.Stream, isCompat)
if err != nil {
return resp, err
}
originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel)
doc := ojson.NewDoc(translated)
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged); err != nil {
return resp, err
}
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
requestPath := helps.PayloadRequestPath(opts)
if helps.ShouldNormalizeOpenAIToolResultsForModel(compat, baseModel, requestedModel) {
helps.NormalizeOpenAIToolResultsTextOnlyDoc(doc)
}
if opts.Alt != "responses/compact" {
useMCT := helps.ShouldUseMaxCompletionTokensForModel(compat, baseModel, requestedModel)
helps.NormalizeOpenAIMaxTokensDoc(doc, useMCT)
if err = e.applyPromptCacheKeyWithCompatDoc(ctx, auth, compat, from, baseModel, req, opts, doc); err != nil {
return resp, err
}
}
if opts.Alt == "responses/compact" {
_ = doc.Delete("stream")
sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURLDoc(ctx, "openai compat executor", doc, true, resolveResponsesSignatureTargetModel(baseModel, req, opts), baseURL)
}
url := strings.TrimSuffix(baseURL, "/") + endpoint
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, baseModel, to.String(), from.String(), "", doc, originalTranslated, requestedModel, requestPath, opts.Headers)
translated = doc.Bytes()
reporter.SetTranslatedReasoningEffort(translated, to.String())
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(translated))
if err != nil {
return resp, err
}
httpReq.Header.Set("Content-Type", "application/json")
if apiKey != "" {
httpReq.Header.Set("Authorization", "Bearer "+apiKey)
}
ensureOpenAICompatUserAgent(httpReq, opts.Headers)
var attrs map[string]string
if auth != nil {
attrs = auth.Attributes
}
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
var authID, authLabel, authType, authValue string
if auth != nil {
authID = auth.ID
authLabel = auth.Label
authType, authValue = auth.AccountInfo()
}
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
URL: url,
Method: http.MethodPost,
Headers: httpReq.Header.Clone(),
Body: translated,
Provider: e.Identifier(),
AuthID: authID,
AuthLabel: authLabel,
AuthType: authType,
AuthValue: authValue,
})
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
httpClient = reporter.TrackHTTPClient(httpClient)
httpResp, err := httpClient.Do(httpReq)
if err != nil {
helps.RecordAPIResponseError(ctx, e.cfg, err)
return resp, err
}
defer func() {
if errClose := httpResp.Body.Close(); errClose != nil {
log.Errorf("openai compat executor: close response body error: %v", errClose)
}
}()
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
b, errRead := io.ReadAll(httpResp.Body)
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
if errRead != nil {
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
}
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
err = newOpenAICompatStatusError(httpResp.StatusCode, httpResp.Header, b)
return resp, err
}
body, err := io.ReadAll(httpResp.Body)
helps.AppendAPIResponseChunk(ctx, e.cfg, body)
if err != nil {
helps.RecordAPIResponseError(ctx, e.cfg, err)
return resp, err
}
reporter.ObserveResponseModel(body)
// Ensure we at least record the request even if upstream doesn't return usage
// Translate response back to source format when needed
var param any
out := sdktranslator.TranslateNonStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translated, body, &param)
if helps.ApplyPatchTranslationError(param) != nil || len(out) == 0 {
return cliproxyexecutor.Response{}, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}
}
reporter.Publish(ctx, helps.ParseOpenAIUsage(body))
if responseFormat == sdktranslator.FormatOpenAIResponse {
out = helps.EnsureResponsesUsageDetails(out)
}
resp = cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}
return resp, nil
}
func (e *OpenAICompatExecutor) executeImages(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, endpointPath string) (resp cliproxyexecutor.Response, err error) {
baseModel := thinking.ParseSuffix(req.Model).ModelName
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
defer reporter.TrackFailure(ctx, &err)
baseURL, apiKey := e.resolveCredentials(auth)
if baseURL == "" {
err = statusErr{code: http.StatusUnauthorized, msg: "missing provider baseURL"}
return resp, err
}
payload, contentType, errPrepare := prepareOpenAICompatImagesPayload(req.Payload, baseModel, opts.Headers.Get("Content-Type"), false)
if errPrepare != nil {
err = errPrepare
return resp, err
}
if contentType == "" {
contentType = "application/json"
}
payload, contentType, errPrepare = helps.ApplyMediaPayloadConfig(e.cfg, e.Identifier(), baseModel, "openai", payload, contentType, req, opts)
if errPrepare != nil {
return resp, errPrepare
}
reporter.SetTranslatedReasoningEffort(payload, "openai")
url := strings.TrimSuffix(baseURL, "/") + endpointPath
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload))
if err != nil {
return resp, err
}
httpReq.Header.Set("Content-Type", contentType)
if apiKey != "" {
httpReq.Header.Set("Authorization", "Bearer "+apiKey)
}
ensureOpenAICompatUserAgent(httpReq, opts.Headers)
var attrs map[string]string
if auth != nil {
attrs = auth.Attributes
}
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
var authID, authLabel, authType, authValue string
if auth != nil {
authID = auth.ID
authLabel = auth.Label
authType, authValue = auth.AccountInfo()
}
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
URL: url,
Method: http.MethodPost,
Headers: httpReq.Header.Clone(),
Body: payload,
Provider: e.Identifier(),
AuthID: authID,
AuthLabel: authLabel,
AuthType: authType,
AuthValue: authValue,
})
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
httpClient = reporter.TrackHTTPClient(httpClient)
httpResp, err := httpClient.Do(httpReq)
if err != nil {
helps.RecordAPIResponseError(ctx, e.cfg, err)
return resp, err
}
defer func() {
if errClose := httpResp.Body.Close(); errClose != nil {
log.Errorf("openai compat executor: close response body error: %v", errClose)
}
}()
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
body, errRead := io.ReadAll(httpResp.Body)
helps.AppendAPIResponseChunk(ctx, e.cfg, body)
if errRead != nil {
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
err = errRead
return resp, err
}
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), body))
err = newOpenAICompatStatusError(httpResp.StatusCode, httpResp.Header, body)
return resp, err
}
reporter.ObserveResponseModel(body)
reporter.Publish(ctx, helps.ParseOpenAIImageUsage(payload, body, contentType))
reporter.EnsurePublished(ctx)
resp = cliproxyexecutor.Response{Payload: body, Headers: httpResp.Header.Clone()}
return resp, nil
}
func (e *OpenAICompatExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (_ *cliproxyexecutor.StreamResult, err error) {
ctx = helps.EnsureSessionContext(ctx, opts, req.Payload)
if endpointPath := openAICompatImageEndpointPath(opts); endpointPath != "" {
return e.executeImagesStream(ctx, auth, req, opts, endpointPath)
}
baseURL, apiKey := e.resolveCredentials(auth)
compat := e.resolveCompatConfig(auth, req)
return e.executeStreamWithCredentials(ctx, auth, req, opts, baseURL, apiKey, compat)
}
func (e *OpenAICompatExecutor) executeStreamWithCredentials(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, baseURL, apiKey string, compat *config.OpenAICompatibility) (_ *cliproxyexecutor.StreamResult, err error) {
ctx = helps.EnsureSessionContext(ctx, opts, req.Payload)
if opts.SourceFormat == sdktranslator.FormatOpenAIResponse && opts.Alt != "responses/compact" {
if errExpand := helps.ExpandResponsesCompactionCapsules(&req, &opts, e.Identifier()); errExpand != nil {
return nil, statusErr{code: http.StatusBadRequest, msg: errExpand.Error()}
}
if helps.HasResponsesCompactionTriggerRequest(req, opts) {
return e.executeOpenAICompatResponsesCompactionStream(ctx, auth, req, opts, baseURL, apiKey, compat)
}
}
baseModel := thinking.ParseSuffix(req.Model).ModelName
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
defer reporter.TrackFailure(ctx, &err)
if baseURL == "" {
err = statusErr{code: http.StatusUnauthorized, msg: "missing provider baseURL"}
return nil, err
}
from := opts.SourceFormat
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
to := sdktranslator.FromString("openai")
originalPayloadSource := req.Payload
if len(opts.OriginalRequest) > 0 {
originalPayloadSource = opts.OriginalRequest
}
originalPayload := originalPayloadSource
isCompat := helps.APIKeyModelIsCompat(req)
targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts)
originalTranslated, translated, updatesChanged, err := helps.TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent(ctx, opts.Headers, e.cfg, from, to, targetModel, originalPayload, req.Payload, true, isCompat)
if err != nil {
return nil, err
}
originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel)
doc := ojson.NewDoc(translated)
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged); err != nil {
return nil, err
}
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
requestPath := helps.PayloadRequestPath(opts)
if helps.ShouldNormalizeOpenAIToolResultsForModel(compat, baseModel, requestedModel) {
helps.NormalizeOpenAIToolResultsTextOnlyDoc(doc)
}
if opts.Alt != "responses/compact" {
useMCT := helps.ShouldUseMaxCompletionTokensForModel(compat, baseModel, requestedModel)
helps.NormalizeOpenAIMaxTokensDoc(doc, useMCT)
if err = e.applyPromptCacheKeyWithCompatDoc(ctx, auth, compat, from, baseModel, req, opts, doc); err != nil {
return nil, err
}
}
// Request usage data in the final streaming chunk so that token statistics
// are captured even when the upstream is an OpenAI-compatible provider.
helps.SetBoolIfDifferentDoc(doc, "stream_options.include_usage", true)
url := strings.TrimSuffix(baseURL, "/") + "/chat/completions"
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, baseModel, to.String(), from.String(), "", doc, originalTranslated, requestedModel, requestPath, opts.Headers)
translated = doc.Bytes()
reporter.SetTranslatedReasoningEffort(translated, to.String())
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(translated))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "application/json")
if apiKey != "" {
httpReq.Header.Set("Authorization", "Bearer "+apiKey)
}
ensureOpenAICompatUserAgent(httpReq, opts.Headers)
var attrs map[string]string
if auth != nil {
attrs = auth.Attributes
}
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
httpReq.Header.Set("Accept", "text/event-stream")
httpReq.Header.Set("Cache-Control", "no-cache")
var authID, authLabel, authType, authValue string
if auth != nil {
authID = auth.ID
authLabel = auth.Label
authType, authValue = auth.AccountInfo()
}
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
URL: url,
Method: http.MethodPost,
Headers: httpReq.Header.Clone(),
Body: translated,
Provider: e.Identifier(),
AuthID: authID,
AuthLabel: authLabel,
AuthType: authType,
AuthValue: authValue,
})
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
// Early role/empty frames are only first-packet fallbacks, not effective tokens.
httpClient = reporter.TrackHTTPClientRoundTripOnly(httpClient)
httpResp, err := httpClient.Do(httpReq)
if err != nil {
helps.RecordAPIResponseError(ctx, e.cfg, err)
return nil, err
}
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
b, errRead := io.ReadAll(httpResp.Body)
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
if errRead != nil {
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
}
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
if errClose := httpResp.Body.Close(); errClose != nil {
log.Errorf("openai compat executor: close response body error: %v", errClose)
}
err = newOpenAICompatStatusError(httpResp.StatusCode, httpResp.Header, b)
return nil, err
}
out := make(chan cliproxyexecutor.StreamChunk)
go func() {
defer close(out)
defer func() {
if errClose := httpResp.Body.Close(); errClose != nil {
log.Errorf("openai compat executor: close response body error: %v", errClose)
}
}()
scanner := bufio.NewScanner(httpResp.Body)
scanner.Buffer(nil, 52_428_800) // 50MB
claudeInputTokens := helps.NewClaudeInputTokenState(from, to, responseFormat, originalPayload)
var param any
helps.InitializeApplyPatchStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translated, &param)
var streamUsage helps.StreamUsageBuffer
var seenDone bool
var terminalDelivered bool
var sawFinishReason bool
var streamFailed bool
var streamAborted bool
var upstreamEvent string
var frameData [][]byte
defer func() {
if errCtx := ctx.Err(); (streamAborted || errCtx != nil) && !seenDone && !terminalDelivered && !streamFailed {
if errCtx == nil {
errCtx = context.Canceled
}
if !streamUsage.PublishFailure(ctx, reporter, errCtx) {
reporter.PublishFailure(ctx, errCtx)
}
return
}
streamUsage.Publish(ctx, reporter)
}()
publishStreamError := func(streamErr statusErr, containsPayload bool) {
loggedErr := streamErr
if containsPayload {
loggedErr = statusErr{code: streamErr.code, msg: "upstream stream returned an error payload"}
}
helps.RecordAPIResponseError(ctx, e.cfg, loggedErr)
if !streamUsage.PublishFailure(ctx, reporter, loggedErr) {
reporter.PublishFailure(ctx, loggedErr)
}
select {
case out <- cliproxyexecutor.StreamChunk{Err: streamErr}:
case <-ctx.Done():
}
streamFailed = true
}
processFrame := func() bool {
eventName := upstreamEvent
upstreamEvent = ""
dataLines := frameData
frameData = nil
if len(dataLines) == 0 {
if openAICompatErrorEvent(eventName) {
publishStreamError(statusErr{code: http.StatusBadGateway, msg: "upstream error event ended without data"}, false)
return true
}
return false
}
if len(dataLines) > 1 {
for _, dataLine := range dataLines {
if bytes.Equal(bytes.TrimSpace(dataLine), []byte("[DONE]")) {
publishStreamError(statusErr{code: http.StatusBadGateway, msg: "upstream stream ended with incomplete data before [DONE]"}, false)
return true
}
}
}
dataPayload := bytes.TrimSpace(bytes.Join(dataLines, []byte("\n")))
isDone := bytes.Equal(dataPayload, []byte("[DONE]"))
if isDone && openAICompatErrorEvent(eventName) {
publishStreamError(statusErr{code: http.StatusBadGateway, msg: "upstream error event ended before [DONE]"}, false)
return true
}
if !isDone && !json.Valid(dataPayload) {
publishStreamError(statusErr{code: http.StatusBadGateway, msg: "upstream stream ended with incomplete SSE data frame"}, false)
return true
}
if !isDone {
if streamErr, isError := openAICompatStreamDataError(dataPayload, eventName); isError {
publishStreamError(streamErr, true)
return true
}
if !sawFinishReason && openAICompatChunkHasFinishReason(dataPayload) {
sawFinishReason = true
}
}
// Observe complete upstream frames before translation or downstream backpressure.
if !isDone && len(dataLines) > 1 {
streamUsage.ObserveOpenAIStream(dataPayload)
}
helps.ObserveChatTokenEvent(reporter, dataPayload)
streamLine := append([]byte("data: "), dataPayload...)
chunks := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translated, streamLine, &param, claudeInputTokens)
helps.RecordApplyPatchStreamFailure(ctx, param, reporter, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
for i := range chunks {
select {
case out <- cliproxyexecutor.StreamChunk{Payload: chunks[i]}:
if openAICompatChunkIsTerminal(responseFormat, chunks[i]) {
terminalDelivered = true
}
case <-ctx.Done():
streamAborted = true
return true
}
}
if helps.ApplyPatchTranslationError(param) != nil {
publishStreamError(statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}, false)
return true
}
if isDone {
seenDone = true
terminalDelivered = true
return true
}
return false
}
scanLoop:
for scanner.Scan() {
line := scanner.Bytes()
helps.AppendAPIResponseChunk(ctx, e.cfg, line)
streamUsage.ObserveOpenAIStream(line)
trimmedLine := bytes.TrimSpace(line)
if len(trimmedLine) == 0 {
if processFrame() {
break scanLoop
}
continue
}
if bytes.HasPrefix(trimmedLine, []byte("data:")) {
frameData = append(frameData, bytes.Clone(bytes.TrimSpace(trimmedLine[len("data:"):])))
continue
}
if bytes.HasPrefix(trimmedLine, []byte("event:")) {
upstreamEvent = strings.TrimSpace(string(trimmedLine[len("event:"):]))
continue
}
if bytes.HasPrefix(trimmedLine, []byte(":")) || bytes.HasPrefix(trimmedLine, []byte("id:")) || bytes.HasPrefix(trimmedLine, []byte("retry:")) {
continue
}
if bytes.HasPrefix(trimmedLine, []byte("{")) || bytes.HasPrefix(trimmedLine, []byte("[")) {
publishStreamError(statusErr{code: http.StatusBadGateway, msg: string(trimmedLine)}, true)
break
}
}
errScan := scanner.Err()
if errScan == nil && !seenDone && !streamFailed && !streamAborted && len(frameData) > 0 {
_ = processFrame()
}
if !streamFailed && !streamAborted && helps.EndApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}) {
return
}
if !streamFailed && !streamAborted && !seenDone && errScan == nil && responseFormat == sdktranslator.FormatOpenAIResponse && ctx.Err() == nil && helps.CanFinalizeResponseStream(param) {
finalChunks := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translated, []byte("data: [DONE]"), &param, claudeInputTokens)
for _, finalChunk := range finalChunks {
select {
case out <- cliproxyexecutor.StreamChunk{Payload: finalChunk}:
case <-ctx.Done():
streamAborted = true
}
if streamAborted {
break
}
}
if len(finalChunks) > 0 && !streamAborted {
seenDone = true
}
}
if streamFailed || streamAborted {
return
}
if errScan != nil && !seenDone {
if terminalDelivered && openAICompatIsContextCancelErr(ctx, errScan) {
return
}
helps.RecordAPIResponseError(ctx, e.cfg, errScan)
if !streamUsage.PublishFailure(ctx, reporter, errScan) {
reporter.PublishFailure(ctx, errScan)
}
select {
case out <- cliproxyexecutor.StreamChunk{Err: errScan}:
case <-ctx.Done():
}
} else if !seenDone && !terminalDelivered {
if ctx.Err() != nil {
return
}
// Without a translator-confirmed terminal state, a clean Responses or Claude EOF
// without [DONE] remains a failed stream instead of completing it.
if responseFormat == sdktranslator.FormatOpenAIResponse || (responseFormat == sdktranslator.FormatClaude && !sawFinishReason) {
streamErr := statusErr{code: http.StatusBadGateway, msg: "upstream stream closed before [DONE]", streamTruncated: true}
helps.RecordAPIResponseError(ctx, e.cfg, streamErr)
if !helps.RecordApplyPatchStreamFailureWithUsage(ctx, param, reporter, &streamUsage, streamErr) {
if !streamUsage.PublishFailure(ctx, reporter, streamErr) {
reporter.PublishFailure(ctx, streamErr)
}
}
select {
case out <- cliproxyexecutor.StreamChunk{Err: streamErr}:
case <-ctx.Done():
}
return
}
// Other protocols retain compatibility with providers that omit [DONE].
chunks := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translated, []byte("data: [DONE]"), &param, claudeInputTokens)
helps.RecordApplyPatchStreamFailure(ctx, param, reporter, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
for i := range chunks {
select {
case out <- cliproxyexecutor.StreamChunk{Payload: chunks[i]}:
case <-ctx.Done():
streamAborted = true
return
}
}
}
// Ensure we record the request if no usage chunk was ever seen.
streamUsage.Publish(ctx, reporter)
reporter.EnsurePublished(ctx)
}()
return &cliproxyexecutor.StreamResult{Headers: httpResp.Header.Clone(), Chunks: out}, nil
}
func (e *OpenAICompatExecutor) executeImagesStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, endpointPath string) (_ *cliproxyexecutor.StreamResult, err error) {
baseModel := thinking.ParseSuffix(req.Model).ModelName
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
defer reporter.TrackFailure(ctx, &err)
baseURL, apiKey := e.resolveCredentials(auth)
if baseURL == "" {
err = statusErr{code: http.StatusUnauthorized, msg: "missing provider baseURL"}
return nil, err
}
payload, contentType, errPrepare := prepareOpenAICompatImagesPayload(req.Payload, baseModel, opts.Headers.Get("Content-Type"), true)
if errPrepare != nil {
err = errPrepare
return nil, err
}
if contentType == "" {
contentType = "application/json"
}
payload, contentType, errPrepare = helps.ApplyMediaPayloadConfig(e.cfg, e.Identifier(), baseModel, "openai", payload, contentType, req, opts)
if errPrepare != nil {
return nil, errPrepare
}
reporter.SetTranslatedReasoningEffort(payload, "openai")
url := strings.TrimSuffix(baseURL, "/") + endpointPath
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", contentType)
httpReq.Header.Set("Accept", "text/event-stream")
httpReq.Header.Set("Cache-Control", "no-cache")
if apiKey != "" {
httpReq.Header.Set("Authorization", "Bearer "+apiKey)
}
ensureOpenAICompatUserAgent(httpReq, opts.Headers)
var attrs map[string]string
if auth != nil {
attrs = auth.Attributes
}
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
var authID, authLabel, authType, authValue string
if auth != nil {
authID = auth.ID
authLabel = auth.Label
authType, authValue = auth.AccountInfo()
}
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
URL: url,
Method: http.MethodPost,
Headers: httpReq.Header.Clone(),
Body: payload,
Provider: e.Identifier(),
AuthID: authID,
AuthLabel: authLabel,
AuthType: authType,
AuthValue: authValue,
})
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
httpClient = reporter.TrackHTTPClient(httpClient)
httpResp, err := httpClient.Do(httpReq)
if err != nil {
helps.RecordAPIResponseError(ctx, e.cfg, err)
return nil, err
}
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
body, errRead := io.ReadAll(httpResp.Body)
if errClose := httpResp.Body.Close(); errClose != nil {
log.Errorf("openai compat executor: close response body error: %v", errClose)
}
helps.AppendAPIResponseChunk(ctx, e.cfg, body)
if errRead != nil {
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
return nil, errRead
}
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), body))
return nil, statusErr{code: httpResp.StatusCode, msg: string(body)}
}
out := make(chan cliproxyexecutor.StreamChunk)
go func() {
defer close(out)
var streamUsage helps.StreamUsageBuffer
streamUsage.SetRequestPayload(payload, contentType)
usageObserver := helps.NewOpenAIStreamUsageObserver(&streamUsage, streamScannerBuffer)
observer := helps.NewStreamResponseModelObserver(reporter)
requestedImages := openAICompatRequestedImageCount(payload, contentType)
var streamCompleted bool
var streamFailed bool
defer func() {
observer.Finish()
if errClose := httpResp.Body.Close(); errClose != nil {
log.Errorf("openai compat executor: close response body error: %v", errClose)
}
usageObserver.Finish()
if errCtx := ctx.Err(); errCtx != nil && !streamCompleted && !streamFailed {
if !streamUsage.PublishFailure(ctx, reporter, errCtx) {
reporter.PublishFailure(ctx, errCtx)
}
return
}
streamUsage.Publish(ctx, reporter)
reporter.EnsurePublished(ctx)
}()
buffer := make([]byte, 32*1024)
for {
n, errRead := httpResp.Body.Read(buffer)
if n > 0 {
chunk := bytes.Clone(buffer[:n])
helps.AppendAPIResponseChunk(ctx, e.cfg, chunk)
observer.Feed(chunk)
usageObserver.Feed(chunk)
select {
case out <- cliproxyexecutor.StreamChunk{Payload: chunk}:
if detail, okDetail := streamUsage.Detail(); okDetail && detail.ImageCount != nil && *detail.ImageCount >= requestedImages {
streamCompleted = true
}
case <-ctx.Done():
return
}
}
if errRead != nil {
if errRead != io.EOF {
if streamCompleted && openAICompatIsContextCancelErr(ctx, errRead) {
return
}
streamFailed = true
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
observer.Finish()
usageObserver.Finish()
if !streamUsage.PublishFailure(ctx, reporter, errRead) {
reporter.PublishFailure(ctx, errRead)
}
select {
case out <- cliproxyexecutor.StreamChunk{Err: errRead}:
case <-ctx.Done():
}
} else if ctx.Err() == nil {
streamCompleted = true
}
return
}
}
}()
return &cliproxyexecutor.StreamResult{Headers: httpResp.Header.Clone(), Chunks: out}, nil
}
func (e *OpenAICompatExecutor) executeOpenAICompatResponsesCompaction(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, baseURL, apiKey string, compat *config.OpenAICompatibility) (cliproxyexecutor.Response, error) {
var resp cliproxyexecutor.Response
baseModel := thinking.ParseSuffix(req.Model).ModelName
summaryPayload := helps.PrepareAntigravityCompactionSummaryPayload(helps.ResponsesCompactionSourcePayload(req, opts), baseModel)
summaryReq := req
summaryReq.Payload = summaryPayload
summaryOpts := opts
summaryOpts.Alt = ""
summaryOpts.Stream = false
summaryOpts.OriginalRequest = nil
summaryOpts.SourceFormat = sdktranslator.FormatOpenAIResponse
summaryOpts.ResponseFormat = sdktranslator.FormatOpenAIResponse
summaryResp, errSummary := e.executeWithCredentials(ctx, auth, summaryReq, summaryOpts, baseURL, apiKey, compat)
if errSummary != nil {
return resp, errSummary
}
respPayload, errBuild := helps.BuildResponsesCompactionResponseFromSummary(baseModel, summaryResp.Payload)
if errBuild != nil {
return resp, statusErr{code: http.StatusBadGateway, msg: errBuild.Error()}
}
return cliproxyexecutor.Response{
Payload: respPayload,
Headers: summaryResp.Headers,
}, nil
}
func (e *OpenAICompatExecutor) executeOpenAICompatResponsesCompactionStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, baseURL, apiKey string, compat *config.OpenAICompatibility) (*cliproxyexecutor.StreamResult, error) {
baseModel := thinking.ParseSuffix(req.Model).ModelName
summaryResp, errSummary := e.executeOpenAICompatResponsesCompaction(ctx, auth, req, opts, baseURL, apiKey, compat)
if errSummary != nil {
return nil, errSummary
}
chunks, errChunks := helps.BuildResponsesCompactionStreamChunksFromResponse(baseModel, summaryResp.Payload)
if errChunks != nil {
return nil, statusErr{code: http.StatusBadGateway, msg: errChunks.Error()}
}
out := make(chan cliproxyexecutor.StreamChunk, len(chunks))
for _, chunk := range chunks {
out <- cliproxyexecutor.StreamChunk{Payload: chunk}
}
close(out)
headers := summaryResp.Headers.Clone()
if headers == nil {
headers = make(http.Header)
}
headers.Set("Content-Type", "text/event-stream")
return &cliproxyexecutor.StreamResult{Headers: headers, Chunks: out}, nil
}
func (e *OpenAICompatExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
if opts.SourceFormat == sdktranslator.FormatOpenAIResponse && opts.Alt != "responses/compact" {
if errExpand := helps.ExpandResponsesCompactionCapsules(&req, &opts, e.Identifier()); errExpand != nil {
return cliproxyexecutor.Response{}, statusErr{code: http.StatusBadRequest, msg: errExpand.Error()}
}
}
baseModel := thinking.ParseSuffix(req.Model).ModelName
from := opts.SourceFormat
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
to := sdktranslator.FromString("openai")
isCompat := helps.APIKeyModelIsCompat(req)
translated, updatesChanged, err := helps.TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntent(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, false, isCompat)
if err != nil {
return cliproxyexecutor.Response{}, err
}
originalTranslatedForPayload := append([]byte(nil), translated...)
if len(opts.OriginalRequest) > 0 {
originalTranslatedForPayload, _, _ = helps.TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntent(ctx, opts.Headers, e.cfg, from, to, baseModel, opts.OriginalRequest, false, isCompat)
}
modelForCounting := baseModel
doc := ojson.NewDoc(translated)
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged); err != nil {
return cliproxyexecutor.Response{}, err
}
ctxWithFinalizer := helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, e.Identifier(), baseModel, to.String(), "", originalTranslatedForPayload, req, opts))
helps.FinalizePayloadDoc(ctxWithFinalizer, doc)
translated = doc.Bytes()
enc, err := helps.TokenizerForModel(modelForCounting)
if err != nil {
return cliproxyexecutor.Response{}, fmt.Errorf("openai compat executor: tokenizer init failed: %w", err)
}
count, err := helps.CountOpenAIChatTokens(enc, translated)
if err != nil {
return cliproxyexecutor.Response{}, fmt.Errorf("openai compat executor: token counting failed: %w", err)
}
usageJSON := helps.BuildOpenAIUsageJSON(count)
translatedUsage := sdktranslator.TranslateTokenCount(ctx, to, responseFormat, count, usageJSON)
return cliproxyexecutor.Response{Payload: translatedUsage}, nil
}
// Refresh is a no-op for API-key based compatibility providers.
// OAuth-style credentials with a refresh token cannot be rotated here; callers
// that need plugin/Home refresh must bind a refresh-capable executor instead.
func (e *OpenAICompatExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) {
log.Debugf("openai compat executor: refresh called")
if refreshed, handled, err := helps.RefreshAuthViaHome(ctx, e.cfg, auth); handled {
return refreshed, err
}
if openAICompatAuthHasRefreshToken(auth) {
provider := ""
if e != nil {
provider = e.Identifier()
}
if provider == "" && auth != nil {
provider = strings.TrimSpace(auth.Provider)
}
return nil, fmt.Errorf("openai compat executor cannot refresh oauth credentials for provider %s", provider)
}
return auth, nil
}
func openAICompatAuthHasRefreshToken(auth *cliproxyauth.Auth) bool {
if auth == nil || auth.Metadata == nil {
return false
}
if token, _ := auth.Metadata["refresh_token"].(string); strings.TrimSpace(token) != "" {
return true
}
if token, _ := auth.Metadata["refreshToken"].(string); strings.TrimSpace(token) != "" {
return true
}
return false
}
func openAICompatIsContextCancelErr(ctx context.Context, err error) bool {
if err == nil {
return false
}
if errors.Is(err, context.Canceled) {
return true
}
if ctx != nil && errors.Is(ctx.Err(), context.Canceled) {
if cause := context.Cause(ctx); cause != nil && errors.Is(err, cause) {
return true
}
}
return false
}
func openAICompatChunkIsTerminal(responseFormat sdktranslator.Format, chunk []byte) bool {
switch responseFormat {
case sdktranslator.FormatClaude:
return bytes.Contains(chunk, []byte(`"type":"message_stop"`))
case sdktranslator.FormatOpenAIResponse:
return bytes.Contains(chunk, []byte(`"type":"response.completed"`)) || bytes.Contains(chunk, []byte(`"type":"response.incomplete"`))
default:
return false
}
}
func openAICompatRequestedImageCount(payload []byte, contentType string) int64 {
if len(payload) == 0 {
return 1
}
if json.Valid(payload) {
if n := ojson.GetBytesNoCopy(payload, "n").Int(); n > 1 {
return n
}
return 1
}
mediaType, params, errParse := mime.ParseMediaType(contentType)
if errParse != nil || !strings.HasPrefix(strings.ToLower(strings.TrimSpace(mediaType)), "multipart/") {
return 1
}
boundary := strings.TrimSpace(params["boundary"])
if boundary == "" {
return 1
}
reader := multipart.NewReader(bytes.NewReader(payload), boundary)
for {
part, errNext := reader.NextPart()
if errNext != nil {
break
}
if part.FormName() == "n" {
raw, errRead := io.ReadAll(io.LimitReader(part, 32))
_ = part.Close()
if errRead == nil {
if n, errConv := strconv.ParseInt(strings.TrimSpace(string(raw)), 10, 64); errConv == nil && n > 1 {
return n
}
}
return 1
}
_ = part.Close()
}
return 1
}
func openAICompatImageEndpointPath(opts cliproxyexecutor.Options) string {
if opts.SourceFormat.String() != openAICompatImageHandlerType {
return ""
}
path := helps.PayloadRequestPath(opts)
if strings.HasSuffix(path, "/images/edits") {
return openAICompatImagesEditsPath
}
if strings.HasSuffix(path, "/images/generations") {
return openAICompatImagesGenerationsPath
}
return openAICompatDefaultImageEndpoint
}
func prepareOpenAICompatImagesPayload(payload []byte, model string, contentType string, stream bool) ([]byte, string, error) {
model = strings.TrimSpace(model)
contentType = strings.TrimSpace(contentType)
if json.Valid(payload) {
doc := ojson.NewDoc(payload)
if model != "" {
helps.SetStringIfDifferentDoc(doc, "model", model)
}
if stream {
helps.SetBoolIfDifferentDoc(doc, "stream", true)
} else {
_ = doc.Delete("stream")
}
return doc.Bytes(), "application/json", nil
}
mediaType, params, errParse := mime.ParseMediaType(contentType)
if errParse != nil || !strings.HasPrefix(strings.ToLower(strings.TrimSpace(mediaType)), "multipart/") {
return payload, contentType, nil
}
boundary := strings.TrimSpace(params["boundary"])
if boundary == "" {
return nil, "", fmt.Errorf("multipart boundary is missing")
}
return rewriteOpenAICompatImagesMultipartPayload(payload, model, boundary, stream)
}
func cloneOpenAICompatMIMEHeader(src textproto.MIMEHeader) textproto.MIMEHeader {
dst := make(textproto.MIMEHeader, len(src))
for key, values := range src {
dst[key] = append([]string(nil), values...)
}
return dst
}
func rewriteOpenAICompatImagesMultipartPayload(payload []byte, model string, boundary string, stream bool) ([]byte, string, error) {
reader := multipart.NewReader(bytes.NewReader(payload), boundary)
form, errRead := reader.ReadForm(openAICompatMultipartMemory)
if errRead != nil {
return nil, "", fmt.Errorf("read multipart form failed: %w", errRead)
}
defer func() {
if errRemove := form.RemoveAll(); errRemove != nil {
log.Errorf("openai compat executor: remove multipart form files error: %v", errRemove)
}
}()
var body bytes.Buffer
writer := multipart.NewWriter(&body)
if model != "" {
if errWrite := writer.WriteField("model", model); errWrite != nil {
return nil, "", fmt.Errorf("write model field failed: %w", errWrite)
}
}
if stream {
if errWrite := writer.WriteField("stream", "true"); errWrite != nil {
return nil, "", fmt.Errorf("write stream field failed: %w", errWrite)
}
}
for key, values := range form.Value {
if key == "model" || key == "stream" {
continue
}
for _, value := range values {
if errWrite := writer.WriteField(key, value); errWrite != nil {
return nil, "", fmt.Errorf("write form field %s failed: %w", key, errWrite)
}
}
}
for key, files := range form.File {
for _, fileHeader := range files {
if fileHeader == nil {
continue
}
header := cloneOpenAICompatMIMEHeader(fileHeader.Header)
header.Set("Content-Disposition", multipart.FileContentDisposition(key, fileHeader.Filename))
if header.Get("Content-Type") == "" {
header.Set("Content-Type", "application/octet-stream")
}
part, errCreate := writer.CreatePart(header)
if errCreate != nil {
return nil, "", fmt.Errorf("create file field %s failed: %w", key, errCreate)
}
src, errOpen := fileHeader.Open()
if errOpen != nil {
return nil, "", fmt.Errorf("open upload file failed: %w", errOpen)
}
_, errCopy := io.Copy(part, src)
if errClose := src.Close(); errClose != nil {
log.Errorf("openai compat executor: close upload file error: %v", errClose)
if errCopy == nil {
errCopy = errClose
}
}
if errCopy != nil {
return nil, "", fmt.Errorf("copy upload file failed: %w", errCopy)
}
}
}
if errClose := writer.Close(); errClose != nil {
return nil, "", fmt.Errorf("close multipart writer failed: %w", errClose)
}
return body.Bytes(), writer.FormDataContentType(), nil
}
func (e *OpenAICompatExecutor) applyPromptCacheKey(ctx context.Context, auth *cliproxyauth.Auth, from sdktranslator.Format, baseModel string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, translated []byte) ([]byte, error) {
return e.applyPromptCacheKeyWithCompat(ctx, auth, e.resolveCompatConfig(auth, req), from, baseModel, req, opts, translated)
}
func (e *OpenAICompatExecutor) applyPromptCacheKeyWithCompat(ctx context.Context, auth *cliproxyauth.Auth, compat *config.OpenAICompatibility, from sdktranslator.Format, baseModel string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, translated []byte) ([]byte, error) {
if compat == nil || !compat.SupportPromptCacheKey {
return translated, nil
}
doc := ojson.NewDoc(translated)
if errCacheKey := e.applyPromptCacheKeyWithCompatDoc(ctx, auth, compat, from, baseModel, req, opts, doc); errCacheKey != nil {
return translated, errCacheKey
}
return doc.Bytes(), nil
}
func (e *OpenAICompatExecutor) applyPromptCacheKeyWithCompatDoc(ctx context.Context, auth *cliproxyauth.Auth, compat *config.OpenAICompatibility, from sdktranslator.Format, baseModel string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) error {
if doc == nil || compat == nil || !compat.SupportPromptCacheKey {
return nil
}
for _, payload := range [][]byte{req.Payload, opts.OriginalRequest} {
if promptCacheKey := strings.TrimSpace(ojson.GetBytesNoCopy(payload, "prompt_cache_key").String()); promptCacheKey != "" {
helps.SetStringIfDifferentDoc(doc, "prompt_cache_key", promptCacheKey)
return nil
}
}
if promptCacheKey := strings.TrimSpace(doc.Get("prompt_cache_key").String()); promptCacheKey != "" {
helps.SetStringIfDifferentDoc(doc, "prompt_cache_key", promptCacheKey)
return nil
}
modelName := strings.TrimSpace(doc.Get("model").String())
if modelName == "" {
modelName = baseModel
}
if sourceFormatEqual(from, sdktranslator.FormatClaude) {
cached, ok, errCache := helps.ClaudeCodePromptCache(ctx, modelName, req.Payload, opts.Headers)
if errCache != nil {
return errCache
}
if ok {
helps.SetStringIfDifferentDoc(doc, "prompt_cache_key", cached.ID)
return nil
}
}
sessionID := helps.ProviderSessionUUID(e.provider, opts.Metadata, req.Metadata)
if sessionID == "" {
return nil
}
provider := strings.TrimSpace(e.provider)
if provider == "" {
provider = strings.TrimSpace(compat.Name)
}
identity := strings.Join([]string{
"cli-proxy-api:openai-compat:prompt-cache",
strings.ToLower(provider),
strings.ToLower(modelName),
strings.ToLower(strings.TrimSpace(from.String())),
sessionID,
}, "\x00")
promptCacheKey := uuid.NewSHA1(uuid.NameSpaceOID, []byte(identity)).String()
helps.SetStringIfDifferentDoc(doc, "prompt_cache_key", promptCacheKey)
return nil
}
func (e *OpenAICompatExecutor) resolveCredentials(auth *cliproxyauth.Auth) (baseURL, apiKey string) {
if auth == nil {
return "", ""
}
if auth.Attributes != nil {
baseURL = strings.TrimSpace(auth.Attributes["base_url"])
apiKey = strings.TrimSpace(auth.Attributes["api_key"])
}
return
}
func (e *OpenAICompatExecutor) resolveCompatConfig(auth *cliproxyauth.Auth, req cliproxyexecutor.Request) *config.OpenAICompatibility {
if auth == nil || e.cfg == nil {
return nil
}
if e.cfg.Home.Enabled {
// Home excludes provider credentials from the global configuration. These
// non-secret options belong to the credential selected for this attempt.
var options struct {
SupportPromptCacheKey bool `json:"support-prompt-cache-key"`
Models []config.OpenAICompatibilityModel `json:"models"`
}
present := false
if rawOptions, exists := auth.Metadata["credential_options"]; exists {
if data, errMarshal := json.Marshal(rawOptions); errMarshal == nil {
present = json.Unmarshal(data, &options) == nil
}
}
if raw, exists := auth.Attributes["support_prompt_cache_key"]; exists {
if value, errParse := strconv.ParseBool(raw); errParse == nil {
options.SupportPromptCacheKey = value
present = true
}
}
if model, ok := cliproxyauth.ResolvedHomeModelOptions(req); ok {
options.Models = []config.OpenAICompatibilityModel{model}
present = true
}
if present {
return &config.OpenAICompatibility{
Name: auth.Attributes["compat_name"],
SupportPromptCacheKey: options.SupportPromptCacheKey,
Models: options.Models,
}
}
}
if auth.AuthSourceKind() == cliproxyauth.AuthSourceConfig && auth.Attributes != nil {
if rawIndex := strings.TrimSpace(auth.Attributes["config_index"]); rawIndex != "" {
configIndex, errIndex := strconv.Atoi(rawIndex)
if errIndex == nil && configIndex >= 0 && configIndex < len(e.cfg.OpenAICompatibility) {
compat := &e.cfg.OpenAICompatibility[configIndex]
if !compat.Disabled {
return compat
}
}
}
}
candidates := make([]string, 0, 3)
if auth.Attributes != nil {
if v := strings.TrimSpace(auth.Attributes["compat_name"]); v != "" {
candidates = append(candidates, v)
}
if v := strings.TrimSpace(auth.Attributes["provider_key"]); v != "" {
candidates = append(candidates, v)
}
}
if v := strings.TrimSpace(auth.Provider); v != "" {
candidates = append(candidates, v)
}
for i := range e.cfg.OpenAICompatibility {
compat := &e.cfg.OpenAICompatibility[i]
if compat.Disabled {
continue
}
for _, candidate := range candidates {
if candidate != "" && strings.EqualFold(strings.TrimSpace(candidate), compat.Name) {
return compat
}
}
}
return nil
}
func (e *OpenAICompatExecutor) overrideModel(payload []byte, model string) []byte {
if len(payload) == 0 || model == "" {
return payload
}
return helps.SetStringIfDifferent(payload, "model", model)
}
func openAICompatErrorEvent(eventName string) bool {
return strings.EqualFold(eventName, "error") || strings.EqualFold(eventName, "response.error") || strings.EqualFold(eventName, "response.failed")
}
func openAICompatChunkHasFinishReason(payload []byte) bool {
for _, choice := range ojson.GetBytesNoCopy(payload, "choices").Array() {
if reason := choice.Get("finish_reason"); reason.Exists() && reason.Type != ojson.Null && strings.TrimSpace(reason.String()) != "" {
return true
}
}
return false
}
func openAICompatStreamDataError(payload []byte, eventName string) (statusErr, bool) {
if len(payload) == 0 || !json.Valid(payload) {
return statusErr{}, false
}
payloadType := ojson.GetBytesNoCopy(payload, "type").String()
hasError := false
for _, path := range []string{"error", "response.error"} {
errorNode := ojson.GetBytesNoCopy(payload, path)
if errorNode.Exists() && errorNode.Raw != "null" {
hasError = true
break
}
}
hasTopLevelErrorFields := ojson.GetBytesNoCopy(payload, "code").Exists() && ojson.GetBytesNoCopy(payload, "message").Exists()
if !hasError && !strings.EqualFold(payloadType, "error") && !strings.EqualFold(payloadType, "response.error") && !strings.EqualFold(payloadType, "response.failed") &&
!openAICompatErrorEvent(eventName) && !hasTopLevelErrorFields {
return statusErr{}, false
}
status := 0
for _, path := range []string{"status", "status_code", "error.status", "error.status_code", "response.error.status", "response.error.status_code"} {
status = int(ojson.GetBytesNoCopy(payload, path).Int())
if status >= http.StatusBadRequest && status <= 599 {
break
}
}
if status < http.StatusBadRequest || status > 599 {
status = http.StatusBadGateway
}
return statusErr{code: status, msg: string(payload)}, true
}
type statusErr struct {
code int
msg string
cause error
retryAfter *time.Duration
requestScoped bool
requestStop bool
credentialScoped bool
unauthorizedRefreshAttempted bool
alternateTokenUnauthorized bool
unclassifiedUnauthorized bool
terminalUnauthorized bool
streamTruncated bool
transientRateLimit bool
transientTransport bool
tosViolation bool
validationRequired bool
tosPoolPaused bool
tosAppealURL string
tosAppealURLText string
// diagnostic is a content-free detail for logs only; it never reaches clients.
diagnostic string
}
func (e statusErr) Error() string {
if e.msg != "" {
return e.msg
}
return fmt.Sprintf("status %d", e.code)
}
func (e statusErr) Unwrap() error { return e.cause }
func (e statusErr) StatusCode() int { return e.code }
func (e statusErr) RetryAfter() *time.Duration { return e.retryAfter }
func (e statusErr) IsRequestScoped() bool { return e.requestScoped }
func (e statusErr) IsRequestStop() bool { return e.requestStop }
func (e statusErr) IsCredentialScoped() bool { return e.credentialScoped }
func (e statusErr) IsUnauthorizedRefreshAttempted() bool { return e.unauthorizedRefreshAttempted }
func (e statusErr) IsAlternateTokenUnauthorized() bool { return e.alternateTokenUnauthorized }
func (e statusErr) IsUnclassifiedUnauthorized() bool { return e.unclassifiedUnauthorized }
func (e statusErr) IsTerminalUnauthorized() bool { return e.terminalUnauthorized }
// LogDiagnostic lets upstream failure logs explain a sanitized client message.
func (e statusErr) LogDiagnostic() string { return e.diagnostic }
// IsStreamTruncated marks a locally synthesized failure for an accepted
// upstream stream that ended without its terminal frame.
func (e statusErr) IsStreamTruncated() bool { return e.streamTruncated }
// IsTransientRateLimit marks a transient per-model capacity/admission 429
// that must not be recorded as credential quota exhaustion.
func (e statusErr) IsTransientRateLimit() bool { return e.transientRateLimit }
// IsTerminalTOSViolation marks a 403 PERMISSION_DENIED carrying an ErrorInfo
// reason=TOS_VIOLATION. The conductor must deactivate the credential rather
// than cool it, so sibling credentials are not hammered during a sweep.
func (e statusErr) IsTerminalTOSViolation() bool { return e.tosViolation }
// IsAntigravityTOSPoolPaused marks the local protective 503 error returned
// while the Antigravity pool is paused after a TOS_VIOLATION.
func (e statusErr) IsAntigravityTOSPoolPaused() bool { return e.tosPoolPaused }
// TOSAppealMetadata exposes the operator-facing appeal detail extracted from
// the ErrorInfo metadata so the conductor can persist it on the auth record.
func (e statusErr) TOSAppealMetadata() map[string]string {
if !e.tosViolation || (e.tosAppealURL == "" && e.tosAppealURLText == "") {
return nil
}
meta := map[string]string{
"tos_appeal_url": e.tosAppealURL,
"tos_appeal_url_text": e.tosAppealURLText,
}
return meta
}
// ErrorCode exposes the machine-readable classification for terminal TOS
// violations or transient non-credential failures so MarkResult routes them
// to the appropriate credential state handler.
func (e statusErr) ErrorCode() string {
if e.tosViolation {
return "tos_violation"
}
if e.validationRequired {
return "validation_required"
}
if e.tosPoolPaused {
return cliproxyauth.ErrorCodeAntigravityTOSPoolPaused
}
if e.transientTransport {
return cliproxyauth.ErrorCodeTransientTransport
}
return ""
}
// newGeminiZeroOutputStatusErr builds a 502 error tagged with
// ErrorCodeTransientTransport so Gemini-family zero-output failures allow
// per-request rotation and failover without cooling healthy credentials.
func newGeminiZeroOutputStatusErr(msg string) statusErr {
return statusErr{
code: http.StatusBadGateway,
msg: msg,
transientTransport: true,
}
}
// newGeminiTranslatedClaudeErrorStatusErr preserves ErrorCodeTransientTransport
// when a translated Claude error payload originates from zero-output
// MALFORMED_FUNCTION_CALL (for instance when groundingChunks bypassed the
// executor zero-output guard), while leaving all other errors coolable.
func newGeminiTranslatedClaudeErrorStatusErr(msg string) statusErr {
if strings.EqualFold(strings.TrimSpace(ojson.Get(msg, "type").String()), "error") &&
strings.Contains(ojson.Get(msg, "error.message").String(), "MALFORMED_FUNCTION_CALL") {
return newGeminiZeroOutputStatusErr(msg)
}
return statusErr{code: http.StatusBadGateway, msg: msg}
}
const openAICompatTPMFallbackRetryAfter = time.Minute
func newOpenAICompatStatusError(status int, headers http.Header, body []byte) statusErr {
return statusErr{
code: status,
msg: string(body),
retryAfter: openAICompatRetryAfter(status, headers, body, time.Now()),
}
}
// openAICompatRetryAfter preserves the provider's standard Retry-After signal.
// Some OpenAI-compatible providers omit that header for explicit per-minute
// token limits; in that narrow case a one-minute fallback prevents immediate
// replay of the same large request while keeping the retry wait bounded.
// parseRetryAfterHeader parses an HTTP Retry-After header in either the
// delay-seconds or HTTP-date form. A present, valid value always yields a
// non-nil result so that an explicit immediate retry (0 seconds or a past
// date) stays distinguishable from an absent hint; missing or malformed
// values yield nil and let body-derived hints apply.
func parseRetryAfterHeader(headers http.Header, now time.Time) *time.Duration {
if headers == nil {
return nil
}
raw := strings.TrimSpace(headers.Get("Retry-After"))
if raw == "" {
return nil
}
if seconds, errParse := strconv.ParseInt(raw, 10, 64); errParse == nil && seconds >= 0 {
if seconds > int64(math.MaxInt64)/int64(time.Second) {
// Out of representable range: clamp to the largest duration so
// far-future deadlines only ever over-report recovery.
delay := time.Duration(math.MaxInt64)
return &delay
}
delay := time.Duration(seconds) * time.Second
return &delay
}
if deadline, errParse := http.ParseTime(raw); errParse == nil {
delay := deadline.Sub(now)
if delay < 0 {
delay = 0
}
return &delay
}
return nil
}
func openAICompatRetryAfter(status int, headers http.Header, body []byte, now time.Time) *time.Duration {
if status != http.StatusTooManyRequests {
return nil
}
if retryAfter := parseRetryAfterHeader(headers, now); retryAfter != nil {
return retryAfter
}
code := strings.ToLower(strings.TrimSpace(ojson.GetBytesNoCopy(body, "error.code").String()))
message := strings.ToLower(strings.TrimSpace(ojson.GetBytesNoCopy(body, "error.message").String()))
if strings.Contains(code, "tpmratelimitexceeded") ||
(strings.Contains(message, "tokens per minute") && strings.Contains(message, "limit") && strings.Contains(message, "exceeded")) {
delay := openAICompatTPMFallbackRetryAfter
return &delay
}
return nil
}
// SupportsApplyPatch reports the actual executor contract, independent of its provider name.
func (e *OpenAICompatExecutor) SupportsApplyPatch() bool { return e != nil }