haive.agents.base.pre_post_agent_mixin ====================================== .. py:module:: haive.agents.base.pre_post_agent_mixin .. autoapi-nested-parse:: Pre/Post Agent Processing Mixin. This mixin generalizes the pre/post agent pattern from the reflection agents to the enhanced base agent, allowing any agent to have pre-processing and post-processing stages with message transformation support. The pattern supports: - Optional pre-processing agent - Main agent processing - Optional post-processing agent - Message transformation between stages - Hook integration for monitoring .. rubric:: Examples Basic usage with reflection:: class MyReflectionAgent(Agent, PrePostAgentMixin): def setup_agent(self): # Set up main agent self.main_agent = SimpleAgent(name="writer", engine=config) # Set up post-processing with reflection self.post_agent = SimpleAgent(name="reflector", engine=reflection_config) self.use_post_transform = True self.post_transform_type = "reflection" Graded reflection pattern:: class MyGradedAgent(Agent, PrePostAgentMixin): def setup_agent(self): self.pre_agent = SimpleAgent(name="grader", engine=grading_config) self.main_agent = SimpleAgent(name="responder", engine=main_config) self.post_agent = SimpleAgent(name="improver", engine=reflection_config) self.use_pre_transform = False self.use_post_transform = True Factory pattern:: agent = create_reflection_agent( main_agent=SimpleAgent(name="writer", engine=config), reflection_type="graded" ) Classes ------- .. autoapisummary:: haive.agents.base.pre_post_agent_mixin.MessageTransformer haive.agents.base.pre_post_agent_mixin.PrePostAgentMixin Functions --------- .. autoapisummary:: haive.agents.base.pre_post_agent_mixin.create_graded_reflection_agent haive.agents.base.pre_post_agent_mixin.create_reflection_agent haive.agents.base.pre_post_agent_mixin.create_structured_output_agent Module Contents --------------- .. py:class:: MessageTransformer(transformation_type = 'reflection', preserve_first = True) Simple message transformer for reflection patterns. Initialize transformer. :param transformation_type: Type of transformation ("reflection", "ai_to_human", etc.) :param preserve_first: Whether to preserve the first message .. py:method:: transform_messages(messages) Transform messages according to the transformation type. :param messages: Input messages to transform :returns: Transformed messages .. py:class:: PrePostAgentMixin Mixin that adds pre/post agent processing capabilities. This mixin generalizes the PrePostMultiAgent pattern from reflection agents to work with any enhanced agent. It provides: - Optional pre-processing agent - Main agent processing (the agent this mixin is applied to) - Optional post-processing agent - Message transformation between stages - Hook integration for monitoring - Configurable transformation types .. py:method:: arun(input_data) :async: Override arun to use pre/post processing if agents are configured. :param input_data: Input for the agent :returns: Result from pre/post processing or standard arun .. py:method:: model_post_init(__context) Initialize the mixin after Pydantic validation. .. py:method:: run_with_pre_post_processing(input_data) :async: Execute the agent with pre/post processing stages. This method orchestrates the full pre → main → post workflow with proper message transformation and hook integration. :param input_data: Input for the agent workflow :returns: Combined results from all processing stages .. py:method:: setup_transformers() Set up message transformers based on configuration. .. py:function:: create_graded_reflection_agent(main_agent, grading_agent = None, reflection_agent = None, name = None, **kwargs) Create an agent with grading and reflection processing. :param main_agent: The primary agent that generates responses :param grading_agent: Optional custom grading agent :param reflection_agent: Optional custom reflection agent :param name: Name for the enhanced agent :param \*\*kwargs: Additional configuration :returns: Agent with grading and reflection capabilities .. py:function:: create_reflection_agent(main_agent, reflection_agent = None, name = None, **kwargs) Create an agent with reflection post-processing. :param main_agent: The primary agent that generates responses :param reflection_agent: Optional custom reflection agent :param name: Name for the enhanced agent :param \*\*kwargs: Additional configuration :returns: Agent with reflection capabilities .. py:function:: create_structured_output_agent(main_agent, output_model, name = None, **kwargs) Create an agent with structured output post-processing. :param main_agent: The primary agent that generates responses :param output_model: Pydantic model for structured output :param name: Name for the enhanced agent :param \*\*kwargs: Additional configuration :returns: Agent with structured output capabilities