refactor(core): 简化回复清理并加强 API 调用此次提交彻底重构了回复清理逻辑,以提升可维护性,并在发送 API 中引入了多项防御性检查,以防止常见的运行时错误。通过将复杂的迭代清理算法替换为单一且更高效的正则表达式,回复生成过程得到了简化。这不仅提高了去除模型生成的头信息的可靠性,还显著降低了代码复杂性。在发送 API 中,实现了多个安全措施:- 修正了异步处理错误,通过移除同步字典操作上无效的 await,防止出现 TypeError。- 添加了预检验证,以确保文件上传有定义的目标,并且临时流指定了平台。- 现在一致将机器人用户 ID 转换为字符串,以消除潜在的类型相关不一致。最后,为了清晰起见,机器人的行为原则术语也进行了调整。

This commit is contained in:
tt-P607
2025-11-09 00:50:23 +08:00
parent d007b98f5c
commit 130a0fdb31
3 changed files with 24 additions and 37 deletions

View File

@@ -62,12 +62,15 @@ async def file_to_stream(
}
action = ""
if target_stream.group_info:
if target_stream.group_info and target_stream.group_info.group_id:
action = "upload_group_file"
params["group_id"] = target_stream.group_info.group_id
else:
elif target_stream.user_info and target_stream.user_info.user_id:
action = "upload_private_file"
params["user_id"] = target_stream.user_info.user_id
else:
logger.error(f"[SendAPI] 无法确定文件发送目标: {stream_id}")
return False
response = await adapter_command_to_stream(
action=action,
@@ -173,10 +176,10 @@ async def wait_adapter_response(request_id: str, timeout: float = 30.0) -> dict:
response = await asyncio.wait_for(future, timeout=timeout)
return response
except asyncio.TimeoutError:
await _adapter_response_pool.pop(request_id, None)
_adapter_response_pool.pop(request_id, None)
return {"status": "error", "message": "timeout"}
except Exception as e:
await _adapter_response_pool.pop(request_id, None)
_adapter_response_pool.pop(request_id, None)
return {"status": "error", "message": str(e)}
@@ -234,7 +237,7 @@ async def _send_to_target(
# 构建机器人用户信息
bot_user_info = UserInfo(
user_id=global_config.bot.qq_account,
user_id=str(global_config.bot.qq_account),
user_nickname=global_config.bot.nickname,
platform=target_stream.platform,
)
@@ -499,6 +502,9 @@ async def adapter_command_to_stream(
logger.debug(f"[SendAPI] 创建临时虚拟聊天流: {stream_id}")
# 创建临时的用户信息和聊天流
if not platform:
logger.error("[SendAPI] 创建临时聊天流失败: platform 未提供")
return {"status": "error", "message": "platform 未提供"}
temp_user_info = UserInfo(user_id="system", user_nickname="System", platform=platform)
@@ -520,7 +526,7 @@ async def adapter_command_to_stream(
# 构建机器人用户信息
bot_user_info = UserInfo(
user_id=global_config.bot.qq_account,
user_id=str(global_config.bot.qq_account),
user_nickname=global_config.bot.nickname,
platform=target_stream.platform,
)