Documentation
¶
Overview ¶
Package proxy implements the admin API handler for runtime configuration management.
Package proxy implements interceptor interfaces for request/response processing.
Package proxy implements the HTTP proxy server.
Index ¶
- Constants
- func CompactRequestIfNeeded(req *anthropic.Request, handler *Handler, providerName string, ...) (*anthropic.Request, bool, error)
- func ParseSSEEvent(line string) (event string, data []byte, err error)
- type AdminHandler
- type Compactor
- type HTTPClient
- type Handler
- func (h *Handler) AddRequestInterceptor(interceptor RequestInterceptor)
- func (h *Handler) AddResponseInterceptor(interceptor ResponseInterceptor)
- func (h *Handler) AddStreamingInterceptor(interceptor StreamingResponseInterceptor)
- func (h *Handler) GetActiveProfile() string
- func (h *Handler) GetAdminToken() string
- func (h *Handler) GetConfig() *config.Config
- func (h *Handler) GetProfiles() map[string]config.ProfileConfig
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *Handler) SetActiveProfile(profile string)
- func (h *Handler) SetAdminToken(token string)
- func (h *Handler) SetConfig(cfg *config.Config)
- func (h *Handler) SetInstanceID(id string)
- func (h *Handler) SetProviderClients(clients map[string]HTTPClient)
- func (h *Handler) SetRequestInterceptors(interceptors []RequestInterceptor)
- func (h *Handler) SetResponseInterceptors(interceptors []ResponseInterceptor)
- func (h *Handler) SetRouter(router Router)
- func (h *Handler) SetStreamingClients(clients map[string]HTTPClient)
- func (h *Handler) SetStreamingInterceptors(interceptors []StreamingResponseInterceptor)
- func (h *Handler) SetTransformerRegistry(reg TransformerRegistry)
- func (h *Handler) SetUsageTracker(tracker UsageTracker)
- func (h *Handler) UpdateActiveProfile(profileName string) error
- type ListProfilesResponse
- type LoggingInterceptor
- type MetricsInterceptor
- func (m *MetricsInterceptor) GetMetrics() (requestCount, responseCount, errorCount int, uptime time.Duration)
- func (m *MetricsInterceptor) InterceptRequest(ctx context.Context, req *anthropic.Request) error
- func (m *MetricsInterceptor) InterceptResponse(ctx context.Context, req *anthropic.Request, resp *anthropic.Response) error
- type ProfileResponse
- type RequestInterceptor
- type ResponseInterceptor
- type Router
- type SSEScanner
- type SSEWriter
- type Server
- func (s *Server) ActualAddr() string
- func (s *Server) AddRequestInterceptor(interceptor RequestInterceptor)
- func (s *Server) AddResponseInterceptor(interceptor ResponseInterceptor)
- func (s *Server) AddStreamingInterceptor(interceptor StreamingResponseInterceptor)
- func (s *Server) Addr() string
- func (s *Server) GetAdminToken() string
- func (s *Server) IsRunning() bool
- func (s *Server) SetActiveProfile(profile string)
- func (s *Server) SetAdminToken(token string)
- func (s *Server) SetConfig(cfg *config.Config)
- func (s *Server) SetInstanceID(id string)
- func (s *Server) SetProviderClients(clients map[string]HTTPClient)
- func (s *Server) SetRequestInterceptors(interceptors []RequestInterceptor)
- func (s *Server) SetResponseInterceptors(interceptors []ResponseInterceptor)
- func (s *Server) SetRouter(router Router)
- func (s *Server) SetStreamingClients(clients map[string]HTTPClient)
- func (s *Server) SetStreamingInterceptors(interceptors []StreamingResponseInterceptor)
- func (s *Server) SetTransformerRegistry(reg TransformerRegistry)
- func (s *Server) SetUsageTracker(tracker UsageTracker)
- func (s *Server) Start() error
- func (s *Server) Stop(ctx context.Context) error
- type ServerConfig
- type StreamingResponseInterceptor
- type SwitchProfileRequest
- type SwitchProfileResponse
- type TimingInterceptor
- type TransformerRegistry
- type UsageTracker
Constants ¶
const (
// FilesAPIBetaVersion is the required beta version for Files API
FilesAPIBetaVersion = "files-api-2025-04-14"
)
Variables ¶
This section is empty.
Functions ¶
func CompactRequestIfNeeded ¶
func CompactRequestIfNeeded(req *anthropic.Request, handler *Handler, providerName string, providerCfg config.ProviderConfig) (*anthropic.Request, bool, error)
CompactRequestIfNeeded checks if compaction is needed and performs it. This is a helper function used by the handler during failover.
Types ¶
type AdminHandler ¶
type AdminHandler struct {
// contains filtered or unexported fields
}
AdminHandler handles admin API requests for profile management.
func NewAdminHandler ¶
func NewAdminHandler(handler *Handler) *AdminHandler
NewAdminHandler creates a new admin handler.
func (*AdminHandler) ServeHTTP ¶
func (a *AdminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles admin API requests.
type Compactor ¶
type Compactor interface {
// Compact reduces the request size to fit within the provider's limit.
// Returns the compacted request and true if compaction occurred, or an error.
Compact(req *anthropic.Request) (*anthropic.Request, bool, error)
// ShouldCompact returns true if the request exceeds the provider's limit.
ShouldCompact(reqSize int64) bool
}
Compactor reduces request size to fit within provider limits.
func NewCompactor ¶
func NewCompactor(handler *Handler, providerName string, providerCfg config.ProviderConfig) Compactor
NewCompactor creates a new compactor for the given provider. Returns nil if the provider has no compaction configured.
type HTTPClient ¶
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
DoWithContext(ctx context.Context, req *http.Request) (*http.Response, error)
}
HTTPClient interface for handler dependency.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles HTTP requests.
func (*Handler) AddRequestInterceptor ¶
func (h *Handler) AddRequestInterceptor(interceptor RequestInterceptor)
AddRequestInterceptor adds a single request interceptor.
func (*Handler) AddResponseInterceptor ¶
func (h *Handler) AddResponseInterceptor(interceptor ResponseInterceptor)
AddResponseInterceptor adds a single response interceptor.
func (*Handler) AddStreamingInterceptor ¶
func (h *Handler) AddStreamingInterceptor(interceptor StreamingResponseInterceptor)
AddStreamingInterceptor adds a single streaming interceptor.
func (*Handler) GetActiveProfile ¶
GetActiveProfile returns the current active profile name (thread-safe).
func (*Handler) GetAdminToken ¶
GetAdminToken returns the admin API token.
func (*Handler) GetProfiles ¶
func (h *Handler) GetProfiles() map[string]config.ProfileConfig
GetProfiles returns all configured profiles (thread-safe).
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles HTTP requests.
func (*Handler) SetActiveProfile ¶
SetActiveProfile sets the initial active profile (called during initialization).
func (*Handler) SetAdminToken ¶
SetAdminToken sets the admin API token.
func (*Handler) SetInstanceID ¶
SetInstanceID sets the instance ID.
func (*Handler) SetProviderClients ¶
func (h *Handler) SetProviderClients(clients map[string]HTTPClient)
SetProviderClients sets the provider clients.
func (*Handler) SetRequestInterceptors ¶
func (h *Handler) SetRequestInterceptors(interceptors []RequestInterceptor)
SetRequestInterceptors sets the request interceptors.
func (*Handler) SetResponseInterceptors ¶
func (h *Handler) SetResponseInterceptors(interceptors []ResponseInterceptor)
SetResponseInterceptors sets the response interceptors.
func (*Handler) SetStreamingClients ¶
func (h *Handler) SetStreamingClients(clients map[string]HTTPClient)
SetStreamingClients sets the provider clients for streaming requests. These clients have no timeout and are optimized for SSE streaming.
func (*Handler) SetStreamingInterceptors ¶
func (h *Handler) SetStreamingInterceptors(interceptors []StreamingResponseInterceptor)
SetStreamingInterceptors sets the streaming response interceptors.
func (*Handler) SetTransformerRegistry ¶
func (h *Handler) SetTransformerRegistry(reg TransformerRegistry)
SetTransformerRegistry sets the transformer registry.
func (*Handler) SetUsageTracker ¶
func (h *Handler) SetUsageTracker(tracker UsageTracker)
SetUsageTracker sets the usage tracker.
func (*Handler) UpdateActiveProfile ¶
UpdateActiveProfile switches to a different profile without restart (hot-reload). This is called by the admin API when a profile switch request is received.
type ListProfilesResponse ¶
type ListProfilesResponse struct {
Profiles []ProfileResponse `json:"profiles"`
ActiveProfile string `json:"activeProfile"`
HasProfiles bool `json:"hasProfiles"`
LegacyRoutes map[string]string `json:"legacyRoutes,omitempty"`
}
ListProfilesResponse represents the response for listing profiles.
type LoggingInterceptor ¶
LoggingInterceptor logs request and response details.
func NewLoggingInterceptor ¶
func NewLoggingInterceptor() *LoggingInterceptor
NewLoggingInterceptor creates a new logging interceptor.
func (*LoggingInterceptor) InterceptRequest ¶
InterceptRequest logs request details.
func (*LoggingInterceptor) InterceptResponse ¶
func (l *LoggingInterceptor) InterceptResponse(ctx context.Context, req *anthropic.Request, resp *anthropic.Response) error
InterceptResponse logs response details.
type MetricsInterceptor ¶
type MetricsInterceptor struct {
RequestCount int
ResponseCount int
ErrorCount int
StartTime time.Time
}
MetricsInterceptor tracks request/response metrics.
func NewMetricsInterceptor ¶
func NewMetricsInterceptor() *MetricsInterceptor
NewMetricsInterceptor creates a new metrics interceptor.
func (*MetricsInterceptor) GetMetrics ¶
func (m *MetricsInterceptor) GetMetrics() (requestCount, responseCount, errorCount int, uptime time.Duration)
GetMetrics returns current metrics.
func (*MetricsInterceptor) InterceptRequest ¶
InterceptRequest increments request counter.
func (*MetricsInterceptor) InterceptResponse ¶
func (m *MetricsInterceptor) InterceptResponse(ctx context.Context, req *anthropic.Request, resp *anthropic.Response) error
InterceptResponse increments response counter.
type ProfileResponse ¶
type ProfileResponse struct {
Key string `json:"key"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Routes map[string]string `json:"routes"`
IsActive bool `json:"isActive"`
}
ProfileResponse represents a profile in API responses.
type RequestInterceptor ¶
type RequestInterceptor interface {
// InterceptRequest is called before the request is sent to the provider.
// It can modify the request, log details, or return an error to prevent processing.
InterceptRequest(ctx context.Context, req *anthropic.Request) error
}
RequestInterceptor intercepts and can modify requests before they are processed.
type ResponseInterceptor ¶
type ResponseInterceptor interface {
// InterceptResponse is called after the provider responds but before
// the response is sent to the client.
InterceptResponse(ctx context.Context, req *anthropic.Request, resp *anthropic.Response) error
}
ResponseInterceptor intercepts responses before they are returned to the client.
type Router ¶
type Router interface {
DetectRoute(req router.RouteRequest) string
GetTargets(routeName string) []config.RouteTarget
SetActiveProfile(profile string)
}
Router interface for handler dependency.
type SSEScanner ¶
type SSEScanner struct {
// contains filtered or unexported fields
}
SSEScanner scans SSE events from a reader.
func NewSSEScanner ¶
func NewSSEScanner(r io.Reader) *SSEScanner
NewSSEScanner creates a new SSE scanner.
func (*SSEScanner) Close ¶
func (s *SSEScanner) Close()
Close returns the scanner's buffer to the pool. Must be called when the scanner is no longer needed.
func (*SSEScanner) Event ¶
func (s *SSEScanner) Event() string
Event returns the current event type.
type SSEWriter ¶
type SSEWriter struct {
// contains filtered or unexported fields
}
SSEWriter handles Server-Sent Events writing.
func NewSSEWriter ¶
func NewSSEWriter(w http.ResponseWriter) *SSEWriter
NewSSEWriter creates a new SSE writer.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the HTTP proxy server.
func NewServer ¶
func NewServer(cfg *ServerConfig) (*Server, error)
NewServer creates a new proxy server.
func (*Server) ActualAddr ¶
ActualAddr returns the actual bound address after Start(). When port 0 is used, this returns the OS-assigned port.
func (*Server) AddRequestInterceptor ¶
func (s *Server) AddRequestInterceptor(interceptor RequestInterceptor)
AddRequestInterceptor adds a single request interceptor.
func (*Server) AddResponseInterceptor ¶
func (s *Server) AddResponseInterceptor(interceptor ResponseInterceptor)
AddResponseInterceptor adds a single response interceptor.
func (*Server) AddStreamingInterceptor ¶
func (s *Server) AddStreamingInterceptor(interceptor StreamingResponseInterceptor)
AddStreamingInterceptor adds a single streaming interceptor.
func (*Server) GetAdminToken ¶
GetAdminToken returns the admin API token.
func (*Server) SetActiveProfile ¶
SetActiveProfile sets the initial active profile for the handler and router.
func (*Server) SetAdminToken ¶
SetAdminToken sets the admin API token for runtime management.
func (*Server) SetInstanceID ¶
SetInstanceID sets the instance ID.
func (*Server) SetProviderClients ¶
func (s *Server) SetProviderClients(clients map[string]HTTPClient)
SetProviderClients sets the provider clients.
func (*Server) SetRequestInterceptors ¶
func (s *Server) SetRequestInterceptors(interceptors []RequestInterceptor)
SetRequestInterceptors sets the request interceptors.
func (*Server) SetResponseInterceptors ¶
func (s *Server) SetResponseInterceptors(interceptors []ResponseInterceptor)
SetResponseInterceptors sets the response interceptors.
func (*Server) SetStreamingClients ¶
func (s *Server) SetStreamingClients(clients map[string]HTTPClient)
SetStreamingClients sets the provider clients for streaming requests. These clients have no timeout and are optimized for SSE streaming.
func (*Server) SetStreamingInterceptors ¶
func (s *Server) SetStreamingInterceptors(interceptors []StreamingResponseInterceptor)
SetStreamingInterceptors sets the streaming interceptors.
func (*Server) SetTransformerRegistry ¶
func (s *Server) SetTransformerRegistry(reg TransformerRegistry)
SetTransformerRegistry sets the transformer registry.
func (*Server) SetUsageTracker ¶
func (s *Server) SetUsageTracker(tracker UsageTracker)
SetUsageTracker sets the usage tracker.
type ServerConfig ¶
ServerConfig represents server configuration.
type StreamingResponseInterceptor ¶
type StreamingResponseInterceptor interface {
// InterceptStreamingEvent is called for each SSE event before it is sent to the client.
InterceptStreamingEvent(ctx context.Context, req *anthropic.Request, eventType string, data []byte) ([]byte, error)
}
StreamingResponseInterceptor intercepts streaming responses.
type SwitchProfileRequest ¶
type SwitchProfileRequest struct {
Profile string `json:"profile"`
}
SwitchProfileRequest represents the request body for switching profiles.
type SwitchProfileResponse ¶
type SwitchProfileResponse struct {
Success bool `json:"success"`
ActiveProfile string `json:"activeProfile"`
ProfileName string `json:"profileName,omitempty"`
Routes map[string]string `json:"routes,omitempty"`
Error string `json:"error,omitempty"`
}
SwitchProfileResponse represents the response for switching profiles.
type TimingInterceptor ¶
type TimingInterceptor struct {
// contains filtered or unexported fields
}
TimingInterceptor measures request processing time.
func NewTimingInterceptor ¶
func NewTimingInterceptor() *TimingInterceptor
NewTimingInterceptor creates a new timing interceptor.
func (*TimingInterceptor) InterceptRequest ¶
InterceptRequest records the start time.
func (*TimingInterceptor) InterceptResponse ¶
func (t *TimingInterceptor) InterceptResponse(ctx context.Context, req *anthropic.Request, resp *anthropic.Response) error
InterceptResponse logs the elapsed time.
type TransformerRegistry ¶
type TransformerRegistry interface {
Get(name string) (transformer.Transformer, error)
}
TransformerRegistry interface for handler dependency.
type UsageTracker ¶
type UsageTracker interface {
Record(instanceID, route, model, profile, provider string, tokens, fallbacks int)
}
UsageTracker interface for tracking usage statistics.