Merge branch 'main' of https://gitea.functen.cn/shihao/WechatHookBot
This commit is contained in:
@@ -167,8 +167,24 @@ class ChatRoomSummary(PluginBase):
|
||||
logger.info(f"群聊 {group_id} {time_desc}消息数量不足 ({len(messages)} < {self.config['behavior']['min_messages']})")
|
||||
return None
|
||||
|
||||
max_messages = self.config.get("behavior", {}).get("max_messages", 1200)
|
||||
try:
|
||||
max_messages = int(max_messages)
|
||||
except Exception:
|
||||
max_messages = 1200
|
||||
|
||||
if max_messages > 0 and len(messages) > max_messages:
|
||||
logger.info(f"群聊 {group_id} {time_desc}消息过多,截断为最近 {max_messages} 条")
|
||||
messages = messages[-max_messages:]
|
||||
|
||||
formatted_messages = self._format_messages(messages)
|
||||
summary = await self._call_ai_api(formatted_messages, group_id, time_desc)
|
||||
if not summary and len(messages) > 300:
|
||||
fallback_count = 300
|
||||
logger.warning(f"群聊 {group_id} {time_desc}总结失败,尝试缩减为最近 {fallback_count} 条重试")
|
||||
trimmed_messages = messages[-fallback_count:]
|
||||
formatted_messages = self._format_messages(trimmed_messages)
|
||||
summary = await self._call_ai_api(formatted_messages, group_id, time_desc)
|
||||
return summary
|
||||
|
||||
except Exception as e:
|
||||
@@ -237,6 +253,11 @@ class ChatRoomSummary(PluginBase):
|
||||
def _format_messages(self, messages: List[Dict]) -> str:
|
||||
"""格式化消息为AI可理解的格式"""
|
||||
formatted_lines = []
|
||||
max_length = self.config.get("behavior", {}).get("max_message_length", 200)
|
||||
try:
|
||||
max_length = int(max_length)
|
||||
except Exception:
|
||||
max_length = 200
|
||||
|
||||
for msg in messages:
|
||||
create_time = msg['create_time']
|
||||
@@ -247,8 +268,8 @@ class ChatRoomSummary(PluginBase):
|
||||
|
||||
nickname = msg.get('nickname') or msg['sender_wxid'][-8:]
|
||||
content = msg['content'].replace('\n', '。').strip()
|
||||
if len(content) > 200:
|
||||
content = content[:200] + "..."
|
||||
if max_length > 0 and len(content) > max_length:
|
||||
content = content[:max_length] + "..."
|
||||
|
||||
formatted_line = f'[{time_str}] {{"{nickname}": "{content}"}}--end--'
|
||||
formatted_lines.append(formatted_line)
|
||||
@@ -648,7 +669,7 @@ class ChatRoomSummary(PluginBase):
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "generate_summary",
|
||||
"description": "生成群聊总结,可以选择今日或昨日的聊天记录",
|
||||
"description": "仅当用户明确要求“群聊总结/今日总结/昨日总结”时调用;不要在闲聊或无总结需求时触发。",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -705,4 +726,4 @@ class ChatRoomSummary(PluginBase):
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"LLM工具执行失败: {e}")
|
||||
return {"success": False, "message": f"执行失败: {str(e)}"}
|
||||
return {"success": False, "message": f"执行失败: {str(e)}"}
|
||||
|
||||
Reference in New Issue
Block a user