Skip to content

Commit 7df2938

Browse files
mrlee-amazonUnshure
authored andcommitted
refactor: reorder toolbox creation logic and add default tools test
- Move toolbox creation before tool validation to enable default toolbox usage - Add test for loading tools from default toolbox without explicit toolbox parameter - Mock strands_tools dependencies in tests to avoid external dependencies - Verify that file_read tool can be loaded from default configuration 🤖 Assisted by Amazon Q Developer
1 parent de4196e commit 7df2938

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/strands/experimental/agent_config.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,16 @@ def __init__(self, config_source: str | dict[str, any], tool_box: ToolBox | None
6767
self.system_prompt = config_data.get('prompt') # Only accept 'prompt' key
6868
self._raise_exception_on_missing_tool = raise_exception_on_missing_tool
6969

70-
# Process tools configuration if provided
71-
config_tools = config_data.get('tools')
72-
if config_tools is not None and tool_box is None:
73-
raise ValueError("Tool names specified in config but no ToolBox provided")
74-
7570
# Handle tool selection from ToolBox
7671
if tool_box is not None:
7772
self._toolbox = tool_box
7873
else:
7974
# Create default ToolBox with strands_tools
8075
self._toolbox = self._create_default_toolbox()
8176

77+
# Process tools configuration if provided
78+
config_tools = config_data.get('tools')
79+
8280
# Track configured tools separately from full tool pool
8381
self._configured_tools = []
8482

tests/strands/experimental/test_agent_config.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,23 @@ def test_agent_config_tool_validation_error(self):
158158
}, tool_box=toolbox)
159159

160160
def test_agent_config_tools_without_toolbox_error(self):
161-
"""Test that specifying tools without ToolBox raises error."""
162-
with pytest.raises(ValueError, match="Tool names specified in config but no ToolBox provided"):
163-
AgentConfig({
161+
"""Test that config can load tools from default toolbox when strands_tools is available."""
162+
import unittest.mock
163+
from strands.experimental.tool_box import ToolBox
164+
165+
# Create a mock toolbox with the tools we want
166+
mock_toolbox = ToolBox()
167+
mock_file_read = self.MockTool("file_read")
168+
mock_toolbox.add_tool(mock_file_read)
169+
170+
# Patch the _create_default_toolbox method to return our mock
171+
with unittest.mock.patch.object(AgentConfig, '_create_default_toolbox', return_value=mock_toolbox):
172+
config = AgentConfig({
164173
"model": "test-model",
165-
"tools": ["calculator"]
174+
"tools": ["file_read"]
166175
})
176+
assert len(config.configured_tools) == 1
177+
assert config.configured_tools[0].tool_name == "file_read"
167178

168179
def test_agent_config_no_strands_tools_error(self):
169180
"""Test that missing strands_tools without ToolBox raises ImportError."""
@@ -208,3 +219,23 @@ def test_agent_config_missing_tool_validation_with_flag_true(self):
208219
"model": "test-model",
209220
"tools": ["missing_tool"]
210221
}, tool_box=custom_toolbox, raise_exception_on_missing_tool=True)
222+
223+
def test_agent_config_loads_from_default_tools_without_toolbox(self):
224+
"""Test that config can load tools from default strands_tools without explicit toolbox."""
225+
import unittest.mock
226+
from strands.experimental.tool_box import ToolBox
227+
228+
# Create a mock toolbox with the tools we want
229+
mock_toolbox = ToolBox()
230+
mock_file_read = self.MockTool("file_read")
231+
mock_toolbox.add_tool(mock_file_read)
232+
233+
# Patch the _create_default_toolbox method to return our mock
234+
with unittest.mock.patch.object(AgentConfig, '_create_default_toolbox', return_value=mock_toolbox):
235+
config = AgentConfig({
236+
"model": "test-model",
237+
"tools": ["file_read"]
238+
})
239+
# Verify the tool was loaded from the default toolbox
240+
assert len(config.configured_tools) == 1
241+
assert config.configured_tools[0].tool_name == "file_read"

0 commit comments

Comments
 (0)