conversation.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 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. 'model_config': fields.Raw,
  20. }
  21. conversation_infinite_scroll_pagination_fields = {
  22. 'limit': fields.Integer,
  23. 'has_more': fields.Boolean,
  24. 'data': fields.List(fields.Nested(conversation_fields))
  25. }
  26. class UniversalChatConversationListApi(UniversalChatResource):
  27. @marshal_with(conversation_infinite_scroll_pagination_fields)
  28. def get(self, universal_app):
  29. app_model = universal_app
  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=current_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 UniversalChatConversationApi(UniversalChatResource):
  49. def delete(self, universal_app, c_id):
  50. app_model = universal_app
  51. conversation_id = str(c_id)
  52. try:
  53. ConversationService.delete(app_model, conversation_id, current_user)
  54. except ConversationNotExistsError:
  55. raise NotFound("Conversation Not Exists.")
  56. WebConversationService.unpin(app_model, conversation_id, current_user)
  57. return {"result": "success"}, 204
  58. class UniversalChatConversationRenameApi(UniversalChatResource):
  59. @marshal_with(conversation_fields)
  60. def post(self, universal_app, c_id):
  61. app_model = universal_app
  62. conversation_id = str(c_id)
  63. parser = reqparse.RequestParser()
  64. parser.add_argument('name', type=str, required=True, location='json')
  65. args = parser.parse_args()
  66. try:
  67. return ConversationService.rename(app_model, conversation_id, current_user, args['name'])
  68. except ConversationNotExistsError:
  69. raise NotFound("Conversation Not Exists.")
  70. class UniversalChatConversationPinApi(UniversalChatResource):
  71. def patch(self, universal_app, c_id):
  72. app_model = universal_app
  73. conversation_id = str(c_id)
  74. try:
  75. WebConversationService.pin(app_model, conversation_id, current_user)
  76. except ConversationNotExistsError:
  77. raise NotFound("Conversation Not Exists.")
  78. return {"result": "success"}
  79. class UniversalChatConversationUnPinApi(UniversalChatResource):
  80. def patch(self, universal_app, c_id):
  81. app_model = universal_app
  82. conversation_id = str(c_id)
  83. WebConversationService.unpin(app_model, conversation_id, current_user)
  84. return {"result": "success"}
  85. api.add_resource(UniversalChatConversationRenameApi, '/universal-chat/conversations/<uuid:c_id>/name')
  86. api.add_resource(UniversalChatConversationListApi, '/universal-chat/conversations')
  87. api.add_resource(UniversalChatConversationApi, '/universal-chat/conversations/<uuid:c_id>')
  88. api.add_resource(UniversalChatConversationPinApi, '/universal-chat/conversations/<uuid:c_id>/pin')
  89. api.add_resource(UniversalChatConversationUnPinApi, '/universal-chat/conversations/<uuid:c_id>/unpin')