conversation.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding:utf-8 -*-
  2. from flask_restful import fields, reqparse, marshal_with
  3. from flask_restful.inputs import int_range
  4. from werkzeug.exceptions import NotFound
  5. from controllers.web import api
  6. from controllers.web.error import NotChatAppError
  7. from controllers.web.wraps import WebApiResource
  8. from libs.helper import TimestampField, uuid_value
  9. from services.conversation_service import ConversationService
  10. from services.errors.conversation import LastConversationNotExistsError, ConversationNotExistsError
  11. from services.web_conversation_service import WebConversationService
  12. conversation_fields = {
  13. 'id': fields.String,
  14. 'name': fields.String,
  15. 'inputs': fields.Raw,
  16. 'status': fields.String,
  17. 'introduction': fields.String,
  18. 'created_at': TimestampField
  19. }
  20. conversation_infinite_scroll_pagination_fields = {
  21. 'limit': fields.Integer,
  22. 'has_more': fields.Boolean,
  23. 'data': fields.List(fields.Nested(conversation_fields))
  24. }
  25. class ConversationListApi(WebApiResource):
  26. @marshal_with(conversation_infinite_scroll_pagination_fields)
  27. def get(self, app_model, end_user):
  28. if app_model.mode != 'chat':
  29. raise NotChatAppError()
  30. parser = reqparse.RequestParser()
  31. parser.add_argument('last_id', type=uuid_value, location='args')
  32. parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
  33. parser.add_argument('pinned', type=str, choices=['true', 'false', None], location='args')
  34. args = parser.parse_args()
  35. pinned = None
  36. if 'pinned' in args and args['pinned'] is not None:
  37. pinned = True if args['pinned'] == 'true' else False
  38. try:
  39. return WebConversationService.pagination_by_last_id(
  40. app_model=app_model,
  41. user=end_user,
  42. last_id=args['last_id'],
  43. limit=args['limit'],
  44. pinned=pinned
  45. )
  46. except LastConversationNotExistsError:
  47. raise NotFound("Last Conversation Not Exists.")
  48. class ConversationApi(WebApiResource):
  49. def delete(self, app_model, end_user, c_id):
  50. if app_model.mode != 'chat':
  51. raise NotChatAppError()
  52. conversation_id = str(c_id)
  53. ConversationService.delete(app_model, conversation_id, end_user)
  54. WebConversationService.unpin(app_model, conversation_id, end_user)
  55. return {"result": "success"}, 204
  56. class ConversationRenameApi(WebApiResource):
  57. @marshal_with(conversation_fields)
  58. def post(self, app_model, end_user, c_id):
  59. if app_model.mode != 'chat':
  60. raise NotChatAppError()
  61. conversation_id = str(c_id)
  62. parser = reqparse.RequestParser()
  63. parser.add_argument('name', type=str, required=True, location='json')
  64. args = parser.parse_args()
  65. try:
  66. return ConversationService.rename(app_model, conversation_id, end_user, args['name'])
  67. except ConversationNotExistsError:
  68. raise NotFound("Conversation Not Exists.")
  69. class ConversationPinApi(WebApiResource):
  70. def patch(self, app_model, end_user, c_id):
  71. if app_model.mode != 'chat':
  72. raise NotChatAppError()
  73. conversation_id = str(c_id)
  74. try:
  75. WebConversationService.pin(app_model, conversation_id, end_user)
  76. except ConversationNotExistsError:
  77. raise NotFound("Conversation Not Exists.")
  78. return {"result": "success"}
  79. class ConversationUnPinApi(WebApiResource):
  80. def patch(self, app_model, end_user, c_id):
  81. if app_model.mode != 'chat':
  82. raise NotChatAppError()
  83. conversation_id = str(c_id)
  84. WebConversationService.unpin(app_model, conversation_id, end_user)
  85. return {"result": "success"}
  86. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='web_conversation_name')
  87. api.add_resource(ConversationListApi, '/conversations')
  88. api.add_resource(ConversationApi, '/conversations/<uuid:c_id>')
  89. api.add_resource(ConversationPinApi, '/conversations/<uuid:c_id>/pin')
  90. api.add_resource(ConversationUnPinApi, '/conversations/<uuid:c_id>/unpin')