Commit Graph

36 Commits

  • 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.
  • Add file watcher for dynamic configuration and client reloading
    - Introduced `Watcher` for monitoring updates to the configuration file and authentication directory.
    - Integrated file watching into `StartService` to handle dynamic changes without restarting.
    - Enhanced API server and handlers to support client and configuration updates.
    - Updated `.gitignore` to include `docs/` directory.
    - Modified go dependencies to include `fsnotify` for the file watcher.
  • Improve getClient logic with optional content generation flag
    - Added `isGenerateContent` optional parameter to `getClient` for conditional client selection.
    - Updated `gemini-handlers` to utilize the new parameter for enhanced control.
  • Fix SSE headers initialization for geminiStreamGenerateContent and internalStreamGenerateContent
    - Added conditional logic to properly initialize SSE headers only when `alt` is empty.
    - Ensured headers like `Content-Type`, `Cache-Control`, and `Access-Control-Allow-Origin` are set for better compatibility.
  • Enhance Gemini request handling with fallback support for contents
    - Added conditional logic to support `contents` as a fallback to `generateContentRequest`.
    - Improved template construction and ensured proper cleanup of request fields.
    - Introduced debug logging for troubleshooting request generation.
  • Add GeminiGetHandler, enhance Gemini functionality, and enable token counting
    - Added `GeminiGetHandler` for handling GET requests with extended Gemini model support.
    - Introduced `geminiCountTokens` function to calculate token usage.
    - Refactored `APIRequest` and related methods to support `alt` parameter for enhanced flexibility.
    - Updated routes and request processing to integrate new handler and functions.
  • Add GeminiModels handler and enhance API key validation
    - Introduced `GeminiModels` handler to serve Gemini model information under `/v1beta/models`.
    - Updated `AuthMiddleware` to validate API keys from query parameters for improved flexibility.
    - Adjusted route to use the new handler for model retrieval.
  • Refactor client handling and improve error responses
    - Centralized client retrieval logic with `getClient` function for reduced redundancy.
    - Simplified client rotation and error handling by removing excessive load balancing logic.
    - Updated server address in `auth.go` to use dynamic binding (`:8085`).
  • Add ClaudeMessages handler for SSE-compatible chat completions
    - Introduced `ClaudeMessages` to handle Claude-compatible streaming chat completions.
    - Implemented client rotation, quota management, and dynamic model name mapping for better load balancing and resource utilization.
    - Enhanced response streaming with real-time chunking and Claude format conversion.
    - Added error handling for quota exhaustion, client disconnections, and backend failures.
  • Add Claude compatibility and enhance API handling
    - Integrated Claude API compatibility in handlers, translators, and server routes.
    - Introduced `/messages` endpoint and upgraded `AuthMiddleware` for `X-Api-Key` header.
    - Improved streaming response handling with `ConvertCliToClaude` for SSE compatibility.
    - Enhanced request processing and tool-response mapping in translators.
    - Updated README to reflect Claude integration and clarify supported features.
  • Restrict CLI access to localhost and update README for Gemini compatibility
    - Added localhost-only access restriction to `CLIHandler` for security.
    - Updated README to reflect Gemini-compatible API and local access limitation notes.
  • Add FixCLIToolResponse for enhanced function call-response mapping
    - Introduced `FixCLIToolResponse` in `translator` to group function calls with corresponding responses.
    - Updated Gemini handlers to integrate new function for improved response handling.
    - Enhanced error handling in case response mapping fails.
  • Add Gemini-compatible API and improve error handling
    - Introduced a new Gemini-compatible API with routes under `/v1beta`.
    - Added `GeminiHandler` to manage `generateContent` and `streamGenerateContent` actions.
    - Enhanced `AuthMiddleware` to support `X-Goog-Api-Key` header.
    - Improved client metadata handling and added conditional project ID updates in API calls.
    - Updated logging to debug raw API request payloads for better traceability.
  • Enhance response and request handling in translators
    - Refactored response handling to process multiple content parts effectively.
    - Improved `tool_calls` structure with unique ID generation and enhanced mapping logic.
    - Simplified `SystemInstruction` and tool message parsing in requests for better accuracy.
    - Enhanced handling of function calls and tool responses with improved data integration.
  • Refactor API handlers and proxy logic
    - Centralized `getClient` logic into a dedicated function to reduce redundancy.
    - Moved proxy initialization to a new utility function `SetProxy` in `internal/util/proxy.go`.
    - Replaced `Internal` handler with `CLIHandler` in `server.go` for improved clarity and consistency.
    - Removed unused functions and redundant HTTP client setup across the codebase for better maintainability.
  • Add system instruction support and enhance internal API handlers
    - Introduced `SystemInstruction` field in `PrepareRequest` and `GenerateContentRequest` for better message parsing.
    - Updated `SendMessage` and `SendMessageStream` to handle system instructions in client API calls.
    - Enhanced error handling and manual flushing logic in response flows.
    - Added new internal API endpoints `/v1internal:generateContent` and `/v1internal:streamGenerateContent`.
    - Improved proxy handling and transport logic in HTTP client initialization.
  • Add support for Generative Language API Key and improve client initialization
    - Added `GlAPIKey` support in configuration to enable Generative Language API.
    - Integrated `GenerativeLanguageAPIKey` handling in client and API handlers.
    - Updated response translators to manage generative language responses properly.
    - Enhanced HTTP client initialization logic with proxy support for API requests.
    - Refactored streaming and non-streaming flows to account for generative language-specific logic.
  • Enhance quota management and refactor configuration handling
    - Introduced `QuotaExceeded` settings in configuration to handle quota limits more effectively.
    - Added preview model switching logic to `Client` to automatically use fallback models on quota exhaustion.
    - Refactored `APIHandlers` to leverage new configuration structure.
    - Simplified server initialization and removed redundant `ServerConfig` structure.
    - Streamlined client initialization by unifying configuration handling throughout the project.
    - Improved error handling and response mechanisms in both streaming and non-streaming flows.
  • Refactor API handlers to streamline response handling
    - Replaced channel-based handling in `SendMessage` flow with direct synchronous execution.
    - Introduced `hasFirstResponse` flag to manage keep-alive signals in streaming handler.
    - Simplified error handling and removed redundant code for enhanced readability and maintainability.
  • Refactor API request flow and streamline response handling
    - Replaced `SendMessageStream` with synchronous `SendMessage` in API handlers for better manageability.
    - Simplified `ConvertCliToOpenAINonStream` to reduce complexity and improve efficiency.
    - Adjusted `client.go` functions to handle both streaming and non-streaming API requests more effectively.
    - Improved error handling and channel communication in API handlers.
    - Removed redundant and unused code for cleaner implementation.
  • Refactor token management, client initialization, and project handling
    - Consolidated `TokenStorage` struct into `internal/auth/models.go` for better organization.
    - Updated `Client` to use `TokenStorage` for managing email and project ID.
    - Simplified `SetupUser` method to ensure proper token and project assignment.
    - Refactored API handlers to leverage new `GetEmail` and `GetProjectID` methods in `Client`.
    - Cleanup: Removed unused structures and redundant code from `client.go` and `auth.go`.
    - Adjusted CLI flow in `login.go` and `run.go` for streamlined user onboarding.
  • Refactor authentication and service initialization code
    - Moved login and service management logic to `internal/cmd` package (`login.go` and `run.go`).
    - Introduced `DoLogin` and `StartService` functions for modularity.
    - Enhanced error handling by using structured `ErrorMessage` in `Client`.
    - Improved token file saving process and added project-specific token identification.
    - Updated API handlers to handle more detailed error responses, including status codes.
  • Add SOCKS5 and HTTP/HTTPS proxy support
    - Updated `GetAuthenticatedClient` to handle proxy configuration via `proxy-url`.
    - Extended `Config` to include `proxy-url` property.
    - Adjusted error handling and removed unused JSON error response logic for API handlers.
    - Updated documentation and configuration examples to reflect new proxy settings.