Documentation
¶
Overview ¶
Package handlers provides core API handler functionality for the CLI Proxy API server. It includes common types, client management, load balancing, and error handling shared across all API endpoint handlers (OpenAI, Claude, Gemini).
Index ¶
- func BuildErrorResponseBody(status int, errText string) []byte
- func StreamingBootstrapRetries(cfg *config.SDKConfig) int
- func StreamingKeepAliveInterval(cfg *config.SDKConfig) time.Duration
- type APIHandlerCancelFunc
- type BaseAPIHandler
- func (h *BaseAPIHandler) ExecuteCountWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)
- func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)
- func (h *BaseAPIHandler) ExecuteWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)
- func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, cancel func(error), data <-chan []byte, ...)
- func (h *BaseAPIHandler) GetAlt(c *gin.Context) string
- func (h *BaseAPIHandler) GetContextWithCancel(handler interfaces.APIHandler, c *gin.Context, ctx context.Context) (context.Context, APIHandlerCancelFunc)
- func (h *BaseAPIHandler) LoggingAPIResponseError(ctx context.Context, err *interfaces.ErrorMessage)
- func (h *BaseAPIHandler) UpdateClients(cfg *config.SDKConfig)
- func (h *BaseAPIHandler) WriteErrorResponse(c *gin.Context, msg *interfaces.ErrorMessage)
- type ErrorDetail
- type ErrorResponse
- type StreamForwardOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildErrorResponseBody ¶ added in v6.6.49
BuildErrorResponseBody builds an OpenAI-compatible JSON error response body. If errText is already valid JSON, it is returned as-is to preserve upstream error payloads.
func StreamingBootstrapRetries ¶ added in v6.6.49
StreamingBootstrapRetries returns how many times a streaming request may be retried before any bytes are sent.
Types ¶
type APIHandlerCancelFunc ¶
type APIHandlerCancelFunc func(params ...interface{})
APIHandlerCancelFunc is a function type for canceling an API handler's context. It can optionally accept parameters, which are used for logging the response.
type BaseAPIHandler ¶
type BaseAPIHandler struct {
// AuthManager manages auth lifecycle and execution in the new architecture.
AuthManager *coreauth.Manager
// Cfg holds the current application configuration.
Cfg *config.SDKConfig
}
BaseAPIHandler contains the handlers for API endpoints. It holds a pool of clients to interact with the backend service and manages load balancing, client selection, and configuration.
func NewBaseAPIHandlers ¶
func NewBaseAPIHandlers(cfg *config.SDKConfig, authManager *coreauth.Manager) *BaseAPIHandler
NewBaseAPIHandlers creates a new API handlers instance. It takes a slice of clients and configuration as input.
Parameters:
- cliClients: A slice of AI service clients
- cfg: The application configuration
Returns:
- *BaseAPIHandler: A new API handlers instance
func (*BaseAPIHandler) ExecuteCountWithAuthManager ¶
func (h *BaseAPIHandler) ExecuteCountWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)
ExecuteCountWithAuthManager executes a non-streaming request via the core auth manager. This path is the only supported execution route.
func (*BaseAPIHandler) ExecuteStreamWithAuthManager ¶
func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)
ExecuteStreamWithAuthManager executes a streaming request via the core auth manager. This path is the only supported execution route.
func (*BaseAPIHandler) ExecuteWithAuthManager ¶
func (h *BaseAPIHandler) ExecuteWithAuthManager(ctx context.Context, handlerType, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)
ExecuteWithAuthManager executes a non-streaming request via the core auth manager. This path is the only supported execution route.
func (*BaseAPIHandler) ForwardStream ¶ added in v6.6.49
func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, cancel func(error), data <-chan []byte, errs <-chan *interfaces.ErrorMessage, opts StreamForwardOptions)
func (*BaseAPIHandler) GetAlt ¶
func (h *BaseAPIHandler) GetAlt(c *gin.Context) string
GetAlt extracts the 'alt' parameter from the request query string. It checks both 'alt' and '$alt' parameters and returns the appropriate value.
Parameters:
- c: The Gin context containing the HTTP request
Returns:
- string: The alt parameter value, or empty string if it's "sse"
func (*BaseAPIHandler) GetContextWithCancel ¶
func (h *BaseAPIHandler) GetContextWithCancel(handler interfaces.APIHandler, c *gin.Context, ctx context.Context) (context.Context, APIHandlerCancelFunc)
GetContextWithCancel creates a new context with cancellation capabilities. It embeds the Gin context and the API handler into the new context for later use. The returned cancel function also handles logging the API response if request logging is enabled.
Parameters:
- handler: The API handler associated with the request.
- c: The Gin context of the current request.
- ctx: The parent context.
Returns:
- context.Context: The new context with cancellation and embedded values.
- APIHandlerCancelFunc: A function to cancel the context and log the response.
func (*BaseAPIHandler) LoggingAPIResponseError ¶
func (h *BaseAPIHandler) LoggingAPIResponseError(ctx context.Context, err *interfaces.ErrorMessage)
func (*BaseAPIHandler) UpdateClients ¶
func (h *BaseAPIHandler) UpdateClients(cfg *config.SDKConfig)
UpdateClients updates the handlers' client list and configuration. This method is called when the configuration or authentication tokens change.
Parameters:
- clients: The new slice of AI service clients
- cfg: The new application configuration
func (*BaseAPIHandler) WriteErrorResponse ¶
func (h *BaseAPIHandler) WriteErrorResponse(c *gin.Context, msg *interfaces.ErrorMessage)
WriteErrorResponse writes an error message to the response writer using the HTTP status embedded in the message.
type ErrorDetail ¶
type ErrorDetail struct {
// Message is a human-readable message providing more details about the error.
Message string `json:"message"`
// Type is the category of error that occurred (e.g., "invalid_request_error").
Type string `json:"type"`
// Code is a short code identifying the error, if applicable.
Code string `json:"code,omitempty"`
}
ErrorDetail provides specific information about an error that occurred. It includes a human-readable message, an error type, and an optional error code.
type ErrorResponse ¶
type ErrorResponse struct {
// Error contains detailed information about the error that occurred.
Error ErrorDetail `json:"error"`
}
ErrorResponse represents a standard error response format for the API. It contains a single ErrorDetail field.
type StreamForwardOptions ¶ added in v6.6.49
type StreamForwardOptions struct {
// KeepAliveInterval overrides the configured streaming keep-alive interval.
// If nil, the configured default is used. If set to <= 0, keep-alives are disabled.
KeepAliveInterval *time.Duration
// WriteChunk writes a single data chunk to the response body. It should not flush.
WriteChunk func(chunk []byte)
// WriteTerminalError writes an error payload to the response body when streaming fails
// after headers have already been committed. It should not flush.
WriteTerminalError func(errMsg *interfaces.ErrorMessage)
// WriteDone optionally writes a terminal marker when the upstream data channel closes
// without an error (e.g. OpenAI's `[DONE]`). It should not flush.
WriteDone func()
// WriteKeepAlive optionally writes a keep-alive heartbeat. It should not flush.
// When nil, a standard SSE comment heartbeat is used.
WriteKeepAlive func()
}
Directories
¶
| Path | Synopsis |
|---|---|
|
Package claude provides HTTP handlers for Claude API code-related functionality.
|
Package claude provides HTTP handlers for Claude API code-related functionality. |
|
Package gemini provides HTTP handlers for Gemini CLI API functionality.
|
Package gemini provides HTTP handlers for Gemini CLI API functionality. |
|
Package openai provides HTTP handlers for OpenAI API endpoints.
|
Package openai provides HTTP handlers for OpenAI API endpoints. |