refactor: standardize constant naming and improve file-based auth handling

- Renamed constants from uppercase to CamelCase for consistency.
- Replaced redundant file-based auth handling logic with the new `util.CountAuthFiles` helper.
- Fixed various error-handling inconsistencies and enhanced robustness in file operations.
- Streamlined auth client reload logic in server and watcher components.
- Applied minor code readability improvements across multiple packages.
This commit is contained in:
Luis Pater
2025-09-22 02:56:45 +08:00
parent 4999fce7f4
commit d9ad65622a
51 changed files with 341 additions and 270 deletions

View File

@@ -43,7 +43,7 @@ func NewClaudeCodeAPIHandler(apiHandlers *handlers.BaseAPIHandler) *ClaudeCodeAP
// HandlerType returns the identifier for this handler implementation.
func (h *ClaudeCodeAPIHandler) HandlerType() string {
return CLAUDE
return Claude
}
// Models returns a list of models supported by this handler.

View File

@@ -38,7 +38,7 @@ func NewGeminiCLIAPIHandler(apiHandlers *handlers.BaseAPIHandler) *GeminiCLIAPIH
// HandlerType returns the type of this handler.
func (h *GeminiCLIAPIHandler) HandlerType() string {
return GEMINICLI
return GeminiCLI
}
// Models returns a list of models supported by this handler.

View File

@@ -38,7 +38,7 @@ func NewGeminiAPIHandler(apiHandlers *handlers.BaseAPIHandler) *GeminiAPIHandler
// HandlerType returns the identifier for this handler implementation.
func (h *GeminiAPIHandler) HandlerType() string {
return GEMINI
return Gemini
}
// Models returns the Gemini-compatible model metadata supported by this handler.

View File

@@ -147,7 +147,7 @@ func (h *Handler) UploadAuthFile(c *gin.Context) {
c.JSON(500, gin.H{"error": fmt.Sprintf("failed to write file: %v", errWrite)})
return
}
if err := h.registerAuthFromFile(ctx, dst, data); err != nil {
if err = h.registerAuthFromFile(ctx, dst, data); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}

View File

@@ -44,7 +44,7 @@ func NewOpenAIAPIHandler(apiHandlers *handlers.BaseAPIHandler) *OpenAIAPIHandler
// HandlerType returns the identifier for this handler implementation.
func (h *OpenAIAPIHandler) HandlerType() string {
return OPENAI
return OpenAI
}
// Models returns the OpenAI-compatible model metadata supported by this handler.

View File

@@ -43,7 +43,7 @@ func NewOpenAIResponsesAPIHandler(apiHandlers *handlers.BaseAPIHandler) *OpenAIR
// HandlerType returns the identifier for this handler implementation.
func (h *OpenAIResponsesAPIHandler) HandlerType() string {
return OPENAI_RESPONSE
return OpenaiResponse
}
// Models returns the OpenAIResponses-compatible model metadata supported by this handler.
@@ -161,6 +161,7 @@ func (h *OpenAIResponsesAPIHandler) forwardResponsesStream(c *gin.Context, flush
return
case chunk, ok := <-data:
if !ok {
_, _ = c.Writer.Write([]byte("\n"))
flusher.Flush()
cancel(nil)
return

View File

@@ -439,38 +439,14 @@ func (s *Server) UpdateClients(cfg *config.Config) {
s.mgmt.SetAuthManager(s.handlers.AuthManager)
}
// Count types from AuthManager state + config
authFiles := 0
glAPIKeyCount := 0
claudeAPIKeyCount := 0
codexAPIKeyCount := 0
// Count client sources from configuration and auth directory
authFiles := util.CountAuthFiles(cfg.AuthDir)
glAPIKeyCount := len(cfg.GlAPIKey)
claudeAPIKeyCount := len(cfg.ClaudeKey)
codexAPIKeyCount := len(cfg.CodexKey)
openAICompatCount := 0
if s.handlers != nil && s.handlers.AuthManager != nil {
for _, a := range s.handlers.AuthManager.List() {
if a == nil {
continue
}
if a.Attributes != nil {
if p := a.Attributes["path"]; p != "" {
authFiles++
continue
}
}
switch strings.ToLower(a.Provider) {
case "gemini":
glAPIKeyCount++
case "claude":
claudeAPIKeyCount++
case "codex":
codexAPIKeyCount++
}
}
}
if cfg != nil {
for i := range cfg.OpenAICompatibility {
openAICompatCount += len(cfg.OpenAICompatibility[i].APIKeys)
}
for i := range cfg.OpenAICompatibility {
openAICompatCount += len(cfg.OpenAICompatibility[i].APIKeys)
}
total := authFiles + glAPIKeyCount + claudeAPIKeyCount + codexAPIKeyCount + openAICompatCount