fix(openai): 透传OAuth强制store/stream并修复Codex识别
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package openai
|
||||
|
||||
import "strings"
|
||||
|
||||
// CodexCLIUserAgentPrefixes matches Codex CLI User-Agent patterns
|
||||
// Examples: "codex_vscode/1.0.0", "codex_cli_rs/0.1.2"
|
||||
var CodexCLIUserAgentPrefixes = []string{
|
||||
@@ -9,8 +11,17 @@ var CodexCLIUserAgentPrefixes = []string{
|
||||
|
||||
// IsCodexCLIRequest checks if the User-Agent indicates a Codex CLI request
|
||||
func IsCodexCLIRequest(userAgent string) bool {
|
||||
ua := strings.ToLower(strings.TrimSpace(userAgent))
|
||||
if ua == "" {
|
||||
return false
|
||||
}
|
||||
for _, prefix := range CodexCLIUserAgentPrefixes {
|
||||
if len(userAgent) >= len(prefix) && userAgent[:len(prefix)] == prefix {
|
||||
normalizedPrefix := strings.ToLower(strings.TrimSpace(prefix))
|
||||
if normalizedPrefix == "" {
|
||||
continue
|
||||
}
|
||||
// 优先前缀匹配;若 UA 被网关/代理拼接为复合字符串时,退化为包含匹配。
|
||||
if strings.HasPrefix(ua, normalizedPrefix) || strings.Contains(ua, normalizedPrefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
28
backend/internal/pkg/openai/request_test.go
Normal file
28
backend/internal/pkg/openai/request_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package openai
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsCodexCLIRequest(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ua string
|
||||
want bool
|
||||
}{
|
||||
{name: "codex_cli_rs 前缀", ua: "codex_cli_rs/0.1.0", want: true},
|
||||
{name: "codex_vscode 前缀", ua: "codex_vscode/1.2.3", want: true},
|
||||
{name: "大小写混合", ua: "Codex_CLI_Rs/0.1.0", want: true},
|
||||
{name: "复合 UA 包含 codex", ua: "Mozilla/5.0 codex_cli_rs/0.1.0", want: true},
|
||||
{name: "空白包裹", ua: " codex_vscode/1.2.3 ", want: true},
|
||||
{name: "非 codex", ua: "curl/8.0.1", want: false},
|
||||
{name: "空字符串", ua: "", want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := IsCodexCLIRequest(tt.ua)
|
||||
if got != tt.want {
|
||||
t.Fatalf("IsCodexCLIRequest(%q) = %v, want %v", tt.ua, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user