Commit Graph

17 Commits

  • fix(logging): capture streaming TTFB on first chunk and make timestamps required
    - Add firstChunkTimestamp field to ResponseWriterWrapper for sync capture
    - Capture TTFB in Write() and WriteString() before async channel send
    - Add SetFirstChunkTimestamp() to StreamingLogWriter interface
    - Make requestTimestamp/apiResponseTimestamp required in LogRequest()
    - Remove timestamp capture from WriteAPIResponse() (now via setter)
    - Fix Gemini handler to set API_RESPONSE_TIMESTAMP before writing response
    
    This ensures accurate TTFB measurement for all streaming API formats
    (OpenAI, Gemini, Claude) by capturing timestamp synchronously when
    the first response chunk arrives, not when the stream finalizes.
  • fix(logging): add API response timestamp and fix request timestamp timing
    Previously:
    - REQUEST INFO timestamp was captured at log write time (not request arrival)
    - API RESPONSE had NO timestamp at all
    
    This fix:
    - Captures REQUEST INFO timestamp when request first arrives
    - Adds API RESPONSE timestamp when upstream response arrives
    
    Changes:
    - Add Timestamp field to RequestInfo, set at middleware initialization
    - Set API_RESPONSE_TIMESTAMP in appendAPIResponse() and gemini handler
    - Pass timestamps through logging chain to writeNonStreamingLog()
    - Add timestamp output to API RESPONSE section
    
    This enables accurate measurement of backend response latency in error logs.
  • fix(api): ensure all response writes are captured for logging
    The response writer wrapper has been refactored to more reliably capture response bodies for logging, fixing several edge cases.
    
    - Implements `WriteString` to capture writes from `io.StringWriter`, which were previously missed by the `Write` method override.
    - A new `shouldBufferResponseBody` helper centralizes the logic to ensure the body is buffered only when logging is active or for errors when `logOnErrorOnly` is enabled.
    - Streaming detection is now more robust. It correctly handles non-streaming error responses (e.g., `application/json`) that are generated for a request that was intended to be streaming.
    
    BREAKING CHANGE: The public methods `Status()`, `Size()`, and `Written()` have been removed from the `ResponseWriterWrapper` as they are no longer required by the new implementation.
  • **feat(logging, middleware): add error-based logging support and error log management**
    - Introduced `logOnErrorOnly` mode to enable logging only for error responses when request logging is disabled.
    - Added endpoints to list and download error logs (`/request-error-logs`).
    - Implemented error log file cleanup to retain only the newest 10 logs.
    - Refactored `ResponseWriterWrapper` to support forced logging for error responses.
    - Enhanced middleware to capture data for upstream error persistence.
    - Improved log file naming and error log filename generation.
  • 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.
  • 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`.
  • 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 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