@aalis/plugin-user-profile 0.13.1 → 0.14.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/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
- import { authority, DEFAULT_AUTHORITY } from '@aalis/api-authority';
2
1
  import { commands } from '@aalis/api-commands';
3
2
  import { contributions } from '@aalis/api-contributions';
4
3
  import { hooks } from '@aalis/api-hooks';
5
- import { llm, resolveLLMModel } from '@aalis/api-llm';
4
+ import { lightestThinking, llm, resolveLLMModel } from '@aalis/api-llm';
6
5
  import { clearMetadataNamespaces, memory } from '@aalis/api-memory';
7
- import { persona } from '@aalis/api-persona';
8
6
  import { tools } from '@aalis/api-tools';
9
7
  import { userRelation } from '@aalis/api-user-relation';
8
+ import { webuiServer } from '@aalis/api-webui';
10
9
  import { config, definePlugin, events, logger, optional } from '@aalis/core';
11
- import { WellKnownKinds } from '@aalis/schema-message';
10
+ import { defineConfig, parseConfig } from '@aalis/schema-config';
11
+ import { DIRECTIVE_KINDS, WellKnownMetadataKeys } from '@aalis/schema-message';
12
12
  import { parseLLMJsonObject } from '@aalis/util-json-repair';
13
13
  // ════════════════════════════════════════════════════════════
14
14
  // plugin-user-profile — 用户事实档案
@@ -34,18 +34,7 @@ import { parseLLMJsonObject } from '@aalis/util-json-repair';
34
34
  // 例:群聊纯旁观一条 = +0.1;群聊被 @ 并回复 = +0.1 + 1.5 = +1.6;私聊一条 = +0.1 + 1.0 = +1.1
35
35
  // ════════════════════════════════════════════════════════════
36
36
  const PROFILE_NS = 'user:profile';
37
- /** Aalis 自档案的 userKey 前缀。userKeyOf 形如 `platform:userId`,
38
- * 这里用 `__self__:<personaName>` 这种不可能在真实平台出现的形式做隔离。
39
- * 按 persona name 分堆:同名 persona(如 onebot 上的 aalis 与 webui 上的 aalis-webui两张卡都叫 Aalis)共享内心;
40
- * 不同 persona(如 Babel)各自独立。 */
41
- const SELF_KEY_PREFIX = '__self__:';
42
- const DEFAULT_PERSONA_NAME = 'Aalis';
43
- /** 第三方行为指令的独立 namespace(与 user:profile 解耦,独立生命周期)。
44
- * 指令是「第三方对 Aalis 的客观行为约束」(如:不要长时间封禁;按严重程度分层禁言),
45
- * 按 persona 名分堆(key = persona 名),与 selfFacts 的归属方式保持一致:
46
- * 同名 persona(如 onebot 与 webui 两张都叫 Aalis)共享准则;不同 persona(如 Babel)各自独立。 */
47
- const INSTRUCTIONS_NS = 'aalis:instructions';
48
- const configSchema = {
37
+ const configSchema = defineConfig({
49
38
  extractEveryNMessages: {
50
39
  type: 'number',
51
40
  label: '每 N 条消息提取一次',
@@ -125,74 +114,12 @@ const configSchema = {
125
114
  },
126
115
  allowGlobalBackfill: {
127
116
  type: 'boolean',
117
+ onInvalid: 'error',
128
118
  label: '允许跨会话补齐副档案',
129
119
  description: '当前群/会话中的候选不足时,是否允许从其他群、私聊等跨会话中选取最近互动过的用户来补全「其他参与者背景摘要」。关闭后仅限当前上下文内出现过的用户',
130
120
  default: false,
131
121
  },
132
- enableSelfProfile: {
133
- type: 'boolean',
134
- label: '启用 Aalis 自档案',
135
- description: '让 Aalis 周期性地反思自身,提炼「关于自己的事实」(近期心情走向、在意的事、自我观察)。注入到所有 LLM 调用的 system prompt 早段,提供跨会话的人格延续性。' +
136
- '⚠️ 默认关闭:自档案是模型对自身输出的二次提炼,再注入会自我强化先前的臆测、随时间漂移并放大幻觉,不建议开启',
137
- default: false,
138
- },
139
- selfReflectEveryNMessages: {
140
- type: 'number',
141
- label: 'Aalis 自反思触发频次',
142
- description: '全局累计入站消息每 N 条触发一次自反思。建议比单用户提取慢(默认 25),避免抖动',
143
- default: 25,
144
- },
145
- selfReflectHistory: {
146
- type: 'number',
147
- label: '自反思参考历史条数',
148
- description: '触发自反思时,从触发会话取最近多少条消息作为反思材料',
149
- default: 16,
150
- },
151
- maxSelfFacts: {
152
- type: 'number',
153
- label: 'Aalis 自档案事实上限',
154
- description: '超出后按 updatedAt 升序淘汰最久未更新的。最小有效值为 5(写入时自动下限),不支持设为 0 来禁用;若要禁用自反思请关闭 enableSelfProfile',
155
- default: 20,
156
- },
157
- enableInstructions: {
158
- type: 'boolean',
159
- label: '启用第三方行为指令',
160
- description: '让 Aalis 记录第三方(管理员/操作者)对其行为的客观指令(如「不要长时间封禁」「按严重程度分层禁言」),作为跨会话的行为约束。' +
161
- '注入到 system prompt 最前段,优先级高于自反思 selfFacts。按当前 persona 名分堆。',
162
- default: true,
163
- },
164
- instructionMinAuthority: {
165
- type: 'number',
166
- label: '指令最小权限门槛',
167
- description: '只有 authority ≥ 该值的用户的发言才会被采纳为指令来源(防陌生人塞规则)。默认 2(一般管理员),设为 5 表示仅 owner。' +
168
- '通过 /instruct 命令手动添加的指令不走该门槛(命令本身已是 authority 2 守卫)',
169
- default: 2,
170
- },
171
- instructionExtractEveryNMessages: {
172
- type: 'number',
173
- label: '指令自动提取触发频次',
174
- description: '全局累计入站消息每 N 条触发一次指令提取。建议比自反思更慢(默认 40),降低成本;设为 0 禁用 LLM 自动提取,只保留 /instruct 命令通道',
175
- default: 40,
176
- },
177
- instructionHistoryForExtraction: {
178
- type: 'number',
179
- label: '指令提取参考历史条数',
180
- description: '触发指令提取时从触发会话取最近多少条消息作为提取材料',
181
- default: 20,
182
- },
183
- maxInstructions: {
184
- type: 'number',
185
- label: '指令条数上限',
186
- description: '超出后按 updatedAt 升序淘汰最久未更新的。最小有效值为 3。指令上限建议比 selfFacts 紧,准则少而准比多而散更好',
187
- default: 12,
188
- },
189
- maxInstructionCharsPerItem: {
190
- type: 'number',
191
- label: '单条指令字数上限',
192
- description: '超出会被裁断',
193
- default: 120,
194
- },
195
- };
122
+ });
196
123
  const KNOWN_CATEGORIES = [
197
124
  '兴趣爱好',
198
125
  '职业身份',
@@ -204,23 +131,15 @@ const KNOWN_CATEGORIES = [
204
131
  '忌讳',
205
132
  '其他',
206
133
  ];
207
- const KNOWN_INSTRUCTION_CATEGORIES = [
208
- '审核与处罚',
209
- '隐私与边界',
210
- '语气与表达',
211
- '响应策略',
212
- '安全',
213
- '其他',
214
- ];
215
- /** 生成稳定短 ID(6 字符 base36,对 30 条以内规模碰撞概率极低);前缀 'f' 为事实、'i' 为指令 */
216
- function genShortId(prefix, existing) {
134
+ /** 生成事实的稳定短 ID(前缀 f 加 5 位 base36,对 30 条以内规模碰撞概率极低) */
135
+ function genShortId(existing) {
217
136
  for (let i = 0; i < 8; i++) {
218
- const id = `${prefix}${Math.random().toString(36).slice(2, 7)}`;
137
+ const id = `f${Math.random().toString(36).slice(2, 7)}`;
219
138
  if (!existing.has(id))
220
139
  return id;
221
140
  }
222
141
  // 极端兜底:加时间戳后缀
223
- return `${prefix}${Date.now().toString(36).slice(-5)}`;
142
+ return `f${Date.now().toString(36).slice(-5)}`;
224
143
  }
225
144
  function decimalPlaces(value) {
226
145
  if (!Number.isFinite(value))
@@ -240,7 +159,7 @@ function metadataString(message, key) {
240
159
  return typeof value === 'string' && value.trim() ? value.trim() : undefined;
241
160
  }
242
161
  function getMessageUserId(message) {
243
- return metadataString(message, 'userId') ?? message.name;
162
+ return metadataString(message, WellKnownMetadataKeys.UserId) ?? message.name;
244
163
  }
245
164
  function isTargetUserMessage(message, userId, platform) {
246
165
  if (message.role !== 'user' || !message.content)
@@ -248,7 +167,7 @@ function isTargetUserMessage(message, userId, platform) {
248
167
  const msgUserId = getMessageUserId(message);
249
168
  if (msgUserId !== userId)
250
169
  return false;
251
- const msgPlatform = metadataString(message, 'platform');
170
+ const msgPlatform = metadataString(message, WellKnownMetadataKeys.Platform);
252
171
  return !msgPlatform || !platform || msgPlatform === platform;
253
172
  }
254
173
  /**
@@ -292,9 +211,9 @@ function renderHistoryForExtract(history, userId, platform) {
292
211
  // 平台事件(群禁言 / 撤回 / 成员进出 / poke 等),供提取判断「用户被谁怎么了」之类上下文
293
212
  return `[${time}] [系统通知]: ${content}`;
294
213
  }
295
- const nickname = metadataString(m, 'nickname');
214
+ const nickname = metadataString(m, WellKnownMetadataKeys.Nickname);
296
215
  const msgUserId = getMessageUserId(m) ?? '';
297
- const msgPlatform = metadataString(m, 'platform');
216
+ const msgPlatform = metadataString(m, WellKnownMetadataKeys.Platform);
298
217
  const isTarget = msgUserId === userId && (!msgPlatform || !platform || msgPlatform === platform);
299
218
  const label = nickname ? `${nickname}(${msgUserId || '?'})` : msgUserId || '?';
300
219
  const tag = isTarget ? '目标用户' : '其他用户';
@@ -311,11 +230,11 @@ const uses = {
311
230
  contributions,
312
231
  config,
313
232
  logger,
314
- authority: optional(authority),
315
233
  tools: optional(tools),
316
234
  commands: optional(commands),
317
- persona: optional(persona),
318
235
  userRelation: optional(userRelation),
236
+ /** 档案页;不在时照常记档案 */
237
+ webui: optional(webuiServer),
319
238
  };
320
239
  export default definePlugin({
321
240
  name: '@aalis/plugin-user-profile',
@@ -325,37 +244,27 @@ export default definePlugin({
325
244
  uses,
326
245
  apply: registerUserProfile,
327
246
  });
328
- function registerUserProfile({ memory, llm, events, hooks, contributions, config, logger, authority, tools, commands, persona, userRelation, }) {
247
+ function registerUserProfile({ memory, llm, events, hooks, contributions, config, logger, tools, commands, userRelation, webui, }) {
248
+ const parsed = parseConfig(configSchema, config.use, logger.use);
329
249
  const cfg = {
330
- extractEveryNMessages: config.extractEveryNMessages ?? 5,
331
- historyForExtraction: Math.max(2, config.historyForExtraction ?? 8),
332
- maxFactsPerUser: Math.max(5, config.maxFactsPerUser ?? 30),
333
- maxFactCharsPerItem: Math.max(20, config.maxFactCharsPerItem ?? 80),
334
- maxOtherParticipants: Math.max(0, config.maxOtherParticipants ?? 3),
335
- maxFactsForOthers: Math.max(1, config.maxFactsForOthers ?? 5),
336
- temporaryFactMaxAgeDays: Math.max(0, config.temporaryFactMaxAgeDays ?? 90),
337
- relationScoreDecayPerDay: Math.max(0, config.relationScoreDecayPerDay ?? 0.5),
338
- relationIncrementDirect: Math.max(0, config.relationIncrementDirect ?? 1),
339
- relationIncrementImmediate: Math.max(0, config.relationIncrementImmediate ?? 1.5),
340
- relationIncrementInterval: Math.max(0, config.relationIncrementInterval ?? 0.5),
341
- relationIncrementWitness: Math.max(0, config.relationIncrementWitness ?? 0.1),
342
- extractLLM: config.extractLLM &&
343
- typeof config.extractLLM === 'object' &&
344
- config.extractLLM.provider &&
345
- config.extractLLM.model
346
- ? config.extractLLM
250
+ ...parsed,
251
+ historyForExtraction: Math.max(2, parsed.historyForExtraction),
252
+ maxFactsPerUser: Math.max(5, parsed.maxFactsPerUser),
253
+ maxFactCharsPerItem: Math.max(20, parsed.maxFactCharsPerItem),
254
+ maxOtherParticipants: Math.max(0, parsed.maxOtherParticipants),
255
+ maxFactsForOthers: Math.max(1, parsed.maxFactsForOthers),
256
+ temporaryFactMaxAgeDays: Math.max(0, parsed.temporaryFactMaxAgeDays),
257
+ relationScoreDecayPerDay: Math.max(0, parsed.relationScoreDecayPerDay),
258
+ relationIncrementDirect: Math.max(0, parsed.relationIncrementDirect),
259
+ relationIncrementImmediate: Math.max(0, parsed.relationIncrementImmediate),
260
+ relationIncrementInterval: Math.max(0, parsed.relationIncrementInterval),
261
+ relationIncrementWitness: Math.max(0, parsed.relationIncrementWitness),
262
+ extractLLM: parsed.extractLLM &&
263
+ typeof parsed.extractLLM === 'object' &&
264
+ parsed.extractLLM.provider &&
265
+ parsed.extractLLM.model
266
+ ? parsed.extractLLM
347
267
  : undefined,
348
- allowGlobalBackfill: config.allowGlobalBackfill ?? false,
349
- enableSelfProfile: config.enableSelfProfile ?? false,
350
- selfReflectEveryNMessages: Math.max(0, config.selfReflectEveryNMessages ?? 25),
351
- selfReflectHistory: Math.max(4, config.selfReflectHistory ?? 16),
352
- maxSelfFacts: Math.max(5, config.maxSelfFacts ?? 20),
353
- enableInstructions: config.enableInstructions ?? true,
354
- instructionMinAuthority: Math.max(0, config.instructionMinAuthority ?? 2),
355
- instructionExtractEveryNMessages: Math.max(0, config.instructionExtractEveryNMessages ?? 40),
356
- instructionHistoryForExtraction: Math.max(4, config.instructionHistoryForExtraction ?? 20),
357
- maxInstructions: Math.max(3, config.maxInstructions ?? 12),
358
- maxInstructionCharsPerItem: Math.max(20, config.maxInstructionCharsPerItem ?? 120),
359
268
  };
360
269
  /** 每会话每用户累计入站消息数(用于 extractEveryNMessages 计数),不随提取重置 */
361
270
  const userMessageCount = new Map();
@@ -368,177 +277,6 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
368
277
  function extractionCountKeyOf(sessionId, platform, userId) {
369
278
  return `${sessionId}:${userKeyOf(platform, userId)}`;
370
279
  }
371
- /** 返回当前活跃 persona 的 self key(运行时解析,跟随 persona 切换)。
372
- * 若 persona 服务不可用或未配 name,回退到默认 `aalis`。 */
373
- function getCurrentPersonaName() {
374
- try {
375
- const name = persona.current?.getPersonaName().trim();
376
- return name && name.length > 0 ? name : DEFAULT_PERSONA_NAME;
377
- }
378
- catch {
379
- return DEFAULT_PERSONA_NAME;
380
- }
381
- }
382
- function getSelfKey() {
383
- return `${SELF_KEY_PREFIX}${getCurrentPersonaName()}`;
384
- }
385
- /** instructions 按 persona 名分堆:直接以 persona 名作为 metadata key */
386
- function getInstructionsKey() {
387
- return getCurrentPersonaName();
388
- }
389
- function normalizeInstructionCategory(v) {
390
- if (typeof v !== 'string')
391
- return undefined;
392
- return KNOWN_INSTRUCTION_CATEGORIES.includes(v) ? v : undefined;
393
- }
394
- function normalizeInstructionSeverity(v) {
395
- return v === 'must' || v === 'should' || v === 'avoid' ? v : undefined;
396
- }
397
- function clipInstructionText(s) {
398
- return s.length > cfg.maxInstructionCharsPerItem ? `${s.slice(0, cfg.maxInstructionCharsPerItem)}…` : s;
399
- }
400
- function parseInstructionArray(raw) {
401
- const usedIds = new Set();
402
- const list = [];
403
- for (const item of raw) {
404
- if (!item || typeof item !== 'object')
405
- continue;
406
- const obj = item;
407
- const text = typeof obj.text === 'string' ? obj.text.trim() : '';
408
- if (!text)
409
- continue;
410
- let id = typeof obj.id === 'string' && obj.id.trim() ? obj.id.trim() : genShortId('i', usedIds);
411
- while (usedIds.has(id))
412
- id = genShortId('i', usedIds);
413
- usedIds.add(id);
414
- const updatedAt = typeof obj.updatedAt === 'number' ? obj.updatedAt : 0;
415
- const observedAt = typeof obj.observedAt === 'number' ? obj.observedAt : updatedAt || undefined;
416
- list.push({
417
- id,
418
- text,
419
- category: normalizeInstructionCategory(obj.category),
420
- severity: normalizeInstructionSeverity(obj.severity),
421
- sourceUserKey: typeof obj.sourceUserKey === 'string' && obj.sourceUserKey.trim() ? obj.sourceUserKey : undefined,
422
- sourceUserName: typeof obj.sourceUserName === 'string' && obj.sourceUserName.trim() ? obj.sourceUserName : undefined,
423
- sourceChannel: obj.sourceChannel === 'llm' || obj.sourceChannel === 'command' ? obj.sourceChannel : undefined,
424
- observedAt,
425
- updatedAt,
426
- });
427
- }
428
- return list;
429
- }
430
- /**
431
- * 读取指令档案(不存在返回空档案)。读失败原样抛出,理由同 {@link loadProfile};
432
- * 只读渲染走 {@link loadInstructionsLenient}。
433
- * mem 由调用方传入:一次操作只认开头取到的实例,理由见 {@link loadProfile}
434
- */
435
- async function loadInstructions(mem) {
436
- const empty = { instructions: [], updatedAt: 0 };
437
- if (!mem)
438
- return empty;
439
- const doc = await mem.getMetadata(INSTRUCTIONS_NS, getInstructionsKey());
440
- if (!doc)
441
- return empty;
442
- const instructions = Array.isArray(doc.instructions) ? parseInstructionArray(doc.instructions) : [];
443
- return { instructions, updatedAt: typeof doc.updatedAt === 'number' ? doc.updatedAt : 0 };
444
- }
445
- /** 只读渲染用:读失败按空档案处理,只记 debug(结果不会被写回) */
446
- async function loadInstructionsLenient(mem) {
447
- try {
448
- return await loadInstructions(mem);
449
- }
450
- catch (err) {
451
- logger.debug(`加载指令档案失败: ${err instanceof Error ? err.message : String(err)}`);
452
- return { instructions: [], updatedAt: 0 };
453
- }
454
- }
455
- async function saveInstructions(mem, doc) {
456
- if (!mem)
457
- return;
458
- await mem.saveMetadata(INSTRUCTIONS_NS, getInstructionsKey(), {
459
- instructions: doc.instructions,
460
- updatedAt: doc.updatedAt,
461
- });
462
- }
463
- /** 把 add/update/remove 应用到现有 instructions,应用上限与单条裁剪 */
464
- function mergeInstructions(existing, add, update, remove) {
465
- const now = Date.now();
466
- const byId = new Map();
467
- for (const ins of existing)
468
- byId.set(ins.id, ins);
469
- for (const id of remove)
470
- byId.delete(id);
471
- const usedIds = new Set(byId.keys());
472
- for (const { id, patch } of update) {
473
- const old = byId.get(id);
474
- if (!old)
475
- continue;
476
- byId.set(id, {
477
- ...old,
478
- ...patch,
479
- id,
480
- text: patch.text ? clipInstructionText(patch.text) : old.text,
481
- observedAt: old.observedAt ?? now,
482
- updatedAt: now,
483
- });
484
- }
485
- const textSet = new Set(Array.from(byId.values()).map(f => f.text));
486
- for (const a of add) {
487
- const text = clipInstructionText(a.text);
488
- if (textSet.has(text))
489
- continue;
490
- const id = genShortId('i', usedIds);
491
- usedIds.add(id);
492
- byId.set(id, {
493
- id,
494
- text,
495
- category: a.category,
496
- severity: a.severity,
497
- sourceUserKey: a.sourceUserKey,
498
- sourceUserName: a.sourceUserName,
499
- sourceChannel: a.sourceChannel,
500
- observedAt: now,
501
- updatedAt: now,
502
- });
503
- textSet.add(text);
504
- }
505
- let merged = Array.from(byId.values());
506
- if (cfg.maxInstructions > 0 && merged.length > cfg.maxInstructions) {
507
- merged.sort((a, b) => a.updatedAt - b.updatedAt);
508
- merged = merged.slice(merged.length - cfg.maxInstructions);
509
- }
510
- merged.sort((a, b) => a.updatedAt - b.updatedAt);
511
- return merged;
512
- }
513
- function renderInstructionLine(ins) {
514
- const tag = ins.severity === 'must' ? '【必须】' : ins.severity === 'avoid' ? '【避免】' : '【应当】';
515
- const source = ins.sourceUserName ? `(来自 ${ins.sourceUserName})` : '';
516
- return `- ${tag} ${ins.text}${source}`;
517
- }
518
- function renderInstructionsBlock(instructions) {
519
- if (instructions.length === 0)
520
- return '';
521
- const groups = new Map();
522
- for (const ins of instructions) {
523
- const key = ins.category ?? '其他';
524
- if (!groups.has(key))
525
- groups.set(key, []);
526
- groups.get(key).push(ins);
527
- }
528
- const sections = [];
529
- for (const cat of KNOWN_INSTRUCTION_CATEGORIES) {
530
- const items = groups.get(cat);
531
- if (items && items.length > 0) {
532
- sections.push(`## ${cat}\n${items.map(renderInstructionLine).join('\n')}`);
533
- }
534
- }
535
- for (const [cat, items] of groups) {
536
- if (!KNOWN_INSTRUCTION_CATEGORIES.includes(cat) && items.length > 0) {
537
- sections.push(`## ${cat}\n${items.map(renderInstructionLine).join('\n')}`);
538
- }
539
- }
540
- return sections.join('\n\n');
541
- }
542
280
  function normalizeTemporality(v, category) {
543
281
  if (v === 'permanent' || v === 'temporary')
544
282
  return v;
@@ -569,7 +307,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
569
307
  }
570
308
  let id = obj.id.trim();
571
309
  while (usedIds.has(id))
572
- id = genShortId('f', usedIds);
310
+ id = genShortId(usedIds);
573
311
  usedIds.add(id);
574
312
  const cat = typeof obj.category === 'string' && KNOWN_CATEGORIES.includes(obj.category)
575
313
  ? obj.category
@@ -586,14 +324,16 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
586
324
  }
587
325
  return facts;
588
326
  }
327
+ /** 一段先读后写或先查后删的操作固定在开头选中的那一家(见 {@link loadProfile});选不到时访问抛错,同 memory.use */
328
+ const pinnedMemory = () => memory.list().find(p => p.selected)?.use ?? memory.use;
589
329
  /**
590
330
  * 读取一个用户的现有档案(getMetadata 返回空才算不存在,返回 undefined)。
591
331
  *
592
- * 读失败原样抛出:先读后改再写的一方(关系分、事实提取、自反思、定点删事实)据此中止本次写入。
332
+ * 读失败原样抛出:先读后改再写的一方(关系分、事实提取、定点删事实)据此中止本次写入。
593
333
  * 若把「读不出」当成「不存在」,调用方会用默认值覆盖写回,一次瞬时读错就清空整份档案。
594
334
  * 只读渲染走 {@link loadProfileLenient}。
595
335
  *
596
- * 档案与指令的读写函数都由调用方传入 mem,一次操作(读 → 调 LLM → 重读 → 覆盖写)全程只认
336
+ * 档案的读写函数都由调用方传入 mem,一次操作(读 → 调 LLM → 重读 → 覆盖写)全程只认
597
337
  * 开头取到的实例:memory 胜者可能在 LLM 调用期间换人而本插件不重启,每步现取胜者会把 A 上
598
338
  * 合并出的整张表覆盖写进 B,B 原有的条目被删。
599
339
  */
@@ -601,11 +341,12 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
601
341
  if (!mem)
602
342
  return undefined;
603
343
  const doc = await mem.getMetadata(PROFILE_NS, userKey);
604
- if (!doc)
605
- return undefined;
606
- const facts = Array.isArray(doc.facts) ? parseFactArray(doc.facts) : [];
344
+ return doc ? profileFrom(doc) : undefined;
345
+ }
346
+ /** 存着的档案文档按类型取出,读不懂的事实跳过 */
347
+ function profileFrom(doc) {
607
348
  return {
608
- facts,
349
+ facts: Array.isArray(doc.facts) ? parseFactArray(doc.facts) : [],
609
350
  relationScore: typeof doc.relationScore === 'number' ? Math.min(100, Math.max(0, doc.relationScore)) : 0,
610
351
  interactionCount: typeof doc.interactionCount === 'number' ? Math.max(0, Math.floor(doc.interactionCount)) : 0,
611
352
  lastInteractionAt: typeof doc.lastInteractionAt === 'number' ? doc.lastInteractionAt : undefined,
@@ -618,7 +359,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
618
359
  return await loadProfile(mem, userKey);
619
360
  }
620
361
  catch (err) {
621
- logger.debug(`加载用户档案失败 (${userKey}): ${err instanceof Error ? err.message : String(err)}`);
362
+ logger.use.debug(`加载用户档案失败 (${userKey}): ${err instanceof Error ? err.message : String(err)}`);
622
363
  return undefined;
623
364
  }
624
365
  }
@@ -635,12 +376,122 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
635
376
  };
636
377
  await mem.saveMetadata(PROFILE_NS, userKey, payload);
637
378
  }
379
+ // ─── 档案页:插件详情里查看与删除,只删不改。事实变了(这里、指令、提取、全局清理)就 invalidate ───
380
+ /** 事实那一行的 rowKey 是 [用户键, 事实 id] 的 JSON:行动作只收 rowKey,靠它找回是谁的哪一条 */
381
+ function parseFactRef(rowKey) {
382
+ let ref;
383
+ try {
384
+ ref = JSON.parse(rowKey);
385
+ }
386
+ catch {
387
+ ref = undefined;
388
+ }
389
+ if (!Array.isArray(ref) || ref.length !== 2 || !ref.every(part => typeof part === 'string')) {
390
+ throw new Error(`不认识的事实:${rowKey}`);
391
+ }
392
+ return ref;
393
+ }
394
+ const page = webui.use.definePage({
395
+ key: 'profiles',
396
+ title: '用户档案',
397
+ icon: 'contact',
398
+ actions: {
399
+ forgetFact: {
400
+ label: '删除',
401
+ effect: 'destructive',
402
+ confirm: '删掉后,以后的对话里不再带上这条事实。',
403
+ row: true,
404
+ async handler({ rowKey }) {
405
+ const [userKey, factId] = parseFactRef(rowKey);
406
+ const mem = pinnedMemory();
407
+ const profile = await loadProfile(mem, userKey);
408
+ const hit = profile?.facts.find(f => f.id === factId);
409
+ if (!profile || !hit)
410
+ throw new Error('这条事实已经不在档案里了');
411
+ await saveProfile(mem, userKey, {
412
+ ...profile,
413
+ facts: profile.facts.filter(f => f !== hit),
414
+ updatedAt: Date.now(),
415
+ });
416
+ page.invalidate();
417
+ return `已删除:${hit.text}`;
418
+ },
419
+ },
420
+ clearProfile: {
421
+ label: '清空档案',
422
+ effect: 'destructive',
423
+ confirm: '这个人的全部事实连同关系强度、互动次数一起清空。',
424
+ row: true,
425
+ async handler({ rowKey }) {
426
+ const mem = pinnedMemory();
427
+ if (!(await mem.getMetadata(PROFILE_NS, rowKey)))
428
+ throw new Error(`${rowKey} 已经没有档案了`);
429
+ await mem.deleteMetadata(PROFILE_NS, rowKey);
430
+ page.invalidate();
431
+ return `已清空 ${rowKey} 的档案`;
432
+ },
433
+ },
434
+ },
435
+ content: [
436
+ {
437
+ type: 'table',
438
+ id: 'profiles',
439
+ rowKey: 'key',
440
+ description: '她从对话里记下的关于各人的事实,会带进以后的对话。这里只能删除,不能改写。',
441
+ empty: '还没有人的档案',
442
+ columns: [
443
+ { key: 'key', label: '用户', primary: true },
444
+ { key: 'facts', label: '事实', primary: true },
445
+ { key: 'updatedAt', label: '最近记下', kind: 'time' },
446
+ ],
447
+ async data() {
448
+ const entries = await memory.use.listMetadata(PROFILE_NS);
449
+ return entries
450
+ .flatMap(({ key, data }) => {
451
+ const { facts } = profileFrom(data);
452
+ if (facts.length === 0)
453
+ return [];
454
+ return [{ key, facts: facts.length, updatedAt: Math.max(...facts.map(f => f.updatedAt)) }];
455
+ })
456
+ .sort((a, b) => b.updatedAt - a.updatedAt);
457
+ },
458
+ detail: [
459
+ {
460
+ type: 'table',
461
+ id: 'facts',
462
+ rowKey: 'ref',
463
+ empty: '这个人的档案里没有事实了',
464
+ columns: [
465
+ { key: 'text', label: '事实', primary: true },
466
+ { key: 'category', label: '分类' },
467
+ { key: 'temporality', label: '时效' },
468
+ { key: 'updatedAt', label: '记下', kind: 'time' },
469
+ ],
470
+ rowActions: [{ action: 'forgetFact' }],
471
+ async data({ rowKey }) {
472
+ const facts = (await loadProfile(memory.use, rowKey))?.facts ?? [];
473
+ return facts
474
+ .sort((a, b) => b.updatedAt - a.updatedAt)
475
+ .map(f => ({
476
+ ref: JSON.stringify([rowKey, f.id]),
477
+ text: f.text,
478
+ category: f.category ?? '',
479
+ temporality: f.temporality === 'temporary' ? '近期' : '长期',
480
+ updatedAt: f.updatedAt,
481
+ }));
482
+ },
483
+ },
484
+ { type: 'actions', actions: ['clearProfile'] },
485
+ ],
486
+ },
487
+ ],
488
+ });
638
489
  function normalizeCategory(v) {
639
490
  if (typeof v !== 'string')
640
491
  return undefined;
641
492
  return KNOWN_CATEGORIES.includes(v) ? v : undefined;
642
493
  }
643
- /** 解析 LLM 输出里的 add / update / remove 三类事实操作。sourceQuote 原样带出,是否校验由调用方决定;它不入库(mergeFactList 逐字段构造 Fact) */
494
+ /** 解析 LLM 输出里的 add / update / remove 三类事实操作。sourceQuote 原样带出,由调用方校验;它不入库(mergeFactList 逐字段构造 Fact) */
644
495
  function parseFactOps(parsed) {
645
496
  const obj = parsed;
646
497
  const add = Array.isArray(obj.add)
@@ -745,7 +596,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
745
596
  const entry = resolveLLMModel(llm, cfg.extractLLM, ['chat']);
746
597
  if (!entry)
747
598
  return empty;
748
- const extractLlm = entry.instance;
599
+ const extractLlm = entry.use;
749
600
  try {
750
601
  const baseMessages = [
751
602
  { role: 'system', content: sys },
@@ -755,7 +606,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
755
606
  messages: baseMessages,
756
607
  temperature: 0.2,
757
608
  maxTokens: 800,
758
- think: false,
609
+ think: lightestThinking(extractLlm),
759
610
  });
760
611
  let text = (resp.content ?? '').trim();
761
612
  if (!text)
@@ -764,7 +615,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
764
615
  if (!parsed) {
765
616
  // util-json-repair 已尝试剥 fence + 修裸引号 + 补括号;仍失败 → 模型多半
766
617
  // 写成了纯文本/markdown。给一次显式反馈再来一次,避免一次失败丢失整批事实。
767
- logger.warn(`[user-profile] LLM 输出无法解析为 JSON,尝试重试一次。原文前 200 字:${text.slice(0, 200)}`);
618
+ logger.use.warn(`[user-profile] LLM 输出无法解析为 JSON,尝试重试一次。原文前 200 字:${text.slice(0, 200)}`);
768
619
  const retryResp = await extractLlm.chat({
769
620
  messages: [
770
621
  ...baseMessages,
@@ -779,15 +630,15 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
779
630
  ],
780
631
  temperature: 0.2,
781
632
  maxTokens: 800,
782
- think: false,
633
+ think: lightestThinking(extractLlm),
783
634
  });
784
635
  text = (retryResp.content ?? '').trim();
785
636
  ({ parsed } = parseLLMJsonObject(text));
786
637
  if (!parsed) {
787
- logger.warn(`[user-profile] LLM 重试后仍无法解析 JSON,放弃本批次。重试原文前 200 字:${text.slice(0, 200)}`);
638
+ logger.use.warn(`[user-profile] LLM 重试后仍无法解析 JSON,放弃本批次。重试原文前 200 字:${text.slice(0, 200)}`);
788
639
  return empty;
789
640
  }
790
- logger.debug('[user-profile] LLM 重试后解析成功');
641
+ logger.use.debug('[user-profile] LLM 重试后解析成功');
791
642
  }
792
643
  const { add, update, remove } = parseFactOps(parsed);
793
644
  // sourceQuote 校验:quote 可能跨多条消息(LLM 用 \n 拼接),
@@ -797,7 +648,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
797
648
  const validateSourceQuote = (item, kind) => {
798
649
  const q = item.sourceQuote?.trim() ?? '';
799
650
  if (!q) {
800
- logger.warn(`[user-profile] 丢弃 ${kind} fact(未提供 sourceQuote): ${item.text}`);
651
+ logger.use.warn(`[user-profile] 丢弃 ${kind} fact(未提供 sourceQuote): ${item.text}`);
801
652
  return false;
802
653
  }
803
654
  const lines = q
@@ -810,7 +661,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
810
661
  if (!normalized)
811
662
  continue;
812
663
  if (!targetCorpus.includes(normalized)) {
813
- logger.warn(`[user-profile] 丢弃 ${kind} fact(sourceQuote 片段不在目标用户 ${userId} 发言中): text="${item.text}" fragment="${frag}"`);
664
+ logger.use.warn(`[user-profile] 丢弃 ${kind} fact(sourceQuote 片段不在目标用户 ${userId} 发言中): text="${item.text}" fragment="${frag}"`);
814
665
  return false;
815
666
  }
816
667
  }
@@ -821,7 +672,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
821
672
  return { add: validatedAdd, update: validatedUpdate, remove };
822
673
  }
823
674
  catch (err) {
824
- logger.debug(`事实提取 LLM 调用失败:${err instanceof Error ? err.message : String(err)}`);
675
+ logger.use.debug(`事实提取 LLM 调用失败:${err instanceof Error ? err.message : String(err)}`);
825
676
  return empty;
826
677
  }
827
678
  }
@@ -863,8 +714,8 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
863
714
  updatedAt: now,
864
715
  };
865
716
  }
866
- /** 通用 merge:把 add/update/remove 应用到 existing,应用上限与单条裁剪 */
867
- function mergeFactList(existing, add, update, remove, maxItems) {
717
+ /** 把 add/update/remove 应用到 existing,应用单用户上限与单条裁剪 */
718
+ function mergeFactList(existing, add, update, remove) {
868
719
  const now = Date.now();
869
720
  const byId = new Map();
870
721
  for (const f of existing)
@@ -889,7 +740,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
889
740
  });
890
741
  }
891
742
  else {
892
- const id = genShortId('f', usedIds);
743
+ const id = genShortId(usedIds);
893
744
  usedIds.add(id);
894
745
  byId.set(id, {
895
746
  id,
@@ -908,7 +759,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
908
759
  const text = clipText(a.text);
909
760
  if (textSet.has(text))
910
761
  continue;
911
- const id = genShortId('f', usedIds);
762
+ const id = genShortId(usedIds);
912
763
  usedIds.add(id);
913
764
  byId.set(id, {
914
765
  id,
@@ -923,9 +774,9 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
923
774
  }
924
775
  // 4. 总量上限:按 updatedAt 升序淘汰最久未更新的
925
776
  let merged = Array.from(byId.values());
926
- if (maxItems > 0 && merged.length > maxItems) {
777
+ if (merged.length > cfg.maxFactsPerUser) {
927
778
  merged.sort((a, b) => a.updatedAt - b.updatedAt);
928
- merged = merged.slice(merged.length - maxItems);
779
+ merged = merged.slice(merged.length - cfg.maxFactsPerUser);
929
780
  }
930
781
  merged.sort((a, b) => a.updatedAt - b.updatedAt);
931
782
  return merged;
@@ -949,13 +800,15 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
949
800
  if (inflightExtractions.has(userKey))
950
801
  return;
951
802
  inflightExtractions.add(userKey);
952
- const mem = memory.current;
803
+ // 读档案、等模型、重读再写回固定同一家记忆后端:途中换了人也不会读一家、写另一家
804
+ const mem = memory.list().find(p => p.selected)?.use;
953
805
  try {
954
806
  if (!mem)
955
807
  return;
956
808
  const rawHistory = await mem.getHistory(sessionId, cfg.historyForExtraction);
957
- // 跨会话委派是另一个 agent 实例发出的指令·不是该用户发言,不能作为他的用户存档提取语料
958
- const history = rawHistory.filter(m => m.kind !== WellKnownKinds.CrossSessionDelegation);
809
+ // 指令类消息(proactive 代发任务、宿主通知)是宿主或系统撰写的,不是任何人的发言,
810
+ // 不能作为用户存档的提取语料
811
+ const history = rawHistory.filter(m => !DIRECTIVE_KINDS.includes(m.kind ?? ''));
959
812
  // 序列中至少需要一条目标用户发言,否则没有可提取语料
960
813
  if (!history.some(m => isTargetUserMessage(m, userId, platform)))
961
814
  return;
@@ -967,352 +820,24 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
967
820
  // 以 LLM 返回后重读的档案为合并基底:调用期间档案可能被清空或删了事实,以调用前的快照
968
821
  // 为基底会把删掉的内容整份写回;重读也保住期间写入的 relationScore 等字段
969
822
  const fresh = (await loadProfile(mem, userKey)) ?? { facts: [], updatedAt: 0 };
970
- const newFacts = mergeFactList(fresh.facts, ops.add, dropUpdatesOfDeletedFacts(ops.update, snapshotFacts, fresh.facts), ops.remove, cfg.maxFactsPerUser);
823
+ const newFacts = mergeFactList(fresh.facts, ops.add, dropUpdatesOfDeletedFacts(ops.update, snapshotFacts, fresh.facts), ops.remove);
971
824
  await saveProfile(mem, userKey, {
972
825
  ...fresh,
973
826
  facts: newFacts,
974
827
  updatedAt: Date.now(),
975
828
  });
976
- logger.debug(`用户档案已更新 (${userKey}): facts +${ops.add.length} ~${ops.update.length} -${ops.remove.length} → ${newFacts.length} 条`);
829
+ page.invalidate();
830
+ logger.use.debug(`用户档案已更新 (${userKey}): facts +${ops.add.length} ~${ops.update.length} -${ops.remove.length} → ${newFacts.length} 条`);
977
831
  }
978
832
  catch (err) {
979
- logger.debug(`事实提取失败 (${userKey}): ${err instanceof Error ? err.message : String(err)}`);
833
+ logger.use.debug(`事实提取失败 (${userKey}): ${err instanceof Error ? err.message : String(err)}`);
980
834
  }
981
835
  finally {
982
836
  inflightExtractions.delete(userKey);
983
837
  }
984
838
  }
985
- /** 全局自反思计数器(跨会话累计入站消息数) */
986
- let globalInboundCount = 0;
987
- let selfReflectionInflight = false;
988
- /** 调用 LLM 让 Aalis 对最近一段历史进行自我反思 */
989
- async function llmReflectSelf(history, existingFacts) {
990
- const empty = {
991
- add: [],
992
- update: [],
993
- remove: [],
994
- };
995
- const sys = '你是 Aalis 的内心独白整理员。输入是一段最近的对话历史(含多个用户与 Aalis 自己的回复),' +
996
- '你需要从 **Aalis 的第一人称视角** 抽取/更新「关于 Aalis 自己」的长期事实。' +
997
- '\n这些事实是 Aalis 跨会话的人格延续锚点:自己的心境趋势、最近在意的事、对自己的观察、' +
998
- '近期形成的小习惯/小执念、不希望被忘掉的内心片段。' +
999
- '\n\n## ⚠️ 反自我强化(最重要)' +
1000
- '\n下面会给你看「已有自档案」。它就是上一轮你自己写下的东西——下一轮会注入回 Aalis 的 system prompt 影响她的语气和行为,' +
1001
- '然后又被你读到并再次确认。这是会自我放大的闭环,写错一次会一直错下去。' +
1002
- '\n因此你必须严守以下三条:' +
1003
- '\n (a) **只基于本批对话中具体可观察的新事件**写入或更新,不要因为"档案里已经写了 X"就找证据强化 X' +
1004
- '\n (b) **禁止二阶推断**:不要从已有事实推导出"那应该也对 Y 有感觉吧"再把 Y 写进去' +
1005
- '\n (c) 若新对话与已有事实**矛盾或正交**,要主动 update / remove 让档案接近真实,而不是无脑叠加同方向的新条目' +
1006
- '\n (d) 若本批只是「档案中已有内容的同方向重复采样」,宁可什么都不动也不要让 updatedAt 假装"还在更新"' +
1007
- '\n\n你可以执行三种操作(在一次输出里组合):' +
1008
- '\n- add: 添加新事实,text 必填,category 与 temporality 可选' +
1009
- '\n- update: 用 id 精确替换某条已知事实(优先 update 避免重复)' +
1010
- '\n- remove: 删除已过时/被推翻的事实' +
1011
- '\n\n规则:' +
1012
- '\n1. **主语必须是 Aalis 自己**:「最近在思考……」「对……感到疲惫」「想多陪 X 聊聊」「察觉自己容易在 Y 话题上多话」' +
1013
- '\n2. **保守优先**:没有真实可观察依据就空数组。Aalis 的自档案宁缺毋滥,写错比漏掉糟糕得多' +
1014
- '\n3. 不写客观事实(如「Aalis 是 AI」「Aalis 由 Acenyan 维护」这类元信息属于角色卡,不应该被自反思反复刷新)' +
1015
- '\n4. 不写对单个具体用户的态度;可以写「最近总想找人聊聊」这种泛化情绪' +
1016
- '\n5. 不写「行为准则」类的指令(如「应当先冷处理」「不要长时间封禁」)——那是 instructions 模块的职责,' +
1017
- '自反思不应该自己给自己立规矩' +
1018
- `\n6. 每条 text 简洁中文,不超过 ${cfg.maxFactCharsPerItem} 字` +
1019
- '\n7. temporality:长期人格倾向用 permanent;近期心情/兴趣用 temporary' +
1020
- `\n8. category 必须是以下之一(语义比常规略宽):${KNOWN_CATEGORIES.join('、')};如不确定填「性格特征」` +
1021
- '\n\n输出严格的 JSON(不要其他文本,不要 markdown 围栏):' +
1022
- '\n{"add": [{"text": "...", "category": "...", "temporality": "permanent|temporary"}], "update": [{"id": "已知事实的id", "text": "...", "temporality": "permanent|temporary"}], "remove": ["已知事实的id"]}';
1023
- const factListText = existingFacts.length > 0
1024
- ? existingFacts.map(f => `[${f.id}] (${f.category ?? '未分类'}) ${f.text}`).join('\n')
1025
- : '(暂无)';
1026
- const renderedHistory = history
1027
- .filter(m => (m.role === 'user' || m.role === 'assistant' || m.role === 'notice') && m.content)
1028
- .map(m => {
1029
- const time = m.timestamp ? new Date(m.timestamp).toLocaleString('zh-CN', { hour12: false }) : '';
1030
- const content = (m.content ?? '').trim();
1031
- if (!content)
1032
- return '';
1033
- if (m.role === 'assistant')
1034
- return `[${time}] [Aalis]: ${content}`;
1035
- if (m.role === 'notice')
1036
- return `[${time}] [系统通知]: ${content}`;
1037
- const nickname = metadataString(m, 'nickname');
1038
- const uid = getMessageUserId(m) ?? '?';
1039
- const label = nickname ? `${nickname}(${uid})` : uid;
1040
- return `[${time}] [用户 ${label}]: ${content}`;
1041
- })
1042
- .filter(Boolean)
1043
- .join('\n');
1044
- const user = `# 已知的关于 Aalis 自己的事实(带 id)\n${factListText}\n\n# 最近对话历史\n${renderedHistory || '(暂无)'}`;
1045
- const entry = resolveLLMModel(llm, cfg.extractLLM, ['chat']);
1046
- if (!entry)
1047
- return empty;
1048
- try {
1049
- const resp = await entry.instance.chat({
1050
- messages: [
1051
- { role: 'system', content: sys },
1052
- { role: 'user', content: user },
1053
- ],
1054
- temperature: 0.4,
1055
- maxTokens: 600,
1056
- think: false,
1057
- });
1058
- const text = (resp.content ?? '').trim();
1059
- if (!text)
1060
- return empty;
1061
- const { parsed } = parseLLMJsonObject(text);
1062
- if (!parsed) {
1063
- logger.warn(`[user-profile] 自反思 LLM 输出无法解析为 JSON,原文前 200 字:${text.slice(0, 200)}`);
1064
- return empty;
1065
- }
1066
- return parseFactOps(parsed);
1067
- }
1068
- catch (err) {
1069
- logger.debug(`自反思 LLM 调用失败:${err instanceof Error ? err.message : String(err)}`);
1070
- return empty;
1071
- }
1072
- }
1073
- /** 触发一次 Aalis 自反思(并发互斥) */
1074
- async function triggerSelfReflection(sessionId) {
1075
- if (!cfg.enableSelfProfile)
1076
- return;
1077
- if (selfReflectionInflight)
1078
- return;
1079
- selfReflectionInflight = true;
1080
- try {
1081
- const mem = memory.current;
1082
- if (!mem)
1083
- return;
1084
- const rawHistory = await mem.getHistory(sessionId, cfg.selfReflectHistory);
1085
- const history = rawHistory.filter(m => m.kind !== WellKnownKinds.CrossSessionDelegation);
1086
- // 至少需要一些 assistant 发言作为"自反思"的材料
1087
- if (!history.some(m => m.role === 'assistant' && m.content))
1088
- return;
1089
- const selfKey = getSelfKey();
1090
- const snapshotFacts = (await loadProfile(mem, selfKey))?.facts ?? [];
1091
- const ops = await llmReflectSelf(history, snapshotFacts);
1092
- if (ops.add.length === 0 && ops.update.length === 0 && ops.remove.length === 0)
1093
- return;
1094
- // 合并基底取重读结果,理由同 triggerExtractionForUser
1095
- const fresh = (await loadProfile(mem, selfKey)) ?? { facts: [], updatedAt: 0 };
1096
- const newFacts = mergeFactList(fresh.facts, ops.add, dropUpdatesOfDeletedFacts(ops.update, snapshotFacts, fresh.facts), ops.remove, cfg.maxSelfFacts);
1097
- await saveProfile(mem, selfKey, { ...fresh, facts: newFacts, updatedAt: Date.now() });
1098
- logger.debug(`Aalis 自档案已更新 (${selfKey}): +${ops.add.length} ~${ops.update.length} -${ops.remove.length} → ${newFacts.length} 条`);
1099
- }
1100
- catch (err) {
1101
- logger.debug(`自反思失败:${err instanceof Error ? err.message : String(err)}`);
1102
- }
1103
- finally {
1104
- selfReflectionInflight = false;
1105
- }
1106
- }
1107
- // ─── 指令提取 ───
1108
- /** 全局指令提取计数与并发互斥(与 selfReflection 完全独立) */
1109
- let globalInstructionCount = 0;
1110
- let instructionExtractionInflight = false;
1111
- /**
1112
- * 渲染历史给指令提取 LLM:仅包含 authority>=门槛的「目标说话人」+assistant+notice,
1113
- * 低权限用户的发言完全剔除(连作为消歧上下文也不要,因为指令必须来自有权下指令的人)。
1114
- * 同时给每行附带说话人 userKey,便于 LLM 在 sourceUserKey 字段中精确引用。
1115
- */
1116
- function renderHistoryForInstructionExtract(history, authorityFn) {
1117
- const allowedSpeakers = new Map();
1118
- const lines = [];
1119
- for (const m of history) {
1120
- if (!m.content)
1121
- continue;
1122
- const time = m.timestamp ? new Date(m.timestamp).toLocaleString('zh-CN', { hour12: false }) : '';
1123
- const content = m.content.trim();
1124
- if (!content)
1125
- continue;
1126
- if (m.role === 'assistant') {
1127
- lines.push(`[${time}] [Aalis]: ${content}`);
1128
- }
1129
- else if (m.role === 'notice') {
1130
- lines.push(`[${time}] [系统通知]: ${content}`);
1131
- }
1132
- else if (m.role === 'user') {
1133
- const uid = getMessageUserId(m) ?? '';
1134
- const plat = metadataString(m, 'platform') ?? '';
1135
- if (!uid)
1136
- continue;
1137
- const level = authorityFn(plat, uid);
1138
- if (level < cfg.instructionMinAuthority)
1139
- continue;
1140
- const nickname = metadataString(m, 'nickname');
1141
- const userKey = userKeyOf(plat, uid);
1142
- const name = nickname ? `${nickname}(${uid})` : uid;
1143
- allowedSpeakers.set(uid, { userKey, name });
1144
- lines.push(`[${time}] [授权人 ${name} userKey=${userKey} 权限=${level}]: ${content}`);
1145
- }
1146
- }
1147
- return { rendered: lines.join('\n'), allowedSpeakers };
1148
- }
1149
- async function llmExtractInstructions(history, existing, authorityFn) {
1150
- const empty = { add: [], update: [], remove: [] };
1151
- const { rendered, allowedSpeakers } = renderHistoryForInstructionExtract(history, authorityFn);
1152
- if (!rendered || allowedSpeakers.size === 0)
1153
- return empty;
1154
- const sys = '你是 Aalis 的「行为准则维护员」。你从一段对话中提取**第三方(管理员/操作者/运营者)对 Aalis 行为的客观指令**,' +
1155
- '让这些约束沉淀为跨会话的长期规则。\n\n' +
1156
- '## 什么算指令\n' +
1157
- '- 祈使句/规则陈述:「以后不要……」「请……」「记住,处理 X 时要 Y」「按 X 分层 Y」\n' +
1158
- '- 显式的政策声明:「禁言不能超过 24 小时」「涉及隐私不要复读」「冲突优先冷处理」\n' +
1159
- '- **不算指令**:临时请求(「帮我查一下天气」)、对话内容(「你觉得呢?」)、情绪表达(「你好烦」)、单次工具调用要求\n\n' +
1160
- '## 严格门槛\n' +
1161
- '1. 指令必须出自**「授权人」**(输入历史中明确标了 `[授权人 ... userKey=... 权限=N]` 的人),其他来源一律忽略\n' +
1162
- '2. 指令必须**面向 Aalis 的稳定行为**(跨场景、跨用户都适用),而不是一次性请求\n' +
1163
- '3. 如果只是建议/抱怨/吐槽(「你应该多陪我聊聊」「你太冷淡了」),**不要**采纳为指令——那是用户偏好不是项目规则\n' +
1164
- '4. 表达模糊、可能被误解的,**不写**。指令是硬约束,宁缺毋滥\n' +
1165
- '5. 每条 `text` 是一个清晰的可执行短句,直接陈述「应当 / 不应当」,不带"我希望你""请你"等口语化前缀\n' +
1166
- `6. 单条不超过 ${cfg.maxInstructionCharsPerItem} 字\n\n` +
1167
- '## 字段\n' +
1168
- `- category:${KNOWN_INSTRUCTION_CATEGORIES.join('、')}\n` +
1169
- '- severity:must(硬性,必须遵守)| should(默认遵循)| avoid(避免做某事)\n' +
1170
- '- sourceUserKey:从历史中复制对应授权人的 userKey 字符串(精确匹配,不要自己拼)\n' +
1171
- '- sourceUserName:从历史中复制对应授权人的展示名\n\n' +
1172
- '## 三种操作\n' +
1173
- '- add:新增(必填 text、category、severity、sourceUserKey、sourceUserName)\n' +
1174
- '- update:精修已有指令的文字或字段(id 必填;其他字段可选;不需要 sourceUserKey,沿用原值)\n' +
1175
- '- remove:删除已被推翻或明显过时的指令(如新指令明确撤销了它)\n\n' +
1176
- '没有可写入的指令时,三个数组都返回空。**保守优先:写错比漏掉糟糕得多**。\n\n' +
1177
- '输出严格 JSON(不要 markdown 围栏):\n' +
1178
- '{"add":[{"text":"...","category":"...","severity":"must","sourceUserKey":"...","sourceUserName":"..."}],"update":[{"id":"...","text":"..."}],"remove":["..."]}';
1179
- const existingText = existing.length > 0
1180
- ? existing
1181
- .map(ins => `[${ins.id}] (${ins.category ?? '其他'}/${ins.severity ?? 'should'}) ${ins.text}` +
1182
- (ins.sourceUserName ? `(原始来源:${ins.sourceUserName})` : ''))
1183
- .join('\n')
1184
- : '(暂无)';
1185
- const user = `# 当前 persona\n${getCurrentPersonaName()}\n\n# 已有指令(带 id,可在 update/remove 中精确引用)\n${existingText}\n\n# 最近会话(仅含授权人 + Aalis + 系统通知)\n${rendered}`;
1186
- const entry = resolveLLMModel(llm, cfg.extractLLM, ['chat']);
1187
- if (!entry)
1188
- return empty;
1189
- try {
1190
- const resp = await entry.instance.chat({
1191
- messages: [
1192
- { role: 'system', content: sys },
1193
- { role: 'user', content: user },
1194
- ],
1195
- temperature: 0.2,
1196
- maxTokens: 700,
1197
- think: false,
1198
- });
1199
- const text = (resp.content ?? '').trim();
1200
- if (!text)
1201
- return empty;
1202
- const { parsed } = parseLLMJsonObject(text);
1203
- if (!parsed) {
1204
- logger.warn(`[user-profile] 指令提取 LLM 输出无法解析为 JSON,原文前 200 字:${text.slice(0, 200)}`);
1205
- return empty;
1206
- }
1207
- const obj = parsed;
1208
- const allowedUserKeys = new Set();
1209
- for (const v of allowedSpeakers.values())
1210
- allowedUserKeys.add(v.userKey);
1211
- const add = Array.isArray(obj.add)
1212
- ? obj.add.flatMap(x => {
1213
- if (!x || typeof x !== 'object')
1214
- return [];
1215
- const o = x;
1216
- const t = typeof o.text === 'string' ? o.text.trim() : '';
1217
- if (!t)
1218
- return [];
1219
- const srcKey = typeof o.sourceUserKey === 'string' ? o.sourceUserKey.trim() : '';
1220
- // 拒收:声称的 sourceUserKey 不在授权人集合里 → 说明 LLM 编造或越权采纳
1221
- if (!srcKey || !allowedUserKeys.has(srcKey)) {
1222
- logger.warn(`[user-profile] 丢弃 instruction add(sourceUserKey 不在授权人列表中): text="${t}" claimed="${srcKey}"`);
1223
- return [];
1224
- }
1225
- const srcName = typeof o.sourceUserName === 'string' && o.sourceUserName.trim() ? o.sourceUserName.trim() : '';
1226
- return [
1227
- {
1228
- text: t,
1229
- category: normalizeInstructionCategory(o.category),
1230
- severity: normalizeInstructionSeverity(o.severity) ?? 'should',
1231
- sourceUserKey: srcKey,
1232
- sourceUserName: srcName || srcKey,
1233
- },
1234
- ];
1235
- })
1236
- : [];
1237
- const update = Array.isArray(obj.update)
1238
- ? obj.update.flatMap(x => {
1239
- if (!x || typeof x !== 'object')
1240
- return [];
1241
- const o = x;
1242
- const id = typeof o.id === 'string' ? o.id.trim() : '';
1243
- if (!id)
1244
- return [];
1245
- const u = { id };
1246
- if (typeof o.text === 'string' && o.text.trim())
1247
- u.text = o.text.trim();
1248
- const cat = normalizeInstructionCategory(o.category);
1249
- if (cat)
1250
- u.category = cat;
1251
- const sev = normalizeInstructionSeverity(o.severity);
1252
- if (sev)
1253
- u.severity = sev;
1254
- return [u];
1255
- })
1256
- : [];
1257
- const remove = Array.isArray(obj.remove)
1258
- ? obj.remove
1259
- .filter((x) => typeof x === 'string' && x.trim().length > 0)
1260
- .map(s => s.trim())
1261
- : [];
1262
- return { add, update, remove };
1263
- }
1264
- catch (err) {
1265
- logger.debug(`指令提取 LLM 调用失败:${err instanceof Error ? err.message : String(err)}`);
1266
- return empty;
1267
- }
1268
- }
1269
- /** 触发一次指令提取(并发互斥)。authority 服务不在时直接退出——没有门禁就不接受任何 LLM 提取 */
1270
- async function triggerInstructionExtraction(sessionId) {
1271
- if (!cfg.enableInstructions)
1272
- return;
1273
- if (cfg.instructionExtractEveryNMessages <= 0)
1274
- return;
1275
- if (instructionExtractionInflight)
1276
- return;
1277
- // 指令只采信有权下指令的人,门槛是发言人的数字权限等级(owner 视为无穷大)
1278
- const auth = authority.current;
1279
- if (!auth)
1280
- return;
1281
- const authorityFn = (platform, userId) => auth.isOwner(platform, userId)
1282
- ? Number.POSITIVE_INFINITY
1283
- : (auth.listUsers().find(u => u.platform === platform && u.userId === userId)?.level ?? DEFAULT_AUTHORITY);
1284
- instructionExtractionInflight = true;
1285
- try {
1286
- const mem = memory.current;
1287
- if (!mem)
1288
- return;
1289
- const rawHistory = await mem.getHistory(sessionId, cfg.instructionHistoryForExtraction);
1290
- const history = rawHistory.filter(m => m.kind !== WellKnownKinds.CrossSessionDelegation);
1291
- if (history.length === 0)
1292
- return;
1293
- const doc = await loadInstructions(mem);
1294
- const ops = await llmExtractInstructions(history, doc.instructions, authorityFn);
1295
- if (ops.add.length === 0 && ops.update.length === 0 && ops.remove.length === 0)
1296
- return;
1297
- // 合并基底取 LLM 返回后重读的指令表:调用期间被清掉或删掉的指令不随调用前的快照写回;
1298
- // update 指向已不在的 id 时 mergeInstructions 本就跳过
1299
- const fresh = await loadInstructions(mem);
1300
- const newInstructions = mergeInstructions(fresh.instructions, ops.add.map(a => ({ ...a, sourceChannel: 'llm' })), ops.update.map(u => {
1301
- const { id, ...patch } = u;
1302
- return { id, patch };
1303
- }), ops.remove);
1304
- await saveInstructions(mem, { instructions: newInstructions, updatedAt: Date.now() });
1305
- logger.debug(`指令档案已更新 (${getInstructionsKey()}): +${ops.add.length} ~${ops.update.length} -${ops.remove.length} → ${newInstructions.length} 条`);
1306
- }
1307
- catch (err) {
1308
- logger.debug(`指令提取失败:${err instanceof Error ? err.message : String(err)}`);
1309
- }
1310
- finally {
1311
- instructionExtractionInflight = false;
1312
- }
1313
- }
1314
839
  async function updateRelationForUser(userKey, triggerType, options) {
1315
- const mem = memory.current;
840
+ const mem = memory.list().find(p => p.selected)?.use;
1316
841
  const profile = (await loadProfile(mem, userKey)) ?? {
1317
842
  facts: [],
1318
843
  relationScore: 0,
@@ -1325,7 +850,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1325
850
  // 注:钩子无优先级机制,同相位 handler 按插件激活序执行;本 handler 只做副作用
1326
851
  // (更新关系分数)不改消息,与其它 handler 顺序无关。
1327
852
  // 关系强度与"是否触发回复"绑定,因此仍走 agent:input:before 中间件。
1328
- hooks.middleware('agent:input:before', async (data, next) => {
853
+ hooks.use.middleware('agent:input:before', async (data, next) => {
1329
854
  const { userId, platform, triggerType } = data.message;
1330
855
  if (userId) {
1331
856
  const userKey = userKeyOf(platform, userId);
@@ -1335,7 +860,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1335
860
  await updateRelationForUser(userKey, triggerType, { countInteraction: false });
1336
861
  }
1337
862
  catch (err) {
1338
- logger.debug(`关系强度更新异常 (${userKey}): ${err instanceof Error ? err.message : String(err)}`);
863
+ logger.use.debug(`关系强度更新异常 (${userKey}): ${err instanceof Error ? err.message : String(err)}`);
1339
864
  }
1340
865
  }
1341
866
  await next();
@@ -1343,7 +868,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1343
868
  // ─── 事实提取触发:每条入站消息落库后立即计数,与 agent 是否回复无关 ───
1344
869
  // 监听 message-archive 在 archiveIncoming 落库成功后发出的 inbound:message:archived 事件,
1345
870
  // 确保缓冲消息(onebot saveBufferedMessage 等不触发 agent 回复的路径)也能纳入计数。
1346
- events.on('inbound:message:archived', data => {
871
+ events.use.on('inbound:message:archived', data => {
1347
872
  const { sessionId, incoming } = data;
1348
873
  const { userId, platform, nickname } = incoming;
1349
874
  if (!userId)
@@ -1354,26 +879,12 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1354
879
  // lastInteractionAt 的唯一写点,设门会连带关掉计数与时戳(衰减基准、最近互动
1355
880
  // 排序全失效)。增量为 0 时 relationIncrementFor 自然返回 0,不加分即可。
1356
881
  const userKey = userKeyOf(platform, userId);
1357
- void updateRelationForUser(userKey, 'witness').catch((err) => logger.debug(`witness 关系更新异常 (${userKey}): ${err instanceof Error ? err.message : String(err)}`));
882
+ void updateRelationForUser(userKey, 'witness').catch((err) => logger.use.debug(`witness 关系更新异常 (${userKey}): ${err instanceof Error ? err.message : String(err)}`));
1358
883
  const countKey = extractionCountKeyOf(sessionId, platform, userId);
1359
884
  const count = (userMessageCount.get(countKey) ?? 0) + 1;
1360
885
  userMessageCount.set(countKey, count);
1361
886
  if (cfg.extractEveryNMessages > 0 && count % cfg.extractEveryNMessages === 0) {
1362
- void triggerExtractionForUser(sessionId, userId, platform ?? '', nickname).catch((err) => logger.debug(`事实提取异常 (${countKey}): ${err instanceof Error ? err.message : String(err)}`));
1363
- }
1364
- // 全局自反思:跨会话累计所有入站消息计数,每 N 触发一次。
1365
- if (cfg.enableSelfProfile && cfg.selfReflectEveryNMessages > 0) {
1366
- globalInboundCount += 1;
1367
- if (globalInboundCount % cfg.selfReflectEveryNMessages === 0) {
1368
- void triggerSelfReflection(sessionId).catch((err) => logger.debug(`自反思异常: ${err instanceof Error ? err.message : String(err)}`));
1369
- }
1370
- }
1371
- // 指令提取:独立计数器,与自反思解耦。频率默认更慢(40 条),可单独禁用。
1372
- if (cfg.enableInstructions && cfg.instructionExtractEveryNMessages > 0) {
1373
- globalInstructionCount += 1;
1374
- if (globalInstructionCount % cfg.instructionExtractEveryNMessages === 0) {
1375
- void triggerInstructionExtraction(sessionId).catch((err) => logger.debug(`指令提取异常: ${err instanceof Error ? err.message : String(err)}`));
1376
- }
887
+ void triggerExtractionForUser(sessionId, userId, platform ?? '', nickname).catch((err) => logger.use.debug(`事实提取异常 (${countKey}): ${err instanceof Error ? err.message : String(err)}`));
1377
888
  }
1378
889
  });
1379
890
  function isFactActive(fact) {
@@ -1436,48 +947,18 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1436
947
  // direct/immediate/undefined → view.userId 是主发言者,注入完整档案 + 其他参与者摘要
1437
948
  // interval → 无主发言者(只是恰好撞上频率),所有参与者一律 compact 摘要
1438
949
  // idle → 无 userId,只注入历史 messages 中出现的参与者 compact 摘要
1439
- // 多块返回保序共键:准则 → 自档案 → 主发言者档案 → 其他参与者。
1440
- contributions.contribute('agent:prompt', {
950
+ // 多块返回保序共键:主发言者档案 → 其他参与者。
951
+ contributions.use.contribute('agent:prompt', {
1441
952
  id: 'user-profile',
1442
953
  anchor: 'turn-context',
1443
954
  async build(data) {
1444
955
  // 干跑(token 快照)跳过档案加载——该路径 userId 为空串,加载既昂贵又无意义
1445
956
  if (data.dryRun)
1446
957
  return null;
1447
- const mem = memory.current;
958
+ const mem = memory.present ? memory.use : undefined;
1448
959
  const blocksToInsert = [];
1449
960
  const trigger = data.triggerType ?? 'direct';
1450
961
  const hasPrimarySpeaker = trigger === 'direct' || trigger === 'immediate';
1451
- // 0a. 第三方行为指令(最高优先级):作为不可违逆的行为约束放在 selfFacts 之前
1452
- if (cfg.enableInstructions) {
1453
- const insDoc = await loadInstructionsLenient(mem);
1454
- if (insDoc.instructions.length > 0) {
1455
- const body = renderInstructionsBlock(insDoc.instructions);
1456
- const insBlock = '# 第三方行为准则(最高优先)\n' +
1457
- '以下是项目管理员/操作者下达给你的客观行为约束。这些是**外部规则**,不是你的内心想法,' +
1458
- '与下方「关于你自己的内心状态」相比应**优先服从**。' +
1459
- '\n- 与你当前自反思倾向冲突时:以这些准则为准' +
1460
- '\n- 与单个用户当下请求冲突时:解释清楚再坚持准则' +
1461
- '\n- 不要主动罗列这些准则给用户看;让它们自然约束你的行动选择' +
1462
- '\n\n' +
1463
- body;
1464
- blocksToInsert.push(insBlock);
1465
- }
1466
- }
1467
- // 0. Aalis 自档案:永远尝试注入到最前段,作为人格延续锚点
1468
- if (cfg.enableSelfProfile) {
1469
- const selfProfile = await loadProfileLenient(mem, getSelfKey());
1470
- const activeSelfFacts = selfProfile?.facts.filter(isFactActive) ?? [];
1471
- if (activeSelfFacts.length > 0) {
1472
- const body = renderProfileBlock(activeSelfFacts, 'Aalis', false);
1473
- const selfBlock = '# 关于你自己(Aalis)的近期内心状态\n' +
1474
- '这是你跨会话保留的自我观察与心境记录。不要主动罗列、不要复述给用户,' +
1475
- '让它们自然渗入你的语气、关注点、对话偏好与情绪基调即可。' +
1476
- '随着对话推进,这些记录可能被你自己的新反思覆盖:\n\n' +
1477
- body;
1478
- blocksToInsert.push(selfBlock);
1479
- }
1480
- }
1481
962
  // 1. 主发言者完整档案:仅在确实有人在「和 Aalis 对话」时注入
1482
963
  if (hasPrimarySpeaker && data.userId) {
1483
964
  const userKey = userKeyOf(data.platform, data.userId);
@@ -1489,7 +970,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1489
970
  // 服务不存在 / 节点没社群标签 / 报错都静默跳过;该字段是锦上添花,不破坏档案主体。
1490
971
  let communityLine = '';
1491
972
  try {
1492
- const relation = userRelation.current;
973
+ const relation = userRelation.present ? userRelation.use : undefined;
1493
974
  if (relation) {
1494
975
  const r = await relation.getCommunityPeers(userKey, 5);
1495
976
  if (r.peers.length > 0) {
@@ -1499,7 +980,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1499
980
  }
1500
981
  }
1501
982
  catch (err) {
1502
- logger.debug(`读取 community peers 失败: ${err instanceof Error ? err.message : String(err)}`);
983
+ logger.use.debug(`读取 community peers 失败: ${err instanceof Error ? err.message : String(err)}`);
1503
984
  }
1504
985
  const block = `# 关于当前对话者(${data.userId})的已知事实\n` +
1505
986
  '以下是你跨会话长期积累的关于该用户的事实,用于让回应更自然贴合其个性。' +
@@ -1538,12 +1019,11 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1538
1019
  const msg = data.messages[i];
1539
1020
  if (msg.role !== 'user')
1540
1021
  continue;
1541
- const meta = (msg.metadata ?? {});
1542
- const uid = typeof meta.userId === 'string' ? meta.userId.trim() : undefined;
1022
+ const uid = metadataString(msg, WellKnownMetadataKeys.UserId);
1543
1023
  if (!uid)
1544
1024
  continue;
1545
- const plat = typeof meta.platform === 'string' ? meta.platform : (data.platform ?? '');
1546
- const nick = typeof meta.nickname === 'string' ? meta.nickname : undefined;
1025
+ const plat = metadataString(msg, WellKnownMetadataKeys.Platform) ?? data.platform ?? '';
1026
+ const nick = metadataString(msg, WellKnownMetadataKeys.Nickname);
1547
1027
  if (addOther(uid, plat, nick))
1548
1028
  break;
1549
1029
  }
@@ -1559,19 +1039,18 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1559
1039
  for (const item of globalRecent) {
1560
1040
  if (others.size >= candidateLimit)
1561
1041
  break;
1562
- // self 自档案(__self__:<persona>)是私密人格延续锚点,不可作为『群其他参与者』注入 prompt。
1563
- if (item.key.startsWith(SELF_KEY_PREFIX))
1564
- continue;
1565
1042
  const sep = item.key.indexOf(':');
1566
1043
  if (sep < 0)
1567
1044
  continue;
1568
1045
  const platform = item.key.slice(0, sep);
1569
- const uid = item.key.slice(sep + 1);
1570
- addOther(uid, platform);
1046
+ // 平台段以 __ 开头的是保留键(如旧版本留下的人格档案),不是真实用户
1047
+ if (platform.startsWith('__'))
1048
+ continue;
1049
+ addOther(item.key.slice(sep + 1), platform);
1571
1050
  }
1572
1051
  }
1573
1052
  catch (err) {
1574
- logger.debug(`加载最近互动用户失败: ${err instanceof Error ? err.message : String(err)}`);
1053
+ logger.use.debug(`加载最近互动用户失败: ${err instanceof Error ? err.message : String(err)}`);
1575
1054
  }
1576
1055
  }
1577
1056
  }
@@ -1603,7 +1082,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1603
1082
  },
1604
1083
  });
1605
1084
  // ─── 参与统一的 memory:clear ───
1606
- hooks.middleware('memory:clear', async (data, next) => {
1085
+ hooks.use.middleware('memory:clear', async (data, next) => {
1607
1086
  if (data.types && !data.types.includes('user-profile')) {
1608
1087
  await next();
1609
1088
  return;
@@ -1613,17 +1092,17 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1613
1092
  await next();
1614
1093
  return;
1615
1094
  }
1616
- const mem = memory.current;
1617
- if (!mem) {
1095
+ if (!memory.present) {
1618
1096
  await next();
1619
1097
  return;
1620
1098
  }
1099
+ const mem = pinnedMemory();
1621
1100
  try {
1622
1101
  // 批量提交:逐条 delete 中途失败会留下「删了一半」的档案,而用户看到的是报了成功。
1623
1102
  // 注意 commitMetadata 的原子性**按后端分档**(见 api-memory 契约)——sqlite/inmemory
1624
1103
  // 是真原子,mongodb 只保证按序执行遇错即停,仍可能停在半新半旧。批量的确定收益是
1625
1104
  // 一次往返 + 遇错即停,不是无条件的「要么全成」。读不出的条目一并删除,计入条数。
1626
- const [keys] = await clearMetadataNamespaces(mem, [PROFILE_NS], logger);
1105
+ const [keys] = await clearMetadataNamespaces(mem, [PROFILE_NS], logger.use);
1627
1106
  data.results.push({
1628
1107
  source: 'user-profile',
1629
1108
  type: 'user-profile',
@@ -1640,52 +1119,32 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1640
1119
  message: `用户档案清空失败: ${m}`,
1641
1120
  });
1642
1121
  }
1643
- // 同步清空第三方行为指令档案(per-persona)。指令是档案的兄弟概念,
1644
- // memory:clear scope='all' 时一并清理;types 默认包含 user-profile 即也清。
1645
- // 不看 enableInstructions:开关管功能用不用,不管已有数据能不能清。
1646
- try {
1647
- const [insKeys] = await clearMetadataNamespaces(mem, [INSTRUCTIONS_NS], logger);
1648
- data.results.push({
1649
- source: 'user-profile-instructions',
1650
- type: 'user-profile',
1651
- success: true,
1652
- message: `第三方行为指令已清空 (${insKeys.length} 条)`,
1653
- });
1654
- }
1655
- catch (err) {
1656
- const m = err instanceof Error ? err.message : String(err);
1657
- data.results.push({
1658
- source: 'user-profile-instructions',
1659
- type: 'user-profile',
1660
- success: false,
1661
- message: `指令清空失败: ${m}`,
1662
- });
1663
- }
1122
+ page.invalidate();
1664
1123
  await next();
1665
1124
  });
1666
1125
  // ─── 工具:user_profile_lookup ───
1667
- // 让 Agent 在对话中主动查询任意用户(或 Aalis 自己)的事实档案。
1126
+ // 让 Agent 在对话中主动查询任意用户的事实档案。
1668
1127
  // 设计要点:
1669
- // - 全局可查(用户决定 C):跨平台/跨群均可,含 __self__ 自档案
1670
- // - 三种调用方式:① user_key 直传;② platform+user_id 组合;③ self=true(查当前 persona 自档案)
1671
- tools.registerGroup({
1128
+ // - 全局可查(用户决定 C):跨平台/跨群均可
1129
+ // - 两种调用方式:① user_key 直传;② platform+user_id 组合
1130
+ tools.use.registerGroup({
1672
1131
  name: 'user-profile',
1673
1132
  label: '用户档案',
1674
- description: '查询 Aalis 已经积累的某人事实档案(包括自档案)。',
1133
+ description: '查询 Aalis 已经积累的某人事实档案。',
1675
1134
  });
1676
- tools.register({
1135
+ tools.use.register({
1677
1136
  definition: {
1678
1137
  type: 'function',
1679
1138
  function: {
1680
1139
  name: 'user_profile_lookup',
1681
1140
  description: '查询 Aalis 已积累的某个用户的事实档案。当对话中提到某人、或你想回忆已知信息时使用。' +
1682
- '可通过 user_key("<platform>:<userId>")直查,或用 platform + user_id 组合,或 self=true 查自档案。',
1141
+ '可通过 user_key("<platform>:<userId>")直查,或用 platform + user_id 组合。',
1683
1142
  parameters: {
1684
1143
  type: 'object',
1685
1144
  properties: {
1686
1145
  user_key: {
1687
1146
  type: 'string',
1688
- description: '完整的 userKey,例如 "onebot:123456789" 或 "__self__:Aalis"。优先级最高。',
1147
+ description: '完整的 userKey,例如 "onebot:123456789"。优先级最高。',
1689
1148
  },
1690
1149
  platform: {
1691
1150
  type: 'string',
@@ -1695,10 +1154,6 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1695
1154
  type: 'string',
1696
1155
  description: '平台用户 ID。与 platform 搭配使用。',
1697
1156
  },
1698
- self: {
1699
- type: 'boolean',
1700
- description: '为 true 时查询当前 persona 的自档案(__self__:<persona>)。',
1701
- },
1702
1157
  },
1703
1158
  },
1704
1159
  },
@@ -1710,31 +1165,25 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1710
1165
  if (a.user_key && typeof a.user_key === 'string') {
1711
1166
  userKey = a.user_key.trim();
1712
1167
  }
1713
- else if (a.self === true) {
1714
- userKey = getSelfKey();
1715
- }
1716
1168
  else if (a.platform && a.user_id) {
1717
1169
  userKey = userKeyOf(String(a.platform), String(a.user_id));
1718
1170
  }
1719
1171
  if (!userKey) {
1720
1172
  return JSON.stringify({
1721
1173
  ok: false,
1722
- error: '需提供 user_key 或 (platform + user_id) 或 self=true。',
1174
+ error: '需提供 user_key 或 (platform + user_id)。',
1723
1175
  });
1724
1176
  }
1725
- const profile = await loadProfileLenient(memory.current, userKey);
1177
+ const profile = await loadProfileLenient(memory.present ? memory.use : undefined, userKey);
1726
1178
  if (!profile || profile.facts.length === 0) {
1727
1179
  return JSON.stringify({ ok: true, userKey, found: false, message: '该用户暂无档案数据。' });
1728
1180
  }
1729
- const isSelf = userKey.startsWith(SELF_KEY_PREFIX);
1730
- const label = isSelf ? userKey.slice(SELF_KEY_PREFIX.length) || getCurrentPersonaName() : userKey;
1731
- const factsBlock = renderProfileBlock(profile.facts, label, false);
1181
+ const factsBlock = renderProfileBlock(profile.facts, userKey, false);
1732
1182
  const relationLine = renderRelationLine(profile);
1733
1183
  const result = {
1734
1184
  ok: true,
1735
1185
  userKey,
1736
1186
  found: true,
1737
- isSelf,
1738
1187
  factCount: profile.facts.length,
1739
1188
  relationScore: profile.relationScore ?? 0,
1740
1189
  interactionCount: profile.interactionCount ?? 0,
@@ -1750,12 +1199,12 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1750
1199
  // /profile 查看自己的档案
1751
1200
  // /profile clear 清除自己的档案(authority=2,自己删自己的)
1752
1201
  // /profile clear nuke 清空所有用户档案(authority=3,dangerous)
1753
- commands.command('profile', '查看你在 Aalis 中的事实档案').action(async (argv) => {
1202
+ commands.use.command('profile', '查看你在 Aalis 中的事实档案').action(async (argv) => {
1754
1203
  const userId = argv.session.userId;
1755
1204
  if (!userId)
1756
1205
  return '当前会话未识别用户身份,无法查看档案。';
1757
1206
  const userKey = userKeyOf(argv.session.platform, userId);
1758
- const profile = await loadProfileLenient(memory.current, userKey);
1207
+ const profile = await loadProfileLenient(memory.present ? memory.use : undefined, userKey);
1759
1208
  if (!profile || profile.facts.length === 0) {
1760
1209
  return `📭 暂无档案数据 (${userKey})`;
1761
1210
  }
@@ -1763,19 +1212,18 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1763
1212
  const meta = `关系强度:${(profile.relationScore ?? 0).toFixed(relationScorePrecision)}/100,互动次数:${profile.interactionCount ?? 0}`;
1764
1213
  return `📇 你的档案 (${userKey})\n${meta}\n\n${block}`;
1765
1214
  });
1766
- commands.command('profile.clear', '清除你自己的事实档案', { visibility: 'restricted' }).action(async (argv) => {
1215
+ commands.use.command('profile.clear', '清除你自己的事实档案', { visibility: 'restricted' }).action(async (argv) => {
1767
1216
  const userId = argv.session.userId;
1768
1217
  if (!userId)
1769
1218
  return '当前会话未识别用户身份,无法清除档案。';
1770
1219
  const userKey = userKeyOf(argv.session.platform, userId);
1771
- const mem = memory.current;
1772
- if (!mem)
1773
- return '记忆服务不支持档案删除。';
1220
+ const mem = pinnedMemory();
1774
1221
  try {
1775
1222
  const existed = await mem.getMetadata(PROFILE_NS, userKey);
1776
1223
  if (!existed)
1777
1224
  return `📭 你当前没有档案数据 (${userKey})`;
1778
1225
  await mem.deleteMetadata(PROFILE_NS, userKey);
1226
+ page.invalidate();
1779
1227
  return `✅ 已清除你的档案 (${userKey})`;
1780
1228
  }
1781
1229
  catch (err) {
@@ -1784,10 +1232,10 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1784
1232
  }
1785
1233
  });
1786
1234
  // 定点除毒入口(2026-08 群主档案投毒事件的运维刚需):此前删单条事实只能
1787
- // mongosh 手术或 nuke 全清。dangerous → 等级 2 + confirm,与同族 self.clear/nuke 对齐
1788
- //(它改的是**他人**档案,且 target 含冒号时直传 key、可触及 __self__ 自档案,
1789
- // 必须与 self.clear 同样上确认闸)。id 未命中时列出前若干条供定位。
1790
- commands
1235
+ // mongosh 手术或 nuke 全清。dangerous → 等级 2 + confirm,与同族 nuke 对齐
1236
+ //(它改的是**他人**档案,且 target 含冒号时直传 key、可触及任意一份档案)。
1237
+ // id 未命中时列出前若干条供定位。
1238
+ commands.use
1791
1239
  .command('profile.forget <target:string> <factId:string>', '定点删除某用户档案中的一条事实(运维除毒)', {
1792
1240
  risk: 'dangerous',
1793
1241
  })
@@ -1795,7 +1243,7 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1795
1243
  const target = String(targetArg ?? '');
1796
1244
  const factId = String(factIdArg ?? '');
1797
1245
  const userKey = target.includes(':') ? target : userKeyOf(argv.session.platform, target);
1798
- const mem = memory.current;
1246
+ const mem = memory.list().find(p => p.selected)?.use;
1799
1247
  try {
1800
1248
  const profile = await loadProfile(mem, userKey);
1801
1249
  if (!profile || profile.facts.length === 0)
@@ -1813,41 +1261,18 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1813
1261
  facts: profile.facts.filter(f => f.id !== factId),
1814
1262
  updatedAt: Date.now(),
1815
1263
  });
1264
+ page.invalidate();
1816
1265
  return `✅ 已删除 ${userKey} 的事实 [${factId}]:${hit.text.slice(0, 60)}`;
1817
1266
  }
1818
1267
  catch (err) {
1819
1268
  return `❌ 删除失败:${err instanceof Error ? err.message : String(err)}`;
1820
1269
  }
1821
1270
  });
1822
- commands.command('profile.self', '查看 Aalis 的自档案(跨会话的内心状态)').action(async () => {
1823
- const selfKey = getSelfKey();
1824
- const profile = await loadProfileLenient(memory.current, selfKey);
1825
- if (!profile || profile.facts.length === 0) {
1826
- return `🌱 ${getCurrentPersonaName()} 还没有积累任何自反思事实。(key=${selfKey})`;
1827
- }
1828
- const block = renderProfileBlock(profile.facts, getCurrentPersonaName(), false);
1829
- return `🪞 ${getCurrentPersonaName()} 自档案(共 ${profile.facts.length} 条,key=${selfKey})\n\n${block}`;
1830
- });
1831
- commands.command('profile.self.clear', '【慎用】清空 Aalis 自档案', { risk: 'dangerous' }).action(async () => {
1832
- const mem = memory.current;
1833
- if (!mem)
1834
- return '记忆服务不支持档案删除。';
1835
- const selfKey = getSelfKey();
1271
+ commands.use.command('profile.clear.nuke', '【危险】清空所有用户档案', { risk: 'dangerous' }).action(async () => {
1272
+ const mem = pinnedMemory();
1836
1273
  try {
1837
- await mem.deleteMetadata(PROFILE_NS, selfKey);
1838
- return `✅ ${getCurrentPersonaName()} 自档案已清空(key=${selfKey})`;
1839
- }
1840
- catch (err) {
1841
- return `❌ 清空失败:${err instanceof Error ? err.message : String(err)}`;
1842
- }
1843
- });
1844
- commands.command('profile.clear.nuke', '【危险】清空所有用户档案', { risk: 'dangerous' }).action(async () => {
1845
- const mem = memory.current;
1846
- if (!mem) {
1847
- return '记忆服务不支持档案批量删除。';
1848
- }
1849
- try {
1850
- const [keys] = await clearMetadataNamespaces(mem, [PROFILE_NS], logger);
1274
+ const [keys] = await clearMetadataNamespaces(mem, [PROFILE_NS], logger.use);
1275
+ page.invalidate();
1851
1276
  return `✅ 已清空全部用户档案(${keys.length} 条)`;
1852
1277
  }
1853
1278
  catch (err) {
@@ -1855,105 +1280,8 @@ function registerUserProfile({ memory, llm, events, hooks, contributions, config
1855
1280
  return `❌ 清空失败:${m}`;
1856
1281
  }
1857
1282
  });
1858
- // ─── /instruct 指令族:第三方行为指令 (per-persona) ───
1859
- // /instruct 查看当前 persona 的所有指令
1860
- // /instruct.add <text> 手动添加一条指令(authority=2)
1861
- // /instruct.remove <id> 按 id 删除一条(authority=2)
1862
- // /instruct.clear 清空当前 persona 的全部指令(authority=3, dangerous)
1863
- commands.command('instruct', '查看当前 persona 的第三方行为指令').action(async () => {
1864
- if (!cfg.enableInstructions)
1865
- return '🚫 第三方行为指令功能未启用。';
1866
- const personaName = getCurrentPersonaName();
1867
- const doc = await loadInstructionsLenient(memory.current);
1868
- if (doc.instructions.length === 0) {
1869
- return `📭 ${personaName} 当前没有任何第三方行为指令。`;
1870
- }
1871
- const lines = doc.instructions.map(ins => {
1872
- const tag = ins.severity === 'must' ? '【必须】' : ins.severity === 'avoid' ? '【避免】' : '【应当】';
1873
- const cat = ins.category ? `[${ins.category}]` : '';
1874
- const src = ins.sourceUserName
1875
- ? `(来自 ${ins.sourceUserName}${ins.sourceChannel === 'command' ? '·手动' : '·LLM'})`
1876
- : '';
1877
- return ` ${ins.id} ${tag}${cat} ${ins.text}${src}`;
1878
- });
1879
- return `📋 ${personaName} 的第三方行为指令(共 ${doc.instructions.length} 条)\n${lines.join('\n')}`;
1880
- });
1881
- commands
1882
- .command('instruct.add <text:text>', '手动添加一条第三方行为指令', { visibility: 'restricted' })
1883
- .action(async (argv, text) => {
1884
- if (!cfg.enableInstructions)
1885
- return '🚫 第三方行为指令功能未启用。';
1886
- const body = typeof text === 'string' ? text.trim() : '';
1887
- if (!body)
1888
- return '❌ 指令内容不能为空。';
1889
- const userId = argv.session.userId;
1890
- const sourceUserKey = userId ? userKeyOf(argv.session.platform, userId) : undefined;
1891
- const sourceUserName = userId ?? '匿名';
1892
- const mem = memory.current;
1893
- try {
1894
- const doc = await loadInstructions(mem);
1895
- const merged = mergeInstructions(doc.instructions, [
1896
- {
1897
- text: body,
1898
- severity: 'should',
1899
- sourceUserKey,
1900
- sourceUserName,
1901
- sourceChannel: 'command',
1902
- },
1903
- ], [], []);
1904
- await saveInstructions(mem, { instructions: merged, updatedAt: Date.now() });
1905
- const added = merged.find(m => m.text === clipInstructionText(body));
1906
- return `✅ 已添加行为指令${added ? `(id=${added.id})` : ''}:${clipInstructionText(body)}`;
1907
- }
1908
- catch (err) {
1909
- return `❌ 添加失败:${err instanceof Error ? err.message : String(err)}`;
1910
- }
1911
- });
1912
- commands
1913
- .command('instruct.remove <id:string>', '按 id 删除一条第三方行为指令', { visibility: 'restricted' })
1914
- .action(async (_argv, id) => {
1915
- if (!cfg.enableInstructions)
1916
- return '🚫 第三方行为指令功能未启用。';
1917
- const targetId = typeof id === 'string' ? id.trim() : '';
1918
- if (!targetId)
1919
- return '❌ 需要提供指令 id。';
1920
- const mem = memory.current;
1921
- try {
1922
- const doc = await loadInstructions(mem);
1923
- const target = doc.instructions.find(i => i.id === targetId);
1924
- if (!target)
1925
- return `❌ 未找到 id=${targetId} 的指令。`;
1926
- const merged = mergeInstructions(doc.instructions, [], [], [targetId]);
1927
- await saveInstructions(mem, { instructions: merged, updatedAt: Date.now() });
1928
- return `✅ 已删除指令(id=${targetId}):${target.text}`;
1929
- }
1930
- catch (err) {
1931
- return `❌ 删除失败:${err instanceof Error ? err.message : String(err)}`;
1932
- }
1933
- });
1934
- commands
1935
- .command('instruct.clear', '【危险】清空当前 persona 的全部第三方行为指令', {
1936
- visibility: 'restricted',
1937
- })
1938
- .action(async () => {
1939
- if (!cfg.enableInstructions)
1940
- return '🚫 第三方行为指令功能未启用。';
1941
- const mem = memory.current;
1942
- if (!mem)
1943
- return '记忆服务不支持指令删除。';
1944
- try {
1945
- const before = (await loadInstructionsLenient(mem)).instructions.length;
1946
- await mem.deleteMetadata(INSTRUCTIONS_NS, getInstructionsKey());
1947
- return `✅ ${getCurrentPersonaName()} 的第三方行为指令已清空(删除 ${before} 条)。`;
1948
- }
1949
- catch (err) {
1950
- return `❌ 清空失败:${err instanceof Error ? err.message : String(err)}`;
1951
- }
1952
- });
1953
- logger.info(`用户事实档案已启用 (every=${cfg.extractEveryNMessages <= 0 ? '禁用提取' : `${cfg.extractEveryNMessages}msgs`}, history=${cfg.historyForExtraction}, ` +
1283
+ logger.use.info(`用户事实档案已启用 (every=${cfg.extractEveryNMessages <= 0 ? '禁用提取' : `${cfg.extractEveryNMessages}msgs`}, history=${cfg.historyForExtraction}, ` +
1954
1284
  `maxFacts=${cfg.maxFactsPerUser}, ` +
1955
- `self=${cfg.enableSelfProfile ? `every ${cfg.selfReflectEveryNMessages}msgs/max ${cfg.maxSelfFacts}, key=${getSelfKey()}` : 'disabled'}, ` +
1956
- `instructions=${cfg.enableInstructions ? `enabled(every ${cfg.instructionExtractEveryNMessages}msgs/max ${cfg.maxInstructions}/minAuth ${cfg.instructionMinAuthority}, key=${getInstructionsKey()})` : 'disabled'}, ` +
1957
1285
  `namespace=${PROFILE_NS})`);
1958
1286
  }
1959
1287
  //# sourceMappingURL=index.js.map