mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
fix(openai): keep the local Codex models fallback inside the group allowlist
The fallback list now passes through FilterCodexModelIDsForGroup, the same listing filter /v1/models uses. A wildcard-only allowlist expands its patterns against account mappings and the default catalog instead of falling through to the unfiltered default catalog. Serving the fallback is logged with the upstream error, since clients only see a 200.
This commit is contained in:
1 parent
aede013903
commit
cff176dfd7
3 files changed
+68
-6
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
|
||||
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
|
||||
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
||||
@@ -109,7 +110,7 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if lastUpstreamErr != nil {
|
||||
if h.writeLocalCodexModelsFallback(c, apiKey.Group) {
|
||||
if h.writeLocalCodexModelsFallback(c, apiKey.Group, lastUpstreamErr) {
|
||||
return
|
||||
}
|
||||
h.errorResponse(c, infraerrors.Code(lastUpstreamErr), "upstream_error", infraerrors.Message(lastUpstreamErr))
|
||||
@@ -139,7 +140,7 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) {
|
||||
// upstream without a usable /models endpoint must not break the
|
||||
// client model picker while the group can describe its models
|
||||
// locally (#5252).
|
||||
if h.writeLocalCodexModelsFallback(c, apiKey.Group) {
|
||||
if h.writeLocalCodexModelsFallback(c, apiKey.Group, err) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -148,7 +149,7 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) {
|
||||
}
|
||||
// A 2xx manifest with no models is as unusable as a failed request for
|
||||
// Codex clients. Prefer the local catalog when one exists.
|
||||
if service.CodexModelsManifestIsEmpty(manifest.Body) && h.writeLocalCodexModelsFallback(c, apiKey.Group) {
|
||||
if service.CodexModelsManifestIsEmpty(manifest.Body) && h.writeLocalCodexModelsFallback(c, apiKey.Group, errors.New("upstream returned an empty models manifest")) {
|
||||
return
|
||||
}
|
||||
if err := h.gatewayService.CompleteAPIKeyCodexModelsManifestForClient(manifest, account); err != nil {
|
||||
@@ -175,18 +176,26 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) {
|
||||
// writeLocalCodexModelsFallback writes a locally generated Codex manifest when
|
||||
// the selected upstreams cannot serve /models. It reports false when the local
|
||||
// model list is empty so the caller can preserve the original upstream error.
|
||||
func (h *OpenAIGatewayHandler) writeLocalCodexModelsFallback(c *gin.Context, group *service.Group) bool {
|
||||
func (h *OpenAIGatewayHandler) writeLocalCodexModelsFallback(c *gin.Context, group *service.Group, upstreamErr error) bool {
|
||||
if h == nil || h.gatewayService == nil {
|
||||
return false
|
||||
}
|
||||
reqLog := requestLogger(c, "handler.openai_gateway.codex_models")
|
||||
manifest, configured, err := h.gatewayService.BuildLocalCodexModelsFallbackManifest(
|
||||
c.Request.Context(),
|
||||
group,
|
||||
c.GetHeader("If-None-Match"),
|
||||
)
|
||||
if err != nil || !configured || manifest == nil {
|
||||
if err != nil {
|
||||
reqLog.Warn("openai.codex_models_local_fallback_failed", zap.Error(err), zap.NamedError("upstream_error", upstreamErr))
|
||||
return false
|
||||
}
|
||||
if !configured || manifest == nil {
|
||||
return false
|
||||
}
|
||||
// The client gets a 200, so this is the only signal that the upstream
|
||||
// /models endpoint is unusable for the group.
|
||||
reqLog.Warn("openai.codex_models_local_fallback_served", zap.NamedError("upstream_error", upstreamErr))
|
||||
h.writeCodexModelsResponse(c, group, manifest)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -71,10 +71,19 @@ func (s *OpenAIGatewayService) BuildLocalCodexModelsFallbackManifest(
|
||||
// localCodexFallbackModelIDs resolves the local model list in the same priority
|
||||
// order the fallback documents. An enabled group list wins over account
|
||||
// mappings, and mappings win over the default catalog.
|
||||
//
|
||||
// The result always passes through FilterCodexModelIDsForGroup, the same
|
||||
// listing filter /v1/models uses, so the degraded catalog can never advertise a
|
||||
// model the group allowlist would reject. A list made only of wildcard entries
|
||||
// has no concrete names to offer, so its patterns are expanded against the
|
||||
// local candidates (account mappings and the default catalog).
|
||||
func localCodexFallbackModelIDs(group *Group, accounts []Account) []string {
|
||||
var modelIDs []string
|
||||
if group != nil && group.ModelAllowlistEnabled() {
|
||||
modelIDs = concreteCodexModelIDs(group.ModelAllowlist.Models)
|
||||
if len(modelIDs) == 0 && groupModelAllowlistHasWildcard(group.ModelAllowlist.Models) {
|
||||
modelIDs = append(openAIConfiguredCodexModelIDs(accounts), openai.DefaultModelIDs()...)
|
||||
}
|
||||
}
|
||||
if len(modelIDs) == 0 {
|
||||
modelIDs = openAIConfiguredCodexModelIDs(accounts)
|
||||
@@ -82,7 +91,16 @@ func localCodexFallbackModelIDs(group *Group, accounts []Account) []string {
|
||||
if len(modelIDs) == 0 {
|
||||
modelIDs = openai.DefaultModelIDs()
|
||||
}
|
||||
return filterRetiredOpenAIModelIDs(accounts, modelIDs)
|
||||
return FilterCodexModelIDsForGroup(filterRetiredOpenAIModelIDs(accounts, modelIDs), group)
|
||||
}
|
||||
|
||||
func groupModelAllowlistHasWildcard(models []string) bool {
|
||||
for _, modelID := range models {
|
||||
if strings.Contains(modelID, "*") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// concreteCodexModelIDs keeps concrete model names. Wildcard patterns are
|
||||
|
||||
@@ -86,6 +86,41 @@ func TestBuildLocalCodexModelsFallbackManifestPrefersGroupModelsListConfig(t *te
|
||||
require.Equal(t, []string{"gpt-5.6-luna", "gpt-6.2-sol"}, codexFallbackManifestSlugs(t, manifest))
|
||||
}
|
||||
|
||||
func TestBuildLocalCodexModelsFallbackManifestWildcardOnlyAllowlistStaysWithinAllowlist(t *testing.T) {
|
||||
accounts := []Account{
|
||||
newCodexFallbackAPIKeyAccount(map[string]any{"gpt-5.5-custom": "upstream-model", "other-alias": "upstream-model"}),
|
||||
}
|
||||
group := &Group{ID: 7, Platform: PlatformOpenAI}
|
||||
group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"gpt-5.5*"}}
|
||||
|
||||
manifest, configured, err := newCodexFallbackTestService(accounts).
|
||||
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
|
||||
|
||||
require.NoError(t, err)
|
||||
require.True(t, configured)
|
||||
slugs := codexFallbackManifestSlugs(t, manifest)
|
||||
require.Contains(t, slugs, "gpt-5.5-custom")
|
||||
require.Contains(t, slugs, "gpt-5.5")
|
||||
for _, slug := range slugs {
|
||||
require.True(t, group.ModelAllowlist.Allows(slug), "fallback must not advertise %q outside the allowlist", slug)
|
||||
}
|
||||
require.NotContains(t, slugs, "other-alias")
|
||||
require.NotContains(t, slugs, "gpt-5.6-sol")
|
||||
}
|
||||
|
||||
func TestBuildLocalCodexModelsFallbackManifestWildcardOnlyAllowlistWithoutMatchKeepsUpstreamError(t *testing.T) {
|
||||
accounts := []Account{newCodexFallbackAPIKeyAccount(map[string]any{"alias-a": "target-a"})}
|
||||
group := &Group{ID: 7, Platform: PlatformOpenAI}
|
||||
group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"no-such-family-*"}}
|
||||
|
||||
manifest, configured, err := newCodexFallbackTestService(accounts).
|
||||
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
|
||||
|
||||
require.NoError(t, err)
|
||||
require.False(t, configured)
|
||||
require.Nil(t, manifest)
|
||||
}
|
||||
|
||||
func TestBuildLocalCodexModelsFallbackManifestUsesAccountModelMapping(t *testing.T) {
|
||||
accounts := []Account{
|
||||
newCodexFallbackAPIKeyAccount(map[string]any{"alias-b": "target-b", "alias-a": "target-a"}),
|
||||
|
||||
Reference in new issue
Block a user