conversation.py 4.7 KB

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