feat(chat): 实现发送错别字后自动撤回修正的功能

引入了一个新的聊天交互机制:当机器人发送了包含“错别字”的消息后,会在短暂延迟后自动撤回该消息,并发送正确的版本。此功能旨在模拟更真实的人类打字行为,增加交互的趣味性和拟人化程度。

主要变更:
- **错别字处理流程**:
  - `ResponseHandler`现在会识别出带有错别字的消息,并在发送后创建一个异步任务来处理后续的修正。
  - 新增`handle_typo_correction`方法,该方法会随机延迟2-4秒,然后调用新的`recall_message` API撤回原消息,并重新发送修正后的内容。
- **API扩展**:
  - `send_api`中增加了`recall_message`函数,用于调用适配器执行消息撤回操作。
  - `send_response`的返回值从单个字符串`reply_text`变更为元组`(reply_text, sent_messages)`,以便将已发送的消息信息(包括ID和类型)传递给上层调用者。
- **数据结构调整**:
  - `process_llm_response`的返回类型从`list[str]`调整为`list[dict[str, str]]`,以支持更复杂的响应类型,如包含原文、错别字和修正建议的`typo`类型。
- **代码优化与重构**:
  - 对`ChineseTypoGenerator`进行了大量的代码清理、注释补充和逻辑优化,使其代码更清晰、更易于维护。
  - 修复了多处代码中的类型注解和潜在的空指针问题,提高了代码的健壮性。
This commit is contained in:
minecraft1024a
2025-09-06 15:44:52 +08:00
parent 8f12dfd93c
commit fd5d951501
6 changed files with 313 additions and 201 deletions

View File

@@ -178,7 +178,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,
)
@@ -188,10 +188,13 @@ async def _send_to_target(
if reply_to_message:
anchor_message = message_dict_to_message_recv(message_dict=reply_to_message)
anchor_message.update_chat_stream(target_stream)
reply_to_platform_id = (
f"{anchor_message.message_info.platform}:{anchor_message.message_info.user_info.user_id}"
)
if anchor_message and anchor_message.message_info and anchor_message.message_info.user_info:
anchor_message.update_chat_stream(target_stream)
reply_to_platform_id = (
f"{anchor_message.message_info.platform}:{anchor_message.message_info.user_info.user_id}"
)
else:
reply_to_platform_id = None
else:
anchor_message = None
reply_to_platform_id = None
@@ -421,10 +424,10 @@ async def adapter_command_to_stream(
# 创建临时的用户信息和聊天流
temp_user_info = UserInfo(user_id="system", user_nickname="System", platform=platform)
temp_user_info = UserInfo(user_id="system", user_nickname="System", platform=platform or "qq")
temp_chat_stream = ChatStream(
stream_id=stream_id, platform=platform, user_info=temp_user_info, group_info=None
stream_id=stream_id, platform=platform or "qq", user_info=temp_user_info, group_info=None
)
target_stream = temp_chat_stream
@@ -441,7 +444,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,
)
@@ -494,3 +497,21 @@ async def adapter_command_to_stream(
logger.error(f"[SendAPI] 发送适配器命令时出错: {e}")
traceback.print_exc()
return {"status": "error", "message": f"发送适配器命令时出错: {str(e)}"}
async def recall_message(message_id: str, stream_id: str) -> bool:
"""撤回消息
Args:
message_id: 消息ID
stream_id: 聊天流ID
Returns:
bool: 是否成功
"""
response = await adapter_command_to_stream(
action="delete_msg",
params={"message_id": message_id},
stream_id=stream_id,
)
return response.get("status") == "ok"