whale-memory-core 0.20.0 → 0.21.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/bin/whale-memory-mcp.mjs +2 -1
- package/index.js +2 -0
- package/lib/changed.d.ts +4 -0
- package/lib/changed.js +73 -0
- package/lib/conversation-privacy.js +5 -1
- package/lib/injection.d.ts +5 -0
- package/lib/injection.js +76 -0
- package/package.json +9 -1
package/bin/whale-memory-mcp.mjs
CHANGED
|
@@ -212,7 +212,8 @@ const INSTRUCTIONS = [
|
|
|
212
212
|
'This is the autobiographical memory of an AI character, kept by herself. Three rules:(这是一个 AI 角色自己的自传体记忆。三条规矩:)',
|
|
213
213
|
'1. What was written then is never changed; if a view changes, append with memory_revise.(1. 当时写下的永远不改;想法变了用 memory_revise 追加。)',
|
|
214
214
|
'2. An empty result means this search found nothing; do not make anything up. For a specific detail use memory_detail; distinguish related events from support for an answer.(2. 查空只说明这次没找到,不要编。核对细节时用 memory_detail,按原文能支持的范围回答。)',
|
|
215
|
-
'3. Do not bring up what she has sealed; only she opens the unsaid drawer.(3. 她封存了的事不要主动提起;没说出口的抽屉只有她自己打开。)'
|
|
215
|
+
'3. Do not bring up what she has sealed; only she opens the unsaid drawer.(3. 她封存了的事不要主动提起;没说出口的抽屉只有她自己打开。)',
|
|
216
|
+
'4. If the user states something as fact, an empty recall is not a prompt to agree: say there is no record and let her correct it, rather than completing their premise or inventing a detail.(4. 对方把某件事当成事实说出来时,召回为空不是你同意的理由:说没有记录,让对方向上纠正,而不是顺着他的前提补细节。)'
|
|
216
217
|
].join('\n')
|
|
217
218
|
|
|
218
219
|
export function createServer(cfg) {
|
package/index.js
CHANGED
|
@@ -24,3 +24,5 @@ export * as conversationPrivacy from './lib/conversation-privacy.js'
|
|
|
24
24
|
export * as factCard from './lib/fact-card.js'
|
|
25
25
|
export * as factStore from './lib/fact-store.js'
|
|
26
26
|
export * as factJudge from './lib/fact-judge.js'
|
|
27
|
+
export * as changed from './lib/changed.js'
|
|
28
|
+
export * as injection from './lib/injection.js'
|
package/lib/changed.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** 找出「后来改过想法」的经历;没改过或超出天数的不会出现 */
|
|
2
|
+
export function changedSince(dir: string, options?: { days?: number; limit?: number; now?: number; includeSealed?: boolean }): Array<{ id: string; day: string; then: string; now: string; at: number; daysAgo: number }>
|
|
3
|
+
/** 渲染成给模型看的一段话;没有就返回空串 */
|
|
4
|
+
export function renderChanged(items: Array<Record<string, unknown>>, options?: { lang?: 'zh' | 'en' }): string
|
package/lib/changed.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// 「我改过主意」:列出当时和后来想法不一样的那些经历。
|
|
2
|
+
//
|
|
3
|
+
// 这是 then/later 这套数据模型唯一别的记忆系统给不出的东西:不是"相似段落",
|
|
4
|
+
// 而是"我哪天怎么想、后来怎么想"的差。宿主可以把它递给她,让她发现自己变过。
|
|
5
|
+
//
|
|
6
|
+
// 纯读取,不改任何文件。
|
|
7
|
+
import { loadAll } from './self-experience.js'
|
|
8
|
+
|
|
9
|
+
const DAY = 24 * 3600 * 1000
|
|
10
|
+
|
|
11
|
+
function tsOf(value) {
|
|
12
|
+
if (value == null) return 0
|
|
13
|
+
if (value instanceof Date) return value.getTime()
|
|
14
|
+
if (typeof value === 'number') return value >= 1e11 ? value : value * 1000
|
|
15
|
+
const t = Date.parse(String(value))
|
|
16
|
+
return Number.isFinite(t) ? t : 0
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function norm(text) {
|
|
20
|
+
return String(text || '').replace(/\s+/g, '').trim()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 找出「后来改过想法」的经历。
|
|
25
|
+
*
|
|
26
|
+
* @param {string} dir 记忆目录
|
|
27
|
+
* @param {object} [options]
|
|
28
|
+
* @param {number} [options.days=30] 只看这段时间内改的
|
|
29
|
+
* @param {number} [options.limit=5] 最多几条
|
|
30
|
+
* @param {number} [options.now=Date.now()] 当前时间(测试用)
|
|
31
|
+
* @param {boolean} [options.includeSealed=false] 封存的是否算
|
|
32
|
+
* @returns {Array<{id:string, day:string, then:string, now:string, at:number, daysAgo:number}>}
|
|
33
|
+
*/
|
|
34
|
+
export function changedSince(dir, { days = 30, limit = 5, now = Date.now(), includeSealed = false } = {}) {
|
|
35
|
+
const horizon = now - Number(days) * DAY
|
|
36
|
+
const out = []
|
|
37
|
+
for (const entry of loadAll(dir)) {
|
|
38
|
+
if (!entry || entry.sealed === true && !includeSealed) continue
|
|
39
|
+
const later = Array.isArray(entry.later) ? entry.later : []
|
|
40
|
+
if (!later.length) continue
|
|
41
|
+
const latest = later[later.length - 1]
|
|
42
|
+
const nowText = String(latest?.thought || '').trim()
|
|
43
|
+
const thenText = String(entry.then?.thought || '').trim()
|
|
44
|
+
if (!nowText) continue
|
|
45
|
+
// 和当时一样就只是"又写了一遍",不算改主意
|
|
46
|
+
if (norm(nowText) === norm(thenText)) continue
|
|
47
|
+
const at = tsOf(latest.at) || tsOf(entry.then?.at) || tsOf(entry.day)
|
|
48
|
+
if (!at || at < horizon) continue
|
|
49
|
+
out.push({
|
|
50
|
+
id: String(entry.id || ''),
|
|
51
|
+
day: String(entry.day || ''),
|
|
52
|
+
then: thenText,
|
|
53
|
+
now: nowText,
|
|
54
|
+
at,
|
|
55
|
+
daysAgo: Math.floor((now - at) / DAY)
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
out.sort((a, b) => b.at - a.at)
|
|
59
|
+
return out.slice(0, Math.max(0, Number(limit) || 0))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 把改主意渲染成给模型看的一段话;没有就返回空串。 */
|
|
63
|
+
export function renderChanged(items, { lang = 'zh' } = {}) {
|
|
64
|
+
const list = Array.isArray(items) ? items : []
|
|
65
|
+
if (!list.length) return ''
|
|
66
|
+
if (lang === 'en') {
|
|
67
|
+
const lines = list.map((x) => `- ${x.daysAgo === 0 ? 'today' : x.daysAgo + ' days ago'}: you thought ${JSON.stringify(x.then)}, later ${JSON.stringify(x.now)}`)
|
|
68
|
+
return ['【Things you changed your mind about】', ...lines, '(These are your own notes, not something the user said.)'].join('\n')
|
|
69
|
+
}
|
|
70
|
+
const ago = (n) => (n <= 0 ? '今天' : `${n} 天前`)
|
|
71
|
+
const lines = list.map((x) => `- ${ago(x.daysAgo)}:你当时想「${x.then}」,后来想「${x.now}」`)
|
|
72
|
+
return ['【你改过主意的事】', ...lines, '(这是你自己写下的,不是用户说的。)'].join('\n')
|
|
73
|
+
}
|
|
@@ -72,13 +72,17 @@ const SENSITIVE_RULES = [
|
|
|
72
72
|
[/https?:\/\/\S+/gi, '(链接略)'],
|
|
73
73
|
[/\b[\w.+-]+@[\w-]+\.[\w.]+\b/g, '(邮箱略)'],
|
|
74
74
|
[/(?:密码|口令|验证码|密钥|token|api[_-]?key)\s*[::=]?\s*\S+/gi, '(密钥略)'],
|
|
75
|
+
// 门牌号:数字后面跟「号/弄/栋/室/单元」才算,裸数字不碰(否则金额、年份全废)
|
|
76
|
+
[/\d{1,4}\s*(?:号楼|号院|号|弄|栋|幢|单元|室)/g, '(门牌略)'],
|
|
77
|
+
[/([路街巷道])\s*\d{1,4}/g, '$1(门牌略)'],
|
|
75
78
|
[/\b[A-Za-z0-9_-]{24,}\b/g, '(长串略)']
|
|
76
79
|
];
|
|
77
80
|
|
|
78
81
|
export function redactSensitive(value) {
|
|
79
82
|
let text = String(value || '');
|
|
80
83
|
for (const [re, replacement] of SENSITIVE_RULES) text = text.replace(re, replacement);
|
|
81
|
-
|
|
84
|
+
// 「3 号楼 502 室」会连出两个占位,合成一个,别让句子读起来像乱码
|
|
85
|
+
return text.replace(/((门牌略))[\s、]*(?:(门牌略))+/g, '$1');
|
|
82
86
|
}
|
|
83
87
|
|
|
84
88
|
/** 跨窗口引用别人私聊内容时,给模型的使用规则(放在召回条目之后,被裁剪时先裁它)。 */
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** 这一轮要不要递记忆;递了之后调用 confirm() 才记为「想起」 */
|
|
2
|
+
export function surface(memDir: string, options: { chatKey: string; text: string; seen?: Record<string, number>; now?: number; mode?: 'strict' | 'loose'; guard?: (ctx: { chatKey: string; text: string }) => boolean }): { block: string; id?: string; reason: string; confirm?: () => boolean }
|
|
3
|
+
/** 只回答是/否,不碰磁盘 */
|
|
4
|
+
export function shouldInject(options?: { chatKey?: string; text?: string; seen?: Record<string, number>; id?: string; now?: number }): { ok: boolean; reason: string }
|
|
5
|
+
export const INJECTION_RULES: string[]
|
package/lib/injection.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// 什么时候把记忆递回去:宿主最该复用的一段判断。
|
|
2
|
+
//
|
|
3
|
+
// recall 只负责"找到",不负责"该不该说"。这里把 docs/injection.md 的规则
|
|
4
|
+
// 做成可调用的函数,宿主不必自己重写一遍。
|
|
5
|
+
//
|
|
6
|
+
// 规则(按顺序,任一不过就返回空 block):
|
|
7
|
+
// 1. 只在一对一里递
|
|
8
|
+
// 2. 一轮最多一块
|
|
9
|
+
// 3. 同一条 24 小时内不重复
|
|
10
|
+
// 4. 用户提到相关话题才递(由 recall 决定,没命中就是没提到)
|
|
11
|
+
// 5. 默认 strict
|
|
12
|
+
// 6. 递了才记(confirm 由调用方在真正塞进上下文后调用)
|
|
13
|
+
// 7. 没命中就闭嘴
|
|
14
|
+
// 8. guard 只能否决
|
|
15
|
+
import { recall, renderTwoPart, markRecalled } from './self-experience.js'
|
|
16
|
+
import { isPrivateKey } from './waking.js'
|
|
17
|
+
|
|
18
|
+
const DAY = 24 * 3600 * 1000
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 判断这一轮要不要递记忆。
|
|
22
|
+
*
|
|
23
|
+
* @param {string} memDir 记忆目录
|
|
24
|
+
* @param {object} options
|
|
25
|
+
* @param {string} options.chatKey 当前会话键
|
|
26
|
+
* @param {string} options.text 当前这条用户消息
|
|
27
|
+
* @param {Record<string, number>} [options.seen] 宿主保存的"递过没"记录:id → 时间戳
|
|
28
|
+
* @param {number} [options.now=Date.now()]
|
|
29
|
+
* @param {'strict'|'loose'} [options.mode='strict']
|
|
30
|
+
* @param {(ctx:{chatKey:string,text:string})=>boolean} [options.guard] 守卫,返回 false 时一律不递
|
|
31
|
+
* @returns {{block:string, id?:string, reason:string, confirm?:()=>boolean}}
|
|
32
|
+
* block 为空时 reason 说明原因:vetoed / not-private / no-hit / recently-shown
|
|
33
|
+
*/
|
|
34
|
+
export function surface(memDir, { chatKey = '', text = '', seen = {}, now = Date.now(), mode = 'strict', guard } = {}) {
|
|
35
|
+
if (typeof guard === 'function' && guard({ chatKey, text }) === false) {
|
|
36
|
+
return { block: '', reason: 'vetoed' }
|
|
37
|
+
}
|
|
38
|
+
if (!isPrivateKey(chatKey)) return { block: '', reason: 'not-private' }
|
|
39
|
+
if (!String(text || '').trim()) return { block: '', reason: 'no-hit' }
|
|
40
|
+
|
|
41
|
+
const hit = recall(memDir, text, { limit: 1, mode })[0]
|
|
42
|
+
if (!hit) return { block: '', reason: 'no-hit' }
|
|
43
|
+
|
|
44
|
+
const last = Number(seen[hit.id] || 0)
|
|
45
|
+
if (last && now - last < DAY) return { block: '', reason: 'recently-shown' }
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
block: renderTwoPart(hit),
|
|
49
|
+
id: hit.id,
|
|
50
|
+
reason: 'ok',
|
|
51
|
+
confirm() {
|
|
52
|
+
markRecalled(memDir, hit.id)
|
|
53
|
+
seen[hit.id] = now
|
|
54
|
+
return true
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 本轮该不该递:只回答是/否,不碰磁盘。用于宿主自己做预算前的快速判断。 */
|
|
60
|
+
export function shouldInject({ chatKey = '', text = '', seen = {}, id = '', now = Date.now() } = {}) {
|
|
61
|
+
if (!isPrivateKey(chatKey)) return { ok: false, reason: 'not-private' }
|
|
62
|
+
if (!String(text || '').trim()) return { ok: false, reason: 'no-hit' }
|
|
63
|
+
if (id && Number(seen[id] || 0) && now - Number(seen[id]) < DAY) return { ok: false, reason: 'recently-shown' }
|
|
64
|
+
return { ok: true, reason: 'ok' }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const INJECTION_RULES = [
|
|
68
|
+
'只在一对一里递',
|
|
69
|
+
'一轮最多一块',
|
|
70
|
+
'同一条 24 小时内不重复',
|
|
71
|
+
'用户提到相关话题才递',
|
|
72
|
+
'默认用 strict',
|
|
73
|
+
'递了才记',
|
|
74
|
+
'没命中就闭嘴',
|
|
75
|
+
'守卫只能否决'
|
|
76
|
+
]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "Self-layer autobiographical memory core: then / later, append-only. Vault, evidence and consolidation live in the QQ Agent plugin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -120,6 +120,14 @@
|
|
|
120
120
|
"./fact-judge": {
|
|
121
121
|
"types": "./lib/fact-judge.d.ts",
|
|
122
122
|
"default": "./lib/fact-judge.js"
|
|
123
|
+
},
|
|
124
|
+
"./changed": {
|
|
125
|
+
"types": "./lib/changed.d.ts",
|
|
126
|
+
"default": "./lib/changed.js"
|
|
127
|
+
},
|
|
128
|
+
"./injection": {
|
|
129
|
+
"types": "./lib/injection.d.ts",
|
|
130
|
+
"default": "./lib/injection.js"
|
|
123
131
|
}
|
|
124
132
|
},
|
|
125
133
|
"files": [
|