Documentation
¶
Index ¶
- Constants
- func BuildUpstreamURL(baseURL, apiPath string) string
- func CountTokens(body []byte) int
- func DetectScene(body []byte, cfg SceneConfig) string
- func FormatToPath(format string) string
- func GeminiGeneratePath(model string, stream bool) string
- type ConfigRouter
- type KeyManager
- func (m *KeyManager) AllKeys(provider string) []string
- func (m *KeyManager) GetKey(provider string) (string, bool)
- func (m *KeyManager) HasFallbackKeys(provider string) bool
- func (m *KeyManager) Mark429(provider, apiKey string) bool
- func (m *KeyManager) RegisterProvider(provider string, primaryKey string, fallbackKeys []string)
- func (m *KeyManager) ResetKey(provider, apiKey string)
- func (m *KeyManager) SyncProviders(entries []ProviderKeys)
- type ProviderKeys
- type RouteResult
- type Router
- type SceneConfig
Constants ¶
const ( SceneDefault = "default" SceneLongContext = "longContext" SceneBackground = "background" SceneWebSearch = "websearch" SceneThink = "think" SceneImage = "image" )
Variables ¶
This section is empty.
Functions ¶
func BuildUpstreamURL ¶
BuildUpstreamURL constructs the full upstream URL from base_url and api_path. Handles both cases: base_url with or without /v1 suffix.
func CountTokens ¶ added in v0.1.4
CountTokens counts the approximate token count of an Anthropic request body. It extracts text from messages, system prompts, and tools. Returns 0 if encoding fails or body is invalid JSON.
func DetectScene ¶
func DetectScene(body []byte, cfg SceneConfig) string
DetectScene detects the Claude Code scenario from an Anthropic request body. Detection priority (matching cc-router): 1. longContext — token count exceeds threshold (> 0 to enable) 2. background — model name contains "haiku" 3. websearch — tools contain web_search_* type 4. think — thinking field present 5. image — user messages contain image content blocks 6. default — fallback
func FormatToPath ¶ added in v0.1.4
FormatToPath returns the default upstream API path for a given protocol format.
func GeminiGeneratePath ¶ added in v0.1.11
GeminiGeneratePath returns the Gemini API path for generateContent.
Types ¶
type ConfigRouter ¶
type ConfigRouter struct {
// contains filtered or unexported fields
}
ConfigRouter implements Router using config-based routing rules.
func NewConfigRouter ¶
func NewConfigRouter(provider *config.Provider) *ConfigRouter
NewConfigRouter creates a new ConfigRouter.
func (*ConfigRouter) Route ¶
func (r *ConfigRouter) Route(clientProtocol, apiKey string, body []byte) (*RouteResult, error)
Route resolves a request to an upstream provider + model.
type KeyManager ¶ added in v0.1.12
type KeyManager struct {
// contains filtered or unexported fields
}
KeyManager manages API key selection with 429 tracking and cooldown. Thread-safe. Each provider has its own key state.
func NewKeyManager ¶ added in v0.1.12
func NewKeyManager() *KeyManager
NewKeyManager creates an empty KeyManager.
func (*KeyManager) AllKeys ¶ added in v0.1.12
func (m *KeyManager) AllKeys(provider string) []string
AllKeys returns all registered keys for a provider (primary + fallbacks).
func (*KeyManager) GetKey ¶ added in v0.1.12
func (m *KeyManager) GetKey(provider string) (string, bool)
GetKey returns the best available key for the given provider. Priority: primary → fallback keys in order. Skips keys in cooldown. Returns ("", false) if all keys are in cooldown or provider not found.
func (*KeyManager) HasFallbackKeys ¶ added in v0.1.12
func (m *KeyManager) HasFallbackKeys(provider string) bool
HasFallbackKeys returns true if the provider has fallback keys registered.
func (*KeyManager) Mark429 ¶ added in v0.1.12
func (m *KeyManager) Mark429(provider, apiKey string) bool
Mark429 records a 429 for the given key. Returns true if the key was cooled down.
func (*KeyManager) RegisterProvider ¶ added in v0.1.12
func (m *KeyManager) RegisterProvider(provider string, primaryKey string, fallbackKeys []string)
RegisterProvider registers the primary and fallback keys for a provider.
func (*KeyManager) ResetKey ¶ added in v0.1.12
func (m *KeyManager) ResetKey(provider, apiKey string)
ResetKey clears consecutive 429 count for the given key (called on success).
func (*KeyManager) SyncProviders ¶ added in v0.1.12
func (m *KeyManager) SyncProviders(entries []ProviderKeys)
SyncProviders rebuilds all provider key states from the given map. Providers not in the map are removed. This is safe to call on reload.
type ProviderKeys ¶ added in v0.1.12
ProviderKeys holds the keys for a single provider for bulk sync.
type RouteResult ¶
type RouteResult struct {
ProviderKey string // config provider key
BaseURL string // upstream base_url
APIKey string // upstream api_key
Format string // upstream format (chat/anthropic/responses)
Model string // resolved model name to send upstream
Path string // upstream API path (resolved from format or overridden by config)
ThinkTag string // optional: strip <tag>...</tag> from responses
CustomHeaders map[string]string // optional: extra headers sent upstream, overriding forwarded client headers (e.g. User-Agent for UA-gated upstreams)
}
RouteResult holds the resolved routing decision.
type Router ¶
type Router interface {
// Route makes a routing decision. clientProtocol is "anthropic"/"responses"/"chat".
// apiKey is from the client's auth header. body is the raw request JSON.
Route(clientProtocol, apiKey string, body []byte) (*RouteResult, error)
}
Router resolves a request to an upstream provider + model.
type SceneConfig ¶
type SceneConfig struct {
LongContextThreshold int
}
SceneConfig holds configuration for scene detection.