conversation.py 4.2 KB

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