haive.agents.dynamic_supervisor.stateΒΆ
State schemas for dynamic supervisor agent.
from typing import Any This module defines the state management for the dynamic supervisor, including agent registry, routing control, and tool generation. Two versions are provided: - SupervisorState: Uses exclude=True for agent serialization (v1) - SupervisorStateV2: Attempts full agent serialization (experimental)
- Classes:
SupervisorState: Base supervisor state with agent registry SupervisorStateWithTools: Extends base with dynamic tool generation SupervisorStateV2: Experimental version with full serialization
Examples
Creating and managing supervisor state:
state = SupervisorState()
state.add_agent("search", search_agent, "Search specialist")
state.activate_agent("search")
# List active agents
active = state.list_active_agents()
ClassesΒΆ
Base state for dynamic supervisor with agent registry. |
|
Experimental supervisor state with full agent serialization. |
|
Supervisor state with dynamic tool generation. |
Module ContentsΒΆ
- class haive.agents.dynamic_supervisor.state.SupervisorState(/, **data)ΒΆ
Bases:
haive.core.schema.prebuilt.messages.messages_with_token_usage.MessagesStateWithTokenUsageBase state for dynamic supervisor with agent registry.
Inherits from MessagesState for message handling and adds agent management capabilities. Agents are stored in a registry with metadata for routing.
- Parameters:
data (Any)
- agentsΒΆ
Registry of available agents by name
- active_agentsΒΆ
List of currently active agent names (unique)
- next_agentΒΆ
Name of agent to execute next (routing control)
- agent_taskΒΆ
Task to pass to the selected agent
- agent_responseΒΆ
Response from the last executed agent
Examples
Basic agent management:
state = SupervisorState() state.add_agent("math", math_agent, "Math specialist") state.set_routing("math", "Calculate 2+2")
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- activate_agent(name)ΒΆ
Activate an inactive agent.
- add_agent(name, agent, description, active=True)ΒΆ
Add an agent to the registry.
- Parameters:
- Return type:
None
Examples
state.add_agent(βsearchβ, search_agent, βWeb search expertβ, active=True)
- clear_execution_state()ΒΆ
Clear execution state after completion.
- Return type:
None
- deactivate_agent(name)ΒΆ
Deactivate an active agent.
- classmethod ensure_unique_agents(v)ΒΆ
Ensure active agents list contains unique values.
- get_agent(name)ΒΆ
Get agent instance by name.
- Parameters:
name (str) β Agent name
- Returns:
Agent instance or None if not found
- Return type:
Any | None
- list_active_agents()ΒΆ
List all active agents with descriptions.
- list_all_agents()ΒΆ
List all agents (active and inactive) with descriptions.
- remove_agent(name)ΒΆ
Remove an agent from the registry completely.
- model_configΒΆ
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class haive.agents.dynamic_supervisor.state.SupervisorStateV2(/, **data)ΒΆ
Bases:
haive.core.schema.prebuilt.messages.messages_with_token_usage.MessagesStateWithTokenUsageExperimental supervisor state with full agent serialization.
This version attempts to serialize agents fully rather than excluding them. May require custom serialization logic or agent reconstruction.
Warning
This is experimental and may not work with all agent types or checkpointing systems. Use SupervisorState for production.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)
- model_configΒΆ
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class haive.agents.dynamic_supervisor.state.SupervisorStateWithTools(/, **data)ΒΆ
Bases:
SupervisorStateSupervisor state with dynamic tool generation.
Extends SupervisorState with automatic tool generation from registered agents. Creates handoff tools for each agent and manages a dynamic choice model.
- Parameters:
data (Any)
- agent_choice_modelΒΆ
Dynamic model for validated agent selection
- generated_toolsΒΆ
List of tool names generated from agents
Examples
Using dynamic tools:
state = SupervisorStateWithTools() state.add_agent("search", agent, "Search expert") state.sync_agents() # Generates handoff_to_search tool tools = state.get_all_tools() # Get tool instances
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- activate_agent(name)ΒΆ
Override to trigger tool regeneration.
- add_agent(name, agent, description, active=True)ΒΆ
Override to trigger tool regeneration.
- deactivate_agent(name)ΒΆ
Override to trigger tool regeneration.
- get_all_tools()ΒΆ
Get all generated tools as callable instances.
- Returns:
List of tool instances ready for use
- Return type:
list[Any]
- remove_agent(name)ΒΆ
Override to trigger tool regeneration.
- sync_agents()ΒΆ
Public method to sync agents with tools and choice model.
Call this after adding/removing agents to regenerate tools.
- Return type:
None
- sync_on_init()ΒΆ
Sync tools and choice model after initialization.
- Return type: