From 41b1ad654e28b03f98751f8eff2faee4a1ac9726 Mon Sep 17 00:00:00 2001 From: RCD <90105158+Finesssee@users.noreply.github.com> Date: Thu, 29 Jan 2026 21:30:09 +0700 Subject: [PATCH] feat(config): add oauth-model-alias migration support Add migration logic for converting oauth-model-mappings to oauth-model-alias with comprehensive test coverage. Co-Authored-By: Claude (gemini-claude-opus-4-5-thinking) --- internal/access/config_access/provider.go | 112 ++ internal/access/reconcile.go | 270 ++++ .../api/handlers/management/auth_files.go | 1187 +++++------------ .../api/handlers/management/config_basic.go | 175 +-- .../api/handlers/management/config_lists.go | 575 ++++++++ internal/api/handlers/management/handler.go | 153 --- .../handlers/management/model_definitions.go | 33 + .../api/handlers/management/oauth_sessions.go | 11 +- internal/api/handlers/management/usage.go | 6 +- internal/api/middleware/metrics.go | 6 + internal/api/middleware/request_logging.go | 90 -- internal/api/middleware/response_writer.go | 25 +- internal/api/server.go | 637 +++------ internal/api/server_test.go | 69 + internal/auth/claude/errors.go | 6 + internal/auth/codex/errors.go | 6 + internal/cache/signature_cache.go | 4 +- internal/cmd/anthropic_login.go | 7 +- internal/cmd/antigravity_login.go | 40 +- internal/cmd/auth_manager.go | 6 +- internal/cmd/iflow_cookie.go | 98 ++ internal/cmd/iflow_login.go | 49 + internal/cmd/login.go | 67 +- internal/cmd/openai_login.go | 10 +- internal/cmd/qwen_login.go | 7 +- internal/cmd/run.go | 47 +- .../config/oauth_model_alias_migration.go | 275 ++++ .../oauth_model_alias_migration_test.go | 242 ++++ internal/config/oauth_model_alias_test.go | 56 + internal/logging/global_logger.go | 43 +- internal/managementasset/updater.go | 468 +++++++ internal/misc/codex_instructions.go | 18 +- internal/registry/model_definitions.go | 750 ++--------- .../registry/model_definitions_static_data.go | 881 ++++++++++++ internal/registry/model_registry.go | 340 +++-- internal/registry/model_registry_hook_test.go | 204 +++ .../runtime/executor/aistudio_executor.go | 5 - .../runtime/executor/antigravity_executor.go | 6 - internal/runtime/executor/cache_helpers.go | 10 + internal/runtime/executor/claude_executor.go | 5 - internal/runtime/executor/codex_executor.go | 5 - .../runtime/executor/gemini_cli_executor.go | 5 - internal/runtime/executor/gemini_executor.go | 5 - .../executor/gemini_vertex_executor.go | 5 - .../executor/github_copilot_executor.go | 431 ++++++ internal/runtime/executor/iflow_executor.go | 532 ++++++++ .../runtime/executor/iflow_executor_test.go | 67 + internal/runtime/executor/kiro_executor.go | 868 +++++++++--- internal/runtime/executor/minimax_executor.go | 13 +- .../executor/openai_compat_executor.go | 5 - internal/runtime/executor/payload_helpers.go | 293 ++-- internal/runtime/executor/qwen_executor.go | 5 - .../runtime/executor/qwen_executor_test.go | 30 + .../runtime/executor/thinking_providers.go | 11 + internal/runtime/executor/token_helpers.go | 75 -- internal/runtime/executor/usage_helpers.go | 38 + internal/runtime/executor/zhipu_executor.go | 13 +- internal/thinking/provider/claude/apply.go | 1 + internal/thinking/provider/codex/apply.go | 1 + internal/thinking/provider/geminicli/apply.go | 126 ++ internal/thinking/provider/iflow/apply.go | 156 +++ internal/thinking/provider/openai/apply.go | 1 + .../translator/antigravity/claude/init.go | 2 +- .../translator/antigravity/gemini/init.go | 2 +- .../antigravity_openai_response.go | 20 +- .../openai/chat-completions/init.go | 2 +- .../antigravity/openai/responses/init.go | 2 +- internal/translator/claude/gemini-cli/init.go | 2 +- internal/translator/claude/gemini/init.go | 2 +- .../claude_openai_response.go | 4 - .../claude/openai/chat-completions/init.go | 2 +- .../claude_openai-responses_request.go | 7 +- .../claude_openai-responses_response.go | 21 +- .../claude/openai/responses/init.go | 2 +- .../codex/claude/codex_claude_request.go | 50 +- .../codex/claude/codex_claude_response.go | 43 +- internal/translator/codex/claude/init.go | 2 +- internal/translator/codex/gemini-cli/init.go | 2 +- .../codex/gemini/codex_gemini_request.go | 28 +- internal/translator/codex/gemini/init.go | 2 +- .../chat-completions/codex_openai_request.go | 40 +- .../codex/openai/chat-completions/init.go | 2 +- .../codex_openai-responses_request.go | 65 +- .../codex_openai-responses_response.go | 18 +- .../translator/codex/openai/responses/init.go | 2 +- .../claude/gemini-cli_claude_request.go | 6 +- internal/translator/gemini-cli/claude/init.go | 2 +- .../gemini/gemini-cli_gemini_request.go | 1 - internal/translator/gemini-cli/gemini/init.go | 2 +- .../gemini-cli_openai_response.go | 16 +- .../openai/chat-completions/init.go | 2 +- .../gemini-cli/openai/responses/init.go | 2 +- .../gemini/claude/gemini_claude_request.go | 8 +- internal/translator/gemini/claude/init.go | 2 +- .../gemini-cli/gemini_gemini-cli_request.go | 1 - internal/translator/gemini/gemini-cli/init.go | 2 +- .../gemini/gemini/gemini_gemini_request.go | 1 - internal/translator/gemini/gemini/init.go | 2 +- .../gemini/openai/chat-completions/init.go | 2 +- .../gemini_openai-responses_request.go | 14 +- .../gemini/openai/responses/init.go | 2 +- internal/translator/init.go | 3 - internal/translator/openai/claude/init.go | 2 +- .../openai/claude/openai_claude_response.go | 8 +- internal/translator/openai/gemini-cli/init.go | 2 +- internal/translator/openai/gemini/init.go | 2 +- .../openai/gemini/openai_gemini_request.go | 14 +- .../openai/openai/chat-completions/init.go | 2 +- .../openai/openai/responses/init.go | 2 +- internal/translator/translator/translator.go | 89 ++ internal/usage/logger_plugin.go | 267 ++-- internal/util/provider.go | 65 +- internal/util/sanitize_test.go | 56 + internal/util/translator.go | 44 - internal/util/util.go | 131 +- internal/watcher/config_reload.go | 3 +- internal/watcher/diff/config_diff.go | 102 ++ internal/watcher/diff/config_diff_test.go | 127 +- internal/watcher/diff/model_hash.go | 15 + internal/watcher/diff/models_summary.go | 121 ++ internal/watcher/diff/oauth_excluded.go | 45 +- internal/watcher/diff/oauth_excluded_test.go | 20 + internal/watcher/diff/oauth_model_alias.go | 101 ++ internal/watcher/events.go | 68 +- internal/watcher/synthesizer/config.go | 119 +- internal/watcher/synthesizer/file.go | 9 +- internal/wsrelay/http.go | 29 +- sdk/api/handlers/claude/code_handlers.go | 51 +- .../handlers/gemini/gemini-cli_handlers.go | 11 +- sdk/api/handlers/gemini/gemini_handlers.go | 164 +-- sdk/api/handlers/handlers.go | 335 ++--- .../handlers/handlers_request_details_test.go | 118 ++ .../handlers_stream_bootstrap_test.go | 132 +- .../openai/openai_embeddings_handlers.go | 5 +- sdk/api/handlers/openai/openai_handlers.go | 26 +- .../openai/openai_responses_handlers.go | 20 +- sdk/api/handlers/stream_forwarder.go | 30 +- sdk/api/management.go | 20 + sdk/auth/antigravity.go | 475 +------ sdk/auth/claude.go | 13 +- sdk/auth/codex.go | 43 +- sdk/auth/errors.go | 27 + sdk/auth/filestore.go | 9 +- sdk/auth/gemini.go | 5 +- sdk/auth/interfaces.go | 9 +- sdk/auth/manager.go | 10 +- sdk/auth/refresh_registry.go | 4 +- sdk/cliproxy/auth/conductor.go | 1098 +++++++++------ sdk/cliproxy/auth/selector.go | 81 +- sdk/cliproxy/auth/selector_test.go | 107 +- sdk/cliproxy/auth/types.go | 46 +- sdk/cliproxy/builder.go | 4 +- sdk/cliproxy/executor/types.go | 4 - sdk/cliproxy/model_registry.go | 9 + sdk/cliproxy/service.go | 469 +++---- sdk/config/config.go | 6 +- sdk/translator/registry.go | 964 ++++--------- sdk/translator/resilience.go | 346 ----- sdk/translator/resilience_test.go | 336 ----- 159 files changed, 9542 insertions(+), 6953 deletions(-) create mode 100644 internal/access/config_access/provider.go create mode 100644 internal/access/reconcile.go create mode 100644 internal/api/handlers/management/model_definitions.go create mode 100644 internal/cmd/iflow_cookie.go create mode 100644 internal/cmd/iflow_login.go create mode 100644 internal/config/oauth_model_alias_migration.go create mode 100644 internal/config/oauth_model_alias_migration_test.go create mode 100644 internal/config/oauth_model_alias_test.go create mode 100644 internal/managementasset/updater.go create mode 100644 internal/registry/model_definitions_static_data.go create mode 100644 internal/registry/model_registry_hook_test.go create mode 100644 internal/runtime/executor/github_copilot_executor.go create mode 100644 internal/runtime/executor/iflow_executor.go create mode 100644 internal/runtime/executor/iflow_executor_test.go create mode 100644 internal/runtime/executor/qwen_executor_test.go create mode 100644 internal/runtime/executor/thinking_providers.go create mode 100644 internal/thinking/provider/geminicli/apply.go create mode 100644 internal/thinking/provider/iflow/apply.go create mode 100644 internal/translator/translator/translator.go create mode 100644 internal/util/sanitize_test.go create mode 100644 internal/watcher/diff/models_summary.go create mode 100644 internal/watcher/diff/oauth_model_alias.go create mode 100644 sdk/api/handlers/handlers_request_details_test.go delete mode 100644 sdk/translator/resilience.go delete mode 100644 sdk/translator/resilience_test.go diff --git a/internal/access/config_access/provider.go b/internal/access/config_access/provider.go new file mode 100644 index 00000000..70824524 --- /dev/null +++ b/internal/access/config_access/provider.go @@ -0,0 +1,112 @@ +package configaccess + +import ( + "context" + "net/http" + "strings" + "sync" + + sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access" + sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" +) + +var registerOnce sync.Once + +// Register ensures the config-access provider is available to the access manager. +func Register() { + registerOnce.Do(func() { + sdkaccess.RegisterProvider(sdkconfig.AccessProviderTypeConfigAPIKey, newProvider) + }) +} + +type provider struct { + name string + keys map[string]struct{} +} + +func newProvider(cfg *sdkconfig.AccessProvider, _ *sdkconfig.SDKConfig) (sdkaccess.Provider, error) { + name := cfg.Name + if name == "" { + name = sdkconfig.DefaultAccessProviderName + } + keys := make(map[string]struct{}, len(cfg.APIKeys)) + for _, key := range cfg.APIKeys { + if key == "" { + continue + } + keys[key] = struct{}{} + } + return &provider{name: name, keys: keys}, nil +} + +func (p *provider) Identifier() string { + if p == nil || p.name == "" { + return sdkconfig.DefaultAccessProviderName + } + return p.name +} + +func (p *provider) Authenticate(_ context.Context, r *http.Request) (*sdkaccess.Result, error) { + if p == nil { + return nil, sdkaccess.ErrNotHandled + } + if len(p.keys) == 0 { + return nil, sdkaccess.ErrNotHandled + } + authHeader := r.Header.Get("Authorization") + authHeaderGoogle := r.Header.Get("X-Goog-Api-Key") + authHeaderAnthropic := r.Header.Get("X-Api-Key") + queryKey := "" + queryAuthToken := "" + if r.URL != nil { + queryKey = r.URL.Query().Get("key") + queryAuthToken = r.URL.Query().Get("auth_token") + } + if authHeader == "" && authHeaderGoogle == "" && authHeaderAnthropic == "" && queryKey == "" && queryAuthToken == "" { + return nil, sdkaccess.ErrNoCredentials + } + + apiKey := extractBearerToken(authHeader) + + candidates := []struct { + value string + source string + }{ + {apiKey, "authorization"}, + {authHeaderGoogle, "x-goog-api-key"}, + {authHeaderAnthropic, "x-api-key"}, + {queryKey, "query-key"}, + {queryAuthToken, "query-auth-token"}, + } + + for _, candidate := range candidates { + if candidate.value == "" { + continue + } + if _, ok := p.keys[candidate.value]; ok { + return &sdkaccess.Result{ + Provider: p.Identifier(), + Principal: candidate.value, + Metadata: map[string]string{ + "source": candidate.source, + }, + }, nil + } + } + + return nil, sdkaccess.ErrInvalidCredential +} + +func extractBearerToken(header string) string { + if header == "" { + return "" + } + parts := strings.SplitN(header, " ", 2) + if len(parts) != 2 { + return header + } + if strings.ToLower(parts[0]) != "bearer" { + return header + } + return strings.TrimSpace(parts[1]) +} diff --git a/internal/access/reconcile.go b/internal/access/reconcile.go new file mode 100644 index 00000000..267d2fe0 --- /dev/null +++ b/internal/access/reconcile.go @@ -0,0 +1,270 @@ +package access + +import ( + "fmt" + "reflect" + "sort" + "strings" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access" + sdkConfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" + log "github.com/sirupsen/logrus" +) + +// ReconcileProviders builds the desired provider list by reusing existing providers when possible +// and creating or removing providers only when their configuration changed. It returns the final +// ordered provider slice along with the identifiers of providers that were added, updated, or +// removed compared to the previous configuration. +func ReconcileProviders(oldCfg, newCfg *config.Config, existing []sdkaccess.Provider) (result []sdkaccess.Provider, added, updated, removed []string, err error) { + if newCfg == nil { + return nil, nil, nil, nil, nil + } + + existingMap := make(map[string]sdkaccess.Provider, len(existing)) + for _, provider := range existing { + if provider == nil { + continue + } + existingMap[provider.Identifier()] = provider + } + + oldCfgMap := accessProviderMap(oldCfg) + newEntries := collectProviderEntries(newCfg) + + result = make([]sdkaccess.Provider, 0, len(newEntries)) + finalIDs := make(map[string]struct{}, len(newEntries)) + + isInlineProvider := func(id string) bool { + return strings.EqualFold(id, sdkConfig.DefaultAccessProviderName) + } + appendChange := func(list *[]string, id string) { + if isInlineProvider(id) { + return + } + *list = append(*list, id) + } + + for _, providerCfg := range newEntries { + key := providerIdentifier(providerCfg) + if key == "" { + continue + } + + forceRebuild := strings.EqualFold(strings.TrimSpace(providerCfg.Type), sdkConfig.AccessProviderTypeConfigAPIKey) + if oldCfgProvider, ok := oldCfgMap[key]; ok { + isAliased := oldCfgProvider == providerCfg + if !forceRebuild && !isAliased && providerConfigEqual(oldCfgProvider, providerCfg) { + if existingProvider, okExisting := existingMap[key]; okExisting { + result = append(result, existingProvider) + finalIDs[key] = struct{}{} + continue + } + } + } + + provider, buildErr := sdkaccess.BuildProvider(providerCfg, &newCfg.SDKConfig) + if buildErr != nil { + return nil, nil, nil, nil, buildErr + } + if _, ok := oldCfgMap[key]; ok { + if _, existed := existingMap[key]; existed { + appendChange(&updated, key) + } else { + appendChange(&added, key) + } + } else { + appendChange(&added, key) + } + result = append(result, provider) + finalIDs[key] = struct{}{} + } + + if len(result) == 0 { + if inline := sdkConfig.MakeInlineAPIKeyProvider(newCfg.APIKeys); inline != nil { + key := providerIdentifier(inline) + if key != "" { + if oldCfgProvider, ok := oldCfgMap[key]; ok { + if providerConfigEqual(oldCfgProvider, inline) { + if existingProvider, okExisting := existingMap[key]; okExisting { + result = append(result, existingProvider) + finalIDs[key] = struct{}{} + goto inlineDone + } + } + } + provider, buildErr := sdkaccess.BuildProvider(inline, &newCfg.SDKConfig) + if buildErr != nil { + return nil, nil, nil, nil, buildErr + } + if _, existed := existingMap[key]; existed { + appendChange(&updated, key) + } else if _, hadOld := oldCfgMap[key]; hadOld { + appendChange(&updated, key) + } else { + appendChange(&added, key) + } + result = append(result, provider) + finalIDs[key] = struct{}{} + } + } + inlineDone: + } + + removedSet := make(map[string]struct{}) + for id := range existingMap { + if _, ok := finalIDs[id]; !ok { + if isInlineProvider(id) { + continue + } + removedSet[id] = struct{}{} + } + } + + removed = make([]string, 0, len(removedSet)) + for id := range removedSet { + removed = append(removed, id) + } + + sort.Strings(added) + sort.Strings(updated) + sort.Strings(removed) + + return result, added, updated, removed, nil +} + +// ApplyAccessProviders reconciles the configured access providers against the +// currently registered providers and updates the manager. It logs a concise +// summary of the detected changes and returns whether any provider changed. +func ApplyAccessProviders(manager *sdkaccess.Manager, oldCfg, newCfg *config.Config) (bool, error) { + if manager == nil || newCfg == nil { + return false, nil + } + + existing := manager.Providers() + providers, added, updated, removed, err := ReconcileProviders(oldCfg, newCfg, existing) + if err != nil { + log.Errorf("failed to reconcile request auth providers: %v", err) + return false, fmt.Errorf("reconciling access providers: %w", err) + } + + manager.SetProviders(providers) + + if len(added)+len(updated)+len(removed) > 0 { + log.Debugf("auth providers reconciled (added=%d updated=%d removed=%d)", len(added), len(updated), len(removed)) + log.Debugf("auth providers changes details - added=%v updated=%v removed=%v", added, updated, removed) + return true, nil + } + + log.Debug("auth providers unchanged after config update") + return false, nil +} + +func accessProviderMap(cfg *config.Config) map[string]*sdkConfig.AccessProvider { + result := make(map[string]*sdkConfig.AccessProvider) + if cfg == nil { + return result + } + for i := range cfg.Access.Providers { + providerCfg := &cfg.Access.Providers[i] + if providerCfg.Type == "" { + continue + } + key := providerIdentifier(providerCfg) + if key == "" { + continue + } + result[key] = providerCfg + } + if len(result) == 0 && len(cfg.APIKeys) > 0 { + if provider := sdkConfig.MakeInlineAPIKeyProvider(cfg.APIKeys); provider != nil { + if key := providerIdentifier(provider); key != "" { + result[key] = provider + } + } + } + return result +} + +func collectProviderEntries(cfg *config.Config) []*sdkConfig.AccessProvider { + entries := make([]*sdkConfig.AccessProvider, 0, len(cfg.Access.Providers)) + for i := range cfg.Access.Providers { + providerCfg := &cfg.Access.Providers[i] + if providerCfg.Type == "" { + continue + } + if key := providerIdentifier(providerCfg); key != "" { + entries = append(entries, providerCfg) + } + } + if len(entries) == 0 && len(cfg.APIKeys) > 0 { + if inline := sdkConfig.MakeInlineAPIKeyProvider(cfg.APIKeys); inline != nil { + entries = append(entries, inline) + } + } + return entries +} + +func providerIdentifier(provider *sdkConfig.AccessProvider) string { + if provider == nil { + return "" + } + if name := strings.TrimSpace(provider.Name); name != "" { + return name + } + typ := strings.TrimSpace(provider.Type) + if typ == "" { + return "" + } + if strings.EqualFold(typ, sdkConfig.AccessProviderTypeConfigAPIKey) { + return sdkConfig.DefaultAccessProviderName + } + return typ +} + +func providerConfigEqual(a, b *sdkConfig.AccessProvider) bool { + if a == nil || b == nil { + return a == nil && b == nil + } + if !strings.EqualFold(strings.TrimSpace(a.Type), strings.TrimSpace(b.Type)) { + return false + } + if strings.TrimSpace(a.SDK) != strings.TrimSpace(b.SDK) { + return false + } + if !stringSetEqual(a.APIKeys, b.APIKeys) { + return false + } + if len(a.Config) != len(b.Config) { + return false + } + if len(a.Config) > 0 && !reflect.DeepEqual(a.Config, b.Config) { + return false + } + return true +} + +func stringSetEqual(a, b []string) bool { + if len(a) != len(b) { + return false + } + if len(a) == 0 { + return true + } + seen := make(map[string]int, len(a)) + for _, val := range a { + seen[val]++ + } + for _, val := range b { + count := seen[val] + if count == 0 { + return false + } + if count == 1 { + delete(seen, val) + } else { + seen[val] = count - 1 + } + } + return len(seen) == 0 +} diff --git a/internal/api/handlers/management/auth_files.go b/internal/api/handlers/management/auth_files.go index 3e0216d3..996ea1a7 100644 --- a/internal/api/handlers/management/auth_files.go +++ b/internal/api/handlers/management/auth_files.go @@ -3,16 +3,14 @@ package management import ( "bytes" "context" - "crypto/rand" "crypto/sha256" - "encoding/base64" + "encoding/hex" "encoding/json" "errors" "fmt" "io" "net" "net/http" - "net/url" "os" "path/filepath" "sort" @@ -22,11 +20,11 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/antigravity" "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/claude" "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/codex" - amazonqauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/amazonq" geminiAuth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/gemini" - kiroauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/kiro" + iflowauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/iflow" "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/qwen" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" @@ -234,14 +232,6 @@ func stopForwarderInstance(port int, forwarder *callbackForwarder) { log.Infof("callback forwarder on port %d stopped", port) } -func sanitizeAntigravityFileName(email string) string { - if strings.TrimSpace(email) == "" { - return "antigravity.json" - } - replacer := strings.NewReplacer("@", "_", ".", "_") - return fmt.Sprintf("antigravity-%s.json", replacer.Replace(email)) -} - func (h *Handler) managementCallbackURL(path string) (string, error) { if h == nil || h.cfg == nil || h.cfg.Port <= 0 { return "", fmt.Errorf("server port is not configured") @@ -386,7 +376,6 @@ func (h *Handler) buildAuthFileEntry(auth *coreauth.Auth) gin.H { "type": strings.TrimSpace(auth.Provider), "provider": strings.TrimSpace(auth.Provider), "label": auth.Label, - "priority": auth.Priority, "status": auth.Status, "status_message": auth.StatusMessage, "disabled": auth.Disabled, @@ -406,14 +395,6 @@ func (h *Handler) buildAuthFileEntry(auth *coreauth.Auth) gin.H { entry["account"] = account } } - if strings.EqualFold(strings.TrimSpace(auth.Provider), "antigravity") && auth.Metadata != nil { - if tierID, ok := auth.Metadata["tier_id"]; ok { - entry["tier_id"] = tierID - } - if tiers, ok := auth.Metadata["allowed_tiers"]; ok { - entry["allowed_tiers"] = tiers - } - } if !auth.CreatedAt.IsZero() { entry["created_at"] = auth.CreatedAt } @@ -424,18 +405,6 @@ func (h *Handler) buildAuthFileEntry(auth *coreauth.Auth) gin.H { if !auth.LastRefreshedAt.IsZero() { entry["last_refresh"] = auth.LastRefreshedAt } - if !auth.TokenExpiresAt.IsZero() { - entry["token_expires_at"] = auth.TokenExpiresAt - } - // Include usage stats for WebUI display - entry["usage"] = gin.H{ - "total_input_tokens": auth.Usage.TotalInputTokens, - "total_output_tokens": auth.Usage.TotalOutputTokens, - "request_count": auth.Usage.RequestCount, - "daily_input_tokens": auth.Usage.DailyInputTokens, - "daily_output_tokens": auth.Usage.DailyOutputTokens, - "daily_request_count": auth.Usage.DailyRequestCount, - } if path != "" { entry["path"] = path entry["source"] = "file" @@ -452,6 +421,9 @@ func (h *Handler) buildAuthFileEntry(auth *coreauth.Auth) gin.H { log.WithError(err).Warnf("failed to stat auth file %s", path) } } + if claims := extractCodexIDTokenClaims(auth); claims != nil { + entry["id_token"] = claims + } return entry } @@ -769,6 +741,72 @@ func (h *Handler) registerAuthFromFile(ctx context.Context, path string, data [] return err } +// PatchAuthFileStatus toggles the disabled state of an auth file +func (h *Handler) PatchAuthFileStatus(c *gin.Context) { + if h.authManager == nil { + c.JSON(http.StatusServiceUnavailable, gin.H{"error": "core auth manager unavailable"}) + return + } + + var req struct { + Name string `json:"name"` + Disabled *bool `json:"disabled"` + } + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"}) + return + } + + name := strings.TrimSpace(req.Name) + if name == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"}) + return + } + if req.Disabled == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "disabled is required"}) + return + } + + ctx := c.Request.Context() + + // Find auth by name or ID + var targetAuth *coreauth.Auth + if auth, ok := h.authManager.GetByID(name); ok { + targetAuth = auth + } else { + auths := h.authManager.List() + for _, auth := range auths { + if auth.FileName == name { + targetAuth = auth + break + } + } + } + + if targetAuth == nil { + c.JSON(http.StatusNotFound, gin.H{"error": "auth file not found"}) + return + } + + // Update disabled state + targetAuth.Disabled = *req.Disabled + if *req.Disabled { + targetAuth.Status = coreauth.StatusDisabled + targetAuth.StatusMessage = "disabled via management API" + } else { + targetAuth.Status = coreauth.StatusActive + targetAuth.StatusMessage = "" + } + targetAuth.UpdatedAt = time.Now() + + if _, err := h.authManager.Update(ctx, targetAuth); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to update auth: %v", err)}) + return + } + + c.JSON(http.StatusOK, gin.H{"status": "ok", "disabled": *req.Disabled}) +} + func (h *Handler) disableAuth(ctx context.Context, id string) { if h == nil || h.authManager == nil { return @@ -935,67 +973,14 @@ func (h *Handler) RequestAnthropicToken(c *gin.Context) { rawCode := resultMap["code"] code := strings.Split(rawCode, "#")[0] - // Exchange code for tokens (replicate logic using updated redirect_uri) - // Extract client_id from the modified auth URL - clientID := "" - if u2, errP := url.Parse(authURL); errP == nil { - clientID = u2.Query().Get("client_id") - } - // Build request - bodyMap := map[string]any{ - "code": code, - "state": state, - "grant_type": "authorization_code", - "client_id": clientID, - "redirect_uri": "http://localhost:54545/callback", - "code_verifier": pkceCodes.CodeVerifier, - } - bodyJSON, _ := json.Marshal(bodyMap) - - httpClient := util.SetProxy(&h.cfg.SDKConfig, &http.Client{}) - req, _ := http.NewRequestWithContext(ctx, "POST", "https://console.anthropic.com/v1/oauth/token", strings.NewReader(string(bodyJSON))) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Accept", "application/json") - resp, errDo := httpClient.Do(req) - if errDo != nil { - authErr := claude.NewAuthenticationError(claude.ErrCodeExchangeFailed, errDo) + // Exchange code for tokens using internal auth service + bundle, errExchange := anthropicAuth.ExchangeCodeForTokens(ctx, code, state, pkceCodes) + if errExchange != nil { + authErr := claude.NewAuthenticationError(claude.ErrCodeExchangeFailed, errExchange) log.Errorf("Failed to exchange authorization code for tokens: %v", authErr) SetOAuthSessionError(state, "Failed to exchange authorization code for tokens") return } - defer func() { - if errClose := resp.Body.Close(); errClose != nil { - log.Errorf("failed to close response body: %v", errClose) - } - }() - respBody, _ := io.ReadAll(resp.Body) - if resp.StatusCode != http.StatusOK { - log.Errorf("token exchange failed with status %d: %s", resp.StatusCode, string(respBody)) - SetOAuthSessionError(state, fmt.Sprintf("token exchange failed with status %d", resp.StatusCode)) - return - } - var tResp struct { - AccessToken string `json:"access_token"` - RefreshToken string `json:"refresh_token"` - ExpiresIn int `json:"expires_in"` - Account struct { - EmailAddress string `json:"email_address"` - } `json:"account"` - } - if errU := json.Unmarshal(respBody, &tResp); errU != nil { - log.Errorf("failed to parse token response: %v", errU) - SetOAuthSessionError(state, "Failed to parse token response") - return - } - bundle := &claude.ClaudeAuthBundle{ - TokenData: claude.ClaudeTokenData{ - AccessToken: tResp.AccessToken, - RefreshToken: tResp.RefreshToken, - Email: tResp.Account.EmailAddress, - Expire: time.Now().Add(time.Duration(tResp.ExpiresIn) * time.Second).Format(time.RFC3339), - }, - LastRefresh: time.Now().Format(time.RFC3339), - } // Create token storage tokenStorage := anthropicAuth.CreateTokenStorage(bundle) @@ -1035,17 +1020,13 @@ func (h *Handler) RequestGeminiCLIToken(c *gin.Context) { fmt.Println("Initializing Google authentication...") - // OAuth2 configuration (mirrors internal/auth/gemini) + // OAuth2 configuration using exported constants from internal/auth/gemini conf := &oauth2.Config{ - ClientID: "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com", - ClientSecret: "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", - RedirectURL: "http://localhost:8085/oauth2callback", - Scopes: []string{ - "https://www.googleapis.com/auth/cloud-platform", - "https://www.googleapis.com/auth/userinfo.email", - "https://www.googleapis.com/auth/userinfo.profile", - }, - Endpoint: google.Endpoint, + ClientID: geminiAuth.ClientID, + ClientSecret: geminiAuth.ClientSecret, + RedirectURL: fmt.Sprintf("http://localhost:%d/oauth2callback", geminiAuth.DefaultCallbackPort), + Scopes: geminiAuth.Scopes, + Endpoint: google.Endpoint, } // Build authorization URL and return it immediately @@ -1167,13 +1148,9 @@ func (h *Handler) RequestGeminiCLIToken(c *gin.Context) { } ifToken["token_uri"] = "https://oauth2.googleapis.com/token" - ifToken["client_id"] = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com" - ifToken["client_secret"] = "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" - ifToken["scopes"] = []string{ - "https://www.googleapis.com/auth/cloud-platform", - "https://www.googleapis.com/auth/userinfo.email", - "https://www.googleapis.com/auth/userinfo.profile", - } + ifToken["client_id"] = geminiAuth.ClientID + ifToken["client_secret"] = geminiAuth.ClientSecret + ifToken["scopes"] = geminiAuth.Scopes ifToken["universe_domain"] = "googleapis.com" ts := geminiAuth.GeminiTokenStorage{ @@ -1360,74 +1337,34 @@ func (h *Handler) RequestCodexToken(c *gin.Context) { } log.Debug("Authorization code received, exchanging for tokens...") - // Extract client_id from authURL - clientID := "" - if u2, errP := url.Parse(authURL); errP == nil { - clientID = u2.Query().Get("client_id") - } - // Exchange code for tokens with redirect equal to mgmtRedirect - form := url.Values{ - "grant_type": {"authorization_code"}, - "client_id": {clientID}, - "code": {code}, - "redirect_uri": {"http://localhost:1455/auth/callback"}, - "code_verifier": {pkceCodes.CodeVerifier}, - } - httpClient := util.SetProxy(&h.cfg.SDKConfig, &http.Client{}) - req, _ := http.NewRequestWithContext(ctx, "POST", "https://auth.openai.com/oauth/token", strings.NewReader(form.Encode())) - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.Header.Set("Accept", "application/json") - resp, errDo := httpClient.Do(req) - if errDo != nil { - authErr := codex.NewAuthenticationError(codex.ErrCodeExchangeFailed, errDo) + // Exchange code for tokens using internal auth service + bundle, errExchange := openaiAuth.ExchangeCodeForTokens(ctx, code, pkceCodes) + if errExchange != nil { + authErr := codex.NewAuthenticationError(codex.ErrCodeExchangeFailed, errExchange) SetOAuthSessionError(state, "Failed to exchange authorization code for tokens") log.Errorf("Failed to exchange authorization code for tokens: %v", authErr) return } - defer func() { _ = resp.Body.Close() }() - respBody, _ := io.ReadAll(resp.Body) - if resp.StatusCode != http.StatusOK { - SetOAuthSessionError(state, fmt.Sprintf("Token exchange failed with status %d", resp.StatusCode)) - log.Errorf("token exchange failed with status %d: %s", resp.StatusCode, string(respBody)) - return - } - var tokenResp struct { - AccessToken string `json:"access_token"` - RefreshToken string `json:"refresh_token"` - IDToken string `json:"id_token"` - ExpiresIn int `json:"expires_in"` - } - if errU := json.Unmarshal(respBody, &tokenResp); errU != nil { - SetOAuthSessionError(state, "Failed to parse token response") - log.Errorf("failed to parse token response: %v", errU) - return - } - claims, _ := codex.ParseJWTToken(tokenResp.IDToken) - email := "" - accountID := "" + + // Extract additional info for filename generation + claims, _ := codex.ParseJWTToken(bundle.TokenData.IDToken) + planType := "" + hashAccountID := "" if claims != nil { - email = claims.GetUserEmail() - accountID = claims.GetAccountID() - } - // Build bundle compatible with existing storage - bundle := &codex.CodexAuthBundle{ - TokenData: codex.CodexTokenData{ - IDToken: tokenResp.IDToken, - AccessToken: tokenResp.AccessToken, - RefreshToken: tokenResp.RefreshToken, - AccountID: accountID, - Email: email, - Expire: time.Now().Add(time.Duration(tokenResp.ExpiresIn) * time.Second).Format(time.RFC3339), - }, - LastRefresh: time.Now().Format(time.RFC3339), + planType = strings.TrimSpace(claims.CodexAuthInfo.ChatgptPlanType) + if accountID := claims.GetAccountID(); accountID != "" { + digest := sha256.Sum256([]byte(accountID)) + hashAccountID = hex.EncodeToString(digest[:])[:8] + } } // Create token storage and persist tokenStorage := openaiAuth.CreateTokenStorage(bundle) + fileName := codex.CredentialFileName(tokenStorage.Email, planType, hashAccountID, true) record := &coreauth.Auth{ - ID: fmt.Sprintf("codex-%s.json", tokenStorage.Email), + ID: fileName, Provider: "codex", - FileName: fmt.Sprintf("codex-%s.json", tokenStorage.Email), + FileName: fileName, Storage: tokenStorage, Metadata: map[string]any{ "email": tokenStorage.Email, @@ -1453,23 +1390,12 @@ func (h *Handler) RequestCodexToken(c *gin.Context) { } func (h *Handler) RequestAntigravityToken(c *gin.Context) { - const ( - antigravityCallbackPort = 51121 - antigravityClientID = "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com" - antigravityClientSecret = "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" - ) - var antigravityScopes = []string{ - "https://www.googleapis.com/auth/cloud-platform", - "https://www.googleapis.com/auth/userinfo.email", - "https://www.googleapis.com/auth/userinfo.profile", - "https://www.googleapis.com/auth/cclog", - "https://www.googleapis.com/auth/experimentsandconfigs", - } - ctx := context.Background() fmt.Println("Initializing Antigravity authentication...") + authSvc := antigravity.NewAntigravityAuth(h.cfg, nil) + state, errState := misc.GenerateRandomState() if errState != nil { log.Errorf("Failed to generate state parameter: %v", errState) @@ -1477,17 +1403,8 @@ func (h *Handler) RequestAntigravityToken(c *gin.Context) { return } - redirectURI := fmt.Sprintf("http://localhost:%d/oauth-callback", antigravityCallbackPort) - - params := url.Values{} - params.Set("access_type", "offline") - params.Set("client_id", antigravityClientID) - params.Set("prompt", "consent") - params.Set("redirect_uri", redirectURI) - params.Set("response_type", "code") - params.Set("scope", strings.Join(antigravityScopes, " ")) - params.Set("state", state) - authURL := "https://accounts.google.com/o/oauth2/v2/auth?" + params.Encode() + redirectURI := fmt.Sprintf("http://localhost:%d/oauth-callback", antigravity.CallbackPort) + authURL := authSvc.BuildAuthURL(state, redirectURI) RegisterOAuthSession(state, "antigravity") @@ -1501,7 +1418,7 @@ func (h *Handler) RequestAntigravityToken(c *gin.Context) { return } var errStart error - if forwarder, errStart = startCallbackForwarder(antigravityCallbackPort, "antigravity", targetURL); errStart != nil { + if forwarder, errStart = startCallbackForwarder(antigravity.CallbackPort, "antigravity", targetURL); errStart != nil { log.WithError(errStart).Error("failed to start antigravity callback forwarder") c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to start callback server"}) return @@ -1510,7 +1427,7 @@ func (h *Handler) RequestAntigravityToken(c *gin.Context) { go func() { if isWebUI { - defer stopCallbackForwarderInstance(antigravityCallbackPort, forwarder) + defer stopCallbackForwarderInstance(antigravity.CallbackPort, forwarder) } waitFile := filepath.Join(h.cfg.AuthDir, fmt.Sprintf(".oauth-antigravity-%s.oauth", state)) @@ -1550,104 +1467,41 @@ func (h *Handler) RequestAntigravityToken(c *gin.Context) { time.Sleep(500 * time.Millisecond) } - httpClient := util.SetProxy(&h.cfg.SDKConfig, &http.Client{}) - form := url.Values{} - form.Set("code", authCode) - form.Set("client_id", antigravityClientID) - form.Set("client_secret", antigravityClientSecret) - form.Set("redirect_uri", redirectURI) - form.Set("grant_type", "authorization_code") - - req, errNewRequest := http.NewRequestWithContext(ctx, http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(form.Encode())) - if errNewRequest != nil { - log.Errorf("Failed to build token request: %v", errNewRequest) - SetOAuthSessionError(state, "Failed to build token request") - return - } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - - resp, errDo := httpClient.Do(req) - if errDo != nil { - log.Errorf("Failed to execute token request: %v", errDo) + tokenResp, errToken := authSvc.ExchangeCodeForTokens(ctx, authCode, redirectURI) + if errToken != nil { + log.Errorf("Failed to exchange token: %v", errToken) SetOAuthSessionError(state, "Failed to exchange token") return } - defer func() { - if errClose := resp.Body.Close(); errClose != nil { - log.Errorf("antigravity token exchange close error: %v", errClose) - } - }() - if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { - bodyBytes, _ := io.ReadAll(resp.Body) - log.Errorf("Antigravity token exchange failed with status %d: %s", resp.StatusCode, string(bodyBytes)) - SetOAuthSessionError(state, fmt.Sprintf("Token exchange failed: %d", resp.StatusCode)) + accessToken := strings.TrimSpace(tokenResp.AccessToken) + if accessToken == "" { + log.Error("antigravity: token exchange returned empty access token") + SetOAuthSessionError(state, "Failed to exchange token") return } - var tokenResp struct { - AccessToken string `json:"access_token"` - RefreshToken string `json:"refresh_token"` - ExpiresIn int64 `json:"expires_in"` - TokenType string `json:"token_type"` - } - if errDecode := json.NewDecoder(resp.Body).Decode(&tokenResp); errDecode != nil { - log.Errorf("Failed to parse token response: %v", errDecode) - SetOAuthSessionError(state, "Failed to parse token response") + email, errInfo := authSvc.FetchUserInfo(ctx, accessToken) + if errInfo != nil { + log.Errorf("Failed to fetch user info: %v", errInfo) + SetOAuthSessionError(state, "Failed to fetch user info") return } - - email := "" - if strings.TrimSpace(tokenResp.AccessToken) != "" { - infoReq, errInfoReq := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json", nil) - if errInfoReq != nil { - log.Errorf("Failed to build user info request: %v", errInfoReq) - SetOAuthSessionError(state, "Failed to build user info request") - return - } - infoReq.Header.Set("Authorization", "Bearer "+tokenResp.AccessToken) - - infoResp, errInfo := httpClient.Do(infoReq) - if errInfo != nil { - log.Errorf("Failed to execute user info request: %v", errInfo) - SetOAuthSessionError(state, "Failed to execute user info request") - return - } - defer func() { - if errClose := infoResp.Body.Close(); errClose != nil { - log.Errorf("antigravity user info close error: %v", errClose) - } - }() - - if infoResp.StatusCode >= http.StatusOK && infoResp.StatusCode < http.StatusMultipleChoices { - var infoPayload struct { - Email string `json:"email"` - } - if errDecodeInfo := json.NewDecoder(infoResp.Body).Decode(&infoPayload); errDecodeInfo == nil { - email = strings.TrimSpace(infoPayload.Email) - } - } else { - bodyBytes, _ := io.ReadAll(infoResp.Body) - log.Errorf("User info request failed with status %d: %s", infoResp.StatusCode, string(bodyBytes)) - SetOAuthSessionError(state, fmt.Sprintf("User info request failed: %d", infoResp.StatusCode)) - return - } + email = strings.TrimSpace(email) + if email == "" { + log.Error("antigravity: user info returned empty email") + SetOAuthSessionError(state, "Failed to fetch user info") + return } projectID := "" - tierID := "" - allowedTiers := []map[string]any(nil) - if strings.TrimSpace(tokenResp.AccessToken) != "" { - accountInfo, errProject := sdkAuth.FetchAntigravityAccountInfo(ctx, tokenResp.AccessToken, httpClient) + if accessToken != "" { + fetchedProjectID, errProject := authSvc.FetchProjectID(ctx, accessToken) if errProject != nil { - log.Warnf("antigravity: failed to fetch account info: %v", errProject) - } else if accountInfo != nil { - projectID = accountInfo.ProjectID - tierID = accountInfo.TierID - allowedTiers = accountInfo.AllowedTiers - if projectID != "" { - log.Infof("antigravity: obtained project ID %s", projectID) - } + log.Warnf("antigravity: failed to fetch project ID: %v", errProject) + } else { + projectID = fetchedProjectID + log.Infof("antigravity: obtained project ID %s", projectID) } } @@ -1666,14 +1520,8 @@ func (h *Handler) RequestAntigravityToken(c *gin.Context) { if projectID != "" { metadata["project_id"] = projectID } - if tierID != "" { - metadata["tier_id"] = tierID - } - if len(allowedTiers) > 0 { - metadata["allowed_tiers"] = allowedTiers - } - fileName := sanitizeAntigravityFileName(email) + fileName := antigravity.CredentialFileName(email) label := strings.TrimSpace(email) if label == "" { label = "antigravity" @@ -1737,7 +1585,7 @@ func (h *Handler) RequestQwenToken(c *gin.Context) { // Create token storage tokenStorage := qwenAuth.CreateTokenStorage(tokenData) - tokenStorage.Email = fmt.Sprintf("qwen-%d", time.Now().UnixMilli()) + tokenStorage.Email = fmt.Sprintf("%d", time.Now().UnixMilli()) record := &coreauth.Auth{ ID: fmt.Sprintf("qwen-%s.json", tokenStorage.Email), Provider: "qwen", @@ -1760,6 +1608,214 @@ func (h *Handler) RequestQwenToken(c *gin.Context) { c.JSON(200, gin.H{"status": "ok", "url": authURL, "state": state}) } +func (h *Handler) RequestIFlowToken(c *gin.Context) { + ctx := context.Background() + + fmt.Println("Initializing iFlow authentication...") + + state := fmt.Sprintf("ifl-%d", time.Now().UnixNano()) + authSvc := iflowauth.NewIFlowAuth(h.cfg) + authURL, redirectURI := authSvc.AuthorizationURL(state, iflowauth.CallbackPort) + + RegisterOAuthSession(state, "iflow") + + isWebUI := isWebUIRequest(c) + var forwarder *callbackForwarder + if isWebUI { + targetURL, errTarget := h.managementCallbackURL("/iflow/callback") + if errTarget != nil { + log.WithError(errTarget).Error("failed to compute iflow callback target") + c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": "callback server unavailable"}) + return + } + var errStart error + if forwarder, errStart = startCallbackForwarder(iflowauth.CallbackPort, "iflow", targetURL); errStart != nil { + log.WithError(errStart).Error("failed to start iflow callback forwarder") + c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": "failed to start callback server"}) + return + } + } + + go func() { + if isWebUI { + defer stopCallbackForwarderInstance(iflowauth.CallbackPort, forwarder) + } + fmt.Println("Waiting for authentication...") + + waitFile := filepath.Join(h.cfg.AuthDir, fmt.Sprintf(".oauth-iflow-%s.oauth", state)) + deadline := time.Now().Add(5 * time.Minute) + var resultMap map[string]string + for { + if !IsOAuthSessionPending(state, "iflow") { + return + } + if time.Now().After(deadline) { + SetOAuthSessionError(state, "Authentication failed") + fmt.Println("Authentication failed: timeout waiting for callback") + return + } + if data, errR := os.ReadFile(waitFile); errR == nil { + _ = os.Remove(waitFile) + _ = json.Unmarshal(data, &resultMap) + break + } + time.Sleep(500 * time.Millisecond) + } + + if errStr := strings.TrimSpace(resultMap["error"]); errStr != "" { + SetOAuthSessionError(state, "Authentication failed") + fmt.Printf("Authentication failed: %s\n", errStr) + return + } + if resultState := strings.TrimSpace(resultMap["state"]); resultState != state { + SetOAuthSessionError(state, "Authentication failed") + fmt.Println("Authentication failed: state mismatch") + return + } + + code := strings.TrimSpace(resultMap["code"]) + if code == "" { + SetOAuthSessionError(state, "Authentication failed") + fmt.Println("Authentication failed: code missing") + return + } + + tokenData, errExchange := authSvc.ExchangeCodeForTokens(ctx, code, redirectURI) + if errExchange != nil { + SetOAuthSessionError(state, "Authentication failed") + fmt.Printf("Authentication failed: %v\n", errExchange) + return + } + + tokenStorage := authSvc.CreateTokenStorage(tokenData) + identifier := strings.TrimSpace(tokenStorage.Email) + if identifier == "" { + identifier = fmt.Sprintf("%d", time.Now().UnixMilli()) + tokenStorage.Email = identifier + } + record := &coreauth.Auth{ + ID: fmt.Sprintf("iflow-%s.json", identifier), + Provider: "iflow", + FileName: fmt.Sprintf("iflow-%s.json", identifier), + Storage: tokenStorage, + Metadata: map[string]any{"email": identifier, "api_key": tokenStorage.APIKey}, + Attributes: map[string]string{"api_key": tokenStorage.APIKey}, + } + + savedPath, errSave := h.saveTokenRecord(ctx, record) + if errSave != nil { + SetOAuthSessionError(state, "Failed to save authentication tokens") + log.Errorf("Failed to save authentication tokens: %v", errSave) + return + } + + fmt.Printf("Authentication successful! Token saved to %s\n", savedPath) + if tokenStorage.APIKey != "" { + fmt.Println("API key obtained and saved") + } + fmt.Println("You can now use iFlow services through this CLI") + CompleteOAuthSession(state) + CompleteOAuthSessionsByProvider("iflow") + }() + + c.JSON(http.StatusOK, gin.H{"status": "ok", "url": authURL, "state": state}) +} + +func (h *Handler) RequestIFlowCookieToken(c *gin.Context) { + ctx := context.Background() + + var payload struct { + Cookie string `json:"cookie"` + } + if err := c.ShouldBindJSON(&payload); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": "cookie is required"}) + return + } + + cookieValue := strings.TrimSpace(payload.Cookie) + + if cookieValue == "" { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": "cookie is required"}) + return + } + + cookieValue, errNormalize := iflowauth.NormalizeCookie(cookieValue) + if errNormalize != nil { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": errNormalize.Error()}) + return + } + + // Check for duplicate BXAuth before authentication + bxAuth := iflowauth.ExtractBXAuth(cookieValue) + if existingFile, err := iflowauth.CheckDuplicateBXAuth(h.cfg.AuthDir, bxAuth); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": "failed to check duplicate"}) + return + } else if existingFile != "" { + existingFileName := filepath.Base(existingFile) + c.JSON(http.StatusConflict, gin.H{"status": "error", "error": "duplicate BXAuth found", "existing_file": existingFileName}) + return + } + + authSvc := iflowauth.NewIFlowAuth(h.cfg) + tokenData, errAuth := authSvc.AuthenticateWithCookie(ctx, cookieValue) + if errAuth != nil { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": errAuth.Error()}) + return + } + + tokenData.Cookie = cookieValue + + tokenStorage := authSvc.CreateCookieTokenStorage(tokenData) + email := strings.TrimSpace(tokenStorage.Email) + if email == "" { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": "failed to extract email from token"}) + return + } + + fileName := iflowauth.SanitizeIFlowFileName(email) + if fileName == "" { + fileName = fmt.Sprintf("iflow-%d", time.Now().UnixMilli()) + } else { + fileName = fmt.Sprintf("iflow-%s", fileName) + } + + tokenStorage.Email = email + timestamp := time.Now().Unix() + + record := &coreauth.Auth{ + ID: fmt.Sprintf("%s-%d.json", fileName, timestamp), + Provider: "iflow", + FileName: fmt.Sprintf("%s-%d.json", fileName, timestamp), + Storage: tokenStorage, + Metadata: map[string]any{ + "email": email, + "api_key": tokenStorage.APIKey, + "expired": tokenStorage.Expire, + "cookie": tokenStorage.Cookie, + "type": tokenStorage.Type, + "last_refresh": tokenStorage.LastRefresh, + }, + Attributes: map[string]string{ + "api_key": tokenStorage.APIKey, + }, + } + + savedPath, errSave := h.saveTokenRecord(ctx, record) + if errSave != nil { + c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": "failed to save authentication tokens"}) + return + } + + fmt.Printf("iFlow cookie authentication successful. Token saved to %s\n", savedPath) + c.JSON(http.StatusOK, gin.H{ + "status": "ok", + "saved_path": savedPath, + "email": email, + "expired": tokenStorage.Expire, + "type": tokenStorage.Type, + }) +} + type projectSelectionRequiredError struct{} func (e *projectSelectionRequiredError) Error() string { @@ -1936,7 +1992,20 @@ func performGeminiCLISetup(ctx context.Context, httpClient *http.Client, storage finalProjectID := projectID if responseProjectID != "" { if explicitProject && !strings.EqualFold(responseProjectID, projectID) { - log.Warnf("Gemini onboarding returned project %s instead of requested %s; keeping requested project ID.", responseProjectID, projectID) + // Check if this is a free user (gen-lang-client projects or free/legacy tier) + isFreeUser := strings.HasPrefix(projectID, "gen-lang-client-") || + strings.EqualFold(tierID, "FREE") || + strings.EqualFold(tierID, "LEGACY") + + if isFreeUser { + // For free users, use backend project ID for preview model access + log.Infof("Gemini onboarding: frontend project %s maps to backend project %s", projectID, responseProjectID) + log.Infof("Using backend project ID: %s (recommended for preview model access)", responseProjectID) + finalProjectID = responseProjectID + } else { + // Pro users: keep requested project ID (original behavior) + log.Warnf("Gemini onboarding returned project %s instead of requested %s; keeping requested project ID.", responseProjectID, projectID) + } } else { finalProjectID = responseProjectID } @@ -2115,556 +2184,8 @@ func (h *Handler) GetAuthStatus(c *gin.Context) { return } if status != "" { - if strings.HasPrefix(status, "device_code|") { - parts := strings.SplitN(status, "|", 3) - if len(parts) == 3 { - c.JSON(http.StatusOK, gin.H{ - "status": "device_code", - "verification_url": parts[1], - "user_code": parts[2], - }) - return - } - } - if strings.HasPrefix(status, "auth_url|") { - authURL := strings.TrimPrefix(status, "auth_url|") - c.JSON(http.StatusOK, gin.H{ - "status": "auth_url", - "url": authURL, - }) - return - } c.JSON(http.StatusOK, gin.H{"status": "error", "error": status}) return } c.JSON(http.StatusOK, gin.H{"status": "wait"}) } - -const kiroCallbackPort = 9876 - -func (h *Handler) RequestKiroToken(c *gin.Context) { - ctx := context.Background() - - // Get the login method from query parameter (default: aws for device code flow) - method := strings.ToLower(strings.TrimSpace(c.Query("method"))) - if method == "" { - method = "aws" - } - - fmt.Println("Initializing Kiro authentication...") - - state := fmt.Sprintf("kiro-%d", time.Now().UnixNano()) - - switch method { - case "aws", "builder-id": - RegisterOAuthSession(state, "kiro") - - // AWS Builder ID uses device code flow (no callback needed) - go func() { - ssoClient := kiroauth.NewSSOOIDCClient(h.cfg) - - // Step 1: Register client - fmt.Println("Registering client...") - regResp, errRegister := ssoClient.RegisterClient(ctx) - if errRegister != nil { - log.Errorf("Failed to register client: %v", errRegister) - SetOAuthSessionError(state, "Failed to register client") - return - } - - // Step 2: Start device authorization - fmt.Println("Starting device authorization...") - authResp, errAuth := ssoClient.StartDeviceAuthorization(ctx, regResp.ClientID, regResp.ClientSecret) - if errAuth != nil { - log.Errorf("Failed to start device auth: %v", errAuth) - SetOAuthSessionError(state, "Failed to start device authorization") - return - } - - // Store the verification URL for the frontend to display. - // Using "|" as separator because URLs contain ":". - SetOAuthSessionError(state, "device_code|"+authResp.VerificationURIComplete+"|"+authResp.UserCode) - - // Step 3: Poll for token - fmt.Println("Waiting for authorization...") - interval := 5 * time.Second - if authResp.Interval > 0 { - interval = time.Duration(authResp.Interval) * time.Second - } - deadline := time.Now().Add(time.Duration(authResp.ExpiresIn) * time.Second) - - for time.Now().Before(deadline) { - select { - case <-ctx.Done(): - SetOAuthSessionError(state, "Authorization cancelled") - return - case <-time.After(interval): - tokenResp, errToken := ssoClient.CreateToken(ctx, regResp.ClientID, regResp.ClientSecret, authResp.DeviceCode) - if errToken != nil { - errStr := errToken.Error() - if strings.Contains(errStr, "authorization_pending") { - continue - } - if strings.Contains(errStr, "slow_down") { - interval += 5 * time.Second - continue - } - log.Errorf("Token creation failed: %v", errToken) - SetOAuthSessionError(state, "Token creation failed") - return - } - - // Success! Save the token - expiresAt := time.Now().Add(time.Duration(tokenResp.ExpiresIn) * time.Second) - email := kiroauth.ExtractEmailFromJWT(tokenResp.AccessToken) - - idPart := kiroauth.SanitizeEmailForFilename(email) - if idPart == "" { - idPart = fmt.Sprintf("%d", time.Now().UnixNano()%100000) - } - - now := time.Now() - fileName := fmt.Sprintf("kiro-aws-%s.json", idPart) - - record := &coreauth.Auth{ - ID: fileName, - Provider: "kiro", - FileName: fileName, - Metadata: map[string]any{ - "type": "kiro", - "access_token": tokenResp.AccessToken, - "refresh_token": tokenResp.RefreshToken, - "expires_at": expiresAt.Format(time.RFC3339), - "auth_method": "builder-id", - "provider": "AWS", - "client_id": regResp.ClientID, - "client_secret": regResp.ClientSecret, - "email": email, - "last_refresh": now.Format(time.RFC3339), - }, - } - - savedPath, errSave := h.saveTokenRecord(ctx, record) - if errSave != nil { - log.Errorf("Failed to save authentication tokens: %v", errSave) - SetOAuthSessionError(state, "Failed to save authentication tokens") - return - } - - fmt.Printf("Authentication successful! Token saved to %s\n", savedPath) - if email != "" { - fmt.Printf("Authenticated as: %s\n", email) - } - CompleteOAuthSession(state) - return - } - } - - SetOAuthSessionError(state, "Authorization timed out") - }() - - // Return immediately with the state for polling - c.JSON(http.StatusOK, gin.H{"status": "ok", "state": state, "method": "device_code"}) - - case "google", "github": - RegisterOAuthSession(state, "kiro") - - // Social auth uses protocol handler - for WEB UI we use a callback forwarder - provider := "Google" - if method == "github" { - provider = "Github" - } - - isWebUI := isWebUIRequest(c) - if isWebUI { - targetURL, errTarget := h.managementCallbackURL("/kiro/callback") - if errTarget != nil { - log.WithError(errTarget).Error("failed to compute kiro callback target") - c.JSON(http.StatusInternalServerError, gin.H{"error": "callback server unavailable"}) - return - } - if _, errStart := startCallbackForwarder(kiroCallbackPort, "kiro", targetURL); errStart != nil { - log.WithError(errStart).Error("failed to start kiro callback forwarder") - c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to start callback server"}) - return - } - } - - go func() { - if isWebUI { - defer stopCallbackForwarder(kiroCallbackPort) - } - - socialClient := kiroauth.NewSocialAuthClient(h.cfg) - - // Generate PKCE codes - codeVerifier, codeChallenge, errPKCE := generateKiroPKCE() - if errPKCE != nil { - log.Errorf("Failed to generate PKCE: %v", errPKCE) - SetOAuthSessionError(state, "Failed to generate PKCE") - return - } - - // Build login URL - authURL := fmt.Sprintf("%s/login?idp=%s&redirect_uri=%s&code_challenge=%s&code_challenge_method=S256&state=%s&prompt=select_account", - "https://prod.us-east-1.auth.desktop.kiro.dev", - provider, - url.QueryEscape(kiroauth.KiroRedirectURI), - codeChallenge, - state, - ) - - // Store auth URL for frontend. - // Using "|" as separator because URLs contain ":". - SetOAuthSessionError(state, "auth_url|"+authURL) - - // Wait for callback file - waitFile := filepath.Join(h.cfg.AuthDir, fmt.Sprintf(".oauth-kiro-%s.oauth", state)) - deadline := time.Now().Add(5 * time.Minute) - - for { - if time.Now().After(deadline) { - log.Error("oauth flow timed out") - SetOAuthSessionError(state, "OAuth flow timed out") - return - } - if data, errRead := os.ReadFile(waitFile); errRead == nil { - var m map[string]string - _ = json.Unmarshal(data, &m) - _ = os.Remove(waitFile) - if errStr := m["error"]; errStr != "" { - log.Errorf("Authentication failed: %s", errStr) - SetOAuthSessionError(state, "Authentication failed") - return - } - if m["state"] != state { - log.Errorf("State mismatch") - SetOAuthSessionError(state, "State mismatch") - return - } - code := m["code"] - if code == "" { - log.Error("No authorization code received") - SetOAuthSessionError(state, "No authorization code received") - return - } - - // Exchange code for tokens - tokenReq := &kiroauth.CreateTokenRequest{ - Code: code, - CodeVerifier: codeVerifier, - RedirectURI: kiroauth.KiroRedirectURI, - } - - tokenResp, errToken := socialClient.CreateToken(ctx, tokenReq) - if errToken != nil { - log.Errorf("Failed to exchange code for tokens: %v", errToken) - SetOAuthSessionError(state, "Failed to exchange code for tokens") - return - } - - // Save the token - expiresIn := tokenResp.ExpiresIn - if expiresIn <= 0 { - expiresIn = 3600 - } - expiresAt := time.Now().Add(time.Duration(expiresIn) * time.Second) - email := kiroauth.ExtractEmailFromJWT(tokenResp.AccessToken) - - idPart := kiroauth.SanitizeEmailForFilename(email) - if idPart == "" { - idPart = fmt.Sprintf("%d", time.Now().UnixNano()%100000) - } - - now := time.Now() - fileName := fmt.Sprintf("kiro-%s-%s.json", strings.ToLower(provider), idPart) - - record := &coreauth.Auth{ - ID: fileName, - Provider: "kiro", - FileName: fileName, - Metadata: map[string]any{ - "type": "kiro", - "access_token": tokenResp.AccessToken, - "refresh_token": tokenResp.RefreshToken, - "profile_arn": tokenResp.ProfileArn, - "expires_at": expiresAt.Format(time.RFC3339), - "auth_method": "social", - "provider": provider, - "email": email, - "last_refresh": now.Format(time.RFC3339), - }, - } - - savedPath, errSave := h.saveTokenRecord(ctx, record) - if errSave != nil { - log.Errorf("Failed to save authentication tokens: %v", errSave) - SetOAuthSessionError(state, "Failed to save authentication tokens") - return - } - - fmt.Printf("Authentication successful! Token saved to %s\n", savedPath) - if email != "" { - fmt.Printf("Authenticated as: %s\n", email) - } - CompleteOAuthSession(state) - return - } - time.Sleep(500 * time.Millisecond) - } - }() - - c.JSON(http.StatusOK, gin.H{"status": "ok", "state": state, "method": "social"}) - - default: - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid method, use 'aws', 'google', or 'github'"}) - } -} - -// generateKiroPKCE generates PKCE code verifier and challenge for Kiro OAuth. -func generateKiroPKCE() (verifier, challenge string, err error) { - b := make([]byte, 32) - if _, errRead := io.ReadFull(rand.Reader, b); errRead != nil { - return "", "", fmt.Errorf("failed to generate random bytes: %w", errRead) - } - verifier = base64.RawURLEncoding.EncodeToString(b) - - h := sha256.Sum256([]byte(verifier)) - challenge = base64.RawURLEncoding.EncodeToString(h[:]) - - return verifier, challenge, nil -} - -// SaveMiniMaxAPIKey saves a MiniMax API key as an auth file. -func (h *Handler) SaveMiniMaxAPIKey(c *gin.Context) { - var req struct { - APIKey string `json:"api_key"` - Label string `json:"label"` - } - if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"}) - return - } - - if req.APIKey == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "api_key is required"}) - return - } - - now := time.Now() - label := req.Label - if label == "" { - label = "minimax-api-key" - } - fileName := fmt.Sprintf("minimax-%s.json", now.Format("20060102-150405")) - - record := &coreauth.Auth{ - ID: fileName, - Provider: "minimax", - FileName: fileName, - Label: label, - Status: coreauth.StatusActive, - CreatedAt: now, - UpdatedAt: now, - Metadata: map[string]any{ - "type": "minimax", - "api_key": req.APIKey, - }, - Attributes: map[string]string{ - "source": "webui", - }, - } - - authPath := filepath.Join(h.cfg.AuthDir, fileName) - data, err := json.MarshalIndent(record, "", " ") - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to serialize auth record"}) - return - } - - if err := os.WriteFile(authPath, data, 0600); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to save auth file"}) - return - } - - if h.authManager != nil { - if reloader, ok := interface{}(h.authManager).(interface{ ReloadAuthFiles() error }); ok { - _ = reloader.ReloadAuthFiles() - } - } - - log.Infof("MiniMax API key saved: %s", fileName) - c.JSON(http.StatusOK, gin.H{"success": true, "message": "MiniMax API key saved", "filename": fileName}) -} - -// SaveZhipuAPIKey saves a Zhipu AI API key as an auth file. -func (h *Handler) SaveZhipuAPIKey(c *gin.Context) { - var req struct { - APIKey string `json:"api_key"` - Label string `json:"label"` - } - if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"}) - return - } - - if req.APIKey == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "api_key is required"}) - return - } - - now := time.Now() - label := req.Label - if label == "" { - label = "zhipu-api-key" - } - fileName := fmt.Sprintf("zhipu-%s.json", now.Format("20060102-150405")) - - record := &coreauth.Auth{ - ID: fileName, - Provider: "zhipu", - FileName: fileName, - Label: label, - Status: coreauth.StatusActive, - CreatedAt: now, - UpdatedAt: now, - Metadata: map[string]any{ - "type": "zhipu", - "api_key": req.APIKey, - }, - Attributes: map[string]string{ - "source": "webui", - }, - } - - authPath := filepath.Join(h.cfg.AuthDir, fileName) - data, err := json.MarshalIndent(record, "", " ") - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to serialize auth record"}) - return - } - - if err := os.WriteFile(authPath, data, 0600); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to save auth file"}) - return - } - - if h.authManager != nil { - if reloader, ok := interface{}(h.authManager).(interface{ ReloadAuthFiles() error }); ok { - _ = reloader.ReloadAuthFiles() - } - } - - log.Infof("Zhipu API key saved: %s", fileName) - c.JSON(http.StatusOK, gin.H{"success": true, "message": "Zhipu API key saved", "filename": fileName}) -} - -// ImportAmazonQToken imports token from Amazon Q CLI's SQLite database. -// Amazon Q CLI stores tokens at ~/.local/share/amazon-q/data.sqlite3 (or WSL path on Windows). -// This provides access to a separate usage quota pool from Kiro IDE. -func (h *Handler) ImportAmazonQToken(c *gin.Context) { - log.Info("Importing Amazon Q CLI token...") - - // Check if Amazon Q CLI tokens are available - if !amazonqauth.IsAvailable() { - c.JSON(http.StatusBadRequest, gin.H{ - "error": "Amazon Q CLI token not available", - "message": "Please run 'q login' in your terminal first to authenticate with Amazon Q CLI", - }) - return - } - - // Load the token - kiroToken, err := amazonqauth.LoadAmazonQToken() - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": "Failed to read Amazon Q CLI token", - "message": err.Error(), - }) - return - } - - // Extract email from JWT if available - email := kiroauth.ExtractEmailFromJWT(kiroToken.AccessToken) - - // Create identifier for file naming - idPart := "amazonq-cli" - if email != "" { - idPart = fmt.Sprintf("amazonq-cli-%s", kiroauth.SanitizeEmailForFilename(email)) - } - - now := time.Now() - fileName := fmt.Sprintf("kiro-%s.json", idPart) - - // Parse expires_at - expiresAt, err := time.Parse(time.RFC3339, kiroToken.ExpiresAt) - if err != nil { - expiresAt = now.Add(1 * time.Hour) - } - - record := &coreauth.Auth{ - ID: fileName, - Provider: "kiro", // Uses kiro provider since same API - FileName: fileName, - Label: "kiro-amazonq-cli", - Status: coreauth.StatusActive, - CreatedAt: now, - UpdatedAt: now, - Metadata: map[string]any{ - "type": "kiro", - "access_token": kiroToken.AccessToken, - "refresh_token": kiroToken.RefreshToken, - "expires_at": kiroToken.ExpiresAt, - "auth_method": kiroToken.AuthMethod, - "provider": kiroToken.Provider, - "client_id": kiroToken.ClientID, - "client_secret": kiroToken.ClientSecret, - "email": email, - "start_url": kiroToken.StartURL, - "region": kiroToken.Region, - "preferred_endpoint": "amazonq", // Force Amazon Q endpoint (CLI origin) - }, - Attributes: map[string]string{ - "source": "amazonq-cli-import", - "email": email, - "preferred_endpoint": "amazonq", // Force Amazon Q endpoint (CLI origin) - }, - NextRefreshAfter: expiresAt.Add(-5 * time.Minute), - } - - // Save the auth file - authPath := filepath.Join(h.cfg.AuthDir, fileName) - data, err := json.MarshalIndent(record, "", " ") - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": "Failed to serialize auth record", - "message": err.Error(), - }) - return - } - - if err := os.WriteFile(authPath, data, 0600); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": "Failed to save auth file", - "message": err.Error(), - }) - return - } - - // Reload auth files - if h.authManager != nil { - if reloader, ok := interface{}(h.authManager).(interface{ ReloadAuthFiles() error }); ok { - _ = reloader.ReloadAuthFiles() - } - } - - log.Infof("Amazon Q CLI token imported successfully: %s", fileName) - - c.JSON(http.StatusOK, gin.H{ - "success": true, - "message": "Amazon Q CLI token imported successfully", - "email": email, - "filename": fileName, - "note": "Amazon Q CLI has separate usage quota from Kiro IDE", - }) -} diff --git a/internal/api/handlers/management/config_basic.go b/internal/api/handlers/management/config_basic.go index d6a280c7..2d3cd1fb 100644 --- a/internal/api/handlers/management/config_basic.go +++ b/internal/api/handlers/management/config_basic.go @@ -12,17 +12,15 @@ import ( "github.com/gin-gonic/gin" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" - "github.com/router-for-me/CLIProxyAPI/v6/internal/routingdebug" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" log "github.com/sirupsen/logrus" - "github.com/tidwall/gjson" "gopkg.in/yaml.v3" ) const ( - latestReleaseURL = "https://api.github.com/repos/router-for-me/CLIProxyAPIPlus/releases/latest" - latestReleaseUserAgent = "CLIProxyAPIPlus" + latestReleaseURL = "https://api.github.com/repos/router-for-me/CLIProxyAPI/releases/latest" + latestReleaseUserAgent = "CLIProxyAPI" ) func (h *Handler) GetConfig(c *gin.Context) { @@ -34,88 +32,6 @@ func (h *Handler) GetConfig(c *gin.Context) { c.JSON(200, &cfgCopy) } -// GetRoutingPreview returns a JSON description of how a given model would be normalised and routed. -// -// Query parameters: -// - model: required model identifier (may be alias, auto, or provider://model) -func (h *Handler) GetRoutingPreview(c *gin.Context) { - if h == nil || h.cfg == nil { - c.JSON(http.StatusServiceUnavailable, gin.H{"error": "unavailable", "message": "configuration not loaded"}) - return - } - model := strings.TrimSpace(c.Query("model")) - if model == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "missing_model", "message": "provide model via ?model="}) - return - } - preview, err := h.computeRoutingPreview(model) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "routing_error", "message": err.Error()}) - return - } - c.JSON(http.StatusOK, preview) -} - -// GetRoutingRecent returns a small ring buffer of recently routed requests using the runtime selection trace. -func (h *Handler) GetRoutingRecent(c *gin.Context) { - // Even if h or cfg are nil, routingdebug.Snapshot() is safe; this endpoint is purely diagnostic. - entries := routingdebug.Snapshot() - if len(entries) == 0 { - c.JSON(http.StatusOK, gin.H{"entries": []routingdebug.Entry{}}) - return - } - c.JSON(http.StatusOK, gin.H{"entries": entries}) -} - -// PostRoutingPreview accepts a full request JSON (OpenAI/OpenAIResponses-compatible) and -// extracts the model name for routing preview. -func (h *Handler) PostRoutingPreview(c *gin.Context) { - if h == nil || h.cfg == nil { - c.JSON(http.StatusServiceUnavailable, gin.H{"error": "unavailable", "message": "configuration not loaded"}) - return - } - - body, err := io.ReadAll(io.LimitReader(c.Request.Body, 1<<20)) // 1 MiB safety cap - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_body", "message": "cannot read request body"}) - return - } - if !gjson.ValidBytes(body) { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_json", "message": "body is not valid JSON"}) - return - } - - // Try a few common shapes to extract the model name. - paths := []string{ - "model", // standard OpenAI - "request.model", // some wrapper shapes - "response.model", // responses-style wrappers - } - var model string - for _, path := range paths { - v := gjson.GetBytes(body, path) - if v.Exists() && strings.TrimSpace(v.String()) != "" { - model = strings.TrimSpace(v.String()) - break - } - } - if model == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "missing_model", "message": "could not find model field in JSON body"}) - return - } - - preview, err := h.computeRoutingPreview(model) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "routing_error", "message": err.Error(), "model": model}) - return - } - - c.JSON(http.StatusOK, gin.H{ - "model": model, - "preview": preview, - }) -} - type releaseInfo struct { TagName string `json:"tag_name"` Name string `json:"name"` @@ -226,24 +142,11 @@ func (h *Handler) PutConfigYAML(c *gin.Context) { defer func() { _ = os.Remove(tempFile) }() - validatedCfg, err := config.LoadConfigOptional(tempFile, false) + _, err = config.LoadConfigOptional(tempFile, false) if err != nil { c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "invalid_config", "message": err.Error()}) return } - if validatedCfg == nil { - c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "invalid_config", "message": "configuration is empty"}) - return - } - if warnings, errValidate := config.ValidateConfig(validatedCfg); errValidate != nil { - c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "invalid_config", "message": errValidate.Error()}) - return - } else if len(warnings) > 0 { - // Surface warnings in logs; the response body will include them after successful persist. - for _, w := range warnings { - log.Warnf("config warning (management update): %s", w) - } - } h.mu.Lock() defer h.mu.Unlock() if WriteConfig(h.configFilePath, body) != nil { @@ -257,11 +160,7 @@ func (h *Handler) PutConfigYAML(c *gin.Context) { return } h.cfg = newCfg - resp := gin.H{"ok": true, "changed": []string{"config"}} - if warnings, errValidate := config.ValidateConfig(newCfg); errValidate == nil && len(warnings) > 0 { - resp["warnings"] = warnings - } - c.JSON(http.StatusOK, resp) + c.JSON(http.StatusOK, gin.H{"ok": true, "changed": []string{"config"}}) } // GetConfigYAML returns the raw config.yaml file bytes without re-encoding. @@ -303,6 +202,26 @@ func (h *Handler) PutLoggingToFile(c *gin.Context) { h.updateBoolField(c, func(v bool) { h.cfg.LoggingToFile = v }) } +// LogsMaxTotalSizeMB +func (h *Handler) GetLogsMaxTotalSizeMB(c *gin.Context) { + c.JSON(200, gin.H{"logs-max-total-size-mb": h.cfg.LogsMaxTotalSizeMB}) +} +func (h *Handler) PutLogsMaxTotalSizeMB(c *gin.Context) { + var body struct { + Value *int `json:"value"` + } + if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil || body.Value == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"}) + return + } + value := *body.Value + if value < 0 { + value = 0 + } + h.cfg.LogsMaxTotalSizeMB = value + h.persist(c) +} + // Request log func (h *Handler) GetRequestLog(c *gin.Context) { c.JSON(200, gin.H{"request-log": h.cfg.RequestLog}) } func (h *Handler) PutRequestLog(c *gin.Context) { @@ -333,6 +252,52 @@ func (h *Handler) PutMaxRetryInterval(c *gin.Context) { h.updateIntField(c, func(v int) { h.cfg.MaxRetryInterval = v }) } +// ForceModelPrefix +func (h *Handler) GetForceModelPrefix(c *gin.Context) { + c.JSON(200, gin.H{"force-model-prefix": h.cfg.ForceModelPrefix}) +} +func (h *Handler) PutForceModelPrefix(c *gin.Context) { + h.updateBoolField(c, func(v bool) { h.cfg.ForceModelPrefix = v }) +} + +func normalizeRoutingStrategy(strategy string) (string, bool) { + normalized := strings.ToLower(strings.TrimSpace(strategy)) + switch normalized { + case "", "round-robin", "roundrobin", "rr": + return "round-robin", true + case "fill-first", "fillfirst", "ff": + return "fill-first", true + default: + return "", false + } +} + +// RoutingStrategy +func (h *Handler) GetRoutingStrategy(c *gin.Context) { + strategy, ok := normalizeRoutingStrategy(h.cfg.Routing.Strategy) + if !ok { + c.JSON(200, gin.H{"strategy": strings.TrimSpace(h.cfg.Routing.Strategy)}) + return + } + c.JSON(200, gin.H{"strategy": strategy}) +} +func (h *Handler) PutRoutingStrategy(c *gin.Context) { + var body struct { + Value *string `json:"value"` + } + if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil || body.Value == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"}) + return + } + normalized, ok := normalizeRoutingStrategy(*body.Value) + if !ok { + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid strategy"}) + return + } + h.cfg.Routing.Strategy = normalized + h.persist(c) +} + // Proxy URL func (h *Handler) GetProxyURL(c *gin.Context) { c.JSON(200, gin.H{"proxy-url": h.cfg.ProxyURL}) } func (h *Handler) PutProxyURL(c *gin.Context) { diff --git a/internal/api/handlers/management/config_lists.go b/internal/api/handlers/management/config_lists.go index 2e30ef0e..4e0e0284 100644 --- a/internal/api/handlers/management/config_lists.go +++ b/internal/api/handlers/management/config_lists.go @@ -487,6 +487,137 @@ func (h *Handler) DeleteOpenAICompat(c *gin.Context) { c.JSON(400, gin.H{"error": "missing name or index"}) } +// vertex-api-key: []VertexCompatKey +func (h *Handler) GetVertexCompatKeys(c *gin.Context) { + c.JSON(200, gin.H{"vertex-api-key": h.cfg.VertexCompatAPIKey}) +} +func (h *Handler) PutVertexCompatKeys(c *gin.Context) { + data, err := c.GetRawData() + if err != nil { + c.JSON(400, gin.H{"error": "failed to read body"}) + return + } + var arr []config.VertexCompatKey + if err = json.Unmarshal(data, &arr); err != nil { + var obj struct { + Items []config.VertexCompatKey `json:"items"` + } + if err2 := json.Unmarshal(data, &obj); err2 != nil || len(obj.Items) == 0 { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + arr = obj.Items + } + for i := range arr { + normalizeVertexCompatKey(&arr[i]) + } + h.cfg.VertexCompatAPIKey = arr + h.cfg.SanitizeVertexCompatKeys() + h.persist(c) +} +func (h *Handler) PatchVertexCompatKey(c *gin.Context) { + type vertexCompatPatch struct { + APIKey *string `json:"api-key"` + Prefix *string `json:"prefix"` + BaseURL *string `json:"base-url"` + ProxyURL *string `json:"proxy-url"` + Headers *map[string]string `json:"headers"` + Models *[]config.VertexCompatModel `json:"models"` + } + var body struct { + Index *int `json:"index"` + Match *string `json:"match"` + Value *vertexCompatPatch `json:"value"` + } + if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil || body.Value == nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + targetIndex := -1 + if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.VertexCompatAPIKey) { + targetIndex = *body.Index + } + if targetIndex == -1 && body.Match != nil { + match := strings.TrimSpace(*body.Match) + if match != "" { + for i := range h.cfg.VertexCompatAPIKey { + if h.cfg.VertexCompatAPIKey[i].APIKey == match { + targetIndex = i + break + } + } + } + } + if targetIndex == -1 { + c.JSON(404, gin.H{"error": "item not found"}) + return + } + + entry := h.cfg.VertexCompatAPIKey[targetIndex] + if body.Value.APIKey != nil { + trimmed := strings.TrimSpace(*body.Value.APIKey) + if trimmed == "" { + h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:targetIndex], h.cfg.VertexCompatAPIKey[targetIndex+1:]...) + h.cfg.SanitizeVertexCompatKeys() + h.persist(c) + return + } + entry.APIKey = trimmed + } + if body.Value.Prefix != nil { + entry.Prefix = strings.TrimSpace(*body.Value.Prefix) + } + if body.Value.BaseURL != nil { + trimmed := strings.TrimSpace(*body.Value.BaseURL) + if trimmed == "" { + h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:targetIndex], h.cfg.VertexCompatAPIKey[targetIndex+1:]...) + h.cfg.SanitizeVertexCompatKeys() + h.persist(c) + return + } + entry.BaseURL = trimmed + } + if body.Value.ProxyURL != nil { + entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL) + } + if body.Value.Headers != nil { + entry.Headers = config.NormalizeHeaders(*body.Value.Headers) + } + if body.Value.Models != nil { + entry.Models = append([]config.VertexCompatModel(nil), (*body.Value.Models)...) + } + normalizeVertexCompatKey(&entry) + h.cfg.VertexCompatAPIKey[targetIndex] = entry + h.cfg.SanitizeVertexCompatKeys() + h.persist(c) +} + +func (h *Handler) DeleteVertexCompatKey(c *gin.Context) { + if val := strings.TrimSpace(c.Query("api-key")); val != "" { + out := make([]config.VertexCompatKey, 0, len(h.cfg.VertexCompatAPIKey)) + for _, v := range h.cfg.VertexCompatAPIKey { + if v.APIKey != val { + out = append(out, v) + } + } + h.cfg.VertexCompatAPIKey = out + h.cfg.SanitizeVertexCompatKeys() + h.persist(c) + return + } + if idxStr := c.Query("index"); idxStr != "" { + var idx int + _, errScan := fmt.Sscanf(idxStr, "%d", &idx) + if errScan == nil && idx >= 0 && idx < len(h.cfg.VertexCompatAPIKey) { + h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:idx], h.cfg.VertexCompatAPIKey[idx+1:]...) + h.cfg.SanitizeVertexCompatKeys() + h.persist(c) + return + } + } + c.JSON(400, gin.H{"error": "missing api-key or index"}) +} + // oauth-excluded-models: map[string][]string func (h *Handler) GetOAuthExcludedModels(c *gin.Context) { c.JSON(200, gin.H{"oauth-excluded-models": config.NormalizeOAuthExcludedModels(h.cfg.OAuthExcludedModels)}) @@ -572,6 +703,103 @@ func (h *Handler) DeleteOAuthExcludedModels(c *gin.Context) { h.persist(c) } +// oauth-model-alias: map[string][]OAuthModelAlias +func (h *Handler) GetOAuthModelAlias(c *gin.Context) { + c.JSON(200, gin.H{"oauth-model-alias": sanitizedOAuthModelAlias(h.cfg.OAuthModelAlias)}) +} + +func (h *Handler) PutOAuthModelAlias(c *gin.Context) { + data, err := c.GetRawData() + if err != nil { + c.JSON(400, gin.H{"error": "failed to read body"}) + return + } + var entries map[string][]config.OAuthModelAlias + if err = json.Unmarshal(data, &entries); err != nil { + var wrapper struct { + Items map[string][]config.OAuthModelAlias `json:"items"` + } + if err2 := json.Unmarshal(data, &wrapper); err2 != nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + entries = wrapper.Items + } + h.cfg.OAuthModelAlias = sanitizedOAuthModelAlias(entries) + h.persist(c) +} + +func (h *Handler) PatchOAuthModelAlias(c *gin.Context) { + var body struct { + Provider *string `json:"provider"` + Channel *string `json:"channel"` + Aliases []config.OAuthModelAlias `json:"aliases"` + } + if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + channelRaw := "" + if body.Channel != nil { + channelRaw = *body.Channel + } else if body.Provider != nil { + channelRaw = *body.Provider + } + channel := strings.ToLower(strings.TrimSpace(channelRaw)) + if channel == "" { + c.JSON(400, gin.H{"error": "invalid channel"}) + return + } + + normalizedMap := sanitizedOAuthModelAlias(map[string][]config.OAuthModelAlias{channel: body.Aliases}) + normalized := normalizedMap[channel] + if len(normalized) == 0 { + if h.cfg.OAuthModelAlias == nil { + c.JSON(404, gin.H{"error": "channel not found"}) + return + } + if _, ok := h.cfg.OAuthModelAlias[channel]; !ok { + c.JSON(404, gin.H{"error": "channel not found"}) + return + } + delete(h.cfg.OAuthModelAlias, channel) + if len(h.cfg.OAuthModelAlias) == 0 { + h.cfg.OAuthModelAlias = nil + } + h.persist(c) + return + } + if h.cfg.OAuthModelAlias == nil { + h.cfg.OAuthModelAlias = make(map[string][]config.OAuthModelAlias) + } + h.cfg.OAuthModelAlias[channel] = normalized + h.persist(c) +} + +func (h *Handler) DeleteOAuthModelAlias(c *gin.Context) { + channel := strings.ToLower(strings.TrimSpace(c.Query("channel"))) + if channel == "" { + channel = strings.ToLower(strings.TrimSpace(c.Query("provider"))) + } + if channel == "" { + c.JSON(400, gin.H{"error": "missing channel"}) + return + } + if h.cfg.OAuthModelAlias == nil { + c.JSON(404, gin.H{"error": "channel not found"}) + return + } + if _, ok := h.cfg.OAuthModelAlias[channel]; !ok { + c.JSON(404, gin.H{"error": "channel not found"}) + return + } + delete(h.cfg.OAuthModelAlias, channel) + if len(h.cfg.OAuthModelAlias) == 0 { + h.cfg.OAuthModelAlias = nil + } + h.persist(c) +} + // codex-api-key: []CodexKey func (h *Handler) GetCodexKeys(c *gin.Context) { c.JSON(200, gin.H{"codex-api-key": h.cfg.CodexKey}) @@ -788,3 +1016,350 @@ func normalizeCodexKey(entry *config.CodexKey) { } entry.Models = normalized } + +func normalizeVertexCompatKey(entry *config.VertexCompatKey) { + if entry == nil { + return + } + entry.APIKey = strings.TrimSpace(entry.APIKey) + entry.Prefix = strings.TrimSpace(entry.Prefix) + entry.BaseURL = strings.TrimSpace(entry.BaseURL) + entry.ProxyURL = strings.TrimSpace(entry.ProxyURL) + entry.Headers = config.NormalizeHeaders(entry.Headers) + if len(entry.Models) == 0 { + return + } + normalized := make([]config.VertexCompatModel, 0, len(entry.Models)) + for i := range entry.Models { + model := entry.Models[i] + model.Name = strings.TrimSpace(model.Name) + model.Alias = strings.TrimSpace(model.Alias) + if model.Name == "" || model.Alias == "" { + continue + } + normalized = append(normalized, model) + } + entry.Models = normalized +} + +func sanitizedOAuthModelAlias(entries map[string][]config.OAuthModelAlias) map[string][]config.OAuthModelAlias { + if len(entries) == 0 { + return nil + } + copied := make(map[string][]config.OAuthModelAlias, len(entries)) + for channel, aliases := range entries { + if len(aliases) == 0 { + continue + } + copied[channel] = append([]config.OAuthModelAlias(nil), aliases...) + } + if len(copied) == 0 { + return nil + } + cfg := config.Config{OAuthModelAlias: copied} + cfg.SanitizeOAuthModelAlias() + if len(cfg.OAuthModelAlias) == 0 { + return nil + } + return cfg.OAuthModelAlias +} + +// GetAmpCode returns the complete ampcode configuration. +func (h *Handler) GetAmpCode(c *gin.Context) { + if h == nil || h.cfg == nil { + c.JSON(200, gin.H{"ampcode": config.AmpCode{}}) + return + } + c.JSON(200, gin.H{"ampcode": h.cfg.AmpCode}) +} + +// GetAmpUpstreamURL returns the ampcode upstream URL. +func (h *Handler) GetAmpUpstreamURL(c *gin.Context) { + if h == nil || h.cfg == nil { + c.JSON(200, gin.H{"upstream-url": ""}) + return + } + c.JSON(200, gin.H{"upstream-url": h.cfg.AmpCode.UpstreamURL}) +} + +// PutAmpUpstreamURL updates the ampcode upstream URL. +func (h *Handler) PutAmpUpstreamURL(c *gin.Context) { + h.updateStringField(c, func(v string) { h.cfg.AmpCode.UpstreamURL = strings.TrimSpace(v) }) +} + +// DeleteAmpUpstreamURL clears the ampcode upstream URL. +func (h *Handler) DeleteAmpUpstreamURL(c *gin.Context) { + h.cfg.AmpCode.UpstreamURL = "" + h.persist(c) +} + +// GetAmpUpstreamAPIKey returns the ampcode upstream API key. +func (h *Handler) GetAmpUpstreamAPIKey(c *gin.Context) { + if h == nil || h.cfg == nil { + c.JSON(200, gin.H{"upstream-api-key": ""}) + return + } + c.JSON(200, gin.H{"upstream-api-key": h.cfg.AmpCode.UpstreamAPIKey}) +} + +// PutAmpUpstreamAPIKey updates the ampcode upstream API key. +func (h *Handler) PutAmpUpstreamAPIKey(c *gin.Context) { + h.updateStringField(c, func(v string) { h.cfg.AmpCode.UpstreamAPIKey = strings.TrimSpace(v) }) +} + +// DeleteAmpUpstreamAPIKey clears the ampcode upstream API key. +func (h *Handler) DeleteAmpUpstreamAPIKey(c *gin.Context) { + h.cfg.AmpCode.UpstreamAPIKey = "" + h.persist(c) +} + +// GetAmpRestrictManagementToLocalhost returns the localhost restriction setting. +func (h *Handler) GetAmpRestrictManagementToLocalhost(c *gin.Context) { + if h == nil || h.cfg == nil { + c.JSON(200, gin.H{"restrict-management-to-localhost": true}) + return + } + c.JSON(200, gin.H{"restrict-management-to-localhost": h.cfg.AmpCode.RestrictManagementToLocalhost}) +} + +// PutAmpRestrictManagementToLocalhost updates the localhost restriction setting. +func (h *Handler) PutAmpRestrictManagementToLocalhost(c *gin.Context) { + h.updateBoolField(c, func(v bool) { h.cfg.AmpCode.RestrictManagementToLocalhost = v }) +} + +// GetAmpModelMappings returns the ampcode model mappings. +func (h *Handler) GetAmpModelMappings(c *gin.Context) { + if h == nil || h.cfg == nil { + c.JSON(200, gin.H{"model-mappings": []config.AmpModelMapping{}}) + return + } + c.JSON(200, gin.H{"model-mappings": h.cfg.AmpCode.ModelMappings}) +} + +// PutAmpModelMappings replaces all ampcode model mappings. +func (h *Handler) PutAmpModelMappings(c *gin.Context) { + var body struct { + Value []config.AmpModelMapping `json:"value"` + } + if err := c.ShouldBindJSON(&body); err != nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + h.cfg.AmpCode.ModelMappings = body.Value + h.persist(c) +} + +// PatchAmpModelMappings adds or updates model mappings. +func (h *Handler) PatchAmpModelMappings(c *gin.Context) { + var body struct { + Value []config.AmpModelMapping `json:"value"` + } + if err := c.ShouldBindJSON(&body); err != nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + + existing := make(map[string]int) + for i, m := range h.cfg.AmpCode.ModelMappings { + existing[strings.TrimSpace(m.From)] = i + } + + for _, newMapping := range body.Value { + from := strings.TrimSpace(newMapping.From) + if idx, ok := existing[from]; ok { + h.cfg.AmpCode.ModelMappings[idx] = newMapping + } else { + h.cfg.AmpCode.ModelMappings = append(h.cfg.AmpCode.ModelMappings, newMapping) + existing[from] = len(h.cfg.AmpCode.ModelMappings) - 1 + } + } + h.persist(c) +} + +// DeleteAmpModelMappings removes specified model mappings by "from" field. +func (h *Handler) DeleteAmpModelMappings(c *gin.Context) { + var body struct { + Value []string `json:"value"` + } + if err := c.ShouldBindJSON(&body); err != nil || len(body.Value) == 0 { + h.cfg.AmpCode.ModelMappings = nil + h.persist(c) + return + } + + toRemove := make(map[string]bool) + for _, from := range body.Value { + toRemove[strings.TrimSpace(from)] = true + } + + newMappings := make([]config.AmpModelMapping, 0, len(h.cfg.AmpCode.ModelMappings)) + for _, m := range h.cfg.AmpCode.ModelMappings { + if !toRemove[strings.TrimSpace(m.From)] { + newMappings = append(newMappings, m) + } + } + h.cfg.AmpCode.ModelMappings = newMappings + h.persist(c) +} + +// GetAmpForceModelMappings returns whether model mappings are forced. +func (h *Handler) GetAmpForceModelMappings(c *gin.Context) { + if h == nil || h.cfg == nil { + c.JSON(200, gin.H{"force-model-mappings": false}) + return + } + c.JSON(200, gin.H{"force-model-mappings": h.cfg.AmpCode.ForceModelMappings}) +} + +// PutAmpForceModelMappings updates the force model mappings setting. +func (h *Handler) PutAmpForceModelMappings(c *gin.Context) { + h.updateBoolField(c, func(v bool) { h.cfg.AmpCode.ForceModelMappings = v }) +} + +// GetAmpUpstreamAPIKeys returns the ampcode upstream API keys mapping. +func (h *Handler) GetAmpUpstreamAPIKeys(c *gin.Context) { + if h == nil || h.cfg == nil { + c.JSON(200, gin.H{"upstream-api-keys": []config.AmpUpstreamAPIKeyEntry{}}) + return + } + c.JSON(200, gin.H{"upstream-api-keys": h.cfg.AmpCode.UpstreamAPIKeys}) +} + +// PutAmpUpstreamAPIKeys replaces all ampcode upstream API keys mappings. +func (h *Handler) PutAmpUpstreamAPIKeys(c *gin.Context) { + var body struct { + Value []config.AmpUpstreamAPIKeyEntry `json:"value"` + } + if err := c.ShouldBindJSON(&body); err != nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + // Normalize entries: trim whitespace, filter empty + normalized := normalizeAmpUpstreamAPIKeyEntries(body.Value) + h.cfg.AmpCode.UpstreamAPIKeys = normalized + h.persist(c) +} + +// PatchAmpUpstreamAPIKeys adds or updates upstream API keys entries. +// Matching is done by upstream-api-key value. +func (h *Handler) PatchAmpUpstreamAPIKeys(c *gin.Context) { + var body struct { + Value []config.AmpUpstreamAPIKeyEntry `json:"value"` + } + if err := c.ShouldBindJSON(&body); err != nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + + existing := make(map[string]int) + for i, entry := range h.cfg.AmpCode.UpstreamAPIKeys { + existing[strings.TrimSpace(entry.UpstreamAPIKey)] = i + } + + for _, newEntry := range body.Value { + upstreamKey := strings.TrimSpace(newEntry.UpstreamAPIKey) + if upstreamKey == "" { + continue + } + normalizedEntry := config.AmpUpstreamAPIKeyEntry{ + UpstreamAPIKey: upstreamKey, + APIKeys: normalizeAPIKeysList(newEntry.APIKeys), + } + if idx, ok := existing[upstreamKey]; ok { + h.cfg.AmpCode.UpstreamAPIKeys[idx] = normalizedEntry + } else { + h.cfg.AmpCode.UpstreamAPIKeys = append(h.cfg.AmpCode.UpstreamAPIKeys, normalizedEntry) + existing[upstreamKey] = len(h.cfg.AmpCode.UpstreamAPIKeys) - 1 + } + } + h.persist(c) +} + +// DeleteAmpUpstreamAPIKeys removes specified upstream API keys entries. +// Body must be JSON: {"value": ["", ...]}. +// If "value" is an empty array, clears all entries. +// If JSON is invalid or "value" is missing/null, returns 400 and does not persist any change. +func (h *Handler) DeleteAmpUpstreamAPIKeys(c *gin.Context) { + var body struct { + Value []string `json:"value"` + } + if err := c.ShouldBindJSON(&body); err != nil { + c.JSON(400, gin.H{"error": "invalid body"}) + return + } + + if body.Value == nil { + c.JSON(400, gin.H{"error": "missing value"}) + return + } + + // Empty array means clear all + if len(body.Value) == 0 { + h.cfg.AmpCode.UpstreamAPIKeys = nil + h.persist(c) + return + } + + toRemove := make(map[string]bool) + for _, key := range body.Value { + trimmed := strings.TrimSpace(key) + if trimmed == "" { + continue + } + toRemove[trimmed] = true + } + if len(toRemove) == 0 { + c.JSON(400, gin.H{"error": "empty value"}) + return + } + + newEntries := make([]config.AmpUpstreamAPIKeyEntry, 0, len(h.cfg.AmpCode.UpstreamAPIKeys)) + for _, entry := range h.cfg.AmpCode.UpstreamAPIKeys { + if !toRemove[strings.TrimSpace(entry.UpstreamAPIKey)] { + newEntries = append(newEntries, entry) + } + } + h.cfg.AmpCode.UpstreamAPIKeys = newEntries + h.persist(c) +} + +// normalizeAmpUpstreamAPIKeyEntries normalizes a list of upstream API key entries. +func normalizeAmpUpstreamAPIKeyEntries(entries []config.AmpUpstreamAPIKeyEntry) []config.AmpUpstreamAPIKeyEntry { + if len(entries) == 0 { + return nil + } + out := make([]config.AmpUpstreamAPIKeyEntry, 0, len(entries)) + for _, entry := range entries { + upstreamKey := strings.TrimSpace(entry.UpstreamAPIKey) + if upstreamKey == "" { + continue + } + apiKeys := normalizeAPIKeysList(entry.APIKeys) + out = append(out, config.AmpUpstreamAPIKeyEntry{ + UpstreamAPIKey: upstreamKey, + APIKeys: apiKeys, + }) + } + if len(out) == 0 { + return nil + } + return out +} + +// normalizeAPIKeysList trims and filters empty strings from a list of API keys. +func normalizeAPIKeysList(keys []string) []string { + if len(keys) == 0 { + return nil + } + out := make([]string, 0, len(keys)) + for _, k := range keys { + trimmed := strings.TrimSpace(k) + if trimmed != "" { + out = append(out, trimmed) + } + } + if len(out) == 0 { + return nil + } + return out +} diff --git a/internal/api/handlers/management/handler.go b/internal/api/handlers/management/handler.go index edafc945..fb4de3b7 100644 --- a/internal/api/handlers/management/handler.go +++ b/internal/api/handlers/management/handler.go @@ -17,7 +17,6 @@ import ( "github.com/router-for-me/CLIProxyAPI/v6/internal/config" "github.com/router-for-me/CLIProxyAPI/v6/internal/integrations" "github.com/router-for-me/CLIProxyAPI/v6/internal/usage" - "github.com/router-for-me/CLIProxyAPI/v6/internal/util" sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth" coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" "golang.org/x/crypto/bcrypt" @@ -52,22 +51,6 @@ type Handler struct { integrationManager *integrations.Manager } -// openAICompatProviderNames returns the configured OpenAI-compat provider names from the current config. -func (h *Handler) openAICompatProviderNames() []string { - cfg := h.cfg - if cfg == nil { - return nil - } - out := make([]string, 0, len(cfg.OpenAICompatibility)) - for _, c := range cfg.OpenAICompatibility { - name := strings.TrimSpace(c.Name) - if name != "" { - out = append(out, name) - } - } - return out -} - // NewHandler creates a new management handler instance. func NewHandler(cfg *config.Config, configFilePath string, manager *coreauth.Manager) *Handler { envSecret, _ := os.LookupEnv("MANAGEMENT_PASSWORD") @@ -83,28 +66,6 @@ func NewHandler(cfg *config.Config, configFilePath string, manager *coreauth.Man allowRemoteOverride: envSecret != "", envSecret: envSecret, } - - // Initialize Integration Manager - // We need the proxy URL. If not in config, assume localhost default. - proxyURL := cfg.ProxyURL - if proxyURL == "" { - host := cfg.Host - if host == "" { - host = "http://127.0.0.1" - } else { - if !strings.HasPrefix(host, "http") { - host = "http://" + host - } - } - proxyURL = fmt.Sprintf("%s:%d", host, cfg.Port) - } - - h.integrationManager = integrations.NewManager(proxyURL) - h.integrationManager.Register(&integrations.CodexIntegration{}) - h.integrationManager.Register(&integrations.ClaudeIntegration{}) - h.integrationManager.Register(&integrations.DroidIntegration{}) - h.integrationManager.Register(&integrations.GeminiIntegration{}) - h.startAttemptCleanup() return h } @@ -150,111 +111,6 @@ func (h *Handler) SetConfig(cfg *config.Config) { h.cfg = cfg } // SetAuthManager updates the auth manager reference used by management endpoints. func (h *Handler) SetAuthManager(manager *coreauth.Manager) { h.authManager = manager } -// routingPreview describes how a model would be normalised and routed according to current config. -type routingPreview struct { - RequestedModel string `json:"requested_model"` - ResolvedModel string `json:"resolved_model"` - NormalizedModel string `json:"normalized_model"` - ProviderCandidates []string `json:"provider_candidates"` - PreferredProvider string `json:"preferred_provider"` - Dynamic map[string]any `json:"dynamic"` - Metadata map[string]any `json:"metadata"` -} - -// computeRoutingPreview replicates BaseAPIHandler.getRequestDetails for management/debug purposes. -func (h *Handler) computeRoutingPreview(modelName string) (*routingPreview, error) { - if strings.TrimSpace(modelName) == "" { - return nil, fmt.Errorf("model name is empty") - } - - requested := strings.TrimSpace(modelName) - - // Droid CLI sometimes sends a "custom:*" model id; normalize it before routing. - modelName = util.NormalizeDroidCustomModel(modelName) - - // Resolve "auto" model to an actual available model first - resolvedModelName := util.ResolveAutoModel(modelName) - - providerName, extractedModelName, isDynamic := parseDynamicModelWithConfig(resolvedModelName, h.openAICompatProviderNames()) - - targetModelName := resolvedModelName - if isDynamic { - targetModelName = extractedModelName - } - - // Normalize the model name to handle dynamic thinking suffixes before determining the provider. - normalizedModel, metadata := util.NormalizeThinkingModel(targetModelName) - - var providers []string - if isDynamic { - providers = []string{providerName} - } else { - // For non-dynamic models, use the normalizedModel to get the provider name. - providers = util.GetProviderName(normalizedModel) - if len(providers) == 0 && metadata != nil { - if originalRaw, ok := metadata[util.ThinkingOriginalModelMetadataKey]; ok { - if originalModel, okStr := originalRaw.(string); okStr { - originalModel = strings.TrimSpace(originalModel) - if originalModel != "" && !strings.EqualFold(originalModel, normalizedModel) { - if altProviders := util.GetProviderName(originalModel); len(altProviders) > 0 { - providers = altProviders - normalizedModel = originalModel - } - } - } - } - } - } - - if len(providers) == 0 { - return nil, fmt.Errorf("unknown provider for model %s", requested) - } - - preferred := "" - if len(providers) > 0 { - preferred = providers[0] - } - - preview := &routingPreview{ - RequestedModel: requested, - ResolvedModel: resolvedModelName, - NormalizedModel: normalizedModel, - ProviderCandidates: providers, - PreferredProvider: preferred, - Dynamic: map[string]any{ - "is_dynamic": isDynamic, - "dynamic_provider": providerName, - "dynamic_model": extractedModelName, - }, - Metadata: metadata, - } - return preview, nil -} - -// parseDynamicModelWithConfig mirrors BaseAPIHandler.parseDynamicModel but uses the config provider list. -func parseDynamicModelWithConfig(modelName string, openAICompatProviders []string) (providerName, model string, isDynamic bool) { - var providerPart, modelPart string - for _, sep := range []string{"://"} { - if parts := strings.SplitN(modelName, sep, 2); len(parts) == 2 { - providerPart = parts[0] - modelPart = parts[1] - break - } - } - - if providerPart == "" { - return "", modelName, false - } - - for _, pName := range openAICompatProviders { - if pName == providerPart { - return providerPart, modelPart, true - } - } - - return "", modelName, false -} - // SetUsageStatistics allows replacing the usage statistics reference. func (h *Handler) SetUsageStatistics(stats *usage.RequestStatistics) { h.usageStats = stats } @@ -376,15 +232,6 @@ func (h *Handler) Middleware() gin.HandlerFunc { return } } - // For localhost, also accept configured API keys (same keys that work for main API) - if cfg != nil { - for _, apiKey := range cfg.APIKeys { - if apiKey != "" && subtle.ConstantTimeCompare([]byte(provided), []byte(apiKey)) == 1 { - c.Next() - return - } - } - } } if envSecret != "" && subtle.ConstantTimeCompare([]byte(provided), []byte(envSecret)) == 1 { diff --git a/internal/api/handlers/management/model_definitions.go b/internal/api/handlers/management/model_definitions.go new file mode 100644 index 00000000..85ff314b --- /dev/null +++ b/internal/api/handlers/management/model_definitions.go @@ -0,0 +1,33 @@ +package management + +import ( + "net/http" + "strings" + + "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v6/internal/registry" +) + +// GetStaticModelDefinitions returns static model metadata for a given channel. +// Channel is provided via path param (:channel) or query param (?channel=...). +func (h *Handler) GetStaticModelDefinitions(c *gin.Context) { + channel := strings.TrimSpace(c.Param("channel")) + if channel == "" { + channel = strings.TrimSpace(c.Query("channel")) + } + if channel == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "channel is required"}) + return + } + + models := registry.GetStaticModelDefinitionsByChannel(channel) + if models == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "unknown channel", "channel": channel}) + return + } + + c.JSON(http.StatusOK, gin.H{ + "channel": strings.ToLower(strings.TrimSpace(channel)), + "models": models, + }) +} diff --git a/internal/api/handlers/management/oauth_sessions.go b/internal/api/handlers/management/oauth_sessions.go index 56388437..05ff8d1f 100644 --- a/internal/api/handlers/management/oauth_sessions.go +++ b/internal/api/handlers/management/oauth_sessions.go @@ -158,12 +158,7 @@ func (s *oauthSessionStore) IsPending(state, provider string) bool { return false } if session.Status != "" { - if !strings.EqualFold(session.Provider, "kiro") { - return false - } - if !strings.HasPrefix(session.Status, "device_code|") && !strings.HasPrefix(session.Status, "auth_url|") { - return false - } + return false } if provider == "" { return true @@ -230,12 +225,12 @@ func NormalizeOAuthProvider(provider string) (string, error) { return "codex", nil case "gemini", "google": return "gemini", nil + case "iflow", "i-flow": + return "iflow", nil case "antigravity", "anti-gravity": return "antigravity", nil case "qwen": return "qwen", nil - case "kiro": - return "kiro", nil default: return "", errUnsupportedOAuthFlow } diff --git a/internal/api/handlers/management/usage.go b/internal/api/handlers/management/usage.go index c4d8a12e..5f794089 100644 --- a/internal/api/handlers/management/usage.go +++ b/internal/api/handlers/management/usage.go @@ -26,12 +26,8 @@ func (h *Handler) GetUsageStatistics(c *gin.Context) { if h != nil && h.usageStats != nil { snapshot = h.usageStats.Snapshot() } - - usageStats := usage.ComputeUsageStats(snapshot) - c.JSON(http.StatusOK, gin.H{ - "usage": usageStats, - "raw_snapshot": snapshot, + "usage": snapshot, "failed_requests": snapshot.FailureCount, }) } diff --git a/internal/api/middleware/metrics.go b/internal/api/middleware/metrics.go index eb6c2577..06f2116b 100644 --- a/internal/api/middleware/metrics.go +++ b/internal/api/middleware/metrics.go @@ -337,6 +337,12 @@ func GetActiveConnections() int64 { return atomic.LoadInt64(&activeConnectionsCount) } +// GetRequestMonitor returns a stub list of recent requests. +// TODO: Implement actual request monitoring if needed. +func GetRequestMonitor() []any { + return []any{} +} + // RecordProviderRequest records a request to a specific AI provider. // This can be called from handlers to track provider-specific metrics. func RecordProviderRequest(provider, model string) { diff --git a/internal/api/middleware/request_logging.go b/internal/api/middleware/request_logging.go index 73654712..49f28f52 100644 --- a/internal/api/middleware/request_logging.go +++ b/internal/api/middleware/request_logging.go @@ -8,41 +8,12 @@ import ( "io" "net/http" "strings" - "sync" - "time" "github.com/gin-gonic/gin" - "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" "github.com/router-for-me/CLIProxyAPI/v6/internal/logging" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" ) -var ( - requestMonitorMu sync.RWMutex - requestMonitor []interfaces.RequestLogEntry - maxMonitorSize = 100 -) - -func addRequestToMonitor(entry interfaces.RequestLogEntry) { - requestMonitorMu.Lock() - defer requestMonitorMu.Unlock() - - requestMonitor = append(requestMonitor, entry) - if len(requestMonitor) > maxMonitorSize { - requestMonitor = requestMonitor[1:] - } -} - -// GetRequestMonitor returns a copy of the current request monitor entries. -func GetRequestMonitor() []interfaces.RequestLogEntry { - requestMonitorMu.RLock() - defer requestMonitorMu.RUnlock() - - result := make([]interfaces.RequestLogEntry, len(requestMonitor)) - copy(result, requestMonitor) - return result -} - // RequestLoggingMiddleware creates a Gin middleware that logs HTTP requests and responses. // It captures detailed information about the request and response, including headers and body, // and uses the provided RequestLogger to record this data. When logging is disabled in the @@ -82,74 +53,13 @@ func RequestLoggingMiddleware(logger logging.RequestLogger) gin.HandlerFunc { c.Writer = wrapper // Process the request - startTime := time.Now() c.Next() - latency := time.Since(startTime) // Finalize logging after request processing if err = wrapper.Finalize(c); err != nil { // Log error but don't interrupt the response // In a real implementation, you might want to use a proper logger here } - - // Add to monitor - entry := interfaces.RequestLogEntry{ - ID: logging.GetGinRequestID(c), - Timestamp: startTime, - Method: c.Request.Method, - Path: c.Request.URL.Path, - Status: c.Writer.Status(), - LatencyMs: latency.Milliseconds(), - } - - // Try to get Model and Provider from context - if model, ok := c.Get("model"); ok { - if m, ok := model.(string); ok { - entry.Model = m - } - } - if provider, ok := c.Get("provider"); ok { - if p, ok := provider.(string); ok { - entry.Provider = p - } - } - - // Try to get tokens from context - if inputTokens, ok := c.Get("input_tokens"); ok { - if it, ok := inputTokens.(int); ok { - entry.InputTokens = it - } else if it, ok := inputTokens.(int64); ok { - entry.InputTokens = int(it) - } - } - if outputTokens, ok := c.Get("output_tokens"); ok { - if ot, ok := outputTokens.(int); ok { - entry.OutputTokens = ot - } else if ot, ok := outputTokens.(int64); ok { - entry.OutputTokens = int(ot) - } - } - - // Try to get error from context - if apiErr, ok := c.Get("API_RESPONSE_ERROR"); ok { - if errs, ok := apiErr.([]*interfaces.ErrorMessage); ok && len(errs) > 0 { - var errMsgs []string - for _, e := range errs { - if e != nil && e.Error != nil { - errMsgs = append(errMsgs, e.Error.Error()) - } - } - entry.Error = strings.Join(errMsgs, "; ") - } - } - - if ShouldSampleRequestHistory() { - addRequestToMonitor(entry) - // Also add to persistent history when enabled - if history := GetRequestHistory(); history != nil { - history.AddEntry(entry) - } - } } } diff --git a/internal/api/middleware/response_writer.go b/internal/api/middleware/response_writer.go index 92fe7417..8029e50a 100644 --- a/internal/api/middleware/response_writer.go +++ b/internal/api/middleware/response_writer.go @@ -7,7 +7,6 @@ import ( "bytes" "net/http" "strings" - "sync" "github.com/gin-gonic/gin" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" @@ -39,12 +38,6 @@ type ResponseWriterWrapper struct { logOnErrorOnly bool // logOnErrorOnly enables logging only when an error response is detected. } -var responseBodyBufferPool = sync.Pool{ - New: func() any { - return new(bytes.Buffer) - }, -} - // NewResponseWriterWrapper creates and initializes a new ResponseWriterWrapper. // It takes the original gin.ResponseWriter, a logger instance, and request information. // @@ -56,11 +49,9 @@ var responseBodyBufferPool = sync.Pool{ // Returns: // - A pointer to a new ResponseWriterWrapper. func NewResponseWriterWrapper(w gin.ResponseWriter, logger logging.RequestLogger, requestInfo *RequestInfo) *ResponseWriterWrapper { - buf := responseBodyBufferPool.Get().(*bytes.Buffer) - buf.Reset() return &ResponseWriterWrapper{ ResponseWriter: w, - body: buf, + body: &bytes.Buffer{}, logger: logger, requestInfo: requestInfo, headers: make(map[string][]string), @@ -303,22 +294,10 @@ func (w *ResponseWriterWrapper) Finalize(c *gin.Context) error { return err } w.streamWriter = nil - w.releaseBodyBuffer() return nil } - err := w.logRequest(finalStatusCode, w.cloneHeaders(), w.body.Bytes(), w.extractAPIRequest(c), w.extractAPIResponse(c), slicesAPIResponseError, forceLog) - w.releaseBodyBuffer() - return err -} - -func (w *ResponseWriterWrapper) releaseBodyBuffer() { - if w.body == nil { - return - } - w.body.Reset() - responseBodyBufferPool.Put(w.body) - w.body = nil + return w.logRequest(finalStatusCode, w.cloneHeaders(), w.body.Bytes(), w.extractAPIRequest(c), w.extractAPIResponse(c), slicesAPIResponseError, forceLog) } func (w *ResponseWriterWrapper) cloneHeaders() map[string][]string { diff --git a/internal/api/server.go b/internal/api/server.go index 9e4877c9..e0c92b3e 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -7,37 +7,36 @@ package api import ( "context" "crypto/subtle" - "encoding/json" "errors" "fmt" "net/http" "os" "path/filepath" + "reflect" "strings" "sync" "sync/atomic" "time" "github.com/gin-gonic/gin" - internalHandlers "github.com/router-for-me/CLIProxyAPI/v6/internal/api/handlers" + "github.com/router-for-me/CLIProxyAPI/v6/internal/access" managementHandlers "github.com/router-for-me/CLIProxyAPI/v6/internal/api/handlers/management" "github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware" - "github.com/router-for-me/CLIProxyAPI/v6/internal/cache" + "github.com/router-for-me/CLIProxyAPI/v6/internal/api/modules" + ampmodule "github.com/router-for-me/CLIProxyAPI/v6/internal/api/modules/amp" "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/memory" + "github.com/router-for-me/CLIProxyAPI/v6/internal/managementasset" "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" "github.com/router-for-me/CLIProxyAPI/v6/internal/usage" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/access" sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access" "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers" "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/claude" "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/gemini" "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/openai" + sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth" "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" - cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" - sdkConfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" ) @@ -127,8 +126,6 @@ type Server struct { // cfg holds the current server configuration. cfg *config.Config - // cfgHolder provides race-safe config snapshots for middleware reads. - cfgHolder atomic.Value // oldConfigYaml stores a YAML snapshot of the previous configuration for change detection. // This prevents issues when the config object is modified in place by Management API. @@ -156,6 +153,9 @@ type Server struct { // management handler mgmt *managementHandlers.Handler + // ampModule is the Amp routing module for model mapping hot-reload + ampModule *ampmodule.AmpModule + // managementRoutesRegistered tracks whether the management routes have been attached to the engine. managementRoutesRegistered atomic.Bool // managementRoutesEnabled controls whether management endpoints serve real handlers. @@ -171,9 +171,6 @@ type Server struct { keepAliveOnTimeout func() keepAliveHeartbeat chan struct{} keepAliveStop chan struct{} - - // agentsV2 is the new unified agent configuration handler - agentsV2 *managementHandlers.AgentsV2Handler } // NewServer creates and initializes a new API server instance. @@ -204,26 +201,9 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk optionState.engineConfigurator(engine) } - metricsEnabled := cfg.IsMetricsEnabled() - requestHistoryEnabled := cfg.IsRequestHistoryEnabled() - agenticHarnessEnabled := cfg.IsAgenticHarnessEnabled() - promptBudgetEnabled := cfg.IsPromptBudgetEnabled() - if cfg.CommercialMode { - metricsEnabled = false - requestHistoryEnabled = false - agenticHarnessEnabled = false - promptBudgetEnabled = false - } - middleware.SetMetricsEnabled(metricsEnabled) - middleware.SetRequestHistoryEnabled(requestHistoryEnabled) - middleware.SetRequestHistorySampleRate(cfg.GetRequestHistorySampleRate()) - usage.SetSamplingRate(cfg.GetUsageSampleRate()) - // Add middleware engine.Use(logging.GinLogrusLogger()) engine.Use(logging.GinLogrusRecovery()) - engine.Use(middleware.ConnectionTrackerMiddleware()) - engine.Use(middleware.PrometheusMiddleware()) for _, mw := range optionState.extraMiddleware { engine.Use(mw) } @@ -244,28 +224,12 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk } } - // Add agentic harness middleware for long-running agents (Anthropic-style) - // Gated by CLIPROXY_HARNESS_ENABLED env var (default: true) - if agenticHarnessEnabled { - engine.Use(middleware.AgenticHarnessMiddleware()) - } - + engine.Use(corsMiddleware()) wd, err := os.Getwd() if err != nil { wd = configFilePath } - // Add context compression middleware for Claude Code / Codex CLI - // Trims long conversations and uses LLM summarization (Factory.ai pattern) - if promptBudgetEnabled { - engine.Use(middleware.CodexPromptBudgetMiddlewareWithRootDir(wd)) - } - - wd, err = os.Getwd() - if err != nil { - wd = configFilePath - } - envAdminPassword, envAdminPasswordSet := os.LookupEnv("MANAGEMENT_PASSWORD") envAdminPassword = strings.TrimSpace(envAdminPassword) envManagementSecret := envAdminPasswordSet && envAdminPassword != "" @@ -283,26 +247,16 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk envManagementSecret: envManagementSecret, wsRoutes: make(map[string]struct{}), } - s.cfgHolder.Store(cfg) s.wsAuthEnabled.Store(cfg.WebsocketAuth) // Save initial YAML snapshot s.oldConfigYaml, _ = yaml.Marshal(cfg) s.applyAccessConfig(nil, cfg) - cache.InitDefaultResponseCache(buildResponseCacheConfig(cfg)) - cache.InitDefaultPromptCache(buildPromptCacheConfig(cfg)) if authManager != nil { authManager.SetRetryConfig(cfg.RequestRetry, time.Duration(cfg.MaxRetryInterval)*time.Second) } + managementasset.SetCurrentConfig(cfg) auth.SetQuotaCooldownDisabled(cfg.DisableCooling) misc.SetCodexInstructionsEnabled(cfg.CodexInstructionsEnabled) - - // Initialize LLM summarizer with auth manager for Factory.ai-style context compression - if authManager != nil { - // Get providers for the summary model (antigravity preferred for gemini-3-flash) - summaryProviders := util.GetProviderName("gemini-3-flash") - middleware.InitSummarizerWithAuthManager(&authManagerAdapter{authManager}, summaryProviders) - } - // Initialize management handler s.mgmt = managementHandlers.NewHandler(cfg, configFilePath, authManager) if optionState.localPassword != "" { @@ -312,30 +266,28 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk s.mgmt.SetLogDirectory(logDir) s.localPassword = optionState.localPassword - // Initialize agents v2 handler - agentsPort := cfg.Port - if agentsPort == 0 { - agentsPort = 8317 - } - if agentsV2Handler, err := managementHandlers.NewAgentsV2Handler(agentsPort); err == nil { - s.agentsV2 = agentsV2Handler - } - // Setup routes - engine.Use(corsMiddleware(s.getConfig)) s.setupRoutes() - // Register ProxyPilot dashboard routes (embedded UI) - s.registerProxyPilotDashboardRoutes() + // Register Amp module using V2 interface with Context + s.ampModule = ampmodule.NewLegacy(accessManager, AuthMiddleware(accessManager)) + ctx := modules.Context{ + Engine: engine, + BaseHandler: s.handlers, + Config: cfg, + AuthMiddleware: AuthMiddleware(accessManager), + } + if err := modules.RegisterModule(ctx, s.ampModule); err != nil { + log.Errorf("Failed to register Amp module: %v", err) + } // Apply additional router configurators from options if optionState.routerConfigurator != nil { optionState.routerConfigurator(engine, s.handlers, cfg) } - // Register management routes when configuration or environment secrets are available, - // or when a local password is configured for localhost management access. - hasManagementSecret := cfg.RemoteManagement.SecretKey != "" || envManagementSecret || optionState.localPassword != "" + // Register management routes when configuration or environment secrets are available. + hasManagementSecret := cfg.RemoteManagement.SecretKey != "" || envManagementSecret s.managementRoutesEnabled.Store(hasManagementSecret) if hasManagementSecret { s.registerManagementRoutes() @@ -357,16 +309,16 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk // setupRoutes configures the API routes for the server. // It defines the endpoints and associates them with their respective handlers. func (s *Server) setupRoutes() { + s.engine.GET("/management.html", s.serveManagementControlPanel) openaiHandlers := openai.NewOpenAIAPIHandler(s.handlers) geminiHandlers := gemini.NewGeminiAPIHandler(s.handlers) geminiCLIHandlers := gemini.NewGeminiCLIAPIHandler(s.handlers) claudeCodeHandlers := claude.NewClaudeCodeAPIHandler(s.handlers) openaiResponsesHandlers := openai.NewOpenAIResponsesAPIHandler(s.handlers) - authMiddleware := AuthMiddleware(s.accessManager, s.allowUnauthenticated) // OpenAI compatible API routes v1 := s.engine.Group("/v1") - v1.Use(authMiddleware) + v1.Use(AuthMiddleware(s.accessManager)) { v1.GET("/models", s.unifiedModelsHandler(openaiHandlers, claudeCodeHandlers)) v1.POST("/chat/completions", openaiHandlers.ChatCompletions) @@ -374,45 +326,17 @@ func (s *Server) setupRoutes() { v1.POST("/messages", claudeCodeHandlers.ClaudeMessages) v1.POST("/messages/count_tokens", claudeCodeHandlers.ClaudeCountTokens) v1.POST("/responses", openaiResponsesHandlers.Responses) - - // Translation API routes - translatorHandler := internalHandlers.NewTranslatorHandler() - v1.GET("/translations", translatorHandler.GetTranslationsMatrix) - v1.GET("/translations/check", translatorHandler.CheckTranslation) - v1.GET("/translations/docs", translatorHandler.GetTranslationDocs) - v1.POST("/translations/score", translatorHandler.ScoreTranslation) - v1.POST("/translations/compare", translatorHandler.CompareStructures) } // Gemini compatible API routes v1beta := s.engine.Group("/v1beta") - v1beta.Use(authMiddleware) + v1beta.Use(AuthMiddleware(s.accessManager)) { v1beta.GET("/models", geminiHandlers.GeminiModels) v1beta.POST("/models/*action", geminiHandlers.GeminiHandler) v1beta.GET("/models/*action", geminiHandlers.GeminiGetHandler) } - // Health check endpoint for Droid CLI and other clients - s.engine.GET("/healthz", func(c *gin.Context) { - cfg := s.getConfig() - port := 0 - if cfg != nil { - port = cfg.Port - } - c.JSON(http.StatusOK, gin.H{"status": "ok", "port": port}) - }) - - // Prometheus metrics endpoint for observability - s.engine.GET("/metrics", middleware.MetricsHandler()) - - // Root-level API routes (mirrors /v1/* for clients that dont add /v1 prefix) - s.engine.GET("/models", authMiddleware, s.unifiedModelsHandler(openaiHandlers, claudeCodeHandlers)) - s.engine.POST("/chat/completions", authMiddleware, openaiHandlers.ChatCompletions) - s.engine.POST("/completions", authMiddleware, openaiHandlers.Completions) - s.engine.POST("/messages", authMiddleware, claudeCodeHandlers.ClaudeMessages) - s.engine.POST("/responses", authMiddleware, openaiResponsesHandlers.Responses) - // Root endpoint s.engine.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ @@ -424,12 +348,6 @@ func (s *Server) setupRoutes() { }, }) }) - - // Event logging endpoint - handles Claude Code telemetry requests - // Returns 200 OK to prevent 404 errors in logs - s.engine.POST("/api/event_logging/batch", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"status": "ok"}) - }) s.engine.POST("/v1internal:method", geminiCLIHandlers.CLIHandler) // OAuth callback endpoints (reuse main server port) @@ -443,9 +361,7 @@ func (s *Server) setupRoutes() { errStr = c.Query("error_description") } if state != "" { - if cfg := s.getConfig(); cfg != nil { - _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(cfg.AuthDir, "anthropic", state, code, errStr) - } + _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(s.cfg.AuthDir, "anthropic", state, code, errStr) } c.Header("Content-Type", "text/html; charset=utf-8") c.String(http.StatusOK, oauthCallbackSuccessHTML) @@ -459,9 +375,7 @@ func (s *Server) setupRoutes() { errStr = c.Query("error_description") } if state != "" { - if cfg := s.getConfig(); cfg != nil { - _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(cfg.AuthDir, "codex", state, code, errStr) - } + _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(s.cfg.AuthDir, "codex", state, code, errStr) } c.Header("Content-Type", "text/html; charset=utf-8") c.String(http.StatusOK, oauthCallbackSuccessHTML) @@ -475,9 +389,21 @@ func (s *Server) setupRoutes() { errStr = c.Query("error_description") } if state != "" { - if cfg := s.getConfig(); cfg != nil { - _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(cfg.AuthDir, "gemini", state, code, errStr) - } + _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(s.cfg.AuthDir, "gemini", state, code, errStr) + } + c.Header("Content-Type", "text/html; charset=utf-8") + c.String(http.StatusOK, oauthCallbackSuccessHTML) + }) + + s.engine.GET("/iflow/callback", func(c *gin.Context) { + code := c.Query("code") + state := c.Query("state") + errStr := c.Query("error") + if errStr == "" { + errStr = c.Query("error_description") + } + if state != "" { + _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(s.cfg.AuthDir, "iflow", state, code, errStr) } c.Header("Content-Type", "text/html; charset=utf-8") c.String(http.StatusOK, oauthCallbackSuccessHTML) @@ -491,25 +417,7 @@ func (s *Server) setupRoutes() { errStr = c.Query("error_description") } if state != "" { - if cfg := s.getConfig(); cfg != nil { - _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(cfg.AuthDir, "antigravity", state, code, errStr) - } - } - c.Header("Content-Type", "text/html; charset=utf-8") - c.String(http.StatusOK, oauthCallbackSuccessHTML) - }) - - s.engine.GET("/kiro/callback", func(c *gin.Context) { - code := c.Query("code") - state := c.Query("state") - errStr := c.Query("error") - if errStr == "" { - errStr = c.Query("error_description") - } - if state != "" { - if cfg := s.getConfig(); cfg != nil { - _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(cfg.AuthDir, "kiro", state, code, errStr) - } + _, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(s.cfg.AuthDir, "antigravity", state, code, errStr) } c.Header("Content-Type", "text/html; charset=utf-8") c.String(http.StatusOK, oauthCallbackSuccessHTML) @@ -539,7 +447,7 @@ func (s *Server) AttachWebsocketRoute(path string, handler http.Handler) { s.wsRoutes[trimmed] = struct{}{} s.wsRouteMu.Unlock() - authMiddleware := AuthMiddleware(s.accessManager, s.allowUnauthenticated) + authMiddleware := AuthMiddleware(s.accessManager) conditionalAuth := func(c *gin.Context) { if !s.wsAuthEnabled.Load() { c.Next() @@ -571,19 +479,6 @@ func (s *Server) registerManagementRoutes() { mgmt.GET("/usage", s.mgmt.GetUsageStatistics) mgmt.GET("/usage/export", s.mgmt.ExportUsageStatistics) mgmt.POST("/usage/import", s.mgmt.ImportUsageStatistics) - - // Rate limit monitoring - mgmt.GET("/rate-limits", s.mgmt.GetRateLimits) - mgmt.GET("/rate-limits/summary", s.mgmt.GetRateLimitsSummary) - - // Request monitoring and history routes - mgmt.GET("/requests", s.mgmt.GetRequests) - mgmt.GET("/request-history", s.mgmt.GetRequestHistory) - mgmt.GET("/request-history/stats", s.mgmt.GetRequestHistoryStats) - mgmt.DELETE("/request-history", s.mgmt.ClearRequestHistory) - mgmt.GET("/request-history/export", s.mgmt.ExportRequestHistory) - mgmt.POST("/request-history/import", s.mgmt.ImportRequestHistory) - mgmt.POST("/request-history/save", s.mgmt.SaveRequestHistory) mgmt.GET("/config", s.mgmt.GetConfig) mgmt.GET("/config.yaml", s.mgmt.GetConfigYAML) mgmt.PUT("/config.yaml", s.mgmt.PutConfigYAML) @@ -597,6 +492,10 @@ func (s *Server) registerManagementRoutes() { mgmt.PUT("/logging-to-file", s.mgmt.PutLoggingToFile) mgmt.PATCH("/logging-to-file", s.mgmt.PutLoggingToFile) + mgmt.GET("/logs-max-total-size-mb", s.mgmt.GetLogsMaxTotalSizeMB) + mgmt.PUT("/logs-max-total-size-mb", s.mgmt.PutLogsMaxTotalSizeMB) + mgmt.PATCH("/logs-max-total-size-mb", s.mgmt.PutLogsMaxTotalSizeMB) + mgmt.GET("/usage-statistics-enabled", s.mgmt.GetUsageStatisticsEnabled) mgmt.PUT("/usage-statistics-enabled", s.mgmt.PutUsageStatisticsEnabled) mgmt.PATCH("/usage-statistics-enabled", s.mgmt.PutUsageStatisticsEnabled) @@ -638,6 +537,30 @@ func (s *Server) registerManagementRoutes() { mgmt.PUT("/ws-auth", s.mgmt.PutWebsocketAuth) mgmt.PATCH("/ws-auth", s.mgmt.PutWebsocketAuth) + mgmt.GET("/ampcode", s.mgmt.GetAmpCode) + mgmt.GET("/ampcode/upstream-url", s.mgmt.GetAmpUpstreamURL) + mgmt.PUT("/ampcode/upstream-url", s.mgmt.PutAmpUpstreamURL) + mgmt.PATCH("/ampcode/upstream-url", s.mgmt.PutAmpUpstreamURL) + mgmt.DELETE("/ampcode/upstream-url", s.mgmt.DeleteAmpUpstreamURL) + mgmt.GET("/ampcode/upstream-api-key", s.mgmt.GetAmpUpstreamAPIKey) + mgmt.PUT("/ampcode/upstream-api-key", s.mgmt.PutAmpUpstreamAPIKey) + mgmt.PATCH("/ampcode/upstream-api-key", s.mgmt.PutAmpUpstreamAPIKey) + mgmt.DELETE("/ampcode/upstream-api-key", s.mgmt.DeleteAmpUpstreamAPIKey) + mgmt.GET("/ampcode/restrict-management-to-localhost", s.mgmt.GetAmpRestrictManagementToLocalhost) + mgmt.PUT("/ampcode/restrict-management-to-localhost", s.mgmt.PutAmpRestrictManagementToLocalhost) + mgmt.PATCH("/ampcode/restrict-management-to-localhost", s.mgmt.PutAmpRestrictManagementToLocalhost) + mgmt.GET("/ampcode/model-mappings", s.mgmt.GetAmpModelMappings) + mgmt.PUT("/ampcode/model-mappings", s.mgmt.PutAmpModelMappings) + mgmt.PATCH("/ampcode/model-mappings", s.mgmt.PatchAmpModelMappings) + mgmt.DELETE("/ampcode/model-mappings", s.mgmt.DeleteAmpModelMappings) + mgmt.GET("/ampcode/force-model-mappings", s.mgmt.GetAmpForceModelMappings) + mgmt.PUT("/ampcode/force-model-mappings", s.mgmt.PutAmpForceModelMappings) + mgmt.PATCH("/ampcode/force-model-mappings", s.mgmt.PutAmpForceModelMappings) + mgmt.GET("/ampcode/upstream-api-keys", s.mgmt.GetAmpUpstreamAPIKeys) + mgmt.PUT("/ampcode/upstream-api-keys", s.mgmt.PutAmpUpstreamAPIKeys) + mgmt.PATCH("/ampcode/upstream-api-keys", s.mgmt.PatchAmpUpstreamAPIKeys) + mgmt.DELETE("/ampcode/upstream-api-keys", s.mgmt.DeleteAmpUpstreamAPIKeys) + mgmt.GET("/request-retry", s.mgmt.GetRequestRetry) mgmt.PUT("/request-retry", s.mgmt.PutRequestRetry) mgmt.PATCH("/request-retry", s.mgmt.PutRequestRetry) @@ -645,6 +568,14 @@ func (s *Server) registerManagementRoutes() { mgmt.PUT("/max-retry-interval", s.mgmt.PutMaxRetryInterval) mgmt.PATCH("/max-retry-interval", s.mgmt.PutMaxRetryInterval) + mgmt.GET("/force-model-prefix", s.mgmt.GetForceModelPrefix) + mgmt.PUT("/force-model-prefix", s.mgmt.PutForceModelPrefix) + mgmt.PATCH("/force-model-prefix", s.mgmt.PutForceModelPrefix) + + mgmt.GET("/routing/strategy", s.mgmt.GetRoutingStrategy) + mgmt.PUT("/routing/strategy", s.mgmt.PutRoutingStrategy) + mgmt.PATCH("/routing/strategy", s.mgmt.PutRoutingStrategy) + mgmt.GET("/claude-api-key", s.mgmt.GetClaudeKeys) mgmt.PUT("/claude-api-key", s.mgmt.PutClaudeKeys) mgmt.PATCH("/claude-api-key", s.mgmt.PatchClaudeKey) @@ -660,19 +591,28 @@ func (s *Server) registerManagementRoutes() { mgmt.PATCH("/openai-compatibility", s.mgmt.PatchOpenAICompat) mgmt.DELETE("/openai-compatibility", s.mgmt.DeleteOpenAICompat) + mgmt.GET("/vertex-api-key", s.mgmt.GetVertexCompatKeys) + mgmt.PUT("/vertex-api-key", s.mgmt.PutVertexCompatKeys) + mgmt.PATCH("/vertex-api-key", s.mgmt.PatchVertexCompatKey) + mgmt.DELETE("/vertex-api-key", s.mgmt.DeleteVertexCompatKey) + mgmt.GET("/oauth-excluded-models", s.mgmt.GetOAuthExcludedModels) mgmt.PUT("/oauth-excluded-models", s.mgmt.PutOAuthExcludedModels) mgmt.PATCH("/oauth-excluded-models", s.mgmt.PatchOAuthExcludedModels) mgmt.DELETE("/oauth-excluded-models", s.mgmt.DeleteOAuthExcludedModels) - mgmt.GET("/auth-files", s.mgmt.ListAuthFiles) - mgmt.GET("/auth-files/models", s.mgmt.GetAuthFileModels) - mgmt.GET("/auth-files/download", s.mgmt.DownloadAuthFile) - mgmt.POST("/auth-files", s.mgmt.UploadAuthFile) - mgmt.DELETE("/auth-files", s.mgmt.DeleteAuthFile) - mgmt.POST("/auth/reset-cooldown", s.mgmt.ResetAuthCooldown) - mgmt.PATCH("/auth/:id/priority", s.mgmt.SetAuthPriority) - mgmt.GET("/auth/:id/usage", s.mgmt.GetAuthUsage) + mgmt.GET("/oauth-model-alias", s.mgmt.GetOAuthModelAlias) + mgmt.PUT("/oauth-model-alias", s.mgmt.PutOAuthModelAlias) + mgmt.PATCH("/oauth-model-alias", s.mgmt.PatchOAuthModelAlias) + mgmt.DELETE("/oauth-model-alias", s.mgmt.DeleteOAuthModelAlias) + + mgmt.GET("/auth-files", s.mgmt.ListAuthFiles) + mgmt.GET("/auth-files/models", s.mgmt.GetAuthFileModels) + mgmt.GET("/model-definitions/:channel", s.mgmt.GetStaticModelDefinitions) + mgmt.GET("/auth-files/download", s.mgmt.DownloadAuthFile) + mgmt.POST("/auth-files", s.mgmt.UploadAuthFile) + mgmt.DELETE("/auth-files", s.mgmt.DeleteAuthFile) + mgmt.PATCH("/auth-files/status", s.mgmt.PatchAuthFileStatus) mgmt.POST("/vertex/import", s.mgmt.ImportVertexCredential) mgmt.GET("/anthropic-auth-url", s.mgmt.RequestAnthropicToken) @@ -680,89 +620,10 @@ func (s *Server) registerManagementRoutes() { mgmt.GET("/gemini-cli-auth-url", s.mgmt.RequestGeminiCLIToken) mgmt.GET("/antigravity-auth-url", s.mgmt.RequestAntigravityToken) mgmt.GET("/qwen-auth-url", s.mgmt.RequestQwenToken) - mgmt.GET("/kiro-auth-url", s.mgmt.RequestKiroToken) - mgmt.POST("/amazonq-import", s.mgmt.ImportAmazonQToken) - mgmt.POST("/minimax-api-key", s.mgmt.SaveMiniMaxAPIKey) - mgmt.POST("/zhipu-api-key", s.mgmt.SaveZhipuAPIKey) + mgmt.GET("/iflow-auth-url", s.mgmt.RequestIFlowToken) + mgmt.POST("/iflow-auth-url", s.mgmt.RequestIFlowCookieToken) mgmt.POST("/oauth-callback", s.mgmt.PostOAuthCallback) mgmt.GET("/get-auth-status", s.mgmt.GetAuthStatus) - - // Harness management routes - mgmt.GET("/harness/files", s.mgmt.GetHarnessFiles) - mgmt.GET("/harness/file", s.mgmt.GetHarnessFile) - mgmt.PUT("/harness/file", s.mgmt.PutHarnessFile) - mgmt.GET("/harness/export", s.mgmt.ExportHarness) - mgmt.POST("/harness/import", s.mgmt.ImportHarness) - - // Global model mappings routes - mgmt.GET("/model-mappings", s.mgmt.GetModelMappings) - mgmt.PUT("/model-mappings", s.mgmt.SetModelMappings) - mgmt.GET("/model-mappings/test", s.mgmt.TestModelMapping) - - // Thinking budget routes - mgmt.GET("/thinking-budget", s.mgmt.GetThinkingBudget) - mgmt.PUT("/thinking-budget", s.mgmt.SetThinkingBudget) - - // Integration detection and setup routes - mgmt.GET("/integrations/status", s.mgmt.GetIntegrationsStatus) - mgmt.POST("/integrations/:id/configure", s.mgmt.PostIntegrationConfigure) - mgmt.GET("/agents", s.mgmt.GetCLIAgents) - mgmt.POST("/agents/:id/configure", s.mgmt.PostCLIAgentConfigure) - mgmt.POST("/agents/:id/unconfigure", s.mgmt.PostCLIAgentUnconfigure) - - // New unified agent configuration routes (v2) - if s.agentsV2 != nil { - mgmt.GET("/v2/agents", s.agentsV2.GetAgents) - mgmt.GET("/v2/agents/state", s.agentsV2.GetAgentState) - mgmt.POST("/v2/agents/enable-all", s.agentsV2.EnableAllAgents) - mgmt.POST("/v2/agents/disable-all", s.agentsV2.DisableAllAgents) - mgmt.GET("/v2/agents/:id", s.agentsV2.GetAgent) - mgmt.POST("/v2/agents/:id/enable", s.agentsV2.EnableAgent) - mgmt.POST("/v2/agents/:id/disable", s.agentsV2.DisableAgent) - } - - // Memory management routes - mgmt.GET("/memory/sessions", s.mgmt.ListMemorySessions) - mgmt.GET("/memory/session", s.mgmt.GetMemorySession) - mgmt.GET("/memory/events", s.mgmt.GetMemoryEvents) - mgmt.GET("/memory/anchors", s.mgmt.GetMemoryAnchors) - mgmt.PUT("/memory/todo", s.mgmt.PutMemoryTodo) - mgmt.PUT("/memory/pinned", s.mgmt.PutMemoryPinned) - mgmt.PUT("/memory/summary", s.mgmt.PutMemorySummary) - mgmt.PUT("/memory/semantic-toggle", s.mgmt.PutMemorySemanticToggle) - mgmt.DELETE("/memory/session", s.mgmt.DeleteMemorySession) - mgmt.POST("/memory/prune", s.mgmt.PruneMemory) - mgmt.GET("/memory/export", s.mgmt.ExportMemorySession) - mgmt.GET("/memory/export-all", s.mgmt.ExportAllMemory) - mgmt.DELETE("/memory/all", s.mgmt.DeleteAllMemory) - mgmt.POST("/memory/import", s.mgmt.ImportMemorySession) - - // Semantic memory routes - mgmt.GET("/semantic/health", s.mgmt.GetSemanticHealth) - mgmt.GET("/semantic/namespaces", s.mgmt.ListSemanticNamespaces) - mgmt.GET("/semantic/items", s.mgmt.GetSemanticItems) - - // ProxyPilot diagnostics and logs routes - mgmt.GET("/proxypilot/logs/tail", s.mgmt.GetProxyPilotLogTail) - mgmt.GET("/proxypilot/diagnostics", s.mgmt.GetProxyPilotDiagnostics) - - mgmt.GET("/updates/check", s.mgmt.GetUpdateInfo) - mgmt.GET("/updates/status", s.mgmt.GetUpdateStatus) - mgmt.POST("/updates/download", s.mgmt.DownloadUpdate) - mgmt.POST("/updates/verify", s.mgmt.VerifyUpdate) - mgmt.POST("/updates/install", s.mgmt.InstallUpdate) - - // Cache management routes - mgmt.GET("/cache/stats", s.mgmt.GetCacheStats) - mgmt.POST("/cache/clear", s.mgmt.ClearCache) - mgmt.PUT("/cache/enabled", s.mgmt.SetCacheEnabled) - - // Prompt cache management routes - mgmt.GET("/prompt-cache/stats", s.mgmt.GetPromptCacheStats) - mgmt.POST("/prompt-cache/clear", s.mgmt.ClearPromptCache) - mgmt.PUT("/prompt-cache/enabled", s.mgmt.SetPromptCacheEnabled) - mgmt.GET("/prompt-cache/top", s.mgmt.GetTopPrompts) - mgmt.POST("/prompt-cache/warm", s.mgmt.WarmPromptCache) } } @@ -776,6 +637,33 @@ func (s *Server) managementAvailabilityMiddleware() gin.HandlerFunc { } } +func (s *Server) serveManagementControlPanel(c *gin.Context) { + cfg := s.cfg + if cfg == nil || cfg.RemoteManagement.DisableControlPanel { + c.AbortWithStatus(http.StatusNotFound) + return + } + filePath := managementasset.FilePath(s.configFilePath) + if strings.TrimSpace(filePath) == "" { + c.AbortWithStatus(http.StatusNotFound) + return + } + + if _, err := os.Stat(filePath); err != nil { + if os.IsNotExist(err) { + go managementasset.EnsureLatestManagementHTML(context.Background(), managementasset.StaticDir(s.configFilePath), cfg.ProxyURL, cfg.RemoteManagement.PanelGitHubRepository) + c.AbortWithStatus(http.StatusNotFound) + return + } + + log.WithError(err).Error("failed to stat management control panel asset") + c.AbortWithStatus(http.StatusInternalServerError) + return + } + + c.File(filePath) +} + func (s *Server) enableKeepAlive(timeout time.Duration, onTimeout func()) { if timeout <= 0 || onTimeout == nil { return @@ -883,11 +771,10 @@ func (s *Server) Start() error { return fmt.Errorf("failed to start HTTP server: server not initialized") } - cfg := s.getConfig() - useTLS := cfg != nil && cfg.TLS.Enable + useTLS := s.cfg != nil && s.cfg.TLS.Enable if useTLS { - cert := strings.TrimSpace(cfg.TLS.Cert) - key := strings.TrimSpace(cfg.TLS.Key) + cert := strings.TrimSpace(s.cfg.TLS.Cert) + key := strings.TrimSpace(s.cfg.TLS.Key) if cert == "" || key == "" { return fmt.Errorf("failed to start HTTPS server: tls.cert or tls.key is empty") } @@ -938,51 +825,11 @@ func (s *Server) Stop(ctx context.Context) error { // // Returns: // - gin.HandlerFunc: The CORS middleware handler -func corsMiddleware(getCfg func() *config.Config) gin.HandlerFunc { +func corsMiddleware() gin.HandlerFunc { return func(c *gin.Context) { - cfg := (*config.Config)(nil) - if getCfg != nil { - cfg = getCfg() - } - - origin := strings.TrimSpace(c.GetHeader("Origin")) - path := c.Request.URL.Path - isManagement := strings.HasPrefix(path, "/v0/management") - - allowOrigins := []string{} - allowMethods := "GET, POST, PUT, PATCH, DELETE, OPTIONS" - allowHeaders := "*" - if cfg != nil { - allowOrigins = cfg.CORS.AllowOrigins - if isManagement && len(cfg.CORS.ManagementAllowOrigins) > 0 { - allowOrigins = cfg.CORS.ManagementAllowOrigins - } - if len(cfg.CORS.AllowMethods) > 0 { - allowMethods = strings.Join(cfg.CORS.AllowMethods, ", ") - } - if len(cfg.CORS.AllowHeaders) > 0 { - allowHeaders = strings.Join(cfg.CORS.AllowHeaders, ", ") - } - } - - allowedOrigin := "" - if origin != "" { - switch { - case len(allowOrigins) == 0 && !isManagement: - allowedOrigin = "*" - case originAllowed(allowOrigins, origin): - allowedOrigin = origin - } - } - - if allowedOrigin != "" { - c.Header("Access-Control-Allow-Origin", allowedOrigin) - c.Header("Access-Control-Allow-Methods", allowMethods) - c.Header("Access-Control-Allow-Headers", allowHeaders) - if allowedOrigin != "*" { - c.Header("Vary", "Origin") - } - } + c.Header("Access-Control-Allow-Origin", "*") + c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") + c.Header("Access-Control-Allow-Headers", "*") if c.Request.Method == "OPTIONS" { c.AbortWithStatus(http.StatusNoContent) @@ -993,34 +840,11 @@ func corsMiddleware(getCfg func() *config.Config) gin.HandlerFunc { } } -func originAllowed(allowOrigins []string, origin string) bool { - if origin == "" || len(allowOrigins) == 0 { - return false - } - for _, allowed := range allowOrigins { - allowed = strings.TrimSpace(allowed) - if allowed == "" { - continue - } - if allowed == "*" || strings.EqualFold(allowed, origin) { - return true - } - } - return false -} - func (s *Server) applyAccessConfig(oldCfg, newCfg *config.Config) { if s == nil || s.accessManager == nil || newCfg == nil { return } - var oldSDK, newSDK *sdkConfig.SDKConfig - if oldCfg != nil { - oldSDK = &oldCfg.SDKConfig - } - if newCfg != nil { - newSDK = &newCfg.SDKConfig - } - if _, err := access.ApplyAccessProviders(s.accessManager, oldSDK, newSDK); err != nil { + if _, err := access.ApplyAccessProviders(s.accessManager, oldCfg, newCfg); err != nil { return } } @@ -1082,17 +906,6 @@ func (s *Server) UpdateClients(cfg *config.Config) { } } - metricsEnabled := cfg.IsMetricsEnabled() - requestHistoryEnabled := cfg.IsRequestHistoryEnabled() - if cfg.CommercialMode { - metricsEnabled = false - requestHistoryEnabled = false - } - middleware.SetMetricsEnabled(metricsEnabled) - middleware.SetRequestHistoryEnabled(requestHistoryEnabled) - middleware.SetRequestHistorySampleRate(cfg.GetRequestHistorySampleRate()) - usage.SetSamplingRate(cfg.GetUsageSampleRate()) - if oldCfg == nil || oldCfg.DisableCooling != cfg.DisableCooling { auth.SetQuotaCooldownDisabled(cfg.DisableCooling) if oldCfg != nil { @@ -1158,26 +971,45 @@ func (s *Server) UpdateClients(cfg *config.Config) { } s.applyAccessConfig(oldCfg, cfg) - cache.InitDefaultResponseCache(buildResponseCacheConfig(cfg)) - cache.InitDefaultPromptCache(buildPromptCacheConfig(cfg)) s.cfg = cfg - s.cfgHolder.Store(cfg) s.wsAuthEnabled.Store(cfg.WebsocketAuth) if oldCfg != nil && s.wsAuthChanged != nil && oldCfg.WebsocketAuth != cfg.WebsocketAuth { s.wsAuthChanged(oldCfg.WebsocketAuth, cfg.WebsocketAuth) } + managementasset.SetCurrentConfig(cfg) // Save YAML snapshot for next comparison s.oldConfigYaml, _ = yaml.Marshal(cfg) s.handlers.UpdateClients(&cfg.SDKConfig) + if !cfg.RemoteManagement.DisableControlPanel { + staticDir := managementasset.StaticDir(s.configFilePath) + go managementasset.EnsureLatestManagementHTML(context.Background(), staticDir, cfg.ProxyURL, cfg.RemoteManagement.PanelGitHubRepository) + } if s.mgmt != nil { s.mgmt.SetConfig(cfg) s.mgmt.SetAuthManager(s.handlers.AuthManager) } - // Count client sources from configuration and auth directory - authFiles := util.CountAuthFiles(cfg.AuthDir) + // Notify Amp module only when Amp config has changed. + ampConfigChanged := oldCfg == nil || !reflect.DeepEqual(oldCfg.AmpCode, cfg.AmpCode) + if ampConfigChanged { + if s.ampModule != nil { + log.Debugf("triggering amp module config update") + if err := s.ampModule.OnConfigUpdated(cfg); err != nil { + log.Errorf("failed to update Amp module config: %v", err) + } + } else { + log.Warnf("amp module is nil, skipping config update") + } + } + + // Count client sources from configuration and auth store. + tokenStore := sdkAuth.GetTokenStore() + if dirSetter, ok := tokenStore.(interface{ SetBaseDir(string) }); ok { + dirSetter.SetBaseDir(cfg.AuthDir) + } + authEntries := util.CountAuthFiles(context.Background(), tokenStore) geminiAPIKeyCount := len(cfg.GeminiKey) claudeAPIKeyCount := len(cfg.ClaudeKey) codexAPIKeyCount := len(cfg.CodexKey) @@ -1188,10 +1020,10 @@ func (s *Server) UpdateClients(cfg *config.Config) { openAICompatCount += len(entry.APIKeyEntries) } - total := authFiles + geminiAPIKeyCount + claudeAPIKeyCount + codexAPIKeyCount + vertexAICompatCount + openAICompatCount - fmt.Printf("server clients and configuration updated: %d clients (%d auth files + %d Gemini API keys + %d Claude API keys + %d Codex keys + %d Vertex-compat + %d OpenAI-compat)\n", + total := authEntries + geminiAPIKeyCount + claudeAPIKeyCount + codexAPIKeyCount + vertexAICompatCount + openAICompatCount + fmt.Printf("server clients and configuration updated: %d clients (%d auth entries + %d Gemini API keys + %d Claude API keys + %d Codex keys + %d Vertex-compat + %d OpenAI-compat)\n", total, - authFiles, + authEntries, geminiAPIKeyCount, claudeAPIKeyCount, codexAPIKeyCount, @@ -1212,14 +1044,10 @@ func (s *Server) SetWebsocketAuthChangeHandler(fn func(bool, bool)) { // AuthMiddleware returns a Gin middleware handler that authenticates requests // using the configured authentication providers. When no providers are available, // it allows all requests (legacy behaviour). -func AuthMiddleware(manager *sdkaccess.Manager, allowUnauthenticated func() bool) gin.HandlerFunc { +func AuthMiddleware(manager *sdkaccess.Manager) gin.HandlerFunc { return func(c *gin.Context) { if manager == nil { - if allowUnauthenticated != nil && allowUnauthenticated() { - c.Next() - return - } - c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Missing API key"}) + c.Next() return } @@ -1247,138 +1075,3 @@ func AuthMiddleware(manager *sdkaccess.Manager, allowUnauthenticated func() bool } } } - -// authManagerAdapter adapts *auth.Manager to the memory.CoreManagerExecutor interface. -// This bridges the typed signature to the interface{} signature expected by the summarizer. -type authManagerAdapter struct { - manager *auth.Manager -} - -// Execute implements memory.CoreManagerExecutor by delegating to the typed manager. -// It converts the interface{} types to/from the concrete executor types. -func (a *authManagerAdapter) Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) (interface{}, error) { - if a.manager == nil { - return nil, errors.New("auth manager adapter: manager is nil") - } - - // Convert the memory package types to cliproxyexecutor types. - var execReq cliproxyexecutor.Request - var execOpts cliproxyexecutor.Options - - switch r := req.(type) { - case nil: - case cliproxyexecutor.Request: - execReq = r - case *cliproxyexecutor.Request: - if r != nil { - execReq = *r - } - case memory.ExecutorRequest: - execReq = cliproxyexecutor.Request{ - Model: r.Model, - Payload: r.Payload, - Metadata: r.Metadata, - } - case *memory.ExecutorRequest: - if r != nil { - execReq = cliproxyexecutor.Request{ - Model: r.Model, - Payload: r.Payload, - Metadata: r.Metadata, - } - } - default: - reqBytes, err := json.Marshal(req) - if err != nil { - return nil, fmt.Errorf("auth manager adapter: failed to marshal request: %w", err) - } - if err := json.Unmarshal(reqBytes, &execReq); err != nil { - return nil, fmt.Errorf("auth manager adapter: failed to unmarshal request: %w", err) - } - } - - switch o := opts.(type) { - case nil: - case cliproxyexecutor.Options: - execOpts = o - case *cliproxyexecutor.Options: - if o != nil { - execOpts = *o - } - case memory.ExecutorOptions: - execOpts = cliproxyexecutor.Options{ - Stream: o.Stream, - Headers: o.Headers, - } - case *memory.ExecutorOptions: - if o != nil { - execOpts = cliproxyexecutor.Options{ - Stream: o.Stream, - Headers: o.Headers, - } - } - default: - optsBytes, err := json.Marshal(opts) - if err != nil { - return nil, fmt.Errorf("auth manager adapter: failed to marshal options: %w", err) - } - if err := json.Unmarshal(optsBytes, &execOpts); err != nil { - return nil, fmt.Errorf("auth manager adapter: failed to unmarshal options: %w", err) - } - } - - resp, err := a.manager.Execute(ctx, providers, execReq, execOpts) - if err != nil { - return nil, err - } - - // Return the response as interface{} - return resp, nil -} - -func (s *Server) getConfig() *config.Config { - if s == nil { - return nil - } - if v := s.cfgHolder.Load(); v != nil { - if cfg, ok := v.(*config.Config); ok { - return cfg - } - } - return s.cfg -} - -func (s *Server) allowUnauthenticated() bool { - cfg := s.getConfig() - if cfg == nil { - return false - } - return cfg.AllowUnauthenticated -} - -func buildResponseCacheConfig(cfg *config.Config) cache.ResponseCacheConfig { - if cfg == nil { - return cache.DefaultResponseCacheConfig() - } - return cache.ResponseCacheConfig{ - Enabled: cfg.ResponseCache.Enabled, - MaxSize: cfg.ResponseCache.GetMaxSize(), - MaxBytes: cfg.ResponseCache.GetMaxBytes(), - TTL: cfg.ResponseCache.GetTTL(), - ExcludeModels: append([]string(nil), cfg.ResponseCache.ExcludeModels...), - PersistFile: cfg.ResponseCache.PersistFile, - } -} - -func buildPromptCacheConfig(cfg *config.Config) cache.PromptCacheConfig { - if cfg == nil { - return cache.DefaultPromptCacheConfig() - } - return cache.PromptCacheConfig{ - Enabled: cfg.PromptCache.Enabled, - MaxSize: cfg.PromptCache.GetMaxSize(), - MaxBytes: cfg.PromptCache.GetMaxBytes(), - TTL: cfg.PromptCache.GetTTL(), - PersistFile: cfg.PromptCache.PersistFile, - } -} diff --git a/internal/api/server_test.go b/internal/api/server_test.go index 1868ad7c..06653210 100644 --- a/internal/api/server_test.go +++ b/internal/api/server_test.go @@ -1,8 +1,11 @@ package api import ( + "net/http" + "net/http/httptest" "os" "path/filepath" + "strings" "testing" gin "github.com/gin-gonic/gin" @@ -40,3 +43,69 @@ func newTestServer(t *testing.T) *Server { configPath := filepath.Join(tmpDir, "config.yaml") return NewServer(cfg, authManager, accessManager, configPath) } + +func TestAmpProviderModelRoutes(t *testing.T) { + testCases := []struct { + name string + path string + wantStatus int + wantContains string + }{ + { + name: "openai root models", + path: "/api/provider/openai/models", + wantStatus: http.StatusOK, + wantContains: `"object":"list"`, + }, + { + name: "groq root models", + path: "/api/provider/groq/models", + wantStatus: http.StatusOK, + wantContains: `"object":"list"`, + }, + { + name: "openai models", + path: "/api/provider/openai/v1/models", + wantStatus: http.StatusOK, + wantContains: `"object":"list"`, + }, + { + name: "anthropic models", + path: "/api/provider/anthropic/v1/models", + wantStatus: http.StatusOK, + wantContains: `"data"`, + }, + { + name: "google models v1", + path: "/api/provider/google/v1/models", + wantStatus: http.StatusOK, + wantContains: `"models"`, + }, + { + name: "google models v1beta", + path: "/api/provider/google/v1beta/models", + wantStatus: http.StatusOK, + wantContains: `"models"`, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + server := newTestServer(t) + + req := httptest.NewRequest(http.MethodGet, tc.path, nil) + req.Header.Set("Authorization", "Bearer test-key") + + rr := httptest.NewRecorder() + server.engine.ServeHTTP(rr, req) + + if rr.Code != tc.wantStatus { + t.Fatalf("unexpected status code for %s: got %d want %d; body=%s", tc.path, rr.Code, tc.wantStatus, rr.Body.String()) + } + if body := rr.Body.String(); !strings.Contains(body, tc.wantContains) { + t.Fatalf("response body for %s missing %q: %s", tc.path, tc.wantContains, body) + } + }) + } +} diff --git a/internal/auth/claude/errors.go b/internal/auth/claude/errors.go index 817011e6..3585209a 100644 --- a/internal/auth/claude/errors.go +++ b/internal/auth/claude/errors.go @@ -60,6 +60,12 @@ func (e *AuthenticationError) Error() string { // Common authentication error types. var ( + // ErrTokenExpired = &AuthenticationError{ + // Type: "token_expired", + // Message: "Access token has expired", + // Code: http.StatusUnauthorized, + // } + // ErrInvalidState represents an error for invalid OAuth state parameter. ErrInvalidState = &AuthenticationError{ Type: "invalid_state", diff --git a/internal/auth/codex/errors.go b/internal/auth/codex/errors.go index 005d2209..d8065f7a 100644 --- a/internal/auth/codex/errors.go +++ b/internal/auth/codex/errors.go @@ -57,6 +57,12 @@ func (e *AuthenticationError) Error() string { // Common authentication error types. var ( + // ErrTokenExpired = &AuthenticationError{ + // Type: "token_expired", + // Message: "Access token has expired", + // Code: http.StatusUnauthorized, + // } + // ErrInvalidState represents an error for invalid OAuth state parameter. ErrInvalidState = &AuthenticationError{ Type: "invalid_state", diff --git a/internal/cache/signature_cache.go b/internal/cache/signature_cache.go index 69c5b701..af5371bf 100644 --- a/internal/cache/signature_cache.go +++ b/internal/cache/signature_cache.go @@ -15,8 +15,8 @@ type SignatureEntry struct { } const ( - // SignatureCacheTTL is how long signatures are valid (reduced from 3h for memory efficiency) - SignatureCacheTTL = 1 * time.Hour + // SignatureCacheTTL is how long signatures are valid + SignatureCacheTTL = 3 * time.Hour // SignatureTextHashLen is the length of the hash key (16 hex chars = 64-bit key space) SignatureTextHashLen = 16 diff --git a/internal/cmd/anthropic_login.go b/internal/cmd/anthropic_login.go index 6efd87a8..dafdd02b 100644 --- a/internal/cmd/anthropic_login.go +++ b/internal/cmd/anthropic_login.go @@ -32,9 +32,10 @@ func DoClaudeLogin(cfg *config.Config, options *LoginOptions) { manager := newAuthManager() authOpts := &sdkAuth.LoginOptions{ - NoBrowser: options.NoBrowser, - Metadata: map[string]string{}, - Prompt: promptFn, + NoBrowser: options.NoBrowser, + CallbackPort: options.CallbackPort, + Metadata: map[string]string{}, + Prompt: promptFn, } _, savedPath, err := manager.Login(context.Background(), "claude", cfg, authOpts) diff --git a/internal/cmd/antigravity_login.go b/internal/cmd/antigravity_login.go index 0a4d5d90..6569de1b 100644 --- a/internal/cmd/antigravity_login.go +++ b/internal/cmd/antigravity_login.go @@ -6,7 +6,6 @@ import ( "github.com/router-for-me/CLIProxyAPI/v6/internal/config" sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth" - coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" log "github.com/sirupsen/logrus" ) @@ -23,9 +22,10 @@ func DoAntigravityLogin(cfg *config.Config, options *LoginOptions) { manager := newAuthManager() authOpts := &sdkAuth.LoginOptions{ - NoBrowser: options.NoBrowser, - Metadata: map[string]string{}, - Prompt: promptFn, + NoBrowser: options.NoBrowser, + CallbackPort: options.CallbackPort, + Metadata: map[string]string{}, + Prompt: promptFn, } record, savedPath, err := manager.Login(context.Background(), "antigravity", cfg, authOpts) @@ -43,32 +43,10 @@ func DoAntigravityLogin(cfg *config.Config, options *LoginOptions) { fmt.Println("Antigravity authentication successful!") } -// DoAntigravityImport imports tokens from Antigravity IDE's storage -// and saves them as Antigravity credentials for use with ProxyPilot. +// DoAntigravityImport imports Antigravity credentials from existing browser sessions or config files. +// This allows using existing Antigravity subscriptions without re-authenticating. func DoAntigravityImport(cfg *config.Config) { - manager := newAuthManager() - - authenticator := sdkAuth.NewAntigravityAuthenticator() - auth, ok := authenticator.(interface { - ImportFromAntigravityIDE(ctx context.Context, cfg *config.Config) (*coreauth.Auth, error) - }) - if !ok { - log.Error("Antigravity authenticator does not support ImportFromAntigravityIDE") - return - } - - record, err := auth.ImportFromAntigravityIDE(context.Background(), cfg) - if err != nil { - log.Errorf("Failed to import from Antigravity IDE: %v", err) - return - } - - savedPath, errSave := manager.SaveAuth(record, cfg) - if errSave != nil { - log.Errorf("Failed to save imported token: %v", errSave) - return - } - - fmt.Printf("Token saved to %s\n", savedPath) - fmt.Println("You can now use Antigravity provider with your Antigravity IDE credentials!") + log.Info("Antigravity import is not yet implemented") + fmt.Println("Antigravity credential import is not yet supported.") + fmt.Println("Please use --antigravity-login to authenticate instead.") } diff --git a/internal/cmd/auth_manager.go b/internal/cmd/auth_manager.go index 760db33c..5d5f895a 100644 --- a/internal/cmd/auth_manager.go +++ b/internal/cmd/auth_manager.go @@ -6,7 +6,7 @@ import ( // newAuthManager creates a new authentication manager instance with all supported // authenticators and a file-based token store. It initializes authenticators for -// Gemini, Codex, Claude, Qwen, Antigravity, Kiro, MiniMax, and Zhipu providers. +// Gemini, Codex, Claude, and Qwen providers. // // Returns: // - *sdkAuth.Manager: A configured authentication manager instance @@ -17,10 +17,8 @@ func newAuthManager() *sdkAuth.Manager { sdkAuth.NewCodexAuthenticator(), sdkAuth.NewClaudeAuthenticator(), sdkAuth.NewQwenAuthenticator(), + sdkAuth.NewIFlowAuthenticator(), sdkAuth.NewAntigravityAuthenticator(), - sdkAuth.NewKiroAuthenticator(), - sdkAuth.NewMiniMaxAuthenticator(), - sdkAuth.NewZhipuAuthenticator(), ) return manager } diff --git a/internal/cmd/iflow_cookie.go b/internal/cmd/iflow_cookie.go new file mode 100644 index 00000000..358b8062 --- /dev/null +++ b/internal/cmd/iflow_cookie.go @@ -0,0 +1,98 @@ +package cmd + +import ( + "bufio" + "context" + "fmt" + "os" + "path/filepath" + "strings" + "time" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/iflow" + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" +) + +// DoIFlowCookieAuth performs the iFlow cookie-based authentication. +func DoIFlowCookieAuth(cfg *config.Config, options *LoginOptions) { + if options == nil { + options = &LoginOptions{} + } + + promptFn := options.Prompt + if promptFn == nil { + reader := bufio.NewReader(os.Stdin) + promptFn = func(prompt string) (string, error) { + fmt.Print(prompt) + value, err := reader.ReadString('\n') + if err != nil { + return "", err + } + return strings.TrimSpace(value), nil + } + } + + // Prompt user for cookie + cookie, err := promptForCookie(promptFn) + if err != nil { + fmt.Printf("Failed to get cookie: %v\n", err) + return + } + + // Check for duplicate BXAuth before authentication + bxAuth := iflow.ExtractBXAuth(cookie) + if existingFile, err := iflow.CheckDuplicateBXAuth(cfg.AuthDir, bxAuth); err != nil { + fmt.Printf("Failed to check duplicate: %v\n", err) + return + } else if existingFile != "" { + fmt.Printf("Duplicate BXAuth found, authentication already exists: %s\n", filepath.Base(existingFile)) + return + } + + // Authenticate with cookie + auth := iflow.NewIFlowAuth(cfg) + ctx := context.Background() + + tokenData, err := auth.AuthenticateWithCookie(ctx, cookie) + if err != nil { + fmt.Printf("iFlow cookie authentication failed: %v\n", err) + return + } + + // Create token storage + tokenStorage := auth.CreateCookieTokenStorage(tokenData) + + // Get auth file path using email in filename + authFilePath := getAuthFilePath(cfg, "iflow", tokenData.Email) + + // Save token to file + if err := tokenStorage.SaveTokenToFile(authFilePath); err != nil { + fmt.Printf("Failed to save authentication: %v\n", err) + return + } + + fmt.Printf("Authentication successful! API key: %s\n", tokenData.APIKey) + fmt.Printf("Expires at: %s\n", tokenData.Expire) + fmt.Printf("Authentication saved to: %s\n", authFilePath) +} + +// promptForCookie prompts the user to enter their iFlow cookie +func promptForCookie(promptFn func(string) (string, error)) (string, error) { + line, err := promptFn("Enter iFlow Cookie (from browser cookies): ") + if err != nil { + return "", fmt.Errorf("failed to read cookie: %w", err) + } + + cookie, err := iflow.NormalizeCookie(line) + if err != nil { + return "", err + } + + return cookie, nil +} + +// getAuthFilePath returns the auth file path for the given provider and email +func getAuthFilePath(cfg *config.Config, provider, email string) string { + fileName := iflow.SanitizeIFlowFileName(email) + return fmt.Sprintf("%s/%s-%s-%d.json", cfg.AuthDir, provider, fileName, time.Now().Unix()) +} diff --git a/internal/cmd/iflow_login.go b/internal/cmd/iflow_login.go new file mode 100644 index 00000000..07360b8c --- /dev/null +++ b/internal/cmd/iflow_login.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "context" + "errors" + "fmt" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth" + log "github.com/sirupsen/logrus" +) + +// DoIFlowLogin performs the iFlow OAuth login via the shared authentication manager. +func DoIFlowLogin(cfg *config.Config, options *LoginOptions) { + if options == nil { + options = &LoginOptions{} + } + + manager := newAuthManager() + + promptFn := options.Prompt + if promptFn == nil { + promptFn = defaultProjectPrompt() + } + + authOpts := &sdkAuth.LoginOptions{ + NoBrowser: options.NoBrowser, + CallbackPort: options.CallbackPort, + Metadata: map[string]string{}, + Prompt: promptFn, + } + + _, savedPath, err := manager.Login(context.Background(), "iflow", cfg, authOpts) + if err != nil { + var emailErr *sdkAuth.EmailRequiredError + if errors.As(err, &emailErr) { + log.Error(emailErr.Error()) + return + } + fmt.Printf("iFlow authentication failed: %v\n", err) + return + } + + if savedPath != "" { + fmt.Printf("Authentication saved to %s\n", savedPath) + } + + fmt.Println("iFlow authentication successful!") +} diff --git a/internal/cmd/login.go b/internal/cmd/login.go index 11411581..b5129cfd 100644 --- a/internal/cmd/login.go +++ b/internal/cmd/login.go @@ -67,10 +67,11 @@ func DoLogin(cfg *config.Config, projectID string, options *LoginOptions) { } loginOpts := &sdkAuth.LoginOptions{ - NoBrowser: options.NoBrowser, - ProjectID: trimmedProjectID, - Metadata: map[string]string{}, - Prompt: callbackPrompt, + NoBrowser: options.NoBrowser, + ProjectID: trimmedProjectID, + CallbackPort: options.CallbackPort, + Metadata: map[string]string{}, + Prompt: callbackPrompt, } authenticator := sdkAuth.NewGeminiAuthenticator() @@ -88,8 +89,9 @@ func DoLogin(cfg *config.Config, projectID string, options *LoginOptions) { geminiAuth := gemini.NewGeminiAuth() httpClient, errClient := geminiAuth.GetAuthenticatedClient(ctx, storage, cfg, &gemini.WebLoginOptions{ - NoBrowser: options.NoBrowser, - Prompt: callbackPrompt, + NoBrowser: options.NoBrowser, + CallbackPort: options.CallbackPort, + Prompt: callbackPrompt, }) if errClient != nil { log.Errorf("Gemini authentication failed: %v", errClient) @@ -116,6 +118,7 @@ func DoLogin(cfg *config.Config, projectID string, options *LoginOptions) { } activatedProjects := make([]string, 0, len(projectSelections)) + seenProjects := make(map[string]bool) for _, candidateID := range projectSelections { log.Infof("Activating project %s", candidateID) if errSetup := performGeminiCLISetup(ctx, httpClient, storage, candidateID); errSetup != nil { @@ -132,6 +135,13 @@ func DoLogin(cfg *config.Config, projectID string, options *LoginOptions) { if finalID == "" { finalID = candidateID } + + // Skip duplicates + if seenProjects[finalID] { + log.Infof("Project %s already activated, skipping", finalID) + continue + } + seenProjects[finalID] = true activatedProjects = append(activatedProjects, finalID) } @@ -140,17 +150,14 @@ func DoLogin(cfg *config.Config, projectID string, options *LoginOptions) { if !storage.Auto && !storage.Checked { for _, pid := range activatedProjects { - // Sleep briefly to avoid hitting 'Mutate requests' rate limits when processing many projects - time.Sleep(1 * time.Second) - isChecked, errCheck := checkCloudAPIIsEnabled(ctx, httpClient, pid) if errCheck != nil { - log.Warnf("Failed to check/enable Cloud AI API for %s (skipping): %v", pid, errCheck) - continue + log.Errorf("Failed to check if Cloud AI API is enabled for %s: %v", pid, errCheck) + return } if !isChecked { - log.Warnf("Cloud AI API is not enabled for project %s (skipping).", pid) - continue + log.Errorf("Failed to check if Cloud AI API is enabled for project %s. If you encounter an error message, please create an issue.", pid) + return } } storage.Checked = true @@ -262,7 +269,39 @@ func performGeminiCLISetup(ctx context.Context, httpClient *http.Client, storage finalProjectID := projectID if responseProjectID != "" { if explicitProject && !strings.EqualFold(responseProjectID, projectID) { - log.Warnf("Gemini onboarding returned project %s instead of requested %s; keeping requested project ID.", responseProjectID, projectID) + // Check if this is a free user (gen-lang-client projects or free/legacy tier) + isFreeUser := strings.HasPrefix(projectID, "gen-lang-client-") || + strings.EqualFold(tierID, "FREE") || + strings.EqualFold(tierID, "LEGACY") + + if isFreeUser { + // Interactive prompt for free users + fmt.Printf("\nGoogle returned a different project ID:\n") + fmt.Printf(" Requested (frontend): %s\n", projectID) + fmt.Printf(" Returned (backend): %s\n\n", responseProjectID) + fmt.Printf(" Backend project IDs have access to preview models (gemini-3-*).\n") + fmt.Printf(" This is normal for free tier users.\n\n") + fmt.Printf("Which project ID would you like to use?\n") + fmt.Printf(" [1] Backend (recommended): %s\n", responseProjectID) + fmt.Printf(" [2] Frontend: %s\n\n", projectID) + fmt.Printf("Enter choice [1]: ") + + reader := bufio.NewReader(os.Stdin) + choice, _ := reader.ReadString('\n') + choice = strings.TrimSpace(choice) + + if choice == "2" { + log.Infof("Using frontend project ID: %s", projectID) + fmt.Println(". Warning: Frontend project IDs may not have access to preview models.") + finalProjectID = projectID + } else { + log.Infof("Using backend project ID: %s (recommended)", responseProjectID) + finalProjectID = responseProjectID + } + } else { + // Pro users: keep requested project ID (original behavior) + log.Warnf("Gemini onboarding returned project %s instead of requested %s; keeping requested project ID.", responseProjectID, projectID) + } } else { finalProjectID = responseProjectID } diff --git a/internal/cmd/openai_login.go b/internal/cmd/openai_login.go index d981f6ae..5f2fb162 100644 --- a/internal/cmd/openai_login.go +++ b/internal/cmd/openai_login.go @@ -19,6 +19,9 @@ type LoginOptions struct { // NoBrowser indicates whether to skip opening the browser automatically. NoBrowser bool + // CallbackPort overrides the local OAuth callback port when set (>0). + CallbackPort int + // Prompt allows the caller to provide interactive input when needed. Prompt func(prompt string) (string, error) } @@ -43,9 +46,10 @@ func DoCodexLogin(cfg *config.Config, options *LoginOptions) { manager := newAuthManager() authOpts := &sdkAuth.LoginOptions{ - NoBrowser: options.NoBrowser, - Metadata: map[string]string{}, - Prompt: promptFn, + NoBrowser: options.NoBrowser, + CallbackPort: options.CallbackPort, + Metadata: map[string]string{}, + Prompt: promptFn, } _, savedPath, err := manager.Login(context.Background(), "codex", cfg, authOpts) diff --git a/internal/cmd/qwen_login.go b/internal/cmd/qwen_login.go index 27edf408..92a57aa5 100644 --- a/internal/cmd/qwen_login.go +++ b/internal/cmd/qwen_login.go @@ -36,9 +36,10 @@ func DoQwenLogin(cfg *config.Config, options *LoginOptions) { } authOpts := &sdkAuth.LoginOptions{ - NoBrowser: options.NoBrowser, - Metadata: map[string]string{}, - Prompt: promptFn, + NoBrowser: options.NoBrowser, + CallbackPort: options.CallbackPort, + Metadata: map[string]string{}, + Prompt: promptFn, } _, savedPath, err := manager.Login(context.Background(), "qwen", cfg, authOpts) diff --git a/internal/cmd/run.go b/internal/cmd/run.go index 737fdcbc..3306ed95 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -25,23 +25,6 @@ import ( // - configPath: The path to the configuration file // - localPassword: Optional password accepted for local management requests func StartService(cfg *config.Config, configPath string, localPassword string) { - StartServiceWithOptions(cfg, configPath, localPassword, true) -} - -// StartServiceStandalone starts the service without keep-alive shutdown. -// Use this for standalone server mode where no tray app is pinging. -func StartServiceStandalone(cfg *config.Config, configPath string, localPassword string) { - StartServiceWithOptions(cfg, configPath, localPassword, false) -} - -// StartServiceWithOptions builds and runs the proxy service with configurable keep-alive behavior. -// -// Parameters: -// - cfg: The application configuration -// - configPath: The path to the configuration file -// - localPassword: Optional password accepted for local management requests -// - shutdownOnKeepAliveTimeout: If true, shutdown when keep-alive times out (for subprocess mode) -func StartServiceWithOptions(cfg *config.Config, configPath string, localPassword string, shutdownOnKeepAliveTimeout bool) { builder := cliproxy.NewBuilder(). WithConfig(cfg). WithConfigPath(configPath). @@ -51,7 +34,7 @@ func StartServiceWithOptions(cfg *config.Config, configPath string, localPasswor defer cancel() runCtx := ctxSignal - if localPassword != "" && shutdownOnKeepAliveTimeout { + if localPassword != "" { var keepAliveCancel context.CancelFunc runCtx, keepAliveCancel = context.WithCancel(ctxSignal) builder = builder.WithServerOptions(api.WithKeepAliveEndpoint(10*time.Second, func() { @@ -85,3 +68,31 @@ func WaitForCloudDeploy() { <-ctxSignal.Done() log.Info("Cloud deploy mode: Shutdown signal received; exiting") } + +// StartServiceStandalone builds and runs the proxy service in standalone mode (no keep-alive shutdown). +// This is used when the server is started directly without the tray app as a parent. +// +// Parameters: +// - cfg: The application configuration +// - configPath: The path to the configuration file +// - localPassword: Optional password accepted for local management requests +func StartServiceStandalone(cfg *config.Config, configPath string, localPassword string) { + builder := cliproxy.NewBuilder(). + WithConfig(cfg). + WithConfigPath(configPath). + WithLocalManagementPassword(localPassword) + + ctxSignal, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer cancel() + + service, err := builder.Build() + if err != nil { + log.Errorf("failed to build proxy service: %v", err) + return + } + + err = service.Run(ctxSignal) + if err != nil && !errors.Is(err, context.Canceled) { + log.Errorf("proxy service exited with error: %v", err) + } +} diff --git a/internal/config/oauth_model_alias_migration.go b/internal/config/oauth_model_alias_migration.go new file mode 100644 index 00000000..5cc8053a --- /dev/null +++ b/internal/config/oauth_model_alias_migration.go @@ -0,0 +1,275 @@ +package config + +import ( + "os" + "strings" + + "gopkg.in/yaml.v3" +) + +// antigravityModelConversionTable maps old built-in aliases to actual model names +// for the antigravity channel during migration. +var antigravityModelConversionTable = map[string]string{ + "gemini-2.5-computer-use-preview-10-2025": "rev19-uic3-1p", + "gemini-3-pro-image-preview": "gemini-3-pro-image", + "gemini-3-pro-preview": "gemini-3-pro-high", + "gemini-3-flash-preview": "gemini-3-flash", + "gemini-claude-sonnet-4-5": "claude-sonnet-4-5", + "gemini-claude-sonnet-4-5-thinking": "claude-sonnet-4-5-thinking", + "gemini-claude-opus-4-5-thinking": "claude-opus-4-5-thinking", +} + +// defaultAntigravityAliases returns the default oauth-model-alias configuration +// for the antigravity channel when neither field exists. +func defaultAntigravityAliases() []OAuthModelAlias { + return []OAuthModelAlias{ + {Name: "rev19-uic3-1p", Alias: "gemini-2.5-computer-use-preview-10-2025"}, + {Name: "gemini-3-pro-image", Alias: "gemini-3-pro-image-preview"}, + {Name: "gemini-3-pro-high", Alias: "gemini-3-pro-preview"}, + {Name: "gemini-3-flash", Alias: "gemini-3-flash-preview"}, + {Name: "claude-sonnet-4-5", Alias: "gemini-claude-sonnet-4-5"}, + {Name: "claude-sonnet-4-5-thinking", Alias: "gemini-claude-sonnet-4-5-thinking"}, + {Name: "claude-opus-4-5-thinking", Alias: "gemini-claude-opus-4-5-thinking"}, + } +} + +// MigrateOAuthModelAlias checks for and performs migration from oauth-model-mappings +// to oauth-model-alias at startup. Returns true if migration was performed. +// +// Migration flow: +// 1. Check if oauth-model-alias exists -> skip migration +// 2. Check if oauth-model-mappings exists -> convert and migrate +// - For antigravity channel, convert old built-in aliases to actual model names +// +// 3. Neither exists -> add default antigravity config +func MigrateOAuthModelAlias(configFile string) (bool, error) { + data, err := os.ReadFile(configFile) + if err != nil { + if os.IsNotExist(err) { + return false, nil + } + return false, err + } + if len(data) == 0 { + return false, nil + } + + // Parse YAML into node tree to preserve structure + var root yaml.Node + if err := yaml.Unmarshal(data, &root); err != nil { + return false, nil + } + if root.Kind != yaml.DocumentNode || len(root.Content) == 0 { + return false, nil + } + rootMap := root.Content[0] + if rootMap == nil || rootMap.Kind != yaml.MappingNode { + return false, nil + } + + // Check if oauth-model-alias already exists + if findMapKeyIndex(rootMap, "oauth-model-alias") >= 0 { + return false, nil + } + + // Check if oauth-model-mappings exists + oldIdx := findMapKeyIndex(rootMap, "oauth-model-mappings") + if oldIdx >= 0 { + // Migrate from old field + return migrateFromOldField(configFile, &root, rootMap, oldIdx) + } + + // Neither field exists - add default antigravity config + return addDefaultAntigravityConfig(configFile, &root, rootMap) +} + +// migrateFromOldField converts oauth-model-mappings to oauth-model-alias +func migrateFromOldField(configFile string, root *yaml.Node, rootMap *yaml.Node, oldIdx int) (bool, error) { + if oldIdx+1 >= len(rootMap.Content) { + return false, nil + } + oldValue := rootMap.Content[oldIdx+1] + if oldValue == nil || oldValue.Kind != yaml.MappingNode { + return false, nil + } + + // Parse the old aliases + oldAliases := parseOldAliasNode(oldValue) + if len(oldAliases) == 0 { + // Remove the old field and write + removeMapKeyByIndex(rootMap, oldIdx) + return writeYAMLNode(configFile, root) + } + + // Convert model names for antigravity channel + newAliases := make(map[string][]OAuthModelAlias, len(oldAliases)) + for channel, entries := range oldAliases { + converted := make([]OAuthModelAlias, 0, len(entries)) + for _, entry := range entries { + newEntry := OAuthModelAlias{ + Name: entry.Name, + Alias: entry.Alias, + Fork: entry.Fork, + } + // Convert model names for antigravity channel + if strings.EqualFold(channel, "antigravity") { + if actual, ok := antigravityModelConversionTable[entry.Name]; ok { + newEntry.Name = actual + } + } + converted = append(converted, newEntry) + } + newAliases[channel] = converted + } + + // For antigravity channel, supplement missing default aliases + if antigravityEntries, exists := newAliases["antigravity"]; exists { + // Build a set of already configured model names (upstream names) + configuredModels := make(map[string]bool, len(antigravityEntries)) + for _, entry := range antigravityEntries { + configuredModels[entry.Name] = true + } + + // Add missing default aliases + for _, defaultAlias := range defaultAntigravityAliases() { + if !configuredModels[defaultAlias.Name] { + antigravityEntries = append(antigravityEntries, defaultAlias) + } + } + newAliases["antigravity"] = antigravityEntries + } + + // Build new node + newNode := buildOAuthModelAliasNode(newAliases) + + // Replace old key with new key and value + rootMap.Content[oldIdx].Value = "oauth-model-alias" + rootMap.Content[oldIdx+1] = newNode + + return writeYAMLNode(configFile, root) +} + +// addDefaultAntigravityConfig adds the default antigravity configuration +func addDefaultAntigravityConfig(configFile string, root *yaml.Node, rootMap *yaml.Node) (bool, error) { + defaults := map[string][]OAuthModelAlias{ + "antigravity": defaultAntigravityAliases(), + } + newNode := buildOAuthModelAliasNode(defaults) + + // Add new key-value pair + keyNode := &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "oauth-model-alias"} + rootMap.Content = append(rootMap.Content, keyNode, newNode) + + return writeYAMLNode(configFile, root) +} + +// parseOldAliasNode parses the old oauth-model-mappings node structure +func parseOldAliasNode(node *yaml.Node) map[string][]OAuthModelAlias { + if node == nil || node.Kind != yaml.MappingNode { + return nil + } + result := make(map[string][]OAuthModelAlias) + for i := 0; i+1 < len(node.Content); i += 2 { + channelNode := node.Content[i] + entriesNode := node.Content[i+1] + if channelNode == nil || entriesNode == nil { + continue + } + channel := strings.ToLower(strings.TrimSpace(channelNode.Value)) + if channel == "" || entriesNode.Kind != yaml.SequenceNode { + continue + } + entries := make([]OAuthModelAlias, 0, len(entriesNode.Content)) + for _, entryNode := range entriesNode.Content { + if entryNode == nil || entryNode.Kind != yaml.MappingNode { + continue + } + entry := parseAliasEntry(entryNode) + if entry.Name != "" && entry.Alias != "" { + entries = append(entries, entry) + } + } + if len(entries) > 0 { + result[channel] = entries + } + } + return result +} + +// parseAliasEntry parses a single alias entry node +func parseAliasEntry(node *yaml.Node) OAuthModelAlias { + var entry OAuthModelAlias + for i := 0; i+1 < len(node.Content); i += 2 { + keyNode := node.Content[i] + valNode := node.Content[i+1] + if keyNode == nil || valNode == nil { + continue + } + switch strings.ToLower(strings.TrimSpace(keyNode.Value)) { + case "name": + entry.Name = strings.TrimSpace(valNode.Value) + case "alias": + entry.Alias = strings.TrimSpace(valNode.Value) + case "fork": + entry.Fork = strings.ToLower(strings.TrimSpace(valNode.Value)) == "true" + } + } + return entry +} + +// buildOAuthModelAliasNode creates a YAML node for oauth-model-alias +func buildOAuthModelAliasNode(aliases map[string][]OAuthModelAlias) *yaml.Node { + node := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"} + for channel, entries := range aliases { + channelNode := &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: channel} + entriesNode := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"} + for _, entry := range entries { + entryNode := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"} + entryNode.Content = append(entryNode.Content, + &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "name"}, + &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: entry.Name}, + &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "alias"}, + &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: entry.Alias}, + ) + if entry.Fork { + entryNode.Content = append(entryNode.Content, + &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "fork"}, + &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!bool", Value: "true"}, + ) + } + entriesNode.Content = append(entriesNode.Content, entryNode) + } + node.Content = append(node.Content, channelNode, entriesNode) + } + return node +} + +// removeMapKeyByIndex removes a key-value pair from a mapping node by index +func removeMapKeyByIndex(mapNode *yaml.Node, keyIdx int) { + if mapNode == nil || mapNode.Kind != yaml.MappingNode { + return + } + if keyIdx < 0 || keyIdx+1 >= len(mapNode.Content) { + return + } + mapNode.Content = append(mapNode.Content[:keyIdx], mapNode.Content[keyIdx+2:]...) +} + +// writeYAMLNode writes the YAML node tree back to file +func writeYAMLNode(configFile string, root *yaml.Node) (bool, error) { + f, err := os.Create(configFile) + if err != nil { + return false, err + } + defer f.Close() + + enc := yaml.NewEncoder(f) + enc.SetIndent(2) + if err := enc.Encode(root); err != nil { + return false, err + } + if err := enc.Close(); err != nil { + return false, err + } + return true, nil +} diff --git a/internal/config/oauth_model_alias_migration_test.go b/internal/config/oauth_model_alias_migration_test.go new file mode 100644 index 00000000..db9c0a11 --- /dev/null +++ b/internal/config/oauth_model_alias_migration_test.go @@ -0,0 +1,242 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "gopkg.in/yaml.v3" +) + +func TestMigrateOAuthModelAlias_SkipsIfNewFieldExists(t *testing.T) { + t.Parallel() + + dir := t.TempDir() + configFile := filepath.Join(dir, "config.yaml") + + content := `oauth-model-alias: + gemini-cli: + - name: "gemini-2.5-pro" + alias: "g2.5p" +` + if err := os.WriteFile(configFile, []byte(content), 0644); err != nil { + t.Fatal(err) + } + + migrated, err := MigrateOAuthModelAlias(configFile) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if migrated { + t.Fatal("expected no migration when oauth-model-alias already exists") + } + + // Verify file unchanged + data, _ := os.ReadFile(configFile) + if !strings.Contains(string(data), "oauth-model-alias:") { + t.Fatal("file should still contain oauth-model-alias") + } +} + +func TestMigrateOAuthModelAlias_MigratesOldField(t *testing.T) { + t.Parallel() + + dir := t.TempDir() + configFile := filepath.Join(dir, "config.yaml") + + content := `oauth-model-mappings: + gemini-cli: + - name: "gemini-2.5-pro" + alias: "g2.5p" + fork: true +` + if err := os.WriteFile(configFile, []byte(content), 0644); err != nil { + t.Fatal(err) + } + + migrated, err := MigrateOAuthModelAlias(configFile) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !migrated { + t.Fatal("expected migration to occur") + } + + // Verify new field exists and old field removed + data, _ := os.ReadFile(configFile) + if strings.Contains(string(data), "oauth-model-mappings:") { + t.Fatal("old field should be removed") + } + if !strings.Contains(string(data), "oauth-model-alias:") { + t.Fatal("new field should exist") + } + + // Parse and verify structure + var root yaml.Node + if err := yaml.Unmarshal(data, &root); err != nil { + t.Fatal(err) + } +} + +func TestMigrateOAuthModelAlias_ConvertsAntigravityModels(t *testing.T) { + t.Parallel() + + dir := t.TempDir() + configFile := filepath.Join(dir, "config.yaml") + + // Use old model names that should be converted + content := `oauth-model-mappings: + antigravity: + - name: "gemini-2.5-computer-use-preview-10-2025" + alias: "computer-use" + - name: "gemini-3-pro-preview" + alias: "g3p" +` + if err := os.WriteFile(configFile, []byte(content), 0644); err != nil { + t.Fatal(err) + } + + migrated, err := MigrateOAuthModelAlias(configFile) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !migrated { + t.Fatal("expected migration to occur") + } + + // Verify model names were converted + data, _ := os.ReadFile(configFile) + content = string(data) + if !strings.Contains(content, "rev19-uic3-1p") { + t.Fatal("expected gemini-2.5-computer-use-preview-10-2025 to be converted to rev19-uic3-1p") + } + if !strings.Contains(content, "gemini-3-pro-high") { + t.Fatal("expected gemini-3-pro-preview to be converted to gemini-3-pro-high") + } + + // Verify missing default aliases were supplemented + if !strings.Contains(content, "gemini-3-pro-image") { + t.Fatal("expected missing default alias gemini-3-pro-image to be added") + } + if !strings.Contains(content, "gemini-3-flash") { + t.Fatal("expected missing default alias gemini-3-flash to be added") + } + if !strings.Contains(content, "claude-sonnet-4-5") { + t.Fatal("expected missing default alias claude-sonnet-4-5 to be added") + } + if !strings.Contains(content, "claude-sonnet-4-5-thinking") { + t.Fatal("expected missing default alias claude-sonnet-4-5-thinking to be added") + } + if !strings.Contains(content, "claude-opus-4-5-thinking") { + t.Fatal("expected missing default alias claude-opus-4-5-thinking to be added") + } +} + +func TestMigrateOAuthModelAlias_AddsDefaultIfNeitherExists(t *testing.T) { + t.Parallel() + + dir := t.TempDir() + configFile := filepath.Join(dir, "config.yaml") + + content := `debug: true +port: 8080 +` + if err := os.WriteFile(configFile, []byte(content), 0644); err != nil { + t.Fatal(err) + } + + migrated, err := MigrateOAuthModelAlias(configFile) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !migrated { + t.Fatal("expected migration to add default config") + } + + // Verify default antigravity config was added + data, _ := os.ReadFile(configFile) + content = string(data) + if !strings.Contains(content, "oauth-model-alias:") { + t.Fatal("expected oauth-model-alias to be added") + } + if !strings.Contains(content, "antigravity:") { + t.Fatal("expected antigravity channel to be added") + } + if !strings.Contains(content, "rev19-uic3-1p") { + t.Fatal("expected default antigravity aliases to include rev19-uic3-1p") + } +} + +func TestMigrateOAuthModelAlias_PreservesOtherConfig(t *testing.T) { + t.Parallel() + + dir := t.TempDir() + configFile := filepath.Join(dir, "config.yaml") + + content := `debug: true +port: 8080 +oauth-model-mappings: + gemini-cli: + - name: "test" + alias: "t" +api-keys: + - "key1" + - "key2" +` + if err := os.WriteFile(configFile, []byte(content), 0644); err != nil { + t.Fatal(err) + } + + migrated, err := MigrateOAuthModelAlias(configFile) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !migrated { + t.Fatal("expected migration to occur") + } + + // Verify other config preserved + data, _ := os.ReadFile(configFile) + content = string(data) + if !strings.Contains(content, "debug: true") { + t.Fatal("expected debug field to be preserved") + } + if !strings.Contains(content, "port: 8080") { + t.Fatal("expected port field to be preserved") + } + if !strings.Contains(content, "api-keys:") { + t.Fatal("expected api-keys field to be preserved") + } +} + +func TestMigrateOAuthModelAlias_NonexistentFile(t *testing.T) { + t.Parallel() + + migrated, err := MigrateOAuthModelAlias("/nonexistent/path/config.yaml") + if err != nil { + t.Fatalf("unexpected error for nonexistent file: %v", err) + } + if migrated { + t.Fatal("expected no migration for nonexistent file") + } +} + +func TestMigrateOAuthModelAlias_EmptyFile(t *testing.T) { + t.Parallel() + + dir := t.TempDir() + configFile := filepath.Join(dir, "config.yaml") + + if err := os.WriteFile(configFile, []byte(""), 0644); err != nil { + t.Fatal(err) + } + + migrated, err := MigrateOAuthModelAlias(configFile) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if migrated { + t.Fatal("expected no migration for empty file") + } +} diff --git a/internal/config/oauth_model_alias_test.go b/internal/config/oauth_model_alias_test.go new file mode 100644 index 00000000..a5886474 --- /dev/null +++ b/internal/config/oauth_model_alias_test.go @@ -0,0 +1,56 @@ +package config + +import "testing" + +func TestSanitizeOAuthModelAlias_PreservesForkFlag(t *testing.T) { + cfg := &Config{ + OAuthModelAlias: map[string][]OAuthModelAlias{ + " CoDeX ": { + {Name: " gpt-5 ", Alias: " g5 ", Fork: true}, + {Name: "gpt-6", Alias: "g6"}, + }, + }, + } + + cfg.SanitizeOAuthModelAlias() + + aliases := cfg.OAuthModelAlias["codex"] + if len(aliases) != 2 { + t.Fatalf("expected 2 sanitized aliases, got %d", len(aliases)) + } + if aliases[0].Name != "gpt-5" || aliases[0].Alias != "g5" || !aliases[0].Fork { + t.Fatalf("expected first alias to be gpt-5->g5 fork=true, got name=%q alias=%q fork=%v", aliases[0].Name, aliases[0].Alias, aliases[0].Fork) + } + if aliases[1].Name != "gpt-6" || aliases[1].Alias != "g6" || aliases[1].Fork { + t.Fatalf("expected second alias to be gpt-6->g6 fork=false, got name=%q alias=%q fork=%v", aliases[1].Name, aliases[1].Alias, aliases[1].Fork) + } +} + +func TestSanitizeOAuthModelAlias_AllowsMultipleAliasesForSameName(t *testing.T) { + cfg := &Config{ + OAuthModelAlias: map[string][]OAuthModelAlias{ + "antigravity": { + {Name: "gemini-claude-opus-4-5-thinking", Alias: "claude-opus-4-5-20251101", Fork: true}, + {Name: "gemini-claude-opus-4-5-thinking", Alias: "claude-opus-4-5-20251101-thinking", Fork: true}, + {Name: "gemini-claude-opus-4-5-thinking", Alias: "claude-opus-4-5", Fork: true}, + }, + }, + } + + cfg.SanitizeOAuthModelAlias() + + aliases := cfg.OAuthModelAlias["antigravity"] + expected := []OAuthModelAlias{ + {Name: "gemini-claude-opus-4-5-thinking", Alias: "claude-opus-4-5-20251101", Fork: true}, + {Name: "gemini-claude-opus-4-5-thinking", Alias: "claude-opus-4-5-20251101-thinking", Fork: true}, + {Name: "gemini-claude-opus-4-5-thinking", Alias: "claude-opus-4-5", Fork: true}, + } + if len(aliases) != len(expected) { + t.Fatalf("expected %d sanitized aliases, got %d", len(expected), len(aliases)) + } + for i, exp := range expected { + if aliases[i].Name != exp.Name || aliases[i].Alias != exp.Alias || aliases[i].Fork != exp.Fork { + t.Fatalf("expected alias %d to be name=%q alias=%q fork=%v, got name=%q alias=%q fork=%v", i, exp.Name, exp.Alias, exp.Fork, aliases[i].Name, aliases[i].Alias, aliases[i].Fork) + } + } +} diff --git a/internal/logging/global_logger.go b/internal/logging/global_logger.go index 76813c42..30e72766 100644 --- a/internal/logging/global_logger.go +++ b/internal/logging/global_logger.go @@ -179,6 +179,27 @@ func ConfigureLogOutput(cfg *config.Config) error { return nil } +// SetLogLevel sets the global log level based on a level name. +// Supported levels: "quiet"/"silent" (fatal only), "verbose" (debug), or any logrus level string. +// All level names are case-insensitive. Unknown strings default to info level. +func SetLogLevel(level string) { + levelLower := strings.ToLower(level) + switch levelLower { + case "quiet", "silent": + log.SetLevel(log.FatalLevel) + case "verbose": + log.SetLevel(log.DebugLevel) + default: + parsed, err := log.ParseLevel(levelLower) + if err == nil { + log.SetLevel(parsed) + } else { + // Default to info level for unknown strings + log.SetLevel(log.InfoLevel) + } + } +} + func closeLogOutputs() { writerMu.Lock() defer writerMu.Unlock() @@ -198,25 +219,3 @@ func closeLogOutputs() { ginErrorWriter = nil } } - -// SetLogLevel sets the logrus log level based on a string level name. -// Supported levels: debug, verbose, info, warn, warning, error, quiet, silent. -// Unknown levels default to InfoLevel. -func SetLogLevel(level string) { - var newLevel log.Level - switch strings.ToLower(strings.TrimSpace(level)) { - case "debug", "verbose": - newLevel = log.DebugLevel - case "info": - newLevel = log.InfoLevel - case "warn", "warning": - newLevel = log.WarnLevel - case "error": - newLevel = log.ErrorLevel - case "quiet", "silent": - newLevel = log.FatalLevel - default: - newLevel = log.InfoLevel - } - log.SetLevel(newLevel) -} diff --git a/internal/managementasset/updater.go b/internal/managementasset/updater.go new file mode 100644 index 00000000..c941da02 --- /dev/null +++ b/internal/managementasset/updater.go @@ -0,0 +1,468 @@ +package managementasset + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "os" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "time" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + "github.com/router-for-me/CLIProxyAPI/v6/internal/util" + sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" + log "github.com/sirupsen/logrus" +) + +const ( + defaultManagementReleaseURL = "https://api.github.com/repos/router-for-me/Cli-Proxy-API-Management-Center/releases/latest" + defaultManagementFallbackURL = "https://cpamc.router-for.me/" + managementAssetName = "management.html" + httpUserAgent = "CLIProxyAPI-management-updater" + updateCheckInterval = 3 * time.Hour +) + +// ManagementFileName exposes the control panel asset filename. +const ManagementFileName = managementAssetName + +var ( + lastUpdateCheckMu sync.Mutex + lastUpdateCheckTime time.Time + + currentConfigPtr atomic.Pointer[config.Config] + disableControlPanel atomic.Bool + schedulerOnce sync.Once + schedulerConfigPath atomic.Value +) + +// SetCurrentConfig stores the latest configuration snapshot for management asset decisions. +func SetCurrentConfig(cfg *config.Config) { + if cfg == nil { + currentConfigPtr.Store(nil) + return + } + + prevDisabled := disableControlPanel.Load() + currentConfigPtr.Store(cfg) + disableControlPanel.Store(cfg.RemoteManagement.DisableControlPanel) + + if prevDisabled && !cfg.RemoteManagement.DisableControlPanel { + lastUpdateCheckMu.Lock() + lastUpdateCheckTime = time.Time{} + lastUpdateCheckMu.Unlock() + } +} + +// StartAutoUpdater launches a background goroutine that periodically ensures the management asset is up to date. +// It respects the disable-control-panel flag on every iteration and supports hot-reloaded configurations. +func StartAutoUpdater(ctx context.Context, configFilePath string) { + configFilePath = strings.TrimSpace(configFilePath) + if configFilePath == "" { + log.Debug("management asset auto-updater skipped: empty config path") + return + } + + schedulerConfigPath.Store(configFilePath) + + schedulerOnce.Do(func() { + go runAutoUpdater(ctx) + }) +} + +func runAutoUpdater(ctx context.Context) { + if ctx == nil { + ctx = context.Background() + } + + ticker := time.NewTicker(updateCheckInterval) + defer ticker.Stop() + + runOnce := func() { + cfg := currentConfigPtr.Load() + if cfg == nil { + log.Debug("management asset auto-updater skipped: config not yet available") + return + } + if disableControlPanel.Load() { + log.Debug("management asset auto-updater skipped: control panel disabled") + return + } + + configPath, _ := schedulerConfigPath.Load().(string) + staticDir := StaticDir(configPath) + EnsureLatestManagementHTML(ctx, staticDir, cfg.ProxyURL, cfg.RemoteManagement.PanelGitHubRepository) + } + + runOnce() + + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + runOnce() + } + } +} + +func newHTTPClient(proxyURL string) *http.Client { + client := &http.Client{Timeout: 15 * time.Second} + + sdkCfg := &sdkconfig.SDKConfig{ProxyURL: strings.TrimSpace(proxyURL)} + util.SetProxy(sdkCfg, client) + + return client +} + +type releaseAsset struct { + Name string `json:"name"` + BrowserDownloadURL string `json:"browser_download_url"` + Digest string `json:"digest"` +} + +type releaseResponse struct { + Assets []releaseAsset `json:"assets"` +} + +// StaticDir resolves the directory that stores the management control panel asset. +func StaticDir(configFilePath string) string { + if override := strings.TrimSpace(os.Getenv("MANAGEMENT_STATIC_PATH")); override != "" { + cleaned := filepath.Clean(override) + if strings.EqualFold(filepath.Base(cleaned), managementAssetName) { + return filepath.Dir(cleaned) + } + return cleaned + } + + if writable := util.WritablePath(); writable != "" { + return filepath.Join(writable, "static") + } + + configFilePath = strings.TrimSpace(configFilePath) + if configFilePath == "" { + return "" + } + + base := filepath.Dir(configFilePath) + fileInfo, err := os.Stat(configFilePath) + if err == nil { + if fileInfo.IsDir() { + base = configFilePath + } + } + + return filepath.Join(base, "static") +} + +// FilePath resolves the absolute path to the management control panel asset. +func FilePath(configFilePath string) string { + if override := strings.TrimSpace(os.Getenv("MANAGEMENT_STATIC_PATH")); override != "" { + cleaned := filepath.Clean(override) + if strings.EqualFold(filepath.Base(cleaned), managementAssetName) { + return cleaned + } + return filepath.Join(cleaned, ManagementFileName) + } + + dir := StaticDir(configFilePath) + if dir == "" { + return "" + } + return filepath.Join(dir, ManagementFileName) +} + +// EnsureLatestManagementHTML checks the latest management.html asset and updates the local copy when needed. +// The function is designed to run in a background goroutine and will never panic. +// It enforces a 3-hour rate limit to avoid frequent checks on config/auth file changes. +func EnsureLatestManagementHTML(ctx context.Context, staticDir string, proxyURL string, panelRepository string) { + if ctx == nil { + ctx = context.Background() + } + + if disableControlPanel.Load() { + log.Debug("management asset sync skipped: control panel disabled by configuration") + return + } + + staticDir = strings.TrimSpace(staticDir) + if staticDir == "" { + log.Debug("management asset sync skipped: empty static directory") + return + } + + localPath := filepath.Join(staticDir, managementAssetName) + localFileMissing := false + if _, errStat := os.Stat(localPath); errStat != nil { + if errors.Is(errStat, os.ErrNotExist) { + localFileMissing = true + } else { + log.WithError(errStat).Debug("failed to stat local management asset") + } + } + + // Rate limiting: check only once every 3 hours + lastUpdateCheckMu.Lock() + now := time.Now() + timeSinceLastCheck := now.Sub(lastUpdateCheckTime) + if timeSinceLastCheck < updateCheckInterval { + lastUpdateCheckMu.Unlock() + log.Debugf("management asset update check skipped: last check was %v ago (interval: %v)", timeSinceLastCheck.Round(time.Second), updateCheckInterval) + return + } + lastUpdateCheckTime = now + lastUpdateCheckMu.Unlock() + + if errMkdirAll := os.MkdirAll(staticDir, 0o755); errMkdirAll != nil { + log.WithError(errMkdirAll).Warn("failed to prepare static directory for management asset") + return + } + + releaseURL := resolveReleaseURL(panelRepository) + client := newHTTPClient(proxyURL) + + localHash, err := fileSHA256(localPath) + if err != nil { + if !errors.Is(err, os.ErrNotExist) { + log.WithError(err).Debug("failed to read local management asset hash") + } + localHash = "" + } + + asset, remoteHash, err := fetchLatestAsset(ctx, client, releaseURL) + if err != nil { + if localFileMissing { + log.WithError(err).Warn("failed to fetch latest management release information, trying fallback page") + if ensureFallbackManagementHTML(ctx, client, localPath) { + return + } + return + } + log.WithError(err).Warn("failed to fetch latest management release information") + return + } + + if remoteHash != "" && localHash != "" && strings.EqualFold(remoteHash, localHash) { + log.Debug("management asset is already up to date") + return + } + + data, downloadedHash, err := downloadAsset(ctx, client, asset.BrowserDownloadURL) + if err != nil { + if localFileMissing { + log.WithError(err).Warn("failed to download management asset, trying fallback page") + if ensureFallbackManagementHTML(ctx, client, localPath) { + return + } + return + } + log.WithError(err).Warn("failed to download management asset") + return + } + + if remoteHash != "" && !strings.EqualFold(remoteHash, downloadedHash) { + log.Warnf("remote digest mismatch for management asset: expected %s got %s", remoteHash, downloadedHash) + } + + if err = atomicWriteFile(localPath, data); err != nil { + log.WithError(err).Warn("failed to update management asset on disk") + return + } + + log.Infof("management asset updated successfully (hash=%s)", downloadedHash) +} + +func ensureFallbackManagementHTML(ctx context.Context, client *http.Client, localPath string) bool { + data, downloadedHash, err := downloadAsset(ctx, client, defaultManagementFallbackURL) + if err != nil { + log.WithError(err).Warn("failed to download fallback management control panel page") + return false + } + + if err = atomicWriteFile(localPath, data); err != nil { + log.WithError(err).Warn("failed to persist fallback management control panel page") + return false + } + + log.Infof("management asset updated from fallback page successfully (hash=%s)", downloadedHash) + return true +} + +func resolveReleaseURL(repo string) string { + repo = strings.TrimSpace(repo) + if repo == "" { + return defaultManagementReleaseURL + } + + parsed, err := url.Parse(repo) + if err != nil || parsed.Host == "" { + return defaultManagementReleaseURL + } + + host := strings.ToLower(parsed.Host) + parsed.Path = strings.TrimSuffix(parsed.Path, "/") + + if host == "api.github.com" { + if !strings.HasSuffix(strings.ToLower(parsed.Path), "/releases/latest") { + parsed.Path = parsed.Path + "/releases/latest" + } + return parsed.String() + } + + if host == "github.com" { + parts := strings.Split(strings.Trim(parsed.Path, "/"), "/") + if len(parts) >= 2 && parts[0] != "" && parts[1] != "" { + repoName := strings.TrimSuffix(parts[1], ".git") + return fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", parts[0], repoName) + } + } + + return defaultManagementReleaseURL +} + +func fetchLatestAsset(ctx context.Context, client *http.Client, releaseURL string) (*releaseAsset, string, error) { + if strings.TrimSpace(releaseURL) == "" { + releaseURL = defaultManagementReleaseURL + } + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, releaseURL, nil) + if err != nil { + return nil, "", fmt.Errorf("create release request: %w", err) + } + req.Header.Set("Accept", "application/vnd.github+json") + req.Header.Set("User-Agent", httpUserAgent) + gitURL := strings.ToLower(strings.TrimSpace(os.Getenv("GITSTORE_GIT_URL"))) + if tok := strings.TrimSpace(os.Getenv("GITSTORE_GIT_TOKEN")); tok != "" && strings.Contains(gitURL, "github.com") { + req.Header.Set("Authorization", "Bearer "+tok) + } + + resp, err := client.Do(req) + if err != nil { + return nil, "", fmt.Errorf("execute release request: %w", err) + } + defer func() { + _ = resp.Body.Close() + }() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024)) + return nil, "", fmt.Errorf("unexpected release status %d: %s", resp.StatusCode, strings.TrimSpace(string(body))) + } + + var release releaseResponse + if err = json.NewDecoder(resp.Body).Decode(&release); err != nil { + return nil, "", fmt.Errorf("decode release response: %w", err) + } + + for i := range release.Assets { + asset := &release.Assets[i] + if strings.EqualFold(asset.Name, managementAssetName) { + remoteHash := parseDigest(asset.Digest) + return asset, remoteHash, nil + } + } + + return nil, "", fmt.Errorf("management asset %s not found in latest release", managementAssetName) +} + +func downloadAsset(ctx context.Context, client *http.Client, downloadURL string) ([]byte, string, error) { + if strings.TrimSpace(downloadURL) == "" { + return nil, "", fmt.Errorf("empty download url") + } + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil) + if err != nil { + return nil, "", fmt.Errorf("create download request: %w", err) + } + req.Header.Set("User-Agent", httpUserAgent) + + resp, err := client.Do(req) + if err != nil { + return nil, "", fmt.Errorf("execute download request: %w", err) + } + defer func() { + _ = resp.Body.Close() + }() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024)) + return nil, "", fmt.Errorf("unexpected download status %d: %s", resp.StatusCode, strings.TrimSpace(string(body))) + } + + data, err := io.ReadAll(resp.Body) + if err != nil { + return nil, "", fmt.Errorf("read download body: %w", err) + } + + sum := sha256.Sum256(data) + return data, hex.EncodeToString(sum[:]), nil +} + +func fileSHA256(path string) (string, error) { + file, err := os.Open(path) + if err != nil { + return "", err + } + defer func() { + _ = file.Close() + }() + + h := sha256.New() + if _, err = io.Copy(h, file); err != nil { + return "", err + } + + return hex.EncodeToString(h.Sum(nil)), nil +} + +func atomicWriteFile(path string, data []byte) error { + tmpFile, err := os.CreateTemp(filepath.Dir(path), "management-*.html") + if err != nil { + return err + } + + tmpName := tmpFile.Name() + defer func() { + _ = tmpFile.Close() + _ = os.Remove(tmpName) + }() + + if _, err = tmpFile.Write(data); err != nil { + return err + } + + if err = tmpFile.Chmod(0o644); err != nil { + return err + } + + if err = tmpFile.Close(); err != nil { + return err + } + + if err = os.Rename(tmpName, path); err != nil { + return err + } + + return nil +} + +func parseDigest(digest string) string { + digest = strings.TrimSpace(digest) + if digest == "" { + return "" + } + + if idx := strings.Index(digest, ":"); idx >= 0 { + digest = digest[idx+1:] + } + + return strings.ToLower(strings.TrimSpace(digest)) +} diff --git a/internal/misc/codex_instructions.go b/internal/misc/codex_instructions.go index 9e906c06..224398bb 100644 --- a/internal/misc/codex_instructions.go +++ b/internal/misc/codex_instructions.go @@ -5,6 +5,7 @@ package misc import ( "embed" + _ "embed" "strings" "sync/atomic" @@ -12,7 +13,7 @@ import ( "github.com/tidwall/sjson" ) -// codexInstructionsEnabled controls whether CodexInstructionsForModel returns custom instructions. +// codexInstructionsEnabled controls whether CodexInstructionsForModel returns official instructions. // When false (default), CodexInstructionsForModel returns (true, "") immediately. // Set via SetCodexInstructionsEnabled from config. var codexInstructionsEnabled atomic.Bool @@ -38,7 +39,6 @@ const ( userAgentOpenAISDK = "ai-sdk/openai/" ) -// InjectCodexUserAgent injects the user agent into the request payload for later extraction. func InjectCodexUserAgent(raw []byte, userAgent string) []byte { if len(raw) == 0 { return raw @@ -54,7 +54,6 @@ func InjectCodexUserAgent(raw []byte, userAgent string) []byte { return updated } -// ExtractCodexUserAgent extracts the user agent from the request payload. func ExtractCodexUserAgent(raw []byte) string { if len(raw) == 0 { return "" @@ -62,7 +61,6 @@ func ExtractCodexUserAgent(raw []byte) string { return strings.TrimSpace(gjson.GetBytes(raw, codexUserAgentKey).String()) } -// StripCodexUserAgent removes the user agent from the request payload. func StripCodexUserAgent(raw []byte) []byte { if len(raw) == 0 { return raw @@ -91,6 +89,10 @@ func useOpenCodeInstructions(userAgent string) bool { return strings.Contains(strings.ToLower(userAgent), userAgentOpenAISDK) } +func IsOpenCodeUserAgent(userAgent string) bool { + return useOpenCodeInstructions(userAgent) +} + func codexInstructionsForCodex(modelName, systemInstructions string) (bool, string) { entries, _ := codexInstructionsDir.ReadDir("codex_instructions") @@ -100,6 +102,7 @@ func codexInstructionsForCodex(modelName, systemInstructions string) (bool, stri last51Prompt := "" last52Prompt := "" last52CodexPrompt := "" + // lastReviewPrompt := "" for _, entry := range entries { content, _ := codexInstructionsDir.ReadFile("codex_instructions/" + entry.Name()) if strings.HasPrefix(systemInstructions, string(content)) { @@ -117,6 +120,8 @@ func codexInstructionsForCodex(modelName, systemInstructions string) (bool, stri last52Prompt = string(content) } else if strings.HasPrefix(entry.Name(), "gpt-5.2-codex_prompt.md") { last52CodexPrompt = string(content) + } else if strings.HasPrefix(entry.Name(), "review_prompt.md") { + // lastReviewPrompt = string(content) } } if strings.Contains(modelName, "codex-max") { @@ -134,14 +139,11 @@ func codexInstructionsForCodex(modelName, systemInstructions string) (bool, stri } } -// CodexInstructionsForModel returns the appropriate instructions for the given model and user agent. -// For OpenCode clients (identified by user agent), it returns OpenCode-specific instructions. -// For Codex clients, it returns the standard Codex instructions based on model name. func CodexInstructionsForModel(modelName, systemInstructions, userAgent string) (bool, string) { if !GetCodexInstructionsEnabled() { return true, "" } - if useOpenCodeInstructions(userAgent) { + if IsOpenCodeUserAgent(userAgent) { return codexInstructionsForOpenCode(systemInstructions) } return codexInstructionsForCodex(modelName, systemInstructions) diff --git a/internal/registry/model_definitions.go b/internal/registry/model_definitions.go index b27b040a..585bdf8c 100644 --- a/internal/registry/model_definitions.go +++ b/internal/registry/model_definitions.go @@ -1,671 +1,105 @@ -// Package registry provides model definitions for various AI service providers. -// This file contains static model definitions that can be used by clients -// when registering their supported models. +// Package registry provides model definitions and lookup helpers for various AI providers. +// Static model metadata is stored in model_definitions_static_data.go. package registry -// GetClaudeModels returns the standard Claude model definitions -func GetClaudeModels() []*ModelInfo { - return []*ModelInfo{ +import ( + "sort" + "strings" +) - { - ID: "claude-haiku-4-5-20251001", - Object: "model", - Created: 1759276800, // 2025-10-01 - OwnedBy: "anthropic", - Type: "claude", - DisplayName: "Claude 4.5 Haiku", - ContextLength: 200000, - MaxCompletionTokens: 64000, - // Thinking: not supported for Haiku models - }, - { - ID: "claude-sonnet-4-5-20250929", - Object: "model", - Created: 1759104000, // 2025-09-29 - OwnedBy: "anthropic", - Type: "claude", - DisplayName: "Claude 4.5 Sonnet", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 100000, ZeroAllowed: false, DynamicAllowed: true}, - }, - { - ID: "claude-opus-4-5-20251101", - Object: "model", - Created: 1761955200, // 2025-11-01 - OwnedBy: "anthropic", - Type: "claude", - DisplayName: "Claude 4.5 Opus", - Description: "Premium model combining maximum intelligence with practical performance", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 100000, ZeroAllowed: false, DynamicAllowed: true}, - }, - } -} - -// GetGeminiModels returns the standard Gemini model definitions -func GetGeminiModels() []*ModelInfo { - return []*ModelInfo{ - { - ID: "gemini-2.5-pro", - Object: "model", - Created: 1750118400, - OwnedBy: "google", - Type: "gemini", - Name: "models/gemini-2.5-pro", - Version: "2.5", - DisplayName: "Gemini 2.5 Pro", - Description: "Stable release (June 17th, 2025) of Gemini 2.5 Pro", - InputTokenLimit: 1048576, - OutputTokenLimit: 65536, - SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, - Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, - }, - { - ID: "gemini-2.5-flash", - Object: "model", - Created: 1750118400, - OwnedBy: "google", - Type: "gemini", - Name: "models/gemini-2.5-flash", - Version: "001", - DisplayName: "Gemini 2.5 Flash", - Description: "Stable version of Gemini 2.5 Flash, our mid-size multimodal model that supports up to 1 million tokens, released in June of 2025.", - InputTokenLimit: 1048576, - OutputTokenLimit: 65536, - SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, - Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, - }, - { - ID: "gemini-2.5-flash-lite", - Object: "model", - Created: 1753142400, - OwnedBy: "google", - Type: "gemini", - Name: "models/gemini-2.5-flash-lite", - Version: "2.5", - DisplayName: "Gemini 2.5 Flash Lite", - Description: "Our smallest and most cost effective model, built for at scale usage.", - InputTokenLimit: 1048576, - OutputTokenLimit: 65536, - SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, - Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, - }, - { - ID: "gemini-3-pro-preview", - Object: "model", - Created: 1737158400, - OwnedBy: "google", - Type: "gemini", - Name: "models/gemini-3-pro-preview", - Version: "3.0", - DisplayName: "Gemini 3 Pro Preview", - Description: "Gemini 3 Pro Preview", - InputTokenLimit: 1048576, - OutputTokenLimit: 65536, - SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, - Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}, - }, - { - ID: "gemini-3-flash-preview", - Object: "model", - Created: 1765929600, - OwnedBy: "google", - Type: "gemini", - Name: "models/gemini-3-flash-preview", - Version: "3.0", - DisplayName: "Gemini 3 Flash Preview", - Description: "Gemini 3 Flash Preview", - InputTokenLimit: 1048576, - OutputTokenLimit: 65536, - SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, - Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"minimal", "low", "medium", "high"}}, - }, - { - ID: "gemini-3-pro-image-preview", - Object: "model", - Created: 1737158400, - OwnedBy: "google", - Type: "gemini", - Name: "models/gemini-3-pro-image-preview", - Version: "3.0", - DisplayName: "Gemini 3 Pro Image Preview", - Description: "Gemini 3 Pro Image Preview", - InputTokenLimit: 1048576, - OutputTokenLimit: 65536, - SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, - Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}, - }, - } -} - -// GetGeminiVertexModels returns Gemini and Imagen models for Vertex AI -func GetGeminiVertexModels() []*ModelInfo { - // Start with base Gemini models - models := GetGeminiModels() - - // Add Imagen image generation models for Vertex AI - imagenModels := []*ModelInfo{ - { - ID: "imagen-4.0-generate-001", - Object: "model", - Created: 1750000000, - OwnedBy: "google", - Type: "gemini", - Name: "models/imagen-4.0-generate-001", - Version: "4.0", - DisplayName: "Imagen 4.0 Generate", - Description: "Imagen 4.0 image generation model", - SupportedGenerationMethods: []string{"predict"}, - }, - { - ID: "imagen-4.0-ultra-generate-001", - Object: "model", - Created: 1750000000, - OwnedBy: "google", - Type: "gemini", - Name: "models/imagen-4.0-ultra-generate-001", - Version: "4.0", - DisplayName: "Imagen 4.0 Ultra Generate", - Description: "Imagen 4.0 Ultra high-quality image generation model", - SupportedGenerationMethods: []string{"predict"}, - }, - { - ID: "imagen-4.0-fast-generate-001", - Object: "model", - Created: 1750000000, - OwnedBy: "google", - Type: "gemini", - Name: "models/imagen-4.0-fast-generate-001", - Version: "4.0", - DisplayName: "Imagen 4.0 Fast Generate", - Description: "Imagen 4.0 fast image generation model", - SupportedGenerationMethods: []string{"predict"}, - }, - { - ID: "imagen-3.0-generate-002", - Object: "model", - Created: 1740000000, - OwnedBy: "google", - Type: "gemini", - Name: "models/imagen-3.0-generate-002", - Version: "3.0", - DisplayName: "Imagen 3.0 Generate", - Description: "Imagen 3.0 image generation model", - SupportedGenerationMethods: []string{"predict"}, - }, - { - ID: "imagen-3.0-fast-generate-001", - Object: "model", - Created: 1740000000, - OwnedBy: "google", - Type: "gemini", - Name: "models/imagen-3.0-fast-generate-001", - Version: "3.0", - DisplayName: "Imagen 3.0 Fast Generate", - Description: "Imagen 3.0 fast image generation model", - SupportedGenerationMethods: []string{"predict"}, - }, - } - return append(models, imagenModels...) -} - -// GetGeminiCLIModels returns GetGeminiModels - consolidated -func GetGeminiCLIModels() []*ModelInfo { - return GetGeminiModels() -} - -// GetAIStudioModels returns the Gemini model definitions for AI Studio integrations -// Includes base models plus -latest aliases specific to AI Studio -func GetAIStudioModels() []*ModelInfo { - // Start with base Gemini models - models := GetGeminiModels() - - // Add AI Studio specific -latest aliases - now := int64(1750118400) - aiStudioExtras := []*ModelInfo{ - { - ID: "gemini-pro-latest", - Object: "model", - Created: now, - OwnedBy: "google", - Type: "gemini", - Name: "models/gemini-pro-latest", - Version: "2.5", - DisplayName: "Gemini Pro Latest", - Description: "Latest release of Gemini Pro", - InputTokenLimit: 1048576, - OutputTokenLimit: 65536, - SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, - Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, - }, - { - ID: "gemini-flash-latest", - Object: "model", - Created: now, - OwnedBy: "google", - Type: "gemini", - Name: "models/gemini-flash-latest", - Version: "2.5", - DisplayName: "Gemini Flash Latest", - Description: "Latest release of Gemini Flash", - InputTokenLimit: 1048576, - OutputTokenLimit: 65536, - SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, - Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, - }, - } - return append(models, aiStudioExtras...) -} - -// GetOpenAIModels returns the standard OpenAI model definitions -func GetOpenAIModels() []*ModelInfo { - return []*ModelInfo{ - { - ID: "gpt-5.2", - Object: "model", - Created: 1765440000, - OwnedBy: "openai", - Type: "openai", - Version: "gpt-5.2", - DisplayName: "GPT 5.2", - Description: "Stable version of GPT 5.2", - ContextLength: 400000, - MaxCompletionTokens: 128000, - SupportedParameters: []string{"tools"}, - Thinking: &ThinkingSupport{Levels: []string{"none", "low", "medium", "high", "xhigh"}}, - }, - { - ID: "gpt-5.2-codex", - Object: "model", - Created: 1765440000, - OwnedBy: "openai", - Type: "openai", - Version: "gpt-5.2", - DisplayName: "GPT 5.2 Codex", - Description: "Stable version of GPT 5.2 Codex, The best model for coding and agentic tasks across domains.", - ContextLength: 400000, - MaxCompletionTokens: 128000, - SupportedParameters: []string{"tools"}, - Thinking: &ThinkingSupport{Levels: []string{"low", "medium", "high", "xhigh"}}, - }, - } -} - -// GetQwenModels returns the standard Qwen model definitions -func GetQwenModels() []*ModelInfo { - return []*ModelInfo{ - { - ID: "qwen3-coder-plus", - Object: "model", - Created: 1753228800, - OwnedBy: "qwen", - Type: "qwen", - Version: "3.0", - DisplayName: "Qwen3 Coder Plus", - Description: "Advanced code generation and understanding model", - ContextLength: 32768, - MaxCompletionTokens: 8192, - SupportedParameters: []string{"temperature", "top_p", "max_tokens", "stream", "stop"}, - }, - { - ID: "qwen3-coder-flash", - Object: "model", - Created: 1753228800, - OwnedBy: "qwen", - Type: "qwen", - Version: "3.0", - DisplayName: "Qwen3 Coder Flash", - Description: "Fast code generation model", - ContextLength: 8192, - MaxCompletionTokens: 2048, - SupportedParameters: []string{"temperature", "top_p", "max_tokens", "stream", "stop"}, - }, - } -} - -// GetMiniMaxModels returns supported models for MiniMax API key accounts. -func GetMiniMaxModels() []*ModelInfo { - entries := []struct { - ID string - DisplayName string - Description string - Created int64 - Thinking *ThinkingSupport - }{ - {ID: "minimax-m2", DisplayName: "MiniMax-M2", Description: "MiniMax M2 base model", Created: 1758672000}, - {ID: "minimax-m2.1", DisplayName: "MiniMax-M2.1", Description: "MiniMax M2.1 with reasoning", Created: 1766448000, Thinking: &ThinkingSupport{Levels: []string{"none", "auto", "low", "medium", "high"}}}, - {ID: "minimax-m2.1-lightning", DisplayName: "MiniMax-M2.1-Lightning", Description: "MiniMax M2.1 fast variant", Created: 1766448000}, - } - models := make([]*ModelInfo, 0, len(entries)) - for _, entry := range entries { - models = append(models, &ModelInfo{ - ID: entry.ID, - Object: "model", - Created: entry.Created, - OwnedBy: "minimax", - Type: "minimax", - DisplayName: entry.DisplayName, - Description: entry.Description, - Thinking: entry.Thinking, +// GetStaticModelDefinitionsByChannel returns static model definitions for a given channel/provider. +// It returns nil when the channel is unknown. +// +// Supported channels: +// - claude +// - gemini +// - vertex +// - gemini-cli +// - aistudio +// - codex +// - qwen +// - iflow +// - antigravity (returns static overrides only) +func GetStaticModelDefinitionsByChannel(channel string) []*ModelInfo { + key := strings.ToLower(strings.TrimSpace(channel)) + switch key { + case "claude": + return GetClaudeModels() + case "gemini": + return GetGeminiModels() + case "vertex": + return GetGeminiVertexModels() + case "gemini-cli": + return GetGeminiCLIModels() + case "aistudio": + return GetAIStudioModels() + case "codex": + return GetOpenAIModels() + case "qwen": + return GetQwenModels() + case "iflow": + return GetIFlowModels() + case "antigravity": + cfg := GetAntigravityModelConfig() + if len(cfg) == 0 { + return nil + } + models := make([]*ModelInfo, 0, len(cfg)) + for modelID, entry := range cfg { + if modelID == "" || entry == nil { + continue + } + models = append(models, &ModelInfo{ + ID: modelID, + Object: "model", + OwnedBy: "antigravity", + Type: "antigravity", + Thinking: entry.Thinking, + MaxCompletionTokens: entry.MaxCompletionTokens, + }) + } + sort.Slice(models, func(i, j int) bool { + return strings.ToLower(models[i].ID) < strings.ToLower(models[j].ID) }) - } - return models -} - -// GetZhipuModels returns supported models for Zhipu AI (GLM) API key accounts. -func GetZhipuModels() []*ModelInfo { - entries := []struct { - ID string - DisplayName string - Description string - Created int64 - Thinking *ThinkingSupport - }{ - {ID: "glm-4.7", DisplayName: "GLM-4.7", Description: "Zhipu GLM 4.7 flagship model with thinking", Created: 1766448000, Thinking: &ThinkingSupport{Levels: []string{"none", "auto", "low", "medium", "high"}}}, - {ID: "glm-4.6", DisplayName: "GLM-4.6", Description: "Zhipu GLM 4.6 high performance model", Created: 1759190400, Thinking: &ThinkingSupport{Levels: []string{"none", "auto", "low", "medium", "high"}}}, - {ID: "glm-4.5", DisplayName: "GLM-4.5", Description: "Zhipu GLM 4.5 excellent for coding", Created: 1752192000}, - {ID: "glm-4-long", DisplayName: "GLM-4-Long", Description: "Zhipu GLM 4 with 1M context", Created: 1745280000}, - {ID: "glm-4.6v", DisplayName: "GLM-4.6V", Description: "Zhipu GLM 4.6 vision model", Created: 1759190400}, - } - models := make([]*ModelInfo, 0, len(entries)) - for _, entry := range entries { - models = append(models, &ModelInfo{ - ID: entry.ID, - Object: "model", - Created: entry.Created, - OwnedBy: "zhipu", - Type: "zhipu", - DisplayName: entry.DisplayName, - Description: entry.Description, - Thinking: entry.Thinking, - }) - } - return models -} - -// AntigravityModelConfig captures static antigravity model overrides, including -// Thinking budget limits and provider max completion tokens. -type AntigravityModelConfig struct { - Thinking *ThinkingSupport - MaxCompletionTokens int -} - -// GetAntigravityModelConfig returns static configuration for antigravity models. -// Keys use the ALIASED model names (after modelName2Alias conversion) for direct lookup. -func GetAntigravityModelConfig() map[string]*AntigravityModelConfig { - return map[string]*AntigravityModelConfig{ - "gemini-2.5-flash": {Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}}, - "gemini-2.5-flash-lite": {Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}}, - "gemini-2.5-computer-use-preview-10-2025": {}, - "rev19-uic3-1p": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}}, - "gemini-3-pro-preview": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}}, - "gemini-3-pro-high": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}}, - "gemini-3-pro-image-preview": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}}, - "gemini-3-pro-image": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}}, - "gemini-3-flash-preview": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"minimal", "low", "medium", "high"}}}, - "gemini-3-flash": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"minimal", "low", "medium", "high"}}}, - "claude-sonnet-4-5-thinking": {Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: true, DynamicAllowed: true}, MaxCompletionTokens: 64000}, - "claude-opus-4-5-thinking": {Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: true, DynamicAllowed: true}, MaxCompletionTokens: 64000}, - "gemini-claude-sonnet-4-5-thinking": {Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: true, DynamicAllowed: true}, MaxCompletionTokens: 64000}, - "gemini-claude-opus-4-5-thinking": {Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: true, DynamicAllowed: true}, MaxCompletionTokens: 64000}, - "claude-sonnet-4-5": {MaxCompletionTokens: 64000}, - "gpt-oss-120b-medium": {}, - "tab_flash_lite_preview": {}, + return models + default: + return nil } } -// GetGitHubCopilotModels returns the available models for GitHub Copilot. -// These models are available through the GitHub Copilot API at api.githubcopilot.com. -func GetGitHubCopilotModels() []*ModelInfo { - now := int64(1732752000) // 2024-11-27 - return []*ModelInfo{ - { - ID: "gpt-5.2", - Object: "model", - Created: now, - OwnedBy: "github-copilot", - Type: "github-copilot", - DisplayName: "GPT-5.2", - Description: "OpenAI GPT-5.2 via GitHub Copilot", - ContextLength: 200000, - MaxCompletionTokens: 32768, - }, - { - ID: "claude-haiku-4.5", - Object: "model", - Created: now, - OwnedBy: "github-copilot", - Type: "github-copilot", - DisplayName: "Claude Haiku 4.5", - Description: "Anthropic Claude Haiku 4.5 via GitHub Copilot", - ContextLength: 200000, - MaxCompletionTokens: 64000, - }, - { - ID: "claude-sonnet-4.5", - Object: "model", - Created: now, - OwnedBy: "github-copilot", - Type: "github-copilot", - DisplayName: "Claude Sonnet 4.5", - Description: "Anthropic Claude Sonnet 4.5 via GitHub Copilot", - ContextLength: 200000, - MaxCompletionTokens: 64000, - }, - { - ID: "claude-opus-4.5", - Object: "model", - Created: now, - OwnedBy: "github-copilot", - Type: "github-copilot", - DisplayName: "Claude Opus 4.5", - Description: "Anthropic Claude Opus 4.5 via GitHub Copilot", - ContextLength: 200000, - MaxCompletionTokens: 64000, - }, - { - ID: "gemini-2.5-pro", - Object: "model", - Created: now, - OwnedBy: "github-copilot", - Type: "github-copilot", - DisplayName: "Gemini 2.5 Pro", - Description: "Google Gemini 2.5 Pro via GitHub Copilot", - ContextLength: 1048576, - MaxCompletionTokens: 65536, - }, - { - ID: "gemini-3-pro", - Object: "model", - Created: now, - OwnedBy: "github-copilot", - Type: "github-copilot", - DisplayName: "Gemini 3 Pro", - Description: "Google Gemini 3 Pro via GitHub Copilot", - ContextLength: 1048576, - MaxCompletionTokens: 65536, - }, +// LookupStaticModelInfo searches all static model definitions for a model by ID. +// Returns nil if no matching model is found. +func LookupStaticModelInfo(modelID string) *ModelInfo { + if modelID == "" { + return nil } -} -// GetKiroModels returns the Kiro (AWS CodeWhisperer) model definitions -func GetKiroModels() []*ModelInfo { - return []*ModelInfo{ - // --- Base Models --- - { - ID: "kiro-claude-opus-4-5", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Opus 4.5", - Description: "Claude Opus 4.5 via Kiro (2.2x credit)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 32000, ZeroAllowed: true, DynamicAllowed: true}, - }, - { - ID: "kiro-claude-sonnet-4-5", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Sonnet 4.5", - Description: "Claude Sonnet 4.5 via Kiro (1.3x credit)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 32000, ZeroAllowed: true, DynamicAllowed: true}, - }, - { - ID: "kiro-claude-sonnet-4", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Sonnet 4", - Description: "Claude Sonnet 4 via Kiro (1.3x credit)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 32000, ZeroAllowed: true, DynamicAllowed: true}, - }, - { - ID: "kiro-claude-haiku-4-5", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Haiku 4.5", - Description: "Claude Haiku 4.5 via Kiro (0.4x credit)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 32000, ZeroAllowed: true, DynamicAllowed: true}, - }, - // --- Thinking Variants (Extended thinking enabled, ZeroAllowed=false forces thinking) --- - { - ID: "kiro-claude-opus-4-5-thinking", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Opus 4.5 (Thinking)", - Description: "Claude Opus 4.5 via Kiro with extended thinking enabled", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: false, DynamicAllowed: true}, - }, - { - ID: "kiro-claude-sonnet-4-5-thinking", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Sonnet 4.5 (Thinking)", - Description: "Claude Sonnet 4.5 via Kiro with extended thinking enabled", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: false, DynamicAllowed: true}, - }, - // --- Agentic Variants (Optimized for coding agents with chunked writes) --- - { - ID: "kiro-claude-opus-4-5-agentic", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Opus 4.5 (Agentic)", - Description: "Claude Opus 4.5 optimized for coding agents (chunked writes)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 32000, ZeroAllowed: true, DynamicAllowed: true}, - }, - { - ID: "kiro-claude-sonnet-4-5-agentic", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Sonnet 4.5 (Agentic)", - Description: "Claude Sonnet 4.5 optimized for coding agents (chunked writes)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 32000, ZeroAllowed: true, DynamicAllowed: true}, - }, - { - ID: "kiro-claude-sonnet-4-agentic", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Sonnet 4 (Agentic)", - Description: "Claude Sonnet 4 optimized for coding agents (chunked writes)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 32000, ZeroAllowed: true, DynamicAllowed: true}, - }, - { - ID: "kiro-claude-haiku-4-5-agentic", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Kiro Claude Haiku 4.5 (Agentic)", - Description: "Claude Haiku 4.5 optimized for coding agents (chunked writes)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - Thinking: &ThinkingSupport{Min: 1024, Max: 32000, ZeroAllowed: true, DynamicAllowed: true}, - }, + allModels := [][]*ModelInfo{ + GetClaudeModels(), + GetGeminiModels(), + GetGeminiVertexModels(), + GetGeminiCLIModels(), + GetAIStudioModels(), + GetOpenAIModels(), + GetQwenModels(), + GetIFlowModels(), + } + for _, models := range allModels { + for _, m := range models { + if m != nil && m.ID == modelID { + return m + } + } } -} -// GetAmazonQModels returns the Amazon Q (AWS CodeWhisperer) model definitions. -// These models use the same API as Kiro and share the same executor. -func GetAmazonQModels() []*ModelInfo { - return []*ModelInfo{ - { - ID: "amazonq-claude-opus-4.5", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Amazon Q Claude Opus 4.5", - Description: "Claude Opus 4.5 via Amazon Q (2.2x credit)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - }, - { - ID: "amazonq-claude-sonnet-4.5", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Amazon Q Claude Sonnet 4.5", - Description: "Claude Sonnet 4.5 via Amazon Q (1.3x credit)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - }, - { - ID: "amazonq-claude-sonnet-4", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Amazon Q Claude Sonnet 4", - Description: "Claude Sonnet 4 via Amazon Q (1.3x credit)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - }, - { - ID: "amazonq-claude-haiku-4.5", - Object: "model", - Created: 1732752000, - OwnedBy: "aws", - Type: "kiro", - DisplayName: "Amazon Q Claude Haiku 4.5", - Description: "Claude Haiku 4.5 via Amazon Q (0.4x credit)", - ContextLength: 200000, - MaxCompletionTokens: 64000, - }, + // Check Antigravity static config + if cfg := GetAntigravityModelConfig()[modelID]; cfg != nil { + return &ModelInfo{ + ID: modelID, + Thinking: cfg.Thinking, + MaxCompletionTokens: cfg.MaxCompletionTokens, + } } + + return nil } diff --git a/internal/registry/model_definitions_static_data.go b/internal/registry/model_definitions_static_data.go new file mode 100644 index 00000000..79a1f0ed --- /dev/null +++ b/internal/registry/model_definitions_static_data.go @@ -0,0 +1,881 @@ +// Package registry provides model definitions for various AI service providers. +// This file stores the static model metadata catalog. +package registry + +// GetClaudeModels returns the standard Claude model definitions +func GetClaudeModels() []*ModelInfo { + return []*ModelInfo{ + + { + ID: "claude-haiku-4-5-20251001", + Object: "model", + Created: 1759276800, // 2025-10-01 + OwnedBy: "anthropic", + Type: "claude", + DisplayName: "Claude 4.5 Haiku", + ContextLength: 200000, + MaxCompletionTokens: 64000, + // Thinking: not supported for Haiku models + }, + { + ID: "claude-sonnet-4-5-20250929", + Object: "model", + Created: 1759104000, // 2025-09-29 + OwnedBy: "anthropic", + Type: "claude", + DisplayName: "Claude 4.5 Sonnet", + ContextLength: 200000, + MaxCompletionTokens: 64000, + Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: true, DynamicAllowed: false}, + }, + { + ID: "claude-opus-4-5-20251101", + Object: "model", + Created: 1761955200, // 2025-11-01 + OwnedBy: "anthropic", + Type: "claude", + DisplayName: "Claude 4.5 Opus", + Description: "Premium model combining maximum intelligence with practical performance", + ContextLength: 200000, + MaxCompletionTokens: 64000, + Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: true, DynamicAllowed: false}, + }, + { + ID: "claude-opus-4-1-20250805", + Object: "model", + Created: 1722945600, // 2025-08-05 + OwnedBy: "anthropic", + Type: "claude", + DisplayName: "Claude 4.1 Opus", + ContextLength: 200000, + MaxCompletionTokens: 32000, + Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: false, DynamicAllowed: false}, + }, + { + ID: "claude-opus-4-20250514", + Object: "model", + Created: 1715644800, // 2025-05-14 + OwnedBy: "anthropic", + Type: "claude", + DisplayName: "Claude 4 Opus", + ContextLength: 200000, + MaxCompletionTokens: 32000, + Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: false, DynamicAllowed: false}, + }, + { + ID: "claude-sonnet-4-20250514", + Object: "model", + Created: 1715644800, // 2025-05-14 + OwnedBy: "anthropic", + Type: "claude", + DisplayName: "Claude 4 Sonnet", + ContextLength: 200000, + MaxCompletionTokens: 64000, + Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: false, DynamicAllowed: false}, + }, + { + ID: "claude-3-7-sonnet-20250219", + Object: "model", + Created: 1708300800, // 2025-02-19 + OwnedBy: "anthropic", + Type: "claude", + DisplayName: "Claude 3.7 Sonnet", + ContextLength: 128000, + MaxCompletionTokens: 8192, + Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: false, DynamicAllowed: false}, + }, + { + ID: "claude-3-5-haiku-20241022", + Object: "model", + Created: 1729555200, // 2024-10-22 + OwnedBy: "anthropic", + Type: "claude", + DisplayName: "Claude 3.5 Haiku", + ContextLength: 128000, + MaxCompletionTokens: 8192, + // Thinking: not supported for Haiku models + }, + } +} + +// GetGeminiModels returns the standard Gemini model definitions +func GetGeminiModels() []*ModelInfo { + return []*ModelInfo{ + { + ID: "gemini-2.5-pro", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-pro", + Version: "2.5", + DisplayName: "Gemini 2.5 Pro", + Description: "Stable release (June 17th, 2025) of Gemini 2.5 Pro", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, + }, + { + ID: "gemini-2.5-flash", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash", + Version: "001", + DisplayName: "Gemini 2.5 Flash", + Description: "Stable version of Gemini 2.5 Flash, our mid-size multimodal model that supports up to 1 million tokens, released in June of 2025.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-2.5-flash-lite", + Object: "model", + Created: 1753142400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash-lite", + Version: "2.5", + DisplayName: "Gemini 2.5 Flash Lite", + Description: "Our smallest and most cost effective model, built for at scale usage.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-3-pro-preview", + Object: "model", + Created: 1737158400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-pro-preview", + Version: "3.0", + DisplayName: "Gemini 3 Pro Preview", + Description: "Gemini 3 Pro Preview", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}, + }, + { + ID: "gemini-3-flash-preview", + Object: "model", + Created: 1765929600, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-flash-preview", + Version: "3.0", + DisplayName: "Gemini 3 Flash Preview", + Description: "Gemini 3 Flash Preview", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"minimal", "low", "medium", "high"}}, + }, + { + ID: "gemini-3-pro-image-preview", + Object: "model", + Created: 1737158400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-pro-image-preview", + Version: "3.0", + DisplayName: "Gemini 3 Pro Image Preview", + Description: "Gemini 3 Pro Image Preview", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}, + }, + } +} + +func GetGeminiVertexModels() []*ModelInfo { + return []*ModelInfo{ + { + ID: "gemini-2.5-pro", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-pro", + Version: "2.5", + DisplayName: "Gemini 2.5 Pro", + Description: "Stable release (June 17th, 2025) of Gemini 2.5 Pro", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, + }, + { + ID: "gemini-2.5-flash", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash", + Version: "001", + DisplayName: "Gemini 2.5 Flash", + Description: "Stable version of Gemini 2.5 Flash, our mid-size multimodal model that supports up to 1 million tokens, released in June of 2025.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-2.5-flash-lite", + Object: "model", + Created: 1753142400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash-lite", + Version: "2.5", + DisplayName: "Gemini 2.5 Flash Lite", + Description: "Our smallest and most cost effective model, built for at scale usage.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-3-pro-preview", + Object: "model", + Created: 1737158400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-pro-preview", + Version: "3.0", + DisplayName: "Gemini 3 Pro Preview", + Description: "Gemini 3 Pro Preview", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}, + }, + { + ID: "gemini-3-flash-preview", + Object: "model", + Created: 1765929600, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-flash-preview", + Version: "3.0", + DisplayName: "Gemini 3 Flash Preview", + Description: "Our most intelligent model built for speed, combining frontier intelligence with superior search and grounding.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"minimal", "low", "medium", "high"}}, + }, + { + ID: "gemini-3-pro-image-preview", + Object: "model", + Created: 1737158400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-pro-image-preview", + Version: "3.0", + DisplayName: "Gemini 3 Pro Image Preview", + Description: "Gemini 3 Pro Image Preview", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}, + }, + // Imagen image generation models - use :predict action + { + ID: "imagen-4.0-generate-001", + Object: "model", + Created: 1750000000, + OwnedBy: "google", + Type: "gemini", + Name: "models/imagen-4.0-generate-001", + Version: "4.0", + DisplayName: "Imagen 4.0 Generate", + Description: "Imagen 4.0 image generation model", + SupportedGenerationMethods: []string{"predict"}, + }, + { + ID: "imagen-4.0-ultra-generate-001", + Object: "model", + Created: 1750000000, + OwnedBy: "google", + Type: "gemini", + Name: "models/imagen-4.0-ultra-generate-001", + Version: "4.0", + DisplayName: "Imagen 4.0 Ultra Generate", + Description: "Imagen 4.0 Ultra high-quality image generation model", + SupportedGenerationMethods: []string{"predict"}, + }, + { + ID: "imagen-3.0-generate-002", + Object: "model", + Created: 1740000000, + OwnedBy: "google", + Type: "gemini", + Name: "models/imagen-3.0-generate-002", + Version: "3.0", + DisplayName: "Imagen 3.0 Generate", + Description: "Imagen 3.0 image generation model", + SupportedGenerationMethods: []string{"predict"}, + }, + { + ID: "imagen-3.0-fast-generate-001", + Object: "model", + Created: 1740000000, + OwnedBy: "google", + Type: "gemini", + Name: "models/imagen-3.0-fast-generate-001", + Version: "3.0", + DisplayName: "Imagen 3.0 Fast Generate", + Description: "Imagen 3.0 fast image generation model", + SupportedGenerationMethods: []string{"predict"}, + }, + { + ID: "imagen-4.0-fast-generate-001", + Object: "model", + Created: 1750000000, + OwnedBy: "google", + Type: "gemini", + Name: "models/imagen-4.0-fast-generate-001", + Version: "4.0", + DisplayName: "Imagen 4.0 Fast Generate", + Description: "Imagen 4.0 fast image generation model", + SupportedGenerationMethods: []string{"predict"}, + }, + } +} + +// GetGeminiCLIModels returns the standard Gemini model definitions +func GetGeminiCLIModels() []*ModelInfo { + return []*ModelInfo{ + { + ID: "gemini-2.5-pro", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-pro", + Version: "2.5", + DisplayName: "Gemini 2.5 Pro", + Description: "Stable release (June 17th, 2025) of Gemini 2.5 Pro", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, + }, + { + ID: "gemini-2.5-flash", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash", + Version: "001", + DisplayName: "Gemini 2.5 Flash", + Description: "Stable version of Gemini 2.5 Flash, our mid-size multimodal model that supports up to 1 million tokens, released in June of 2025.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-2.5-flash-lite", + Object: "model", + Created: 1753142400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash-lite", + Version: "2.5", + DisplayName: "Gemini 2.5 Flash Lite", + Description: "Our smallest and most cost effective model, built for at scale usage.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-3-pro-preview", + Object: "model", + Created: 1737158400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-pro-preview", + Version: "3.0", + DisplayName: "Gemini 3 Pro Preview", + Description: "Our most intelligent model with SOTA reasoning and multimodal understanding, and powerful agentic and vibe coding capabilities", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}, + }, + { + ID: "gemini-3-flash-preview", + Object: "model", + Created: 1765929600, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-flash-preview", + Version: "3.0", + DisplayName: "Gemini 3 Flash Preview", + Description: "Our most intelligent model built for speed, combining frontier intelligence with superior search and grounding.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"minimal", "low", "medium", "high"}}, + }, + } +} + +// GetAIStudioModels returns the Gemini model definitions for AI Studio integrations +func GetAIStudioModels() []*ModelInfo { + return []*ModelInfo{ + { + ID: "gemini-2.5-pro", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-pro", + Version: "2.5", + DisplayName: "Gemini 2.5 Pro", + Description: "Stable release (June 17th, 2025) of Gemini 2.5 Pro", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, + }, + { + ID: "gemini-2.5-flash", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash", + Version: "001", + DisplayName: "Gemini 2.5 Flash", + Description: "Stable version of Gemini 2.5 Flash, our mid-size multimodal model that supports up to 1 million tokens, released in June of 2025.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-2.5-flash-lite", + Object: "model", + Created: 1753142400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash-lite", + Version: "2.5", + DisplayName: "Gemini 2.5 Flash Lite", + Description: "Our smallest and most cost effective model, built for at scale usage.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-3-pro-preview", + Object: "model", + Created: 1737158400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-pro-preview", + Version: "3.0", + DisplayName: "Gemini 3 Pro Preview", + Description: "Gemini 3 Pro Preview", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, + }, + { + ID: "gemini-3-flash-preview", + Object: "model", + Created: 1765929600, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-3-flash-preview", + Version: "3.0", + DisplayName: "Gemini 3 Flash Preview", + Description: "Our most intelligent model built for speed, combining frontier intelligence with superior search and grounding.", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, + }, + { + ID: "gemini-pro-latest", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-pro-latest", + Version: "2.5", + DisplayName: "Gemini Pro Latest", + Description: "Latest release of Gemini Pro", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}, + }, + { + ID: "gemini-flash-latest", + Object: "model", + Created: 1750118400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-flash-latest", + Version: "2.5", + DisplayName: "Gemini Flash Latest", + Description: "Latest release of Gemini Flash", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + { + ID: "gemini-flash-lite-latest", + Object: "model", + Created: 1753142400, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-flash-lite-latest", + Version: "2.5", + DisplayName: "Gemini Flash-Lite Latest", + Description: "Latest release of Gemini Flash-Lite", + InputTokenLimit: 1048576, + OutputTokenLimit: 65536, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + Thinking: &ThinkingSupport{Min: 512, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}, + }, + // { + // ID: "gemini-2.5-flash-image-preview", + // Object: "model", + // Created: 1756166400, + // OwnedBy: "google", + // Type: "gemini", + // Name: "models/gemini-2.5-flash-image-preview", + // Version: "2.5", + // DisplayName: "Gemini 2.5 Flash Image Preview", + // Description: "State-of-the-art image generation and editing model.", + // InputTokenLimit: 1048576, + // OutputTokenLimit: 8192, + // SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + // // image models don't support thinkingConfig; leave Thinking nil + // }, + { + ID: "gemini-2.5-flash-image", + Object: "model", + Created: 1759363200, + OwnedBy: "google", + Type: "gemini", + Name: "models/gemini-2.5-flash-image", + Version: "2.5", + DisplayName: "Gemini 2.5 Flash Image", + Description: "State-of-the-art image generation and editing model.", + InputTokenLimit: 1048576, + OutputTokenLimit: 8192, + SupportedGenerationMethods: []string{"generateContent", "countTokens", "createCachedContent", "batchGenerateContent"}, + // image models don't support thinkingConfig; leave Thinking nil + }, + } +} + +// GetOpenAIModels returns the standard OpenAI model definitions +func GetOpenAIModels() []*ModelInfo { + return []*ModelInfo{ + { + ID: "gpt-5", + Object: "model", + Created: 1754524800, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5-2025-08-07", + DisplayName: "GPT 5", + Description: "Stable version of GPT 5, The best model for coding and agentic tasks across domains.", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"minimal", "low", "medium", "high"}}, + }, + { + ID: "gpt-5-codex", + Object: "model", + Created: 1757894400, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5-2025-09-15", + DisplayName: "GPT 5 Codex", + Description: "Stable version of GPT 5 Codex, The best model for coding and agentic tasks across domains.", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"low", "medium", "high"}}, + }, + { + ID: "gpt-5-codex-mini", + Object: "model", + Created: 1762473600, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5-2025-11-07", + DisplayName: "GPT 5 Codex Mini", + Description: "Stable version of GPT 5 Codex Mini: cheaper, faster, but less capable version of GPT 5 Codex.", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"low", "medium", "high"}}, + }, + { + ID: "gpt-5.1", + Object: "model", + Created: 1762905600, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5.1-2025-11-12", + DisplayName: "GPT 5", + Description: "Stable version of GPT 5, The best model for coding and agentic tasks across domains.", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"none", "low", "medium", "high"}}, + }, + { + ID: "gpt-5.1-codex", + Object: "model", + Created: 1762905600, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5.1-2025-11-12", + DisplayName: "GPT 5.1 Codex", + Description: "Stable version of GPT 5.1 Codex, The best model for coding and agentic tasks across domains.", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"low", "medium", "high"}}, + }, + { + ID: "gpt-5.1-codex-mini", + Object: "model", + Created: 1762905600, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5.1-2025-11-12", + DisplayName: "GPT 5.1 Codex Mini", + Description: "Stable version of GPT 5.1 Codex Mini: cheaper, faster, but less capable version of GPT 5.1 Codex.", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"low", "medium", "high"}}, + }, + { + ID: "gpt-5.1-codex-max", + Object: "model", + Created: 1763424000, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5.1-max", + DisplayName: "GPT 5.1 Codex Max", + Description: "Stable version of GPT 5.1 Codex Max", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"low", "medium", "high", "xhigh"}}, + }, + { + ID: "gpt-5.2", + Object: "model", + Created: 1765440000, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5.2", + DisplayName: "GPT 5.2", + Description: "Stable version of GPT 5.2", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"none", "low", "medium", "high", "xhigh"}}, + }, + { + ID: "gpt-5.2-codex", + Object: "model", + Created: 1765440000, + OwnedBy: "openai", + Type: "openai", + Version: "gpt-5.2", + DisplayName: "GPT 5.2 Codex", + Description: "Stable version of GPT 5.2 Codex, The best model for coding and agentic tasks across domains.", + ContextLength: 400000, + MaxCompletionTokens: 128000, + SupportedParameters: []string{"tools"}, + Thinking: &ThinkingSupport{Levels: []string{"low", "medium", "high", "xhigh"}}, + }, + } +} + +// GetQwenModels returns the standard Qwen model definitions +func GetQwenModels() []*ModelInfo { + return []*ModelInfo{ + { + ID: "qwen3-coder-plus", + Object: "model", + Created: 1753228800, + OwnedBy: "qwen", + Type: "qwen", + Version: "3.0", + DisplayName: "Qwen3 Coder Plus", + Description: "Advanced code generation and understanding model", + ContextLength: 32768, + MaxCompletionTokens: 8192, + SupportedParameters: []string{"temperature", "top_p", "max_tokens", "stream", "stop"}, + }, + { + ID: "qwen3-coder-flash", + Object: "model", + Created: 1753228800, + OwnedBy: "qwen", + Type: "qwen", + Version: "3.0", + DisplayName: "Qwen3 Coder Flash", + Description: "Fast code generation model", + ContextLength: 8192, + MaxCompletionTokens: 2048, + SupportedParameters: []string{"temperature", "top_p", "max_tokens", "stream", "stop"}, + }, + { + ID: "vision-model", + Object: "model", + Created: 1758672000, + OwnedBy: "qwen", + Type: "qwen", + Version: "3.0", + DisplayName: "Qwen3 Vision Model", + Description: "Vision model model", + ContextLength: 32768, + MaxCompletionTokens: 2048, + SupportedParameters: []string{"temperature", "top_p", "max_tokens", "stream", "stop"}, + }, + } +} + +// iFlowThinkingSupport is a shared ThinkingSupport configuration for iFlow models +// that support thinking mode via chat_template_kwargs.enable_thinking (boolean toggle). +// Uses level-based configuration so standard normalization flows apply before conversion. +var iFlowThinkingSupport = &ThinkingSupport{ + Levels: []string{"none", "auto", "minimal", "low", "medium", "high", "xhigh"}, +} + +// GetIFlowModels returns supported models for iFlow OAuth accounts. +func GetIFlowModels() []*ModelInfo { + entries := []struct { + ID string + DisplayName string + Description string + Created int64 + Thinking *ThinkingSupport + }{ + {ID: "tstars2.0", DisplayName: "TStars-2.0", Description: "iFlow TStars-2.0 multimodal assistant", Created: 1746489600}, + {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, 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}, + {ID: "kimi-k2", DisplayName: "Kimi-K2", Description: "Moonshot Kimi K2 general model", Created: 1752192000}, + {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, 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}, + {ID: "qwen3-235b-a22b-thinking-2507", DisplayName: "Qwen3-235B-A22B-Thinking", Description: "Qwen3 235B A22B Thinking (2507)", Created: 1753401600}, + {ID: "qwen3-235b-a22b-instruct", DisplayName: "Qwen3-235B-A22B-Instruct", Description: "Qwen3 235B A22B Instruct", Created: 1753401600}, + {ID: "qwen3-235b", DisplayName: "Qwen3-235B-A22B", Description: "Qwen3 235B A22B", Created: 1753401600}, + {ID: "minimax-m2", DisplayName: "MiniMax-M2", Description: "MiniMax M2", Created: 1758672000, Thinking: iFlowThinkingSupport}, + {ID: "minimax-m2.1", DisplayName: "MiniMax-M2.1", Description: "MiniMax M2.1", Created: 1766448000, Thinking: iFlowThinkingSupport}, + {ID: "iflow-rome-30ba3b", DisplayName: "iFlow-ROME", Description: "iFlow Rome 30BA3B model", Created: 1736899200}, + } + models := make([]*ModelInfo, 0, len(entries)) + for _, entry := range entries { + models = append(models, &ModelInfo{ + ID: entry.ID, + Object: "model", + Created: entry.Created, + OwnedBy: "iflow", + Type: "iflow", + DisplayName: entry.DisplayName, + Description: entry.Description, + Thinking: entry.Thinking, + }) + } + return models +} + +// AntigravityModelConfig captures static antigravity model overrides, including +// Thinking budget limits and provider max completion tokens. +type AntigravityModelConfig struct { + Thinking *ThinkingSupport + MaxCompletionTokens int +} + +// GetAntigravityModelConfig returns static configuration for antigravity models. +// Keys use upstream model names returned by the Antigravity models endpoint. +func GetAntigravityModelConfig() map[string]*AntigravityModelConfig { + return map[string]*AntigravityModelConfig{ + // "rev19-uic3-1p": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true}}, + "gemini-2.5-flash": {Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}}, + "gemini-2.5-flash-lite": {Thinking: &ThinkingSupport{Min: 0, Max: 24576, ZeroAllowed: true, DynamicAllowed: true}}, + "gemini-3-pro-high": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}}, + "gemini-3-pro-image": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"low", "high"}}}, + "gemini-3-flash": {Thinking: &ThinkingSupport{Min: 128, Max: 32768, ZeroAllowed: false, DynamicAllowed: true, Levels: []string{"minimal", "low", "medium", "high"}}}, + "claude-sonnet-4-5-thinking": {Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: true, DynamicAllowed: true}, MaxCompletionTokens: 64000}, + "claude-opus-4-5-thinking": {Thinking: &ThinkingSupport{Min: 1024, Max: 128000, ZeroAllowed: true, DynamicAllowed: true}, MaxCompletionTokens: 64000}, + "claude-sonnet-4-5": {MaxCompletionTokens: 64000}, + "gpt-oss-120b-medium": {}, + "tab_flash_lite_preview": {}, + } +} + +// GetMiniMaxModels returns supported models for MiniMax accounts. +func GetMiniMaxModels() []*ModelInfo { + return []*ModelInfo{ + {ID: "MiniMax-Text-01", DisplayName: "MiniMax-Text-01", Description: "MiniMax Text model"}, + } +} + +// GetZhipuModels returns supported models for Zhipu (GLM) accounts. +func GetZhipuModels() []*ModelInfo { + return []*ModelInfo{ + {ID: "glm-4", DisplayName: "GLM-4", Description: "Zhipu GLM-4 model"}, + } +} + +// GetKiroModels returns supported models for Kiro accounts. +func GetKiroModels() []*ModelInfo { + return []*ModelInfo{ + {ID: "kiro-default", DisplayName: "Kiro Default", Description: "Kiro default model"}, + } +} + +// GetAmazonQModels returns supported models for Amazon Q accounts. +func GetAmazonQModels() []*ModelInfo { + return []*ModelInfo{ + {ID: "amazon-q", DisplayName: "Amazon Q", Description: "Amazon Q Developer model"}, + } +} + +// GetGitHubCopilotModels returns supported models for GitHub Copilot accounts. +func GetGitHubCopilotModels() []*ModelInfo { + return []*ModelInfo{ + {ID: "gpt-4o", DisplayName: "GPT-4o", Description: "OpenAI GPT-4o via GitHub Copilot"}, + } +} diff --git a/internal/registry/model_registry.go b/internal/registry/model_registry.go index 6d7334d1..edb1f124 100644 --- a/internal/registry/model_registry.go +++ b/internal/registry/model_registry.go @@ -4,6 +4,7 @@ package registry import ( + "context" "fmt" "sort" "strings" @@ -51,9 +52,9 @@ type ModelInfo struct { // This is optional and currently used for Gemini thinking budget normalization. Thinking *ThinkingSupport `json:"thinking,omitempty"` - // UserDefined indicates whether this model was registered via user configuration - // (e.g., openai-compatibility.*.models[], *-api-key.models[]). - // User-defined models skip thinking validation and pass through to upstream. + // UserDefined indicates this model was defined through config file's models[] + // array (e.g., openai-compatibility.*.models[], *-api-key.models[]). + // UserDefined models have thinking configuration passed through without validation. UserDefined bool `json:"-"` } @@ -91,6 +92,13 @@ type ModelRegistration struct { SuspendedClients map[string]string } +// ModelRegistryHook provides optional callbacks for external integrations to track model list changes. +// Hook implementations must be non-blocking and resilient; calls are executed asynchronously and panics are recovered. +type ModelRegistryHook interface { + OnModelsRegistered(ctx context.Context, provider, clientID string, models []*ModelInfo) + OnModelsUnregistered(ctx context.Context, provider, clientID string) +} + // ModelRegistry manages the global registry of available models type ModelRegistry struct { // models maps model ID to registration information @@ -104,6 +112,8 @@ type ModelRegistry struct { clientProviders map[string]string // mutex ensures thread-safe access to the registry mutex *sync.RWMutex + // hook is an optional callback sink for model registration changes + hook ModelRegistryHook } // Global model registry instance @@ -124,6 +134,71 @@ func GetGlobalRegistry() *ModelRegistry { return globalRegistry } +// LookupModelInfo searches dynamic registry (provider-specific > global) then static definitions. +func LookupModelInfo(modelID string, provider ...string) *ModelInfo { + modelID = strings.TrimSpace(modelID) + if modelID == "" { + return nil + } + + p := "" + if len(provider) > 0 { + p = strings.ToLower(strings.TrimSpace(provider[0])) + } + + if info := GetGlobalRegistry().GetModelInfo(modelID, p); info != nil { + return info + } + return LookupStaticModelInfo(modelID) +} + +// SetHook sets an optional hook for observing model registration changes. +func (r *ModelRegistry) SetHook(hook ModelRegistryHook) { + if r == nil { + return + } + r.mutex.Lock() + defer r.mutex.Unlock() + r.hook = hook +} + +const defaultModelRegistryHookTimeout = 5 * time.Second + +func (r *ModelRegistry) triggerModelsRegistered(provider, clientID string, models []*ModelInfo) { + hook := r.hook + if hook == nil { + return + } + modelsCopy := cloneModelInfosUnique(models) + go func() { + defer func() { + if recovered := recover(); recovered != nil { + log.Errorf("model registry hook OnModelsRegistered panic: %v", recovered) + } + }() + ctx, cancel := context.WithTimeout(context.Background(), defaultModelRegistryHookTimeout) + defer cancel() + hook.OnModelsRegistered(ctx, provider, clientID, modelsCopy) + }() +} + +func (r *ModelRegistry) triggerModelsUnregistered(provider, clientID string) { + hook := r.hook + if hook == nil { + return + } + go func() { + defer func() { + if recovered := recover(); recovered != nil { + log.Errorf("model registry hook OnModelsUnregistered panic: %v", recovered) + } + }() + ctx, cancel := context.WithTimeout(context.Background(), defaultModelRegistryHookTimeout) + defer cancel() + hook.OnModelsUnregistered(ctx, provider, clientID) + }() +} + // RegisterClient registers a client and its supported models // Parameters: // - clientID: Unique identifier for the client @@ -184,6 +259,7 @@ func (r *ModelRegistry) RegisterClient(clientID, clientProvider string, models [ } else { delete(r.clientProviders, clientID) } + r.triggerModelsRegistered(provider, clientID, models) log.Debugf("Registered client %s from provider %s with %d models", clientID, clientProvider, len(rawModelIDs)) misc.LogCredentialSeparator() return @@ -326,6 +402,7 @@ func (r *ModelRegistry) RegisterClient(clientID, clientProvider string, models [ delete(r.clientProviders, clientID) } + r.triggerModelsRegistered(provider, clientID, models) if len(added) == 0 && len(removed) == 0 && !providerChanged { // Only metadata (e.g., display name) changed; skip separator when no log output. return @@ -425,6 +502,25 @@ func cloneModelInfo(model *ModelInfo) *ModelInfo { return ©Model } +func cloneModelInfosUnique(models []*ModelInfo) []*ModelInfo { + if len(models) == 0 { + return nil + } + cloned := make([]*ModelInfo, 0, len(models)) + seen := make(map[string]struct{}, len(models)) + for _, model := range models { + if model == nil || model.ID == "" { + continue + } + if _, exists := seen[model.ID]; exists { + continue + } + seen[model.ID] = struct{}{} + cloned = append(cloned, cloneModelInfo(model)) + } + return cloned +} + // UnregisterClient removes a client and decrements counts for its models // Parameters: // - clientID: Unique identifier for the client to remove @@ -488,6 +584,7 @@ func (r *ModelRegistry) unregisterClientInternal(clientID string) { log.Debugf("Unregistered client %s", clientID) // Separator line after completing client unregistration (after the summary line) misc.LogCredentialSeparator() + r.triggerModelsUnregistered(provider, clientID) } // SetModelQuotaExceeded marks a model as quota exceeded for a specific client @@ -598,19 +695,6 @@ func (r *ModelRegistry) ClientSupportsModel(clientID, modelID string) bool { return false } -// ClientModelSupportKnown reports whether the registry has an explicit model list for the client. -// When false, the caller should treat model support as unknown (and typically allow probing). -func (r *ModelRegistry) ClientModelSupportKnown(clientID string) bool { - clientID = strings.TrimSpace(clientID) - if clientID == "" { - return false - } - r.mutex.RLock() - defer r.mutex.RUnlock() - models, exists := r.clientModels[clientID] - return exists && len(models) > 0 -} - // GetAvailableModels returns all models that have at least one available client // Parameters: // - handlerType: The handler type to filter models for (e.g., "openai", "claude", "gemini") @@ -666,6 +750,131 @@ func (r *ModelRegistry) GetAvailableModels(handlerType string) []map[string]any return models } +// GetAvailableModelsByProvider returns models available for the given provider identifier. +// Parameters: +// - provider: Provider identifier (e.g., "codex", "gemini", "antigravity") +// +// Returns: +// - []*ModelInfo: List of available models for the provider +func (r *ModelRegistry) GetAvailableModelsByProvider(provider string) []*ModelInfo { + provider = strings.ToLower(strings.TrimSpace(provider)) + if provider == "" { + return nil + } + + r.mutex.RLock() + defer r.mutex.RUnlock() + + type providerModel struct { + count int + info *ModelInfo + } + + providerModels := make(map[string]*providerModel) + + for clientID, clientProvider := range r.clientProviders { + if clientProvider != provider { + continue + } + modelIDs := r.clientModels[clientID] + if len(modelIDs) == 0 { + continue + } + clientInfos := r.clientModelInfos[clientID] + for _, modelID := range modelIDs { + modelID = strings.TrimSpace(modelID) + if modelID == "" { + continue + } + entry := providerModels[modelID] + if entry == nil { + entry = &providerModel{} + providerModels[modelID] = entry + } + entry.count++ + if entry.info == nil { + if clientInfos != nil { + if info := clientInfos[modelID]; info != nil { + entry.info = info + } + } + if entry.info == nil { + if reg, ok := r.models[modelID]; ok && reg != nil && reg.Info != nil { + entry.info = reg.Info + } + } + } + } + } + + if len(providerModels) == 0 { + return nil + } + + quotaExpiredDuration := 5 * time.Minute + now := time.Now() + result := make([]*ModelInfo, 0, len(providerModels)) + + for modelID, entry := range providerModels { + if entry == nil || entry.count <= 0 { + continue + } + registration, ok := r.models[modelID] + + expiredClients := 0 + cooldownSuspended := 0 + otherSuspended := 0 + if ok && registration != nil { + if registration.QuotaExceededClients != nil { + for clientID, quotaTime := range registration.QuotaExceededClients { + if clientID == "" { + continue + } + if p, okProvider := r.clientProviders[clientID]; !okProvider || p != provider { + continue + } + if quotaTime != nil && now.Sub(*quotaTime) < quotaExpiredDuration { + expiredClients++ + } + } + } + if registration.SuspendedClients != nil { + for clientID, reason := range registration.SuspendedClients { + if clientID == "" { + continue + } + if p, okProvider := r.clientProviders[clientID]; !okProvider || p != provider { + continue + } + if strings.EqualFold(reason, "quota") { + cooldownSuspended++ + continue + } + otherSuspended++ + } + } + } + + availableClients := entry.count + effectiveClients := availableClients - expiredClients - otherSuspended + if effectiveClients < 0 { + effectiveClients = 0 + } + + if effectiveClients > 0 || (availableClients > 0 && (expiredClients > 0 || cooldownSuspended > 0) && otherSuspended == 0) { + if entry.info != nil { + result = append(result, entry.info) + continue + } + if ok && registration != nil && registration.Info != nil { + result = append(result, registration.Info) + } + } + } + + return result +} + // GetModelCount returns the number of available clients for a specific model // Parameters: // - modelID: The model ID to check @@ -786,29 +995,10 @@ func (r *ModelRegistry) convertModelToMap(model *ModelInfo, handlerType string) switch handlerType { case "openai": - // For OpenAI-compatible /v1/models, expose a consistent pair of limits for *all* models: - // - context_length (max input tokens) - // - max_completion_tokens (max output tokens) - // - // Some providers populate InputTokenLimit/OutputTokenLimit (Gemini-style) while others use - // ContextLength/MaxCompletionTokens (OpenAI/Claude/Qwen-style). Prefer the explicit fields, - // and fall back to Gemini-style fields when needed. - contextLength := model.ContextLength - if contextLength == 0 { - contextLength = model.InputTokenLimit - } - maxCompletion := model.MaxCompletionTokens - if maxCompletion == 0 { - maxCompletion = model.OutputTokenLimit - } - result := map[string]any{ "id": model.ID, "object": "model", "owned_by": model.OwnedBy, - // Always include these keys; 0 means unknown/unset. - "context_length": contextLength, - "max_completion_tokens": maxCompletion, } if model.Created > 0 { result["created"] = model.Created @@ -825,40 +1015,32 @@ func (r *ModelRegistry) convertModelToMap(model *ModelInfo, handlerType string) if model.Description != "" { result["description"] = model.Description } + if model.ContextLength > 0 { + result["context_length"] = model.ContextLength + } + if model.MaxCompletionTokens > 0 { + result["max_completion_tokens"] = model.MaxCompletionTokens + } if len(model.SupportedParameters) > 0 { result["supported_parameters"] = model.SupportedParameters } return result - case "claude", "kiro", "antigravity": - // Claude, Kiro, and Antigravity all use Claude-compatible format for Claude Code client + case "claude": result := map[string]any{ "id": model.ID, "object": "model", "owned_by": model.OwnedBy, } if model.Created > 0 { - result["created"] = model.Created + result["created_at"] = model.Created } if model.Type != "" { - result["type"] = model.Type + result["type"] = "model" } if model.DisplayName != "" { result["display_name"] = model.DisplayName } - // Add thinking support for Claude Code client - // Claude Code checks for "thinking" field (simple boolean) to enable tab toggle - // Also add "extended_thinking" for detailed budget info - if model.Thinking != nil { - result["thinking"] = true - result["extended_thinking"] = map[string]any{ - "supported": true, - "min": model.Thinking.Min, - "max": model.Thinking.Max, - "zero_allowed": model.Thinking.ZeroAllowed, - "dynamic_allowed": model.Thinking.DynamicAllowed, - } - } return result case "gemini": @@ -1008,57 +1190,3 @@ func (r *ModelRegistry) GetModelsForClient(clientID string) []*ModelInfo { } return result } - -// LookupModelInfo searches dynamic registry (provider-specific > global) then static definitions. -func LookupModelInfo(modelID string, provider ...string) *ModelInfo { - modelID = strings.TrimSpace(modelID) - if modelID == "" { - return nil - } - - p := "" - if len(provider) > 0 { - p = strings.ToLower(strings.TrimSpace(provider[0])) - } - - if info := GetGlobalRegistry().GetModelInfo(modelID, p); info != nil { - return info - } - return LookupStaticModelInfo(modelID) -} - -// LookupStaticModelInfo searches static model definitions for the given model ID. -// Returns nil if the model is not found in any static definition. -func LookupStaticModelInfo(modelID string) *ModelInfo { - if modelID == "" { - return nil - } - - allModels := [][]*ModelInfo{ - GetClaudeModels(), - GetGeminiModels(), - GetGeminiVertexModels(), - GetGeminiCLIModels(), - GetAIStudioModels(), - GetOpenAIModels(), - GetQwenModels(), - } - for _, models := range allModels { - for _, m := range models { - if m != nil && m.ID == modelID { - return m - } - } - } - - // Check Antigravity static config - if cfg := GetAntigravityModelConfig()[modelID]; cfg != nil { - return &ModelInfo{ - ID: modelID, - Thinking: cfg.Thinking, - MaxCompletionTokens: cfg.MaxCompletionTokens, - } - } - - return nil -} diff --git a/internal/registry/model_registry_hook_test.go b/internal/registry/model_registry_hook_test.go new file mode 100644 index 00000000..70226b9e --- /dev/null +++ b/internal/registry/model_registry_hook_test.go @@ -0,0 +1,204 @@ +package registry + +import ( + "context" + "sync" + "testing" + "time" +) + +func newTestModelRegistry() *ModelRegistry { + return &ModelRegistry{ + models: make(map[string]*ModelRegistration), + clientModels: make(map[string][]string), + clientModelInfos: make(map[string]map[string]*ModelInfo), + clientProviders: make(map[string]string), + mutex: &sync.RWMutex{}, + } +} + +type registeredCall struct { + provider string + clientID string + models []*ModelInfo +} + +type unregisteredCall struct { + provider string + clientID string +} + +type capturingHook struct { + registeredCh chan registeredCall + unregisteredCh chan unregisteredCall +} + +func (h *capturingHook) OnModelsRegistered(ctx context.Context, provider, clientID string, models []*ModelInfo) { + h.registeredCh <- registeredCall{provider: provider, clientID: clientID, models: models} +} + +func (h *capturingHook) OnModelsUnregistered(ctx context.Context, provider, clientID string) { + h.unregisteredCh <- unregisteredCall{provider: provider, clientID: clientID} +} + +func TestModelRegistryHook_OnModelsRegisteredCalled(t *testing.T) { + r := newTestModelRegistry() + hook := &capturingHook{ + registeredCh: make(chan registeredCall, 1), + unregisteredCh: make(chan unregisteredCall, 1), + } + r.SetHook(hook) + + inputModels := []*ModelInfo{ + {ID: "m1", DisplayName: "Model One"}, + {ID: "m2", DisplayName: "Model Two"}, + } + r.RegisterClient("client-1", "OpenAI", inputModels) + + select { + case call := <-hook.registeredCh: + if call.provider != "openai" { + t.Fatalf("provider mismatch: got %q, want %q", call.provider, "openai") + } + if call.clientID != "client-1" { + t.Fatalf("clientID mismatch: got %q, want %q", call.clientID, "client-1") + } + if len(call.models) != 2 { + t.Fatalf("models length mismatch: got %d, want %d", len(call.models), 2) + } + if call.models[0] == nil || call.models[0].ID != "m1" { + t.Fatalf("models[0] mismatch: got %#v, want ID=%q", call.models[0], "m1") + } + if call.models[1] == nil || call.models[1].ID != "m2" { + t.Fatalf("models[1] mismatch: got %#v, want ID=%q", call.models[1], "m2") + } + case <-time.After(2 * time.Second): + t.Fatal("timeout waiting for OnModelsRegistered hook call") + } +} + +func TestModelRegistryHook_OnModelsUnregisteredCalled(t *testing.T) { + r := newTestModelRegistry() + hook := &capturingHook{ + registeredCh: make(chan registeredCall, 1), + unregisteredCh: make(chan unregisteredCall, 1), + } + r.SetHook(hook) + + r.RegisterClient("client-1", "OpenAI", []*ModelInfo{{ID: "m1"}}) + select { + case <-hook.registeredCh: + case <-time.After(2 * time.Second): + t.Fatal("timeout waiting for OnModelsRegistered hook call") + } + + r.UnregisterClient("client-1") + + select { + case call := <-hook.unregisteredCh: + if call.provider != "openai" { + t.Fatalf("provider mismatch: got %q, want %q", call.provider, "openai") + } + if call.clientID != "client-1" { + t.Fatalf("clientID mismatch: got %q, want %q", call.clientID, "client-1") + } + case <-time.After(2 * time.Second): + t.Fatal("timeout waiting for OnModelsUnregistered hook call") + } +} + +type blockingHook struct { + started chan struct{} + unblock chan struct{} +} + +func (h *blockingHook) OnModelsRegistered(ctx context.Context, provider, clientID string, models []*ModelInfo) { + select { + case <-h.started: + default: + close(h.started) + } + <-h.unblock +} + +func (h *blockingHook) OnModelsUnregistered(ctx context.Context, provider, clientID string) {} + +func TestModelRegistryHook_DoesNotBlockRegisterClient(t *testing.T) { + r := newTestModelRegistry() + hook := &blockingHook{ + started: make(chan struct{}), + unblock: make(chan struct{}), + } + r.SetHook(hook) + defer close(hook.unblock) + + done := make(chan struct{}) + go func() { + r.RegisterClient("client-1", "OpenAI", []*ModelInfo{{ID: "m1"}}) + close(done) + }() + + select { + case <-hook.started: + case <-time.After(2 * time.Second): + t.Fatal("timeout waiting for hook to start") + } + + select { + case <-done: + case <-time.After(200 * time.Millisecond): + t.Fatal("RegisterClient appears to be blocked by hook") + } + + if !r.ClientSupportsModel("client-1", "m1") { + t.Fatal("model registration failed; expected client to support model") + } +} + +type panicHook struct { + registeredCalled chan struct{} + unregisteredCalled chan struct{} +} + +func (h *panicHook) OnModelsRegistered(ctx context.Context, provider, clientID string, models []*ModelInfo) { + if h.registeredCalled != nil { + h.registeredCalled <- struct{}{} + } + panic("boom") +} + +func (h *panicHook) OnModelsUnregistered(ctx context.Context, provider, clientID string) { + if h.unregisteredCalled != nil { + h.unregisteredCalled <- struct{}{} + } + panic("boom") +} + +func TestModelRegistryHook_PanicDoesNotAffectRegistry(t *testing.T) { + r := newTestModelRegistry() + hook := &panicHook{ + registeredCalled: make(chan struct{}, 1), + unregisteredCalled: make(chan struct{}, 1), + } + r.SetHook(hook) + + r.RegisterClient("client-1", "OpenAI", []*ModelInfo{{ID: "m1"}}) + + select { + case <-hook.registeredCalled: + case <-time.After(2 * time.Second): + t.Fatal("timeout waiting for OnModelsRegistered hook call") + } + + if !r.ClientSupportsModel("client-1", "m1") { + t.Fatal("model registration failed; expected client to support model") + } + + r.UnregisterClient("client-1") + + select { + case <-hook.unregisteredCalled: + case <-time.After(2 * time.Second): + t.Fatal("timeout waiting for OnModelsUnregistered hook call") + } +} diff --git a/internal/runtime/executor/aistudio_executor.go b/internal/runtime/executor/aistudio_executor.go index 0aaf6b48..e08492fd 100644 --- a/internal/runtime/executor/aistudio_executor.go +++ b/internal/runtime/executor/aistudio_executor.go @@ -485,8 +485,3 @@ func ensureColonSpacedJSON(payload []byte) []byte { return compacted } - -// Embed is not supported by the AIStudio executor. -func (e *AIStudioExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("aistudio executor does not support embeddings") -} diff --git a/internal/runtime/executor/antigravity_executor.go b/internal/runtime/executor/antigravity_executor.go index e3b2c2b4..64d19951 100644 --- a/internal/runtime/executor/antigravity_executor.go +++ b/internal/runtime/executor/antigravity_executor.go @@ -1594,9 +1594,3 @@ func generateProjectID() string { randomPart := strings.ToLower(uuid.NewString())[:5] return adj + "-" + noun + "-" + randomPart } - -// Embed is a stub to satisfy the ProviderExecutor interface. -// Antigravity does not support embeddings. -func (e *AntigravityExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("antigravity executor does not support embeddings") -} diff --git a/internal/runtime/executor/cache_helpers.go b/internal/runtime/executor/cache_helpers.go index b6de886d..1e32f43a 100644 --- a/internal/runtime/executor/cache_helpers.go +++ b/internal/runtime/executor/cache_helpers.go @@ -29,6 +29,7 @@ func startCodexCacheCleanup() { go func() { ticker := time.NewTicker(codexCacheCleanupInterval) defer ticker.Stop() + for range ticker.C { purgeExpiredCodexCache() } @@ -38,8 +39,10 @@ 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) @@ -66,3 +69,10 @@ 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/claude_executor.go b/internal/runtime/executor/claude_executor.go index c237275a..170ebb90 100644 --- a/internal/runtime/executor/claude_executor.go +++ b/internal/runtime/executor/claude_executor.go @@ -990,8 +990,3 @@ func applyCloaking(ctx context.Context, cfg *config.Config, auth *cliproxyauth.A return payload } - -// Embed is not supported by the claude executor. -func (e *ClaudeExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("claude executor does not support embeddings") -} diff --git a/internal/runtime/executor/codex_executor.go b/internal/runtime/executor/codex_executor.go index 2a39e065..1f368b84 100644 --- a/internal/runtime/executor/codex_executor.go +++ b/internal/runtime/executor/codex_executor.go @@ -642,8 +642,3 @@ func (e *CodexExecutor) resolveCodexConfig(auth *cliproxyauth.Auth) *config.Code } return nil } - -// Embed is not supported by the codex executor. -func (e *CodexExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("codex executor does not support embeddings") -} diff --git a/internal/runtime/executor/gemini_cli_executor.go b/internal/runtime/executor/gemini_cli_executor.go index 678dc75e..e8a244ab 100644 --- a/internal/runtime/executor/gemini_cli_executor.go +++ b/internal/runtime/executor/gemini_cli_executor.go @@ -899,8 +899,3 @@ func parseRetryDelay(errorBody []byte) (*time.Duration, error) { return nil, fmt.Errorf("no RetryInfo found") } - -// Embed is not supported by the gemini cli executor. -func (e *GeminiCLIExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("gemini cli executor does not support embeddings") -} diff --git a/internal/runtime/executor/gemini_executor.go b/internal/runtime/executor/gemini_executor.go index 5bb16408..58bd71a2 100644 --- a/internal/runtime/executor/gemini_executor.go +++ b/internal/runtime/executor/gemini_executor.go @@ -540,8 +540,3 @@ func fixGeminiImageAspectRatio(modelName string, rawJSON []byte) []byte { } return rawJSON } - -// Embed is not supported by the gemini executor. -func (e *GeminiExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("gemini executor does not support embeddings") -} diff --git a/internal/runtime/executor/gemini_vertex_executor.go b/internal/runtime/executor/gemini_vertex_executor.go index c3174ef7..ceea42ff 100644 --- a/internal/runtime/executor/gemini_vertex_executor.go +++ b/internal/runtime/executor/gemini_vertex_executor.go @@ -1056,8 +1056,3 @@ func (e *GeminiVertexExecutor) resolveVertexConfig(auth *cliproxyauth.Auth) *con } return nil } - -// Embed is not supported by the gemini vertex executor. -func (e *GeminiVertexExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("gemini vertex executor does not support embeddings") -} diff --git a/internal/runtime/executor/github_copilot_executor.go b/internal/runtime/executor/github_copilot_executor.go new file mode 100644 index 00000000..147b32cd --- /dev/null +++ b/internal/runtime/executor/github_copilot_executor.go @@ -0,0 +1,431 @@ +package executor + +import ( + "bufio" + "bytes" + "context" + "fmt" + "io" + "net/http" + "sync" + "time" + + "github.com/google/uuid" + copilotauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/copilot" + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + log "github.com/sirupsen/logrus" + "github.com/tidwall/sjson" +) + +const ( + githubCopilotBaseURL = "https://api.githubcopilot.com" + githubCopilotChatPath = "/chat/completions" + githubCopilotResponsesPath = "/responses" + githubCopilotAuthType = "github-copilot" + githubCopilotTokenCacheTTL = 25 * time.Minute + // tokenExpiryBuffer is the time before expiry when we should refresh the token. + tokenExpiryBuffer = 5 * time.Minute + // maxScannerBufferSize is the maximum buffer size for SSE scanning (20MB). + maxScannerBufferSize = 20_971_520 + + // Copilot API header values. + copilotUserAgent = "GithubCopilot/1.0" + copilotEditorVersion = "vscode/1.100.0" + copilotPluginVersion = "copilot/1.300.0" + copilotIntegrationID = "vscode-chat" + copilotOpenAIIntent = "conversation-panel" +) + +// GitHubCopilotExecutor handles requests to the GitHub Copilot API. +type GitHubCopilotExecutor struct { + cfg *config.Config + mu sync.RWMutex + cache map[string]*cachedAPIToken +} + +// cachedAPIToken stores a cached Copilot API token with its expiry. +type cachedAPIToken struct { + token string + expiresAt time.Time +} + +// NewGitHubCopilotExecutor constructs a new executor instance. +func NewGitHubCopilotExecutor(cfg *config.Config) *GitHubCopilotExecutor { + return &GitHubCopilotExecutor{ + cfg: cfg, + cache: make(map[string]*cachedAPIToken), + } +} + +// Identifier implements ProviderExecutor. +func (e *GitHubCopilotExecutor) Identifier() string { return githubCopilotAuthType } + +// PrepareRequest implements ProviderExecutor. +func (e *GitHubCopilotExecutor) PrepareRequest(req *http.Request, auth *cliproxyauth.Auth) error { + if req == nil { + return nil + } + ctx := req.Context() + if ctx == nil { + ctx = context.Background() + } + apiToken, errToken := e.ensureAPIToken(ctx, auth) + if errToken != nil { + return errToken + } + e.applyHeaders(req, apiToken) + return nil +} + +// HttpRequest injects GitHub Copilot credentials into the request and executes it. +func (e *GitHubCopilotExecutor) HttpRequest(ctx context.Context, auth *cliproxyauth.Auth, req *http.Request) (*http.Response, error) { + if req == nil { + return nil, fmt.Errorf("github-copilot executor: request is nil") + } + if ctx == nil { + ctx = req.Context() + } + httpReq := req.WithContext(ctx) + if errPrepare := e.PrepareRequest(httpReq, auth); errPrepare != nil { + return nil, errPrepare + } + httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0) + return httpClient.Do(httpReq) +} + +// Execute handles non-streaming requests to GitHub Copilot. +func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) { + apiToken, errToken := e.ensureAPIToken(ctx, auth) + if errToken != nil { + return resp, errToken + } + + reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) + defer reporter.trackFailure(ctx, &err) + + from := opts.SourceFormat + useResponses := useGitHubCopilotResponsesEndpoint(from) + to := sdktranslator.FromString("openai") + if useResponses { + to = sdktranslator.FromString("openai-response") + } + originalPayload := bytes.Clone(req.Payload) + if len(opts.OriginalRequest) > 0 { + originalPayload = bytes.Clone(opts.OriginalRequest) + } + originalTranslated := sdktranslator.TranslateRequest(from, to, req.Model, originalPayload, false) + body := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), false) + body = e.normalizeModel(req.Model, body) + requestedModel := payloadRequestedModel(opts, req.Model) + body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated, requestedModel) + body, _ = sjson.SetBytes(body, "stream", false) + + path := githubCopilotChatPath + if useResponses { + path = githubCopilotResponsesPath + } + url := githubCopilotBaseURL + path + httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) + if err != nil { + return resp, err + } + e.applyHeaders(httpReq, apiToken) + + var authID, authLabel, authType, authValue string + if auth != nil { + authID = auth.ID + authLabel = auth.Label + authType, authValue = auth.AccountInfo() + } + recordAPIRequest(ctx, e.cfg, upstreamRequestLog{ + URL: url, + Method: http.MethodPost, + Headers: httpReq.Header.Clone(), + Body: body, + Provider: e.Identifier(), + AuthID: authID, + AuthLabel: authLabel, + AuthType: authType, + AuthValue: authValue, + }) + + httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0) + httpResp, err := httpClient.Do(httpReq) + if err != nil { + recordAPIResponseError(ctx, e.cfg, err) + return resp, err + } + defer func() { + if errClose := httpResp.Body.Close(); errClose != nil { + log.Errorf("github-copilot executor: close response body error: %v", errClose) + } + }() + + recordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone()) + + if !isHTTPSuccess(httpResp.StatusCode) { + data, _ := io.ReadAll(httpResp.Body) + appendAPIResponseChunk(ctx, e.cfg, data) + log.Debugf("github-copilot executor: upstream error status: %d, body: %s", httpResp.StatusCode, summarizeErrorBody(httpResp.Header.Get("Content-Type"), data)) + err = statusErr{code: httpResp.StatusCode, msg: string(data)} + return resp, err + } + + data, err := io.ReadAll(httpResp.Body) + if err != nil { + recordAPIResponseError(ctx, e.cfg, err) + return resp, err + } + appendAPIResponseChunk(ctx, e.cfg, data) + + detail := parseOpenAIUsage(data) + if useResponses && detail.TotalTokens == 0 { + detail = parseOpenAIResponsesUsage(data) + } + if detail.TotalTokens > 0 { + reporter.publish(ctx, detail) + } + + var param any + converted := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, data, ¶m) + resp = cliproxyexecutor.Response{Payload: []byte(converted)} + reporter.ensurePublished(ctx) + return resp, nil +} + +// ExecuteStream handles streaming requests to GitHub Copilot. +func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (stream <-chan cliproxyexecutor.StreamChunk, err error) { + apiToken, errToken := e.ensureAPIToken(ctx, auth) + if errToken != nil { + return nil, errToken + } + + reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) + defer reporter.trackFailure(ctx, &err) + + from := opts.SourceFormat + useResponses := useGitHubCopilotResponsesEndpoint(from) + to := sdktranslator.FromString("openai") + if useResponses { + to = sdktranslator.FromString("openai-response") + } + originalPayload := bytes.Clone(req.Payload) + if len(opts.OriginalRequest) > 0 { + originalPayload = bytes.Clone(opts.OriginalRequest) + } + originalTranslated := sdktranslator.TranslateRequest(from, to, req.Model, originalPayload, false) + body := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), true) + body = e.normalizeModel(req.Model, body) + requestedModel := payloadRequestedModel(opts, req.Model) + body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated, requestedModel) + body, _ = sjson.SetBytes(body, "stream", true) + // Enable stream options for usage stats in stream + if !useResponses { + body, _ = sjson.SetBytes(body, "stream_options.include_usage", true) + } + + path := githubCopilotChatPath + if useResponses { + path = githubCopilotResponsesPath + } + url := githubCopilotBaseURL + path + httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) + if err != nil { + return nil, err + } + e.applyHeaders(httpReq, apiToken) + + var authID, authLabel, authType, authValue string + if auth != nil { + authID = auth.ID + authLabel = auth.Label + authType, authValue = auth.AccountInfo() + } + recordAPIRequest(ctx, e.cfg, upstreamRequestLog{ + URL: url, + Method: http.MethodPost, + Headers: httpReq.Header.Clone(), + Body: body, + Provider: e.Identifier(), + AuthID: authID, + AuthLabel: authLabel, + AuthType: authType, + AuthValue: authValue, + }) + + httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0) + httpResp, err := httpClient.Do(httpReq) + if err != nil { + recordAPIResponseError(ctx, e.cfg, err) + return nil, err + } + + recordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone()) + + if !isHTTPSuccess(httpResp.StatusCode) { + data, readErr := io.ReadAll(httpResp.Body) + if errClose := httpResp.Body.Close(); errClose != nil { + log.Errorf("github-copilot executor: close response body error: %v", errClose) + } + if readErr != nil { + recordAPIResponseError(ctx, e.cfg, readErr) + return nil, readErr + } + appendAPIResponseChunk(ctx, e.cfg, data) + log.Debugf("github-copilot executor: upstream error status: %d, body: %s", httpResp.StatusCode, summarizeErrorBody(httpResp.Header.Get("Content-Type"), data)) + err = statusErr{code: httpResp.StatusCode, msg: string(data)} + return nil, err + } + + out := make(chan cliproxyexecutor.StreamChunk) + stream = out + + go func() { + defer close(out) + defer func() { + if errClose := httpResp.Body.Close(); errClose != nil { + log.Errorf("github-copilot executor: close response body error: %v", errClose) + } + }() + + scanner := bufio.NewScanner(httpResp.Body) + scanner.Buffer(nil, maxScannerBufferSize) + var param any + + for scanner.Scan() { + line := scanner.Bytes() + appendAPIResponseChunk(ctx, e.cfg, line) + + // Parse SSE data + if bytes.HasPrefix(line, dataTag) { + data := bytes.TrimSpace(line[5:]) + if bytes.Equal(data, []byte("[DONE]")) { + continue + } + if detail, ok := parseOpenAIStreamUsage(line); ok { + reporter.publish(ctx, detail) + } else if useResponses { + if detail, ok := parseOpenAIResponsesStreamUsage(line); ok { + reporter.publish(ctx, detail) + } + } + } + + chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), ¶m) + for i := range chunks { + out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])} + } + } + + if errScan := scanner.Err(); errScan != nil { + recordAPIResponseError(ctx, e.cfg, errScan) + reporter.publishFailure(ctx) + out <- cliproxyexecutor.StreamChunk{Err: errScan} + } else { + reporter.ensurePublished(ctx) + } + }() + + return stream, nil +} + +// CountTokens is not supported for GitHub Copilot. +func (e *GitHubCopilotExecutor) CountTokens(_ context.Context, _ *cliproxyauth.Auth, _ cliproxyexecutor.Request, _ cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + return cliproxyexecutor.Response{}, statusErr{code: http.StatusNotImplemented, msg: "count tokens not supported for github-copilot"} +} + +// Refresh validates the GitHub token is still working. +// GitHub OAuth tokens don't expire traditionally, so we just validate. +func (e *GitHubCopilotExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) { + if auth == nil { + return nil, statusErr{code: http.StatusUnauthorized, msg: "missing auth"} + } + + // Get the GitHub access token + accessToken := metaStringValue(auth.Metadata, "access_token") + if accessToken == "" { + return auth, nil + } + + // Validate the token can still get a Copilot API token + copilotAuth := copilotauth.NewCopilotAuth(e.cfg) + _, err := copilotAuth.GetCopilotAPIToken(ctx, accessToken) + if err != nil { + return nil, statusErr{code: http.StatusUnauthorized, msg: fmt.Sprintf("github-copilot token validation failed: %v", err)} + } + + return auth, nil +} + +// ensureAPIToken gets or refreshes the Copilot API token. +func (e *GitHubCopilotExecutor) ensureAPIToken(ctx context.Context, auth *cliproxyauth.Auth) (string, error) { + if auth == nil { + return "", statusErr{code: http.StatusUnauthorized, msg: "missing auth"} + } + + // Get the GitHub access token + accessToken := metaStringValue(auth.Metadata, "access_token") + if accessToken == "" { + return "", statusErr{code: http.StatusUnauthorized, msg: "missing github access token"} + } + + // Check for cached API token using thread-safe access + e.mu.RLock() + if cached, ok := e.cache[accessToken]; ok && cached.expiresAt.After(time.Now().Add(tokenExpiryBuffer)) { + e.mu.RUnlock() + return cached.token, nil + } + e.mu.RUnlock() + + // Get a new Copilot API token + copilotAuth := copilotauth.NewCopilotAuth(e.cfg) + apiToken, err := copilotAuth.GetCopilotAPIToken(ctx, accessToken) + if err != nil { + return "", statusErr{code: http.StatusUnauthorized, msg: fmt.Sprintf("failed to get copilot api token: %v", err)} + } + + // Cache the token with thread-safe access + expiresAt := time.Now().Add(githubCopilotTokenCacheTTL) + if apiToken.ExpiresAt > 0 { + expiresAt = time.Unix(apiToken.ExpiresAt, 0) + } + e.mu.Lock() + e.cache[accessToken] = &cachedAPIToken{ + token: apiToken.Token, + expiresAt: expiresAt, + } + e.mu.Unlock() + + return apiToken.Token, nil +} + +// applyHeaders sets the required headers for GitHub Copilot API requests. +func (e *GitHubCopilotExecutor) applyHeaders(r *http.Request, apiToken string) { + r.Header.Set("Content-Type", "application/json") + r.Header.Set("Authorization", "Bearer "+apiToken) + r.Header.Set("Accept", "application/json") + r.Header.Set("User-Agent", copilotUserAgent) + r.Header.Set("Editor-Version", copilotEditorVersion) + r.Header.Set("Editor-Plugin-Version", copilotPluginVersion) + r.Header.Set("Openai-Intent", copilotOpenAIIntent) + r.Header.Set("Copilot-Integration-Id", copilotIntegrationID) + r.Header.Set("X-Request-Id", uuid.NewString()) +} + +// normalizeModel is a no-op as GitHub Copilot accepts model names directly. +// Model mapping should be done at the registry level if needed. +func (e *GitHubCopilotExecutor) normalizeModel(_ string, body []byte) []byte { + return body +} + +func useGitHubCopilotResponsesEndpoint(sourceFormat sdktranslator.Format) bool { + return sourceFormat.String() == "openai-response" +} + +// isHTTPSuccess checks if the status code indicates success (2xx). +func isHTTPSuccess(statusCode int) bool { + return statusCode >= 200 && statusCode < 300 +} diff --git a/internal/runtime/executor/iflow_executor.go b/internal/runtime/executor/iflow_executor.go new file mode 100644 index 00000000..270f5aa4 --- /dev/null +++ b/internal/runtime/executor/iflow_executor.go @@ -0,0 +1,532 @@ +package executor + +import ( + "bufio" + "bytes" + "context" + "fmt" + "io" + "net/http" + "strings" + "time" + + iflowauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/iflow" + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" + "github.com/router-for-me/CLIProxyAPI/v6/internal/util" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +const ( + iflowDefaultEndpoint = "/chat/completions" + iflowUserAgent = "iFlow-Cli" +) + +// IFlowExecutor executes OpenAI-compatible chat completions against the iFlow API using API keys derived from OAuth. +type IFlowExecutor struct { + cfg *config.Config +} + +// NewIFlowExecutor constructs a new executor instance. +func NewIFlowExecutor(cfg *config.Config) *IFlowExecutor { return &IFlowExecutor{cfg: cfg} } + +// Identifier returns the provider key. +func (e *IFlowExecutor) Identifier() string { return "iflow" } + +// PrepareRequest injects iFlow credentials into the outgoing HTTP request. +func (e *IFlowExecutor) PrepareRequest(req *http.Request, auth *cliproxyauth.Auth) error { + if req == nil { + return nil + } + apiKey, _ := iflowCreds(auth) + if strings.TrimSpace(apiKey) != "" { + req.Header.Set("Authorization", "Bearer "+apiKey) + } + return nil +} + +// HttpRequest injects iFlow credentials into the request and executes it. +func (e *IFlowExecutor) HttpRequest(ctx context.Context, auth *cliproxyauth.Auth, req *http.Request) (*http.Response, error) { + if req == nil { + return nil, fmt.Errorf("iflow executor: request is nil") + } + if ctx == nil { + ctx = req.Context() + } + httpReq := req.WithContext(ctx) + if err := e.PrepareRequest(httpReq, auth); err != nil { + return nil, err + } + httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0) + return httpClient.Do(httpReq) +} + +// Execute performs a non-streaming chat completion request. +func (e *IFlowExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) { + baseModel := thinking.ParseSuffix(req.Model).ModelName + + apiKey, baseURL := iflowCreds(auth) + if strings.TrimSpace(apiKey) == "" { + err = fmt.Errorf("iflow executor: missing api key") + return resp, err + } + if baseURL == "" { + baseURL = iflowauth.DefaultAPIBaseURL + } + + reporter := newUsageReporter(ctx, e.Identifier(), baseModel, auth) + defer reporter.trackFailure(ctx, &err) + + from := opts.SourceFormat + to := sdktranslator.FromString("openai") + originalPayload := bytes.Clone(req.Payload) + if len(opts.OriginalRequest) > 0 { + originalPayload = bytes.Clone(opts.OriginalRequest) + } + originalTranslated := sdktranslator.TranslateRequest(from, to, baseModel, originalPayload, false) + body := sdktranslator.TranslateRequest(from, to, baseModel, bytes.Clone(req.Payload), false) + body, _ = sjson.SetBytes(body, "model", baseModel) + + body, err = thinking.ApplyThinking(body, req.Model, from.String(), "iflow", e.Identifier()) + if err != nil { + return resp, err + } + + body = preserveReasoningContentInMessages(body) + requestedModel := payloadRequestedModel(opts, req.Model) + body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel) + + endpoint := strings.TrimSuffix(baseURL, "/") + iflowDefaultEndpoint + + httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) + if err != nil { + return resp, err + } + applyIFlowHeaders(httpReq, apiKey, false) + var authID, authLabel, authType, authValue string + if auth != nil { + authID = auth.ID + authLabel = auth.Label + authType, authValue = auth.AccountInfo() + } + recordAPIRequest(ctx, e.cfg, upstreamRequestLog{ + URL: endpoint, + Method: http.MethodPost, + Headers: httpReq.Header.Clone(), + Body: body, + Provider: e.Identifier(), + AuthID: authID, + AuthLabel: authLabel, + AuthType: authType, + AuthValue: authValue, + }) + + httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0) + httpResp, err := httpClient.Do(httpReq) + if err != nil { + recordAPIResponseError(ctx, e.cfg, err) + return resp, err + } + defer func() { + if errClose := httpResp.Body.Close(); errClose != nil { + log.Errorf("iflow executor: close response body error: %v", errClose) + } + }() + recordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone()) + + if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 { + b, _ := io.ReadAll(httpResp.Body) + appendAPIResponseChunk(ctx, e.cfg, b) + logWithRequestID(ctx).Debugf("request error, error status: %d error message: %s", httpResp.StatusCode, summarizeErrorBody(httpResp.Header.Get("Content-Type"), b)) + err = statusErr{code: httpResp.StatusCode, msg: string(b)} + return resp, err + } + + data, err := io.ReadAll(httpResp.Body) + if err != nil { + recordAPIResponseError(ctx, e.cfg, err) + return resp, err + } + appendAPIResponseChunk(ctx, e.cfg, data) + reporter.publish(ctx, parseOpenAIUsage(data)) + // Ensure usage is recorded even if upstream omits usage metadata. + reporter.ensurePublished(ctx) + + var param any + // Note: TranslateNonStream uses req.Model (original with suffix) to preserve + // the original model name in the response for client compatibility. + out := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, data, ¶m) + resp = cliproxyexecutor.Response{Payload: []byte(out)} + return resp, nil +} + +// ExecuteStream performs a streaming chat completion request. +func (e *IFlowExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (stream <-chan cliproxyexecutor.StreamChunk, err error) { + baseModel := thinking.ParseSuffix(req.Model).ModelName + + apiKey, baseURL := iflowCreds(auth) + if strings.TrimSpace(apiKey) == "" { + err = fmt.Errorf("iflow executor: missing api key") + return nil, err + } + if baseURL == "" { + baseURL = iflowauth.DefaultAPIBaseURL + } + + reporter := newUsageReporter(ctx, e.Identifier(), baseModel, auth) + defer reporter.trackFailure(ctx, &err) + + from := opts.SourceFormat + to := sdktranslator.FromString("openai") + originalPayload := bytes.Clone(req.Payload) + if len(opts.OriginalRequest) > 0 { + originalPayload = bytes.Clone(opts.OriginalRequest) + } + originalTranslated := sdktranslator.TranslateRequest(from, to, baseModel, originalPayload, true) + body := sdktranslator.TranslateRequest(from, to, baseModel, bytes.Clone(req.Payload), true) + body, _ = sjson.SetBytes(body, "model", baseModel) + + body, err = thinking.ApplyThinking(body, req.Model, from.String(), "iflow", e.Identifier()) + if err != nil { + return nil, err + } + + body = preserveReasoningContentInMessages(body) + // Ensure tools array exists to avoid provider quirks similar to Qwen's behaviour. + toolsResult := gjson.GetBytes(body, "tools") + if toolsResult.Exists() && toolsResult.IsArray() && len(toolsResult.Array()) == 0 { + body = ensureToolsArray(body) + } + requestedModel := payloadRequestedModel(opts, req.Model) + body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel) + + endpoint := strings.TrimSuffix(baseURL, "/") + iflowDefaultEndpoint + + httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) + if err != nil { + return nil, err + } + applyIFlowHeaders(httpReq, apiKey, true) + var authID, authLabel, authType, authValue string + if auth != nil { + authID = auth.ID + authLabel = auth.Label + authType, authValue = auth.AccountInfo() + } + recordAPIRequest(ctx, e.cfg, upstreamRequestLog{ + URL: endpoint, + Method: http.MethodPost, + Headers: httpReq.Header.Clone(), + Body: body, + Provider: e.Identifier(), + AuthID: authID, + AuthLabel: authLabel, + AuthType: authType, + AuthValue: authValue, + }) + + httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0) + httpResp, err := httpClient.Do(httpReq) + if err != nil { + recordAPIResponseError(ctx, e.cfg, err) + return nil, err + } + + recordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone()) + if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 { + data, _ := io.ReadAll(httpResp.Body) + if errClose := httpResp.Body.Close(); errClose != nil { + log.Errorf("iflow executor: close response body error: %v", errClose) + } + appendAPIResponseChunk(ctx, e.cfg, data) + logWithRequestID(ctx).Debugf("request error, error status: %d error message: %s", httpResp.StatusCode, summarizeErrorBody(httpResp.Header.Get("Content-Type"), data)) + err = statusErr{code: httpResp.StatusCode, msg: string(data)} + return nil, err + } + + out := make(chan cliproxyexecutor.StreamChunk) + stream = out + go func() { + defer close(out) + defer func() { + if errClose := httpResp.Body.Close(); errClose != nil { + log.Errorf("iflow executor: close response body error: %v", errClose) + } + }() + + scanner := bufio.NewScanner(httpResp.Body) + scanner.Buffer(nil, 52_428_800) // 50MB + var param any + for scanner.Scan() { + line := scanner.Bytes() + appendAPIResponseChunk(ctx, e.cfg, line) + if detail, ok := parseOpenAIStreamUsage(line); ok { + reporter.publish(ctx, detail) + } + chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), ¶m) + for i := range chunks { + out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])} + } + } + if errScan := scanner.Err(); errScan != nil { + recordAPIResponseError(ctx, e.cfg, errScan) + reporter.publishFailure(ctx) + out <- cliproxyexecutor.StreamChunk{Err: errScan} + } + // Guarantee a usage record exists even if the stream never emitted usage data. + reporter.ensurePublished(ctx) + }() + + return stream, nil +} + +func (e *IFlowExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + baseModel := thinking.ParseSuffix(req.Model).ModelName + + from := opts.SourceFormat + to := sdktranslator.FromString("openai") + body := sdktranslator.TranslateRequest(from, to, baseModel, bytes.Clone(req.Payload), false) + + enc, err := tokenizerForModel(baseModel) + if err != nil { + return cliproxyexecutor.Response{}, fmt.Errorf("iflow executor: tokenizer init failed: %w", err) + } + + count, err := countOpenAIChatTokens(enc, body) + if err != nil { + return cliproxyexecutor.Response{}, fmt.Errorf("iflow executor: token counting failed: %w", err) + } + + usageJSON := buildOpenAIUsageJSON(count) + translated := sdktranslator.TranslateTokenCount(ctx, to, from, count, usageJSON) + return cliproxyexecutor.Response{Payload: []byte(translated)}, nil +} + +// Refresh refreshes OAuth tokens or cookie-based API keys and updates the stored API key. +func (e *IFlowExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) { + log.Debugf("iflow executor: refresh called") + if auth == nil { + return nil, fmt.Errorf("iflow executor: auth is nil") + } + + // Check if this is cookie-based authentication + var cookie string + var email string + if auth.Metadata != nil { + if v, ok := auth.Metadata["cookie"].(string); ok { + cookie = strings.TrimSpace(v) + } + if v, ok := auth.Metadata["email"].(string); ok { + email = strings.TrimSpace(v) + } + } + + // If cookie is present, use cookie-based refresh + if cookie != "" && email != "" { + return e.refreshCookieBased(ctx, auth, cookie, email) + } + + // Otherwise, use OAuth-based refresh + return e.refreshOAuthBased(ctx, auth) +} + +// refreshCookieBased refreshes API key using browser cookie +func (e *IFlowExecutor) refreshCookieBased(ctx context.Context, auth *cliproxyauth.Auth, cookie, email string) (*cliproxyauth.Auth, error) { + log.Debugf("iflow executor: checking refresh need for cookie-based API key for user: %s", email) + + // Get current expiry time from metadata + var currentExpire string + if auth.Metadata != nil { + if v, ok := auth.Metadata["expired"].(string); ok { + currentExpire = strings.TrimSpace(v) + } + } + + // Check if refresh is needed + needsRefresh, _, err := iflowauth.ShouldRefreshAPIKey(currentExpire) + if err != nil { + log.Warnf("iflow executor: failed to check refresh need: %v", err) + // If we can't check, continue with refresh anyway as a safety measure + } else if !needsRefresh { + log.Debugf("iflow executor: no refresh needed for user: %s", email) + return auth, nil + } + + log.Infof("iflow executor: refreshing cookie-based API key for user: %s", email) + + svc := iflowauth.NewIFlowAuth(e.cfg) + keyData, err := svc.RefreshAPIKey(ctx, cookie, email) + if err != nil { + log.Errorf("iflow executor: cookie-based API key refresh failed: %v", err) + return nil, err + } + + if auth.Metadata == nil { + auth.Metadata = make(map[string]any) + } + auth.Metadata["api_key"] = keyData.APIKey + auth.Metadata["expired"] = keyData.ExpireTime + auth.Metadata["type"] = "iflow" + auth.Metadata["last_refresh"] = time.Now().Format(time.RFC3339) + auth.Metadata["cookie"] = cookie + auth.Metadata["email"] = email + + log.Infof("iflow executor: cookie-based API key refreshed successfully, new expiry: %s", keyData.ExpireTime) + + if auth.Attributes == nil { + auth.Attributes = make(map[string]string) + } + auth.Attributes["api_key"] = keyData.APIKey + + return auth, nil +} + +// refreshOAuthBased refreshes tokens using OAuth refresh token +func (e *IFlowExecutor) refreshOAuthBased(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) { + refreshToken := "" + oldAccessToken := "" + if auth.Metadata != nil { + if v, ok := auth.Metadata["refresh_token"].(string); ok { + refreshToken = strings.TrimSpace(v) + } + if v, ok := auth.Metadata["access_token"].(string); ok { + oldAccessToken = strings.TrimSpace(v) + } + } + if refreshToken == "" { + return auth, nil + } + + // Log the old access token (masked) before refresh + if oldAccessToken != "" { + log.Debugf("iflow executor: refreshing access token, old: %s", util.HideAPIKey(oldAccessToken)) + } + + svc := iflowauth.NewIFlowAuth(e.cfg) + tokenData, err := svc.RefreshTokens(ctx, refreshToken) + if err != nil { + log.Errorf("iflow executor: token refresh failed: %v", err) + return nil, err + } + + if auth.Metadata == nil { + auth.Metadata = make(map[string]any) + } + auth.Metadata["access_token"] = tokenData.AccessToken + if tokenData.RefreshToken != "" { + auth.Metadata["refresh_token"] = tokenData.RefreshToken + } + if tokenData.APIKey != "" { + auth.Metadata["api_key"] = tokenData.APIKey + } + auth.Metadata["expired"] = tokenData.Expire + auth.Metadata["type"] = "iflow" + auth.Metadata["last_refresh"] = time.Now().Format(time.RFC3339) + + // Log the new access token (masked) after successful refresh + log.Debugf("iflow executor: token refresh successful, new: %s", util.HideAPIKey(tokenData.AccessToken)) + + if auth.Attributes == nil { + auth.Attributes = make(map[string]string) + } + if tokenData.APIKey != "" { + auth.Attributes["api_key"] = tokenData.APIKey + } + + return auth, nil +} + +func applyIFlowHeaders(r *http.Request, apiKey string, stream bool) { + r.Header.Set("Content-Type", "application/json") + r.Header.Set("Authorization", "Bearer "+apiKey) + r.Header.Set("User-Agent", iflowUserAgent) + if stream { + r.Header.Set("Accept", "text/event-stream") + } else { + r.Header.Set("Accept", "application/json") + } +} + +func iflowCreds(a *cliproxyauth.Auth) (apiKey, baseURL string) { + if a == nil { + return "", "" + } + if a.Attributes != nil { + if v := strings.TrimSpace(a.Attributes["api_key"]); v != "" { + apiKey = v + } + if v := strings.TrimSpace(a.Attributes["base_url"]); v != "" { + baseURL = v + } + } + if apiKey == "" && a.Metadata != nil { + if v, ok := a.Metadata["api_key"].(string); ok { + apiKey = strings.TrimSpace(v) + } + } + if baseURL == "" && a.Metadata != nil { + if v, ok := a.Metadata["base_url"].(string); ok { + baseURL = strings.TrimSpace(v) + } + } + return apiKey, baseURL +} + +func ensureToolsArray(body []byte) []byte { + placeholder := `[{"type":"function","function":{"name":"noop","description":"Placeholder tool to stabilise streaming","parameters":{"type":"object"}}}]` + updated, err := sjson.SetRawBytes(body, "tools", []byte(placeholder)) + if err != nil { + return body + } + return updated +} + +// preserveReasoningContentInMessages checks if reasoning_content from assistant messages +// is preserved in conversation history for iFlow models that support thinking. +// This is helpful for multi-turn conversations where the model may benefit from seeing +// its previous reasoning to maintain coherent thought chains. +// +// For GLM-4.6/4.7 and MiniMax M2/M2.1, it is recommended to include the full assistant +// response (including reasoning_content) in message history for better context continuity. +func preserveReasoningContentInMessages(body []byte) []byte { + model := strings.ToLower(gjson.GetBytes(body, "model").String()) + + // Only apply to models that support thinking with history preservation + needsPreservation := strings.HasPrefix(model, "glm-4") || strings.HasPrefix(model, "minimax-m2") + + if !needsPreservation { + return body + } + + messages := gjson.GetBytes(body, "messages") + if !messages.Exists() || !messages.IsArray() { + return body + } + + // Check if any assistant message already has reasoning_content preserved + hasReasoningContent := false + messages.ForEach(func(_, msg gjson.Result) bool { + role := msg.Get("role").String() + if role == "assistant" { + rc := msg.Get("reasoning_content") + if rc.Exists() && rc.String() != "" { + hasReasoningContent = true + return false // stop iteration + } + } + return true + }) + + // If reasoning content is already present, the messages are properly formatted + // No need to modify - the client has correctly preserved reasoning in history + if hasReasoningContent { + log.Debugf("iflow executor: reasoning_content found in message history for %s", model) + } + + return body +} diff --git a/internal/runtime/executor/iflow_executor_test.go b/internal/runtime/executor/iflow_executor_test.go new file mode 100644 index 00000000..e588548b --- /dev/null +++ b/internal/runtime/executor/iflow_executor_test.go @@ -0,0 +1,67 @@ +package executor + +import ( + "testing" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" +) + +func TestIFlowExecutorParseSuffix(t *testing.T) { + tests := []struct { + name string + model string + wantBase string + wantLevel string + }{ + {"no suffix", "glm-4", "glm-4", ""}, + {"glm with suffix", "glm-4.1-flash(high)", "glm-4.1-flash", "high"}, + {"minimax no suffix", "minimax-m2", "minimax-m2", ""}, + {"minimax with suffix", "minimax-m2.1(medium)", "minimax-m2.1", "medium"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := thinking.ParseSuffix(tt.model) + if result.ModelName != tt.wantBase { + t.Errorf("ParseSuffix(%q).ModelName = %q, want %q", tt.model, result.ModelName, tt.wantBase) + } + }) + } +} + +func TestPreserveReasoningContentInMessages(t *testing.T) { + tests := []struct { + name string + input []byte + want []byte // nil means output should equal input + }{ + { + "non-glm model passthrough", + []byte(`{"model":"gpt-4","messages":[]}`), + nil, + }, + { + "glm model with empty messages", + []byte(`{"model":"glm-4","messages":[]}`), + nil, + }, + { + "glm model preserves existing reasoning_content", + []byte(`{"model":"glm-4","messages":[{"role":"assistant","content":"hi","reasoning_content":"thinking..."}]}`), + nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := preserveReasoningContentInMessages(tt.input) + want := tt.want + if want == nil { + want = tt.input + } + if string(got) != string(want) { + t.Errorf("preserveReasoningContentInMessages() = %s, want %s", got, want) + } + }) + } +} diff --git a/internal/runtime/executor/kiro_executor.go b/internal/runtime/executor/kiro_executor.go index 00e5cdfd..57574268 100644 --- a/internal/runtime/executor/kiro_executor.go +++ b/internal/runtime/executor/kiro_executor.go @@ -7,13 +7,16 @@ import ( "encoding/base64" "encoding/binary" "encoding/json" + "errors" "fmt" "io" + "net" "net/http" "os" "path/filepath" "strings" "sync" + "syscall" "time" "github.com/google/uuid" @@ -28,7 +31,6 @@ import ( "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage" sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" log "github.com/sirupsen/logrus" - ) const ( @@ -54,8 +56,28 @@ const ( kiroIDEUserAgent = "aws-sdk-js/1.0.18 ua/2.1 os/darwin#25.0.0 lang/js md/nodejs#20.16.0 api/codewhispererstreaming#1.0.18 m/E KiroIDE-0.2.13-66c23a8c5d15afabec89ef9954ef52a119f10d369df04d548fc6c1eac694b0d1" kiroIDEAmzUserAgent = "aws-sdk-js/1.0.18 KiroIDE-0.2.13-66c23a8c5d15afabec89ef9954ef52a119f10d369df04d548fc6c1eac694b0d1" kiroIDEAgentModeSpec = "spec" + + // Socket retry configuration constants (based on kiro2Api reference implementation) + // Maximum number of retry attempts for socket/network errors + kiroSocketMaxRetries = 3 + // Base delay between retry attempts (uses exponential backoff: delay * 2^attempt) + kiroSocketBaseRetryDelay = 1 * time.Second + // Maximum delay between retry attempts (cap for exponential backoff) + kiroSocketMaxRetryDelay = 30 * time.Second + // First token timeout for streaming responses (how long to wait for first response) + kiroFirstTokenTimeout = 15 * time.Second + // Streaming read timeout (how long to wait between chunks) + kiroStreamingReadTimeout = 300 * time.Second ) +// retryableHTTPStatusCodes defines HTTP status codes that are considered retryable. +// Based on kiro2Api reference: 502 (Bad Gateway), 503 (Service Unavailable), 504 (Gateway Timeout) +var retryableHTTPStatusCodes = map[int]bool{ + 502: true, // Bad Gateway - upstream server error + 503: true, // Service Unavailable - server temporarily overloaded + 504: true, // Gateway Timeout - upstream server timeout +} + // Real-time usage estimation configuration // These control how often usage updates are sent during streaming var ( @@ -63,6 +85,241 @@ var ( usageUpdateTimeInterval = 15 * time.Second // Or every 15 seconds, whichever comes first ) +// Global FingerprintManager for dynamic User-Agent generation per token +// Each token gets a unique fingerprint on first use, which is cached for subsequent requests +var ( + globalFingerprintManager *kiroauth.FingerprintManager + globalFingerprintManagerOnce sync.Once +) + +// getGlobalFingerprintManager returns the global FingerprintManager instance +func getGlobalFingerprintManager() *kiroauth.FingerprintManager { + globalFingerprintManagerOnce.Do(func() { + globalFingerprintManager = kiroauth.NewFingerprintManager() + log.Infof("kiro: initialized global FingerprintManager for dynamic UA generation") + }) + return globalFingerprintManager +} + +// retryConfig holds configuration for socket retry logic. +// Based on kiro2Api Python implementation patterns. +type retryConfig struct { + MaxRetries int // Maximum number of retry attempts + BaseDelay time.Duration // Base delay between retries (exponential backoff) + MaxDelay time.Duration // Maximum delay cap + RetryableErrors []string // List of retryable error patterns + RetryableStatus map[int]bool // HTTP status codes to retry + FirstTokenTmout time.Duration // Timeout for first token in streaming + StreamReadTmout time.Duration // Timeout between stream chunks +} + +// defaultRetryConfig returns the default retry configuration for Kiro socket operations. +func defaultRetryConfig() retryConfig { + return retryConfig{ + MaxRetries: kiroSocketMaxRetries, + BaseDelay: kiroSocketBaseRetryDelay, + MaxDelay: kiroSocketMaxRetryDelay, + RetryableStatus: retryableHTTPStatusCodes, + RetryableErrors: []string{ + "connection reset", + "connection refused", + "broken pipe", + "EOF", + "timeout", + "temporary failure", + "no such host", + "network is unreachable", + "i/o timeout", + }, + FirstTokenTmout: kiroFirstTokenTimeout, + StreamReadTmout: kiroStreamingReadTimeout, + } +} + +// isRetryableError checks if an error is retryable based on error type and message. +// Returns true for network timeouts, connection resets, and temporary failures. +// Based on kiro2Api's retry logic patterns. +func isRetryableError(err error) bool { + if err == nil { + return false + } + + // Check for context cancellation - not retryable + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + return false + } + + // Check for net.Error (timeout, temporary) + var netErr net.Error + if errors.As(err, &netErr) { + if netErr.Timeout() { + log.Debugf("kiro: isRetryableError: network timeout detected") + return true + } + // Note: Temporary() is deprecated but still useful for some error types + } + + // Check for specific syscall errors (connection reset, broken pipe, etc.) + var syscallErr syscall.Errno + if errors.As(err, &syscallErr) { + switch syscallErr { + case syscall.ECONNRESET: // Connection reset by peer + log.Debugf("kiro: isRetryableError: ECONNRESET detected") + return true + case syscall.ECONNREFUSED: // Connection refused + log.Debugf("kiro: isRetryableError: ECONNREFUSED detected") + return true + case syscall.EPIPE: // Broken pipe + log.Debugf("kiro: isRetryableError: EPIPE (broken pipe) detected") + return true + case syscall.ETIMEDOUT: // Connection timed out + log.Debugf("kiro: isRetryableError: ETIMEDOUT detected") + return true + case syscall.ENETUNREACH: // Network is unreachable + log.Debugf("kiro: isRetryableError: ENETUNREACH detected") + return true + case syscall.EHOSTUNREACH: // No route to host + log.Debugf("kiro: isRetryableError: EHOSTUNREACH detected") + return true + } + } + + // Check for net.OpError wrapping other errors + var opErr *net.OpError + if errors.As(err, &opErr) { + log.Debugf("kiro: isRetryableError: net.OpError detected, op=%s", opErr.Op) + // Recursively check the wrapped error + if opErr.Err != nil { + return isRetryableError(opErr.Err) + } + return true + } + + // Check error message for retryable patterns + errMsg := strings.ToLower(err.Error()) + cfg := defaultRetryConfig() + for _, pattern := range cfg.RetryableErrors { + if strings.Contains(errMsg, pattern) { + log.Debugf("kiro: isRetryableError: pattern '%s' matched in error: %s", pattern, errMsg) + return true + } + } + + // Check for EOF which may indicate connection was closed + if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { + log.Debugf("kiro: isRetryableError: EOF/UnexpectedEOF detected") + return true + } + + return false +} + +// isRetryableHTTPStatus checks if an HTTP status code is retryable. +// Based on kiro2Api: 502, 503, 504 are retryable server errors. +func isRetryableHTTPStatus(statusCode int) bool { + return retryableHTTPStatusCodes[statusCode] +} + +// calculateRetryDelay calculates the delay for the next retry attempt using exponential backoff. +// delay = min(baseDelay * 2^attempt, maxDelay) +// Adds ±30% jitter to prevent thundering herd. +func calculateRetryDelay(attempt int, cfg retryConfig) time.Duration { + return kiroauth.ExponentialBackoffWithJitter(attempt, cfg.BaseDelay, cfg.MaxDelay) +} + +// logRetryAttempt logs a retry attempt with relevant context. +func logRetryAttempt(attempt, maxRetries int, reason string, delay time.Duration, endpoint string) { + log.Warnf("kiro: retry attempt %d/%d for %s, waiting %v before next attempt (endpoint: %s)", + attempt+1, maxRetries, reason, delay, endpoint) +} + +// kiroHTTPClientPool provides a shared HTTP client with connection pooling for Kiro API. +// This reduces connection overhead and improves performance for concurrent requests. +// Based on kiro2Api's connection pooling pattern. +var ( + kiroHTTPClientPool *http.Client + kiroHTTPClientPoolOnce sync.Once +) + +// getKiroPooledHTTPClient returns a shared HTTP client with optimized connection pooling. +// The client is lazily initialized on first use and reused across requests. +// This is especially beneficial for: +// - Reducing TCP handshake overhead +// - Enabling HTTP/2 multiplexing +// - Better handling of keep-alive connections +func getKiroPooledHTTPClient() *http.Client { + kiroHTTPClientPoolOnce.Do(func() { + transport := &http.Transport{ + // Connection pool settings + MaxIdleConns: 100, // Max idle connections across all hosts + MaxIdleConnsPerHost: 20, // Max idle connections per host + MaxConnsPerHost: 50, // Max total connections per host + IdleConnTimeout: 90 * time.Second, // How long idle connections stay in pool + + // Timeouts for connection establishment + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, // TCP connection timeout + KeepAlive: 30 * time.Second, // TCP keep-alive interval + }).DialContext, + + // TLS handshake timeout + TLSHandshakeTimeout: 10 * time.Second, + + // Response header timeout + ResponseHeaderTimeout: 30 * time.Second, + + // Expect 100-continue timeout + ExpectContinueTimeout: 1 * time.Second, + + // Enable HTTP/2 when available + ForceAttemptHTTP2: true, + } + + kiroHTTPClientPool = &http.Client{ + Transport: transport, + // No global timeout - let individual requests set their own timeouts via context + } + + log.Debugf("kiro: initialized pooled HTTP client (MaxIdleConns=%d, MaxIdleConnsPerHost=%d, MaxConnsPerHost=%d)", + transport.MaxIdleConns, transport.MaxIdleConnsPerHost, transport.MaxConnsPerHost) + }) + + return kiroHTTPClientPool +} + +// newKiroHTTPClientWithPooling creates an HTTP client that uses connection pooling when appropriate. +// It respects proxy configuration from auth or config, falling back to the pooled client. +// This provides the best of both worlds: custom proxy support + connection reuse. +func newKiroHTTPClientWithPooling(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) *http.Client { + // Check if a proxy is configured - if so, we need a custom client + var proxyURL string + if auth != nil { + proxyURL = strings.TrimSpace(auth.ProxyURL) + } + if proxyURL == "" && cfg != nil { + proxyURL = strings.TrimSpace(cfg.ProxyURL) + } + + // If proxy is configured, use the existing proxy-aware client (doesn't pool) + if proxyURL != "" { + log.Debugf("kiro: using proxy-aware HTTP client (proxy=%s)", proxyURL) + return newProxyAwareHTTPClient(ctx, cfg, auth, timeout) + } + + // No proxy - use pooled client for better performance + pooledClient := getKiroPooledHTTPClient() + + // If timeout is specified, we need to wrap the pooled transport with timeout + if timeout > 0 { + return &http.Client{ + Transport: pooledClient.Transport, + Timeout: timeout, + } + } + + return pooledClient +} + // kiroEndpointConfig bundles endpoint URL with its compatible Origin and AmzTarget values. // This solves the "triple mismatch" problem where different endpoints require matching // Origin and X-Amz-Target header values. @@ -217,8 +474,81 @@ func NewKiroExecutor(cfg *config.Config) *KiroExecutor { // Identifier returns the unique identifier for this executor. func (e *KiroExecutor) Identifier() string { return "kiro" } +// applyDynamicFingerprint applies token-specific fingerprint headers to the request +// For IDC auth, uses dynamic fingerprint-based User-Agent +// For other auth types, uses static Amazon Q CLI style headers +func applyDynamicFingerprint(req *http.Request, auth *cliproxyauth.Auth) { + if isIDCAuth(auth) { + // Get token-specific fingerprint for dynamic UA generation + tokenKey := getTokenKey(auth) + fp := getGlobalFingerprintManager().GetFingerprint(tokenKey) + + // Use fingerprint-generated dynamic User-Agent + req.Header.Set("User-Agent", fp.BuildUserAgent()) + req.Header.Set("X-Amz-User-Agent", fp.BuildAmzUserAgent()) + req.Header.Set("x-amzn-kiro-agent-mode", kiroIDEAgentModeSpec) + + log.Debugf("kiro: using dynamic fingerprint for token %s (SDK:%s, OS:%s/%s, Kiro:%s)", + tokenKey[:8]+"...", fp.SDKVersion, fp.OSType, fp.OSVersion, fp.KiroVersion) + } else { + // Use static Amazon Q CLI style headers for non-IDC auth + req.Header.Set("User-Agent", kiroUserAgent) + req.Header.Set("X-Amz-User-Agent", kiroFullUserAgent) + } +} + // PrepareRequest prepares the HTTP request before execution. -func (e *KiroExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error { return nil } +func (e *KiroExecutor) PrepareRequest(req *http.Request, auth *cliproxyauth.Auth) error { + if req == nil { + return nil + } + accessToken, _ := kiroCredentials(auth) + if strings.TrimSpace(accessToken) == "" { + return statusErr{code: http.StatusUnauthorized, msg: "missing access token"} + } + + // Apply dynamic fingerprint-based headers + applyDynamicFingerprint(req, auth) + + req.Header.Set("Amz-Sdk-Request", "attempt=1; max=3") + req.Header.Set("Amz-Sdk-Invocation-Id", uuid.New().String()) + req.Header.Set("Authorization", "Bearer "+accessToken) + var attrs map[string]string + if auth != nil { + attrs = auth.Attributes + } + util.ApplyCustomHeadersFromAttrs(req, attrs) + return nil +} + +// HttpRequest injects Kiro credentials into the request and executes it. +func (e *KiroExecutor) HttpRequest(ctx context.Context, auth *cliproxyauth.Auth, req *http.Request) (*http.Response, error) { + if req == nil { + return nil, fmt.Errorf("kiro executor: request is nil") + } + if ctx == nil { + ctx = req.Context() + } + httpReq := req.WithContext(ctx) + if errPrepare := e.PrepareRequest(httpReq, auth); errPrepare != nil { + return nil, errPrepare + } + httpClient := newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 0) + return httpClient.Do(httpReq) +} + +// getTokenKey returns a unique key for rate limiting based on auth credentials. +// Uses auth ID if available, otherwise falls back to a hash of the access token. +func getTokenKey(auth *cliproxyauth.Auth) string { + if auth != nil && auth.ID != "" { + return auth.ID + } + accessToken, _ := kiroCredentials(auth) + if len(accessToken) > 16 { + return accessToken[:16] + } + return accessToken +} // Execute sends the request to Kiro API and returns the response. // Supports automatic token refresh on 401/403 errors. @@ -228,23 +558,53 @@ func (e *KiroExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req return resp, fmt.Errorf("kiro: access token not found in auth") } + // Rate limiting: get token key for tracking + tokenKey := getTokenKey(auth) + rateLimiter := kiroauth.GetGlobalRateLimiter() + cooldownMgr := kiroauth.GetGlobalCooldownManager() + + // Check if token is in cooldown period + if cooldownMgr.IsInCooldown(tokenKey) { + remaining := cooldownMgr.GetRemainingCooldown(tokenKey) + reason := cooldownMgr.GetCooldownReason(tokenKey) + log.Warnf("kiro: token %s is in cooldown (reason: %s), remaining: %v", tokenKey, reason, remaining) + return resp, fmt.Errorf("kiro: token is in cooldown for %v (reason: %s)", remaining, reason) + } + + // Wait for rate limiter before proceeding + log.Debugf("kiro: waiting for rate limiter for token %s", tokenKey) + rateLimiter.WaitForToken(tokenKey) + log.Debugf("kiro: rate limiter cleared for token %s", tokenKey) + reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) defer reporter.trackFailure(ctx, &err) // Check if token is expired before making request if e.isTokenExpired(accessToken) { - log.Infof("kiro: access token expired, attempting refresh before request") - refreshedAuth, refreshErr := e.Refresh(ctx, auth) - if refreshErr != nil { - log.Warnf("kiro: pre-request token refresh failed: %v", refreshErr) - } else if refreshedAuth != nil { - auth = refreshedAuth - // Persist the refreshed auth to file so subsequent requests use it - if persistErr := e.persistRefreshedAuth(auth); persistErr != nil { - log.Warnf("kiro: failed to persist refreshed auth: %v", persistErr) - } + log.Infof("kiro: access token expired, attempting recovery") + + // 方案 B: 先尝试从文件重新加载 token(后台刷新器可能已更新文件) + reloadedAuth, reloadErr := e.reloadAuthFromFile(auth) + if reloadErr == nil && reloadedAuth != nil { + // 文件中有更新的 token,使用它 + auth = reloadedAuth accessToken, profileArn = kiroCredentials(auth) - log.Infof("kiro: token refreshed successfully before request") + log.Infof("kiro: recovered token from file (background refresh), expires_at: %v", auth.Metadata["expires_at"]) + } else { + // 文件中的 token 也过期了,执行主动刷新 + log.Debugf("kiro: file reload failed (%v), attempting active refresh", reloadErr) + refreshedAuth, refreshErr := e.Refresh(ctx, auth) + if refreshErr != nil { + log.Warnf("kiro: pre-request token refresh failed: %v", refreshErr) + } else if refreshedAuth != nil { + auth = refreshedAuth + // Persist the refreshed auth to file so subsequent requests use it + if persistErr := e.persistRefreshedAuth(auth); persistErr != nil { + log.Warnf("kiro: failed to persist refreshed auth: %v", persistErr) + } + accessToken, profileArn = kiroCredentials(auth) + log.Infof("kiro: token refreshed successfully before request") + } } } @@ -260,7 +620,7 @@ func (e *KiroExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req // Execute with retry on 401/403 and 429 (quota exhausted) // Note: currentOrigin and kiroPayload are built inside executeWithRetry for each endpoint - resp, err = e.executeWithRetry(ctx, auth, req, opts, accessToken, effectiveProfileArn, nil, body, from, to, reporter, "", kiroModelID, isAgentic, isChatOnly) + resp, err = e.executeWithRetry(ctx, auth, req, opts, accessToken, effectiveProfileArn, nil, body, from, to, reporter, "", kiroModelID, isAgentic, isChatOnly, tokenKey) return resp, err } @@ -269,9 +629,12 @@ func (e *KiroExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req // - Amazon Q endpoint (CLI origin) uses Amazon Q Developer quota // - CodeWhisperer endpoint (AI_EDITOR origin) uses Kiro IDE quota // Also supports multi-endpoint fallback similar to Antigravity implementation. -func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, accessToken, profileArn string, kiroPayload, body []byte, from, to sdktranslator.Format, reporter *usageReporter, currentOrigin, kiroModelID string, isAgentic, isChatOnly bool) (cliproxyexecutor.Response, error) { +// tokenKey is used for rate limiting and cooldown tracking. +func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, accessToken, profileArn string, kiroPayload, body []byte, from, to sdktranslator.Format, reporter *usageReporter, currentOrigin, kiroModelID string, isAgentic, isChatOnly bool, tokenKey string) (cliproxyexecutor.Response, error) { var resp cliproxyexecutor.Response maxRetries := 2 // Allow retries for token refresh + endpoint fallback + rateLimiter := kiroauth.GetGlobalRateLimiter() + cooldownMgr := kiroauth.GetGlobalCooldownManager() endpointConfigs := getKiroEndpointConfigs(auth) var last429Err error @@ -289,6 +652,12 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. endpointIdx+1, len(endpointConfigs), url, endpointConfig.Name, currentOrigin) for attempt := 0; attempt <= maxRetries; attempt++ { + // Apply human-like delay before first request (not on retries) + // This mimics natural user behavior patterns + if attempt == 0 && endpointIdx == 0 { + kiroauth.ApplyHumanLikeDelay() + } + httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(kiroPayload)) if err != nil { return resp, err @@ -299,18 +668,9 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. // Use endpoint-specific X-Amz-Target (critical for avoiding 403 errors) httpReq.Header.Set("X-Amz-Target", endpointConfig.AmzTarget) - // Use different headers based on auth type - // IDC auth uses Kiro IDE style headers (from kiro2api) - // Other auth types use Amazon Q CLI style headers - if isIDCAuth(auth) { - httpReq.Header.Set("User-Agent", kiroIDEUserAgent) - httpReq.Header.Set("X-Amz-User-Agent", kiroIDEAmzUserAgent) - httpReq.Header.Set("x-amzn-kiro-agent-mode", kiroIDEAgentModeSpec) - log.Debugf("kiro: using Kiro IDE headers for IDC auth") - } else { - httpReq.Header.Set("User-Agent", kiroUserAgent) - httpReq.Header.Set("X-Amz-User-Agent", kiroFullUserAgent) - } + // Apply dynamic fingerprint-based headers + applyDynamicFingerprint(httpReq, auth) + httpReq.Header.Set("Amz-Sdk-Request", "attempt=1; max=3") httpReq.Header.Set("Amz-Sdk-Invocation-Id", uuid.New().String()) @@ -341,10 +701,34 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. AuthValue: authValue, }) - httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 120*time.Second) + httpClient := newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 120*time.Second) httpResp, err := httpClient.Do(httpReq) if err != nil { + // Check for context cancellation first - client disconnected, not a server error + // Use 499 (Client Closed Request - nginx convention) instead of 500 + if errors.Is(err, context.Canceled) { + log.Debugf("kiro: request canceled by client (context.Canceled)") + return resp, statusErr{code: 499, msg: "client canceled request"} + } + + // Check for context deadline exceeded - request timed out + // Return 504 Gateway Timeout instead of 500 + if errors.Is(err, context.DeadlineExceeded) { + log.Debugf("kiro: request timed out (context.DeadlineExceeded)") + return resp, statusErr{code: http.StatusGatewayTimeout, msg: "upstream request timed out"} + } + recordAPIResponseError(ctx, e.cfg, err) + + // Enhanced socket retry: Check if error is retryable (network timeout, connection reset, etc.) + retryCfg := defaultRetryConfig() + if isRetryableError(err) && attempt < retryCfg.MaxRetries { + delay := calculateRetryDelay(attempt, retryCfg) + logRetryAttempt(attempt, retryCfg.MaxRetries, fmt.Sprintf("socket error: %v", err), delay, endpointConfig.Name) + time.Sleep(delay) + continue + } + return resp, err } recordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone()) @@ -356,6 +740,12 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. _ = httpResp.Body.Close() appendAPIResponseChunk(ctx, e.cfg, respBody) + // Record failure and set cooldown for 429 + rateLimiter.MarkTokenFailed(tokenKey) + cooldownDuration := kiroauth.CalculateCooldownFor429(attempt) + cooldownMgr.SetCooldown(tokenKey, cooldownDuration, kiroauth.CooldownReason429) + log.Warnf("kiro: rate limit hit (429), token %s set to cooldown for %v", tokenKey, cooldownDuration) + // Preserve last 429 so callers can correctly backoff when all endpoints are exhausted last429Err = statusErr{code: httpResp.StatusCode, msg: string(respBody)} @@ -367,13 +757,21 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. } // Handle 5xx server errors with exponential backoff retry + // Enhanced: Use retryConfig for consistent retry behavior if httpResp.StatusCode >= 500 && httpResp.StatusCode < 600 { respBody, _ := io.ReadAll(httpResp.Body) _ = httpResp.Body.Close() appendAPIResponseChunk(ctx, e.cfg, respBody) - if attempt < maxRetries { - // Exponential backoff: 1s, 2s, 4s... (max 30s) + retryCfg := defaultRetryConfig() + // Check if this specific 5xx code is retryable (502, 503, 504) + if isRetryableHTTPStatus(httpResp.StatusCode) && attempt < retryCfg.MaxRetries { + delay := calculateRetryDelay(attempt, retryCfg) + logRetryAttempt(attempt, retryCfg.MaxRetries, fmt.Sprintf("HTTP %d", httpResp.StatusCode), delay, endpointConfig.Name) + time.Sleep(delay) + continue + } else if attempt < maxRetries { + // Fallback for other 5xx errors (500, 501, etc.) backoff := time.Duration(1< 30*time.Second { backoff = 30 * time.Second @@ -393,28 +791,28 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. _ = httpResp.Body.Close() appendAPIResponseChunk(ctx, e.cfg, respBody) - if attempt < maxRetries { - log.Warnf("kiro: received 401 error, attempting token refresh and retry (attempt %d/%d)", attempt+1, maxRetries+1) + log.Warnf("kiro: received 401 error, attempting token refresh") + refreshedAuth, refreshErr := e.Refresh(ctx, auth) + if refreshErr != nil { + log.Errorf("kiro: token refresh failed: %v", refreshErr) + return resp, statusErr{code: httpResp.StatusCode, msg: string(respBody)} + } - refreshedAuth, refreshErr := e.Refresh(ctx, auth) - if refreshErr != nil { - log.Errorf("kiro: token refresh failed: %v", refreshErr) - return resp, statusErr{code: httpResp.StatusCode, msg: string(respBody)} + if refreshedAuth != nil { + auth = refreshedAuth + // Persist the refreshed auth to file so subsequent requests use it + if persistErr := e.persistRefreshedAuth(auth); persistErr != nil { + log.Warnf("kiro: failed to persist refreshed auth: %v", persistErr) + // Continue anyway - the token is valid for this request } - - if refreshedAuth != nil { - auth = refreshedAuth - // Persist the refreshed auth to file so subsequent requests use it - if persistErr := e.persistRefreshedAuth(auth); persistErr != nil { - log.Warnf("kiro: failed to persist refreshed auth: %v", persistErr) - // Continue anyway - the token is valid for this request - } - accessToken, profileArn = kiroCredentials(auth) - // Rebuild payload with new profile ARN if changed - kiroPayload, _ = buildKiroPayloadForFormat(body, kiroModelID, profileArn, currentOrigin, isAgentic, isChatOnly, from, opts.Headers) - log.Infof("kiro: token refreshed successfully, retrying request") + accessToken, profileArn = kiroCredentials(auth) + // Rebuild payload with new profile ARN if changed + kiroPayload, _ = buildKiroPayloadForFormat(body, kiroModelID, profileArn, currentOrigin, isAgentic, isChatOnly, from, opts.Headers) + if attempt < maxRetries { + log.Infof("kiro: token refreshed successfully, retrying request (attempt %d/%d)", attempt+1, maxRetries+1) continue } + log.Infof("kiro: token refreshed successfully, no retries remaining") } log.Warnf("kiro request error, status: 401, body: %s", summarizeErrorBody(httpResp.Header.Get("Content-Type"), respBody)) @@ -447,7 +845,10 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. // Check for SUSPENDED status - return immediately without retry if strings.Contains(respBodyStr, "SUSPENDED") || strings.Contains(respBodyStr, "TEMPORARILY_SUSPENDED") { - log.Errorf("kiro: account is suspended, cannot proceed") + // Set long cooldown for suspended accounts + rateLimiter.CheckAndMarkSuspended(tokenKey, respBodyStr) + cooldownMgr.SetCooldown(tokenKey, kiroauth.LongCooldown, kiroauth.CooldownReasonSuspended) + log.Errorf("kiro: account is suspended, token %s set to cooldown for %v", tokenKey, kiroauth.LongCooldown) return resp, statusErr{code: httpResp.StatusCode, msg: "account suspended: " + string(respBody)} } @@ -536,6 +937,10 @@ func (e *KiroExecutor) executeWithRetry(ctx context.Context, auth *cliproxyauth. appendAPIResponseChunk(ctx, e.cfg, []byte(content)) reporter.publish(ctx, usageInfo) + // Record success for rate limiting + rateLimiter.MarkTokenSuccess(tokenKey) + log.Debugf("kiro: request successful, token %s marked as success", tokenKey) + // Build response in Claude format for Kiro translator // stopReason is extracted from upstream response by parseEventStream kiroResponse := kiroclaude.BuildClaudeResponse(content, toolUses, req.Model, usageInfo, stopReason) @@ -563,23 +968,53 @@ func (e *KiroExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Aut return nil, fmt.Errorf("kiro: access token not found in auth") } + // Rate limiting: get token key for tracking + tokenKey := getTokenKey(auth) + rateLimiter := kiroauth.GetGlobalRateLimiter() + cooldownMgr := kiroauth.GetGlobalCooldownManager() + + // Check if token is in cooldown period + if cooldownMgr.IsInCooldown(tokenKey) { + remaining := cooldownMgr.GetRemainingCooldown(tokenKey) + reason := cooldownMgr.GetCooldownReason(tokenKey) + log.Warnf("kiro: token %s is in cooldown (reason: %s), remaining: %v", tokenKey, reason, remaining) + return nil, fmt.Errorf("kiro: token is in cooldown for %v (reason: %s)", remaining, reason) + } + + // Wait for rate limiter before proceeding + log.Debugf("kiro: stream waiting for rate limiter for token %s", tokenKey) + rateLimiter.WaitForToken(tokenKey) + log.Debugf("kiro: stream rate limiter cleared for token %s", tokenKey) + reporter := newUsageReporter(ctx, e.Identifier(), req.Model, auth) defer reporter.trackFailure(ctx, &err) // Check if token is expired before making request if e.isTokenExpired(accessToken) { - log.Infof("kiro: access token expired, attempting refresh before stream request") - refreshedAuth, refreshErr := e.Refresh(ctx, auth) - if refreshErr != nil { - log.Warnf("kiro: pre-request token refresh failed: %v", refreshErr) - } else if refreshedAuth != nil { - auth = refreshedAuth - // Persist the refreshed auth to file so subsequent requests use it - if persistErr := e.persistRefreshedAuth(auth); persistErr != nil { - log.Warnf("kiro: failed to persist refreshed auth: %v", persistErr) - } + log.Infof("kiro: access token expired, attempting recovery before stream request") + + // 方案 B: 先尝试从文件重新加载 token(后台刷新器可能已更新文件) + reloadedAuth, reloadErr := e.reloadAuthFromFile(auth) + if reloadErr == nil && reloadedAuth != nil { + // 文件中有更新的 token,使用它 + auth = reloadedAuth accessToken, profileArn = kiroCredentials(auth) - log.Infof("kiro: token refreshed successfully before stream request") + log.Infof("kiro: recovered token from file (background refresh) for stream, expires_at: %v", auth.Metadata["expires_at"]) + } else { + // 文件中的 token 也过期了,执行主动刷新 + log.Debugf("kiro: file reload failed (%v), attempting active refresh for stream", reloadErr) + refreshedAuth, refreshErr := e.Refresh(ctx, auth) + if refreshErr != nil { + log.Warnf("kiro: pre-request token refresh failed: %v", refreshErr) + } else if refreshedAuth != nil { + auth = refreshedAuth + // Persist the refreshed auth to file so subsequent requests use it + if persistErr := e.persistRefreshedAuth(auth); persistErr != nil { + log.Warnf("kiro: failed to persist refreshed auth: %v", persistErr) + } + accessToken, profileArn = kiroCredentials(auth) + log.Infof("kiro: token refreshed successfully before stream request") + } } } @@ -595,7 +1030,7 @@ func (e *KiroExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Aut // Execute stream with retry on 401/403 and 429 (quota exhausted) // Note: currentOrigin and kiroPayload are built inside executeStreamWithRetry for each endpoint - return e.executeStreamWithRetry(ctx, auth, req, opts, accessToken, effectiveProfileArn, nil, body, from, reporter, "", kiroModelID, isAgentic, isChatOnly) + return e.executeStreamWithRetry(ctx, auth, req, opts, accessToken, effectiveProfileArn, nil, body, from, reporter, "", kiroModelID, isAgentic, isChatOnly, tokenKey) } // executeStreamWithRetry performs the streaming HTTP request with automatic retry on auth errors. @@ -603,8 +1038,11 @@ func (e *KiroExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Aut // - Amazon Q endpoint (CLI origin) uses Amazon Q Developer quota // - CodeWhisperer endpoint (AI_EDITOR origin) uses Kiro IDE quota // Also supports multi-endpoint fallback similar to Antigravity implementation. -func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, accessToken, profileArn string, kiroPayload, body []byte, from sdktranslator.Format, reporter *usageReporter, currentOrigin, kiroModelID string, isAgentic, isChatOnly bool) (<-chan cliproxyexecutor.StreamChunk, error) { +// tokenKey is used for rate limiting and cooldown tracking. +func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, accessToken, profileArn string, kiroPayload, body []byte, from sdktranslator.Format, reporter *usageReporter, currentOrigin, kiroModelID string, isAgentic, isChatOnly bool, tokenKey string) (<-chan cliproxyexecutor.StreamChunk, error) { maxRetries := 2 // Allow retries for token refresh + endpoint fallback + rateLimiter := kiroauth.GetGlobalRateLimiter() + cooldownMgr := kiroauth.GetGlobalCooldownManager() endpointConfigs := getKiroEndpointConfigs(auth) var last429Err error @@ -622,6 +1060,13 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox endpointIdx+1, len(endpointConfigs), url, endpointConfig.Name, currentOrigin) for attempt := 0; attempt <= maxRetries; attempt++ { + // Apply human-like delay before first streaming request (not on retries) + // This mimics natural user behavior patterns + // Note: Delay is NOT applied during streaming response - only before initial request + if attempt == 0 && endpointIdx == 0 { + kiroauth.ApplyHumanLikeDelay() + } + httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(kiroPayload)) if err != nil { return nil, err @@ -632,18 +1077,9 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox // Use endpoint-specific X-Amz-Target (critical for avoiding 403 errors) httpReq.Header.Set("X-Amz-Target", endpointConfig.AmzTarget) - // Use different headers based on auth type - // IDC auth uses Kiro IDE style headers (from kiro2api) - // Other auth types use Amazon Q CLI style headers - if isIDCAuth(auth) { - httpReq.Header.Set("User-Agent", kiroIDEUserAgent) - httpReq.Header.Set("X-Amz-User-Agent", kiroIDEAmzUserAgent) - httpReq.Header.Set("x-amzn-kiro-agent-mode", kiroIDEAgentModeSpec) - log.Debugf("kiro: using Kiro IDE headers for IDC auth") - } else { - httpReq.Header.Set("User-Agent", kiroUserAgent) - httpReq.Header.Set("X-Amz-User-Agent", kiroFullUserAgent) - } + // Apply dynamic fingerprint-based headers + applyDynamicFingerprint(httpReq, auth) + httpReq.Header.Set("Amz-Sdk-Request", "attempt=1; max=3") httpReq.Header.Set("Amz-Sdk-Invocation-Id", uuid.New().String()) @@ -674,10 +1110,20 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox AuthValue: authValue, }) - httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0) + httpClient := newKiroHTTPClientWithPooling(ctx, e.cfg, auth, 0) httpResp, err := httpClient.Do(httpReq) if err != nil { recordAPIResponseError(ctx, e.cfg, err) + + // Enhanced socket retry for streaming: Check if error is retryable (network timeout, connection reset, etc.) + retryCfg := defaultRetryConfig() + if isRetryableError(err) && attempt < retryCfg.MaxRetries { + delay := calculateRetryDelay(attempt, retryCfg) + logRetryAttempt(attempt, retryCfg.MaxRetries, fmt.Sprintf("stream socket error: %v", err), delay, endpointConfig.Name) + time.Sleep(delay) + continue + } + return nil, err } recordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone()) @@ -689,6 +1135,12 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox _ = httpResp.Body.Close() appendAPIResponseChunk(ctx, e.cfg, respBody) + // Record failure and set cooldown for 429 + rateLimiter.MarkTokenFailed(tokenKey) + cooldownDuration := kiroauth.CalculateCooldownFor429(attempt) + cooldownMgr.SetCooldown(tokenKey, cooldownDuration, kiroauth.CooldownReason429) + log.Warnf("kiro: stream rate limit hit (429), token %s set to cooldown for %v", tokenKey, cooldownDuration) + // Preserve last 429 so callers can correctly backoff when all endpoints are exhausted last429Err = statusErr{code: httpResp.StatusCode, msg: string(respBody)} @@ -700,13 +1152,21 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox } // Handle 5xx server errors with exponential backoff retry + // Enhanced: Use retryConfig for consistent retry behavior if httpResp.StatusCode >= 500 && httpResp.StatusCode < 600 { respBody, _ := io.ReadAll(httpResp.Body) _ = httpResp.Body.Close() appendAPIResponseChunk(ctx, e.cfg, respBody) - if attempt < maxRetries { - // Exponential backoff: 1s, 2s, 4s... (max 30s) + retryCfg := defaultRetryConfig() + // Check if this specific 5xx code is retryable (502, 503, 504) + if isRetryableHTTPStatus(httpResp.StatusCode) && attempt < retryCfg.MaxRetries { + delay := calculateRetryDelay(attempt, retryCfg) + logRetryAttempt(attempt, retryCfg.MaxRetries, fmt.Sprintf("stream HTTP %d", httpResp.StatusCode), delay, endpointConfig.Name) + time.Sleep(delay) + continue + } else if attempt < maxRetries { + // Fallback for other 5xx errors (500, 501, etc.) backoff := time.Duration(1< 30*time.Second { backoff = 30 * time.Second @@ -739,28 +1199,28 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox _ = httpResp.Body.Close() appendAPIResponseChunk(ctx, e.cfg, respBody) - if attempt < maxRetries { - log.Warnf("kiro: stream received 401 error, attempting token refresh and retry (attempt %d/%d)", attempt+1, maxRetries+1) + log.Warnf("kiro: stream received 401 error, attempting token refresh") + refreshedAuth, refreshErr := e.Refresh(ctx, auth) + if refreshErr != nil { + log.Errorf("kiro: token refresh failed: %v", refreshErr) + return nil, statusErr{code: httpResp.StatusCode, msg: string(respBody)} + } - refreshedAuth, refreshErr := e.Refresh(ctx, auth) - if refreshErr != nil { - log.Errorf("kiro: token refresh failed: %v", refreshErr) - return nil, statusErr{code: httpResp.StatusCode, msg: string(respBody)} + if refreshedAuth != nil { + auth = refreshedAuth + // Persist the refreshed auth to file so subsequent requests use it + if persistErr := e.persistRefreshedAuth(auth); persistErr != nil { + log.Warnf("kiro: failed to persist refreshed auth: %v", persistErr) + // Continue anyway - the token is valid for this request } - - if refreshedAuth != nil { - auth = refreshedAuth - // Persist the refreshed auth to file so subsequent requests use it - if persistErr := e.persistRefreshedAuth(auth); persistErr != nil { - log.Warnf("kiro: failed to persist refreshed auth: %v", persistErr) - // Continue anyway - the token is valid for this request - } - accessToken, profileArn = kiroCredentials(auth) - // Rebuild payload with new profile ARN if changed - kiroPayload, _ = buildKiroPayloadForFormat(body, kiroModelID, profileArn, currentOrigin, isAgentic, isChatOnly, from, opts.Headers) - log.Infof("kiro: token refreshed successfully, retrying stream request") + accessToken, profileArn = kiroCredentials(auth) + // Rebuild payload with new profile ARN if changed + kiroPayload, _ = buildKiroPayloadForFormat(body, kiroModelID, profileArn, currentOrigin, isAgentic, isChatOnly, from, opts.Headers) + if attempt < maxRetries { + log.Infof("kiro: token refreshed successfully, retrying stream request (attempt %d/%d)", attempt+1, maxRetries+1) continue } + log.Infof("kiro: token refreshed successfully, no retries remaining") } log.Warnf("kiro stream error, status: 401, body: %s", string(respBody)) @@ -793,7 +1253,10 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox // Check for SUSPENDED status - return immediately without retry if strings.Contains(respBodyStr, "SUSPENDED") || strings.Contains(respBodyStr, "TEMPORARILY_SUSPENDED") { - log.Errorf("kiro: account is suspended, cannot proceed") + // Set long cooldown for suspended accounts + rateLimiter.CheckAndMarkSuspended(tokenKey, respBodyStr) + cooldownMgr.SetCooldown(tokenKey, kiroauth.LongCooldown, kiroauth.CooldownReasonSuspended) + log.Errorf("kiro: stream account is suspended, token %s set to cooldown for %v", tokenKey, kiroauth.LongCooldown) return nil, statusErr{code: httpResp.StatusCode, msg: "account suspended: " + string(respBody)} } @@ -843,6 +1306,11 @@ func (e *KiroExecutor) executeStreamWithRetry(ctx context.Context, auth *cliprox out := make(chan cliproxyexecutor.StreamChunk) + // Record success immediately since connection was established successfully + // Streaming errors will be handled separately + rateLimiter.MarkTokenSuccess(tokenKey) + log.Debugf("kiro: stream request successful, token %s marked as success", tokenKey) + go func(resp *http.Response, thinkingEnabled bool) { defer close(out) defer func() { @@ -1004,7 +1472,7 @@ func findRealThinkingEndTag(content string, alreadyInCodeBlock, alreadyInInlineC discussionPatterns := []string{ "标签", "返回", "输出", "包含", "使用", "解析", "转换", "生成", // Chinese "tag", "return", "output", "contain", "use", "parse", "emit", "convert", "generate", // English - "", // discussing both tags together + "", // discussing both tags together "``", // explicitly in inline code } isDiscussion := false @@ -1137,9 +1605,6 @@ func (e *KiroExecutor) mapModelToKiro(model string) string { "kiro-claude-sonnet-4-5-agentic": "claude-sonnet-4.5", "kiro-claude-sonnet-4-agentic": "claude-sonnet-4", "kiro-claude-haiku-4-5-agentic": "claude-haiku-4.5", - // Thinking variants (same backend model IDs, but with thinking enabled) - "kiro-claude-opus-4-5-thinking": "claude-opus-4.5", - "kiro-claude-sonnet-4-5-thinking": "claude-sonnet-4.5", } if kiroID, ok := modelMap[model]; ok { return kiroID @@ -1855,7 +2320,6 @@ func (e *KiroExecutor) extractEventTypeFromBytes(headers []byte) string { return "" } - // NOTE: Response building functions moved to internal/translator/kiro/claude/kiro_claude_response.go // The executor now uses kiroclaude.BuildClaudeResponse() and kiroclaude.ExtractThinkingFromContent() instead @@ -1892,18 +2356,18 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out var lastReportedOutputTokens int64 // Last reported output token count // Upstream usage tracking - Kiro API returns credit usage and context percentage - var upstreamCreditUsage float64 // Credit usage from upstream (e.g., 1.458) - var upstreamContextPercentage float64 // Context usage percentage from upstream (e.g., 78.56) - var hasUpstreamUsage bool // Whether we received usage from upstream + var upstreamCreditUsage float64 // Credit usage from upstream (e.g., 1.458) + var upstreamContextPercentage float64 // Context usage percentage from upstream (e.g., 78.56) + var hasUpstreamUsage bool // Whether we received usage from upstream // Translator param for maintaining tool call state across streaming events // IMPORTANT: This must persist across all TranslateStream calls var translatorParam any // Thinking mode state tracking - tag-based parsing for tags in content - inThinkBlock := false // Whether we're currently inside a block - isThinkingBlockOpen := false // Track if thinking content block SSE event is open - thinkingBlockIndex := -1 // Index of the thinking content block + inThinkBlock := false // Whether we're currently inside a block + isThinkingBlockOpen := false // Track if thinking content block SSE event is open + thinkingBlockIndex := -1 // Index of the thinking content block var accumulatedThinkingContent strings.Builder // Accumulate thinking content for token counting // Buffer for handling partial tag matches at chunk boundaries @@ -2322,16 +2786,16 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out lastUsageUpdateLen = accumulatedContent.Len() lastUsageUpdateTime = time.Now() - } + } - // TAG-BASED THINKING PARSING: Parse tags from content - // Combine pending content with new content for processing - pendingContent.WriteString(contentDelta) - processContent := pendingContent.String() - pendingContent.Reset() + // TAG-BASED THINKING PARSING: Parse tags from content + // Combine pending content with new content for processing + pendingContent.WriteString(contentDelta) + processContent := pendingContent.String() + pendingContent.Reset() - // Process content looking for thinking tags - for len(processContent) > 0 { + // Process content looking for thinking tags + for len(processContent) > 0 { if inThinkBlock { // We're inside a thinking block, look for endIdx := strings.Index(processContent, kirocommon.ThinkingEndTag) @@ -2506,7 +2970,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out processContent = "" } } - } + } } // Handle tool uses in response (with deduplication) @@ -2930,7 +3394,7 @@ func (e *KiroExecutor) streamToChannel(ctx context.Context, body io.Reader, out // Calculate input tokens from context percentage // Using 200k as the base since that's what Kiro reports against calculatedInputTokens := int64(upstreamContextPercentage * 200000 / 100) - + // Only use calculated value if it's significantly different from local estimate // This provides more accurate token counts based on upstream data if calculatedInputTokens > 0 { @@ -3073,14 +3537,14 @@ func (e *KiroExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*c // Also check if expires_at is now in the future with sufficient buffer if expiresAt, ok := auth.Metadata["expires_at"].(string); ok { if expTime, err := time.Parse(time.RFC3339, expiresAt); err == nil { - // If token expires more than 5 minutes from now, it's still valid - if time.Until(expTime) > 5*time.Minute { + // If token expires more than 20 minutes from now, it's still valid + if time.Until(expTime) > 20*time.Minute { log.Debugf("kiro executor: token is still valid (expires in %v), skipping refresh", time.Until(expTime)) // CRITICAL FIX: Set NextRefreshAfter to prevent frequent refresh checks - // Without this, shouldRefresh() will return true again in 5 seconds + // Without this, shouldRefresh() will return true again in 30 seconds updated := auth.Clone() - // Set next refresh to 5 minutes before expiry, or at least 30 seconds from now - nextRefresh := expTime.Add(-5 * time.Minute) + // Set next refresh to 20 minutes before expiry, or at least 30 seconds from now + nextRefresh := expTime.Add(-20 * time.Minute) minNextRefresh := time.Now().Add(30 * time.Second) if nextRefresh.Before(minNextRefresh) { nextRefresh = minNextRefresh @@ -3177,6 +3641,13 @@ func (e *KiroExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*c if tokenData.ClientSecret != "" { updated.Metadata["client_secret"] = tokenData.ClientSecret } + // Preserve region and start_url for IDC token refresh + if tokenData.Region != "" { + updated.Metadata["region"] = tokenData.Region + } + if tokenData.StartURL != "" { + updated.Metadata["start_url"] = tokenData.StartURL + } if updated.Attributes == nil { updated.Attributes = make(map[string]string) @@ -3186,11 +3657,9 @@ func (e *KiroExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*c updated.Attributes["profile_arn"] = tokenData.ProfileArn } - // NextRefreshAfter is aligned with RefreshLead (5min) + // NextRefreshAfter is aligned with RefreshLead (20min) if expiresAt, parseErr := time.Parse(time.RFC3339, tokenData.ExpiresAt); parseErr == nil { - updated.NextRefreshAfter = expiresAt.Add(-5 * time.Minute) - // Update TokenExpiresAt for proactive refresh scheduling. - updated.TokenExpiresAt = expiresAt + updated.NextRefreshAfter = expiresAt.Add(-20 * time.Minute) } log.Infof("kiro executor: token refreshed successfully, expires at %s", tokenData.ExpiresAt) @@ -3244,6 +3713,121 @@ func (e *KiroExecutor) persistRefreshedAuth(auth *cliproxyauth.Auth) error { return nil } +// reloadAuthFromFile 从文件重新加载 auth 数据(方案 B: Fallback 机制) +// 当内存中的 token 已过期时,尝试从文件读取最新的 token +// 这解决了后台刷新器已更新文件但内存中 Auth 对象尚未同步的时间差问题 +func (e *KiroExecutor) reloadAuthFromFile(auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) { + if auth == nil { + return nil, fmt.Errorf("kiro executor: cannot reload nil auth") + } + + // 确定文件路径 + var authPath string + if auth.Attributes != nil { + if p := strings.TrimSpace(auth.Attributes["path"]); p != "" { + authPath = p + } + } + if authPath == "" { + fileName := strings.TrimSpace(auth.FileName) + if fileName == "" { + return nil, fmt.Errorf("kiro executor: auth has no file path or filename for reload") + } + if filepath.IsAbs(fileName) { + authPath = fileName + } else if e.cfg != nil && e.cfg.AuthDir != "" { + authPath = filepath.Join(e.cfg.AuthDir, fileName) + } else { + return nil, fmt.Errorf("kiro executor: cannot determine auth file path for reload") + } + } + + // 读取文件 + raw, err := os.ReadFile(authPath) + if err != nil { + return nil, fmt.Errorf("kiro executor: failed to read auth file %s: %w", authPath, err) + } + + // 解析 JSON + var metadata map[string]any + if err := json.Unmarshal(raw, &metadata); err != nil { + return nil, fmt.Errorf("kiro executor: failed to parse auth file %s: %w", authPath, err) + } + + // 检查文件中的 token 是否比内存中的更新 + fileExpiresAt, _ := metadata["expires_at"].(string) + fileAccessToken, _ := metadata["access_token"].(string) + memExpiresAt, _ := auth.Metadata["expires_at"].(string) + memAccessToken, _ := auth.Metadata["access_token"].(string) + + // 文件中必须有有效的 access_token + if fileAccessToken == "" { + return nil, fmt.Errorf("kiro executor: auth file has no access_token field") + } + + // 如果有 expires_at,检查是否过期 + if fileExpiresAt != "" { + fileExpTime, parseErr := time.Parse(time.RFC3339, fileExpiresAt) + if parseErr == nil { + // 如果文件中的 token 也已过期,不使用它 + if time.Now().After(fileExpTime) { + log.Debugf("kiro executor: file token also expired at %s, not using", fileExpiresAt) + return nil, fmt.Errorf("kiro executor: file token also expired") + } + } + } + + // 判断文件中的 token 是否比内存中的更新 + // 条件1: access_token 不同(说明已刷新) + // 条件2: expires_at 更新(说明已刷新) + isNewer := false + + // 优先检查 access_token 是否变化 + if fileAccessToken != memAccessToken { + isNewer = true + log.Debugf("kiro executor: file access_token differs from memory, using file token") + } + + // 如果 access_token 相同,检查 expires_at + if !isNewer && fileExpiresAt != "" && memExpiresAt != "" { + fileExpTime, fileParseErr := time.Parse(time.RFC3339, fileExpiresAt) + memExpTime, memParseErr := time.Parse(time.RFC3339, memExpiresAt) + if fileParseErr == nil && memParseErr == nil && fileExpTime.After(memExpTime) { + isNewer = true + log.Debugf("kiro executor: file expires_at (%s) is newer than memory (%s)", fileExpiresAt, memExpiresAt) + } + } + + // 如果文件中没有 expires_at 但 access_token 相同,无法判断是否更新 + if !isNewer && fileExpiresAt == "" && fileAccessToken == memAccessToken { + return nil, fmt.Errorf("kiro executor: cannot determine if file token is newer (no expires_at, same access_token)") + } + + if !isNewer { + log.Debugf("kiro executor: file token not newer than memory token") + return nil, fmt.Errorf("kiro executor: file token not newer") + } + + // 创建更新后的 auth 对象 + updated := auth.Clone() + updated.Metadata = metadata + updated.UpdatedAt = time.Now() + + // 同步更新 Attributes + if updated.Attributes == nil { + updated.Attributes = make(map[string]string) + } + if accessToken, ok := metadata["access_token"].(string); ok { + updated.Attributes["access_token"] = accessToken + } + if profileArn, ok := metadata["profile_arn"].(string); ok { + updated.Attributes["profile_arn"] = profileArn + } + + log.Infof("kiro executor: reloaded auth from file %s, new expires_at: %s", authPath, fileExpiresAt) + return updated, nil +} + // isTokenExpired checks if a JWT access token has expired. // Returns true if the token is expired or cannot be parsed. func (e *KiroExecutor) isTokenExpired(accessToken string) bool { @@ -3305,35 +3889,3 @@ func (e *KiroExecutor) isTokenExpired(accessToken string) bool { // NOTE: Message merging functions moved to internal/translator/kiro/common/message_merge.go // NOTE: Tool calling support functions moved to internal/translator/kiro/claude/kiro_claude_tools.go // The executor now uses kiroclaude.* and kirocommon.* functions instead - -// Embed returns an error because AWS CodeWhisperer/Amazon Q Developer does not provide -// an embeddings API. -// -// Background: -// - The Kiro executor uses AWS CodeWhisperer (AmazonCodeWhispererStreamingService) and -// Amazon Q Developer (AmazonQDeveloperStreamingService) APIs for chat/code generation. -// - These services only expose GenerateAssistantResponse and SendMessage endpoints. -// - Text embeddings in AWS are provided through Amazon Bedrock (e.g., Amazon Titan -// Embeddings models), which requires separate AWS IAM authentication - not the OAuth -// tokens used by CodeWhisperer/Amazon Q. -// -// Alternatives for embeddings: -// - Use the Ollama executor with a local embedding model (e.g., nomic-embed-text, -// embeddinggemma) - this is the default for semantic memory in CLIProxyAPI. -// - Use the Gemini executor with text-embedding models if you have a Gemini API key. -// - Use the OpenAI-compatible executor with any OpenAI-compatible embeddings endpoint. -// -// The handler layer (sdk/api/handlers/openai/openai_embeddings_handlers.go) provides a -// fallback that generates deterministic synthetic embeddings when real embeddings are -// not available, keeping IDE features functional. -func (e *KiroExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, statusErr{ - code: http.StatusNotImplemented, - msg: "embeddings not supported: AWS CodeWhisperer/Amazon Q Developer does not provide an embeddings API; use Ollama, Gemini, or OpenAI-compatible provider for embeddings", - } -} - -// HttpRequest injects Kiro credentials and executes an arbitrary HTTP request. -func (e *KiroExecutor) HttpRequest(_ context.Context, _ *cliproxyauth.Auth, _ *http.Request) (*http.Response, error) { - return nil, statusErr{code: http.StatusNotImplemented, msg: "http request not supported for kiro"} -} diff --git a/internal/runtime/executor/minimax_executor.go b/internal/runtime/executor/minimax_executor.go index 8ff48ad9..8c41923d 100644 --- a/internal/runtime/executor/minimax_executor.go +++ b/internal/runtime/executor/minimax_executor.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" @@ -49,9 +50,9 @@ func (e *MiniMaxExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, if upstreamModel != "" { body, _ = sjson.SetBytes(body, "model", upstreamModel) } - body = NormalizeThinkingConfig(body, upstreamModel, false) - if errValidate := ValidateThinkingConfig(body, upstreamModel); errValidate != nil { - return resp, errValidate + body, err = thinking.ApplyThinking(body, req.Model, from.String(), to.String(), e.Identifier()) + if err != nil { + return resp, err } body = applyPayloadConfig(e.cfg, req.Model, body) @@ -132,9 +133,9 @@ func (e *MiniMaxExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth. if upstreamModel != "" { body, _ = sjson.SetBytes(body, "model", upstreamModel) } - body = NormalizeThinkingConfig(body, upstreamModel, false) - if errValidate := ValidateThinkingConfig(body, upstreamModel); errValidate != nil { - return nil, errValidate + body, err = thinking.ApplyThinking(body, req.Model, from.String(), to.String(), e.Identifier()) + if err != nil { + return nil, err } body, _ = sjson.SetBytes(body, "stream_options.include_usage", true) body = applyPayloadConfig(e.cfg, req.Model, body) diff --git a/internal/runtime/executor/openai_compat_executor.go b/internal/runtime/executor/openai_compat_executor.go index 0cd33d13..85df21b1 100644 --- a/internal/runtime/executor/openai_compat_executor.go +++ b/internal/runtime/executor/openai_compat_executor.go @@ -386,8 +386,3 @@ func (e statusErr) Error() string { } func (e statusErr) StatusCode() int { return e.code } func (e statusErr) RetryAfter() *time.Duration { return e.retryAfter } - -// Embed is not supported by the openai compat executor. -func (e *OpenAICompatExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("openai compat executor does not support embeddings") -} diff --git a/internal/runtime/executor/payload_helpers.go b/internal/runtime/executor/payload_helpers.go index 4a9d3e64..7a214885 100644 --- a/internal/runtime/executor/payload_helpers.go +++ b/internal/runtime/executor/payload_helpers.go @@ -2,101 +2,20 @@ package executor import ( "encoding/json" - "fmt" - "net/http" "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" - "github.com/router-for-me/CLIProxyAPI/v6/internal/util" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" - log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) -// ApplyThinkingMetadata applies thinking config from model suffix metadata (e.g., (high), (8192)) -// for standard Gemini format payloads. It normalizes the budget when the model supports thinking. -func ApplyThinkingMetadata(payload []byte, metadata map[string]any, model string) []byte { - budgetOverride, includeOverride, ok := util.ResolveThinkingConfigFromMetadata(model, metadata) - if !ok || (budgetOverride == nil && includeOverride == nil) { - return payload - } - if !util.ModelSupportsThinking(model) { - return payload - } - if budgetOverride != nil { - norm := util.NormalizeThinkingBudget(model, *budgetOverride) - budgetOverride = &norm - } - return util.ApplyGeminiThinkingConfig(payload, budgetOverride, includeOverride) -} - -// applyThinkingMetadataCLI applies thinking config from model suffix metadata (e.g., (high), (8192)) -// for Gemini CLI format payloads (nested under "request"). It normalizes the budget when the model supports thinking. -func applyThinkingMetadataCLI(payload []byte, metadata map[string]any, model string) []byte { - budgetOverride, includeOverride, ok := util.ResolveThinkingConfigFromMetadata(model, metadata) - if !ok || (budgetOverride == nil && includeOverride == nil) { - return payload - } - if !util.ModelSupportsThinking(model) { - return payload - } - if budgetOverride != nil { - norm := util.NormalizeThinkingBudget(model, *budgetOverride) - budgetOverride = &norm - } - return util.ApplyGeminiCLIThinkingConfig(payload, budgetOverride, includeOverride) -} - -// ApplyReasoningEffortMetadata applies reasoning effort overrides from metadata to the given JSON path. -// Metadata values take precedence over any existing field when the model supports thinking, intentionally -// overwriting caller-provided values to honor suffix/default metadata priority. -func ApplyReasoningEffortMetadata(payload []byte, metadata map[string]any, model, field string, allowCompat bool) []byte { - if len(metadata) == 0 { - return payload - } - if field == "" { - return payload - } - baseModel := util.ResolveOriginalModel(model, metadata) - if baseModel == "" { - baseModel = model - } - if !util.ModelSupportsThinking(baseModel) && !allowCompat { - return payload - } - if effort, ok := util.ReasoningEffortFromMetadata(metadata); ok && effort != "" { - if util.ModelUsesThinkingLevels(baseModel) || allowCompat { - if updated, err := sjson.SetBytes(payload, field, effort); err == nil { - return updated - } - } - } - // Fallback: numeric thinking_budget suffix for level-based (OpenAI-style) models. - if util.ModelUsesThinkingLevels(baseModel) || allowCompat { - if budget, _, _, matched := util.ThinkingFromMetadata(metadata); matched && budget != nil { - if effort, ok := util.ThinkingBudgetToEffort(baseModel, *budget); ok && effort != "" { - if updated, err := sjson.SetBytes(payload, field, effort); err == nil { - return updated - } - } - } - } - return payload -} - -// applyPayloadConfig applies payload default and override rules from configuration -// to the given JSON payload for the specified model. -// Defaults only fill missing fields, while overrides always overwrite existing values. -func applyPayloadConfig(cfg *config.Config, model string, payload []byte) []byte { - return applyPayloadConfigWithRoot(cfg, model, "", "", payload, nil, "") -} - // applyPayloadConfigWithRoot behaves like applyPayloadConfig but treats all parameter // paths as relative to the provided root path (for example, "request" for Gemini CLI) // and restricts matches to the given protocol when supplied. Defaults are checked -// against the original payload when provided. The requestedModel parameter is used -// for matching payload rules when different from the resolved model. +// against the original payload when provided. requestedModel carries the client-visible +// model name before alias resolution so payload rules can target aliases precisely. func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string, payload, original []byte, requestedModel string) []byte { if cfg == nil || len(payload) == 0 { return payload @@ -106,9 +25,11 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string return payload } model = strings.TrimSpace(model) - if model == "" { + requestedModel = strings.TrimSpace(requestedModel) + if model == "" && requestedModel == "" { return payload } + candidates := payloadModelCandidates(model, requestedModel) out := payload source := original if len(source) == 0 { @@ -118,7 +39,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string // Apply default rules: first write wins per field across all matching rules. for i := range rules.Default { rule := &rules.Default[i] - if !payloadRuleMatchesModel(rule, model, protocol) { + if !payloadRuleMatchesModels(rule, protocol, candidates) { continue } for path, value := range rule.Params { @@ -143,7 +64,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string // Apply default raw rules: first write wins per field across all matching rules. for i := range rules.DefaultRaw { rule := &rules.DefaultRaw[i] - if !payloadRuleMatchesModel(rule, model, protocol) { + if !payloadRuleMatchesModels(rule, protocol, candidates) { continue } for path, value := range rule.Params { @@ -172,7 +93,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string // Apply override rules: last write wins per field across all matching rules. for i := range rules.Override { rule := &rules.Override[i] - if !payloadRuleMatchesModel(rule, model, protocol) { + if !payloadRuleMatchesModels(rule, protocol, candidates) { continue } for path, value := range rule.Params { @@ -190,7 +111,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string // Apply override raw rules: last write wins per field across all matching rules. for i := range rules.OverrideRaw { rule := &rules.OverrideRaw[i] - if !payloadRuleMatchesModel(rule, model, protocol) { + if !payloadRuleMatchesModels(rule, protocol, candidates) { continue } for path, value := range rule.Params { @@ -212,6 +133,18 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string return out } +func payloadRuleMatchesModels(rule *config.PayloadRule, protocol string, models []string) bool { + if rule == nil || len(models) == 0 { + return false + } + for _, model := range models { + if payloadRuleMatchesModel(rule, model, protocol) { + return true + } + } + return false +} + func payloadRuleMatchesModel(rule *config.PayloadRule, model, protocol string) bool { if rule == nil { return false @@ -234,6 +167,42 @@ func payloadRuleMatchesModel(rule *config.PayloadRule, model, protocol string) b return false } +func payloadModelCandidates(model, requestedModel string) []string { + model = strings.TrimSpace(model) + requestedModel = strings.TrimSpace(requestedModel) + if model == "" && requestedModel == "" { + return nil + } + candidates := make([]string, 0, 3) + seen := make(map[string]struct{}, 3) + addCandidate := func(value string) { + value = strings.TrimSpace(value) + if value == "" { + return + } + key := strings.ToLower(value) + if _, ok := seen[key]; ok { + return + } + seen[key] = struct{}{} + candidates = append(candidates, value) + } + if model != "" { + addCandidate(model) + } + if requestedModel != "" { + parsed := thinking.ParseSuffix(requestedModel) + base := strings.TrimSpace(parsed.ModelName) + if base != "" { + addCandidate(base) + } + if parsed.HasSuffix { + addCandidate(requestedModel) + } + } + return candidates +} + // buildPayloadPath combines an optional root path with a relative parameter path. // When root is empty, the parameter path is used as-is. When root is non-empty, // the parameter path is treated as relative to root. @@ -270,8 +239,6 @@ func payloadRawValue(value any) ([]byte, bool) { } } -// payloadRequestedModel extracts the originally requested model from executor options metadata. -// Returns the fallback value if not found or empty. func payloadRequestedModel(opts cliproxyexecutor.Options, fallback string) string { fallback = strings.TrimSpace(fallback) if len(opts.Metadata) == 0 { @@ -346,126 +313,38 @@ func matchModelPattern(pattern, model string) bool { return pi == len(pattern) } -// NormalizeThinkingConfig normalizes thinking-related fields in the payload -// based on model capabilities. For models without thinking support, it strips -// reasoning fields and logs a warning. For models with level-based thinking, -// it validates and normalizes the reasoning effort level. For models with -// numeric budget thinking, it strips the effort string fields. -func NormalizeThinkingConfig(payload []byte, model string, allowCompat bool) []byte { - if len(payload) == 0 || model == "" { +// applyPayloadConfig applies payload configuration rules without a root path prefix. +// This is a convenience wrapper for executors that don't need nested path handling. +func applyPayloadConfig(cfg *config.Config, model string, payload []byte) []byte { + return applyPayloadConfigWithRoot(cfg, model, "", "", payload, nil, "") +} + +// ApplyReasoningEffortMetadata applies reasoning effort metadata to the request payload. +// It extracts the value from metadata and sets it on the specified path. +func ApplyReasoningEffortMetadata(payload []byte, metadata map[string]any, model string, path string, force bool) []byte { + if metadata == nil { return payload } - - if !util.ModelSupportsThinking(model) { - if allowCompat { - return payload - } - return StripThinkingFieldsWithWarning(payload, model, false) + effort, ok := metadata["reasoning_effort"] + if !ok { + return payload } - - if util.ModelUsesThinkingLevels(model) { - return NormalizeReasoningEffortLevel(payload, model) + if !force && gjson.GetBytes(payload, path).Exists() { + return payload } - - // Model supports thinking but uses numeric budgets, not levels. - // Strip effort string fields since they are not applicable. - return StripThinkingFields(payload, true) + updated, err := sjson.SetBytes(payload, path, effort) + if err != nil { + return payload + } + return updated } -// StripThinkingFieldsWithWarning removes thinking-related fields from the payload -// and logs a warning when fields are actually stripped. This helps users understand -// why their thinking/reasoning parameters are not being applied. -func StripThinkingFieldsWithWarning(payload []byte, model string, effortOnly bool) []byte { - fieldsToRemove := []string{ - "reasoning_effort", - "reasoning.effort", - } - if !effortOnly { - fieldsToRemove = append([]string{"reasoning", "thinking"}, fieldsToRemove...) - } - out := payload - var strippedFields []string - for _, field := range fieldsToRemove { - if gjson.GetBytes(out, field).Exists() { - strippedFields = append(strippedFields, field) - out, _ = sjson.DeleteBytes(out, field) - } - } - if len(strippedFields) > 0 { - log.Warnf("model %q does not support thinking/reasoning; stripped fields: %s", model, strings.Join(strippedFields, ", ")) - } - return out -} - -// StripThinkingFields removes thinking-related fields from the payload for -// models that do not support thinking. If effortOnly is true, only removes -// effort string fields (for models using numeric budgets). -func StripThinkingFields(payload []byte, effortOnly bool) []byte { - fieldsToRemove := []string{ - "reasoning_effort", - "reasoning.effort", - } - if !effortOnly { - fieldsToRemove = append([]string{"reasoning", "thinking"}, fieldsToRemove...) - } - out := payload - for _, field := range fieldsToRemove { - if gjson.GetBytes(out, field).Exists() { - out, _ = sjson.DeleteBytes(out, field) - } - } - return out -} - -// NormalizeReasoningEffortLevel validates and normalizes the reasoning_effort -// or reasoning.effort field for level-based thinking models. -func NormalizeReasoningEffortLevel(payload []byte, model string) []byte { - out := payload - - if effort := gjson.GetBytes(out, "reasoning_effort"); effort.Exists() { - if normalized, ok := util.NormalizeReasoningEffortLevel(model, effort.String()); ok { - out, _ = sjson.SetBytes(out, "reasoning_effort", normalized) - } - } - - if effort := gjson.GetBytes(out, "reasoning.effort"); effort.Exists() { - if normalized, ok := util.NormalizeReasoningEffortLevel(model, effort.String()); ok { - out, _ = sjson.SetBytes(out, "reasoning.effort", normalized) - } - } - - return out -} - -// ValidateThinkingConfig checks for unsupported reasoning levels on level-based models. -// Returns a statusErr with 400 when an unsupported level is supplied to avoid silently -// downgrading requests. -func ValidateThinkingConfig(payload []byte, model string) error { - if len(payload) == 0 || model == "" { - return nil - } - if !util.ModelSupportsThinking(model) || !util.ModelUsesThinkingLevels(model) { - return nil - } - - levels := util.GetModelThinkingLevels(model) - checkField := func(path string) error { - if effort := gjson.GetBytes(payload, path); effort.Exists() { - if _, ok := util.NormalizeReasoningEffortLevel(model, effort.String()); !ok { - return statusErr{ - code: http.StatusBadRequest, - msg: fmt.Sprintf("unsupported reasoning effort level %q for model %s (supported: %s)", effort.String(), model, strings.Join(levels, ", ")), - } - } - } - return nil - } - - if err := checkField("reasoning_effort"); err != nil { - return err - } - if err := checkField("reasoning.effort"); err != nil { - return err - } - return nil +// EstimateRequestTokens estimates the number of tokens in a request payload. +// This is a heuristic approximation based on byte length. +// Returns an error to indicate the estimate is not precise and callers should use fallback logic. +func EstimateRequestTokens(model string, body []byte) (int64, error) { + // Simple heuristic: approximately 4 characters per token for most models + // This is intentionally conservative and returns an error to signal + // that the estimate is not authoritative. + return int64(len(body) / 4), nil } diff --git a/internal/runtime/executor/qwen_executor.go b/internal/runtime/executor/qwen_executor.go index 25eaa45e..d05579d4 100644 --- a/internal/runtime/executor/qwen_executor.go +++ b/internal/runtime/executor/qwen_executor.go @@ -367,8 +367,3 @@ func qwenCreds(a *cliproxyauth.Auth) (token, baseURL string) { } return } - -// Embed is not supported by the qwen executor. -func (e *QwenExecutor) Embed(context.Context, *cliproxyauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, fmt.Errorf("qwen executor does not support embeddings") -} diff --git a/internal/runtime/executor/qwen_executor_test.go b/internal/runtime/executor/qwen_executor_test.go new file mode 100644 index 00000000..6a777c53 --- /dev/null +++ b/internal/runtime/executor/qwen_executor_test.go @@ -0,0 +1,30 @@ +package executor + +import ( + "testing" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" +) + +func TestQwenExecutorParseSuffix(t *testing.T) { + tests := []struct { + name string + model string + wantBase string + wantLevel string + }{ + {"no suffix", "qwen-max", "qwen-max", ""}, + {"with level suffix", "qwen-max(high)", "qwen-max", "high"}, + {"with budget suffix", "qwen-max(16384)", "qwen-max", "16384"}, + {"complex model name", "qwen-plus-latest(medium)", "qwen-plus-latest", "medium"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := thinking.ParseSuffix(tt.model) + if result.ModelName != tt.wantBase { + t.Errorf("ParseSuffix(%q).ModelName = %q, want %q", tt.model, result.ModelName, tt.wantBase) + } + }) + } +} diff --git a/internal/runtime/executor/thinking_providers.go b/internal/runtime/executor/thinking_providers.go new file mode 100644 index 00000000..5a143670 --- /dev/null +++ b/internal/runtime/executor/thinking_providers.go @@ -0,0 +1,11 @@ +package executor + +import ( + _ "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking/provider/antigravity" + _ "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking/provider/claude" + _ "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking/provider/codex" + _ "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking/provider/gemini" + _ "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking/provider/geminicli" + _ "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking/provider/iflow" + _ "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking/provider/openai" +) diff --git a/internal/runtime/executor/token_helpers.go b/internal/runtime/executor/token_helpers.go index cd298a09..54188599 100644 --- a/internal/runtime/executor/token_helpers.go +++ b/internal/runtime/executor/token_helpers.go @@ -495,78 +495,3 @@ func addIfNotEmpty(segments *[]string, value string) { *segments = append(*segments, trimmed) } } - -// EstimateRequestTokens estimates total tokens for a request payload. -// It auto-detects the format (OpenAI chat, Claude, etc.) and counts accordingly. -func EstimateRequestTokens(model string, payload []byte) (int64, error) { - enc, err := getTokenizer(model) - if err != nil { - // Fallback: estimate 4 chars per token - return int64(len(payload) / 4), nil - } - - // Detect format and count accordingly - if gjson.GetBytes(payload, "messages").Exists() { - if gjson.GetBytes(payload, "system").Exists() { - // Claude format - return countClaudeChatTokens(enc, payload) - } - // OpenAI chat format - return countOpenAIChatTokens(enc, payload) - } - - // OpenAI responses format or other - use general text counting - count, err := enc.Count(string(payload)) - if err != nil { - return int64(len(payload) / 4), nil - } - return int64(count), nil -} - -// CalculateCompressionBudget determines how many tokens should be compressed -// to bring context usage under the target percentage. -func CalculateCompressionBudget(currentTokens int64, contextWindow int, targetPercent float64) int64 { - if contextWindow <= 0 || targetPercent <= 0 || targetPercent >= 1 { - return 0 - } - targetTokens := int64(float64(contextWindow) * targetPercent) - if currentTokens <= targetTokens { - return 0 - } - return currentTokens - targetTokens -} - -// ShouldTriggerCompression checks if compression should be triggered based on -// current token usage vs context window size. -func ShouldTriggerCompression(currentTokens int64, contextWindow int, thresholdPercent float64) bool { - if contextWindow <= 0 || thresholdPercent <= 0 { - return false - } - ratio := float64(currentTokens) / float64(contextWindow) - return ratio >= thresholdPercent -} - -// GetDefaultContextWindow returns a conservative default context window for a model. -func GetDefaultContextWindow(model string) int { - lowerModel := strings.ToLower(model) - switch { - case strings.Contains(lowerModel, "claude-3.5"), strings.Contains(lowerModel, "claude-3-5"): - return 200000 - case strings.Contains(lowerModel, "claude-3"): - return 200000 - case strings.Contains(lowerModel, "claude"): - return 100000 - case strings.Contains(lowerModel, "gpt-4-turbo"), strings.Contains(lowerModel, "gpt-4o"): - return 128000 - case strings.Contains(lowerModel, "gpt-4"): - return 8192 - case strings.Contains(lowerModel, "gpt-3.5"): - return 16384 - case strings.Contains(lowerModel, "gemini"): - return 1000000 - case strings.Contains(lowerModel, "o1"), strings.Contains(lowerModel, "o3"): - return 128000 - default: - return 100000 // Conservative default - } -} diff --git a/internal/runtime/executor/usage_helpers.go b/internal/runtime/executor/usage_helpers.go index a3ce270c..3aa1e7ff 100644 --- a/internal/runtime/executor/usage_helpers.go +++ b/internal/runtime/executor/usage_helpers.go @@ -236,6 +236,44 @@ func parseOpenAIStreamUsage(line []byte) (usage.Detail, bool) { return detail, true } +func parseOpenAIResponsesUsageDetail(usageNode gjson.Result) usage.Detail { + detail := usage.Detail{ + InputTokens: usageNode.Get("input_tokens").Int(), + OutputTokens: usageNode.Get("output_tokens").Int(), + TotalTokens: usageNode.Get("total_tokens").Int(), + } + if detail.TotalTokens == 0 { + detail.TotalTokens = detail.InputTokens + detail.OutputTokens + } + if cached := usageNode.Get("input_tokens_details.cached_tokens"); cached.Exists() { + detail.CachedTokens = cached.Int() + } + if reasoning := usageNode.Get("output_tokens_details.reasoning_tokens"); reasoning.Exists() { + detail.ReasoningTokens = reasoning.Int() + } + return detail +} + +func parseOpenAIResponsesUsage(data []byte) usage.Detail { + usageNode := gjson.ParseBytes(data).Get("usage") + if !usageNode.Exists() { + return usage.Detail{} + } + return parseOpenAIResponsesUsageDetail(usageNode) +} + +func parseOpenAIResponsesStreamUsage(line []byte) (usage.Detail, bool) { + payload := jsonPayload(line) + if len(payload) == 0 || !gjson.ValidBytes(payload) { + return usage.Detail{}, false + } + usageNode := gjson.GetBytes(payload, "usage") + if !usageNode.Exists() { + return usage.Detail{}, false + } + return parseOpenAIResponsesUsageDetail(usageNode), true +} + func parseClaudeUsage(data []byte) usage.Detail { usageNode := gjson.ParseBytes(data).Get("usage") if !usageNode.Exists() { diff --git a/internal/runtime/executor/zhipu_executor.go b/internal/runtime/executor/zhipu_executor.go index ea77de4c..ab1fc448 100644 --- a/internal/runtime/executor/zhipu_executor.go +++ b/internal/runtime/executor/zhipu_executor.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" @@ -49,9 +50,9 @@ func (e *ZhipuExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re if upstreamModel != "" { body, _ = sjson.SetBytes(body, "model", upstreamModel) } - body = NormalizeThinkingConfig(body, upstreamModel, false) - if errValidate := ValidateThinkingConfig(body, upstreamModel); errValidate != nil { - return resp, errValidate + body, err = thinking.ApplyThinking(body, req.Model, from.String(), to.String(), e.Identifier()) + if err != nil { + return resp, err } body = applyPayloadConfig(e.cfg, req.Model, body) @@ -132,9 +133,9 @@ func (e *ZhipuExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au if upstreamModel != "" { body, _ = sjson.SetBytes(body, "model", upstreamModel) } - body = NormalizeThinkingConfig(body, upstreamModel, false) - if errValidate := ValidateThinkingConfig(body, upstreamModel); errValidate != nil { - return nil, errValidate + body, err = thinking.ApplyThinking(body, req.Model, from.String(), to.String(), e.Identifier()) + if err != nil { + return nil, err } body, _ = sjson.SetBytes(body, "stream_options.include_usage", true) body = applyPayloadConfig(e.cfg, req.Model, body) diff --git a/internal/thinking/provider/claude/apply.go b/internal/thinking/provider/claude/apply.go index f83a7c6f..3c74d514 100644 --- a/internal/thinking/provider/claude/apply.go +++ b/internal/thinking/provider/claude/apply.go @@ -3,6 +3,7 @@ // Claude models use the thinking.budget_tokens format with values in the range // 1024-128000. Some Claude models support ZeroAllowed (sonnet-4-5, opus-4-5), // while older models do not. +// See: _bmad-output/planning-artifacts/architecture.md#Epic-6 package claude import ( diff --git a/internal/thinking/provider/codex/apply.go b/internal/thinking/provider/codex/apply.go index 42406e74..3bed318b 100644 --- a/internal/thinking/provider/codex/apply.go +++ b/internal/thinking/provider/codex/apply.go @@ -3,6 +3,7 @@ // Codex models use the reasoning.effort format with discrete levels // (low/medium/high). This is similar to OpenAI but uses nested field // "reasoning.effort" instead of "reasoning_effort". +// See: _bmad-output/planning-artifacts/architecture.md#Epic-8 package codex import ( diff --git a/internal/thinking/provider/geminicli/apply.go b/internal/thinking/provider/geminicli/apply.go new file mode 100644 index 00000000..75d9242a --- /dev/null +++ b/internal/thinking/provider/geminicli/apply.go @@ -0,0 +1,126 @@ +// Package geminicli implements thinking configuration for Gemini CLI API format. +// +// Gemini CLI uses request.generationConfig.thinkingConfig.* path instead of +// generationConfig.thinkingConfig.* used by standard Gemini API. +package geminicli + +import ( + "github.com/router-for-me/CLIProxyAPI/v6/internal/registry" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +// Applier applies thinking configuration for Gemini CLI API format. +type Applier struct{} + +var _ thinking.ProviderApplier = (*Applier)(nil) + +// NewApplier creates a new Gemini CLI thinking applier. +func NewApplier() *Applier { + return &Applier{} +} + +func init() { + thinking.RegisterProvider("gemini-cli", NewApplier()) +} + +// Apply applies thinking configuration to Gemini CLI request body. +func (a *Applier) Apply(body []byte, config thinking.ThinkingConfig, modelInfo *registry.ModelInfo) ([]byte, error) { + if thinking.IsUserDefinedModel(modelInfo) { + return a.applyCompatible(body, config) + } + if modelInfo.Thinking == nil { + return body, nil + } + + if config.Mode != thinking.ModeBudget && config.Mode != thinking.ModeLevel && config.Mode != thinking.ModeNone && config.Mode != thinking.ModeAuto { + return body, nil + } + + if len(body) == 0 || !gjson.ValidBytes(body) { + body = []byte(`{}`) + } + + // ModeAuto: Always use Budget format with thinkingBudget=-1 + if config.Mode == thinking.ModeAuto { + return a.applyBudgetFormat(body, config) + } + if config.Mode == thinking.ModeBudget { + return a.applyBudgetFormat(body, config) + } + + // For non-auto modes, choose format based on model capabilities + support := modelInfo.Thinking + if len(support.Levels) > 0 { + return a.applyLevelFormat(body, config) + } + return a.applyBudgetFormat(body, config) +} + +func (a *Applier) applyCompatible(body []byte, config thinking.ThinkingConfig) ([]byte, error) { + if config.Mode != thinking.ModeBudget && config.Mode != thinking.ModeLevel && config.Mode != thinking.ModeNone && config.Mode != thinking.ModeAuto { + return body, nil + } + + if len(body) == 0 || !gjson.ValidBytes(body) { + body = []byte(`{}`) + } + + if config.Mode == thinking.ModeAuto { + return a.applyBudgetFormat(body, config) + } + + if config.Mode == thinking.ModeLevel || (config.Mode == thinking.ModeNone && config.Level != "") { + return a.applyLevelFormat(body, config) + } + + return a.applyBudgetFormat(body, config) +} + +func (a *Applier) applyLevelFormat(body []byte, config thinking.ThinkingConfig) ([]byte, error) { + // Remove conflicting field to avoid both thinkingLevel and thinkingBudget in output + result, _ := sjson.DeleteBytes(body, "request.generationConfig.thinkingConfig.thinkingBudget") + // Normalize includeThoughts field name to avoid oneof conflicts in upstream JSON parsing. + result, _ = sjson.DeleteBytes(result, "request.generationConfig.thinkingConfig.include_thoughts") + + if config.Mode == thinking.ModeNone { + result, _ = sjson.SetBytes(result, "request.generationConfig.thinkingConfig.includeThoughts", false) + if config.Level != "" { + result, _ = sjson.SetBytes(result, "request.generationConfig.thinkingConfig.thinkingLevel", string(config.Level)) + } + return result, nil + } + + // Only handle ModeLevel - budget conversion should be done by upper layer + if config.Mode != thinking.ModeLevel { + return body, nil + } + + level := string(config.Level) + result, _ = sjson.SetBytes(result, "request.generationConfig.thinkingConfig.thinkingLevel", level) + result, _ = sjson.SetBytes(result, "request.generationConfig.thinkingConfig.includeThoughts", true) + return result, nil +} + +func (a *Applier) applyBudgetFormat(body []byte, config thinking.ThinkingConfig) ([]byte, error) { + // Remove conflicting field to avoid both thinkingLevel and thinkingBudget in output + result, _ := sjson.DeleteBytes(body, "request.generationConfig.thinkingConfig.thinkingLevel") + // Normalize includeThoughts field name to avoid oneof conflicts in upstream JSON parsing. + result, _ = sjson.DeleteBytes(result, "request.generationConfig.thinkingConfig.include_thoughts") + + budget := config.Budget + includeThoughts := false + switch config.Mode { + case thinking.ModeNone: + includeThoughts = false + case thinking.ModeAuto: + includeThoughts = true + default: + includeThoughts = budget > 0 + } + + result, _ = sjson.SetBytes(result, "request.generationConfig.thinkingConfig.thinkingBudget", budget) + result, _ = sjson.SetBytes(result, "request.generationConfig.thinkingConfig.includeThoughts", includeThoughts) + return result, nil +} diff --git a/internal/thinking/provider/iflow/apply.go b/internal/thinking/provider/iflow/apply.go new file mode 100644 index 00000000..da986d22 --- /dev/null +++ b/internal/thinking/provider/iflow/apply.go @@ -0,0 +1,156 @@ +// Package iflow implements thinking configuration for iFlow models (GLM, MiniMax). +// +// iFlow models use boolean toggle semantics: +// - GLM models: chat_template_kwargs.enable_thinking (boolean) +// - MiniMax models: reasoning_split (boolean) +// +// Level values are converted to boolean: none=false, all others=true +// See: _bmad-output/planning-artifacts/architecture.md#Epic-9 +package iflow + +import ( + "strings" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/registry" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +// Applier implements thinking.ProviderApplier for iFlow models. +// +// iFlow-specific behavior: +// - GLM models: enable_thinking boolean + clear_thinking=false +// - MiniMax models: reasoning_split boolean +// - Level to boolean: none=false, others=true +// - No quantized support (only on/off) +type Applier struct{} + +var _ thinking.ProviderApplier = (*Applier)(nil) + +// NewApplier creates a new iFlow thinking applier. +func NewApplier() *Applier { + return &Applier{} +} + +func init() { + thinking.RegisterProvider("iflow", NewApplier()) +} + +// Apply applies thinking configuration to iFlow request body. +// +// Expected output format (GLM): +// +// { +// "chat_template_kwargs": { +// "enable_thinking": true, +// "clear_thinking": false +// } +// } +// +// Expected output format (MiniMax): +// +// { +// "reasoning_split": true +// } +func (a *Applier) Apply(body []byte, config thinking.ThinkingConfig, modelInfo *registry.ModelInfo) ([]byte, error) { + if thinking.IsUserDefinedModel(modelInfo) { + return body, nil + } + if modelInfo.Thinking == nil { + return body, nil + } + + if isGLMModel(modelInfo.ID) { + return applyGLM(body, config), nil + } + + if isMiniMaxModel(modelInfo.ID) { + return applyMiniMax(body, config), nil + } + + return body, nil +} + +// configToBoolean converts ThinkingConfig to boolean for iFlow models. +// +// Conversion rules: +// - ModeNone: false +// - ModeAuto: true +// - ModeBudget + Budget=0: false +// - ModeBudget + Budget>0: true +// - ModeLevel + Level="none": false +// - ModeLevel + any other level: true +// - Default (unknown mode): true +func configToBoolean(config thinking.ThinkingConfig) bool { + switch config.Mode { + case thinking.ModeNone: + return false + case thinking.ModeAuto: + return true + case thinking.ModeBudget: + return config.Budget > 0 + case thinking.ModeLevel: + return config.Level != thinking.LevelNone + default: + return true + } +} + +// applyGLM applies thinking configuration for GLM models. +// +// Output format when enabled: +// +// {"chat_template_kwargs": {"enable_thinking": true, "clear_thinking": false}} +// +// Output format when disabled: +// +// {"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 { + enableThinking := configToBoolean(config) + + if len(body) == 0 || !gjson.ValidBytes(body) { + body = []byte(`{}`) + } + + result, _ := sjson.SetBytes(body, "chat_template_kwargs.enable_thinking", enableThinking) + + // clear_thinking only needed when thinking is enabled + if enableThinking { + result, _ = sjson.SetBytes(result, "chat_template_kwargs.clear_thinking", false) + } + + return result +} + +// applyMiniMax applies thinking configuration for MiniMax models. +// +// Output format: +// +// {"reasoning_split": true/false} +func applyMiniMax(body []byte, config thinking.ThinkingConfig) []byte { + reasoningSplit := configToBoolean(config) + + if len(body) == 0 || !gjson.ValidBytes(body) { + body = []byte(`{}`) + } + + result, _ := sjson.SetBytes(body, "reasoning_split", reasoningSplit) + + return result +} + +// 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") +} + +// isMiniMaxModel determines if the model is a MiniMax series model. +// MiniMax models use reasoning_split format. +func isMiniMaxModel(modelID string) bool { + return strings.HasPrefix(strings.ToLower(modelID), "minimax") +} diff --git a/internal/thinking/provider/openai/apply.go b/internal/thinking/provider/openai/apply.go index 37b8b89f..eaad30ee 100644 --- a/internal/thinking/provider/openai/apply.go +++ b/internal/thinking/provider/openai/apply.go @@ -2,6 +2,7 @@ // // OpenAI models use the reasoning_effort format with discrete levels // (low/medium/high). Some models support xhigh and none levels. +// See: _bmad-output/planning-artifacts/architecture.md#Epic-8 package openai import ( diff --git a/internal/translator/antigravity/claude/init.go b/internal/translator/antigravity/claude/init.go index 0197ae7e..21fe0b26 100644 --- a/internal/translator/antigravity/claude/init.go +++ b/internal/translator/antigravity/claude/init.go @@ -3,7 +3,7 @@ package claude import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/antigravity/gemini/init.go b/internal/translator/antigravity/gemini/init.go index 5d392279..39558248 100644 --- a/internal/translator/antigravity/gemini/init.go +++ b/internal/translator/antigravity/gemini/init.go @@ -3,7 +3,7 @@ package gemini import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/antigravity/openai/chat-completions/antigravity_openai_response.go b/internal/translator/antigravity/openai/chat-completions/antigravity_openai_response.go index 9dae687a..1b7866d0 100644 --- a/internal/translator/antigravity/openai/chat-completions/antigravity_openai_response.go +++ b/internal/translator/antigravity/openai/chat-completions/antigravity_openai_response.go @@ -8,7 +8,6 @@ package chat_completions import ( "bytes" "context" - "encoding/json" "fmt" "strings" "sync/atomic" @@ -88,15 +87,15 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq // Extract and set usage metadata (token counts). if usageResult := gjson.GetBytes(rawJSON, "response.usageMetadata"); usageResult.Exists() { + cachedTokenCount := usageResult.Get("cachedContentTokenCount").Int() if candidatesTokenCountResult := usageResult.Get("candidatesTokenCount"); candidatesTokenCountResult.Exists() { template, _ = sjson.Set(template, "usage.completion_tokens", candidatesTokenCountResult.Int()) } if totalTokenCountResult := usageResult.Get("totalTokenCount"); totalTokenCountResult.Exists() { template, _ = sjson.Set(template, "usage.total_tokens", totalTokenCountResult.Int()) } - promptTokenCount := usageResult.Get("promptTokenCount").Int() + promptTokenCount := usageResult.Get("promptTokenCount").Int() - cachedTokenCount thoughtsTokenCount := usageResult.Get("thoughtsTokenCount").Int() - cachedTokenCount := usageResult.Get("cachedContentTokenCount").Int() template, _ = sjson.Set(template, "usage.prompt_tokens", promptTokenCount+thoughtsTokenCount) if thoughtsTokenCount > 0 { template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", thoughtsTokenCount) @@ -187,18 +186,11 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`) } imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array()) - imagePayload, err := json.Marshal(map[string]any{ - "index": imageIndex, - "type": "image_url", - "image_url": map[string]string{ - "url": imageURL, - }, - }) - if err != nil { - continue - } + imagePayload := `{"type":"image_url","image_url":{"url":""}}` + imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex) + imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL) template, _ = sjson.Set(template, "choices.0.delta.role", "assistant") - template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayload)) + template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload) } } } diff --git a/internal/translator/antigravity/openai/chat-completions/init.go b/internal/translator/antigravity/openai/chat-completions/init.go index a15da38d..5c5c71e4 100644 --- a/internal/translator/antigravity/openai/chat-completions/init.go +++ b/internal/translator/antigravity/openai/chat-completions/init.go @@ -3,7 +3,7 @@ package chat_completions import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/antigravity/openai/responses/init.go b/internal/translator/antigravity/openai/responses/init.go index 12630b6e..8d137032 100644 --- a/internal/translator/antigravity/openai/responses/init.go +++ b/internal/translator/antigravity/openai/responses/init.go @@ -3,7 +3,7 @@ package responses import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/claude/gemini-cli/init.go b/internal/translator/claude/gemini-cli/init.go index 96c4a0c0..ca364a6e 100644 --- a/internal/translator/claude/gemini-cli/init.go +++ b/internal/translator/claude/gemini-cli/init.go @@ -3,7 +3,7 @@ package geminiCLI import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/claude/gemini/init.go b/internal/translator/claude/gemini/init.go index 15ce79f6..8924f62c 100644 --- a/internal/translator/claude/gemini/init.go +++ b/internal/translator/claude/gemini/init.go @@ -3,7 +3,7 @@ package gemini import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/claude/openai/chat-completions/claude_openai_response.go b/internal/translator/claude/openai/chat-completions/claude_openai_response.go index 346db69a..0ddfeaec 100644 --- a/internal/translator/claude/openai/chat-completions/claude_openai_response.go +++ b/internal/translator/claude/openai/chat-completions/claude_openai_response.go @@ -50,10 +50,6 @@ type ToolCallAccumulator struct { // Returns: // - []string: A slice of strings, each containing an OpenAI-compatible JSON response func ConvertClaudeResponseToOpenAI(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string { - var localParam any - if param == nil { - param = &localParam - } if *param == nil { *param = &ConvertAnthropicResponseToOpenAIParams{ CreatedAt: 0, diff --git a/internal/translator/claude/openai/chat-completions/init.go b/internal/translator/claude/openai/chat-completions/init.go index 21244f0a..a18840ba 100644 --- a/internal/translator/claude/openai/chat-completions/init.go +++ b/internal/translator/claude/openai/chat-completions/init.go @@ -3,7 +3,7 @@ package chat_completions import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/claude/openai/responses/claude_openai-responses_request.go b/internal/translator/claude/openai/responses/claude_openai-responses_request.go index d4b7e05f..5cbe23bf 100644 --- a/internal/translator/claude/openai/responses/claude_openai-responses_request.go +++ b/internal/translator/claude/openai/responses/claude_openai-responses_request.go @@ -10,7 +10,7 @@ import ( "strings" "github.com/google/uuid" - "github.com/router-for-me/CLIProxyAPI/v6/internal/util" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -53,10 +53,11 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte root := gjson.ParseBytes(rawJSON) - if v := root.Get("reasoning.effort"); v.Exists() && util.ModelSupportsThinking(modelName) && !util.ModelUsesThinkingLevels(modelName) { + // Convert OpenAI Responses reasoning.effort to Claude thinking config. + if v := root.Get("reasoning.effort"); v.Exists() { effort := strings.ToLower(strings.TrimSpace(v.String())) if effort != "" { - budget, ok := util.ThinkingEffortToBudget(modelName, effort) + budget, ok := thinking.ConvertLevelToBudget(effort) if ok { switch budget { case 0: diff --git a/internal/translator/claude/openai/responses/claude_openai-responses_response.go b/internal/translator/claude/openai/responses/claude_openai-responses_response.go index 354be56e..e77b09e1 100644 --- a/internal/translator/claude/openai/responses/claude_openai-responses_response.go +++ b/internal/translator/claude/openai/responses/claude_openai-responses_response.go @@ -40,6 +40,16 @@ type claudeToResponsesState struct { var dataTag = []byte("data:") +func pickRequestJSON(originalRequestRawJSON, requestRawJSON []byte) []byte { + if len(originalRequestRawJSON) > 0 && gjson.ValidBytes(originalRequestRawJSON) { + return originalRequestRawJSON + } + if len(requestRawJSON) > 0 && gjson.ValidBytes(requestRawJSON) { + return requestRawJSON + } + return nil +} + func emitEvent(event string, payload string) string { return fmt.Sprintf("event: %s\ndata: %s", event, payload) } @@ -241,6 +251,7 @@ func ConvertClaudeResponseToOpenAIResponses(ctx context.Context, modelName strin itemDone, _ = sjson.Set(itemDone, "item.id", fmt.Sprintf("fc_%s", st.CurrentFCID)) itemDone, _ = sjson.Set(itemDone, "item.arguments", args) itemDone, _ = sjson.Set(itemDone, "item.call_id", st.CurrentFCID) + itemDone, _ = sjson.Set(itemDone, "item.name", st.FuncNames[idx]) out = append(out, emitEvent("response.output_item.done", itemDone)) st.InFuncBlock = false } else if st.ReasoningActive { @@ -279,8 +290,9 @@ func ConvertClaudeResponseToOpenAIResponses(ctx context.Context, modelName strin completed, _ = sjson.Set(completed, "response.created_at", st.CreatedAt) // Inject original request fields into response as per docs/response.completed.json - if requestRawJSON != nil { - req := gjson.ParseBytes(requestRawJSON) + reqBytes := pickRequestJSON(originalRequestRawJSON, requestRawJSON) + if len(reqBytes) > 0 { + req := gjson.ParseBytes(reqBytes) if v := req.Get("instructions"); v.Exists() { completed, _ = sjson.Set(completed, "response.instructions", v.String()) } @@ -549,8 +561,9 @@ func ConvertClaudeResponseToOpenAIResponsesNonStream(_ context.Context, _ string out, _ = sjson.Set(out, "created_at", createdAt) // Inject request echo fields as top-level (similar to streaming variant) - if requestRawJSON != nil { - req := gjson.ParseBytes(requestRawJSON) + reqBytes := pickRequestJSON(originalRequestRawJSON, requestRawJSON) + if len(reqBytes) > 0 { + req := gjson.ParseBytes(reqBytes) if v := req.Get("instructions"); v.Exists() { out, _ = sjson.Set(out, "instructions", v.String()) } diff --git a/internal/translator/claude/openai/responses/init.go b/internal/translator/claude/openai/responses/init.go index 1e4d59c8..595fecc6 100644 --- a/internal/translator/claude/openai/responses/init.go +++ b/internal/translator/claude/openai/responses/init.go @@ -3,7 +3,7 @@ package responses import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/codex/claude/codex_claude_request.go b/internal/translator/codex/claude/codex_claude_request.go index 59cd5ecf..f0f5d867 100644 --- a/internal/translator/codex/claude/codex_claude_request.go +++ b/internal/translator/codex/claude/codex_claude_request.go @@ -12,7 +12,7 @@ import ( "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" - "github.com/router-for-me/CLIProxyAPI/v6/internal/util" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -51,7 +51,7 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool) systemsResult := rootResult.Get("system") if systemsResult.IsArray() { systemResults := systemsResult.Array() - message := `{"type":"message","role":"user","content":[]}` + message := `{"type":"message","role":"developer","content":[]}` for i := 0; i < len(systemResults); i++ { systemResult := systemResults[i] systemTypeResult := systemResult.Get("type") @@ -217,21 +217,19 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool) // Add additional configuration parameters for the Codex API. template, _ = sjson.Set(template, "parallel_tool_calls", true) - // Convert thinking.budget_tokens to reasoning.effort for level-based models - reasoningEffort := "medium" // default - if thinking := rootResult.Get("thinking"); thinking.Exists() && thinking.IsObject() { - switch thinking.Get("type").String() { + // Convert thinking.budget_tokens to reasoning.effort. + reasoningEffort := "medium" + if thinkingConfig := rootResult.Get("thinking"); thinkingConfig.Exists() && thinkingConfig.IsObject() { + switch thinkingConfig.Get("type").String() { case "enabled": - if util.ModelUsesThinkingLevels(modelName) { - if budgetTokens := thinking.Get("budget_tokens"); budgetTokens.Exists() { - budget := int(budgetTokens.Int()) - if effort, ok := util.ThinkingBudgetToEffort(modelName, budget); ok && effort != "" { - reasoningEffort = effort - } + if budgetTokens := thinkingConfig.Get("budget_tokens"); budgetTokens.Exists() { + budget := int(budgetTokens.Int()) + if effort, ok := thinking.ConvertBudgetToLevel(budget); ok && effort != "" { + reasoningEffort = effort } } case "disabled": - if effort, ok := util.ThinkingBudgetToEffort(modelName, 0); ok && effort != "" { + if effort, ok := thinking.ConvertBudgetToLevel(0); ok && effort != "" { reasoningEffort = effort } } @@ -243,21 +241,23 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool) template, _ = sjson.Set(template, "include", []string{"reasoning.encrypted_content"}) // Add a first message to ignore system instructions and ensure proper execution. - inputResult := gjson.Get(template, "input") - if inputResult.Exists() && inputResult.IsArray() { - inputResults := inputResult.Array() - newInput := "[]" - for i := 0; i < len(inputResults); i++ { - if i == 0 { - firstText := inputResults[i].Get("content.0.text") - firstInstructions := "EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!" - if firstText.Exists() && firstText.String() != firstInstructions { - newInput, _ = sjson.SetRaw(newInput, "-1", `{"type":"message","role":"user","content":[{"type":"input_text","text":"EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!"}]}`) + if misc.GetCodexInstructionsEnabled() { + inputResult := gjson.Get(template, "input") + if inputResult.Exists() && inputResult.IsArray() { + inputResults := inputResult.Array() + newInput := "[]" + for i := 0; i < len(inputResults); i++ { + if i == 0 { + firstText := inputResults[i].Get("content.0.text") + firstInstructions := "EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!" + if firstText.Exists() && firstText.String() != firstInstructions { + newInput, _ = sjson.SetRaw(newInput, "-1", `{"type":"message","role":"user","content":[{"type":"input_text","text":"EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!"}]}`) + } } + newInput, _ = sjson.SetRaw(newInput, "-1", inputResults[i].Raw) } - newInput, _ = sjson.SetRaw(newInput, "-1", inputResults[i].Raw) + template, _ = sjson.SetRaw(template, "input", newInput) } - template, _ = sjson.SetRaw(template, "input", newInput) } return []byte(template) diff --git a/internal/translator/codex/claude/codex_claude_response.go b/internal/translator/codex/claude/codex_claude_response.go index a33bc6da..5223cd94 100644 --- a/internal/translator/codex/claude/codex_claude_response.go +++ b/internal/translator/codex/claude/codex_claude_response.go @@ -20,6 +20,12 @@ var ( dataTag = []byte("data:") ) +// ConvertCodexResponseToClaudeParams holds parameters for response conversion. +type ConvertCodexResponseToClaudeParams struct { + HasToolCall bool + BlockIndex int +} + // ConvertCodexResponseToClaude performs sophisticated streaming response format conversion. // This function implements a complex state machine that translates Codex API responses // into Claude Code-compatible Server-Sent Events (SSE) format. It manages different response types @@ -38,8 +44,10 @@ var ( // - []string: A slice of strings, each containing a Claude Code-compatible JSON response func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string { if *param == nil { - hasToolCall := false - *param = &hasToolCall + *param = &ConvertCodexResponseToClaudeParams{ + HasToolCall: false, + BlockIndex: 0, + } } // log.Debugf("rawJSON: %s", string(rawJSON)) @@ -62,46 +70,49 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa output += fmt.Sprintf("data: %s\n\n", template) } else if typeStr == "response.reasoning_summary_part.added" { template = `{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) output = "event: content_block_start\n" output += fmt.Sprintf("data: %s\n\n", template) } else if typeStr == "response.reasoning_summary_text.delta" { template = `{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":""}}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) template, _ = sjson.Set(template, "delta.thinking", rootResult.Get("delta").String()) output = "event: content_block_delta\n" output += fmt.Sprintf("data: %s\n\n", template) } else if typeStr == "response.reasoning_summary_part.done" { template = `{"type":"content_block_stop","index":0}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) + (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex++ output = "event: content_block_stop\n" output += fmt.Sprintf("data: %s\n\n", template) + } else if typeStr == "response.content_part.added" { template = `{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) output = "event: content_block_start\n" output += fmt.Sprintf("data: %s\n\n", template) } else if typeStr == "response.output_text.delta" { template = `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) template, _ = sjson.Set(template, "delta.text", rootResult.Get("delta").String()) output = "event: content_block_delta\n" output += fmt.Sprintf("data: %s\n\n", template) } else if typeStr == "response.content_part.done" { template = `{"type":"content_block_stop","index":0}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) + (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex++ output = "event: content_block_stop\n" output += fmt.Sprintf("data: %s\n\n", template) } else if typeStr == "response.completed" { template = `{"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"input_tokens":0,"output_tokens":0}}` - p := (*param).(*bool) - if *p { + p := (*param).(*ConvertCodexResponseToClaudeParams).HasToolCall + if p { template, _ = sjson.Set(template, "delta.stop_reason", "tool_use") } else { template, _ = sjson.Set(template, "delta.stop_reason", "end_turn") @@ -122,10 +133,9 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa itemResult := rootResult.Get("item") itemType := itemResult.Get("type").String() if itemType == "function_call" { - p := true - *param = &p + (*param).(*ConvertCodexResponseToClaudeParams).HasToolCall = true template = `{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) template, _ = sjson.Set(template, "content_block.id", itemResult.Get("call_id").String()) { // Restore original tool name if shortened @@ -141,7 +151,7 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa output += fmt.Sprintf("data: %s\n\n", template) template = `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) output += "event: content_block_delta\n" output += fmt.Sprintf("data: %s\n\n", template) @@ -151,14 +161,15 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa itemType := itemResult.Get("type").String() if itemType == "function_call" { template = `{"type":"content_block_stop","index":0}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) + (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex++ output = "event: content_block_stop\n" output += fmt.Sprintf("data: %s\n\n", template) } } else if typeStr == "response.function_call_arguments.delta" { template = `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` - template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int()) + template, _ = sjson.Set(template, "index", (*param).(*ConvertCodexResponseToClaudeParams).BlockIndex) template, _ = sjson.Set(template, "delta.partial_json", rootResult.Get("delta").String()) output += "event: content_block_delta\n" diff --git a/internal/translator/codex/claude/init.go b/internal/translator/codex/claude/init.go index de14d79f..7126edc3 100644 --- a/internal/translator/codex/claude/init.go +++ b/internal/translator/codex/claude/init.go @@ -3,7 +3,7 @@ package claude import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/codex/gemini-cli/init.go b/internal/translator/codex/gemini-cli/init.go index 3fb14530..8bcd3de5 100644 --- a/internal/translator/codex/gemini-cli/init.go +++ b/internal/translator/codex/gemini-cli/init.go @@ -3,7 +3,7 @@ package geminiCLI import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/codex/gemini/codex_gemini_request.go b/internal/translator/codex/gemini/codex_gemini_request.go index 944b95f6..342c5b1a 100644 --- a/internal/translator/codex/gemini/codex_gemini_request.go +++ b/internal/translator/codex/gemini/codex_gemini_request.go @@ -14,6 +14,7 @@ import ( "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" "github.com/tidwall/gjson" "github.com/tidwall/sjson" @@ -93,7 +94,7 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) // System instruction -> as a user message with input_text parts sysParts := root.Get("system_instruction.parts") if sysParts.IsArray() { - msg := `{"type":"message","role":"user","content":[]}` + msg := `{"type":"message","role":"developer","content":[]}` arr := sysParts.Array() for i := 0; i < len(arr); i++ { p := arr[i] @@ -247,21 +248,28 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) // Fixed flags aligning with Codex expectations out, _ = sjson.Set(out, "parallel_tool_calls", true) - // Convert thinkingBudget to reasoning.effort for level-based models - reasoningEffort := "medium" // default + // Convert Gemini thinkingConfig to Codex reasoning.effort. + effortSet := false if genConfig := root.Get("generationConfig"); genConfig.Exists() { if thinkingConfig := genConfig.Get("thinkingConfig"); thinkingConfig.Exists() && thinkingConfig.IsObject() { - if util.ModelUsesThinkingLevels(modelName) { - if thinkingBudget := thinkingConfig.Get("thinkingBudget"); thinkingBudget.Exists() { - budget := int(thinkingBudget.Int()) - if effort, ok := util.ThinkingBudgetToEffort(modelName, budget); ok && effort != "" { - reasoningEffort = effort - } + if thinkingLevel := thinkingConfig.Get("thinkingLevel"); thinkingLevel.Exists() { + effort := strings.ToLower(strings.TrimSpace(thinkingLevel.String())) + if effort != "" { + out, _ = sjson.Set(out, "reasoning.effort", effort) + effortSet = true + } + } else if thinkingBudget := thinkingConfig.Get("thinkingBudget"); thinkingBudget.Exists() { + if effort, ok := thinking.ConvertBudgetToLevel(int(thinkingBudget.Int())); ok { + out, _ = sjson.Set(out, "reasoning.effort", effort) + effortSet = true } } } } - out, _ = sjson.Set(out, "reasoning.effort", reasoningEffort) + if !effortSet { + // No thinking config, set default effort + out, _ = sjson.Set(out, "reasoning.effort", "medium") + } out, _ = sjson.Set(out, "reasoning.summary", "auto") out, _ = sjson.Set(out, "stream", true) out, _ = sjson.Set(out, "store", false) diff --git a/internal/translator/codex/gemini/init.go b/internal/translator/codex/gemini/init.go index 6036f512..41d30559 100644 --- a/internal/translator/codex/gemini/init.go +++ b/internal/translator/codex/gemini/init.go @@ -3,7 +3,7 @@ package gemini import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/codex/openai/chat-completions/codex_openai_request.go b/internal/translator/codex/openai/chat-completions/codex_openai_request.go index eb237770..40f56f88 100644 --- a/internal/translator/codex/openai/chat-completions/codex_openai_request.go +++ b/internal/translator/codex/openai/chat-completions/codex_openai_request.go @@ -33,7 +33,7 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b rawJSON := bytes.Clone(inputRawJSON) userAgent := misc.ExtractCodexUserAgent(rawJSON) // Start with empty JSON object - out := `{}` + out := `{"instructions":""}` // Stream must be set to true out, _ = sjson.Set(out, "stream", stream) @@ -67,11 +67,6 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b out, _ = sjson.Set(out, "reasoning.summary", "auto") out, _ = sjson.Set(out, "include", []string{"reasoning.encrypted_content"}) - // The chatgpt.com Codex backend requires a specific instruction prefix (Codex CLI prompt). - // Always provide the official Codex instructions for the target model. - _, official := misc.CodexInstructionsForModel(modelName, "", userAgent) - out, _ = sjson.Set(out, "instructions", official) - // Model out, _ = sjson.Set(out, "model", modelName) @@ -100,9 +95,27 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b } } - // The chatgpt.com Codex backend can reject top-level "instructions". - // Preserve system prompts by leaving system messages inside "input" instead. + // Extract system instructions from first system message (string or text object) messages := gjson.GetBytes(rawJSON, "messages") + _, instructions := misc.CodexInstructionsForModel(modelName, "", userAgent) + if misc.GetCodexInstructionsEnabled() { + out, _ = sjson.Set(out, "instructions", instructions) + } + // if messages.IsArray() { + // arr := messages.Array() + // for i := 0; i < len(arr); i++ { + // m := arr[i] + // if m.Get("role").String() == "system" { + // c := m.Get("content") + // if c.Type == gjson.String { + // out, _ = sjson.Set(out, "instructions", c.String()) + // } else if c.IsObject() && c.Get("type").String() == "text" { + // out, _ = sjson.Set(out, "instructions", c.Get("text").String()) + // } + // break + // } + // } + // } // Build input from messages, handling all message types including tool calls out, _ = sjson.SetRaw(out, "input", `[]`) @@ -111,11 +124,6 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b for i := 0; i < len(arr); i++ { m := arr[i] role := m.Get("role").String() - if role == "system" { - // Codex backend rejects role=system in input ("System messages are not allowed"). - // Preserve the content by downgrading to a user message. - role = "user" - } switch role { case "tool": @@ -134,7 +142,11 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b // Handle regular messages msg := `{}` msg, _ = sjson.Set(msg, "type", "message") - msg, _ = sjson.Set(msg, "role", role) + if role == "system" { + msg, _ = sjson.Set(msg, "role", "developer") + } else { + msg, _ = sjson.Set(msg, "role", role) + } msg, _ = sjson.SetRaw(msg, "content", `[]`) diff --git a/internal/translator/codex/openai/chat-completions/init.go b/internal/translator/codex/openai/chat-completions/init.go index c4795f4b..8f782fda 100644 --- a/internal/translator/codex/openai/chat-completions/init.go +++ b/internal/translator/codex/openai/chat-completions/init.go @@ -3,7 +3,7 @@ package chat_completions import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/codex/openai/responses/codex_openai-responses_request.go b/internal/translator/codex/openai/responses/codex_openai-responses_request.go index 331ffc87..33dbf112 100644 --- a/internal/translator/codex/openai/responses/codex_openai-responses_request.go +++ b/internal/translator/codex/openai/responses/codex_openai-responses_request.go @@ -2,6 +2,7 @@ package responses import ( "bytes" + "strconv" "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" @@ -25,17 +26,15 @@ func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte, rawJSON, _ = sjson.DeleteBytes(rawJSON, "top_p") rawJSON, _ = sjson.DeleteBytes(rawJSON, "service_tier") + originalInstructions := "" originalInstructionsText := "" originalInstructionsResult := gjson.GetBytes(rawJSON, "instructions") if originalInstructionsResult.Exists() { + originalInstructions = originalInstructionsResult.Raw originalInstructionsText = originalInstructionsResult.String() } - // The chatgpt.com Codex backend requires a specific instruction prefix (Codex CLI prompt). - // Always provide the official Codex instructions for the target model, and move any caller - // instructions into a system message in the conversation history. hasOfficialInstructions, instructions := misc.CodexInstructionsForModel(modelName, originalInstructionsResult.String(), userAgent) - rawJSON, _ = sjson.SetBytes(rawJSON, "instructions", instructions) inputResult := gjson.GetBytes(rawJSON, "input") var inputResults []gjson.Result @@ -51,19 +50,63 @@ func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte, inputResults = []gjson.Result{} } - // Preserve caller instructions by converting them into an explicit leading user message inside "input". - // Note: Codex backend rejects role=system in input ("System messages are not allowed"). - // Only add the original instructions if we replaced them with official instructions. - if !hasOfficialInstructions && strings.TrimSpace(originalInstructionsText) != "" { - sys := `{"type":"message","role":"user","content":[{"type":"input_text","text":""}]}` - sys, _ = sjson.Set(sys, "content.0.text", originalInstructionsText) + extractedSystemInstructions := false + if originalInstructions == "" && len(inputResults) > 0 { + for _, item := range inputResults { + if strings.EqualFold(item.Get("role").String(), "system") { + var builder strings.Builder + if content := item.Get("content"); content.Exists() && content.IsArray() { + content.ForEach(func(_, contentItem gjson.Result) bool { + text := contentItem.Get("text").String() + if builder.Len() > 0 && text != "" { + builder.WriteByte('\n') + } + builder.WriteString(text) + return true + }) + } + originalInstructionsText = builder.String() + originalInstructions = strconv.Quote(originalInstructionsText) + extractedSystemInstructions = true + break + } + } + } + if hasOfficialInstructions { newInput := "[]" - newInput, _ = sjson.SetRaw(newInput, "-1", sys) for _, item := range inputResults { newInput, _ = sjson.SetRaw(newInput, "-1", item.Raw) } rawJSON, _ = sjson.SetRawBytes(rawJSON, "input", []byte(newInput)) + return rawJSON } + // log.Debugf("instructions not matched, %s\n", originalInstructions) + + if len(inputResults) > 0 { + newInput := "[]" + firstMessageHandled := false + for _, item := range inputResults { + if extractedSystemInstructions && strings.EqualFold(item.Get("role").String(), "system") { + continue + } + if !firstMessageHandled { + firstText := item.Get("content.0.text") + firstInstructions := "EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!" + if firstText.Exists() && firstText.String() != firstInstructions { + firstTextTemplate := `{"type":"message","role":"user","content":[{"type":"input_text","text":"EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!"}]}` + firstTextTemplate, _ = sjson.Set(firstTextTemplate, "content.1.text", originalInstructionsText) + firstTextTemplate, _ = sjson.Set(firstTextTemplate, "content.1.type", "input_text") + newInput, _ = sjson.SetRaw(newInput, "-1", firstTextTemplate) + } + firstMessageHandled = true + } + newInput, _ = sjson.SetRaw(newInput, "-1", item.Raw) + } + rawJSON, _ = sjson.SetRawBytes(rawJSON, "input", []byte(newInput)) + } + + rawJSON, _ = sjson.SetBytes(rawJSON, "instructions", instructions) + return rawJSON } diff --git a/internal/translator/codex/openai/responses/codex_openai-responses_response.go b/internal/translator/codex/openai/responses/codex_openai-responses_response.go index 02054afb..c18e573b 100644 --- a/internal/translator/codex/openai/responses/codex_openai-responses_response.go +++ b/internal/translator/codex/openai/responses/codex_openai-responses_response.go @@ -5,6 +5,7 @@ import ( "context" "fmt" + "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -18,7 +19,10 @@ func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string if typeResult := gjson.GetBytes(rawJSON, "type"); typeResult.Exists() { typeStr := typeResult.String() if typeStr == "response.created" || typeStr == "response.in_progress" || typeStr == "response.completed" { - rawJSON, _ = sjson.SetBytes(rawJSON, "response.instructions", gjson.GetBytes(requestRawJSON, "instructions").String()) + if gjson.GetBytes(rawJSON, "response.instructions").Exists() { + instructions := selectInstructions(originalRequestRawJSON, requestRawJSON) + rawJSON, _ = sjson.SetBytes(rawJSON, "response.instructions", instructions) + } } } out := fmt.Sprintf("data: %s", string(rawJSON)) @@ -37,6 +41,16 @@ func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, modelName } responseResult := rootResult.Get("response") template := responseResult.Raw - template, _ = sjson.Set(template, "instructions", gjson.GetBytes(requestRawJSON, "instructions").String()) + if responseResult.Get("instructions").Exists() { + template, _ = sjson.Set(template, "instructions", selectInstructions(originalRequestRawJSON, requestRawJSON)) + } return template } + +func selectInstructions(originalRequestRawJSON, requestRawJSON []byte) string { + userAgent := misc.ExtractCodexUserAgent(originalRequestRawJSON) + if misc.IsOpenCodeUserAgent(userAgent) { + return gjson.GetBytes(requestRawJSON, "instructions").String() + } + return gjson.GetBytes(originalRequestRawJSON, "instructions").String() +} diff --git a/internal/translator/codex/openai/responses/init.go b/internal/translator/codex/openai/responses/init.go index 7e9c7dfc..cab759f2 100644 --- a/internal/translator/codex/openai/responses/init.go +++ b/internal/translator/codex/openai/responses/init.go @@ -3,7 +3,7 @@ package responses import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/gemini-cli/claude/gemini-cli_claude_request.go b/internal/translator/gemini-cli/claude/gemini-cli_claude_request.go index fb16f218..f4a51e8b 100644 --- a/internal/translator/gemini-cli/claude/gemini-cli_claude_request.go +++ b/internal/translator/gemini-cli/claude/gemini-cli_claude_request.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/common" - "github.com/router-for-me/CLIProxyAPI/v6/internal/util" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -144,7 +143,6 @@ func ConvertClaudeRequestToCLI(modelName string, inputRawJSON []byte, _ bool) [] tool, _ = sjson.Delete(tool, "input_examples") tool, _ = sjson.Delete(tool, "type") tool, _ = sjson.Delete(tool, "cache_control") - tool, _ = sjson.Delete(tool, "defer_loading") if gjson.Valid(tool) && gjson.Parse(tool).IsObject() { if !hasTools { out, _ = sjson.SetRaw(out, "request.tools", `[{"functionDeclarations":[]}]`) @@ -160,8 +158,8 @@ func ConvertClaudeRequestToCLI(modelName string, inputRawJSON []byte, _ bool) [] } } - // Map Anthropic thinking -> Gemini thinkingBudget/includeThoughts when type==enabled - if t := gjson.GetBytes(rawJSON, "thinking"); t.Exists() && t.IsObject() && util.ModelSupportsThinking(modelName) { + // Map Anthropic thinking -> Gemini thinkingBudget/include_thoughts when type==enabled + if t := gjson.GetBytes(rawJSON, "thinking"); t.Exists() && t.IsObject() { if t.Get("type").String() == "enabled" { if b := t.Get("budget_tokens"); b.Exists() && b.Type == gjson.Number { budget := int(b.Int()) diff --git a/internal/translator/gemini-cli/claude/init.go b/internal/translator/gemini-cli/claude/init.go index aec0f1ff..79ed03c6 100644 --- a/internal/translator/gemini-cli/claude/init.go +++ b/internal/translator/gemini-cli/claude/init.go @@ -3,7 +3,7 @@ package claude import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/gemini-cli/gemini/gemini-cli_gemini_request.go b/internal/translator/gemini-cli/gemini/gemini-cli_gemini_request.go index 2dd5f781..ac6227fe 100644 --- a/internal/translator/gemini-cli/gemini/gemini-cli_gemini_request.go +++ b/internal/translator/gemini-cli/gemini/gemini-cli_gemini_request.go @@ -92,7 +92,6 @@ func ConvertGeminiRequestToGeminiCLI(_ string, inputRawJSON []byte, _ bool) []by strJson, _ := util.RenameKey(string(rawJSON), fmt.Sprintf("request.tools.%d.function_declarations.%d.parameters", i, j), fmt.Sprintf("request.tools.%d.function_declarations.%d.parametersJsonSchema", i, j)) rawJSON = []byte(strJson) } - rawJSON, _ = sjson.DeleteBytes(rawJSON, fmt.Sprintf("request.tools.%d.function_declarations.%d.defer_loading", i, j)) } } } diff --git a/internal/translator/gemini-cli/gemini/init.go b/internal/translator/gemini-cli/gemini/init.go index aadfc0ea..fbad4ab5 100644 --- a/internal/translator/gemini-cli/gemini/init.go +++ b/internal/translator/gemini-cli/gemini/init.go @@ -3,7 +3,7 @@ package gemini import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_response.go b/internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_response.go index f02d3658..5a1faf51 100644 --- a/internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_response.go +++ b/internal/translator/gemini-cli/openai/chat-completions/gemini-cli_openai_response.go @@ -8,7 +8,6 @@ package chat_completions import ( "bytes" "context" - "encoding/json" "fmt" "strings" "sync/atomic" @@ -176,18 +175,11 @@ func ConvertCliResponseToOpenAI(_ context.Context, _ string, originalRequestRawJ template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`) } imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array()) - imagePayload, err := json.Marshal(map[string]any{ - "index": imageIndex, - "type": "image_url", - "image_url": map[string]string{ - "url": imageURL, - }, - }) - if err != nil { - continue - } + imagePayload := `{"type":"image_url","image_url":{"url":""}}` + imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex) + imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL) template, _ = sjson.Set(template, "choices.0.delta.role", "assistant") - template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", string(imagePayload)) + template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload) } } } diff --git a/internal/translator/gemini-cli/openai/chat-completions/init.go b/internal/translator/gemini-cli/openai/chat-completions/init.go index 9f9a7305..3bd76c51 100644 --- a/internal/translator/gemini-cli/openai/chat-completions/init.go +++ b/internal/translator/gemini-cli/openai/chat-completions/init.go @@ -3,7 +3,7 @@ package chat_completions import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/gemini-cli/openai/responses/init.go b/internal/translator/gemini-cli/openai/responses/init.go index 73281bae..b25d6708 100644 --- a/internal/translator/gemini-cli/openai/responses/init.go +++ b/internal/translator/gemini-cli/openai/responses/init.go @@ -3,7 +3,7 @@ package responses import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/gemini/claude/gemini_claude_request.go b/internal/translator/gemini/claude/gemini_claude_request.go index 6852dc49..0d5361a5 100644 --- a/internal/translator/gemini/claude/gemini_claude_request.go +++ b/internal/translator/gemini/claude/gemini_claude_request.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/common" - "github.com/router-for-me/CLIProxyAPI/v6/internal/util" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -137,7 +136,6 @@ func ConvertClaudeRequestToGemini(modelName string, inputRawJSON []byte, _ bool) tool, _ = sjson.Delete(tool, "input_examples") tool, _ = sjson.Delete(tool, "type") tool, _ = sjson.Delete(tool, "cache_control") - tool, _ = sjson.Delete(tool, "defer_loading") if gjson.Valid(tool) && gjson.Parse(tool).IsObject() { if !hasTools { out, _ = sjson.SetRaw(out, "tools", `[{"functionDeclarations":[]}]`) @@ -153,9 +151,9 @@ func ConvertClaudeRequestToGemini(modelName string, inputRawJSON []byte, _ bool) } } - // Map Anthropic thinking -> Gemini thinkingBudget/includeThoughts when enabled - // Only apply for models that use numeric budgets, not discrete levels. - if t := gjson.GetBytes(rawJSON, "thinking"); t.Exists() && t.IsObject() && util.ModelSupportsThinking(modelName) && !util.ModelUsesThinkingLevels(modelName) { + // Map Anthropic thinking -> Gemini thinkingBudget/include_thoughts when enabled + // Translator only does format conversion, ApplyThinking handles model capability validation. + if t := gjson.GetBytes(rawJSON, "thinking"); t.Exists() && t.IsObject() { if t.Get("type").String() == "enabled" { if b := t.Get("budget_tokens"); b.Exists() && b.Type == gjson.Number { budget := int(b.Int()) diff --git a/internal/translator/gemini/claude/init.go b/internal/translator/gemini/claude/init.go index 183bc0bb..66fe51e7 100644 --- a/internal/translator/gemini/claude/init.go +++ b/internal/translator/gemini/claude/init.go @@ -3,7 +3,7 @@ package claude import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/gemini/gemini-cli/gemini_gemini-cli_request.go b/internal/translator/gemini/gemini-cli/gemini_gemini-cli_request.go index 4f311f92..3b70bd3e 100644 --- a/internal/translator/gemini/gemini-cli/gemini_gemini-cli_request.go +++ b/internal/translator/gemini/gemini-cli/gemini_gemini-cli_request.go @@ -41,7 +41,6 @@ func ConvertGeminiCLIRequestToGemini(_ string, inputRawJSON []byte, _ bool) []by strJson, _ := util.RenameKey(string(rawJSON), fmt.Sprintf("tools.%d.function_declarations.%d.parameters", i, j), fmt.Sprintf("tools.%d.function_declarations.%d.parametersJsonSchema", i, j)) rawJSON = []byte(strJson) } - rawJSON, _ = sjson.DeleteBytes(rawJSON, fmt.Sprintf("tools.%d.function_declarations.%d.defer_loading", i, j)) } } } diff --git a/internal/translator/gemini/gemini-cli/init.go b/internal/translator/gemini/gemini-cli/init.go index 33ce5013..2c2224f7 100644 --- a/internal/translator/gemini/gemini-cli/init.go +++ b/internal/translator/gemini/gemini-cli/init.go @@ -3,7 +3,7 @@ package geminiCLI import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/gemini/gemini/gemini_gemini_request.go b/internal/translator/gemini/gemini/gemini_gemini_request.go index 5a3ec47b..2388aaf8 100644 --- a/internal/translator/gemini/gemini/gemini_gemini_request.go +++ b/internal/translator/gemini/gemini/gemini_gemini_request.go @@ -44,7 +44,6 @@ func ConvertGeminiRequestToGemini(_ string, inputRawJSON []byte, _ bool) []byte strJson, _ := util.RenameKey(string(rawJSON), fmt.Sprintf("tools.%d.function_declarations.%d.parameters", i, j), fmt.Sprintf("tools.%d.function_declarations.%d.parametersJsonSchema", i, j)) rawJSON = []byte(strJson) } - rawJSON, _ = sjson.DeleteBytes(rawJSON, fmt.Sprintf("tools.%d.function_declarations.%d.defer_loading", i, j)) } } } diff --git a/internal/translator/gemini/gemini/init.go b/internal/translator/gemini/gemini/init.go index 63cf9674..28c97083 100644 --- a/internal/translator/gemini/gemini/init.go +++ b/internal/translator/gemini/gemini/init.go @@ -3,7 +3,7 @@ package gemini import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) // Register a no-op response translator and a request normalizer for Gemini→Gemini. diff --git a/internal/translator/gemini/openai/chat-completions/init.go b/internal/translator/gemini/openai/chat-completions/init.go index 8eedc67b..800e07db 100644 --- a/internal/translator/gemini/openai/chat-completions/init.go +++ b/internal/translator/gemini/openai/chat-completions/init.go @@ -3,7 +3,7 @@ package chat_completions import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go b/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go index be363714..5277b71b 100644 --- a/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go +++ b/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go @@ -192,7 +192,7 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte var partJSON string switch contentType { case "input_text", "output_text": - if text := contentItem.Get("text"); text.Exists() && text.String() != "" { + if text := contentItem.Get("text"); text.Exists() { partJSON = `{"text":""}` partJSON, _ = sjson.Set(partJSON, "text", text.String()) } @@ -289,15 +289,9 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte // Set the raw JSON output directly (preserves string encoding) if outputRaw != "" && outputRaw != "null" { - // Only treat as raw JSON if the ENTIRE string is valid JSON - // gjson.Parse may parse partial JSON (e.g., "[1,2,3]" from "[1,2,3]\n\nextra text") - if gjson.Valid(outputRaw) { - output := gjson.Parse(outputRaw) - if output.Type == gjson.JSON { - functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.result", output.Raw) - } else { - functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.result", outputRaw) - } + output := gjson.Parse(outputRaw) + if output.Type == gjson.JSON { + functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.result", output.Raw) } else { functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.result", outputRaw) } diff --git a/internal/translator/gemini/openai/responses/init.go b/internal/translator/gemini/openai/responses/init.go index f03a3564..b53cac3d 100644 --- a/internal/translator/gemini/openai/responses/init.go +++ b/internal/translator/gemini/openai/responses/init.go @@ -3,7 +3,7 @@ package responses import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/init.go b/internal/translator/init.go index 0754db03..084ea7ac 100644 --- a/internal/translator/init.go +++ b/internal/translator/init.go @@ -33,7 +33,4 @@ import ( _ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/antigravity/gemini" _ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/antigravity/openai/chat-completions" _ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/antigravity/openai/responses" - - _ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/kiro/claude" - _ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/kiro/openai" ) diff --git a/internal/translator/openai/claude/init.go b/internal/translator/openai/claude/init.go index 534adf47..0e0f82ea 100644 --- a/internal/translator/openai/claude/init.go +++ b/internal/translator/openai/claude/init.go @@ -3,7 +3,7 @@ package claude import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/openai/claude/openai_claude_response.go b/internal/translator/openai/claude/openai_claude_response.go index eba08499..b6e0d005 100644 --- a/internal/translator/openai/claude/openai_claude_response.go +++ b/internal/translator/openai/claude/openai_claude_response.go @@ -474,15 +474,15 @@ func collectOpenAIReasoningTexts(node gjson.Result) []string { switch node.Type { case gjson.String: - if text := strings.TrimSpace(node.String()); text != "" { + if text := node.String(); text != "" { texts = append(texts, text) } case gjson.JSON: if text := node.Get("text"); text.Exists() { - if trimmed := strings.TrimSpace(text.String()); trimmed != "" { - texts = append(texts, trimmed) + if textStr := text.String(); textStr != "" { + texts = append(texts, textStr) } - } else if raw := strings.TrimSpace(node.Raw); raw != "" && !strings.HasPrefix(raw, "{") && !strings.HasPrefix(raw, "[") { + } else if raw := node.Raw; raw != "" && !strings.HasPrefix(raw, "{") && !strings.HasPrefix(raw, "[") { texts = append(texts, raw) } } diff --git a/internal/translator/openai/gemini-cli/init.go b/internal/translator/openai/gemini-cli/init.go index 59dd8867..12aec5ec 100644 --- a/internal/translator/openai/gemini-cli/init.go +++ b/internal/translator/openai/gemini-cli/init.go @@ -3,7 +3,7 @@ package geminiCLI import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/openai/gemini/init.go b/internal/translator/openai/gemini/init.go index 755ee1c3..4f056ace 100644 --- a/internal/translator/openai/gemini/init.go +++ b/internal/translator/openai/gemini/init.go @@ -3,7 +3,7 @@ package gemini import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/openai/gemini/openai_gemini_request.go b/internal/translator/openai/gemini/openai_gemini_request.go index 5dba2421..5469a123 100644 --- a/internal/translator/openai/gemini/openai_gemini_request.go +++ b/internal/translator/openai/gemini/openai_gemini_request.go @@ -12,7 +12,7 @@ import ( "math/big" "strings" - "github.com/router-for-me/CLIProxyAPI/v6/internal/util" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -82,12 +82,16 @@ func ConvertGeminiRequestToOpenAI(modelName string, inputRawJSON []byte, stream out, _ = sjson.Set(out, "n", candidateCount.Int()) } - // Convert thinkingBudget to reasoning_effort + // Map Gemini thinkingConfig to OpenAI reasoning_effort. // Always perform conversion to support allowCompat models that may not be in registry if thinkingConfig := genConfig.Get("thinkingConfig"); thinkingConfig.Exists() && thinkingConfig.IsObject() { - if thinkingBudget := thinkingConfig.Get("thinkingBudget"); thinkingBudget.Exists() { - budget := int(thinkingBudget.Int()) - if effort, ok := util.ThinkingBudgetToEffort(modelName, budget); ok && effort != "" { + if thinkingLevel := thinkingConfig.Get("thinkingLevel"); thinkingLevel.Exists() { + effort := strings.ToLower(strings.TrimSpace(thinkingLevel.String())) + if effort != "" { + out, _ = sjson.Set(out, "reasoning_effort", effort) + } + } else if thinkingBudget := thinkingConfig.Get("thinkingBudget"); thinkingBudget.Exists() { + if effort, ok := thinking.ConvertBudgetToLevel(int(thinkingBudget.Int())); ok { out, _ = sjson.Set(out, "reasoning_effort", effort) } } diff --git a/internal/translator/openai/openai/chat-completions/init.go b/internal/translator/openai/openai/chat-completions/init.go index eab6d088..90fa3dcd 100644 --- a/internal/translator/openai/openai/chat-completions/init.go +++ b/internal/translator/openai/openai/chat-completions/init.go @@ -3,7 +3,7 @@ package chat_completions import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/openai/openai/responses/init.go b/internal/translator/openai/openai/responses/init.go index 0d74dfa1..e6f60e0e 100644 --- a/internal/translator/openai/openai/responses/init.go +++ b/internal/translator/openai/openai/responses/init.go @@ -3,7 +3,7 @@ package responses import ( . "github.com/router-for-me/CLIProxyAPI/v6/internal/constant" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" - "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator" ) func init() { diff --git a/internal/translator/translator/translator.go b/internal/translator/translator/translator.go new file mode 100644 index 00000000..11a881ad --- /dev/null +++ b/internal/translator/translator/translator.go @@ -0,0 +1,89 @@ +// Package translator provides request and response translation functionality +// between different AI API formats. It acts as a wrapper around the SDK translator +// registry, providing convenient functions for translating requests and responses +// between OpenAI, Claude, Gemini, and other API formats. +package translator + +import ( + "context" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" +) + +// registry holds the default translator registry instance. +var registry = sdktranslator.Default() + +// Register registers a new translator for converting between two API formats. +// +// Parameters: +// - from: The source API format identifier +// - to: The target API format identifier +// - request: The request translation function +// - response: The response translation function +func Register(from, to string, request interfaces.TranslateRequestFunc, response interfaces.TranslateResponse) { + registry.Register(sdktranslator.FromString(from), sdktranslator.FromString(to), request, response) +} + +// Request translates a request from one API format to another. +// +// Parameters: +// - from: The source API format identifier +// - to: The target API format identifier +// - modelName: The model name for the request +// - rawJSON: The raw JSON request data +// - stream: Whether this is a streaming request +// +// Returns: +// - []byte: The translated request JSON +func Request(from, to, modelName string, rawJSON []byte, stream bool) []byte { + return registry.TranslateRequest(sdktranslator.FromString(from), sdktranslator.FromString(to), modelName, rawJSON, stream) +} + +// NeedConvert checks if a response translation is needed between two API formats. +// +// Parameters: +// - from: The source API format identifier +// - to: The target API format identifier +// +// Returns: +// - bool: True if response translation is needed, false otherwise +func NeedConvert(from, to string) bool { + return registry.HasResponseTransformer(sdktranslator.FromString(from), sdktranslator.FromString(to)) +} + +// Response translates a streaming response from one API format to another. +// +// Parameters: +// - from: The source API format identifier +// - to: The target API format identifier +// - ctx: The context for the translation +// - modelName: The model name for the response +// - originalRequestRawJSON: The original request JSON +// - requestRawJSON: The translated request JSON +// - rawJSON: The raw response JSON +// - param: Additional parameters for translation +// +// Returns: +// - []string: The translated response lines +func Response(from, to string, ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string { + return registry.TranslateStream(ctx, sdktranslator.FromString(from), sdktranslator.FromString(to), modelName, originalRequestRawJSON, requestRawJSON, rawJSON, param) +} + +// ResponseNonStream translates a non-streaming response from one API format to another. +// +// Parameters: +// - from: The source API format identifier +// - to: The target API format identifier +// - ctx: The context for the translation +// - modelName: The model name for the response +// - originalRequestRawJSON: The original request JSON +// - requestRawJSON: The translated request JSON +// - rawJSON: The raw response JSON +// - param: Additional parameters for translation +// +// Returns: +// - string: The translated response JSON +func ResponseNonStream(from, to string, ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) string { + return registry.TranslateNonStream(ctx, sdktranslator.FromString(from), sdktranslator.FromString(to), modelName, originalRequestRawJSON, requestRawJSON, rawJSON, param) +} diff --git a/internal/usage/logger_plugin.go b/internal/usage/logger_plugin.go index 78ab91b5..7fe5a51c 100644 --- a/internal/usage/logger_plugin.go +++ b/internal/usage/logger_plugin.go @@ -12,18 +12,13 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" coreusage "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage" ) var statisticsEnabled atomic.Bool -var usageSamplePermille atomic.Uint32 -var usageRandSeed atomic.Uint64 func init() { statisticsEnabled.Store(true) - usageSamplePermille.Store(1000) - usageRandSeed.Store(uint64(time.Now().UnixNano())) coreusage.RegisterPlugin(NewLoggerPlugin()) } @@ -49,9 +44,6 @@ func (p *LoggerPlugin) HandleUsage(ctx context.Context, record coreusage.Record) if !statisticsEnabled.Load() { return } - if !shouldSampleUsage() { - return - } if p == nil || p.stats == nil { return } @@ -61,60 +53,24 @@ func (p *LoggerPlugin) HandleUsage(ctx context.Context, record coreusage.Record) // SetStatisticsEnabled toggles whether in-memory statistics are recorded. func SetStatisticsEnabled(enabled bool) { statisticsEnabled.Store(enabled) } -// SetSamplingRate sets the usage sampling rate (0.0-1.0). -func SetSamplingRate(rate float64) { - if rate < 0 { - rate = 1 - } - if rate > 1 { - rate = 1 - } - usageSamplePermille.Store(uint32(rate * 1000)) -} - // StatisticsEnabled reports the current recording state. func StatisticsEnabled() bool { return statisticsEnabled.Load() } -func shouldSampleUsage() bool { - rate := usageSamplePermille.Load() - if rate >= 1000 { - return true - } - if rate == 0 { - return false - } - return fastRandPermille() < rate -} - -func fastRandPermille() uint32 { - x := usageRandSeed.Add(0x9e3779b97f4a7c15) - x ^= x >> 30 - x *= 0xbf58476d1ce4e5b9 - x ^= x >> 27 - x *= 0x94d049bb133111eb - x ^= x >> 31 - return uint32(x % 1000) -} - // RequestStatistics maintains aggregated request metrics in memory. type RequestStatistics struct { mu sync.RWMutex - totalRequests int64 - successCount int64 - failureCount int64 - totalTokens int64 - totalInputTokens int64 - totalOutputTokens int64 + totalRequests int64 + successCount int64 + failureCount int64 + totalTokens int64 apis map[string]*apiStats - requestsByDay map[string]int64 - requestsByHour map[int]int64 - tokensByDay map[string]int64 - inputTokensByDay map[string]int64 - outputTokensByDay map[string]int64 - tokensByHour map[int]int64 + requestsByDay map[string]int64 + requestsByHour map[int]int64 + tokensByDay map[string]int64 + tokensByHour map[int]int64 } // apiStats holds aggregated metrics for a single API key. @@ -151,21 +107,22 @@ type TokenStats struct { // StatisticsSnapshot represents an immutable view of the aggregated metrics. type StatisticsSnapshot struct { - TotalRequests int64 `json:"total_requests"` - SuccessCount int64 `json:"success_count"` - FailureCount int64 `json:"failure_count"` - TotalTokens int64 `json:"total_tokens"` + TotalRequests int64 `json:"total_requests"` + SuccessCount int64 `json:"success_count"` + FailureCount int64 `json:"failure_count"` + TotalTokens int64 `json:"total_tokens"` + TotalInputTokens int64 `json:"total_input_tokens"` TotalOutputTokens int64 `json:"total_output_tokens"` APIs map[string]APISnapshot `json:"apis"` - RequestsByDay map[string]int64 `json:"requests_by_day"` - RequestsByHour map[string]int64 `json:"requests_by_hour"` - TokensByDay map[string]int64 `json:"tokens_by_day"` + RequestsByDay map[string]int64 `json:"requests_by_day"` + RequestsByHour map[string]int64 `json:"requests_by_hour"` + TokensByDay map[string]int64 `json:"tokens_by_day"` + TokensByHour map[string]int64 `json:"tokens_by_hour"` InputTokensByDay map[string]int64 `json:"input_tokens_by_day"` OutputTokensByDay map[string]int64 `json:"output_tokens_by_day"` - TokensByHour map[string]int64 `json:"tokens_by_hour"` } // APISnapshot summarises metrics for a single API key. @@ -190,13 +147,11 @@ func GetRequestStatistics() *RequestStatistics { return defaultRequestStatistics // NewRequestStatistics constructs an empty statistics store. func NewRequestStatistics() *RequestStatistics { return &RequestStatistics{ - apis: make(map[string]*apiStats), - requestsByDay: make(map[string]int64), - requestsByHour: make(map[int]int64), - tokensByDay: make(map[string]int64), - inputTokensByDay: make(map[string]int64), - outputTokensByDay: make(map[string]int64), - tokensByHour: make(map[int]int64), + apis: make(map[string]*apiStats), + requestsByDay: make(map[string]int64), + requestsByHour: make(map[int]int64), + tokensByDay: make(map[string]int64), + tokensByHour: make(map[int]int64), } } @@ -214,8 +169,6 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record) } detail := normaliseDetail(record.Detail) totalTokens := detail.TotalTokens - inputTokens := detail.InputTokens - outputTokens := detail.OutputTokens statsKey := record.APIKey if statsKey == "" { statsKey = resolveAPIIdentifier(ctx, record) @@ -242,8 +195,6 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record) s.failureCount++ } s.totalTokens += totalTokens - s.totalInputTokens += inputTokens - s.totalOutputTokens += outputTokens stats, ok := s.apis[statsKey] if !ok { @@ -261,8 +212,6 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record) s.requestsByDay[dayKey]++ s.requestsByHour[hourKey]++ s.tokensByDay[dayKey] += totalTokens - s.inputTokensByDay[dayKey] += inputTokens - s.outputTokensByDay[dayKey] += outputTokens s.tokensByHour[hourKey] += totalTokens } @@ -293,8 +242,6 @@ func (s *RequestStatistics) Snapshot() StatisticsSnapshot { result.SuccessCount = s.successCount result.FailureCount = s.failureCount result.TotalTokens = s.totalTokens - result.TotalInputTokens = s.totalInputTokens - result.TotalOutputTokens = s.totalOutputTokens result.APIs = make(map[string]APISnapshot, len(s.apis)) for apiName, stats := range s.apis { @@ -331,16 +278,6 @@ func (s *RequestStatistics) Snapshot() StatisticsSnapshot { result.TokensByDay[k] = v } - result.InputTokensByDay = make(map[string]int64, len(s.inputTokensByDay)) - for k, v := range s.inputTokensByDay { - result.InputTokensByDay[k] = v - } - - result.OutputTokensByDay = make(map[string]int64, len(s.outputTokensByDay)) - for k, v := range s.outputTokensByDay { - result.OutputTokensByDay[k] = v - } - result.TokensByHour = make(map[string]int64, len(s.tokensByHour)) for hour, v := range s.tokensByHour { key := formatHour(hour) @@ -539,121 +476,67 @@ func formatHour(hour int) string { return fmt.Sprintf("%02d", hour) } -// ComputeUsageStats converts a StatisticsSnapshot into a structured UsageStats object. -func ComputeUsageStats(snapshot StatisticsSnapshot) interfaces.UsageStats { - stats := interfaces.UsageStats{ - TotalRequests: snapshot.TotalRequests, - SuccessCount: snapshot.SuccessCount, - FailureCount: snapshot.FailureCount, - TotalInputTokens: snapshot.TotalInputTokens, - TotalOutputTokens: snapshot.TotalOutputTokens, - EstimatedCostSaved: 0, - ActualCost: 0, - DirectAPICost: 0, - Savings: 0, - SavingsPercent: 0, - ByModel: make(map[string]int64), - ByProvider: make(map[string]int64), - CostByModel: make(map[string]float64), - CostByProvider: make(map[string]float64), - Daily: make([]interfaces.DailyUsage, 0), +// UsageStats represents processed usage statistics for display. +type UsageStats struct { + TotalRequests int64 `json:"total_requests"` + SuccessCount int64 `json:"success_count"` + FailureCount int64 `json:"failure_count"` + TotalTokens int64 `json:"total_tokens"` + TotalInputTokens int64 `json:"total_input_tokens"` + TotalOutputTokens int64 `json:"total_output_tokens"` + SuccessRate float64 `json:"success_rate"` + RequestsByDay map[string]int64 `json:"requests_by_day"` + TokensByDay map[string]int64 `json:"tokens_by_day"` + TopModels []ModelUsage `json:"top_models"` +} + +// ModelUsage represents usage stats for a single model. +type ModelUsage struct { + Model string `json:"model"` + Requests int64 `json:"requests"` + Tokens int64 `json:"tokens"` + InputTokens int64 `json:"input_tokens"` + OutputTokens int64 `json:"output_tokens"` +} + +// ComputeUsageStats computes processed usage statistics from a snapshot. +func ComputeUsageStats(snapshot StatisticsSnapshot) UsageStats { + stats := UsageStats{ + TotalRequests: snapshot.TotalRequests, + SuccessCount: snapshot.SuccessCount, + FailureCount: snapshot.FailureCount, + TotalTokens: snapshot.TotalTokens, + TotalInputTokens: snapshot.TotalInputTokens, + TotalOutputTokens: snapshot.TotalOutputTokens, + RequestsByDay: snapshot.RequestsByDay, + TokensByDay: snapshot.TokensByDay, + TopModels: make([]ModelUsage, 0), } - var totalProxyCost float64 - var totalDirectCost float64 + if snapshot.TotalRequests > 0 { + stats.SuccessRate = float64(snapshot.SuccessCount) / float64(snapshot.TotalRequests) * 100 + } - for apiName, apiSnapshot := range snapshot.APIs { - // Try to identify provider from apiName - provider := apiName - // If apiName is "METHOD PATH", it's not a provider name. - if strings.Contains(apiName, " ") { - provider = "unknown" - } - - for modelName, modelSnapshot := range apiSnapshot.Models { - stats.ByModel[modelName] += modelSnapshot.TotalRequests - - // If provider is unknown, try to infer from model name - actualProvider := provider - if actualProvider == "unknown" { - actualProvider = inferProviderFromModel(modelName) - } - stats.ByProvider[actualProvider] += modelSnapshot.TotalRequests - - for _, detail := range modelSnapshot.Details { - proxyCost, directCost := estimateCostWithSavings( - actualProvider, - modelName, - detail.Tokens.InputTokens, - detail.Tokens.OutputTokens, - detail.Tokens.CachedTokens, - ) - totalProxyCost += proxyCost - totalDirectCost += directCost - stats.CostByModel[modelName] += proxyCost - stats.CostByProvider[actualProvider] += proxyCost + // Collect model usage across all APIs + modelUsage := make(map[string]*ModelUsage) + for _, api := range snapshot.APIs { + for modelName, model := range api.Models { + if existing, ok := modelUsage[modelName]; ok { + existing.Requests += model.TotalRequests + existing.Tokens += model.TotalTokens + } else { + modelUsage[modelName] = &ModelUsage{ + Model: modelName, + Requests: model.TotalRequests, + Tokens: model.TotalTokens, + } } } } - stats.ActualCost = totalProxyCost - stats.DirectAPICost = totalDirectCost - stats.Savings = totalDirectCost - totalProxyCost - if totalDirectCost > 0 { - stats.SavingsPercent = (stats.Savings / totalDirectCost) * 100 - } - // Keep backward compatibility - stats.EstimatedCostSaved = totalProxyCost - - // Last 7 days - now := time.Now() - for i := 6; i >= 0; i-- { - date := now.AddDate(0, 0, -i).Format("2006-01-02") - daily := interfaces.DailyUsage{ - Date: date, - Requests: snapshot.RequestsByDay[date], - Tokens: snapshot.TokensByDay[date], - InputTokens: snapshot.InputTokensByDay[date], - OutputTokens: snapshot.OutputTokensByDay[date], - } - stats.Daily = append(stats.Daily, daily) + for _, m := range modelUsage { + stats.TopModels = append(stats.TopModels, *m) } return stats } - -func inferProviderFromModel(model string) string { - m := strings.ToLower(model) - if strings.Contains(m, "claude") { - return "anthropic" - } - if strings.Contains(m, "gpt") { - return "openai" - } - if strings.Contains(m, "gemini") { - return "google" - } - return "unknown" -} - -func estimateCost(provider string, model string, inputTokens, outputTokens int64) float64 { - // Try model-specific pricing first - proxyCost, _, found := EstimateModelCost(model, inputTokens, outputTokens, 0) - if found { - return proxyCost - } - // Fallback to provider-based estimation - return FallbackEstimateCost(provider, model, inputTokens, outputTokens) -} - -// estimateCostWithSavings calculates both proxy cost and direct API cost. -// Returns (proxyCost, directCost). -func estimateCostWithSavings(provider string, model string, inputTokens, outputTokens, cachedTokens int64) (float64, float64) { - proxyCost, directCost, found := EstimateModelCost(model, inputTokens, outputTokens, cachedTokens) - if found { - return proxyCost, directCost - } - // Fallback - same cost for both - fallback := FallbackEstimateCost(provider, model, inputTokens, outputTokens) - return fallback, fallback -} diff --git a/internal/util/provider.go b/internal/util/provider.go index 9ab8720d..15351354 100644 --- a/internal/util/provider.go +++ b/internal/util/provider.go @@ -34,11 +34,7 @@ func GetProviderName(modelName string) []string { if modelName == "" { return nil } - if log.IsLevelEnabled(log.DebugLevel) { - log.Debugf("GetProviderName called for %q", modelName) - } - normalized := strings.ToLower(strings.TrimSpace(modelName)) providers := make([]string, 0, 4) seen := make(map[string]struct{}) @@ -53,73 +49,14 @@ func GetProviderName(modelName string) []string { providers = append(providers, name) } - regProviders := registry.GetGlobalRegistry().GetModelProviders(modelName) - for _, provider := range regProviders { + for _, provider := range registry.GetGlobalRegistry().GetModelProviders(modelName) { appendProvider(provider) } if len(providers) > 0 { - // Special-case: for gemini-3-* and antigravity-* models we prefer antigravity as primary and - // keep gemini-cli as a fallback (no load balancing between them). - if strings.HasPrefix(normalized, "gemini-3-") || strings.HasPrefix(normalized, "antigravity-") { - hasAntigravity := false - hasGeminiCLI := false - - // Explicitly enable Antigravity for models with the antigravity- prefix - if strings.HasPrefix(normalized, "antigravity-") { - hasAntigravity = true - } - - for _, p := range providers { - switch p { - case "antigravity": - hasAntigravity = true - case "gemini-cli": - hasGeminiCLI = true - } - } - if hasAntigravity || hasGeminiCLI { - ordered := make([]string, 0, len(providers)) - if hasAntigravity { - ordered = append(ordered, "antigravity") - } - if hasGeminiCLI { - ordered = append(ordered, "gemini-cli") - } - for _, p := range providers { - if p == "antigravity" || p == "gemini-cli" { - continue - } - ordered = append(ordered, p) - } - return ordered - } - } return providers } - // Fallback: infer provider from model name when registry has no entry. - // This keeps the server resilient when clients request models not yet registered. - switch { - case strings.HasPrefix(normalized, "gemini-"): - appendProvider("gemini") - case strings.HasPrefix(normalized, "antigravity-"): - appendProvider("antigravity") - case strings.HasPrefix(normalized, "kiro-"): - appendProvider("kiro") - case strings.HasPrefix(normalized, "claude-"): - appendProvider("claude") - case strings.HasPrefix(normalized, "gpt-"), - strings.HasPrefix(normalized, "chatgpt-"), - strings.HasPrefix(normalized, "o1"), - strings.HasPrefix(normalized, "o3"), - strings.HasPrefix(normalized, "o4"), - strings.HasPrefix(normalized, "o-"): - appendProvider("codex") - case strings.HasPrefix(normalized, "qwen-"): - appendProvider("qwen") - } - return providers } diff --git a/internal/util/sanitize_test.go b/internal/util/sanitize_test.go new file mode 100644 index 00000000..4ff8454b --- /dev/null +++ b/internal/util/sanitize_test.go @@ -0,0 +1,56 @@ +package util + +import ( + "testing" +) + +func TestSanitizeFunctionName(t *testing.T) { + tests := []struct { + name string + input string + expected string + }{ + {"Normal", "valid_name", "valid_name"}, + {"With Dots", "name.with.dots", "name.with.dots"}, + {"With Colons", "name:with:colons", "name:with:colons"}, + {"With Dashes", "name-with-dashes", "name-with-dashes"}, + {"Mixed Allowed", "name.with_dots:colons-dashes", "name.with_dots:colons-dashes"}, + {"Invalid Characters", "name!with@invalid#chars", "name_with_invalid_chars"}, + {"Spaces", "name with spaces", "name_with_spaces"}, + {"Non-ASCII", "name_with_你好_chars", "name_with____chars"}, + {"Starts with digit", "123name", "_123name"}, + {"Starts with dot", ".name", "_.name"}, + {"Starts with colon", ":name", "_:name"}, + {"Starts with dash", "-name", "_-name"}, + {"Starts with invalid char", "!name", "_name"}, + {"Exactly 64 chars", "this_is_a_very_long_name_that_exactly_reaches_sixty_four_charact", "this_is_a_very_long_name_that_exactly_reaches_sixty_four_charact"}, + {"Too long (65 chars)", "this_is_a_very_long_name_that_exactly_reaches_sixty_four_charactX", "this_is_a_very_long_name_that_exactly_reaches_sixty_four_charact"}, + {"Very long", "this_is_a_very_long_name_that_exceeds_the_sixty_four_character_limit_for_function_names", "this_is_a_very_long_name_that_exceeds_the_sixty_four_character_l"}, + {"Starts with digit (64 chars total)", "1234567890123456789012345678901234567890123456789012345678901234", "_123456789012345678901234567890123456789012345678901234567890123"}, + {"Starts with invalid char (64 chars total)", "!234567890123456789012345678901234567890123456789012345678901234", "_234567890123456789012345678901234567890123456789012345678901234"}, + {"Empty", "", ""}, + {"Single character invalid", "@", "_"}, + {"Single character valid", "a", "a"}, + {"Single character digit", "1", "_1"}, + {"Single character underscore", "_", "_"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := SanitizeFunctionName(tt.input) + if got != tt.expected { + t.Errorf("SanitizeFunctionName(%q) = %v, want %v", tt.input, got, tt.expected) + } + // Verify Gemini compliance + if len(got) > 64 { + t.Errorf("SanitizeFunctionName(%q) result too long: %d", tt.input, len(got)) + } + if len(got) > 0 { + first := got[0] + if !((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || first == '_') { + t.Errorf("SanitizeFunctionName(%q) result starts with invalid char: %c", tt.input, first) + } + } + }) + } +} diff --git a/internal/util/translator.go b/internal/util/translator.go index 642baf98..eca38a30 100644 --- a/internal/util/translator.go +++ b/internal/util/translator.go @@ -96,50 +96,6 @@ func DeleteKey(jsonStr, keyName string) string { return jsonStr } -// NormalizeNullableTypes converts JSON schema array types like ["string", "null"] -// or ["STRING", "NULL"] to just the first non-null type ("string" or "STRING"). -// This is required because Vertex AI / Antigravity doesn't accept array-style -// nullable type definitions in function/tool parameter schemas. -// -// For example: -// -// {"type": ["string", "null"]} => {"type": "string"} -// {"type": ["STRING", "NULL"]} => {"type": "STRING"} -// {"type": ["array", "null"]} => {"type": "array"} -// -// The function recursively finds all "type" fields throughout the JSON structure -// and normalizes any that have array values. -func NormalizeNullableTypes(jsonStr string) string { - // Find all paths to "type" fields - paths := make([]string, 0) - Walk(gjson.Parse(jsonStr), "", "type", &paths) - - for _, p := range paths { - value := gjson.Get(jsonStr, p) - if !value.IsArray() { - continue - } - - // Extract the first non-null type from the array - var primaryType string - for _, item := range value.Array() { - typeVal := item.String() - // Check for both lowercase and uppercase null - if typeVal != "null" && typeVal != "NULL" { - primaryType = typeVal - break - } - } - - // If we found a primary type, replace the array with just that type - if primaryType != "" { - jsonStr, _ = sjson.Set(jsonStr, p, primaryType) - } - } - - return jsonStr -} - // FixJSON converts non-standard JSON that uses single quotes for strings into // RFC 8259-compliant JSON by converting those single-quoted strings to // double-quoted strings with proper escaping. diff --git a/internal/util/util.go b/internal/util/util.go index de416fd0..1b954cd8 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -4,16 +4,56 @@ package util import ( + "context" "fmt" - "io/fs" "os" "path/filepath" + "regexp" "strings" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" log "github.com/sirupsen/logrus" ) +var functionNameSanitizer = regexp.MustCompile(`[^a-zA-Z0-9_.:-]`) + +// SanitizeFunctionName ensures a function name matches the requirements for Gemini/Vertex AI. +// It replaces invalid characters with underscores, ensures it starts with a letter or underscore, +// and truncates it to 64 characters if necessary. +// Regex Rule: [^a-zA-Z0-9_.:-] replaced with _. +func SanitizeFunctionName(name string) string { + if name == "" { + return "" + } + + // Replace invalid characters with underscore + sanitized := functionNameSanitizer.ReplaceAllString(name, "_") + + // Ensure it starts with a letter or underscore + // Re-reading requirements: Must start with a letter or an underscore. + if len(sanitized) > 0 { + first := sanitized[0] + if !((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || first == '_') { + // If it starts with an allowed character but not allowed at the beginning (digit, dot, colon, dash), + // we must prepend an underscore. + + // To stay within the 64-character limit while prepending, we must truncate first. + if len(sanitized) >= 64 { + sanitized = sanitized[:63] + } + sanitized = "_" + sanitized + } + } else { + sanitized = "_" + } + + // Truncate to 64 characters + if len(sanitized) > 64 { + sanitized = sanitized[:64] + } + return sanitized +} + // SetLogLevel configures the logrus log level based on the configuration. // It sets the log level to DebugLevel if debug mode is enabled, otherwise to InfoLevel. func SetLogLevel(cfg *config.Config) { @@ -31,29 +71,11 @@ func SetLogLevel(cfg *config.Config) { } } -// DefaultAuthDir returns the default auth directory path based on the OS. -// On Windows: %LOCALAPPDATA%\ProxyPilot\auth -// On other OS: ~/.config/proxypilot/auth (XDG compliant) -func DefaultAuthDir() string { - if dir := os.Getenv("LOCALAPPDATA"); dir != "" { - // Windows - return filepath.Join(dir, "ProxyPilot", "auth") - } - if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" { - return filepath.Join(dir, "proxypilot", "auth") - } - if home, err := os.UserHomeDir(); err == nil { - return filepath.Join(home, ".config", "proxypilot", "auth") - } - return "" -} - // ResolveAuthDir normalizes the auth directory path for consistent reuse throughout the app. // It expands a leading tilde (~) to the user's home directory and returns a cleaned path. -// If authDir is empty, it returns the default auth directory. func ResolveAuthDir(authDir string) (string, error) { if authDir == "" { - return DefaultAuthDir(), nil + return "", nil } if strings.HasPrefix(authDir, "~") { home, err := os.UserHomeDir() @@ -71,36 +93,23 @@ func ResolveAuthDir(authDir string) (string, error) { return filepath.Clean(authDir), nil } -// CountAuthFiles returns the number of JSON auth files located under the provided directory. -// The function resolves leading tildes to the user's home directory and performs a case-insensitive -// match on the ".json" suffix so that files saved with uppercase extensions are also counted. -func CountAuthFiles(authDir string) int { - dir, err := ResolveAuthDir(authDir) +// CountAuthFiles returns the number of auth records available through the provided Store. +// For filesystem-backed stores, this reflects the number of JSON auth files under the configured directory. +func CountAuthFiles[T any](ctx context.Context, store interface { + List(context.Context) ([]T, error) +}) int { + if store == nil { + return 0 + } + if ctx == nil { + ctx = context.Background() + } + entries, err := store.List(ctx) if err != nil { - log.Debugf("countAuthFiles: failed to resolve auth directory: %v", err) + log.Debugf("countAuthFiles: failed to list auth records: %v", err) return 0 } - if dir == "" { - return 0 - } - count := 0 - walkErr := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { - if err != nil { - log.Debugf("countAuthFiles: error accessing %s: %v", path, err) - return nil - } - if d.IsDir() { - return nil - } - if strings.HasSuffix(strings.ToLower(d.Name()), ".json") { - count++ - } - return nil - }) - if walkErr != nil { - log.Debugf("countAuthFiles: walk error: %v", walkErr) - } - return count + return len(entries) } // WritablePath returns the cleaned WRITABLE_PATH environment variable when it is set. @@ -116,3 +125,31 @@ func WritablePath() string { } return "" } + +// DefaultAuthDir returns the default auth directory path. +// It uses WRITABLE_PATH if set, otherwise falls back to ~/.cliproxy/auth. +func DefaultAuthDir() string { + if wp := WritablePath(); wp != "" { + return filepath.Join(wp, "auth") + } + home, err := os.UserHomeDir() + if err != nil { + return "" + } + return filepath.Join(home, ".cliproxy", "auth") +} + +// nullableTypePattern matches JSON type arrays with null (e.g., ["STRING","NULL"] or ["null","string"]) +var nullableTypePattern = regexp.MustCompile(`"type"\s*:\s*\[\s*"([^"]+)"\s*,\s*"(?:[Nn][Uu][Ll][Ll])"\s*\]`) +var nullableTypePatternReverse = regexp.MustCompile(`"type"\s*:\s*\[\s*"(?:[Nn][Uu][Ll][Ll])"\s*,\s*"([^"]+)"\s*\]`) + +// NormalizeNullableTypes converts JSON Schema nullable type arrays to single types. +// This is used to convert OpenAI-style nullable types (["string", "null"]) to Gemini-style +// simple types ("string") since Gemini doesn't support nullable type arrays. +func NormalizeNullableTypes(input string) string { + // Handle ["TYPE", "null"] format + result := nullableTypePattern.ReplaceAllString(input, `"type":"$1"`) + // Handle ["null", "TYPE"] format + result = nullableTypePatternReverse.ReplaceAllString(result, `"type":"$1"`) + return result +} diff --git a/internal/watcher/config_reload.go b/internal/watcher/config_reload.go index 244f738e..edac3474 100644 --- a/internal/watcher/config_reload.go +++ b/internal/watcher/config_reload.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "encoding/hex" "os" + "reflect" "time" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" @@ -126,7 +127,7 @@ func (w *Watcher) reloadConfig() bool { } authDirChanged := oldConfig == nil || oldConfig.AuthDir != newConfig.AuthDir - forceAuthRefresh := oldConfig != nil && oldConfig.ForceModelPrefix != newConfig.ForceModelPrefix + forceAuthRefresh := oldConfig != nil && (oldConfig.ForceModelPrefix != newConfig.ForceModelPrefix || !reflect.DeepEqual(oldConfig.OAuthModelAlias, newConfig.OAuthModelAlias)) log.Infof("config successfully reloaded, triggering client reload") w.reloadClients(authDirChanged, affectedOAuthProviders, forceAuthRefresh) diff --git a/internal/watcher/diff/config_diff.go b/internal/watcher/diff/config_diff.go index 05a27a5f..85b9f378 100644 --- a/internal/watcher/diff/config_diff.go +++ b/internal/watcher/diff/config_diff.go @@ -54,6 +54,9 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { if oldCfg.ForceModelPrefix != newCfg.ForceModelPrefix { changes = append(changes, fmt.Sprintf("force-model-prefix: %t -> %t", oldCfg.ForceModelPrefix, newCfg.ForceModelPrefix)) } + if oldCfg.NonStreamKeepAliveInterval != newCfg.NonStreamKeepAliveInterval { + changes = append(changes, fmt.Sprintf("nonstream-keepalive-interval: %d -> %d", oldCfg.NonStreamKeepAliveInterval, newCfg.NonStreamKeepAliveInterval)) + } // Quota-exceeded behavior if oldCfg.QuotaExceeded.SwitchProject != newCfg.QuotaExceeded.SwitchProject { @@ -90,6 +93,11 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { if !equalStringMap(o.Headers, n.Headers) { changes = append(changes, fmt.Sprintf("gemini[%d].headers: updated", i)) } + oldModels := SummarizeGeminiModels(o.Models) + newModels := SummarizeGeminiModels(n.Models) + if oldModels.hash != newModels.hash { + changes = append(changes, fmt.Sprintf("gemini[%d].models: updated (%d -> %d entries)", i, oldModels.count, newModels.count)) + } oldExcluded := SummarizeExcludedModels(o.ExcludedModels) newExcluded := SummarizeExcludedModels(n.ExcludedModels) if oldExcluded.hash != newExcluded.hash { @@ -120,6 +128,11 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { if !equalStringMap(o.Headers, n.Headers) { changes = append(changes, fmt.Sprintf("claude[%d].headers: updated", i)) } + oldModels := SummarizeClaudeModels(o.Models) + newModels := SummarizeClaudeModels(n.Models) + if oldModels.hash != newModels.hash { + changes = append(changes, fmt.Sprintf("claude[%d].models: updated (%d -> %d entries)", i, oldModels.count, newModels.count)) + } oldExcluded := SummarizeExcludedModels(o.ExcludedModels) newExcluded := SummarizeExcludedModels(n.ExcludedModels) if oldExcluded.hash != newExcluded.hash { @@ -150,6 +163,11 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { if !equalStringMap(o.Headers, n.Headers) { changes = append(changes, fmt.Sprintf("codex[%d].headers: updated", i)) } + oldModels := SummarizeCodexModels(o.Models) + newModels := SummarizeCodexModels(n.Models) + if oldModels.hash != newModels.hash { + changes = append(changes, fmt.Sprintf("codex[%d].models: updated (%d -> %d entries)", i, oldModels.count, newModels.count)) + } oldExcluded := SummarizeExcludedModels(o.ExcludedModels) newExcluded := SummarizeExcludedModels(n.ExcludedModels) if oldExcluded.hash != newExcluded.hash { @@ -158,14 +176,58 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { } } + // AmpCode settings (redacted where needed) + oldAmpURL := strings.TrimSpace(oldCfg.AmpCode.UpstreamURL) + newAmpURL := strings.TrimSpace(newCfg.AmpCode.UpstreamURL) + if oldAmpURL != newAmpURL { + changes = append(changes, fmt.Sprintf("ampcode.upstream-url: %s -> %s", oldAmpURL, newAmpURL)) + } + oldAmpKey := strings.TrimSpace(oldCfg.AmpCode.UpstreamAPIKey) + newAmpKey := strings.TrimSpace(newCfg.AmpCode.UpstreamAPIKey) + switch { + case oldAmpKey == "" && newAmpKey != "": + changes = append(changes, "ampcode.upstream-api-key: added") + case oldAmpKey != "" && newAmpKey == "": + changes = append(changes, "ampcode.upstream-api-key: removed") + case oldAmpKey != newAmpKey: + changes = append(changes, "ampcode.upstream-api-key: updated") + } + if oldCfg.AmpCode.RestrictManagementToLocalhost != newCfg.AmpCode.RestrictManagementToLocalhost { + changes = append(changes, fmt.Sprintf("ampcode.restrict-management-to-localhost: %t -> %t", oldCfg.AmpCode.RestrictManagementToLocalhost, newCfg.AmpCode.RestrictManagementToLocalhost)) + } + oldMappings := SummarizeAmpModelMappings(oldCfg.AmpCode.ModelMappings) + newMappings := SummarizeAmpModelMappings(newCfg.AmpCode.ModelMappings) + if oldMappings.hash != newMappings.hash { + changes = append(changes, fmt.Sprintf("ampcode.model-mappings: updated (%d -> %d entries)", oldMappings.count, newMappings.count)) + } + if oldCfg.AmpCode.ForceModelMappings != newCfg.AmpCode.ForceModelMappings { + changes = append(changes, fmt.Sprintf("ampcode.force-model-mappings: %t -> %t", oldCfg.AmpCode.ForceModelMappings, newCfg.AmpCode.ForceModelMappings)) + } + oldUpstreamAPIKeysCount := len(oldCfg.AmpCode.UpstreamAPIKeys) + newUpstreamAPIKeysCount := len(newCfg.AmpCode.UpstreamAPIKeys) + if !equalUpstreamAPIKeys(oldCfg.AmpCode.UpstreamAPIKeys, newCfg.AmpCode.UpstreamAPIKeys) { + changes = append(changes, fmt.Sprintf("ampcode.upstream-api-keys: updated (%d -> %d entries)", oldUpstreamAPIKeysCount, newUpstreamAPIKeysCount)) + } + if entries, _ := DiffOAuthExcludedModelChanges(oldCfg.OAuthExcludedModels, newCfg.OAuthExcludedModels); len(entries) > 0 { changes = append(changes, entries...) } + if entries, _ := DiffOAuthModelAliasChanges(oldCfg.OAuthModelAlias, newCfg.OAuthModelAlias); len(entries) > 0 { + changes = append(changes, entries...) + } // Remote management (never print the key) if oldCfg.RemoteManagement.AllowRemote != newCfg.RemoteManagement.AllowRemote { changes = append(changes, fmt.Sprintf("remote-management.allow-remote: %t -> %t", oldCfg.RemoteManagement.AllowRemote, newCfg.RemoteManagement.AllowRemote)) } + if oldCfg.RemoteManagement.DisableControlPanel != newCfg.RemoteManagement.DisableControlPanel { + changes = append(changes, fmt.Sprintf("remote-management.disable-control-panel: %t -> %t", oldCfg.RemoteManagement.DisableControlPanel, newCfg.RemoteManagement.DisableControlPanel)) + } + oldPanelRepo := strings.TrimSpace(oldCfg.RemoteManagement.PanelGitHubRepository) + newPanelRepo := strings.TrimSpace(newCfg.RemoteManagement.PanelGitHubRepository) + if oldPanelRepo != newPanelRepo { + changes = append(changes, fmt.Sprintf("remote-management.panel-github-repository: %s -> %s", oldPanelRepo, newPanelRepo)) + } if oldCfg.RemoteManagement.SecretKey != newCfg.RemoteManagement.SecretKey { switch { case oldCfg.RemoteManagement.SecretKey == "" && newCfg.RemoteManagement.SecretKey != "": @@ -265,3 +327,43 @@ func formatProxyURL(raw string) string { } return scheme + "://" + host } + +func equalStringSet(a, b []string) bool { + if len(a) == 0 && len(b) == 0 { + return true + } + aSet := make(map[string]struct{}, len(a)) + for _, k := range a { + aSet[strings.TrimSpace(k)] = struct{}{} + } + bSet := make(map[string]struct{}, len(b)) + for _, k := range b { + bSet[strings.TrimSpace(k)] = struct{}{} + } + if len(aSet) != len(bSet) { + return false + } + for k := range aSet { + if _, ok := bSet[k]; !ok { + return false + } + } + return true +} + +// equalUpstreamAPIKeys compares two slices of AmpUpstreamAPIKeyEntry for equality. +// Comparison is done by count and content (upstream key and client keys). +func equalUpstreamAPIKeys(a, b []config.AmpUpstreamAPIKeyEntry) bool { + if len(a) != len(b) { + return false + } + for i := range a { + if strings.TrimSpace(a[i].UpstreamAPIKey) != strings.TrimSpace(b[i].UpstreamAPIKey) { + return false + } + if !equalStringSet(a[i].APIKeys, b[i].APIKeys) { + return false + } + } + return true +} diff --git a/internal/watcher/diff/config_diff_test.go b/internal/watcher/diff/config_diff_test.go index bc563a13..82486659 100644 --- a/internal/watcher/diff/config_diff_test.go +++ b/internal/watcher/diff/config_diff_test.go @@ -14,9 +14,16 @@ func TestBuildConfigChangeDetails(t *testing.T) { GeminiKey: []config.GeminiKey{ {APIKey: "old", BaseURL: "http://old", ExcludedModels: []string{"old-model"}}, }, + AmpCode: config.AmpCode{ + UpstreamURL: "http://old-upstream", + ModelMappings: []config.AmpModelMapping{{From: "from-old", To: "to-old"}}, + RestrictManagementToLocalhost: false, + }, RemoteManagement: config.RemoteManagement{ - AllowRemote: false, - SecretKey: "old", + AllowRemote: false, + SecretKey: "old", + DisableControlPanel: false, + PanelGitHubRepository: "repo-old", }, OAuthExcludedModels: map[string][]string{ "providerA": {"m1"}, @@ -38,9 +45,19 @@ func TestBuildConfigChangeDetails(t *testing.T) { GeminiKey: []config.GeminiKey{ {APIKey: "old", BaseURL: "http://old", ExcludedModels: []string{"old-model", "extra"}}, }, + AmpCode: config.AmpCode{ + UpstreamURL: "http://new-upstream", + RestrictManagementToLocalhost: true, + ModelMappings: []config.AmpModelMapping{ + {From: "from-old", To: "to-old"}, + {From: "from-new", To: "to-new"}, + }, + }, RemoteManagement: config.RemoteManagement{ - AllowRemote: true, - SecretKey: "new", + AllowRemote: true, + SecretKey: "new", + DisableControlPanel: true, + PanelGitHubRepository: "repo-new", }, OAuthExcludedModels: map[string][]string{ "providerA": {"m1", "m2"}, @@ -68,6 +85,8 @@ func TestBuildConfigChangeDetails(t *testing.T) { expectContains(t, details, "port: 8080 -> 9090") expectContains(t, details, "auth-dir: /tmp/auth-old -> /tmp/auth-new") expectContains(t, details, "gemini[0].excluded-models: updated (1 -> 2 entries)") + expectContains(t, details, "ampcode.upstream-url: http://old-upstream -> http://new-upstream") + expectContains(t, details, "ampcode.model-mappings: updated (1 -> 2 entries)") expectContains(t, details, "remote-management.allow-remote: false -> true") expectContains(t, details, "remote-management.secret-key: updated") expectContains(t, details, "oauth-excluded-models[providera]: updated (1 -> 2 entries)") @@ -86,7 +105,7 @@ func TestBuildConfigChangeDetails_NoChanges(t *testing.T) { } } -func TestBuildConfigChangeDetails_GeminiVertexHeaders(t *testing.T) { +func TestBuildConfigChangeDetails_GeminiVertexHeadersAndForceMappings(t *testing.T) { oldCfg := &config.Config{ GeminiKey: []config.GeminiKey{ {APIKey: "g1", Headers: map[string]string{"H": "1"}, ExcludedModels: []string{"a"}}, @@ -94,6 +113,10 @@ func TestBuildConfigChangeDetails_GeminiVertexHeaders(t *testing.T) { VertexCompatAPIKey: []config.VertexCompatKey{ {APIKey: "v1", BaseURL: "http://v-old", Models: []config.VertexCompatModel{{Name: "m1"}}}, }, + AmpCode: config.AmpCode{ + ModelMappings: []config.AmpModelMapping{{From: "a", To: "b"}}, + ForceModelMappings: false, + }, } newCfg := &config.Config{ GeminiKey: []config.GeminiKey{ @@ -102,11 +125,17 @@ func TestBuildConfigChangeDetails_GeminiVertexHeaders(t *testing.T) { VertexCompatAPIKey: []config.VertexCompatKey{ {APIKey: "v1", BaseURL: "http://v-new", Models: []config.VertexCompatModel{{Name: "m1"}, {Name: "m2"}}}, }, + AmpCode: config.AmpCode{ + ModelMappings: []config.AmpModelMapping{{From: "a", To: "c"}}, + ForceModelMappings: true, + }, } details := BuildConfigChangeDetails(oldCfg, newCfg) expectContains(t, details, "gemini[0].headers: updated") expectContains(t, details, "gemini[0].excluded-models: updated (1 -> 2 entries)") + expectContains(t, details, "ampcode.model-mappings: updated (1 -> 1 entries)") + expectContains(t, details, "ampcode.force-model-mappings: false -> true") } func TestBuildConfigChangeDetails_ModelPrefixes(t *testing.T) { @@ -160,6 +189,9 @@ func TestBuildConfigChangeDetails_SecretsAndCounts(t *testing.T) { SDKConfig: sdkconfig.SDKConfig{ APIKeys: []string{"a"}, }, + AmpCode: config.AmpCode{ + UpstreamAPIKey: "", + }, RemoteManagement: config.RemoteManagement{ SecretKey: "", }, @@ -168,6 +200,9 @@ func TestBuildConfigChangeDetails_SecretsAndCounts(t *testing.T) { SDKConfig: sdkconfig.SDKConfig{ APIKeys: []string{"a", "b", "c"}, }, + AmpCode: config.AmpCode{ + UpstreamAPIKey: "new-key", + }, RemoteManagement: config.RemoteManagement{ SecretKey: "new-secret", }, @@ -175,6 +210,7 @@ func TestBuildConfigChangeDetails_SecretsAndCounts(t *testing.T) { details := BuildConfigChangeDetails(oldCfg, newCfg) expectContains(t, details, "api-keys count: 1 -> 3") + expectContains(t, details, "ampcode.upstream-api-key: added") expectContains(t, details, "remote-management.secret-key: created") } @@ -190,14 +226,16 @@ func TestBuildConfigChangeDetails_FlagsAndKeys(t *testing.T) { MaxRetryInterval: 1, WebsocketAuth: false, QuotaExceeded: config.QuotaExceeded{SwitchProject: false, SwitchPreviewModel: false}, - ClaudeKey: []config.ClaudeKey{{APIKey: "c1"}}, - CodexKey: []config.CodexKey{{APIKey: "x1"}}, - RemoteManagement: config.RemoteManagement{SecretKey: "keep"}, + ClaudeKey: []config.ClaudeKey{{APIKey: "c1"}}, + CodexKey: []config.CodexKey{{APIKey: "x1"}}, + AmpCode: config.AmpCode{UpstreamAPIKey: "keep", RestrictManagementToLocalhost: false}, + RemoteManagement: config.RemoteManagement{DisableControlPanel: false, PanelGitHubRepository: "old/repo", SecretKey: "keep"}, SDKConfig: sdkconfig.SDKConfig{ - RequestLog: false, - ProxyURL: "http://old-proxy", - APIKeys: []string{"key-1"}, - ForceModelPrefix: false, + RequestLog: false, + ProxyURL: "http://old-proxy", + APIKeys: []string{"key-1"}, + ForceModelPrefix: false, + NonStreamKeepAliveInterval: 0, }, } newCfg := &config.Config{ @@ -219,14 +257,22 @@ func TestBuildConfigChangeDetails_FlagsAndKeys(t *testing.T) { {APIKey: "x1", BaseURL: "http://x", ProxyURL: "http://px", Headers: map[string]string{"H": "2"}, ExcludedModels: []string{"b"}}, {APIKey: "x2"}, }, + AmpCode: config.AmpCode{ + UpstreamAPIKey: "", + RestrictManagementToLocalhost: true, + ModelMappings: []config.AmpModelMapping{{From: "a", To: "b"}}, + }, RemoteManagement: config.RemoteManagement{ - SecretKey: "", + DisableControlPanel: true, + PanelGitHubRepository: "new/repo", + SecretKey: "", }, SDKConfig: sdkconfig.SDKConfig{ - RequestLog: true, - ProxyURL: "http://new-proxy", - APIKeys: []string{" key-1 ", "key-2"}, - ForceModelPrefix: true, + RequestLog: true, + ProxyURL: "http://new-proxy", + APIKeys: []string{" key-1 ", "key-2"}, + ForceModelPrefix: true, + NonStreamKeepAliveInterval: 5, }, } @@ -241,11 +287,16 @@ func TestBuildConfigChangeDetails_FlagsAndKeys(t *testing.T) { expectContains(t, details, "proxy-url: http://old-proxy -> http://new-proxy") expectContains(t, details, "ws-auth: false -> true") expectContains(t, details, "force-model-prefix: false -> true") + expectContains(t, details, "nonstream-keepalive-interval: 0 -> 5") expectContains(t, details, "quota-exceeded.switch-project: false -> true") expectContains(t, details, "quota-exceeded.switch-preview-model: false -> true") expectContains(t, details, "api-keys count: 1 -> 2") expectContains(t, details, "claude-api-key count: 1 -> 2") expectContains(t, details, "codex-api-key count: 1 -> 2") + expectContains(t, details, "ampcode.restrict-management-to-localhost: false -> true") + expectContains(t, details, "ampcode.upstream-api-key: removed") + expectContains(t, details, "remote-management.disable-control-panel: false -> true") + expectContains(t, details, "remote-management.panel-github-repository: old/repo -> new/repo") expectContains(t, details, "remote-management.secret-key: deleted") } @@ -273,9 +324,18 @@ func TestBuildConfigChangeDetails_AllBranches(t *testing.T) { VertexCompatAPIKey: []config.VertexCompatKey{ {APIKey: "v-old", BaseURL: "http://v-old", ProxyURL: "http://vp-old", Headers: map[string]string{"H": "1"}, Models: []config.VertexCompatModel{{Name: "m1"}}}, }, + AmpCode: config.AmpCode{ + UpstreamURL: "http://amp-old", + UpstreamAPIKey: "old-key", + RestrictManagementToLocalhost: false, + ModelMappings: []config.AmpModelMapping{{From: "a", To: "b"}}, + ForceModelMappings: false, + }, RemoteManagement: config.RemoteManagement{ - AllowRemote: false, - SecretKey: "old", + AllowRemote: false, + DisableControlPanel: false, + PanelGitHubRepository: "old/repo", + SecretKey: "old", }, SDKConfig: sdkconfig.SDKConfig{ RequestLog: false, @@ -316,9 +376,18 @@ func TestBuildConfigChangeDetails_AllBranches(t *testing.T) { VertexCompatAPIKey: []config.VertexCompatKey{ {APIKey: "v-new", BaseURL: "http://v-new", ProxyURL: "http://vp-new", Headers: map[string]string{"H": "2"}, Models: []config.VertexCompatModel{{Name: "m1"}, {Name: "m2"}}}, }, + AmpCode: config.AmpCode{ + UpstreamURL: "http://amp-new", + UpstreamAPIKey: "", + RestrictManagementToLocalhost: true, + ModelMappings: []config.AmpModelMapping{{From: "a", To: "c"}}, + ForceModelMappings: true, + }, RemoteManagement: config.RemoteManagement{ - AllowRemote: true, - SecretKey: "", + AllowRemote: true, + DisableControlPanel: true, + PanelGitHubRepository: "new/repo", + SecretKey: "", }, SDKConfig: sdkconfig.SDKConfig{ RequestLog: true, @@ -376,9 +445,16 @@ func TestBuildConfigChangeDetails_AllBranches(t *testing.T) { expectContains(t, changes, "vertex[0].api-key: updated") expectContains(t, changes, "vertex[0].models: updated (1 -> 2 entries)") expectContains(t, changes, "vertex[0].headers: updated") + expectContains(t, changes, "ampcode.upstream-url: http://amp-old -> http://amp-new") + expectContains(t, changes, "ampcode.upstream-api-key: removed") + expectContains(t, changes, "ampcode.restrict-management-to-localhost: false -> true") + expectContains(t, changes, "ampcode.model-mappings: updated (1 -> 1 entries)") + expectContains(t, changes, "ampcode.force-model-mappings: false -> true") expectContains(t, changes, "oauth-excluded-models[p1]: updated (1 -> 2 entries)") expectContains(t, changes, "oauth-excluded-models[p2]: added (1 entries)") expectContains(t, changes, "remote-management.allow-remote: false -> true") + expectContains(t, changes, "remote-management.disable-control-panel: false -> true") + expectContains(t, changes, "remote-management.panel-github-repository: old/repo -> new/repo") expectContains(t, changes, "remote-management.secret-key: deleted") expectContains(t, changes, "openai-compatibility:") } @@ -407,19 +483,26 @@ func TestFormatProxyURL(t *testing.T) { } } -func TestBuildConfigChangeDetails_SecretUpdates(t *testing.T) { +func TestBuildConfigChangeDetails_SecretAndUpstreamUpdates(t *testing.T) { oldCfg := &config.Config{ + AmpCode: config.AmpCode{ + UpstreamAPIKey: "old", + }, RemoteManagement: config.RemoteManagement{ SecretKey: "old", }, } newCfg := &config.Config{ + AmpCode: config.AmpCode{ + UpstreamAPIKey: "new", + }, RemoteManagement: config.RemoteManagement{ SecretKey: "new", }, } changes := BuildConfigChangeDetails(oldCfg, newCfg) + expectContains(t, changes, "ampcode.upstream-api-key: updated") expectContains(t, changes, "remote-management.secret-key: updated") } diff --git a/internal/watcher/diff/model_hash.go b/internal/watcher/diff/model_hash.go index a224bdca..5779facc 100644 --- a/internal/watcher/diff/model_hash.go +++ b/internal/watcher/diff/model_hash.go @@ -71,6 +71,21 @@ func ComputeCodexModelsHash(models []config.CodexModel) string { return hashJoined(keys) } +// ComputeGeminiModelsHash returns a stable hash for Gemini model aliases. +func ComputeGeminiModelsHash(models []config.GeminiModel) string { + keys := normalizeModelPairs(func(out func(key string)) { + for _, model := range models { + name := strings.TrimSpace(model.Name) + alias := strings.TrimSpace(model.Alias) + if name == "" && alias == "" { + continue + } + out(strings.ToLower(name) + "|" + strings.ToLower(alias)) + } + }) + return hashJoined(keys) +} + // ComputeExcludedModelsHash returns a normalized hash for excluded model lists. func ComputeExcludedModelsHash(excluded []string) string { if len(excluded) == 0 { diff --git a/internal/watcher/diff/models_summary.go b/internal/watcher/diff/models_summary.go new file mode 100644 index 00000000..9c2aa91a --- /dev/null +++ b/internal/watcher/diff/models_summary.go @@ -0,0 +1,121 @@ +package diff + +import ( + "crypto/sha256" + "encoding/hex" + "sort" + "strings" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" +) + +type GeminiModelsSummary struct { + hash string + count int +} + +type ClaudeModelsSummary struct { + hash string + count int +} + +type CodexModelsSummary struct { + hash string + count int +} + +type VertexModelsSummary struct { + hash string + count int +} + +// SummarizeGeminiModels hashes Gemini model aliases for change detection. +func SummarizeGeminiModels(models []config.GeminiModel) GeminiModelsSummary { + if len(models) == 0 { + return GeminiModelsSummary{} + } + keys := normalizeModelPairs(func(out func(key string)) { + for _, model := range models { + name := strings.TrimSpace(model.Name) + alias := strings.TrimSpace(model.Alias) + if name == "" && alias == "" { + continue + } + out(strings.ToLower(name) + "|" + strings.ToLower(alias)) + } + }) + return GeminiModelsSummary{ + hash: hashJoined(keys), + count: len(keys), + } +} + +// SummarizeClaudeModels hashes Claude model aliases for change detection. +func SummarizeClaudeModels(models []config.ClaudeModel) ClaudeModelsSummary { + if len(models) == 0 { + return ClaudeModelsSummary{} + } + keys := normalizeModelPairs(func(out func(key string)) { + for _, model := range models { + name := strings.TrimSpace(model.Name) + alias := strings.TrimSpace(model.Alias) + if name == "" && alias == "" { + continue + } + out(strings.ToLower(name) + "|" + strings.ToLower(alias)) + } + }) + return ClaudeModelsSummary{ + hash: hashJoined(keys), + count: len(keys), + } +} + +// SummarizeCodexModels hashes Codex model aliases for change detection. +func SummarizeCodexModels(models []config.CodexModel) CodexModelsSummary { + if len(models) == 0 { + return CodexModelsSummary{} + } + keys := normalizeModelPairs(func(out func(key string)) { + for _, model := range models { + name := strings.TrimSpace(model.Name) + alias := strings.TrimSpace(model.Alias) + if name == "" && alias == "" { + continue + } + out(strings.ToLower(name) + "|" + strings.ToLower(alias)) + } + }) + return CodexModelsSummary{ + hash: hashJoined(keys), + count: len(keys), + } +} + +// SummarizeVertexModels hashes Vertex-compatible model aliases for change detection. +func SummarizeVertexModels(models []config.VertexCompatModel) VertexModelsSummary { + if len(models) == 0 { + return VertexModelsSummary{} + } + names := make([]string, 0, len(models)) + for _, model := range models { + name := strings.TrimSpace(model.Name) + alias := strings.TrimSpace(model.Alias) + if name == "" && alias == "" { + continue + } + if alias != "" { + name = alias + } + names = append(names, name) + } + if len(names) == 0 { + return VertexModelsSummary{} + } + sort.Strings(names) + sum := sha256.Sum256([]byte(strings.Join(names, "|"))) + return VertexModelsSummary{ + hash: hex.EncodeToString(sum[:]), + count: len(names), + } +} diff --git a/internal/watcher/diff/oauth_excluded.go b/internal/watcher/diff/oauth_excluded.go index 4c2a611b..0efd44df 100644 --- a/internal/watcher/diff/oauth_excluded.go +++ b/internal/watcher/diff/oauth_excluded.go @@ -15,11 +15,6 @@ type ExcludedModelsSummary struct { count int } -type VertexModelsSummary struct { - hash string - count int -} - // SummarizeExcludedModels normalizes and hashes an excluded-model list. func SummarizeExcludedModels(list []string) ExcludedModelsSummary { if len(list) == 0 { @@ -92,30 +87,32 @@ func DiffOAuthExcludedModelChanges(oldMap, newMap map[string][]string) ([]string return changes, affected } -// SummarizeVertexModels hashes vertex-compatible models for change detection. -func SummarizeVertexModels(models []config.VertexCompatModel) VertexModelsSummary { - if len(models) == 0 { - return VertexModelsSummary{} +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{} } - names := make([]string, 0, len(models)) - for _, m := range models { - name := strings.TrimSpace(m.Name) - alias := strings.TrimSpace(m.Alias) - if name == "" && alias == "" { + entries := make([]string, 0, len(mappings)) + for _, mapping := range mappings { + from := strings.TrimSpace(mapping.From) + to := strings.TrimSpace(mapping.To) + if from == "" && to == "" { continue } - if alias != "" { - name = alias - } - names = append(names, name) + entries = append(entries, from+"->"+to) } - if len(names) == 0 { - return VertexModelsSummary{} + if len(entries) == 0 { + return AmpModelMappingsSummary{} } - sort.Strings(names) - sum := sha256.Sum256([]byte(strings.Join(names, "|"))) - return VertexModelsSummary{ + sort.Strings(entries) + sum := sha256.Sum256([]byte(strings.Join(entries, "|"))) + return AmpModelMappingsSummary{ hash: hex.EncodeToString(sum[:]), - count: len(names), + count: len(entries), } } diff --git a/internal/watcher/diff/oauth_excluded_test.go b/internal/watcher/diff/oauth_excluded_test.go index 9646c848..f5ad3913 100644 --- a/internal/watcher/diff/oauth_excluded_test.go +++ b/internal/watcher/diff/oauth_excluded_test.go @@ -39,6 +39,26 @@ func TestDiffOAuthExcludedModelChanges(t *testing.T) { } } +func TestSummarizeAmpModelMappings(t *testing.T) { + summary := SummarizeAmpModelMappings([]config.AmpModelMapping{ + {From: "a", To: "A"}, + {From: "b", To: "B"}, + {From: " ", To: " "}, // ignored + }) + if summary.count != 2 { + t.Fatalf("expected 2 entries, got %d", summary.count) + } + if summary.hash == "" { + t.Fatal("expected non-empty hash") + } + if empty := SummarizeAmpModelMappings(nil); empty.count != 0 || empty.hash != "" { + t.Fatalf("expected empty summary for nil input, got %+v", empty) + } + if blank := SummarizeAmpModelMappings([]config.AmpModelMapping{{From: " ", To: " "}}); blank.count != 0 || blank.hash != "" { + t.Fatalf("expected blank mappings ignored, got %+v", blank) + } +} + func TestSummarizeOAuthExcludedModels_NormalizesKeys(t *testing.T) { out := SummarizeOAuthExcludedModels(map[string][]string{ "ProvA": {"X"}, diff --git a/internal/watcher/diff/oauth_model_alias.go b/internal/watcher/diff/oauth_model_alias.go new file mode 100644 index 00000000..c5a17d29 --- /dev/null +++ b/internal/watcher/diff/oauth_model_alias.go @@ -0,0 +1,101 @@ +package diff + +import ( + "crypto/sha256" + "encoding/hex" + "fmt" + "sort" + "strings" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" +) + +type OAuthModelAliasSummary struct { + hash string + count int +} + +// SummarizeOAuthModelAlias summarizes OAuth model alias per channel. +func SummarizeOAuthModelAlias(entries map[string][]config.OAuthModelAlias) map[string]OAuthModelAliasSummary { + if len(entries) == 0 { + return nil + } + out := make(map[string]OAuthModelAliasSummary, len(entries)) + for k, v := range entries { + key := strings.ToLower(strings.TrimSpace(k)) + if key == "" { + continue + } + out[key] = summarizeOAuthModelAliasList(v) + } + if len(out) == 0 { + return nil + } + return out +} + +// DiffOAuthModelAliasChanges compares OAuth model alias maps. +func DiffOAuthModelAliasChanges(oldMap, newMap map[string][]config.OAuthModelAlias) ([]string, []string) { + oldSummary := SummarizeOAuthModelAlias(oldMap) + newSummary := SummarizeOAuthModelAlias(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-model-alias[%s]: removed", key)) + affected = append(affected, key) + case !okOld && okNew: + changes = append(changes, fmt.Sprintf("oauth-model-alias[%s]: added (%d entries)", key, newInfo.count)) + affected = append(affected, key) + case okOld && okNew && oldInfo.hash != newInfo.hash: + changes = append(changes, fmt.Sprintf("oauth-model-alias[%s]: updated (%d -> %d entries)", key, oldInfo.count, newInfo.count)) + affected = append(affected, key) + } + } + sort.Strings(changes) + sort.Strings(affected) + return changes, affected +} + +func summarizeOAuthModelAliasList(list []config.OAuthModelAlias) OAuthModelAliasSummary { + if len(list) == 0 { + return OAuthModelAliasSummary{} + } + seen := make(map[string]struct{}, len(list)) + normalized := make([]string, 0, len(list)) + for _, alias := range list { + name := strings.ToLower(strings.TrimSpace(alias.Name)) + aliasVal := strings.ToLower(strings.TrimSpace(alias.Alias)) + if name == "" || aliasVal == "" { + continue + } + key := name + "->" + aliasVal + if alias.Fork { + key += "|fork" + } + if _, exists := seen[key]; exists { + continue + } + seen[key] = struct{}{} + normalized = append(normalized, key) + } + if len(normalized) == 0 { + return OAuthModelAliasSummary{} + } + sort.Strings(normalized) + sum := sha256.Sum256([]byte(strings.Join(normalized, "|"))) + return OAuthModelAliasSummary{ + hash: hex.EncodeToString(sum[:]), + count: len(normalized), + } +} diff --git a/internal/watcher/events.go b/internal/watcher/events.go index eb428353..250cf75c 100644 --- a/internal/watcher/events.go +++ b/internal/watcher/events.go @@ -13,7 +13,6 @@ import ( "time" "github.com/fsnotify/fsnotify" - kiroauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/kiro" log "github.com/sirupsen/logrus" ) @@ -40,35 +39,12 @@ func (w *Watcher) start(ctx context.Context) error { } log.Debugf("watching auth directory: %s", w.authDir) - w.watchKiroIDETokenFile() - go w.processEvents(ctx) w.reloadClients(true, nil, false) return nil } -func (w *Watcher) watchKiroIDETokenFile() { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Debugf("failed to get home directory for Kiro IDE token watch: %v", err) - return - } - - kiroTokenDir := filepath.Join(homeDir, ".aws", "sso", "cache") - - if _, statErr := os.Stat(kiroTokenDir); os.IsNotExist(statErr) { - log.Debugf("Kiro IDE token directory does not exist: %s", kiroTokenDir) - return - } - - if errAdd := w.watcher.Add(kiroTokenDir); errAdd != nil { - log.Debugf("failed to watch Kiro IDE token directory %s: %v", kiroTokenDir, errAdd) - return - } - log.Debugf("watching Kiro IDE token directory: %s", kiroTokenDir) -} - func (w *Watcher) processEvents(ctx context.Context) { for { select { @@ -97,17 +73,11 @@ func (w *Watcher) handleEvent(event fsnotify.Event) { isConfigEvent := normalizedName == normalizedConfigPath && event.Op&configOps != 0 authOps := fsnotify.Create | fsnotify.Write | fsnotify.Remove | fsnotify.Rename isAuthJSON := strings.HasPrefix(normalizedName, normalizedAuthDir) && strings.HasSuffix(normalizedName, ".json") && event.Op&authOps != 0 - isKiroIDEToken := w.isKiroIDETokenFile(event.Name) && event.Op&authOps != 0 - if !isConfigEvent && !isAuthJSON && !isKiroIDEToken { + if !isConfigEvent && !isAuthJSON { // Ignore unrelated files (e.g., cookie snapshots *.cookie) and other noise. return } - if isKiroIDEToken { - w.handleKiroIDETokenChange(event) - return - } - now := time.Now() log.Debugf("file system event detected: %s %s", event.Op.String(), event.Name) @@ -154,42 +124,6 @@ func (w *Watcher) handleEvent(event fsnotify.Event) { } } -func (w *Watcher) isKiroIDETokenFile(path string) bool { - normalized := filepath.ToSlash(path) - return strings.HasSuffix(normalized, "kiro-auth-token.json") && strings.Contains(normalized, ".aws/sso/cache") -} - -func (w *Watcher) handleKiroIDETokenChange(event fsnotify.Event) { - log.Debugf("Kiro IDE token file event detected: %s %s", event.Op.String(), event.Name) - - if event.Op&(fsnotify.Remove|fsnotify.Rename) != 0 { - time.Sleep(replaceCheckDelay) - if _, statErr := os.Stat(event.Name); statErr != nil { - log.Debugf("Kiro IDE token file removed: %s", event.Name) - return - } - } - - tokenData, err := kiroauth.LoadKiroIDEToken() - if err != nil { - log.Debugf("failed to load Kiro IDE token after change: %v", err) - return - } - - log.Infof("Kiro IDE token file updated, access token refreshed (provider: %s)", tokenData.Provider) - - w.refreshAuthState(true) - - w.clientsMutex.RLock() - cfg := w.config - w.clientsMutex.RUnlock() - - if w.reloadCallback != nil && cfg != nil { - log.Debugf("triggering server update callback after Kiro IDE token change") - w.reloadCallback(cfg) - } -} - func (w *Watcher) authFileUnchanged(path string) (bool, error) { data, errRead := os.ReadFile(path) if errRead != nil { diff --git a/internal/watcher/synthesizer/config.go b/internal/watcher/synthesizer/config.go index ece7c29c..b1ae5885 100644 --- a/internal/watcher/synthesizer/config.go +++ b/internal/watcher/synthesizer/config.go @@ -2,12 +2,11 @@ package synthesizer import ( "fmt" + "strconv" "strings" - kiroauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/kiro" "github.com/router-for-me/CLIProxyAPI/v6/internal/watcher/diff" coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" - log "github.com/sirupsen/logrus" ) // ConfigSynthesizer generates Auth entries from configuration API keys. @@ -32,8 +31,6 @@ func (s *ConfigSynthesizer) Synthesize(ctx *SynthesisContext) ([]*coreauth.Auth, out = append(out, s.synthesizeClaudeKeys(ctx)...) // Codex API Keys out = append(out, s.synthesizeCodexKeys(ctx)...) - // Kiro (AWS CodeWhisperer) - out = append(out, s.synthesizeKiroKeys(ctx)...) // OpenAI-compat out = append(out, s.synthesizeOpenAICompat(ctx)...) // Vertex-compat @@ -63,9 +60,15 @@ func (s *ConfigSynthesizer) synthesizeGeminiKeys(ctx *SynthesisContext) []*corea "source": fmt.Sprintf("config:gemini[%s]", token), "api_key": key, } + if entry.Priority != 0 { + attrs["priority"] = strconv.Itoa(entry.Priority) + } if base != "" { attrs["base_url"] = base } + if hash := diff.ComputeGeminiModelsHash(entry.Models); hash != "" { + attrs["models_hash"] = hash + } addConfigHeadersToAttrs(entry.Headers, attrs) a := &coreauth.Auth{ ID: id, @@ -104,6 +107,9 @@ func (s *ConfigSynthesizer) synthesizeClaudeKeys(ctx *SynthesisContext) []*corea "source": fmt.Sprintf("config:claude[%s]", token), "api_key": key, } + if ck.Priority != 0 { + attrs["priority"] = strconv.Itoa(ck.Priority) + } if base != "" { attrs["base_url"] = base } @@ -148,6 +154,9 @@ func (s *ConfigSynthesizer) synthesizeCodexKeys(ctx *SynthesisContext) []*coreau "source": fmt.Sprintf("config:codex[%s]", token), "api_key": key, } + if ck.Priority != 0 { + attrs["priority"] = strconv.Itoa(ck.Priority) + } if ck.BaseURL != "" { attrs["base_url"] = ck.BaseURL } @@ -203,6 +212,9 @@ func (s *ConfigSynthesizer) synthesizeOpenAICompat(ctx *SynthesisContext) []*cor "compat_name": compat.Name, "provider_key": providerName, } + if compat.Priority != 0 { + attrs["priority"] = strconv.Itoa(compat.Priority) + } if key != "" { attrs["api_key"] = key } @@ -234,6 +246,9 @@ func (s *ConfigSynthesizer) synthesizeOpenAICompat(ctx *SynthesisContext) []*cor "compat_name": compat.Name, "provider_key": providerName, } + if compat.Priority != 0 { + attrs["priority"] = strconv.Itoa(compat.Priority) + } if hash := diff.ComputeOpenAICompatModelsHash(compat.Models); hash != "" { attrs["models_hash"] = hash } @@ -276,6 +291,9 @@ func (s *ConfigSynthesizer) synthesizeVertexCompat(ctx *SynthesisContext) []*cor "base_url": base, "provider_key": providerName, } + if compat.Priority != 0 { + attrs["priority"] = strconv.Itoa(compat.Priority) + } if key != "" { attrs["api_key"] = key } @@ -299,96 +317,3 @@ func (s *ConfigSynthesizer) synthesizeVertexCompat(ctx *SynthesisContext) []*cor } return out } - -// synthesizeKiroKeys creates Auth entries for Kiro (AWS CodeWhisperer) tokens. -func (s *ConfigSynthesizer) synthesizeKiroKeys(ctx *SynthesisContext) []*coreauth.Auth { - cfg := ctx.Config - now := ctx.Now - idGen := ctx.IDGenerator - - if len(cfg.KiroKey) == 0 { - return nil - } - - out := make([]*coreauth.Auth, 0, len(cfg.KiroKey)) - kAuth := kiroauth.NewKiroAuth(cfg) - - for i := range cfg.KiroKey { - kk := cfg.KiroKey[i] - var accessToken, profileArn, refreshToken string - - // Try to load from token file first - if kk.TokenFile != "" && kAuth != nil { - tokenData, err := kAuth.LoadTokenFromFile(kk.TokenFile) - if err != nil { - log.Warnf("failed to load kiro token file %s: %v", kk.TokenFile, err) - } else { - accessToken = tokenData.AccessToken - profileArn = tokenData.ProfileArn - refreshToken = tokenData.RefreshToken - } - } - - // Override with direct config values if provided - if kk.AccessToken != "" { - accessToken = kk.AccessToken - } - if kk.ProfileArn != "" { - profileArn = kk.ProfileArn - } - if kk.RefreshToken != "" { - refreshToken = kk.RefreshToken - } - - if accessToken == "" { - log.Warnf("kiro config[%d] missing access_token, skipping", i) - continue - } - - // profileArn is optional for AWS Builder ID users - id, token := idGen.Next("kiro:token", accessToken, profileArn) - attrs := map[string]string{ - "source": fmt.Sprintf("config:kiro[%s]", token), - "access_token": accessToken, - } - if profileArn != "" { - attrs["profile_arn"] = profileArn - } - if kk.Region != "" { - attrs["region"] = kk.Region - } - if kk.AgentTaskType != "" { - attrs["agent_task_type"] = kk.AgentTaskType - } - if kk.PreferredEndpoint != "" { - attrs["preferred_endpoint"] = kk.PreferredEndpoint - } else if cfg.KiroPreferredEndpoint != "" { - // Apply global default if not overridden by specific key - attrs["preferred_endpoint"] = cfg.KiroPreferredEndpoint - } - if refreshToken != "" { - attrs["refresh_token"] = refreshToken - } - proxyURL := strings.TrimSpace(kk.ProxyURL) - a := &coreauth.Auth{ - ID: id, - Provider: "kiro", - Label: "kiro-token", - Status: coreauth.StatusActive, - ProxyURL: proxyURL, - Attributes: attrs, - CreatedAt: now, - UpdatedAt: now, - } - - if refreshToken != "" { - if a.Metadata == nil { - a.Metadata = make(map[string]any) - } - a.Metadata["refresh_token"] = refreshToken - } - - out = append(out, a) - } - return out -} diff --git a/internal/watcher/synthesizer/file.go b/internal/watcher/synthesizer/file.go index ef0eb8c9..c80ebc66 100644 --- a/internal/watcher/synthesizer/file.go +++ b/internal/watcher/synthesizer/file.go @@ -86,12 +86,19 @@ func (s *FileSynthesizer) Synthesize(ctx *SynthesisContext) ([]*coreauth.Auth, e } } + disabled, _ := metadata["disabled"].(bool) + status := coreauth.StatusActive + if disabled { + status = coreauth.StatusDisabled + } + a := &coreauth.Auth{ ID: id, Provider: provider, Label: label, Prefix: prefix, - Status: coreauth.StatusActive, + Status: status, + Disabled: disabled, Attributes: map[string]string{ "source": full, "path": full, diff --git a/internal/wsrelay/http.go b/internal/wsrelay/http.go index 52ea2a1d..abdb277c 100644 --- a/internal/wsrelay/http.go +++ b/internal/wsrelay/http.go @@ -124,32 +124,47 @@ func (m *Manager) Stream(ctx context.Context, provider string, req *HTTPRequest) out := make(chan StreamEvent) go func() { defer close(out) + send := func(ev StreamEvent) bool { + if ctx == nil { + out <- ev + return true + } + select { + case <-ctx.Done(): + return false + case out <- ev: + return true + } + } for { select { case <-ctx.Done(): - out <- StreamEvent{Err: ctx.Err()} return case msg, ok := <-respCh: if !ok { - out <- StreamEvent{Err: errors.New("wsrelay: stream closed")} + _ = send(StreamEvent{Err: errors.New("wsrelay: stream closed")}) return } switch msg.Type { case MessageTypeStreamStart: resp := decodeResponse(msg.Payload) - out <- StreamEvent{Type: MessageTypeStreamStart, Status: resp.Status, Headers: resp.Headers} + if okSend := send(StreamEvent{Type: MessageTypeStreamStart, Status: resp.Status, Headers: resp.Headers}); !okSend { + return + } case MessageTypeStreamChunk: chunk := decodeChunk(msg.Payload) - out <- StreamEvent{Type: MessageTypeStreamChunk, Payload: chunk} + if okSend := send(StreamEvent{Type: MessageTypeStreamChunk, Payload: chunk}); !okSend { + return + } case MessageTypeStreamEnd: - out <- StreamEvent{Type: MessageTypeStreamEnd} + _ = send(StreamEvent{Type: MessageTypeStreamEnd}) return case MessageTypeError: - out <- StreamEvent{Type: MessageTypeError, Err: decodeError(msg.Payload)} + _ = send(StreamEvent{Type: MessageTypeError, Err: decodeError(msg.Payload)}) return case MessageTypeHTTPResp: resp := decodeResponse(msg.Payload) - out <- StreamEvent{Type: MessageTypeHTTPResp, Status: resp.Status, Headers: resp.Headers, Payload: resp.Body} + _ = send(StreamEvent{Type: MessageTypeHTTPResp, Status: resp.Status, Headers: resp.Headers, Payload: resp.Body}) return default: } diff --git a/sdk/api/handlers/claude/code_handlers.go b/sdk/api/handlers/claude/code_handlers.go index 62126f7f..22e10fa5 100644 --- a/sdk/api/handlers/claude/code_handlers.go +++ b/sdk/api/handlers/claude/code_handlers.go @@ -76,10 +76,6 @@ func (h *ClaudeCodeAPIHandler) ClaudeMessages(c *gin.Context) { return } - // Track system prompt in cache and set header - cacheStatus, _ := handlers.ExtractAndTrackSystemPrompt(h.HandlerType(), rawJSON, "anthropic") - handlers.SetPromptCacheHeader(c, cacheStatus) - // Check if the client requested a streaming response. streamResult := gjson.GetBytes(rawJSON, "stream") if !streamResult.Exists() || streamResult.Type == gjson.False { @@ -132,8 +128,23 @@ func (h *ClaudeCodeAPIHandler) ClaudeCountTokens(c *gin.Context) { // Parameters: // - c: The Gin context for the request. func (h *ClaudeCodeAPIHandler) ClaudeModels(c *gin.Context) { + models := h.Models() + firstID := "" + lastID := "" + if len(models) > 0 { + if id, ok := models[0]["id"].(string); ok { + firstID = id + } + if id, ok := models[len(models)-1]["id"].(string); ok { + lastID = id + } + } + c.JSON(http.StatusOK, gin.H{ - "data": h.Models(), + "data": models, + "has_more": false, + "first_id": firstID, + "last_id": lastID, }) } @@ -150,36 +161,40 @@ func (h *ClaudeCodeAPIHandler) handleNonStreamingResponse(c *gin.Context, rawJSO c.Header("Content-Type", "application/json") alt := h.GetAlt(c) cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background()) + stopKeepAlive := h.StartNonStreamingKeepAlive(c, cliCtx) modelName := gjson.GetBytes(rawJSON, "model").String() resp, errMsg := h.ExecuteWithAuthManager(cliCtx, h.HandlerType(), modelName, rawJSON, alt) + stopKeepAlive() if errMsg != nil { h.WriteErrorResponse(c, errMsg) cliCancel(errMsg.Error) return } - handlers.SetCacheHeader(c, resp.CacheHit) - // Decompress gzipped responses - Claude API sometimes returns gzip without Content-Encoding header // This fixes title generation and other non-streaming responses that arrive compressed - respPayload := resp.Payload - if len(respPayload) >= 2 && respPayload[0] == 0x1f && respPayload[1] == 0x8b { - gzReader, err := gzip.NewReader(bytes.NewReader(respPayload)) - if err != nil { - log.Warnf("failed to decompress gzipped Claude response: %v", err) + if len(resp) >= 2 && resp[0] == 0x1f && resp[1] == 0x8b { + gzReader, errGzip := gzip.NewReader(bytes.NewReader(resp)) + if errGzip != nil { + log.Warnf("failed to decompress gzipped Claude response: %v", errGzip) } else { - defer gzReader.Close() - if decompressed, err := io.ReadAll(gzReader); err != nil { - log.Warnf("failed to read decompressed Claude response: %v", err) + defer func() { + if errClose := gzReader.Close(); errClose != nil { + log.Warnf("failed to close Claude gzip reader: %v", errClose) + } + }() + decompressed, errRead := io.ReadAll(gzReader) + if errRead != nil { + log.Warnf("failed to read decompressed Claude response: %v", errRead) } else { - respPayload = decompressed + resp = decompressed } } } - _, _ = c.Writer.Write(respPayload) + _, _ = c.Writer.Write(resp) cliCancel() } @@ -282,7 +297,7 @@ func (h *ClaudeCodeAPIHandler) forwardClaudeStream(c *gin.Context, flusher http. c.Status(status) errorBytes, _ := json.Marshal(h.toClaudeError(errMsg)) - handlers.WriteSSEError(c.Writer, errorBytes, false) + _, _ = fmt.Fprintf(c.Writer, "event: error\ndata: %s\n\n", errorBytes) }, }) } diff --git a/sdk/api/handlers/gemini/gemini-cli_handlers.go b/sdk/api/handlers/gemini/gemini-cli_handlers.go index db7613ad..ea78657d 100644 --- a/sdk/api/handlers/gemini/gemini-cli_handlers.go +++ b/sdk/api/handlers/gemini/gemini-cli_handlers.go @@ -62,12 +62,6 @@ func (h *GeminiCLIAPIHandler) CLIHandler(c *gin.Context) { rawJSON, _ := c.GetRawData() requestRawURI := c.Request.URL.Path - // Track system prompt in cache and set header for content generation endpoints - if requestRawURI == "/v1internal:generateContent" || requestRawURI == "/v1internal:streamGenerateContent" { - cacheStatus, _ := handlers.ExtractAndTrackSystemPrompt(h.HandlerType(), rawJSON, "gemini") - handlers.SetPromptCacheHeader(c, cacheStatus) - } - if requestRawURI == "/v1internal:generateContent" { h.handleInternalGenerateContent(c, rawJSON) } else if requestRawURI == "/v1internal:streamGenerateContent" { @@ -183,8 +177,7 @@ func (h *GeminiCLIAPIHandler) handleInternalGenerateContent(c *gin.Context, rawJ cliCancel(errMsg.Error) return } - handlers.SetCacheHeader(c, resp.CacheHit) - _, _ = c.Writer.Write(resp.Payload) + _, _ = c.Writer.Write(resp) cliCancel() } @@ -227,7 +220,7 @@ func (h *GeminiCLIAPIHandler) forwardCLIStream(c *gin.Context, flusher http.Flus } body := handlers.BuildErrorResponseBody(status, errText) if alt == "" { - handlers.WriteSSEError(c.Writer, body, false) + _, _ = fmt.Fprintf(c.Writer, "event: error\ndata: %s\n\n", string(body)) } else { _, _ = c.Writer.Write(body) } diff --git a/sdk/api/handlers/gemini/gemini_handlers.go b/sdk/api/handlers/gemini/gemini_handlers.go index 3220b1dd..71c485ad 100644 --- a/sdk/api/handlers/gemini/gemini_handlers.go +++ b/sdk/api/handlers/gemini/gemini_handlers.go @@ -56,8 +56,16 @@ func (h *GeminiAPIHandler) GeminiModels(c *gin.Context) { for k, v := range model { normalizedModel[k] = v } - if name, ok := normalizedModel["name"].(string); ok && name != "" && !strings.HasPrefix(name, "models/") { - normalizedModel["name"] = "models/" + name + if name, ok := normalizedModel["name"].(string); ok && name != "" { + if !strings.HasPrefix(name, "models/") { + normalizedModel["name"] = "models/" + name + } + if displayName, _ := normalizedModel["displayName"].(string); displayName == "" { + normalizedModel["displayName"] = name + } + if description, _ := normalizedModel["description"].(string); description == "" { + normalizedModel["description"] = name + } } if _, ok := normalizedModel["supportedGenerationMethods"]; !ok { normalizedModel["supportedGenerationMethods"] = defaultMethods @@ -85,116 +93,35 @@ func (h *GeminiAPIHandler) GeminiGetHandler(c *gin.Context) { return } action := strings.TrimPrefix(request.Action, "/") - switch action { - case "gemini-3-pro-preview": - c.JSON(http.StatusOK, gin.H{ - "name": "models/gemini-3-pro-preview", - "version": "3", - "displayName": "Gemini 3 Pro Preview", - "description": "Gemini 3 Pro Preview", - "inputTokenLimit": 1048576, - "outputTokenLimit": 65536, - "supportedGenerationMethods": []string{ - "generateContent", - "countTokens", - "createCachedContent", - "batchGenerateContent", - }, - "temperature": 1, - "topP": 0.95, - "topK": 64, - "maxTemperature": 2, - "thinking": true, - }, - ) - case "gemini-3-pro-low-preview": - c.JSON(http.StatusOK, gin.H{ - "name": "models/gemini-3-pro-low", - "version": "3", - "displayName": "Gemini 3 Pro Low Preview", - "description": "Gemini 3 Pro Low (Antigravity)", - "inputTokenLimit": 1048576, - "outputTokenLimit": 65536, - "supportedGenerationMethods": []string{ - "generateContent", - "countTokens", - "createCachedContent", - "batchGenerateContent", - }, - "temperature": 1, - "topP": 0.95, - "topK": 64, - "maxTemperature": 2, - "thinking": true, - }, - ) - case "gemini-3-flash-preview": - c.JSON(http.StatusOK, gin.H{ - "name": "models/gemini-3-flash-preview", - "version": "3", - "displayName": "Gemini 3 Flash Preview", - "description": "Gemini 3 Flash Preview", - "inputTokenLimit": 1048576, - "outputTokenLimit": 65536, - "supportedGenerationMethods": []string{ - "generateContent", - "countTokens", - "createCachedContent", - "batchGenerateContent", - }, - "temperature": 1, - "topP": 0.95, - "topK": 64, - "maxTemperature": 2, - "thinking": false, - }, - ) - case "gemini-3-flash": - c.JSON(http.StatusOK, gin.H{ - "name": "models/gemini-3-flash", - "version": "3", - "displayName": "Gemini 3 Flash", - "description": "Gemini 3 Flash", - "inputTokenLimit": 1048576, - "outputTokenLimit": 65536, - "supportedGenerationMethods": []string{ - "generateContent", - "countTokens", - "createCachedContent", - "batchGenerateContent", - }, - "temperature": 1, - "topP": 0.95, - "topK": 64, - "maxTemperature": 2, - "thinking": false, - }, - ) - case "gpt-5.2": - c.JSON(http.StatusOK, gin.H{ - "name": "gpt-5.2", - "version": "5.2", - "displayName": "GPT 5.2", - "description": "Stable version of GPT 5.2", - "inputTokenLimit": 400000, - "outputTokenLimit": 128000, - "supportedGenerationMethods": []string{ - "generateContent", - }, - "temperature": 1, - "topP": 0.95, - "topK": 64, - "maxTemperature": 2, - "thinking": true, - }) - default: - c.JSON(http.StatusNotFound, handlers.ErrorResponse{ - Error: handlers.ErrorDetail{ - Message: "Not Found", - Type: "not_found", - }, - }) + + // Get dynamic models from the global registry and find the matching one + availableModels := h.Models() + var targetModel map[string]any + + for _, model := range availableModels { + name, _ := model["name"].(string) + // Match name with or without 'models/' prefix + if name == action || name == "models/"+action { + targetModel = model + break + } } + + if targetModel != nil { + // Ensure the name has 'models/' prefix in the output if it's a Gemini model + if name, ok := targetModel["name"].(string); ok && name != "" && !strings.HasPrefix(name, "models/") { + targetModel["name"] = "models/" + name + } + c.JSON(http.StatusOK, targetModel) + return + } + + c.JSON(http.StatusNotFound, handlers.ErrorResponse{ + Error: handlers.ErrorDetail{ + Message: "Not Found", + Type: "not_found", + }, + }) } // GeminiHandler handles POST requests for Gemini API operations. @@ -226,10 +153,6 @@ func (h *GeminiAPIHandler) GeminiHandler(c *gin.Context) { method := action[1] rawJSON, _ := c.GetRawData() - // Track system prompt in cache and set header - cacheStatus, _ := handlers.ExtractAndTrackSystemPrompt(h.HandlerType(), rawJSON, "gemini") - handlers.SetPromptCacheHeader(c, cacheStatus) - switch method { case "generateContent": h.handleGenerateContent(c, action[0], rawJSON) @@ -362,14 +285,15 @@ func (h *GeminiAPIHandler) handleGenerateContent(c *gin.Context, modelName strin c.Header("Content-Type", "application/json") alt := h.GetAlt(c) cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background()) + stopKeepAlive := h.StartNonStreamingKeepAlive(c, cliCtx) resp, errMsg := h.ExecuteWithAuthManager(cliCtx, h.HandlerType(), modelName, rawJSON, alt) + stopKeepAlive() if errMsg != nil { h.WriteErrorResponse(c, errMsg) cliCancel(errMsg.Error) return } - handlers.SetCacheHeader(c, resp.CacheHit) - _, _ = c.Writer.Write(resp.Payload) + _, _ = c.Writer.Write(resp) cliCancel() } @@ -384,7 +308,9 @@ func (h *GeminiAPIHandler) forwardGeminiStream(c *gin.Context, flusher http.Flus KeepAliveInterval: keepAliveInterval, WriteChunk: func(chunk []byte) { if alt == "" { - handlers.WriteSSEData(c.Writer, chunk) + _, _ = c.Writer.Write([]byte("data: ")) + _, _ = c.Writer.Write(chunk) + _, _ = c.Writer.Write([]byte("\n\n")) } else { _, _ = c.Writer.Write(chunk) } @@ -403,7 +329,7 @@ func (h *GeminiAPIHandler) forwardGeminiStream(c *gin.Context, flusher http.Flus } body := handlers.BuildErrorResponseBody(status, errText) if alt == "" { - handlers.WriteSSEError(c.Writer, body, false) + _, _ = fmt.Fprintf(c.Writer, "event: error\ndata: %s\n\n", string(body)) } else { _, _ = c.Writer.Write(body) } diff --git a/sdk/api/handlers/handlers.go b/sdk/api/handlers/handlers.go index 3256a509..b1da9664 100644 --- a/sdk/api/handlers/handlers.go +++ b/sdk/api/handlers/handlers.go @@ -14,9 +14,9 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" - "github.com/router-for-me/CLIProxyAPI/v6/internal/cache" "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" "github.com/router-for-me/CLIProxyAPI/v6/internal/logging" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" coreexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" @@ -50,7 +50,6 @@ const idempotencyKeyMetadataKey = "idempotency_key" const ( defaultStreamingKeepAliveSeconds = 0 defaultStreamingBootstrapRetries = 0 - defaultStreamingMaxChunkSize = 65536 // 64KB like vibeproxy ) // BuildErrorResponseBody builds an OpenAI-compatible JSON error response body. @@ -116,6 +115,19 @@ func StreamingKeepAliveInterval(cfg *config.SDKConfig) time.Duration { return time.Duration(seconds) * time.Second } +// NonStreamingKeepAliveInterval returns the keep-alive interval for non-streaming responses. +// Returning 0 disables keep-alives (default when unset). +func NonStreamingKeepAliveInterval(cfg *config.SDKConfig) time.Duration { + seconds := 0 + if cfg != nil { + seconds = cfg.NonStreamKeepAliveInterval + } + if seconds <= 0 { + return 0 + } + return time.Duration(seconds) * time.Second +} + // StreamingBootstrapRetries returns how many times a streaming request may be retried before any bytes are sent. func StreamingBootstrapRetries(cfg *config.SDKConfig) int { retries := defaultStreamingBootstrapRetries @@ -128,36 +140,6 @@ func StreamingBootstrapRetries(cfg *config.SDKConfig) int { return retries } -// ExecuteResult contains the response payload and metadata from a request execution. -type ExecuteResult struct { - // Payload is the raw response body. - Payload []byte - // CacheHit indicates whether the response was served from cache. - CacheHit bool -} - -// SetCacheHeader sets the X-Cache header on the response based on cache status. -func SetCacheHeader(c *gin.Context, cacheHit bool) { - if cacheHit { - c.Header("X-Cache", "HIT") - } else { - c.Header("X-Cache", "MISS") - } -} - -// StreamingMaxChunkSize returns the maximum size (in bytes) for individual response chunks. -// Returns 0 when chunk size limiting is disabled. Default is 64KB. -func StreamingMaxChunkSize(cfg *config.SDKConfig) int { - chunkSize := defaultStreamingMaxChunkSize - if cfg != nil && cfg.Streaming.MaxChunkSize != nil { - chunkSize = *cfg.Streaming.MaxChunkSize - } - if chunkSize < 0 { - chunkSize = 0 - } - return chunkSize -} - func requestExecutionMetadata(ctx context.Context) map[string]any { // Idempotency-Key is an optional client-supplied header used to correlate retries. // It is forwarded as execution metadata; when absent we generate a UUID. @@ -196,9 +178,6 @@ type BaseAPIHandler struct { // Cfg holds the current application configuration. Cfg *config.SDKConfig - - detailsCacheMu sync.RWMutex - detailsCache map[string]cachedRequestDetails } // NewBaseAPIHandlers creates a new API handlers instance. @@ -211,12 +190,10 @@ type BaseAPIHandler struct { // Returns: // - *BaseAPIHandler: A new API handlers instance func NewBaseAPIHandlers(cfg *config.SDKConfig, authManager *coreauth.Manager) *BaseAPIHandler { - h := &BaseAPIHandler{ - Cfg: cfg, - AuthManager: authManager, - detailsCache: make(map[string]cachedRequestDetails), + return &BaseAPIHandler{ + Cfg: cfg, + AuthManager: authManager, } - return h } // UpdateClients updates the handlers' client list and configuration. @@ -225,12 +202,7 @@ func NewBaseAPIHandlers(cfg *config.SDKConfig, authManager *coreauth.Manager) *B // Parameters: // - clients: The new slice of AI service clients // - cfg: The new application configuration -func (h *BaseAPIHandler) UpdateClients(cfg *config.SDKConfig) { - h.Cfg = cfg - h.detailsCacheMu.Lock() - h.detailsCache = make(map[string]cachedRequestDetails) - h.detailsCacheMu.Unlock() -} +func (h *BaseAPIHandler) UpdateClients(cfg *config.SDKConfig) { h.Cfg = cfg } // GetAlt extracts the 'alt' parameter from the request query string. // It checks both 'alt' and '$alt' parameters and returns the appropriate value. @@ -336,6 +308,53 @@ func (h *BaseAPIHandler) GetContextWithCancel(handler interfaces.APIHandler, c * } } +// StartNonStreamingKeepAlive emits blank lines every 5 seconds while waiting for a non-streaming response. +// It returns a stop function that must be called before writing the final response. +func (h *BaseAPIHandler) StartNonStreamingKeepAlive(c *gin.Context, ctx context.Context) func() { + if h == nil || c == nil { + return func() {} + } + interval := NonStreamingKeepAliveInterval(h.Cfg) + if interval <= 0 { + return func() {} + } + flusher, ok := c.Writer.(http.Flusher) + if !ok { + return func() {} + } + if ctx == nil { + ctx = context.Background() + } + + stopChan := make(chan struct{}) + var stopOnce sync.Once + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + ticker := time.NewTicker(interval) + defer ticker.Stop() + for { + select { + case <-stopChan: + return + case <-ctx.Done(): + return + case <-ticker.C: + _, _ = c.Writer.Write([]byte("\n")) + flusher.Flush() + } + } + }() + + return func() { + stopOnce.Do(func() { + close(stopChan) + }) + wg.Wait() + } +} + // appendAPIResponse preserves any previously captured API response and appends new data. func appendAPIResponse(c *gin.Context, data []byte) { if c == nil || len(data) == 0 { @@ -360,40 +379,24 @@ func appendAPIResponse(c *gin.Context, data []byte) { // ExecuteWithAuthManager executes a non-streaming request via the core auth manager. // This path is the only supported execution route. -func (h *BaseAPIHandler) ExecuteWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) (ExecuteResult, *interfaces.ErrorMessage) { - providers, normalizedModel, metadata, errMsg := h.getRequestDetails(modelName) +func (h *BaseAPIHandler) ExecuteWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage) { + providers, normalizedModel, errMsg := h.getRequestDetails(modelName) if errMsg != nil { - return ExecuteResult{}, errMsg + return nil, errMsg } - - // Set model and provider in context for logging/monitoring - if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil { - ginCtx.Set("model", normalizedModel) - if len(providers) > 0 { - ginCtx.Set("provider", providers[0]) - } - } - - // Check response cache before executing - if cachedResp, _, found := cache.TryGetCached(normalizedModel, rawJSON); found { - return ExecuteResult{Payload: cachedResp, CacheHit: true}, nil - } - reqMeta := requestExecutionMetadata(ctx) + reqMeta[coreexecutor.RequestedModelMetadataKey] = normalizedModel req := coreexecutor.Request{ Model: normalizedModel, Payload: cloneBytes(rawJSON), } - if cloned := cloneMetadata(metadata); cloned != nil { - req.Metadata = cloned - } opts := coreexecutor.Options{ Stream: false, Alt: alt, OriginalRequest: cloneBytes(rawJSON), SourceFormat: sdktranslator.FromString(handlerType), } - opts.Metadata = mergeMetadata(cloneMetadata(metadata), reqMeta) + opts.Metadata = reqMeta resp, err := h.AuthManager.Execute(ctx, providers, req, opts) if err != nil { status := http.StatusInternalServerError @@ -408,46 +411,31 @@ func (h *BaseAPIHandler) ExecuteWithAuthManager(ctx context.Context, handlerType addon = hdr.Clone() } } - return ExecuteResult{}, &interfaces.ErrorMessage{StatusCode: status, Error: err, Addon: addon} + return nil, &interfaces.ErrorMessage{StatusCode: status, Error: err, Addon: addon} } - - // Store successful response in cache - cache.StoreCached(normalizedModel, rawJSON, resp.Payload, "application/json", http.StatusOK) - - return ExecuteResult{Payload: cloneBytes(resp.Payload), CacheHit: false}, nil + return cloneBytes(resp.Payload), nil } // ExecuteCountWithAuthManager executes a non-streaming request via the core auth manager. // This path is the only supported execution route. func (h *BaseAPIHandler) ExecuteCountWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage) { - providers, normalizedModel, metadata, errMsg := h.getRequestDetails(modelName) + providers, normalizedModel, errMsg := h.getRequestDetails(modelName) if errMsg != nil { return nil, errMsg } - - // Set model and provider in context for logging/monitoring - if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil { - ginCtx.Set("model", normalizedModel) - if len(providers) > 0 { - ginCtx.Set("provider", providers[0]) - } - } - reqMeta := requestExecutionMetadata(ctx) + reqMeta[coreexecutor.RequestedModelMetadataKey] = normalizedModel req := coreexecutor.Request{ Model: normalizedModel, Payload: cloneBytes(rawJSON), } - if cloned := cloneMetadata(metadata); cloned != nil { - req.Metadata = cloned - } opts := coreexecutor.Options{ Stream: false, Alt: alt, OriginalRequest: cloneBytes(rawJSON), SourceFormat: sdktranslator.FromString(handlerType), } - opts.Metadata = mergeMetadata(cloneMetadata(metadata), reqMeta) + opts.Metadata = reqMeta resp, err := h.AuthManager.ExecuteCount(ctx, providers, req, opts) if err != nil { status := http.StatusInternalServerError @@ -470,37 +458,26 @@ func (h *BaseAPIHandler) ExecuteCountWithAuthManager(ctx context.Context, handle // ExecuteStreamWithAuthManager executes a streaming request via the core auth manager. // This path is the only supported execution route. func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage) { - providers, normalizedModel, metadata, errMsg := h.getRequestDetails(modelName) + providers, normalizedModel, errMsg := h.getRequestDetails(modelName) if errMsg != nil { errChan := make(chan *interfaces.ErrorMessage, 1) errChan <- errMsg close(errChan) return nil, errChan } - - // Set model and provider in context for logging/monitoring - if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil { - ginCtx.Set("model", normalizedModel) - if len(providers) > 0 { - ginCtx.Set("provider", providers[0]) - } - } - reqMeta := requestExecutionMetadata(ctx) + reqMeta[coreexecutor.RequestedModelMetadataKey] = normalizedModel req := coreexecutor.Request{ Model: normalizedModel, Payload: cloneBytes(rawJSON), } - if cloned := cloneMetadata(metadata); cloned != nil { - req.Metadata = cloned - } opts := coreexecutor.Options{ Stream: true, Alt: alt, OriginalRequest: cloneBytes(rawJSON), SourceFormat: sdktranslator.FromString(handlerType), } - opts.Metadata = mergeMetadata(cloneMetadata(metadata), reqMeta) + opts.Metadata = reqMeta chunks, err := h.AuthManager.ExecuteStream(ctx, providers, req, opts) if err != nil { errChan := make(chan *interfaces.ErrorMessage, 1) @@ -529,6 +506,32 @@ func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handl bootstrapRetries := 0 maxBootstrapRetries := StreamingBootstrapRetries(h.Cfg) + sendErr := func(msg *interfaces.ErrorMessage) bool { + if ctx == nil { + errChan <- msg + return true + } + select { + case <-ctx.Done(): + return false + case errChan <- msg: + return true + } + } + + sendData := func(chunk []byte) bool { + if ctx == nil { + dataChan <- chunk + return true + } + select { + case <-ctx.Done(): + return false + case dataChan <- chunk: + return true + } + } + bootstrapEligible := func(err error) bool { status := statusFromError(err) if status == 0 { @@ -588,12 +591,14 @@ func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handl addon = hdr.Clone() } } - errChan <- &interfaces.ErrorMessage{StatusCode: status, Error: streamErr, Addon: addon} + _ = sendErr(&interfaces.ErrorMessage{StatusCode: status, Error: streamErr, Addon: addon}) return } if len(chunk.Payload) > 0 { sentPayload = true - dataChan <- cloneBytes(chunk.Payload) + if okSendData := sendData(cloneBytes(chunk.Payload)); !okSendData { + return + } } } } @@ -613,110 +618,40 @@ func statusFromError(err error) int { return 0 } -func (h *BaseAPIHandler) getRequestDetails(modelName string) (providers []string, normalizedModel string, metadata map[string]any, err *interfaces.ErrorMessage) { - if modelName != "" { - if cached, ok := h.loadRequestDetailsCache(modelName); ok { - return cached.providers, cached.normalizedModel, cached.metadata, cached.err +func (h *BaseAPIHandler) getRequestDetails(modelName string) (providers []string, normalizedModel string, err *interfaces.ErrorMessage) { + resolvedModelName := modelName + initialSuffix := thinking.ParseSuffix(modelName) + if initialSuffix.ModelName == "auto" { + resolvedBase := util.ResolveAutoModel(initialSuffix.ModelName) + if initialSuffix.HasSuffix { + resolvedModelName = fmt.Sprintf("%s(%s)", resolvedBase, initialSuffix.RawSuffix) + } else { + resolvedModelName = resolvedBase } + } else { + resolvedModelName = util.ResolveAutoModel(modelName) } - // Resolve "auto" model to an actual available model first - resolvedModelName := util.ResolveAutoModel(modelName) + parsed := thinking.ParseSuffix(resolvedModelName) + baseModel := strings.TrimSpace(parsed.ModelName) - // Check for global model mapping (provider-agnostic first pass) - // This allows mappings like "gpt-4o" -> "claude-sonnet-4-20250514" to work - if h.Cfg != nil { - if mappedModel := h.Cfg.LookupGlobalModelMapping(resolvedModelName, ""); mappedModel != "" { - resolvedModelName = mappedModel - } - } - - // Normalize the model name to handle dynamic thinking suffixes before determining the provider. - normalizedModel, metadata = normalizeModelMetadata(resolvedModelName) - - // Use the normalizedModel to get the provider name. - providers = util.GetProviderName(normalizedModel) - if len(providers) == 0 && metadata != nil { - if originalRaw, ok := metadata[util.ThinkingOriginalModelMetadataKey]; ok { - if originalModel, okStr := originalRaw.(string); okStr { - originalModel = strings.TrimSpace(originalModel) - if originalModel != "" && !strings.EqualFold(originalModel, normalizedModel) { - if altProviders := util.GetProviderName(originalModel); len(altProviders) > 0 { - providers = altProviders - normalizedModel = originalModel - } - } - } - } + providers = util.GetProviderName(baseModel) + // Fallback: if baseModel has no provider but differs from resolvedModelName, + // try using the full model name. This handles edge cases where custom models + // may be registered with their full suffixed name (e.g., "my-model(8192)"). + // Evaluated in Story 11.8: This fallback is intentionally preserved to support + // custom model registrations that include thinking suffixes. + if len(providers) == 0 && baseModel != resolvedModelName { + providers = util.GetProviderName(resolvedModelName) } if len(providers) == 0 { - errMsg := &interfaces.ErrorMessage{StatusCode: http.StatusBadRequest, Error: fmt.Errorf("unknown provider for model %s", modelName)} - h.storeRequestDetailsCache(modelName, nil, "", nil, errMsg) - return nil, "", nil, errMsg + return nil, "", &interfaces.ErrorMessage{StatusCode: http.StatusBadRequest, Error: fmt.Errorf("unknown provider for model %s", modelName)} } - // If it's a dynamic model, the normalizedModel was already set to extractedModelName. - // If it's a non-dynamic model, normalizedModel was set by normalizeModelMetadata. - // So, normalizedModel is already correctly set at this point. - - h.storeRequestDetailsCache(modelName, providers, normalizedModel, metadata, nil) - return providers, normalizedModel, metadata, nil -} - -type cachedRequestDetails struct { - providers []string - normalizedModel string - metadata map[string]any - err *interfaces.ErrorMessage -} - -func (h *BaseAPIHandler) loadRequestDetailsCache(modelName string) (cachedRequestDetails, bool) { - h.detailsCacheMu.RLock() - defer h.detailsCacheMu.RUnlock() - if h.detailsCache == nil { - return cachedRequestDetails{}, false - } - cached, ok := h.detailsCache[modelName] - if !ok { - return cachedRequestDetails{}, false - } - return cached.clone(), true -} - -func (h *BaseAPIHandler) storeRequestDetailsCache(modelName string, providers []string, normalizedModel string, metadata map[string]any, errMsg *interfaces.ErrorMessage) { - if modelName == "" { - return - } - h.detailsCacheMu.Lock() - if h.detailsCache == nil { - h.detailsCache = make(map[string]cachedRequestDetails) - } - h.detailsCache[modelName] = cachedRequestDetails{ - providers: cloneProviders(providers), - normalizedModel: normalizedModel, - metadata: cloneMetadata(metadata), - err: errMsg, - } - h.detailsCacheMu.Unlock() -} - -func (c cachedRequestDetails) clone() cachedRequestDetails { - return cachedRequestDetails{ - providers: cloneProviders(c.providers), - normalizedModel: c.normalizedModel, - metadata: cloneMetadata(c.metadata), - err: c.err, - } -} - -func cloneProviders(src []string) []string { - if len(src) == 0 { - return nil - } - out := make([]string, len(src)) - copy(out, src) - return out + // The thinking suffix is preserved in the model name itself, so no + // metadata-based configuration passing is needed. + return providers, resolvedModelName, nil } func cloneBytes(src []byte) []byte { @@ -728,10 +663,6 @@ func cloneBytes(src []byte) []byte { return dst } -func normalizeModelMetadata(modelName string) (string, map[string]any) { - return util.NormalizeThinkingModel(modelName) -} - func cloneMetadata(src map[string]any) map[string]any { if len(src) == 0 { return nil diff --git a/sdk/api/handlers/handlers_request_details_test.go b/sdk/api/handlers/handlers_request_details_test.go new file mode 100644 index 00000000..b0f6b132 --- /dev/null +++ b/sdk/api/handlers/handlers_request_details_test.go @@ -0,0 +1,118 @@ +package handlers + +import ( + "reflect" + "testing" + "time" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/registry" + coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" + sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" +) + +func TestGetRequestDetails_PreservesSuffix(t *testing.T) { + modelRegistry := registry.GetGlobalRegistry() + now := time.Now().Unix() + + modelRegistry.RegisterClient("test-request-details-gemini", "gemini", []*registry.ModelInfo{ + {ID: "gemini-2.5-pro", Created: now + 30}, + {ID: "gemini-2.5-flash", Created: now + 25}, + }) + modelRegistry.RegisterClient("test-request-details-openai", "openai", []*registry.ModelInfo{ + {ID: "gpt-5.2", Created: now + 20}, + }) + modelRegistry.RegisterClient("test-request-details-claude", "claude", []*registry.ModelInfo{ + {ID: "claude-sonnet-4-5", Created: now + 5}, + }) + + // Ensure cleanup of all test registrations. + clientIDs := []string{ + "test-request-details-gemini", + "test-request-details-openai", + "test-request-details-claude", + } + for _, clientID := range clientIDs { + id := clientID + t.Cleanup(func() { + modelRegistry.UnregisterClient(id) + }) + } + + handler := NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, coreauth.NewManager(nil, nil, nil)) + + tests := []struct { + name string + inputModel string + wantProviders []string + wantModel string + wantErr bool + }{ + { + name: "numeric suffix preserved", + inputModel: "gemini-2.5-pro(8192)", + wantProviders: []string{"gemini"}, + wantModel: "gemini-2.5-pro(8192)", + wantErr: false, + }, + { + name: "level suffix preserved", + inputModel: "gpt-5.2(high)", + wantProviders: []string{"openai"}, + wantModel: "gpt-5.2(high)", + wantErr: false, + }, + { + name: "no suffix unchanged", + inputModel: "claude-sonnet-4-5", + wantProviders: []string{"claude"}, + wantModel: "claude-sonnet-4-5", + wantErr: false, + }, + { + name: "unknown model with suffix", + inputModel: "unknown-model(8192)", + wantProviders: nil, + wantModel: "", + wantErr: true, + }, + { + name: "auto suffix resolved", + inputModel: "auto(high)", + wantProviders: []string{"gemini"}, + wantModel: "gemini-2.5-pro(high)", + wantErr: false, + }, + { + name: "special suffix none preserved", + inputModel: "gemini-2.5-flash(none)", + wantProviders: []string{"gemini"}, + wantModel: "gemini-2.5-flash(none)", + wantErr: false, + }, + { + name: "special suffix auto preserved", + inputModel: "claude-sonnet-4-5(auto)", + wantProviders: []string{"claude"}, + wantModel: "claude-sonnet-4-5(auto)", + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + providers, model, errMsg := handler.getRequestDetails(tt.inputModel) + if (errMsg != nil) != tt.wantErr { + t.Fatalf("getRequestDetails() error = %v, wantErr %v", errMsg, tt.wantErr) + } + if errMsg != nil { + return + } + if !reflect.DeepEqual(providers, tt.wantProviders) { + t.Fatalf("getRequestDetails() providers = %v, want %v", providers, tt.wantProviders) + } + if model != tt.wantModel { + t.Fatalf("getRequestDetails() model = %v, want %v", model, tt.wantModel) + } + }) + } +} diff --git a/sdk/api/handlers/handlers_stream_bootstrap_test.go b/sdk/api/handlers/handlers_stream_bootstrap_test.go index 1b3fc427..7814ff1b 100644 --- a/sdk/api/handlers/handlers_stream_bootstrap_test.go +++ b/sdk/api/handlers/handlers_stream_bootstrap_test.go @@ -56,12 +56,12 @@ func (e *failOnceStreamExecutor) CountTokens(context.Context, *coreauth.Auth, co return coreexecutor.Response{}, &coreauth.Error{Code: "not_implemented", Message: "CountTokens not implemented"} } -func (e *failOnceStreamExecutor) Embed(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { - return coreexecutor.Response{}, coreexecutor.ErrNotImplemented -} - -func (e *failOnceStreamExecutor) HttpRequest(_ context.Context, _ *coreauth.Auth, _ *http.Request) (*http.Response, error) { - return nil, coreexecutor.ErrNotImplemented +func (e *failOnceStreamExecutor) HttpRequest(ctx context.Context, auth *coreauth.Auth, req *http.Request) (*http.Response, error) { + return nil, &coreauth.Error{ + Code: "not_implemented", + Message: "HttpRequest not implemented", + HTTPStatus: http.StatusNotImplemented, + } } func (e *failOnceStreamExecutor) Calls() int { @@ -70,6 +70,58 @@ func (e *failOnceStreamExecutor) Calls() int { return e.calls } +type payloadThenErrorStreamExecutor struct { + mu sync.Mutex + calls int +} + +func (e *payloadThenErrorStreamExecutor) Identifier() string { return "codex" } + +func (e *payloadThenErrorStreamExecutor) Execute(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { + return coreexecutor.Response{}, &coreauth.Error{Code: "not_implemented", Message: "Execute not implemented"} +} + +func (e *payloadThenErrorStreamExecutor) ExecuteStream(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (<-chan coreexecutor.StreamChunk, error) { + e.mu.Lock() + e.calls++ + e.mu.Unlock() + + ch := make(chan coreexecutor.StreamChunk, 2) + ch <- coreexecutor.StreamChunk{Payload: []byte("partial")} + ch <- coreexecutor.StreamChunk{ + Err: &coreauth.Error{ + Code: "upstream_closed", + Message: "upstream closed", + Retryable: false, + HTTPStatus: http.StatusBadGateway, + }, + } + close(ch) + return ch, nil +} + +func (e *payloadThenErrorStreamExecutor) Refresh(ctx context.Context, auth *coreauth.Auth) (*coreauth.Auth, error) { + return auth, nil +} + +func (e *payloadThenErrorStreamExecutor) CountTokens(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { + return coreexecutor.Response{}, &coreauth.Error{Code: "not_implemented", Message: "CountTokens not implemented"} +} + +func (e *payloadThenErrorStreamExecutor) HttpRequest(ctx context.Context, auth *coreauth.Auth, req *http.Request) (*http.Response, error) { + return nil, &coreauth.Error{ + Code: "not_implemented", + Message: "HttpRequest not implemented", + HTTPStatus: http.StatusNotImplemented, + } +} + +func (e *payloadThenErrorStreamExecutor) Calls() int { + e.mu.Lock() + defer e.mu.Unlock() + return e.calls +} + func TestExecuteStreamWithAuthManager_RetriesBeforeFirstByte(t *testing.T) { executor := &failOnceStreamExecutor{} manager := coreauth.NewManager(nil, nil, nil) @@ -131,56 +183,6 @@ func TestExecuteStreamWithAuthManager_RetriesBeforeFirstByte(t *testing.T) { } } -type payloadThenErrorStreamExecutor struct { - mu sync.Mutex - calls int -} - -func (e *payloadThenErrorStreamExecutor) Identifier() string { return "codex" } - -func (e *payloadThenErrorStreamExecutor) Execute(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { - return coreexecutor.Response{}, &coreauth.Error{Code: "not_implemented", Message: "Execute not implemented"} -} - -func (e *payloadThenErrorStreamExecutor) ExecuteStream(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (<-chan coreexecutor.StreamChunk, error) { - e.mu.Lock() - e.calls++ - e.mu.Unlock() - - ch := make(chan coreexecutor.StreamChunk, 2) - ch <- coreexecutor.StreamChunk{Payload: []byte("partial")} - ch <- coreexecutor.StreamChunk{ - Err: &coreauth.Error{ - Code: "server_error", - Message: "error after partial", - }, - } - close(ch) - return ch, nil -} - -func (e *payloadThenErrorStreamExecutor) Refresh(ctx context.Context, auth *coreauth.Auth) (*coreauth.Auth, error) { - return auth, nil -} - -func (e *payloadThenErrorStreamExecutor) CountTokens(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { - return coreexecutor.Response{}, &coreauth.Error{Code: "not_implemented", Message: "CountTokens not implemented"} -} - -func (e *payloadThenErrorStreamExecutor) Embed(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { - return coreexecutor.Response{}, coreexecutor.ErrNotImplemented -} - -func (e *payloadThenErrorStreamExecutor) HttpRequest(_ context.Context, _ *coreauth.Auth, _ *http.Request) (*http.Response, error) { - return nil, coreexecutor.ErrNotImplemented -} - -func (e *payloadThenErrorStreamExecutor) Calls() int { - e.mu.Lock() - defer e.mu.Unlock() - return e.calls -} - func TestExecuteStreamWithAuthManager_DoesNotRetryAfterFirstByte(t *testing.T) { executor := &payloadThenErrorStreamExecutor{} manager := coreauth.NewManager(nil, nil, nil) @@ -228,17 +230,25 @@ func TestExecuteStreamWithAuthManager_DoesNotRetryAfterFirstByte(t *testing.T) { got = append(got, chunk...) } + var gotErr error + var gotStatus int for msg := range errChan { - if msg != nil { - // We expect an error after the partial payload, that's fine - _ = msg + if msg != nil && msg.Error != nil { + gotErr = msg.Error + gotStatus = msg.StatusCode } } if string(got) != "partial" { - t.Fatalf("expected payload 'partial', got %q", string(got)) + t.Fatalf("expected payload partial, got %q", string(got)) + } + if gotErr == nil { + t.Fatalf("expected terminal error, got nil") + } + if gotStatus != http.StatusBadGateway { + t.Fatalf("expected status %d, got %d", http.StatusBadGateway, gotStatus) } if executor.Calls() != 1 { - t.Fatalf("expected 1 stream attempt (no retries after first byte), got %d", executor.Calls()) + t.Fatalf("expected 1 stream attempt, got %d", executor.Calls()) } } diff --git a/sdk/api/handlers/openai/openai_embeddings_handlers.go b/sdk/api/handlers/openai/openai_embeddings_handlers.go index 868a9f5f..7acce821 100644 --- a/sdk/api/handlers/openai/openai_embeddings_handlers.go +++ b/sdk/api/handlers/openai/openai_embeddings_handlers.go @@ -58,10 +58,9 @@ func (h *OpenAIAPIHandler) Embeddings(c *gin.Context) { defer cliCancel() resp, errMsg := h.ExecuteWithAuthManager(cliCtx, h.HandlerType(), model, rawJSON, alt) - if errMsg == nil && len(resp.Payload) > 0 { + if errMsg == nil && len(resp) > 0 { c.Header("Content-Type", "application/json") - handlers.SetCacheHeader(c, resp.CacheHit) - _, _ = c.Writer.Write(resp.Payload) + _, _ = c.Writer.Write(resp) return } diff --git a/sdk/api/handlers/openai/openai_handlers.go b/sdk/api/handlers/openai/openai_handlers.go index 387da192..09471ce1 100644 --- a/sdk/api/handlers/openai/openai_handlers.go +++ b/sdk/api/handlers/openai/openai_handlers.go @@ -108,10 +108,6 @@ func (h *OpenAIAPIHandler) ChatCompletions(c *gin.Context) { return } - // Track system prompt in cache and set header - cacheStatus, _ := handlers.ExtractAndTrackSystemPrompt(h.HandlerType(), rawJSON, "openai") - handlers.SetPromptCacheHeader(c, cacheStatus) - // Check if the client requested a streaming response. streamResult := gjson.GetBytes(rawJSON, "stream") stream := streamResult.Type == gjson.True @@ -435,8 +431,7 @@ func (h *OpenAIAPIHandler) handleNonStreamingResponse(c *gin.Context, rawJSON [] cliCancel(errMsg.Error) return } - handlers.SetCacheHeader(c, resp.CacheHit) - _, _ = c.Writer.Write(resp.Payload) + _, _ = c.Writer.Write(resp) cliCancel() } @@ -495,7 +490,7 @@ func (h *OpenAIAPIHandler) handleStreamingResponse(c *gin.Context, rawJSON []byt if !ok { // Stream closed without data? Send DONE or just headers. setSSEHeaders() - handlers.WriteSSEDone(c.Writer) + _, _ = fmt.Fprintf(c.Writer, "data: [DONE]\n\n") flusher.Flush() cliCancel(nil) return @@ -504,7 +499,7 @@ func (h *OpenAIAPIHandler) handleStreamingResponse(c *gin.Context, rawJSON []byt // Success! Commit to streaming headers. setSSEHeaders() - handlers.WriteSSEData(c.Writer, chunk) + _, _ = fmt.Fprintf(c.Writer, "data: %s\n\n", string(chunk)) flusher.Flush() // Continue streaming the rest @@ -529,14 +524,15 @@ func (h *OpenAIAPIHandler) handleCompletionsNonStreamingResponse(c *gin.Context, modelName := gjson.GetBytes(chatCompletionsJSON, "model").String() cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background()) + stopKeepAlive := h.StartNonStreamingKeepAlive(c, cliCtx) resp, errMsg := h.ExecuteWithAuthManager(cliCtx, h.HandlerType(), modelName, chatCompletionsJSON, "") + stopKeepAlive() if errMsg != nil { h.WriteErrorResponse(c, errMsg) cliCancel(errMsg.Error) return } - handlers.SetCacheHeader(c, resp.CacheHit) - completionsResp := convertChatCompletionsResponseToCompletions(resp.Payload) + completionsResp := convertChatCompletionsResponseToCompletions(resp) _, _ = c.Writer.Write(completionsResp) cliCancel() } @@ -597,7 +593,7 @@ func (h *OpenAIAPIHandler) handleCompletionsStreamingResponse(c *gin.Context, ra case chunk, ok := <-dataChan: if !ok { setSSEHeaders() - handlers.WriteSSEDone(c.Writer) + _, _ = fmt.Fprintf(c.Writer, "data: [DONE]\n\n") flusher.Flush() cliCancel(nil) return @@ -609,7 +605,7 @@ func (h *OpenAIAPIHandler) handleCompletionsStreamingResponse(c *gin.Context, ra // Write the first chunk converted := convertChatCompletionsStreamChunkToCompletions(chunk) if converted != nil { - handlers.WriteSSEData(c.Writer, converted) + _, _ = fmt.Fprintf(c.Writer, "data: %s\n\n", string(converted)) flusher.Flush() } @@ -652,7 +648,7 @@ func (h *OpenAIAPIHandler) handleCompletionsStreamingResponse(c *gin.Context, ra func (h *OpenAIAPIHandler) handleStreamResult(c *gin.Context, flusher http.Flusher, cancel func(error), data <-chan []byte, errs <-chan *interfaces.ErrorMessage) { h.ForwardStream(c, flusher, cancel, data, errs, handlers.StreamForwardOptions{ WriteChunk: func(chunk []byte) { - handlers.WriteSSEData(c.Writer, chunk) + _, _ = fmt.Fprintf(c.Writer, "data: %s\n\n", string(chunk)) }, WriteTerminalError: func(errMsg *interfaces.ErrorMessage) { if errMsg == nil { @@ -667,10 +663,10 @@ func (h *OpenAIAPIHandler) handleStreamResult(c *gin.Context, flusher http.Flush errText = errMsg.Error.Error() } body := handlers.BuildErrorResponseBody(status, errText) - handlers.WriteSSEData(c.Writer, body) + _, _ = fmt.Fprintf(c.Writer, "data: %s\n\n", string(body)) }, WriteDone: func() { - handlers.WriteSSEDone(c.Writer) + _, _ = fmt.Fprint(c.Writer, "data: [DONE]\n\n") }, }) } diff --git a/sdk/api/handlers/openai/openai_responses_handlers.go b/sdk/api/handlers/openai/openai_responses_handlers.go index 01316aa7..31099f81 100644 --- a/sdk/api/handlers/openai/openai_responses_handlers.go +++ b/sdk/api/handlers/openai/openai_responses_handlers.go @@ -81,10 +81,6 @@ func (h *OpenAIResponsesAPIHandler) Responses(c *gin.Context) { return } - // Track system prompt in cache and set header - cacheStatus, _ := handlers.ExtractAndTrackSystemPrompt(h.HandlerType(), rawJSON, "openai") - handlers.SetPromptCacheHeader(c, cacheStatus) - // Check if the client requested a streaming response. streamResult := gjson.GetBytes(rawJSON, "stream") if streamResult.Type == gjson.True { @@ -107,21 +103,17 @@ func (h *OpenAIResponsesAPIHandler) handleNonStreamingResponse(c *gin.Context, r modelName := gjson.GetBytes(rawJSON, "model").String() cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background()) - defer func() { - cliCancel() - }() + stopKeepAlive := h.StartNonStreamingKeepAlive(c, cliCtx) resp, errMsg := h.ExecuteWithAuthManager(cliCtx, h.HandlerType(), modelName, rawJSON, "") + stopKeepAlive() if errMsg != nil { h.WriteErrorResponse(c, errMsg) + cliCancel(errMsg.Error) return } - handlers.SetCacheHeader(c, resp.CacheHit) - _, _ = c.Writer.Write(resp.Payload) - return - - // no legacy fallback - + _, _ = c.Writer.Write(resp) + cliCancel() } // handleStreamingResponse handles streaming responses for Gemini models. @@ -226,7 +218,7 @@ func (h *OpenAIResponsesAPIHandler) forwardResponsesStream(c *gin.Context, flush errText = errMsg.Error.Error() } body := handlers.BuildErrorResponseBody(status, errText) - handlers.WriteSSEError(c.Writer, body, true) + _, _ = fmt.Fprintf(c.Writer, "\nevent: error\ndata: %s\n\n", string(body)) }, WriteDone: func() { _, _ = c.Writer.Write([]byte("\n")) diff --git a/sdk/api/handlers/stream_forwarder.go b/sdk/api/handlers/stream_forwarder.go index a8e87e55..401baca8 100644 --- a/sdk/api/handlers/stream_forwarder.go +++ b/sdk/api/handlers/stream_forwarder.go @@ -13,10 +13,6 @@ type StreamForwardOptions struct { // If nil, the configured default is used. If set to <= 0, keep-alives are disabled. KeepAliveInterval *time.Duration - // MaxChunkSize overrides the configured maximum chunk size. - // If nil, the configured default is used. If set to <= 0, chunk splitting is disabled. - MaxChunkSize *int - // WriteChunk writes a single data chunk to the response body. It should not flush. WriteChunk func(chunk []byte) @@ -49,7 +45,7 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can writeKeepAlive := opts.WriteKeepAlive if writeKeepAlive == nil { writeKeepAlive = func() { - _, _ = c.Writer.Write(sseKeepAlive) + _, _ = c.Writer.Write([]byte(": keep-alive\n\n")) } } @@ -65,12 +61,6 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can keepAliveC = keepAlive.C } - // Determine max chunk size for splitting large payloads - maxChunkSize := StreamingMaxChunkSize(h.Cfg) - if opts.MaxChunkSize != nil { - maxChunkSize = *opts.MaxChunkSize - } - var terminalErr *interfaces.ErrorMessage for { select { @@ -104,20 +94,8 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can cancel(nil) return } - // Split large chunks to reduce latency (like vibeproxy's 64KB optimization) - if maxChunkSize > 0 && len(chunk) > maxChunkSize { - for offset := 0; offset < len(chunk); offset += maxChunkSize { - end := offset + maxChunkSize - if end > len(chunk) { - end = len(chunk) - } - writeChunk(chunk[offset:end]) - flusher.Flush() - } - } else { - writeChunk(chunk) - flusher.Flush() - } + writeChunk(chunk) + flusher.Flush() case errMsg, ok := <-errs: if !ok { continue @@ -141,5 +119,3 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can } } } - -var sseKeepAlive = []byte(": keep-alive\n\n") diff --git a/sdk/api/management.go b/sdk/api/management.go index 579c1d4e..66af41ae 100644 --- a/sdk/api/management.go +++ b/sdk/api/management.go @@ -18,6 +18,10 @@ type ManagementTokenRequester interface { RequestCodexToken(*gin.Context) RequestAntigravityToken(*gin.Context) RequestQwenToken(*gin.Context) + RequestIFlowToken(*gin.Context) + RequestIFlowCookieToken(*gin.Context) + GetAuthStatus(c *gin.Context) + PostOAuthCallback(c *gin.Context) } type managementTokenRequester struct { @@ -50,3 +54,19 @@ func (m *managementTokenRequester) RequestAntigravityToken(c *gin.Context) { func (m *managementTokenRequester) RequestQwenToken(c *gin.Context) { m.handler.RequestQwenToken(c) } + +func (m *managementTokenRequester) RequestIFlowToken(c *gin.Context) { + m.handler.RequestIFlowToken(c) +} + +func (m *managementTokenRequester) RequestIFlowCookieToken(c *gin.Context) { + m.handler.RequestIFlowCookieToken(c) +} + +func (m *managementTokenRequester) GetAuthStatus(c *gin.Context) { + m.handler.GetAuthStatus(c) +} + +func (m *managementTokenRequester) PostOAuthCallback(c *gin.Context) { + m.handler.PostOAuthCallback(c) +} diff --git a/sdk/auth/antigravity.go b/sdk/auth/antigravity.go index 9acabf9a..ecca0a00 100644 --- a/sdk/auth/antigravity.go +++ b/sdk/auth/antigravity.go @@ -2,16 +2,13 @@ package auth import ( "context" - "encoding/json" "fmt" - "io" "net" "net/http" - "net/url" "strings" "time" - antigravityauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/antigravity" + "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/antigravity" "github.com/router-for-me/CLIProxyAPI/v6/internal/browser" "github.com/router-for-me/CLIProxyAPI/v6/internal/config" "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" @@ -20,20 +17,6 @@ import ( log "github.com/sirupsen/logrus" ) -const ( - antigravityClientID = "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com" - antigravityClientSecret = "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" - antigravityCallbackPort = 51121 -) - -var antigravityScopes = []string{ - "https://www.googleapis.com/auth/cloud-platform", - "https://www.googleapis.com/auth/userinfo.email", - "https://www.googleapis.com/auth/userinfo.profile", - "https://www.googleapis.com/auth/cclog", - "https://www.googleapis.com/auth/experimentsandconfigs", -} - // AntigravityAuthenticator implements OAuth login for the antigravity provider. type AntigravityAuthenticator struct{} @@ -61,14 +44,19 @@ func (AntigravityAuthenticator) Login(ctx context.Context, cfg *config.Config, o opts = &LoginOptions{} } - httpClient := util.SetProxy(&cfg.SDKConfig, &http.Client{}) + callbackPort := antigravity.CallbackPort + if opts.CallbackPort > 0 { + callbackPort = opts.CallbackPort + } + + authSvc := antigravity.NewAntigravityAuth(cfg, nil) state, err := misc.GenerateRandomState() if err != nil { return nil, fmt.Errorf("antigravity: failed to generate state: %w", err) } - srv, port, cbChan, errServer := startAntigravityCallbackServer() + srv, port, cbChan, errServer := startAntigravityCallbackServer(callbackPort) if errServer != nil { return nil, fmt.Errorf("antigravity: failed to start callback server: %w", errServer) } @@ -79,7 +67,7 @@ func (AntigravityAuthenticator) Login(ctx context.Context, cfg *config.Config, o }() redirectURI := fmt.Sprintf("http://localhost:%d/oauth-callback", port) - authURL := buildAntigravityAuthURL(redirectURI, state) + authURL := authSvc.BuildAuthURL(state, redirectURI) if !opts.NoBrowser { fmt.Println("Opening browser for antigravity authentication") @@ -160,33 +148,34 @@ waitForCallback: return nil, fmt.Errorf("antigravity: missing authorization code") } - tokenResp, errToken := exchangeAntigravityCode(ctx, cbRes.Code, redirectURI, httpClient) + tokenResp, errToken := authSvc.ExchangeCodeForTokens(ctx, cbRes.Code, redirectURI) if errToken != nil { return nil, fmt.Errorf("antigravity: token exchange failed: %w", errToken) } - email := "" - if tokenResp.AccessToken != "" { - if info, errInfo := fetchAntigravityUserInfo(ctx, tokenResp.AccessToken, httpClient); errInfo == nil && strings.TrimSpace(info.Email) != "" { - email = strings.TrimSpace(info.Email) - } + accessToken := strings.TrimSpace(tokenResp.AccessToken) + if accessToken == "" { + return nil, fmt.Errorf("antigravity: token exchange returned empty access token") } - // Fetch project/tier info via loadCodeAssist (same approach as Gemini CLI) + email, errInfo := authSvc.FetchUserInfo(ctx, accessToken) + if errInfo != nil { + return nil, fmt.Errorf("antigravity: fetch user info failed: %w", errInfo) + } + email = strings.TrimSpace(email) + if email == "" { + return nil, fmt.Errorf("antigravity: empty email returned from user info") + } + + // Fetch project ID via loadCodeAssist (same approach as Gemini CLI) projectID := "" - tierID := "" - allowedTiers := []map[string]any(nil) - if tokenResp.AccessToken != "" { - accountInfo, errProject := fetchAntigravityAccountInfo(ctx, tokenResp.AccessToken, httpClient) + if accessToken != "" { + fetchedProjectID, errProject := authSvc.FetchProjectID(ctx, accessToken) if errProject != nil { - log.Warnf("antigravity: failed to fetch account info: %v", errProject) - } else if accountInfo != nil { - projectID = accountInfo.ProjectID - tierID = accountInfo.TierID - allowedTiers = accountInfo.AllowedTiers - if projectID != "" { - log.Infof("antigravity: obtained project ID %s", projectID) - } + log.Warnf("antigravity: failed to fetch project ID: %v", errProject) + } else { + projectID = fetchedProjectID + log.Infof("antigravity: obtained project ID %s", projectID) } } @@ -205,14 +194,8 @@ waitForCallback: if projectID != "" { metadata["project_id"] = projectID } - if tierID != "" { - metadata["tier_id"] = tierID - } - if len(allowedTiers) > 0 { - metadata["allowed_tiers"] = allowedTiers - } - fileName := sanitizeAntigravityFileName(email) + fileName := antigravity.CredentialFileName(email) label := email if label == "" { label = "antigravity" @@ -237,13 +220,16 @@ type callbackResult struct { State string } -func startAntigravityCallbackServer() (*http.Server, int, <-chan callbackResult, error) { - addr := fmt.Sprintf(":%d", antigravityCallbackPort) +func startAntigravityCallbackServer(port int) (*http.Server, int, <-chan callbackResult, error) { + if port <= 0 { + port = antigravity.CallbackPort + } + addr := fmt.Sprintf(":%d", port) listener, err := net.Listen("tcp", addr) if err != nil { return nil, 0, nil, err } - port := listener.Addr().(*net.TCPAddr).Port + port = listener.Addr().(*net.TCPAddr).Port resultCh := make(chan callbackResult, 1) mux := http.NewServeMux() @@ -272,392 +258,9 @@ func startAntigravityCallbackServer() (*http.Server, int, <-chan callbackResult, return srv, port, resultCh, nil } -type antigravityTokenResponse struct { - AccessToken string `json:"access_token"` - RefreshToken string `json:"refresh_token"` - ExpiresIn int64 `json:"expires_in"` - TokenType string `json:"token_type"` -} - -func exchangeAntigravityCode(ctx context.Context, code, redirectURI string, httpClient *http.Client) (*antigravityTokenResponse, error) { - data := url.Values{} - data.Set("code", code) - data.Set("client_id", antigravityClientID) - data.Set("client_secret", antigravityClientSecret) - data.Set("redirect_uri", redirectURI) - data.Set("grant_type", "authorization_code") - - req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(data.Encode())) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - - resp, errDo := httpClient.Do(req) - if errDo != nil { - return nil, errDo - } - defer func() { - if errClose := resp.Body.Close(); errClose != nil { - log.Errorf("antigravity token exchange: close body error: %v", errClose) - } - }() - - var token antigravityTokenResponse - if errDecode := json.NewDecoder(resp.Body).Decode(&token); errDecode != nil { - return nil, errDecode - } - if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { - return nil, fmt.Errorf("oauth token exchange failed: status %d", resp.StatusCode) - } - return &token, nil -} - -type antigravityUserInfo struct { - Email string `json:"email"` -} - -func fetchAntigravityUserInfo(ctx context.Context, accessToken string, httpClient *http.Client) (*antigravityUserInfo, error) { - if strings.TrimSpace(accessToken) == "" { - return &antigravityUserInfo{}, nil - } - req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json", nil) - if err != nil { - return nil, err - } - req.Header.Set("Authorization", "Bearer "+accessToken) - - resp, errDo := httpClient.Do(req) - if errDo != nil { - return nil, errDo - } - defer func() { - if errClose := resp.Body.Close(); errClose != nil { - log.Errorf("antigravity userinfo: close body error: %v", errClose) - } - }() - - if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { - return &antigravityUserInfo{}, nil - } - var info antigravityUserInfo - if errDecode := json.NewDecoder(resp.Body).Decode(&info); errDecode != nil { - return nil, errDecode - } - return &info, nil -} - -func buildAntigravityAuthURL(redirectURI, state string) string { - params := url.Values{} - params.Set("access_type", "offline") - params.Set("client_id", antigravityClientID) - params.Set("prompt", "consent") - params.Set("redirect_uri", redirectURI) - params.Set("response_type", "code") - params.Set("scope", strings.Join(antigravityScopes, " ")) - params.Set("state", state) - return "https://accounts.google.com/o/oauth2/v2/auth?" + params.Encode() -} - -func sanitizeAntigravityFileName(email string) string { - if strings.TrimSpace(email) == "" { - return "antigravity.json" - } - replacer := strings.NewReplacer("@", "_", ".", "_") - return fmt.Sprintf("antigravity-%s.json", replacer.Replace(email)) -} - -// Antigravity API constants for project discovery -const ( - antigravityAPIEndpoint = "https://cloudcode-pa.googleapis.com" - antigravityAPIVersion = "v1internal" - antigravityAPIUserAgent = "google-api-nodejs-client/9.15.1" - antigravityAPIClient = "google-cloud-sdk vscode_cloudshelleditor/0.1" - antigravityClientMetadata = `{"ideType":"IDE_UNSPECIFIED","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}` -) - -// AntigravityAccountInfo captures metadata returned by loadCodeAssist. -type AntigravityAccountInfo struct { - ProjectID string - TierID string - AllowedTiers []map[string]any -} - // FetchAntigravityProjectID exposes project discovery for external callers. func FetchAntigravityProjectID(ctx context.Context, accessToken string, httpClient *http.Client) (string, error) { - info, err := fetchAntigravityAccountInfo(ctx, accessToken, httpClient) - if err != nil { - return "", err - } - return info.ProjectID, nil -} - -// FetchAntigravityAccountInfo exposes project and tier discovery for external callers. -func FetchAntigravityAccountInfo(ctx context.Context, accessToken string, httpClient *http.Client) (*AntigravityAccountInfo, error) { - return fetchAntigravityAccountInfo(ctx, accessToken, httpClient) -} - -// fetchAntigravityAccountInfo retrieves the project/tier metadata for the authenticated user via loadCodeAssist. -// This uses the same approach as Gemini CLI to get the cloudaicompanionProject. -func fetchAntigravityAccountInfo(ctx context.Context, accessToken string, httpClient *http.Client) (*AntigravityAccountInfo, error) { - // Call loadCodeAssist to get the project - loadReqBody := map[string]any{ - "metadata": map[string]string{ - "ideType": "IDE_UNSPECIFIED", - "platform": "PLATFORM_UNSPECIFIED", - "pluginType": "GEMINI", - }, - } - - rawBody, errMarshal := json.Marshal(loadReqBody) - if errMarshal != nil { - return nil, fmt.Errorf("marshal request body: %w", errMarshal) - } - - endpointURL := fmt.Sprintf("%s/%s:loadCodeAssist", antigravityAPIEndpoint, antigravityAPIVersion) - req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpointURL, strings.NewReader(string(rawBody))) - if err != nil { - return nil, fmt.Errorf("create request: %w", err) - } - req.Header.Set("Authorization", "Bearer "+accessToken) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("User-Agent", antigravityAPIUserAgent) - req.Header.Set("X-Goog-Api-Client", antigravityAPIClient) - req.Header.Set("Client-Metadata", antigravityClientMetadata) - - resp, errDo := httpClient.Do(req) - if errDo != nil { - return nil, fmt.Errorf("execute request: %w", errDo) - } - defer func() { - if errClose := resp.Body.Close(); errClose != nil { - log.Errorf("antigravity loadCodeAssist: close body error: %v", errClose) - } - }() - - bodyBytes, errRead := io.ReadAll(resp.Body) - if errRead != nil { - return nil, fmt.Errorf("read response: %w", errRead) - } - - if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { - return nil, fmt.Errorf("request failed with status %d: %s", resp.StatusCode, strings.TrimSpace(string(bodyBytes))) - } - - var loadResp map[string]any - if errDecode := json.Unmarshal(bodyBytes, &loadResp); errDecode != nil { - return nil, fmt.Errorf("decode response: %w", errDecode) - } - - info := parseAntigravityAccountInfo(loadResp) - if info.ProjectID == "" { - return nil, fmt.Errorf("no cloudaicompanionProject in response") - } - - return info, nil -} - -func parseAntigravityAccountInfo(loadResp map[string]any) *AntigravityAccountInfo { - info := &AntigravityAccountInfo{TierID: "legacy-tier"} - if loadResp == nil { - return info - } - if id, ok := loadResp["cloudaicompanionProject"].(string); ok { - info.ProjectID = strings.TrimSpace(id) - } - if info.ProjectID == "" { - if projectMap, ok := loadResp["cloudaicompanionProject"].(map[string]any); ok { - if id, okID := projectMap["id"].(string); okID { - info.ProjectID = strings.TrimSpace(id) - } - } - } - - // Check for paidTier first (Google AI Ultra subscription) - // This takes priority as it indicates the user has a paid subscription - if paidTier, ok := loadResp["paidTier"].(map[string]any); ok { - if id, okID := paidTier["id"].(string); okID && strings.TrimSpace(id) != "" { - info.TierID = strings.TrimSpace(id) - log.Debugf("antigravity: using paidTier %s", info.TierID) - } - } - - // If no paidTier, check currentTier - if info.TierID == "legacy-tier" { - if currentTier, ok := loadResp["currentTier"].(map[string]any); ok { - if id, okID := currentTier["id"].(string); okID && strings.TrimSpace(id) != "" { - info.TierID = strings.TrimSpace(id) - log.Debugf("antigravity: using currentTier %s", info.TierID) - } - } - } - - if tiers, okTiers := loadResp["allowedTiers"].([]any); okTiers { - for _, rawTier := range tiers { - tier, okTier := rawTier.(map[string]any) - if !okTier { - continue - } - entry := map[string]any{} - if id, okID := tier["id"].(string); okID { - id = strings.TrimSpace(id) - if id != "" { - entry["id"] = id - } - } - if name, okName := tier["name"].(string); okName { - name = strings.TrimSpace(name) - if name != "" { - entry["name"] = name - } - } - if display, okDisplay := tier["displayName"].(string); okDisplay { - display = strings.TrimSpace(display) - if display != "" { - entry["display_name"] = display - } - } - if isDefault, okDefault := tier["isDefault"].(bool); okDefault { - entry["is_default"] = isDefault - // Only use isDefault tier if we haven't found a better tier - if isDefault && info.TierID == "legacy-tier" { - if id, okID := entry["id"].(string); okID && id != "" { - info.TierID = id - } - } - } - if len(entry) > 0 { - info.AllowedTiers = append(info.AllowedTiers, entry) - } - } - } - return info -} - -// ImportFromAntigravityIDE imports token from Antigravity IDE's token file. -// This is useful for users who have already logged in via Antigravity IDE -// and want to use the same credentials in ProxyPilot. -// -// Parameters: -// - ctx: The context for the operation -// - cfg: The application configuration -// -// Returns: -// - *coreauth.Auth: The imported auth record -// - error: An error if the import fails -func (AntigravityAuthenticator) ImportFromAntigravityIDE(ctx context.Context, cfg *config.Config) (*coreauth.Auth, error) { - // Load token from Antigravity IDE - tokenData, err := antigravityauth.LoadAntigravityToken() - if err != nil { - return nil, fmt.Errorf("failed to load Antigravity IDE token: %w", err) - } - - // Get email from token or fetch from API - email := tokenData.Email - projectID := tokenData.ProjectID - tierID := "" - var allowedTiers []map[string]any - - // If email is not in token, try to fetch user info - if email == "" && tokenData.GetAccessToken() != "" { - httpClient := util.SetProxy(&cfg.SDKConfig, &http.Client{}) - if info, errInfo := fetchAntigravityUserInfo(ctx, tokenData.GetAccessToken(), httpClient); errInfo == nil && strings.TrimSpace(info.Email) != "" { - email = strings.TrimSpace(info.Email) - } - } - - // Try to fetch project/tier info if not in token - if projectID == "" && tokenData.GetAccessToken() != "" { - httpClient := util.SetProxy(&cfg.SDKConfig, &http.Client{}) - if accountInfo, errProject := fetchAntigravityAccountInfo(ctx, tokenData.GetAccessToken(), httpClient); errProject == nil && accountInfo != nil { - projectID = accountInfo.ProjectID - tierID = accountInfo.TierID - allowedTiers = accountInfo.AllowedTiers - } - } - - // Parse expiry time - expiresAt := tokenData.GetExpiry() - if expiresAt.IsZero() { - // Default to 1 hour from now if no expiry - expiresAt = time.Now().Add(1 * time.Hour) - } - - // Calculate expires_in (seconds until expiry) - expiresIn := int64(time.Until(expiresAt).Seconds()) - if expiresIn < 0 { - expiresIn = 0 - } - - now := time.Now() - metadata := map[string]any{ - "type": "antigravity", - "access_token": tokenData.GetAccessToken(), - "refresh_token": tokenData.GetRefreshToken(), - "expires_in": expiresIn, - "timestamp": now.UnixMilli(), - "expired": expiresAt.Format(time.RFC3339), - } - if email != "" { - metadata["email"] = email - } - if projectID != "" { - metadata["project_id"] = projectID - } - if tierID != "" { - metadata["tier_id"] = tierID - } - if len(allowedTiers) > 0 { - metadata["allowed_tiers"] = allowedTiers - } - - // Add client credentials if available for token refresh - if tokenData.Token != nil { - if tokenData.Token.ClientID != "" { - metadata["client_id"] = tokenData.Token.ClientID - } - if tokenData.Token.ClientSecret != "" { - metadata["client_secret"] = tokenData.Token.ClientSecret - } - } - - fileName := sanitizeAntigravityFileName(email) - label := email - if label == "" { - label = "antigravity" - } - - // Check if token is expired and warn user - if tokenData.IsExpired() { - fmt.Println("Warning: The imported token is expired. ProxyPilot will attempt to refresh it on first use.") - } - - record := &coreauth.Auth{ - ID: fileName, - Provider: "antigravity", - FileName: fileName, - Label: label, - Status: coreauth.StatusActive, - CreatedAt: now, - UpdatedAt: now, - Metadata: metadata, - Attributes: map[string]string{ - "source": "antigravity-ide-import", - "email": email, - }, - // NextRefreshAfter is set to 5 minutes before expiry - NextRefreshAfter: expiresAt.Add(-5 * time.Minute), - } - - // Display success message - if email != "" { - fmt.Printf("\n✓ Imported Antigravity token from Antigravity IDE (Account: %s)\n", email) - } else { - fmt.Println("\n✓ Imported Antigravity token from Antigravity IDE") - } - if projectID != "" { - fmt.Printf(" Project: %s\n", projectID) - } - - return record, nil + cfg := &config.Config{} + authSvc := antigravity.NewAntigravityAuth(cfg, httpClient) + return authSvc.FetchProjectID(ctx, accessToken) } diff --git a/sdk/auth/claude.go b/sdk/auth/claude.go index c43b78cd..2c7a8988 100644 --- a/sdk/auth/claude.go +++ b/sdk/auth/claude.go @@ -47,6 +47,11 @@ func (a *ClaudeAuthenticator) Login(ctx context.Context, cfg *config.Config, opt opts = &LoginOptions{} } + callbackPort := a.CallbackPort + if opts.CallbackPort > 0 { + callbackPort = opts.CallbackPort + } + pkceCodes, err := claude.GeneratePKCECodes() if err != nil { return nil, fmt.Errorf("claude pkce generation failed: %w", err) @@ -57,7 +62,7 @@ func (a *ClaudeAuthenticator) Login(ctx context.Context, cfg *config.Config, opt return nil, fmt.Errorf("claude state generation failed: %w", err) } - oauthServer := claude.NewOAuthServer(a.CallbackPort) + oauthServer := claude.NewOAuthServer(callbackPort) if err = oauthServer.Start(); err != nil { if strings.Contains(err.Error(), "already in use") { return nil, claude.NewAuthenticationError(claude.ErrPortInUse, err) @@ -84,15 +89,15 @@ func (a *ClaudeAuthenticator) Login(ctx context.Context, cfg *config.Config, opt fmt.Println("Opening browser for Claude authentication") if !browser.IsAvailable() { log.Warn("No browser available; please open the URL manually") - util.PrintSSHTunnelInstructions(a.CallbackPort) + util.PrintSSHTunnelInstructions(callbackPort) fmt.Printf("Visit the following URL to continue authentication:\n%s\n", authURL) } else if err = browser.OpenURL(authURL); err != nil { log.Warnf("Failed to open browser automatically: %v", err) - util.PrintSSHTunnelInstructions(a.CallbackPort) + util.PrintSSHTunnelInstructions(callbackPort) fmt.Printf("Visit the following URL to continue authentication:\n%s\n", authURL) } } else { - util.PrintSSHTunnelInstructions(a.CallbackPort) + util.PrintSSHTunnelInstructions(callbackPort) fmt.Printf("Visit the following URL to continue authentication:\n%s\n", authURL) } diff --git a/sdk/auth/codex.go b/sdk/auth/codex.go index 903b40d0..b655a239 100644 --- a/sdk/auth/codex.go +++ b/sdk/auth/codex.go @@ -49,6 +49,11 @@ func (a *CodexAuthenticator) Login(ctx context.Context, cfg *config.Config, opts opts = &LoginOptions{} } + callbackPort := a.CallbackPort + if opts.CallbackPort > 0 { + callbackPort = opts.CallbackPort + } + pkceCodes, err := codex.GeneratePKCECodes() if err != nil { return nil, fmt.Errorf("codex pkce generation failed: %w", err) @@ -59,7 +64,7 @@ func (a *CodexAuthenticator) Login(ctx context.Context, cfg *config.Config, opts return nil, fmt.Errorf("codex state generation failed: %w", err) } - oauthServer := codex.NewOAuthServer(a.CallbackPort) + oauthServer := codex.NewOAuthServer(callbackPort) if err = oauthServer.Start(); err != nil { if strings.Contains(err.Error(), "already in use") { return nil, codex.NewAuthenticationError(codex.ErrPortInUse, err) @@ -85,15 +90,15 @@ func (a *CodexAuthenticator) Login(ctx context.Context, cfg *config.Config, opts fmt.Println("Opening browser for Codex authentication") if !browser.IsAvailable() { log.Warn("No browser available; please open the URL manually") - util.PrintSSHTunnelInstructions(a.CallbackPort) + util.PrintSSHTunnelInstructions(callbackPort) fmt.Printf("Visit the following URL to continue authentication:\n%s\n", authURL) } else if err = browser.OpenURL(authURL); err != nil { log.Warnf("Failed to open browser automatically: %v", err) - util.PrintSSHTunnelInstructions(a.CallbackPort) + util.PrintSSHTunnelInstructions(callbackPort) fmt.Printf("Visit the following URL to continue authentication:\n%s\n", authURL) } } else { - util.PrintSSHTunnelInstructions(a.CallbackPort) + util.PrintSSHTunnelInstructions(callbackPort) fmt.Printf("Visit the following URL to continue authentication:\n%s\n", authURL) } @@ -188,7 +193,6 @@ waitForCallback: return nil, fmt.Errorf("codex token storage missing account information") } - // Extract plan type and account ID for filename disambiguation planType := "" hashAccountID := "" if tokenStorage.IDToken != "" { @@ -201,9 +205,7 @@ waitForCallback: } } } - - // Generate filename with plan type and hashed account ID for team plans - fileName := codexCredentialFileName(tokenStorage.Email, planType, hashAccountID) + fileName := codex.CredentialFileName(tokenStorage.Email, planType, hashAccountID, true) metadata := map[string]any{ "email": tokenStorage.Email, } @@ -221,28 +223,3 @@ waitForCallback: Metadata: metadata, }, nil } - -// codexCredentialFileName generates the filename for Codex credential storage. -// For team plans, it includes a hashed account ID to disambiguate multiple accounts. -func codexCredentialFileName(email, planType, hashAccountID string) string { - email = strings.TrimSpace(email) - plan := normalizePlanTypeForFilename(planType) - - if plan == "" { - return fmt.Sprintf("codex-%s.json", email) - } else if plan == "team" && hashAccountID != "" { - return fmt.Sprintf("codex-%s-%s-%s.json", hashAccountID, email, plan) - } - return fmt.Sprintf("codex-%s-%s.json", email, plan) -} - -// normalizePlanTypeForFilename converts plan types to filename-safe values. -func normalizePlanTypeForFilename(planType string) string { - planType = strings.ToLower(strings.TrimSpace(planType)) - switch planType { - case "plus", "team", "enterprise": - return planType - default: - return "" - } -} diff --git a/sdk/auth/errors.go b/sdk/auth/errors.go index eee4019f..78fe9a17 100644 --- a/sdk/auth/errors.go +++ b/sdk/auth/errors.go @@ -1,5 +1,32 @@ package auth +import ( + "fmt" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces" +) + +// ProjectSelectionError indicates that the user must choose a specific project ID. +type ProjectSelectionError struct { + Email string + Projects []interfaces.GCPProjectProjects +} + +func (e *ProjectSelectionError) Error() string { + if e == nil { + return "cliproxy auth: project selection required" + } + return fmt.Sprintf("cliproxy auth: project selection required for %s", e.Email) +} + +// ProjectsDisplay returns the projects list for caller presentation. +func (e *ProjectSelectionError) ProjectsDisplay() []interfaces.GCPProjectProjects { + if e == nil { + return nil + } + return e.Projects +} + // EmailRequiredError indicates that the calling context must provide an email or alias. type EmailRequiredError struct { Prompt string diff --git a/sdk/auth/filestore.go b/sdk/auth/filestore.go index 77b24db4..0bb7ff7d 100644 --- a/sdk/auth/filestore.go +++ b/sdk/auth/filestore.go @@ -68,6 +68,7 @@ func (s *FileTokenStore) Save(ctx context.Context, auth *cliproxyauth.Auth) (str return "", err } case auth.Metadata != nil: + auth.Metadata["disabled"] = auth.Disabled raw, errMarshal := json.Marshal(auth.Metadata) if errMarshal != nil { return "", fmt.Errorf("auth filestore: marshal metadata failed: %w", errMarshal) @@ -214,12 +215,18 @@ func (s *FileTokenStore) readAuthFile(path, baseDir string) (*cliproxyauth.Auth, return nil, fmt.Errorf("stat file: %w", err) } id := s.idFor(path, baseDir) + disabled, _ := metadata["disabled"].(bool) + status := cliproxyauth.StatusActive + if disabled { + status = cliproxyauth.StatusDisabled + } auth := &cliproxyauth.Auth{ ID: id, Provider: provider, FileName: id, Label: s.labelFor(metadata), - Status: cliproxyauth.StatusActive, + Status: status, + Disabled: disabled, Attributes: map[string]string{"path": path}, Metadata: metadata, CreatedAt: info.ModTime(), diff --git a/sdk/auth/gemini.go b/sdk/auth/gemini.go index 75ef4579..2b8f9c2b 100644 --- a/sdk/auth/gemini.go +++ b/sdk/auth/gemini.go @@ -45,8 +45,9 @@ func (a *GeminiAuthenticator) Login(ctx context.Context, cfg *config.Config, opt geminiAuth := gemini.NewGeminiAuth() _, err := geminiAuth.GetAuthenticatedClient(ctx, &ts, cfg, &gemini.WebLoginOptions{ - NoBrowser: opts.NoBrowser, - Prompt: opts.Prompt, + NoBrowser: opts.NoBrowser, + CallbackPort: opts.CallbackPort, + Prompt: opts.Prompt, }) if err != nil { return nil, fmt.Errorf("gemini authentication failed: %w", err) diff --git a/sdk/auth/interfaces.go b/sdk/auth/interfaces.go index 7a7868e1..64cf8ed0 100644 --- a/sdk/auth/interfaces.go +++ b/sdk/auth/interfaces.go @@ -14,10 +14,11 @@ var ErrRefreshNotSupported = errors.New("cliproxy auth: refresh not supported") // LoginOptions captures generic knobs shared across authenticators. // Provider-specific logic can inspect Metadata for extra parameters. type LoginOptions struct { - NoBrowser bool - ProjectID string - Metadata map[string]string - Prompt func(prompt string) (string, error) + NoBrowser bool + ProjectID string + CallbackPort int + Metadata map[string]string + Prompt func(prompt string) (string, error) } // Authenticator manages login and optional refresh flows for a provider. diff --git a/sdk/auth/manager.go b/sdk/auth/manager.go index d630f128..a00bccb7 100644 --- a/sdk/auth/manager.go +++ b/sdk/auth/manager.go @@ -75,15 +75,19 @@ func (m *Manager) Login(ctx context.Context, provider string, cfg *config.Config return record, savedPath, nil } -// SaveAuth persists an auth record directly without going through the login flow. +// SaveAuth persists an existing auth record to storage. func (m *Manager) SaveAuth(record *coreauth.Auth, cfg *config.Config) (string, error) { if m.store == nil { - return "", fmt.Errorf("no store configured") + return "", fmt.Errorf("cliproxy auth: no token store configured") } + if record == nil { + return "", fmt.Errorf("cliproxy auth: nil record") + } + ctx := context.Background() if cfg != nil { if dirSetter, ok := m.store.(interface{ SetBaseDir(string) }); ok { dirSetter.SetBaseDir(cfg.AuthDir) } } - return m.store.Save(context.Background(), record) + return m.store.Save(ctx, record) } diff --git a/sdk/auth/refresh_registry.go b/sdk/auth/refresh_registry.go index fc4363ce..e82ac684 100644 --- a/sdk/auth/refresh_registry.go +++ b/sdk/auth/refresh_registry.go @@ -10,12 +10,10 @@ func init() { registerRefreshLead("codex", func() Authenticator { return NewCodexAuthenticator() }) registerRefreshLead("claude", func() Authenticator { return NewClaudeAuthenticator() }) registerRefreshLead("qwen", func() Authenticator { return NewQwenAuthenticator() }) + registerRefreshLead("iflow", func() Authenticator { return NewIFlowAuthenticator() }) registerRefreshLead("gemini", func() Authenticator { return NewGeminiAuthenticator() }) registerRefreshLead("gemini-cli", func() Authenticator { return NewGeminiAuthenticator() }) registerRefreshLead("antigravity", func() Authenticator { return NewAntigravityAuthenticator() }) - registerRefreshLead("kiro", func() Authenticator { return NewKiroAuthenticator() }) - registerRefreshLead("minimax", func() Authenticator { return NewMiniMaxAuthenticator() }) - registerRefreshLead("zhipu", func() Authenticator { return NewZhipuAuthenticator() }) } func registerRefreshLead(provider string, factory func() Authenticator) { diff --git a/sdk/cliproxy/auth/conductor.go b/sdk/cliproxy/auth/conductor.go index 025f1845..b82e0e10 100644 --- a/sdk/cliproxy/auth/conductor.go +++ b/sdk/cliproxy/auth/conductor.go @@ -15,8 +15,10 @@ import ( "time" "github.com/google/uuid" + 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/registry" + "github.com/router-for-me/CLIProxyAPI/v6/internal/thinking" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" log "github.com/sirupsen/logrus" @@ -34,8 +36,6 @@ type ProviderExecutor interface { Refresh(ctx context.Context, auth *Auth) (*Auth, error) // CountTokens returns the token count for the given request. CountTokens(ctx context.Context, auth *Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) - // Embed returns embeddings for the given request. - Embed(ctx context.Context, auth *Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) // HttpRequest injects provider credentials into the supplied HTTP request and executes it. // Callers must close the response body when non-nil. HttpRequest(ctx context.Context, auth *Auth, req *http.Request) (*http.Response, error) @@ -49,7 +49,7 @@ type RefreshEvaluator interface { const ( refreshCheckInterval = 5 * time.Second refreshPendingBackoff = time.Minute - refreshFailureBackoff = 1 * time.Minute + refreshFailureBackoff = 5 * time.Minute quotaBackoffBase = time.Second quotaBackoffMax = 30 * time.Minute ) @@ -61,6 +61,15 @@ func SetQuotaCooldownDisabled(disable bool) { quotaCooldownDisabled.Store(disable) } +func quotaCooldownDisabledForAuth(auth *Auth) bool { + if auth != nil { + if override, ok := auth.DisableCoolingOverride(); ok { + return override + } + } + return quotaCooldownDisabled.Load() +} + // Result captures execution outcome used to adjust auth state. type Result struct { // AuthID references the auth that produced this result. @@ -75,8 +84,6 @@ type Result struct { RetryAfter *time.Duration // Error describes the failure when Success is false. Error *Error - // Usage carries token counts from the completed request. - Usage *ResultUsage } // Selector chooses an auth candidate for execution. @@ -121,9 +128,16 @@ type Manager struct { requestRetry atomic.Int32 maxRetryInterval atomic.Int64 - // Auto refresh configuration - autoRefreshBuffer atomic.Int64 // nanoseconds - dailyResetHour atomic.Int32 + // oauthModelAlias stores global OAuth model alias mappings (alias -> upstream name) keyed by channel. + oauthModelAlias atomic.Value + + // apiKeyModelAlias caches resolved model alias mappings for API-key auths. + // Keyed by auth.ID, value is alias(lower) -> upstream model (including suffix). + apiKeyModelAlias atomic.Value + + // runtimeConfig stores the latest application config for request-time decisions. + // It is initialized in NewManager; never Load() before first Store(). + runtimeConfig atomic.Value // Optional HTTP RoundTripper provider injected by host. rtProvider RoundTripperProvider @@ -140,7 +154,7 @@ func NewManager(store Store, selector Selector, hook Hook) *Manager { if hook == nil { hook = NoopHook{} } - return &Manager{ + manager := &Manager{ store: store, executors: make(map[string]ProviderExecutor), selector: selector, @@ -148,6 +162,10 @@ func NewManager(store Store, selector Selector, hook Hook) *Manager { auths: make(map[string]*Auth), providerOffsets: make(map[string]int), } + // atomic.Value requires non-nil initial value. + manager.runtimeConfig.Store(&internalconfig.Config{}) + manager.apiKeyModelAlias.Store(apiKeyModelAliasTable(nil)) + return manager } func (m *Manager) SetSelector(selector Selector) { @@ -176,6 +194,181 @@ func (m *Manager) SetRoundTripperProvider(p RoundTripperProvider) { m.mu.Unlock() } +// SetConfig updates the runtime config snapshot used by request-time helpers. +// Callers should provide the latest config on reload so per-credential alias mapping stays in sync. +func (m *Manager) SetConfig(cfg *internalconfig.Config) { + if m == nil { + return + } + if cfg == nil { + cfg = &internalconfig.Config{} + } + m.runtimeConfig.Store(cfg) + m.rebuildAPIKeyModelAliasFromRuntimeConfig() +} + +func (m *Manager) lookupAPIKeyUpstreamModel(authID, requestedModel string) string { + if m == nil { + return "" + } + authID = strings.TrimSpace(authID) + if authID == "" { + return "" + } + requestedModel = strings.TrimSpace(requestedModel) + if requestedModel == "" { + return "" + } + table, _ := m.apiKeyModelAlias.Load().(apiKeyModelAliasTable) + if table == nil { + return "" + } + byAlias := table[authID] + if len(byAlias) == 0 { + return "" + } + key := strings.ToLower(thinking.ParseSuffix(requestedModel).ModelName) + if key == "" { + key = strings.ToLower(requestedModel) + } + resolved := strings.TrimSpace(byAlias[key]) + if resolved == "" { + return "" + } + // Preserve thinking suffix from the client's requested model unless config already has one. + requestResult := thinking.ParseSuffix(requestedModel) + if thinking.ParseSuffix(resolved).HasSuffix { + return resolved + } + if requestResult.HasSuffix && requestResult.RawSuffix != "" { + return resolved + "(" + requestResult.RawSuffix + ")" + } + return resolved + +} + +func (m *Manager) rebuildAPIKeyModelAliasFromRuntimeConfig() { + if m == nil { + return + } + cfg, _ := m.runtimeConfig.Load().(*internalconfig.Config) + if cfg == nil { + cfg = &internalconfig.Config{} + } + m.mu.Lock() + defer m.mu.Unlock() + m.rebuildAPIKeyModelAliasLocked(cfg) +} + +func (m *Manager) rebuildAPIKeyModelAliasLocked(cfg *internalconfig.Config) { + if m == nil { + return + } + if cfg == nil { + cfg = &internalconfig.Config{} + } + + out := make(apiKeyModelAliasTable) + for _, auth := range m.auths { + if auth == nil { + continue + } + if strings.TrimSpace(auth.ID) == "" { + continue + } + kind, _ := auth.AccountInfo() + if !strings.EqualFold(strings.TrimSpace(kind), "api_key") { + continue + } + + byAlias := make(map[string]string) + provider := strings.ToLower(strings.TrimSpace(auth.Provider)) + switch provider { + case "gemini": + if entry := resolveGeminiAPIKeyConfig(cfg, auth); entry != nil { + compileAPIKeyModelAliasForModels(byAlias, entry.Models) + } + case "claude": + if entry := resolveClaudeAPIKeyConfig(cfg, auth); entry != nil { + compileAPIKeyModelAliasForModels(byAlias, entry.Models) + } + case "codex": + if entry := resolveCodexAPIKeyConfig(cfg, auth); entry != nil { + compileAPIKeyModelAliasForModels(byAlias, entry.Models) + } + case "vertex": + if entry := resolveVertexAPIKeyConfig(cfg, auth); entry != nil { + compileAPIKeyModelAliasForModels(byAlias, entry.Models) + } + default: + // OpenAI-compat uses config selection from auth.Attributes. + providerKey := "" + compatName := "" + if auth.Attributes != nil { + providerKey = strings.TrimSpace(auth.Attributes["provider_key"]) + compatName = strings.TrimSpace(auth.Attributes["compat_name"]) + } + if compatName != "" || strings.EqualFold(strings.TrimSpace(auth.Provider), "openai-compatibility") { + if entry := resolveOpenAICompatConfig(cfg, providerKey, compatName, auth.Provider); entry != nil { + compileAPIKeyModelAliasForModels(byAlias, entry.Models) + } + } + } + + if len(byAlias) > 0 { + out[auth.ID] = byAlias + } + } + + m.apiKeyModelAlias.Store(out) +} + +func compileAPIKeyModelAliasForModels[T interface { + GetName() string + GetAlias() string +}](out map[string]string, models []T) { + if out == nil { + return + } + for i := range models { + alias := strings.TrimSpace(models[i].GetAlias()) + name := strings.TrimSpace(models[i].GetName()) + if alias == "" || name == "" { + continue + } + aliasKey := strings.ToLower(thinking.ParseSuffix(alias).ModelName) + if aliasKey == "" { + aliasKey = strings.ToLower(alias) + } + // Config priority: first alias wins. + if _, exists := out[aliasKey]; exists { + continue + } + out[aliasKey] = name + // Also allow direct lookup by upstream name (case-insensitive), so lookups on already-upstream + // models remain a cheap no-op. + nameKey := strings.ToLower(thinking.ParseSuffix(name).ModelName) + if nameKey == "" { + nameKey = strings.ToLower(name) + } + if nameKey != "" { + if _, exists := out[nameKey]; !exists { + out[nameKey] = name + } + } + // Preserve config suffix priority by seeding a base-name lookup when name already has suffix. + nameResult := thinking.ParseSuffix(name) + if nameResult.HasSuffix { + baseKey := strings.ToLower(strings.TrimSpace(nameResult.ModelName)) + if baseKey != "" { + if _, exists := out[baseKey]; !exists { + out[baseKey] = name + } + } + } + } +} + // SetRetryConfig updates retry attempts and cooldown wait interval. func (m *Manager) SetRetryConfig(retry int, maxRetryInterval time.Duration) { if m == nil { @@ -191,21 +384,6 @@ func (m *Manager) SetRetryConfig(retry int, maxRetryInterval time.Duration) { m.maxRetryInterval.Store(maxRetryInterval.Nanoseconds()) } -// SetRefreshConfig updates auto-refresh buffer and daily reset hour settings. -func (m *Manager) SetRefreshConfig(autoRefreshBuffer time.Duration, dailyResetHour int) { - if m == nil { - return - } - if autoRefreshBuffer <= 0 { - autoRefreshBuffer = 5 * time.Minute // default - } - if dailyResetHour < 0 || dailyResetHour > 23 { - dailyResetHour = 0 // default to midnight UTC - } - m.autoRefreshBuffer.Store(autoRefreshBuffer.Nanoseconds()) - m.dailyResetHour.Store(int32(dailyResetHour)) -} - // RegisterExecutor registers a provider executor with the manager. func (m *Manager) RegisterExecutor(executor ProviderExecutor) { if executor == nil { @@ -236,15 +414,10 @@ func (m *Manager) Register(ctx context.Context, auth *Auth) (*Auth, error) { auth.ID = uuid.NewString() } auth.EnsureIndex() - // Populate TokenExpiresAt from metadata if not already set - if auth.TokenExpiresAt.IsZero() { - if exp, ok := auth.ExpirationTime(); ok { - auth.TokenExpiresAt = exp - } - } m.mu.Lock() m.auths[auth.ID] = auth.Clone() m.mu.Unlock() + m.rebuildAPIKeyModelAliasFromRuntimeConfig() _ = m.persist(ctx, auth) m.hook.OnAuthRegistered(ctx, auth.Clone()) return auth.Clone(), nil @@ -261,14 +434,9 @@ func (m *Manager) Update(ctx context.Context, auth *Auth) (*Auth, error) { auth.indexAssigned = existing.indexAssigned } auth.EnsureIndex() - // Populate TokenExpiresAt from metadata if not already set - if auth.TokenExpiresAt.IsZero() { - if exp, ok := auth.ExpirationTime(); ok { - auth.TokenExpiresAt = exp - } - } m.auths[auth.ID] = auth.Clone() m.mu.Unlock() + m.rebuildAPIKeyModelAliasFromRuntimeConfig() _ = m.persist(ctx, auth) m.hook.OnAuthUpdated(ctx, auth.Clone()) return auth.Clone(), nil @@ -291,14 +459,13 @@ func (m *Manager) Load(ctx context.Context) error { continue } auth.EnsureIndex() - // Populate TokenExpiresAt from metadata if not already set - if auth.TokenExpiresAt.IsZero() { - if exp, ok := auth.ExpirationTime(); ok { - auth.TokenExpiresAt = exp - } - } m.auths[auth.ID] = auth.Clone() } + cfg, _ := m.runtimeConfig.Load().(*internalconfig.Config) + if cfg == nil { + cfg = &internalconfig.Config{} + } + m.rebuildAPIKeyModelAliasLocked(cfg) return nil } @@ -309,24 +476,17 @@ func (m *Manager) Execute(ctx context.Context, providers []string, req cliproxye if len(normalized) == 0 { return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "no provider supplied"} } - rotated := m.rotateProviders(req.Model, normalized) - retryTimes, maxWait := m.retrySettings() - attempts := retryTimes + 1 - if attempts < 1 { - attempts = 1 - } + _, maxWait := m.retrySettings() var lastErr error - for attempt := 0; attempt < attempts; attempt++ { - resp, errExec := m.executeProvidersOnce(ctx, rotated, func(execCtx context.Context, provider string) (cliproxyexecutor.Response, error) { - return m.executeWithProvider(execCtx, provider, req, opts) - }) + for attempt := 0; ; attempt++ { + resp, errExec := m.executeMixedOnce(ctx, normalized, req, opts) if errExec == nil { return resp, nil } lastErr = errExec - wait, shouldRetry := m.shouldRetryAfterError(errExec, attempt, attempts, rotated, req.Model, maxWait) + wait, shouldRetry := m.shouldRetryAfterError(errExec, attempt, normalized, req.Model, maxWait) if !shouldRetry { break } @@ -347,24 +507,17 @@ func (m *Manager) ExecuteCount(ctx context.Context, providers []string, req clip if len(normalized) == 0 { return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "no provider supplied"} } - rotated := m.rotateProviders(req.Model, normalized) - retryTimes, maxWait := m.retrySettings() - attempts := retryTimes + 1 - if attempts < 1 { - attempts = 1 - } + _, maxWait := m.retrySettings() var lastErr error - for attempt := 0; attempt < attempts; attempt++ { - resp, errExec := m.executeProvidersOnce(ctx, rotated, func(execCtx context.Context, provider string) (cliproxyexecutor.Response, error) { - return m.executeCountWithProvider(execCtx, provider, req, opts) - }) + for attempt := 0; ; attempt++ { + resp, errExec := m.executeCountMixedOnce(ctx, normalized, req, opts) if errExec == nil { return resp, nil } lastErr = errExec - wait, shouldRetry := m.shouldRetryAfterError(errExec, attempt, attempts, rotated, req.Model, maxWait) + wait, shouldRetry := m.shouldRetryAfterError(errExec, attempt, normalized, req.Model, maxWait) if !shouldRetry { break } @@ -385,24 +538,17 @@ func (m *Manager) ExecuteStream(ctx context.Context, providers []string, req cli if len(normalized) == 0 { return nil, &Error{Code: "provider_not_found", Message: "no provider supplied"} } - rotated := m.rotateProviders(req.Model, normalized) - retryTimes, maxWait := m.retrySettings() - attempts := retryTimes + 1 - if attempts < 1 { - attempts = 1 - } + _, maxWait := m.retrySettings() var lastErr error - for attempt := 0; attempt < attempts; attempt++ { - chunks, errStream := m.executeStreamProvidersOnce(ctx, rotated, func(execCtx context.Context, provider string) (<-chan cliproxyexecutor.StreamChunk, error) { - return m.executeStreamWithProvider(execCtx, provider, req, opts) - }) + for attempt := 0; ; attempt++ { + chunks, errStream := m.executeStreamMixedOnce(ctx, normalized, req, opts) if errStream == nil { return chunks, nil } lastErr = errStream - wait, shouldRetry := m.shouldRetryAfterError(errStream, attempt, attempts, rotated, req.Model, maxWait) + wait, shouldRetry := m.shouldRetryAfterError(errStream, attempt, normalized, req.Model, maxWait) if !shouldRetry { break } @@ -416,15 +562,16 @@ func (m *Manager) ExecuteStream(ctx context.Context, providers []string, req cli return nil, &Error{Code: "auth_not_found", Message: "no auth available"} } -func (m *Manager) executeWithProvider(ctx context.Context, provider string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - if provider == "" { - return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "provider identifier is empty"} +func (m *Manager) executeMixedOnce(ctx context.Context, providers []string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + if len(providers) == 0 { + return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "no provider supplied"} } routeModel := req.Model + opts = ensureRequestedModelMetadata(opts, routeModel) tried := make(map[string]struct{}) var lastErr error for { - auth, executor, errPick := m.pickNext(ctx, provider, routeModel, opts, tried) + auth, executor, provider, errPick := m.pickNextMixed(ctx, providers, routeModel, opts, tried) if errPick != nil { if lastErr != nil { return cliproxyexecutor.Response{}, lastErr @@ -436,17 +583,21 @@ func (m *Manager) executeWithProvider(ctx context.Context, provider string, req debugLogAuthSelection(entry, auth, provider, req.Model) tried[auth.ID] = struct{}{} - recordSelection(ctx, provider, auth) execCtx := ctx if rt := m.roundTripperFor(auth); rt != nil { execCtx = context.WithValue(execCtx, roundTripperContextKey{}, rt) execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt) } execReq := req - execReq.Model, execReq.Metadata = rewriteModelForAuth(routeModel, req.Metadata, auth) + execReq.Model = rewriteModelForAuth(routeModel, auth) + execReq.Model = m.applyOAuthModelAlias(auth, execReq.Model) + execReq.Model = m.applyAPIKeyModelAlias(auth, execReq.Model) resp, errExec := executor.Execute(execCtx, auth, execReq, opts) result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: errExec == nil} if errExec != nil { + if errCtx := execCtx.Err(); errCtx != nil { + return cliproxyexecutor.Response{}, errCtx + } result.Error = &Error{Message: errExec.Error()} var se cliproxyexecutor.StatusError if errors.As(errExec, &se) && se != nil { @@ -464,15 +615,16 @@ func (m *Manager) executeWithProvider(ctx context.Context, provider string, req } } -func (m *Manager) executeCountWithProvider(ctx context.Context, provider string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - if provider == "" { - return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "provider identifier is empty"} +func (m *Manager) executeCountMixedOnce(ctx context.Context, providers []string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + if len(providers) == 0 { + return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "no provider supplied"} } routeModel := req.Model + opts = ensureRequestedModelMetadata(opts, routeModel) tried := make(map[string]struct{}) var lastErr error for { - auth, executor, errPick := m.pickNext(ctx, provider, routeModel, opts, tried) + auth, executor, provider, errPick := m.pickNextMixed(ctx, providers, routeModel, opts, tried) if errPick != nil { if lastErr != nil { return cliproxyexecutor.Response{}, lastErr @@ -484,17 +636,21 @@ func (m *Manager) executeCountWithProvider(ctx context.Context, provider string, debugLogAuthSelection(entry, auth, provider, req.Model) tried[auth.ID] = struct{}{} - recordSelection(ctx, provider, auth) execCtx := ctx if rt := m.roundTripperFor(auth); rt != nil { execCtx = context.WithValue(execCtx, roundTripperContextKey{}, rt) execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt) } execReq := req - execReq.Model, execReq.Metadata = rewriteModelForAuth(routeModel, req.Metadata, auth) + execReq.Model = rewriteModelForAuth(routeModel, auth) + execReq.Model = m.applyOAuthModelAlias(auth, execReq.Model) + execReq.Model = m.applyAPIKeyModelAlias(auth, execReq.Model) resp, errExec := executor.CountTokens(execCtx, auth, execReq, opts) result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: errExec == nil} if errExec != nil { + if errCtx := execCtx.Err(); errCtx != nil { + return cliproxyexecutor.Response{}, errCtx + } result.Error = &Error{Message: errExec.Error()} var se cliproxyexecutor.StatusError if errors.As(errExec, &se) && se != nil { @@ -512,86 +668,16 @@ func (m *Manager) executeCountWithProvider(ctx context.Context, provider string, } } -// ExecuteEmbed executes an embedding request via the first available provider and auth. -func (m *Manager) ExecuteEmbed(ctx context.Context, providers []string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - providers = m.normalizeProviders(providers) +func (m *Manager) executeStreamMixedOnce(ctx context.Context, providers []string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (<-chan cliproxyexecutor.StreamChunk, error) { if len(providers) == 0 { - return cliproxyexecutor.Response{}, &Error{Message: "no providers specified for embedding"} - } - - var lastErr error - for _, provider := range providers { - resp, err := m.executeEmbedWithProvider(ctx, provider, req, opts) - if err == nil { - return resp, nil - } - lastErr = err - } - - if lastErr != nil { - return cliproxyexecutor.Response{}, lastErr - } - return cliproxyexecutor.Response{}, &Error{Message: "failed to execute embedding request on all providers"} -} - -func (m *Manager) executeEmbedWithProvider(ctx context.Context, provider string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - if provider == "" { - return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "provider identifier is empty"} - } - tried := make(map[string]struct{}) - var lastErr error - for { - auth, executor, errPick := m.pickNext(ctx, provider, req.Model, opts, tried) - if errPick != nil { - if lastErr != nil { - return cliproxyexecutor.Response{}, lastErr - } - return cliproxyexecutor.Response{}, errPick - } - - accountType, accountInfo := auth.AccountInfo() - if accountType == "api_key" { - log.Debugf("Use API key %s for model %s", util.HideAPIKey(accountInfo), req.Model) - } else if accountType == "oauth" { - log.Debugf("Use OAuth %s for model %s", accountInfo, req.Model) - } - - tried[auth.ID] = struct{}{} - recordSelection(ctx, provider, auth) - execCtx := ctx - if rt := m.roundTripperFor(auth); rt != nil { - execCtx = context.WithValue(execCtx, roundTripperContextKey{}, rt) - execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt) - } - resp, errExec := executor.Embed(execCtx, auth, req, opts) - result := Result{AuthID: auth.ID, Provider: provider, Model: req.Model, Success: errExec == nil} - if errExec != nil { - result.Error = &Error{Message: errExec.Error()} - var se cliproxyexecutor.StatusError - if errors.As(errExec, &se) && se != nil { - result.Error.HTTPStatus = se.StatusCode() - } - if ra := retryAfterFromError(errExec); ra != nil { - result.RetryAfter = ra - } - m.MarkResult(execCtx, result) - lastErr = errExec - continue - } - m.MarkResult(execCtx, result) - return resp, nil - } -} - -func (m *Manager) executeStreamWithProvider(ctx context.Context, provider string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (<-chan cliproxyexecutor.StreamChunk, error) { - if provider == "" { - return nil, &Error{Code: "provider_not_found", Message: "provider identifier is empty"} + return nil, &Error{Code: "provider_not_found", Message: "no provider supplied"} } routeModel := req.Model + opts = ensureRequestedModelMetadata(opts, routeModel) tried := make(map[string]struct{}) var lastErr error for { - auth, executor, errPick := m.pickNext(ctx, provider, routeModel, opts, tried) + auth, executor, provider, errPick := m.pickNextMixed(ctx, providers, routeModel, opts, tried) if errPick != nil { if lastErr != nil { return nil, lastErr @@ -603,16 +689,20 @@ func (m *Manager) executeStreamWithProvider(ctx context.Context, provider string debugLogAuthSelection(entry, auth, provider, req.Model) tried[auth.ID] = struct{}{} - recordSelection(ctx, provider, auth) execCtx := ctx if rt := m.roundTripperFor(auth); rt != nil { execCtx = context.WithValue(execCtx, roundTripperContextKey{}, rt) execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt) } execReq := req - execReq.Model, execReq.Metadata = rewriteModelForAuth(routeModel, req.Metadata, auth) + execReq.Model = rewriteModelForAuth(routeModel, auth) + execReq.Model = m.applyOAuthModelAlias(auth, execReq.Model) + execReq.Model = m.applyAPIKeyModelAlias(auth, execReq.Model) chunks, errStream := executor.ExecuteStream(execCtx, auth, execReq, opts) if errStream != nil { + if errCtx := execCtx.Err(); errCtx != nil { + return nil, errCtx + } rerr := &Error{Message: errStream.Error()} var se cliproxyexecutor.StatusError if errors.As(errStream, &se) && se != nil { @@ -628,6 +718,7 @@ func (m *Manager) executeStreamWithProvider(ctx context.Context, provider string go func(streamCtx context.Context, streamAuth *Auth, streamProvider string, streamChunks <-chan cliproxyexecutor.StreamChunk) { defer close(out) var failed bool + forward := true for chunk := range streamChunks { if chunk.Err != nil && !failed { failed = true @@ -638,7 +729,18 @@ func (m *Manager) executeStreamWithProvider(ctx context.Context, provider string } m.MarkResult(streamCtx, Result{AuthID: streamAuth.ID, Provider: streamProvider, Model: routeModel, Success: false, Error: rerr}) } - out <- chunk + if !forward { + continue + } + if streamCtx == nil { + out <- chunk + continue + } + select { + case <-streamCtx.Done(): + forward = false + case out <- chunk: + } } if !failed { m.MarkResult(streamCtx, Result{AuthID: streamAuth.ID, Provider: streamProvider, Model: routeModel, Success: true}) @@ -648,50 +750,268 @@ func (m *Manager) executeStreamWithProvider(ctx context.Context, provider string } } -func rewriteModelForAuth(model string, metadata map[string]any, auth *Auth) (string, map[string]any) { +func ensureRequestedModelMetadata(opts cliproxyexecutor.Options, requestedModel string) cliproxyexecutor.Options { + requestedModel = strings.TrimSpace(requestedModel) + if requestedModel == "" { + return opts + } + if hasRequestedModelMetadata(opts.Metadata) { + return opts + } + if len(opts.Metadata) == 0 { + opts.Metadata = map[string]any{cliproxyexecutor.RequestedModelMetadataKey: requestedModel} + return opts + } + meta := make(map[string]any, len(opts.Metadata)+1) + for k, v := range opts.Metadata { + meta[k] = v + } + meta[cliproxyexecutor.RequestedModelMetadataKey] = requestedModel + opts.Metadata = meta + return opts +} + +func hasRequestedModelMetadata(meta map[string]any) bool { + if len(meta) == 0 { + return false + } + raw, ok := meta[cliproxyexecutor.RequestedModelMetadataKey] + if !ok || raw == nil { + return false + } + switch v := raw.(type) { + case string: + return strings.TrimSpace(v) != "" + case []byte: + return strings.TrimSpace(string(v)) != "" + default: + return false + } +} + +func rewriteModelForAuth(model string, auth *Auth) string { if auth == nil || model == "" { - return model, metadata + return model } prefix := strings.TrimSpace(auth.Prefix) if prefix == "" { - return model, metadata + return model } needle := prefix + "/" if !strings.HasPrefix(model, needle) { - return model, metadata + return model } - rewritten := strings.TrimPrefix(model, needle) - return rewritten, stripPrefixFromMetadata(metadata, needle) + return strings.TrimPrefix(model, needle) } -func stripPrefixFromMetadata(metadata map[string]any, needle string) map[string]any { - if len(metadata) == 0 || needle == "" { - return metadata +func (m *Manager) applyAPIKeyModelAlias(auth *Auth, requestedModel string) string { + if m == nil || auth == nil { + return requestedModel } - keys := []string{ - util.ThinkingOriginalModelMetadataKey, - util.GeminiOriginalModelMetadataKey, + + kind, _ := auth.AccountInfo() + if !strings.EqualFold(strings.TrimSpace(kind), "api_key") { + return requestedModel } - var out map[string]any - for _, key := range keys { - raw, ok := metadata[key] - if !ok { + + requestedModel = strings.TrimSpace(requestedModel) + if requestedModel == "" { + return requestedModel + } + + // Fast path: lookup per-auth mapping table (keyed by auth.ID). + if resolved := m.lookupAPIKeyUpstreamModel(auth.ID, requestedModel); resolved != "" { + return resolved + } + + // Slow path: scan config for the matching credential entry and resolve alias. + // This acts as a safety net if mappings are stale or auth.ID is missing. + cfg, _ := m.runtimeConfig.Load().(*internalconfig.Config) + if cfg == nil { + cfg = &internalconfig.Config{} + } + + provider := strings.ToLower(strings.TrimSpace(auth.Provider)) + upstreamModel := "" + switch provider { + case "gemini": + upstreamModel = resolveUpstreamModelForGeminiAPIKey(cfg, auth, requestedModel) + case "claude": + upstreamModel = resolveUpstreamModelForClaudeAPIKey(cfg, auth, requestedModel) + case "codex": + upstreamModel = resolveUpstreamModelForCodexAPIKey(cfg, auth, requestedModel) + case "vertex": + upstreamModel = resolveUpstreamModelForVertexAPIKey(cfg, auth, requestedModel) + default: + upstreamModel = resolveUpstreamModelForOpenAICompatAPIKey(cfg, auth, requestedModel) + } + + // Return upstream model if found, otherwise return requested model. + if upstreamModel != "" { + return upstreamModel + } + return requestedModel +} + +// APIKeyConfigEntry is a generic interface for API key configurations. +type APIKeyConfigEntry interface { + GetAPIKey() string + GetBaseURL() string +} + +func resolveAPIKeyConfig[T APIKeyConfigEntry](entries []T, auth *Auth) *T { + if auth == nil || len(entries) == 0 { + return nil + } + attrKey, attrBase := "", "" + if auth.Attributes != nil { + attrKey = strings.TrimSpace(auth.Attributes["api_key"]) + attrBase = strings.TrimSpace(auth.Attributes["base_url"]) + } + for i := range entries { + entry := &entries[i] + cfgKey := strings.TrimSpace((*entry).GetAPIKey()) + cfgBase := strings.TrimSpace((*entry).GetBaseURL()) + if attrKey != "" && attrBase != "" { + if strings.EqualFold(cfgKey, attrKey) && strings.EqualFold(cfgBase, attrBase) { + return entry + } continue } - value, okStr := raw.(string) - if !okStr || !strings.HasPrefix(value, needle) { - continue - } - if out == nil { - out = make(map[string]any, len(metadata)) - for k, v := range metadata { - out[k] = v + if attrKey != "" && strings.EqualFold(cfgKey, attrKey) { + if cfgBase == "" || strings.EqualFold(cfgBase, attrBase) { + return entry } } - out[key] = strings.TrimPrefix(value, needle) + if attrKey == "" && attrBase != "" && strings.EqualFold(cfgBase, attrBase) { + return entry + } } - if out == nil { - return metadata + if attrKey != "" { + for i := range entries { + entry := &entries[i] + if strings.EqualFold(strings.TrimSpace((*entry).GetAPIKey()), attrKey) { + return entry + } + } + } + return nil +} + +func resolveGeminiAPIKeyConfig(cfg *internalconfig.Config, auth *Auth) *internalconfig.GeminiKey { + if cfg == nil { + return nil + } + return resolveAPIKeyConfig(cfg.GeminiKey, auth) +} + +func resolveClaudeAPIKeyConfig(cfg *internalconfig.Config, auth *Auth) *internalconfig.ClaudeKey { + if cfg == nil { + return nil + } + return resolveAPIKeyConfig(cfg.ClaudeKey, auth) +} + +func resolveCodexAPIKeyConfig(cfg *internalconfig.Config, auth *Auth) *internalconfig.CodexKey { + if cfg == nil { + return nil + } + return resolveAPIKeyConfig(cfg.CodexKey, auth) +} + +func resolveVertexAPIKeyConfig(cfg *internalconfig.Config, auth *Auth) *internalconfig.VertexCompatKey { + if cfg == nil { + return nil + } + return resolveAPIKeyConfig(cfg.VertexCompatAPIKey, auth) +} + +func resolveUpstreamModelForGeminiAPIKey(cfg *internalconfig.Config, auth *Auth, requestedModel string) string { + entry := resolveGeminiAPIKeyConfig(cfg, auth) + if entry == nil { + return "" + } + return resolveModelAliasFromConfigModels(requestedModel, asModelAliasEntries(entry.Models)) +} + +func resolveUpstreamModelForClaudeAPIKey(cfg *internalconfig.Config, auth *Auth, requestedModel string) string { + entry := resolveClaudeAPIKeyConfig(cfg, auth) + if entry == nil { + return "" + } + return resolveModelAliasFromConfigModels(requestedModel, asModelAliasEntries(entry.Models)) +} + +func resolveUpstreamModelForCodexAPIKey(cfg *internalconfig.Config, auth *Auth, requestedModel string) string { + entry := resolveCodexAPIKeyConfig(cfg, auth) + if entry == nil { + return "" + } + return resolveModelAliasFromConfigModels(requestedModel, asModelAliasEntries(entry.Models)) +} + +func resolveUpstreamModelForVertexAPIKey(cfg *internalconfig.Config, auth *Auth, requestedModel string) string { + entry := resolveVertexAPIKeyConfig(cfg, auth) + if entry == nil { + return "" + } + return resolveModelAliasFromConfigModels(requestedModel, asModelAliasEntries(entry.Models)) +} + +func resolveUpstreamModelForOpenAICompatAPIKey(cfg *internalconfig.Config, auth *Auth, requestedModel string) string { + providerKey := "" + compatName := "" + if auth != nil && len(auth.Attributes) > 0 { + providerKey = strings.TrimSpace(auth.Attributes["provider_key"]) + compatName = strings.TrimSpace(auth.Attributes["compat_name"]) + } + if compatName == "" && !strings.EqualFold(strings.TrimSpace(auth.Provider), "openai-compatibility") { + return "" + } + entry := resolveOpenAICompatConfig(cfg, providerKey, compatName, auth.Provider) + if entry == nil { + return "" + } + return resolveModelAliasFromConfigModels(requestedModel, asModelAliasEntries(entry.Models)) +} + +type apiKeyModelAliasTable map[string]map[string]string + +func resolveOpenAICompatConfig(cfg *internalconfig.Config, providerKey, compatName, authProvider string) *internalconfig.OpenAICompatibility { + if cfg == nil { + return nil + } + candidates := make([]string, 0, 3) + if v := strings.TrimSpace(compatName); v != "" { + candidates = append(candidates, v) + } + if v := strings.TrimSpace(providerKey); v != "" { + candidates = append(candidates, v) + } + if v := strings.TrimSpace(authProvider); v != "" { + candidates = append(candidates, v) + } + for i := range cfg.OpenAICompatibility { + compat := &cfg.OpenAICompatibility[i] + for _, candidate := range candidates { + if candidate != "" && strings.EqualFold(strings.TrimSpace(candidate), compat.Name) { + return compat + } + } + } + return nil +} + +func asModelAliasEntries[T interface { + GetName() string + GetAlias() string +}](models []T) []modelAliasEntry { + if len(models) == 0 { + return nil + } + out := make([]modelAliasEntry, 0, len(models)) + for i := range models { + out = append(out, models[i]) } return out } @@ -716,60 +1036,52 @@ func (m *Manager) normalizeProviders(providers []string) []string { return result } -// rotateProviders returns a rotated view of the providers list starting from the -// current offset for the model, and atomically increments the offset for the next call. -// This ensures concurrent requests get different starting providers. -// For gemini-3 models with both antigravity and gemini-cli, order is preserved. +// 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 nil + return providers } - // For gemini-3 models with both providers, keep antigravity primary - if shouldKeepProviderOrder(model, providers) { + if len(providers) == 1 { return providers } - // Atomic read-and-increment: get current offset and advance cursor in one lock + // Special case: gemini-3 models with antigravity and gemini-cli should keep stable order + if strings.Contains(strings.ToLower(model), "gemini-3") { + hasAntigravity := false + hasGeminiCLI := false + for _, p := range providers { + pLower := strings.ToLower(p) + if pLower == "antigravity" { + hasAntigravity = true + } + if pLower == "gemini-cli" { + hasGeminiCLI = true + } + } + if hasAntigravity && hasGeminiCLI { + // Keep stable order for gemini-3 models + return providers + } + } + + // Apply round-robin rotation m.mu.Lock() offset := m.providerOffsets[model] m.providerOffsets[model] = (offset + 1) % len(providers) m.mu.Unlock() - if len(providers) > 0 { - offset %= len(providers) - } - if offset < 0 { - offset = 0 - } if offset == 0 { return providers } - rotated := make([]string, 0, len(providers)) - rotated = append(rotated, providers[offset:]...) - rotated = append(rotated, providers[:offset]...) - return rotated -} -func shouldKeepProviderOrder(model string, providers []string) bool { - if len(providers) < 2 { - return false + // Rotate the providers slice + result := make([]string, len(providers)) + for i, p := range providers { + result[(i-offset+len(providers))%len(providers)] = p } - normalizedModel := strings.ToLower(strings.TrimSpace(model)) - if !strings.HasPrefix(normalizedModel, "gemini-3-") { - return false - } - hasAntigravity := false - hasGeminiCLI := false - for _, p := range providers { - switch strings.ToLower(strings.TrimSpace(p)) { - case "antigravity": - hasAntigravity = true - case "gemini-cli": - hasGeminiCLI = true - } - } - // When both providers exist, antigravity should remain primary and gemini-cli should be fallback. - return hasAntigravity && hasGeminiCLI + return result } func (m *Manager) retrySettings() (int, time.Duration) { @@ -779,11 +1091,15 @@ func (m *Manager) retrySettings() (int, time.Duration) { return int(m.requestRetry.Load()), time.Duration(m.maxRetryInterval.Load()) } -func (m *Manager) closestCooldownWait(providers []string, model string) (time.Duration, bool) { +func (m *Manager) closestCooldownWait(providers []string, model string, attempt int) (time.Duration, bool) { if m == nil || len(providers) == 0 { return 0, false } now := time.Now() + defaultRetry := int(m.requestRetry.Load()) + if defaultRetry < 0 { + defaultRetry = 0 + } providerSet := make(map[string]struct{}, len(providers)) for i := range providers { key := strings.TrimSpace(strings.ToLower(providers[i])) @@ -806,6 +1122,16 @@ func (m *Manager) closestCooldownWait(providers []string, model string) (time.Du if _, ok := providerSet[providerKey]; !ok { continue } + effectiveRetry := defaultRetry + if override, ok := auth.RequestRetryOverride(); ok { + effectiveRetry = override + } + if effectiveRetry < 0 { + effectiveRetry = 0 + } + if attempt >= effectiveRetry { + continue + } blocked, reason, next := isAuthBlockedForModel(auth, model, now) if !blocked || next.IsZero() || reason == blockReasonDisabled { continue @@ -822,8 +1148,8 @@ func (m *Manager) closestCooldownWait(providers []string, model string) (time.Du return minWait, found } -func (m *Manager) shouldRetryAfterError(err error, attempt, maxAttempts int, providers []string, model string, maxWait time.Duration) (time.Duration, bool) { - if err == nil || attempt >= maxAttempts-1 { +func (m *Manager) shouldRetryAfterError(err error, attempt int, providers []string, model string, maxWait time.Duration) (time.Duration, bool) { + if err == nil { return 0, false } if maxWait <= 0 { @@ -832,7 +1158,7 @@ func (m *Manager) shouldRetryAfterError(err error, attempt, maxAttempts int, pro if status := statusCodeFromError(err); status == http.StatusOK { return 0, false } - wait, found := m.closestCooldownWait(providers, model) + wait, found := m.closestCooldownWait(providers, model, attempt) if !found || wait > maxWait { return 0, false } @@ -853,42 +1179,6 @@ func waitForCooldown(ctx context.Context, wait time.Duration) error { } } -func (m *Manager) executeProvidersOnce(ctx context.Context, providers []string, fn func(context.Context, string) (cliproxyexecutor.Response, error)) (cliproxyexecutor.Response, error) { - if len(providers) == 0 { - return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "no provider supplied"} - } - var lastErr error - for _, provider := range providers { - resp, errExec := fn(ctx, provider) - if errExec == nil { - return resp, nil - } - lastErr = errExec - } - if lastErr != nil { - return cliproxyexecutor.Response{}, lastErr - } - return cliproxyexecutor.Response{}, &Error{Code: "auth_not_found", Message: "no auth available"} -} - -func (m *Manager) executeStreamProvidersOnce(ctx context.Context, providers []string, fn func(context.Context, string) (<-chan cliproxyexecutor.StreamChunk, error)) (<-chan cliproxyexecutor.StreamChunk, error) { - if len(providers) == 0 { - return nil, &Error{Code: "provider_not_found", Message: "no provider supplied"} - } - var lastErr error - for _, provider := range providers { - chunks, errExec := fn(ctx, provider) - if errExec == nil { - return chunks, nil - } - lastErr = errExec - } - if lastErr != nil { - return nil, lastErr - } - return nil, &Error{Code: "auth_not_found", Message: "no auth available"} -} - // MarkResult records an execution result and notifies hooks. func (m *Manager) MarkResult(ctx context.Context, result Result) { if result.AuthID == "" { @@ -906,25 +1196,6 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { now := time.Now() if result.Success { - // Track usage stats on successful requests - if result.Usage != nil { - auth.Usage.TotalInputTokens += result.Usage.InputTokens - auth.Usage.TotalOutputTokens += result.Usage.OutputTokens - auth.Usage.RequestCount++ - auth.Usage.LastRequestAt = now - - // Reset daily if past configured reset hour (default: midnight UTC) - if m.shouldResetDailyUsage(auth.Usage.DayStartedAt, now) { - auth.Usage.DailyInputTokens = 0 - auth.Usage.DailyOutputTokens = 0 - auth.Usage.DailyRequestCount = 0 - auth.Usage.DayStartedAt = m.getDayStartTime(now) - } - auth.Usage.DailyInputTokens += result.Usage.InputTokens - auth.Usage.DailyOutputTokens += result.Usage.OutputTokens - auth.Usage.DailyRequestCount++ - } - if result.Model != "" { state := ensureModelState(auth, result.Model) resetModelState(state, now) @@ -976,7 +1247,7 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { if result.RetryAfter != nil { next = now.Add(*result.RetryAfter) } else { - cooldown, nextLevel := nextQuotaCooldown(backoffLevel) + cooldown, nextLevel := nextQuotaCooldown(backoffLevel, quotaCooldownDisabledForAuth(auth)) if cooldown > 0 { next = now.Add(cooldown) } @@ -993,8 +1264,12 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { shouldSuspendModel = true setModelQuota = true case 408, 500, 502, 503, 504: - next := now.Add(1 * time.Minute) - state.NextRetryAfter = next + if quotaCooldownDisabledForAuth(auth) { + state.NextRetryAfter = time.Time{} + } else { + next := now.Add(1 * time.Minute) + state.NextRetryAfter = next + } default: state.NextRetryAfter = time.Time{} } @@ -1003,11 +1278,6 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { auth.UpdatedAt = now updateAggregatedAvailability(auth, now) } else { - if result.Model != "" { - state := ensureModelState(auth, result.Model) - applyModelStateFailure(state, result.Error, result.RetryAfter, now) - updateAggregatedAvailability(auth, now) - } applyAuthFailureState(auth, result.Error, result.RetryAfter, now) } } @@ -1031,56 +1301,6 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { m.hook.OnResult(ctx, result) } -func applyModelStateFailure(state *ModelState, resultErr *Error, retryAfter *time.Duration, now time.Time) { - if state == nil { - return - } - state.Unavailable = true - state.Status = StatusError - state.UpdatedAt = now - if resultErr != nil { - state.LastError = cloneError(resultErr) - if resultErr.Message != "" { - state.StatusMessage = resultErr.Message - } - } - statusCode := statusCodeFromResult(resultErr) - switch statusCode { - case 401: - state.StatusMessage = "unauthorized" - state.NextRetryAfter = now.Add(30 * time.Minute) - case 402, 403: - state.StatusMessage = "payment_required" - state.NextRetryAfter = now.Add(30 * time.Minute) - case 404: - state.StatusMessage = "not_found" - state.NextRetryAfter = now.Add(12 * time.Hour) - case 429: - state.StatusMessage = "quota exhausted" - state.Quota.Exceeded = true - state.Quota.Reason = "quota" - var next time.Time - if retryAfter != nil { - next = now.Add(*retryAfter) - } else { - cooldown, nextLevel := nextQuotaCooldown(state.Quota.BackoffLevel) - if cooldown > 0 { - next = now.Add(cooldown) - } - state.Quota.BackoffLevel = nextLevel - } - state.Quota.NextRecoverAt = next - state.NextRetryAfter = next - case 408, 500, 502, 503, 504: - state.StatusMessage = "transient upstream error" - state.NextRetryAfter = now.Add(1 * time.Minute) - default: - if state.StatusMessage == "" { - state.StatusMessage = "request failed" - } - } -} - func ensureModelState(auth *Auth, model string) *ModelState { if auth == nil || model == "" { return nil @@ -1290,7 +1510,7 @@ func applyAuthFailureState(auth *Auth, resultErr *Error, retryAfter *time.Durati if retryAfter != nil { next = now.Add(*retryAfter) } else { - cooldown, nextLevel := nextQuotaCooldown(auth.Quota.BackoffLevel) + cooldown, nextLevel := nextQuotaCooldown(auth.Quota.BackoffLevel, quotaCooldownDisabledForAuth(auth)) if cooldown > 0 { next = now.Add(cooldown) } @@ -1300,7 +1520,11 @@ func applyAuthFailureState(auth *Auth, resultErr *Error, retryAfter *time.Durati auth.NextRetryAfter = next case 408, 500, 502, 503, 504: auth.StatusMessage = "transient upstream error" - auth.NextRetryAfter = now.Add(1 * time.Minute) + if quotaCooldownDisabledForAuth(auth) { + auth.NextRetryAfter = time.Time{} + } else { + auth.NextRetryAfter = now.Add(1 * time.Minute) + } default: if auth.StatusMessage == "" { auth.StatusMessage = "request failed" @@ -1309,11 +1533,11 @@ func applyAuthFailureState(auth *Auth, resultErr *Error, retryAfter *time.Durati } // nextQuotaCooldown returns the next cooldown duration and updated backoff level for repeated quota errors. -func nextQuotaCooldown(prevLevel int) (time.Duration, int) { +func nextQuotaCooldown(prevLevel int, disableCooling bool) (time.Duration, int) { if prevLevel < 0 { prevLevel = 0 } - if quotaCooldownDisabled.Load() { + if disableCooling { return 0, prevLevel } cooldown := quotaBackoffBase * time.Duration(1< refreshCheckInterval { + interval = refreshCheckInterval + } else { interval = refreshCheckInterval } if m.refreshCancel != nil { @@ -1501,12 +1811,6 @@ func (m *Manager) shouldRefresh(a *Auth, now time.Time) bool { return evaluator.ShouldRefresh(now, a) } - // Check if token expires within buffer (configurable, default 5 minutes) - refreshBuffer := m.getAutoRefreshBuffer() - if !a.TokenExpiresAt.IsZero() && a.TokenExpiresAt.Sub(now) < refreshBuffer { - return true - } - lastRefresh := a.LastRefreshedAt if lastRefresh.IsZero() { if ts, ok := authLastRefreshTimestamp(a); ok { @@ -1551,52 +1855,6 @@ func (m *Manager) shouldRefresh(a *Auth, now time.Time) bool { return true } -func (m *Manager) getAutoRefreshBuffer() time.Duration { - if m == nil { - return 5 * time.Minute - } - d := time.Duration(m.autoRefreshBuffer.Load()) - if d <= 0 { - return 5 * time.Minute // default - } - return d -} - -func (m *Manager) getDailyResetHour() int { - if m == nil { - return 0 - } - h := int(m.dailyResetHour.Load()) - if h < 0 || h > 23 { - return 0 - } - return h -} - -// shouldResetDailyUsage checks if daily usage stats should be reset based on configured reset hour. -func (m *Manager) shouldResetDailyUsage(dayStartedAt, now time.Time) bool { - if dayStartedAt.IsZero() { - return true - } - resetHour := m.getDailyResetHour() - // Calculate next reset time from dayStartedAt - nextReset := time.Date(dayStartedAt.Year(), dayStartedAt.Month(), dayStartedAt.Day(), resetHour, 0, 0, 0, time.UTC) - if !nextReset.After(dayStartedAt) { - nextReset = nextReset.Add(24 * time.Hour) - } - return now.After(nextReset) || now.Equal(nextReset) -} - -// getDayStartTime returns the start time for the current usage day based on configured reset hour. -func (m *Manager) getDayStartTime(now time.Time) time.Time { - resetHour := m.getDailyResetHour() - dayStart := time.Date(now.Year(), now.Month(), now.Day(), resetHour, 0, 0, 0, time.UTC) - if dayStart.After(now) { - dayStart = dayStart.Add(-24 * time.Hour) - } - return dayStart -} - func authPreferredInterval(a *Auth) time.Duration { if a == nil { return 0 @@ -1763,6 +2021,9 @@ func (m *Manager) markRefreshPending(id string, now time.Time) bool { } func (m *Manager) refreshAuth(ctx context.Context, id string) { + if ctx == nil { + ctx = context.Background() + } m.mu.RLock() auth := m.auths[id] var exec ProviderExecutor @@ -1775,6 +2036,10 @@ func (m *Manager) refreshAuth(ctx context.Context, id string) { } cloned := auth.Clone() updated, err := exec.Refresh(ctx, cloned) + if err != nil && errors.Is(err, context.Canceled) { + log.Debugf("refresh canceled for %s, %s", auth.Provider, auth.ID) + return + } log.Debugf("refreshed %s, %s, %v", auth.Provider, auth.ID, err) now := time.Now() if err != nil { @@ -1796,9 +2061,7 @@ func (m *Manager) refreshAuth(ctx context.Context, id string) { updated.Runtime = auth.Runtime } updated.LastRefreshedAt = now - // Preserve NextRefreshAfter set by the Authenticator - // If the Authenticator set a reasonable refresh time, it should not be overwritten - // If the Authenticator did not set it (zero value), shouldRefresh will use default logic + updated.NextRefreshAfter = time.Time{} updated.LastError = nil updated.UpdatedAt = now _, _ = m.Update(ctx, updated) @@ -1889,7 +2152,6 @@ func formatOauthIdentity(auth *Auth, provider string, accountInfo string) string if auth == nil { return "" } - authIndex := auth.EnsureIndex() // Prefer the auth's provider when available. providerName := strings.TrimSpace(auth.Provider) if providerName == "" { @@ -1911,16 +2173,10 @@ func formatOauthIdentity(auth *Auth, provider string, accountInfo string) string if authFile != "" { parts = append(parts, "auth_file="+authFile) } - if authIndex != "" { - parts = append(parts, "auth_index="+authIndex) - } if len(parts) == 0 { return accountInfo } - if accountInfo == "" { - return strings.Join(parts, " ") - } - return strings.Join(parts, " ") + " account=" + strconv.Quote(accountInfo) + return strings.Join(parts, " ") } // InjectCredentials delegates per-provider HTTP request preparation when supported. diff --git a/sdk/cliproxy/auth/selector.go b/sdk/cliproxy/auth/selector.go index 47ebab53..7febf219 100644 --- a/sdk/cliproxy/auth/selector.go +++ b/sdk/cliproxy/auth/selector.go @@ -8,6 +8,7 @@ import ( "net/http" "sort" "strconv" + "strings" "sync" "time" @@ -25,10 +26,6 @@ type RoundRobinSelector struct { // rolling-window subscription caps (e.g. chat message limits). type FillFirstSelector struct{} -// UsageAwareSelector selects the credential with the lowest daily usage. -// This spreads load across accounts to avoid hitting quota limits. -type UsageAwareSelector struct{} - type blockReason int const ( @@ -107,13 +104,29 @@ func (e *modelCooldownError) Headers() http.Header { return headers } -func collectAvailable(auths []*Auth, model string, now time.Time) (available []*Auth, cooldownCount int, earliest time.Time) { - available = make([]*Auth, 0, len(auths)) +func authPriority(auth *Auth) int { + if auth == nil || auth.Attributes == nil { + return 0 + } + raw := strings.TrimSpace(auth.Attributes["priority"]) + if raw == "" { + return 0 + } + parsed, err := strconv.Atoi(raw) + if err != nil { + return 0 + } + return parsed +} + +func collectAvailableByPriority(auths []*Auth, model string, now time.Time) (available map[int][]*Auth, cooldownCount int, earliest time.Time) { + available = make(map[int][]*Auth) for i := 0; i < len(auths); i++ { candidate := auths[i] blocked, reason, next := isAuthBlockedForModel(candidate, model, now) if !blocked { - available = append(available, candidate) + priority := authPriority(candidate) + available[priority] = append(available[priority], candidate) continue } if reason == blockReasonCooldown { @@ -123,14 +136,6 @@ func collectAvailable(auths []*Auth, model string, now time.Time) (available []* } } } - if len(available) > 1 { - sort.Slice(available, func(i, j int) bool { - if available[i].Priority != available[j].Priority { - return available[i].Priority < available[j].Priority - } - return available[i].ID < available[j].ID // stable secondary sort - }) - } return available, cooldownCount, earliest } @@ -139,18 +144,35 @@ func getAvailableAuths(auths []*Auth, provider, model string, now time.Time) ([] return nil, &Error{Code: "auth_not_found", Message: "no auth candidates"} } - available, cooldownCount, earliest := collectAvailable(auths, model, now) - if len(available) == 0 { + availableByPriority, cooldownCount, earliest := collectAvailableByPriority(auths, model, now) + if len(availableByPriority) == 0 { if cooldownCount == len(auths) && !earliest.IsZero() { + providerForError := provider + if providerForError == "mixed" { + providerForError = "" + } resetIn := earliest.Sub(now) if resetIn < 0 { resetIn = 0 } - return nil, newModelCooldownError(model, provider, resetIn) + return nil, newModelCooldownError(model, providerForError, resetIn) } return nil, &Error{Code: "auth_unavailable", Message: "no auth available"} } + bestPriority := 0 + found := false + for priority := range availableByPriority { + if !found || priority > bestPriority { + bestPriority = priority + found = true + } + } + + available := availableByPriority[bestPriority] + if len(available) > 1 { + sort.Slice(available, func(i, j int) bool { return available[i].ID < available[j].ID }) + } return available, nil } @@ -192,29 +214,6 @@ func (s *FillFirstSelector) Pick(ctx context.Context, provider, model string, op return available[0], nil } -// Pick selects the auth with the lowest daily output token usage. -func (s *UsageAwareSelector) Pick(ctx context.Context, provider, model string, opts cliproxyexecutor.Options, auths []*Auth) (*Auth, error) { - _ = ctx - _ = opts - now := time.Now() - available, err := getAvailableAuths(auths, provider, model, now) - if err != nil { - return nil, err - } - // Sort by daily output tokens ascending (least usage first) - sort.Slice(available, func(i, j int) bool { - if available[i].Usage.DailyOutputTokens != available[j].Usage.DailyOutputTokens { - return available[i].Usage.DailyOutputTokens < available[j].Usage.DailyOutputTokens - } - // Tie-breaker: priority, then ID - if available[i].Priority != available[j].Priority { - return available[i].Priority < available[j].Priority - } - return available[i].ID < available[j].ID - }) - return available[0], nil -} - func isAuthBlockedForModel(auth *Auth, model string, now time.Time) (bool, blockReason, time.Time) { if auth == nil { return true, blockReasonOther, time.Time{} diff --git a/sdk/cliproxy/auth/selector_test.go b/sdk/cliproxy/auth/selector_test.go index 2dd74bdc..91a7ed14 100644 --- a/sdk/cliproxy/auth/selector_test.go +++ b/sdk/cliproxy/auth/selector_test.go @@ -5,6 +5,7 @@ import ( "errors" "sync" "testing" + "time" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" ) @@ -56,6 +57,69 @@ func TestRoundRobinSelectorPick_CyclesDeterministic(t *testing.T) { } } +func TestRoundRobinSelectorPick_PriorityBuckets(t *testing.T) { + t.Parallel() + + selector := &RoundRobinSelector{} + auths := []*Auth{ + {ID: "c", Attributes: map[string]string{"priority": "0"}}, + {ID: "a", Attributes: map[string]string{"priority": "10"}}, + {ID: "b", Attributes: map[string]string{"priority": "10"}}, + } + + want := []string{"a", "b", "a", "b"} + for i, id := range want { + got, err := selector.Pick(context.Background(), "mixed", "", cliproxyexecutor.Options{}, auths) + if err != nil { + t.Fatalf("Pick() #%d error = %v", i, err) + } + if got == nil { + t.Fatalf("Pick() #%d auth = nil", i) + } + if got.ID != id { + t.Fatalf("Pick() #%d auth.ID = %q, want %q", i, got.ID, id) + } + if got.ID == "c" { + t.Fatalf("Pick() #%d unexpectedly selected lower priority auth", i) + } + } +} + +func TestFillFirstSelectorPick_PriorityFallbackCooldown(t *testing.T) { + t.Parallel() + + selector := &FillFirstSelector{} + now := time.Now() + model := "test-model" + + high := &Auth{ + ID: "high", + Attributes: map[string]string{"priority": "10"}, + ModelStates: map[string]*ModelState{ + model: { + Status: StatusActive, + Unavailable: true, + NextRetryAfter: now.Add(30 * time.Minute), + Quota: QuotaState{ + Exceeded: true, + }, + }, + }, + } + low := &Auth{ID: "low", Attributes: map[string]string{"priority": "0"}} + + got, err := selector.Pick(context.Background(), "mixed", model, cliproxyexecutor.Options{}, []*Auth{high, low}) + if err != nil { + t.Fatalf("Pick() error = %v", err) + } + if got == nil { + t.Fatalf("Pick() auth = nil") + } + if got.ID != "low" { + t.Fatalf("Pick() auth.ID = %q, want %q", got.ID, "low") + } +} + func TestRoundRobinSelectorPick_Concurrent(t *testing.T) { selector := &RoundRobinSelector{} auths := []*Auth{ @@ -111,46 +175,3 @@ func TestRoundRobinSelectorPick_Concurrent(t *testing.T) { default: } } - -func TestUsageAwareSelectorPick_LeastUsedFirst(t *testing.T) { - t.Parallel() - - selector := &UsageAwareSelector{} - auths := []*Auth{ - {ID: "high", Usage: UsageStats{DailyOutputTokens: 1000}}, - {ID: "low", Usage: UsageStats{DailyOutputTokens: 100}}, - {ID: "mid", Usage: UsageStats{DailyOutputTokens: 500}}, - } - - got, err := selector.Pick(context.Background(), "gemini", "", cliproxyexecutor.Options{}, auths) - if err != nil { - t.Fatalf("Pick() error = %v", err) - } - if got == nil { - t.Fatalf("Pick() auth = nil") - } - if got.ID != "low" { - t.Fatalf("Pick() auth.ID = %q, want %q (lowest usage)", got.ID, "low") - } -} - -func TestUsageAwareSelectorPick_TiebreakByPriority(t *testing.T) { - t.Parallel() - - selector := &UsageAwareSelector{} - auths := []*Auth{ - {ID: "b", Priority: 2, Usage: UsageStats{DailyOutputTokens: 100}}, - {ID: "a", Priority: 1, Usage: UsageStats{DailyOutputTokens: 100}}, - } - - got, err := selector.Pick(context.Background(), "gemini", "", cliproxyexecutor.Options{}, auths) - if err != nil { - t.Fatalf("Pick() error = %v", err) - } - if got == nil { - t.Fatalf("Pick() auth = nil") - } - if got.ID != "a" { - t.Fatalf("Pick() auth.ID = %q, want %q (lower priority wins on tie)", got.ID, "a") - } -} diff --git a/sdk/cliproxy/auth/types.go b/sdk/cliproxy/auth/types.go index 98042a72..7be7a1b5 100644 --- a/sdk/cliproxy/auth/types.go +++ b/sdk/cliproxy/auth/types.go @@ -12,24 +12,6 @@ import ( baseauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth" ) -// UsageStats tracks token consumption per auth. -type UsageStats struct { - TotalInputTokens int64 `json:"total_input_tokens"` - TotalOutputTokens int64 `json:"total_output_tokens"` - RequestCount int64 `json:"request_count"` - LastRequestAt time.Time `json:"last_request_at,omitempty"` - DailyInputTokens int64 `json:"daily_input_tokens"` - DailyOutputTokens int64 `json:"daily_output_tokens"` - DailyRequestCount int64 `json:"daily_request_count"` - DayStartedAt time.Time `json:"day_started_at,omitempty"` -} - -// ResultUsage carries token counts from a completed request. -type ResultUsage struct { - InputTokens int64 `json:"input_tokens"` - OutputTokens int64 `json:"output_tokens"` -} - // Auth encapsulates the runtime state and metadata associated with a single credential. type Auth struct { // ID uniquely identifies the auth record across restarts. @@ -46,8 +28,6 @@ type Auth struct { Storage baseauth.TokenStorage `json:"-"` // Label is an optional human readable label for logging. Label string `json:"label,omitempty"` - // Priority determines selection order. Lower = higher priority (0 is highest). - Priority int `json:"priority,omitempty"` // Status is the lifecycle status managed by the AuthManager. Status Status `json:"status"` // StatusMessage holds a short description for the current status. @@ -78,9 +58,13 @@ type Auth struct { NextRetryAfter time.Time `json:"next_retry_after"` // ModelStates tracks per-model runtime availability data. ModelStates map[string]*ModelState `json:"model_states,omitempty"` - // Usage tracks aggregated token consumption statistics. - Usage UsageStats `json:"usage"` - // TokenExpiresAt is the explicit token expiration time for proactive refresh. + + // Priority is the routing priority for this credential (higher = more preferred). + Priority int `json:"priority,omitempty"` + // Usage tracks cumulative usage statistics for this credential. + Usage *AuthUsage `json:"usage,omitempty"` + + // TokenExpiresAt is when the current OAuth token expires. TokenExpiresAt time.Time `json:"token_expires_at,omitempty"` // Runtime carries non-serialisable data used during execution (in-memory only). @@ -89,6 +73,22 @@ type Auth struct { indexAssigned bool `json:"-"` } +// AuthUsage tracks cumulative usage statistics for an auth credential. +type AuthUsage struct { + TotalRequests int64 `json:"total_requests"` + TotalInputs int64 `json:"total_inputs"` + TotalOutputs int64 `json:"total_outputs"` + TotalCacheRead int64 `json:"total_cache_read"` + TotalCost float64 `json:"total_cost"` + LastUpdated string `json:"last_updated"` + TotalInputTokens int64 `json:"total_input_tokens"` + TotalOutputTokens int64 `json:"total_output_tokens"` + RequestCount int64 `json:"request_count"` + DailyInputTokens int64 `json:"daily_input_tokens"` + DailyOutputTokens int64 `json:"daily_output_tokens"` + DailyRequestCount int64 `json:"daily_request_count"` +} + // QuotaState contains limiter tracking data for a credential. type QuotaState struct { // Exceeded indicates the credential recently hit a quota error. diff --git a/sdk/cliproxy/builder.go b/sdk/cliproxy/builder.go index 59ad62f6..5eba18a0 100644 --- a/sdk/cliproxy/builder.go +++ b/sdk/cliproxy/builder.go @@ -207,8 +207,6 @@ func (b *Builder) Build() (*Service, error) { switch strategy { case "fill-first", "fillfirst", "ff": selector = &coreauth.FillFirstSelector{} - case "usage-aware", "usageaware", "usage", "least-used": - selector = &coreauth.UsageAwareSelector{} default: selector = &coreauth.RoundRobinSelector{} } @@ -217,6 +215,8 @@ func (b *Builder) Build() (*Service, error) { } // Attach a default RoundTripper provider so providers can opt-in per-auth transports. coreManager.SetRoundTripperProvider(newDefaultRoundTripperProvider()) + coreManager.SetConfig(b.cfg) + coreManager.SetOAuthModelAlias(b.cfg.OAuthModelAlias) service := &Service{ cfg: b.cfg, diff --git a/sdk/cliproxy/executor/types.go b/sdk/cliproxy/executor/types.go index dba34895..8c11bbc4 100644 --- a/sdk/cliproxy/executor/types.go +++ b/sdk/cliproxy/executor/types.go @@ -1,16 +1,12 @@ package executor import ( - "errors" "net/http" "net/url" sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" ) -// ErrNotImplemented is returned when an executor does not support a particular operation. -var ErrNotImplemented = errors.New("operation not implemented") - // RequestedModelMetadataKey stores the client-requested model name in Options.Metadata. const RequestedModelMetadataKey = "requested_model" diff --git a/sdk/cliproxy/model_registry.go b/sdk/cliproxy/model_registry.go index 44ef8d7d..01cea5b7 100644 --- a/sdk/cliproxy/model_registry.go +++ b/sdk/cliproxy/model_registry.go @@ -5,6 +5,9 @@ import "github.com/router-for-me/CLIProxyAPI/v6/internal/registry" // ModelInfo re-exports the registry model info structure. type ModelInfo = registry.ModelInfo +// ModelRegistryHook re-exports the registry hook interface for external integrations. +type ModelRegistryHook = registry.ModelRegistryHook + // ModelRegistry describes registry operations consumed by external callers. type ModelRegistry interface { RegisterClient(clientID, clientProvider string, models []*ModelInfo) @@ -13,9 +16,15 @@ type ModelRegistry interface { ClearModelQuotaExceeded(clientID, modelID string) ClientSupportsModel(clientID, modelID string) bool GetAvailableModels(handlerType string) []map[string]any + GetAvailableModelsByProvider(provider string) []*ModelInfo } // GlobalModelRegistry returns the shared registry instance. func GlobalModelRegistry() ModelRegistry { return registry.GetGlobalRegistry() } + +// SetGlobalModelRegistryHook registers an optional hook on the shared global registry instance. +func SetGlobalModelRegistryHook(hook ModelRegistryHook) { + registry.GetGlobalRegistry().SetHook(hook) +} diff --git a/sdk/cliproxy/service.go b/sdk/cliproxy/service.go index 9bff0840..ee224db5 100644 --- a/sdk/cliproxy/service.go +++ b/sdk/cliproxy/service.go @@ -13,99 +13,19 @@ import ( "time" "github.com/router-for-me/CLIProxyAPI/v6/internal/api" - "github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware" - "github.com/router-for-me/CLIProxyAPI/v6/internal/cache" - "github.com/router-for-me/CLIProxyAPI/v6/internal/memory" "github.com/router-for-me/CLIProxyAPI/v6/internal/registry" "github.com/router-for-me/CLIProxyAPI/v6/internal/runtime/executor" - _ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator" // Auto-register all translators _ "github.com/router-for-me/CLIProxyAPI/v6/internal/usage" - "github.com/router-for-me/CLIProxyAPI/v6/internal/util" "github.com/router-for-me/CLIProxyAPI/v6/internal/watcher" "github.com/router-for-me/CLIProxyAPI/v6/internal/wsrelay" sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access" sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth" coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" - cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage" "github.com/router-for-me/CLIProxyAPI/v6/sdk/config" log "github.com/sirupsen/logrus" ) -// coreManagerWrapper adapts coreauth.Manager to memory.CoreManagerExecutor interface. -// This allows the summarization executor to route requests through the auth manager -// without importing the cliproxyexecutor package directly in the memory package. -type coreManagerWrapper struct { - manager *coreauth.Manager -} - -// Execute implements memory.CoreManagerExecutor by converting interface{} parameters -// to/from cliproxyexecutor types. -func (w *coreManagerWrapper) Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) (interface{}, error) { - if w.manager == nil { - return nil, errors.New("coreManagerWrapper: manager not configured") - } - - // Convert request from memory.ExecutorRequest to cliproxyexecutor.Request - memReq, ok := req.(memory.ExecutorRequest) - if !ok { - return nil, fmt.Errorf("coreManagerWrapper: expected memory.ExecutorRequest, got %T", req) - } - - coreReq := cliproxyexecutor.Request{ - Model: memReq.Model, - Payload: memReq.Payload, - Metadata: memReq.Metadata, - } - - // Convert options from memory.ExecutorOptions to cliproxyexecutor.Options - var coreOpts cliproxyexecutor.Options - if memOpts, ok := opts.(memory.ExecutorOptions); ok { - coreOpts = cliproxyexecutor.Options{ - Stream: memOpts.Stream, - Headers: memOpts.Headers, - } - } - - // Execute through the real manager - resp, err := w.manager.Execute(ctx, providers, coreReq, coreOpts) - if err != nil { - return nil, err - } - - // Return response as interface{} - the adapter will extract Payload - return resp, nil -} - -// authUsagePlugin bridges usage records to per-auth usage tracking. -// It implements usage.Plugin to update Auth.Usage stats. -type authUsagePlugin struct { - manager *coreauth.Manager -} - -// HandleUsage implements usage.Plugin. -func (p *authUsagePlugin) HandleUsage(ctx context.Context, record usage.Record) { - if p == nil || p.manager == nil { - return - } - authID := strings.TrimSpace(record.AuthID) - if authID == "" { - return - } - result := coreauth.Result{ - AuthID: authID, - Model: record.Model, - Success: !record.Failed, - } - if record.Detail.InputTokens > 0 || record.Detail.OutputTokens > 0 { - result.Usage = &coreauth.ResultUsage{ - InputTokens: record.Detail.InputTokens, - OutputTokens: record.Detail.OutputTokens, - } - } - p.manager.MarkResult(ctx, result) -} - // Service wraps the proxy server lifecycle so external programs can embed the CLI proxy. // It manages the complete lifecycle including authentication, file watching, HTTP server, // and integration with various AI service providers. @@ -204,6 +124,7 @@ func (s *Service) ensureAuthUpdateQueue(ctx context.Context) { } func (s *Service) consumeAuthUpdates(ctx context.Context) { + ctx = coreauth.WithSkipPersist(ctx) for { select { case <-ctx.Done(): @@ -457,12 +378,8 @@ func (s *Service) ensureExecutorsForAuth(a *coreauth.Auth) { s.coreManager.RegisterExecutor(executor.NewCodexExecutor(s.cfg)) case "qwen": s.coreManager.RegisterExecutor(executor.NewQwenExecutor(s.cfg)) - case "kiro": - s.coreManager.RegisterExecutor(executor.NewKiroExecutor(s.cfg)) - case "minimax": - s.coreManager.RegisterExecutor(executor.NewMiniMaxExecutor(s.cfg)) - case "zhipu": - s.coreManager.RegisterExecutor(executor.NewZhipuExecutor(s.cfg)) + case "iflow": + s.coreManager.RegisterExecutor(executor.NewIFlowExecutor(s.cfg)) default: providerKey := strings.ToLower(strings.TrimSpace(a.Provider)) if providerKey == "" { @@ -502,75 +419,6 @@ func (s *Service) Run(ctx context.Context) error { usage.StartDefault(ctx) - // Register auth usage plugin to update per-auth usage stats - if s.coreManager != nil { - usage.RegisterPlugin(&authUsagePlugin{manager: s.coreManager}) - } - - // Initialize response cache from config - if s.cfg != nil && s.cfg.ResponseCache.Enabled { - cacheCfg := cache.ResponseCacheConfig{ - Enabled: s.cfg.ResponseCache.Enabled, - MaxSize: s.cfg.ResponseCache.GetMaxSize(), - TTL: s.cfg.ResponseCache.GetTTL(), - ExcludeModels: s.cfg.ResponseCache.ExcludeModels, - PersistFile: s.cfg.ResponseCache.PersistFile, - } - cache.InitDefaultResponseCache(cacheCfg) - respCache := cache.GetDefaultResponseCache() - - // Load persisted cache from file - if s.cfg.ResponseCache.PersistFile != "" { - if err := respCache.LoadFromFile(s.cfg.ResponseCache.PersistFile); err != nil { - log.Warnf("failed to load response cache from %s: %v", s.cfg.ResponseCache.PersistFile, err) - } - } - - respCache.StartPeriodicEviction(ctx, cache.DefaultEvictionInterval) - - // Start periodic persistence (every 5 minutes) - if s.cfg.ResponseCache.PersistFile != "" { - respCache.StartPeriodicPersistence(ctx, s.cfg.ResponseCache.PersistFile, 5*time.Minute) - } - - log.Infof("response cache enabled: max=%d ttl=%s persist=%s", cacheCfg.MaxSize, cacheCfg.TTL, cacheCfg.PersistFile) - } - - // Initialize prompt cache from config - if s.cfg != nil && s.cfg.PromptCache.Enabled { - promptCacheCfg := cache.PromptCacheConfig{ - Enabled: s.cfg.PromptCache.Enabled, - MaxSize: s.cfg.PromptCache.GetMaxSize(), - TTL: s.cfg.PromptCache.GetTTL(), - PersistFile: s.cfg.PromptCache.PersistFile, - } - cache.InitDefaultPromptCache(promptCacheCfg) - promptCache := cache.GetDefaultPromptCache() - - // Load persisted cache from file - if s.cfg.PromptCache.PersistFile != "" { - if err := promptCache.LoadFromFile(s.cfg.PromptCache.PersistFile); err != nil { - log.Warnf("failed to load prompt cache from %s: %v", s.cfg.PromptCache.PersistFile, err) - } - } - - // Start periodic persistence (every 5 minutes) - if s.cfg.PromptCache.PersistFile != "" { - promptCache.StartPeriodicPersistence(ctx, s.cfg.PromptCache.PersistFile, 5*time.Minute) - } - - // Warm cache from file if configured - if s.cfg.PromptCache.WarmFromFile != "" { - if result, err := promptCache.LoadWarmFile(s.cfg.PromptCache.WarmFromFile); err != nil { - log.Warnf("failed to warm prompt cache from %s: %v", s.cfg.PromptCache.WarmFromFile, err) - } else if result.Added > 0 { - log.Infof("prompt cache warmed from %s: added=%d skipped=%d", s.cfg.PromptCache.WarmFromFile, result.Added, result.Skipped) - } - } - - log.Infof("prompt cache enabled: max=%d ttl=%s persist=%s", promptCacheCfg.MaxSize, promptCacheCfg.TTL, promptCacheCfg.PersistFile) - } - shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second) defer shutdownCancel() defer func() { @@ -612,13 +460,6 @@ func (s *Service) Run(ctx context.Context) error { // handlers no longer depend on legacy clients; pass nil slice initially s.server = api.NewServer(s.cfg, s.coreManager, s.accessManager, s.configPath, s.serverOptions...) - // Wire up LLM summarization for Factory.ai-style context compression - if s.coreManager != nil { - providers := []string{"claude", "codex", "gemini"} - wrapper := &coreManagerWrapper{manager: s.coreManager} - middleware.InitSummarizerWithAuthManager(wrapper, providers) - } - if s.authManager == nil { s.authManager = newDefaultAuthManager() } @@ -687,8 +528,6 @@ func (s *Service) Run(ctx context.Context) error { switch strategy { case "fill-first", "fillfirst", "ff": return "fill-first" - case "usage-aware", "usageaware", "usage", "least-used": - return "usage-aware" default: return "round-robin" } @@ -700,8 +539,6 @@ func (s *Service) Run(ctx context.Context) error { switch nextStrategy { case "fill-first": selector = &coreauth.FillFirstSelector{} - case "usage-aware": - selector = &coreauth.UsageAwareSelector{} default: selector = &coreauth.RoundRobinSelector{} } @@ -716,6 +553,10 @@ func (s *Service) Run(ctx context.Context) error { s.cfgMu.Lock() s.cfg = newCfg s.cfgMu.Unlock() + if s.coreManager != nil { + s.coreManager.SetConfig(newCfg) + s.coreManager.SetOAuthModelAlias(newCfg.OAuthModelAlias) + } s.rebindExecutors() } @@ -772,28 +613,6 @@ func (s *Service) Shutdown(ctx context.Context) error { ctx = context.Background() } - // Save caches before shutdown - if s.cfg != nil { - if s.cfg.ResponseCache.Enabled && s.cfg.ResponseCache.PersistFile != "" { - if respCache := cache.GetDefaultResponseCache(); respCache != nil { - if err := respCache.SaveToFile(s.cfg.ResponseCache.PersistFile); err != nil { - log.Warnf("failed to save response cache on shutdown: %v", err) - } else { - log.Info("response cache saved on shutdown") - } - } - } - if s.cfg.PromptCache.Enabled && s.cfg.PromptCache.PersistFile != "" { - if promptCache := cache.GetDefaultPromptCache(); promptCache != nil { - if err := promptCache.SaveToFile(s.cfg.PromptCache.PersistFile); err != nil { - log.Warnf("failed to save prompt cache on shutdown: %v", err) - } else { - log.Info("prompt cache saved on shutdown") - } - } - } - } - // legacy refresh loop removed; only stopping core auth manager below if s.watcherCancel != nil { @@ -840,17 +659,6 @@ func (s *Service) Shutdown(ctx context.Context) error { } func (s *Service) ensureAuthDir() error { - // Resolve auth-dir to apply default if empty and expand ~ prefix - resolvedAuthDir, errResolve := util.ResolveAuthDir(s.cfg.AuthDir) - if errResolve != nil { - log.Warnf("failed to resolve auth dir %q: %v, using default", s.cfg.AuthDir, errResolve) - resolvedAuthDir = util.DefaultAuthDir() - } - if resolvedAuthDir == "" { - return fmt.Errorf("cliproxy: failed to determine auth directory: LOCALAPPDATA and HOME not available") - } - s.cfg.AuthDir = resolvedAuthDir - info, err := os.Stat(s.cfg.AuthDir) if err != nil { if os.IsNotExist(err) { @@ -873,7 +681,16 @@ func (s *Service) registerModelsForAuth(a *coreauth.Auth) { if a == nil || a.ID == "" { return } + if a.Disabled { + GlobalModelRegistry().UnregisterClient(a.ID) + return + } authKind := strings.ToLower(strings.TrimSpace(a.Attributes["auth_kind"])) + if authKind == "" { + if kind, _ := a.AccountInfo(); strings.EqualFold(kind, "api_key") { + authKind = "apikey" + } + } if a.Attributes != nil { if v := strings.TrimSpace(a.Attributes["gemini_virtual_primary"]); strings.EqualFold(v, "true") { GlobalModelRegistry().UnregisterClient(a.ID) @@ -899,6 +716,9 @@ func (s *Service) registerModelsForAuth(a *coreauth.Auth) { case "gemini": models = registry.GetGeminiModels() if entry := s.resolveConfigGeminiKey(a); entry != nil { + if len(entry.Models) > 0 { + models = buildGeminiConfigModels(entry) + } if authKind == "apikey" { excluded = entry.ExcludedModels } @@ -949,17 +769,8 @@ func (s *Service) registerModelsForAuth(a *coreauth.Auth) { case "qwen": models = registry.GetQwenModels() models = applyExcludedModels(models, excluded) - case "github-copilot": - models = registry.GetGitHubCopilotModels() - models = applyExcludedModels(models, excluded) - case "kiro": - models = registry.GetKiroModels() - models = applyExcludedModels(models, excluded) - case "minimax": - models = registry.GetMiniMaxModels() - models = applyExcludedModels(models, excluded) - case "zhipu": - models = registry.GetZhipuModels() + case "iflow": + models = registry.GetIFlowModels() models = applyExcludedModels(models, excluded) default: // Handle OpenAI-compatibility providers by name using config @@ -1020,6 +831,7 @@ func (s *Service) registerModelsForAuth(a *coreauth.Auth) { OwnedBy: compat.Name, Type: "openai-compatibility", DisplayName: modelID, + UserDefined: true, }) } // Register and return @@ -1042,6 +854,7 @@ func (s *Service) registerModelsForAuth(a *coreauth.Auth) { } } } + models = applyOAuthModelAlias(s.cfg, provider, authKind, models) if len(models) > 0 { key := provider if key == "" { @@ -1313,17 +1126,22 @@ func matchWildcard(pattern, value string) bool { return true } -func buildVertexCompatConfigModels(entry *config.VertexCompatKey) []*ModelInfo { - if entry == nil || len(entry.Models) == 0 { +type modelEntry interface { + GetName() string + GetAlias() string +} + +func buildConfigModels[T modelEntry](models []T, ownedBy, modelType string) []*ModelInfo { + if len(models) == 0 { return nil } now := time.Now().Unix() - out := make([]*ModelInfo, 0, len(entry.Models)) - seen := make(map[string]struct{}, len(entry.Models)) - for i := range entry.Models { - model := entry.Models[i] - name := strings.TrimSpace(model.Name) - alias := strings.TrimSpace(model.Alias) + out := make([]*ModelInfo, 0, len(models)) + seen := make(map[string]struct{}, len(models)) + for i := range models { + model := models[i] + name := strings.TrimSpace(model.GetName()) + alias := strings.TrimSpace(model.GetAlias()) if alias == "" { alias = name } @@ -1339,90 +1157,179 @@ func buildVertexCompatConfigModels(entry *config.VertexCompatKey) []*ModelInfo { if display == "" { display = alias } - out = append(out, &ModelInfo{ + info := &ModelInfo{ ID: alias, Object: "model", Created: now, - OwnedBy: "vertex", - Type: "vertex", + OwnedBy: ownedBy, + Type: modelType, DisplayName: display, - }) + UserDefined: true, + } + if name != "" { + if upstream := registry.LookupStaticModelInfo(name); upstream != nil && upstream.Thinking != nil { + info.Thinking = upstream.Thinking + } + } + out = append(out, info) } return out } +func buildVertexCompatConfigModels(entry *config.VertexCompatKey) []*ModelInfo { + if entry == nil { + return nil + } + return buildConfigModels(entry.Models, "google", "vertex") +} + +func buildGeminiConfigModels(entry *config.GeminiKey) []*ModelInfo { + if entry == nil { + return nil + } + return buildConfigModels(entry.Models, "google", "gemini") +} + func buildClaudeConfigModels(entry *config.ClaudeKey) []*ModelInfo { - if entry == nil || len(entry.Models) == 0 { + if entry == nil { return nil } - now := time.Now().Unix() - out := make([]*ModelInfo, 0, len(entry.Models)) - seen := make(map[string]struct{}, len(entry.Models)) - for i := range entry.Models { - model := entry.Models[i] - name := strings.TrimSpace(model.Name) - alias := strings.TrimSpace(model.Alias) - if alias == "" { - alias = name - } - if alias == "" { - continue - } - key := strings.ToLower(alias) - if _, exists := seen[key]; exists { - continue - } - seen[key] = struct{}{} - display := name - if display == "" { - display = alias - } - out = append(out, &ModelInfo{ - ID: alias, - Object: "model", - Created: now, - OwnedBy: "claude", - Type: "claude", - DisplayName: display, - }) - } - return out + return buildConfigModels(entry.Models, "anthropic", "claude") } func buildCodexConfigModels(entry *config.CodexKey) []*ModelInfo { - if entry == nil || len(entry.Models) == 0 { + if entry == nil { return nil } - now := time.Now().Unix() - out := make([]*ModelInfo, 0, len(entry.Models)) - seen := make(map[string]struct{}, len(entry.Models)) - for i := range entry.Models { - model := entry.Models[i] - name := strings.TrimSpace(model.Name) - alias := strings.TrimSpace(model.Alias) - if alias == "" { - alias = name - } - if alias == "" { + return buildConfigModels(entry.Models, "openai", "openai") +} + +func rewriteModelInfoName(name, oldID, newID string) string { + trimmed := strings.TrimSpace(name) + if trimmed == "" { + return name + } + oldID = strings.TrimSpace(oldID) + newID = strings.TrimSpace(newID) + if oldID == "" || newID == "" { + return name + } + if strings.EqualFold(oldID, newID) { + return name + } + if strings.EqualFold(trimmed, oldID) { + return newID + } + if strings.HasSuffix(trimmed, "/"+oldID) { + prefix := strings.TrimSuffix(trimmed, oldID) + return prefix + newID + } + if trimmed == "models/"+oldID { + return "models/" + newID + } + return name +} + +func applyOAuthModelAlias(cfg *config.Config, provider, authKind string, models []*ModelInfo) []*ModelInfo { + if cfg == nil || len(models) == 0 { + return models + } + channel := coreauth.OAuthModelAliasChannel(provider, authKind) + if channel == "" || len(cfg.OAuthModelAlias) == 0 { + return models + } + aliases := cfg.OAuthModelAlias[channel] + if len(aliases) == 0 { + return models + } + + type aliasEntry struct { + alias string + fork bool + } + + forward := make(map[string][]aliasEntry, len(aliases)) + for i := range aliases { + name := strings.TrimSpace(aliases[i].Name) + alias := strings.TrimSpace(aliases[i].Alias) + if name == "" || alias == "" { continue } - key := strings.ToLower(alias) - if _, exists := seen[key]; exists { + if strings.EqualFold(name, alias) { continue } - seen[key] = struct{}{} - display := name - if display == "" { - display = alias + key := strings.ToLower(name) + forward[key] = append(forward[key], aliasEntry{alias: alias, fork: aliases[i].Fork}) + } + if len(forward) == 0 { + return models + } + + out := make([]*ModelInfo, 0, len(models)) + seen := make(map[string]struct{}, len(models)) + for _, model := range models { + if model == nil { + continue + } + id := strings.TrimSpace(model.ID) + if id == "" { + continue + } + key := strings.ToLower(id) + entries := forward[key] + if len(entries) == 0 { + if _, exists := seen[key]; exists { + continue + } + seen[key] = struct{}{} + out = append(out, model) + continue + } + + keepOriginal := false + for _, entry := range entries { + if entry.fork { + keepOriginal = true + break + } + } + if keepOriginal { + if _, exists := seen[key]; !exists { + seen[key] = struct{}{} + out = append(out, model) + } + } + + addedAlias := false + for _, entry := range entries { + mappedID := strings.TrimSpace(entry.alias) + if mappedID == "" { + continue + } + if strings.EqualFold(mappedID, id) { + continue + } + aliasKey := strings.ToLower(mappedID) + if _, exists := seen[aliasKey]; exists { + continue + } + seen[aliasKey] = struct{}{} + clone := *model + clone.ID = mappedID + if clone.Name != "" { + clone.Name = rewriteModelInfoName(clone.Name, id, mappedID) + } + out = append(out, &clone) + addedAlias = true + } + + if !keepOriginal && !addedAlias { + if _, exists := seen[key]; exists { + continue + } + seen[key] = struct{}{} + out = append(out, model) } - out = append(out, &ModelInfo{ - ID: alias, - Object: "model", - Created: now, - OwnedBy: "openai", - Type: "openai", - DisplayName: display, - }) } return out } diff --git a/sdk/config/config.go b/sdk/config/config.go index 0168917a..304ccdd8 100644 --- a/sdk/config/config.go +++ b/sdk/config/config.go @@ -15,6 +15,8 @@ type Config = internalconfig.Config type StreamingConfig = internalconfig.StreamingConfig type TLSConfig = internalconfig.TLSConfig type RemoteManagement = internalconfig.RemoteManagement +type AmpCode = internalconfig.AmpCode +type OAuthModelAlias = internalconfig.OAuthModelAlias type PayloadConfig = internalconfig.PayloadConfig type PayloadRule = internalconfig.PayloadRule type PayloadModelRule = internalconfig.PayloadModelRule @@ -27,15 +29,13 @@ type VertexCompatModel = internalconfig.VertexCompatModel type OpenAICompatibility = internalconfig.OpenAICompatibility type OpenAICompatibilityAPIKey = internalconfig.OpenAICompatibilityAPIKey type OpenAICompatibilityModel = internalconfig.OpenAICompatibilityModel -type GlobalModelMapping = internalconfig.GlobalModelMapping type TLS = internalconfig.TLSConfig const ( AccessProviderTypeConfigAPIKey = internalconfig.AccessProviderTypeConfigAPIKey DefaultAccessProviderName = internalconfig.DefaultAccessProviderName - DefaultAutoRefreshBuffer = internalconfig.DefaultAutoRefreshBuffer - DefaultDailyResetHour = internalconfig.DefaultDailyResetHour + DefaultPanelGitHubRepository = internalconfig.DefaultPanelGitHubRepository ) func MakeInlineAPIKeyProvider(keys []string) *AccessProvider { diff --git a/sdk/translator/registry.go b/sdk/translator/registry.go index 19019c80..95df31a9 100644 --- a/sdk/translator/registry.go +++ b/sdk/translator/registry.go @@ -2,54 +2,9 @@ package translator import ( "context" - "encoding/json" - "fmt" - "sort" - "strings" "sync" - "sync/atomic" - "time" - - log "github.com/sirupsen/logrus" ) -// translationStats holds atomic counters for translation statistics. -type translationStats struct { - totalCount atomic.Int64 - failedCount atomic.Int64 -} - -// performanceMetrics holds timing data for translation performance. -type performanceMetrics struct { - mu sync.RWMutex - totalTime time.Duration - requestCount int64 -} - -// LazyRequestLoader is a function that returns a RequestTransform when called. -// Used for expensive translator initialization that should be deferred. -type LazyRequestLoader func() RequestTransform - -// LazyResponseLoader is a function that returns a ResponseTransform when called. -// Used for expensive translator initialization that should be deferred. -type LazyResponseLoader func() ResponseTransform - -// lazyRequestEntry holds a loader function and tracks loading state. -type lazyRequestEntry struct { - loader LazyRequestLoader - loaded RequestTransform - isLoaded bool - loadMu sync.Mutex -} - -// lazyResponseEntry holds a loader function and tracks loading state. -type lazyResponseEntry struct { - loader LazyResponseLoader - loaded ResponseTransform - isLoaded bool - loadMu sync.Mutex -} - // Registry manages translation functions across schemas. type Registry struct { mu sync.RWMutex @@ -59,257 +14,34 @@ type Registry struct { // Lazy loading support lazyRequests map[Format]map[Format]*lazyRequestEntry lazyResponses map[Format]map[Format]*lazyResponseEntry - loadedCount atomic.Int64 - lazyCount atomic.Int64 +} - // Observability features - debugMode atomic.Bool - dryRunMode atomic.Bool - statsMu sync.RWMutex - stats map[string]*translationStats // key: "from->to" - perfMu sync.RWMutex - perfMetrics map[string]*performanceMetrics // key: "from->to" +type lazyRequestEntry struct { + loader func() RequestTransform + loaded bool +} + +type lazyResponseEntry struct { + loader func() ResponseTransform + loaded bool +} + +// LazyLoadStats contains statistics about lazy loaded translators. +type LazyLoadStats struct { + TotalLazy int + TotalLoaded int } // NewRegistry constructs an empty translator registry. func NewRegistry() *Registry { return &Registry{ requests: make(map[Format]map[Format]RequestTransform), - responses: make(map[Format]map[Format]ResponseTransform), + responses: make(map[Format]map[Format]ResponseTransform), lazyRequests: make(map[Format]map[Format]*lazyRequestEntry), lazyResponses: make(map[Format]map[Format]*lazyResponseEntry), - stats: make(map[string]*translationStats), - perfMetrics: make(map[string]*performanceMetrics), } } -// pairKey generates a consistent key for a (from, to) translation pair. -func pairKey(from, to Format) string { - return fmt.Sprintf("%s->%s", from, to) -} - -// truncatePayload truncates a byte slice to the specified max length for logging. -func truncatePayload(data []byte, maxLen int) string { - if len(data) <= maxLen { - return string(data) - } - return string(data[:maxLen]) + "...[truncated]" -} - -// SetDebugMode enables or disables debug mode for the registry. -// When enabled, translation methods will log before/after payloads (truncated to 500 chars). -func (r *Registry) SetDebugMode(enabled bool) { - r.debugMode.Store(enabled) - if enabled { - log.Info("translator: debug mode enabled - payloads will be logged") - } else { - log.Info("translator: debug mode disabled") - } -} - -// IsDebugMode returns the current debug mode state. -func (r *Registry) IsDebugMode() bool { - return r.debugMode.Load() -} - -// LogRegisteredTranslators logs all registered translators at INFO level with a count summary, -// and DEBUG level details for each registered pair. -func (r *Registry) LogRegisteredTranslators() { - r.mu.RLock() - defer r.mu.RUnlock() - - requestCount := 0 - responseCount := 0 - var requestPairs []string - var responsePairs []string - - // Count and collect request translators - for from, targets := range r.requests { - for to := range targets { - requestCount++ - requestPairs = append(requestPairs, pairKey(from, to)) - } - } - - // Count and collect response translators - for from, targets := range r.responses { - for to := range targets { - responseCount++ - responsePairs = append(responsePairs, pairKey(from, to)) - } - } - - // Sort for consistent output - sort.Strings(requestPairs) - sort.Strings(responsePairs) - - log.Infof("translator: registered %d request translator(s) and %d response translator(s)", - requestCount, responseCount) - - // Log details at DEBUG level - for _, pair := range requestPairs { - log.Debugf("translator: request translator registered: %s", pair) - } - for _, pair := range responsePairs { - log.Debugf("translator: response translator registered: %s", pair) - } -} - -// getOrCreateStats returns the stats for a pair, creating if necessary. -func (r *Registry) getOrCreateStats(key string) *translationStats { - r.statsMu.RLock() - s, ok := r.stats[key] - r.statsMu.RUnlock() - if ok { - return s - } - - r.statsMu.Lock() - defer r.statsMu.Unlock() - // Double-check after acquiring write lock - if s, ok = r.stats[key]; ok { - return s - } - s = &translationStats{} - r.stats[key] = s - return s -} - -// getOrCreatePerfMetrics returns the performance metrics for a pair, creating if necessary. -func (r *Registry) getOrCreatePerfMetrics(key string) *performanceMetrics { - r.perfMu.RLock() - p, ok := r.perfMetrics[key] - r.perfMu.RUnlock() - if ok { - return p - } - - r.perfMu.Lock() - defer r.perfMu.Unlock() - // Double-check after acquiring write lock - if p, ok = r.perfMetrics[key]; ok { - return p - } - p = &performanceMetrics{} - r.perfMetrics[key] = p - return p -} - -// recordTranslation records a successful translation. -func (r *Registry) recordTranslation(from, to Format, duration time.Duration) { - key := pairKey(from, to) - stats := r.getOrCreateStats(key) - stats.totalCount.Add(1) - - perf := r.getOrCreatePerfMetrics(key) - perf.mu.Lock() - perf.totalTime += duration - perf.requestCount++ - perf.mu.Unlock() -} - -// recordFailedTranslation records a failed translation (no translator found). -func (r *Registry) recordFailedTranslation(from, to Format) { - key := pairKey(from, to) - stats := r.getOrCreateStats(key) - stats.failedCount.Add(1) -} - -// GetTranslationStats returns a map of translation statistics. -// Keys are in the format "from->to:total" and "from->to:failed". -func (r *Registry) GetTranslationStats() map[string]int64 { - r.statsMu.RLock() - defer r.statsMu.RUnlock() - - result := make(map[string]int64) - for key, s := range r.stats { - result[key+":total"] = s.totalCount.Load() - result[key+":failed"] = s.failedCount.Load() - } - return result -} - -// GetPerformanceMetrics returns the average translation latency per (from, to) pair. -func (r *Registry) GetPerformanceMetrics() map[string]time.Duration { - r.perfMu.RLock() - defer r.perfMu.RUnlock() - - result := make(map[string]time.Duration) - for key, p := range r.perfMetrics { - p.mu.RLock() - if p.requestCount > 0 { - result[key] = p.totalTime / time.Duration(p.requestCount) - } else { - result[key] = 0 - } - p.mu.RUnlock() - } - return result -} - -// DiffPayloads compares two JSON payloads and returns a human-readable diff summary -// showing fields added, removed, or changed. -func DiffPayloads(before, after []byte) string { - var beforeMap, afterMap map[string]interface{} - - if err := json.Unmarshal(before, &beforeMap); err != nil { - return fmt.Sprintf("error parsing 'before' payload: %v", err) - } - if err := json.Unmarshal(after, &afterMap); err != nil { - return fmt.Sprintf("error parsing 'after' payload: %v", err) - } - - var added, removed, changed []string - - // Find removed and changed fields - for key, beforeVal := range beforeMap { - afterVal, exists := afterMap[key] - if !exists { - removed = append(removed, key) - } else if !jsonValuesEqual(beforeVal, afterVal) { - changed = append(changed, key) - } - } - - // Find added fields - for key := range afterMap { - if _, exists := beforeMap[key]; !exists { - added = append(added, key) - } - } - - // Sort for consistent output - sort.Strings(added) - sort.Strings(removed) - sort.Strings(changed) - - var parts []string - if len(added) > 0 { - parts = append(parts, fmt.Sprintf("added: [%s]", strings.Join(added, ", "))) - } - if len(removed) > 0 { - parts = append(parts, fmt.Sprintf("removed: [%s]", strings.Join(removed, ", "))) - } - if len(changed) > 0 { - parts = append(parts, fmt.Sprintf("changed: [%s]", strings.Join(changed, ", "))) - } - - if len(parts) == 0 { - return "no changes detected" - } - return strings.Join(parts, "; ") -} - -// jsonValuesEqual compares two JSON values for equality. -func jsonValuesEqual(a, b interface{}) bool { - aJSON, err1 := json.Marshal(a) - bJSON, err2 := json.Marshal(b) - if err1 != nil || err2 != nil { - return false - } - return string(aJSON) == string(bJSON) -} - // Register stores request/response transforms between two formats. func (r *Registry) Register(from, to Format, request RequestTransform, response ResponseTransform) { r.mu.Lock() @@ -328,205 +60,71 @@ func (r *Registry) Register(from, to Format, request RequestTransform, response r.responses[from][to] = response } -// RegisterLazy registers a lazy-loaded request transformer. -// The loader function is called on first use, which is useful for expensive initialization. -func (r *Registry) RegisterLazy(from, to Format, loader LazyRequestLoader) { - r.mu.Lock() - defer r.mu.Unlock() - - if _, ok := r.lazyRequests[from]; !ok { - r.lazyRequests[from] = make(map[Format]*lazyRequestEntry) - } - r.lazyRequests[from][to] = &lazyRequestEntry{ - loader: loader, - } - r.lazyCount.Add(1) -} - -// RegisterLazyResponse registers a lazy-loaded response transformer. -// The loader function is called on first use. -func (r *Registry) RegisterLazyResponse(from, to Format, loader LazyResponseLoader) { - r.mu.Lock() - defer r.mu.Unlock() - - if _, ok := r.lazyResponses[from]; !ok { - r.lazyResponses[from] = make(map[Format]*lazyResponseEntry) - } - r.lazyResponses[from][to] = &lazyResponseEntry{ - loader: loader, - } - r.lazyCount.Add(1) -} - -// RegisterLazyBoth registers both request and response transformers as lazy-loaded. -func (r *Registry) RegisterLazyBoth(from, to Format, reqLoader LazyRequestLoader, respLoader LazyResponseLoader) { - r.RegisterLazy(from, to, reqLoader) - r.RegisterLazyResponse(from, to, respLoader) -} - -// loadLazyRequest loads a lazy request transformer if not already loaded. -func (r *Registry) loadLazyRequest(from, to Format) RequestTransform { - r.mu.RLock() - entry, exists := r.lazyRequests[from][to] - r.mu.RUnlock() - - if !exists || entry == nil { - return nil - } - - entry.loadMu.Lock() - defer entry.loadMu.Unlock() - - if entry.isLoaded { - return entry.loaded - } - - if entry.loader != nil { - entry.loaded = entry.loader() - entry.isLoaded = true - r.loadedCount.Add(1) - log.Debugf("translator: lazy-loaded request transformer for %s -> %s", from, to) - } - - return entry.loaded -} - -// loadLazyResponse loads a lazy response transformer if not already loaded. -func (r *Registry) loadLazyResponse(from, to Format) *ResponseTransform { - r.mu.RLock() - entry, exists := r.lazyResponses[from][to] - r.mu.RUnlock() - - if !exists || entry == nil { - return nil - } - - entry.loadMu.Lock() - defer entry.loadMu.Unlock() - - if entry.isLoaded { - return &entry.loaded - } - - if entry.loader != nil { - entry.loaded = entry.loader() - entry.isLoaded = true - r.loadedCount.Add(1) - log.Debugf("translator: lazy-loaded response transformer for %s -> %s", from, to) - } - - return &entry.loaded -} - -// GetLazyLoadStats returns statistics about lazy loading. -func (r *Registry) GetLazyLoadStats() LazyLoadStats { - return LazyLoadStats{ - TotalLazy: r.lazyCount.Load(), - TotalLoaded: r.loadedCount.Load(), - } -} - -// LazyLoadStats contains statistics about lazy loading. -type LazyLoadStats struct { - TotalLazy int64 `json:"total_lazy"` - TotalLoaded int64 `json:"total_loaded"` -} - -// IsLazyLoaded checks if a lazy transformer has been loaded. -func (r *Registry) IsLazyLoaded(from, to Format) bool { - r.mu.RLock() - defer r.mu.RUnlock() - - if byTarget, ok := r.lazyRequests[from]; ok { - if entry, exists := byTarget[to]; exists { - entry.loadMu.Lock() - defer entry.loadMu.Unlock() - return entry.isLoaded - } - } - return false -} - -// ForceLoadLazy forces loading of all lazy transformers. -func (r *Registry) ForceLoadLazy() { - r.mu.RLock() - defer r.mu.RUnlock() - - for from, targets := range r.lazyRequests { - for to := range targets { - r.loadLazyRequest(from, to) - } - } - for from, targets := range r.lazyResponses { - for to := range targets { - r.loadLazyResponse(from, to) - } - } -} - // TranslateRequest converts a payload between schemas, returning the original payload -// if no translator is registered. +// if no translator is registered. It also loads lazy transforms on-demand. func (r *Registry) TranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) []byte { - start := time.Now() - debugMode := r.debugMode.Load() - - if debugMode { - log.Debugf("translator: TranslateRequest [%s->%s] BEFORE (model: %s): %s", - from, to, model, truncatePayload(rawJSON, 500)) + // First try with read lock + r.mu.RLock() + if byTarget, ok := r.requests[from]; ok { + if fn, isOk := byTarget[to]; isOk && fn != nil { + r.mu.RUnlock() + return fn(model, rawJSON, stream) + } } - r.mu.RLock() - var fn RequestTransform - var found bool - if byTarget, ok := r.requests[from]; ok { - if f, isOk := byTarget[to]; isOk && f != nil { - fn = f - found = true + // Check if there's a lazy transform to load + var needsLoad bool + if byTarget, ok := r.lazyRequests[from]; ok { + if entry, isOk := byTarget[to]; isOk && !entry.loaded { + needsLoad = true } } r.mu.RUnlock() - // Check lazy loaders if not found in regular registry - if !found { - if lazyFn := r.loadLazyRequest(from, to); lazyFn != nil { - fn = lazyFn - found = true + // Load lazy transform if needed + if needsLoad { + r.mu.Lock() + // Double-check after acquiring write lock + if byTarget, ok := r.lazyRequests[from]; ok { + if entry, isOk := byTarget[to]; isOk && !entry.loaded && entry.loader != nil { + fn := entry.loader() + entry.loaded = true + if fn != nil { + if _, ok := r.requests[from]; !ok { + r.requests[from] = make(map[Format]RequestTransform) + } + r.requests[from][to] = fn + } + } } - } + r.mu.Unlock() - if found { - result := fn(model, rawJSON, stream) - r.recordTranslation(from, to, time.Since(start)) - - if debugMode { - log.Debugf("translator: TranslateRequest [%s->%s] AFTER (model: %s): %s", - from, to, model, truncatePayload(result, 500)) - log.Debugf("translator: TranslateRequest [%s->%s] DIFF: %s", - from, to, DiffPayloads(rawJSON, result)) + // Try again with the loaded transform + r.mu.RLock() + if byTarget, ok := r.requests[from]; ok { + if fn, isOk := byTarget[to]; isOk && fn != nil { + r.mu.RUnlock() + return fn(model, rawJSON, stream) + } } - return result - } - - // Warn if translation was expected but not found (from != to means translation should happen) - if from != to && from != "" && to != "" { - log.Warnf("translator: no request translator registered for %s -> %s (model: %s), passing through unchanged", from, to, model) - r.recordFailedTranslation(from, to) + r.mu.RUnlock() } return rawJSON } -// HasResponseTransformer indicates whether a response translator exists. +// HasResponseTransformer indicates whether a response translator (eager or lazy) exists. func (r *Registry) HasResponseTransformer(from, to Format) bool { r.mu.RLock() defer r.mu.RUnlock() + // Check eager transforms first if byTarget, ok := r.responses[from]; ok { if _, isOk := byTarget[to]; isOk { return true } } - // Check lazy responses + // Check lazy transforms if byTarget, ok := r.lazyResponses[from]; ok { if _, isOk := byTarget[to]; isOk { return true @@ -535,219 +133,103 @@ func (r *Registry) HasResponseTransformer(from, to Format) bool { return false } -// HasRequestTranslator indicates whether a request translator exists for the given format pair. -func (r *Registry) HasRequestTranslator(from, to Format) bool { - r.mu.RLock() - defer r.mu.RUnlock() - - if byTarget, ok := r.requests[from]; ok { - if fn, isOk := byTarget[to]; isOk && fn != nil { - return true - } - } - // Check lazy requests - if byTarget, ok := r.lazyRequests[from]; ok { - if _, isOk := byTarget[to]; isOk { - return true - } - } - return false -} - -// MustTranslateRequest converts a payload between schemas, returning an error if no translator is registered. -// Unlike TranslateRequest, this method fails fast instead of returning the original payload. -func (r *Registry) MustTranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) ([]byte, error) { - // If formats are the same, no translation needed - if from == to { - return rawJSON, nil - } - - // Check dry-run mode first - if r.dryRunMode.Load() { - log.Infof("translator [DRY-RUN]: would translate request from %s to %s (model: %s, stream: %v, payload size: %d bytes)", - from, to, model, stream, len(rawJSON)) - if r.debugMode.Load() { - log.Debugf("translator [DRY-RUN]: payload preview: %s", truncatePayload(rawJSON, 500)) - } - return rawJSON, nil - } - - start := time.Now() - debugMode := r.debugMode.Load() - - if debugMode { - log.Debugf("translator: MustTranslateRequest [%s->%s] BEFORE (model: %s): %s", - from, to, model, truncatePayload(rawJSON, 500)) - } - - r.mu.RLock() - var fn RequestTransform - var found bool - if byTarget, ok := r.requests[from]; ok { - if f, isOk := byTarget[to]; isOk && f != nil { - fn = f - found = true - } - } - r.mu.RUnlock() - - if found { - result := fn(model, rawJSON, stream) - r.recordTranslation(from, to, time.Since(start)) - - if debugMode { - log.Debugf("translator: MustTranslateRequest [%s->%s] AFTER (model: %s): %s", - from, to, model, truncatePayload(result, 500)) - log.Debugf("translator: MustTranslateRequest [%s->%s] DIFF: %s", - from, to, DiffPayloads(rawJSON, result)) - } - - return result, nil - } - - r.recordFailedTranslation(from, to) - return nil, fmt.Errorf("%w: no request translator for %s -> %s", ErrNoTranslator, from, to) -} - -// ValidateTranslationPath checks if a complete translation path exists between two formats. -// It verifies both request and response translators are registered. -func (r *Registry) ValidateTranslationPath(from, to Format) error { - if from == to { - return nil // Same format, no translation needed - } - - r.mu.RLock() - defer r.mu.RUnlock() - - var errors []string - - // Check request translator - hasRequest := false - if byTarget, ok := r.requests[from]; ok { - if fn, isOk := byTarget[to]; isOk && fn != nil { - hasRequest = true - } - } - if !hasRequest { - errors = append(errors, fmt.Sprintf("no request translator for %s -> %s", from, to)) - } - - // Check response translator (note: responses use reversed lookup direction) - hasResponse := false - if byTarget, ok := r.responses[to]; ok { - if _, isOk := byTarget[from]; isOk { - hasResponse = true - } - } - if !hasResponse { - errors = append(errors, fmt.Sprintf("no response translator for %s -> %s", from, to)) - } - - if len(errors) > 0 { - return fmt.Errorf("%w: %s", ErrNoTranslator, strings.Join(errors, "; ")) - } - - return nil -} - -// SetDryRunMode enables or disables dry-run mode for the registry. -// When enabled, translation methods will log what WOULD be translated but return the original payload. -func (r *Registry) SetDryRunMode(enabled bool) { - r.dryRunMode.Store(enabled) - if enabled { - log.Info("translator: dry-run mode enabled - translations will be logged but not applied") - } else { - log.Info("translator: dry-run mode disabled") - } -} - -// IsDryRunMode returns the current dry-run mode state. -func (r *Registry) IsDryRunMode() bool { - return r.dryRunMode.Load() -} - // TranslateStream applies the registered streaming response translator. +// It also loads lazy response transforms on-demand. func (r *Registry) TranslateStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string { - start := time.Now() - debugMode := r.debugMode.Load() - - if debugMode { - log.Debugf("translator: TranslateStream [%s->%s] BEFORE (model: %s): %s", - from, to, model, truncatePayload(rawJSON, 500)) - } - + // First try with read lock r.mu.RLock() - var streamFn func(context.Context, string, []byte, []byte, []byte, *any) []string - var found bool if byTarget, ok := r.responses[to]; ok { if fn, isOk := byTarget[from]; isOk && fn.Stream != nil { - streamFn = fn.Stream - found = true + r.mu.RUnlock() + return fn.Stream(ctx, model, originalRequestRawJSON, requestRawJSON, rawJSON, param) + } + } + + // Check if there's a lazy transform to load + var needsLoad bool + if byTarget, ok := r.lazyResponses[to]; ok { + if entry, isOk := byTarget[from]; isOk && !entry.loaded { + needsLoad = true } } r.mu.RUnlock() - if found { - result := streamFn(ctx, model, originalRequestRawJSON, requestRawJSON, rawJSON, param) - r.recordTranslation(from, to, time.Since(start)) - - if debugMode { - // For stream, log first chunk only to avoid excessive logging - if len(result) > 0 { - log.Debugf("translator: TranslateStream [%s->%s] AFTER (model: %s, %d chunks): first chunk: %s", - from, to, model, len(result), truncatePayload([]byte(result[0]), 500)) + // Load lazy transform if needed + if needsLoad { + r.mu.Lock() + // Double-check after acquiring write lock + if byTarget, ok := r.lazyResponses[to]; ok { + if entry, isOk := byTarget[from]; isOk && !entry.loaded && entry.loader != nil { + fn := entry.loader() + entry.loaded = true + if _, ok := r.responses[to]; !ok { + r.responses[to] = make(map[Format]ResponseTransform) + } + r.responses[to][from] = fn } } - return result - } + r.mu.Unlock() - // Warn if translation was expected but not found - if from != to && from != "" && to != "" { - log.Warnf("translator: no stream response translator registered for %s -> %s (model: %s), passing through unchanged", from, to, model) - r.recordFailedTranslation(from, to) + // Try again with the loaded transform + r.mu.RLock() + if byTarget, ok := r.responses[to]; ok { + if fn, isOk := byTarget[from]; isOk && fn.Stream != nil { + r.mu.RUnlock() + return fn.Stream(ctx, model, originalRequestRawJSON, requestRawJSON, rawJSON, param) + } + } + r.mu.RUnlock() } return []string{string(rawJSON)} } // TranslateNonStream applies the registered non-stream response translator. +// It also loads lazy response transforms on-demand. func (r *Registry) TranslateNonStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) string { - start := time.Now() - debugMode := r.debugMode.Load() - - if debugMode { - log.Debugf("translator: TranslateNonStream [%s->%s] BEFORE (model: %s): %s", - from, to, model, truncatePayload(rawJSON, 500)) - } - + // First try with read lock r.mu.RLock() - var nonStreamFn func(context.Context, string, []byte, []byte, []byte, *any) string - var found bool if byTarget, ok := r.responses[to]; ok { if fn, isOk := byTarget[from]; isOk && fn.NonStream != nil { - nonStreamFn = fn.NonStream - found = true + r.mu.RUnlock() + return fn.NonStream(ctx, model, originalRequestRawJSON, requestRawJSON, rawJSON, param) + } + } + + // Check if there's a lazy transform to load + var needsLoad bool + if byTarget, ok := r.lazyResponses[to]; ok { + if entry, isOk := byTarget[from]; isOk && !entry.loaded { + needsLoad = true } } r.mu.RUnlock() - if found { - result := nonStreamFn(ctx, model, originalRequestRawJSON, requestRawJSON, rawJSON, param) - r.recordTranslation(from, to, time.Since(start)) - - if debugMode { - log.Debugf("translator: TranslateNonStream [%s->%s] AFTER (model: %s): %s", - from, to, model, truncatePayload([]byte(result), 500)) - log.Debugf("translator: TranslateNonStream [%s->%s] DIFF: %s", - from, to, DiffPayloads(rawJSON, []byte(result))) + // Load lazy transform if needed + if needsLoad { + r.mu.Lock() + // Double-check after acquiring write lock + if byTarget, ok := r.lazyResponses[to]; ok { + if entry, isOk := byTarget[from]; isOk && !entry.loaded && entry.loader != nil { + fn := entry.loader() + entry.loaded = true + if _, ok := r.responses[to]; !ok { + r.responses[to] = make(map[Format]ResponseTransform) + } + r.responses[to][from] = fn + } } - return result - } + r.mu.Unlock() - // Warn if translation was expected but not found - if from != to && from != "" && to != "" { - log.Warnf("translator: no non-stream response translator registered for %s -> %s (model: %s), passing through unchanged", from, to, model) - r.recordFailedTranslation(from, to) + // Try again with the loaded transform + r.mu.RLock() + if byTarget, ok := r.responses[to]; ok { + if fn, isOk := byTarget[from]; isOk && fn.NonStream != nil { + r.mu.RUnlock() + return fn.NonStream(ctx, model, originalRequestRawJSON, requestRawJSON, rawJSON, param) + } + } + r.mu.RUnlock() } return string(rawJSON) @@ -766,12 +248,6 @@ func (r *Registry) TranslateTokenCount(ctx context.Context, from, to Format, cou return string(rawJSON) } -// ErrNoTranslator is returned when no translator is registered for a path. -var ErrNoTranslator = fmt.Errorf("no translator registered") - -// ErrTranslationFailed is returned when translation fails. -var ErrTranslationFailed = fmt.Errorf("translation failed") - var defaultRegistry = NewRegistry() // Default exposes the package-level registry for shared use. @@ -809,82 +285,174 @@ func TranslateTokenCount(ctx context.Context, from, to Format, count int64, rawJ return defaultRegistry.TranslateTokenCount(ctx, from, to, count, rawJSON) } -// SetDebugMode enables or disables debug mode on the default registry. -func SetDebugMode(enabled bool) { - defaultRegistry.SetDebugMode(enabled) +// RegisterLazy stores a lazy-loaded request transform between two formats. +func (r *Registry) RegisterLazy(from, to Format, loader func() RequestTransform) { + r.mu.Lock() + defer r.mu.Unlock() + + if _, ok := r.lazyRequests[from]; !ok { + r.lazyRequests[from] = make(map[Format]*lazyRequestEntry) + } + r.lazyRequests[from][to] = &lazyRequestEntry{loader: loader} } -// IsDebugMode returns the current debug mode state of the default registry. -func IsDebugMode() bool { - return defaultRegistry.IsDebugMode() +// RegisterLazyResponse stores a lazy-loaded response transform between two formats. +func (r *Registry) RegisterLazyResponse(from, to Format, loader func() ResponseTransform) { + r.mu.Lock() + defer r.mu.Unlock() + + if _, ok := r.lazyResponses[from]; !ok { + r.lazyResponses[from] = make(map[Format]*lazyResponseEntry) + } + r.lazyResponses[from][to] = &lazyResponseEntry{loader: loader} } -// LogRegisteredTranslators logs all registered translators in the default registry. -func LogRegisteredTranslators() { - defaultRegistry.LogRegisteredTranslators() +// RegisterLazyBoth stores both request and response lazy-loaded transforms. +func (r *Registry) RegisterLazyBoth(from, to Format, reqLoader func() RequestTransform, respLoader func() ResponseTransform) { + r.RegisterLazy(from, to, reqLoader) + r.RegisterLazyResponse(from, to, respLoader) } -// GetTranslationStats returns translation statistics from the default registry. -func GetTranslationStats() map[string]int64 { - return defaultRegistry.GetTranslationStats() +// IsLazyLoaded checks if a lazy-loaded transform has been loaded. +func (r *Registry) IsLazyLoaded(from, to Format) bool { + r.mu.RLock() + defer r.mu.RUnlock() + + if byTarget, ok := r.lazyRequests[from]; ok { + if entry, isOk := byTarget[to]; isOk { + return entry.loaded + } + } + return false } -// GetPerformanceMetrics returns performance metrics from the default registry. -func GetPerformanceMetrics() map[string]time.Duration { - return defaultRegistry.GetPerformanceMetrics() +// HasRequestTranslator checks if a request translator (eager or lazy) exists. +func (r *Registry) HasRequestTranslator(from, to Format) bool { + r.mu.RLock() + defer r.mu.RUnlock() + + // Check eager transforms first + if byTarget, ok := r.requests[from]; ok { + if _, isOk := byTarget[to]; isOk { + return true + } + } + // Check lazy transforms + if byTarget, ok := r.lazyRequests[from]; ok { + if _, isOk := byTarget[to]; isOk { + return true + } + } + return false } -// HasRequestTranslator inspects the default registry for request translators. -func HasRequestTranslator(from, to Format) bool { - return defaultRegistry.HasRequestTranslator(from, to) +// GetLazyLoadStats returns statistics about lazy loaded translators. +func (r *Registry) GetLazyLoadStats() LazyLoadStats { + r.mu.RLock() + defer r.mu.RUnlock() + + stats := LazyLoadStats{} + for _, byTarget := range r.lazyRequests { + for _, entry := range byTarget { + stats.TotalLazy++ + if entry.loaded { + stats.TotalLoaded++ + } + } + } + for _, byTarget := range r.lazyResponses { + for _, entry := range byTarget { + stats.TotalLazy++ + if entry.loaded { + stats.TotalLoaded++ + } + } + } + return stats } -// MustTranslateRequest is a helper on the default registry that fails fast. -func MustTranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) ([]byte, error) { - return defaultRegistry.MustTranslateRequest(from, to, model, rawJSON, stream) +// ForceLoadLazy loads all lazy-loaded transforms immediately. +func (r *Registry) ForceLoadLazy() { + r.mu.Lock() + defer r.mu.Unlock() + + for from, byTarget := range r.lazyRequests { + for to, entry := range byTarget { + if !entry.loaded && entry.loader != nil { + fn := entry.loader() + entry.loaded = true + if fn != nil { + if _, ok := r.requests[from]; !ok { + r.requests[from] = make(map[Format]RequestTransform) + } + r.requests[from][to] = fn + } + } + } + } + for from, byTarget := range r.lazyResponses { + for to, entry := range byTarget { + if !entry.loaded && entry.loader != nil { + fn := entry.loader() + entry.loaded = true + if _, ok := r.responses[from]; !ok { + r.responses[from] = make(map[Format]ResponseTransform) + } + r.responses[from][to] = fn + } + } + } } -// ValidateTranslationPath checks if a translation path exists in the default registry. -func ValidateTranslationPath(from, to Format) error { - return defaultRegistry.ValidateTranslationPath(from, to) -} - -// SetDryRunMode enables or disables dry-run mode on the default registry. -func SetDryRunMode(enabled bool) { - defaultRegistry.SetDryRunMode(enabled) -} - -// IsDryRunMode returns the current dry-run mode state of the default registry. -func IsDryRunMode() bool { - return defaultRegistry.IsDryRunMode() -} - -// RegisterLazy registers a lazy-loaded request transformer on the default registry. -func RegisterLazy(from, to Format, loader LazyRequestLoader) { +// RegisterLazy is a helper on the default registry. +func RegisterLazy(from, to Format, loader func() RequestTransform) { defaultRegistry.RegisterLazy(from, to, loader) } -// RegisterLazyResponse registers a lazy-loaded response transformer on the default registry. -func RegisterLazyResponse(from, to Format, loader LazyResponseLoader) { +// RegisterLazyResponse is a helper on the default registry. +func RegisterLazyResponse(from, to Format, loader func() ResponseTransform) { defaultRegistry.RegisterLazyResponse(from, to, loader) } -// RegisterLazyBoth registers both transformers as lazy-loaded on the default registry. -func RegisterLazyBoth(from, to Format, reqLoader LazyRequestLoader, respLoader LazyResponseLoader) { +// RegisterLazyBoth is a helper on the default registry. +func RegisterLazyBoth(from, to Format, reqLoader func() RequestTransform, respLoader func() ResponseTransform) { defaultRegistry.RegisterLazyBoth(from, to, reqLoader, respLoader) } -// GetLazyLoadStats returns lazy loading statistics from the default registry. -func GetLazyLoadStats() LazyLoadStats { - return defaultRegistry.GetLazyLoadStats() -} - -// IsLazyLoaded checks if a lazy transformer has been loaded in the default registry. +// IsLazyLoaded is a helper on the default registry. func IsLazyLoaded(from, to Format) bool { return defaultRegistry.IsLazyLoaded(from, to) } -// ForceLoadLazy forces loading of all lazy transformers in the default registry. +// HasRequestTranslator is a helper on the default registry. +func HasRequestTranslator(from, to Format) bool { + return defaultRegistry.HasRequestTranslator(from, to) +} + +// GetLazyLoadStats is a helper on the default registry. +func GetLazyLoadStats() LazyLoadStats { + return defaultRegistry.GetLazyLoadStats() +} + +// ForceLoadLazy is a helper on the default registry. func ForceLoadLazy() { defaultRegistry.ForceLoadLazy() } + +// Unregister removes transforms for the given from->to direction from the default registry. +func Unregister(from, to Format) { + defaultRegistry.Unregister(from, to) +} + +// Unregister removes transforms for the given from->to direction from the registry. +func (r *Registry) Unregister(from, to Format) { + r.mu.Lock() + defer r.mu.Unlock() + + if byTarget, ok := r.requests[from]; ok { + delete(byTarget, to) + } + if byTarget, ok := r.responses[from]; ok { + delete(byTarget, to) + } +} diff --git a/sdk/translator/resilience.go b/sdk/translator/resilience.go deleted file mode 100644 index 7a72ee0a..00000000 --- a/sdk/translator/resilience.go +++ /dev/null @@ -1,346 +0,0 @@ -package translator - -import ( - "fmt" - "strings" - "sync/atomic" -) - -// Unregister removes a translator pair from the registry. -func (r *Registry) Unregister(from, to Format) { - r.mu.Lock() - defer r.mu.Unlock() - - if byTarget, ok := r.requests[from]; ok { - delete(byTarget, to) - if len(byTarget) == 0 { - delete(r.requests, from) - } - } - - if byTarget, ok := r.responses[from]; ok { - delete(byTarget, to) - if len(byTarget) == 0 { - delete(r.responses, from) - } - } - - // Also remove lazy loaders - if byTarget, ok := r.lazyRequests[from]; ok { - delete(byTarget, to) - if len(byTarget) == 0 { - delete(r.lazyRequests, from) - } - } - - if byTarget, ok := r.lazyResponses[from]; ok { - delete(byTarget, to) - if len(byTarget) == 0 { - delete(r.lazyResponses, from) - } - } -} - -// Unregister removes a translator pair from the default registry. -func Unregister(from, to Format) { - defaultRegistry.Unregister(from, to) -} - -// Clone creates a deep copy of the Registry. -// Note: Transform functions are shared (not deep copied) as they are typically stateless. -func (r *Registry) Clone() *Registry { - r.mu.RLock() - defer r.mu.RUnlock() - - newReg := NewRegistry() - - // Copy requests - for from, targets := range r.requests { - newReg.requests[from] = make(map[Format]RequestTransform) - for to, fn := range targets { - newReg.requests[from][to] = fn - } - } - - // Copy responses - for from, targets := range r.responses { - newReg.responses[from] = make(map[Format]ResponseTransform) - for to, fn := range targets { - newReg.responses[from][to] = fn - } - } - - // Copy lazy requests - for from, targets := range r.lazyRequests { - newReg.lazyRequests[from] = make(map[Format]*lazyRequestEntry) - for to, entry := range targets { - entry.loadMu.Lock() - newReg.lazyRequests[from][to] = &lazyRequestEntry{ - loader: entry.loader, - loaded: entry.loaded, - isLoaded: entry.isLoaded, - } - entry.loadMu.Unlock() - } - } - - // Copy lazy responses - for from, targets := range r.lazyResponses { - newReg.lazyResponses[from] = make(map[Format]*lazyResponseEntry) - for to, entry := range targets { - entry.loadMu.Lock() - newReg.lazyResponses[from][to] = &lazyResponseEntry{ - loader: entry.loader, - loaded: entry.loaded, - isLoaded: entry.isLoaded, - } - entry.loadMu.Unlock() - } - } - - // Copy debug/dry-run settings - newReg.debugMode.Store(r.debugMode.Load()) - newReg.dryRunMode.Store(r.dryRunMode.Load()) - newReg.loadedCount.Store(r.loadedCount.Load()) - newReg.lazyCount.Store(r.lazyCount.Load()) - - return newReg -} - -// CloneRegistry creates a deep copy of the default registry. -func CloneRegistry() *Registry { - return defaultRegistry.Clone() -} - -// registryPtr holds a pointer to the current default registry for atomic swap. -var registryPtr atomic.Pointer[Registry] - -func init() { - registryPtr.Store(defaultRegistry) -} - -// ReplaceRegistry atomically swaps the default registry with a new one. -// Returns the old registry. -func ReplaceRegistry(newRegistry *Registry) *Registry { - if newRegistry == nil { - return nil - } - old := registryPtr.Swap(newRegistry) - defaultRegistry = newRegistry - return old -} - -// HasRequestTransformer indicates whether a request translator exists. -// Alias for HasRequestTranslator for consistency with HasResponseTransformer. -func (r *Registry) HasRequestTransformer(from, to Format) bool { - return r.HasRequestTranslator(from, to) -} - -// HasRequestTransformer inspects the default registry. -func HasRequestTransformer(from, to Format) bool { - return defaultRegistry.HasRequestTransformer(from, to) -} - -// TranslationError provides detailed information about a failed translation attempt. -type TranslationError struct { - From Format - To Format - AttemptedPaths [][]Format - DetectedFormat Format - Cause error -} - -func (e *TranslationError) Error() string { - var sb strings.Builder - sb.WriteString(fmt.Sprintf("translation failed from %s to %s", e.From, e.To)) - if e.DetectedFormat != "" { - sb.WriteString(fmt.Sprintf(" (detected: %s)", e.DetectedFormat)) - } - if len(e.AttemptedPaths) > 0 { - sb.WriteString("; attempted paths: ") - for i, path := range e.AttemptedPaths { - if i > 0 { - sb.WriteString(", ") - } - sb.WriteString("[") - for j, f := range path { - if j > 0 { - sb.WriteString("->") - } - sb.WriteString(string(f)) - } - sb.WriteString("]") - } - } - if e.Cause != nil { - sb.WriteString(fmt.Sprintf("; cause: %v", e.Cause)) - } - return sb.String() -} - -func (e *TranslationError) Unwrap() error { - return e.Cause -} - -// TranslationResult contains the result of a translation with recovery. -type TranslationResult struct { - Payload []byte - UsedPath []Format - Detected Format - Success bool - DirectHit bool - FallbackHit bool - AutoDetectHit bool -} - -// TranslateRequestWithRecovery attempts translation with multiple recovery strategies: -// 1. Direct translation -// 2. Fallback chains -// 3. Auto-detect format and retry -func (r *Registry) TranslateRequestWithRecovery( - from, to Format, - model string, - rawJSON []byte, - stream bool, - fallbackReg *FallbackRegistry, -) (*TranslationResult, error) { - if fallbackReg == nil { - fallbackReg = DefaultFallbackRegistry() - } - - result := &TranslationResult{ - Payload: rawJSON, - } - var attemptedPaths [][]Format - - // Strategy 1: Try direct translation - if r.HasRequestTransformer(from, to) { - translated := r.TranslateRequest(from, to, model, rawJSON, stream) - result.Payload = translated - result.UsedPath = []Format{from, to} - result.Success = true - result.DirectHit = true - return result, nil - } - attemptedPaths = append(attemptedPaths, []Format{from, to}) - - // Strategy 2: Try fallback chains - if chain := fallbackReg.GetChain(from, to); chain != nil { - path := buildFullPath(from, to, chain) - - // Verify all steps in the chain exist - chainValid := true - for i := 0; i < len(path)-1; i++ { - if !r.HasRequestTransformer(path[i], path[i+1]) { - chainValid = false - break - } - } - - if chainValid { - translated := r.TranslateRequestViaChain(path, model, rawJSON, stream) - result.Payload = translated - result.UsedPath = path - result.Success = true - result.FallbackHit = true - return result, nil - } - attemptedPaths = append(attemptedPaths, path) - } - - // Strategy 3: Auto-detect format and retry - detected := DetectFormat(rawJSON) - result.Detected = detected - - if detected != "" && detected != from { - // Try direct translation from detected format - if r.HasRequestTransformer(detected, to) { - translated := r.TranslateRequest(detected, to, model, rawJSON, stream) - result.Payload = translated - result.UsedPath = []Format{detected, to} - result.Success = true - result.AutoDetectHit = true - return result, nil - } - attemptedPaths = append(attemptedPaths, []Format{detected, to}) - - // Try fallback chain from detected format - if chain := fallbackReg.GetChain(detected, to); chain != nil { - path := buildFullPath(detected, to, chain) - - chainValid := true - for i := 0; i < len(path)-1; i++ { - if !r.HasRequestTransformer(path[i], path[i+1]) { - chainValid = false - break - } - } - - if chainValid { - translated := r.TranslateRequestViaChain(path, model, rawJSON, stream) - result.Payload = translated - result.UsedPath = path - result.Success = true - result.FallbackHit = true - result.AutoDetectHit = true - return result, nil - } - attemptedPaths = append(attemptedPaths, path) - } - } - - // All strategies failed - return result, &TranslationError{ - From: from, - To: to, - AttemptedPaths: attemptedPaths, - DetectedFormat: detected, - Cause: ErrNoTranslator, - } -} - -// TranslateRequestWithRecovery is a helper on the default registry. -func TranslateRequestWithRecovery(from, to Format, model string, rawJSON []byte, stream bool) (*TranslationResult, error) { - return defaultRegistry.TranslateRequestWithRecovery(from, to, model, rawJSON, stream, nil) -} - -// ListRegisteredPairs returns all registered (from, to) pairs for requests. -func (r *Registry) ListRegisteredPairs() [][2]Format { - r.mu.RLock() - defer r.mu.RUnlock() - - var pairs [][2]Format - for from, targets := range r.requests { - for to := range targets { - pairs = append(pairs, [2]Format{from, to}) - } - } - // Include lazy registered pairs - for from, targets := range r.lazyRequests { - for to := range targets { - pairs = append(pairs, [2]Format{from, to}) - } - } - return pairs -} - -// ListRegisteredPairs returns all registered pairs from the default registry. -func ListRegisteredPairs() [][2]Format { - return defaultRegistry.ListRegisteredPairs() -} - -// Clear removes all registered translators from the registry. -func (r *Registry) Clear() { - r.mu.Lock() - defer r.mu.Unlock() - - r.requests = make(map[Format]map[Format]RequestTransform) - r.responses = make(map[Format]map[Format]ResponseTransform) - r.lazyRequests = make(map[Format]map[Format]*lazyRequestEntry) - r.lazyResponses = make(map[Format]map[Format]*lazyResponseEntry) -} - -// Clear removes all registered translators from the default registry. -func Clear() { - defaultRegistry.Clear() -} diff --git a/sdk/translator/resilience_test.go b/sdk/translator/resilience_test.go deleted file mode 100644 index fa197763..00000000 --- a/sdk/translator/resilience_test.go +++ /dev/null @@ -1,336 +0,0 @@ -package translator - -import ( - "errors" - "testing" -) - -func TestTranslateRequestWithRecovery_DirectHit(t *testing.T) { - reg := NewRegistry() - reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte { - return append([]byte("translated:"), data...) - }, ResponseTransform{}) - - result, err := reg.TranslateRequestWithRecovery(FormatOpenAI, FormatClaude, "model", []byte("test"), false, nil) - - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - if !result.Success { - t.Error("Result should be successful") - } - if !result.DirectHit { - t.Error("Should be a direct hit") - } - if result.FallbackHit { - t.Error("Should not be a fallback hit") - } - if string(result.Payload) != "translated:test" { - t.Errorf("Payload = %q, want %q", string(result.Payload), "translated:test") - } - if len(result.UsedPath) != 2 { - t.Error("UsedPath should have 2 elements") - } -} - -func TestTranslateRequestWithRecovery_FallbackChain(t *testing.T) { - reg := NewRegistry() - - // Register indirect path: OpenAI -> Gemini -> Claude - reg.Register(FormatOpenAI, FormatGemini, func(model string, data []byte, stream bool) []byte { - return append(data, []byte("-gemini")...) - }, ResponseTransform{}) - reg.Register(FormatGemini, FormatClaude, func(model string, data []byte, stream bool) []byte { - return append(data, []byte("-claude")...) - }, ResponseTransform{}) - - // Register fallback chain - fallbackReg := NewFallbackRegistry() - fallbackReg.RegisterChain(FormatOpenAI, FormatClaude, []Format{FormatGemini}) - - result, err := reg.TranslateRequestWithRecovery(FormatOpenAI, FormatClaude, "model", []byte("test"), false, fallbackReg) - - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - if !result.Success { - t.Error("Result should be successful") - } - if result.DirectHit { - t.Error("Should not be a direct hit") - } - if !result.FallbackHit { - t.Error("Should be a fallback hit") - } - if string(result.Payload) != "test-gemini-claude" { - t.Errorf("Payload = %q, want %q", string(result.Payload), "test-gemini-claude") - } -} - -func TestTranslateRequestWithRecovery_AutoDetect(t *testing.T) { - reg := NewRegistry() - - // Register Claude -> OpenAI translator - reg.Register(FormatClaude, FormatOpenAI, func(model string, data []byte, stream bool) []byte { - return append([]byte("from-claude:"), data...) - }, ResponseTransform{}) - - // Payload that will be auto-detected as Claude format - payload := []byte(`{"model": "claude-3", "messages": [], "anthropic_version": "2023-06-01"}`) - - // Request translation from unknown format to OpenAI - // The auto-detect should identify it as Claude and use that translator - result, err := reg.TranslateRequestWithRecovery("unknown", FormatOpenAI, "model", payload, false, nil) - - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - if !result.Success { - t.Error("Result should be successful via auto-detect") - } - if !result.AutoDetectHit { - t.Error("Should be an auto-detect hit") - } - if result.Detected != FormatClaude { - t.Errorf("Detected = %v, want %v", result.Detected, FormatClaude) - } -} - -func TestTranslateRequestWithRecovery_AllStrategiesFail(t *testing.T) { - reg := NewRegistry() - // Empty registry - no translators - - result, err := reg.TranslateRequestWithRecovery(FormatOpenAI, FormatClaude, "model", []byte("test"), false, nil) - - if err == nil { - t.Error("Should return error when all strategies fail") - } - if result.Success { - t.Error("Result should not be successful") - } - - // Check it's a TranslationError - var translationErr *TranslationError - if !errors.As(err, &translationErr) { - t.Error("Error should be a TranslationError") - } -} - -func TestTranslationError_Error(t *testing.T) { - err := &TranslationError{ - From: FormatOpenAI, - To: FormatClaude, - AttemptedPaths: [][]Format{{FormatOpenAI, FormatClaude}, {FormatOpenAI, FormatGemini, FormatClaude}}, - DetectedFormat: FormatGemini, - Cause: ErrNoTranslator, - } - - errStr := err.Error() - - if !containsStr(errStr, "openai") { - t.Error("Error message should contain source format") - } - if !containsStr(errStr, "claude") { - t.Error("Error message should contain target format") - } - if !containsStr(errStr, "detected: gemini") { - t.Error("Error message should contain detected format") - } - if !containsStr(errStr, "attempted paths") { - t.Error("Error message should mention attempted paths") - } -} - -func TestTranslationError_Unwrap(t *testing.T) { - cause := errors.New("root cause") - err := &TranslationError{ - From: FormatOpenAI, - To: FormatClaude, - Cause: cause, - } - - unwrapped := err.Unwrap() - if unwrapped != cause { - t.Error("Unwrap should return the cause") - } -} - -func TestUnregister(t *testing.T) { - reg := NewRegistry() - - reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte { - return data - }, ResponseTransform{}) - - // Verify they exist - if !reg.HasRequestTranslator(FormatOpenAI, FormatClaude) { - t.Fatal("Request translator should exist before unregister") - } - - reg.Unregister(FormatOpenAI, FormatClaude) - - // Verify they're gone - if reg.HasRequestTranslator(FormatOpenAI, FormatClaude) { - t.Error("Request translator should be removed after unregister") - } -} - -func TestCloneRegistry(t *testing.T) { - reg := NewRegistry() - - reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte { - return data - }, ResponseTransform{}) - reg.SetDebugMode(true) - reg.SetDryRunMode(true) - - clone := reg.Clone() - - // Verify clone has same translators - if !clone.HasRequestTranslator(FormatOpenAI, FormatClaude) { - t.Error("Clone should have the same translators") - } - - // Verify clone has same settings - if !clone.debugMode.Load() { - t.Error("Clone should have same debug mode") - } - if !clone.dryRunMode.Load() { - t.Error("Clone should have same dry-run mode") - } - - // Modify original, clone should be unaffected - reg.Unregister(FormatOpenAI, FormatClaude) - - if !clone.HasRequestTranslator(FormatOpenAI, FormatClaude) { - t.Error("Clone should be independent of original") - } -} - -func TestReplaceRegistry(t *testing.T) { - // Save original - original := defaultRegistry - - newReg := NewRegistry() - newReg.Register("test", "test2", func(model string, data []byte, stream bool) []byte { - return data - }, ResponseTransform{}) - - old := ReplaceRegistry(newReg) - - if old != original { - t.Error("ReplaceRegistry should return the old registry") - } - - if !defaultRegistry.HasRequestTranslator("test", "test2") { - t.Error("New registry should be active") - } - - // Restore original - ReplaceRegistry(original) -} - -func TestReplaceRegistry_Nil(t *testing.T) { - result := ReplaceRegistry(nil) - if result != nil { - t.Error("ReplaceRegistry(nil) should return nil") - } -} - -func TestListRegisteredPairs(t *testing.T) { - reg := NewRegistry() - - reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte { - return data - }, ResponseTransform{}) - reg.Register(FormatClaude, FormatGemini, func(model string, data []byte, stream bool) []byte { - return data - }, ResponseTransform{}) - - pairs := reg.ListRegisteredPairs() - - if len(pairs) != 2 { - t.Errorf("Expected 2 pairs, got %d", len(pairs)) - } - - foundPairs := make(map[string]bool) - for _, p := range pairs { - key := string(p[0]) + "->" + string(p[1]) - foundPairs[key] = true - } - - if !foundPairs["openai->claude"] { - t.Error("Should contain openai->claude pair") - } - if !foundPairs["claude->gemini"] { - t.Error("Should contain claude->gemini pair") - } -} - -func TestClear(t *testing.T) { - reg := NewRegistry() - - reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte { - return data - }, ResponseTransform{}) - - reg.Clear() - - pairs := reg.ListRegisteredPairs() - if len(pairs) != 0 { - t.Error("Clear should remove all translators") - } -} - -func TestHasRequestTransformer(t *testing.T) { - reg := NewRegistry() - - reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte { - return data - }, ResponseTransform{}) - - // HasRequestTransformer is an alias for HasRequestTranslator - if !reg.HasRequestTransformer(FormatOpenAI, FormatClaude) { - t.Error("HasRequestTransformer should return true for registered translator") - } - if reg.HasRequestTransformer(FormatClaude, FormatOpenAI) { - t.Error("HasRequestTransformer should return false for unregistered translator") - } -} - -func TestPackageLevelResilienceFunctions(t *testing.T) { - // Test package-level Unregister (on default registry) - // This just verifies it doesn't panic - Unregister("nonexistent", "format") - - // Test package-level HasRequestTransformer - _ = HasRequestTransformer(FormatOpenAI, FormatClaude) - - // Test package-level ListRegisteredPairs - pairs := ListRegisteredPairs() - if pairs == nil { - t.Error("ListRegisteredPairs should not return nil") - } - - // Test package-level TranslateRequestWithRecovery - result, _ := TranslateRequestWithRecovery(FormatOpenAI, FormatClaude, "model", []byte("test"), false) - if result == nil { - t.Error("TranslateRequestWithRecovery should not return nil result") - } - - // Test package-level CloneRegistry - clone := CloneRegistry() - if clone == nil { - t.Error("CloneRegistry should not return nil") - } -} - -// Helper function -func containsStr(s, substr string) bool { - for i := 0; i <= len(s)-len(substr); i++ { - if s[i:i+len(substr)] == substr { - return true - } - } - return false -}