haive.core.common.mixins.tool_route_mixin¶
Tool routing mixin for managing tool destinations and metadata.
This module provides a mixin for managing tool routes and related metadata in configuration classes. It enables mapping tool names to their types or destinations, keeping track of metadata, and provides utilities for creating tools from configs.
Usage:
from pydantic import BaseModel
from haive.core.common.mixins import ToolRouteMixin
class AgentConfig(ToolRouteMixin, BaseModel):
name: str
description: str
def _create_tool_implementation(self, name, description, **kwargs):
# Custom tool creation logic
return SomeTool(name=name, description=description)
# Create config with tool routes
config = AgentConfig(
name="MyAgent",
description="Agent configuration"
)
# Set tool routes
config.set_tool_route("search", "retriever", {"source": "web"})
config.set_tool_route("math", "function", {"language": "python"})
# Create a tool
search_tool = config.to_tool(name="search", description="Web search tool")
# Get routes by type
retriever_tools = config.list_tools_by_route("retriever")
Classes¶
Enhanced mixin for managing tools, routes, and converting configurations to tools. |
Functions¶
|
Sanitize tool names for OpenAI compliance and readability. |
Module Contents¶
- class haive.core.common.mixins.tool_route_mixin.ToolRouteMixin(/, **data)[source]¶
Bases:
pydantic.BaseModelEnhanced mixin for managing tools, routes, and converting configurations to tools.
This mixin provides functionality for: - Setting and managing tool routes (mapping tool names to types/destinations) - Storing and retrieving tool metadata - Supporting tools as dict with string keys for tool lists - Supporting routed tools with before validators for tuple handling - Generating tools from configurations - Visualizing tool routing information
Tool routes define where a tool request should be directed, such as to a specific retriever, model, or function. This helps implement routing logic in agents and other tool-using components.
- Parameters:
data (Any)
- tool_routes¶
Dictionary mapping tool names to their routes/types.
- tool_metadata¶
Dictionary with additional metadata for each tool.
- tools_dict¶
Dictionary mapping tool category strings to lists of tools.
- routed_tools¶
List of tuples containing (tool, route) pairs.
- before_tool_validator¶
Optional callable to validate tools before routing.
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.
- add_routed_tool(tool, route)[source]¶
Add a single tool with explicit route.
- Parameters:
tool (Any)
route (str)
- Return type:
- add_tool(tool, route=None, metadata=None)[source]¶
Add a tool with automatic routing and metadata.
- Parameters:
- Returns:
Self for method chaining
- Return type:
- add_tools_from_list(tools, clear_existing=False)[source]¶
Add tools from a list to tool_routes without clearing existing routes.
This method analyzes a list of tools and automatically creates appropriate routes based on their types. Supports both regular tools and tuples of (tool, route) for explicit routing.
- Parameters:
- Returns:
Self for method chaining.
- Return type:
- add_tools_to_category(category, tools)[source]¶
Add tools to a specific category in tools_dict.
- Parameters:
- Return type:
- clear_tool_routes()[source]¶
Clear all tool routes and metadata.
- Returns:
Self for method chaining.
- Return type:
- debug_tool_routes()[source]¶
Print debug information about tool routes.
This method uses the Rich library to create a visual representation of the tool routes and metadata, including the new dict and routed tools.
- Returns:
Self for method chaining.
- Return type:
- get_all_tools_flat()[source]¶
Get all tools from tools_dict and routed_tools as a flat list.
- Return type:
list[Any]
- get_tool(tool_name)[source]¶
Get a tool instance by name.
- Parameters:
tool_name (str) – Name of the tool
- Returns:
Tool instance or None if not found
- Return type:
Any | None
- list_tools_by_route(route)[source]¶
Get all tool names for a specific route.
This method finds all tools that are routed to a specific destination.
- remove_tool_route(tool_name)[source]¶
Remove a tool route and its metadata.
- Parameters:
tool_name (str) – Name of the tool to remove.
- Returns:
Self for method chaining.
- Return type:
- set_tool_route(tool_name, route, metadata=None)[source]¶
Set a tool route with optional metadata.
This method defines where a tool request should be routed, along with optional metadata to inform the routing decision.
- Parameters:
- Returns:
Self for method chaining.
- Return type:
- set_tool_route_for_existing(tool_identifier, new_route)[source]¶
Set or update the route for an existing tool by name or partial match.
- Parameters:
- Returns:
Self for method chaining.
- Return type:
- sync_tool_routes_from_tools(tools)[source]¶
Synchronize tool_routes with a list of tools.
This method analyzes a list of tools and automatically creates appropriate routes based on their types.
- Parameters:
tools (list[Any]) – List of tools to analyze and create routes for.
- Returns:
Self for method chaining.
- Return type:
- to_tool(name=None, description=None, route=None, **kwargs)[source]¶
Convert this configuration to a tool.
This method provides a base implementation for creating tools from configuration objects. Specific config classes should override the _create_tool_implementation method to provide custom tool creation logic.
- Parameters:
- Returns:
A tool that can be used with LLMs.
- Return type:
Any