You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conversation.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from flask_restful import Resource, marshal_with, reqparse
  2. from flask_restful.inputs import int_range
  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 fields.conversation_variable_fields import (
  17. conversation_variable_infinite_scroll_pagination_fields,
  18. )
  19. from libs.helper import uuid_value
  20. from models.model import App, AppMode, EndUser
  21. from services.conversation_service import ConversationService
  22. class ConversationApi(Resource):
  23. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
  24. @marshal_with(conversation_infinite_scroll_pagination_fields)
  25. def get(self, app_model: App, end_user: EndUser):
  26. app_mode = AppMode.value_of(app_model.mode)
  27. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  28. raise NotChatAppError()
  29. parser = reqparse.RequestParser()
  30. parser.add_argument("last_id", type=uuid_value, location="args")
  31. parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
  32. parser.add_argument(
  33. "sort_by",
  34. type=str,
  35. choices=["created_at", "-created_at", "updated_at", "-updated_at"],
  36. required=False,
  37. default="-updated_at",
  38. location="args",
  39. )
  40. args = parser.parse_args()
  41. try:
  42. with Session(db.engine) as session:
  43. return ConversationService.pagination_by_last_id(
  44. session=session,
  45. app_model=app_model,
  46. user=end_user,
  47. last_id=args["last_id"],
  48. limit=args["limit"],
  49. invoke_from=InvokeFrom.SERVICE_API,
  50. sort_by=args["sort_by"],
  51. )
  52. except services.errors.conversation.LastConversationNotExistsError:
  53. raise NotFound("Last Conversation Not Exists.")
  54. class ConversationDetailApi(Resource):
  55. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  56. @marshal_with(conversation_delete_fields)
  57. def delete(self, app_model: App, end_user: EndUser, c_id):
  58. app_mode = AppMode.value_of(app_model.mode)
  59. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  60. raise NotChatAppError()
  61. conversation_id = str(c_id)
  62. try:
  63. ConversationService.delete(app_model, conversation_id, end_user)
  64. except services.errors.conversation.ConversationNotExistsError:
  65. raise NotFound("Conversation Not Exists.")
  66. return {"result": "success"}, 204
  67. class ConversationRenameApi(Resource):
  68. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  69. @marshal_with(simple_conversation_fields)
  70. def post(self, app_model: App, end_user: EndUser, c_id):
  71. app_mode = AppMode.value_of(app_model.mode)
  72. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  73. raise NotChatAppError()
  74. conversation_id = str(c_id)
  75. parser = reqparse.RequestParser()
  76. parser.add_argument("name", type=str, required=False, location="json")
  77. parser.add_argument("auto_generate", type=bool, required=False, default=False, location="json")
  78. args = parser.parse_args()
  79. try:
  80. return ConversationService.rename(app_model, conversation_id, end_user, args["name"], args["auto_generate"])
  81. except services.errors.conversation.ConversationNotExistsError:
  82. raise NotFound("Conversation Not Exists.")
  83. class ConversationVariablesApi(Resource):
  84. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
  85. @marshal_with(conversation_variable_infinite_scroll_pagination_fields)
  86. def get(self, app_model: App, end_user: EndUser, c_id):
  87. # conversational variable only for chat app
  88. app_mode = AppMode.value_of(app_model.mode)
  89. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  90. raise NotChatAppError()
  91. conversation_id = str(c_id)
  92. parser = reqparse.RequestParser()
  93. parser.add_argument("last_id", type=uuid_value, location="args")
  94. parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
  95. args = parser.parse_args()
  96. try:
  97. return ConversationService.get_conversational_variable(
  98. app_model, conversation_id, end_user, args["limit"], args["last_id"]
  99. )
  100. except services.errors.conversation.ConversationNotExistsError:
  101. raise NotFound("Conversation Not Exists.")
  102. api.add_resource(ConversationRenameApi, "/conversations/<uuid:c_id>/name", endpoint="conversation_name")
  103. api.add_resource(ConversationApi, "/conversations")
  104. api.add_resource(ConversationDetailApi, "/conversations/<uuid:c_id>", endpoint="conversation_detail")
  105. api.add_resource(ConversationVariablesApi, "/conversations/<uuid:c_id>/variables", endpoint="conversation_variables")