conversation.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ConversationService.delete(app_model, conversation_id, end_user)
  57. except services.errors.conversation.ConversationNotExistsError:
  58. raise NotFound("Conversation Not Exists.")
  59. return {"result": "success"}, 200
  60. class ConversationRenameApi(Resource):
  61. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  62. @marshal_with(simple_conversation_fields)
  63. def post(self, app_model: App, end_user: EndUser, c_id):
  64. app_mode = AppMode.value_of(app_model.mode)
  65. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  66. raise NotChatAppError()
  67. conversation_id = str(c_id)
  68. parser = reqparse.RequestParser()
  69. parser.add_argument("name", type=str, required=False, location="json")
  70. parser.add_argument("auto_generate", type=bool, required=False, default=False, location="json")
  71. args = parser.parse_args()
  72. try:
  73. return ConversationService.rename(app_model, conversation_id, end_user, args["name"], args["auto_generate"])
  74. except services.errors.conversation.ConversationNotExistsError:
  75. raise NotFound("Conversation Not Exists.")
  76. api.add_resource(ConversationRenameApi, "/conversations/<uuid:c_id>/name", endpoint="conversation_name")
  77. api.add_resource(ConversationApi, "/conversations")
  78. api.add_resource(ConversationDetailApi, "/conversations/<uuid:c_id>", endpoint="conversation_detail")