feat: 为多个模块添加类型注解以增强代码可读性

为 ConnectionHandler 相关的函数参数添加类型注解,使用 TYPE_CHECKING 避免循环导入。主要修改包括:
- 在 abortHandle、textHandle 等处理模块中为 conn 参数添加 ConnectionHandler 类型注解
- 在 websocket_server、connection 等核心模块中为方法参数添加类型注解
- 在 plugins_func 下的多个功能模块中为函数参数添加类型注解
- 在 providers 相关模块中为工具执行器和方法添加类型注解
- 统一代码格式,如将单引号字符串改为双引号

Fixes #2034
This commit is contained in:
huozaimengli
2026-01-25 17:47:52 +08:00
parent 275102f5b7
commit 4b573fb4e2
36 changed files with 338 additions and 164 deletions
@@ -4,7 +4,10 @@
"""
import os
from typing import Dict, Any
from typing import Dict, Any, TYPE_CHECKING
if TYPE_CHECKING:
from core.connection import ConnectionHandler
from config.logger import setup_logging
from jinja2 import Template
@@ -59,9 +62,10 @@ class PromptManager:
self.cache_manager = cache_manager
self.CacheType = CacheType
# 初始化上下文源
from core.utils.context_provider import ContextDataProvider
self.context_provider = ContextDataProvider(config, self.logger)
self.context_data = {}
@@ -157,7 +161,7 @@ class PromptManager:
self.logger.bind(tag=TAG).error(f"获取位置信息失败: {e}")
return "未知位置"
def _get_weather_info(self, conn, location: str) -> str:
def _get_weather_info(self, conn: "ConnectionHandler", location: str) -> str:
"""获取天气信息"""
try:
# 先从缓存获取
@@ -203,14 +207,17 @@ class PromptManager:
):
# 获取天气信息(使用全局缓存)
self._get_weather_info(conn, local_address)
# 获取配置的上下文数据
if hasattr(conn, "device_id") and conn.device_id:
if self.base_prompt_template and "dynamic_context" in self.base_prompt_template:
if (
self.base_prompt_template
and "dynamic_context" in self.base_prompt_template
):
self.context_data = self.context_provider.fetch_all(conn.device_id)
else:
self.context_data = ""
self.logger.bind(tag=TAG).debug(f"上下文信息更新完成")
except Exception as e:
+6 -2
View File
@@ -1,4 +1,8 @@
import json
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from core.connection import ConnectionHandler
TAG = __name__
EMOJI_MAP = {
@@ -77,7 +81,7 @@ def is_punctuation_or_emoji(char):
return is_emoji(char)
async def get_emotion(conn, text):
async def get_emotion(conn: "ConnectionHandler", text):
"""获取文本内的情绪消息"""
emoji = "🙂"
emotion = "happy"
@@ -110,4 +114,4 @@ def is_emoji(char):
def check_emoji(text):
"""去除文本中的所有emoji表情"""
return ''.join(char for char in text if not is_emoji(char) and char != "\n")
return "".join(char for char in text if not is_emoji(char) and char != "\n")