agency_visualization.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. """
  3. Agency Swarm Visualization Demo
  4. This example demonstrates the interactive HTML visualization capabilities of Agency Swarm v1.x.
  5. It generates an interactive HTML file showing your agency structure with drag & drop and zoom.
  6. """
  7. import os
  8. import sys
  9. from pathlib import Path
  10. # Add the src directory to the path so we can import agency_swarm
  11. sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
  12. from agency_swarm import Agency, Agent, RunContextWrapper, function_tool
  13. from agency_swarm.tools import Handoff
  14. @function_tool()
  15. async def example_tool(wrapper: RunContextWrapper) -> str:
  16. """Example tool for visualization demo"""
  17. return "Example tool executed"
  18. def create_demo_agency():
  19. """Create a demo agency for visualization"""
  20. # Create agents using v1.x pattern (direct instantiation)
  21. ceo = Agent(
  22. name="CEO",
  23. description="Chief Executive Officer - oversees all operations",
  24. instructions="You are the CEO responsible for high-level decision making and coordination.",
  25. tools=[],
  26. )
  27. pm = Agent(
  28. name="ProjectManager",
  29. description="Manages project timelines and coordinates between teams",
  30. instructions="You manage projects, timelines, and coordinate between different teams.",
  31. tools=[example_tool],
  32. )
  33. dev = Agent(
  34. name="Developer",
  35. description="Writes and maintains code",
  36. instructions="You write, test, and maintain code for various projects.",
  37. tools=[example_tool],
  38. )
  39. qa = Agent(
  40. name="QA",
  41. description="Tests software and ensures quality",
  42. instructions="You test software, find bugs, and ensure quality standards.",
  43. tools=[example_tool],
  44. )
  45. # Create agency with communication flows (v1.x pattern)
  46. agency = Agency(
  47. ceo, # Entry point agent (positional argument)
  48. communication_flows=[
  49. (dev < ceo > pm > dev), # Multi-agent communication flow example
  50. (dev > qa, Handoff), # Developer can communicate with QA using handoff
  51. ],
  52. name="Software Development Agency",
  53. shared_instructions="This is a software development agency with clear hierarchy and communication flows.",
  54. )
  55. return agency
  56. def main():
  57. """Generate interactive HTML visualization demo"""
  58. print("Agency Swarm Visualization Demo")
  59. print("=" * 50)
  60. print()
  61. # Create output directory
  62. os.makedirs("visualization_output", exist_ok=True)
  63. os.chdir("visualization_output")
  64. try:
  65. print("=== Interactive HTML Visualization ===\n")
  66. agency = create_demo_agency()
  67. # Generate interactive HTML visualization
  68. html_file = agency.visualize(
  69. output_file="agency_interactive_demo.html",
  70. include_tools=True,
  71. open_browser=True, # auto-open the demo
  72. )
  73. file_size = os.path.getsize(html_file)
  74. print(f" ✅ Generated: {html_file} ({file_size:,} bytes)")
  75. print(" Features: Interactive drag & drop, zoom")
  76. print(" Includes: Agency statistics, communication flows, tool information")
  77. print()
  78. print("Demo completed successfully.")
  79. print(f"Output file saved in: {os.getcwd()}")
  80. print("If open_browser=True, the HTML should open in your browser.")
  81. except Exception as e:
  82. print(f"❌ Demo failed with error: {e}")
  83. import traceback
  84. traceback.print_exc()
  85. finally:
  86. # Return to original directory
  87. os.chdir("..")
  88. if __name__ == "__main__":
  89. main()