Underground-Digital/Workflow-Engine
0
1from flask_restful import Resource, marshal_with, reqparse2from flask_restful.inputs import int_range3from werkzeug.exceptions import NotFound4 5import services6from controllers.service_api import api7from controllers.service_api.app.error import NotChatAppError8from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token9from core.app.entities.app_invoke_entities import InvokeFrom10from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields11from libs.helper import uuid_value12from models.model import App, AppMode, EndUser13from services.conversation_service import ConversationService14 15 16class ConversationApi(Resource):17 @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))18 @marshal_with(conversation_infinite_scroll_pagination_fields)19 def get(self, app_model: App, end_user: EndUser):20 app_mode = AppMode.value_of(app_model.mode)21 if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:22 raise NotChatAppError()23 24 parser = reqparse.RequestParser()25 parser.add_argument("last_id", type=uuid_value, location="args")26 parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")27 parser.add_argument(28 "sort_by",29 type=str,30 choices=["created_at", "-created_at", "updated_at", "-updated_at"],31 required=False,32 default="-updated_at",33 location="args",34 )35 args = parser.parse_args()36 37 try:38 return ConversationService.pagination_by_last_id(39 app_model=app_model,40 user=end_user,41 last_id=args["last_id"],42 limit=args["limit"],43 invoke_from=InvokeFrom.SERVICE_API,44 sort_by=args["sort_by"],45 )46 except services.errors.conversation.LastConversationNotExistsError:47 raise NotFound("Last Conversation Not Exists.")48 49 50class ConversationDetailApi(Resource):51 @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))52 @marshal_with(simple_conversation_fields)53 def delete(self, app_model: App, end_user: EndUser, c_id):54 app_mode = AppMode.value_of(app_model.mode)55 if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:56 raise NotChatAppError()57 58 conversation_id = str(c_id)59 60 try:61 ConversationService.delete(app_model, conversation_id, end_user)62 except services.errors.conversation.ConversationNotExistsError:63 raise NotFound("Conversation Not Exists.")64 return {"result": "success"}, 20065 66 67class ConversationRenameApi(Resource):68 @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))69 @marshal_with(simple_conversation_fields)70 def post(self, app_model: App, end_user: EndUser, c_id):71 app_mode = AppMode.value_of(app_model.mode)72 if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:73 raise NotChatAppError()74 75 conversation_id = str(c_id)76 77 parser = reqparse.RequestParser()78 parser.add_argument("name", type=str, required=False, location="json")79 parser.add_argument("auto_generate", type=bool, required=False, default=False, location="json")80 args = parser.parse_args()81 82 try:83 return ConversationService.rename(app_model, conversation_id, end_user, args["name"], args["auto_generate"])84 except services.errors.conversation.ConversationNotExistsError:85 raise NotFound("Conversation Not Exists.")86 87 88api.add_resource(ConversationRenameApi, "/conversations/<uuid:c_id>/name", endpoint="conversation_name")89api.add_resource(ConversationApi, "/conversations")90api.add_resource(ConversationDetailApi, "/conversations/<uuid:c_id>", endpoint="conversation_detail")91 