Skip to content

Commit

Permalink
add conversation_id & message_id to advanced-chat workflow-runs API
Browse files Browse the repository at this point in the history
  • Loading branch information
takatost committed Mar 15, 2024
1 parent 50482b0 commit cfb80ca
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
26 changes: 26 additions & 0 deletions api/controllers/console/app/workflow_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -78,6 +103,7 @@ def get(self, app_model: App, run_id):
}


api.add_resource(AdvancedChatAppWorkflowRunListApi, '/apps/<uuid:app_id>/advanced-chat/workflow-runs')
api.add_resource(WorkflowRunListApi, '/apps/<uuid:app_id>/workflow-runs')
api.add_resource(WorkflowRunDetailApi, '/apps/<uuid:app_id>/workflow-runs/<uuid:run_id>')
api.add_resource(WorkflowRunNodeExecutionListApi, '/apps/<uuid:app_id>/workflow-runs/<uuid:run_id>/node-executions')
21 changes: 21 additions & 0 deletions api/fields/workflow_run_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
10 changes: 9 additions & 1 deletion api/models/workflow.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down
35 changes: 35 additions & 0 deletions api/services/workflow_run_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cfb80ca

Please sign in to comment.