koishi-plugin-group-analysis-mx 1.7.9 → 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/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.9",
4
+ "version": "1.8.0",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",
@@ -64,3 +64,4 @@
64
64
 
65
65
 
66
66
 
67
+