@nestim/koishi-plugin-qq-group-manager 1.4.13 → 1.5.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.
package/src/service.js CHANGED
@@ -10,6 +10,7 @@ const node_fs_1 = __importDefault(require("node:fs"));
10
10
  const { AiEngine } = require("./ai/engine");
11
11
  const { getSharedAiHooks } = require("./ai/hooks");
12
12
  const { getAiRuntimeFacade } = require("./ai/runtime");
13
+ const { getSdkFacade } = require("./ai/sdk");
13
14
  let PLUGIN_VERSION = 'unknown';
14
15
  try {
15
16
  PLUGIN_VERSION = require('../package.json').version || 'unknown';
@@ -300,10 +301,17 @@ class QQGroupManagerService extends koishi_1.Service {
300
301
  const hooks = sharedHooks;
301
302
  const facade = getAiRuntimeFacade();
302
303
  facade.bind(this);
304
+ const sdk = getSdkFacade();
305
+ this.sdk = sdk;
306
+ this.events = sdk.events;
307
+ sdk.bind(this);
303
308
  ctx.root.set('qqgmAiHooks', hooks);
304
309
  ctx.root.set('qqgmAiRuntime', facade);
310
+ ctx.root.set('qqgmSdk', sdk);
311
+ this._logger.info(`[sdk] v${sdk.version} 已挂载(events / storage / telemetry / hooks v${hooks.version})`);
305
312
  ctx.on('dispose', () => {
306
313
  try {
314
+ sdk.unbind(this);
307
315
  facade.unbind(this);
308
316
  // 只有当前没有新实例接管时才撤掉对外暴露:旧实例的 dispose
309
317
  // 可能晚于新实例的 bind,此时不能把新指针清掉。
@@ -925,6 +933,17 @@ class QQGroupManagerService extends koishi_1.Service {
925
933
  kept.splice(insertAt, 0, block);
926
934
  const next = kept.length ? `${kept.join('\n\n---\n\n')}\n` : `${block}\n`;
927
935
  await this.writeMemoryFile(next, scope);
936
+ try {
937
+ this.events?.emit?.('memory.committed', {
938
+ scopeKind: scope && scope.kind,
939
+ scopeId: scope && scope.id,
940
+ category: category || '其他',
941
+ chars: add.length,
942
+ removed: remove.size,
943
+ at: Date.now(),
944
+ });
945
+ }
946
+ catch { /* ignore */ }
928
947
  return block;
929
948
  });
930
949
  }
@@ -2816,6 +2835,8 @@ class QQGroupManagerService extends koishi_1.Service {
2816
2835
  '解除禁言 ' + P('cmdUnmute', '[unmute]') + ' qq reason',
2817
2836
  '记忆保存 ' + P('cmdMemory', '[memory]') + ' content',
2818
2837
  '记忆查询 ' + P('cmdMemory', '[memory]') + ' search keyword',
2838
+ '记忆归纳 ' + P('cmdMemory', '[memory]') + ' summarize',
2839
+ '记忆整理 ' + P('cmdMemory', '[memory]') + ' organize',
2819
2840
  '拉黑 ' + P('cmdBan', '[ban]') + ' qq reason',
2820
2841
  '解除拉黑 ' + P('cmdUnban', '[unban]') + ' qq reason',
2821
2842
  '黑名单查询 ' + P('cmdBanList', '[banlist]'),
@@ -2996,7 +3017,7 @@ class QQGroupManagerService extends koishi_1.Service {
2996
3017
  }
2997
3018
  const sub = rest.trim();
2998
3019
  if (!sub) {
2999
- await session.send(this.styleText(`用法:${this.cmdPrefix('cmdMemory', '[memory]')} content 保存|${this.cmdPrefix('cmdMemory', '[memory]')} list 列出|${this.cmdPrefix('cmdMemory', '[memory]')} search keyword 查询|${this.cmdPrefix('cmdMemory', '[memory]')} delete keyword 删除`));
3020
+ await session.send(this.styleText(`用法:${this.cmdPrefix('cmdMemory', '[memory]')} content 保存|${this.cmdPrefix('cmdMemory', '[memory]')} list 列出|${this.cmdPrefix('cmdMemory', '[memory]')} search keyword 查询|${this.cmdPrefix('cmdMemory', '[memory]')} delete keyword 删除|${this.cmdPrefix('cmdMemory', '[memory]')} summarize 归纳|${this.cmdPrefix('cmdMemory', '[memory]')} organize 整理`));
3000
3021
  return true;
3001
3022
  }
3002
3023
  const delMatch = sub.match(/^(?:delete|del)\s*([\s\S]*)$/i);
@@ -3055,6 +3076,51 @@ class QQGroupManagerService extends koishi_1.Service {
3055
3076
  this._logger.info(`[cmd:memory] 查询 keyword=${keyword || '-'} 命中=${results.length} | ${this.sessionTag(session)}`);
3056
3077
  return true;
3057
3078
  }
3079
+ // 手动归纳:[memory] summarize / [memory] 归纳 / [memory] 总结
3080
+ const summarizeMatch = /^(?:summarize|summary|归纳|总结)\s*$/i.exec(sub);
3081
+ if (summarizeMatch) {
3082
+ const auth = await this.authorizeCommand('memory', session);
3083
+ if (!auth.ok)
3084
+ return this.denyAdmin(session, auth.userMessage || '只有主人或管理员可以触发记忆归纳。');
3085
+ await session.send(this.styleText('正在归纳,稍等…'));
3086
+ const r = await this.ai.learnNow(session);
3087
+ if (!r.ok) {
3088
+ const message = r.reason === 'empty'
3089
+ ? '没有可归纳的消息。'
3090
+ : r.reason === 'no-scope'
3091
+ ? '当前会话无法确定记忆作用域。'
3092
+ : '这次没有提炼出值得长期记住的内容。';
3093
+ await session.send(this.styleText(message));
3094
+ return true;
3095
+ }
3096
+ const body = r.text.length > 400 ? `${r.text.slice(0, 400)}…` : r.text;
3097
+ await session.send(this.styleText(`已归纳 ${r.entries} 条:\n${body}`));
3098
+ this._logger.info(`[cmd:memory] 手动归纳 entries=${r.entries} source=${r.reason} | ${this.sessionTag(session)}`);
3099
+ return true;
3100
+ }
3101
+ // 手动整理:[memory] organize / [memory] 整理
3102
+ const organizeMatch = /^(?:organize|tidy|整理)\s*$/i.exec(sub);
3103
+ if (organizeMatch) {
3104
+ const auth = await this.authorizeCommand('memory', session);
3105
+ if (!auth.ok)
3106
+ return this.denyAdmin(session, auth.userMessage || '只有主人或管理员可以触发记忆整理。');
3107
+ await session.send(this.styleText('正在整理,稍等…'));
3108
+ const r = await this.ai.organizeNow(session);
3109
+ if (!r.ok) {
3110
+ const message = r.reason === 'empty'
3111
+ ? '记忆库里还没有自动归纳的内容。'
3112
+ : '当前会话无法确定记忆作用域。';
3113
+ await session.send(this.styleText(message));
3114
+ return true;
3115
+ }
3116
+ if (!r.categories) {
3117
+ await session.send(this.styleText(`当前自动记忆已经足够精简(${r.after} 条),没有可合并的同类条目。`));
3118
+ return true;
3119
+ }
3120
+ await session.send(this.styleText(`已整理:合并 ${r.categories} 个分类,净减 ${r.removed} 条,现在 ${r.after} 条自动记忆。`));
3121
+ this._logger.info(`[cmd:memory] 手动整理 categories=${r.categories} removed=${r.removed} after=${r.after} | ${this.sessionTag(session)}`);
3122
+ return true;
3123
+ }
3058
3124
  // 保存
3059
3125
  try {
3060
3126
  const mscope = this.memoryScopeOf(session);