Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/modules/workflow_graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ sequential_workflow_graph.save_module("examples/output/my_sequential_workflow.js
### Visualizing the Workflow

```python
# Display the workflow graph with node statuses visually
# Open a non-blocking window. Node colors update while the workflow runs.
# Pass block=True to wait until the window is closed.
workflow_graph.display()
workflow_graph.close_display()
```

The `WorkFlowGraph` and `SequentialWorkFlowGraph` classes provide a flexible and powerful way to design complex agent workflows, track their execution, and manage the flow of data between tasks.
3 changes: 2 additions & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ workflow_graph: WorkFlowGraph = wf_generator.generate_workflow(goal=goal)

You can optionally **visualize** or **save** the generated workflow:
```python
# Visualize the workflow structure (optional)
# Visualize the workflow without blocking later steps (optional).
# Node colors update while the workflow runs. Use display(block=True) to wait.
workflow_graph.display()

# Save the workflow to a JSON file (optional)
Expand Down
4 changes: 3 additions & 1 deletion docs/zh/modules/workflow_graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,10 @@ sequential_workflow_graph.save_module("examples/output/my_sequential_workflow.js
### 可视化工作流

```python
# 以可视方式显示工作流图
# 打开非阻塞窗口。工作流运行时节点颜色会随之更新。
# 传入 block=True 会等待窗口关闭。
workflow_graph.display()
workflow_graph.close_display()
```

`WorkFlowGraph` 和 `SequentialWorkFlowGraph` 类提供了一种灵活而强大的方式来设计复杂的代理工作流、跟踪其执行并管理任务之间的数据流。
3 changes: 2 additions & 1 deletion docs/zh/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ workflow_graph: WorkFlowGraph = wf_generator.generate_workflow(goal=goal)

可选:可视化或保存生成的工作流:
```python
# 可视化工作流结构(可选)
# 可视化工作流且不阻塞后续步骤(可选)。
# 运行过程中节点颜色会更新。使用 display(block=True) 会等待窗口关闭。
workflow_graph.display()

# 将工作流保存为 JSON 文件(可选)
Expand Down
171 changes: 138 additions & 33 deletions evoagentx/workflow/workflow_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,9 @@ class WorkFlowGraph(BaseModule):

def init_module(self):
self._lock = threading.Lock()
self._display_fig = None
self._display_live = False
self._display_refreshing = False
if not self.graph:
self._init_from_nodes(self.nodes, explicit_edges=self.edges)
elif isinstance(self.graph, MultiDiGraph):
Expand Down Expand Up @@ -1156,6 +1159,7 @@ def reset_graph(self):
"""
for node in self.nodes:
node.set_status(WorkFlowNodeState.PENDING)
self._refresh_display()

def set_node_status(self, node: Union[str, WorkFlowNode], new_state: WorkFlowNodeState) -> bool:
"""
Expand All @@ -1176,6 +1180,8 @@ def set_node_status(self, node: Union[str, WorkFlowNode], new_state: WorkFlowNod
flag = True
except Exception as e:
raise ValueError(f"An error occurs when setting node status: {e}")
if flag:
self._refresh_display()
return flag

def pending(self, node: Union[str, WorkFlowNode]) -> bool:
Expand Down Expand Up @@ -1460,79 +1466,178 @@ def format_action_graph(action_graph: ActionGraph) -> str:
)
return desc

def display(self):
"""
Display the workflow graph with node and edge attributes.
Nodes are colored based on their status.
"""
@staticmethod
def _matplotlib_gui_available() -> bool:
"""Return whether the current Matplotlib backend can open a GUI window."""
import matplotlib
from matplotlib.backends.registry import backend_registry

try:
_backend, framework = backend_registry.resolve_backend(matplotlib.get_backend())
except Exception:
return False
return framework is not None

def _get_open_display_figure(self):
"""Return the live display figure, or None if it was closed."""
fig = getattr(self, "_display_fig", None)
if fig is None:
return None
import matplotlib.pyplot as plt
try:
still_open = plt.fignum_exists(fig.number)
except Exception:
still_open = False
if still_open:
return fig
self._display_fig = None
self._display_live = False
return None

def _redraw_display(self, fig) -> None:
"""Draw the current graph and node statuses onto ``fig``."""
import matplotlib.pyplot as plt

# Define colors for node statuses
status_colors = {
WorkFlowNodeState.PENDING: 'lightgray',
WorkFlowNodeState.RUNNING: 'orange',
WorkFlowNodeState.COMPLETED: 'green',
WorkFlowNodeState.FAILED: 'red'
}

if not self.graph.nodes:
print("Graph is empty. No nodes to display.")
return
fig.clf()
ax = fig.add_subplot(111)

# Get node colors based on their statuses
node_colors = [status_colors.get(self.get_node_status(node), 'lightgray') for node in self.graph.nodes]

# Prepare node labels with additional information
node_labels = {node: self.get_node_description(data["ref"]) for node, data in self.graph.nodes(data=True)}

# Draw the graph
# pos = nx.shell_layout(self.graph)

if len(self.graph.nodes) == 1:
single_node = list(self.graph.nodes)[0]
pos = {single_node: (0, 0)} # Place the single node at the center
pos = {single_node: (0, 0)}
else:
pos = nx.shell_layout(self.graph)

plt.figure(figsize=(12, 8))

nx.draw(
self.graph, pos, with_labels=False, node_color=node_colors, edge_color='black',
self.graph, pos, ax=ax, with_labels=False, node_color=node_colors, edge_color='black',
node_size=1500, font_size=8, font_color='black', font_weight='bold'
)

if len(self.graph.nodes) == 1:
for node, (x, y) in pos.items():
plt.text(x+0.005, y, node_labels[node], ha='left', va='center', fontsize=9, bbox=dict(facecolor='white', alpha=0.7))
ax.text(x+0.005, y, node_labels[node], ha='left', va='center', fontsize=9, bbox=dict(facecolor='white', alpha=0.7))
else:
# Draw node labels next to the nodes (left-aligned)
# text_offsets = {node: (pos[node][0]-0.2, pos[node][1]-0.22) for node in self.graph.nodes}
y_positions = [y for _, y in pos.values()]
y_min, y_max = min(y_positions), max(y_positions)
lower_third_boundary = y_min + (y_max - y_min) / 3

# Adjust text offsets based on node position in the graph
text_offsets = {}
for node, (x, y) in pos.items():
if y < lower_third_boundary: # If in the lower third, display label above the node
if y < lower_third_boundary:
text_offsets[node] = (x-0.2, y + 0.23)
else: # Otherwise, display label below the node
else:
text_offsets[node] = (x-0.2, y - 0.23)

for node, (x, y) in text_offsets.items():
plt.text(x, y, node_labels[node], ha='left', va='center', fontsize=9, bbox=dict(facecolor='white', alpha=0.7))
ax.text(x, y, node_labels[node], ha='left', va='center', fontsize=9, bbox=dict(facecolor='white', alpha=0.7))

# Draw edge labels for priorities
edge_labels = nx.get_edge_attributes(self.graph, 'priority')
nx.draw_networkx_edge_labels(self.graph, pos, edge_labels=edge_labels)
nx.draw_networkx_edge_labels(self.graph, pos, edge_labels=edge_labels, ax=ax)

# Add a legend to show node status colors
legend_elements = [
plt.Line2D([0], [0], marker='o', color='w', label=status.name, markersize=10, markerfacecolor=color)
for status, color in status_colors.items()
]
plt.legend(handles=legend_elements, title="Workflow Node Status", loc='upper left', fontsize='medium')
ax.legend(handles=legend_elements, title="Workflow Node Status", loc='upper left', fontsize='medium')
ax.set_title("Workflow Graph")

plt.title("Workflow Graph")
plt.show()
def _refresh_display(self) -> None:
"""Redraw an open non-blocking display. Never raises into workflow execution."""
if not getattr(self, "_display_live", False):
return
if getattr(self, "_display_refreshing", False):
return
fig = self._get_open_display_figure()
if fig is None:
return

self._display_refreshing = True
try:
self._redraw_display(fig)
fig.canvas.draw()
if self._matplotlib_gui_available():
fig.canvas.flush_events()
except Exception as exc:
logger.warning("Failed to refresh workflow graph display: {}", exc)
self._display_live = False
finally:
self._display_refreshing = False

def close_display(self) -> None:
"""Close the window opened by ``display()``."""
import matplotlib.pyplot as plt

self._display_live = False
fig = getattr(self, "_display_fig", None)
self._display_fig = None
if fig is None:
return
try:
plt.close(fig)
except Exception as exc:
logger.debug("Failed to close workflow graph display: {}", exc)

def display(self, block: bool = False):
"""
Display the workflow graph with node and edge attributes.
Nodes are colored based on their status.

The window is non-blocking by default, so later steps keep running and
the figure refreshes when a node status changes. Pass ``block=True`` to
wait until the window is closed. Call ``close_display()`` to close a
non-blocking window.

Args:
block: When True, block until the window is closed. Defaults to False.

Returns:
The Matplotlib figure, or None when the graph has no nodes.
"""
import matplotlib.pyplot as plt

if not self.graph.nodes:
print("Graph is empty. No nodes to display.")
return None

fig = self._get_open_display_figure()
if fig is None:
fig = plt.figure(figsize=(12, 8))
self._display_fig = fig

self._display_live = not block
self._display_refreshing = True
try:
self._redraw_display(fig)
finally:
self._display_refreshing = False

if not self._matplotlib_gui_available():
fig.canvas.draw()
if block:
self._display_live = False
return fig

if block:
plt.show(block=True)
self._display_fig = None
self._display_live = False
return fig

plt.show(block=False)
try:
fig.canvas.flush_events()
except Exception as exc:
logger.debug("Failed to flush workflow graph display events: {}", exc)
return fig

def get_workflow_description(self) -> str:

Expand Down
91 changes: 91 additions & 0 deletions tests/src/workflow/test_workflow_graph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import inspect
import unittest
from unittest.mock import patch

import pytest

from evoagentx.core.base_config import Parameter
Expand Down Expand Up @@ -754,6 +757,94 @@ def test_node_input_unknown_source_raises(self):
workflow_outputs=[workflow_output],
)

def _node_facecolors(self, fig):
from matplotlib.collections import PathCollection

ax = fig.axes[0]
collection = next(coll for coll in ax.collections if isinstance(coll, PathCollection))
return [tuple(round(float(channel), 5) for channel in row) for row in collection.get_facecolor()]

def test_display_default_is_nonblocking(self):
signature = inspect.signature(WorkFlowGraph.display)
self.assertIs(signature.parameters["block"].default, False)

def test_display_updates_colors_without_blocking(self):
import matplotlib
matplotlib.use("Agg", force=True)
import matplotlib.pyplot as plt

graph = self.linear_graph
fig = graph.display()
try:
self.assertIsNotNone(fig)
self.assertTrue(graph._display_live)
self.assertIs(graph._display_fig, fig)

before = self._node_facecolors(fig)
graph.running("Task1")
after = self._node_facecolors(fig)
task_index = list(graph.graph.nodes).index("Task1")
self.assertNotEqual(before[task_index], after[task_index])
self.assertGreater(after[task_index][0], after[task_index][2])

graph.close_display()
self.assertFalse(graph._display_live)
self.assertIsNone(graph._display_fig)
self.assertFalse(plt.fignum_exists(fig.number))

graph.completed("Task1")
self.assertIsNone(graph._display_fig)
finally:
plt.close("all")

def test_display_block_waits_until_window_closes(self):
import matplotlib
matplotlib.use("Agg", force=True)
import matplotlib.pyplot as plt

graph = self.linear_graph
with patch.object(WorkFlowGraph, "_matplotlib_gui_available", return_value=True), \
patch("matplotlib.pyplot.show") as show:
fig = graph.display(block=True)
try:
show.assert_called_once_with(block=True)
self.assertFalse(graph._display_live)
self.assertIsNone(graph._display_fig)
finally:
plt.close(fig)
plt.close("all")

def test_display_show_uses_nonblocking_flag(self):
import matplotlib
matplotlib.use("Agg", force=True)
import matplotlib.pyplot as plt

graph = self.linear_graph
with patch.object(WorkFlowGraph, "_matplotlib_gui_available", return_value=True), \
patch("matplotlib.pyplot.show") as show:
fig = graph.display()
try:
show.assert_called_once_with(block=False)
self.assertTrue(graph._display_live)
finally:
graph.close_display()
plt.close(fig)
plt.close("all")

def test_display_empty_graph_does_not_open_window(self):
import matplotlib
matplotlib.use("Agg", force=True)
import matplotlib.pyplot as plt

graph = self.linear_graph
graph.graph.clear()
open_figures = set(plt.get_fignums())
try:
self.assertIsNone(graph.display())
self.assertEqual(open_figures, set(plt.get_fignums()))
finally:
plt.close("all")


if __name__ == "__main__":
unittest.main()