tui.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """
  2. Agency Swarm TUI example.
  3. This example is the main product-style demo for `agency.tui()`.
  4. It intentionally exercises the same important surfaces as the richer FastAPI
  5. example:
  6. - reasoning
  7. - handoffs
  8. - web search
  9. - file search
  10. - code/file analysis through the files folder
  11. Suggested prompts:
  12. - "Tell me about daily_revenue_report.pdf."
  13. - "Search the web for the latest Bun release notes."
  14. - "What is 345 * 18?"
  15. - "Compare the findings in research_report.txt with daily_revenue_report.pdf."
  16. """
  17. import atexit
  18. import shutil
  19. import sys
  20. import tempfile
  21. from pathlib import Path
  22. from typing import Literal
  23. # Add the src directory to the path so we can import agency_swarm
  24. sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
  25. from agency_swarm import Agency, Agent, Handoff, ModelSettings, Reasoning, WebSearchTool, function_tool
  26. def _files() -> str:
  27. source = Path(__file__).resolve().parent.parent / "data"
  28. if not source.exists():
  29. raise FileNotFoundError(f"Expected example data directory at: {source}")
  30. root = Path(tempfile.mkdtemp(prefix="agency-swarm-terminal-demo-"))
  31. path = root / "files"
  32. shutil.copytree(source, path)
  33. atexit.register(lambda: shutil.rmtree(root, ignore_errors=True))
  34. return str(path)
  35. @function_tool
  36. def calculate(a: float, b: float, operation: Literal["add", "subtract", "multiply", "divide"] = "add") -> str:
  37. """Perform a basic arithmetic operation."""
  38. if operation == "add":
  39. value = a + b
  40. elif operation == "subtract":
  41. value = a - b
  42. elif operation == "multiply":
  43. value = a * b
  44. else:
  45. if b == 0:
  46. return "Error: Cannot divide by zero."
  47. value = a / b
  48. return f"Result: {value}"
  49. def create_demo_agency() -> Agency:
  50. """Create a richer TUI demo agency for manual QA."""
  51. support = Agent(
  52. name="UserSupportAgent",
  53. description="Receives user requests and coordinates reasoning, search, and file work.",
  54. instructions=(
  55. "You are UserSupportAgent. Handle general requests, reason step by step when useful, "
  56. "use your web and file tools when the user asks about files or the web, and hand off "
  57. "math-heavy work to MathAgent. When files contain charts, reports, or data, use the "
  58. "available file/code tools to inspect them before answering."
  59. ),
  60. # files_folder=_files(),
  61. include_search_results=True,
  62. tools=[WebSearchTool()],
  63. model="gpt-5.4-mini",
  64. model_settings=ModelSettings(reasoning=Reasoning(effort="low", summary="auto")),
  65. conversation_starters=[
  66. "Tell me about daily_revenue_report.pdf.",
  67. "Search the web for the latest Bun release notes.",
  68. "What is 345 * 18?",
  69. "Compare research_report.txt with daily_revenue_report.pdf.",
  70. ],
  71. )
  72. math = Agent(
  73. name="MathAgent",
  74. description="Handles arithmetic and calculation-heavy requests.",
  75. instructions="You are MathAgent. Use the calculate tool for arithmetic questions.",
  76. tools=[calculate],
  77. model="gpt-5.4-mini",
  78. model_settings=ModelSettings(reasoning=Reasoning(effort="high", summary="auto")),
  79. )
  80. return Agency(
  81. support,
  82. math,
  83. communication_flows=[(support, math, Handoff)],
  84. shared_instructions="Demonstrate reasoning, web search, file search, and handoffs.",
  85. name="TuiDemoAgency",
  86. )
  87. if __name__ == "__main__":
  88. create_demo_agency().tui(show_reasoning=True)