ingress

package
v0.0.0-...-dc958b9 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAdminTokenMissing = errors.New("admin: token not configured")

Functions

func AdaptForChatGPTBackend

func AdaptForChatGPTBackend(body []byte) ([]byte, error)

func AdaptForChatGPTCompact

func AdaptForChatGPTCompact(body []byte) ([]byte, error)

func ApplyClaudeTarget

func ApplyClaudeTarget(body []byte, model, variant string) ([]byte, error)

func ApplyClaudeVariant

func ApplyClaudeVariant(body []byte, variant string) ([]byte, error)

func DetectClaudeVariant

func DetectClaudeVariant(body []byte, headers http.Header) string

func ExtractModel

func ExtractModel(body []byte) string

func ExtractPreviousResponseID

func ExtractPreviousResponseID(body []byte) string

func ExtractServiceTier

func ExtractServiceTier(body []byte) string

func IsStreaming

func IsStreaming(body []byte) bool

func NormalizeClaudeModel

func NormalizeClaudeModel(model string) string

func ReadAndDecompress

func ReadAndDecompress(r *http.Request) ([]byte, string, error)

func RequireAdmin

func RequireAdmin(token string, h http.HandlerFunc) http.HandlerFunc

func RequireProxyToken

func RequireProxyToken(token string, h http.Handler) http.Handler

func RequireProxyTokenOr

func RequireProxyTokenOr(token string, h http.Handler, allow func(string) bool) http.Handler

Types

type ChatHandler

type ChatHandler struct {
	KeyPool  *broker.APIKeyPool
	Chat     *provider.ChatCompletionsClient
	Recorder RequestRecorder
	Pricer   Pricer
	Logger   *slog.Logger
}

func (*ChatHandler) ServeHTTP

func (h *ChatHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ClaudeFallbackMatch

type ClaudeFallbackMatch struct {
	Rule        ClaudeFallbackRule
	FromModel   string
	FromVariant string
	ToModel     string
	ToVariant   string
}

type ClaudeFallbackPolicy

type ClaudeFallbackPolicy struct {
	Enabled bool                 `json:"enabled"`
	Rules   []ClaudeFallbackRule `json:"rules"`
}

func DefaultClaudeFallbackPolicy

func DefaultClaudeFallbackPolicy() ClaudeFallbackPolicy

func ParseClaudeFallbackPolicy

func ParseClaudeFallbackPolicy(raw []byte) (ClaudeFallbackPolicy, error)

func (ClaudeFallbackPolicy) Match

func (p ClaudeFallbackPolicy) Match(body []byte, headers http.Header) (ClaudeFallbackMatch, bool)

func (ClaudeFallbackPolicy) WithDefaults

func (p ClaudeFallbackPolicy) WithDefaults() ClaudeFallbackPolicy

type ClaudeFallbackRule

type ClaudeFallbackRule struct {
	Enabled     bool   `json:"enabled"`
	FromModel   string `json:"from_model"`
	FromVariant string `json:"from_variant,omitempty"`
	ToModel     string `json:"to_model"`
	ToVariant   string `json:"to_variant,omitempty"`
}

type ClaudeFallbackRuntime

type ClaudeFallbackRuntime struct {
	// contains filtered or unexported fields
}

func NewClaudeFallbackRuntime

func NewClaudeFallbackRuntime(policy ClaudeFallbackPolicy) *ClaudeFallbackRuntime

func (*ClaudeFallbackRuntime) Enabled

func (r *ClaudeFallbackRuntime) Enabled() bool

func (*ClaudeFallbackRuntime) Match

func (r *ClaudeFallbackRuntime) Match(body []byte, headers http.Header) (ClaudeFallbackMatch, bool)

func (*ClaudeFallbackRuntime) Policy

func (*ClaudeFallbackRuntime) Set

type ClaudeTokenRefresher

type ClaudeTokenRefresher interface {
	RefreshClaudeToken(ctx context.Context, acc *broker.ClaudeAccount) error
}

type ClaudeVariantInfo

type ClaudeVariantInfo struct {
	ID          string `json:"id"`
	Label       string `json:"label"`
	Description string `json:"description"`
}

func ClaudeVariantCatalog

func ClaudeVariantCatalog() []ClaudeVariantInfo

type MCPHandler

type MCPHandler struct {
	Pool        *broker.Pool
	Refresher   TokenRefresher
	UpstreamURL string // e.g. "https://chatgpt.com/backend-api/wham/apps"
	Logger      *slog.Logger
}

MCPHandler proxies MCP (Model Context Protocol) Streamable HTTP requests to the ChatGPT backend's MCP endpoint. It handles path rewriting (Codex sends to /api/codex/apps, ChatGPT expects /backend-api/wham/apps), session stickiness via Mcp-Session-Id, and SSE streaming with no timeout.

func (*MCPHandler) ServeHTTP

func (h *MCPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type MessagesHandler

type MessagesHandler struct {
	ClaudePool      *broker.ClaudePool
	KeyPool         *broker.APIKeyPool
	Anthropic       *provider.AnthropicClient
	Vertex          *provider.AnthropicVertexClient
	Recorder        RequestRecorder
	ClaudeRefresher ClaudeTokenRefresher
	Pricer          Pricer
	Priority        string
	FallbackPolicy  ClaudeFallbackPolicy
	FallbackRuntime *ClaudeFallbackRuntime
	Logger          *slog.Logger
}

func (*MessagesHandler) ServeHTTP

func (h *MessagesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ModelsHandler

type ModelsHandler struct{}

func (*ModelsHandler) ServeHTTP

func (h *ModelsHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request)

type PassthroughHandler

type PassthroughHandler struct {
	Pool      *broker.Pool
	Refresher TokenRefresher
	BaseURL   string // upstream base, e.g. "https://chatgpt.com"
	Logger    *slog.Logger
}

PassthroughHandler proxies arbitrary requests to the ChatGPT backend using a pooled account for authentication. It is used for non-responses endpoints such as /backend-api/codex/models and plugin APIs.

func (*PassthroughHandler) ServeHTTP

func (h *PassthroughHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Pricer

type Pricer interface {
	CalculateTokens(input, cached, output int64, model, tier string) (cost float64, priced bool)
	RecordMiss(billingKey string)
}

type RequestRecorder

type RequestRecorder interface {
	RecordRequest(rec UsageRecord)
}

type ResponsesHandler

type ResponsesHandler struct {
	Pool      *broker.Pool
	KeyPool   *broker.APIKeyPool
	ChatGPT   *provider.ChatGPTClient
	OpenAI    *provider.OpenAIClient
	Recorder  RequestRecorder
	Refresher TokenRefresher
	Pricer    Pricer
	Priority  string
	Logger    *slog.Logger
}

func (*ResponsesHandler) ServeHTTP

func (h *ResponsesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type TokenRefresher

type TokenRefresher interface {
	RefreshToken(ctx context.Context, acc *broker.Account) error
}

type UsageRecord

type UsageRecord struct {
	Account      string
	KeyID        string
	Provider     string
	Model        string
	ServiceTier  string
	Route        string
	InputTokens  int64
	CachedTokens int64
	OutputTokens int64
	ReasoningTkn int64
	TotalTokens  int64
	Cost         float64
	CostUnpriced bool
	DurationMs   int64
	Status       int
	Success      bool
	Error        string
	ResponseID   string
}

Jump to

Keyboard shortcuts

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