handlers

package
v6.2.33 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2025 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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) 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.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL