This commit is contained in:
Windpicker-owo
2025-10-30 17:35:29 +08:00
13 changed files with 200 additions and 55 deletions

View File

@@ -104,6 +104,23 @@ class ChatterPlanFilter:
# 预解析 action_type 来进行判断
thinking = item.get("thinking", "未提供思考过程")
actions_obj = item.get("actions", {})
# 记录决策历史
if hasattr(global_config.chat, "enable_decision_history") and global_config.chat.enable_decision_history:
action_types_to_log = []
actions_to_process_for_log = []
if isinstance(actions_obj, dict):
actions_to_process_for_log.append(actions_obj)
elif isinstance(actions_obj, list):
actions_to_process_for_log.extend(actions_obj)
for single_action in actions_to_process_for_log:
if isinstance(single_action, dict):
action_types_to_log.append(single_action.get("action_type", "no_action"))
if thinking != "未提供思考过程" and action_types_to_log:
await self._add_decision_to_history(plan, thinking, ", ".join(action_types_to_log))
# 处理actions字段可能是字典或列表的情况
if isinstance(actions_obj, dict):
@@ -141,6 +158,65 @@ class ChatterPlanFilter:
return plan
async def _add_decision_to_history(self, plan: Plan, thought: str, action: str):
"""添加决策记录到历史中"""
try:
from src.chat.message_receive.chat_stream import get_chat_manager
from src.common.data_models.message_manager_data_model import DecisionRecord
chat_manager = get_chat_manager()
chat_stream = await chat_manager.get_stream(plan.chat_id)
if not chat_stream:
return
if not thought or not action:
logger.debug("尝试添加空的决策历史,已跳过")
return
context = chat_stream.context_manager.context
new_record = DecisionRecord(thought=thought, action=action)
# 添加新记录
context.decision_history.append(new_record)
# 获取历史长度限制
max_history_length = getattr(global_config.chat, "decision_history_length", 3)
# 如果历史记录超过长度,则移除最旧的记录
if len(context.decision_history) > max_history_length:
context.decision_history.pop(0)
logger.debug(f"已添加决策历史,当前长度: {len(context.decision_history)}")
except Exception as e:
logger.warning(f"记录决策历史失败: {e}")
async def _build_decision_history_block(self, plan: Plan) -> str:
"""构建决策历史块"""
if not hasattr(global_config.chat, "enable_decision_history") or not global_config.chat.enable_decision_history:
return ""
try:
from src.chat.message_receive.chat_stream import get_chat_manager
chat_manager = get_chat_manager()
chat_stream = await chat_manager.get_stream(plan.chat_id)
if not chat_stream:
return ""
context = chat_stream.context_manager.context
if not context.decision_history:
return ""
history_records = []
for i, record in enumerate(context.decision_history):
history_records.append(f"- 思考: {record.thought}\n - 动作: {record.action}")
history_str = "\n".join(history_records)
return f"{history_str}"
except Exception as e:
logger.warning(f"构建决策历史块失败: {e}")
return ""
async def _build_prompt(self, plan: Plan) -> tuple[str, list]:
"""
根据 Plan 对象构建提示词。
@@ -166,6 +242,9 @@ class ChatterPlanFilter:
chat_mood = mood_manager.get_mood_by_chat_id(plan.chat_id)
mood_block = f"你现在的心情是:{chat_mood.mood_state}"
# 构建决策历史
decision_history_block = await self._build_decision_history_block(plan)
# 构建已读/未读历史消息
read_history_block, unread_history_block, message_id_list = await self._build_read_unread_history_blocks(
plan
@@ -239,6 +318,7 @@ class ChatterPlanFilter:
mood_block=mood_block,
time_block=time_block,
chat_context_description=chat_context_description,
decision_history_block=decision_history_block,
read_history_block=read_history_block,
unread_history_block=unread_history_block,
actions_before_now_block=actions_before_now_block,

View File

@@ -30,6 +30,9 @@ def init_prompts():
{actions_before_now_block}
## 🤔 最近的决策历史 (回顾你之前的思考与动作,可以帮助你避免重复,并做出更有趣的连贯回应)
{decision_history_block}
## 📜 已读历史(仅供理解,不可作为动作对象)
{read_history_block}

View File

@@ -719,12 +719,16 @@ class MessageHandler:
reply_message = [Seg(type="text", data="(获取发言内容失败)")]
sender_info: dict = message_detail.get("sender")
sender_nickname: str = sender_info.get("nickname")
sender_id = sender_info.get("user_id")
seg_message: List[Seg] = []
if not sender_nickname:
logger.warning("无法获取被引用的人的昵称,返回默认值")
seg_message.append(Seg(type="text", data="[回复 未知用户:"))
else:
seg_message.append(Seg(type="text", data=f"[回复<{sender_nickname}>"))
if sender_id:
seg_message.append(Seg(type="text", data=f"[回复<{sender_nickname}({sender_id})>"))
else:
seg_message.append(Seg(type="text", data=f"[回复<{sender_nickname}>"))
seg_message += reply_message
seg_message.append(Seg(type="text", data="],说:"))
return seg_message