diff --git a/.gitignore b/.gitignore index 9df44de3..81523607 100644 --- a/.gitignore +++ b/.gitignore @@ -27,7 +27,6 @@ static/* !static/icon.png !static/icon.ico test/ -refs/* dist/* .proxypilot/* memory/* @@ -56,8 +55,8 @@ GEMINI.md # Tooling metadata .vscode/* -.claude/* .codex/* +.claude/* .gemini/* .serena/* .agent/* @@ -109,7 +108,6 @@ tray_click_log.txt node_modules/ dist/ .turbo/ -.env build/ # Local files diff --git a/UPSTREAM_VERSION b/UPSTREAM_VERSION new file mode 100644 index 00000000..c99b8819 --- /dev/null +++ b/UPSTREAM_VERSION @@ -0,0 +1 @@ +v6.7.42 \ No newline at end of file diff --git a/cmd/server/main.go b/cmd/server/main.go index 08a04f6b..896736e2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -73,6 +73,7 @@ func main() { var claudeLogin bool var qwenLogin bool var noBrowser bool + var oauthCallbackPort int var antigravityLogin bool var kiroLogin bool var kiroGoogleLogin bool @@ -141,6 +142,7 @@ func main() { flag.BoolVar(&noBrowser, "no-browser", false, "Don't open browser automatically for OAuth") flag.BoolVar(&useIncognito, "incognito", false, "Open browser in incognito/private mode for OAuth (useful for multiple accounts)") flag.BoolVar(&noIncognito, "no-incognito", false, "Force disable incognito mode (uses existing browser session)") + flag.IntVar(&oauthCallbackPort, "oauth-callback-port", 0, "Override OAuth callback port (defaults to provider-specific port)") flag.BoolVar(&antigravityLogin, "antigravity-login", false, "Login to Antigravity using OAuth") flag.BoolVar(&kiroLogin, "kiro-login", false, "Login to Kiro using Google OAuth") flag.BoolVar(&kiroGoogleLogin, "kiro-google-login", false, "Login to Kiro using Google OAuth (same as --kiro-login)") @@ -619,7 +621,8 @@ func main() { // Create login options to be used in authentication flows. options := &cmd.LoginOptions{ - NoBrowser: noBrowser, + NoBrowser: noBrowser, + CallbackPort: oauthCallbackPort, } // Register the shared token store once so all components use the same persistence backend. diff --git a/docker-compose.yml b/docker-compose.yml index 730a8836..2fde4fc6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,7 +22,7 @@ services: - "51121:51121" - "11451:11451" volumes: - - ./config.yaml:/ProxyPilot/config.yaml - - ./auths:/root/.cli-proxy-api - - ./logs:/ProxyPilot/logs + - ${CLI_PROXY_CONFIG_PATH:-./config.yaml}:/ProxyPilot/config.yaml + - ${CLI_PROXY_AUTH_PATH:-./auths}:/root/.cli-proxy-api + - ${CLI_PROXY_LOG_PATH:-./logs}:/ProxyPilot/logs restart: unless-stopped diff --git a/examples/custom-provider/main.go b/examples/custom-provider/main.go index c83e90d7..2f530d7c 100644 --- a/examples/custom-provider/main.go +++ b/examples/custom-provider/main.go @@ -21,7 +21,6 @@ import ( "os" "path/filepath" "strings" - "sync" "time" "github.com/gin-gonic/gin" @@ -33,8 +32,6 @@ import ( "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" "github.com/router-for-me/CLIProxyAPI/v6/sdk/logging" sdktr "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" - "github.com/tidwall/gjson" - "github.com/tiktoken-go/tokenizer" ) const ( @@ -65,185 +62,6 @@ func init() { ) } -// ============================================================================ -// Token Counting Utilities -// ============================================================================ -// -// This section demonstrates how to implement token counting for a custom provider. -// Token counting is important for: -// - Quota management and billing estimation -// - Ensuring requests don't exceed model context limits -// - Client-side optimization and request batching -// -// There are several approaches you can use: -// -// 1. SIMPLE ESTIMATION (shown in estimateTokens): -// - Fast and requires no external dependencies beyond string operations -// - Uses character count / 4 as a rough approximation -// - Good for quick estimates, less accurate for complex text -// -// 2. TIKTOKEN (shown in countWithTiktoken): -// - Uses OpenAI's tiktoken library for accurate BPE token counting -// - Best for OpenAI-compatible models (GPT-4, GPT-3.5, etc.) -// - Very accurate but requires the tiktoken-go package -// -// 3. UPSTREAM API (not shown): -// - Call your provider's token counting API endpoint -// - Most accurate but adds latency and API costs -// - Implement this if your provider offers a /count_tokens endpoint -// -// Choose the approach that best fits your accuracy needs and performance requirements. - -var ( - // tokenizerCache stores tokenizer instances to avoid repeated initialization. - // The sync.Map is safe for concurrent access. - tokenizerCache sync.Map -) - -// getOrCreateTokenizer returns a cached tokenizer codec for the given model. -// This improves performance by avoiding repeated tokenizer initialization. -// -// Customize this function to map your provider's model names to appropriate -// tokenizer encodings. The tiktoken-go library provides encodings for: -// - O200kBase: GPT-4o, GPT-5, and newer models (default fallback) -// - Cl100kBase: GPT-4, GPT-3.5-turbo -// - P50kBase: text-davinci-003, Codex models -func getOrCreateTokenizer(model string) (tokenizer.Codec, error) { - // Check cache first - if cached, ok := tokenizerCache.Load(model); ok { - return cached.(tokenizer.Codec), nil - } - - // Select encoding based on model prefix - // Customize this mapping for your provider's model naming convention - var enc tokenizer.Codec - var err error - - sanitized := strings.ToLower(strings.TrimSpace(model)) - switch { - // Example: Map your custom models to appropriate encodings - case strings.HasPrefix(sanitized, "myprov-gpt4"): - enc, err = tokenizer.ForModel(tokenizer.GPT4) - case strings.HasPrefix(sanitized, "myprov-gpt3"): - enc, err = tokenizer.ForModel(tokenizer.GPT35Turbo) - case strings.HasPrefix(sanitized, "gpt-4o"), strings.HasPrefix(sanitized, "gpt-4.1"): - enc, err = tokenizer.ForModel(tokenizer.GPT4o) - case strings.HasPrefix(sanitized, "gpt-4"): - enc, err = tokenizer.ForModel(tokenizer.GPT4) - case strings.HasPrefix(sanitized, "gpt-3.5"): - enc, err = tokenizer.ForModel(tokenizer.GPT35Turbo) - default: - // Default to O200kBase - the most recent encoding for modern models - enc, err = tokenizer.Get(tokenizer.O200kBase) - } - - if err != nil { - return nil, err - } - - // Store in cache (use LoadOrStore to handle race conditions) - actual, _ := tokenizerCache.LoadOrStore(model, enc) - return actual.(tokenizer.Codec), nil -} - -// estimateTokens provides a simple token count estimation without external libraries. -// This is useful as a fallback when tiktoken is unavailable or for quick approximations. -// -// The algorithm uses character count / 4, which is based on the observation that -// English text averages about 4 characters per token. This is less accurate for: -// - Code (typically more tokens per character) -// - Non-English text (varies by language) -// - Text with many special characters or numbers -// -// For production use, consider tiktoken for better accuracy. -func estimateTokens(text string) int { - if len(text) == 0 { - return 0 - } - // Simple estimation: ~4 characters per token on average - // This is a reasonable approximation for English text - tokens := len(text) / 4 - if tokens == 0 && len(text) > 0 { - tokens = 1 // At least 1 token for non-empty text - } - return tokens -} - -// countWithTiktoken counts tokens using the tiktoken BPE tokenizer. -// This provides accurate token counts for OpenAI-compatible models. -// -// For custom providers with different tokenization schemes, you may need to: -// - Apply an adjustment factor (e.g., multiply by 1.1 for Claude models) -// - Use a custom tokenizer that matches your provider's implementation -// - Call your provider's native token counting API instead -func countWithTiktoken(enc tokenizer.Codec, text string) (int, error) { - if enc == nil { - return 0, fmt.Errorf("tokenizer is nil") - } - _, tokens, err := enc.Encode(text) - if err != nil { - return 0, err - } - return len(tokens), nil -} - -// extractMessagesText extracts all text content from an OpenAI-format chat request. -// This handles the standard messages array structure with role/content fields. -// -// Customize this function if your provider uses a different request format. -func extractMessagesText(payload []byte) string { - if len(payload) == 0 { - return "" - } - - root := gjson.ParseBytes(payload) - var segments []string - - // Extract from messages array (OpenAI chat format) - messages := root.Get("messages") - if messages.Exists() && messages.IsArray() { - messages.ForEach(func(_, msg gjson.Result) bool { - // Extract role for context (adds ~1 token overhead per message) - if role := msg.Get("role").String(); role != "" { - segments = append(segments, role) - } - // Extract text content - content := msg.Get("content") - if content.IsArray() { - // Handle array content (e.g., with images) - content.ForEach(func(_, part gjson.Result) bool { - if part.Get("type").String() == "text" { - if text := part.Get("text").String(); text != "" { - segments = append(segments, text) - } - } - return true - }) - } else if text := content.String(); text != "" { - segments = append(segments, text) - } - return true - }) - } - - // Extract from prompt field (completion format) - if prompt := root.Get("prompt").String(); prompt != "" { - segments = append(segments, prompt) - } - - // Extract from input field (some APIs use this) - if input := root.Get("input").String(); input != "" { - segments = append(segments, input) - } - - // Extract system prompt if present at top level - if system := root.Get("system").String(); system != "" { - segments = append(segments, system) - } - - return strings.Join(segments, "\n") -} - // MyExecutor is a minimal provider implementation for demonstration purposes. // It implements the Executor interface to handle requests to a custom AI provider. type MyExecutor struct{} @@ -322,57 +140,6 @@ func (MyExecutor) Execute(ctx context.Context, a *coreauth.Auth, req clipexec.Re return clipexec.Response{Payload: body}, nil } -// CountTokens counts tokens in the request payload. -// This implementation demonstrates a layered approach: -// 1. Try tiktoken for accurate BPE-based counting -// 2. Fall back to simple character-based estimation if tiktoken fails -// -// The response format follows the Anthropic API convention: {"count": } -// Adjust the response format to match your provider's expected format. -// -// For production use, consider: -// - Caching token counts for identical payloads -// - Using your provider's native token counting API if available -// - Applying model-specific adjustment factors -func (MyExecutor) CountTokens(_ context.Context, _ *coreauth.Auth, req clipexec.Request, _ clipexec.Options) (clipexec.Response, error) { - var tokenCount int - - // Extract text content from the request payload - text := extractMessagesText(req.Payload) - - // If no text was extracted from structured fields, use raw payload - if text == "" && len(req.Payload) > 0 { - text = string(req.Payload) - } - - // Try tiktoken first for accurate counting - enc, err := getOrCreateTokenizer(req.Model) - if err == nil { - // Successfully got a tokenizer, use it for accurate counting - count, countErr := countWithTiktoken(enc, text) - if countErr == nil { - tokenCount = count - } else { - // Tiktoken encoding failed, fall back to estimation - tokenCount = estimateTokens(text) - } - } else { - // Could not get tokenizer, fall back to simple estimation - tokenCount = estimateTokens(text) - } - - // Return response in the expected format - // The format follows Anthropic's count_tokens response: {"count": } - // Adjust this format to match your provider's API specification - response := fmt.Sprintf(`{"count":%d}`, tokenCount) - - return clipexec.Response{Payload: []byte(response)}, nil -} - -func (MyExecutor) Embed(context.Context, *coreauth.Auth, clipexec.Request, clipexec.Options) (clipexec.Response, error) { - return clipexec.Response{}, errors.New("embeddings not implemented") -} - func (MyExecutor) HttpRequest(ctx context.Context, a *coreauth.Auth, req *http.Request) (*http.Response, error) { if req == nil { return nil, fmt.Errorf("myprov executor: request is nil") @@ -388,6 +155,10 @@ func (MyExecutor) HttpRequest(ctx context.Context, a *coreauth.Auth, req *http.R return client.Do(httpReq) } +func (MyExecutor) CountTokens(context.Context, *coreauth.Auth, clipexec.Request, clipexec.Options) (clipexec.Response, error) { + return clipexec.Response{}, errors.New("count tokens not implemented") +} + func (MyExecutor) ExecuteStream(ctx context.Context, a *coreauth.Auth, req clipexec.Request, opts clipexec.Options) (<-chan clipexec.StreamChunk, error) { ch := make(chan clipexec.StreamChunk, 1) go func() { diff --git a/examples/http-request/main.go b/examples/http-request/main.go index 280dec9a..4daee547 100644 --- a/examples/http-request/main.go +++ b/examples/http-request/main.go @@ -70,10 +70,6 @@ func (EchoExecutor) CountTokens(context.Context, *coreauth.Auth, clipexec.Reques return clipexec.Response{}, errors.New("echo executor: CountTokens not implemented") } -func (EchoExecutor) Embed(context.Context, *coreauth.Auth, clipexec.Request, clipexec.Options) (clipexec.Response, error) { - return clipexec.Response{}, errors.New("echo executor: Embed not implemented") -} - func main() { log.SetLevel(log.InfoLevel) diff --git a/internal/api/server.go b/internal/api/server.go index ed737aa6..bbb9740e 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -876,36 +876,21 @@ func (s *Server) UpdateClients(cfg *config.Config) { } else if toggler, ok := s.requestLogger.(interface{ SetEnabled(bool) }); ok { toggler.SetEnabled(cfg.RequestLog) } - if oldCfg != nil { - log.Debugf("request logging updated from %t to %t", previousRequestLog, cfg.RequestLog) - } else { - log.Debugf("request logging toggled to %t", cfg.RequestLog) - } } if oldCfg == nil || oldCfg.LoggingToFile != cfg.LoggingToFile || oldCfg.LogsMaxTotalSizeMB != cfg.LogsMaxTotalSizeMB { if err := logging.ConfigureLogOutput(cfg); err != nil { log.Errorf("failed to reconfigure log output: %v", err) - } else { - if oldCfg == nil { - log.Debug("log output configuration refreshed") - } else { - if oldCfg.LoggingToFile != cfg.LoggingToFile { - log.Debugf("logging_to_file updated from %t to %t", oldCfg.LoggingToFile, cfg.LoggingToFile) - } - if oldCfg.LogsMaxTotalSizeMB != cfg.LogsMaxTotalSizeMB { - log.Debugf("logs_max_total_size_mb updated from %d to %d", oldCfg.LogsMaxTotalSizeMB, cfg.LogsMaxTotalSizeMB) - } - } } } if oldCfg == nil || oldCfg.UsageStatisticsEnabled != cfg.UsageStatisticsEnabled { usage.SetStatisticsEnabled(cfg.UsageStatisticsEnabled) - if oldCfg != nil { - log.Debugf("usage_statistics_enabled updated from %t to %t", oldCfg.UsageStatisticsEnabled, cfg.UsageStatisticsEnabled) - } else { - log.Debugf("usage_statistics_enabled toggled to %t", cfg.UsageStatisticsEnabled) + } + + if s.requestLogger != nil && (oldCfg == nil || oldCfg.ErrorLogsMaxFiles != cfg.ErrorLogsMaxFiles) { + if setter, ok := s.requestLogger.(interface{ SetErrorLogsMaxFiles(int) }); ok { + setter.SetErrorLogsMaxFiles(cfg.ErrorLogsMaxFiles) } } @@ -920,11 +905,6 @@ func (s *Server) UpdateClients(cfg *config.Config) { if oldCfg == nil || oldCfg.DisableCooling != cfg.DisableCooling { auth.SetQuotaCooldownDisabled(cfg.DisableCooling) - if oldCfg != nil { - log.Debugf("disable_cooling updated from %t to %t", oldCfg.DisableCooling, cfg.DisableCooling) - } else { - log.Debugf("disable_cooling toggled to %t", cfg.DisableCooling) - } } if s.handlers != nil && s.handlers.AuthManager != nil { @@ -934,11 +914,6 @@ func (s *Server) UpdateClients(cfg *config.Config) { // Update log level dynamically when debug flag changes if oldCfg == nil || oldCfg.Debug != cfg.Debug { util.SetLogLevel(cfg) - if oldCfg != nil { - log.Debugf("debug mode updated from %t to %t", oldCfg.Debug, cfg.Debug) - } else { - log.Debugf("debug mode toggled to %t", cfg.Debug) - } } prevSecretEmpty := true diff --git a/internal/registry/model_definitions_static_data.go b/internal/registry/model_definitions_static_data.go index b1a524fb..cf5f1402 100644 --- a/internal/registry/model_definitions_static_data.go +++ b/internal/registry/model_definitions_static_data.go @@ -784,7 +784,7 @@ func GetIFlowModels() []*ModelInfo { {ID: "qwen3-coder-plus", DisplayName: "Qwen3-Coder-Plus", Description: "Qwen3 Coder Plus code generation", Created: 1753228800}, {ID: "qwen3-max", DisplayName: "Qwen3-Max", Description: "Qwen3 flagship model", Created: 1758672000}, {ID: "qwen3-vl-plus", DisplayName: "Qwen3-VL-Plus", Description: "Qwen3 multimodal vision-language", Created: 1758672000}, - {ID: "qwen3-max-preview", DisplayName: "Qwen3-Max-Preview", Description: "Qwen3 Max preview build", Created: 1757030400}, + {ID: "qwen3-max-preview", DisplayName: "Qwen3-Max-Preview", Description: "Qwen3 Max preview build", Created: 1757030400, Thinking: iFlowThinkingSupport}, {ID: "kimi-k2-0905", DisplayName: "Kimi-K2-Instruct-0905", Description: "Moonshot Kimi K2 instruct 0905", Created: 1757030400}, {ID: "glm-4.6", DisplayName: "GLM-4.6", Description: "Zhipu GLM 4.6 general model", Created: 1759190400, Thinking: iFlowThinkingSupport}, {ID: "glm-4.7", DisplayName: "GLM-4.7", Description: "Zhipu GLM 4.7 general model", Created: 1766448000, Thinking: iFlowThinkingSupport}, @@ -792,8 +792,8 @@ func GetIFlowModels() []*ModelInfo { {ID: "kimi-k2-thinking", DisplayName: "Kimi-K2-Thinking", Description: "Moonshot Kimi K2 thinking model", Created: 1762387200}, {ID: "deepseek-v3.2-chat", DisplayName: "DeepSeek-V3.2", Description: "DeepSeek V3.2 Chat", Created: 1764576000}, {ID: "deepseek-v3.2-reasoner", DisplayName: "DeepSeek-V3.2", Description: "DeepSeek V3.2 Reasoner", Created: 1764576000}, - {ID: "deepseek-v3.2", DisplayName: "DeepSeek-V3.2-Exp", Description: "DeepSeek V3.2 experimental", Created: 1759104000}, - {ID: "deepseek-v3.1", DisplayName: "DeepSeek-V3.1-Terminus", Description: "DeepSeek V3.1 Terminus", Created: 1756339200}, + {ID: "deepseek-v3.2", DisplayName: "DeepSeek-V3.2-Exp", Description: "DeepSeek V3.2 experimental", Created: 1759104000, Thinking: iFlowThinkingSupport}, + {ID: "deepseek-v3.1", DisplayName: "DeepSeek-V3.1-Terminus", Description: "DeepSeek V3.1 Terminus", Created: 1756339200, Thinking: iFlowThinkingSupport}, {ID: "deepseek-r1", DisplayName: "DeepSeek-R1", Description: "DeepSeek reasoning model R1", Created: 1737331200}, {ID: "deepseek-v3", DisplayName: "DeepSeek-V3-671B", Description: "DeepSeek V3 671B", Created: 1734307200}, {ID: "qwen3-32b", DisplayName: "Qwen3-32B", Description: "Qwen3 32B", Created: 1747094400}, diff --git a/internal/runtime/executor/cache_helpers.go b/internal/runtime/executor/cache_helpers.go index 1e32f43a..b6de886d 100644 --- a/internal/runtime/executor/cache_helpers.go +++ b/internal/runtime/executor/cache_helpers.go @@ -29,7 +29,6 @@ func startCodexCacheCleanup() { go func() { ticker := time.NewTicker(codexCacheCleanupInterval) defer ticker.Stop() - for range ticker.C { purgeExpiredCodexCache() } @@ -39,10 +38,8 @@ func startCodexCacheCleanup() { // purgeExpiredCodexCache removes entries that have expired. func purgeExpiredCodexCache() { now := time.Now() - codexCacheMu.Lock() defer codexCacheMu.Unlock() - for key, cache := range codexCacheMap { if cache.Expire.Before(now) { delete(codexCacheMap, key) @@ -69,10 +66,3 @@ func setCodexCache(key string, cache codexCache) { codexCacheMap[key] = cache codexCacheMu.Unlock() } - -// deleteCodexCache deletes a cache entry. -func deleteCodexCache(key string) { - codexCacheMu.Lock() - delete(codexCacheMap, key) - codexCacheMu.Unlock() -} diff --git a/internal/runtime/executor/gemini_vertex_executor.go b/internal/runtime/executor/gemini_vertex_executor.go index 83456a86..2db0e37c 100644 --- a/internal/runtime/executor/gemini_vertex_executor.go +++ b/internal/runtime/executor/gemini_vertex_executor.go @@ -1003,6 +1003,8 @@ func vertexBaseURL(location string) string { loc := strings.TrimSpace(location) if loc == "" { loc = "us-central1" + } else if loc == "global" { + return "https://aiplatform.googleapis.com" } return fmt.Sprintf("https://%s-aiplatform.googleapis.com", loc) } diff --git a/internal/thinking/provider/iflow/apply.go b/internal/thinking/provider/iflow/apply.go index da986d22..35d13f59 100644 --- a/internal/thinking/provider/iflow/apply.go +++ b/internal/thinking/provider/iflow/apply.go @@ -1,7 +1,7 @@ -// Package iflow implements thinking configuration for iFlow models (GLM, MiniMax). +// Package iflow implements thinking configuration for iFlow models. // // iFlow models use boolean toggle semantics: -// - GLM models: chat_template_kwargs.enable_thinking (boolean) +// - Models using chat_template_kwargs.enable_thinking (boolean toggle) // - MiniMax models: reasoning_split (boolean) // // Level values are converted to boolean: none=false, all others=true @@ -20,6 +20,7 @@ import ( // Applier implements thinking.ProviderApplier for iFlow models. // // iFlow-specific behavior: +// - enable_thinking toggle models: enable_thinking boolean // - GLM models: enable_thinking boolean + clear_thinking=false // - MiniMax models: reasoning_split boolean // - Level to boolean: none=false, others=true @@ -61,8 +62,8 @@ func (a *Applier) Apply(body []byte, config thinking.ThinkingConfig, modelInfo * return body, nil } - if isGLMModel(modelInfo.ID) { - return applyGLM(body, config), nil + if isEnableThinkingModel(modelInfo.ID) { + return applyEnableThinking(body, config, isGLMModel(modelInfo.ID)), nil } if isMiniMaxModel(modelInfo.ID) { @@ -97,7 +98,8 @@ func configToBoolean(config thinking.ThinkingConfig) bool { } } -// applyGLM applies thinking configuration for GLM models. +// applyEnableThinking applies thinking configuration for models that use +// chat_template_kwargs.enable_thinking format. // // Output format when enabled: // @@ -107,9 +109,8 @@ func configToBoolean(config thinking.ThinkingConfig) bool { // // {"chat_template_kwargs": {"enable_thinking": false}} // -// Note: clear_thinking is only set when thinking is enabled, to preserve -// thinking output in the response. -func applyGLM(body []byte, config thinking.ThinkingConfig) []byte { +// Note: clear_thinking is only set for GLM models when thinking is enabled. +func applyEnableThinking(body []byte, config thinking.ThinkingConfig, setClearThinking bool) []byte { enableThinking := configToBoolean(config) if len(body) == 0 || !gjson.ValidBytes(body) { @@ -118,8 +119,11 @@ func applyGLM(body []byte, config thinking.ThinkingConfig) []byte { result, _ := sjson.SetBytes(body, "chat_template_kwargs.enable_thinking", enableThinking) + // clear_thinking is a GLM-only knob, strip it for other models. + result, _ = sjson.DeleteBytes(result, "chat_template_kwargs.clear_thinking") + // clear_thinking only needed when thinking is enabled - if enableThinking { + if enableThinking && setClearThinking { result, _ = sjson.SetBytes(result, "chat_template_kwargs.clear_thinking", false) } @@ -143,8 +147,21 @@ func applyMiniMax(body []byte, config thinking.ThinkingConfig) []byte { return result } +// isEnableThinkingModel determines if the model uses chat_template_kwargs.enable_thinking format. +func isEnableThinkingModel(modelID string) bool { + if isGLMModel(modelID) { + return true + } + id := strings.ToLower(modelID) + switch id { + case "qwen3-max-preview", "deepseek-v3.2", "deepseek-v3.1": + return true + default: + return false + } +} + // isGLMModel determines if the model is a GLM series model. -// GLM models use chat_template_kwargs.enable_thinking format. func isGLMModel(modelID string) bool { return strings.HasPrefix(strings.ToLower(modelID), "glm") } diff --git a/internal/watcher/diff/oauth_excluded.go b/internal/watcher/diff/oauth_excluded.go index 0efd44df..2039cf48 100644 --- a/internal/watcher/diff/oauth_excluded.go +++ b/internal/watcher/diff/oauth_excluded.go @@ -1,118 +1,118 @@ -package diff - -import ( - "crypto/sha256" - "encoding/hex" - "fmt" - "sort" - "strings" - - "github.com/router-for-me/CLIProxyAPI/v6/internal/config" -) - -type ExcludedModelsSummary struct { - hash string - count int -} - -// SummarizeExcludedModels normalizes and hashes an excluded-model list. -func SummarizeExcludedModels(list []string) ExcludedModelsSummary { - if len(list) == 0 { - return ExcludedModelsSummary{} - } - seen := make(map[string]struct{}, len(list)) - normalized := make([]string, 0, len(list)) - for _, entry := range list { - if trimmed := strings.ToLower(strings.TrimSpace(entry)); trimmed != "" { - if _, exists := seen[trimmed]; exists { - continue - } - seen[trimmed] = struct{}{} - normalized = append(normalized, trimmed) - } - } - sort.Strings(normalized) - return ExcludedModelsSummary{ - hash: ComputeExcludedModelsHash(normalized), - count: len(normalized), - } -} - -// SummarizeOAuthExcludedModels summarizes OAuth excluded models per provider. -func SummarizeOAuthExcludedModels(entries map[string][]string) map[string]ExcludedModelsSummary { - if len(entries) == 0 { - return nil - } - out := make(map[string]ExcludedModelsSummary, len(entries)) - for k, v := range entries { - key := strings.ToLower(strings.TrimSpace(k)) - if key == "" { - continue - } - out[key] = SummarizeExcludedModels(v) - } - return out -} - -// DiffOAuthExcludedModelChanges compares OAuth excluded models maps. -func DiffOAuthExcludedModelChanges(oldMap, newMap map[string][]string) ([]string, []string) { - oldSummary := SummarizeOAuthExcludedModels(oldMap) - newSummary := SummarizeOAuthExcludedModels(newMap) - keys := make(map[string]struct{}, len(oldSummary)+len(newSummary)) - for k := range oldSummary { - keys[k] = struct{}{} - } - for k := range newSummary { - keys[k] = struct{}{} - } - changes := make([]string, 0, len(keys)) - affected := make([]string, 0, len(keys)) - for key := range keys { - oldInfo, okOld := oldSummary[key] - newInfo, okNew := newSummary[key] - switch { - case okOld && !okNew: - changes = append(changes, fmt.Sprintf("oauth-excluded-models[%s]: removed", key)) - affected = append(affected, key) - case !okOld && okNew: - changes = append(changes, fmt.Sprintf("oauth-excluded-models[%s]: added (%d entries)", key, newInfo.count)) - affected = append(affected, key) - case okOld && okNew && oldInfo.hash != newInfo.hash: - changes = append(changes, fmt.Sprintf("oauth-excluded-models[%s]: updated (%d -> %d entries)", key, oldInfo.count, newInfo.count)) - affected = append(affected, key) - } - } - sort.Strings(changes) - sort.Strings(affected) - return changes, affected -} - -type AmpModelMappingsSummary struct { - hash string - count int -} - -// SummarizeAmpModelMappings hashes Amp model mappings for change detection. -func SummarizeAmpModelMappings(mappings []config.AmpModelMapping) AmpModelMappingsSummary { - if len(mappings) == 0 { - return AmpModelMappingsSummary{} - } - entries := make([]string, 0, len(mappings)) - for _, mapping := range mappings { - from := strings.TrimSpace(mapping.From) - to := strings.TrimSpace(mapping.To) - if from == "" && to == "" { - continue - } - entries = append(entries, from+"->"+to) - } - if len(entries) == 0 { - return AmpModelMappingsSummary{} - } - sort.Strings(entries) - sum := sha256.Sum256([]byte(strings.Join(entries, "|"))) - return AmpModelMappingsSummary{ - hash: hex.EncodeToString(sum[:]), - count: len(entries), - } -} +package diff + +import ( + "crypto/sha256" + "encoding/hex" + "fmt" + "sort" + "strings" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" +) + +type ExcludedModelsSummary struct { + hash string + count int +} + +// SummarizeExcludedModels normalizes and hashes an excluded-model list. +func SummarizeExcludedModels(list []string) ExcludedModelsSummary { + if len(list) == 0 { + return ExcludedModelsSummary{} + } + seen := make(map[string]struct{}, len(list)) + normalized := make([]string, 0, len(list)) + for _, entry := range list { + if trimmed := strings.ToLower(strings.TrimSpace(entry)); trimmed != "" { + if _, exists := seen[trimmed]; exists { + continue + } + seen[trimmed] = struct{}{} + normalized = append(normalized, trimmed) + } + } + sort.Strings(normalized) + return ExcludedModelsSummary{ + hash: ComputeExcludedModelsHash(normalized), + count: len(normalized), + } +} + +// SummarizeOAuthExcludedModels summarizes OAuth excluded models per provider. +func SummarizeOAuthExcludedModels(entries map[string][]string) map[string]ExcludedModelsSummary { + if len(entries) == 0 { + return nil + } + out := make(map[string]ExcludedModelsSummary, len(entries)) + for k, v := range entries { + key := strings.ToLower(strings.TrimSpace(k)) + if key == "" { + continue + } + out[key] = SummarizeExcludedModels(v) + } + return out +} + +// DiffOAuthExcludedModelChanges compares OAuth excluded models maps. +func DiffOAuthExcludedModelChanges(oldMap, newMap map[string][]string) ([]string, []string) { + oldSummary := SummarizeOAuthExcludedModels(oldMap) + newSummary := SummarizeOAuthExcludedModels(newMap) + keys := make(map[string]struct{}, len(oldSummary)+len(newSummary)) + for k := range oldSummary { + keys[k] = struct{}{} + } + for k := range newSummary { + keys[k] = struct{}{} + } + changes := make([]string, 0, len(keys)) + affected := make([]string, 0, len(keys)) + for key := range keys { + oldInfo, okOld := oldSummary[key] + newInfo, okNew := newSummary[key] + switch { + case okOld && !okNew: + changes = append(changes, fmt.Sprintf("oauth-excluded-models[%s]: removed", key)) + affected = append(affected, key) + case !okOld && okNew: + changes = append(changes, fmt.Sprintf("oauth-excluded-models[%s]: added (%d entries)", key, newInfo.count)) + affected = append(affected, key) + case okOld && okNew && oldInfo.hash != newInfo.hash: + changes = append(changes, fmt.Sprintf("oauth-excluded-models[%s]: updated (%d -> %d entries)", key, oldInfo.count, newInfo.count)) + affected = append(affected, key) + } + } + sort.Strings(changes) + sort.Strings(affected) + return changes, affected +} + +type AmpModelMappingsSummary struct { + hash string + count int +} + +// SummarizeAmpModelMappings hashes Amp model mappings for change detection. +func SummarizeAmpModelMappings(mappings []config.AmpModelMapping) AmpModelMappingsSummary { + if len(mappings) == 0 { + return AmpModelMappingsSummary{} + } + entries := make([]string, 0, len(mappings)) + for _, mapping := range mappings { + from := strings.TrimSpace(mapping.From) + to := strings.TrimSpace(mapping.To) + if from == "" && to == "" { + continue + } + entries = append(entries, from+"->"+to) + } + if len(entries) == 0 { + return AmpModelMappingsSummary{} + } + sort.Strings(entries) + sum := sha256.Sum256([]byte(strings.Join(entries, "|"))) + return AmpModelMappingsSummary{ + hash: hex.EncodeToString(sum[:]), + count: len(entries), + } +} diff --git a/scripts/docker-build.sh b/scripts/docker-build.sh index edfd5ead..944f3e78 100644 --- a/scripts/docker-build.sh +++ b/scripts/docker-build.sh @@ -5,9 +5,115 @@ # This script automates the process of building and running the Docker container # with version information dynamically injected at build time. -# Exit immediately if a command exits with a non-zero status. +# Hidden feature: Preserve usage statistics across rebuilds +# Usage: ./docker-build.sh --with-usage +# First run prompts for management API key, saved to temp/stats/.api_secret + set -euo pipefail +STATS_DIR="temp/stats" +STATS_FILE="${STATS_DIR}/.usage_backup.json" +SECRET_FILE="${STATS_DIR}/.api_secret" +WITH_USAGE=false + +get_port() { + if [[ -f "config.yaml" ]]; then + grep -E "^port:" config.yaml | sed -E 's/^port: *["'"'"']?([0-9]+)["'"'"']?.*$/\1/' + else + echo "8317" + fi +} + +export_stats_api_secret() { + if [[ -f "${SECRET_FILE}" ]]; then + API_SECRET=$(cat "${SECRET_FILE}") + else + if [[ ! -d "${STATS_DIR}" ]]; then + mkdir -p "${STATS_DIR}" + fi + echo "First time using --with-usage. Management API key required." + read -r -p "Enter management key: " -s API_SECRET + echo + echo "${API_SECRET}" > "${SECRET_FILE}" + chmod 600 "${SECRET_FILE}" + fi +} + +check_container_running() { + local port + port=$(get_port) + + if ! curl -s -o /dev/null -w "%{http_code}" "http://localhost:${port}/" | grep -q "200"; then + echo "Error: cli-proxy-api service is not responding at localhost:${port}" + echo "Please start the container first or use without --with-usage flag." + exit 1 + fi +} + +export_stats() { + local port + port=$(get_port) + + if [[ ! -d "${STATS_DIR}" ]]; then + mkdir -p "${STATS_DIR}" + fi + check_container_running + echo "Exporting usage statistics..." + EXPORT_RESPONSE=$(curl -s -w "\n%{http_code}" -H "X-Management-Key: ${API_SECRET}" \ + "http://localhost:${port}/v0/management/usage/export") + HTTP_CODE=$(echo "${EXPORT_RESPONSE}" | tail -n1) + RESPONSE_BODY=$(echo "${EXPORT_RESPONSE}" | sed '$d') + + if [[ "${HTTP_CODE}" != "200" ]]; then + echo "Export failed (HTTP ${HTTP_CODE}): ${RESPONSE_BODY}" + exit 1 + fi + + echo "${RESPONSE_BODY}" > "${STATS_FILE}" + echo "Statistics exported to ${STATS_FILE}" +} + +import_stats() { + local port + port=$(get_port) + + echo "Importing usage statistics..." + IMPORT_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \ + -H "X-Management-Key: ${API_SECRET}" \ + -H "Content-Type: application/json" \ + -d @"${STATS_FILE}" \ + "http://localhost:${port}/v0/management/usage/import") + IMPORT_CODE=$(echo "${IMPORT_RESPONSE}" | tail -n1) + IMPORT_BODY=$(echo "${IMPORT_RESPONSE}" | sed '$d') + + if [[ "${IMPORT_CODE}" == "200" ]]; then + echo "Statistics imported successfully" + else + echo "Import failed (HTTP ${IMPORT_CODE}): ${IMPORT_BODY}" + fi + + rm -f "${STATS_FILE}" +} + +wait_for_service() { + local port + port=$(get_port) + + echo "Waiting for service to be ready..." + for i in {1..30}; do + if curl -s -o /dev/null -w "%{http_code}" "http://localhost:${port}/" | grep -q "200"; then + break + fi + sleep 1 + done + sleep 2 +} + +if [[ "${1:-}" == "--with-usage" ]]; then + WITH_USAGE=true + export_stats_api_secret +fi + # --- Step 1: Choose Environment --- echo "Please select an option:" echo "1) Run using Pre-built Image (Recommended)" @@ -18,7 +124,14 @@ read -r -p "Enter choice [1-2]: " choice case "$choice" in 1) echo "--- Running with Pre-built Image ---" + if [[ "${WITH_USAGE}" == "true" ]]; then + export_stats + fi docker compose up -d --remove-orphans --no-build + if [[ "${WITH_USAGE}" == "true" ]]; then + wait_for_service + import_stats + fi echo "Services are starting from remote image." echo "Run 'docker compose logs -f' to see the logs." ;; @@ -38,16 +151,25 @@ case "$choice" in # Build and start the services with a local-only image tag export CLI_PROXY_IMAGE="cli-proxy-api:local" - + echo "Building the Docker image..." docker compose build \ --build-arg VERSION="${VERSION}" \ --build-arg COMMIT="${COMMIT}" \ --build-arg BUILD_DATE="${BUILD_DATE}" + if [[ "${WITH_USAGE}" == "true" ]]; then + export_stats + fi + echo "Starting the services..." docker compose up -d --remove-orphans --pull never + if [[ "${WITH_USAGE}" == "true" ]]; then + wait_for_service + import_stats + fi + echo "Build complete. Services are starting." echo "Run 'docker compose logs -f' to see the logs." ;; @@ -55,4 +177,4 @@ case "$choice" in echo "Invalid choice. Please enter 1 or 2." exit 1 ;; -esac \ No newline at end of file +esac diff --git a/sdk/cliproxy/auth/api_key_model_alias_test.go b/sdk/cliproxy/auth/api_key_model_alias_test.go new file mode 100644 index 00000000..70915d9e --- /dev/null +++ b/sdk/cliproxy/auth/api_key_model_alias_test.go @@ -0,0 +1,180 @@ +package auth + +import ( + "context" + "testing" + + internalconfig "github.com/router-for-me/CLIProxyAPI/v6/internal/config" +) + +func TestLookupAPIKeyUpstreamModel(t *testing.T) { + cfg := &internalconfig.Config{ + GeminiKey: []internalconfig.GeminiKey{ + { + APIKey: "k", + BaseURL: "https://example.com", + Models: []internalconfig.GeminiModel{ + {Name: "gemini-2.5-pro-exp-03-25", Alias: "g25p"}, + {Name: "gemini-2.5-flash(low)", Alias: "g25f"}, + }, + }, + }, + } + + mgr := NewManager(nil, nil, nil) + mgr.SetConfig(cfg) + + ctx := context.Background() + _, _ = mgr.Register(ctx, &Auth{ID: "a1", Provider: "gemini", Attributes: map[string]string{"api_key": "k", "base_url": "https://example.com"}}) + + tests := []struct { + name string + authID string + input string + want string + }{ + // Fast path + suffix preservation + {"alias with suffix", "a1", "g25p(8192)", "gemini-2.5-pro-exp-03-25(8192)"}, + {"alias without suffix", "a1", "g25p", "gemini-2.5-pro-exp-03-25"}, + + // Config suffix takes priority + {"config suffix priority", "a1", "g25f(high)", "gemini-2.5-flash(low)"}, + {"config suffix no user suffix", "a1", "g25f", "gemini-2.5-flash(low)"}, + + // Case insensitive + {"uppercase alias", "a1", "G25P", "gemini-2.5-pro-exp-03-25"}, + {"mixed case with suffix", "a1", "G25p(4096)", "gemini-2.5-pro-exp-03-25(4096)"}, + + // Direct name lookup + {"upstream name direct", "a1", "gemini-2.5-pro-exp-03-25", "gemini-2.5-pro-exp-03-25"}, + {"upstream name with suffix", "a1", "gemini-2.5-pro-exp-03-25(8192)", "gemini-2.5-pro-exp-03-25(8192)"}, + + // Cache miss scenarios + {"non-existent auth", "non-existent", "g25p", ""}, + {"unknown alias", "a1", "unknown-alias", ""}, + {"empty auth ID", "", "g25p", ""}, + {"empty model", "a1", "", ""}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + resolved := mgr.lookupAPIKeyUpstreamModel(tt.authID, tt.input) + if resolved != tt.want { + t.Errorf("lookupAPIKeyUpstreamModel(%q, %q) = %q, want %q", tt.authID, tt.input, resolved, tt.want) + } + }) + } +} + +func TestAPIKeyModelAlias_ConfigHotReload(t *testing.T) { + cfg := &internalconfig.Config{ + GeminiKey: []internalconfig.GeminiKey{ + { + APIKey: "k", + Models: []internalconfig.GeminiModel{{Name: "gemini-2.5-pro-exp-03-25", Alias: "g25p"}}, + }, + }, + } + + mgr := NewManager(nil, nil, nil) + mgr.SetConfig(cfg) + + ctx := context.Background() + _, _ = mgr.Register(ctx, &Auth{ID: "a1", Provider: "gemini", Attributes: map[string]string{"api_key": "k"}}) + + // Initial alias + if resolved := mgr.lookupAPIKeyUpstreamModel("a1", "g25p"); resolved != "gemini-2.5-pro-exp-03-25" { + t.Fatalf("before reload: got %q, want %q", resolved, "gemini-2.5-pro-exp-03-25") + } + + // Hot reload with new alias + mgr.SetConfig(&internalconfig.Config{ + GeminiKey: []internalconfig.GeminiKey{ + { + APIKey: "k", + Models: []internalconfig.GeminiModel{{Name: "gemini-2.5-flash", Alias: "g25p"}}, + }, + }, + }) + + // New alias should take effect + if resolved := mgr.lookupAPIKeyUpstreamModel("a1", "g25p"); resolved != "gemini-2.5-flash" { + t.Fatalf("after reload: got %q, want %q", resolved, "gemini-2.5-flash") + } +} + +func TestAPIKeyModelAlias_MultipleProviders(t *testing.T) { + cfg := &internalconfig.Config{ + GeminiKey: []internalconfig.GeminiKey{{APIKey: "gemini-key", Models: []internalconfig.GeminiModel{{Name: "gemini-2.5-pro", Alias: "gp"}}}}, + ClaudeKey: []internalconfig.ClaudeKey{{APIKey: "claude-key", Models: []internalconfig.ClaudeModel{{Name: "claude-sonnet-4", Alias: "cs4"}}}}, + CodexKey: []internalconfig.CodexKey{{APIKey: "codex-key", Models: []internalconfig.CodexModel{{Name: "o3", Alias: "o"}}}}, + } + + mgr := NewManager(nil, nil, nil) + mgr.SetConfig(cfg) + + ctx := context.Background() + _, _ = mgr.Register(ctx, &Auth{ID: "gemini-auth", Provider: "gemini", Attributes: map[string]string{"api_key": "gemini-key"}}) + _, _ = mgr.Register(ctx, &Auth{ID: "claude-auth", Provider: "claude", Attributes: map[string]string{"api_key": "claude-key"}}) + _, _ = mgr.Register(ctx, &Auth{ID: "codex-auth", Provider: "codex", Attributes: map[string]string{"api_key": "codex-key"}}) + + tests := []struct { + authID, input, want string + }{ + {"gemini-auth", "gp", "gemini-2.5-pro"}, + {"claude-auth", "cs4", "claude-sonnet-4"}, + {"codex-auth", "o", "o3"}, + } + + for _, tt := range tests { + if resolved := mgr.lookupAPIKeyUpstreamModel(tt.authID, tt.input); resolved != tt.want { + t.Errorf("lookupAPIKeyUpstreamModel(%q, %q) = %q, want %q", tt.authID, tt.input, resolved, tt.want) + } + } +} + +func TestApplyAPIKeyModelAlias(t *testing.T) { + cfg := &internalconfig.Config{ + GeminiKey: []internalconfig.GeminiKey{ + {APIKey: "k", Models: []internalconfig.GeminiModel{{Name: "gemini-2.5-pro-exp-03-25", Alias: "g25p"}}}, + }, + } + + mgr := NewManager(nil, nil, nil) + mgr.SetConfig(cfg) + + ctx := context.Background() + apiKeyAuth := &Auth{ID: "a1", Provider: "gemini", Attributes: map[string]string{"api_key": "k"}} + oauthAuth := &Auth{ID: "oauth-auth", Provider: "gemini", Attributes: map[string]string{"auth_kind": "oauth"}} + _, _ = mgr.Register(ctx, apiKeyAuth) + + tests := []struct { + name string + auth *Auth + inputModel string + wantModel string + }{ + { + name: "api_key auth with alias", + auth: apiKeyAuth, + inputModel: "g25p(8192)", + wantModel: "gemini-2.5-pro-exp-03-25(8192)", + }, + { + name: "oauth auth passthrough", + auth: oauthAuth, + inputModel: "some-model", + wantModel: "some-model", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + resolvedModel := mgr.applyAPIKeyModelAlias(tt.auth, tt.inputModel) + + if resolvedModel != tt.wantModel { + t.Errorf("model = %q, want %q", resolvedModel, tt.wantModel) + } + }) + } +} diff --git a/sdk/cliproxy/auth/conductor.go b/sdk/cliproxy/auth/conductor.go index 39e2411e..e98932c1 100644 --- a/sdk/cliproxy/auth/conductor.go +++ b/sdk/cliproxy/auth/conductor.go @@ -658,9 +658,6 @@ func (m *Manager) normalizeProviders(providers []string) []string { return result } -// rotateProviders applies round-robin rotation to the provider list for load balancing. -// Special handling: gemini-3 models with both "antigravity" and "gemini-cli" providers -// preserve their order to prefer antigravity for these specific models. func (m *Manager) rotateProviders(model string, providers []string) []string { if len(providers) == 0 { return providers diff --git a/sdk/cliproxy/auth/oauth_model_alias.go b/sdk/cliproxy/auth/oauth_model_alias.go new file mode 100644 index 00000000..4111663e --- /dev/null +++ b/sdk/cliproxy/auth/oauth_model_alias.go @@ -0,0 +1,253 @@ +package auth + +import ( + "strings" + + internalconfig "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" +) + +type modelAliasEntry interface { + GetName() string + GetAlias() string +} + +type oauthModelAliasTable struct { + // reverse maps channel -> alias (lower) -> original upstream model name. + reverse map[string]map[string]string +} + +func compileOAuthModelAliasTable(aliases map[string][]internalconfig.OAuthModelAlias) *oauthModelAliasTable { + if len(aliases) == 0 { + return &oauthModelAliasTable{} + } + out := &oauthModelAliasTable{ + reverse: make(map[string]map[string]string, len(aliases)), + } + for rawChannel, entries := range aliases { + channel := strings.ToLower(strings.TrimSpace(rawChannel)) + if channel == "" || len(entries) == 0 { + continue + } + rev := make(map[string]string, len(entries)) + for _, entry := range entries { + name := strings.TrimSpace(entry.Name) + alias := strings.TrimSpace(entry.Alias) + if name == "" || alias == "" { + continue + } + if strings.EqualFold(name, alias) { + continue + } + aliasKey := strings.ToLower(alias) + if _, exists := rev[aliasKey]; exists { + continue + } + rev[aliasKey] = name + } + if len(rev) > 0 { + out.reverse[channel] = rev + } + } + if len(out.reverse) == 0 { + out.reverse = nil + } + return out +} + +// SetOAuthModelAlias updates the OAuth model name alias table used during execution. +// The alias is applied per-auth channel to resolve the upstream model name while keeping the +// client-visible model name unchanged for translation/response formatting. +func (m *Manager) SetOAuthModelAlias(aliases map[string][]internalconfig.OAuthModelAlias) { + if m == nil { + return + } + table := compileOAuthModelAliasTable(aliases) + // atomic.Value requires non-nil store values. + if table == nil { + table = &oauthModelAliasTable{} + } + m.oauthModelAlias.Store(table) +} + +// applyOAuthModelAlias resolves the upstream model from OAuth model alias. +// If an alias exists, the returned model is the upstream model. +func (m *Manager) applyOAuthModelAlias(auth *Auth, requestedModel string) string { + upstreamModel := m.resolveOAuthUpstreamModel(auth, requestedModel) + if upstreamModel == "" { + return requestedModel + } + return upstreamModel +} + +func resolveModelAliasFromConfigModels(requestedModel string, models []modelAliasEntry) string { + requestedModel = strings.TrimSpace(requestedModel) + if requestedModel == "" { + return "" + } + if len(models) == 0 { + return "" + } + + requestResult := thinking.ParseSuffix(requestedModel) + base := requestResult.ModelName + candidates := []string{base} + if base != requestedModel { + candidates = append(candidates, requestedModel) + } + + preserveSuffix := func(resolved string) string { + resolved = strings.TrimSpace(resolved) + if resolved == "" { + return "" + } + if thinking.ParseSuffix(resolved).HasSuffix { + return resolved + } + if requestResult.HasSuffix && requestResult.RawSuffix != "" { + return resolved + "(" + requestResult.RawSuffix + ")" + } + return resolved + } + + for i := range models { + name := strings.TrimSpace(models[i].GetName()) + alias := strings.TrimSpace(models[i].GetAlias()) + for _, candidate := range candidates { + if candidate == "" { + continue + } + if alias != "" && strings.EqualFold(alias, candidate) { + if name != "" { + return preserveSuffix(name) + } + return preserveSuffix(candidate) + } + if name != "" && strings.EqualFold(name, candidate) { + return preserveSuffix(name) + } + } + } + return "" +} + +// resolveOAuthUpstreamModel resolves the upstream model name from OAuth model alias. +// If an alias exists, returns the original (upstream) model name that corresponds +// to the requested alias. +// +// If the requested model contains a thinking suffix (e.g., "gemini-2.5-pro(8192)"), +// the suffix is preserved in the returned model name. However, if the alias's +// original name already contains a suffix, the config suffix takes priority. +func (m *Manager) resolveOAuthUpstreamModel(auth *Auth, requestedModel string) string { + return resolveUpstreamModelFromAliasTable(m, auth, requestedModel, modelAliasChannel(auth)) +} + +func resolveUpstreamModelFromAliasTable(m *Manager, auth *Auth, requestedModel, channel string) string { + if m == nil || auth == nil { + return "" + } + if channel == "" { + return "" + } + + // Extract thinking suffix from requested model using ParseSuffix + requestResult := thinking.ParseSuffix(requestedModel) + baseModel := requestResult.ModelName + + // Candidate keys to match: base model and raw input (handles suffix-parsing edge cases). + candidates := []string{baseModel} + if baseModel != requestedModel { + candidates = append(candidates, requestedModel) + } + + raw := m.oauthModelAlias.Load() + table, _ := raw.(*oauthModelAliasTable) + if table == nil || table.reverse == nil { + return "" + } + rev := table.reverse[channel] + if rev == nil { + return "" + } + + for _, candidate := range candidates { + key := strings.ToLower(strings.TrimSpace(candidate)) + if key == "" { + continue + } + original := strings.TrimSpace(rev[key]) + if original == "" { + continue + } + if strings.EqualFold(original, baseModel) { + return "" + } + + // If config already has suffix, it takes priority. + if thinking.ParseSuffix(original).HasSuffix { + return original + } + // Preserve user's thinking suffix on the resolved model. + if requestResult.HasSuffix && requestResult.RawSuffix != "" { + return original + "(" + requestResult.RawSuffix + ")" + } + return original + } + + return "" +} + +// modelAliasChannel extracts the OAuth model alias channel from an Auth object. +// It determines the provider and auth kind from the Auth's attributes and delegates +// to OAuthModelAliasChannel for the actual channel resolution. +func modelAliasChannel(auth *Auth) string { + if auth == nil { + return "" + } + provider := strings.ToLower(strings.TrimSpace(auth.Provider)) + authKind := "" + if auth.Attributes != nil { + authKind = strings.ToLower(strings.TrimSpace(auth.Attributes["auth_kind"])) + } + if authKind == "" { + if kind, _ := auth.AccountInfo(); strings.EqualFold(kind, "api_key") { + authKind = "apikey" + } + } + return OAuthModelAliasChannel(provider, authKind) +} + +// OAuthModelAliasChannel returns the OAuth model alias channel name for a given provider +// and auth kind. Returns empty string if the provider/authKind combination doesn't support +// OAuth model alias (e.g., API key authentication). +// +// Supported channels: gemini-cli, vertex, aistudio, antigravity, claude, codex, qwen, iflow. +func OAuthModelAliasChannel(provider, authKind string) string { + provider = strings.ToLower(strings.TrimSpace(provider)) + authKind = strings.ToLower(strings.TrimSpace(authKind)) + switch provider { + case "gemini": + // gemini provider uses gemini-api-key config, not oauth-model-alias. + // OAuth-based gemini auth is converted to "gemini-cli" by the synthesizer. + return "" + case "vertex": + if authKind == "apikey" { + return "" + } + return "vertex" + case "claude": + if authKind == "apikey" { + return "" + } + return "claude" + case "codex": + if authKind == "apikey" { + return "" + } + return "codex" + case "gemini-cli", "aistudio", "antigravity", "qwen", "iflow": + return provider + default: + return "" + } +} diff --git a/sdk/cliproxy/auth/oauth_model_alias_test.go b/sdk/cliproxy/auth/oauth_model_alias_test.go new file mode 100644 index 00000000..6956411c --- /dev/null +++ b/sdk/cliproxy/auth/oauth_model_alias_test.go @@ -0,0 +1,177 @@ +package auth + +import ( + "testing" + + internalconfig "github.com/router-for-me/CLIProxyAPI/v6/internal/config" +) + +func TestResolveOAuthUpstreamModel_SuffixPreservation(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + aliases map[string][]internalconfig.OAuthModelAlias + channel string + input string + want string + }{ + { + name: "numeric suffix preserved", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro"}}, + }, + channel: "gemini-cli", + input: "gemini-2.5-pro(8192)", + want: "gemini-2.5-pro-exp-03-25(8192)", + }, + { + name: "level suffix preserved", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "claude": {{Name: "claude-sonnet-4-5-20250514", Alias: "claude-sonnet-4-5"}}, + }, + channel: "claude", + input: "claude-sonnet-4-5(high)", + want: "claude-sonnet-4-5-20250514(high)", + }, + { + name: "no suffix unchanged", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro"}}, + }, + channel: "gemini-cli", + input: "gemini-2.5-pro", + want: "gemini-2.5-pro-exp-03-25", + }, + { + name: "config suffix takes priority", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "claude": {{Name: "claude-sonnet-4-5-20250514(low)", Alias: "claude-sonnet-4-5"}}, + }, + channel: "claude", + input: "claude-sonnet-4-5(high)", + want: "claude-sonnet-4-5-20250514(low)", + }, + { + name: "auto suffix preserved", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro"}}, + }, + channel: "gemini-cli", + input: "gemini-2.5-pro(auto)", + want: "gemini-2.5-pro-exp-03-25(auto)", + }, + { + name: "none suffix preserved", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro"}}, + }, + channel: "gemini-cli", + input: "gemini-2.5-pro(none)", + want: "gemini-2.5-pro-exp-03-25(none)", + }, + { + name: "case insensitive alias lookup with suffix", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "Gemini-2.5-Pro"}}, + }, + channel: "gemini-cli", + input: "gemini-2.5-pro(high)", + want: "gemini-2.5-pro-exp-03-25(high)", + }, + { + name: "no alias returns empty", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro"}}, + }, + channel: "gemini-cli", + input: "unknown-model(high)", + want: "", + }, + { + name: "wrong channel returns empty", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro"}}, + }, + channel: "claude", + input: "gemini-2.5-pro(high)", + want: "", + }, + { + name: "empty suffix filtered out", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro"}}, + }, + channel: "gemini-cli", + input: "gemini-2.5-pro()", + want: "gemini-2.5-pro-exp-03-25", + }, + { + name: "incomplete suffix treated as no suffix", + aliases: map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro(high"}}, + }, + channel: "gemini-cli", + input: "gemini-2.5-pro(high", + want: "gemini-2.5-pro-exp-03-25", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + mgr := NewManager(nil, nil, nil) + mgr.SetConfig(&internalconfig.Config{}) + mgr.SetOAuthModelAlias(tt.aliases) + + auth := createAuthForChannel(tt.channel) + got := mgr.resolveOAuthUpstreamModel(auth, tt.input) + if got != tt.want { + t.Errorf("resolveOAuthUpstreamModel(%q) = %q, want %q", tt.input, got, tt.want) + } + }) + } +} + +func createAuthForChannel(channel string) *Auth { + switch channel { + case "gemini-cli": + return &Auth{Provider: "gemini-cli"} + case "claude": + return &Auth{Provider: "claude", Attributes: map[string]string{"auth_kind": "oauth"}} + case "vertex": + return &Auth{Provider: "vertex", Attributes: map[string]string{"auth_kind": "oauth"}} + case "codex": + return &Auth{Provider: "codex", Attributes: map[string]string{"auth_kind": "oauth"}} + case "aistudio": + return &Auth{Provider: "aistudio"} + case "antigravity": + return &Auth{Provider: "antigravity"} + case "qwen": + return &Auth{Provider: "qwen"} + case "iflow": + return &Auth{Provider: "iflow"} + default: + return &Auth{Provider: channel} + } +} + +func TestApplyOAuthModelAlias_SuffixPreservation(t *testing.T) { + t.Parallel() + + aliases := map[string][]internalconfig.OAuthModelAlias{ + "gemini-cli": {{Name: "gemini-2.5-pro-exp-03-25", Alias: "gemini-2.5-pro"}}, + } + + mgr := NewManager(nil, nil, nil) + mgr.SetConfig(&internalconfig.Config{}) + mgr.SetOAuthModelAlias(aliases) + + auth := &Auth{ID: "test-auth-id", Provider: "gemini-cli"} + + resolvedModel := mgr.applyOAuthModelAlias(auth, "gemini-2.5-pro(8192)") + if resolvedModel != "gemini-2.5-pro-exp-03-25(8192)" { + t.Errorf("applyOAuthModelAlias() model = %q, want %q", resolvedModel, "gemini-2.5-pro-exp-03-25(8192)") + } +} diff --git a/sdk/cliproxy/service_oauth_model_alias_test.go b/sdk/cliproxy/service_oauth_model_alias_test.go new file mode 100644 index 00000000..2caf7a17 --- /dev/null +++ b/sdk/cliproxy/service_oauth_model_alias_test.go @@ -0,0 +1,92 @@ +package cliproxy + +import ( + "testing" + + "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" +) + +func TestApplyOAuthModelAlias_Rename(t *testing.T) { + cfg := &config.Config{ + OAuthModelAlias: map[string][]config.OAuthModelAlias{ + "codex": { + {Name: "gpt-5", Alias: "g5"}, + }, + }, + } + models := []*ModelInfo{ + {ID: "gpt-5", Name: "models/gpt-5"}, + } + + out := applyOAuthModelAlias(cfg, "codex", "oauth", models) + if len(out) != 1 { + t.Fatalf("expected 1 model, got %d", len(out)) + } + if out[0].ID != "g5" { + t.Fatalf("expected model id %q, got %q", "g5", out[0].ID) + } + if out[0].Name != "models/g5" { + t.Fatalf("expected model name %q, got %q", "models/g5", out[0].Name) + } +} + +func TestApplyOAuthModelAlias_ForkAddsAlias(t *testing.T) { + cfg := &config.Config{ + OAuthModelAlias: map[string][]config.OAuthModelAlias{ + "codex": { + {Name: "gpt-5", Alias: "g5", Fork: true}, + }, + }, + } + models := []*ModelInfo{ + {ID: "gpt-5", Name: "models/gpt-5"}, + } + + out := applyOAuthModelAlias(cfg, "codex", "oauth", models) + if len(out) != 2 { + t.Fatalf("expected 2 models, got %d", len(out)) + } + if out[0].ID != "gpt-5" { + t.Fatalf("expected first model id %q, got %q", "gpt-5", out[0].ID) + } + if out[1].ID != "g5" { + t.Fatalf("expected second model id %q, got %q", "g5", out[1].ID) + } + if out[1].Name != "models/g5" { + t.Fatalf("expected forked model name %q, got %q", "models/g5", out[1].Name) + } +} + +func TestApplyOAuthModelAlias_ForkAddsMultipleAliases(t *testing.T) { + cfg := &config.Config{ + OAuthModelAlias: map[string][]config.OAuthModelAlias{ + "codex": { + {Name: "gpt-5", Alias: "g5", Fork: true}, + {Name: "gpt-5", Alias: "g5-2", Fork: true}, + }, + }, + } + models := []*ModelInfo{ + {ID: "gpt-5", Name: "models/gpt-5"}, + } + + out := applyOAuthModelAlias(cfg, "codex", "oauth", models) + if len(out) != 3 { + t.Fatalf("expected 3 models, got %d", len(out)) + } + if out[0].ID != "gpt-5" { + t.Fatalf("expected first model id %q, got %q", "gpt-5", out[0].ID) + } + if out[1].ID != "g5" { + t.Fatalf("expected second model id %q, got %q", "g5", out[1].ID) + } + if out[1].Name != "models/g5" { + t.Fatalf("expected forked model name %q, got %q", "models/g5", out[1].Name) + } + if out[2].ID != "g5-2" { + t.Fatalf("expected third model id %q, got %q", "g5-2", out[2].ID) + } + if out[2].Name != "models/g5-2" { + t.Fatalf("expected forked model name %q, got %q", "models/g5-2", out[2].Name) + } +}