style: apply ruff linting and pyupgrade fixes

This commit is contained in:
SXP-Simon
2026-02-07 23:58:41 +08:00
parent 031c91940e
commit 4477c8b862
5 changed files with 21 additions and 16 deletions
+1
View File
@@ -7,6 +7,7 @@ import asyncio
from typing import Any from typing import Any
from astrbot.api import logger from astrbot.api import logger
from ...utils.resilience import CircuitBreaker, global_llm_rate_limiter from ...utils.resilience import CircuitBreaker, global_llm_rate_limiter
_circuit_breakers = {} _circuit_breakers = {}
+10 -11
View File
@@ -1,10 +1,9 @@
import base64 import base64
import logging
import asyncio
import aiohttp import aiohttp
from typing import Optional, List, Dict, Any
from astrbot.api import logger from astrbot.api import logger
from ..utils.trace_context import TraceContext from ..utils.trace_context import TraceContext
@@ -20,7 +19,7 @@ class MessageSender:
self.retry_manager = retry_manager self.retry_manager = retry_manager
async def send_text( async def send_text(
self, group_id: str, text: str, platform_id: Optional[str] = None self, group_id: str, text: str, platform_id: str | None = None
) -> bool: ) -> bool:
""" """
发送文本消息 发送文本消息
@@ -53,7 +52,7 @@ class MessageSender:
group_id: str, group_id: str,
image_url: str, image_url: str,
text_prefix: str = "", text_prefix: str = "",
platform_id: Optional[str] = None, platform_id: str | None = None,
) -> bool: ) -> bool:
""" """
发送图片 (URL 模式) 发送图片 (URL 模式)
@@ -86,7 +85,7 @@ class MessageSender:
group_id: str, group_id: str,
image_url: str, image_url: str,
text_prefix: str = "", text_prefix: str = "",
platform_id: Optional[str] = None, platform_id: str | None = None,
) -> bool: ) -> bool:
""" """
发送图片 (Base64 模式) - 需先下载图片 发送图片 (Base64 模式) - 需先下载图片
@@ -130,7 +129,7 @@ class MessageSender:
group_id: str, group_id: str,
image_url: str, image_url: str,
text_prefix: str = "", text_prefix: str = "",
platform_id: Optional[str] = None, platform_id: str | None = None,
) -> bool: ) -> bool:
""" """
智能发送图片:先尝试 URL,失败则回退到 Base64 智能发送图片:先尝试 URL,失败则回退到 Base64
@@ -150,7 +149,7 @@ class MessageSender:
group_id: str, group_id: str,
pdf_path: str, pdf_path: str,
text_prefix: str = "", text_prefix: str = "",
platform_id: Optional[str] = None, platform_id: str | None = None,
) -> bool: ) -> bool:
""" """
发送 PDF 文件 发送 PDF 文件
@@ -179,8 +178,8 @@ class MessageSender:
return False return False
def _get_available_platforms( def _get_available_platforms(
self, group_id: str, specific_platform_id: Optional[str] = None self, group_id: str, specific_platform_id: str | None = None
) -> List[tuple]: ) -> list[tuple]:
""" """
获取可用的发送平台列表 获取可用的发送平台列表
""" """
@@ -198,7 +197,7 @@ class MessageSender:
return [] return []
async def _download_image(self, url: str) -> Optional[bytes]: async def _download_image(self, url: str) -> bytes | None:
"""下载图片 helper""" """下载图片 helper"""
try: try:
timeout = aiohttp.ClientTimeout(total=15) timeout = aiohttp.ClientTimeout(total=15)
+2
View File
@@ -1,6 +1,8 @@
from collections.abc import Callable from collections.abc import Callable
from typing import Any from typing import Any
from astrbot.api import logger from astrbot.api import logger
from ..utils.trace_context import TraceContext from ..utils.trace_context import TraceContext
+2 -2
View File
@@ -1,6 +1,6 @@
import time
import asyncio import asyncio
from typing import Dict import time
from astrbot.api import logger from astrbot.api import logger
+4 -1
View File
@@ -1,11 +1,12 @@
import contextvars import contextvars
import logging import logging
import uuid
import time import time
import uuid
# 定义 ContextVar # 定义 ContextVar
_trace_id_ctx = contextvars.ContextVar("trace_id", default="") _trace_id_ctx = contextvars.ContextVar("trace_id", default="")
class TraceContext: class TraceContext:
""" """
链路追踪上下文管理器 链路追踪上下文管理器
@@ -35,10 +36,12 @@ class TraceContext:
"""清除当前上下文的 TraceID""" """清除当前上下文的 TraceID"""
_trace_id_ctx.set("") _trace_id_ctx.set("")
class TraceLogFilter(logging.Filter): class TraceLogFilter(logging.Filter):
""" """
日志过滤器,自动注入 TraceID 日志过滤器,自动注入 TraceID
""" """
def filter(self, record): def filter(self, record):
trace_id = _trace_id_ctx.get() trace_id = _trace_id_ctx.get()
if trace_id: if trace_id: