From 7471802eb74ab822c8f6795be1208d679bcdad3f Mon Sep 17 00:00:00 2001 From: AlexChimTrm <132866042+AlexChim1231@users.noreply.github.com> Date: Wed, 23 Apr 2025 17:08:55 +0800 Subject: [PATCH] complete --- .../service_api/app/conversation.py | 12 +++++-- api/fields/conversation_variable_fields.py | 6 ++++ api/services/conversation_service.py | 34 +++++++++---------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/api/controllers/service_api/app/conversation.py b/api/controllers/service_api/app/conversation.py index 9d9ed46686..432d8a44a9 100644 --- a/api/controllers/service_api/app/conversation.py +++ b/api/controllers/service_api/app/conversation.py @@ -15,7 +15,7 @@ from fields.conversation_fields import ( simple_conversation_fields, ) from fields.conversation_variable_fields import ( - paginated_conversation_variable_fields, + conversation_variable_infinite_scroll_pagination_fields, ) from libs.helper import uuid_value from models.model import App, AppMode, EndUser @@ -98,7 +98,7 @@ class ConversationRenameApi(Resource): class ConversationVariablesApi(Resource): @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY)) - @marshal_with(paginated_conversation_variable_fields) + @marshal_with(conversation_variable_infinite_scroll_pagination_fields) def get(self, app_model: App, end_user: EndUser, c_id): # conversational variable only for chat app @@ -108,8 +108,14 @@ class ConversationVariablesApi(Resource): conversation_id = str(c_id) + parser = reqparse.RequestParser() + parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args") + args = parser.parse_args() + try: - return ConversationService.get_conversational_variable(app_model, conversation_id, end_user) + return ConversationService.get_conversational_variable( + app_model, conversation_id, end_user, args["limit"] + ) except services.errors.conversation.ConversationNotExistsError: raise NotFound("Conversation Not Exists.") diff --git a/api/fields/conversation_variable_fields.py b/api/fields/conversation_variable_fields.py index c6385efb5a..3aa3838def 100644 --- a/api/fields/conversation_variable_fields.py +++ b/api/fields/conversation_variable_fields.py @@ -19,3 +19,9 @@ paginated_conversation_variable_fields = { "has_more": fields.Boolean, "data": fields.List(fields.Nested(conversation_variable_fields), attribute="data"), } + +conversation_variable_infinite_scroll_pagination_fields = { + "limit": fields.Integer, + "has_more": fields.Boolean, + "data": fields.List(fields.Nested(conversation_variable_fields)), +} diff --git a/api/services/conversation_service.py b/api/services/conversation_service.py index 19c1415374..24e2b43d7a 100644 --- a/api/services/conversation_service.py +++ b/api/services/conversation_service.py @@ -173,7 +173,8 @@ class ConversationService: cls, app_model: App, conversation_id: str, - user: Optional[Union[Account, EndUser]] + user: Optional[Union[Account, EndUser]], + limit: int, ) -> InfiniteScrollPagination: conversation = cls.get_conversation(app_model, conversation_id, user) @@ -184,24 +185,21 @@ class ConversationService: .order_by(ConversationVariable.created_at) ) - page = 1 - page_size = 100 - stmt = stmt.limit(page_size).offset((page - 1) * page_size) + total_count = db.session.scalar(select(func.count()).select_from(stmt)) + has_more = total_count > limit + + stmt = stmt.limit(limit) with Session(db.engine) as session: rows = session.scalars(stmt).all() - return { - "page": page, - "limit": page_size, - "total": len(rows), - "has_more": False, - "data": [ - { - "created_at": row.created_at, - "updated_at": row.updated_at, - **row.to_variable().model_dump(), - } - for row in rows - ], - } + variables = [ + { + "created_at": row.created_at, + "updated_at": row.updated_at, + **row.to_variable().model_dump(), + } + for row in rows + ] + + return InfiniteScrollPagination(variables, limit, has_more)