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

@@ -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()