committed by
Windpicker-owo
parent
086a7322b3
commit
796daf8ddc
@@ -71,12 +71,7 @@ class CycleProcessor:
|
||||
"""
|
||||
# 发送回复
|
||||
with Timer("回复发送", cycle_timers):
|
||||
reply_text, sent_messages = await self.response_handler.send_response(
|
||||
response_set, loop_start_time, action_message
|
||||
)
|
||||
if sent_messages:
|
||||
# 异步处理错别字修正
|
||||
asyncio.create_task(self.response_handler.handle_typo_correction(sent_messages))
|
||||
reply_text = await self.response_handler.send_response(response_set, loop_start_time, action_message)
|
||||
|
||||
# 存储reply action信息
|
||||
person_info_manager = get_person_info_manager()
|
||||
@@ -185,8 +180,7 @@ class CycleProcessor:
|
||||
cycle_timers, thinking_id = self.cycle_tracker.start_cycle()
|
||||
logger.info(f"{self.log_prefix} 开始第{self.context.cycle_counter}次思考")
|
||||
|
||||
# 发送正在输入状态
|
||||
if ENABLE_S4U and self.context.chat_stream and self.context.chat_stream.user_info:
|
||||
if ENABLE_S4U:
|
||||
await send_typing(self.context.chat_stream.user_info.user_id)
|
||||
|
||||
loop_start_time = time.time()
|
||||
@@ -214,7 +208,7 @@ class CycleProcessor:
|
||||
result = await event_manager.trigger_event(
|
||||
EventType.ON_PLAN, plugin_name="SYSTEM", stream_id=self.context.chat_stream
|
||||
)
|
||||
if result and not result.all_continue_process():
|
||||
if not result.all_continue_process():
|
||||
raise UserWarning(f"插件{result.get_summary().get('stopped_handlers', '')}于规划前中断了内容生成")
|
||||
|
||||
# 规划动作
|
||||
@@ -427,7 +421,7 @@ class CycleProcessor:
|
||||
if fallback_action and fallback_action != action:
|
||||
logger.info(f"{self.context.log_prefix} 使用回退动作: {fallback_action}")
|
||||
action_handler = self.context.action_manager.create_action(
|
||||
action_name=str(fallback_action),
|
||||
action_name=fallback_action if isinstance(fallback_action, list) else fallback_action,
|
||||
action_data=action_data,
|
||||
reasoning=f"原动作'{action}'不可用,自动回退。{reasoning}",
|
||||
cycle_timers=cycle_timers,
|
||||
|
||||
@@ -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