@nestim/koishi-plugin-qq-group-manager 1.4.5 → 1.4.7

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/service.js CHANGED
@@ -8,6 +8,8 @@ const koishi_1 = require("koishi");
8
8
  const node_path_1 = __importDefault(require("node:path"));
9
9
  const node_fs_1 = __importDefault(require("node:fs"));
10
10
  const { AiEngine } = require("./ai/engine");
11
+ const { getSharedAiHooks } = require("./ai/hooks");
12
+ const { getAiRuntimeFacade } = require("./ai/runtime");
11
13
  let PLUGIN_VERSION = 'unknown';
12
14
  try {
13
15
  PLUGIN_VERSION = require('../package.json').version || 'unknown';
@@ -281,22 +283,32 @@ class QQGroupManagerService extends koishi_1.Service {
281
283
  this.ctx = ctx;
282
284
  this.config = config;
283
285
  this._logger = ctx.logger('nestim-qq-group-manager');
284
- this.ai = new AiEngine(this);
286
+ // 进程级共享钩子:主插件重载后仍是同一个对象,外部插件注册的钩子不会丢。
287
+ const sharedHooks = getSharedAiHooks(this._logger);
288
+ this.ai = new AiEngine(this, { hooks: sharedHooks });
285
289
  // Koishi 的 Service 注册在插件 fork 作用域,兄弟插件的 inject 拿不到;
286
- // 额外把扩展点挂到 root,外部插件 inject 'qqgmAiHooks' 即可。
290
+ // 额外把扩展点挂到 root,外部插件运行时探测即可。
291
+ //
292
+ // 关键:hooks 与 runtime 都必须是「进程级稳定对象」。主插件单独重载
293
+ // 时不能换成新对象,否则 meme / chat-style / private 手里的旧引用会
294
+ // 指向已销毁的实例:新引擎看不到它们的钩子(表情包不发、风格段丢失),
295
+ // 私聊还会继续用旧配置和旧引擎。
287
296
  try {
288
- ctx.root.set('qqgmAiHooks', this.ai.hooks);
289
- const runtime = {
290
- config: this.config,
291
- hooks: this.ai.hooks,
292
- chat: (request) => this.ai.chat(request),
293
- };
294
- ctx.root.set('qqgmAiRuntime', runtime);
297
+ const hooks = sharedHooks;
298
+ const facade = getAiRuntimeFacade();
299
+ facade.bind(this);
300
+ ctx.root.set('qqgmAiHooks', hooks);
301
+ ctx.root.set('qqgmAiRuntime', facade);
295
302
  ctx.on('dispose', () => {
296
303
  try {
297
- if (ctx.root.get('qqgmAiHooks') === this.ai.hooks)
304
+ facade.unbind(this);
305
+ // 只有当前没有新实例接管时才撤掉对外暴露:旧实例的 dispose
306
+ // 可能晚于新实例的 bind,此时不能把新指针清掉。
307
+ if (facade.current)
308
+ return;
309
+ if (ctx.root.get('qqgmAiHooks') === hooks)
298
310
  ctx.root.set('qqgmAiHooks', undefined);
299
- if (ctx.root.get('qqgmAiRuntime')?.hooks === this.ai.hooks)
311
+ if (ctx.root.get('qqgmAiRuntime') === facade)
300
312
  ctx.root.set('qqgmAiRuntime', undefined);
301
313
  }
302
314
  catch { /* ignore */ }
@@ -875,6 +887,42 @@ class QQGroupManagerService extends koishi_1.Service {
875
887
  return block;
876
888
  });
877
889
  }
890
+ /**
891
+ * 自动归纳专用写入:追加一条新记忆,并可同时删掉若干旧条目。
892
+ * 用于「每 N 条压缩成 1 条」——remove 里的旧条目会被新条目替换。
893
+ */
894
+ async commitAutoMemory(scope, payload) {
895
+ const add = String(payload && payload.add || '').trim();
896
+ if (!add)
897
+ return null;
898
+ const remove = new Set((payload && payload.remove || [])
899
+ .map((item) => String(item || '').trim())
900
+ .filter(Boolean));
901
+ const label = String(payload && payload.label || '自动归纳');
902
+ return this.runMemoryWriteLocked(async () => {
903
+ const now = new Date();
904
+ const stamp = now.toLocaleString('zh-CN', { hour12: false });
905
+ const block = `## ${stamp} · ${label}\n${add}`;
906
+ const existing = (await this.readMemoryFile(scope)).trim();
907
+ const blocks = this.extractMemoryBlocks(existing);
908
+ const kept = [];
909
+ let insertAt = -1;
910
+ for (const item of blocks) {
911
+ if (remove.has(item)) {
912
+ if (insertAt < 0)
913
+ insertAt = kept.length;
914
+ continue;
915
+ }
916
+ kept.push(item);
917
+ }
918
+ if (insertAt < 0)
919
+ insertAt = kept.length;
920
+ kept.splice(insertAt, 0, block);
921
+ const next = kept.length ? `${kept.join('\n\n---\n\n')}\n` : `${block}\n`;
922
+ await this.writeMemoryFile(next, scope);
923
+ return block;
924
+ });
925
+ }
878
926
  async searchMemory(keyword, limit = 8, scope) {
879
927
  const blocks = this.extractMemoryBlocks(await this.readMemoryFile(scope));
880
928
  const kw = String(keyword || '').trim();
@@ -2161,6 +2209,12 @@ class QQGroupManagerService extends koishi_1.Service {
2161
2209
  }
2162
2210
  await session.send(koishi_1.h.text(text));
2163
2211
  }
2212
+ /** 系统提示(例如「冷却中」),不引用原消息、不写入 AI 上下文 */
2213
+ async sendNotice(session, text) {
2214
+ if (!text)
2215
+ return;
2216
+ await session.send(koishi_1.h.text(String(text)));
2217
+ }
2164
2218
  extractTextForFilter(session) {
2165
2219
  const raw = (session.content ?? '')
2166
2220
  .replace(/<[^>]+>/g, ' ')
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.5",
4
+ "version": "1.4.7",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [