ccproxy.core.plugins.hooks¶
ccproxy.core.plugins.hooks
¶
Hook system for CCProxy.
This package provides a flexible, event-driven hook system that enables metrics collection, analytics, logging, and custom provider behaviors without modifying core code.
Key components: - HookEvent: Enumeration of all supported events - HookContext: Context data passed to hooks - Hook: Protocol for hook implementations - HookRegistry: Registry for managing hooks - HookManager: Manager for executing hooks - BackgroundHookThreadManager: Background thread manager for async hook execution
HookContext
dataclass
¶
HookContext(
event,
timestamp,
data,
metadata,
request=None,
response=None,
provider=None,
plugin=None,
error=None,
)
Context passed to all hooks
HookManager
¶
Manages hook execution with error isolation and async/sync support.
The HookManager is responsible for emitting events to registered hooks and ensuring that hook failures don't crash the system. It handles both async and sync hooks by running sync hooks in a thread pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
HookRegistry
|
The hook registry to get hooks from |
required |
background_manager
|
BackgroundHookThreadManager | None
|
Optional background thread manager for fire-and-forget execution |
None
|
Source code in ccproxy/core/plugins/hooks/manager.py
emit
async
¶
Emit an event to all registered hooks.
Creates a HookContext with the provided data and emits it to all hooks registered for the given event. Handles errors gracefully to ensure one failing hook doesn't affect others.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
HookEvent
|
The event to emit |
required |
data
|
dict[str, Any] | None
|
Optional data dictionary to include in context |
None
|
fire_and_forget
|
bool
|
If True, execute hooks in background thread (default) |
True
|
**kwargs
|
Any
|
Additional context fields (request, response, provider, etc.) |
{}
|
Source code in ccproxy/core/plugins/hooks/manager.py
emit_with_context
async
¶
Emit an event using a pre-built HookContext.
This is useful when you need to build the context with specific metadata before emitting the event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
HookContext
|
The HookContext to emit |
required |
fire_and_forget
|
bool
|
If True, execute hooks in background thread (default) |
True
|
Source code in ccproxy/core/plugins/hooks/manager.py
shutdown
¶
Shutdown the background hook processing.
This method should be called during application shutdown to ensure proper cleanup of the background thread.
Source code in ccproxy/core/plugins/hooks/manager.py
HookRegistry
¶
Central registry for all hooks with priority-based ordering.
Source code in ccproxy/core/plugins/hooks/registry.py
register
¶
Register a hook for its events with priority ordering
Source code in ccproxy/core/plugins/hooks/registry.py
unregister
¶
Remove a hook from all events
Source code in ccproxy/core/plugins/hooks/registry.py
get
¶
list
¶
Get summary of all registered hooks organized by event.
Returns:
| Type | Description |
|---|---|
dict[str, list[dict[str, Any]]]
|
Dictionary mapping event names to lists of hook info |
Source code in ccproxy/core/plugins/hooks/registry.py
has
¶
clear
¶
Clear all registered hooks and reset ordering (testing or shutdown).
BackgroundHookThreadManager
¶
Manages a dedicated async thread for hook execution.
Source code in ccproxy/core/plugins/hooks/thread_manager.py
start
¶
Start the background thread with its own event loop.
Source code in ccproxy/core/plugins/hooks/thread_manager.py
stop
¶
Gracefully shutdown the background thread.
Source code in ccproxy/core/plugins/hooks/thread_manager.py
emit_async
¶
Queue a hook task for background execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
HookContext
|
Hook context to execute |
required |
registry
|
Any
|
Hook registry to get hooks from |
required |