From f262b8b224b8b5987ba2a172e2d6e95a9358a5f6 Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Sat, 11 Oct 2025 19:02:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(cross=5Fcontext):=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=BA=E8=B7=A8=E4=B8=8A=E4=B8=8B=E6=96=87=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=B6=88=E6=81=AF=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 允许用户在配置文件中为每个聊天流(群聊或私聊)单独设置要检索的历史消息数量(limit)。如果未指定,则默认为5条。 - 更新了 `build_cross_context_normal` 和 `build_cross_context_s4u` 方法以解析和使用这个新的 `limit` 参数。 - 在S4U(Search for User)模式下,获取的消息数量调整为 `limit` 的4倍,以确保有足够的消息可供筛选。 - 更新了配置文件模板 `bot_config_template.toml` 以反映此项新功能,并提供了示例。 --- src/plugin_system/apis/cross_context_api.py | 22 ++++++++++++++------- template/bot_config_template.toml | 11 ++++++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/plugin_system/apis/cross_context_api.py b/src/plugin_system/apis/cross_context_api.py index eed13697c..43286a5b1 100644 --- a/src/plugin_system/apis/cross_context_api.py +++ b/src/plugin_system/apis/cross_context_api.py @@ -50,7 +50,8 @@ async def build_cross_context_normal(chat_stream: ChatStream, other_chat_infos: 构建跨群聊/私聊上下文 (Normal模式) """ cross_context_messages = [] - for chat_type, chat_raw_id in other_chat_infos: + for chat_info in other_chat_infos: + chat_type, chat_raw_id, limit = chat_info[0], chat_info[1], int(chat_info[2]) if len(chat_info) > 2 else 5 is_group = chat_type == "group" stream_id = get_chat_manager().get_stream_id(chat_stream.platform, chat_raw_id, is_group=is_group) if not stream_id: @@ -60,7 +61,7 @@ async def build_cross_context_normal(chat_stream: ChatStream, other_chat_infos: messages = await get_raw_msg_before_timestamp_with_chat( chat_id=stream_id, timestamp=time.time(), - limit=5, # 可配置 + limit=limit, ) if messages: chat_name = await get_chat_manager().get_stream_name(stream_id) or chat_raw_id @@ -89,19 +90,25 @@ async def build_cross_context_s4u( user_id = target_user_info.get("user_id") if user_id: - for chat_type, chat_raw_id in other_chat_infos: + for chat_info in other_chat_infos: + chat_type, chat_raw_id, limit = ( + chat_info[0], + chat_info[1], + int(chat_info[2]) if len(chat_info) > 2 else 5, + ) is_group = chat_type == "group" stream_id = get_chat_manager().get_stream_id(chat_stream.platform, chat_raw_id, is_group=is_group) if not stream_id: continue try: + # S4U模式下,我们获取更多消息以供筛选 messages = await get_raw_msg_before_timestamp_with_chat( chat_id=stream_id, timestamp=time.time(), - limit=20, # 获取更多消息以供筛选 + limit=limit * 4, # 获取4倍limit的消息以供筛选 ) - user_messages = [msg for msg in messages if msg.get("user_id") == user_id][-5:] + user_messages = [msg for msg in messages if msg.get("user_id") == user_id][-limit:] if user_messages: chat_name = await get_chat_manager().get_stream_name(stream_id) or chat_raw_id @@ -144,7 +151,8 @@ async def get_chat_history_by_group_name(group_name: str) -> str: chat_manager = get_chat_manager() cross_context_messages = [] - for chat_type, chat_raw_id in chat_infos: + for chat_info in chat_infos: + chat_type, chat_raw_id, limit = chat_info[0], chat_info[1], int(chat_info[2]) if len(chat_info) > 2 else 5 is_group = chat_type == "group" found_stream = None @@ -168,7 +176,7 @@ async def get_chat_history_by_group_name(group_name: str) -> str: messages = await get_raw_msg_before_timestamp_with_chat( chat_id=stream_id, timestamp=time.time(), - limit=5, # 可配置 + limit=limit, ) if messages: chat_name = await get_chat_manager().get_stream_name(stream_id) or chat_raw_id diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index de20a2e0d..8dce0eb00 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "7.2.6" +version = "7.2.7" #----以下是给开发人员阅读的,如果你只是部署了MoFox-Bot,不需要阅读---- #如果你想要修改配置文件,请递增version的值 @@ -547,14 +547,15 @@ insomnia_trigger_delay_minutes = [15, 45] enable = true # 在这里定义您的“共享组” # 只有在同一个组内的聊天才会共享上下文 -# 格式:chat_ids = [["type", "id"], ["type", "id"], ...] +# 格式:chat_ids = [["type", "id", limit (可选)], ["type", "id", limit (可选)], ...] # type 可选 "group" 或 "private" +# limit 是一个可选的整数,用于指定从该聊天流中获取的消息数量,如果未指定,默认为5 [[cross_context.groups]] name = "项目A技术讨论组" chat_ids = [ - ["group", "169850076"], # 假设这是“开发群”的ID - ["group", "1025509724"], # 假设这是“产品群”的ID - ["private", "123456789"] # 假设这是某个用户的私聊 + ["group", "169850076", 10], # 假设这是“开发群”的ID, 从这个群里拿10条消息 + ["group", "1025509724", 5], # 假设这是“产品群”的ID,拿5条 + ["private", "123456789"] # 假设这是某个用户的私聊,使用默认值5 ] # 定义QQ空间互通组 # 同一个组的chat_id会共享上下文,用于生成更相关的说说