conversation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 (
  10. conversation_delete_fields,
  11. conversation_infinite_scroll_pagination_fields,
  12. simple_conversation_fields,
  13. )
  14. from libs.helper import uuid_value
  15. from models.model import App, AppMode, EndUser
  16. from services.conversation_service import ConversationService
  17. class ConversationApi(Resource):
  18. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
  19. @marshal_with(conversation_infinite_scroll_pagination_fields)
  20. def get(self, app_model: App, end_user: EndUser):
  21. app_mode = AppMode.value_of(app_model.mode)
  22. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  23. raise NotChatAppError()
  24. parser = reqparse.RequestParser()
  25. parser.add_argument("last_id", type=uuid_value, location="args")
  26. parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
  27. parser.add_argument(
  28. "sort_by",
  29. type=str,
  30. choices=["created_at", "-created_at", "updated_at", "-updated_at"],
  31. required=False,
  32. default="-updated_at",
  33. location="args",
  34. )
  35. args = parser.parse_args()
  36. try:
  37. return ConversationService.pagination_by_last_id(
  38. app_model=app_model,
  39. user=end_user,
  40. last_id=args["last_id"],
  41. limit=args["limit"],
  42. invoke_from=InvokeFrom.SERVICE_API,
  43. sort_by=args["sort_by"],
  44. )
  45. except services.errors.conversation.LastConversationNotExistsError:
  46. raise NotFound("Last Conversation Not Exists.")
  47. class ConversationDetailApi(Resource):
  48. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  49. @marshal_with(conversation_delete_fields)
  50. def delete(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. try:
  56. return ConversationService.delete(app_model, conversation_id, end_user)
  57. except services.errors.conversation.ConversationNotExistsError:
  58. raise NotFound("Conversation Not Exists.")
  59. class ConversationRenameApi(Resource):
  60. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  61. @marshal_with(simple_conversation_fields)
  62. def post(self, app_model: App, end_user: EndUser, c_id):
  63. app_mode = AppMode.value_of(app_model.mode)
  64. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  65. raise NotChatAppError()
  66. conversation_id = str(c_id)
  67. parser = reqparse.RequestParser()
  68. parser.add_argument("name", type=str, required=False, location="json")
  69. parser.add_argument("auto_generate", type=bool, required=False, default=False, location="json")
  70. args = parser.parse_args()
  71. try:
  72. return ConversationService.rename(app_model, conversation_id, end_user, args["name"], args["auto_generate"])
  73. except services.errors.conversation.ConversationNotExistsError:
  74. raise NotFound("Conversation Not Exists.")
  75. api.add_resource(ConversationRenameApi, "/conversations/<uuid:c_id>/name", endpoint="conversation_name")
  76. api.add_resource(ConversationApi, "/conversations")
  77. api.add_resource(ConversationDetailApi, "/conversations/<uuid:c_id>", endpoint="conversation_detail")