mirror of
https://github.com/langgenius/dify.git
synced 2025-11-25 17:31:56 +00:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Hanqing Zhao <sherry9277@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Harry <xh001x@hotmail.com>
130 lines
4.1 KiB
Python
130 lines
4.1 KiB
Python
import time
|
|
import uuid
|
|
from unittest.mock import MagicMock
|
|
|
|
from core.app.entities.app_invoke_entities import InvokeFrom
|
|
from core.tools.utils.configuration import ToolParameterConfigurationManager
|
|
from core.workflow.entities import GraphInitParams, GraphRuntimeState, VariablePool
|
|
from core.workflow.enums import WorkflowNodeExecutionStatus
|
|
from core.workflow.graph import Graph
|
|
from core.workflow.node_events import StreamCompletedEvent
|
|
from core.workflow.nodes.node_factory import DifyNodeFactory
|
|
from core.workflow.nodes.tool.tool_node import ToolNode
|
|
from core.workflow.system_variable import SystemVariable
|
|
from models.enums import UserFrom
|
|
|
|
|
|
def init_tool_node(config: dict):
|
|
graph_config = {
|
|
"edges": [
|
|
{
|
|
"id": "start-source-next-target",
|
|
"source": "start",
|
|
"target": "1",
|
|
},
|
|
],
|
|
"nodes": [{"data": {"type": "start", "title": "Start"}, "id": "start"}, config],
|
|
}
|
|
|
|
init_params = GraphInitParams(
|
|
tenant_id="1",
|
|
app_id="1",
|
|
workflow_id="1",
|
|
graph_config=graph_config,
|
|
user_id="1",
|
|
user_from=UserFrom.ACCOUNT,
|
|
invoke_from=InvokeFrom.DEBUGGER,
|
|
call_depth=0,
|
|
)
|
|
|
|
# construct variable pool
|
|
variable_pool = VariablePool(
|
|
system_variables=SystemVariable(user_id="aaa", files=[]),
|
|
user_inputs={},
|
|
environment_variables=[],
|
|
conversation_variables=[],
|
|
)
|
|
|
|
graph_runtime_state = GraphRuntimeState(variable_pool=variable_pool, start_at=time.perf_counter())
|
|
|
|
# Create node factory
|
|
node_factory = DifyNodeFactory(
|
|
graph_init_params=init_params,
|
|
graph_runtime_state=graph_runtime_state,
|
|
)
|
|
|
|
graph = Graph.init(graph_config=graph_config, node_factory=node_factory)
|
|
|
|
node = ToolNode(
|
|
id=str(uuid.uuid4()),
|
|
config=config,
|
|
graph_init_params=init_params,
|
|
graph_runtime_state=graph_runtime_state,
|
|
)
|
|
node.init_node_data(config.get("data", {}))
|
|
return node
|
|
|
|
|
|
def test_tool_variable_invoke():
|
|
node = init_tool_node(
|
|
config={
|
|
"id": "1",
|
|
"data": {
|
|
"type": "tool",
|
|
"title": "a",
|
|
"desc": "a",
|
|
"provider_id": "time",
|
|
"provider_type": "builtin",
|
|
"provider_name": "time",
|
|
"tool_name": "current_time",
|
|
"tool_label": "current_time",
|
|
"tool_configurations": {},
|
|
"tool_parameters": {},
|
|
},
|
|
}
|
|
)
|
|
|
|
ToolParameterConfigurationManager.decrypt_tool_parameters = MagicMock(return_value={"format": "%Y-%m-%d %H:%M:%S"})
|
|
|
|
node.graph_runtime_state.variable_pool.add(["1", "args1"], "1+1")
|
|
|
|
# execute node
|
|
result = node._run()
|
|
for item in result:
|
|
if isinstance(item, StreamCompletedEvent):
|
|
assert item.node_run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
|
assert item.node_run_result.outputs is not None
|
|
assert item.node_run_result.outputs.get("text") is not None
|
|
|
|
|
|
def test_tool_mixed_invoke():
|
|
node = init_tool_node(
|
|
config={
|
|
"id": "1",
|
|
"data": {
|
|
"type": "tool",
|
|
"title": "a",
|
|
"desc": "a",
|
|
"provider_id": "time",
|
|
"provider_type": "builtin",
|
|
"provider_name": "time",
|
|
"tool_name": "current_time",
|
|
"tool_label": "current_time",
|
|
"tool_configurations": {
|
|
"format": "%Y-%m-%d %H:%M:%S",
|
|
},
|
|
"tool_parameters": {},
|
|
},
|
|
}
|
|
)
|
|
|
|
ToolParameterConfigurationManager.decrypt_tool_parameters = MagicMock(return_value={"format": "%Y-%m-%d %H:%M:%S"})
|
|
|
|
# execute node
|
|
result = node._run()
|
|
for item in result:
|
|
if isinstance(item, StreamCompletedEvent):
|
|
assert item.node_run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
|
assert item.node_run_result.outputs is not None
|
|
assert item.node_run_result.outputs.get("text") is not None
|