visualization.mdx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ---
  2. title: "Agency Visualization"
  3. description: "Create interactive HTML visualizations and get ReactFlow-compatible data"
  4. icon: "chart-network"
  5. ---
  6. Agency Swarm provides two visualization methods:
  7. 1. **`visualize()`** - Creates interactive HTML files
  8. 2. **`get_agency_graph()`** - Returns ReactFlow-compatible JSON
  9. <Tabs>
  10. <Tab title="HTML Visualization">
  11. ## `visualize()`
  12. Creates a self-contained HTML file with interactive agency visualization.
  13. ```python
  14. def visualize(
  15. self,
  16. output_file: str = "agency_visualization.html",
  17. include_tools: bool = True,
  18. open_browser: bool = True
  19. ) -> str
  20. ```
  21. ### Parameters
  22. | Parameter | Type | Default | Description |
  23. |-----------|------|---------|-------------|
  24. | `output_file` | `str` | `"agency_visualization.html"` | Path to save HTML file |
  25. | `include_tools` | `bool` | `True` | Whether to include agent tools |
  26. | `open_browser` | `bool` | `True` | Whether to open in browser |
  27. ### Example
  28. ```python
  29. from agency_swarm import Agency, Agent, function_tool
  30. @function_tool
  31. def analyze_data(data: str) -> str:
  32. """Analyze data"""
  33. return f"Analysis: {data}"
  34. analyst = Agent(
  35. name="Analyst",
  36. instructions="You analyze data.",
  37. tools=[analyze_data]
  38. )
  39. manager = Agent(
  40. name="Manager",
  41. instructions="You coordinate work."
  42. )
  43. agency = Agency(
  44. manager,
  45. communication_flows=[(manager, analyst)],
  46. name="Analysis Agency"
  47. )
  48. # Create visualization
  49. html_file = agency.visualize()
  50. print(f"Saved to: {html_file}")
  51. ```
  52. </Tab>
  53. <Tab title="ReactFlow Integration">
  54. ## `get_agency_graph()`
  55. Returns ReactFlow-compatible JSON data for custom frontend integration.
  56. ```python
  57. def get_agency_graph(
  58. self,
  59. include_tools: bool = True
  60. ) -> dict[str, Any]
  61. ```
  62. ### Returns
  63. ```python
  64. {
  65. "nodes": [
  66. {
  67. "id": "Manager",
  68. "type": "agent",
  69. "position": {"x": 100, "y": 50},
  70. "data": {
  71. "label": "Manager",
  72. "description": "Agent description",
  73. "isEntryPoint": True,
  74. "toolCount": 0
  75. }
  76. }
  77. ],
  78. "edges": [
  79. {
  80. "id": "Manager->Analyst",
  81. "source": "Manager",
  82. "target": "Analyst",
  83. "type": "communication"
  84. }
  85. ]
  86. }
  87. ```
  88. ### Node Types
  89. - **Agent nodes**: `type: "agent"` with agent metadata
  90. - **Tool nodes**: `type: "tool"` with parent agent reference
  91. ### Edge Types
  92. - **Communication**: `type: "communication"` between agents
  93. - **Ownership**: `type: "owns"` from agent to tool
  94. ## Frontend Integration
  95. Expose `get_agency_graph()` in your API, then use the JSON directly in React:
  96. ```tsx
  97. import ReactFlow from 'reactflow';
  98. const agencyData = await fetch('/your-graph-endpoint').then(r => r.json());
  99. function AgencyVisualization() {
  100. return (
  101. <div style={{ height: '600px' }}>
  102. <ReactFlow
  103. nodes={agencyData.nodes}
  104. edges={agencyData.edges}
  105. fitView
  106. />
  107. </div>
  108. );
  109. }
  110. ```
  111. </Tab>
  112. </Tabs>