Underground-Digital/Workflow-Engine
0
1from flask_login import current_user2from flask_restful import marshal_with, reqparse3from flask_restful.inputs import int_range4from werkzeug.exceptions import NotFound5 6from controllers.console import api7from controllers.console.explore.error import NotChatAppError8from controllers.console.explore.wraps import InstalledAppResource9from 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 AppMode13from services.conversation_service import ConversationService14from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError15from services.web_conversation_service import WebConversationService16 17 18class ConversationListApi(InstalledAppResource):19 @marshal_with(conversation_infinite_scroll_pagination_fields)20 def get(self, installed_app):21 app_model = installed_app.app22 app_mode = AppMode.value_of(app_model.mode)23 if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:24 raise NotChatAppError()25 26 parser = reqparse.RequestParser()27 parser.add_argument("last_id", type=uuid_value, location="args")28 parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")29 parser.add_argument("pinned", type=str, choices=["true", "false", None], location="args")30 args = parser.parse_args()31 32 pinned = None33 if "pinned" in args and args["pinned"] is not None:34 pinned = True if args["pinned"] == "true" else False35 36 try:37 return WebConversationService.pagination_by_last_id(38 app_model=app_model,39 user=current_user,40 last_id=args["last_id"],41 limit=args["limit"],42 invoke_from=InvokeFrom.EXPLORE,43 pinned=pinned,44 )45 except LastConversationNotExistsError:46 raise NotFound("Last Conversation Not Exists.")47 48 49class ConversationApi(InstalledAppResource):50 def delete(self, installed_app, c_id):51 app_model = installed_app.app52 app_mode = AppMode.value_of(app_model.mode)53 if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:54 raise NotChatAppError()55 56 conversation_id = str(c_id)57 try:58 ConversationService.delete(app_model, conversation_id, current_user)59 except ConversationNotExistsError:60 raise NotFound("Conversation Not Exists.")61 WebConversationService.unpin(app_model, conversation_id, current_user)62 63 return {"result": "success"}, 20464 65 66class ConversationRenameApi(InstalledAppResource):67 @marshal_with(simple_conversation_fields)68 def post(self, installed_app, c_id):69 app_model = installed_app.app70 app_mode = AppMode.value_of(app_model.mode)71 if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:72 raise NotChatAppError()73 74 conversation_id = str(c_id)75 76 parser = reqparse.RequestParser()77 parser.add_argument("name", type=str, required=False, location="json")78 parser.add_argument("auto_generate", type=bool, required=False, default=False, location="json")79 args = parser.parse_args()80 81 try:82 return ConversationService.rename(83 app_model, conversation_id, current_user, args["name"], args["auto_generate"]84 )85 except ConversationNotExistsError:86 raise NotFound("Conversation Not Exists.")87 88 89class ConversationPinApi(InstalledAppResource):90 def patch(self, installed_app, c_id):91 app_model = installed_app.app92 app_mode = AppMode.value_of(app_model.mode)93 if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:94 raise NotChatAppError()95 96 conversation_id = str(c_id)97 98 try:99 WebConversationService.pin(app_model, conversation_id, current_user)100 except ConversationNotExistsError:101 raise NotFound("Conversation Not Exists.")102 103 return {"result": "success"}104 105 106class ConversationUnPinApi(InstalledAppResource):107 def patch(self, installed_app, c_id):108 app_model = installed_app.app109 app_mode = AppMode.value_of(app_model.mode)110 if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:111 raise NotChatAppError()112 113 conversation_id = str(c_id)114 WebConversationService.unpin(app_model, conversation_id, current_user)115 116 return {"result": "success"}117 118 119api.add_resource(120 ConversationRenameApi,121 "/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/name",122 endpoint="installed_app_conversation_rename",123)124api.add_resource(125 ConversationListApi, "/installed-apps/<uuid:installed_app_id>/conversations", endpoint="installed_app_conversations"126)127api.add_resource(128 ConversationApi,129 "/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>",130 endpoint="installed_app_conversation",131)132api.add_resource(133 ConversationPinApi,134 "/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/pin",135 endpoint="installed_app_conversation_pin",136)137api.add_resource(138 ConversationUnPinApi,139 "/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/unpin",140 endpoint="installed_app_conversation_unpin",141)142 