You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

workflow_service.py 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. import json
  2. import time
  3. from collections.abc import Callable, Generator, Sequence
  4. from datetime import UTC, datetime
  5. from typing import Any, Optional
  6. from uuid import uuid4
  7. from sqlalchemy import select
  8. from sqlalchemy.orm import Session
  9. from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfigManager
  10. from core.app.apps.workflow.app_config_manager import WorkflowAppConfigManager
  11. from core.model_runtime.utils.encoders import jsonable_encoder
  12. from core.repositories import SQLAlchemyWorkflowNodeExecutionRepository
  13. from core.variables import Variable
  14. from core.workflow.entities.node_entities import NodeRunResult
  15. from core.workflow.errors import WorkflowNodeRunFailedError
  16. from core.workflow.graph_engine.entities.event import InNodeEvent
  17. from core.workflow.nodes import NodeType
  18. from core.workflow.nodes.base.node import BaseNode
  19. from core.workflow.nodes.enums import ErrorStrategy
  20. from core.workflow.nodes.event import RunCompletedEvent
  21. from core.workflow.nodes.event.types import NodeEvent
  22. from core.workflow.nodes.node_mapping import LATEST_VERSION, NODE_TYPE_CLASSES_MAPPING
  23. from core.workflow.workflow_entry import WorkflowEntry
  24. from events.app_event import app_draft_workflow_was_synced, app_published_workflow_was_updated
  25. from extensions.ext_database import db
  26. from models.account import Account
  27. from models.enums import CreatedByRole
  28. from models.model import App, AppMode
  29. from models.tools import WorkflowToolProvider
  30. from models.workflow import (
  31. Workflow,
  32. WorkflowNodeExecution,
  33. WorkflowNodeExecutionStatus,
  34. WorkflowNodeExecutionTriggeredFrom,
  35. WorkflowType,
  36. )
  37. from services.errors.app import WorkflowHashNotEqualError
  38. from services.workflow.workflow_converter import WorkflowConverter
  39. from .errors.workflow_service import DraftWorkflowDeletionError, WorkflowInUseError
  40. class WorkflowService:
  41. """
  42. Workflow Service
  43. """
  44. def get_draft_workflow(self, app_model: App) -> Optional[Workflow]:
  45. """
  46. Get draft workflow
  47. """
  48. # fetch draft workflow by app_model
  49. workflow = (
  50. db.session.query(Workflow)
  51. .filter(
  52. Workflow.tenant_id == app_model.tenant_id, Workflow.app_id == app_model.id, Workflow.version == "draft"
  53. )
  54. .first()
  55. )
  56. # return draft workflow
  57. return workflow
  58. def get_published_workflow(self, app_model: App) -> Optional[Workflow]:
  59. """
  60. Get published workflow
  61. """
  62. if not app_model.workflow_id:
  63. return None
  64. # fetch published workflow by workflow_id
  65. workflow = (
  66. db.session.query(Workflow)
  67. .filter(
  68. Workflow.tenant_id == app_model.tenant_id,
  69. Workflow.app_id == app_model.id,
  70. Workflow.id == app_model.workflow_id,
  71. )
  72. .first()
  73. )
  74. return workflow
  75. def get_all_published_workflow(
  76. self,
  77. *,
  78. session: Session,
  79. app_model: App,
  80. page: int,
  81. limit: int,
  82. user_id: str | None,
  83. named_only: bool = False,
  84. ) -> tuple[Sequence[Workflow], bool]:
  85. """
  86. Get published workflow with pagination
  87. """
  88. if not app_model.workflow_id:
  89. return [], False
  90. stmt = (
  91. select(Workflow)
  92. .where(Workflow.app_id == app_model.id)
  93. .order_by(Workflow.version.desc())
  94. .limit(limit + 1)
  95. .offset((page - 1) * limit)
  96. )
  97. if user_id:
  98. stmt = stmt.where(Workflow.created_by == user_id)
  99. if named_only:
  100. stmt = stmt.where(Workflow.marked_name != "")
  101. workflows = session.scalars(stmt).all()
  102. has_more = len(workflows) > limit
  103. if has_more:
  104. workflows = workflows[:-1]
  105. return workflows, has_more
  106. def sync_draft_workflow(
  107. self,
  108. *,
  109. app_model: App,
  110. graph: dict,
  111. features: dict,
  112. unique_hash: Optional[str],
  113. account: Account,
  114. environment_variables: Sequence[Variable],
  115. conversation_variables: Sequence[Variable],
  116. ) -> Workflow:
  117. """
  118. Sync draft workflow
  119. :raises WorkflowHashNotEqualError
  120. """
  121. # fetch draft workflow by app_model
  122. workflow = self.get_draft_workflow(app_model=app_model)
  123. if workflow and workflow.unique_hash != unique_hash:
  124. raise WorkflowHashNotEqualError()
  125. # validate features structure
  126. self.validate_features_structure(app_model=app_model, features=features)
  127. # create draft workflow if not found
  128. if not workflow:
  129. workflow = Workflow(
  130. tenant_id=app_model.tenant_id,
  131. app_id=app_model.id,
  132. type=WorkflowType.from_app_mode(app_model.mode).value,
  133. version="draft",
  134. graph=json.dumps(graph),
  135. features=json.dumps(features),
  136. created_by=account.id,
  137. environment_variables=environment_variables,
  138. conversation_variables=conversation_variables,
  139. )
  140. db.session.add(workflow)
  141. # update draft workflow if found
  142. else:
  143. workflow.graph = json.dumps(graph)
  144. workflow.features = json.dumps(features)
  145. workflow.updated_by = account.id
  146. workflow.updated_at = datetime.now(UTC).replace(tzinfo=None)
  147. workflow.environment_variables = environment_variables
  148. workflow.conversation_variables = conversation_variables
  149. # commit db session changes
  150. db.session.commit()
  151. # trigger app workflow events
  152. app_draft_workflow_was_synced.send(app_model, synced_draft_workflow=workflow)
  153. # return draft workflow
  154. return workflow
  155. def publish_workflow(
  156. self,
  157. *,
  158. session: Session,
  159. app_model: App,
  160. account: Account,
  161. marked_name: str = "",
  162. marked_comment: str = "",
  163. ) -> Workflow:
  164. draft_workflow_stmt = select(Workflow).where(
  165. Workflow.tenant_id == app_model.tenant_id,
  166. Workflow.app_id == app_model.id,
  167. Workflow.version == "draft",
  168. )
  169. draft_workflow = session.scalar(draft_workflow_stmt)
  170. if not draft_workflow:
  171. raise ValueError("No valid workflow found.")
  172. # create new workflow
  173. workflow = Workflow.new(
  174. tenant_id=app_model.tenant_id,
  175. app_id=app_model.id,
  176. type=draft_workflow.type,
  177. version=str(datetime.now(UTC).replace(tzinfo=None)),
  178. graph=draft_workflow.graph,
  179. features=draft_workflow.features,
  180. created_by=account.id,
  181. environment_variables=draft_workflow.environment_variables,
  182. conversation_variables=draft_workflow.conversation_variables,
  183. marked_name=marked_name,
  184. marked_comment=marked_comment,
  185. )
  186. # commit db session changes
  187. session.add(workflow)
  188. # trigger app workflow events
  189. app_published_workflow_was_updated.send(app_model, published_workflow=workflow)
  190. # return new workflow
  191. return workflow
  192. def get_default_block_configs(self) -> list[dict]:
  193. """
  194. Get default block configs
  195. """
  196. # return default block config
  197. default_block_configs = []
  198. for node_class_mapping in NODE_TYPE_CLASSES_MAPPING.values():
  199. node_class = node_class_mapping[LATEST_VERSION]
  200. default_config = node_class.get_default_config()
  201. if default_config:
  202. default_block_configs.append(default_config)
  203. return default_block_configs
  204. def get_default_block_config(self, node_type: str, filters: Optional[dict] = None) -> Optional[dict]:
  205. """
  206. Get default config of node.
  207. :param node_type: node type
  208. :param filters: filter by node config parameters.
  209. :return:
  210. """
  211. node_type_enum = NodeType(node_type)
  212. # return default block config
  213. if node_type_enum not in NODE_TYPE_CLASSES_MAPPING:
  214. return None
  215. node_class = NODE_TYPE_CLASSES_MAPPING[node_type_enum][LATEST_VERSION]
  216. default_config = node_class.get_default_config(filters=filters)
  217. if not default_config:
  218. return None
  219. return default_config
  220. def run_draft_workflow_node(
  221. self, app_model: App, node_id: str, user_inputs: dict, account: Account
  222. ) -> WorkflowNodeExecution:
  223. """
  224. Run draft workflow node
  225. """
  226. # fetch draft workflow by app_model
  227. draft_workflow = self.get_draft_workflow(app_model=app_model)
  228. if not draft_workflow:
  229. raise ValueError("Workflow not initialized")
  230. # run draft workflow node
  231. start_at = time.perf_counter()
  232. workflow_node_execution = self._handle_node_run_result(
  233. getter=lambda: WorkflowEntry.single_step_run(
  234. workflow=draft_workflow,
  235. node_id=node_id,
  236. user_inputs=user_inputs,
  237. user_id=account.id,
  238. ),
  239. start_at=start_at,
  240. tenant_id=app_model.tenant_id,
  241. node_id=node_id,
  242. )
  243. workflow_node_execution.app_id = app_model.id
  244. workflow_node_execution.created_by = account.id
  245. workflow_node_execution.workflow_id = draft_workflow.id
  246. # Use the repository to save the workflow node execution
  247. repository = SQLAlchemyWorkflowNodeExecutionRepository(
  248. session_factory=db.engine, tenant_id=app_model.tenant_id, app_id=app_model.id
  249. )
  250. repository.save(workflow_node_execution)
  251. return workflow_node_execution
  252. def run_free_workflow_node(
  253. self, node_data: dict, tenant_id: str, user_id: str, node_id: str, user_inputs: dict[str, Any]
  254. ) -> WorkflowNodeExecution:
  255. """
  256. Run draft workflow node
  257. """
  258. # run draft workflow node
  259. start_at = time.perf_counter()
  260. workflow_node_execution = self._handle_node_run_result(
  261. getter=lambda: WorkflowEntry.run_free_node(
  262. node_id=node_id,
  263. node_data=node_data,
  264. tenant_id=tenant_id,
  265. user_id=user_id,
  266. user_inputs=user_inputs,
  267. ),
  268. start_at=start_at,
  269. tenant_id=tenant_id,
  270. node_id=node_id,
  271. )
  272. return workflow_node_execution
  273. def _handle_node_run_result(
  274. self,
  275. getter: Callable[[], tuple[BaseNode, Generator[NodeEvent | InNodeEvent, None, None]]],
  276. start_at: float,
  277. tenant_id: str,
  278. node_id: str,
  279. ) -> WorkflowNodeExecution:
  280. """
  281. Handle node run result
  282. :param getter: Callable[[], tuple[BaseNode, Generator[RunEvent | InNodeEvent, None, None]]]
  283. :param start_at: float
  284. :param tenant_id: str
  285. :param node_id: str
  286. """
  287. try:
  288. node_instance, generator = getter()
  289. node_run_result: NodeRunResult | None = None
  290. for event in generator:
  291. if isinstance(event, RunCompletedEvent):
  292. node_run_result = event.run_result
  293. # sign output files
  294. node_run_result.outputs = WorkflowEntry.handle_special_values(node_run_result.outputs)
  295. break
  296. if not node_run_result:
  297. raise ValueError("Node run failed with no run result")
  298. # single step debug mode error handling return
  299. if node_run_result.status == WorkflowNodeExecutionStatus.FAILED and node_instance.should_continue_on_error:
  300. node_error_args: dict[str, Any] = {
  301. "status": WorkflowNodeExecutionStatus.EXCEPTION,
  302. "error": node_run_result.error,
  303. "inputs": node_run_result.inputs,
  304. "metadata": {"error_strategy": node_instance.node_data.error_strategy},
  305. }
  306. if node_instance.node_data.error_strategy is ErrorStrategy.DEFAULT_VALUE:
  307. node_run_result = NodeRunResult(
  308. **node_error_args,
  309. outputs={
  310. **node_instance.node_data.default_value_dict,
  311. "error_message": node_run_result.error,
  312. "error_type": node_run_result.error_type,
  313. },
  314. )
  315. else:
  316. node_run_result = NodeRunResult(
  317. **node_error_args,
  318. outputs={
  319. "error_message": node_run_result.error,
  320. "error_type": node_run_result.error_type,
  321. },
  322. )
  323. run_succeeded = node_run_result.status in (
  324. WorkflowNodeExecutionStatus.SUCCEEDED,
  325. WorkflowNodeExecutionStatus.EXCEPTION,
  326. )
  327. error = node_run_result.error if not run_succeeded else None
  328. except WorkflowNodeRunFailedError as e:
  329. node_instance = e.node_instance
  330. run_succeeded = False
  331. node_run_result = None
  332. error = e.error
  333. workflow_node_execution = WorkflowNodeExecution()
  334. workflow_node_execution.id = str(uuid4())
  335. workflow_node_execution.tenant_id = tenant_id
  336. workflow_node_execution.triggered_from = WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP.value
  337. workflow_node_execution.index = 1
  338. workflow_node_execution.node_id = node_id
  339. workflow_node_execution.node_type = node_instance.node_type
  340. workflow_node_execution.title = node_instance.node_data.title
  341. workflow_node_execution.elapsed_time = time.perf_counter() - start_at
  342. workflow_node_execution.created_by_role = CreatedByRole.ACCOUNT.value
  343. workflow_node_execution.created_at = datetime.now(UTC).replace(tzinfo=None)
  344. workflow_node_execution.finished_at = datetime.now(UTC).replace(tzinfo=None)
  345. if run_succeeded and node_run_result:
  346. # create workflow node execution
  347. inputs = WorkflowEntry.handle_special_values(node_run_result.inputs) if node_run_result.inputs else None
  348. process_data = (
  349. WorkflowEntry.handle_special_values(node_run_result.process_data)
  350. if node_run_result.process_data
  351. else None
  352. )
  353. outputs = WorkflowEntry.handle_special_values(node_run_result.outputs) if node_run_result.outputs else None
  354. workflow_node_execution.inputs = json.dumps(inputs)
  355. workflow_node_execution.process_data = json.dumps(process_data)
  356. workflow_node_execution.outputs = json.dumps(outputs)
  357. workflow_node_execution.execution_metadata = (
  358. json.dumps(jsonable_encoder(node_run_result.metadata)) if node_run_result.metadata else None
  359. )
  360. if node_run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED:
  361. workflow_node_execution.status = WorkflowNodeExecutionStatus.SUCCEEDED.value
  362. elif node_run_result.status == WorkflowNodeExecutionStatus.EXCEPTION:
  363. workflow_node_execution.status = WorkflowNodeExecutionStatus.EXCEPTION.value
  364. workflow_node_execution.error = node_run_result.error
  365. else:
  366. # create workflow node execution
  367. workflow_node_execution.status = WorkflowNodeExecutionStatus.FAILED.value
  368. workflow_node_execution.error = error
  369. return workflow_node_execution
  370. def convert_to_workflow(self, app_model: App, account: Account, args: dict) -> App:
  371. """
  372. Basic mode of chatbot app(expert mode) to workflow
  373. Completion App to Workflow App
  374. :param app_model: App instance
  375. :param account: Account instance
  376. :param args: dict
  377. :return:
  378. """
  379. # chatbot convert to workflow mode
  380. workflow_converter = WorkflowConverter()
  381. if app_model.mode not in {AppMode.CHAT.value, AppMode.COMPLETION.value}:
  382. raise ValueError(f"Current App mode: {app_model.mode} is not supported convert to workflow.")
  383. # convert to workflow
  384. new_app: App = workflow_converter.convert_to_workflow(
  385. app_model=app_model,
  386. account=account,
  387. name=args.get("name", "Default Name"),
  388. icon_type=args.get("icon_type", "emoji"),
  389. icon=args.get("icon", "🤖"),
  390. icon_background=args.get("icon_background", "#FFEAD5"),
  391. )
  392. return new_app
  393. def validate_features_structure(self, app_model: App, features: dict) -> dict:
  394. if app_model.mode == AppMode.ADVANCED_CHAT.value:
  395. return AdvancedChatAppConfigManager.config_validate(
  396. tenant_id=app_model.tenant_id, config=features, only_structure_validate=True
  397. )
  398. elif app_model.mode == AppMode.WORKFLOW.value:
  399. return WorkflowAppConfigManager.config_validate(
  400. tenant_id=app_model.tenant_id, config=features, only_structure_validate=True
  401. )
  402. else:
  403. raise ValueError(f"Invalid app mode: {app_model.mode}")
  404. def update_workflow(
  405. self, *, session: Session, workflow_id: str, tenant_id: str, account_id: str, data: dict
  406. ) -> Optional[Workflow]:
  407. """
  408. Update workflow attributes
  409. :param session: SQLAlchemy database session
  410. :param workflow_id: Workflow ID
  411. :param tenant_id: Tenant ID
  412. :param account_id: Account ID (for permission check)
  413. :param data: Dictionary containing fields to update
  414. :return: Updated workflow or None if not found
  415. """
  416. stmt = select(Workflow).where(Workflow.id == workflow_id, Workflow.tenant_id == tenant_id)
  417. workflow = session.scalar(stmt)
  418. if not workflow:
  419. return None
  420. allowed_fields = ["marked_name", "marked_comment"]
  421. for field, value in data.items():
  422. if field in allowed_fields:
  423. setattr(workflow, field, value)
  424. workflow.updated_by = account_id
  425. workflow.updated_at = datetime.now(UTC).replace(tzinfo=None)
  426. return workflow
  427. def delete_workflow(self, *, session: Session, workflow_id: str, tenant_id: str) -> bool:
  428. """
  429. Delete a workflow
  430. :param session: SQLAlchemy database session
  431. :param workflow_id: Workflow ID
  432. :param tenant_id: Tenant ID
  433. :return: True if successful
  434. :raises: ValueError if workflow not found
  435. :raises: WorkflowInUseError if workflow is in use
  436. :raises: DraftWorkflowDeletionError if workflow is a draft version
  437. """
  438. stmt = select(Workflow).where(Workflow.id == workflow_id, Workflow.tenant_id == tenant_id)
  439. workflow = session.scalar(stmt)
  440. if not workflow:
  441. raise ValueError(f"Workflow with ID {workflow_id} not found")
  442. # Check if workflow is a draft version
  443. if workflow.version == "draft":
  444. raise DraftWorkflowDeletionError("Cannot delete draft workflow versions")
  445. # Check if this workflow is currently referenced by an app
  446. stmt = select(App).where(App.workflow_id == workflow_id)
  447. app = session.scalar(stmt)
  448. if app:
  449. # Cannot delete a workflow that's currently in use by an app
  450. raise WorkflowInUseError(f"Cannot delete workflow that is currently in use by app '{app.name}'")
  451. # Don't use workflow.tool_published as it's not accurate for specific workflow versions
  452. # Check if there's a tool provider using this specific workflow version
  453. tool_provider = (
  454. session.query(WorkflowToolProvider)
  455. .filter(
  456. WorkflowToolProvider.tenant_id == workflow.tenant_id,
  457. WorkflowToolProvider.app_id == workflow.app_id,
  458. WorkflowToolProvider.version == workflow.version,
  459. )
  460. .first()
  461. )
  462. if tool_provider:
  463. # Cannot delete a workflow that's published as a tool
  464. raise WorkflowInUseError("Cannot delete workflow that is published as a tool")
  465. session.delete(workflow)
  466. return True