dsh-qqbot-muu 0.1.20 → 0.1.21
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/CHANGELOG.md +168 -154
- package/client/qqbot-settings.js +13401 -13380
- package/dist/config.js +573 -581
- package/dist/features/contextless-store.js +247 -247
- package/dist/features/four-source.js +338 -336
- package/dist/features/group-join-request.js +289 -287
- package/dist/features/local-models.js +1494 -1467
- package/dist/features/style-layer.js +501 -498
- package/dist/features/thinking-log.js +71 -69
- package/dist/gateway/bootstrap.js +1704 -1652
- package/dist/model/settings-reader.js +193 -102
- package/dist/shared/paths.js +128 -0
- package/dist/shared/settings-io.js +79 -83
- package/dshqqbot/.qqbot-extensions/inbound/00-vision.mjs +111 -0
- package/dshqqbot/.qqbot-extensions/inbound/aa-memory-recall.mjs +802 -794
- package/dshqqbot/.qqbot-extensions/inbound/az-feedback.mjs +113 -115
- package/dshqqbot/.qqbot-extensions/inbound/local-answer.mjs +689 -568
- package/dshqqbot/.qqbot-extensions/lib/local-llm.mjs +455 -444
- package/dshqqbot/.qqbot-extensions/lib/memory-agent.mjs +930 -928
- package/dshqqbot/.qqbot-extensions/lib/memory-core.mjs +537 -537
- package/dshqqbot/.qqbot-extensions/lib/memory-db.mjs +2476 -2245
- package/dshqqbot/.qqbot-extensions/lib/memory-hippo.mjs +18 -0
- package/dshqqbot/.qqbot-extensions/lib/memory.mjs +191 -186
- package/dshqqbot/.qqbot-extensions/lib/scope-util.mjs +84 -0
- package/dshqqbot/.qqbot-extensions/outbound/aa-sanitize.mjs +158 -154
- package/dshqqbot/.qqbot-extensions/outbound/memory-agent.mjs +121 -130
- package/dshqqbot/.qqbot-extensions/outbound/turn-memory.mjs +92 -101
- package/dshqqbot/.qqbot-extensions/tools/memory.mjs +178 -120
- package/dshqqbot/.qqbot-extensions/vendor/abogus/test-abogus.mjs +128 -126
- package/package.json +119 -118
- package/scripts/__pycache__/fan-mode.cpython-311.pyc +0 -0
- package/scripts/__pycache__/memory-audit.cpython-311.pyc +0 -0
- package/scripts/__pycache__/qq-ip-sync.cpython-311.pyc +0 -0
- package/scripts/__pycache__/qq-whitelist.cpython-311.pyc +0 -0
- package/scripts/ai-toolkit.py +607 -478
- package/scripts/chat-archive/__pycache__/build-sft-v4.cpython-311.pyc +0 -0
- package/scripts/chat-archive/__pycache__/build-sft.cpython-311.pyc +0 -0
- package/scripts/chat-archive/build-sft-v4.py +566 -0
- package/scripts/chat-archive/build-sft.py +390 -0
- package/scripts/chat-archive/eval-sft.py +155 -0
- package/scripts/chat-archive/extract-memory.py +143 -0
- package/scripts/chat-archive/inventory.py +182 -0
- package/scripts/chat-archive/merge-sft.py +114 -0
- package/scripts/chat-archive/qce-pull.py +217 -0
- package/scripts/chat-archive/qqheader.py +110 -0
- package/scripts/chat-archive/qqkey2.py +113 -0
- package/scripts/chat-archive/qqprobe.py +153 -0
- package/scripts/chat-archive/screen-collect.py +163 -0
- package/scripts/chat-archive/sqlcipher_probe.py +444 -0
- package/scripts/episode.py +529 -0
- package/scripts/fan-curve.py +218 -0
- package/scripts/fan-guard.cmd +8 -0
- package/scripts/fan-mode.py +380 -263
- package/scripts/gen-image.py +420 -413
- package/scripts/gen-video.py +186 -178
- package/scripts/hw-profile.py +175 -0
- package/scripts/media-enhance.py +402 -0
- package/scripts/memory-audit.py +581 -0
- package/scripts/migrate-scope.mjs +140 -139
- package/scripts/qq-connect-login.py +133 -0
- package/scripts/qq-ip-sync-hidden.vbs +5 -0
- package/scripts/qq-ip-sync.py +300 -0
- package/scripts/qq-whitelist.py +71 -0
- package/scripts/release-all.mjs +636 -509
- package/scripts/release-check.mjs +244 -226
- package/scripts/secure-room/index.html +334 -0
- package/scripts/secure-room/server.py +582 -0
- package/scripts/stickers/auto-tag.mjs +161 -0
- package/scripts/train/__pycache__/train-lora.cpython-311.pyc +0 -0
- package/scripts/train/gguf-quantize.py +92 -0
- package/scripts/train/gguf-to-hf.py +112 -0
- package/scripts/train/merge-lora.py +51 -0
- package/scripts/train/probe-mirrors.sh +12 -0
- package/scripts/train/probe-mirrors2.sh +20 -0
- package/scripts/train/setup-wsl.sh +23 -0
- package/scripts/train/setup-wsl2.sh +25 -0
- package/scripts/train/setup-wsl3.sh +22 -0
- package/scripts/train/setup-wsl4.sh +22 -0
- package/scripts/train/train-lora.py +319 -0
- package/dist/_backup_ctx_20261125_183653/dist/config.d.ts +0 -473
- package/dist/_backup_ctx_20261125_183653/dist/config.js +0 -450
- package/dist/_backup_ctx_20261125_183653/dist/features/contextless-trim.js +0 -304
- package/dist/_backup_ctx_20261125_183653/dist/session/session-manager.js +0 -1588
- package/dist/_backup_ctx_20261125_183653/dist/transport/inbound.js +0 -1567
- package/dist/_backup_ctx_20261125_183653/dist/transport/outbound.js +0 -647
- package/scripts/__pycache__/ai-mode.cpython-311.pyc +0 -0
- package/scripts/__pycache__/ai-toolkit.cpython-311.pyc +0 -0
- package/scripts/__pycache__/comfy_gen.cpython-311.pyc +0 -0
- package/scripts/__pycache__/gen-image.cpython-311.pyc +0 -0
- package/scripts/__pycache__/gen-video.cpython-311.pyc +0 -0
- package/scripts/__pycache__/model-registry.cpython-311.pyc +0 -0
- package/scripts/__pycache__/purge-standby.cpython-311.pyc +0 -0
|
@@ -205,6 +205,19 @@ export async function hippoRecall(dataRoot, scope, queryText, cfg = loadConfig(d
|
|
|
205
205
|
if (x.why.includes('gist')) x.rel = Math.max(x.rel, 0.6);
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
+
// 置顶(2026-12-03 主人口径):同一件事的多个版本,新的必须排在旧的上面;
|
|
209
|
+
// 旧版不删、不标过期,只是靠后 —— 供没有"最新"需求的场景溯源。
|
|
210
|
+
// 注意:MMR 会因两句几乎同义而惩罚后选的那条,所以先摆正新旧,再走 MMR。
|
|
211
|
+
{
|
|
212
|
+
const byId = new Map(list.map((x) => [x.id, x]));
|
|
213
|
+
for (const old of list) {
|
|
214
|
+
const newer = old.supersededBy ? byId.get(old.supersededBy) : null;
|
|
215
|
+
if (!newer) continue;
|
|
216
|
+
old.rel = old.rel * 0.8;
|
|
217
|
+
newer.rel = Math.max(newer.rel, Math.min(1, old.rel + 0.05));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
208
221
|
// MMR:先挑最相关,再挑「既相关又不重复」的(模式分离)
|
|
209
222
|
const picked = [];
|
|
210
223
|
const rest = list.slice();
|
|
@@ -248,6 +261,11 @@ export async function hippoRecall(dataRoot, scope, queryText, cfg = loadConfig(d
|
|
|
248
261
|
ent: num(x.ent),
|
|
249
262
|
why: x.why,
|
|
250
263
|
mmr: x.mmr,
|
|
264
|
+
// 置顶/溯源字段透传(面板与注入块要能看出"这条是更新版/被谁接上")
|
|
265
|
+
priority: num(x.priority),
|
|
266
|
+
pinnedAt: num(x.pinnedAt),
|
|
267
|
+
supersedes: String(x.supersedes || ''),
|
|
268
|
+
supersededBy: String(x.supersededBy || ''),
|
|
251
269
|
});
|
|
252
270
|
}
|
|
253
271
|
return out;
|
|
@@ -1,186 +1,191 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* memory.mjs — 记忆门面(调用方只认这一个入口)
|
|
3
|
-
*
|
|
4
|
-
* 为什么需要门面:
|
|
5
|
-
* 我们有两套实现 —— memory-db.mjs(SQLite,正式)和 memory-core.mjs(JSONL,回退)。
|
|
6
|
-
* 调用方不该关心用哪套,也不该因为宿主没编 node:sqlite 就整个记忆功能崩掉。
|
|
7
|
-
*
|
|
8
|
-
* 策略:
|
|
9
|
-
* 模块加载时先 try 一次 memory-db.mjs,并调用一次 openDb() 探测(内存库,不碰磁盘)。
|
|
10
|
-
* 成功 → 全部 re-export db 版;失败 → 全部 re-export core 版。
|
|
11
|
-
* 两套的导出**同名同签名**,所以调用方只需把 import 路径改成 ../lib/memory.mjs,逻辑一行不动。
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
let _impl = null;
|
|
15
|
-
let _backend = 'jsonl';
|
|
16
|
-
let _note = '';
|
|
17
|
-
|
|
18
|
-
// 调试开关:QQBOT_MEMORY_BACKEND=jsonl 可强制走老的 JSONL 实现(不需要改代码)
|
|
19
|
-
const _force = String(process.env.QQBOT_MEMORY_BACKEND || '').trim().toLowerCase();
|
|
20
|
-
|
|
21
|
-
if (_force === 'jsonl') {
|
|
22
|
-
_note = '环境变量 QQBOT_MEMORY_BACKEND=jsonl,强制走 JSONL';
|
|
23
|
-
} else {
|
|
24
|
-
try {
|
|
25
|
-
const db = await import('./memory-db.mjs');
|
|
26
|
-
const probe = db.openDb(); // 无参 = 只探测 node:sqlite 可用性
|
|
27
|
-
if (probe && probe.ok) {
|
|
28
|
-
_impl = db;
|
|
29
|
-
_backend = 'sqlite';
|
|
30
|
-
_note = probe.fts ? 'sqlite+FTS5' : 'sqlite(无 FTS5)';
|
|
31
|
-
} else {
|
|
32
|
-
_note = 'sqlite 探测失败,回退 JSONL';
|
|
33
|
-
}
|
|
34
|
-
} catch (e) {
|
|
35
|
-
_note = 'node:sqlite 不可用,回退 JSONL:' + (e instanceof Error ? e.message : String(e));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (!_impl) {
|
|
40
|
-
_impl = await import('./memory-core.mjs');
|
|
41
|
-
_backend = 'jsonl';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** 当前生效的后端名:'sqlite' | 'jsonl' */
|
|
45
|
-
export const backendName = _backend;
|
|
46
|
-
/** 换后端的原因(调试用,空字符串 = 一次就成功) */
|
|
47
|
-
export const backendNote = _note;
|
|
48
|
-
/** 后端自述 */
|
|
49
|
-
export function memoryBackend() {
|
|
50
|
-
return { backend: _backend, note: _note || 'sqlite 正常' };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// ── 聊天原始台账(chatlog-db,和 mem 共用一个 SQLite 文件)──────────────
|
|
54
|
-
// 只在 sqlite 后端可用;JSONL 回退时给安全空实现,保证入站/出站链路不炸。
|
|
55
|
-
let _chat = null;
|
|
56
|
-
if (_backend === 'sqlite') {
|
|
57
|
-
try { _chat = await import('./chatlog-db.mjs'); } catch { _chat = null; }
|
|
58
|
-
}
|
|
59
|
-
const chatFn = (name, fallback) => (_chat && _chat[name]) || fallback;
|
|
60
|
-
|
|
61
|
-
export const ingestChatHistory = chatFn('ingestChatHistory', () => ({ ok: false, imported: 0, reason: 'chatlog 只在 sqlite 后端可用' }));
|
|
62
|
-
export const archiveMessages = chatFn('archiveMessages', () => ({ ok: false, added: 0, reason: 'chatlog 只在 sqlite 后端可用' }));
|
|
63
|
-
export const appendChat = chatFn('appendChat', () => false);
|
|
64
|
-
export const queryChat = chatFn('queryChat', () => []);
|
|
65
|
-
export const searchChat = chatFn('searchChat', () => []);
|
|
66
|
-
export const chatStats = chatFn('chatStats', () => ({ backend: 'jsonl', total: 0, scopes: [] }));
|
|
67
|
-
export const pruneChat = chatFn('pruneChat', () => ({ ok: false, deleted: 0, reason: 'chatlog 只在 sqlite 后端可用' }));
|
|
68
|
-
export const dropChatScope = chatFn('dropChatScope', () => 0);
|
|
69
|
-
export const getChatState = chatFn('getChatState', () => ({}));
|
|
70
|
-
export const setChatState = chatFn('setChatState', () => false);
|
|
71
|
-
export const cleanChatText = chatFn('cleanChatText', (t) => String(t ?? '').replace(/\s+/g, ' ').trim());
|
|
72
|
-
|
|
73
|
-
const pick = (name) => _impl[name];
|
|
74
|
-
|
|
75
|
-
// ── 类海马体层(只在 sqlite 后端有意义;JSONL 后端退回原始召回)──────────
|
|
76
|
-
let _hippo = null;
|
|
77
|
-
if (_backend === 'sqlite') {
|
|
78
|
-
try { _hippo = await import('./memory-hippo.mjs'); } catch { _hippo = null; }
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// ── 与 memory-core 完全同名的 18 个函数 ─────────────────────────────────
|
|
82
|
-
export const loadConfig = pick('loadConfig');
|
|
83
|
-
export const memoryDir = pick('memoryDir');
|
|
84
|
-
export const memoryFile = pick('memoryFile');
|
|
85
|
-
export const embedderMode = pick('embedderMode');
|
|
86
|
-
export const cos = pick('cos');
|
|
87
|
-
export const loadMemories = pick('loadMemories');
|
|
88
|
-
export const remember = pick('remember');
|
|
89
|
-
export const forget = pick('forget');
|
|
90
|
-
// 召回默认走类海马体版:先多路召回拿候选,再做时间邻近扩散 + 归纳补全 + MMR 去重。
|
|
91
|
-
// 失败会自己降级回原始 recall,签名不变,调用方无感。
|
|
92
|
-
export const recall = (_hippo && _hippo.hippoRecall) || pick('recall');
|
|
93
|
-
export const recallRaw = pick('recall');
|
|
94
|
-
export const formatRecallBlock = pick('formatRecallBlock');
|
|
95
|
-
// 历史聊天记录导入后的人物画像:只在 sqlite 后端有(JSONL 没有 scope 分表的概念),
|
|
96
|
-
// 回退时给安全空实现,保证入站链路不炸。
|
|
97
|
-
export const personProfile = pick('personProfile')
|
|
98
|
-
|| (() => ({ ok: false, scope: '', impression: '', say: 0, first: 0, last: 0 }));
|
|
99
|
-
export const stats = pick('stats');
|
|
100
|
-
export const inboxFile = pick('inboxFile');
|
|
101
|
-
export const pushInbox = pick('pushInbox');
|
|
102
|
-
export const drainInbox = pick('drainInbox');
|
|
103
|
-
export const listAll = pick('listAll');
|
|
104
|
-
export const forgetAnywhere = pick('forgetAnywhere');
|
|
105
|
-
export const digestScope = pick('digestScope');
|
|
106
|
-
export const shouldDigest = pick('shouldDigest');
|
|
107
|
-
|
|
108
|
-
// ── SQLite 后端专属(JSONL 后端下是安全空实现,不会抛)──────────────────
|
|
109
|
-
const noop = (reason) => () => { throw new Error(reason); };
|
|
110
|
-
const emptyArr = () => [];
|
|
111
|
-
const falseRet = () => false;
|
|
112
|
-
|
|
113
|
-
export const openDb = _impl.openDb || noop('当前是 JSONL 后端,没有 openDb');
|
|
114
|
-
export const closeDb = _impl.closeDb || falseRet;
|
|
115
|
-
export const migrateJsonl = _impl.migrateJsonl || (() => ({ ok: false, reason: '当前是 JSONL 后端,无需迁移' }));
|
|
116
|
-
export const searchText = _impl.searchText || emptyArr;
|
|
117
|
-
export const vecToBlob = _impl.vecToBlob || (() => null);
|
|
118
|
-
export const blobToVec = _impl.blobToVec || emptyArr;
|
|
119
|
-
|
|
120
|
-
export const rebuildEmbeddings = _impl.rebuildEmbeddings || (async () => ({ ok: false, reason: '向量重嵌只在 sqlite 后端可用' }));
|
|
121
|
-
export const embeddingModelInfo = _impl.embeddingModelInfo || (() => ({ model: '', mode: '', rows: 0, ts: 0 }));
|
|
122
|
-
export const currentEmbedderInfo = _impl.currentEmbedderInfo || (async () => ({ model: '', mode: 'unknown', dim: 0, error: 'jsonl 后端' }));
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
export const
|
|
127
|
-
export const
|
|
128
|
-
export const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
export const
|
|
134
|
-
export const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
//
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
export const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
export const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
export const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
export const
|
|
179
|
-
export const
|
|
180
|
-
export const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
export const
|
|
185
|
-
export const
|
|
186
|
-
export const
|
|
1
|
+
/**
|
|
2
|
+
* memory.mjs — 记忆门面(调用方只认这一个入口)
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要门面:
|
|
5
|
+
* 我们有两套实现 —— memory-db.mjs(SQLite,正式)和 memory-core.mjs(JSONL,回退)。
|
|
6
|
+
* 调用方不该关心用哪套,也不该因为宿主没编 node:sqlite 就整个记忆功能崩掉。
|
|
7
|
+
*
|
|
8
|
+
* 策略:
|
|
9
|
+
* 模块加载时先 try 一次 memory-db.mjs,并调用一次 openDb() 探测(内存库,不碰磁盘)。
|
|
10
|
+
* 成功 → 全部 re-export db 版;失败 → 全部 re-export core 版。
|
|
11
|
+
* 两套的导出**同名同签名**,所以调用方只需把 import 路径改成 ../lib/memory.mjs,逻辑一行不动。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
let _impl = null;
|
|
15
|
+
let _backend = 'jsonl';
|
|
16
|
+
let _note = '';
|
|
17
|
+
|
|
18
|
+
// 调试开关:QQBOT_MEMORY_BACKEND=jsonl 可强制走老的 JSONL 实现(不需要改代码)
|
|
19
|
+
const _force = String(process.env.QQBOT_MEMORY_BACKEND || '').trim().toLowerCase();
|
|
20
|
+
|
|
21
|
+
if (_force === 'jsonl') {
|
|
22
|
+
_note = '环境变量 QQBOT_MEMORY_BACKEND=jsonl,强制走 JSONL';
|
|
23
|
+
} else {
|
|
24
|
+
try {
|
|
25
|
+
const db = await import('./memory-db.mjs');
|
|
26
|
+
const probe = db.openDb(); // 无参 = 只探测 node:sqlite 可用性
|
|
27
|
+
if (probe && probe.ok) {
|
|
28
|
+
_impl = db;
|
|
29
|
+
_backend = 'sqlite';
|
|
30
|
+
_note = probe.fts ? 'sqlite+FTS5' : 'sqlite(无 FTS5)';
|
|
31
|
+
} else {
|
|
32
|
+
_note = 'sqlite 探测失败,回退 JSONL';
|
|
33
|
+
}
|
|
34
|
+
} catch (e) {
|
|
35
|
+
_note = 'node:sqlite 不可用,回退 JSONL:' + (e instanceof Error ? e.message : String(e));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!_impl) {
|
|
40
|
+
_impl = await import('./memory-core.mjs');
|
|
41
|
+
_backend = 'jsonl';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 当前生效的后端名:'sqlite' | 'jsonl' */
|
|
45
|
+
export const backendName = _backend;
|
|
46
|
+
/** 换后端的原因(调试用,空字符串 = 一次就成功) */
|
|
47
|
+
export const backendNote = _note;
|
|
48
|
+
/** 后端自述 */
|
|
49
|
+
export function memoryBackend() {
|
|
50
|
+
return { backend: _backend, note: _note || 'sqlite 正常' };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ── 聊天原始台账(chatlog-db,和 mem 共用一个 SQLite 文件)──────────────
|
|
54
|
+
// 只在 sqlite 后端可用;JSONL 回退时给安全空实现,保证入站/出站链路不炸。
|
|
55
|
+
let _chat = null;
|
|
56
|
+
if (_backend === 'sqlite') {
|
|
57
|
+
try { _chat = await import('./chatlog-db.mjs'); } catch { _chat = null; }
|
|
58
|
+
}
|
|
59
|
+
const chatFn = (name, fallback) => (_chat && _chat[name]) || fallback;
|
|
60
|
+
|
|
61
|
+
export const ingestChatHistory = chatFn('ingestChatHistory', () => ({ ok: false, imported: 0, reason: 'chatlog 只在 sqlite 后端可用' }));
|
|
62
|
+
export const archiveMessages = chatFn('archiveMessages', () => ({ ok: false, added: 0, reason: 'chatlog 只在 sqlite 后端可用' }));
|
|
63
|
+
export const appendChat = chatFn('appendChat', () => false);
|
|
64
|
+
export const queryChat = chatFn('queryChat', () => []);
|
|
65
|
+
export const searchChat = chatFn('searchChat', () => []);
|
|
66
|
+
export const chatStats = chatFn('chatStats', () => ({ backend: 'jsonl', total: 0, scopes: [] }));
|
|
67
|
+
export const pruneChat = chatFn('pruneChat', () => ({ ok: false, deleted: 0, reason: 'chatlog 只在 sqlite 后端可用' }));
|
|
68
|
+
export const dropChatScope = chatFn('dropChatScope', () => 0);
|
|
69
|
+
export const getChatState = chatFn('getChatState', () => ({}));
|
|
70
|
+
export const setChatState = chatFn('setChatState', () => false);
|
|
71
|
+
export const cleanChatText = chatFn('cleanChatText', (t) => String(t ?? '').replace(/\s+/g, ' ').trim());
|
|
72
|
+
|
|
73
|
+
const pick = (name) => _impl[name];
|
|
74
|
+
|
|
75
|
+
// ── 类海马体层(只在 sqlite 后端有意义;JSONL 后端退回原始召回)──────────
|
|
76
|
+
let _hippo = null;
|
|
77
|
+
if (_backend === 'sqlite') {
|
|
78
|
+
try { _hippo = await import('./memory-hippo.mjs'); } catch { _hippo = null; }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ── 与 memory-core 完全同名的 18 个函数 ─────────────────────────────────
|
|
82
|
+
export const loadConfig = pick('loadConfig');
|
|
83
|
+
export const memoryDir = pick('memoryDir');
|
|
84
|
+
export const memoryFile = pick('memoryFile');
|
|
85
|
+
export const embedderMode = pick('embedderMode');
|
|
86
|
+
export const cos = pick('cos');
|
|
87
|
+
export const loadMemories = pick('loadMemories');
|
|
88
|
+
export const remember = pick('remember');
|
|
89
|
+
export const forget = pick('forget');
|
|
90
|
+
// 召回默认走类海马体版:先多路召回拿候选,再做时间邻近扩散 + 归纳补全 + MMR 去重。
|
|
91
|
+
// 失败会自己降级回原始 recall,签名不变,调用方无感。
|
|
92
|
+
export const recall = (_hippo && _hippo.hippoRecall) || pick('recall');
|
|
93
|
+
export const recallRaw = pick('recall');
|
|
94
|
+
export const formatRecallBlock = pick('formatRecallBlock');
|
|
95
|
+
// 历史聊天记录导入后的人物画像:只在 sqlite 后端有(JSONL 没有 scope 分表的概念),
|
|
96
|
+
// 回退时给安全空实现,保证入站链路不炸。
|
|
97
|
+
export const personProfile = pick('personProfile')
|
|
98
|
+
|| (() => ({ ok: false, scope: '', impression: '', say: 0, first: 0, last: 0 }));
|
|
99
|
+
export const stats = pick('stats');
|
|
100
|
+
export const inboxFile = pick('inboxFile');
|
|
101
|
+
export const pushInbox = pick('pushInbox');
|
|
102
|
+
export const drainInbox = pick('drainInbox');
|
|
103
|
+
export const listAll = pick('listAll');
|
|
104
|
+
export const forgetAnywhere = pick('forgetAnywhere');
|
|
105
|
+
export const digestScope = pick('digestScope');
|
|
106
|
+
export const shouldDigest = pick('shouldDigest');
|
|
107
|
+
|
|
108
|
+
// ── SQLite 后端专属(JSONL 后端下是安全空实现,不会抛)──────────────────
|
|
109
|
+
const noop = (reason) => () => { throw new Error(reason); };
|
|
110
|
+
const emptyArr = () => [];
|
|
111
|
+
const falseRet = () => false;
|
|
112
|
+
|
|
113
|
+
export const openDb = _impl.openDb || noop('当前是 JSONL 后端,没有 openDb');
|
|
114
|
+
export const closeDb = _impl.closeDb || falseRet;
|
|
115
|
+
export const migrateJsonl = _impl.migrateJsonl || (() => ({ ok: false, reason: '当前是 JSONL 后端,无需迁移' }));
|
|
116
|
+
export const searchText = _impl.searchText || emptyArr;
|
|
117
|
+
export const vecToBlob = _impl.vecToBlob || (() => null);
|
|
118
|
+
export const blobToVec = _impl.blobToVec || emptyArr;
|
|
119
|
+
|
|
120
|
+
export const rebuildEmbeddings = _impl.rebuildEmbeddings || (async () => ({ ok: false, reason: '向量重嵌只在 sqlite 后端可用' }));
|
|
121
|
+
export const embeddingModelInfo = _impl.embeddingModelInfo || (() => ({ model: '', mode: '', rows: 0, ts: 0 }));
|
|
122
|
+
export const currentEmbedderInfo = _impl.currentEmbedderInfo || (async () => ({ model: '', mode: 'unknown', dim: 0, error: 'jsonl 后端' }));
|
|
123
|
+
|
|
124
|
+
// ── 置顶 / 溯源(2026-12-03 主人口径:旧事实不标过期,只让新版置顶)──────
|
|
125
|
+
// sqlite 后端有真实现;JSONL 后端如实说"不支持",调用方不用判后端。
|
|
126
|
+
export const pinMemory = _impl.pinMemory || (() => ({ ok: false, reason: '置顶只在 sqlite 后端可用' }));
|
|
127
|
+
export const pinChain = _impl.pinChain || (() => ({ ok: false, chain: [], reason: '溯源链只在 sqlite 后端可用' }));
|
|
128
|
+
export const backfillPins = _impl.backfillPins || (() => ({ ok: false, reason: '只在 sqlite 后端可用' }));
|
|
129
|
+
|
|
130
|
+
export const setPersona = _impl.setPersona || (() => ({ ok: false, reason: 'persona 只在 sqlite 后端可用' }));export const getPersona = _impl.getPersona || emptyArr;
|
|
131
|
+
export const getPersonaValue = _impl.getPersonaValue || (() => null);
|
|
132
|
+
export const listPersona = _impl.listPersona || emptyArr;
|
|
133
|
+
export const forgetPersona = _impl.forgetPersona || falseRet;
|
|
134
|
+
export const deletePersona = _impl.deletePersona || (() => ({ ok: false, reason: 'persona 只在 sqlite 后端可用' }));
|
|
135
|
+
|
|
136
|
+
// ── 维护用(清洗/权重/库文件路径)──────────────────────────────────────
|
|
137
|
+
// cleanMemText 两边都有实现;weightOf / dbFile 只有 sqlite 版才有意义。
|
|
138
|
+
export const cleanMemText = _impl.cleanMemText || ((t) => String(t ?? '').replace(/\s+/g, ' ').trim());
|
|
139
|
+
export const weightOf = _impl.weightOf || (() => 0);
|
|
140
|
+
export const dbFile = _impl.dbFile || (() => memoryFile('', loadConfig('')));
|
|
141
|
+
|
|
142
|
+
// ── 类海马体层对外接口(JSONL 后端给空实现,调用方不用判后端)────────────
|
|
143
|
+
const hippoNoop = {
|
|
144
|
+
replay: () => ({ ok: false, reason: '类海马体层只在 sqlite 后端可用' }),
|
|
145
|
+
beliefs: () => [],
|
|
146
|
+
observeTrait: () => ({ ok: false, reason: '类海马体层只在 sqlite 后端可用' }),
|
|
147
|
+
anchorBeliefs: () => ({ ok: false, reason: '类海马体层只在 sqlite 后端可用' }),
|
|
148
|
+
driftReport: () => ({ rows: [], stability: 100, worst: null, alarm: 0 }),
|
|
149
|
+
hippoStats: () => ({ ok: false }),
|
|
150
|
+
};
|
|
151
|
+
export const replay = (_hippo && _hippo.replay) || hippoNoop.replay;
|
|
152
|
+
export const beliefs = (_hippo && _hippo.beliefs) || hippoNoop.beliefs;
|
|
153
|
+
export const observeTrait = (_hippo && _hippo.observeTrait) || hippoNoop.observeTrait;
|
|
154
|
+
export const anchorBeliefs = (_hippo && _hippo.anchorBeliefs) || hippoNoop.anchorBeliefs;
|
|
155
|
+
export const driftReport = (_hippo && _hippo.driftReport) || hippoNoop.driftReport;
|
|
156
|
+
export const hippoStats = (_hippo && _hippo.hippoStats) || hippoNoop.hippoStats;
|
|
157
|
+
|
|
158
|
+
// ── 时序图谱 + 数据主权(JSONL 后端给空实现,调用方不用判后端)────────────
|
|
159
|
+
// 图谱只在 sqlite 后端有意义:JSONL 版没有 mem_edge 表。
|
|
160
|
+
const graphNoop = {
|
|
161
|
+
addEdges: () => ({ ok: false, added: 0, superseded: 0, reason: '图谱只在 sqlite 后端可用' }),
|
|
162
|
+
liveNeighbors: () => [],
|
|
163
|
+
edgeHistory: () => [],
|
|
164
|
+
personalizedPageRank: () => new Map(),
|
|
165
|
+
};
|
|
166
|
+
export const addEdges = _impl.addEdges || graphNoop.addEdges;
|
|
167
|
+
export const liveNeighbors = _impl.liveNeighbors || graphNoop.liveNeighbors;
|
|
168
|
+
export const edgeHistory = _impl.edgeHistory || graphNoop.edgeHistory;
|
|
169
|
+
export const personalizedPageRank = _impl.personalizedPageRank || graphNoop.personalizedPageRank;
|
|
170
|
+
|
|
171
|
+
// 导出/彻底删除/审计:两边都尽量实现(JSONL 版只有导出和删除文件)
|
|
172
|
+
export const exportScope = _impl.exportScope || ((dataRoot, scope) => ({
|
|
173
|
+
scope: String(scope || ''), exportedAt: Date.now(),
|
|
174
|
+
total: (loadMemories(dataRoot, scope, loadConfig(dataRoot)) || []).length,
|
|
175
|
+
memories: loadMemories(dataRoot, scope, loadConfig(dataRoot)) || [], persona: [], edges: [],
|
|
176
|
+
}));
|
|
177
|
+
export const purgeScope = _impl.purgeScope || (() => ({ ok: false, reason: '彻底删除只在 sqlite 后端可用' }));
|
|
178
|
+
export const audit = _impl.audit || (() => false);
|
|
179
|
+
export const readAudit = _impl.readAudit || (() => []);
|
|
180
|
+
export const auditFile = _impl.auditFile || (() => '');
|
|
181
|
+
|
|
182
|
+
// 隐私:字段级加密(sqlite 后端真实现;JSONL 后端如实说"不适用")
|
|
183
|
+
export const privacyState = _impl.privacyState || (() => ({ encryptAtRest: false, keyReady: false, cipher: 'none', backend: 'jsonl' }));
|
|
184
|
+
export const setEncryption = _impl.setEncryption || (() => ({ ok: false, reason: '字段级加密只在 sqlite 后端可用' }));
|
|
185
|
+
export const reencryptAll = _impl.reencryptAll || (() => ({ ok: false, reason: '只在 sqlite 后端可用' }));
|
|
186
|
+
export const memDecrypt = _impl.memDecrypt || ((dataRoot, s) => String(s ?? ''));
|
|
187
|
+
|
|
188
|
+
// 持久化运维:体检 / 维护 / schema 自述(JSONL 后端如实说"不适用",不假装健康)
|
|
189
|
+
export const dbHealth = _impl.dbHealth || (() => ({ ok: false, healthy: false, verdict: 'JSONL 后端没有 SQLite 库' }));
|
|
190
|
+
export const maintainDb = _impl.maintainDb || (() => ({ ok: false, reason: 'JSONL 后端没有 SQLite 库' }));
|
|
191
|
+
export const schemaState = _impl.schemaState || (() => ({ version: 0, missing: [], error: 'JSONL 后端' }));
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* scope-util.mjs — 会话作用域的唯一口径(写入端 / 召回端 / 扩展工具共用)
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独抽出来:
|
|
5
|
+
* 记忆是按 scope 分桶的。以前三个地方各写一份判断:
|
|
6
|
+
* · inbound/aa-memory-recall.mjs scopeOf(ctx.message)
|
|
7
|
+
* · outbound/memory-agent.mjs scopeOfTarget(ctx.target)
|
|
8
|
+
* · tools/memory.mjs 拿不到 scope,只能写公共收件箱
|
|
9
|
+
* 口径一旦漂移(例如宿主把私聊的 scope 字段从 'c2c' 改成 'friend',或者
|
|
10
|
+
* 出站只给了 targetId 没给 groupId),同一个人的记忆就会被写到两个桶里,
|
|
11
|
+
* 读的时候又只读一个桶 —— 表现就是"记忆没记住 / 记到别人名下"。
|
|
12
|
+
* 这里只保留一份实现,三个入口都调它。
|
|
13
|
+
*
|
|
14
|
+
* 约定(与 person-link / chatlog 的 scope 形状一致):
|
|
15
|
+
* 群 group:<群 openid>
|
|
16
|
+
* 私聊 c2c:<对方 openid / wxid>
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const GROUP_KINDS = new Set(['group', 'gchat', 'group_chat', 'channel', 'guild', '群聊', '群'])
|
|
20
|
+
const C2C_KINDS = new Set(['c2c', 'user', 'private', 'friend', 'direct', 'dm', 'p2p', 'private_chat', '好友', '私聊'])
|
|
21
|
+
|
|
22
|
+
/** 32 位 openid / wxid / QQ 号这类"长得像 id"的串,不能当人名用 */
|
|
23
|
+
export function looksLikeOpenid(s) {
|
|
24
|
+
const t = String(s || '').trim()
|
|
25
|
+
if (!t) return false
|
|
26
|
+
if (/^wxid_[0-9a-zA-Z]+$/.test(t)) return true
|
|
27
|
+
if (/^[0-9A-Fa-f]{16,}$/.test(t)) return true
|
|
28
|
+
return t.length >= 16 && !/[\u4e00-\u9fa5]/.test(t) && /^[A-Za-z0-9_-]+$/.test(t)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function firstId(...vals) {
|
|
32
|
+
for (const v of vals) {
|
|
33
|
+
// 宿主有时把 group 字段给成对象(群资料)——只认字符串/数字,
|
|
34
|
+
// 否则 String({}) 会拼出 "group:[object Object]" 这种假 scope。
|
|
35
|
+
if (typeof v === 'number' && Number.isFinite(v)) return String(v)
|
|
36
|
+
if (typeof v === 'string' && v.trim()) return v.trim()
|
|
37
|
+
}
|
|
38
|
+
return ''
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 入站消息 → scope。
|
|
43
|
+
* 群的判据是"带群标识字段",而不是只看 groupOpenid 一个字段名:
|
|
44
|
+
* 不同宿主版本会叫 groupOpenid / groupId / group_id / group。
|
|
45
|
+
*/
|
|
46
|
+
export function scopeOfMessage(m) {
|
|
47
|
+
if (!m || typeof m !== 'object') return ''
|
|
48
|
+
const gid = firstId(m.groupOpenid, m.groupId, m.group_id, m.group)
|
|
49
|
+
const kind = String(m.scopeKind ?? m.chatType ?? m.chat_type ?? '').trim().toLowerCase()
|
|
50
|
+
if (gid || GROUP_KINDS.has(kind)) return gid ? 'group:' + gid : ''
|
|
51
|
+
if (C2C_KINDS.has(kind) || !kind) {
|
|
52
|
+
const sid = firstId(m.senderId, m.sender_id, m.openid, m.userId, m.user_id)
|
|
53
|
+
return sid ? 'c2c:' + sid : ''
|
|
54
|
+
}
|
|
55
|
+
const sid = firstId(m.senderId, m.sender_id, m.openid, m.userId, m.user_id)
|
|
56
|
+
return sid ? 'c2c:' + sid : ''
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 出站 target → scope。
|
|
61
|
+
* ★ 旧实现没认出来的 kind 一律当群('group:' + peer):宿主新增一个
|
|
62
|
+
* 'friend'/'dm' 之类的值时,私聊会被整段写成群记忆,这是最隐蔽的串号源。
|
|
63
|
+
* 现在反过来:没有明确群标识就当私聊。
|
|
64
|
+
*/
|
|
65
|
+
export function scopeOfTarget(t) {
|
|
66
|
+
if (!t || typeof t !== 'object') return ''
|
|
67
|
+
const peer = firstId(t.peerId, t.targetId, t.target_id, t.openid, t.id)
|
|
68
|
+
if (!peer) return ''
|
|
69
|
+
const kind = String(t.scope ?? t.scopeKind ?? t.chatType ?? '').trim().toLowerCase()
|
|
70
|
+
if (GROUP_KINDS.has(kind)) return 'group:' + peer
|
|
71
|
+
if (C2C_KINDS.has(kind)) return 'c2c:' + peer
|
|
72
|
+
// kind 缺失/不认识:有群字段才是群,否则按私聊(宁可少一个群,不把私聊写串)
|
|
73
|
+
const gid = firstId(t.groupOpenid, t.groupId, t.group_id)
|
|
74
|
+
return gid ? 'group:' + peer : 'c2c:' + peer
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 消息里的"说话人名字";拿不到真名就返回空串,绝不能把 openid 写进 subj */
|
|
78
|
+
export function subjOfMessage(m) {
|
|
79
|
+
const name = String(m?.senderName ?? m?.sender_name ?? m?.nickname ?? '').trim()
|
|
80
|
+
if (!name || looksLikeOpenid(name) || name === '机器人') return ''
|
|
81
|
+
return name
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export default { scopeOfMessage, scopeOfTarget, subjOfMessage, looksLikeOpenid }
|