diff --git a/api/controllers/console/app/workflow_run.py b/api/controllers/console/app/workflow_run.py index 8a4c0492a1551f..35d982e37ce4e3 100644 --- a/api/controllers/console/app/workflow_run.py +++ b/api/controllers/console/app/workflow_run.py @@ -6,6 +6,7 @@ from controllers.console.setup import setup_required from controllers.console.wraps import account_initialization_required from fields.workflow_run_fields import ( + advanced_chat_workflow_run_pagination_fields, workflow_run_detail_fields, workflow_run_node_execution_list_fields, workflow_run_pagination_fields, @@ -16,6 +17,30 @@ from services.workflow_run_service import WorkflowRunService +class AdvancedChatAppWorkflowRunListApi(Resource): + @setup_required + @login_required + @account_initialization_required + @get_app_model(mode=[AppMode.ADVANCED_CHAT]) + @marshal_with(advanced_chat_workflow_run_pagination_fields) + def get(self, app_model: App): + """ + Get advanced chat app workflow run list + """ + parser = reqparse.RequestParser() + parser.add_argument('last_id', type=uuid_value, location='args') + parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args') + args = parser.parse_args() + + workflow_run_service = WorkflowRunService() + result = workflow_run_service.get_paginate_advanced_chat_workflow_runs( + app_model=app_model, + args=args + ) + + return result + + class WorkflowRunListApi(Resource): @setup_required @login_required @@ -78,6 +103,7 @@ def get(self, app_model: App, run_id): } +api.add_resource(AdvancedChatAppWorkflowRunListApi, '/apps//advanced-chat/workflow-runs') api.add_resource(WorkflowRunListApi, '/apps//workflow-runs') api.add_resource(WorkflowRunDetailApi, '/apps//workflow-runs/') api.add_resource(WorkflowRunNodeExecutionListApi, '/apps//workflow-runs//node-executions') diff --git a/api/fields/workflow_run_fields.py b/api/fields/workflow_run_fields.py index 72510cd27ac621..902d0948c1ae13 100644 --- a/api/fields/workflow_run_fields.py +++ b/api/fields/workflow_run_fields.py @@ -29,6 +29,27 @@ "finished_at": TimestampField } +advanced_chat_workflow_run_for_list_fields = { + "id": fields.String, + "conversation_id": fields.String, + "message_id": fields.String, + "sequence_number": fields.Integer, + "version": fields.String, + "status": fields.String, + "elapsed_time": fields.Float, + "total_tokens": fields.Integer, + "total_steps": fields.Integer, + "created_by_account": fields.Nested(simple_account_fields, attribute='created_by_account', allow_null=True), + "created_at": TimestampField, + "finished_at": TimestampField +} + +advanced_chat_workflow_run_pagination_fields = { + 'limit': fields.Integer(attribute='limit'), + 'has_more': fields.Boolean(attribute='has_more'), + 'data': fields.List(fields.Nested(advanced_chat_workflow_run_for_list_fields), attribute='data') +} + workflow_run_pagination_fields = { 'limit': fields.Integer(attribute='limit'), 'has_more': fields.Boolean(attribute='has_more'), diff --git a/api/models/workflow.py b/api/models/workflow.py index 9c5b2a0b8f3135..dccb69498d2ae5 100644 --- a/api/models/workflow.py +++ b/api/models/workflow.py @@ -1,6 +1,6 @@ import json from enum import Enum -from typing import Union +from typing import Optional, Union from sqlalchemy.dialects.postgresql import UUID @@ -280,6 +280,14 @@ def inputs_dict(self): def outputs_dict(self): return json.loads(self.outputs) if self.outputs else None + @property + def message(self) -> Optional['Message']: + from models.model import Message + return db.session.query(Message).filter( + Message.app_id == self.app_id, + Message.workflow_run_id == self.id + ).first() + class WorkflowNodeExecutionTriggeredFrom(Enum): """ diff --git a/api/services/workflow_run_service.py b/api/services/workflow_run_service.py index 1d3f93f2247d19..ccce38ada05fcd 100644 --- a/api/services/workflow_run_service.py +++ b/api/services/workflow_run_service.py @@ -10,6 +10,41 @@ class WorkflowRunService: + def get_paginate_advanced_chat_workflow_runs(self, app_model: App, args: dict) -> InfiniteScrollPagination: + """ + Get advanced chat app workflow run list + Only return triggered_from == advanced_chat + + :param app_model: app model + :param args: request args + """ + class WorkflowWithMessage: + message_id: str + conversation_id: str + + def __init__(self, workflow_run: WorkflowRun): + self._workflow_run = workflow_run + + def __getattr__(self, item): + return getattr(self._workflow_run, item) + + pagination = self.get_paginate_workflow_runs(app_model, args) + + with_message_workflow_runs = [] + for workflow_run in pagination.data: + message = workflow_run.message + with_message_workflow_run = WorkflowWithMessage( + workflow_run=workflow_run + ) + if message: + with_message_workflow_run.message_id = message.id + with_message_workflow_run.conversation_id = message.conversation_id + + with_message_workflow_runs.append(with_message_workflow_run) + + pagination.data = with_message_workflow_runs + return pagination + def get_paginate_workflow_runs(self, app_model: App, args: dict) -> InfiniteScrollPagination: """ Get debug workflow run list