feat:将action记录存到数据库,并显示到prompt中
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
# Changelog
|
||||
|
||||
## [0.7.2] -2025-6-4
|
||||
重点升级
|
||||
|
||||
添加了新的插件API
|
||||
大大优化action的选择能力
|
||||
|
||||
|
||||
## [0.7.1] -2025-6-3
|
||||
重点优化
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ from src.chat.focus_chat.planners.modify_actions import ActionModifier
|
||||
from src.chat.focus_chat.planners.action_manager import ActionManager
|
||||
from src.chat.focus_chat.working_memory.working_memory import WorkingMemory
|
||||
from src.config.config import global_config
|
||||
from src.common.database.database_model import ActionRecords
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
@@ -552,6 +553,9 @@ class HeartFChatting:
|
||||
tuple[bool, str, str]: (是否执行了动作, 思考消息ID, 命令)
|
||||
"""
|
||||
try:
|
||||
action_time = time.time()
|
||||
action_id = f"{action_time}_{thinking_id}"
|
||||
|
||||
# 使用工厂创建动作处理器实例
|
||||
try:
|
||||
action_handler = self.action_manager.create_action(
|
||||
@@ -586,6 +590,7 @@ class HeartFChatting:
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 麦麦执行了'{action}', 返回结果'{success}', '{reply_text}', '{command}'"
|
||||
)
|
||||
|
||||
return success, reply_text, command
|
||||
|
||||
except Exception as e:
|
||||
|
||||
@@ -118,7 +118,7 @@ class MemoryActivator:
|
||||
|
||||
# 只取response的第一个元素(字符串)
|
||||
response_str = response[0]
|
||||
print(f"response_str: {response_str[1]}")
|
||||
# print(f"response_str: {response_str[1]}")
|
||||
keywords = list(get_keywords_from_json(response_str))
|
||||
|
||||
# 更新关键词缓存
|
||||
|
||||
@@ -11,6 +11,8 @@ from src.config.config import global_config
|
||||
import os
|
||||
import inspect
|
||||
import toml # 导入 toml 库
|
||||
from src.common.database.database_model import ActionRecords
|
||||
import time
|
||||
|
||||
logger = get_logger("plugin_action")
|
||||
|
||||
@@ -391,3 +393,39 @@ class PluginAction(BaseAction):
|
||||
Tuple[bool, str]: (是否执行成功, 回复文本)
|
||||
"""
|
||||
return await self.process()
|
||||
|
||||
async def store_action_info(self, action_build_into_prompt: bool = False, action_prompt_display: str = "", action_done: bool = True) -> None:
|
||||
"""存储action执行信息到数据库
|
||||
|
||||
Args:
|
||||
action_build_into_prompt: 是否构建到提示中
|
||||
action_prompt_display: 动作显示内容
|
||||
"""
|
||||
try:
|
||||
chat_stream = self._services.get("chat_stream")
|
||||
if not chat_stream:
|
||||
logger.error(f"{self.log_prefix} 无法存储action信息:缺少chat_stream服务")
|
||||
return
|
||||
|
||||
action_time = time.time()
|
||||
action_id = f"{action_time}_{self.thinking_id}"
|
||||
|
||||
ActionRecords.create(
|
||||
action_id=action_id,
|
||||
time=action_time,
|
||||
action_name=self.__class__.__name__,
|
||||
action_data=str(self.action_data),
|
||||
action_done=action_done,
|
||||
action_build_into_prompt=action_build_into_prompt,
|
||||
action_prompt_display=action_prompt_display,
|
||||
chat_id=chat_stream.stream_id,
|
||||
chat_info_stream_id=chat_stream.stream_id,
|
||||
chat_info_platform=chat_stream.platform,
|
||||
user_id=chat_stream.user_info.user_id if chat_stream.user_info else "",
|
||||
user_nickname=chat_stream.user_info.user_nickname if chat_stream.user_info else "",
|
||||
user_cardname=chat_stream.user_info.user_cardname if chat_stream.user_info else ""
|
||||
)
|
||||
logger.debug(f"{self.log_prefix} 已存储action信息: {action_prompt_display}")
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 存储action信息时出错: {e}")
|
||||
traceback.print_exc()
|
||||
|
||||
@@ -8,6 +8,9 @@ from src.chat.focus_chat.replyer.default_replyer import DefaultReplyer
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
|
||||
from src.chat.focus_chat.hfc_utils import create_empty_anchor_message
|
||||
import time
|
||||
import traceback
|
||||
from src.common.database.database_model import ActionRecords
|
||||
|
||||
logger = get_logger("action_taken")
|
||||
|
||||
@@ -72,12 +75,19 @@ class ReplyAction(BaseAction):
|
||||
Tuple[bool, str]: (是否执行成功, 回复文本)
|
||||
"""
|
||||
# 注意: 此处可能会使用不同的expressor实现根据任务类型切换不同的回复策略
|
||||
return await self._handle_reply(
|
||||
success, reply_text = await self._handle_reply(
|
||||
reasoning=self.reasoning,
|
||||
reply_data=self.action_data,
|
||||
cycle_timers=self.cycle_timers,
|
||||
thinking_id=self.thinking_id,
|
||||
)
|
||||
|
||||
await self.store_action_info(
|
||||
action_build_into_prompt=False,
|
||||
action_prompt_display=f"{reply_text}",
|
||||
)
|
||||
|
||||
return success, reply_text
|
||||
|
||||
async def _handle_reply(
|
||||
self, reasoning: str, reply_data: dict, cycle_timers: dict, thinking_id: str
|
||||
@@ -130,3 +140,40 @@ class ReplyAction(BaseAction):
|
||||
reply_text += data
|
||||
|
||||
return success, reply_text
|
||||
|
||||
|
||||
async def store_action_info(self, action_build_into_prompt: bool = False, action_prompt_display: str = "", action_done: bool = True) -> None:
|
||||
"""存储action执行信息到数据库
|
||||
|
||||
Args:
|
||||
action_build_into_prompt: 是否构建到提示中
|
||||
action_prompt_display: 动作显示内容
|
||||
"""
|
||||
try:
|
||||
chat_stream = self.chat_stream
|
||||
if not chat_stream:
|
||||
logger.error(f"{self.log_prefix} 无法存储action信息:缺少chat_stream服务")
|
||||
return
|
||||
|
||||
action_time = time.time()
|
||||
action_id = f"{action_time}_{self.thinking_id}"
|
||||
|
||||
ActionRecords.create(
|
||||
action_id=action_id,
|
||||
time=action_time,
|
||||
action_name=self.__class__.__name__,
|
||||
action_data=str(self.action_data),
|
||||
action_done=action_done,
|
||||
action_build_into_prompt=action_build_into_prompt,
|
||||
action_prompt_display=action_prompt_display,
|
||||
chat_id=chat_stream.stream_id,
|
||||
chat_info_stream_id=chat_stream.stream_id,
|
||||
chat_info_platform=chat_stream.platform,
|
||||
user_id=chat_stream.user_info.user_id if chat_stream.user_info else "",
|
||||
user_nickname=chat_stream.user_info.user_nickname if chat_stream.user_info else "",
|
||||
user_cardname=chat_stream.user_info.user_cardname if chat_stream.user_info else ""
|
||||
)
|
||||
logger.debug(f"{self.log_prefix} 已存储action信息: {action_prompt_display}")
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 存储action信息时出错: {e}")
|
||||
traceback.print_exc()
|
||||
@@ -1,141 +0,0 @@
|
||||
#!/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
|
||||
from typing import Tuple, List
|
||||
from src.chat.heart_flow.observation.observation import Observation
|
||||
from chat.focus_chat.replyer.default_expressor import DefaultExpressor
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
|
||||
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 ReplyAction(BaseAction):
|
||||
"""回复动作处理类
|
||||
|
||||
处理构建和发送消息回复的动作。
|
||||
"""
|
||||
|
||||
action_name: str = "reply"
|
||||
action_description: str = "表达想法,可以只包含文本、表情或两者都有"
|
||||
action_parameters: dict[str:str] = {
|
||||
"text": "你想要表达的内容(可选)",
|
||||
"emojis": "描述当前使用表情包的场景,一段话描述(可选)",
|
||||
"target": "你想要回复的原始文本内容(非必须,仅文本,不包含发送者)(可选)",
|
||||
}
|
||||
action_require: list[str] = [
|
||||
"有实质性内容需要表达",
|
||||
"有人提到你,但你还没有回应他",
|
||||
"在合适的时候添加表情(不要总是添加),表情描述要详细,描述当前场景,一段话描述",
|
||||
"如果你有明确的,要回复特定某人的某句话,或者你想回复较早的消息,请在target中指定那句话的原始文本",
|
||||
"一次只回复一个人,一次只回复一个话题,突出重点",
|
||||
"如果是自己发的消息想继续,需自然衔接",
|
||||
"避免重复或评价自己的发言,不要和自己聊天",
|
||||
f"注意你的回复要求:{global_config.expression.expression_style}",
|
||||
]
|
||||
|
||||
associated_types: list[str] = ["text", "emoji"]
|
||||
|
||||
default = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
action_data: dict,
|
||||
reasoning: str,
|
||||
cycle_timers: dict,
|
||||
thinking_id: str,
|
||||
observations: List[Observation],
|
||||
expressor: DefaultExpressor,
|
||||
chat_stream: ChatStream,
|
||||
log_prefix: str,
|
||||
**kwargs,
|
||||
):
|
||||
"""初始化回复动作处理器
|
||||
|
||||
Args:
|
||||
action_name: 动作名称
|
||||
action_data: 动作数据,包含 message, emojis, target 等
|
||||
reasoning: 执行该动作的理由
|
||||
cycle_timers: 计时器字典
|
||||
thinking_id: 思考ID
|
||||
observations: 观察列表
|
||||
expressor: 表达器
|
||||
chat_stream: 聊天流
|
||||
log_prefix: 日志前缀
|
||||
"""
|
||||
super().__init__(action_data, reasoning, cycle_timers, thinking_id)
|
||||
self.observations = observations
|
||||
self.expressor = expressor
|
||||
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格式:
|
||||
{
|
||||
"text": "你好啊" # 文本内容列表(可选)
|
||||
"target": "锚定消息", # 锚定消息的文本内容
|
||||
"emojis": "微笑" # 表情关键词列表(可选)
|
||||
}
|
||||
"""
|
||||
logger.info(f"{self.log_prefix} 决定回复: {self.reasoning}")
|
||||
|
||||
# 从聊天观察获取锚定消息
|
||||
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)
|
||||
|
||||
success, reply_set = await self.expressor.deal_reply(
|
||||
cycle_timers=cycle_timers,
|
||||
action_data=reply_data,
|
||||
anchor_message=anchor_message,
|
||||
reasoning=reasoning,
|
||||
thinking_id=thinking_id,
|
||||
)
|
||||
|
||||
reply_text = ""
|
||||
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
|
||||
@@ -43,8 +43,6 @@ def init_prompt():
|
||||
|
||||
{chat_content_block}
|
||||
|
||||
{mind_info_block}
|
||||
|
||||
{cycle_info_block}
|
||||
|
||||
{moderation_prompt}
|
||||
@@ -323,11 +321,11 @@ class ActionPlanner(BasePlanner):
|
||||
else:
|
||||
chat_content_block = "你还未开始聊天"
|
||||
|
||||
mind_info_block = ""
|
||||
if current_mind:
|
||||
mind_info_block = f"对聊天的规划:{current_mind}"
|
||||
else:
|
||||
mind_info_block = "你刚参与聊天"
|
||||
# mind_info_block = ""
|
||||
# if current_mind:
|
||||
# mind_info_block = f"对聊天的规划:{current_mind}"
|
||||
# else:
|
||||
# mind_info_block = "你刚参与聊天"
|
||||
|
||||
personality_block = individuality.get_prompt(x_person=2, level=2)
|
||||
|
||||
@@ -388,7 +386,7 @@ class ActionPlanner(BasePlanner):
|
||||
prompt_personality=personality_block,
|
||||
chat_context_description=chat_context_description,
|
||||
chat_content_block=chat_content_block,
|
||||
mind_info_block=mind_info_block,
|
||||
# mind_info_block=mind_info_block,
|
||||
cycle_info_block=cycle_info,
|
||||
action_options_text=action_options_block,
|
||||
# action_available_block=action_available_block,
|
||||
|
||||
@@ -350,6 +350,7 @@ class DefaultReplyer:
|
||||
timestamp_mode="normal_no_YMD",
|
||||
read_mark=0.0,
|
||||
truncate=True,
|
||||
show_actions=True,
|
||||
)
|
||||
|
||||
(
|
||||
|
||||
@@ -66,7 +66,7 @@ class ChattingObservation(Observation):
|
||||
initial_messages = get_raw_msg_before_timestamp_with_chat(self.chat_id, self.last_observe_time, 10)
|
||||
self.last_observe_time = initial_messages[-1]["time"] if initial_messages else self.last_observe_time
|
||||
self.talking_message = initial_messages
|
||||
self.talking_message_str = build_readable_messages(self.talking_message)
|
||||
self.talking_message_str = build_readable_messages(self.talking_message, show_actions=True)
|
||||
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
@@ -220,7 +220,7 @@ class ChattingObservation(Observation):
|
||||
|
||||
# print(f"压缩中:oldest_messages: {oldest_messages}")
|
||||
oldest_messages_str = build_readable_messages(
|
||||
messages=oldest_messages, timestamp_mode="normal_no_YMD", read_mark=0
|
||||
messages=oldest_messages, timestamp_mode="normal_no_YMD", read_mark=0, show_actions=True
|
||||
)
|
||||
|
||||
# --- Build prompt using template ---
|
||||
@@ -267,6 +267,7 @@ class ChattingObservation(Observation):
|
||||
messages=self.talking_message,
|
||||
timestamp_mode="lite",
|
||||
read_mark=last_obs_time_mark,
|
||||
show_actions=True,
|
||||
)
|
||||
# print(f"构建中:self.talking_message_str: {self.talking_message_str}")
|
||||
self.talking_message_str_truncate = build_readable_messages(
|
||||
@@ -274,6 +275,7 @@ class ChattingObservation(Observation):
|
||||
timestamp_mode="normal_no_YMD",
|
||||
read_mark=last_obs_time_mark,
|
||||
truncate=True,
|
||||
show_actions=True,
|
||||
)
|
||||
# print(f"构建中:self.talking_message_str_truncate: {self.talking_message_str_truncate}")
|
||||
|
||||
|
||||
@@ -24,6 +24,8 @@ from src.chat.normal_chat.normal_chat_planner import NormalChatPlanner
|
||||
from src.chat.normal_chat.normal_chat_action_modifier import NormalChatActionModifier
|
||||
from src.chat.normal_chat.normal_chat_expressor import NormalChatExpressor
|
||||
from src.chat.focus_chat.replyer.default_replyer import DefaultReplyer
|
||||
from src.common.database.database_model import ActionRecords
|
||||
|
||||
|
||||
logger = get_logger("normal_chat")
|
||||
|
||||
@@ -442,7 +444,8 @@ 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()
|
||||
|
||||
@@ -656,13 +659,16 @@ class NormalChat:
|
||||
if action_handler:
|
||||
# 执行动作
|
||||
result = await action_handler.handle_action()
|
||||
success = False
|
||||
|
||||
if result and isinstance(result, tuple) and len(result) >= 2:
|
||||
# handle_action返回 (success: bool, message: str)
|
||||
success, _ = result[0], result[1]
|
||||
return success
|
||||
success = result[0]
|
||||
elif result:
|
||||
# 如果返回了其他结果,假设成功
|
||||
return True
|
||||
success = True
|
||||
|
||||
return success
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[{self.stream_name}] 执行动作 {action_type} 失败: {e}")
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
from re import A
|
||||
from typing import Dict, Any
|
||||
from rich.traceback import install
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
@@ -115,13 +116,6 @@ class NormalChatPlanner:
|
||||
}
|
||||
|
||||
# 构建normal_chat的上下文 (使用与normal_chat相同的prompt构建方法)
|
||||
# chat_context = await prompt_builder.build_prompt_normal(
|
||||
# enable_planner=True,
|
||||
# message_txt=message.processed_plain_text,
|
||||
# sender_name=sender_name,
|
||||
# chat_stream=message.chat_stream,
|
||||
# )
|
||||
|
||||
message_list_before_now = get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=message.chat_stream.stream_id,
|
||||
timestamp=time.time(),
|
||||
@@ -134,6 +128,7 @@ class NormalChatPlanner:
|
||||
merge_messages=False,
|
||||
timestamp_mode="relative",
|
||||
read_mark=0.0,
|
||||
show_actions=True,
|
||||
)
|
||||
|
||||
# 构建planner的prompt
|
||||
@@ -155,7 +150,6 @@ class NormalChatPlanner:
|
||||
try:
|
||||
content, reasoning_content, model_name = await self.planner_llm.generate_response(prompt)
|
||||
|
||||
|
||||
logger.info(f"{self.log_prefix}规划器原始提示词: {prompt}")
|
||||
logger.info(f"{self.log_prefix}规划器原始响应: {content}")
|
||||
|
||||
@@ -206,7 +200,21 @@ class NormalChatPlanner:
|
||||
f"{self.log_prefix}规划后恢复到默认动作集, 当前可用: {list(self.action_manager.get_using_actions().keys())}"
|
||||
)
|
||||
|
||||
action_result = {"action_type": action, "action_data": action_data, "reasoning": reasoning}
|
||||
# 构建 action 记录
|
||||
action_record = {
|
||||
"action_type": action,
|
||||
"action_data": action_data,
|
||||
"reasoning": reasoning,
|
||||
"timestamp": time.time(),
|
||||
"model_name": model_name if 'model_name' in locals() else None
|
||||
}
|
||||
|
||||
action_result = {
|
||||
"action_type": action,
|
||||
"action_data": action_data,
|
||||
"reasoning": reasoning,
|
||||
"action_record": json.dumps(action_record, ensure_ascii=False)
|
||||
}
|
||||
|
||||
plan_result = {
|
||||
"action_result": action_result,
|
||||
@@ -228,17 +236,25 @@ class NormalChatPlanner:
|
||||
action_options_text = ""
|
||||
|
||||
# 添加特殊的change_to_focus_chat动作
|
||||
action_options_text += "action_name: change_to_focus_chat\n"
|
||||
action_options_text += "动作:change_to_focus_chat\n"
|
||||
action_options_text += (
|
||||
" 描述:当聊天变得热烈、自己回复条数很多或需要深入交流时使用,正常回复消息并切换到focus_chat模式\n"
|
||||
"该动作的描述:当聊天变得热烈、自己回复条数很多或需要深入交流时使用,正常回复消息并切换到focus_chat模式\n"
|
||||
)
|
||||
action_options_text += " 参数:\n"
|
||||
action_options_text += " 动作要求:\n"
|
||||
action_options_text += " - 聊天上下文中自己的回复条数较多(超过3-4条)\n"
|
||||
action_options_text += " - 对话进行得非常热烈活跃\n"
|
||||
action_options_text += " - 用户表现出深入交流的意图\n"
|
||||
action_options_text += " - 话题需要更专注和深入的讨论\n\n"
|
||||
|
||||
action_options_text += "使用该动作的场景:\n"
|
||||
action_options_text += "- 聊天上下文中自己的回复条数较多(超过3-4条)\n"
|
||||
action_options_text += "- 对话进行得非常热烈活跃\n"
|
||||
action_options_text += "- 用户表现出深入交流的意图\n"
|
||||
action_options_text += "- 话题需要更专注和深入的讨论\n\n"
|
||||
|
||||
action_options_text += "输出要求:\n"
|
||||
action_options_text += "{{"
|
||||
action_options_text += " \"action\": \"change_to_focus_chat\""
|
||||
action_options_text += "}}\n\n"
|
||||
|
||||
|
||||
|
||||
|
||||
for action_name, action_info in current_available_actions.items():
|
||||
action_description = action_info.get("description", "")
|
||||
action_parameters = action_info.get("parameters", {})
|
||||
|
||||
@@ -184,6 +184,7 @@ class PromptBuilder:
|
||||
merge_messages=False,
|
||||
timestamp_mode="relative",
|
||||
read_mark=0.0,
|
||||
show_actions=True,
|
||||
)
|
||||
|
||||
# 关键词检测与反应
|
||||
|
||||
@@ -7,6 +7,7 @@ from src.common.message_repository import find_messages, count_messages
|
||||
from src.person_info.person_info import person_info_manager
|
||||
from src.chat.utils.utils import translate_timestamp_to_human_readable
|
||||
from rich.traceback import install
|
||||
from src.common.database.database_model import ActionRecords
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
@@ -177,6 +178,15 @@ def _build_readable_messages_internal(
|
||||
|
||||
# 1 & 2: 获取发送者信息并提取消息组件
|
||||
for msg in messages:
|
||||
# 检查是否是动作记录
|
||||
if msg.get("is_action_record", False):
|
||||
is_action = True
|
||||
timestamp = msg.get("time")
|
||||
content = msg.get("display_message", "")
|
||||
# 对于动作记录,直接使用内容
|
||||
message_details_raw.append((timestamp, global_config.bot.nickname, content, is_action))
|
||||
continue
|
||||
|
||||
# 检查并修复缺少的user_info字段
|
||||
if "user_info" not in msg:
|
||||
# 创建user_info字段
|
||||
@@ -263,18 +273,32 @@ def _build_readable_messages_internal(
|
||||
content = content.replace(target_str, "")
|
||||
|
||||
if content != "":
|
||||
message_details_raw.append((timestamp, person_name, content))
|
||||
message_details_raw.append((timestamp, person_name, content, False))
|
||||
|
||||
if not message_details_raw:
|
||||
return "", []
|
||||
|
||||
message_details_raw.sort(key=lambda x: x[0]) # 按时间戳(第一个元素)升序排序,越早的消息排在前面
|
||||
|
||||
# 为每条消息添加一个标记,指示它是否是动作记录
|
||||
message_details_with_flags = []
|
||||
for timestamp, name, content, is_action in message_details_raw:
|
||||
message_details_with_flags.append((timestamp, name, content, is_action))
|
||||
# print(f"content:{content}")
|
||||
# print(f"is_action:{is_action}")
|
||||
|
||||
# print(f"message_details_with_flags:{message_details_with_flags}")
|
||||
|
||||
# 应用截断逻辑 (如果 truncate 为 True)
|
||||
message_details: List[Tuple[float, str, str]] = []
|
||||
n_messages = len(message_details_raw)
|
||||
message_details: List[Tuple[float, str, str, bool]] = []
|
||||
n_messages = len(message_details_with_flags)
|
||||
if truncate and n_messages > 0:
|
||||
for i, (timestamp, name, content) in enumerate(message_details_raw):
|
||||
for i, (timestamp, name, content, is_action) in enumerate(message_details_with_flags):
|
||||
# 对于动作记录,不进行截断
|
||||
if is_action:
|
||||
message_details.append((timestamp, name, content, is_action))
|
||||
continue
|
||||
|
||||
percentile = i / n_messages # 计算消息在列表中的位置百分比 (0 <= percentile < 1)
|
||||
original_len = len(content)
|
||||
limit = -1 # 默认不截断
|
||||
@@ -296,10 +320,12 @@ def _build_readable_messages_internal(
|
||||
if 0 < limit < original_len:
|
||||
truncated_content = f"{content[:limit]}{replace_content}"
|
||||
|
||||
message_details.append((timestamp, name, truncated_content))
|
||||
message_details.append((timestamp, name, truncated_content, is_action))
|
||||
else:
|
||||
# 如果不截断,直接使用原始列表
|
||||
message_details = message_details_raw
|
||||
message_details = message_details_with_flags
|
||||
|
||||
# print(f"message_details:{message_details}")
|
||||
|
||||
# 3: 合并连续消息 (如果 merge_messages 为 True)
|
||||
merged_messages = []
|
||||
@@ -310,10 +336,26 @@ def _build_readable_messages_internal(
|
||||
"start_time": message_details[0][0],
|
||||
"end_time": message_details[0][0],
|
||||
"content": [message_details[0][2]],
|
||||
"is_action": message_details[0][3]
|
||||
}
|
||||
|
||||
for i in range(1, len(message_details)):
|
||||
timestamp, name, content = message_details[i]
|
||||
timestamp, name, content, is_action = message_details[i]
|
||||
|
||||
# 对于动作记录,不进行合并
|
||||
if is_action or current_merge["is_action"]:
|
||||
# 保存当前的合并块
|
||||
merged_messages.append(current_merge)
|
||||
# 创建新的块
|
||||
current_merge = {
|
||||
"name": name,
|
||||
"start_time": timestamp,
|
||||
"end_time": timestamp,
|
||||
"content": [content],
|
||||
"is_action": is_action
|
||||
}
|
||||
continue
|
||||
|
||||
# 如果是同一个人发送的连续消息且时间间隔小于等于60秒
|
||||
if name == current_merge["name"] and (timestamp - current_merge["end_time"] <= 60):
|
||||
current_merge["content"].append(content)
|
||||
@@ -322,19 +364,27 @@ def _build_readable_messages_internal(
|
||||
# 保存上一个合并块
|
||||
merged_messages.append(current_merge)
|
||||
# 开始新的合并块
|
||||
current_merge = {"name": name, "start_time": timestamp, "end_time": timestamp, "content": [content]}
|
||||
current_merge = {
|
||||
"name": name,
|
||||
"start_time": timestamp,
|
||||
"end_time": timestamp,
|
||||
"content": [content],
|
||||
"is_action": is_action
|
||||
}
|
||||
# 添加最后一个合并块
|
||||
merged_messages.append(current_merge)
|
||||
elif message_details: # 如果不合并消息,则每个消息都是一个独立的块
|
||||
for timestamp, name, content in message_details:
|
||||
for timestamp, name, content, is_action in message_details:
|
||||
merged_messages.append(
|
||||
{
|
||||
"name": name,
|
||||
"start_time": timestamp, # 起始和结束时间相同
|
||||
"end_time": timestamp,
|
||||
"content": [content], # 内容只有一个元素
|
||||
"is_action": is_action
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# 4 & 5: 格式化为字符串
|
||||
output_lines = []
|
||||
@@ -342,20 +392,25 @@ def _build_readable_messages_internal(
|
||||
# 使用指定的 timestamp_mode 格式化时间
|
||||
readable_time = translate_timestamp_to_human_readable(merged["start_time"], mode=timestamp_mode)
|
||||
|
||||
header = f"{readable_time}, {merged['name']} :"
|
||||
output_lines.append(header)
|
||||
# 将内容合并,并添加缩进
|
||||
for line in merged["content"]:
|
||||
stripped_line = line.strip()
|
||||
if stripped_line: # 过滤空行
|
||||
# 移除末尾句号,添加分号 - 这个逻辑似乎有点奇怪,暂时保留
|
||||
if stripped_line.endswith("。"):
|
||||
stripped_line = stripped_line[:-1]
|
||||
# 如果内容被截断,结尾已经是 ...(内容太长),不再添加分号
|
||||
if not stripped_line.endswith("(内容太长)"):
|
||||
output_lines.append(f"{stripped_line}")
|
||||
else:
|
||||
output_lines.append(stripped_line) # 直接添加截断后的内容
|
||||
# 检查是否是动作记录
|
||||
if merged["is_action"]:
|
||||
# 对于动作记录,使用特殊格式
|
||||
output_lines.append(f"{readable_time}, {merged['content'][0]}")
|
||||
else:
|
||||
header = f"{readable_time}, {merged['name']} :"
|
||||
output_lines.append(header)
|
||||
# 将内容合并,并添加缩进
|
||||
for line in merged["content"]:
|
||||
stripped_line = line.strip()
|
||||
if stripped_line: # 过滤空行
|
||||
# 移除末尾句号,添加分号 - 这个逻辑似乎有点奇怪,暂时保留
|
||||
if stripped_line.endswith("。"):
|
||||
stripped_line = stripped_line[:-1]
|
||||
# 如果内容被截断,结尾已经是 ...(内容太长),不再添加分号
|
||||
if not stripped_line.endswith("(内容太长)"):
|
||||
output_lines.append(f"{stripped_line}")
|
||||
else:
|
||||
output_lines.append(stripped_line) # 直接添加截断后的内容
|
||||
output_lines.append("\n") # 在每个消息块后添加换行,保持可读性
|
||||
|
||||
# 移除可能的多余换行,然后合并
|
||||
@@ -363,7 +418,7 @@ def _build_readable_messages_internal(
|
||||
|
||||
# 返回格式化后的字符串和 *应用截断后* 的 message_details 列表
|
||||
# 注意:如果外部调用者需要原始未截断的内容,可能需要调整返回策略
|
||||
return formatted_string, message_details
|
||||
return formatted_string, [(t, n, c) for t, n, c, is_action in message_details if not is_action]
|
||||
|
||||
|
||||
async def build_readable_messages_with_list(
|
||||
@@ -390,26 +445,88 @@ def build_readable_messages(
|
||||
timestamp_mode: str = "relative",
|
||||
read_mark: float = 0.0,
|
||||
truncate: bool = False,
|
||||
show_actions: bool = False,
|
||||
) -> str:
|
||||
"""
|
||||
将消息列表转换为可读的文本格式。
|
||||
如果提供了 read_mark,则在相应位置插入已读标记。
|
||||
允许通过参数控制格式化行为。
|
||||
|
||||
Args:
|
||||
messages: 消息列表
|
||||
replace_bot_name: 是否替换机器人名称为"你"
|
||||
merge_messages: 是否合并连续消息
|
||||
timestamp_mode: 时间戳显示模式
|
||||
read_mark: 已读标记时间戳
|
||||
truncate: 是否截断长消息
|
||||
show_actions: 是否显示动作记录
|
||||
"""
|
||||
# 创建messages的深拷贝,避免修改原始列表
|
||||
copy_messages = [msg.copy() for msg in messages]
|
||||
|
||||
if show_actions and copy_messages:
|
||||
# 获取所有消息的时间范围
|
||||
min_time = min(msg.get("time", 0) for msg in copy_messages)
|
||||
max_time = max(msg.get("time", 0) for msg in copy_messages)
|
||||
|
||||
# 从第一条消息中获取chat_id
|
||||
chat_id = copy_messages[0].get("chat_id") if copy_messages else None
|
||||
|
||||
# 获取这个时间范围内的动作记录,并匹配chat_id
|
||||
actions = ActionRecords.select().where(
|
||||
(ActionRecords.time >= min_time) &
|
||||
(ActionRecords.time <= max_time) &
|
||||
(ActionRecords.chat_id == chat_id)
|
||||
).order_by(ActionRecords.time)
|
||||
|
||||
# 将动作记录转换为消息格式
|
||||
for action in actions:
|
||||
# 只有当build_into_prompt为True时才添加动作记录
|
||||
if action.action_build_into_prompt:
|
||||
action_msg = {
|
||||
"time": action.time,
|
||||
"user_id": global_config.bot.qq_account, # 使用机器人的QQ账号
|
||||
"user_nickname": global_config.bot.nickname, # 使用机器人的昵称
|
||||
"user_cardname": "", # 机器人没有群名片
|
||||
"processed_plain_text": f"{action.action_prompt_display}",
|
||||
"display_message": f"{action.action_prompt_display}",
|
||||
"chat_info_platform": action.chat_info_platform,
|
||||
"is_action_record": True, # 添加标识字段
|
||||
"action_name": action.action_name, # 保存动作名称
|
||||
}
|
||||
copy_messages.append(action_msg)
|
||||
|
||||
# 重新按时间排序
|
||||
copy_messages.sort(key=lambda x: x.get("time", 0))
|
||||
|
||||
if read_mark <= 0:
|
||||
# 没有有效的 read_mark,直接格式化所有消息
|
||||
|
||||
# for message in messages:
|
||||
# print(f"message:{message}")
|
||||
|
||||
|
||||
formatted_string, _ = _build_readable_messages_internal(
|
||||
messages, replace_bot_name, merge_messages, timestamp_mode, truncate
|
||||
copy_messages, replace_bot_name, merge_messages, timestamp_mode, truncate
|
||||
)
|
||||
|
||||
# print(f"formatted_string:{formatted_string}")
|
||||
|
||||
|
||||
|
||||
return formatted_string
|
||||
else:
|
||||
# 按 read_mark 分割消息
|
||||
messages_before_mark = [msg for msg in messages if msg.get("time", 0) <= read_mark]
|
||||
messages_after_mark = [msg for msg in messages if msg.get("time", 0) > read_mark]
|
||||
messages_before_mark = [msg for msg in copy_messages if msg.get("time", 0) <= read_mark]
|
||||
messages_after_mark = [msg for msg in copy_messages if msg.get("time", 0) > read_mark]
|
||||
|
||||
# for message in messages_before_mark:
|
||||
# print(f"message:{message}")
|
||||
|
||||
# for message in messages_after_mark:
|
||||
# print(f"message:{message}")
|
||||
|
||||
# 分别格式化
|
||||
# 注意:这里决定对已读和未读部分都应用相同的 truncate 设置
|
||||
# 如果需要不同的行为(例如只截断已读部分),需要调整这里的调用
|
||||
formatted_before, _ = _build_readable_messages_internal(
|
||||
messages_before_mark, replace_bot_name, merge_messages, timestamp_mode, truncate
|
||||
)
|
||||
@@ -419,11 +536,13 @@ def build_readable_messages(
|
||||
merge_messages,
|
||||
timestamp_mode,
|
||||
)
|
||||
|
||||
# print(f"formatted_before:{formatted_before}")
|
||||
# print(f"formatted_after:{formatted_after}")
|
||||
|
||||
# readable_read_mark = translate_timestamp_to_human_readable(read_mark, mode=timestamp_mode)
|
||||
read_mark_line = "\n--- 以上消息是你已经看过---\n--- 请关注以下未读的新消息---\n"
|
||||
|
||||
# 组合结果,确保空部分不引入多余的标记或换行
|
||||
# 组合结果
|
||||
if formatted_before and formatted_after:
|
||||
return f"{formatted_before}{read_mark_line}{formatted_after}"
|
||||
elif formatted_before:
|
||||
@@ -431,8 +550,7 @@ def build_readable_messages(
|
||||
elif formatted_after:
|
||||
return f"{read_mark_line}{formatted_after}"
|
||||
else:
|
||||
# 理论上不应该发生,但作为保险
|
||||
return read_mark_line.strip() # 如果前后都无消息,只返回标记行
|
||||
return read_mark_line.strip()
|
||||
|
||||
|
||||
async def build_anonymous_messages(messages: List[Dict[str, Any]]) -> str:
|
||||
|
||||
@@ -154,6 +154,29 @@ class Messages(BaseModel):
|
||||
class Meta:
|
||||
# database = db # 继承自 BaseModel
|
||||
table_name = "messages"
|
||||
|
||||
class ActionRecords(BaseModel):
|
||||
"""
|
||||
用于存储动作记录数据的模型。
|
||||
"""
|
||||
|
||||
action_id = TextField(index=True) # 消息 ID (更改自 IntegerField)
|
||||
time = DoubleField() # 消息时间戳
|
||||
|
||||
action_name = TextField()
|
||||
action_data = TextField()
|
||||
action_done = BooleanField(default=False)
|
||||
|
||||
action_build_into_prompt = BooleanField(default=False)
|
||||
action_prompt_display = TextField()
|
||||
|
||||
chat_id = TextField(index=True) # 对应的 ChatStreams stream_id
|
||||
chat_info_stream_id = TextField()
|
||||
chat_info_platform = TextField()
|
||||
|
||||
class Meta:
|
||||
# database = db # 继承自 BaseModel
|
||||
table_name = "action_records"
|
||||
|
||||
|
||||
class Images(BaseModel):
|
||||
@@ -326,6 +349,7 @@ def create_tables():
|
||||
RecalledMessages, # 添加新模型
|
||||
GraphNodes, # 添加图节点表
|
||||
GraphEdges, # 添加图边表
|
||||
ActionRecords, # 添加 ActionRecords 到初始化列表
|
||||
]
|
||||
)
|
||||
|
||||
@@ -350,6 +374,7 @@ def initialize_database():
|
||||
RecalledMessages,
|
||||
GraphNodes,
|
||||
GraphEdges,
|
||||
ActionRecords, # 添加 ActionRecords 到初始化列表
|
||||
]
|
||||
|
||||
try:
|
||||
|
||||
@@ -46,7 +46,7 @@ TEMPLATE_DIR = "template"
|
||||
|
||||
# 考虑到,实际上配置文件中的mai_version是不会自动更新的,所以采用硬编码
|
||||
# 对该字段的更新,请严格参照语义化版本规范:https://semver.org/lang/zh-CN/
|
||||
MMC_VERSION = "0.7.1-snapshot.1"
|
||||
MMC_VERSION = "0.7.2-snapshot.1"
|
||||
|
||||
|
||||
def update_config():
|
||||
|
||||
@@ -16,7 +16,7 @@ class ImpressionUpdateTask(AsyncTask):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
task_name="impression_update",
|
||||
wait_before_start=10, # 启动后等待10秒
|
||||
wait_before_start=100, # 启动后等待10秒
|
||||
run_interval=600, # 每1分钟运行一次
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user