Commit Graph

75 Commits

  • feat(claude-executor): add ZSTD decoding support for Claude executor responses
    - Integrated ZSTD decompression via `github.com/klauspost/compress` for responses with "zstd" content-encoding.
    - Added helper `hasZSTDEcoding` to detect ZSTD-encoded responses.
    - Updated response handling logic to initialize and use a ZSTD decoder when necessary.
    
    refactor(api-handlers): split streaming and non-streaming response handling
    
    - Introduced `handleNonStreamingResponse` for processing non-streaming requests in `ClaudeCodeAPIHandler`.
    - Improved code clarity by separating streaming and non-streaming logic.
    
    fix(service): remove redundant token refresh interval assignment logic in `cliproxy` service.
  • fix(auth): address index logic bug and remove redundant conditions
    - Updated loop iteration in `AuthSelector` to correct index management for selecting candidates.
    - Fixed cursor index reset condition for large values to prevent overflow.
    - Removed unnecessary conditional reassignment of `allowRemote` in management handler for clarity and correctness.
  • feat(executor): add CountTokens support across all executors
    - Introduced `CountTokens` method to Codex, Claude, Gemini, Qwen, OpenAI-compatible, and other executors.
    - Implemented `ExecuteCount` in `AuthManager` for token counting via provider round-robin.
    - Updated handlers to leverage `ExecuteCountWithAuthManager` for streamlined token counting.
    - Added fallback and error handling logic for token counting requests.
  • feat(auth): standardize last_refresh metadata handling across executors
    - Added `last_refresh` timestamp to metadata for Codex, Claude, Qwen, and Gemini executors.
    - Implemented `extractLastRefreshTimestamp` utility for parsing diverse timestamp formats in management handlers.
    - Ensured consistent update and preservation of `last_refresh` in file-based auth handling.
  • feat(logging): integrate logrus with custom Gin middleware for enhanced request logging and recovery
    - Added `GinLogrusLogger` for structured request logging using Logrus.
    - Implemented `GinLogrusRecovery` to handle panics and log stack traces.
    - Configured log rotation using Lumberjack for efficient log management.
    - Replaced Gin's default logger and recovery middleware with the custom implementations.
  • feat(auth): introduce auth.providers for flexible authentication configuration
    - Replaced legacy `api-keys` field with `auth.providers` in configuration, supporting multiple authentication providers including `config-api-key`.
    - Added synchronization to maintain compatibility with legacy `api-keys`.
    - Updated core components like request handling and middleware to use the new provider system.
    - Enhanced management API endpoints for seamless integration with `auth.providers`.
  • 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.
  • feat: add client availability tracking and error handling improvements
    - Introduced `IsAvailable` and `SetUnavailable` methods to clients for availability tracking.
    - Integrated availability checks in client selection logic to skip unavailable clients.
    - Enhanced error handling by marking clients unavailable on specific error codes (e.g., 401, 402).
    - Removed redundant quota verification logs in client reordering logic.
  • feat: add graceful shutdown for streaming response handling
    - Introduced `streamDone` channel to signal the completion of streaming goroutines.
    - Updated `processStreamingChunks` to incorporate proper cleanup and defer close operations.
    - Ensured `streamWriter` and associated resources are released when streaming completes.
  • Add support for forcing GPT-5 Codex model configuration
    - Introduced a new `ForceGPT5Codex` configuration option in settings.
    - Added relevant API endpoints for managing `ForceGPT5Codex`.
    - Enhanced Codex client to handle GPT-5 Codex-specific logic and mapping.
    - Updated example configuration file to include the new option.
    
    Add GPT-5 Codex model support and configuration options in documentation
  • Update internal module imports to use v5 package path
    - Updated all `github.com/luispater/CLIProxyAPI/internal/...` imports to point to `github.com/luispater/CLIProxyAPI/v5/internal/...`.
    - Adjusted `go.mod` to specify `module github.com/luispater/CLIProxyAPI/v5`.
  • Implement IP-based rate limiting and ban mechanism for management API
    - Introduced a new `attemptInfo` structure to track failed login attempts per IP.
    - Added logic to temporarily ban IPs exceeding the allowed number of failures.
    - Enhanced middleware to reset failed attempt counters on successful authentication.
    - Updated `Handler` to include a `failedAttempts` map with thread-safe access.
  • Enhance OAuth handling for Anthropic, Codex, Gemini, and Qwen tokens
    - Transitioned OAuth callback handling from temporary servers to predefined persistent endpoints.
    - Simplified token retrieval by replacing in-memory handling with state-file-based persistence.
    - Introduced unified `oauthStatus` map for tracking flow progress and errors.
    - Added new `/auth/*/callback` routes, streamlining code and state management for OAuth flows.
    - Improved error handling and logging in token exchange and callback flows.
  • Add OAuth support for Gemini CLI, Claude, Codex, and Qwen authentication
    - Implemented new handlers (`RequestGeminiCLIToken`, `RequestAnthropicToken`, `RequestCodexToken`, `RequestQwenToken`) for initiating OAuth flows.
    - Added endpoints for authorization URLs in management API.
    - Extracted `GenerateRandomState` to a reusable utility in `misc` package.
    - Refactored related logic in `openai_login.go` and `anthropic_login.go` for consistency.
    
    Add endpoints for initiating OAuth flows in management API documentation
    
    - Documented new endpoints for Anthropic, Codex, Gemini CLI, and Qwen login URLs.
    - Provided request and response examples for each endpoint in both English and Chinese versions.
  • Fix bug: #38 about lobechat cors policy
    Relax CORS policy by allowing all headers in API responses
  • Add dynamic log level adjustment and "type" field to auth files response
    - Introduced `SetLogLevel` utility function for unified log level management.
    - Updated dynamic log level handling across server and watcher components.
    - Extended auth files response by extracting and including the `type` field from file content.
    - Updated management API documentation with the new `type` field in auth files response.
  • Add JSON annotations to configuration structs and new /config management endpoint
    - Added JSON annotations across all configuration structs in `config.go`.
    - Introduced `/config` management API endpoint to fetch complete configuration.
    - Updated management API documentation (`MANAGEMENT_API.md`, `MANAGEMENT_API_CN.md`) with `/config` usage.
    - Implemented `GetConfig` handler in `config_basic.go`.
  • Add reverse mappings for original tool names and improve error logging
    - Introduced reverse mapping logic for tool names in translators to restore original names when shortened.
    - Enhanced error handling by logging API response errors consistently across handlers.
    - Refactored request and response loggers to include API error details, improving debugging capabilities.
    - Integrated robust tool name shortening and uniqueness mechanisms for OpenAI, Gemini, and Claude requests.
    - Improved handler retry logic to properly capture and respond to errors.
  • Add support for Codex API key authentication
    - Introduced functionality to handle Codex API keys, including initialization and management via new endpoints in the management API.
    - Updated Codex client to support both OAuth and API key authentication.
    - Documented Codex API key configuration in both English and Chinese README files.
    - Enhanced logging to distinguish between API key and OAuth usage scenarios.
  • Refactor translator packages for OpenAI Chat Completions
    - Renamed `openai` packages to `chat_completions` across translator modules.
    - Introduced `openai_responses_handlers` with handlers for `/v1/models` and OpenAI-compatible chat completions endpoints.
    - Updated constants and registry identifiers for OpenAI response type.
    - Simplified request/response conversions and added detailed retry/error handling.
    - Added `golang.org/x/crypto` for additional cryptographic functions.
  • Refactor translator packages for OpenAI Chat Completions
    - Renamed `openai` packages to `chat_completions` across translator modules.
    - Introduced `openai_responses_handlers` with handlers for `/v1/models` and OpenAI-compatible chat completions endpoints.
    - Updated constants and registry identifiers for OpenAI response type.
    - Simplified request/response conversions and added detailed retry/error handling.
    - Added `golang.org/x/crypto` for additional cryptographic functions.
  • Add management API handlers for config and auth file management
    - Implemented CRUD operations for authentication files.
    - Added endpoints for managing API keys, quotas, proxy settings, and other configurations.
    - Enhanced management access with robust validation, remote access control, and persistence support.
    - Updated README with new configuration details.
    
    Fixed OpenAI Chat Completions for codex
  • Add token refresh handling for 401 responses across clients
    - Implemented `RefreshTokens` method in client interfaces and Gemini clients.
    - Updated handlers to call `RefreshTokens` on 401 responses and retry requests if token refresh succeeds.
    - Enhanced error handling and retry logic to accommodate token refresh flow.
  • Update README documentation to clarify auth-dir configuration for Windows users
    - Added a note for setting `auth-dir` on Windows systems in both English and Chinese README files.
    - Improved descriptions for existing configuration options.
    
    Address Qwen3 tool injection issue to prevent random token insertions
    
    - Modify Qwen client to insert a placeholder tool when none is defined, avoiding erratic behavior in streaming responses.
  • Add nil-check for GetRequestMutex across handlers to prevent potential panics
    - Updated all handlers to safely unlock the request mutex only if it's non-nil.
    - Enhanced mutex locking and unlocking logic to avoid runtime errors.
    - Improved robustness of resource cleanup across clients.
    
    Add `GetRequestMutex` method for synchronization across clients
    
    - Introduced a new `GetRequestMutex` method in OpenAICompatibilityClient, CodexClient, GeminiCLIClient, GeminiClient, and QwenClient for request synchronization.
    - Ensures only one request is processed at a time to manage quotas effectively.
  • Add /v1/completions endpoint with OpenAI compatibility
    - Implemented `/v1/completions` endpoint mirroring OpenAI's completions API specification.
    - Added conversion functions to translate between completions and chat completions formats.
    - Introduced streaming and non-streaming response handling for completions requests.
    - Updated `server.go` to register the new endpoint and include it in the API's metadata.
  • Suppress debug logs for model routing and ignore empty tools arrays
    - Comment out verbose routing logs in the API server to reduce noise.
    - Remove the `tools` field from Qwen client requests when it is an empty array.
    - Add guards in Claude, Codex, Gemini‑CLI, and Gemini translators to skip tool conversion when the `tools` array is empty, preventing unnecessary payload modifications.
  • Add support for localhost unauthenticated requests
    - Introduced `AllowLocalhostUnauthenticated` flag allowing unauthenticated requests from localhost.
    - Updated authentication middleware to bypass checks for localhost when enabled.
    
    Add new Gemini CLI models and update model registry function
    
    - Introduced `GetGeminiCLIModels` for updated Gemini CLI model definitions.
    - Added new models: "Gemini 2.5 Flash Lite" and "Gemini 2.5 Pro".
    - Updated `RegisterModels` to use `GetGeminiCLIModels` in Gemini client initialization.
  • Add OpenAI compatibility support and improve resource cleanup
    - Introduced OpenAI compatibility configurations for external providers, enabling model alias routing via the OpenAI API format.
    - Enhanced provider logic in `GetProviderName` to handle OpenAI aliases and added new helper functions for compatibility checks.
    - Updated API handlers and client initialization to support OpenAI compatibility models.
    - Improved resource cleanup across clients by closing response bodies and streams using deferred functions.
  • Refactor API handlers to implement retry mechanism with configurable limits and improved error handling
    - Introduced retry counter with a configurable ` RequestRetry ` limit in all handlers.
    - Enhanced error handling with specific HTTP status codes for switching clients.
    - Standardized response forwarding for non-retriable errors.
    - Improved logging for quota and client switch scenarios.
  • Refactor error handling and variable declarations in browser and logging modules
    - Simplified variable initialization in `browser.go` for readability.
    - Updated error handling in `request_logger.go` with better resource cleanup using deferred anonymous functions.
    
    Refactor API handlers to use `GetContextWithCancel` for streamlined context creation and response handling
    
    - Replaced redundant `context.WithCancel` and `context.WithValue` logic with the new `GetContextWithCancel` utility in all handlers.
    - Centralized API response storage in the given context during cancellation.
    - Updated associated cancellation calls for consistency and improved resource management.
    
    - Replaced `apiResponseData` with `AddAPIResponseData` for centralized response recording.
    - Simplified cancellation logic by switching to a boolean-based `cliCancel` method.
    - Removed unused `apiResponseData` slices across handlers to reduce memory usage.
    - Updated `handlers.go` to support unified response data storage per request context.
  • Add request logging capabilities to API handlers and update .gitignore
    Enhance API response handling by storing responses in context and updating request logger to include API responses
  • Refactor API handlers organization and simplify error response handling
    - Modularized handlers into dedicated packages (`gemini`, `claude`, `cli`) for better structure.
    - Centralized `ErrorResponse` and `ErrorDetail` types under `handlers` package for reuse.
    - Updated all handlers to utilize the shared `ErrorResponse` model.
    - Introduced specialization of handler structs (`GeminiAPIHandlers`, `ClaudeCodeAPIHandlers`, `GeminiCLIAPIHandlers`) for improved clarity and separation of concerns.
    - Refactored `getClient` logic with additional properties and better state management.
    
    Refactor `translator` package by modularizing code for `claude` and `gemini`
    
    - Moved Claude-specific logic (`PrepareClaudeRequest`, `ConvertCliToClaude`) to `translator/claude/code`.
    - Moved Gemini-specific logic (`FixCLIToolResponse`) to `translator/gemini/cli` for better package structure.
    - Updated affected handler imports and method references.
    
    Add comprehensive package-level documentation across key modules
    
    - Introduced detailed package-level documentation for core modules: `auth`, `client`, `cmd`, `handlers`, `util`, `watcher`, `config`, `translator`, and `api`.
    - Enhanced code readability and maintainability by clarifying the purpose and functionality of each package.
    - Aligned documentation style and tone with existing codebase conventions.
    
    Refactor API handlers and translator modules for improved clarity and consistency
    
    - Standardized handler struct names (`GeminiAPIHandlers`, `ClaudeCodeAPIHandlers`, `GeminiCLIAPIHandlers`, `OpenAIAPIHandlers`) and updated related comments.
    - Fixed unnecessary `else` blocks in streaming logic for cleaner error handling.
    - Renamed variables for better readability (`responseIdResult` to `responseIDResult`, `activationUrl` to `activationURL`, etc.).
    - Addressed minor inconsistencies in API handler comments and SSE header initialization.
    - Improved modularization of `claude` and `gemini` translator components.
    
    Standardize configuration field naming for consistency across modules
    
    - Renamed `ProxyUrl` to `ProxyURL`, `ApiKeys` to `APIKeys`, and `ConfigQuotaExceeded` to `QuotaExceeded`.
    - Updated all relevant references and comments in `config`, `auth`, `api`, `util`, and `watcher`.
    - Ensured consistent casing for `GlAPIKey` debug logs.
  • Enhance Gemini request handling for contents support and improve error logging
    - Added conditional logic to process `contents` in Gemini request templates, ensuring fallback behavior.
    - Introduced detailed debug logs for quota errors and request issues.
    - Updated handling of `rawJson` to construct templates more dynamically.