conversation.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from flask_restful import Resource, marshal_with, reqparse
  2. from flask_restful.inputs import int_range
  3. from werkzeug.exceptions import NotFound
  4. import services
  5. from controllers.service_api import api
  6. from controllers.service_api.app.error import NotChatAppError
  7. from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token
  8. from core.app.entities.app_invoke_entities import InvokeFrom
  9. from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields
  10. from libs.helper import uuid_value
  11. from models.model import App, AppMode, EndUser
  12. from services.conversation_service import ConversationService
  13. class ConversationApi(Resource):
  14. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
  15. @marshal_with(conversation_infinite_scroll_pagination_fields)
  16. def get(self, app_model: App, end_user: EndUser):
  17. app_mode = AppMode.value_of(app_model.mode)
  18. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  19. raise NotChatAppError()
  20. parser = reqparse.RequestParser()
  21. parser.add_argument('last_id', type=uuid_value, location='args')
  22. parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
  23. args = parser.parse_args()
  24. try:
  25. return ConversationService.pagination_by_last_id(
  26. app_model=app_model,
  27. user=end_user,
  28. last_id=args['last_id'],
  29. limit=args['limit'],
  30. invoke_from=InvokeFrom.SERVICE_API
  31. )
  32. except services.errors.conversation.LastConversationNotExistsError:
  33. raise NotFound("Last Conversation Not Exists.")
  34. class ConversationDetailApi(Resource):
  35. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  36. @marshal_with(simple_conversation_fields)
  37. def delete(self, app_model: App, end_user: EndUser, c_id):
  38. app_mode = AppMode.value_of(app_model.mode)
  39. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  40. raise NotChatAppError()
  41. conversation_id = str(c_id)
  42. try:
  43. ConversationService.delete(app_model, conversation_id, end_user)
  44. except services.errors.conversation.ConversationNotExistsError:
  45. raise NotFound("Conversation Not Exists.")
  46. return {"result": "success"}, 204
  47. class ConversationRenameApi(Resource):
  48. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  49. @marshal_with(simple_conversation_fields)
  50. def post(self, app_model: App, end_user: EndUser, c_id):
  51. app_mode = AppMode.value_of(app_model.mode)
  52. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  53. raise NotChatAppError()
  54. conversation_id = str(c_id)
  55. parser = reqparse.RequestParser()
  56. parser.add_argument('name', type=str, required=False, location='json')
  57. parser.add_argument('auto_generate', type=bool, required=False, default=False, location='json')
  58. args = parser.parse_args()
  59. try:
  60. return ConversationService.rename(
  61. app_model,
  62. conversation_id,
  63. end_user,
  64. args['name'],
  65. args['auto_generate']
  66. )
  67. except services.errors.conversation.ConversationNotExistsError:
  68. raise NotFound("Conversation Not Exists.")
  69. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='conversation_name')
  70. api.add_resource(ConversationApi, '/conversations')
  71. api.add_resource(ConversationDetailApi, '/conversations/<uuid:c_id>', endpoint='conversation_detail')