workflow_run_service.py 4.8 KB

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