conversation.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. try:
  57. ConversationService.delete(app_model, conversation_id, current_user)
  58. except ConversationNotExistsError:
  59. raise NotFound("Conversation Not Exists.")
  60. WebConversationService.unpin(app_model, conversation_id, current_user)
  61. return {"result": "success"}, 204
  62. class ConversationRenameApi(InstalledAppResource):
  63. @marshal_with(conversation_fields)
  64. def post(self, installed_app, c_id):
  65. app_model = installed_app.app
  66. if app_model.mode != 'chat':
  67. raise NotChatAppError()
  68. conversation_id = str(c_id)
  69. parser = reqparse.RequestParser()
  70. parser.add_argument('name', type=str, required=True, location='json')
  71. args = parser.parse_args()
  72. try:
  73. return ConversationService.rename(app_model, conversation_id, current_user, args['name'])
  74. except ConversationNotExistsError:
  75. raise NotFound("Conversation Not Exists.")
  76. class ConversationPinApi(InstalledAppResource):
  77. def patch(self, installed_app, c_id):
  78. app_model = installed_app.app
  79. if app_model.mode != '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. if app_model.mode != 'chat':
  91. raise NotChatAppError()
  92. conversation_id = str(c_id)
  93. WebConversationService.unpin(app_model, conversation_id, current_user)
  94. return {"result": "success"}
  95. api.add_resource(ConversationRenameApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/name', endpoint='installed_app_conversation_rename')
  96. api.add_resource(ConversationListApi, '/installed-apps/<uuid:installed_app_id>/conversations', endpoint='installed_app_conversations')
  97. api.add_resource(ConversationApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>', endpoint='installed_app_conversation')
  98. api.add_resource(ConversationPinApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/pin', endpoint='installed_app_conversation_pin')
  99. api.add_resource(ConversationUnPinApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/unpin', endpoint='installed_app_conversation_unpin')