From 71cc0074ef86cb841b270072fbae1f7f63a84c21 Mon Sep 17 00:00:00 2001 From: "xin.gao" <34891602+gaoxin-pen@users.noreply.github.com> Date: Mon, 22 Apr 2024 11:12:00 +0800 Subject: [PATCH] fix: delete tool parameters cache when sync draft workflow for run workflow use new parameter change in draft workflow (#3637) --- api/events/app_event.py | 3 +++ api/events/event_handlers/__init__.py | 1 + ...rameters_cache_when_sync_draft_workflow.py | 26 +++++++++++++++++++ api/services/workflow_service.py | 5 +++- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 api/events/event_handlers/delete_tool_parameters_cache_when_sync_draft_workflow.py diff --git a/api/events/app_event.py b/api/events/app_event.py index 8dbf34cbd..3a975958f 100644 --- a/api/events/app_event.py +++ b/api/events/app_event.py @@ -11,3 +11,6 @@ app_model_config_was_updated = signal('app-model-config-was-updated') # sender: app, kwargs: published_workflow app_published_workflow_was_updated = signal('app-published-workflow-was-updated') + +# sender: app, kwargs: synced_draft_workflow +app_draft_workflow_was_synced = signal('app-draft-workflow-was-synced') diff --git a/api/events/event_handlers/__init__.py b/api/events/event_handlers/__init__.py index cb640fa22..688b80aa8 100644 --- a/api/events/event_handlers/__init__.py +++ b/api/events/event_handlers/__init__.py @@ -5,6 +5,7 @@ from .create_installed_app_when_app_created import handle from .create_site_record_when_app_created import handle from .deduct_quota_when_messaeg_created import handle from .delete_installed_app_when_app_deleted import handle +from .delete_tool_parameters_cache_when_sync_draft_workflow import handle from .update_app_dataset_join_when_app_model_config_updated import handle from .update_app_dataset_join_when_app_published_workflow_updated import handle from .update_provider_last_used_at_when_messaeg_created import handle diff --git a/api/events/event_handlers/delete_tool_parameters_cache_when_sync_draft_workflow.py b/api/events/event_handlers/delete_tool_parameters_cache_when_sync_draft_workflow.py new file mode 100644 index 000000000..1f631be1c --- /dev/null +++ b/api/events/event_handlers/delete_tool_parameters_cache_when_sync_draft_workflow.py @@ -0,0 +1,26 @@ +from core.tools.tool_manager import ToolManager +from core.tools.utils.configuration import ToolParameterConfigurationManager +from core.workflow.entities.node_entities import NodeType +from core.workflow.nodes.tool.entities import ToolEntity +from events.app_event import app_draft_workflow_was_synced + + +@app_draft_workflow_was_synced.connect +def handle(sender, **kwargs): + app = sender + for node_data in kwargs.get('synced_draft_workflow').graph_dict.get('nodes', []): + if node_data.get('data', {}).get('type') == NodeType.TOOL.value: + tool_entity = ToolEntity(**node_data["data"]) + tool_runtime = ToolManager.get_tool_runtime( + provider_type=tool_entity.provider_type, + provider_name=tool_entity.provider_id, + tool_name=tool_entity.tool_name, + tenant_id=app.tenant_id, + ) + manager = ToolParameterConfigurationManager( + tenant_id=app.tenant_id, + tool_runtime=tool_runtime, + provider_name=tool_entity.provider_name, + provider_type=tool_entity.provider_type, + ) + manager.delete_tool_parameters_cache() diff --git a/api/services/workflow_service.py b/api/services/workflow_service.py index e1cffdd1b..01fd3aa4a 100644 --- a/api/services/workflow_service.py +++ b/api/services/workflow_service.py @@ -9,7 +9,7 @@ from core.model_runtime.utils.encoders import jsonable_encoder from core.workflow.entities.node_entities import NodeType from core.workflow.errors import WorkflowNodeRunFailedError from core.workflow.workflow_engine_manager import WorkflowEngineManager -from events.app_event import app_published_workflow_was_updated +from events.app_event import app_draft_workflow_was_synced, app_published_workflow_was_updated from extensions.ext_database import db from models.account import Account from models.model import App, AppMode @@ -98,6 +98,9 @@ class WorkflowService: # commit db session changes db.session.commit() + # trigger app workflow events + app_draft_workflow_was_synced.send(app_model, synced_draft_workflow=workflow) + # return draft workflow return workflow