From ebec293497c77d07ef818748a9c6143f9a4ae14d Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 3 Jan 2026 04:53:47 +0800 Subject: [PATCH] feat(api): integrate `TokenStore` for improved auth entry management Replaced file-based auth entry counting with `TokenStore`-backed implementation, enhancing flexibility and context-aware token management. Updated related logic to reflect this change. --- internal/api/server.go | 15 ++++++++++----- internal/util/util.go | 43 +++++++++++++++--------------------------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/internal/api/server.go b/internal/api/server.go index bac13759..ceafd6b5 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -33,6 +33,7 @@ import ( "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" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" @@ -966,8 +967,12 @@ func (s *Server) UpdateClients(cfg *config.Config) { log.Warnf("amp module is nil, skipping config update") } - // Count client sources from configuration and auth directory - authFiles := util.CountAuthFiles(cfg.AuthDir) + // 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) @@ -978,10 +983,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, diff --git a/internal/util/util.go b/internal/util/util.go index 6ecaa8e2..9bf630f2 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -4,8 +4,8 @@ package util import ( + "context" "fmt" - "io/fs" "os" "path/filepath" "regexp" @@ -93,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.