Documentation
¶
Overview ¶
Package amp implements the Amp CLI routing module, providing OAuth-based integration with Amp CLI for ChatGPT and Anthropic subscriptions.
Package amp provides model mapping functionality for routing Amp CLI requests to alternative models when the requested model is not available locally.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AmpModule ¶
type AmpModule struct {
// contains filtered or unexported fields
}
AmpModule implements the RouteModuleV2 interface for Amp CLI integration. It provides:
- Reverse proxy to Amp control plane for OAuth/management
- Provider-specific route aliases (/api/provider/{provider}/...)
- Automatic gzip decompression for misconfigured upstreams
- Model mapping for routing unavailable models to alternatives
func New ¶
New creates a new Amp routing module with the given options. This is the preferred constructor using the Option pattern.
Example:
ampModule := amp.New(
amp.WithAccessManager(accessManager),
amp.WithAuthMiddleware(authMiddleware),
amp.WithSecretSource(customSecret),
)
func NewLegacy ¶
func NewLegacy(accessManager *sdkaccess.Manager, authMiddleware gin.HandlerFunc) *AmpModule
NewLegacy creates a new Amp routing module using the legacy constructor signature. This is provided for backwards compatibility.
DEPRECATED: Use New with options instead.
func (*AmpModule) GetModelMapper ¶ added in v6.5.32
func (m *AmpModule) GetModelMapper() *DefaultModelMapper
GetModelMapper returns the model mapper instance (for testing/debugging).
func (*AmpModule) OnConfigUpdated ¶
OnConfigUpdated handles configuration updates. Currently requires restart for URL changes (could be enhanced for dynamic updates).
type AmpRouteType ¶ added in v6.5.32
type AmpRouteType string
AmpRouteType represents the type of routing decision made for an Amp request
const ( // RouteTypeLocalProvider indicates the request is handled by a local OAuth provider (free) RouteTypeLocalProvider AmpRouteType = "LOCAL_PROVIDER" // RouteTypeModelMapping indicates the request was remapped to another available model (free) RouteTypeModelMapping AmpRouteType = "MODEL_MAPPING" // RouteTypeAmpCredits indicates the request is forwarded to ampcode.com (uses Amp credits) RouteTypeAmpCredits AmpRouteType = "AMP_CREDITS" // RouteTypeNoProvider indicates no provider or fallback available RouteTypeNoProvider AmpRouteType = "NO_PROVIDER" )
type DefaultModelMapper ¶ added in v6.5.32
type DefaultModelMapper struct {
// contains filtered or unexported fields
}
DefaultModelMapper implements ModelMapper with thread-safe mapping storage.
func NewModelMapper ¶ added in v6.5.32
func NewModelMapper(mappings []config.AmpModelMapping) *DefaultModelMapper
NewModelMapper creates a new model mapper with the given initial mappings.
func (*DefaultModelMapper) GetMappings ¶ added in v6.5.32
func (m *DefaultModelMapper) GetMappings() map[string]string
GetMappings returns a copy of current mappings (for debugging/status).
func (*DefaultModelMapper) MapModel ¶ added in v6.5.32
func (m *DefaultModelMapper) MapModel(requestedModel string) string
MapModel checks if a mapping exists for the requested model and if the target model has available local providers. Returns the mapped model name or empty string if no valid mapping exists.
func (*DefaultModelMapper) UpdateMappings ¶ added in v6.5.32
func (m *DefaultModelMapper) UpdateMappings(mappings []config.AmpModelMapping)
UpdateMappings refreshes the mapping configuration from config. This is called during initialization and on config hot-reload.
type FallbackHandler ¶
type FallbackHandler struct {
// contains filtered or unexported fields
}
FallbackHandler wraps a standard handler with fallback logic to ampcode.com when the model's provider is not available in CLIProxyAPI
func NewFallbackHandler ¶
func NewFallbackHandler(getProxy func() *httputil.ReverseProxy) *FallbackHandler
NewFallbackHandler creates a new fallback handler wrapper The getProxy function allows lazy evaluation of the proxy (useful when proxy is created after routes)
func NewFallbackHandlerWithMapper ¶ added in v6.5.32
func NewFallbackHandlerWithMapper(getProxy func() *httputil.ReverseProxy, mapper ModelMapper) *FallbackHandler
NewFallbackHandlerWithMapper creates a new fallback handler with model mapping support
func (*FallbackHandler) SetModelMapper ¶ added in v6.5.32
func (fh *FallbackHandler) SetModelMapper(mapper ModelMapper)
SetModelMapper sets the model mapper for this handler (allows late binding)
func (*FallbackHandler) WrapHandler ¶
func (fh *FallbackHandler) WrapHandler(handler gin.HandlerFunc) gin.HandlerFunc
WrapHandler wraps a gin.HandlerFunc with fallback logic If the model's provider is not configured in CLIProxyAPI, it forwards to ampcode.com
type ModelMapper ¶ added in v6.5.32
type ModelMapper interface {
// MapModel returns the target model name if a mapping exists and the target
// model has available providers. Returns empty string if no mapping applies.
MapModel(requestedModel string) string
// UpdateMappings refreshes the mapping configuration (for hot-reload).
UpdateMappings(mappings []config.AmpModelMapping)
}
ModelMapper provides model name mapping/aliasing for Amp CLI requests. When an Amp request comes in for a model that isn't available locally, this mapper can redirect it to an alternative model that IS available.
type MultiSourceSecret ¶
type MultiSourceSecret struct {
// contains filtered or unexported fields
}
MultiSourceSecret implements precedence-based secret lookup: 1. Explicit config value (highest priority) 2. Environment variable AMP_API_KEY 3. File-based secret (lowest priority)
func NewMultiSourceSecret ¶
func NewMultiSourceSecret(explicitKey string, cacheTTL time.Duration) *MultiSourceSecret
NewMultiSourceSecret creates a secret source with precedence and caching
func NewMultiSourceSecretWithPath ¶
func NewMultiSourceSecretWithPath(explicitKey string, filePath string, cacheTTL time.Duration) *MultiSourceSecret
NewMultiSourceSecretWithPath creates a secret source with a custom file path (for testing)
func (*MultiSourceSecret) Get ¶
func (s *MultiSourceSecret) Get(ctx context.Context) (string, error)
Get retrieves the Amp API key using precedence: config > env > file Results are cached for cacheTTL duration to avoid excessive file reads
func (*MultiSourceSecret) InvalidateCache ¶
func (s *MultiSourceSecret) InvalidateCache()
InvalidateCache clears the cached secret, forcing a fresh read on next Get
type Option ¶
type Option func(*AmpModule)
Option configures the AmpModule.
func WithAccessManager ¶
WithAccessManager sets the access manager for the module.
func WithAuthMiddleware ¶
func WithAuthMiddleware(middleware gin.HandlerFunc) Option
WithAuthMiddleware sets the authentication middleware for provider routes.
func WithSecretSource ¶
func WithSecretSource(source SecretSource) Option
WithSecretSource sets a custom secret source for the module.
type SecretSource ¶
SecretSource provides Amp API keys with configurable precedence and caching
type StaticSecretSource ¶
type StaticSecretSource struct {
// contains filtered or unexported fields
}
StaticSecretSource returns a fixed API key (for testing)
func NewStaticSecretSource ¶
func NewStaticSecretSource(key string) *StaticSecretSource
NewStaticSecretSource creates a secret source with a fixed key