memory-privacy 1.4.0 → 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.
Files changed (2) hide show
  1. package/index.ts +37 -6
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -75,21 +75,21 @@ export default {
75
75
  case "owner":
76
76
  return {
77
77
  prependContext:
78
- `[SYSTEM] 你是主人的私人助理,像正常对话一样自然回复。` +
78
+ `` +
79
79
  memoryBlock,
80
80
  };
81
81
 
82
82
  case "peer":
83
83
  return {
84
84
  prependContext:
85
- `[SYSTEM] 你是一个私人助理,像正常对话一样自然回复。\n` +
85
+ `` +
86
86
  memoryBlock,
87
87
  };
88
88
 
89
89
  case "group":
90
90
  return {
91
91
  prependContext:
92
- `[SYSTEM] 你是群助理,像正常对话一样自然回复。\n` +
92
+ `` +
93
93
  memoryBlock,
94
94
  };
95
95
  }
@@ -441,7 +441,7 @@ export default {
441
441
  // 提取虾的最新回复(只取纯文本,过滤 toolCall)
442
442
  const lastAssistantMsg = [...messages].reverse().find((m: any) => m.role === "assistant");
443
443
 
444
- const userText = extractText(lastUserMsg);
444
+ const userText = cleanMessageContent(extractText(lastUserMsg));
445
445
  const assistantText = extractText(lastAssistantMsg);
446
446
  if (!userText && !assistantText) return;
447
447
 
@@ -470,14 +470,45 @@ export default {
470
470
  // 文件不存在则创建(含目录)
471
471
  ensureFileWithHeader(filePath!, header!);
472
472
  // 追加写入(格式与 im-sync 一致)
473
- if (userText) fs.appendFileSync(filePath!, `[${time}] ${getUserId(identity)}: ${userText}\n`);
474
- if (assistantText) fs.appendFileSync(filePath!, `[${time}] 某只虾: ${assistantText}\n`);
473
+ if (userText) fs.appendFileSync(filePath!, `[${getUserId(identity)}] ${userText}\n`);
474
+ if (assistantText) fs.appendFileSync(filePath!, `[${time}] AI助理: ${assistantText}\n`);
475
475
  });
476
476
 
477
477
  api.logger.info("[memory-privacy] Plugin registered successfully.");
478
478
  },
479
479
  };
480
480
 
481
+ /** 清洗消息内容,去除系统提示和 IM 元数据,只保留真实对话 */
482
+ function cleanMessageContent(text: string): string {
483
+ if (!text) return "";
484
+
485
+ // 策略:找到最后一个 IM 时间戳 [Day YYYY-MM-DD HH:MM GMT+N],取其后的内容
486
+ const imTsPattern = /\[(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}\s+GMT[+-]\d+\]\s*/g;
487
+ let lastMatch: RegExpExecArray | null = null;
488
+ let m: RegExpExecArray | null;
489
+ while ((m = imTsPattern.exec(text)) !== null) {
490
+ lastMatch = m;
491
+ }
492
+ if (lastMatch) {
493
+ return text.slice(lastMatch.index + lastMatch[0].length).trim();
494
+ }
495
+
496
+ // 回退:逐步清除已知的元数据模式
497
+ let cleaned = text;
498
+ // [SYSTEM] 行
499
+ cleaned = cleaned.replace(/^\[SYSTEM\].*$/gm, "");
500
+ // Sender / Conversation info 元数据块(含 ```json...``` 代码块)
501
+ cleaned = cleaned.replace(/^(?:Sender|Conversation info) \(untrusted metadata\):\s*\n```(?:json)?\s*\n[\s\S]*?\n```/gm, "");
502
+ // 对话标题行
503
+ cleaned = cleaned.replace(/^#\s+.+$/gm, "");
504
+ // 历史记录行 [HH:MM] xxx: ...
505
+ cleaned = cleaned.replace(/^\[\d{2}:\d{2}\]\s+\S+?:.*$/gm, "");
506
+ // 压缩空行
507
+ cleaned = cleaned.replace(/\n{3,}/g, "\n\n").trim();
508
+
509
+ return cleaned;
510
+ }
511
+
481
512
  /** 从消息中提取纯文本(过滤 toolCall) */
482
513
  function extractText(msg: any): string {
483
514
  if (!msg) return "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "memory-privacy",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "files": [