copilot_demo.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. Agency Swarm Copilot Demo
  3. This example demonstrates the Copilot UI capabilities of Agency Swarm v1.x.
  4. Sets up a frontend and backend server for the Copilot UI chat demo.
  5. """
  6. import sys
  7. from pathlib import Path
  8. # Add the src directory to the path so we can import agency_swarm
  9. sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
  10. from agency_swarm import Agency, Agent, RunContextWrapper, function_tool
  11. from agency_swarm.ui.demos.copilot import CopilotDemoLauncher
  12. @function_tool()
  13. async def example_tool(wrapper: RunContextWrapper) -> str:
  14. """Example tool for copilot demo"""
  15. return "Example tool executed"
  16. def create_demo_agency():
  17. """Create a demo agency for copilot demo"""
  18. # Create agents using v1.x pattern (direct instantiation)
  19. ceo = Agent(
  20. name="CEO",
  21. description="Chief Executive Officer - oversees all operations",
  22. instructions="You are the CEO responsible for high-level decision making and coordination.",
  23. tools=[example_tool],
  24. )
  25. worker = Agent(
  26. name="Worker",
  27. description="Worker - performs tasks",
  28. instructions="Follow instructions given by the CEO.",
  29. tools=[example_tool],
  30. )
  31. # Create agency with communication flows (v1.x pattern)
  32. agency = Agency(
  33. ceo, # Entry point agent (positional argument)
  34. communication_flows=[ceo > worker],
  35. name="CopilotDemoAgency",
  36. )
  37. return agency
  38. def main():
  39. """Launch interactive Copilot demo"""
  40. print("Agency Swarm Copilot Demo")
  41. print("=" * 50)
  42. print()
  43. try:
  44. agency = create_demo_agency()
  45. # Launch the Copilot UI demo with backend and frontend servers.
  46. launcher = CopilotDemoLauncher()
  47. launcher.start(agency)
  48. except Exception as e:
  49. print(f"❌ Demo failed with error: {e}")
  50. import traceback
  51. traceback.print_exc()
  52. if __name__ == "__main__":
  53. main()