| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import logging | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from flask_login import current_user | 
					
						
							|  |  |  | from flask_restful import Resource, fields, marshal_with, reqparse | 
					
						
							|  |  |  | from flask_restful.inputs import int_range | 
					
						
							|  |  |  | from werkzeug.exceptions import Forbidden, InternalServerError, NotFound | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.console import api | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from controllers.console.app.error import ( | 
					
						
							|  |  |  |     CompletionRequestError, | 
					
						
							|  |  |  |     ProviderModelCurrentlyNotSupportError, | 
					
						
							|  |  |  |     ProviderNotInitializeError, | 
					
						
							|  |  |  |     ProviderQuotaExceededError, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from controllers.console.app.wraps import get_app_model | 
					
						
							|  |  |  | from controllers.console.explore.error import AppSuggestedQuestionsAfterAnswerDisabledError | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.console.setup import setup_required | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  | from controllers.console.wraps import account_initialization_required, cloud_edition_billing_resource_check | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from core.app.entities.app_invoke_entities import InvokeFrom | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | from core.model_runtime.errors.invoke import InvokeError | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from extensions.ext_database import db | 
					
						
							|  |  |  | from fields.conversation_fields import annotation_fields, message_detail_fields | 
					
						
							| 
									
										
										
										
											2023-10-08 05:21:32 -05:00
										 |  |  | from libs.helper import uuid_value | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from libs.infinite_scroll_pagination import InfiniteScrollPagination | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from libs.login import login_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from models.model import AppMode, Conversation, Message, MessageAnnotation, MessageFeedback | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  | from services.annotation_service import AppAnnotationService | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from services.errors.conversation import ConversationNotExistsError | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from services.errors.message import MessageNotExistsError, SuggestedQuestionsAfterAnswerDisabledError | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from services.message_service import MessageService | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 16:51:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ChatMessageListApi(Resource): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     message_infinite_scroll_pagination_fields = { | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         "limit": fields.Integer, | 
					
						
							|  |  |  |         "has_more": fields.Boolean, | 
					
						
							|  |  |  |         "data": fields.List(fields.Nested(message_detail_fields)), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model(mode=[AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(message_infinite_scroll_pagination_fields) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("conversation_id", required=True, type=uuid_value, location="args") | 
					
						
							|  |  |  |         parser.add_argument("first_id", type=uuid_value, location="args") | 
					
						
							|  |  |  |         parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         conversation = ( | 
					
						
							|  |  |  |             db.session.query(Conversation) | 
					
						
							|  |  |  |             .filter(Conversation.id == args["conversation_id"], Conversation.app_id == app_model.id) | 
					
						
							|  |  |  |             .first() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not conversation: | 
					
						
							|  |  |  |             raise NotFound("Conversation Not Exists.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["first_id"]: | 
					
						
							|  |  |  |             first_message = ( | 
					
						
							|  |  |  |                 db.session.query(Message) | 
					
						
							|  |  |  |                 .filter(Message.conversation_id == conversation.id, Message.id == args["first_id"]) | 
					
						
							|  |  |  |                 .first() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if not first_message: | 
					
						
							|  |  |  |                 raise NotFound("First message not found") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             history_messages = ( | 
					
						
							|  |  |  |                 db.session.query(Message) | 
					
						
							|  |  |  |                 .filter( | 
					
						
							|  |  |  |                     Message.conversation_id == conversation.id, | 
					
						
							|  |  |  |                     Message.created_at < first_message.created_at, | 
					
						
							|  |  |  |                     Message.id != first_message.id, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 .order_by(Message.created_at.desc()) | 
					
						
							|  |  |  |                 .limit(args["limit"]) | 
					
						
							|  |  |  |                 .all() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             history_messages = ( | 
					
						
							|  |  |  |                 db.session.query(Message) | 
					
						
							|  |  |  |                 .filter(Message.conversation_id == conversation.id) | 
					
						
							|  |  |  |                 .order_by(Message.created_at.desc()) | 
					
						
							|  |  |  |                 .limit(args["limit"]) | 
					
						
							|  |  |  |                 .all() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         has_more = False | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if len(history_messages) == args["limit"]: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             current_page_first_message = history_messages[-1] | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             rest_count = ( | 
					
						
							|  |  |  |                 db.session.query(Message) | 
					
						
							|  |  |  |                 .filter( | 
					
						
							|  |  |  |                     Message.conversation_id == conversation.id, | 
					
						
							|  |  |  |                     Message.created_at < current_page_first_message.created_at, | 
					
						
							|  |  |  |                     Message.id != current_page_first_message.id, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 .count() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if rest_count > 0: | 
					
						
							|  |  |  |                 has_more = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-24 12:09:46 +08:00
										 |  |  |         history_messages = list(reversed(history_messages)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return InfiniteScrollPagination(data=history_messages, limit=args["limit"], has_more=has_more) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MessageFeedbackApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							|  |  |  |     def post(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("message_id", required=True, type=uuid_value, location="json") | 
					
						
							|  |  |  |         parser.add_argument("rating", type=str, choices=["like", "dislike", None], location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         message_id = str(args["message_id"]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         message = db.session.query(Message).filter(Message.id == message_id, Message.app_id == app_model.id).first() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not message: | 
					
						
							|  |  |  |             raise NotFound("Message Not Exists.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         feedback = message.admin_feedback | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if not args["rating"] and feedback: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             db.session.delete(feedback) | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         elif args["rating"] and feedback: | 
					
						
							|  |  |  |             feedback.rating = args["rating"] | 
					
						
							|  |  |  |         elif not args["rating"] and not feedback: | 
					
						
							|  |  |  |             raise ValueError("rating cannot be None when feedback not exists") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             feedback = MessageFeedback( | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                 app_id=app_model.id, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |                 conversation_id=message.conversation_id, | 
					
						
							|  |  |  |                 message_id=message.id, | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 rating=args["rating"], | 
					
						
							|  |  |  |                 from_source="admin", | 
					
						
							|  |  |  |                 from_account_id=current_user.id, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |             db.session.add(feedback) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         db.session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"result": "success"} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MessageAnnotationApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |     @cloud_edition_billing_resource_check("annotation") | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     @marshal_with(annotation_fields) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     def post(self, app_model): | 
					
						
							| 
									
										
										
										
											2024-08-05 04:55:55 +00:00
										 |  |  |         if not current_user.is_editor: | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |             raise Forbidden() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("message_id", required=False, type=uuid_value, location="json") | 
					
						
							|  |  |  |         parser.add_argument("question", required=True, type=str, location="json") | 
					
						
							|  |  |  |         parser.add_argument("answer", required=True, type=str, location="json") | 
					
						
							|  |  |  |         parser.add_argument("annotation_reply", required=False, type=dict, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         annotation = AppAnnotationService.up_insert_app_annotation_from_message(args, app_model.id) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |         return annotation | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MessageAnnotationCountApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         count = db.session.query(MessageAnnotation).filter(MessageAnnotation.app_id == app_model.id).count() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"count": count} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MessageSuggestedQuestionApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model(mode=[AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]) | 
					
						
							|  |  |  |     def get(self, app_model, message_id): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         message_id = str(message_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             questions = MessageService.get_suggested_questions_after_answer( | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 app_model=app_model, message_id=message_id, user=current_user, invoke_from=InvokeFrom.DEBUGGER | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |         except MessageNotExistsError: | 
					
						
							|  |  |  |             raise NotFound("Message not found") | 
					
						
							|  |  |  |         except ConversationNotExistsError: | 
					
						
							|  |  |  |             raise NotFound("Conversation not found") | 
					
						
							| 
									
										
										
										
											2023-07-17 00:14:19 +08:00
										 |  |  |         except ProviderTokenNotInitError as ex: | 
					
						
							|  |  |  |             raise ProviderNotInitializeError(ex.description) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         except QuotaExceededError: | 
					
						
							|  |  |  |             raise ProviderQuotaExceededError() | 
					
						
							|  |  |  |         except ModelCurrentlyNotSupportError: | 
					
						
							|  |  |  |             raise ProviderModelCurrentlyNotSupportError() | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         except InvokeError as e: | 
					
						
							| 
									
										
										
										
											2024-01-04 17:49:55 +08:00
										 |  |  |             raise CompletionRequestError(e.description) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         except SuggestedQuestionsAfterAnswerDisabledError: | 
					
						
							|  |  |  |             raise AppSuggestedQuestionsAfterAnswerDisabledError() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         except Exception: | 
					
						
							|  |  |  |             logging.exception("internal server error.") | 
					
						
							|  |  |  |             raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"data": questions} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 16:51:39 +08:00
										 |  |  | class MessageApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							| 
									
										
										
										
											2023-05-16 16:51:39 +08:00
										 |  |  |     @marshal_with(message_detail_fields) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     def get(self, app_model, message_id): | 
					
						
							| 
									
										
										
										
											2023-05-16 16:51:39 +08:00
										 |  |  |         message_id = str(message_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         message = db.session.query(Message).filter(Message.id == message_id, Message.app_id == app_model.id).first() | 
					
						
							| 
									
										
										
										
											2023-05-16 16:51:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not message: | 
					
						
							|  |  |  |             raise NotFound("Message Not Exists.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return message | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(MessageSuggestedQuestionApi, "/apps/<uuid:app_id>/chat-messages/<uuid:message_id>/suggested-questions") | 
					
						
							|  |  |  | api.add_resource(ChatMessageListApi, "/apps/<uuid:app_id>/chat-messages", endpoint="console_chat_messages") | 
					
						
							|  |  |  | api.add_resource(MessageFeedbackApi, "/apps/<uuid:app_id>/feedbacks") | 
					
						
							|  |  |  | api.add_resource(MessageAnnotationApi, "/apps/<uuid:app_id>/annotations") | 
					
						
							|  |  |  | api.add_resource(MessageAnnotationCountApi, "/apps/<uuid:app_id>/annotations/count") | 
					
						
							|  |  |  | api.add_resource(MessageApi, "/apps/<uuid:app_id>/messages/<uuid:message_id>", endpoint="console_message") |