Commit Graph

94 Commits

  • refactor(config): Implement reconciliation for providers and clients
    This commit introduces a reconciliation mechanism for handling configuration updates, significantly improving efficiency and resource management.
    
    Previously, reloading the configuration would tear down and recreate all access providers from scratch, regardless of whether their individual configurations had changed. This was inefficient and could disrupt services.
    
    The new `sdkaccess.ReconcileProviders` function now compares the old and new configurations to intelligently manage the provider lifecycle:
    - Unchanged providers are kept.
    - New providers are created.
    - Providers removed from the config are closed and discarded.
    - Providers with updated configurations are gracefully closed and recreated.
    
    To support this, a `Close()` method has been added to the `Provider` interface.
    
    A similar reconciliation logic has been applied to the client registration state in `state.RegisterClient`. This ensures that model registrations are accurately tracked when a client's configuration is updated, correctly handling added, removed, and unchanged models. Enhanced logging provides visibility into these operations.
  • feat(config, usage): add usage-statistics-enabled option and dynamic toggling
    - Introduced `usage-statistics-enabled` configuration to control in-memory usage aggregation.
    - Updated API to include handlers for managing `usage-statistics-enabled` and `logging-to-file` options.
    - Enhanced `watcher` to log changes to both configurations dynamically.
    - Updated documentation and examples to reflect new configuration options.
  • feat(server): add keep-alive endpoint with timeout handling
    - Introduced a keep-alive endpoint to monitor service activity.
    - Added timeout-specific shutdown functionality when the endpoint is idle.
    - Implemented password-protected access for the keep-alive endpoint.
    - Updated server startup to support configurable keep-alive options.
  • feat(logging): introduce centralized logging with custom format and Gin integration
    - Implemented a global logger with structured formatting for consistent log output.
    - Added support for rotating log files using Lumberjack.
    - Integrated new logging functionality with Gin HTTP server for unified log handling.
    - Replaced direct `log.Info` calls with `fmt.Printf` in non-critical paths to simplify core functionality.
  • feat(auth, docs): add label support for Gemini web token management
    - Added `label` field to the management API for better token identification.
    - Updated request payload and validation logic to include `label` as a required field.
    - Adjusted documentation (`MANAGEMENT_API.md`, `MANAGEMENT_API_CN.md`) to reflect changes.
  • feat(auth, docs): add SDK guides and local password support for management
    - Added extensive SDK usage guides for `cliproxy`, `sdk/access`, and watcher integration.
    - Introduced `--password` flag for specifying local management access passwords.
    - Enhanced management API with local password checks to secure localhost requests.
    - Updated documentation to reflect the new password functionality.
  • refactor(config, auth): remove allow-localhost-unauthenticated support and related handlers
    - Eliminated `allow-localhost-unauthenticated` configuration field and its usage.
    - Removed associated management API handlers and middleware logic.
    - Simplified authentication middleware by deprecating localhost-specific checks.
  • refactor(logging, mgmt): update timestamp format in logs and remove force-gpt-5-codex handlers
    - Changed log timestamp format in `request_logger.go` to align with ISO standards for improved readability.
    - Removed deprecated `force-gpt-5-codex` handlers from management API.
  • refactor(config): remove force-gpt-5-codex option and related handlers
    - Eliminated the deprecated `force-gpt-5-codex` configuration option from all configs, handlers, and documentation.
    - Updated examples and README files to reflect the removal.
    - Streamlined related code by dropping unused fields and logging.
  • feat(auth): add Gemini Web token saving endpoint
    - Introduced `POST /gemini-web-token` endpoint to save Gemini Web cookies directly.
    - Added payload validation and hashed-based file naming for persistence.
    - Updated documentation to reflect the new management API functionality.
  • feat(auth): centralize token store management and enhance persistence
    - Introduced `RegisterTokenStore` and `GetTokenStore` to centralize token store access.
    - Replaced direct file operations with a unified token persistence API.
    - Updated all components to use the shared token store for consistent behavior.
    - Improved logging for token save operations to include file paths.
  • feat(usage): add in-memory usage statistics tracking and API endpoint
    - Introduced in-memory request statistics aggregation in `LoggerPlugin`.
    - Added new structures for detailed metrics collection (e.g., token breakdown, request success/failure).
    - Implemented `/usage` management API endpoint for retrieving aggregated statistics.
    - Updated management handlers to support the new usage statistics functionality.
    - Enhanced documentation to describe the usage metrics API.
  • fix(gemini): handle "[DONE]" chunk, trim "data:" prefix, and remove session_id from requests
    - Adjusted stream handling to skip "[DONE]" chunks.
    - Ensured "data:" prefix is trimmed for non-prefixed input in translation.
    - Removed `session_id` from request bodies before processing.
  • refactor(executor): remove ClientAdapter and legacy fallback logic
    - Deleted `ClientAdapter` implementation and associated fallback methods.
    - Removed legacy executor logic from `codex`, `claude`, `gemini`, and `qwen` executors.
    - Simplified `handlers` by eliminating `UnwrapError` handling and related dependencies.
    - Cleaned up `model_registry` by removing logic associated with suspended clients.
    - Updated `.gitignore` to ignore `.serena/` directory.
  • feat(translators): add token counting support for Claude and Gemini responses
    - Implemented `TokenCount` transform method across translators to calculate token usage.
    - Integrated token counting logic into executor pipelines for Claude, Gemini, and CLI translators.
    - Added corresponding API endpoints and handlers (`/messages/count_tokens`) for token usage retrieval.
    - Enhanced translation registry to support `TokenCount` functionality alongside existing response types.
  • 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.