From 993bf7c2c87eba98b86f5113dda7cdc14038523d Mon Sep 17 00:00:00 2001 From: Jin Hai Date: Mon, 22 Dec 2025 16:47:21 +0800 Subject: [PATCH] Fix IDE warnings (#12085) ### What problem does this PR solve? As title ### Type of change - [x] Refactoring Signed-off-by: Jin Hai --- api/apps/__init__.py | 2 +- api/apps/mcp_server_app.py | 2 -- api/apps/sdk/chat.py | 6 +++--- api/apps/sdk/files.py | 13 +++++++++---- api/apps/sdk/session.py | 10 +++++++--- api/apps/user_app.py | 2 +- common/constants.py | 1 + 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/api/apps/__init__.py b/api/apps/__init__.py index 63de1fbf3..6934e7aed 100644 --- a/api/apps/__init__.py +++ b/api/apps/__init__.py @@ -108,7 +108,7 @@ def _load_user(): authorization = request.headers.get("Authorization") g.user = None if not authorization: - return + return None try: access_token = str(jwt.loads(authorization)) diff --git a/api/apps/mcp_server_app.py b/api/apps/mcp_server_app.py index 2c656df18..62ae2e3c0 100644 --- a/api/apps/mcp_server_app.py +++ b/api/apps/mcp_server_app.py @@ -326,7 +326,6 @@ async def list_tools() -> Response: try: tools = await asyncio.to_thread(tool_call_session.get_tools, timeout) except Exception as e: - tools = [] return get_data_error_result(message=f"MCP list tools error: {e}") results[server_key] = [] @@ -428,7 +427,6 @@ async def test_mcp() -> Response: try: tools = await asyncio.to_thread(tool_call_session.get_tools, timeout) except Exception as e: - tools = [] return get_data_error_result(message=f"Test MCP error: {e}") finally: # PERF: blocking call to close sessions — consider moving to background thread or task queue diff --git a/api/apps/sdk/chat.py b/api/apps/sdk/chat.py index fb426b0f0..1efb628f1 100644 --- a/api/apps/sdk/chat.py +++ b/api/apps/sdk/chat.py @@ -287,7 +287,7 @@ def list_chat(tenant_id): chats = DialogService.get_list(tenant_id, page_number, items_per_page, orderby, desc, id, name) if not chats: return get_result(data=[]) - list_assts = [] + list_assistants = [] key_mapping = { "parameters": "variables", "prologue": "opener", @@ -321,5 +321,5 @@ def list_chat(tenant_id): del res["kb_ids"] res["datasets"] = kb_list res["avatar"] = res.pop("icon") - list_assts.append(res) - return get_result(data=list_assts) + list_assistants.append(res) + return get_result(data=list_assistants) diff --git a/api/apps/sdk/files.py b/api/apps/sdk/files.py index 9587d0f15..a61877788 100644 --- a/api/apps/sdk/files.py +++ b/api/apps/sdk/files.py @@ -205,7 +205,8 @@ async def create(tenant_id): if not FileService.is_parent_folder_exist(pf_id): return get_json_result(data=False, message="Parent Folder Doesn't Exist!", code=RetCode.BAD_REQUEST) if FileService.query(name=req["name"], parent_id=pf_id): - return get_json_result(data=False, message="Duplicated folder name in the same folder.", code=409) + return get_json_result(data=False, message="Duplicated folder name in the same folder.", + code=RetCode.CONFLICT) if input_file_type == FileType.FOLDER.value: file_type = FileType.FOLDER.value @@ -565,11 +566,13 @@ async def rename(tenant_id): if file.type != FileType.FOLDER.value and pathlib.Path(req["name"].lower()).suffix != pathlib.Path( file.name.lower()).suffix: - return get_json_result(data=False, message="The extension of file can't be changed", code=RetCode.BAD_REQUEST) + return get_json_result(data=False, message="The extension of file can't be changed", + code=RetCode.BAD_REQUEST) for existing_file in FileService.query(name=req["name"], pf_id=file.parent_id): if existing_file.name == req["name"]: - return get_json_result(data=False, message="Duplicated file name in the same folder.", code=409) + return get_json_result(data=False, message="Duplicated file name in the same folder.", + code=RetCode.CONFLICT) if not FileService.update_by_id(req["file_id"], {"name": req["name"]}): return get_json_result(message="Database error (File rename)!", code=RetCode.SERVER_ERROR) @@ -631,9 +634,10 @@ async def get(tenant_id, file_id): except Exception as e: return server_error_response(e) + @manager.route("/file/download/", methods=["GET"]) # noqa: F821 @token_required -async def download_attachment(tenant_id,attachment_id): +async def download_attachment(tenant_id, attachment_id): try: ext = request.args.get("ext", "markdown") data = await asyncio.to_thread(settings.STORAGE_IMPL.get, tenant_id, attachment_id) @@ -645,6 +649,7 @@ async def download_attachment(tenant_id,attachment_id): except Exception as e: return server_error_response(e) + @manager.route('/file/mv', methods=['POST']) # noqa: F821 @token_required async def move(tenant_id): diff --git a/api/apps/sdk/session.py b/api/apps/sdk/session.py index e4db54216..ef83ad24e 100644 --- a/api/apps/sdk/session.py +++ b/api/apps/sdk/session.py @@ -448,7 +448,7 @@ async def chat_completion_openai_like(tenant_id, chat_id): @token_required async def agents_completion_openai_compatibility(tenant_id, agent_id): req = await get_request_json() - tiktokenenc = tiktoken.get_encoding("cl100k_base") + tiktoken_encode = tiktoken.get_encoding("cl100k_base") messages = req.get("messages", []) if not messages: return get_error_data_result("You must provide at least one message.") @@ -456,7 +456,7 @@ async def agents_completion_openai_compatibility(tenant_id, agent_id): return get_error_data_result(f"You don't own the agent {agent_id}") filtered_messages = [m for m in messages if m["role"] in ["user", "assistant"]] - prompt_tokens = sum(len(tiktokenenc.encode(m["content"])) for m in filtered_messages) + prompt_tokens = sum(len(tiktoken_encode.encode(m["content"])) for m in filtered_messages) if not filtered_messages: return jsonify( get_data_openai( @@ -464,7 +464,7 @@ async def agents_completion_openai_compatibility(tenant_id, agent_id): content="No valid messages found (user or assistant).", finish_reason="stop", model=req.get("model", ""), - completion_tokens=len(tiktokenenc.encode("No valid messages found (user or assistant).")), + completion_tokens=len(tiktoken_encode.encode("No valid messages found (user or assistant).")), prompt_tokens=prompt_tokens, ) ) @@ -501,6 +501,8 @@ async def agents_completion_openai_compatibility(tenant_id, agent_id): ): return jsonify(response) + return None + @manager.route("/agents//completions", methods=["POST"]) # noqa: F821 @token_required @@ -920,6 +922,7 @@ async def chatbot_completions(dialog_id): async for answer in iframe_completion(dialog_id, **req): return get_result(data=answer) + return None @manager.route("/chatbots//info", methods=["GET"]) # noqa: F821 async def chatbots_inputs(dialog_id): @@ -967,6 +970,7 @@ async def agent_bot_completions(agent_id): async for answer in agent_completion(objs[0].tenant_id, agent_id, **req): return get_result(data=answer) + return None @manager.route("/agentbots//inputs", methods=["GET"]) # noqa: F821 async def begin_inputs(agent_id): diff --git a/api/apps/user_app.py b/api/apps/user_app.py index 50c0b4d76..e1ad157bc 100644 --- a/api/apps/user_app.py +++ b/api/apps/user_app.py @@ -660,7 +660,7 @@ def user_register(user_id, user): tenant_llm = get_init_tenant_llm(user_id) if not UserService.save(**user): - return + return None TenantService.insert(**tenant) UserTenantService.insert(**usr_tenant) TenantLLMService.insert_many(tenant_llm) diff --git a/common/constants.py b/common/constants.py index 490a5a4e3..7bd00c3b0 100644 --- a/common/constants.py +++ b/common/constants.py @@ -54,6 +54,7 @@ class RetCode(IntEnum, CustomEnum): SERVER_ERROR = 500 FORBIDDEN = 403 NOT_FOUND = 404 + CONFLICT = 409 class StatusEnum(Enum):