From 7b1aa33ad4ce8cdf07a03bc71f17d3502dd7eee1 Mon Sep 17 00:00:00 2001 From: Lillian <11332799+Lillian68@users.noreply.github.com> Date: Sun, 24 May 2026 15:58:10 +0800 Subject: [PATCH] chore: add UUID/str type annotations to api endpoints for files in api/controllers/files and api/controllers/web (#36562) --- api/controllers/files/image_preview.py | 19 ++++++++++--------- api/controllers/files/tool_files.py | 11 +++++++---- api/controllers/web/completion.py | 4 ++-- api/controllers/web/conversation.py | 9 +++++---- api/controllers/web/message.py | 19 ++++++++++--------- api/controllers/web/saved_message.py | 8 +++++--- 6 files changed, 39 insertions(+), 31 deletions(-) diff --git a/api/controllers/files/image_preview.py b/api/controllers/files/image_preview.py index be7886e831..0d4b89096a 100644 --- a/api/controllers/files/image_preview.py +++ b/api/controllers/files/image_preview.py @@ -1,4 +1,5 @@ from urllib.parse import quote +from uuid import UUID from flask import Response, request from flask_restx import Resource @@ -49,8 +50,8 @@ class ImagePreviewApi(Resource): 415: "Unsupported file type", } ) - def get(self, file_id): - file_id = str(file_id) + def get(self, file_id: UUID): + file_id_str = str(file_id) args = FileSignatureQuery.model_validate(request.args.to_dict(flat=True)) timestamp = args.timestamp @@ -59,7 +60,7 @@ class ImagePreviewApi(Resource): try: generator, mimetype = FileService(db.engine).get_image_preview( - file_id=file_id, + file_id=file_id_str, timestamp=timestamp, nonce=nonce, sign=sign, @@ -91,14 +92,14 @@ class FilePreviewApi(Resource): 415: "Unsupported file type", } ) - def get(self, file_id): - file_id = str(file_id) + def get(self, file_id: UUID): + file_id_str = str(file_id) args = FilePreviewQuery.model_validate(request.args.to_dict(flat=True)) try: generator, upload_file = FileService(db.engine).get_file_generator_by_file_id( - file_id=file_id, + file_id=file_id_str, timestamp=args.timestamp, nonce=args.nonce, sign=args.sign, @@ -159,10 +160,10 @@ class WorkspaceWebappLogoApi(Resource): 415: "Unsupported file type", } ) - def get(self, workspace_id): - workspace_id = str(workspace_id) + def get(self, workspace_id: UUID): + workspace_id_str = str(workspace_id) - custom_config = TenantService.get_custom_config(workspace_id) + custom_config = TenantService.get_custom_config(workspace_id_str) webapp_logo_file_id = custom_config.get("replace_webapp_logo") if custom_config is not None else None if not webapp_logo_file_id: diff --git a/api/controllers/files/tool_files.py b/api/controllers/files/tool_files.py index 8ae16ce7f4..ef47485d80 100644 --- a/api/controllers/files/tool_files.py +++ b/api/controllers/files/tool_files.py @@ -1,4 +1,5 @@ from urllib.parse import quote +from uuid import UUID from flask import Response, request from flask_restx import Resource @@ -45,17 +46,19 @@ class ToolFileApi(Resource): 415: "Unsupported file type", } ) - def get(self, file_id, extension): - file_id = str(file_id) + def get(self, file_id: UUID, extension: str): + file_id_str = str(file_id) args = ToolFileQuery.model_validate(request.args.to_dict()) - if not verify_tool_file_signature(file_id=file_id, timestamp=args.timestamp, nonce=args.nonce, sign=args.sign): + if not verify_tool_file_signature( + file_id=file_id_str, timestamp=args.timestamp, nonce=args.nonce, sign=args.sign + ): raise Forbidden("Invalid request.") try: tool_file_manager = ToolFileManager() stream, tool_file = tool_file_manager.get_file_generator_by_tool_file_id( - file_id, + file_id_str, ) if not stream or not tool_file: diff --git a/api/controllers/web/completion.py b/api/controllers/web/completion.py index 8f52988ef9..f08d08ab7d 100644 --- a/api/controllers/web/completion.py +++ b/api/controllers/web/completion.py @@ -140,7 +140,7 @@ class CompletionStopApi(WebApiResource): } ) @web_ns.response(200, "Success", web_ns.models[SimpleResultResponse.__name__]) - def post(self, app_model, end_user, task_id): + def post(self, app_model, end_user, task_id: str): if app_model.mode != AppMode.COMPLETION: raise NotCompletionAppError() @@ -226,7 +226,7 @@ class ChatStopApi(WebApiResource): } ) @web_ns.response(200, "Success", web_ns.models[SimpleResultResponse.__name__]) - def post(self, app_model, end_user, task_id): + def post(self, app_model, end_user, task_id: str): app_mode = AppMode.value_of(app_model.mode) if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}: raise NotChatAppError() diff --git a/api/controllers/web/conversation.py b/api/controllers/web/conversation.py index a99adb391f..00db29a606 100644 --- a/api/controllers/web/conversation.py +++ b/api/controllers/web/conversation.py @@ -1,4 +1,5 @@ from typing import Literal +from uuid import UUID from flask import request from pydantic import BaseModel, Field, TypeAdapter, field_validator @@ -126,7 +127,7 @@ class ConversationApi(WebApiResource): 500: "Internal Server Error", } ) - def delete(self, app_model, end_user, c_id): + def delete(self, app_model, end_user, c_id: UUID): app_mode = AppMode.value_of(app_model.mode) if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}: raise NotChatAppError() @@ -165,7 +166,7 @@ class ConversationRenameApi(WebApiResource): 500: "Internal Server Error", } ) - def post(self, app_model, end_user, c_id): + def post(self, app_model, end_user, c_id: UUID): app_mode = AppMode.value_of(app_model.mode) if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}: raise NotChatAppError() @@ -203,7 +204,7 @@ class ConversationPinApi(WebApiResource): } ) @web_ns.response(200, "Conversation pinned successfully", web_ns.models[ResultResponse.__name__]) - def patch(self, app_model, end_user, c_id): + def patch(self, app_model, end_user, c_id: UUID): app_mode = AppMode.value_of(app_model.mode) if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}: raise NotChatAppError() @@ -234,7 +235,7 @@ class ConversationUnPinApi(WebApiResource): } ) @web_ns.response(200, "Conversation unpinned successfully", web_ns.models[ResultResponse.__name__]) - def patch(self, app_model, end_user, c_id): + def patch(self, app_model, end_user, c_id: UUID): app_mode = AppMode.value_of(app_model.mode) if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}: raise NotChatAppError() diff --git a/api/controllers/web/message.py b/api/controllers/web/message.py index fc5e266c5c..cf0363b66e 100644 --- a/api/controllers/web/message.py +++ b/api/controllers/web/message.py @@ -1,5 +1,6 @@ import logging from typing import Literal +from uuid import UUID from flask import request from pydantic import BaseModel, Field, TypeAdapter @@ -132,15 +133,15 @@ class MessageFeedbackApi(WebApiResource): } ) @web_ns.response(200, "Feedback submitted successfully", web_ns.models[ResultResponse.__name__]) - def post(self, app_model, end_user, message_id): - message_id = str(message_id) + def post(self, app_model, end_user, message_id: UUID): + message_id_str = str(message_id) payload = MessageFeedbackPayload.model_validate(web_ns.payload or {}) try: MessageService.create_feedback( app_model=app_model, - message_id=message_id, + message_id=message_id_str, user=end_user, rating=FeedbackRating(payload.rating) if payload.rating else None, content=payload.content, @@ -166,11 +167,11 @@ class MessageMoreLikeThisApi(WebApiResource): 500: "Internal Server Error", } ) - def get(self, app_model, end_user, message_id): + def get(self, app_model, end_user, message_id: UUID): if app_model.mode != "completion": raise NotCompletionAppError() - message_id = str(message_id) + message_id_str = str(message_id) raw_args = request.args.to_dict() query = MessageMoreLikeThisQuery.model_validate(raw_args) @@ -181,7 +182,7 @@ class MessageMoreLikeThisApi(WebApiResource): response = AppGenerateService.generate_more_like_this( app_model=app_model, user=end_user, - message_id=message_id, + message_id=message_id_str, invoke_from=InvokeFrom.WEB_APP, streaming=streaming, ) @@ -222,16 +223,16 @@ class MessageSuggestedQuestionApi(WebApiResource): 500: "Internal Server Error", } ) - def get(self, app_model, end_user, message_id): + def get(self, app_model, end_user, message_id: UUID): app_mode = AppMode.value_of(app_model.mode) if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}: raise NotChatAppError() - message_id = str(message_id) + message_id_str = str(message_id) try: questions = MessageService.get_suggested_questions_after_answer( - app_model=app_model, user=end_user, message_id=message_id, invoke_from=InvokeFrom.WEB_APP + app_model=app_model, user=end_user, message_id=message_id_str, invoke_from=InvokeFrom.WEB_APP ) # questions is a list of strings, not a list of Message objects except MessageNotExistsError: diff --git a/api/controllers/web/saved_message.py b/api/controllers/web/saved_message.py index e307367b64..766cfc6c60 100644 --- a/api/controllers/web/saved_message.py +++ b/api/controllers/web/saved_message.py @@ -1,3 +1,5 @@ +from uuid import UUID + from flask import request from pydantic import TypeAdapter from werkzeug.exceptions import NotFound @@ -104,12 +106,12 @@ class SavedMessageApi(WebApiResource): 500: "Internal Server Error", } ) - def delete(self, app_model, end_user, message_id): - message_id = str(message_id) + def delete(self, app_model, end_user, message_id: UUID): + message_id_str = str(message_id) if app_model.mode != "completion": raise NotCompletionAppError() - SavedMessageService.delete(app_model, end_user, message_id) + SavedMessageService.delete(app_model, end_user, message_id_str) return "", 204