|
| 1 | +# ABOUTME: Experimental agent configuration with toAgent() method for creating Agent instances |
| 2 | +# ABOUTME: Extends core AgentConfig with experimental instantiation patterns using ToolPool |
| 3 | +"""Experimental agent configuration with enhanced instantiation patterns.""" |
| 4 | + |
| 5 | +import json |
| 6 | +import importlib |
| 7 | + |
| 8 | +from .tool_pool import ToolPool |
| 9 | + |
| 10 | +# File prefix for configuration file paths |
| 11 | +FILE_PREFIX = "file://" |
| 12 | + |
| 13 | +# Minimum viable list of tools to enable agent building |
| 14 | +# This list is experimental and will be revisited as tools evolve |
| 15 | +DEFAULT_TOOLS = ["file_read", "editor", "http_request", "shell", "use_agent"] |
| 16 | + |
| 17 | + |
| 18 | +class AgentConfig: |
| 19 | + """Agent configuration with toAgent() method and ToolPool integration.""" |
| 20 | + |
| 21 | + def __init__(self, config_source: str | dict[str, any], tool_pool: ToolPool | None = None): |
| 22 | + """Initialize AgentConfig from file path or dictionary. |
| 23 | + |
| 24 | + Args: |
| 25 | + config_source: Path to JSON config file (must start with 'file://') or config dictionary |
| 26 | + tool_pool: Optional ToolPool to select tools from when 'tools' is specified in config |
| 27 | + """ |
| 28 | + if isinstance(config_source, str): |
| 29 | + # Require file:// prefix for file paths |
| 30 | + if not config_source.startswith(FILE_PREFIX): |
| 31 | + raise ValueError(f"File paths must be prefixed with '{FILE_PREFIX}'") |
| 32 | + |
| 33 | + # Remove file:// prefix and load from file |
| 34 | + file_path = config_source.removeprefix(FILE_PREFIX) |
| 35 | + with open(file_path, 'r') as f: |
| 36 | + config_data = json.load(f) |
| 37 | + else: |
| 38 | + # Use dictionary directly |
| 39 | + config_data = config_source |
| 40 | + |
| 41 | + self.model = config_data.get('model') |
| 42 | + self.system_prompt = config_data.get('prompt') # Only accept 'prompt' key |
| 43 | + |
| 44 | + # Process tools configuration if provided |
| 45 | + config_tools = config_data.get('tools') |
| 46 | + if config_tools is not None and tool_pool is None: |
| 47 | + raise ValueError("Tool names specified in config but no ToolPool provided") |
| 48 | + |
| 49 | + # Handle tool selection from ToolPool |
| 50 | + if tool_pool is not None: |
| 51 | + self._tool_pool = tool_pool |
| 52 | + else: |
| 53 | + # Create default ToolPool with strands_tools |
| 54 | + self._tool_pool = self._create_default_tool_pool() |
| 55 | + |
| 56 | + # Apply tool selection if specified |
| 57 | + if config_tools is not None: |
| 58 | + # Validate all tool names exist in the ToolPool |
| 59 | + available_tools = tool_pool.list_tool_names() |
| 60 | + for tool_name in config_tools: |
| 61 | + if tool_name not in available_tools: |
| 62 | + raise ValueError(f"Tool '{tool_name}' not found in ToolPool. Available tools: {available_tools}") |
| 63 | + |
| 64 | + # Create new ToolPool with only selected tools |
| 65 | + selected_pool = ToolPool() |
| 66 | + all_tools = tool_pool.get_tools() |
| 67 | + for tool in all_tools: |
| 68 | + if tool.tool_name in config_tools: |
| 69 | + selected_pool.add_tool(tool) |
| 70 | + |
| 71 | + self._tool_pool = selected_pool |
| 72 | + |
| 73 | + def _create_default_tool_pool(self) -> ToolPool: |
| 74 | + """Create default ToolPool with strands_tools.""" |
| 75 | + pool = ToolPool() |
| 76 | + |
| 77 | + for tool in DEFAULT_TOOLS: |
| 78 | + try: |
| 79 | + module_name = f"strands_tools.{tool}" |
| 80 | + tool_module = importlib.import_module(module_name) |
| 81 | + pool.add_tools_from_module(tool_module) |
| 82 | + except ImportError: |
| 83 | + raise ImportError( |
| 84 | + f"strands_tools is not available and no ToolPool was specified. " |
| 85 | + f"Either install strands_tools with 'pip install strands-agents-tools' " |
| 86 | + f"or provide your own ToolPool with your own tools." |
| 87 | + ) |
| 88 | + |
| 89 | + return pool |
| 90 | + |
| 91 | + @property |
| 92 | + def tool_pool(self) -> "ToolPool": |
| 93 | + """Get the tool pool for this configuration. |
| 94 | + |
| 95 | + Returns: |
| 96 | + ToolPool instance |
| 97 | + """ |
| 98 | + return self._tool_pool |
| 99 | + |
| 100 | + def toAgent(self, tools: "ToolPool | None" = None, **kwargs: any): |
| 101 | + """Create an Agent instance from this configuration. |
| 102 | + |
| 103 | + Args: |
| 104 | + tools: ToolPool to use (overrides default empty pool) |
| 105 | + **kwargs: Additional parameters to override config values. |
| 106 | + Supports all Agent constructor parameters. |
| 107 | + |
| 108 | + Returns: |
| 109 | + Configured Agent instance |
| 110 | + |
| 111 | + Example: |
| 112 | + config = AgentConfig({"model": "anthropic.claude-3-5-sonnet-20241022-v2:0", "prompt": "You are helpful"}) |
| 113 | + pool = ToolPool() |
| 114 | + pool.add_tool_function(calculator) |
| 115 | + agent = config.toAgent(tools=pool) |
| 116 | + """ |
| 117 | + # Import here to avoid circular imports: |
| 118 | + # experimental/agent_config.py -> agent.agent -> event_loop.event_loop -> |
| 119 | + # experimental.hooks -> experimental.__init__.py -> AgentConfig |
| 120 | + from ..agent.agent import Agent |
| 121 | + |
| 122 | + # Start with config values |
| 123 | + agent_params = {} |
| 124 | + |
| 125 | + if self.model is not None: |
| 126 | + agent_params['model'] = self.model |
| 127 | + if self.system_prompt is not None: |
| 128 | + agent_params['system_prompt'] = self.system_prompt |
| 129 | + |
| 130 | + # Use provided ToolPool or default empty one |
| 131 | + tool_pool = tools if tools is not None else self._tool_pool |
| 132 | + agent_params['tools'] = tool_pool.get_tools() |
| 133 | + |
| 134 | + # Override with any other provided kwargs |
| 135 | + agent_params.update(kwargs) |
| 136 | + |
| 137 | + return Agent(**agent_params) |
0 commit comments