From acd0952857fed486c7affc5f8ebd375ef0c2aece Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Fri, 19 Dec 2025 18:18:38 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=81=9A=E6=A2=A6=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E7=A7=81=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/official_configs.py | 7 +++ src/dream/dream_generator.py | 52 ++++++++++++++++++- src/dream/tools/__init__.py | 1 + src/dream/tools/create_chat_history_tool.py | 1 + src/dream/tools/delete_chat_history_tool.py | 1 + src/dream/tools/delete_jargon_tool.py | 1 + src/dream/tools/finish_maintenance_tool.py | 1 + .../tools/get_chat_history_detail_tool.py | 1 + src/dream/tools/search_chat_history_tool.py | 1 + src/dream/tools/update_chat_history_tool.py | 1 + src/dream/tools/update_jargon_tool.py | 1 + template/bot_config_template.toml | 7 ++- 12 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/config/official_configs.py b/src/config/official_configs.py index de4bc1da..62c851b4 100644 --- a/src/config/official_configs.py +++ b/src/config/official_configs.py @@ -736,6 +736,13 @@ class DreamConfig(ConfigBase): first_delay_seconds: int = 60 """程序启动后首次做梦前的延迟时间(秒),默认60秒""" + dream_send: str = "" + """ + 做梦结果推送目标,格式为 "platform:user_id" + 例如: "qq:123456" 表示在做梦结束后,将梦境文本额外发送给该QQ私聊用户。 + 为空字符串时不推送。 + """ + dream_time_ranges: list[str] = field(default_factory=lambda: []) """ 做梦时间段配置列表,格式:["HH:MM-HH:MM", ...] diff --git a/src/dream/dream_generator.py b/src/dream/dream_generator.py index 945aebf9..6207f7e7 100644 --- a/src/dream/dream_generator.py +++ b/src/dream/dream_generator.py @@ -2,10 +2,12 @@ import random from typing import List, Optional from src.common.logger import get_logger -from src.config.config import model_config +from src.config.config import global_config, model_config from src.chat.utils.prompt_builder import Prompt from src.llm_models.payload_content.message import RoleType, Message from src.llm_models.utils_model import LLMRequest +from src.chat.message_receive.chat_stream import get_chat_manager +from src.plugin_system.apis import send_api logger = get_logger("dream_generator") @@ -82,7 +84,7 @@ async def generate_dream_summary( total_iterations: int, time_cost: float, ) -> None: - """生成梦境总结并输出到日志""" + """生成梦境总结,输出到日志,并根据配置可选地推送给指定用户""" try: import json from src.chat.utils.prompt_builder import global_prompt_manager @@ -193,6 +195,52 @@ async def generate_dream_summary( if dream_content: logger.info(f"[dream][梦境总结] 对 chat_id={chat_id} 的整理过程梦境:\n{dream_content}") + + # 第五步:根据配置决定是否将梦境发送给指定用户 + try: + dream_send_raw = getattr(global_config.dream, "dream_send", "") or "" + dream_send = dream_send_raw.strip() + if dream_send: + parts = dream_send.split(":") + if len(parts) != 2: + logger.warning( + f"[dream][梦境总结] dream_send 配置格式不正确,应为 'platform:user_id',当前值: {dream_send_raw!r}" + ) + else: + platform, user_id = parts[0].strip(), parts[1].strip() + if not platform or not user_id: + logger.warning( + f"[dream][梦境总结] dream_send 平台或用户ID为空,当前值: {dream_send_raw!r}" + ) + else: + # 默认为私聊会话 + stream_id = get_chat_manager().get_stream_id( + platform=platform, + id_str=str(user_id), + is_group=False, + ) + if not stream_id: + logger.error( + f"[dream][梦境总结] 无法根据 dream_send 找到有效的聊天流," + f"platform={platform!r}, user_id={user_id!r}" + ) + else: + ok = await send_api.text_to_stream( + dream_content, + stream_id=stream_id, + typing=False, + storage_message=True, + ) + if ok: + logger.info( + f"[dream][梦境总结] 已将梦境结果发送给配置的目标用户: {platform}:{user_id}" + ) + else: + logger.error( + f"[dream][梦境总结] 向 {platform}:{user_id} 发送梦境结果失败" + ) + except Exception as send_exc: + logger.error(f"[dream][梦境总结] 发送梦境结果到配置用户时出错: {send_exc}", exc_info=True) else: logger.warning("[dream][梦境总结] 未能生成梦境总结") diff --git a/src/dream/tools/__init__.py b/src/dream/tools/__init__.py index cd784b02..da5c237e 100644 --- a/src/dream/tools/__init__.py +++ b/src/dream/tools/__init__.py @@ -4,3 +4,4 @@ dream agent 工具实现模块。 每个工具的具体实现放在独立文件中,通过 make_xxx(chat_id) 工厂函数 生成绑定到特定 chat_id 的协程函数,由 dream_agent.init_dream_tools 统一注册。 """ + diff --git a/src/dream/tools/create_chat_history_tool.py b/src/dream/tools/create_chat_history_tool.py index 551b7ec6..ccf27e4e 100644 --- a/src/dream/tools/create_chat_history_tool.py +++ b/src/dream/tools/create_chat_history_tool.py @@ -60,3 +60,4 @@ def make_create_chat_history(chat_id: str): return f"create_chat_history 执行失败: {e}" return create_chat_history + diff --git a/src/dream/tools/delete_chat_history_tool.py b/src/dream/tools/delete_chat_history_tool.py index 18c32f27..6a14d57f 100644 --- a/src/dream/tools/delete_chat_history_tool.py +++ b/src/dream/tools/delete_chat_history_tool.py @@ -23,3 +23,4 @@ def make_delete_chat_history(chat_id: str): # chat_id 目前未直接使用, return f"delete_chat_history 执行失败: {e}" return delete_chat_history + diff --git a/src/dream/tools/delete_jargon_tool.py b/src/dream/tools/delete_jargon_tool.py index 8edd3245..cfab51aa 100644 --- a/src/dream/tools/delete_jargon_tool.py +++ b/src/dream/tools/delete_jargon_tool.py @@ -23,3 +23,4 @@ def make_delete_jargon(chat_id: str): # chat_id 目前未直接使用,预留 return f"delete_jargon 执行失败: {e}" return delete_jargon + diff --git a/src/dream/tools/finish_maintenance_tool.py b/src/dream/tools/finish_maintenance_tool.py index 403b6c6e..c2dda545 100644 --- a/src/dream/tools/finish_maintenance_tool.py +++ b/src/dream/tools/finish_maintenance_tool.py @@ -14,3 +14,4 @@ def make_finish_maintenance(chat_id: str): # chat_id 目前未直接使用, return msg return finish_maintenance + diff --git a/src/dream/tools/get_chat_history_detail_tool.py b/src/dream/tools/get_chat_history_detail_tool.py index 92f1d4d9..47c427d0 100644 --- a/src/dream/tools/get_chat_history_detail_tool.py +++ b/src/dream/tools/get_chat_history_detail_tool.py @@ -42,3 +42,4 @@ def make_get_chat_history_detail(chat_id: str): # chat_id 目前未直接使用 return f"get_chat_history_detail 执行失败: {e}" return get_chat_history_detail + diff --git a/src/dream/tools/search_chat_history_tool.py b/src/dream/tools/search_chat_history_tool.py index 5d216f00..1de0f253 100644 --- a/src/dream/tools/search_chat_history_tool.py +++ b/src/dream/tools/search_chat_history_tool.py @@ -212,3 +212,4 @@ def make_search_chat_history(chat_id: str): return f"search_chat_history 执行失败: {e}" return search_chat_history + diff --git a/src/dream/tools/update_chat_history_tool.py b/src/dream/tools/update_chat_history_tool.py index a65e78a7..c2e92fb9 100644 --- a/src/dream/tools/update_chat_history_tool.py +++ b/src/dream/tools/update_chat_history_tool.py @@ -49,3 +49,4 @@ def make_update_chat_history(chat_id: str): # chat_id 目前未直接使用, return f"update_chat_history 执行失败: {e}" return update_chat_history + diff --git a/src/dream/tools/update_jargon_tool.py b/src/dream/tools/update_jargon_tool.py index 1d559cf6..59ea1230 100644 --- a/src/dream/tools/update_jargon_tool.py +++ b/src/dream/tools/update_jargon_tool.py @@ -49,3 +49,4 @@ def make_update_jargon(chat_id: str): # chat_id 目前未直接使用,预留 return f"update_jargon 执行失败: {e}" return update_jargon + diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index 27da018f..b4543433 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "7.2.2" +version = "7.2.3" #----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读---- # 如果你想要修改配置文件,请递增version的值 @@ -129,6 +129,11 @@ interval_minutes = 60 # 做梦时间间隔(分钟),默认30分钟 max_iterations = 20 # 做梦最大轮次,默认20轮 first_delay_seconds = 1800 # 程序启动后首次做梦前的延迟时间(秒),默认60秒 +# 做梦结果推送目标,格式为 "platform:user_id" +# 例如: "qq:123456" 表示在做梦结束后,将梦境文本额外发送给该QQ私聊用户。 +# 为空字符串时不推送。 +dream_send = "" + # 做梦时间段配置,格式:["HH:MM-HH:MM", ...] # 如果列表为空,则表示全天允许做梦。 # 如果配置了时间段,则只有在这些时间段内才会实际执行做梦流程。