feat:动作现在区分focus和normal,并且可选不同的激活策略

This commit is contained in:
SengokuCola
2025-06-09 15:10:38 +08:00
parent 2ce5114b8c
commit 97ffbe5145
25 changed files with 1180 additions and 855 deletions

View File

@@ -304,7 +304,7 @@ class ExpressionLearner:
# 如果没选够,随机补充
if len(remove_set) < remove_count:
remaining = set(indices) - remove_set
remove_set.update(random.sample(remaining, remove_count - len(remove_set)))
remove_set.update(random.sample(list(remaining), remove_count - len(remove_set)))
remove_indices = list(remove_set)

View File

@@ -33,8 +33,8 @@ def init_prompt():
</调取记录>
{name_block}
请你阅读聊天记录,查看是否需要调取某个人的信息。
你不同程度上认识群聊里的人,你可以根据聊天记录,回忆起有关他们的信息,帮助你参与聊天
请你阅读聊天记录,查看是否需要调取某个人的信息,这个人可以是出现在聊天记录中的,也可以是记录中提到的人
你不同程度上认识群聊里的人,以及他们谈论到的人,你可以根据聊天记录,回忆起有关他们的信息,帮助你参与聊天
1.你需要提供用户名,以及你想要提取的信息名称类型来进行调取
2.你也可以完全不输出任何信息
3.阅读调取记录,如果已经回忆过某个人的信息,请不要重复调取,除非你忘记了
@@ -205,10 +205,10 @@ class RelationshipProcessor(BaseProcessor):
)
try:
# logger.info(f"{self.log_prefix} 人物信息prompt: \n{prompt}\n")
logger.info(f"{self.log_prefix} 人物信息prompt: \n{prompt}\n")
content, _ = await self.llm_model.generate_response_async(prompt=prompt)
if content:
# print(f"content: {content}")
print(f"content: {content}")
content_json = json.loads(repair_json(content))
for person_name, info_type in content_json.items():

View File

@@ -41,6 +41,9 @@ class ActionManager:
# 初始化时将默认动作加载到使用中的动作
self._using_actions = self._default_actions.copy()
# 添加系统核心动作
self._add_system_core_actions()
def _load_registered_actions(self) -> None:
"""
@@ -59,14 +62,22 @@ class ActionManager:
action_parameters: dict[str:str] = getattr(action_class, "action_parameters", {})
action_require: list[str] = getattr(action_class, "action_require", [])
associated_types: list[str] = getattr(action_class, "associated_types", [])
is_default: bool = getattr(action_class, "default", False)
is_enabled: bool = getattr(action_class, "enable_plugin", True)
# 获取激活类型相关属性
activation_type: str = getattr(action_class, "action_activation_type", "always")
focus_activation_type: str = getattr(action_class, "focus_activation_type", "always")
normal_activation_type: str = getattr(action_class, "normal_activation_type", "always")
random_probability: float = getattr(action_class, "random_activation_probability", 0.3)
llm_judge_prompt: str = getattr(action_class, "llm_judge_prompt", "")
activation_keywords: list[str] = getattr(action_class, "activation_keywords", [])
keyword_case_sensitive: bool = getattr(action_class, "keyword_case_sensitive", False)
# 获取模式启用属性
mode_enable: str = getattr(action_class, "mode_enable", "all")
# 获取并行执行属性
parallel_action: bool = getattr(action_class, "parallel_action", False)
if action_name and action_description:
# 创建动作信息字典
@@ -75,18 +86,21 @@ class ActionManager:
"parameters": action_parameters,
"require": action_require,
"associated_types": associated_types,
"activation_type": activation_type,
"focus_activation_type": focus_activation_type,
"normal_activation_type": normal_activation_type,
"random_probability": random_probability,
"llm_judge_prompt": llm_judge_prompt,
"activation_keywords": activation_keywords,
"keyword_case_sensitive": keyword_case_sensitive,
"mode_enable": mode_enable,
"parallel_action": parallel_action,
}
# 添加到所有已注册的动作
self._registered_actions[action_name] = action_info
# 添加到默认动作(如果是默认动作
if is_default:
# 添加到默认动作(如果启用插件
if is_enabled:
self._default_actions[action_name] = action_info
# logger.info(f"所有注册动作: {list(self._registered_actions.keys())}")
@@ -212,9 +226,34 @@ class ActionManager:
return self._default_actions.copy()
def get_using_actions(self) -> Dict[str, ActionInfo]:
"""获取当前正在使用的动作集"""
"""获取当前正在使用的动作集"""
return self._using_actions.copy()
def get_using_actions_for_mode(self, mode: str) -> Dict[str, ActionInfo]:
"""
根据聊天模式获取可用的动作集合
Args:
mode: 聊天模式 ("focus", "normal", "all")
Returns:
Dict[str, ActionInfo]: 在指定模式下可用的动作集合
"""
filtered_actions = {}
for action_name, action_info in self._using_actions.items():
action_mode = action_info.get("mode_enable", "all")
# 检查动作是否在当前模式下启用
if action_mode == "all" or action_mode == mode:
filtered_actions[action_name] = action_info
logger.debug(f"动作 {action_name} 在模式 {mode} 下可用 (mode_enable: {action_mode})")
else:
logger.debug(f"动作 {action_name} 在模式 {mode} 下不可用 (mode_enable: {action_mode})")
logger.info(f"模式 {mode} 下可用动作: {list(filtered_actions.keys())}")
return filtered_actions
def add_action_to_using(self, action_name: str) -> bool:
"""
添加已注册的动作到当前使用的动作集
@@ -306,6 +345,36 @@ class ActionManager:
def restore_default_actions(self) -> None:
"""恢复默认动作集到使用集"""
self._using_actions = self._default_actions.copy()
# 添加系统核心动作即使enable_plugin为False的系统动作
self._add_system_core_actions()
def _add_system_core_actions(self) -> None:
"""
添加系统核心动作到使用集
系统核心动作是那些enable_plugin为False但是系统必需的动作
"""
system_core_actions = ["exit_focus_chat"] # 可以根据需要扩展
for action_name in system_core_actions:
if action_name in self._registered_actions and action_name not in self._using_actions:
self._using_actions[action_name] = self._registered_actions[action_name]
logger.info(f"添加系统核心动作到使用集: {action_name}")
def add_system_action_if_needed(self, action_name: str) -> bool:
"""
根据需要添加系统动作到使用集
Args:
action_name: 动作名称
Returns:
bool: 是否成功添加
"""
if action_name in self._registered_actions and action_name not in self._using_actions:
self._using_actions[action_name] = self._registered_actions[action_name]
logger.info(f"临时添加系统动作到使用集: {action_name}")
return True
return False
def get_action(self, action_name: str) -> Optional[Type[BaseAction]]:
"""

View File

@@ -2,5 +2,6 @@
from . import reply_action # noqa
from . import no_reply_action # noqa
from . import exit_focus_chat_action # noqa
from . import emoji_action # noqa
# 在此处添加更多动作模块导入

View File

@@ -15,6 +15,12 @@ class ActionActivationType:
RANDOM = "random" # 随机启用action到planner
KEYWORD = "keyword" # 关键词触发启用action到planner
# 聊天模式枚举
class ChatMode:
FOCUS = "focus" # Focus聊天模式
NORMAL = "normal" # Normal聊天模式
ALL = "all" # 所有聊天模式
def register_action(cls):
"""
动作注册装饰器
@@ -24,7 +30,10 @@ def register_action(cls):
class MyAction(BaseAction):
action_name = "my_action"
action_description = "我的动作"
action_activation_type = ActionActivationType.ALWAYS
focus_activation_type = ActionActivationType.ALWAYS
normal_activation_type = ActionActivationType.ALWAYS
mode_enable = ChatMode.ALL
parallel_action = False
...
"""
# 检查类是否有必要的属性
@@ -34,7 +43,7 @@ def register_action(cls):
action_name = cls.action_name
action_description = cls.action_description
is_default = getattr(cls, "default", False)
is_enabled = getattr(cls, "enable_plugin", True) # 默认启用插件
if not action_name or not action_description:
logger.error(f"动作类 {cls.__name__} 的 action_name 或 action_description 为空")
@@ -43,11 +52,11 @@ def register_action(cls):
# 将动作类注册到全局注册表
_ACTION_REGISTRY[action_name] = cls
# 如果是默认动作,添加到默认动作集
if is_default:
# 如果启用插件,添加到默认动作集
if is_enabled:
_DEFAULT_ACTIONS[action_name] = action_description
logger.info(f"已注册动作: {action_name} -> {cls.__name__}默认: {is_default}")
logger.info(f"已注册动作: {action_name} -> {cls.__name__}插件启用: {is_enabled}")
return cls
@@ -73,20 +82,32 @@ class BaseAction(ABC):
self.action_parameters: dict = {}
self.action_require: list[str] = []
# 动作激活类型默认为always
self.action_activation_type: str = ActionActivationType.ALWAYS
# 随机激活的概率(0.0-1.0)仅当activation_type为random时有效
# 动作激活类型设置
# Focus模式下的激活类型默认为always
self.focus_activation_type: str = ActionActivationType.ALWAYS
# Normal模式下的激活类型默认为always
self.normal_activation_type: str = ActionActivationType.ALWAYS
# 随机激活的概率(0.0-1.0)用于RANDOM激活类型
self.random_activation_probability: float = 0.3
# LLM判定的提示词仅当activation_type为llm_judge时有效
# LLM判定的提示词用于LLM_JUDGE激活类型
self.llm_judge_prompt: str = ""
# 关键词触发列表,仅当activation_type为keyword时有效
# 关键词触发列表,用于KEYWORD激活类型
self.activation_keywords: list[str] = []
# 关键词匹配是否区分大小写
self.keyword_case_sensitive: bool = False
# 模式启用设置:指定在哪些聊天模式下启用此动作
# 可选值: "focus"(仅Focus模式), "normal"(仅Normal模式), "all"(所有模式)
self.mode_enable: str = ChatMode.ALL
# 并行执行设置仅在Normal模式下生效设置为True的动作可以与回复动作并行执行
# 而不是替代回复动作适用于图片生成、TTS、禁言等不需要覆盖回复的动作
self.parallel_action: bool = False
self.associated_types: list[str] = []
self.default: bool = False
self.enable_plugin: bool = True # 是否启用插件,默认启用
self.action_data = action_data
self.reasoning = reasoning

View File

@@ -0,0 +1,150 @@
from src.common.logger_manager import get_logger
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action, ActionActivationType, ChatMode
from typing import Tuple, List
from src.chat.heart_flow.observation.observation import Observation
from src.chat.focus_chat.replyer.default_replyer import DefaultReplyer
from src.chat.message_receive.chat_stream import ChatStream
from src.chat.focus_chat.hfc_utils import create_empty_anchor_message
from src.config.config import global_config
logger = get_logger("action_taken")
@register_action
class EmojiAction(BaseAction):
"""表情动作处理类
处理构建和发送消息表情的动作。
"""
action_name: str = "emoji"
action_description: str = "当你想单独发送一个表情包辅助你的回复表达"
action_parameters: dict[str:str] = {
"description": "文字描述你想要发送的表情包内容",
}
action_require: list[str] = [
"表达情绪时可以选择使用",
"重点:不要连续发,如果你已经发过[表情包],就不要选择此动作"]
associated_types: list[str] = ["emoji"]
enable_plugin = True
focus_activation_type = ActionActivationType.LLM_JUDGE
normal_activation_type = ActionActivationType.RANDOM
random_activation_probability = global_config.normal_chat.emoji_chance
parallel_action = True
llm_judge_prompt = """
判定是否需要使用表情动作的条件:
1. 用户明确要求使用表情包
2. 这是一个适合表达强烈情绪的场合
3. 不要发送太多表情包,如果你已经发送过多个表情包
"""
# 模式启用设置 - 表情动作只在Focus模式下使用
mode_enable = ChatMode.ALL
def __init__(
self,
action_data: dict,
reasoning: str,
cycle_timers: dict,
thinking_id: str,
observations: List[Observation],
chat_stream: ChatStream,
log_prefix: str,
replyer: DefaultReplyer,
**kwargs,
):
"""初始化回复动作处理器
Args:
action_name: 动作名称
action_data: 动作数据,包含 message, emojis, target 等
reasoning: 执行该动作的理由
cycle_timers: 计时器字典
thinking_id: 思考ID
observations: 观察列表
replyer: 回复器
chat_stream: 聊天流
log_prefix: 日志前缀
"""
super().__init__(action_data, reasoning, cycle_timers, thinking_id)
self.observations = observations
self.replyer = replyer
self.chat_stream = chat_stream
self.log_prefix = log_prefix
async def handle_action(self) -> Tuple[bool, str]:
"""
处理回复动作
Returns:
Tuple[bool, str]: (是否执行成功, 回复文本)
"""
# 注意: 此处可能会使用不同的expressor实现根据任务类型切换不同的回复策略
return await self._handle_reply(
reasoning=self.reasoning,
reply_data=self.action_data,
cycle_timers=self.cycle_timers,
thinking_id=self.thinking_id,
)
async def _handle_reply(
self, reasoning: str, reply_data: dict, cycle_timers: dict, thinking_id: str
) -> tuple[bool, str]:
"""
处理统一的回复动作 - 可包含文本和表情,顺序任意
reply_data格式:
{
"description": "描述你想要发送的表情"
}
"""
logger.info(f"{self.log_prefix} 决定发送表情")
# 从聊天观察获取锚定消息
# chatting_observation: ChattingObservation = next(
# obs for obs in self.observations if isinstance(obs, ChattingObservation)
# )
# if reply_data.get("target"):
# anchor_message = chatting_observation.search_message_by_text(reply_data["target"])
# else:
# anchor_message = None
# 如果没有找到锚点消息,创建一个占位符
# if not anchor_message:
# logger.info(f"{self.log_prefix} 未找到锚点消息,创建占位符")
# anchor_message = await create_empty_anchor_message(
# self.chat_stream.platform, self.chat_stream.group_info, self.chat_stream
# )
# else:
# anchor_message.update_chat_stream(self.chat_stream)
logger.info(f"{self.log_prefix} 为了表情包创建占位符")
anchor_message = await create_empty_anchor_message(
self.chat_stream.platform, self.chat_stream.group_info, self.chat_stream
)
success, reply_set = await self.replyer.deal_emoji(
cycle_timers=cycle_timers,
action_data=reply_data,
anchor_message=anchor_message,
# reasoning=reasoning,
thinking_id=thinking_id,
)
reply_text = ""
if reply_set:
for reply in reply_set:
type = reply[0]
data = reply[1]
if type == "text":
reply_text += data
elif type == "emoji":
reply_text += data
return success, reply_text

View File

@@ -1,7 +1,7 @@
import asyncio
import traceback
from src.common.logger_manager import get_logger
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action, ChatMode
from typing import Tuple, List
from src.chat.heart_flow.observation.observation import Observation
from src.chat.message_receive.chat_stream import ChatStream
@@ -25,7 +25,11 @@ class ExitFocusChatAction(BaseAction):
"当前内容不需要持续专注关注,你决定退出专注聊天",
"聊天内容已经完成,你决定退出专注聊天",
]
default = False
# 退出专注聊天是系统核心功能,不是插件,但默认不启用(需要特定条件触发)
enable_plugin = False
# 模式启用设置 - 退出专注聊天动作只在Focus模式下使用
mode_enable = ChatMode.FOCUS
def __init__(
self,

View File

@@ -2,7 +2,7 @@ import asyncio
import traceback
from src.common.logger_manager import get_logger
from src.chat.utils.timer_calculator import Timer
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action, ActionActivationType
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action, ActionActivationType, ChatMode
from typing import Tuple, List
from src.chat.heart_flow.observation.observation import Observation
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
@@ -28,10 +28,13 @@ class NoReplyAction(BaseAction):
"你连续发送了太多消息,且无人回复",
"想要休息一下",
]
default = True
enable_plugin = True
# 激活类型设置
action_activation_type = ActionActivationType.ALWAYS
focus_activation_type = ActionActivationType.ALWAYS
# 模式启用设置 - no_reply动作只在Focus模式下使用
mode_enable = ChatMode.FOCUS
def __init__(
self,

View File

@@ -1,6 +1,6 @@
import traceback
from typing import Tuple, Dict, List, Any, Optional, Union, Type
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action, ActionActivationType # noqa F401
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action, ActionActivationType, ChatMode # noqa F401
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
from src.chat.focus_chat.hfc_utils import create_empty_anchor_message
from src.common.logger_manager import get_logger
@@ -35,11 +35,15 @@ class PluginAction(BaseAction):
action_config_file_name: Optional[str] = None # 插件可以覆盖此属性来指定配置文件名
# 默认激活类型设置,插件可以覆盖
action_activation_type = ActionActivationType.ALWAYS
focus_activation_type = ActionActivationType.ALWAYS
normal_activation_type = ActionActivationType.ALWAYS
random_activation_probability: float = 0.3
llm_judge_prompt: str = ""
activation_keywords: list[str] = []
keyword_case_sensitive: bool = False
# 默认模式启用设置 - 插件动作默认在所有模式下可用,插件可以覆盖
mode_enable = ChatMode.ALL
def __init__(
self,

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from src.common.logger_manager import get_logger
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action, ActionActivationType
from src.chat.focus_chat.planners.actions.base_action import BaseAction, register_action, ActionActivationType, ChatMode
from typing import Tuple, List
from src.chat.heart_flow.observation.observation import Observation
from src.chat.focus_chat.replyer.default_replyer import DefaultReplyer
@@ -26,21 +26,23 @@ class ReplyAction(BaseAction):
action_name: str = "reply"
action_description: str = "当你想要参与回复或者聊天"
action_parameters: dict[str:str] = {
"reply_to": "如果是明确回复某个人的发言请在reply_to参数中指定格式用户名:发言内容如果不是reply_to的值设为none",
"emoji": "如果你想用表情包辅助你的回答请在emoji参数中用文字描述你想要发送的表情包内容如果没有值设为空",
"reply_to": "如果是明确回复某个人的发言请在reply_to参数中指定格式用户名:发言内容如果不是reply_to的值设为none"
}
action_require: list[str] = [
"你想要闲聊或者随便附和",
"有人提到你",
"如果你刚刚回复,不要对同一个话题重复回应"
"如果你刚刚进行了回复,不要对同一个话题重复回应"
]
associated_types: list[str] = ["text", "emoji"]
associated_types: list[str] = ["text"]
default = True
enable_plugin = True
# 激活类型设置
action_activation_type = ActionActivationType.ALWAYS
focus_activation_type = ActionActivationType.ALWAYS
# 模式启用设置 - 回复动作只在Focus模式下使用
mode_enable = ChatMode.FOCUS
def __init__(
self,
@@ -105,7 +107,6 @@ class ReplyAction(BaseAction):
{
"text": "你好啊" # 文本内容列表(可选)
"target": "锚定消息", # 锚定消息的文本内容
"emojis": "微笑" # 表情关键词列表(可选)
}
"""
logger.info(f"{self.log_prefix} 决定回复: {self.reasoning}")

View File

@@ -6,7 +6,7 @@ from src.chat.heart_flow.observation.chatting_observation import ChattingObserva
from src.chat.message_receive.chat_stream import chat_manager
from src.config.config import global_config
from src.llm_models.utils_model import LLMRequest
from src.chat.focus_chat.planners.actions.base_action import ActionActivationType
from src.chat.focus_chat.planners.actions.base_action import ActionActivationType, ChatMode
import random
import asyncio
import hashlib
@@ -29,7 +29,7 @@ class ActionModifier:
def __init__(self, action_manager: ActionManager):
"""初始化动作处理器"""
self.action_manager = action_manager
self.all_actions = self.action_manager.get_registered_actions()
self.all_actions = self.action_manager.get_using_actions_for_mode(ChatMode.FOCUS)
# 用于LLM判定的小模型
self.llm_judge = LLMRequest(
@@ -78,7 +78,8 @@ class ActionModifier:
# 处理HFCloopObservation - 传统的循环历史分析
if hfc_obs:
obs = hfc_obs
all_actions = self.all_actions
# 获取适用于FOCUS模式的动作
all_actions = self.action_manager.get_using_actions_for_mode(ChatMode.FOCUS)
action_changes = await self.analyze_loop_actions(obs)
if action_changes["add"] or action_changes["remove"]:
# 合并动作变更
@@ -129,9 +130,9 @@ class ActionModifier:
if chat_content is not None:
logger.debug(f"{self.log_prefix}开始激活类型判定阶段")
# 获取当前使用的动作集(经过第一阶段处理)
# 获取当前使用的动作集(经过第一阶段处理且适用于FOCUS模式
current_using_actions = self.action_manager.get_using_actions()
all_registered_actions = self.action_manager.get_registered_actions()
all_registered_actions = self.action_manager.get_using_actions_for_mode(ChatMode.FOCUS)
# 构建完整的动作信息
current_actions_with_info = {}
@@ -157,7 +158,7 @@ class ActionModifier:
# 确定移除原因
if action_name in all_registered_actions:
action_info = all_registered_actions[action_name]
activation_type = action_info.get("activation_type", ActionActivationType.ALWAYS)
activation_type = action_info.get("focus_activation_type", ActionActivationType.ALWAYS)
if activation_type == ActionActivationType.RANDOM:
probability = action_info.get("random_probability", 0.3)
@@ -207,7 +208,7 @@ class ActionModifier:
keyword_actions = {}
for action_name, action_info in actions_with_info.items():
activation_type = action_info.get("activation_type", ActionActivationType.ALWAYS)
activation_type = action_info.get("focus_activation_type", ActionActivationType.ALWAYS)
if activation_type == ActionActivationType.ALWAYS:
always_actions[action_name] = action_info
@@ -433,6 +434,7 @@ class ActionModifier:
action_require = action_info.get("require", [])
custom_prompt = action_info.get("llm_judge_prompt", "")
# 构建基础判定提示词
base_prompt = f"""
你需要判断在当前聊天情况下,是否应该激活名为"{action_name}"的动作。
@@ -462,7 +464,7 @@ class ActionModifier:
# 解析响应
response = response.strip().lower()
print(base_prompt)
# print(base_prompt)
print(f"LLM判定动作 {action_name}:响应='{response}'")

View File

@@ -16,6 +16,7 @@ from src.chat.utils.prompt_builder import Prompt, global_prompt_manager
from src.individuality.individuality import individuality
from src.chat.focus_chat.planners.action_manager import ActionManager
from src.chat.focus_chat.planners.modify_actions import ActionModifier
from src.chat.focus_chat.planners.actions.base_action import ChatMode
from json_repair import repair_json
from src.chat.focus_chat.planners.base_planner import BasePlanner
from datetime import datetime
@@ -144,7 +145,8 @@ class ActionPlanner(BasePlanner):
# 获取经过modify_actions处理后的最终可用动作集
# 注意动作的激活判定现在在主循环的modify_actions中完成
current_available_actions_dict = self.action_manager.get_using_actions()
# 使用Focus模式过滤动作
current_available_actions_dict = self.action_manager.get_using_actions_for_mode(ChatMode.FOCUS)
# 获取完整的动作信息
all_registered_actions = self.action_manager.get_registered_actions()

View File

@@ -150,17 +150,6 @@ class DefaultReplyer:
action_data=action_data,
)
with Timer("选择表情", cycle_timers):
emoji_keyword = action_data.get("emoji", "")
print(f"emoji_keyword: {emoji_keyword}")
if emoji_keyword:
emoji_base64, _description, _emotion = await self._choose_emoji(emoji_keyword)
# print(f"emoji_base64: {emoji_base64}")
# print(f"emoji_description: {_description}")
# print(f"emoji_emotion: {emotion}")
if emoji_base64:
reply.append(("emoji", emoji_base64))
if reply:
with Timer("发送消息", cycle_timers):
sent_msg_list = await self.send_response_messages(

View File

@@ -280,28 +280,26 @@ class NormalChat:
info_catcher = info_catcher_manager.get_info_catcher(thinking_id)
info_catcher.catch_decide_to_response(message)
# 如果启用planner预先修改可用actions避免在并行任务中重复调用
available_actions = None
if self.enable_planner:
try:
await self.action_modifier.modify_actions_for_normal_chat(
self.chat_stream, self.recent_replies, message.processed_plain_text
)
available_actions = self.action_manager.get_using_actions()
except Exception as e:
logger.warning(f"[{self.stream_name}] 获取available_actions失败: {e}")
available_actions = None
# 定义并行执行的任务
async def generate_normal_response():
"""生成普通回复"""
try:
# 如果启用planner获取可用actions
enable_planner = self.enable_planner
available_actions = None
if enable_planner:
try:
await self.action_modifier.modify_actions_for_normal_chat(
self.chat_stream, self.recent_replies
)
available_actions = self.action_manager.get_using_actions()
except Exception as e:
logger.warning(f"[{self.stream_name}] 获取available_actions失败: {e}")
available_actions = None
return await self.gpt.generate_response(
message=message,
thinking_id=thinking_id,
enable_planner=enable_planner,
enable_planner=self.enable_planner,
available_actions=available_actions,
)
except Exception as e:
@@ -315,38 +313,37 @@ class NormalChat:
return None
try:
# 并行执行动作修改和规划准备
async def modify_actions():
"""修改可用动作集合"""
return await self.action_modifier.modify_actions_for_normal_chat(
self.chat_stream, self.recent_replies
)
async def prepare_planning():
"""准备规划所需的信息"""
return self._get_sender_name(message)
# 并行执行动作修改和准备工作
_, sender_name = await asyncio.gather(modify_actions(), prepare_planning())
# 获取发送者名称(动作修改已在并行执行前完成)
sender_name = self._get_sender_name(message)
no_action = {
"action_result": {"action_type": "no_action", "action_data": {}, "reasoning": "规划器初始化默认", "is_parallel": True},
"chat_context": "",
"action_prompt": "",
}
# 检查是否应该跳过规划
if self.action_modifier.should_skip_planning():
logger.debug(f"[{self.stream_name}] 没有可用动作,跳过规划")
return None
self.action_type = "no_action"
return no_action
# 执行规划
plan_result = await self.planner.plan(message, sender_name)
action_type = plan_result["action_result"]["action_type"]
action_data = plan_result["action_result"]["action_data"]
reasoning = plan_result["action_result"]["reasoning"]
is_parallel = plan_result["action_result"].get("is_parallel", False)
logger.info(f"[{self.stream_name}] Planner决策: {action_type}, 理由: {reasoning}")
logger.info(f"[{self.stream_name}] Planner决策: {action_type}, 理由: {reasoning}, 并行执行: {is_parallel}")
self.action_type = action_type # 更新实例属性
self.is_parallel_action = is_parallel # 新增:保存并行执行标志
# 如果规划器决定不执行任何动作
if action_type == "no_action":
logger.debug(f"[{self.stream_name}] Planner决定不执行任何额外动作")
return None
return no_action
elif action_type == "change_to_focus_chat":
logger.info(f"[{self.stream_name}] Planner决定切换到focus聊天模式")
return None
@@ -358,14 +355,15 @@ class NormalChat:
else:
logger.warning(f"[{self.stream_name}] 额外动作 {action_type} 执行失败")
return {"action_type": action_type, "action_data": action_data, "reasoning": reasoning}
return {"action_type": action_type, "action_data": action_data, "reasoning": reasoning, "is_parallel": is_parallel}
except Exception as e:
logger.error(f"[{self.stream_name}] Planner执行失败: {e}")
return None
return no_action
# 并行执行回复生成和动作规划
self.action_type = None # 初始化动作类型
self.is_parallel_action = False # 初始化并行动作标志
with Timer("并行生成回复和规划", timing_results):
response_set, plan_result = await asyncio.gather(
generate_normal_response(), plan_and_execute_actions(), return_exceptions=True
@@ -382,15 +380,15 @@ class NormalChat:
if isinstance(plan_result, Exception):
logger.error(f"[{self.stream_name}] 动作规划异常: {plan_result}")
elif plan_result:
logger.debug(f"[{self.stream_name}] 额外动作处理完成: {plan_result['action_type']}")
logger.debug(f"[{self.stream_name}] 额外动作处理完成: {self.action_type}")
if not response_set or (
self.enable_planner and self.action_type not in ["no_action", "change_to_focus_chat"]
self.enable_planner and self.action_type not in ["no_action", "change_to_focus_chat"] and not self.is_parallel_action
):
if not response_set:
logger.info(f"[{self.stream_name}] 模型未生成回复内容")
elif self.enable_planner and self.action_type not in ["no_action", "change_to_focus_chat"]:
logger.info(f"[{self.stream_name}] 模型选择其他动作")
elif self.enable_planner and self.action_type not in ["no_action", "change_to_focus_chat"] and not self.is_parallel_action:
logger.info(f"[{self.stream_name}] 模型选择其他动作(非并行动作)")
# 如果模型未生成回复,移除思考消息
container = await message_manager.get_container(self.stream_id) # 使用 self.stream_id
for msg in container.messages[:]:
@@ -446,7 +444,7 @@ class NormalChat:
logger.warning(f"[{self.stream_name}] 没有设置切换到focus聊天模式的回调函数无法执行切换")
return
else:
await self._check_switch_to_focus()
# await self._check_switch_to_focus()
pass
info_catcher.done_catch()

View File

@@ -1,6 +1,11 @@
from typing import List, Any
from typing import List, Any, Dict
from src.common.logger_manager import get_logger
from src.chat.focus_chat.planners.action_manager import ActionManager
from src.chat.focus_chat.planners.actions.base_action import ActionActivationType, ChatMode
from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat
from src.config.config import global_config
import random
import time
logger = get_logger("normal_chat_action_modifier")
@@ -9,6 +14,7 @@ class NormalChatActionModifier:
"""Normal Chat动作修改器
负责根据Normal Chat的上下文和状态动态调整可用的动作集合
实现与Focus Chat类似的动作激活策略但将LLM_JUDGE转换为概率激活以提升性能
"""
def __init__(self, action_manager: ActionManager, stream_id: str, stream_name: str):
@@ -25,9 +31,14 @@ class NormalChatActionModifier:
self,
chat_stream,
recent_replies: List[dict],
message_content: str,
**kwargs: Any,
):
"""为Normal Chat修改可用动作集合
实现动作激活策略:
1. 基于关联类型的动态过滤
2. 基于激活类型的智能判定LLM_JUDGE转为概率激活
Args:
chat_stream: 聊天流对象
@@ -35,24 +46,19 @@ class NormalChatActionModifier:
**kwargs: 其他参数
"""
# 合并所有动作变更
merged_action_changes = {"add": [], "remove": []}
reasons = []
merged_action_changes = {"add": [], "remove": []}
type_mismatched_actions = [] # 在外层定义避免作用域问题
self.action_manager.restore_default_actions()
# 1. 移除Normal Chat不适用的动作
excluded_actions = ["exit_focus_chat_action", "no_reply", "reply"]
for action_name in excluded_actions:
if action_name in self.action_manager.get_using_actions():
merged_action_changes["remove"].append(action_name)
reasons.append(f"移除{action_name}(Normal Chat不适用)")
# 2. 检查动作的关联类型
# 第一阶段:基于关联类型的动态过滤
if chat_stream:
chat_context = chat_stream.context if hasattr(chat_stream, "context") else None
if chat_context:
type_mismatched_actions = []
current_using_actions = self.action_manager.get_using_actions()
# 获取Normal模式下的可用动作已经过滤了mode_enable
current_using_actions = self.action_manager.get_using_actions_for_mode(ChatMode.NORMAL)
# print(f"current_using_actions: {current_using_actions}")
for action_name in current_using_actions.keys():
if action_name in self.all_actions:
data = self.all_actions[action_name]
@@ -65,26 +71,218 @@ class NormalChatActionModifier:
merged_action_changes["remove"].extend(type_mismatched_actions)
reasons.append(f"移除{type_mismatched_actions}(关联类型不匹配)")
# 应用动作变更
# 第二阶段:应用激活类型判定
# 构建聊天内容 - 使用与planner一致的方式
chat_content = ""
if chat_stream and hasattr(chat_stream, 'stream_id'):
try:
# 获取消息历史使用与normal_chat_planner相同的方法
message_list_before_now = get_raw_msg_before_timestamp_with_chat(
chat_id=chat_stream.stream_id,
timestamp=time.time(),
limit=global_config.focus_chat.observation_context_size, # 使用相同的配置
)
# 构建可读的聊天上下文
chat_content = build_readable_messages(
message_list_before_now,
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",
read_mark=0.0,
show_actions=True,
)
logger.debug(f"{self.log_prefix} 成功构建聊天内容,长度: {len(chat_content)}")
except Exception as e:
logger.warning(f"{self.log_prefix} 构建聊天内容失败: {e}")
chat_content = ""
# 获取当前Normal模式下的动作集进行激活判定
current_actions = self.action_manager.get_using_actions_for_mode(ChatMode.NORMAL)
# print(f"current_actions: {current_actions}")
# print(f"chat_content: {chat_content}")
final_activated_actions = await self._apply_normal_activation_filtering(
current_actions,
chat_content,
message_content
)
# print(f"final_activated_actions: {final_activated_actions}")
# 统一处理所有需要移除的动作,避免重复移除
all_actions_to_remove = set() # 使用set避免重复
# 添加关联类型不匹配的动作
if type_mismatched_actions:
all_actions_to_remove.update(type_mismatched_actions)
# 添加激活类型判定未通过的动作
for action_name in current_actions.keys():
if action_name not in final_activated_actions:
all_actions_to_remove.add(action_name)
# 统计移除原因(避免重复)
activation_failed_actions = [name for name in current_actions.keys() if name not in final_activated_actions and name not in type_mismatched_actions]
if activation_failed_actions:
reasons.append(f"移除{activation_failed_actions}(激活类型判定未通过)")
# 统一执行移除操作
for action_name in all_actions_to_remove:
success = self.action_manager.remove_action_from_using(action_name)
if success:
logger.debug(f"{self.log_prefix} 移除动作: {action_name}")
else:
logger.debug(f"{self.log_prefix} 动作 {action_name} 已经不在使用集中,跳过移除")
# 应用动作添加(如果有的话)
for action_name in merged_action_changes["add"]:
if action_name in self.all_actions and action_name not in excluded_actions:
if action_name in self.all_actions:
success = self.action_manager.add_action_to_using(action_name)
if success:
logger.debug(f"{self.log_prefix} 添加动作: {action_name}")
for action_name in merged_action_changes["remove"]:
success = self.action_manager.remove_action_from_using(action_name)
if success:
logger.debug(f"{self.log_prefix} 移除动作: {action_name}")
# 记录变更原因
if merged_action_changes["add"] or merged_action_changes["remove"]:
if reasons:
logger.info(f"{self.log_prefix} 动作调整完成: {' | '.join(reasons)}")
logger.debug(f"{self.log_prefix} 当前可用动作: {list(self.action_manager.get_using_actions().keys())}")
# 获取最终的Normal模式可用动作并记录
final_actions = self.action_manager.get_using_actions_for_mode(ChatMode.NORMAL)
logger.debug(f"{self.log_prefix} 当前Normal模式可用动作: {list(final_actions.keys())}")
async def _apply_normal_activation_filtering(
self,
actions_with_info: Dict[str, Any],
chat_content: str = "",
message_content: str = "",
) -> Dict[str, Any]:
"""
应用Normal模式的激活类型过滤逻辑
与Focus模式的区别
1. LLM_JUDGE类型转换为概率激活避免LLM调用
2. RANDOM类型保持概率激活
3. KEYWORD类型保持关键词匹配
4. ALWAYS类型直接激活
Args:
actions_with_info: 带完整信息的动作字典
chat_content: 聊天内容
Returns:
Dict[str, Any]: 过滤后激活的actions字典
"""
activated_actions = {}
# 分类处理不同激活类型的actions
always_actions = {}
random_actions = {}
keyword_actions = {}
for action_name, action_info in actions_with_info.items():
# 使用normal_activation_type
activation_type = action_info.get("normal_activation_type", ActionActivationType.ALWAYS)
if activation_type == ActionActivationType.ALWAYS:
always_actions[action_name] = action_info
elif activation_type == ActionActivationType.RANDOM or activation_type == ActionActivationType.LLM_JUDGE:
random_actions[action_name] = action_info
elif activation_type == ActionActivationType.KEYWORD:
keyword_actions[action_name] = action_info
else:
logger.warning(f"{self.log_prefix}未知的激活类型: {activation_type},跳过处理")
# 1. 处理ALWAYS类型直接激活
for action_name, action_info in always_actions.items():
activated_actions[action_name] = action_info
logger.debug(f"{self.log_prefix}激活动作: {action_name},原因: ALWAYS类型直接激活")
# 2. 处理RANDOM类型概率激活
for action_name, action_info in random_actions.items():
probability = action_info.get("random_probability", 0.3)
should_activate = random.random() < probability
if should_activate:
activated_actions[action_name] = action_info
logger.info(f"{self.log_prefix}激活动作: {action_name},原因: RANDOM类型触发概率{probability}")
else:
logger.debug(f"{self.log_prefix}未激活动作: {action_name},原因: RANDOM类型未触发概率{probability}")
# 3. 处理KEYWORD类型关键词匹配
for action_name, action_info in keyword_actions.items():
should_activate = self._check_keyword_activation(
action_name,
action_info,
chat_content,
message_content
)
if should_activate:
activated_actions[action_name] = action_info
keywords = action_info.get("activation_keywords", [])
logger.info(f"{self.log_prefix}激活动作: {action_name},原因: KEYWORD类型匹配关键词{keywords}")
else:
keywords = action_info.get("activation_keywords", [])
logger.info(f"{self.log_prefix}未激活动作: {action_name},原因: KEYWORD类型未匹配关键词{keywords}")
# print(f"keywords: {keywords}")
# print(f"chat_content: {chat_content}")
logger.debug(f"{self.log_prefix}Normal模式激活类型过滤完成: {list(activated_actions.keys())}")
return activated_actions
def _check_keyword_activation(
self,
action_name: str,
action_info: Dict[str, Any],
chat_content: str = "",
message_content: str = "",
) -> bool:
"""
检查是否匹配关键词触发条件
Args:
action_name: 动作名称
action_info: 动作信息
chat_content: 聊天内容(已经是格式化后的可读消息)
Returns:
bool: 是否应该激活此action
"""
activation_keywords = action_info.get("activation_keywords", [])
case_sensitive = action_info.get("keyword_case_sensitive", False)
if not activation_keywords:
logger.warning(f"{self.log_prefix}动作 {action_name} 设置为关键词触发但未配置关键词")
return False
# 使用构建好的聊天内容作为检索文本
search_text = chat_content +message_content
# 如果不区分大小写,转换为小写
if not case_sensitive:
search_text = search_text.lower()
# 检查每个关键词
matched_keywords = []
for keyword in activation_keywords:
check_keyword = keyword if case_sensitive else keyword.lower()
if check_keyword in search_text:
matched_keywords.append(keyword)
# print(f"search_text: {search_text}")
# print(f"activation_keywords: {activation_keywords}")
if matched_keywords:
logger.info(f"{self.log_prefix}动作 {action_name} 匹配到关键词: {matched_keywords}")
return True
else:
logger.info(f"{self.log_prefix}动作 {action_name} 未匹配到任何关键词: {activation_keywords}")
return False
def get_available_actions_count(self) -> int:
"""获取当前可用动作数量排除默认的no_action"""
current_actions = self.action_manager.get_using_actions()
current_actions = self.action_manager.get_using_actions_for_mode(ChatMode.NORMAL)
# 排除no_action如果存在
filtered_actions = {k: v for k, v in current_actions.items() if k != "no_action"}
return len(filtered_actions)

View File

@@ -7,6 +7,7 @@ from src.common.logger_manager import get_logger
from src.chat.utils.prompt_builder import Prompt, global_prompt_manager
from src.individuality.individuality import individuality
from src.chat.focus_chat.planners.action_manager import ActionManager
from src.chat.focus_chat.planners.actions.base_action import ChatMode
from src.chat.message_receive.message import MessageThinking
from json_repair import repair_json
from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat
@@ -98,16 +99,18 @@ class NormalChatPlanner:
self_info = name_block + personality_block + identity_block
# 获取当前可用的动作
current_available_actions = self.action_manager.get_using_actions()
# 获取当前可用的动作使用Normal模式过滤
current_available_actions = self.action_manager.get_using_actions_for_mode(ChatMode.NORMAL)
# 注意:动作的激活判定现在在 normal_chat_action_modifier 中完成
# 这里直接使用经过 action_modifier 处理后的最终动作集
# 符合职责分离原则ActionModifier负责动作管理Planner专注于决策
# 如果没有可用动作或只有no_action动作直接返回no_action
if not current_available_actions or (
len(current_available_actions) == 1 and "no_action" in current_available_actions
):
logger.debug(f"{self.log_prefix}规划器: 没有可用动作或只有no_action动作返回no_action")
# 如果没有可用动作直接返回no_action
if not current_available_actions:
logger.debug(f"{self.log_prefix}规划器: 没有可用动作返回no_action")
return {
"action_result": {"action_type": action, "action_data": action_data, "reasoning": reasoning},
"action_result": {"action_type": action, "action_data": action_data, "reasoning": reasoning, "is_parallel": True},
"chat_context": "",
"action_prompt": "",
}
@@ -138,7 +141,7 @@ class NormalChatPlanner:
if not prompt:
logger.warning(f"{self.log_prefix}规划器: 构建提示词失败")
return {
"action_result": {"action_type": action, "action_data": action_data, "reasoning": reasoning},
"action_result": {"action_type": action, "action_data": action_data, "reasoning": reasoning, "is_parallel": False},
"chat_context": chat_context,
"action_prompt": "",
}
@@ -185,13 +188,21 @@ class NormalChatPlanner:
except Exception as outer_e:
logger.error(f"{self.log_prefix}规划器异常: {outer_e}")
chat_context = "无法获取聊天上下文" # 设置默认值
prompt = "" # 设置默认值
# 设置异常时的默认值
current_available_actions = {}
chat_context = "无法获取聊天上下文"
prompt = ""
action = "no_action"
reasoning = "规划器出现异常,使用默认动作"
action_data = {}
logger.debug(f"{self.log_prefix}规划器决策动作:{action}, 动作信息: '{action_data}', 理由: {reasoning}")
# 检查动作是否支持并行执行
is_parallel = False
if action in current_available_actions:
action_info = current_available_actions[action]
is_parallel = action_info.get("parallel_action", False)
logger.debug(f"{self.log_prefix}规划器决策动作:{action}, 动作信息: '{action_data}', 理由: {reasoning}, 并行执行: {is_parallel}")
# 恢复到默认动作集
self.action_manager.restore_actions()
@@ -212,6 +223,7 @@ class NormalChatPlanner:
"action_type": action,
"action_data": action_data,
"reasoning": reasoning,
"is_parallel": is_parallel,
"action_record": json.dumps(action_record, ensure_ascii=False)
}
@@ -304,4 +316,6 @@ class NormalChatPlanner:
return ""
init_prompt()