chore: add UUID/str type annotations to api endpoints for files in api/controllers/files and api/controllers/web (#36562)

This commit is contained in:
Lillian
2026-05-24 15:58:10 +08:00
committed by GitHub
parent 5645ea0def
commit 7b1aa33ad4
6 changed files with 39 additions and 31 deletions

View File

@@ -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:

View File

@@ -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:

View File

@@ -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()

View File

@@ -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()

View File

@@ -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:

View File

@@ -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