test_tools_folder_integration.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import pytest
  2. from agents import ModelSettings, RunResult
  3. from agents.items import ToolCallItem, ToolCallOutputItem
  4. from openai.types.responses.response_function_tool_call import ResponseFunctionToolCall
  5. from agency_swarm import Agent
  6. @pytest.mark.asyncio
  7. async def test_tools_folder_with_real_agent(tmp_path):
  8. """Integration test: tools_folder loads and executes tools with real OpenAI API."""
  9. tools_dir = tmp_path / "tools"
  10. tools_dir.mkdir()
  11. # Create a simple test tool
  12. tool_code = """
  13. from agents import function_tool
  14. @function_tool
  15. def echo_tool(message: str) -> str:
  16. '''Echo the input message with a prefix.'''
  17. return f"Tool executed: {message}"
  18. """
  19. (tools_dir / "echo_tool.py").write_text(tool_code)
  20. # Create agent with tools_folder
  21. agent = Agent(
  22. name="TestAgent",
  23. instructions="You are a test agent. When asked to echo something, use the echo_tool with the provided message.",
  24. tools_folder=str(tools_dir),
  25. model_settings=ModelSettings(temperature=0.0),
  26. )
  27. # Verify tool was loaded
  28. tool_names = [tool.name for tool in agent.tools]
  29. assert "echo_tool" in tool_names
  30. # Test real execution with OpenAI API
  31. result: RunResult = await agent.get_response("Use the echo tool to echo 'hello world'")
  32. tool_call_names = [
  33. item.raw_item.name
  34. for item in result.new_items
  35. if isinstance(item, ToolCallItem) and isinstance(item.raw_item, ResponseFunctionToolCall)
  36. ]
  37. assert "echo_tool" in tool_call_names, f"Expected echo_tool call in new_items, got: {tool_call_names}"
  38. tool_outputs = [str(item.output) for item in result.new_items if isinstance(item, ToolCallOutputItem)]
  39. assert any("Tool executed: hello world" in output for output in tool_outputs), (
  40. f"Expected echo_tool output in new_items, got: {tool_outputs}"
  41. )