@@ -1,6 +1,5 @@
|
||||
import time
|
||||
import random
|
||||
import asyncio
|
||||
from typing import Dict, Any, Tuple
|
||||
|
||||
from src.common.logger import get_logger
|
||||
@@ -64,11 +63,7 @@ class ResponseHandler:
|
||||
- 构建并返回完整的循环信息
|
||||
- 用于上级方法的状态跟踪
|
||||
"""
|
||||
# 发送回复
|
||||
reply_text, sent_messages = await self.send_response(response_set, loop_start_time, action_message)
|
||||
if sent_messages:
|
||||
# 异步处理错别字修正
|
||||
asyncio.create_task(self.handle_typo_correction(sent_messages))
|
||||
reply_text = await self.send_response(response_set, loop_start_time, action_message)
|
||||
|
||||
person_info_manager = get_person_info_manager()
|
||||
|
||||
@@ -113,17 +108,18 @@ class ResponseHandler:
|
||||
|
||||
return loop_info, reply_text, cycle_timers
|
||||
|
||||
async def send_response(self, reply_set, thinking_start_time, message_data) -> tuple[str, list[dict[str, str]]]:
|
||||
async def send_response(self, reply_set, thinking_start_time, message_data) -> str:
|
||||
"""
|
||||
发送回复内容的具体实现
|
||||
|
||||
Args:
|
||||
reply_set: 回复内容集合,包含多个回复段
|
||||
reply_to: 回复目标
|
||||
thinking_start_time: 思考开始时间
|
||||
message_data: 消息数据
|
||||
|
||||
Returns:
|
||||
tuple[str, list[dict[str, str]]]: (完整的回复文本, 已发送消息列表)
|
||||
str: 完整的回复文本
|
||||
|
||||
功能说明:
|
||||
- 检查是否有新消息需要回复
|
||||
@@ -142,18 +138,19 @@ class ResponseHandler:
|
||||
need_reply = new_message_count >= random.randint(2, 4)
|
||||
|
||||
reply_text = ""
|
||||
sent_messages = []
|
||||
is_proactive_thinking = message_data.get("message_type") == "proactive_thinking"
|
||||
|
||||
first_replied = False
|
||||
for reply_seg in reply_set:
|
||||
# 调试日志:验证reply_seg的格式
|
||||
logger.debug(f"Processing reply_seg type: {type(reply_seg)}, content: {reply_seg}")
|
||||
|
||||
# 提取回复内容
|
||||
if reply_seg["type"] == "typo":
|
||||
data = reply_seg["typo"]
|
||||
# 修正:正确处理元组格式 (格式为: (type, content))
|
||||
if isinstance(reply_seg, tuple) and len(reply_seg) >= 2:
|
||||
_, data = reply_seg
|
||||
else:
|
||||
data = reply_seg["content"]
|
||||
# 向下兼容:如果已经是字符串,则直接使用
|
||||
data = str(reply_seg)
|
||||
|
||||
if isinstance(data, list):
|
||||
data = "".join(map(str, data))
|
||||
@@ -166,7 +163,7 @@ class ResponseHandler:
|
||||
|
||||
# 发送第一段回复
|
||||
if not first_replied:
|
||||
sent_message = await send_api.text_to_stream(
|
||||
await send_api.text_to_stream(
|
||||
text=data,
|
||||
stream_id=self.context.stream_id,
|
||||
reply_to_message=message_data,
|
||||
@@ -183,29 +180,5 @@ class ResponseHandler:
|
||||
set_reply=False,
|
||||
typing=True,
|
||||
)
|
||||
# 记录已发送的错别字消息
|
||||
if sent_message and reply_seg["type"] == "typo":
|
||||
sent_messages.append(
|
||||
{
|
||||
"type": "typo",
|
||||
"message_id": sent_message,
|
||||
"original_message": message_data,
|
||||
"correction": reply_seg["correction"],
|
||||
}
|
||||
)
|
||||
|
||||
return reply_text, sent_messages
|
||||
|
||||
async def handle_typo_correction(self, sent_messages: list[dict[str, Any]]):
|
||||
"""处理错别字修正"""
|
||||
for msg in sent_messages:
|
||||
if msg["type"] == "typo":
|
||||
# 随机等待一段时间
|
||||
await asyncio.sleep(random.uniform(2, 4))
|
||||
# 撤回消息
|
||||
recalled = await send_api.recall_message(str(msg["message_id"]), self.context.stream_id)
|
||||
if recalled:
|
||||
# 发送修正后的消息
|
||||
await send_api.text_to_stream(
|
||||
str(msg["correction"]), self.context.stream_id, reply_to_message=msg["original_message"]
|
||||
)
|
||||
return reply_text
|
||||
|
||||
Reference in New Issue
Block a user