koishi-plugin-group-analysis-mx 1.7.8 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -847,21 +847,23 @@ class AnalysisService extends koishi_1.Service {
847
847
  .join('\n');
848
848
  // LLM analyses in parallel
849
849
  const users = Object.values(userStats);
850
- const [topics, userTitles, goldenQuotes] = await Promise.all([
850
+ const [topicsResult, titlesResult, quotesResult] = await Promise.allSettled([
851
851
  this.ctx.group_analysis_llm.summarizeTopics(idBasedMessagesText || messagesText, context),
852
852
  this.config.userTitleAnalysis
853
853
  ? this.ctx.group_analysis_llm.analyzeUserTitles(users, context)
854
854
  : Promise.resolve([]),
855
855
  this.ctx.group_analysis_llm.analyzeGoldenQuotes(idBasedMessagesText || messagesText, this.config.maxGoldenQuotes, context)
856
- ]).catch((error) => {
857
- this.ctx.logger.error('LLM analysis failed:', error);
858
- // On LLM failure, return empty results to avoid crashing the entire analysis.
859
- return [
860
- [],
861
- [],
862
- []
863
- ];
864
- });
856
+ ]);
857
+ // 单项失败只丢该项,避免一项 YAML 异常把话题/称号/金句全部清空。
858
+ const topics = topicsResult.status === 'fulfilled' ? (topicsResult.value ?? []) : [];
859
+ const userTitles = titlesResult.status === 'fulfilled' ? (titlesResult.value ?? []) : [];
860
+ const goldenQuotes = quotesResult.status === 'fulfilled' ? (quotesResult.value ?? []) : [];
861
+ if (topicsResult.status === 'rejected')
862
+ this.ctx.logger.error('话题分析失败:', topicsResult.reason);
863
+ if (titlesResult.status === 'rejected')
864
+ this.ctx.logger.error('用户称号分析失败:', titlesResult.reason);
865
+ if (quotesResult.status === 'rejected')
866
+ this.ctx.logger.error('金句分析失败:', quotesResult.reason);
865
867
  // Final statistics
866
868
  const sortedUsers = users.sort((a, b) => b.messageCount - a.messageCount);
867
869
  // 群名片解析:官方 QQ bot 消息作者名是"个人昵称",群名片需通过 getGuildMember 获取。
@@ -15,6 +15,25 @@ function repairUnclosedQuotedFields(source) {
15
15
  }).join('\n');
16
16
  return changed ? repaired : null;
17
17
  }
18
+ /**
19
+ * 从模型回复中取出 YAML 正文。
20
+ * 兼容三类常见输出:完整代码围栏、缺少结尾围栏、完全没有围栏但正文是 YAML 列表。
21
+ */
22
+ function extractYamlSource(rawContent) {
23
+ const text = String(rawContent ?? '');
24
+ const fenced = text.match(/```ya?ml[ \t]*\r?\n([\s\S]*?)\r?\n?[ \t]*```/i);
25
+ if (fenced)
26
+ return fenced[1];
27
+ // 只有开头围栏,结尾被模型截断/漏写
28
+ const openOnly = text.match(/```ya?ml[ \t]*\r?\n([\s\S]*)$/i);
29
+ if (openOnly)
30
+ return openOnly[1].replace(/[ \t]*```\s*$/, '').trim();
31
+ // 没有任何围栏,但正文以 YAML 列表项开头
32
+ const listStart = text.search(/^[ \t]*-[ \t]+\S/m);
33
+ if (listStart >= 0)
34
+ return text.slice(listStart).trim();
35
+ return null;
36
+ }
18
37
  class LLMService extends koishi_1.Service {
19
38
  config;
20
39
  constructor(ctx, config) {
@@ -48,20 +67,19 @@ class LLMService extends koishi_1.Service {
48
67
  }
49
68
  async _callLLM(prompt, taskName) {
50
69
  const rawContent = await this._callOpenAI(prompt, taskName);
51
- // Extract YAML from markdown code block (supports both yaml and yml)
52
- const yamlMatch = rawContent.match(/```ya?ml\s*([\s\S]*?)\s*```/);
53
- if (!yamlMatch) {
54
- this.ctx.logger.warn(`未找到 YAML 代码块,无法解析。`);
70
+ const yamlSource = extractYamlSource(rawContent);
71
+ if (!yamlSource) {
72
+ this.ctx.logger.warn(`未找到 YAML 内容,无法解析。`);
55
73
  throw new Error('未找到 YAML 响应。');
56
74
  }
57
75
  try {
58
76
  // json: true 容忍重复 key(LLM 偶尔会重复输出 sender 等字段),后者覆盖前者
59
77
  let data;
60
78
  try {
61
- data = (0, js_yaml_1.load)(yamlMatch[1], { json: true });
79
+ data = (0, js_yaml_1.load)(yamlSource, { json: true });
62
80
  }
63
81
  catch (parseError) {
64
- const repairedYaml = repairUnclosedQuotedFields(yamlMatch[1]);
82
+ const repairedYaml = repairUnclosedQuotedFields(yamlSource);
65
83
  if (!repairedYaml)
66
84
  throw parseError;
67
85
  data = (0, js_yaml_1.load)(repairedYaml, { json: true });
@@ -74,7 +92,7 @@ class LLMService extends koishi_1.Service {
74
92
  }
75
93
  catch (err) {
76
94
  this.ctx.logger.error('解析 YAML 失败:', err);
77
- this.ctx.logger.error('待解析的 YAML 字符串:', yamlMatch[1] || '[空字符串]');
95
+ this.ctx.logger.error('待解析的 YAML 字符串:', yamlSource || '[空字符串]');
78
96
  throw err;
79
97
  }
80
98
  }
package/lib/utils.js CHANGED
@@ -28,6 +28,7 @@ exports.buildGroupAnalysisCacheKey = buildGroupAnalysisCacheKey;
28
28
  exports.buildMessagePersistenceKey = buildMessagePersistenceKey;
29
29
  exports.isNapCatBot = isNapCatBot;
30
30
  exports.isJunkMessage = isJunkMessage;
31
+ exports.isForwardedChatRecord = isForwardedChatRecord;
31
32
  exports.cleanContent = cleanContent;
32
33
  const koishi_1 = require("koishi");
33
34
  const skins_1 = require("./skins");
@@ -372,6 +373,28 @@ async function isNapCatBot(bot) {
372
373
  const name = versionInfo.data['app_name'].toLowerCase();
373
374
  return { isRunningNapCat: name.includes('napcat'), botAppName: name };
374
375
  }
376
+ /**
377
+ * 判断消息是否为 QQ“合并转发/聊天记录”卡片。
378
+ * 官方适配器没有解析 102 类型的聊天记录,只把卡片预览文字当成普通文本,
379
+ * 直接分析会把它当成转发者的发言,因此这里整体识别并跳过。
380
+ */
381
+ function isForwardedChatRecord(content) {
382
+ if (content === null || content === undefined)
383
+ return false;
384
+ const s = String(content).replace(/\s+/g, ' ').trim();
385
+ if (!s)
386
+ return false;
387
+ // 卡片底部固定文案,如“查看2条转发消息”
388
+ if (/查看\s*\d+\s*条(?:转发)?消息/.test(s))
389
+ return true;
390
+ // 卡片标题,如“群聊的聊天记录”
391
+ if (/^群聊的聊天记录(?:$|\s|[::])/.test(s))
392
+ return true;
393
+ // 部分版本只给占位标签
394
+ if (/^\[(?:聊天记录|合并转发)\]$/.test(s))
395
+ return true;
396
+ return false;
397
+ }
375
398
  /**
376
399
  * 判断消息是否为非聊天的垃圾内容(腾讯官方 bot 的 ROBOT1.0_ 签名、超长无空白 token 串等)。
377
400
  * 这类内容不应存入消息库,也不应参与分析。
@@ -382,6 +405,9 @@ function isJunkMessage(content) {
382
405
  const s = String(content).trim();
383
406
  if (!s)
384
407
  return false;
408
+ // 合并转发/聊天记录卡片
409
+ if (isForwardedChatRecord(s))
410
+ return true;
385
411
  // 腾讯 QQ 官方机器人消息签名(任意位置出现都过滤)
386
412
  if (/ROBOT1\.0_/.test(s))
387
413
  return true;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-group-analysis-mx",
3
3
  "description": "群聊分析插件 - 生成精美群聊统计报告,支持 AI 话题总结(独立版,不依赖 ChatLuna)",
4
- "version": "1.7.8",
4
+ "version": "1.8.0",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",
@@ -63,3 +63,5 @@
63
63
 
64
64
 
65
65
 
66
+
67
+