Skip to content

Commit a8de3fa

Browse files
authored
Merge pull request #111 from xunxun1982/xxdev
feat: 优化集中管理路由逻辑说明和优先级提示
2 parents c769d60 + 74ec3a7 commit a8de3fa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2274
-517
lines changed

internal/centralizedmgmt/channel_compatibility.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ type ChannelCompatibility struct {
1515
// channelCompatibilityMap maps relay formats to their compatible channel types.
1616
// Priority order: Native channel first, then compatible channels.
1717
// Note: CC (Claude Code) support only converts Claude format to other formats (one-way conversion).
18+
// Unknown formats fallback to OpenAI for maximum compatibility.
1819
var channelCompatibilityMap = map[types.RelayFormat]ChannelCompatibility{
20+
// Unknown format - fallback to OpenAI for unrecognized paths
21+
// This ensures requests with unknown paths can still be routed instead of failing
22+
types.RelayFormatUnknown: {
23+
Native: "openai",
24+
Compatible: []string{},
25+
},
26+
1927
// OpenAI formats - native to OpenAI only (no Azure in this project)
2028
types.RelayFormatOpenAIChat: {
2129
Native: "openai",
@@ -79,10 +87,14 @@ var channelCompatibilityMap = map[types.RelayFormat]ChannelCompatibility{
7987

8088
// GetCompatibleChannels returns all compatible channel types for a given relay format.
8189
// Returns native channel first, followed by compatible channels in priority order.
90+
// For unknown formats, returns ["openai"] as a fallback to ensure maximum compatibility.
8291
func GetCompatibleChannels(format types.RelayFormat) []string {
8392
compat, exists := channelCompatibilityMap[format]
8493
if !exists {
85-
return []string{} // Unknown format, no compatible channels
94+
// Fallback to OpenAI for formats not defined in the map
95+
// This handles edge cases like typos or future formats not yet added to the map
96+
// The defensive fallback ensures requests with unknown paths can still be routed
97+
return []string{"openai"}
8698
}
8799

88100
// Build result: native first, then compatible channels
@@ -93,19 +105,23 @@ func GetCompatibleChannels(format types.RelayFormat) []string {
93105
}
94106

95107
// GetNativeChannel returns the native (preferred) channel type for a given relay format.
108+
// For unknown formats, returns "openai" as a fallback.
96109
func GetNativeChannel(format types.RelayFormat) string {
97110
compat, exists := channelCompatibilityMap[format]
98111
if !exists {
99-
return "" // Unknown format
112+
return "openai" // Fallback to OpenAI for unknown formats
100113
}
101114
return compat.Native
102115
}
103116

104117
// IsChannelCompatible checks if a channel type is compatible with a relay format.
118+
// For unknown formats not in the map, returns true only for "openai" to match
119+
// the fallback behavior of GetCompatibleChannels/GetNativeChannel.
105120
func IsChannelCompatible(channelType string, format types.RelayFormat) bool {
106121
compat, exists := channelCompatibilityMap[format]
107122
if !exists {
108-
return false
123+
// Fallback: only openai is compatible with truly unknown formats
124+
return channelType == "openai"
109125
}
110126

111127
// Check native channel
@@ -126,9 +142,14 @@ func IsChannelCompatible(channelType string, format types.RelayFormat) bool {
126142
// GetChannelPriority returns the priority of a channel type for a given relay format.
127143
// Lower number = higher priority. Returns -1 if not compatible.
128144
// Priority: 0 = native, 1+ = compatible channels in order.
145+
// For unknown formats not in the map, returns 0 for "openai" to match fallback behavior.
129146
func GetChannelPriority(channelType string, format types.RelayFormat) int {
130147
compat, exists := channelCompatibilityMap[format]
131148
if !exists {
149+
// Fallback: openai has priority 0 for truly unknown formats
150+
if channelType == "openai" {
151+
return 0
152+
}
132153
return -1
133154
}
134155

internal/centralizedmgmt/channel_compatibility_test.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,17 @@ func TestGetCompatibleChannels(t *testing.T) {
4545
wantCompatible: []string{},
4646
},
4747
{
48-
name: "Unknown format",
49-
format: types.RelayFormat("unknown"),
50-
wantNative: "",
51-
wantCompatible: nil,
48+
name: "Unknown format (fallback to OpenAI)",
49+
format: types.RelayFormatUnknown,
50+
wantNative: "openai",
51+
wantCompatible: []string{},
5252
},
5353
}
5454

5555
for _, tt := range tests {
5656
t.Run(tt.name, func(t *testing.T) {
5757
got := GetCompatibleChannels(tt.format)
5858

59-
if tt.wantNative == "" {
60-
// Unknown format should return empty slice
61-
if len(got) != 0 {
62-
t.Errorf("GetCompatibleChannels() = %v, want empty slice", got)
63-
}
64-
return
65-
}
66-
6759
// Check native channel (first element)
6860
if len(got) == 0 {
6961
t.Fatalf("GetCompatibleChannels() returned empty slice, want native=%s", tt.wantNative)
@@ -102,7 +94,7 @@ func TestGetNativeChannel(t *testing.T) {
10294
{"Claude", types.RelayFormatClaude, "anthropic"},
10395
{"Gemini", types.RelayFormatGemini, "gemini"},
10496
{"Codex", types.RelayFormatCodex, "codex"},
105-
{"Unknown", types.RelayFormat("unknown"), ""},
97+
{"Unknown (fallback to OpenAI)", types.RelayFormatUnknown, "openai"},
10698
}
10799

108100
for _, tt := range tests {
@@ -150,8 +142,9 @@ func TestIsChannelCompatible(t *testing.T) {
150142
{"Codex native", "codex", types.RelayFormatCodex, true},
151143
{"OpenAI NOT compatible for Codex", "openai", types.RelayFormatCodex, false},
152144

153-
// Unknown format
154-
{"Unknown format", "openai", types.RelayFormat("unknown"), false},
145+
// Unknown format (fallback to OpenAI)
146+
{"Unknown format fallback to OpenAI", "openai", types.RelayFormatUnknown, true},
147+
{"Unknown format NOT compatible with Anthropic", "anthropic", types.RelayFormatUnknown, false},
155148
}
156149

157150
for _, tt := range tests {
@@ -189,8 +182,9 @@ func TestGetChannelPriority(t *testing.T) {
189182
{"Anthropic NOT compatible for embedding", "anthropic", types.RelayFormatOpenAIEmbedding, -1},
190183
{"OpenAI NOT compatible for Gemini", "openai", types.RelayFormatGemini, -1},
191184

192-
// Unknown format
193-
{"Unknown format", "openai", types.RelayFormat("unknown"), -1},
185+
// Unknown format (fallback to OpenAI)
186+
{"Unknown format fallback to OpenAI", "openai", types.RelayFormatUnknown, 0},
187+
{"Unknown format NOT compatible with Anthropic", "anthropic", types.RelayFormatUnknown, -1},
194188
}
195189

196190
for _, tt := range tests {

0 commit comments

Comments
 (0)