conversation.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from flask_login import current_user # type: ignore
  2. from flask_restful import marshal_with, reqparse # type: ignore
  3. from flask_restful.inputs import int_range # type: ignore
  4. from sqlalchemy.orm import Session
  5. from werkzeug.exceptions import NotFound
  6. from controllers.console.explore.error import NotChatAppError
  7. from controllers.console.explore.wraps import InstalledAppResource
  8. from core.app.entities.app_invoke_entities import InvokeFrom
  9. from extensions.ext_database import db
  10. from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields
  11. from libs.helper import uuid_value
  12. from models.model import AppMode
  13. from services.conversation_service import ConversationService
  14. from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError
  15. from services.web_conversation_service import WebConversationService
  16. class ConversationListApi(InstalledAppResource):
  17. @marshal_with(conversation_infinite_scroll_pagination_fields)
  18. def get(self, installed_app):
  19. app_model = installed_app.app
  20. app_mode = AppMode.value_of(app_model.mode)
  21. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  22. raise NotChatAppError()
  23. parser = reqparse.RequestParser()
  24. parser.add_argument("last_id", type=uuid_value, location="args")
  25. parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
  26. parser.add_argument("pinned", type=str, choices=["true", "false", None], location="args")
  27. args = parser.parse_args()
  28. pinned = None
  29. if "pinned" in args and args["pinned"] is not None:
  30. pinned = args["pinned"] == "true"
  31. try:
  32. with Session(db.engine) as session:
  33. return WebConversationService.pagination_by_last_id(
  34. session=session,
  35. app_model=app_model,
  36. user=current_user,
  37. last_id=args["last_id"],
  38. limit=args["limit"],
  39. invoke_from=InvokeFrom.EXPLORE,
  40. pinned=pinned,
  41. )
  42. except LastConversationNotExistsError:
  43. raise NotFound("Last Conversation Not Exists.")
  44. class ConversationApi(InstalledAppResource):
  45. def delete(self, installed_app, c_id):
  46. app_model = installed_app.app
  47. app_mode = AppMode.value_of(app_model.mode)
  48. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  49. raise NotChatAppError()
  50. conversation_id = str(c_id)
  51. try:
  52. ConversationService.delete(app_model, conversation_id, current_user)
  53. except ConversationNotExistsError:
  54. raise NotFound("Conversation Not Exists.")
  55. WebConversationService.unpin(app_model, conversation_id, current_user)
  56. return {"result": "success"}, 204
  57. class ConversationRenameApi(InstalledAppResource):
  58. @marshal_with(simple_conversation_fields)
  59. def post(self, installed_app, c_id):
  60. app_model = installed_app.app
  61. app_mode = AppMode.value_of(app_model.mode)
  62. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  63. raise NotChatAppError()
  64. conversation_id = str(c_id)
  65. parser = reqparse.RequestParser()
  66. parser.add_argument("name", type=str, required=False, location="json")
  67. parser.add_argument("auto_generate", type=bool, required=False, default=False, location="json")
  68. args = parser.parse_args()
  69. try:
  70. return ConversationService.rename(
  71. app_model, conversation_id, current_user, args["name"], args["auto_generate"]
  72. )
  73. except ConversationNotExistsError:
  74. raise NotFound("Conversation Not Exists.")
  75. class ConversationPinApi(InstalledAppResource):
  76. def patch(self, installed_app, c_id):
  77. app_model = installed_app.app
  78. app_mode = AppMode.value_of(app_model.mode)
  79. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  80. raise NotChatAppError()
  81. conversation_id = str(c_id)
  82. try:
  83. WebConversationService.pin(app_model, conversation_id, current_user)
  84. except ConversationNotExistsError:
  85. raise NotFound("Conversation Not Exists.")
  86. return {"result": "success"}
  87. class ConversationUnPinApi(InstalledAppResource):
  88. def patch(self, installed_app, c_id):
  89. app_model = installed_app.app
  90. app_mode = AppMode.value_of(app_model.mode)
  91. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  92. raise NotChatAppError()
  93. conversation_id = str(c_id)
  94. WebConversationService.unpin(app_model, conversation_id, current_user)
  95. return {"result": "success"}