mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-24 15:20:38 +00:00
115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
|
|
)
|
|
|
|
// WithResolvedTargetPlatform stores the concrete provider chosen for a request
|
|
// made through a composite group.
|
|
func WithResolvedTargetPlatform(ctx context.Context, platform string) context.Context {
|
|
platform = strings.TrimSpace(platform)
|
|
if ctx == nil || platform == "" {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, ctxkey.ResolvedTargetPlatform, platform)
|
|
}
|
|
|
|
// ResolvedTargetPlatformFromContext returns the concrete provider chosen for
|
|
// the current request, if one was resolved.
|
|
func ResolvedTargetPlatformFromContext(ctx context.Context) (string, bool) {
|
|
if ctx == nil {
|
|
return "", false
|
|
}
|
|
platform, ok := ctx.Value(ctxkey.ResolvedTargetPlatform).(string)
|
|
platform = strings.TrimSpace(platform)
|
|
if !ok || platform == "" {
|
|
return "", false
|
|
}
|
|
return platform, true
|
|
}
|
|
|
|
// DetectModelPlatform maps common public model IDs to the concrete provider
|
|
// platform used by sub2api. It intentionally returns false for ambiguous model
|
|
// names so composite groups fail closed instead of guessing.
|
|
func DetectModelPlatform(model string) (string, bool) {
|
|
normalized := strings.ToLower(strings.TrimSpace(model))
|
|
if normalized == "" {
|
|
return "", false
|
|
}
|
|
|
|
normalized = strings.TrimPrefix(normalized, "models/")
|
|
if slash := strings.IndexByte(normalized, '/'); slash > 0 {
|
|
provider := strings.TrimSpace(normalized[:slash])
|
|
rest := strings.TrimSpace(normalized[slash+1:])
|
|
switch provider {
|
|
case "anthropic", "claude":
|
|
return PlatformAnthropic, true
|
|
case "openai", "chatgpt":
|
|
return PlatformOpenAI, true
|
|
case "google", "google-ai-studio", "gemini":
|
|
return PlatformGemini, true
|
|
case "xai", "x-ai", "grok":
|
|
return PlatformGrok, true
|
|
}
|
|
if rest != "" {
|
|
normalized = strings.TrimPrefix(rest, "models/")
|
|
}
|
|
}
|
|
|
|
switch {
|
|
case strings.HasPrefix(normalized, "anthropic.claude-"),
|
|
strings.HasPrefix(normalized, "claude-"):
|
|
return PlatformAnthropic, true
|
|
case strings.HasPrefix(normalized, "gpt-"),
|
|
strings.HasPrefix(normalized, "chatgpt-"),
|
|
strings.HasPrefix(normalized, "codex-"),
|
|
strings.HasPrefix(normalized, "text-embedding-"),
|
|
strings.HasPrefix(normalized, "text-moderation-"),
|
|
strings.HasPrefix(normalized, "omni-moderation-"),
|
|
strings.HasPrefix(normalized, "dall-e-"),
|
|
strings.HasPrefix(normalized, "gpt-image-"),
|
|
strings.HasPrefix(normalized, "tts-"),
|
|
strings.HasPrefix(normalized, "whisper-"),
|
|
hasOpenAISeriesPrefix(normalized):
|
|
return PlatformOpenAI, true
|
|
case strings.HasPrefix(normalized, "gemini-"),
|
|
strings.HasPrefix(normalized, "learnlm-"):
|
|
return PlatformGemini, true
|
|
case normalized == "grok" || strings.HasPrefix(normalized, "grok-"):
|
|
return PlatformGrok, true
|
|
default:
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
func hasOpenAISeriesPrefix(model string) bool {
|
|
for _, prefix := range []string{"o1", "o3", "o4", "o5"} {
|
|
if model == prefix || strings.HasPrefix(model, prefix+"-") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func resolveCompositeTargetPlatform(ctx context.Context, group *Group, requestedModel string) (string, bool) {
|
|
if platform, ok := ResolvedTargetPlatformFromContext(ctx); ok {
|
|
return platform, true
|
|
}
|
|
if group == nil || group.Platform != PlatformComposite {
|
|
return "", false
|
|
}
|
|
return DetectModelPlatform(requestedModel)
|
|
}
|
|
|
|
func isConcreteRequestPlatform(platform string) bool {
|
|
switch platform {
|
|
case PlatformAnthropic, PlatformOpenAI, PlatformGemini, PlatformAntigravity, PlatformGrok:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|