+18


![dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)

![autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)




FFXN
GitHub
yyh
盐粒 Yanli
autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Tianle
dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Yunlu Wen
zyssyz123
Claude Opus 4.7
chariri
Asuka Minato
Copilot Autofix powered by AI
Nian
非法操作
Carmen Fernández Ruiz
wangxiaolei
QuantumGhost
L1nSn0w
Evan
Escape0707
Jingyi
Amr Sherif
ZHOU ZHICHEN
unknown
JzoNg
Xiyuan Chen
-LAN-
107bba0116
Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: EvanYao826 <[email protected]> Co-authored-by: yyh <[email protected]> Co-authored-by: 盐粒 Yanli <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Tianle <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Yunlu Wen <[email protected]> Co-authored-by: zyssyz123 <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> Co-authored-by: chariri <[email protected]> Co-authored-by: Asuka Minato <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]> Co-authored-by: Nian <[email protected]> Co-authored-by: 非法操作 <[email protected]> Co-authored-by: Carmen Fernández Ruiz <[email protected]> Co-authored-by: wangxiaolei <[email protected]> Co-authored-by: QuantumGhost <[email protected]> Co-authored-by: L1nSn0w <[email protected]> Co-authored-by: Evan <[email protected]> Co-authored-by: Escape0707 <[email protected]> Co-authored-by: Jingyi <[email protected]> Co-authored-by: Amr Sherif <[email protected]> Co-authored-by: ZHOU ZHICHEN <[email protected]> Co-authored-by: unknown <[email protected]> Co-authored-by: JzoNg <[email protected]> Co-authored-by: Xiyuan Chen <[email protected]> Co-authored-by: -LAN- <[email protected]>
119 lines
4.4 KiB
Python
119 lines
4.4 KiB
Python
from uuid import UUID
|
|
|
|
from flask import request
|
|
from pydantic import TypeAdapter
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
from controllers.common.controller_schemas import SavedMessageCreatePayload, SavedMessageListQuery
|
|
from controllers.common.schema import register_response_schema_models, register_schema_models
|
|
from controllers.web import web_ns
|
|
from controllers.web.error import NotCompletionAppError
|
|
from controllers.web.wraps import WebApiResource
|
|
from fields.conversation_fields import ResultResponse
|
|
from fields.message_fields import SavedMessageInfiniteScrollPagination, SavedMessageItem
|
|
from models.model import App, EndUser
|
|
from services.errors.message import MessageNotExistsError
|
|
from services.saved_message_service import SavedMessageService
|
|
|
|
register_schema_models(web_ns, SavedMessageListQuery, SavedMessageCreatePayload)
|
|
register_response_schema_models(web_ns, ResultResponse)
|
|
|
|
|
|
@web_ns.route("/saved-messages")
|
|
class SavedMessageListApi(WebApiResource):
|
|
@web_ns.doc("Get Saved Messages")
|
|
@web_ns.doc(description="Retrieve paginated list of saved messages for a completion application.")
|
|
@web_ns.doc(
|
|
params={
|
|
"last_id": {"description": "Last message ID for pagination", "type": "string", "required": False},
|
|
"limit": {
|
|
"description": "Number of messages to return (1-100)",
|
|
"type": "integer",
|
|
"required": False,
|
|
"default": 20,
|
|
},
|
|
}
|
|
)
|
|
@web_ns.doc(
|
|
responses={
|
|
200: "Success",
|
|
400: "Bad Request - Not a completion app",
|
|
401: "Unauthorized",
|
|
403: "Forbidden",
|
|
404: "App Not Found",
|
|
500: "Internal Server Error",
|
|
}
|
|
)
|
|
def get(self, app_model: App, end_user: EndUser):
|
|
if app_model.mode != "completion":
|
|
raise NotCompletionAppError()
|
|
|
|
raw_args = request.args.to_dict()
|
|
query = SavedMessageListQuery.model_validate(raw_args)
|
|
|
|
pagination = SavedMessageService.pagination_by_last_id(app_model, end_user, query.last_id, query.limit)
|
|
adapter = TypeAdapter(SavedMessageItem)
|
|
items = [adapter.validate_python(message, from_attributes=True) for message in pagination.data]
|
|
return SavedMessageInfiniteScrollPagination(
|
|
limit=pagination.limit,
|
|
has_more=pagination.has_more,
|
|
data=items,
|
|
).model_dump(mode="json")
|
|
|
|
@web_ns.doc("Save Message")
|
|
@web_ns.doc(description="Save a specific message for later reference.")
|
|
@web_ns.doc(
|
|
params={
|
|
"message_id": {"description": "Message UUID to save", "type": "string", "required": True},
|
|
}
|
|
)
|
|
@web_ns.doc(
|
|
responses={
|
|
200: "Message saved successfully",
|
|
400: "Bad Request - Not a completion app",
|
|
401: "Unauthorized",
|
|
403: "Forbidden",
|
|
404: "Message Not Found",
|
|
500: "Internal Server Error",
|
|
}
|
|
)
|
|
@web_ns.response(200, "Message saved successfully", web_ns.models[ResultResponse.__name__])
|
|
def post(self, app_model: App, end_user: EndUser):
|
|
if app_model.mode != "completion":
|
|
raise NotCompletionAppError()
|
|
|
|
payload = SavedMessageCreatePayload.model_validate(web_ns.payload or {})
|
|
|
|
try:
|
|
SavedMessageService.save(app_model, end_user, payload.message_id)
|
|
except MessageNotExistsError:
|
|
raise NotFound("Message Not Exists.")
|
|
|
|
return ResultResponse(result="success").model_dump(mode="json")
|
|
|
|
|
|
@web_ns.route("/saved-messages/<uuid:message_id>")
|
|
class SavedMessageApi(WebApiResource):
|
|
@web_ns.doc("Delete Saved Message")
|
|
@web_ns.doc(description="Remove a message from saved messages.")
|
|
@web_ns.doc(params={"message_id": {"description": "Message UUID to delete", "type": "string", "required": True}})
|
|
@web_ns.doc(
|
|
responses={
|
|
204: "Message removed successfully",
|
|
400: "Bad Request - Not a completion app",
|
|
401: "Unauthorized",
|
|
403: "Forbidden",
|
|
404: "Message Not Found",
|
|
500: "Internal Server Error",
|
|
}
|
|
)
|
|
def delete(self, app_model: App, end_user: EndUser, message_id: UUID):
|
|
message_id_str = str(message_id)
|
|
|
|
if app_model.mode != "completion":
|
|
raise NotCompletionAppError()
|
|
|
|
SavedMessageService.delete(app_model, end_user, message_id_str)
|
|
|
|
return "", 204
|