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.

пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
пре 2 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from flask_login import current_user
  2. from flask_restx import marshal_with, reqparse
  3. from flask_restx.inputs import int_range
  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. return {"result": "success"}, 204
  56. class ConversationRenameApi(InstalledAppResource):
  57. @marshal_with(simple_conversation_fields)
  58. def post(self, installed_app, c_id):
  59. app_model = installed_app.app
  60. app_mode = AppMode.value_of(app_model.mode)
  61. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  62. raise NotChatAppError()
  63. conversation_id = str(c_id)
  64. parser = reqparse.RequestParser()
  65. parser.add_argument("name", type=str, required=False, location="json")
  66. parser.add_argument("auto_generate", type=bool, required=False, default=False, location="json")
  67. args = parser.parse_args()
  68. try:
  69. return ConversationService.rename(
  70. app_model, conversation_id, current_user, args["name"], args["auto_generate"]
  71. )
  72. except ConversationNotExistsError:
  73. raise NotFound("Conversation Not Exists.")
  74. class ConversationPinApi(InstalledAppResource):
  75. def patch(self, installed_app, c_id):
  76. app_model = installed_app.app
  77. app_mode = AppMode.value_of(app_model.mode)
  78. if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
  79. raise NotChatAppError()
  80. conversation_id = str(c_id)
  81. try:
  82. WebConversationService.pin(app_model, conversation_id, current_user)
  83. except ConversationNotExistsError:
  84. raise NotFound("Conversation Not Exists.")
  85. return {"result": "success"}
  86. class ConversationUnPinApi(InstalledAppResource):
  87. def patch(self, installed_app, c_id):
  88. app_model = installed_app.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. WebConversationService.unpin(app_model, conversation_id, current_user)
  94. return {"result": "success"}