conversation.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding:utf-8 -*-
  2. from flask_login import current_user
  3. from flask_restful import fields, reqparse, marshal_with
  4. from flask_restful.inputs import int_range
  5. from werkzeug.exceptions import NotFound
  6. from controllers.console import api
  7. from controllers.console.universal_chat.wraps import UniversalChatResource
  8. from fields.conversation_fields import conversation_with_model_config_infinite_scroll_pagination_fields, \
  9. conversation_with_model_config_fields
  10. from libs.helper import TimestampField, uuid_value
  11. from services.conversation_service import ConversationService
  12. from services.errors.conversation import LastConversationNotExistsError, ConversationNotExistsError
  13. from services.web_conversation_service import WebConversationService
  14. class UniversalChatConversationListApi(UniversalChatResource):
  15. @marshal_with(conversation_with_model_config_infinite_scroll_pagination_fields)
  16. def get(self, universal_app):
  17. app_model = universal_app
  18. parser = reqparse.RequestParser()
  19. parser.add_argument('last_id', type=uuid_value, location='args')
  20. parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
  21. parser.add_argument('pinned', type=str, choices=['true', 'false', None], location='args')
  22. args = parser.parse_args()
  23. pinned = None
  24. if 'pinned' in args and args['pinned'] is not None:
  25. pinned = True if args['pinned'] == 'true' else False
  26. try:
  27. return WebConversationService.pagination_by_last_id(
  28. app_model=app_model,
  29. user=current_user,
  30. last_id=args['last_id'],
  31. limit=args['limit'],
  32. pinned=pinned
  33. )
  34. except LastConversationNotExistsError:
  35. raise NotFound("Last Conversation Not Exists.")
  36. class UniversalChatConversationApi(UniversalChatResource):
  37. def delete(self, universal_app, c_id):
  38. app_model = universal_app
  39. conversation_id = str(c_id)
  40. try:
  41. ConversationService.delete(app_model, conversation_id, current_user)
  42. except ConversationNotExistsError:
  43. raise NotFound("Conversation Not Exists.")
  44. WebConversationService.unpin(app_model, conversation_id, current_user)
  45. return {"result": "success"}, 204
  46. class UniversalChatConversationRenameApi(UniversalChatResource):
  47. @marshal_with(conversation_with_model_config_fields)
  48. def post(self, universal_app, c_id):
  49. app_model = universal_app
  50. conversation_id = str(c_id)
  51. parser = reqparse.RequestParser()
  52. parser.add_argument('name', type=str, required=False, location='json')
  53. parser.add_argument('auto_generate', type=bool, required=False, default='False', location='json')
  54. args = parser.parse_args()
  55. try:
  56. return ConversationService.rename(
  57. app_model,
  58. conversation_id,
  59. current_user,
  60. args['name'],
  61. args['auto_generate']
  62. )
  63. except ConversationNotExistsError:
  64. raise NotFound("Conversation Not Exists.")
  65. class UniversalChatConversationPinApi(UniversalChatResource):
  66. def patch(self, universal_app, c_id):
  67. app_model = universal_app
  68. conversation_id = str(c_id)
  69. try:
  70. WebConversationService.pin(app_model, conversation_id, current_user)
  71. except ConversationNotExistsError:
  72. raise NotFound("Conversation Not Exists.")
  73. return {"result": "success"}
  74. class UniversalChatConversationUnPinApi(UniversalChatResource):
  75. def patch(self, universal_app, c_id):
  76. app_model = universal_app
  77. conversation_id = str(c_id)
  78. WebConversationService.unpin(app_model, conversation_id, current_user)
  79. return {"result": "success"}
  80. api.add_resource(UniversalChatConversationRenameApi, '/universal-chat/conversations/<uuid:c_id>/name')
  81. api.add_resource(UniversalChatConversationListApi, '/universal-chat/conversations')
  82. api.add_resource(UniversalChatConversationApi, '/universal-chat/conversations/<uuid:c_id>')
  83. api.add_resource(UniversalChatConversationPinApi, '/universal-chat/conversations/<uuid:c_id>/pin')
  84. api.add_resource(UniversalChatConversationUnPinApi, '/universal-chat/conversations/<uuid:c_id>/unpin')