praisonaiagents.hooks.middleware
Core SDK Middleware System for PraisonAI Agents. Provides wrap_model_call and wrap_tool_call patterns for intercepting and modifying agent behavior at model/tool call boundaries. Zero Performance Impact:- All middleware is optional
- Empty middleware chain is O(1) fast path
- No overhead when middleware not registered
Overview
This module provides components for middleware.Classes
InvocationContext
Context passed through middleware chain.
ModelRequest
Request data for model calls.
ModelResponse
Response data from model calls.
ToolRequest
Request data for tool calls.
ToolResponse
Response data from tool calls.
MiddlewareChain
Synchronous middleware chain executor.
AsyncMiddlewareChain
Asynchronous middleware chain executor.
MiddlewareManager
Manages middleware for an agent.
Functions
before_model()
Decorator to mark a function as a before_model hook.
after_model()
Decorator to mark a function as an after_model hook.
wrap_model_call()
Decorator to mark a function as a wrap_model_call middleware.
before_tool()
Decorator to mark a function as a before_tool hook.
after_tool()
Decorator to mark a function as an after_tool hook.
wrap_tool_call()
Decorator to mark a function as a wrap_tool_call middleware.
get_hook_type()
Get the hook type of a decorated function.
is_middleware()
Check if a function is a middleware (wrap_* type).
categorize_hooks()
Categorize hooks by their type.
Constants
| Name | Value |
|---|---|
T | TypeVar('T') |

