cliproxy

package
v6.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package cliproxy provides the core service implementation for the CLI Proxy API. It includes service lifecycle management, authentication handling, file watching, and integration with various AI service providers through a unified interface.

Package cliproxy provides the core service implementation for the CLI Proxy API. It includes service lifecycle management, authentication handling, file watching, and integration with various AI service providers through a unified interface.

Package cliproxy provides the core service implementation for the CLI Proxy API. It includes service lifecycle management, authentication handling, file watching, and integration with various AI service providers through a unified interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIKeyClientProvider

type APIKeyClientProvider interface {
	// Load loads API key-based clients from the configuration.
	//
	// Parameters:
	//   - ctx: The context for the loading operation
	//   - cfg: The application configuration
	//
	// Returns:
	//   - *APIKeyClientResult: The result containing loaded clients
	//   - error: An error if loading fails
	Load(ctx context.Context, cfg *config.Config) (*APIKeyClientResult, error)
}

APIKeyClientProvider loads clients backed directly by configured API keys. It provides an interface for loading API key-based clients for various AI service providers.

func NewAPIKeyClientProvider

func NewAPIKeyClientProvider() APIKeyClientProvider

NewAPIKeyClientProvider returns the default API key client loader that reuses existing logic.

type APIKeyClientResult

type APIKeyClientResult struct {
	// GeminiKeyCount is the number of Gemini API key clients loaded.
	GeminiKeyCount int

	// ClaudeKeyCount is the number of Claude API key clients loaded.
	ClaudeKeyCount int

	// CodexKeyCount is the number of Codex API key clients loaded.
	CodexKeyCount int

	// OpenAICompatCount is the number of OpenAI-compatible API key clients loaded.
	OpenAICompatCount int
}

APIKeyClientResult contains API key based clients along with type counts. It provides metadata about the number of clients loaded for each provider type.

type Builder

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

Builder constructs a Service instance with customizable providers. It provides a fluent interface for configuring all aspects of the service including authentication, file watching, HTTP server options, and lifecycle hooks.

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates a Builder with default dependencies left unset. Use the fluent interface methods to configure the service before calling Build().

Returns:

  • *Builder: A new builder instance ready for configuration

func (*Builder) Build

func (b *Builder) Build() (*Service, error)

Build validates inputs, applies defaults, and returns a ready-to-run service.

func (*Builder) WithAPIKeyClientProvider

func (b *Builder) WithAPIKeyClientProvider(provider APIKeyClientProvider) *Builder

WithAPIKeyClientProvider overrides the provider responsible for API key-backed clients.

func (*Builder) WithAuthManager

func (b *Builder) WithAuthManager(mgr *sdkAuth.Manager) *Builder

WithAuthManager overrides the authentication manager used for token lifecycle operations.

func (*Builder) WithConfig

func (b *Builder) WithConfig(cfg *config.Config) *Builder

WithConfig sets the configuration instance used by the service.

Parameters:

  • cfg: The application configuration

Returns:

  • *Builder: The builder instance for method chaining

func (*Builder) WithConfigPath

func (b *Builder) WithConfigPath(path string) *Builder

WithConfigPath sets the absolute configuration file path used for reload watching.

Parameters:

  • path: The absolute path to the configuration file

Returns:

  • *Builder: The builder instance for method chaining

func (*Builder) WithCoreAuthManager

func (b *Builder) WithCoreAuthManager(mgr *coreauth.Manager) *Builder

WithCoreAuthManager overrides the runtime auth manager responsible for request execution.

func (*Builder) WithHooks

func (b *Builder) WithHooks(h Hooks) *Builder

WithHooks registers lifecycle hooks executed around service startup.

func (*Builder) WithLocalManagementPassword added in v6.0.1

func (b *Builder) WithLocalManagementPassword(password string) *Builder

WithLocalManagementPassword configures a password that is only accepted from localhost management requests.

func (*Builder) WithRequestAccessManager

func (b *Builder) WithRequestAccessManager(mgr *sdkaccess.Manager) *Builder

WithRequestAccessManager overrides the request authentication manager.

func (*Builder) WithServerOptions

func (b *Builder) WithServerOptions(opts ...api.ServerOption) *Builder

WithServerOptions appends server configuration options used during construction.

func (*Builder) WithTokenClientProvider

func (b *Builder) WithTokenClientProvider(provider TokenClientProvider) *Builder

WithTokenClientProvider overrides the provider responsible for token-backed clients.

func (*Builder) WithWatcherFactory

func (b *Builder) WithWatcherFactory(factory WatcherFactory) *Builder

WithWatcherFactory allows customizing the watcher factory that handles reloads.

type Hooks

type Hooks struct {
	// OnBeforeStart is called before the service starts, allowing configuration
	// modifications or additional setup.
	OnBeforeStart func(*config.Config)

	// OnAfterStart is called after the service has started successfully,
	// providing access to the service instance for additional operations.
	OnAfterStart func(*Service)
}

Hooks allows callers to plug into service lifecycle stages. These callbacks provide opportunities to perform custom initialization and cleanup operations during service startup and shutdown.

type ModelInfo

type ModelInfo = registry.ModelInfo

ModelInfo re-exports the registry model info structure.

type ModelRegistry

type ModelRegistry interface {
	RegisterClient(clientID, clientProvider string, models []*ModelInfo)
	UnregisterClient(clientID string)
	SetModelQuotaExceeded(clientID, modelID string)
	ClearModelQuotaExceeded(clientID, modelID string)
	GetAvailableModels(handlerType string) []map[string]any
}

ModelRegistry describes registry operations consumed by external callers.

func GlobalModelRegistry

func GlobalModelRegistry() ModelRegistry

GlobalModelRegistry returns the shared registry instance.

type Service

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

Service wraps the proxy server lifecycle so external programs can embed the CLI proxy. It manages the complete lifecycle including authentication, file watching, HTTP server, and integration with various AI service providers.

func (*Service) RegisterUsagePlugin

func (s *Service) RegisterUsagePlugin(plugin usage.Plugin)

RegisterUsagePlugin registers a usage plugin on the global usage manager. This allows external code to monitor API usage and token consumption.

Parameters:

  • plugin: The usage plugin to register

func (*Service) Run

func (s *Service) Run(ctx context.Context) error

Run starts the service and blocks until the context is cancelled or the server stops. It initializes all components including authentication, file watching, HTTP server, and starts processing requests. The method blocks until the context is cancelled.

Parameters:

  • ctx: The context for controlling the service lifecycle

Returns:

  • error: An error if the service fails to start or run

func (*Service) Shutdown

func (s *Service) Shutdown(ctx context.Context) error

Shutdown gracefully stops background workers and the HTTP server. It ensures all resources are properly cleaned up and connections are closed. The shutdown is idempotent and can be called multiple times safely.

Parameters:

  • ctx: The context for controlling the shutdown timeout

Returns:

  • error: An error if shutdown fails

type TokenClientProvider

type TokenClientProvider interface {
	// Load loads token-based clients from the configured source.
	//
	// Parameters:
	//   - ctx: The context for the loading operation
	//   - cfg: The application configuration
	//
	// Returns:
	//   - *TokenClientResult: The result containing loaded clients
	//   - error: An error if loading fails
	Load(ctx context.Context, cfg *config.Config) (*TokenClientResult, error)
}

TokenClientProvider loads clients backed by stored authentication tokens. It provides an interface for loading authentication tokens from various sources and creating clients for AI service providers.

func NewFileTokenClientProvider

func NewFileTokenClientProvider() TokenClientProvider

NewFileTokenClientProvider returns the default token-backed client loader.

type TokenClientResult

type TokenClientResult struct {
	// SuccessfulAuthed is the number of successfully authenticated clients.
	SuccessfulAuthed int
}

TokenClientResult represents clients generated from persisted tokens. It contains metadata about the loading operation and the number of successful authentications.

type WatcherFactory

type WatcherFactory func(configPath, authDir string, reload func(*config.Config)) (*WatcherWrapper, error)

WatcherFactory creates a watcher for configuration and token changes. The reload callback receives the updated configuration when changes are detected.

Parameters:

  • configPath: The path to the configuration file to watch
  • authDir: The directory containing authentication tokens to watch
  • reload: The callback function to call when changes are detected

Returns:

  • *WatcherWrapper: A watcher wrapper instance
  • error: An error if watcher creation fails

type WatcherWrapper

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

WatcherWrapper exposes the subset of watcher methods required by the SDK.

func (*WatcherWrapper) SetAuthUpdateQueue

func (w *WatcherWrapper) SetAuthUpdateQueue(queue chan<- watcher.AuthUpdate)

SetAuthUpdateQueue registers the channel used to propagate auth updates.

func (*WatcherWrapper) SetConfig

func (w *WatcherWrapper) SetConfig(cfg *config.Config)

SetConfig updates the watcher configuration cache.

func (*WatcherWrapper) SnapshotAuths

func (w *WatcherWrapper) SnapshotAuths() []*coreauth.Auth

SnapshotAuths returns the current auth entries derived from legacy clients.

func (*WatcherWrapper) Start

func (w *WatcherWrapper) Start(ctx context.Context) error

Start proxies to the underlying watcher Start implementation.

func (*WatcherWrapper) Stop

func (w *WatcherWrapper) Stop() error

Stop proxies to the underlying watcher Stop implementation.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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