conversation.py 5.3 KB

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