fix:修复reply导致的异常空跳

This commit is contained in:
SengokuCola
2025-07-25 13:45:16 +08:00
parent 3495926f55
commit 5251905744
5 changed files with 21 additions and 109 deletions

View File

@@ -3,10 +3,10 @@ import sys
import os import os
from typing import Dict, List, Tuple, Optional from typing import Dict, List, Tuple, Optional
from datetime import datetime from datetime import datetime
from src.common.database.database_model import Messages, ChatStreams
# Add project root to Python path # Add project root to Python path
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root) sys.path.insert(0, project_root)
from src.common.database.database_model import Messages, ChatStreams #noqa

View File

@@ -439,93 +439,3 @@ class ActionModifier:
else: else:
logger.debug(f"{self.log_prefix}动作 {action_name} 未匹配到任何关键词: {activation_keywords}") logger.debug(f"{self.log_prefix}动作 {action_name} 未匹配到任何关键词: {activation_keywords}")
return False return False
# async def analyze_loop_actions(self, history_loop: List[CycleDetail]) -> List[tuple[str, str]]:
# """分析最近的循环内容并决定动作的移除
# Returns:
# List[Tuple[str, str]]: 包含要删除的动作及原因的元组列表
# [("action3", "some reason")]
# """
# removals = []
# # 获取最近10次循环
# recent_cycles = history_loop[-10:] if len(history_loop) > 10 else history_loop
# if not recent_cycles:
# return removals
# reply_sequence = [] # 记录最近的动作序列
# for cycle in recent_cycles:
# action_result = cycle.loop_plan_info.get("action_result", {})
# action_type = action_result.get("action_type", "unknown")
# reply_sequence.append(action_type == "reply")
# # 计算连续回复的相关阈值
# max_reply_num = int(global_config.focus_chat.consecutive_replies * 3.2)
# sec_thres_reply_num = int(global_config.focus_chat.consecutive_replies * 2)
# one_thres_reply_num = int(global_config.focus_chat.consecutive_replies * 1.5)
# # 获取最近max_reply_num次的reply状态
# if len(reply_sequence) >= max_reply_num:
# last_max_reply_num = reply_sequence[-max_reply_num:]
# else:
# last_max_reply_num = reply_sequence[:]
# # 详细打印阈值和序列信息,便于调试
# logger.info(
# f"连续回复阈值: max={max_reply_num}, sec={sec_thres_reply_num}, one={one_thres_reply_num}"
# f"最近reply序列: {last_max_reply_num}"
# )
# # print(f"consecutive_replies: {consecutive_replies}")
# # 根据最近的reply情况决定是否移除reply动作
# if len(last_max_reply_num) >= max_reply_num and all(last_max_reply_num):
# # 如果最近max_reply_num次都是reply直接移除
# reason = f"连续回复过多(最近{len(last_max_reply_num)}次全是reply超过阈值{max_reply_num}"
# removals.append(("reply", reason))
# # reply_count = len(last_max_reply_num) - no_reply_count
# elif len(last_max_reply_num) >= sec_thres_reply_num and all(last_max_reply_num[-sec_thres_reply_num:]):
# # 如果最近sec_thres_reply_num次都是reply40%概率移除
# removal_probability = 0.4 / global_config.focus_chat.consecutive_replies
# if random.random() < removal_probability:
# reason = (
# f"连续回复较多(最近{sec_thres_reply_num}次全是reply{removal_probability:.2f}概率移除,触发移除)"
# )
# removals.append(("reply", reason))
# elif len(last_max_reply_num) >= one_thres_reply_num and all(last_max_reply_num[-one_thres_reply_num:]):
# # 如果最近one_thres_reply_num次都是reply20%概率移除
# removal_probability = 0.2 / global_config.focus_chat.consecutive_replies
# if random.random() < removal_probability:
# reason = (
# f"连续回复检测(最近{one_thres_reply_num}次全是reply{removal_probability:.2f}概率移除,触发移除)"
# )
# removals.append(("reply", reason))
# else:
# logger.debug(f"{self.log_prefix}连续回复检测无需移除reply动作最近回复模式正常")
# return removals
# def get_available_actions_count(self, mode: str = "focus") -> int:
# """获取当前可用动作数量排除默认的no_action"""
# current_actions = self.action_manager.get_using_actions_for_mode(mode)
# # 排除no_action如果存在
# filtered_actions = {k: v for k, v in current_actions.items() if k != "no_action"}
# return len(filtered_actions)
# def should_skip_planning_for_no_reply(self) -> bool:
# """判断是否应该跳过规划过程"""
# current_actions = self.action_manager.get_using_actions_for_mode("focus")
# # 排除no_action如果存在
# if len(current_actions) == 1 and "no_reply" in current_actions:
# return True
# return False
# def should_skip_planning_for_no_action(self) -> bool:
# """判断是否应该跳过规划过程"""
# available_count = self.action_manager.get_using_actions_for_mode("normal")
# if available_count == 0:
# logger.debug(f"{self.log_prefix} 没有可用动作,跳过规划")
# return True
# return False

View File

@@ -40,7 +40,6 @@ def init_prompt():
{moderation_prompt} {moderation_prompt}
现在请你根据{by_what}选择合适的action和触发action的消息: 现在请你根据{by_what}选择合适的action和触发action的消息:
你刚刚选择并执行过的action是
{actions_before_now_block} {actions_before_now_block}
{no_action_block} {no_action_block}
@@ -130,17 +129,18 @@ class ActionPlanner:
logger.warning(f"{self.log_prefix}使用中的动作 {action_name} 未在已注册动作中找到") logger.warning(f"{self.log_prefix}使用中的动作 {action_name} 未在已注册动作中找到")
# 如果没有可用动作或只有no_reply动作直接返回no_reply # 如果没有可用动作或只有no_reply动作直接返回no_reply
if not current_available_actions: # 因为现在reply是永远激活所以不需要空跳判定
action = "no_reply" if mode == ChatMode.FOCUS else "no_action" # if not current_available_actions:
reasoning = "没有可用的动作" # action = "no_reply" if mode == ChatMode.FOCUS else "no_action"
logger.info(f"{self.log_prefix}{reasoning}") # reasoning = "没有可用的动作"
return { # logger.info(f"{self.log_prefix}{reasoning}")
"action_result": { # return {
"action_type": action, # "action_result": {
"action_data": action_data, # "action_type": action,
"reasoning": reasoning, # "action_data": action_data,
}, # "reasoning": reasoning,
}, None # },
# }, None
# --- 构建提示词 (调用修改后的 PromptBuilder 方法) --- # --- 构建提示词 (调用修改后的 PromptBuilder 方法) ---
prompt, message_id_list = await self.build_planner_prompt( prompt, message_id_list = await self.build_planner_prompt(
@@ -268,6 +268,7 @@ class ActionPlanner:
actions_before_now = get_actions_by_timestamp_with_chat( actions_before_now = get_actions_by_timestamp_with_chat(
chat_id=self.chat_id, chat_id=self.chat_id,
timestamp_start=time.time()-3600,
timestamp_end=time.time(), timestamp_end=time.time(),
limit=5, limit=5,
) )
@@ -276,6 +277,8 @@ class ActionPlanner:
actions=actions_before_now, actions=actions_before_now,
) )
actions_before_now_block = f"你刚刚选择并执行过的action是\n{actions_before_now_block}"
self.last_obs_time_mark = time.time() self.last_obs_time_mark = time.time()
if mode == ChatMode.FOCUS: if mode == ChatMode.FOCUS:
@@ -288,7 +291,7 @@ class ActionPlanner:
by_what = "聊天内容" by_what = "聊天内容"
target_prompt = '\n "target_message_id":"触发action的消息id"' target_prompt = '\n "target_message_id":"触发action的消息id"'
no_action_block = f"""重要说明1 no_action_block = f"""重要说明:
- 'no_reply' 表示只进行不进行回复,等待合适的回复时机 - 'no_reply' 表示只进行不进行回复,等待合适的回复时机
- 当你刚刚发送了消息没有人回复时选择no_reply - 当你刚刚发送了消息没有人回复时选择no_reply
- 当你一次发送了太多消息为了避免打扰聊天节奏选择no_reply - 当你一次发送了太多消息为了避免打扰聊天节奏选择no_reply

View File

@@ -419,7 +419,7 @@ class RelationshipBuilder:
async def update_impression_on_segments(self, person_id: str, chat_id: str, segments: List[Dict[str, Any]]): async def update_impression_on_segments(self, person_id: str, chat_id: str, segments: List[Dict[str, Any]]):
"""基于消息段更新用户印象""" """基于消息段更新用户印象"""
original_segment_count = len(segments) original_segment_count = len(segments)
logger.info(f"开始为 {person_id} 基于 {original_segment_count} 个消息段更新印象") logger.debug(f"开始为 {person_id} 基于 {original_segment_count} 个消息段更新印象")
try: try:
# 筛选要处理的消息段每个消息段有10%的概率被丢弃 # 筛选要处理的消息段每个消息段有10%的概率被丢弃
segments_to_process = [s for s in segments if random.random() >= 0.1] segments_to_process = [s for s in segments if random.random() >= 0.1]
@@ -432,7 +432,7 @@ class RelationshipBuilder:
dropped_count = original_segment_count - len(segments_to_process) dropped_count = original_segment_count - len(segments_to_process)
if dropped_count > 0: if dropped_count > 0:
logger.info(f"{person_id} 随机丢弃了 {dropped_count} / {original_segment_count} 个消息段") logger.debug(f"{person_id} 随机丢弃了 {dropped_count} / {original_segment_count} 个消息段")
processed_messages = [] processed_messages = []

View File

@@ -20,8 +20,7 @@ class EmojiAction(BaseAction):
"""表情动作 - 发送表情包""" """表情动作 - 发送表情包"""
# 激活设置 # 激活设置
focus_activation_type = ActionActivationType.RANDOM activation_type = ActionActivationType.RANDOM
normal_activation_type = ActionActivationType.RANDOM
mode_enable = ChatMode.ALL mode_enable = ChatMode.ALL
parallel_action = True parallel_action = True
random_activation_probability = 0.2 # 默认值,可通过配置覆盖 random_activation_probability = 0.2 # 默认值,可通过配置覆盖