Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

conversation.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from flask_restx import Resource, marshal_with, reqparse
  2. from flask_restx.inputs import int_range
  3. from sqlalchemy.orm import Session
  4. from werkzeug.exceptions import BadRequest, 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_fields,
  18. conversation_variable_infinite_scroll_pagination_fields,
  19. )
  20. from libs.helper import uuid_value
  21. from models.model import App, AppMode, EndUser
  22. from services.conversation_service import ConversationService
  23. class ConversationApi(Resource):
  24. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
  25. @marshal_with(conversation_infinite_scroll_pagination_fields)
  26. def get(self, app_model: App, end_user: EndUser):
  27. app_mode = AppMode.value_of(app_model.mode)
  28. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  29. raise NotChatAppError()
  30. parser = reqparse.RequestParser()
  31. parser.add_argument("last_id", type=uuid_value, location="args")
  32. parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
  33. parser.add_argument(
  34. "sort_by",
  35. type=str,
  36. choices=["created_at", "-created_at", "updated_at", "-updated_at"],
  37. required=False,
  38. default="-updated_at",
  39. location="args",
  40. )
  41. args = parser.parse_args()
  42. try:
  43. with Session(db.engine) as session:
  44. return ConversationService.pagination_by_last_id(
  45. session=session,
  46. app_model=app_model,
  47. user=end_user,
  48. last_id=args["last_id"],
  49. limit=args["limit"],
  50. invoke_from=InvokeFrom.SERVICE_API,
  51. sort_by=args["sort_by"],
  52. )
  53. except services.errors.conversation.LastConversationNotExistsError:
  54. raise NotFound("Last Conversation Not Exists.")
  55. class ConversationDetailApi(Resource):
  56. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  57. @marshal_with(conversation_delete_fields)
  58. def delete(self, app_model: App, end_user: EndUser, c_id):
  59. app_mode = AppMode.value_of(app_model.mode)
  60. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  61. raise NotChatAppError()
  62. conversation_id = str(c_id)
  63. try:
  64. ConversationService.delete(app_model, conversation_id, end_user)
  65. except services.errors.conversation.ConversationNotExistsError:
  66. raise NotFound("Conversation Not Exists.")
  67. return {"result": "success"}, 204
  68. class ConversationRenameApi(Resource):
  69. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  70. @marshal_with(simple_conversation_fields)
  71. def post(self, app_model: App, end_user: EndUser, c_id):
  72. app_mode = AppMode.value_of(app_model.mode)
  73. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  74. raise NotChatAppError()
  75. conversation_id = str(c_id)
  76. parser = reqparse.RequestParser()
  77. parser.add_argument("name", type=str, required=False, location="json")
  78. parser.add_argument("auto_generate", type=bool, required=False, default=False, location="json")
  79. args = parser.parse_args()
  80. try:
  81. return ConversationService.rename(app_model, conversation_id, end_user, args["name"], args["auto_generate"])
  82. except services.errors.conversation.ConversationNotExistsError:
  83. raise NotFound("Conversation Not Exists.")
  84. class ConversationVariablesApi(Resource):
  85. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
  86. @marshal_with(conversation_variable_infinite_scroll_pagination_fields)
  87. def get(self, app_model: App, end_user: EndUser, c_id):
  88. # conversational variable only for chat app
  89. app_mode = AppMode.value_of(app_model.mode)
  90. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  91. raise NotChatAppError()
  92. conversation_id = str(c_id)
  93. parser = reqparse.RequestParser()
  94. parser.add_argument("last_id", type=uuid_value, location="args")
  95. parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
  96. args = parser.parse_args()
  97. try:
  98. return ConversationService.get_conversational_variable(
  99. app_model, conversation_id, end_user, args["limit"], args["last_id"]
  100. )
  101. except services.errors.conversation.ConversationNotExistsError:
  102. raise NotFound("Conversation Not Exists.")
  103. class ConversationVariableDetailApi(Resource):
  104. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  105. @marshal_with(conversation_variable_fields)
  106. def put(self, app_model: App, end_user: EndUser, c_id, variable_id):
  107. """Update a conversation variable's value"""
  108. app_mode = AppMode.value_of(app_model.mode)
  109. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  110. raise NotChatAppError()
  111. conversation_id = str(c_id)
  112. variable_id = str(variable_id)
  113. parser = reqparse.RequestParser()
  114. # using lambda is for passing the already-typed value without modification
  115. # if no lambda, it will be converted to string
  116. # the string cannot be converted using json.loads
  117. parser.add_argument("value", required=True, location="json", type=lambda x: x)
  118. args = parser.parse_args()
  119. try:
  120. return ConversationService.update_conversation_variable(
  121. app_model, conversation_id, variable_id, end_user, args["value"]
  122. )
  123. except services.errors.conversation.ConversationNotExistsError:
  124. raise NotFound("Conversation Not Exists.")
  125. except services.errors.conversation.ConversationVariableNotExistsError:
  126. raise NotFound("Conversation Variable Not Exists.")
  127. except services.errors.conversation.ConversationVariableTypeMismatchError as e:
  128. raise BadRequest(str(e))
  129. api.add_resource(ConversationRenameApi, "/conversations/<uuid:c_id>/name", endpoint="conversation_name")
  130. api.add_resource(ConversationApi, "/conversations")
  131. api.add_resource(ConversationDetailApi, "/conversations/<uuid:c_id>", endpoint="conversation_detail")
  132. api.add_resource(ConversationVariablesApi, "/conversations/<uuid:c_id>/variables", endpoint="conversation_variables")
  133. api.add_resource(
  134. ConversationVariableDetailApi,
  135. "/conversations/<uuid:c_id>/variables/<uuid:variable_id>",
  136. endpoint="conversation_variable_detail",
  137. methods=["PUT"],
  138. )