proxy

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 17 Imported by: 0

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

View Source
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.

func ParseSSEEvent

func ParseSSEEvent(line string) (event string, data []byte, err error)

ParseSSEEvent parses an SSE line (or multi-line string) into event type and data.

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 NewHandler

func NewHandler(maxRequestSize int64) *Handler

NewHandler creates a new handler.

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

func (h *Handler) GetActiveProfile() string

GetActiveProfile returns the current active profile name (thread-safe).

func (*Handler) GetAdminToken

func (h *Handler) GetAdminToken() string

GetAdminToken returns the admin API token.

func (*Handler) GetConfig

func (h *Handler) GetConfig() *config.Config

GetConfig returns the current configuration (thread-safe).

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

func (h *Handler) SetActiveProfile(profile string)

SetActiveProfile sets the initial active profile (called during initialization).

func (*Handler) SetAdminToken

func (h *Handler) SetAdminToken(token string)

SetAdminToken sets the admin API token.

func (*Handler) SetConfig

func (h *Handler) SetConfig(cfg *config.Config)

SetConfig sets the configuration.

func (*Handler) SetInstanceID

func (h *Handler) SetInstanceID(id string)

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

func (h *Handler) SetRouter(router Router)

SetRouter sets the router.

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

func (h *Handler) UpdateActiveProfile(profileName string) error

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

type LoggingInterceptor struct {
	LogRequestDetails  bool
	LogResponseDetails bool
}

LoggingInterceptor logs request and response details.

func NewLoggingInterceptor

func NewLoggingInterceptor() *LoggingInterceptor

NewLoggingInterceptor creates a new logging interceptor.

func (*LoggingInterceptor) InterceptRequest

func (l *LoggingInterceptor) InterceptRequest(ctx context.Context, req *anthropic.Request) error

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

func (m *MetricsInterceptor) InterceptRequest(ctx context.Context, req *anthropic.Request) error

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

func (s *SSEScanner) Data() []byte

Data returns the current event data.

func (*SSEScanner) Err

func (s *SSEScanner) Err() error

Err returns any error encountered.

func (*SSEScanner) Event

func (s *SSEScanner) Event() string

Event returns the current event type.

func (*SSEScanner) Scan

func (s *SSEScanner) Scan() bool

Scan advances to the next event.

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.

func (*SSEWriter) Flush

func (s *SSEWriter) Flush()

Flush flushes the response.

func (*SSEWriter) WriteEvent

func (s *SSEWriter) WriteEvent(event string, data []byte) error

WriteEvent writes an SSE event.

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

func (s *Server) ActualAddr() string

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

func (s *Server) Addr() string

Addr returns the configured server address.

func (*Server) GetAdminToken

func (s *Server) GetAdminToken() string

GetAdminToken returns the admin API token.

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns true if the server is running.

func (*Server) SetActiveProfile

func (s *Server) SetActiveProfile(profile string)

SetActiveProfile sets the initial active profile for the handler and router.

func (*Server) SetAdminToken

func (s *Server) SetAdminToken(token string)

SetAdminToken sets the admin API token for runtime management.

func (*Server) SetConfig

func (s *Server) SetConfig(cfg *config.Config)

SetConfig sets the configuration.

func (*Server) SetInstanceID

func (s *Server) SetInstanceID(id string)

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

func (s *Server) SetRouter(router Router)

SetRouter sets the router for the handler.

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.

func (*Server) Start

func (s *Server) Start() error

Start starts the server and waits until it's ready to accept connections.

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop stops the server.

type ServerConfig

type ServerConfig struct {
	Host           string
	Port           int
	MaxRequestSize int64
}

ServerConfig represents server configuration.

func Defaults

func Defaults() *ServerConfig

Defaults returns default 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

func (t *TimingInterceptor) InterceptRequest(ctx context.Context, req *anthropic.Request) error

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.

Jump to

Keyboard shortcuts

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