feat(gateway): expose routed Codex model catalogs

Codex model discovery expects a top-level models manifest, but Composite and other non-OpenAI groups either reached the OpenAI live-manifest handler or had no Codex-specific response at all. API key users also had no supported way to fetch that manifest and reference it from config.toml.

Generate minimal manifests from each group's effective model list while preserving the official OpenAI live path and the ordinary /models response. Add the Use Key flow for authenticated catalog download and model_catalog_json configuration without writing the API key into the downloaded file.
This commit is contained in:
Long Li committed 2026-08-22 14:43:40 +09:00
1 parent d45135d87d
commit 22e1b8144a
12 files changed
+758 -17

No files matched your search

@@ -1140,6 +1140,59 @@ func (h *GatewayHandler) Models(c *gin.Context) {
})
}
// CodexModels returns the effective group model list using the manifest shape
// expected by Codex custom providers. Official OpenAI groups continue to use
// OpenAIGatewayHandler.CodexModels so their live upstream metadata is preserved.
func (h *GatewayHandler) CodexModels(c *gin.Context) {
apiKey, ok := middleware2.GetAPIKeyFromContext(c)
if !ok || apiKey == nil || apiKey.Group == nil {
h.errorResponse(c, http.StatusUnauthorized, "invalid_request_error", "API key group is required")
return
}
modelIDs := h.codexModelIDsForGroup(c.Request.Context(), apiKey.Group)
body, err := service.BuildCodexModelsManifest(modelIDs)
if err != nil {
h.errorResponse(c, http.StatusInternalServerError, "api_error", "Failed to build Codex models manifest")
return
}
c.Data(http.StatusOK, "application/json", body)
}
func (h *GatewayHandler) codexModelIDsForGroup(ctx context.Context, group *service.Group) []string {
if h == nil || h.gatewayService == nil || group == nil {
return nil
}
groupID := &group.ID
platform := group.Platform
if platform == service.PlatformComposite {
availableModels := h.compositeAvailableModels(ctx, groupID)
fallbackModels := defaultModelIDsForPlatform(service.PlatformComposite)
if group.CustomModelsListEnabled() {
return filterModelsByCustomList(availableModels, fallbackModels, group.ModelsListConfig.Models)
}
if len(availableModels) > 0 {
return availableModels
}
return fallbackModels
}
availableModels := h.gatewayService.GetAvailableModels(ctx, groupID, platform)
fallbackModels := defaultModelIDsForPlatform(platform)
if group.CustomModelsListEnabled() {
return filterModelsByCustomList(
customModelsListSource(platform, availableModels, fallbackModels),
fallbackModels,
group.ModelsListConfig.Models,
)
}
if len(availableModels) > 0 {
return availableModels
}
return fallbackModels
}
func (h *GatewayHandler) compositeAvailableModels(ctx context.Context, groupID *int64) []string {
if h == nil || h.gatewayService == nil {
return nil
@@ -25,6 +25,12 @@ type gatewayModelsResponseForTest struct {
Data []gatewayModelItemForTest `json:"data"`
}
type codexModelsResponseForTest struct {
Models []struct {
Slug string `json:"slug"`
} `json:"models"`
}
type gatewayModelItemForTest struct {
ID string `json:"id"`
Object string `json:"object"`
@@ -70,6 +76,150 @@ func TestDefaultModelIDsForCompositeIncludesAntigravityDefaults(t *testing.T) {
require.Contains(t, compositeIDs, antigravityIDs[0])
}
// Scenario: non-OpenAI groups return a Codex manifest instead of a standard model list.
func TestGatewayCodexModels_NonOpenAIGroupsUseMappedModels(t *testing.T) {
tests := []struct {
name string
platform string
model string
}{
{name: "Grok", platform: service.PlatformGrok, model: "grok-4.6"},
{name: "DeepSeek", platform: service.PlatformDeepseek, model: "deepseek-v4-pro"},
}
for index, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gin.SetMode(gin.TestMode)
groupID := int64(100 + index)
h := newGatewayModelsHandlerForTest(&gatewayModelsAccountRepoStub{
byGroup: map[int64][]service.Account{
groupID: {
{
ID: 1,
Platform: tt.platform,
Credentials: map[string]any{
"model_mapping": map[string]any{tt.model: tt.model},
},
},
},
},
})
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = httptest.NewRequest(http.MethodGet, "/models?client_version=0.147.0", nil)
c.Set(string(middleware2.ContextKeyAPIKey), &service.APIKey{
Group: &service.Group{ID: groupID, Platform: tt.platform},
})
h.CodexModels(c)
require.Equal(t, http.StatusOK, rec.Code)
var got codexModelsResponseForTest
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
require.Len(t, got.Models, 1)
require.Equal(t, tt.model, got.Models[0].Slug)
})
}
}
// Scenario: Composite manifests aggregate models across routed platforms.
func TestGatewayCodexModels_CompositeUsesCompleteEffectiveModelList(t *testing.T) {
gin.SetMode(gin.TestMode)
const groupID int64 = 120
h := newGatewayModelsHandlerForTest(&gatewayModelsAccountRepoStub{
byGroup: map[int64][]service.Account{
groupID: {
{
ID: 1,
Platform: service.PlatformOpenAI,
Credentials: map[string]any{
"model_mapping": map[string]any{"gpt-5.5": "gpt-5.5"},
},
},
{
ID: 2,
Platform: service.PlatformGrok,
Credentials: map[string]any{
"model_mapping": map[string]any{"grok-4.6": "grok-4.6"},
},
},
},
},
})
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = httptest.NewRequest(http.MethodGet, "/models?client_version=0.147.0", nil)
c.Set(string(middleware2.ContextKeyAPIKey), &service.APIKey{
Group: &service.Group{ID: groupID, Platform: service.PlatformComposite},
})
h.CodexModels(c)
require.Equal(t, http.StatusOK, rec.Code)
var got codexModelsResponseForTest
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
require.Equal(t, []string{"gpt-5.5", "grok-4.6"}, codexModelSlugsForTest(got.Models))
}
// Scenario: group models_list_config limits the generated Codex manifest.
func TestGatewayCodexModels_CustomModelsListFiltersCompositeManifest(t *testing.T) {
gin.SetMode(gin.TestMode)
const groupID int64 = 121
h := newGatewayModelsHandlerForTest(&gatewayModelsAccountRepoStub{
byGroup: map[int64][]service.Account{
groupID: {
{
ID: 1,
Platform: service.PlatformOpenAI,
Credentials: map[string]any{
"model_mapping": map[string]any{"gpt-5.5": "gpt-5.5"},
},
},
{
ID: 2,
Platform: service.PlatformGrok,
Credentials: map[string]any{
"model_mapping": map[string]any{"grok-4.6": "grok-4.6"},
},
},
},
},
})
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = httptest.NewRequest(http.MethodGet, "/models?client_version=0.147.0", nil)
c.Set(string(middleware2.ContextKeyAPIKey), &service.APIKey{
Group: &service.Group{
ID: groupID,
Platform: service.PlatformComposite,
ModelsListConfig: service.GroupModelsListConfig{
Enabled: true,
Models: []string{"grok-4.6"},
},
},
})
h.CodexModels(c)
require.Equal(t, http.StatusOK, rec.Code)
var got codexModelsResponseForTest
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
require.Equal(t, []string{"grok-4.6"}, codexModelSlugsForTest(got.Models))
}
func codexModelSlugsForTest(models []struct {
Slug string `json:"slug"`
}) []string {
slugs := make([]string, 0, len(models))
for _, model := range models {
slugs = append(slugs, model.Slug)
}
return slugs
}
func TestGatewayModels_GeminiGroupFallsBackToGeminiModels(t *testing.T) {
gin.SetMode(gin.TestMode)
+14 -6
View File
@@ -65,13 +65,13 @@ func RegisterGatewayRoutes(
h.Gateway.CountTokens(c)
}
}
codexModelsHandler := func(c *gin.Context) {
dispatchCodexModelsGateway(c, h.OpenAIGateway.CodexModels, h.Gateway.CodexModels)
}
modelsHandler := func(c *gin.Context) {
if c.Query("client_version") != "" {
switch getGroupPlatform(c) {
case service.PlatformOpenAI, service.PlatformComposite:
h.OpenAIGateway.CodexModels(c)
return
}
codexModelsHandler(c)
return
}
h.Gateway.Models(c)
}
@@ -377,7 +377,7 @@ func RegisterGatewayRoutes(
codexDirect.GET("/responses", func(c *gin.Context) {
h.OpenAIGateway.ResponsesWebSocket(c)
})
codexDirect.GET("/models", h.OpenAIGateway.CodexModels)
codexDirect.GET("/models", codexModelsHandler)
}
// OpenAI Chat Completions API(不带v1前缀的别名)— auto-route based on group platform
r.POST("/chat/completions", bodyLimit, clientRequestID, opsErrorLogger, endpointNorm, gin.HandlerFunc(apiKeyAuth), compositeTarget, requireGroupAnthropic, func(c *gin.Context) {
@@ -504,6 +504,14 @@ func RegisterGatewayRoutes(
}
func dispatchCodexModelsGateway(c *gin.Context, openAIHandler, generatedHandler gin.HandlerFunc) {
if getGroupPlatform(c) == service.PlatformOpenAI {
openAIHandler(c)
return
}
generatedHandler(c)
}
// getGroupPlatform extracts the group platform from the API Key stored in context.
func getGroupPlatform(c *gin.Context) string {
apiKey, ok := middleware.GetAPIKeyFromContext(c)
@@ -2,8 +2,12 @@ package routes
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
@@ -22,3 +26,39 @@ func TestGatewayRoutesCodexModelsManifestPathIsRegistered(t *testing.T) {
require.NotEmpty(t, registered["/models"], "GET /models should be registered")
require.Equal(t, registered["/v1/models"], registered["/models"], "root alias should use the same platform-aware handler")
}
func TestDispatchCodexModelsGatewayKeepsOnlyOpenAIOnLiveManifestHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
platform string
wantOpenAI bool
}{
{platform: service.PlatformOpenAI, wantOpenAI: true},
{platform: service.PlatformComposite},
{platform: service.PlatformGrok},
{platform: service.PlatformDeepseek},
}
for _, tt := range tests {
t.Run(tt.platform, func(t *testing.T) {
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = httptest.NewRequest(http.MethodGet, "/models?client_version=0.147.0", nil)
c.Set(string(middleware.ContextKeyAPIKey), &service.APIKey{
Group: &service.Group{Platform: tt.platform},
})
called := ""
dispatchCodexModelsGateway(c,
func(c *gin.Context) { called = "openai" },
func(c *gin.Context) { called = "generated" },
)
if tt.wantOpenAI {
require.Equal(t, "openai", called)
} else {
require.Equal(t, "generated", called)
}
})
}
}
@@ -647,27 +647,48 @@ func convertOpenAIModelListToCodexManifest(body []byte) []byte {
if err := json.Unmarshal(data, &entries); err != nil {
return body
}
type codexModelEntry struct {
Slug string `json:"slug"`
}
models := make([]codexModelEntry, 0, len(entries))
modelIDs := make([]string, 0, len(entries))
for _, entry := range entries {
id := strings.TrimSpace(entry.ID)
if id == "" {
continue
}
models = append(models, codexModelEntry{Slug: id})
modelIDs = append(modelIDs, id)
}
if len(models) == 0 {
if len(modelIDs) == 0 {
return body
}
converted, err := json.Marshal(map[string][]codexModelEntry{"models": models})
converted, err := BuildCodexModelsManifest(modelIDs)
if err != nil {
return body
}
return converted
}
// BuildCodexModelsManifest creates the minimal manifest accepted by Codex
// custom providers while preserving the effective model order for the group.
func BuildCodexModelsManifest(modelIDs []string) ([]byte, error) {
type codexModelEntry struct {
Slug string `json:"slug"`
}
seen := make(map[string]struct{}, len(modelIDs))
models := make([]codexModelEntry, 0, len(modelIDs))
for _, rawModelID := range modelIDs {
modelID := strings.TrimSpace(rawModelID)
if modelID == "" {
continue
}
if _, exists := seen[modelID]; exists {
continue
}
seen[modelID] = struct{}{}
models = append(models, codexModelEntry{Slug: modelID})
}
return json.Marshal(map[string][]codexModelEntry{"models": models})
}
func validateCodexModelsManifestEnvelope(body []byte) error {
var envelope map[string]json.RawMessage
if err := json.Unmarshal(body, &envelope); err != nil {
@@ -2,6 +2,7 @@ package service
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
@@ -23,6 +24,28 @@ import (
"golang.org/x/net/http2"
)
// Scenario: non-OpenAI groups expose their effective model IDs as a Codex manifest.
func TestBuildCodexModelsManifest(t *testing.T) {
body, err := BuildCodexModelsManifest([]string{
" grok-4.6 ",
"deepseek-v4-pro",
"grok-4.6",
"",
})
require.NoError(t, err)
var got struct {
Models []struct {
Slug string `json:"slug"`
} `json:"models"`
}
require.NoError(t, json.Unmarshal(body, &got))
require.Equal(t, []string{"grok-4.6", "deepseek-v4-pro"}, []string{
got.Models[0].Slug,
got.Models[1].Slug,
})
}
type codexModelsHTTPUpstreamStub struct {
do func(req *http.Request, proxyURL string, accountID int64, accountConcurrency int) (*http.Response, error)
}