workflow_run_service.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from extensions.ext_database import db
  2. from libs.infinite_scroll_pagination import InfiniteScrollPagination
  3. from models.model import App
  4. from models.workflow import (
  5. WorkflowNodeExecution,
  6. WorkflowNodeExecutionTriggeredFrom,
  7. WorkflowRun,
  8. WorkflowRunTriggeredFrom,
  9. )
  10. class WorkflowRunService:
  11. def get_paginate_advanced_chat_workflow_runs(self, app_model: App, args: dict) -> InfiniteScrollPagination:
  12. """
  13. Get advanced chat app workflow run list
  14. Only return triggered_from == advanced_chat
  15. :param app_model: app model
  16. :param args: request args
  17. """
  18. class WorkflowWithMessage:
  19. message_id: str
  20. conversation_id: str
  21. def __init__(self, workflow_run: WorkflowRun):
  22. self._workflow_run = workflow_run
  23. def __getattr__(self, item):
  24. return getattr(self._workflow_run, item)
  25. pagination = self.get_paginate_workflow_runs(app_model, args)
  26. with_message_workflow_runs = []
  27. for workflow_run in pagination.data:
  28. message = workflow_run.message
  29. with_message_workflow_run = WorkflowWithMessage(
  30. workflow_run=workflow_run
  31. )
  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 = base_query.filter(
  58. WorkflowRun.created_at < last_workflow_run.created_at,
  59. WorkflowRun.id != last_workflow_run.id
  60. ).order_by(WorkflowRun.created_at.desc()).limit(limit).all()
  61. else:
  62. workflow_runs = base_query.order_by(WorkflowRun.created_at.desc()).limit(limit).all()
  63. has_more = False
  64. if len(workflow_runs) == limit:
  65. current_page_first_workflow_run = workflow_runs[-1]
  66. rest_count = base_query.filter(
  67. WorkflowRun.created_at < current_page_first_workflow_run.created_at,
  68. WorkflowRun.id != current_page_first_workflow_run.id
  69. ).count()
  70. if rest_count > 0:
  71. has_more = True
  72. return InfiniteScrollPagination(
  73. data=workflow_runs,
  74. limit=limit,
  75. has_more=has_more
  76. )
  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 = db.session.query(WorkflowRun).filter(
  84. WorkflowRun.tenant_id == app_model.tenant_id,
  85. WorkflowRun.app_id == app_model.id,
  86. WorkflowRun.id == run_id,
  87. ).first()
  88. return workflow_run
  89. def get_workflow_run_node_executions(self, app_model: App, run_id: str) -> list[WorkflowNodeExecution]:
  90. """
  91. Get workflow run node execution list
  92. """
  93. workflow_run = self.get_workflow_run(app_model, run_id)
  94. if not workflow_run:
  95. return []
  96. node_executions = db.session.query(WorkflowNodeExecution).filter(
  97. WorkflowNodeExecution.tenant_id == app_model.tenant_id,
  98. WorkflowNodeExecution.app_id == app_model.id,
  99. WorkflowNodeExecution.workflow_id == workflow_run.workflow_id,
  100. WorkflowNodeExecution.triggered_from == WorkflowNodeExecutionTriggeredFrom.WORKFLOW_RUN.value,
  101. WorkflowNodeExecution.workflow_run_id == run_id,
  102. ).order_by(WorkflowNodeExecution.index.desc()).all()
  103. return node_executions