mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 12:20:52 +08:00
Fixes thinking signature validation errors
Addresses an issue where thinking signature validation fails due to model mapping and empty internal registry. - Implements a fallback mechanism in the router to use the global model registry when the internal registry is empty. This ensures that models registered via API keys are correctly resolved even without local provider configurations. - Modifies `GetModelGroup` to use registry-based grouping in addition to name pattern matching, covering cases where models are registered with API keys but lack provider names in their names. - Updates signature validation to compare model groups instead of exact model names. These changes resolve thinking signature validation errors and improve the accuracy of model resolution.
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/thinking"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
||||
)
|
||||
|
||||
// Router resolves models to provider candidates.
|
||||
@@ -115,25 +118,47 @@ func (r *Router) ResolveV2(req RoutingRequest) *RoutingDecision {
|
||||
}
|
||||
|
||||
// findLocalCandidates finds local provider candidates for a model.
|
||||
// If the internal registry is empty, it falls back to the global model registry.
|
||||
func (r *Router) findLocalCandidates(model string, suffixResult thinking.SuffixResult) []ProviderCandidate {
|
||||
var candidates []ProviderCandidate
|
||||
|
||||
for _, p := range r.registry.All() {
|
||||
if !p.SupportsModel(model) {
|
||||
continue
|
||||
}
|
||||
// Check internal registry first
|
||||
registryProviders := r.registry.All()
|
||||
if len(registryProviders) > 0 {
|
||||
for _, p := range registryProviders {
|
||||
if !p.SupportsModel(model) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Apply thinking suffix if needed
|
||||
actualModel := model
|
||||
if suffixResult.HasSuffix && !thinking.ParseSuffix(model).HasSuffix {
|
||||
actualModel = model + "(" + suffixResult.RawSuffix + ")"
|
||||
}
|
||||
// Apply thinking suffix if needed
|
||||
actualModel := model
|
||||
if suffixResult.HasSuffix && !thinking.ParseSuffix(model).HasSuffix {
|
||||
actualModel = model + "(" + suffixResult.RawSuffix + ")"
|
||||
}
|
||||
|
||||
if p.Available(actualModel) {
|
||||
candidates = append(candidates, ProviderCandidate{
|
||||
Provider: p,
|
||||
Model: actualModel,
|
||||
})
|
||||
if p.Available(actualModel) {
|
||||
candidates = append(candidates, ProviderCandidate{
|
||||
Provider: p,
|
||||
Model: actualModel,
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback to global model registry (same logic as FallbackHandler)
|
||||
// This ensures compatibility when the wrapper is initialized with an empty registry
|
||||
providers := registry.GetGlobalRegistry().GetModelProviders(model)
|
||||
if len(providers) > 0 {
|
||||
actualModel := model
|
||||
if suffixResult.HasSuffix && !thinking.ParseSuffix(model).HasSuffix {
|
||||
actualModel = model + "(" + suffixResult.RawSuffix + ")"
|
||||
}
|
||||
// Create a synthetic provider candidate for each provider
|
||||
for _, providerName := range providers {
|
||||
candidates = append(candidates, ProviderCandidate{
|
||||
Provider: &globalRegistryProvider{name: providerName, model: actualModel},
|
||||
Model: actualModel,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +170,31 @@ func (r *Router) findLocalCandidates(model string, suffixResult thinking.SuffixR
|
||||
return candidates
|
||||
}
|
||||
|
||||
// globalRegistryProvider is a synthetic Provider implementation that wraps
|
||||
// a provider name from the global model registry. It is used only for routing
|
||||
// decisions when the internal registry is empty - actual execution goes through
|
||||
// the normal handler path, not through this provider's Execute methods.
|
||||
type globalRegistryProvider struct {
|
||||
name string
|
||||
model string
|
||||
}
|
||||
|
||||
func (p *globalRegistryProvider) Name() string { return p.name }
|
||||
func (p *globalRegistryProvider) Type() ProviderType { return ProviderTypeOAuth }
|
||||
func (p *globalRegistryProvider) Priority() int { return 0 }
|
||||
func (p *globalRegistryProvider) SupportsModel(string) bool { return true }
|
||||
func (p *globalRegistryProvider) Available(string) bool { return true }
|
||||
|
||||
// Execute is not used for globalRegistryProvider - routing wrapper calls the handler directly.
|
||||
func (p *globalRegistryProvider) Execute(ctx context.Context, model string, req executor.Request) (executor.Response, error) {
|
||||
return executor.Response{}, nil
|
||||
}
|
||||
|
||||
// ExecuteStream is not used for globalRegistryProvider - routing wrapper calls the handler directly.
|
||||
func (p *globalRegistryProvider) ExecuteStream(ctx context.Context, model string, req executor.Request) (<-chan executor.StreamChunk, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// buildLocalProviderDecision creates a decision for local provider routing.
|
||||
func (r *Router) buildLocalProviderDecision(requestedModel string, candidates []ProviderCandidate, thinkingSuffix string) *RoutingDecision {
|
||||
resolvedModel := requestedModel
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
globalRegistry "github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -113,3 +114,89 @@ func TestRouter_Resolve_NoProviders(t *testing.T) {
|
||||
assert.Equal(t, "unknown-model", decision.ResolvedModel)
|
||||
assert.Empty(t, decision.Candidates)
|
||||
}
|
||||
|
||||
// === Global Registry Fallback Tests (T-027) ===
|
||||
// These tests verify that when the internal registry is empty,
|
||||
// the router falls back to the global model registry.
|
||||
// This is the core fix for the thinking signature 400 error.
|
||||
|
||||
func TestRouter_GlobalRegistryFallback_LocalProvider(t *testing.T) {
|
||||
// This test requires registering a model in the global registry.
|
||||
// We use a model that's already registered via api-key config in production.
|
||||
// For isolated testing, we can skip if global registry is not populated.
|
||||
|
||||
globalReg := globalRegistry.GetGlobalRegistry()
|
||||
modelCount := globalReg.GetModelCount("claude-sonnet-4-20250514")
|
||||
|
||||
if modelCount == 0 {
|
||||
t.Skip("Global registry not populated - run with server context")
|
||||
}
|
||||
|
||||
// Empty internal registry
|
||||
emptyRegistry := NewRegistry()
|
||||
cfg := &config.Config{}
|
||||
router := NewRouter(emptyRegistry, cfg)
|
||||
|
||||
req := RoutingRequest{
|
||||
RequestedModel: "claude-sonnet-4-20250514",
|
||||
PreferLocalProvider: true,
|
||||
}
|
||||
decision := router.ResolveV2(req)
|
||||
|
||||
// Should find provider from global registry
|
||||
assert.Equal(t, RouteTypeLocalProvider, decision.RouteType)
|
||||
assert.Equal(t, "claude-sonnet-4-20250514", decision.ResolvedModel)
|
||||
assert.False(t, decision.ShouldProxy)
|
||||
}
|
||||
|
||||
func TestRouter_GlobalRegistryFallback_ModelMapping(t *testing.T) {
|
||||
// This test verifies that model mapping works with global registry fallback.
|
||||
|
||||
globalReg := globalRegistry.GetGlobalRegistry()
|
||||
modelCount := globalReg.GetModelCount("claude-opus-4-5-thinking")
|
||||
|
||||
if modelCount == 0 {
|
||||
t.Skip("Global registry not populated - run with server context")
|
||||
}
|
||||
|
||||
// Empty internal registry
|
||||
emptyRegistry := NewRegistry()
|
||||
cfg := &config.Config{
|
||||
AmpCode: config.AmpCode{
|
||||
ModelMappings: []config.AmpModelMapping{
|
||||
{From: "claude-opus-4-5-20251101", To: "claude-opus-4-5-thinking"},
|
||||
},
|
||||
},
|
||||
}
|
||||
router := NewRouter(emptyRegistry, cfg)
|
||||
|
||||
req := RoutingRequest{
|
||||
RequestedModel: "claude-opus-4-5-20251101",
|
||||
PreferLocalProvider: true,
|
||||
}
|
||||
decision := router.ResolveV2(req)
|
||||
|
||||
// Should find mapped model from global registry
|
||||
assert.Equal(t, RouteTypeModelMapping, decision.RouteType)
|
||||
assert.Equal(t, "claude-opus-4-5-thinking", decision.ResolvedModel)
|
||||
assert.False(t, decision.ShouldProxy)
|
||||
}
|
||||
|
||||
func TestRouter_GlobalRegistryFallback_AmpCreditsWhenNotFound(t *testing.T) {
|
||||
// Empty internal registry
|
||||
emptyRegistry := NewRegistry()
|
||||
cfg := &config.Config{}
|
||||
router := NewRouter(emptyRegistry, cfg)
|
||||
|
||||
// Use a model that definitely doesn't exist anywhere
|
||||
req := RoutingRequest{
|
||||
RequestedModel: "nonexistent-model-12345",
|
||||
PreferLocalProvider: true,
|
||||
}
|
||||
decision := router.ResolveV2(req)
|
||||
|
||||
// Should fall back to AMP credits proxy
|
||||
assert.Equal(t, RouteTypeAmpCredits, decision.RouteType)
|
||||
assert.Equal(t, "nonexistent-model-12345", decision.ResolvedModel)
|
||||
assert.True(t, decision.ShouldProxy)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user