ccproxy.core.plugins¶
ccproxy.core.plugins
¶
CCProxy Plugin System public API (minimal re-exports).
This module exposes the common symbols used by plugins and app code while keeping imports straightforward to avoid circular dependencies.
AuthCommandSpec
dataclass
¶
AuthCommandSpec(
command_name, description, handler, options=dict()
)
Specification for auth commands.
FormatAdapterSpec
dataclass
¶
Specification for format adapter registration.
MiddlewareSpec
dataclass
¶
MiddlewareSpec(
middleware_class, priority=APPLICATION, kwargs=dict()
)
Specification for plugin middleware.
PluginContext
¶
Context provided to plugin runtime during initialization.
Source code in ccproxy/core/plugins/declaration.py
get_service
¶
Get a service instance by type with proper type safety.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_type
|
type[T]
|
The type of service to retrieve |
required |
Returns:
| Type | Description |
|---|---|
T
|
The service instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the service is not available |
Source code in ccproxy/core/plugins/declaration.py
get
¶
Get service by type (new) or by string key (backward compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_or_type
|
type[T] | str
|
Service type for type-safe access or string key for compatibility |
required |
default
|
Any
|
Default value for string-based access (ignored for type-safe access) |
None
|
Returns:
| Type | Description |
|---|---|
T | Any
|
Service instance for type-safe access, or attribute value for string access |
Source code in ccproxy/core/plugins/declaration.py
get_attr
¶
Get attribute by string name - for backward compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
String attribute name |
required |
default
|
Any
|
Default value if attribute not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Attribute value or default |
Source code in ccproxy/core/plugins/declaration.py
keys
¶
Backward compatibility: Return list of available service keys.
Source code in ccproxy/core/plugins/declaration.py
PluginManifest
dataclass
¶
PluginManifest(
name,
version,
description="",
dependencies=list(),
is_provider=False,
provides=list(),
requires=list(),
optional_requires=list(),
middleware=list(),
routes=list(),
tasks=list(),
hooks=list(),
auth_commands=list(),
config_class=None,
tool_accumulator_class=None,
oauth_client_factory=None,
oauth_provider_factory=None,
token_manager_factory=None,
oauth_config_class=None,
oauth_routes=list(),
format_adapters=list(),
requires_format_adapters=list(),
cli_commands=list(),
cli_arguments=list(),
)
Complete static declaration of a plugin's capabilities.
This manifest is created at module import time and contains all static information needed to integrate the plugin into the application.
validate_dependencies
¶
validate_service_dependencies
¶
Validate that required services are available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
available_services
|
set[str]
|
Set of available service names |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of missing required services |
Source code in ccproxy/core/plugins/declaration.py
get_sorted_middleware
¶
validate_format_adapter_requirements
¶
Validate that required format adapters are available.
Source code in ccproxy/core/plugins/declaration.py
PluginRuntimeProtocol
¶
RouteSpec
dataclass
¶
Specification for plugin routes.
TaskSpec
dataclass
¶
TaskSpec(
task_name,
task_type,
task_class,
interval_seconds,
enabled=True,
kwargs=dict(),
)
Specification for scheduled tasks.
BaseProviderPluginFactory
¶
Bases: ProviderPluginFactory
Base factory for provider plugins that eliminates common boilerplate.
This class uses class attributes for plugin configuration and implements common methods that all provider factories share. Subclasses only need to define class attributes and override methods that need custom behavior.
Required class attributes to be defined by subclasses: - plugin_name: str - plugin_description: str - runtime_class: type[ProviderPluginRuntime] - adapter_class: type[BaseAdapter] - config_class: type[BaseSettings]
Optional class attributes with defaults: - plugin_version: str = "1.0.0" - detection_service_class: type | None = None - credentials_manager_class: type | None = None - router: APIRouter | None = None - route_prefix: str = "/api" - dependencies: list[str] = [] - optional_requires: list[str] = [] - tasks: list[TaskSpec] = []
Source code in ccproxy/core/plugins/factories.py
validate_format_adapters_with_settings
¶
Validate format adapter specifications (feature flags removed).
create_runtime
¶
create_adapter
async
¶
Create adapter instance with explicit dependencies.
This method extracts services from context and creates the adapter with explicit dependency injection. Subclasses can override this method if they need custom adapter creation logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PluginContext
|
Plugin context |
required |
Returns:
| Type | Description |
|---|---|
BaseAdapter
|
Adapter instance |
Source code in ccproxy/core/plugins/factories.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
create_detection_service
¶
Create detection service instance if class is configured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PluginContext
|
Plugin context |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Detection service instance or None if no class configured |
Source code in ccproxy/core/plugins/factories.py
create_credentials_manager
async
¶
Resolve credentials manager via the shared auth registry.
Source code in ccproxy/core/plugins/factories.py
get_auth_manager_name
¶
Get auth manager name, allowing config override.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PluginContext
|
Plugin context containing config |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Auth manager name or None if not configured |
Source code in ccproxy/core/plugins/factories.py
create_context
¶
Create context with provider-specific components.
This method provides a hook for subclasses to customize context creation. The default implementation just returns the base context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_services
|
Core services container |
required |
Returns:
| Type | Description |
|---|---|
PluginContext
|
Plugin context |
Source code in ccproxy/core/plugins/factories.py
PluginRegistry
¶
Registry for managing plugin factories and runtime instances.
Source code in ccproxy/core/plugins/factories.py
register_service
¶
Register a service provided by a plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_name
|
str
|
Name of the service |
required |
service_instance
|
Any
|
Service instance |
required |
provider_plugin
|
str
|
Name of the plugin providing the service |
required |
Source code in ccproxy/core/plugins/factories.py
get_service
¶
Get a service by name with optional type checking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_name
|
str
|
Name of the service |
required |
service_type
|
type[T] | None
|
Optional expected service type |
None
|
Returns:
| Type | Description |
|---|---|
T | None
|
Service instance or None if not found |
Source code in ccproxy/core/plugins/factories.py
has_service
¶
get_required_services
¶
Get required and optional services for a plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin_name
|
str
|
Name of the plugin |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[str], list[str]]
|
Tuple of (required_services, optional_services) |
Source code in ccproxy/core/plugins/factories.py
register_factory
¶
Register a plugin factory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factory
|
PluginFactory
|
Plugin factory to register |
required |
Source code in ccproxy/core/plugins/factories.py
get_factory
¶
Get a plugin factory by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Plugin name |
required |
Returns:
| Type | Description |
|---|---|
PluginFactory | None
|
Plugin factory or None |
get_all_manifests
¶
Get all registered plugin manifests.
Returns:
| Type | Description |
|---|---|
dict[str, PluginManifest]
|
Dictionary mapping plugin names to manifests |
Source code in ccproxy/core/plugins/factories.py
resolve_dependencies
¶
Resolve plugin dependencies and return initialization order.
Skips plugins with missing hard dependencies or required services instead of failing the entire plugin system. Logs skipped plugins and continues with the rest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Settings
|
Settings instance |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of plugin names in initialization order |
Source code in ccproxy/core/plugins/factories.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |
create_runtime
async
¶
Create and initialize a plugin runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Plugin name |
required |
service_container
|
ServiceContainer
|
Service container with all available services |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Initialized plugin runtime |
Raises:
| Type | Description |
|---|---|
ValueError
|
If plugin not found |
Source code in ccproxy/core/plugins/factories.py
initialize_all
async
¶
Initialize all registered plugins with format adapter support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_container
|
ServiceContainer
|
Service container with all available services |
required |
Source code in ccproxy/core/plugins/factories.py
shutdown_all
async
¶
Shutdown all plugin runtimes in reverse initialization order.
Source code in ccproxy/core/plugins/factories.py
get_runtime
¶
list_plugins
¶
AuthProviderPluginFactory
¶
Bases: BasePluginFactory
Factory for authentication provider plugins.
Auth provider plugins provide OAuth authentication flows and token management without directly proxying requests to API providers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
PluginManifest
|
Plugin manifest |
required |
Source code in ccproxy/core/plugins/interfaces.py
create_context
¶
Create context with auth provider-specific components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_services
|
Core services container |
required |
Returns:
| Type | Description |
|---|---|
PluginContext
|
Plugin context with auth provider components |
Source code in ccproxy/core/plugins/interfaces.py
get_auth_manager_registry_name
¶
Return registry key used for this auth manager.
create_auth_provider
abstractmethod
¶
Create the OAuth provider for this auth plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PluginContext | None
|
Optional plugin context for initialization |
None
|
Returns:
| Type | Description |
|---|---|
OAuthProviderProtocol
|
OAuth provider instance implementing OAuthProviderProtocol |
Source code in ccproxy/core/plugins/interfaces.py
create_token_manager
¶
Create the token manager for this auth plugin.
Returns:
| Type | Description |
|---|---|
BaseTokenManager[Any] | None
|
Token manager instance or None if not needed |
create_storage
¶
Create the storage implementation for this auth plugin.
Returns:
| Type | Description |
|---|---|
TokenStorage[Any] | None
|
Storage instance or None if using default |
BasePluginFactory
¶
Bases: PluginFactory
Base implementation of plugin factory.
This class provides common functionality for creating plugin runtime instances from manifests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
PluginManifest
|
Plugin manifest |
required |
runtime_class
|
type[Any]
|
Runtime class to instantiate |
required |
Source code in ccproxy/core/plugins/interfaces.py
get_manifest
¶
create_runtime
¶
create_context
¶
Create base context for plugin initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_container
|
ServiceContainer
|
Service container with all available services |
required |
Returns:
| Type | Description |
|---|---|
PluginContext
|
Plugin context with base services |
Source code in ccproxy/core/plugins/interfaces.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
PluginFactory
¶
Bases: ABC
Abstract factory for creating plugin runtime instances.
Each plugin must provide a factory that knows how to create its runtime instance from its manifest.
get_manifest
abstractmethod
¶
Get the plugin manifest with static declarations.
Returns:
| Type | Description |
|---|---|
PluginManifest
|
Plugin manifest |
create_runtime
abstractmethod
¶
create_context
abstractmethod
¶
Create the context for plugin initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_services
|
ServiceContainer
|
Core services container |
required |
Returns:
| Type | Description |
|---|---|
PluginContext
|
Plugin context with required services |
Source code in ccproxy/core/plugins/interfaces.py
ProviderPluginFactory
¶
Bases: BasePluginFactory
Factory for provider plugins.
Provider plugins require additional components like adapters and detection services that must be created during initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
PluginManifest
|
Plugin manifest |
required |
Source code in ccproxy/core/plugins/interfaces.py
create_context
¶
Create context with provider-specific components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_services
|
Core services container |
required |
Returns:
| Type | Description |
|---|---|
PluginContext
|
Plugin context with provider components |
Source code in ccproxy/core/plugins/interfaces.py
create_adapter
abstractmethod
async
¶
Create the adapter for this provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PluginContext
|
Plugin context |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Provider adapter instance |
create_detection_service
abstractmethod
¶
Create the detection service for this provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PluginContext
|
Plugin context |
required |
Returns:
| Type | Description |
|---|---|
DetectionServiceProtocol | None
|
Detection service instance or None |
Source code in ccproxy/core/plugins/interfaces.py
create_credentials_manager
abstractmethod
async
¶
Create the credentials manager for this provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PluginContext
|
Plugin context |
required |
Returns:
| Type | Description |
|---|---|
BaseTokenManager[Any] | None
|
Credentials manager instance or None |
Source code in ccproxy/core/plugins/interfaces.py
SystemPluginFactory
¶
Bases: BasePluginFactory
Factory for system plugins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
PluginManifest
|
Plugin manifest |
required |
Source code in ccproxy/core/plugins/interfaces.py
CoreMiddlewareSpec
dataclass
¶
CoreMiddlewareSpec(
middleware_class,
priority=APPLICATION,
kwargs=dict(),
source="core",
)
Bases: MiddlewareSpec
Specification for core application middleware.
Extends MiddlewareSpec with a source field to distinguish between core and plugin middleware.
MiddlewareManager
¶
Manages middleware registration and ordering.
Source code in ccproxy/core/plugins/middleware.py
add_core_middleware
¶
Add core application middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
middleware_class
|
type[BaseHTTPMiddleware]
|
Middleware class |
required |
priority
|
int
|
Priority for ordering |
APPLICATION
|
**kwargs
|
Any
|
Additional middleware arguments |
{}
|
Source code in ccproxy/core/plugins/middleware.py
add_plugin_middleware
¶
Add middleware from a plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin_name
|
str
|
Name of the plugin |
required |
specs
|
list[MiddlewareSpec]
|
List of middleware specifications |
required |
Source code in ccproxy/core/plugins/middleware.py
get_ordered_middleware
¶
Get all middleware sorted by priority.
Returns:
| Type | Description |
|---|---|
list[CoreMiddlewareSpec]
|
List of middleware specs sorted by priority (lower first) |
Source code in ccproxy/core/plugins/middleware.py
apply_to_app
¶
Apply all middleware to the FastAPI app in correct order.
Note: Middleware in FastAPI/Starlette is applied in reverse order (last added runs first), so we add them in reverse priority order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
FastAPI application |
required |
Source code in ccproxy/core/plugins/middleware.py
get_middleware_summary
¶
Get a summary of registered middleware.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with middleware statistics and order |
Source code in ccproxy/core/plugins/middleware.py
AuthProviderPluginRuntime
¶
Bases: BasePluginRuntime
Runtime for authentication provider plugins.
Auth provider plugins provide OAuth authentication flows and token management for various API providers without directly proxying requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
PluginManifest
|
Plugin manifest with static declarations |
required |
Source code in ccproxy/core/plugins/runtime.py
BasePluginRuntime
¶
Bases: PluginRuntimeProtocol
Base implementation of plugin runtime.
This class provides common functionality for all plugin runtimes. Specific plugin types (system, provider) can extend this base class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
PluginManifest
|
Plugin manifest with static declarations |
required |
Source code in ccproxy/core/plugins/runtime.py
initialize
async
¶
Initialize the plugin with runtime context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PluginContext
|
Runtime context with services and configuration |
required |
Source code in ccproxy/core/plugins/runtime.py
shutdown
async
¶
Cleanup on shutdown.
Source code in ccproxy/core/plugins/runtime.py
validate
async
¶
Validate plugin is ready.
Returns:
| Type | Description |
|---|---|
bool
|
True if plugin is ready, False otherwise |
Source code in ccproxy/core/plugins/runtime.py
health_check
async
¶
Perform health check.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Health check result following IETF format |
Source code in ccproxy/core/plugins/runtime.py
ProviderPluginRuntime
¶
Bases: BasePluginRuntime
Runtime for provider plugins.
Provider plugins proxy requests to external API providers and require additional components like adapters and detection services.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
PluginManifest
|
Plugin manifest with static declarations |
required |
Source code in ccproxy/core/plugins/runtime.py
SystemPluginRuntime
¶
Bases: BasePluginRuntime
Runtime for system plugins (non-provider plugins).
System plugins provide functionality like logging, monitoring, permissions, etc., but don't proxy to external providers.
Source code in ccproxy/core/plugins/runtime.py
factory_type_name
¶
Return a stable type name for a plugin factory.
Returns one of: "auth_provider", "provider", "system", or "plugin" (fallback).
Source code in ccproxy/core/plugins/interfaces.py
load_cli_plugins
¶
Load filtered plugins for CLI operations.
This function creates a lightweight plugin registry for CLI commands that: - Includes only CLI-safe plugins (marked with cli_safe = True) - Optionally includes a specific auth provider plugin if requested - Excludes heavy provider plugins that cause DuckDB locks, task manager errors, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Any
|
Application settings |
required |
auth_provider
|
str | None
|
Name of auth provider to include (e.g., "codex", "claude-api") |
None
|
allow_plugins
|
list[str] | None
|
Additional plugins to explicitly allow (beyond cli_safe ones) |
None
|
Returns:
| Type | Description |
|---|---|
PluginRegistry
|
Filtered PluginRegistry containing only CLI-appropriate plugins |
Source code in ccproxy/core/plugins/loader.py
load_plugin_system
¶
Discover plugins and build a registry + middleware manager.
This function is the single entry point to set up the plugin layer for the application factory. It avoids scattering discovery/registry logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Settings
|
Application settings (with plugin config) |
required |
Returns:
| Type | Description |
|---|---|
tuple[PluginRegistry, MiddlewareManager]
|
Tuple of (PluginRegistry, MiddlewareManager) |
Source code in ccproxy/core/plugins/loader.py
setup_default_middleware
¶
Setup default core middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
MiddlewareManager
|
Middleware manager |
required |