Spaces:
Sleeping
Sleeping
Upload runtime_state_manager.py
Browse files
examples/runtime_state_manager.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from aworld.runners.state_manager import RuntimeStateManager, RunNodeBusiType, RunNodeStatus, RunNode
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
|
5 |
+
def test_runtime_state_manager():
|
6 |
+
state_manager = RuntimeStateManager()
|
7 |
+
session_id = "1"
|
8 |
+
|
9 |
+
state_manager.create_node(busi_type=RunNodeBusiType.TASK,
|
10 |
+
busi_id="1", session_id=session_id, msg_id="1")
|
11 |
+
|
12 |
+
state_manager.run_node(busi_type=RunNodeBusiType.TASK, busi_id="1")
|
13 |
+
node = state_manager.get_node(busi_type=RunNodeBusiType.TASK, busi_id="1")
|
14 |
+
assert node.status == RunNodeStatus.RUNNING
|
15 |
+
|
16 |
+
state_manager.break_node(busi_type=RunNodeBusiType.TASK, busi_id="1")
|
17 |
+
node = state_manager.get_node(busi_type=RunNodeBusiType.TASK, busi_id="1")
|
18 |
+
assert node.status == RunNodeStatus.BREAKED
|
19 |
+
|
20 |
+
state_manager.run_succeed(busi_type=RunNodeBusiType.TASK, busi_id="1")
|
21 |
+
node = state_manager.get_node(busi_type=RunNodeBusiType.TASK, busi_id="1")
|
22 |
+
assert node.status == RunNodeStatus.SUCCESS
|
23 |
+
|
24 |
+
state_manager.create_node(busi_type=RunNodeBusiType.TASK,
|
25 |
+
busi_id="2", session_id=session_id, msg_id="2", msg_from="1")
|
26 |
+
|
27 |
+
state_manager.run_node(busi_type=RunNodeBusiType.TASK, busi_id="2")
|
28 |
+
state_manager.run_failed(busi_type=RunNodeBusiType.TASK, busi_id="2")
|
29 |
+
node = state_manager.get_node(busi_type=RunNodeBusiType.TASK, busi_id="2")
|
30 |
+
assert node.status == RunNodeStatus.FAILED
|
31 |
+
|
32 |
+
state_manager.create_node(busi_type=RunNodeBusiType.TASK,
|
33 |
+
busi_id="3", session_id=session_id, msg_id="3", msg_from="1")
|
34 |
+
state_manager.run_node(busi_type=RunNodeBusiType.TASK, busi_id="3")
|
35 |
+
state_manager.run_timeout(busi_type=RunNodeBusiType.TASK, busi_id="3")
|
36 |
+
node = state_manager.get_node(busi_type=RunNodeBusiType.TASK, busi_id="3")
|
37 |
+
assert node.status == RunNodeStatus.TIMEOUNT
|
38 |
+
|
39 |
+
state_manager.create_node(busi_type=RunNodeBusiType.TASK,
|
40 |
+
busi_id="4", session_id=session_id, msg_id="4", msg_from="3")
|
41 |
+
state_manager.run_succeed(busi_type=RunNodeBusiType.TASK, busi_id="1")
|
42 |
+
|
43 |
+
nodes = state_manager.get_nodes(session_id=session_id)
|
44 |
+
build_run_flow(nodes)
|
45 |
+
|
46 |
+
|
47 |
+
def build_run_flow(nodes: List[RunNode]):
|
48 |
+
graph = {}
|
49 |
+
start_nodes = []
|
50 |
+
|
51 |
+
for node in nodes:
|
52 |
+
if hasattr(node, 'parent_node_id') and node.parent_node_id:
|
53 |
+
if node.parent_node_id not in graph:
|
54 |
+
graph[node.parent_node_id] = []
|
55 |
+
graph[node.parent_node_id].append(node.node_id)
|
56 |
+
else:
|
57 |
+
start_nodes.append(node.node_id)
|
58 |
+
|
59 |
+
for start in start_nodes:
|
60 |
+
print("-----------------------------------")
|
61 |
+
_print_tree(graph, start, "", True)
|
62 |
+
print("-----------------------------------")
|
63 |
+
|
64 |
+
|
65 |
+
def _print_tree(graph, node_id, prefix, is_last):
|
66 |
+
print(prefix + ("└── " if is_last else "├── ") + node_id)
|
67 |
+
if node_id in graph:
|
68 |
+
children = graph[node_id]
|
69 |
+
for i, child in enumerate(children):
|
70 |
+
_print_tree(graph, child, prefix +
|
71 |
+
(" " if is_last else "│ "), i == len(children)-1)
|
72 |
+
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
test_runtime_state_manager()
|