@nestim/koishi-plugin-qq-group-manager 1.4.10 → 1.4.12

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.
package/lib/ai/learn.d.ts CHANGED
@@ -8,6 +8,13 @@
8
8
  * 归纳出来的内容是一次性的一长串,150 字以内,写入对应作用域的记忆文件。
9
9
  */
10
10
  export declare const MAX_SUMMARY_CHARS = 150;
11
+ /** 记忆分类:一条记忆只属于一类,便于存储与检索 */
12
+ export declare const MEMORY_CATEGORIES: readonly ["人物关系", "偏好立场", "群内黑话", "长期话题", "约定事件", "其他"];
13
+ export declare function normalizeMemoryCategory(raw: string): string;
14
+ export interface SummaryEntry {
15
+ category: string;
16
+ text: string;
17
+ }
11
18
  export interface EvictedLike {
12
19
  id?: string;
13
20
  userId?: string;
@@ -50,7 +57,7 @@ export declare class LearnStore {
50
57
  }
51
58
  /** 把一批消息渲染成模型可读的文本 */
52
59
  export declare function renderForSummary(records: EvictedLike[], selfName?: string): string;
53
- /** 归纳提示词 */
60
+ /** 归纳提示词:按类别输出多条,一条只写一类 */
54
61
  export declare function buildSummaryPrompt(records: EvictedLike[], options?: {
55
62
  kind?: string;
56
63
  selfName?: string;
@@ -58,6 +65,8 @@ export declare function buildSummaryPrompt(records: EvictedLike[], options?: {
58
65
  system: string;
59
66
  user: string;
60
67
  };
68
+ /** 解析模型输出的多分类记忆 */
69
+ export declare function parseSummaryEntries(raw: string, maxChars?: number): SummaryEntry[];
61
70
  /** 清洗模型输出 */
62
71
  export declare function cleanSummary(raw: string, maxChars?: number): string;
63
72
  /** Jaccard 相似度,用于挡住重复记忆 */
@@ -70,8 +79,10 @@ export declare const AUTO_MEMORY_LABEL = "\u81EA\u52A8\u5F52\u7EB3";
70
79
  export declare function isAutoMemoryBlock(block: string): boolean;
71
80
  /** 取记忆块正文(去掉 `## 时间 · 来源` 标题行) */
72
81
  export declare function memoryBlockBody(block: string): string;
82
+ /** 取记忆块的分类;旧格式没有分类时归入「其他」 */
83
+ export declare function memoryBlockCategory(block: string): string;
73
84
  /**
74
- * 记忆压缩提示词:把 N 条自动归纳合并成一条更短的。
85
+ * 记忆压缩提示词:把 N 条同分类的自动归纳合并成一条更短的。
75
86
  *
76
87
  * 目的不是“再总结一遍”,而是去掉重复与过时信息——群里连续几天聊同一件事时,
77
88
  * 自动归纳会反复产出高度相似的条目,压缩后只保留事实差异。
@@ -79,6 +90,7 @@ export declare function memoryBlockBody(block: string): string;
79
90
  export declare function buildConsolidatePrompt(blocks: string[], options?: {
80
91
  maxChars?: number;
81
92
  kind?: string;
93
+ category?: string;
82
94
  }): {
83
95
  system: string;
84
96
  user: string;
package/lib/ai/learn.js CHANGED
@@ -9,17 +9,53 @@
9
9
  * 归纳出来的内容是一次性的一长串,150 字以内,写入对应作用域的记忆文件。
10
10
  */
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.AUTO_MEMORY_LABEL = exports.LearnStore = exports.LearnBucket = exports.MAX_SUMMARY_CHARS = void 0;
12
+ exports.AUTO_MEMORY_LABEL = exports.LearnStore = exports.LearnBucket = exports.MEMORY_CATEGORIES = exports.MAX_SUMMARY_CHARS = void 0;
13
+ exports.normalizeMemoryCategory = normalizeMemoryCategory;
13
14
  exports.renderForSummary = renderForSummary;
14
15
  exports.buildSummaryPrompt = buildSummaryPrompt;
16
+ exports.parseSummaryEntries = parseSummaryEntries;
15
17
  exports.cleanSummary = cleanSummary;
16
18
  exports.similarityOf = similarityOf;
17
19
  exports.isDuplicate = isDuplicate;
18
20
  exports.isAutoMemoryBlock = isAutoMemoryBlock;
19
21
  exports.memoryBlockBody = memoryBlockBody;
22
+ exports.memoryBlockCategory = memoryBlockCategory;
20
23
  exports.buildConsolidatePrompt = buildConsolidatePrompt;
21
24
  const text_1 = require("../core/text");
22
25
  exports.MAX_SUMMARY_CHARS = 150;
26
+ /** 记忆分类:一条记忆只属于一类,便于存储与检索 */
27
+ exports.MEMORY_CATEGORIES = ['人物关系', '偏好立场', '群内黑话', '长期话题', '约定事件', '其他'];
28
+ const CATEGORY_ALIASES = {
29
+ 人物: '人物关系',
30
+ 关系: '人物关系',
31
+ 称呼: '人物关系',
32
+ 身份: '人物关系',
33
+ 偏好: '偏好立场',
34
+ 立场: '偏好立场',
35
+ 喜好: '偏好立场',
36
+ 观点: '偏好立场',
37
+ 黑话: '群内黑话',
38
+ 梗: '群内黑话',
39
+ 术语: '群内黑话',
40
+ 缩写: '群内黑话',
41
+ 话题: '长期话题',
42
+ 约定: '约定事件',
43
+ 事件: '约定事件',
44
+ 计划: '约定事件',
45
+ 其他: '其他',
46
+ };
47
+ function normalizeMemoryCategory(raw) {
48
+ const value = String(raw || '').trim().replace(/[「」【】\[\]]/g, '');
49
+ if (!value)
50
+ return '其他';
51
+ if (exports.MEMORY_CATEGORIES.includes(value))
52
+ return value;
53
+ for (const key of Object.keys(CATEGORY_ALIASES)) {
54
+ if (value.includes(key))
55
+ return CATEGORY_ALIASES[key];
56
+ }
57
+ return '其他';
58
+ }
23
59
  /** 正在累计的一批消息 */
24
60
  class LearnBucket {
25
61
  records = [];
@@ -111,7 +147,7 @@ function renderForSummary(records, selfName = 'bot') {
111
147
  }
112
148
  return lines.join('\n');
113
149
  }
114
- /** 归纳提示词 */
150
+ /** 归纳提示词:按类别输出多条,一条只写一类 */
115
151
  function buildSummaryPrompt(records, options = {}) {
116
152
  const label = options.kind === 'private' ? '私聊' : '群聊';
117
153
  const body = renderForSummary(records, options.selfName || 'bot');
@@ -123,18 +159,61 @@ function buildSummaryPrompt(records, options = {}) {
123
159
  '2. 稳定的偏好、立场、习惯(不是一时的情绪)',
124
160
  '3. 群内或两人之间约定俗成的说法、黑话',
125
161
  '4. 反复出现、以后还会聊到的话题',
162
+ '5. 明确的约定、时间、计划',
126
163
  '',
127
164
  '必须丢掉:寒暄、打招呼、一次性的抱怨或玩笑、已经结束的接梗、纯表情。',
128
165
  '',
129
166
  '输出要求:',
130
- '- 只输出一条记忆,纯文本,不要标题、不要列表、不要编号、不要 Markdown;',
131
- `- 不超过 ${exports.MAX_SUMMARY_CHARS} 字;`,
167
+ '- 按类别拆开,每条一行,格式必须是「类别:内容」;',
168
+ `- 类别只能是:${exports.MEMORY_CATEGORIES.join('、')};`,
169
+ '- 一条记忆只写一个类别,不要在一句里混入另一个类别的内容;',
170
+ '- 最多输出 3 条;同类最多 2 条;',
171
+ '- 不要标题、列表符号、编号、Markdown、引号;',
172
+ `- 每条不超过 ${exports.MAX_SUMMARY_CHARS} 字;`,
132
173
  '- 用陈述句写事实,不要写「他们聊到」「讨论了」这类过程描述;',
133
174
  '- 如果没有任何值得长期记住的内容,只输出一个词:NONE',
134
175
  ].join('\n');
135
- const user = ['【记录】', body || '(空)', '', '现在输出那一条记忆,或输出 NONE。'].join('\n');
176
+ const user = ['【记录】', body || '(空)', '', '现在输出分类后的记忆(类别:内容),或输出 NONE。'].join('\n');
136
177
  return { system, user };
137
178
  }
179
+ /** 解析模型输出的多分类记忆 */
180
+ function parseSummaryEntries(raw, maxChars = exports.MAX_SUMMARY_CHARS) {
181
+ let text = String(raw || '').trim();
182
+ if (!text)
183
+ return [];
184
+ text = text
185
+ .replace(/^```[a-zA-Z0-9_-]*\s*\n?/, '')
186
+ .replace(/\n?\s*```\s*$/, '')
187
+ .trim();
188
+ if (/^(?:none|无|没有|n\/a)$/i.test(text))
189
+ return [];
190
+ const lines = text
191
+ .split(/\r?\n/)
192
+ .map((line) => line.replace(/^\s*(?:[-*+·]|\d+[.)、])\s*/, '').trim())
193
+ .filter(Boolean);
194
+ const out = [];
195
+ const seen = new Set();
196
+ for (const line of lines) {
197
+ let category = '其他';
198
+ let body = line;
199
+ const match = /^([^::]{1,12})[::]\s*(.+)$/.exec(line);
200
+ if (match) {
201
+ category = normalizeMemoryCategory(match[1]);
202
+ body = match[2];
203
+ }
204
+ const cleaned = cleanSummary(body, maxChars);
205
+ if (!cleaned)
206
+ continue;
207
+ const key = `${category}|${cleaned}`;
208
+ if (seen.has(key))
209
+ continue;
210
+ seen.add(key);
211
+ out.push({ category, text: cleaned });
212
+ if (out.length >= 3)
213
+ break;
214
+ }
215
+ return out;
216
+ }
138
217
  /** 清洗模型输出 */
139
218
  function cleanSummary(raw, maxChars = exports.MAX_SUMMARY_CHARS) {
140
219
  let text = String(raw || '').trim();
@@ -231,8 +310,14 @@ function memoryBlockBody(block) {
231
310
  return String(block || '').trim();
232
311
  return lines.slice(1).join('\n').trim();
233
312
  }
313
+ /** 取记忆块的分类;旧格式没有分类时归入「其他」 */
314
+ function memoryBlockCategory(block) {
315
+ const first = String(block || '').split('\n', 1)[0] || '';
316
+ const match = /·\s*自动归纳\s*·\s*([^·\n]+)/.exec(first);
317
+ return normalizeMemoryCategory(match ? match[1] : '其他');
318
+ }
234
319
  /**
235
- * 记忆压缩提示词:把 N 条自动归纳合并成一条更短的。
320
+ * 记忆压缩提示词:把 N 条同分类的自动归纳合并成一条更短的。
236
321
  *
237
322
  * 目的不是“再总结一遍”,而是去掉重复与过时信息——群里连续几天聊同一件事时,
238
323
  * 自动归纳会反复产出高度相似的条目,压缩后只保留事实差异。
@@ -240,14 +325,16 @@ function memoryBlockBody(block) {
240
325
  function buildConsolidatePrompt(blocks, options = {}) {
241
326
  const maxChars = Math.max(40, Number(options.maxChars) || 90);
242
327
  const label = options.kind === 'private' ? '私聊' : '群聊';
328
+ const category = normalizeMemoryCategory(options.category || '其他');
243
329
  const bodies = (blocks || [])
244
330
  .map((block) => memoryBlockBody(block).replace(/\s+/g, ' ').trim())
245
331
  .filter(Boolean);
246
332
  const system = [
247
- `你在压缩一段${label}的长期记忆。`,
333
+ `你在压缩一段${label}里「${category}」类的长期记忆。`,
248
334
  `下面有 ${bodies.length} 条已经归纳过的记忆,请合并成一条。`,
249
335
  '',
250
336
  '要求:',
337
+ `- 只合并「${category}」这一类的信息,不要混入其他类别;`,
251
338
  '- 只保留互不重复的事实:人物与称呼、关系、稳定偏好、反复出现的话题;',
252
339
  '- 删掉重复表述、一次性情绪、已经过时的细节;',
253
340
  '- 不要编造,不要写“他们聊到”“讨论了”这类过程描述;',
@@ -25,5 +25,7 @@ export declare class AiRuntimeFacade {
25
25
  get config(): any;
26
26
  get hooks(): AiHookRegistry;
27
27
  chat(request: AiRuntimeChatRequest): Promise<any>;
28
+ /** 把一条消息里的图片解析成 data URL(私聊插件复用主插件的视觉链路) */
29
+ images(session: any): Promise<string[]>;
28
30
  }
29
31
  export declare function getAiRuntimeFacade(): AiRuntimeFacade;
package/lib/ai/runtime.js CHANGED
@@ -39,6 +39,14 @@ class AiRuntimeFacade {
39
39
  }
40
40
  return this.current.ai.chat(request);
41
41
  }
42
+ /** 把一条消息里的图片解析成 data URL(私聊插件复用主插件的视觉链路) */
43
+ images(session) {
44
+ if (!this.current || !this.current.ai
45
+ || typeof this.current.ai.resolveSessionImages !== 'function') {
46
+ return Promise.resolve([]);
47
+ }
48
+ return this.current.ai.resolveSessionImages(session);
49
+ }
42
50
  }
43
51
  exports.AiRuntimeFacade = AiRuntimeFacade;
44
52
  function getAiRuntimeFacade() {
package/lib/index.d.ts CHANGED
@@ -153,6 +153,7 @@ export declare const Config: Schema<Schemastery.ObjectS<{
153
153
  aiIdleTopicMinHour: Schema<number, number>;
154
154
  aiIdleTopicMaxHour: Schema<number, number>;
155
155
  aiIdleTopicCooldownMinutes: Schema<number, number>;
156
+ aiIdleTopicWebSearch: Schema<boolean, boolean>;
156
157
  aiSystemPrompt: Schema<string, string>;
157
158
  aiActivePersona: Schema<string, string>;
158
159
  aiPersonas: Schema<Schemastery.ObjectS<{
@@ -324,6 +325,7 @@ export declare const Config: Schema<Schemastery.ObjectS<{
324
325
  aiIdleTopicMinHour: number;
325
326
  aiIdleTopicMaxHour: number;
326
327
  aiIdleTopicCooldownMinutes: number;
328
+ aiIdleTopicWebSearch: boolean;
327
329
  aiSystemPrompt: string;
328
330
  aiActivePersona: string;
329
331
  aiPersonas: Schemastery.ObjectT<{
package/lib/index.js CHANGED
@@ -136,6 +136,7 @@ exports.Config = koishi_1.Schema.intersect([
136
136
  aiIdleTopicMinHour: koishi_1.Schema.natural().min(0).max(23).default(9).description('主动话题允许的最早小时(0-23)。'),
137
137
  aiIdleTopicMaxHour: koishi_1.Schema.natural().min(0).max(24).default(23).description('主动话题允许的最晚小时(不含)。'),
138
138
  aiIdleTopicCooldownMinutes: koishi_1.Schema.natural().min(1).max(1440).default(60).description('两次主动话题之间的最小间隔(分钟)。'),
139
+ aiIdleTopicWebSearch: koishi_1.Schema.boolean().default(true).description('主动话题前先搜一次(优先用最近聊天内容做 query,没有上下文就搜当天热点);搜不到或无关会自动忽略。'),
139
140
  aiSystemPrompt: koishi_1.Schema.string().role('textarea').default('你是群聊中的友好机器人,请简洁、自然、符合中文互联网语境地回复。').description('默认系统提示词。'),
140
141
  aiActivePersona: koishi_1.Schema.string().default('').description('启用的人格 ID(为空时使用默认自称与默认提示词)。'),
141
142
  aiPersonas: koishi_1.Schema.array(koishi_1.Schema.object({
@@ -179,7 +180,7 @@ exports.Config = koishi_1.Schema.intersect([
179
180
  ]).role('radio').default('auto').description('联网搜索触发方式。'),
180
181
  aiWebSearchMaxResults: koishi_1.Schema.natural().min(1).max(10).default(5).description('每次联网搜索取几条结果。'),
181
182
  aiWebSearchTimeoutMs: koishi_1.Schema.natural().min(500).max(15000).default(4000).description('搜索请求超时(毫秒)。'),
182
- aiWebSearchMaxPerHour: koishi_1.Schema.natural().min(0).max(100).default(10).description('每个群每小时最多搜索次数;0=不限。'),
183
+ aiWebSearchMaxPerHour: koishi_1.Schema.natural().min(0).max(100).default(10).description('每个群每小时最多搜索次数;0=不限。命中缓存不占次数。'),
183
184
  aiWebSearchCacheMinutes: koishi_1.Schema.natural().min(0).max(1440).default(10).description('相同 query 的结果缓存分钟数;0=不缓存。'),
184
185
  aiRequestTimeoutSeconds: koishi_1.Schema.natural().min(5).max(120).default(20).description('单次模型请求超时(秒)。'),
185
186
  aiDebugPrompt: koishi_1.Schema.boolean().default(false).description('调试:把最终系统提示词与用户提示词写入日志(排查"答非所问"时打开,平时关闭)。'),
package/lib/service.js CHANGED
@@ -900,10 +900,12 @@ class QQGroupManagerService extends koishi_1.Service {
900
900
  .map((item) => String(item || '').trim())
901
901
  .filter(Boolean));
902
902
  const label = String(payload && payload.label || '自动归纳');
903
+ const category = String(payload && payload.category || '').trim();
904
+ const fullLabel = category ? `${label} · ${category}` : label;
903
905
  return this.runMemoryWriteLocked(async () => {
904
906
  const now = new Date();
905
907
  const stamp = now.toLocaleString('zh-CN', { hour12: false });
906
- const block = `## ${stamp} · ${label}\n${add}`;
908
+ const block = `## ${stamp} · ${fullLabel}\n${add}`;
907
909
  const existing = (await this.readMemoryFile(scope)).trim();
908
910
  const blocks = this.extractMemoryBlocks(existing);
909
911
  const kept = [];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nestim/koishi-plugin-qq-group-manager",
3
3
  "description": "Koishi QQ群管理插件(OneBot/LLOneBot):群管命令、权限校验、图片菜单、状态卡片、复读、AI群聊回复、记忆库、识图与自动管控(禁言/踢人)。",
4
- "version": "1.4.10",
4
+ "version": "1.4.12",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -190,11 +190,11 @@ npm i @nestim/koishi-plugin-qq-group-manager
190
190
  累计到阈值(aiContextWindow × memoryLearnThresholdScale)
191
191
  或空闲满 memoryLearnIdleHours(且期间有新消息)
192
192
  ↓
193
- 模型归纳成一条 ≤ memoryLearnMaxChars 的记忆
193
+ 模型归纳成 1~3 条分类记忆(每条 ≤ memoryLearnMaxChars)
194
194
  ↓ 与已有记忆去重
195
195
  写入 Memory_<群号>_群聊.md / Memory_<QQ号>_私聊.md
196
196
  ↓ 回推原会话
197
- 「刚记住了:……」
197
+ 「刚记住了:人物关系:……;偏好立场:……」
198
198
  ```
199
199
 
200
200
  要点:
@@ -203,12 +203,29 @@ npm i @nestim/koishi-plugin-qq-group-manager
203
203
  - **不会串台**:私聊发私聊,群聊发对应群。
204
204
  - **不会记废话**:提示词只允许人物关系、稳定偏好、群内黑话、反复出现的话题;
205
205
  寒暄、一次性情绪、已结束的接梗、纯表情全部丢弃。
206
+ - **按类分开存**:人物关系 / 偏好立场 / 群内黑话 / 长期话题 / 约定事件 / 其他;
207
+ 一条记忆只写一类,标题形如 `## 2026/11/18 18:51:00 · 自动归纳 · 人物关系`。
208
+ 同类每 `memoryLearnConsolidateEvery` 条压缩成 1 条,自动记忆总数上限 `memoryLearnMaxEntries`。
206
209
  - **不会重复记**:写入前做相似度比对,超过 `memoryLearnDuplicateThreshold` 就跳过。
207
210
  - **不会刷爆额度**:每作用域每天最多 `memoryLearnDailyLimit` 次。
208
211
  - **系统备注与指令消息不参与归纳**(复读、命令结果等)。
209
212
 
210
213
  自动写入了什么,可以直接在 Koishi 控制台「探索器」打开对应的 `Memory_*.md` 查看。
211
214
  条目带的时间戳与来源标记能让你区分手动 `[memory]` 与 AI 自动归纳。
215
+
216
+ ## 主动话题
217
+
218
+ 群内超过 `aiIdleTopicMinutes` 没有新消息,且处于 `aiIdleTopicMinHour`~`aiIdleTopicMaxHour`
219
+ 之间时,AI 会主动发一条话题(两次之间至少隔 `aiIdleTopicCooldownMinutes`)。
220
+
221
+ 提示词会带上:
222
+
223
+ - 当前日期、星期、时间与季节(例如“2026年11月18日星期三 18:51(冬)”),并明确要求不要提与季节/时间不符的内容;
224
+ - 该群最近 12 条聊天,要求优先接着最近的话题或梗往下说,而不是凭空抛一个无关话题;
225
+ - 按最近聊天检索到的相关记忆;
226
+ - `aiIdleTopicWebSearch` 开启时(默认开),用最近一句做 query 搜一次;没有上下文就搜当天热点,
227
+ 结果不相关会自动忽略,搜不到也不影响发话题。
228
+
212
229
  ## 外部插件接口(1.2.2+)
213
230
 
214
231
  主插件暴露一个很小的扩展点,第三方插件可以给 AI 追加“说话风格”,或在发送前过滤输出,
@@ -227,16 +244,21 @@ export function apply(ctx) {
227
244
  // 发送前过滤;返回 null 表示这条不发(按 pass 处理)
228
245
  const off2 = hooks.registerOutputFilter((text) => text.replace(/首先/g, ''))
229
246
 
247
+ // 按群覆盖主插件默认人设(v3+;返回空表示不覆盖)
248
+ const off3 = hooks.registerPersonaOverride((ctx) => (ctx.groupId === '123456' ? '你是本群专属的猫娘' : null))
249
+
230
250
  ctx.on('dispose', () => {
231
251
  off1()
232
252
  off2()
253
+ off3()
233
254
  })
234
255
  }
235
256
  ```
236
257
 
237
258
  - 风格段插在基础规则之后、`<pass/>` 规则之前,并声明“与系统规则冲突时以系统规则为准”。
238
259
  - 过滤器按注册顺序执行,异常会被隔离;过滤后的文本才会写回上下文。
239
- - `hooks.version` 目前为 `1`,外部插件应先检查版本。
260
+ - `hooks.version` 目前为 `3`:`>=2` 支持 `registerContextEvicted`,`>=3` 支持 `registerPersonaOverride`。
261
+ - 私聊类插件还可以用 `ctx.root.get('qqgmAiRuntime').images(session)` 复用主插件的图片解析链路。
240
262
 
241
263
 
242
264
  ## 数据文件