mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
Codex caches the /models catalog together with its ETag and, on every Responses WebSocket turn, compares that ETag with the x-models-etag header of the codex.response.metadata event. Upstream reports the ETag of its own catalog, but the gateway serves a group-specific manifest whose ETag changes as soon as group policy adjusts it, so Codex re-downloaded the full catalog on every turn. Record, per group, which served ETag corresponds to each upstream catalog ETag, and translate the metadata header when relaying WebSocket events in both ctx_pool and passthrough modes. Unknown values are removed so clients fall back to their regular catalog cache TTL. Pinned manifests keep the upstream ETags of every merged account, and generated manifests now use weak ETags so the value still matches behind a compressing reverse proxy.
164 lines
6.2 KiB
Go
164 lines
6.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
|
|
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
)
|
|
|
|
// CodexModels serves the Codex models manifest for Codex clients.
|
|
//
|
|
// Codex CLI and the Codex desktop app refresh their model picker from
|
|
// GET {base_url}/models?client_version=... (custom provider mode) or
|
|
// GET /backend-api/codex/models (chatgpt_base_url mode). Both routes land
|
|
// here. Pinned discovery takes precedence over local account model mappings;
|
|
// when disabled, groups with explicit mappings are generated locally;
|
|
// otherwise ChatGPT manifests are proxied verbatim and custom API key manifests
|
|
// receive provider-compatibility normalization plus short-lived caching.
|
|
func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) {
|
|
if c.Request.Context().Err() != nil {
|
|
return
|
|
}
|
|
apiKey, ok := middleware2.GetAPIKeyFromContext(c)
|
|
if !ok || apiKey.Group == nil {
|
|
h.errorResponse(c, http.StatusUnauthorized, "invalid_request_error", "API key group is required")
|
|
return
|
|
}
|
|
if apiKey.Group.Platform != service.PlatformOpenAI && apiKey.Group.Platform != service.PlatformComposite {
|
|
h.errorResponse(c, http.StatusNotFound, "not_found_error", "Codex models manifest is only available for OpenAI and Composite groups")
|
|
return
|
|
}
|
|
|
|
ifNoneMatch := c.GetHeader("If-None-Match")
|
|
// 固定账号分支:开启后只用选定账号拉取 manifest,不经过调度器;
|
|
// 全部不可用/全部失败时按 FallbackToScheduler 决定回退调度器或返回错误。
|
|
if apiKey.Group.Platform == service.PlatformOpenAI &&
|
|
apiKey.Group.CodexModelsManifestConfig.Enabled {
|
|
pinnedManifest, pinnedAccount, pinnedErr := h.gatewayService.FetchPinnedCodexModelsManifest(
|
|
c.Request.Context(),
|
|
apiKey.Group,
|
|
c.Query("client_version"),
|
|
)
|
|
if pinnedErr != nil {
|
|
if c.Request.Context().Err() != nil {
|
|
return
|
|
}
|
|
if !apiKey.Group.CodexModelsManifestConfig.FallbackToScheduler {
|
|
if errors.Is(pinnedErr, service.ErrNoPinnedCodexModelsAccounts) {
|
|
h.errorResponse(c, http.StatusServiceUnavailable, "upstream_error", "No available pinned OpenAI accounts")
|
|
return
|
|
}
|
|
h.errorResponse(c, infraerrors.Code(pinnedErr), "upstream_error", infraerrors.Message(pinnedErr))
|
|
return
|
|
}
|
|
// 回退开启:跌入下方调度器循环。
|
|
} else {
|
|
// 让 ops 错误日志携带实际拉取成功的首个固定账号。
|
|
setOpsSelectedAccount(c, pinnedAccount.ID, pinnedAccount.Platform)
|
|
if err := h.gatewayService.MergeGroupConfiguredCodexModels(c.Request.Context(), apiKey.Group, pinnedManifest, ifNoneMatch); err != nil {
|
|
h.errorResponse(c, http.StatusInternalServerError, "api_error", "Failed to build Codex models manifest")
|
|
return
|
|
}
|
|
if c.Request.Context().Err() != nil {
|
|
return
|
|
}
|
|
h.writeCodexModelsResponse(c, apiKey.Group, pinnedManifest)
|
|
return
|
|
}
|
|
}
|
|
|
|
if !apiKey.Group.CodexModelsManifestConfig.Enabled {
|
|
configuredManifest, configured, err := h.gatewayService.BuildGroupConfiguredCodexModelsManifest(
|
|
c.Request.Context(),
|
|
apiKey.Group,
|
|
ifNoneMatch,
|
|
)
|
|
if err != nil {
|
|
if c.Request.Context().Err() != nil {
|
|
return
|
|
}
|
|
h.errorResponse(c, http.StatusInternalServerError, "api_error", "Failed to build Codex models manifest")
|
|
return
|
|
}
|
|
if configured {
|
|
h.writeCodexModelsResponse(c, apiKey.Group, configuredManifest)
|
|
return
|
|
}
|
|
}
|
|
|
|
maxAccountSwitches := h.maxAccountSwitches
|
|
if maxAccountSwitches <= 0 {
|
|
maxAccountSwitches = 3
|
|
}
|
|
failedAccountIDs := make(map[int64]struct{})
|
|
switchCount := 0
|
|
var lastUpstreamErr error
|
|
|
|
for {
|
|
account, err := h.gatewayService.SelectAccountForModelWithExclusions(c.Request.Context(), apiKey.GroupID, "", "", failedAccountIDs)
|
|
if err != nil {
|
|
if c.Request.Context().Err() != nil {
|
|
return
|
|
}
|
|
if lastUpstreamErr != nil {
|
|
h.errorResponse(c, infraerrors.Code(lastUpstreamErr), "upstream_error", infraerrors.Message(lastUpstreamErr))
|
|
return
|
|
}
|
|
h.errorResponse(c, http.StatusServiceUnavailable, "upstream_error", "No available OpenAI accounts")
|
|
return
|
|
}
|
|
// 让 ops 错误日志携带实际选中的上游账号,便于定位失效账号(#4544)。
|
|
setOpsSelectedAccount(c, account.ID, account.Platform)
|
|
|
|
// The client ETag represents the final group-specific body, so fetch the
|
|
// source manifest before applying local filtering and alias metadata.
|
|
manifest, err := h.gatewayService.FetchCodexModelsManifest(c.Request.Context(), account, c.Query("client_version"), "")
|
|
if err != nil {
|
|
if c.Request.Context().Err() != nil {
|
|
return
|
|
}
|
|
if service.IsRetryableCodexModelsManifestError(err) && switchCount < maxAccountSwitches {
|
|
failedAccountIDs[account.ID] = struct{}{}
|
|
switchCount++
|
|
lastUpstreamErr = err
|
|
continue
|
|
}
|
|
h.errorResponse(c, infraerrors.Code(err), "upstream_error", infraerrors.Message(err))
|
|
return
|
|
}
|
|
if err := h.gatewayService.CompleteAPIKeyCodexModelsManifestForClient(manifest, account); err != nil {
|
|
h.errorResponse(c, http.StatusInternalServerError, "api_error", "Failed to complete Codex models manifest")
|
|
return
|
|
}
|
|
if err := service.ApplyPinnedCodexModelsMapping(manifest, account, apiKey.Group); err != nil {
|
|
h.errorResponse(c, http.StatusInternalServerError, "api_error", "Failed to apply model mappings")
|
|
return
|
|
}
|
|
if err := h.gatewayService.MergeGroupConfiguredCodexModels(c.Request.Context(), apiKey.Group, manifest, ifNoneMatch); err != nil {
|
|
h.errorResponse(c, http.StatusInternalServerError, "api_error", "Failed to build Codex models manifest")
|
|
return
|
|
}
|
|
if c.Request.Context().Err() != nil {
|
|
return
|
|
}
|
|
|
|
h.writeCodexModelsResponse(c, apiKey.Group, manifest)
|
|
return
|
|
}
|
|
}
|
|
|
|
// writeCodexModelsResponse records the ETag the group's manifest is served with,
|
|
// so codex.response.metadata events can be translated to it, and writes the
|
|
// manifest response.
|
|
func (h *OpenAIGatewayHandler) writeCodexModelsResponse(c *gin.Context, group *service.Group, manifest *service.OpenAIModelsResponse) {
|
|
if group != nil && h.gatewayService != nil {
|
|
h.gatewayService.RecordCodexModelsManifestETag(group.ID, manifest)
|
|
}
|
|
writeOpenAIModelsResponse(c, manifest)
|
|
}
|