diff --git a/api/controllers/service_api/app/conversation.py b/api/controllers/service_api/app/conversation.py index 432d8a44a9..3c897df7c1 100644 --- a/api/controllers/service_api/app/conversation.py +++ b/api/controllers/service_api/app/conversation.py @@ -109,12 +109,13 @@ class ConversationVariablesApi(Resource): conversation_id = str(c_id) 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() try: return ConversationService.get_conversational_variable( - app_model, conversation_id, end_user, args["limit"] + app_model, conversation_id, end_user, args["limit"], args["last_id"] ) except services.errors.conversation.ConversationNotExistsError: raise NotFound("Conversation Not Exists.") diff --git a/api/services/conversation_service.py b/api/services/conversation_service.py index 24e2b43d7a..1cfecd1973 100644 --- a/api/services/conversation_service.py +++ b/api/services/conversation_service.py @@ -12,7 +12,11 @@ from libs.infinite_scroll_pagination import InfiniteScrollPagination from models import ConversationVariable from models.account import Account from models.model import App, Conversation, EndUser, Message -from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError +from services.errors.conversation import ( + ConversationNotExistsError, + LastConversationNotExistsError, + ConversationVariableNotExistsError, +) from services.errors.message import MessageNotExistsError @@ -175,6 +179,7 @@ class ConversationService: conversation_id: str, user: Optional[Union[Account, EndUser]], limit: int, + last_id: Optional[str], ) -> InfiniteScrollPagination: conversation = cls.get_conversation(app_model, conversation_id, user) @@ -185,13 +190,23 @@ class ConversationService: .order_by(ConversationVariable.created_at) ) - 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() + if last_id: + last_variable = session.scalar(stmt.where(ConversationVariable.id == last_id)) + if not last_variable: + raise ConversationVariableNotExistsError() + + # Filter for variables created after the last_id + stmt = stmt.where(ConversationVariable.created_at > last_variable.created_at) + + # Apply limit to query + query_stmt = stmt.limit(limit) # Get one extra to check if there are more + rows = session.scalars(query_stmt).all() + + has_more = False + if len(rows) > limit: + has_more = True + rows = rows[:limit] # Remove the extra item variables = [ { diff --git a/api/services/errors/conversation.py b/api/services/errors/conversation.py index 139dd9a70a..f8051e3417 100644 --- a/api/services/errors/conversation.py +++ b/api/services/errors/conversation.py @@ -11,3 +11,7 @@ class ConversationNotExistsError(BaseServiceError): class ConversationCompletedError(Exception): pass + + +class ConversationVariableNotExistsError(BaseServiceError): + pass