| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  | import time | 
					
						
							|  |  |  | import uuid | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from core.app.entities.app_invoke_entities import InvokeFrom | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from core.workflow.entities.node_entities import NodeRunResult | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from core.workflow.entities.variable_pool import VariablePool | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  | from core.workflow.enums import SystemVariableKey | 
					
						
							|  |  |  | from core.workflow.graph_engine.entities.graph import Graph | 
					
						
							|  |  |  | from core.workflow.graph_engine.entities.graph_init_params import GraphInitParams | 
					
						
							|  |  |  | from core.workflow.graph_engine.entities.graph_runtime_state import GraphRuntimeState | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from core.workflow.nodes.tool.tool_node import ToolNode | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from models.enums import UserFrom | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  | from models.workflow import WorkflowNodeExecutionStatus, WorkflowType | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  | def init_tool_node(config: dict): | 
					
						
							|  |  |  |     graph_config = { | 
					
						
							|  |  |  |         "edges": [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 "id": "start-source-next-target", | 
					
						
							|  |  |  |                 "source": "start", | 
					
						
							|  |  |  |                 "target": "1", | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         "nodes": [{"data": {"type": "start"}, "id": "start"}, config], | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |     graph = Graph.init(graph_config=graph_config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     init_params = GraphInitParams( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         tenant_id="1", | 
					
						
							|  |  |  |         app_id="1", | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |         workflow_type=WorkflowType.WORKFLOW, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         workflow_id="1", | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |         graph_config=graph_config, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         user_id="1", | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |         user_from=UserFrom.ACCOUNT, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |         invoke_from=InvokeFrom.DEBUGGER, | 
					
						
							|  |  |  |         call_depth=0, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # construct variable pool | 
					
						
							|  |  |  |     variable_pool = VariablePool( | 
					
						
							|  |  |  |         system_variables={SystemVariableKey.FILES: [], SystemVariableKey.USER_ID: "aaa"}, | 
					
						
							|  |  |  |         user_inputs={}, | 
					
						
							|  |  |  |         environment_variables=[], | 
					
						
							|  |  |  |         conversation_variables=[], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ToolNode( | 
					
						
							|  |  |  |         id=str(uuid.uuid4()), | 
					
						
							|  |  |  |         graph_init_params=init_params, | 
					
						
							|  |  |  |         graph=graph, | 
					
						
							|  |  |  |         graph_runtime_state=GraphRuntimeState(variable_pool=variable_pool, start_at=time.perf_counter()), | 
					
						
							|  |  |  |         config=config, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_tool_variable_invoke(): | 
					
						
							|  |  |  |     node = init_tool_node( | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         config={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "id": "1", | 
					
						
							|  |  |  |             "data": { | 
					
						
							|  |  |  |                 "title": "a", | 
					
						
							|  |  |  |                 "desc": "a", | 
					
						
							|  |  |  |                 "provider_id": "maths", | 
					
						
							|  |  |  |                 "provider_type": "builtin", | 
					
						
							|  |  |  |                 "provider_name": "maths", | 
					
						
							|  |  |  |                 "tool_name": "eval_expression", | 
					
						
							|  |  |  |                 "tool_label": "eval_expression", | 
					
						
							|  |  |  |                 "tool_configurations": {}, | 
					
						
							|  |  |  |                 "tool_parameters": { | 
					
						
							|  |  |  |                     "expression": { | 
					
						
							|  |  |  |                         "type": "variable", | 
					
						
							|  |  |  |                         "value": ["1", "123", "args1"], | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 }, | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |     node.graph_runtime_state.variable_pool.add(["1", "123", "args1"], "1+1") | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |     # execute node | 
					
						
							|  |  |  |     result = node._run() | 
					
						
							|  |  |  |     assert isinstance(result, NodeRunResult) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |     assert result.outputs is not None | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |     assert "2" in result.outputs["text"] | 
					
						
							|  |  |  |     assert result.outputs["files"] == [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_tool_mixed_invoke(): | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |     node = init_tool_node( | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         config={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "id": "1", | 
					
						
							|  |  |  |             "data": { | 
					
						
							|  |  |  |                 "title": "a", | 
					
						
							|  |  |  |                 "desc": "a", | 
					
						
							|  |  |  |                 "provider_id": "maths", | 
					
						
							|  |  |  |                 "provider_type": "builtin", | 
					
						
							|  |  |  |                 "provider_name": "maths", | 
					
						
							|  |  |  |                 "tool_name": "eval_expression", | 
					
						
							|  |  |  |                 "tool_label": "eval_expression", | 
					
						
							|  |  |  |                 "tool_configurations": {}, | 
					
						
							|  |  |  |                 "tool_parameters": { | 
					
						
							|  |  |  |                     "expression": { | 
					
						
							|  |  |  |                         "type": "mixed", | 
					
						
							|  |  |  |                         "value": "{{#1.args1#}}", | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 }, | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |     node.graph_runtime_state.variable_pool.add(["1", "args1"], "1+1") | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |     # execute node | 
					
						
							|  |  |  |     result = node._run() | 
					
						
							|  |  |  |     assert isinstance(result, NodeRunResult) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED | 
					
						
							| 
									
										
										
										
											2024-09-10 15:23:16 +08:00
										 |  |  |     assert result.outputs is not None | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |     assert "2" in result.outputs["text"] | 
					
						
							|  |  |  |     assert result.outputs["files"] == [] |