|
@@ -1,8 +1,8 @@
|
|
import json
|
|
import json
|
|
import time
|
|
import time
|
|
-from collections.abc import Sequence
|
|
|
|
|
|
+from collections.abc import Callable, Generator, Sequence
|
|
from datetime import datetime, timezone
|
|
from datetime import datetime, timezone
|
|
-from typing import Optional
|
|
|
|
|
|
+from typing import Any, Optional
|
|
|
|
|
|
from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfigManager
|
|
from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfigManager
|
|
from core.app.apps.workflow.app_config_manager import WorkflowAppConfigManager
|
|
from core.app.apps.workflow.app_config_manager import WorkflowAppConfigManager
|
|
@@ -10,7 +10,9 @@ from core.app.segments import Variable
|
|
from core.model_runtime.utils.encoders import jsonable_encoder
|
|
from core.model_runtime.utils.encoders import jsonable_encoder
|
|
from core.workflow.entities.node_entities import NodeRunResult, NodeType
|
|
from core.workflow.entities.node_entities import NodeRunResult, NodeType
|
|
from core.workflow.errors import WorkflowNodeRunFailedError
|
|
from core.workflow.errors import WorkflowNodeRunFailedError
|
|
-from core.workflow.nodes.event import RunCompletedEvent
|
|
|
|
|
|
+from core.workflow.graph_engine.entities.event import InNodeEvent
|
|
|
|
+from core.workflow.nodes.base_node import BaseNode
|
|
|
|
+from core.workflow.nodes.event import RunCompletedEvent, RunEvent
|
|
from core.workflow.nodes.node_mapping import node_classes
|
|
from core.workflow.nodes.node_mapping import node_classes
|
|
from core.workflow.workflow_entry import WorkflowEntry
|
|
from core.workflow.workflow_entry import WorkflowEntry
|
|
from events.app_event import app_draft_workflow_was_synced, app_published_workflow_was_updated
|
|
from events.app_event import app_draft_workflow_was_synced, app_published_workflow_was_updated
|
|
@@ -216,13 +218,64 @@ class WorkflowService:
|
|
# run draft workflow node
|
|
# run draft workflow node
|
|
start_at = time.perf_counter()
|
|
start_at = time.perf_counter()
|
|
|
|
|
|
- try:
|
|
|
|
- node_instance, generator = WorkflowEntry.single_step_run(
|
|
|
|
|
|
+ workflow_node_execution = self._handle_node_run_result(
|
|
|
|
+ getter=lambda: WorkflowEntry.single_step_run(
|
|
workflow=draft_workflow,
|
|
workflow=draft_workflow,
|
|
node_id=node_id,
|
|
node_id=node_id,
|
|
user_inputs=user_inputs,
|
|
user_inputs=user_inputs,
|
|
user_id=account.id,
|
|
user_id=account.id,
|
|
- )
|
|
|
|
|
|
+ ),
|
|
|
|
+ start_at=start_at,
|
|
|
|
+ tenant_id=app_model.tenant_id,
|
|
|
|
+ node_id=node_id,
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ db.session.add(workflow_node_execution)
|
|
|
|
+ db.session.commit()
|
|
|
|
+
|
|
|
|
+ return workflow_node_execution
|
|
|
|
+
|
|
|
|
+ def run_free_workflow_node(
|
|
|
|
+ self, node_data: dict, tenant_id: str, user_id: str, node_id: str, user_inputs: dict[str, Any]
|
|
|
|
+ ) -> WorkflowNodeExecution:
|
|
|
|
+ """
|
|
|
|
+ Run draft workflow node
|
|
|
|
+ """
|
|
|
|
+ # run draft workflow node
|
|
|
|
+ start_at = time.perf_counter()
|
|
|
|
+
|
|
|
|
+ workflow_node_execution = self._handle_node_run_result(
|
|
|
|
+ getter=lambda: WorkflowEntry.run_free_node(
|
|
|
|
+ node_id=node_id,
|
|
|
|
+ node_data=node_data,
|
|
|
|
+ tenant_id=tenant_id,
|
|
|
|
+ user_id=user_id,
|
|
|
|
+ user_inputs=user_inputs,
|
|
|
|
+ ),
|
|
|
|
+ start_at=start_at,
|
|
|
|
+ tenant_id=tenant_id,
|
|
|
|
+ node_id=node_id
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ return workflow_node_execution
|
|
|
|
+
|
|
|
|
+ def _handle_node_run_result(
|
|
|
|
+ self,
|
|
|
|
+ getter: Callable[[], tuple[BaseNode, Generator[RunEvent | InNodeEvent, None, None]]],
|
|
|
|
+ start_at: float,
|
|
|
|
+ tenant_id: str,
|
|
|
|
+ node_id: str,
|
|
|
|
+ ):
|
|
|
|
+ """
|
|
|
|
+ Handle node run result
|
|
|
|
+
|
|
|
|
+ :param getter: Callable[[], tuple[BaseNode, Generator[RunEvent | InNodeEvent, None, None]]]
|
|
|
|
+ :param start_at: float
|
|
|
|
+ :param tenant_id: str
|
|
|
|
+ :param node_id: str
|
|
|
|
+ """
|
|
|
|
+ try:
|
|
|
|
+ node_instance, generator = getter()
|
|
|
|
|
|
node_run_result: NodeRunResult | None = None
|
|
node_run_result: NodeRunResult | None = None
|
|
for event in generator:
|
|
for event in generator:
|
|
@@ -245,9 +298,7 @@ class WorkflowService:
|
|
error = e.error
|
|
error = e.error
|
|
|
|
|
|
workflow_node_execution = WorkflowNodeExecution()
|
|
workflow_node_execution = WorkflowNodeExecution()
|
|
- workflow_node_execution.tenant_id = app_model.tenant_id
|
|
|
|
- workflow_node_execution.app_id = app_model.id
|
|
|
|
- workflow_node_execution.workflow_id = draft_workflow.id
|
|
|
|
|
|
+ workflow_node_execution.tenant_id = tenant_id
|
|
workflow_node_execution.triggered_from = WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP.value
|
|
workflow_node_execution.triggered_from = WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP.value
|
|
workflow_node_execution.index = 1
|
|
workflow_node_execution.index = 1
|
|
workflow_node_execution.node_id = node_id
|
|
workflow_node_execution.node_id = node_id
|
|
@@ -255,7 +306,6 @@ class WorkflowService:
|
|
workflow_node_execution.title = node_instance.node_data.title
|
|
workflow_node_execution.title = node_instance.node_data.title
|
|
workflow_node_execution.elapsed_time = time.perf_counter() - start_at
|
|
workflow_node_execution.elapsed_time = time.perf_counter() - start_at
|
|
workflow_node_execution.created_by_role = CreatedByRole.ACCOUNT.value
|
|
workflow_node_execution.created_by_role = CreatedByRole.ACCOUNT.value
|
|
- workflow_node_execution.created_by = account.id
|
|
|
|
workflow_node_execution.created_at = datetime.now(timezone.utc).replace(tzinfo=None)
|
|
workflow_node_execution.created_at = datetime.now(timezone.utc).replace(tzinfo=None)
|
|
workflow_node_execution.finished_at = datetime.now(timezone.utc).replace(tzinfo=None)
|
|
workflow_node_execution.finished_at = datetime.now(timezone.utc).replace(tzinfo=None)
|
|
|
|
|
|
@@ -277,9 +327,6 @@ class WorkflowService:
|
|
workflow_node_execution.status = WorkflowNodeExecutionStatus.FAILED.value
|
|
workflow_node_execution.status = WorkflowNodeExecutionStatus.FAILED.value
|
|
workflow_node_execution.error = error
|
|
workflow_node_execution.error = error
|
|
|
|
|
|
- db.session.add(workflow_node_execution)
|
|
|
|
- db.session.commit()
|
|
|
|
-
|
|
|
|
return workflow_node_execution
|
|
return workflow_node_execution
|
|
|
|
|
|
def convert_to_workflow(self, app_model: App, account: Account, args: dict) -> App:
|
|
def convert_to_workflow(self, app_model: App, account: Account, args: dict) -> App:
|
|
@@ -302,10 +349,10 @@ class WorkflowService:
|
|
new_app = workflow_converter.convert_to_workflow(
|
|
new_app = workflow_converter.convert_to_workflow(
|
|
app_model=app_model,
|
|
app_model=app_model,
|
|
account=account,
|
|
account=account,
|
|
- name=args.get("name"),
|
|
|
|
- icon_type=args.get("icon_type"),
|
|
|
|
- icon=args.get("icon"),
|
|
|
|
- icon_background=args.get("icon_background"),
|
|
|
|
|
|
+ name=args.get("name", ""),
|
|
|
|
+ icon_type=args.get("icon_type", ""),
|
|
|
|
+ icon=args.get("icon", ""),
|
|
|
|
+ icon_background=args.get("icon_background", ""),
|
|
)
|
|
)
|
|
|
|
|
|
return new_app
|
|
return new_app
|