forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow.py
More file actions
520 lines (426 loc) · 16.1 KB
/
Copy pathworkflow.py
File metadata and controls
520 lines (426 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
"""
Workflow model and related classes for representing workflows in Agent Flow.
"""
import json
import uuid
from datetime import datetime
from enum import Enum, auto
from typing import Dict, List, Optional, Set, Any, Union, Tuple
class NodeType(Enum):
"""Types of nodes in a workflow."""
AGENT = auto()
TOOL = auto()
INPUT = auto()
OUTPUT = auto()
CONDITIONAL = auto()
TRANSFORM = auto()
ROUTER = auto()
class EdgeType(Enum):
"""Types of connections between nodes."""
DATA = auto() # Regular data flow
SUCCESS = auto() # Execute on success
ERROR = auto() # Execute on error
CONDITION_TRUE = auto() # Conditional branch when true
CONDITION_FALSE = auto() # Conditional branch when false
ROUTE_OUTPUT = auto() # Router output (used with port number in config)
class WorkflowNode:
"""
Represents a single node in a workflow.
A node can be an agent, a tool, an input/output node, or a
conditional/transform node that modifies the flow of data.
"""
def __init__(
self,
id: Optional[str] = None,
name: str = "Unnamed Node",
node_type: NodeType = NodeType.AGENT,
config: Optional[Dict[str, Any]] = None,
position: Optional[Dict[str, int]] = None
):
"""
Initialize a workflow node.
Args:
id: Unique identifier for the node (generated if not provided)
name: Human-readable name for the node
node_type: Type of the node (agent, tool, etc.)
config: Configuration parameters for the node
position: {x, y} position for visual display
"""
self.id = id or str(uuid.uuid4())
self.name = name
self.node_type = node_type
self.config = config or {}
self.position = position or {"x": 0, "y": 0}
self.incoming_edges: List['WorkflowEdge'] = []
self.outgoing_edges: List['WorkflowEdge'] = []
def add_incoming_edge(self, edge: 'WorkflowEdge') -> None:
"""Add an incoming edge to the node."""
self.incoming_edges.append(edge)
def add_outgoing_edge(self, edge: 'WorkflowEdge') -> None:
"""Add an outgoing edge from the node."""
self.outgoing_edges.append(edge)
def to_dict(self) -> Dict[str, Any]:
"""Convert the node to a dictionary for serialization."""
return {
"id": self.id,
"name": self.name,
"type": self.node_type.name,
"config": self.config,
"position": self.position
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'WorkflowNode':
"""Create a WorkflowNode from a dictionary representation."""
return cls(
id=data.get("id"),
name=data.get("name", "Unnamed Node"),
node_type=NodeType[data.get("type", "AGENT")],
config=data.get("config", {}),
position=data.get("position", {"x": 0, "y": 0})
)
class WorkflowEdge:
"""
Represents a connection between two nodes in a workflow.
An edge defines how data flows from one node to another, and can represent
different types of connections like regular data flow, conditional branches,
or error handling.
"""
def __init__(
self,
id: Optional[str] = None,
source_node_id: str = "",
target_node_id: str = "",
edge_type: EdgeType = EdgeType.DATA,
config: Optional[Dict[str, Any]] = None
):
"""
Initialize a workflow edge.
Args:
id: Unique identifier for the edge (generated if not provided)
source_node_id: ID of the source node
target_node_id: ID of the target node
edge_type: Type of the connection
config: Configuration parameters for the edge
"""
self.id = id or str(uuid.uuid4())
self.source_node_id = source_node_id
self.target_node_id = target_node_id
self.edge_type = edge_type
self.config = config or {}
def to_dict(self) -> Dict[str, Any]:
"""Convert the edge to a dictionary for serialization."""
return {
"id": self.id,
"source": self.source_node_id,
"target": self.target_node_id,
"type": self.edge_type.name,
"config": self.config
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'WorkflowEdge':
"""Create a WorkflowEdge from a dictionary representation."""
return cls(
id=data.get("id"),
source_node_id=data.get("source", ""),
target_node_id=data.get("target", ""),
edge_type=EdgeType[data.get("type", "DATA")],
config=data.get("config", {})
)
class Workflow:
"""
Represents a complete workflow with nodes and edges.
A workflow defines a network of connected agents, tools, and control
nodes that process data and execute tasks.
"""
def __init__(
self,
id: Optional[str] = None,
name: str = "Unnamed Workflow",
description: str = "",
created_at: Optional[datetime] = None,
updated_at: Optional[datetime] = None,
version: str = "1.0",
metadata: Optional[Dict[str, Any]] = None
):
"""
Initialize a workflow.
Args:
id: Unique identifier for the workflow (generated if not provided)
name: Human-readable name for the workflow
description: Description of the workflow
created_at: Creation timestamp (default: now)
updated_at: Last update timestamp (default: now)
version: Version of the workflow
metadata: Additional metadata for the workflow
"""
self.id = id or str(uuid.uuid4())
self.name = name
self.description = description
self.created_at = created_at or datetime.now()
self.updated_at = updated_at or datetime.now()
self.version = version
self.metadata = metadata or {}
self.nodes: Dict[str, WorkflowNode] = {}
self.edges: Dict[str, WorkflowEdge] = {}
def add_node(self, node: WorkflowNode) -> WorkflowNode:
"""
Add a node to the workflow.
Args:
node: The node to add
Returns:
The added node
"""
self.nodes[node.id] = node
return node
def get_node(self, node_id: str) -> Optional[WorkflowNode]:
"""
Get a node by ID.
Args:
node_id: ID of the node to get
Returns:
The node if found, None otherwise
"""
return self.nodes.get(node_id)
def remove_node(self, node_id: str) -> bool:
"""
Remove a node from the workflow.
Args:
node_id: ID of the node to remove
Returns:
True if the node was removed, False if it wasn't found
"""
if node_id not in self.nodes:
return False
# Remove any edges connected to this node
self.edges = {
edge_id: edge
for edge_id, edge in self.edges.items()
if edge.source_node_id != node_id and edge.target_node_id != node_id
}
# Remove the node itself
del self.nodes[node_id]
return True
def add_edge(
self,
source_node_id: str,
target_node_id: str,
edge_type: EdgeType = EdgeType.DATA,
config: Optional[Dict[str, Any]] = None
) -> Optional[WorkflowEdge]:
"""
Add an edge connecting two nodes.
Args:
source_node_id: ID of the source node
target_node_id: ID of the target node
edge_type: Type of the edge
config: Configuration parameters for the edge
Returns:
The created edge, or None if either node doesn't exist
"""
source_node = self.nodes.get(source_node_id)
target_node = self.nodes.get(target_node_id)
if not source_node or not target_node:
return None
edge = WorkflowEdge(
source_node_id=source_node_id,
target_node_id=target_node_id,
edge_type=edge_type,
config=config or {}
)
self.edges[edge.id] = edge
source_node.add_outgoing_edge(edge)
target_node.add_incoming_edge(edge)
return edge
def get_edge(self, edge_id: str) -> Optional[WorkflowEdge]:
"""
Get an edge by ID.
Args:
edge_id: ID of the edge to get
Returns:
The edge if found, None otherwise
"""
return self.edges.get(edge_id)
def remove_edge(self, edge_id: str) -> bool:
"""
Remove an edge from the workflow.
Args:
edge_id: ID of the edge to remove
Returns:
True if the edge was removed, False if it wasn't found
"""
if edge_id not in self.edges:
return False
edge = self.edges[edge_id]
# Remove edge from connected nodes
source_node = self.nodes.get(edge.source_node_id)
target_node = self.nodes.get(edge.target_node_id)
if source_node:
source_node.outgoing_edges = [
e for e in source_node.outgoing_edges if e.id != edge_id
]
if target_node:
target_node.incoming_edges = [
e for e in target_node.incoming_edges if e.id != edge_id
]
# Remove the edge itself
del self.edges[edge_id]
return True
def get_start_nodes(self) -> List[WorkflowNode]:
"""
Get all nodes that have no incoming edges (start nodes).
Returns:
List of start nodes
"""
return [
node for node in self.nodes.values()
if not node.incoming_edges
]
def get_end_nodes(self) -> List[WorkflowNode]:
"""
Get all nodes that have no outgoing edges (end nodes).
Returns:
List of end nodes
"""
return [
node for node in self.nodes.values()
if not node.outgoing_edges
]
def validate(self) -> Tuple[bool, List[str]]:
"""
Validate the workflow for correctness.
Returns:
(is_valid, errors) tuple
"""
errors = []
# Check for empty workflow
if not self.nodes:
errors.append("Workflow has no nodes")
return False, errors
# Check for orphaned nodes (no connections)
# Router nodes are valid even if they don't have connections yet
# Also check if this is a development/testing workflow with a force flag
orphaned_nodes = [
node.id for node in self.nodes.values()
if not node.incoming_edges and not node.outgoing_edges
and node.node_type != NodeType.ROUTER
and node.node_type != NodeType.INPUT # INPUT nodes can exist without incoming edges
and node.node_type != NodeType.OUTPUT # OUTPUT nodes can exist during construction
]
if orphaned_nodes and not self.metadata.get("force_validate", False):
# Only add as warning, not error to support in-progress workflows
if self.metadata.get("ignore_orphaned", False):
# Just log it but don't add to errors
import logging
logging.getLogger("WorkflowValidator").warning(f"Ignoring orphaned nodes: {', '.join(orphaned_nodes)}")
else:
errors.append(f"Orphaned nodes found: {', '.join(orphaned_nodes)}")
# Check for cycles
try:
self._detect_cycles()
except ValueError as e:
errors.append(str(e))
return len(errors) == 0, errors
def _detect_cycles(self) -> None:
"""
Detect cycles in the workflow graph.
Raises:
ValueError: If a cycle is detected
"""
visited: Set[str] = set()
path: Set[str] = set()
def dfs(node_id: str) -> None:
"""Depth-first search to detect cycles."""
if node_id in path:
raise ValueError(f"Cycle detected involving node {node_id}")
if node_id in visited:
return
visited.add(node_id)
path.add(node_id)
node = self.nodes.get(node_id)
if node:
for edge in node.outgoing_edges:
dfs(edge.target_node_id)
path.remove(node_id)
# Start DFS from all nodes to ensure we catch all cycles
for node_id in self.nodes:
if node_id not in visited:
dfs(node_id)
def to_dict(self) -> Dict[str, Any]:
"""
Convert the workflow to a dictionary for serialization.
Returns:
Dictionary representation of the workflow
"""
return {
"id": self.id,
"name": self.name,
"description": self.description,
"created_at": self.created_at.isoformat(),
"updated_at": self.updated_at.isoformat(),
"version": self.version,
"metadata": self.metadata,
"nodes": [node.to_dict() for node in self.nodes.values()],
"edges": [edge.to_dict() for edge in self.edges.values()]
}
def to_json(self, indent: Optional[int] = None) -> str:
"""
Convert the workflow to a JSON string.
Args:
indent: Number of spaces for indentation (None for compact)
Returns:
JSON string representing the workflow
"""
return json.dumps(self.to_dict(), indent=indent, default=str)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'Workflow':
"""
Create a Workflow from a dictionary representation.
Args:
data: Dictionary representation of the workflow
Returns:
Workflow instance
"""
# Parse timestamps
created_at = data.get("created_at")
if isinstance(created_at, str):
created_at = datetime.fromisoformat(created_at)
updated_at = data.get("updated_at")
if isinstance(updated_at, str):
updated_at = datetime.fromisoformat(updated_at)
# Create workflow instance
workflow = cls(
id=data.get("id"),
name=data.get("name", "Unnamed Workflow"),
description=data.get("description", ""),
created_at=created_at,
updated_at=updated_at,
version=data.get("version", "1.0"),
metadata=data.get("metadata", {})
)
# Add nodes
for node_data in data.get("nodes", []):
node = WorkflowNode.from_dict(node_data)
workflow.nodes[node.id] = node
# Add edges
for edge_data in data.get("edges", []):
edge = WorkflowEdge.from_dict(edge_data)
workflow.edges[edge.id] = edge
# Connect edges to nodes
source_node = workflow.nodes.get(edge.source_node_id)
target_node = workflow.nodes.get(edge.target_node_id)
if source_node:
source_node.add_outgoing_edge(edge)
if target_node:
target_node.add_incoming_edge(edge)
return workflow
@classmethod
def from_json(cls, json_str: str) -> 'Workflow':
"""
Create a Workflow from a JSON string.
Args:
json_str: JSON string representing the workflow
Returns:
Workflow instance
"""
data = json.loads(json_str)
return cls.from_dict(data)