|
| 1 | +from typing import Any |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +from agno.agent.agent import Agent |
| 5 | +from agno.agent._tools import parse_tools |
| 6 | +from agno.tools.function import Function |
| 7 | +from agno.tools.toolkit import Toolkit |
| 8 | + |
| 9 | + |
| 10 | +def _mock_model(): |
| 11 | + model = MagicMock() |
| 12 | + model.supports_native_structured_outputs = False |
| 13 | + return model |
| 14 | + |
| 15 | + |
| 16 | +def _mock_team(): |
| 17 | + team = MagicMock() |
| 18 | + team.__class__.__name__ = "Team" |
| 19 | + return team |
| 20 | + |
| 21 | + |
| 22 | +# -- Callable tools ---------------------------------------------------------- |
| 23 | + |
| 24 | + |
| 25 | +def test_callable_tool_receives_team_from_member_agent(): |
| 26 | + def my_tool(query: str, team: Any) -> str: |
| 27 | + return "ok" |
| 28 | + |
| 29 | + agent = Agent(tools=[my_tool]) |
| 30 | + agent._team = _mock_team() |
| 31 | + |
| 32 | + functions = parse_tools(agent=agent, tools=agent.tools, model=_mock_model()) |
| 33 | + |
| 34 | + assert len(functions) == 1 |
| 35 | + assert functions[0]._team is agent._team |
| 36 | + |
| 37 | + |
| 38 | +def test_callable_tool_team_is_none_when_agent_has_no_team(): |
| 39 | + def my_tool(query: str) -> str: |
| 40 | + return "ok" |
| 41 | + |
| 42 | + agent = Agent(tools=[my_tool]) |
| 43 | + |
| 44 | + functions = parse_tools(agent=agent, tools=agent.tools, model=_mock_model()) |
| 45 | + |
| 46 | + assert len(functions) == 1 |
| 47 | + assert functions[0]._team is None |
| 48 | + |
| 49 | + |
| 50 | +# -- Function objects --------------------------------------------------------- |
| 51 | + |
| 52 | + |
| 53 | +def test_function_tool_receives_team_from_member_agent(): |
| 54 | + def my_tool(query: str, team: Any) -> str: |
| 55 | + return "ok" |
| 56 | + |
| 57 | + func = Function.from_callable(my_tool) |
| 58 | + agent = Agent(tools=[func]) |
| 59 | + agent._team = _mock_team() |
| 60 | + |
| 61 | + functions = parse_tools(agent=agent, tools=agent.tools, model=_mock_model()) |
| 62 | + |
| 63 | + assert len(functions) == 1 |
| 64 | + assert functions[0]._team is agent._team |
| 65 | + |
| 66 | + |
| 67 | +# -- Toolkit functions -------------------------------------------------------- |
| 68 | + |
| 69 | + |
| 70 | +def test_toolkit_tool_receives_team_from_member_agent(): |
| 71 | + class MyToolkit(Toolkit): |
| 72 | + def __init__(self): |
| 73 | + super().__init__(name="my_toolkit") |
| 74 | + self.register(self.my_tool) |
| 75 | + |
| 76 | + def my_tool(self, query: str) -> str: |
| 77 | + return "ok" |
| 78 | + |
| 79 | + agent = Agent(tools=[MyToolkit()]) |
| 80 | + agent._team = _mock_team() |
| 81 | + |
| 82 | + functions = parse_tools(agent=agent, tools=agent.tools, model=_mock_model()) |
| 83 | + |
| 84 | + toolkit_funcs = [f for f in functions if isinstance(f, Function)] |
| 85 | + assert len(toolkit_funcs) == 1 |
| 86 | + assert toolkit_funcs[0]._team is agent._team |
0 commit comments