mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 12:20:52 +08:00
Refactors context keys for model routing
Uses centralized context keys for accessing mapped and fallback models. This change deprecates the string-based context keys used in the AMP fallback handlers in favor of the `ctxkeys` package, promoting consistency and reducing the risk of typos. The authentication conductor now retrieves fallback models using the shared `ctxkeys` constants.
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/routing/ctxkeys"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/thinking"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/thinking"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -32,11 +33,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// MappedModelContextKey is the Gin context key for passing mapped model names.
|
// MappedModelContextKey is the Gin context key for passing mapped model names.
|
||||||
const MappedModelContextKey = "mapped_model"
|
// Deprecated: Use ctxkeys.MappedModel instead.
|
||||||
|
const MappedModelContextKey = string(ctxkeys.MappedModel)
|
||||||
|
|
||||||
// FallbackModelsContextKey is the Gin context key for passing fallback model names.
|
// FallbackModelsContextKey is the Gin context key for passing fallback model names.
|
||||||
// When the primary mapped model fails (e.g., quota exceeded), these models can be tried.
|
// When the primary mapped model fails (e.g., quota exceeded), these models can be tried.
|
||||||
const FallbackModelsContextKey = "fallback_models"
|
// Deprecated: Use ctxkeys.FallbackModels instead.
|
||||||
|
const FallbackModelsContextKey = string(ctxkeys.FallbackModels)
|
||||||
|
|
||||||
// logAmpRouting logs the routing decision for an Amp request with structured fields
|
// logAmpRouting logs the routing decision for an Amp request with structured fields
|
||||||
func logAmpRouting(routeType AmpRouteType, requestedModel, resolvedModel, provider, path string) {
|
func logAmpRouting(routeType AmpRouteType, requestedModel, resolvedModel, provider, path string) {
|
||||||
@@ -227,9 +230,9 @@ func (fh *FallbackHandler) WrapHandler(handler gin.HandlerFunc) gin.HandlerFunc
|
|||||||
bodyBytes = rewriteModelInRequest(bodyBytes, mappedModels[0])
|
bodyBytes = rewriteModelInRequest(bodyBytes, mappedModels[0])
|
||||||
c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
|
c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
|
||||||
// Store mapped model and fallbacks in context for handlers
|
// Store mapped model and fallbacks in context for handlers
|
||||||
c.Set(MappedModelContextKey, mappedModels[0])
|
c.Set(string(ctxkeys.MappedModel), mappedModels[0])
|
||||||
if len(mappedModels) > 1 {
|
if len(mappedModels) > 1 {
|
||||||
c.Set(FallbackModelsContextKey, mappedModels[1:])
|
c.Set(string(ctxkeys.FallbackModels), mappedModels[1:])
|
||||||
}
|
}
|
||||||
resolvedModel = mappedModels[0]
|
resolvedModel = mappedModels[0]
|
||||||
usedMapping = true
|
usedMapping = true
|
||||||
@@ -251,9 +254,9 @@ func (fh *FallbackHandler) WrapHandler(handler gin.HandlerFunc) gin.HandlerFunc
|
|||||||
bodyBytes = rewriteModelInRequest(bodyBytes, mappedModels[0])
|
bodyBytes = rewriteModelInRequest(bodyBytes, mappedModels[0])
|
||||||
c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
|
c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
|
||||||
// Store mapped model and fallbacks in context for handlers
|
// Store mapped model and fallbacks in context for handlers
|
||||||
c.Set(MappedModelContextKey, mappedModels[0])
|
c.Set(string(ctxkeys.MappedModel), mappedModels[0])
|
||||||
if len(mappedModels) > 1 {
|
if len(mappedModels) > 1 {
|
||||||
c.Set(FallbackModelsContextKey, mappedModels[1:])
|
c.Set(string(ctxkeys.FallbackModels), mappedModels[1:])
|
||||||
}
|
}
|
||||||
resolvedModel = mappedModels[0]
|
resolvedModel = mappedModels[0]
|
||||||
usedMapping = true
|
usedMapping = true
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
internalconfig "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
internalconfig "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||||
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/routing/ctxkeys"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/thinking"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/thinking"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
||||||
@@ -577,7 +578,7 @@ func (m *Manager) executeWithFallback(
|
|||||||
|
|
||||||
// Track fallback models from context (provided by Amp module fallback_models key)
|
// Track fallback models from context (provided by Amp module fallback_models key)
|
||||||
var fallbacks []string
|
var fallbacks []string
|
||||||
if v := ctx.Value("fallback_models"); v != nil {
|
if v := ctx.Value(ctxkeys.FallbackModels); v != nil {
|
||||||
if fs, ok := v.([]string); ok {
|
if fs, ok := v.([]string); ok {
|
||||||
fallbacks = fs
|
fallbacks = fs
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user