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 astrbot.api import logger
from ...utils.resilience import CircuitBreaker, global_llm_rate_limiter
_circuit_breakers = {}
+10 -11
View File
@@ -1,10 +1,9 @@
import base64
import logging
import asyncio
import aiohttp
from typing import Optional, List, Dict, Any
from astrbot.api import logger
from ..utils.trace_context import TraceContext
@@ -20,7 +19,7 @@ class MessageSender:
self.retry_manager = retry_manager
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:
"""
发送文本消息
@@ -53,7 +52,7 @@ class MessageSender:
group_id: str,
image_url: str,
text_prefix: str = "",
platform_id: Optional[str] = None,
platform_id: str | None = None,
) -> bool:
"""
发送图片 (URL 模式)
@@ -86,7 +85,7 @@ class MessageSender:
group_id: str,
image_url: str,
text_prefix: str = "",
platform_id: Optional[str] = None,
platform_id: str | None = None,
) -> bool:
"""
发送图片 (Base64 模式) - 需先下载图片
@@ -130,7 +129,7 @@ class MessageSender:
group_id: str,
image_url: str,
text_prefix: str = "",
platform_id: Optional[str] = None,
platform_id: str | None = None,
) -> bool:
"""
智能发送图片:先尝试 URL,失败则回退到 Base64
@@ -150,7 +149,7 @@ class MessageSender:
group_id: str,
pdf_path: str,
text_prefix: str = "",
platform_id: Optional[str] = None,
platform_id: str | None = None,
) -> bool:
"""
发送 PDF 文件
@@ -179,8 +178,8 @@ class MessageSender:
return False
def _get_available_platforms(
self, group_id: str, specific_platform_id: Optional[str] = None
) -> List[tuple]:
self, group_id: str, specific_platform_id: str | None = None
) -> list[tuple]:
"""
获取可用的发送平台列表
"""
@@ -198,7 +197,7 @@ class MessageSender:
return []
async def _download_image(self, url: str) -> Optional[bytes]:
async def _download_image(self, url: str) -> bytes | None:
"""下载图片 helper"""
try:
timeout = aiohttp.ClientTimeout(total=15)
+2
View File
@@ -1,6 +1,8 @@
from collections.abc import Callable
from typing import Any
from astrbot.api import logger
from ..utils.trace_context import TraceContext
+2 -2
View File
@@ -1,6 +1,6 @@
import time
import asyncio
from typing import Dict
import time
from astrbot.api import logger
+6 -3
View File
@@ -1,16 +1,17 @@
import contextvars
import logging
import uuid
import time
import uuid
# 定义 ContextVar
_trace_id_ctx = contextvars.ContextVar("trace_id", default="")
class TraceContext:
"""
链路追踪上下文管理器
"""
@staticmethod
def set(trace_id: str):
"""设置当前上下文的 TraceID"""
@@ -20,7 +21,7 @@ class TraceContext:
def get() -> str:
"""获取当前上下文的 TraceID"""
return _trace_id_ctx.get()
@staticmethod
def generate(prefix: str = "") -> str:
"""生成一个新的 TraceID (Prefix + Timestamp + UUID前8位)"""
@@ -35,10 +36,12 @@ class TraceContext:
"""清除当前上下文的 TraceID"""
_trace_id_ctx.set("")
class TraceLogFilter(logging.Filter):
"""
日志过滤器,自动注入 TraceID
"""
def filter(self, record):
trace_id = _trace_id_ctx.get()
if trace_id: