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_run_service.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import threading
  2. from collections.abc import Sequence
  3. from typing import Optional
  4. import contexts
  5. from core.repositories import SQLAlchemyWorkflowNodeExecutionRepository
  6. from core.workflow.repository.workflow_node_execution_repository import OrderConfig
  7. from extensions.ext_database import db
  8. from libs.infinite_scroll_pagination import InfiniteScrollPagination
  9. from models import (
  10. Account,
  11. App,
  12. EndUser,
  13. WorkflowNodeExecution,
  14. WorkflowRun,
  15. WorkflowRunTriggeredFrom,
  16. )
  17. class WorkflowRunService:
  18. def get_paginate_advanced_chat_workflow_runs(self, app_model: App, args: dict) -> InfiniteScrollPagination:
  19. """
  20. Get advanced chat app workflow run list
  21. Only return triggered_from == advanced_chat
  22. :param app_model: app model
  23. :param args: request args
  24. """
  25. class WorkflowWithMessage:
  26. message_id: str
  27. conversation_id: str
  28. def __init__(self, workflow_run: WorkflowRun):
  29. self._workflow_run = workflow_run
  30. def __getattr__(self, item):
  31. return getattr(self._workflow_run, item)
  32. pagination = self.get_paginate_workflow_runs(app_model, args)
  33. with_message_workflow_runs = []
  34. for workflow_run in pagination.data:
  35. message = workflow_run.message
  36. with_message_workflow_run = WorkflowWithMessage(workflow_run=workflow_run)
  37. if message:
  38. with_message_workflow_run.message_id = message.id
  39. with_message_workflow_run.conversation_id = message.conversation_id
  40. with_message_workflow_runs.append(with_message_workflow_run)
  41. pagination.data = with_message_workflow_runs
  42. return pagination
  43. def get_paginate_workflow_runs(self, app_model: App, args: dict) -> InfiniteScrollPagination:
  44. """
  45. Get debug workflow run list
  46. Only return triggered_from == debugging
  47. :param app_model: app model
  48. :param args: request args
  49. """
  50. limit = int(args.get("limit", 20))
  51. base_query = db.session.query(WorkflowRun).filter(
  52. WorkflowRun.tenant_id == app_model.tenant_id,
  53. WorkflowRun.app_id == app_model.id,
  54. WorkflowRun.triggered_from == WorkflowRunTriggeredFrom.DEBUGGING.value,
  55. )
  56. if args.get("last_id"):
  57. last_workflow_run = base_query.filter(
  58. WorkflowRun.id == args.get("last_id"),
  59. ).first()
  60. if not last_workflow_run:
  61. raise ValueError("Last workflow run not exists")
  62. workflow_runs = (
  63. base_query.filter(
  64. WorkflowRun.created_at < last_workflow_run.created_at, WorkflowRun.id != last_workflow_run.id
  65. )
  66. .order_by(WorkflowRun.created_at.desc())
  67. .limit(limit)
  68. .all()
  69. )
  70. else:
  71. workflow_runs = base_query.order_by(WorkflowRun.created_at.desc()).limit(limit).all()
  72. has_more = False
  73. if len(workflow_runs) == limit:
  74. current_page_first_workflow_run = workflow_runs[-1]
  75. rest_count = base_query.filter(
  76. WorkflowRun.created_at < current_page_first_workflow_run.created_at,
  77. WorkflowRun.id != current_page_first_workflow_run.id,
  78. ).count()
  79. if rest_count > 0:
  80. has_more = True
  81. return InfiniteScrollPagination(data=workflow_runs, limit=limit, has_more=has_more)
  82. def get_workflow_run(self, app_model: App, run_id: str) -> Optional[WorkflowRun]:
  83. """
  84. Get workflow run detail
  85. :param app_model: app model
  86. :param run_id: workflow run id
  87. """
  88. workflow_run = (
  89. db.session.query(WorkflowRun)
  90. .filter(
  91. WorkflowRun.tenant_id == app_model.tenant_id,
  92. WorkflowRun.app_id == app_model.id,
  93. WorkflowRun.id == run_id,
  94. )
  95. .first()
  96. )
  97. return workflow_run
  98. def get_workflow_run_node_executions(
  99. self,
  100. app_model: App,
  101. run_id: str,
  102. user: Account | EndUser,
  103. ) -> Sequence[WorkflowNodeExecution]:
  104. """
  105. Get workflow run node execution list
  106. """
  107. workflow_run = self.get_workflow_run(app_model, run_id)
  108. contexts.plugin_tool_providers.set({})
  109. contexts.plugin_tool_providers_lock.set(threading.Lock())
  110. if not workflow_run:
  111. return []
  112. repository = SQLAlchemyWorkflowNodeExecutionRepository(
  113. session_factory=db.engine,
  114. user=user,
  115. app_id=app_model.id,
  116. triggered_from=None,
  117. )
  118. # Use the repository to get the node executions with ordering
  119. order_config = OrderConfig(order_by=["index"], order_direction="desc")
  120. node_executions = repository.get_db_models_by_workflow_run(workflow_run_id=run_id, order_config=order_config)
  121. return node_executions