conversation.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from flask_login import current_user
  2. from flask_restful import marshal_with, reqparse
  3. from flask_restful.inputs import int_range
  4. from werkzeug.exceptions import NotFound
  5. from controllers.console import api
  6. from controllers.console.explore.error import NotChatAppError
  7. from controllers.console.explore.wraps import InstalledAppResource
  8. from core.app.entities.app_invoke_entities import InvokeFrom
  9. from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields
  10. from libs.helper import uuid_value
  11. from models.model import AppMode
  12. from services.conversation_service import ConversationService
  13. from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError
  14. from services.web_conversation_service import WebConversationService
  15. class ConversationListApi(InstalledAppResource):
  16. @marshal_with(conversation_infinite_scroll_pagination_fields)
  17. def get(self, installed_app):
  18. app_model = installed_app.app
  19. app_mode = AppMode.value_of(app_model.mode)
  20. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  21. raise NotChatAppError()
  22. parser = reqparse.RequestParser()
  23. parser.add_argument('last_id', type=uuid_value, location='args')
  24. parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
  25. parser.add_argument('pinned', type=str, choices=['true', 'false', None], location='args')
  26. args = parser.parse_args()
  27. pinned = None
  28. if 'pinned' in args and args['pinned'] is not None:
  29. pinned = True if args['pinned'] == 'true' else False
  30. try:
  31. return WebConversationService.pagination_by_last_id(
  32. app_model=app_model,
  33. user=current_user,
  34. last_id=args['last_id'],
  35. limit=args['limit'],
  36. invoke_from=InvokeFrom.EXPLORE,
  37. pinned=pinned,
  38. )
  39. except LastConversationNotExistsError:
  40. raise NotFound("Last Conversation Not Exists.")
  41. class ConversationApi(InstalledAppResource):
  42. def delete(self, installed_app, c_id):
  43. app_model = installed_app.app
  44. app_mode = AppMode.value_of(app_model.mode)
  45. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  46. raise NotChatAppError()
  47. conversation_id = str(c_id)
  48. try:
  49. ConversationService.delete(app_model, conversation_id, current_user)
  50. except ConversationNotExistsError:
  51. raise NotFound("Conversation Not Exists.")
  52. WebConversationService.unpin(app_model, conversation_id, current_user)
  53. return {"result": "success"}, 204
  54. class ConversationRenameApi(InstalledAppResource):
  55. @marshal_with(simple_conversation_fields)
  56. def post(self, installed_app, c_id):
  57. app_model = installed_app.app
  58. app_mode = AppMode.value_of(app_model.mode)
  59. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  60. raise NotChatAppError()
  61. conversation_id = str(c_id)
  62. parser = reqparse.RequestParser()
  63. parser.add_argument('name', type=str, required=False, location='json')
  64. parser.add_argument('auto_generate', type=bool, required=False, default=False, location='json')
  65. args = parser.parse_args()
  66. try:
  67. return ConversationService.rename(
  68. app_model,
  69. conversation_id,
  70. current_user,
  71. args['name'],
  72. args['auto_generate']
  73. )
  74. except ConversationNotExistsError:
  75. raise NotFound("Conversation Not Exists.")
  76. class ConversationPinApi(InstalledAppResource):
  77. def patch(self, installed_app, c_id):
  78. app_model = installed_app.app
  79. app_mode = AppMode.value_of(app_model.mode)
  80. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  81. raise NotChatAppError()
  82. conversation_id = str(c_id)
  83. try:
  84. WebConversationService.pin(app_model, conversation_id, current_user)
  85. except ConversationNotExistsError:
  86. raise NotFound("Conversation Not Exists.")
  87. return {"result": "success"}
  88. class ConversationUnPinApi(InstalledAppResource):
  89. def patch(self, installed_app, c_id):
  90. app_model = installed_app.app
  91. app_mode = AppMode.value_of(app_model.mode)
  92. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  93. raise NotChatAppError()
  94. conversation_id = str(c_id)
  95. WebConversationService.unpin(app_model, conversation_id, current_user)
  96. return {"result": "success"}
  97. api.add_resource(ConversationRenameApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/name', endpoint='installed_app_conversation_rename')
  98. api.add_resource(ConversationListApi, '/installed-apps/<uuid:installed_app_id>/conversations', endpoint='installed_app_conversations')
  99. api.add_resource(ConversationApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>', endpoint='installed_app_conversation')
  100. api.add_resource(ConversationPinApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/pin', endpoint='installed_app_conversation_pin')
  101. api.add_resource(ConversationUnPinApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/unpin', endpoint='installed_app_conversation_unpin')