mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
Use the configured-manifest candidate set (mapping keys, plus the default catalog only when an account serves unmapped models) and tag the fallback log with the group.
146 lines
4.9 KiB
Go
146 lines
4.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
|
|
)
|
|
|
|
// BuildLocalCodexModelsFallbackManifest generates a Codex models manifest from
|
|
// local configuration when no selected upstream can serve its /models endpoint.
|
|
// Model sources are tried in priority order: the group's models_list_config
|
|
// (ModelAllowlist), the group accounts' model_mapping keys, and finally the
|
|
// default OpenAI catalog used by /v1/models. Retired models are removed with
|
|
// the same filter as /v1/models so the degraded catalog cannot advertise
|
|
// upstream models that the accounts can no longer serve.
|
|
//
|
|
// A configured=false result with a nil error means the local list is empty, so
|
|
// callers must keep the original upstream error instead of returning an empty
|
|
// manifest.
|
|
func (s *OpenAIGatewayService) BuildLocalCodexModelsFallbackManifest(
|
|
ctx context.Context,
|
|
group *Group,
|
|
ifNoneMatch string,
|
|
) (*OpenAIModelsResponse, bool, error) {
|
|
if group == nil || group.Platform != PlatformOpenAI {
|
|
return nil, false, nil
|
|
}
|
|
var visible, catalog []Account
|
|
if s != nil && s.accountRepo != nil {
|
|
var err error
|
|
visible, catalog, err = loadCodexGroupCatalogAccounts(ctx, s.accountRepo, group.ID)
|
|
if err != nil {
|
|
return nil, false, fmt.Errorf("load local Codex models accounts: %w", err)
|
|
}
|
|
}
|
|
modelIDs := localCodexFallbackModelIDs(group, visible)
|
|
if len(modelIDs) == 0 {
|
|
return nil, false, nil
|
|
}
|
|
body, err := buildCodexModelsManifestForAccounts(
|
|
PlatformOpenAI,
|
|
modelIDs,
|
|
catalog,
|
|
group,
|
|
nil,
|
|
true,
|
|
)
|
|
if err != nil {
|
|
return nil, false, fmt.Errorf("build local Codex models manifest: %w", err)
|
|
}
|
|
// The builder drops models that a Codex manifest cannot advertise (for
|
|
// example dedicated image-generation models). An empty result must not
|
|
// replace the upstream error with an empty 200 response.
|
|
if CodexModelsManifestIsEmpty(body) {
|
|
return nil, false, nil
|
|
}
|
|
manifest := &OpenAIModelsResponse{
|
|
Body: body,
|
|
ETag: codexModelsManifestBodyETag(body),
|
|
}
|
|
if codexModelsManifestETagMatches(ifNoneMatch, manifest.ETag) {
|
|
manifest.Body = nil
|
|
manifest.NotModified = true
|
|
}
|
|
return manifest, true, nil
|
|
}
|
|
|
|
// 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
|
|
// models the group accounts can serve (mapping keys, and the default catalog
|
|
// when an account is unrestricted).
|
|
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) {
|
|
// Same candidate set as the configured manifest: mapping keys,
|
|
// plus the default catalog only when an account can serve
|
|
// unmapped models.
|
|
modelIDs = supplementUnmappedOpenAIModels(accounts, openAIConfiguredCodexModelIDs(accounts))
|
|
if len(modelIDs) == 0 {
|
|
modelIDs = openai.DefaultModelIDs()
|
|
}
|
|
}
|
|
}
|
|
if len(modelIDs) == 0 {
|
|
modelIDs = openAIConfiguredCodexModelIDs(accounts)
|
|
}
|
|
if len(modelIDs) == 0 {
|
|
modelIDs = openai.DefaultModelIDs()
|
|
}
|
|
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
|
|
// routing rules that cannot be expanded without an upstream catalog; emitting
|
|
// them verbatim would advertise a literal "*" slug to Codex clients.
|
|
func concreteCodexModelIDs(models []string) []string {
|
|
seen := make(map[string]struct{}, len(models))
|
|
filtered := make([]string, 0, len(models))
|
|
for _, modelID := range models {
|
|
modelID = strings.TrimSpace(modelID)
|
|
if modelID == "" || strings.Contains(modelID, "*") {
|
|
continue
|
|
}
|
|
key := strings.ToLower(modelID)
|
|
if _, ok := seen[key]; ok {
|
|
continue
|
|
}
|
|
seen[key] = struct{}{}
|
|
filtered = append(filtered, modelID)
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
// CodexModelsManifestIsEmpty reports whether a validated Codex manifest
|
|
// advertises no models. A body that cannot be decoded is never treated as
|
|
// empty so an unexpected upstream shape keeps its existing response path.
|
|
func CodexModelsManifestIsEmpty(body []byte) bool {
|
|
var envelope struct {
|
|
Models []json.RawMessage `json:"models"`
|
|
}
|
|
if err := json.Unmarshal(body, &envelope); err != nil {
|
|
return false
|
|
}
|
|
return len(envelope.Models) == 0
|
|
}
|