diff --git a/commands/handlers.py b/commands/handlers.py index 32b3a0f..59d7c84 100644 --- a/commands/handlers.py +++ b/commands/handlers.py @@ -241,6 +241,14 @@ def handle_chitchat(ctx: 'MessageContext', match: Optional[Match]) -> bool: context_window = arguments.get("context_window", 5) max_results = arguments.get("max_results", 20) + print(f"[search_chat_history] chat_id={chat_id}, keywords={deduped_keywords}, " + f"context_window={context_window}, max_results={max_results}") + if ctx.logger: + ctx.logger.info( + f"[search_chat_history] keywords={deduped_keywords}, " + f"context_window={context_window}, max_results={max_results}" + ) + search_results = message_summary.search_messages_with_context( chat_id=chat_id, keywords=deduped_keywords, @@ -254,6 +262,10 @@ def handle_chitchat(ctx: 'MessageContext', match: Optional[Match]) -> bool: "keywords": deduped_keywords } + print(f"[search_chat_history] returned_groups={len(search_results)}") + if ctx.logger: + ctx.logger.info(f"[search_chat_history] returned_groups={len(search_results)}") + if not search_results: response_payload["notice"] = "No messages matched the provided keywords." diff --git a/function/func_summary.py b/function/func_summary.py index 34df439..1dfbd82 100644 --- a/function/func_summary.py +++ b/function/func_summary.py @@ -9,12 +9,15 @@ import sqlite3 # 添加sqlite3模块 import os # 用于处理文件路径 from function.func_xml_process import XmlProcessor # 导入XmlProcessor +MAX_DB_HISTORY_LIMIT = 10000 + + class MessageSummary: """消息总结功能类 (使用SQLite持久化) 用于记录、管理和生成聊天历史消息的总结 """ - def __init__(self, max_history=200, db_path="data/message_history.db"): # 默认max_history 改为 200 + def __init__(self, max_history=MAX_DB_HISTORY_LIMIT, db_path="data/message_history.db"): """初始化消息总结功能 Args: @@ -22,7 +25,21 @@ class MessageSummary: db_path: SQLite数据库文件路径 """ self.LOG = logging.getLogger("MessageSummary") - self.max_history = max_history # 使用传入的 max_history + try: + parsed_history_limit = int(max_history) + except (TypeError, ValueError): + parsed_history_limit = MAX_DB_HISTORY_LIMIT + + if parsed_history_limit <= 0: + parsed_history_limit = MAX_DB_HISTORY_LIMIT + + if parsed_history_limit > MAX_DB_HISTORY_LIMIT: + self.LOG.warning( + f"传入的 max_history={parsed_history_limit} 超过上限,将截断为 {MAX_DB_HISTORY_LIMIT}" + ) + parsed_history_limit = MAX_DB_HISTORY_LIMIT + + self.max_history = parsed_history_limit self.db_path = db_path # 实例化XML处理器用于提取引用消息