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
|
@@ -1,101 +1,92 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* turn-memory.mjs — 回合结束把"她自己说过的话"沉淀进记忆库
|
|
3
|
-
*
|
|
4
|
-
* 为什么需要:现在的记忆只有"别人说了什么"(入站沉淀)。
|
|
5
|
-
* 结果她记不得自己答应过什么、说过什么梗 —— 而 Neuro 那种"像个人"的关键之一,
|
|
6
|
-
* 就是**记得自己说过的话**("你上次说教我钓鱼的")。
|
|
7
|
-
*
|
|
8
|
-
* 挂点:第六套(出站)。出站链每回合会多跑一次 `phase === 'turnEnd'` 的收尾,
|
|
9
|
-
* 正好当"回合结束"信号用,不需要插件再暴露新钩子。
|
|
10
|
-
*
|
|
11
|
-
* 省钱/防污染:
|
|
12
|
-
* · 只记 ≥ 12 字的正文(太短没信息量);
|
|
13
|
-
* · 纯媒体/纯表情不记;
|
|
14
|
-
* · 含"答应/保证/下次/回头"这类承诺词的标成 promise,召回声量(hits 权重)用得上;
|
|
15
|
-
* · 去重交给记忆库(cos > 0.92 视为同一条)。
|
|
16
|
-
*/
|
|
17
|
-
import { join } from 'node:path';
|
|
18
|
-
import { fileURLToPath } from 'node:url';
|
|
19
|
-
import { dirname } from 'node:path';
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
.replace(/\[
|
|
36
|
-
.replace(
|
|
37
|
-
.replace(
|
|
38
|
-
.replace(
|
|
39
|
-
.replace(
|
|
40
|
-
.replace(
|
|
41
|
-
.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}, cfg);
|
|
94
|
-
if (r && r.ok) {
|
|
95
|
-
ctx?.logger?.debug?.(`[turn-memory] 记住自己说的(${kind}):${text.slice(0, 24)}`);
|
|
96
|
-
}
|
|
97
|
-
} catch (e) {
|
|
98
|
-
ctx?.logger?.debug?.(`[turn-memory] 跳过(${e instanceof Error ? e.message : String(e)})`);
|
|
99
|
-
}
|
|
100
|
-
return next();
|
|
101
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* turn-memory.mjs — 回合结束把"她自己说过的话"沉淀进记忆库
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:现在的记忆只有"别人说了什么"(入站沉淀)。
|
|
5
|
+
* 结果她记不得自己答应过什么、说过什么梗 —— 而 Neuro 那种"像个人"的关键之一,
|
|
6
|
+
* 就是**记得自己说过的话**("你上次说教我钓鱼的")。
|
|
7
|
+
*
|
|
8
|
+
* 挂点:第六套(出站)。出站链每回合会多跑一次 `phase === 'turnEnd'` 的收尾,
|
|
9
|
+
* 正好当"回合结束"信号用,不需要插件再暴露新钩子。
|
|
10
|
+
*
|
|
11
|
+
* 省钱/防污染:
|
|
12
|
+
* · 只记 ≥ 12 字的正文(太短没信息量);
|
|
13
|
+
* · 纯媒体/纯表情不记;
|
|
14
|
+
* · 含"答应/保证/下次/回头"这类承诺词的标成 promise,召回声量(hits 权重)用得上;
|
|
15
|
+
* · 去重交给记忆库(cos > 0.92 视为同一条)。
|
|
16
|
+
*/
|
|
17
|
+
import { join } from 'node:path';
|
|
18
|
+
import { fileURLToPath } from 'node:url';
|
|
19
|
+
import { dirname } from 'node:path';
|
|
20
|
+
import { scopeOfTarget } from '../lib/scope-util.mjs';
|
|
21
|
+
|
|
22
|
+
export const name = 'turn-memory';
|
|
23
|
+
|
|
24
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
|
|
26
|
+
function dataRootOf(ctx) {
|
|
27
|
+
const env = String(process.env.QQBOT_MEMORY_DATA_ROOT || '').trim();
|
|
28
|
+
if (env) return env;
|
|
29
|
+
if (ctx && typeof ctx.dataRoot === 'string' && ctx.dataRoot) return ctx.dataRoot;
|
|
30
|
+
return dirname(dirname(HERE)); // .../{dataRoot}/.qqbot-extensions/outbound/
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function plainReply(text) {
|
|
34
|
+
return String(text || '')
|
|
35
|
+
.replace(/\[rf:[^\]]*\]/gi, '')
|
|
36
|
+
.replace(/\[MEDIA:[^\]]*\]/gi, '')
|
|
37
|
+
.replace(/!\[[^\]]*\]\([^)]*\)/g, '')
|
|
38
|
+
.replace(/<@[^>]*>/g, '')
|
|
39
|
+
.replace(/<qqbot-at-everyone\s*\/?>/gi, '')
|
|
40
|
+
.replace(/```[\s\S]*?```/g, ' ')
|
|
41
|
+
.replace(/\s+/g, ' ')
|
|
42
|
+
.trim();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 承诺/约定类:以后能被"你答应过我什么"召回 */
|
|
46
|
+
const PROMISE_RE = /(答应|保证|说好|下次|回头|以后|会给你|带你去|讲给你|教你|带你|给你)/;
|
|
47
|
+
|
|
48
|
+
/** 目标 → 记忆作用域(统一口径见 lib/scope-util.mjs) */
|
|
49
|
+
|
|
50
|
+
function worthKeeping(text) {
|
|
51
|
+
const t = String(text || '').trim();
|
|
52
|
+
if (t.length < 12) return false;
|
|
53
|
+
if (/^\[(图片|语音|视频|表情|文件)/.test(t)) return false;
|
|
54
|
+
if (!/[\u4e00-\u9fa5A-Za-z0-9]/.test(t)) return false;
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function onOutbound(ctx, next) {
|
|
59
|
+
// 只在回合收尾那一次动手,正文块照常放行
|
|
60
|
+
if (ctx?.phase !== 'turnEnd') return next();
|
|
61
|
+
|
|
62
|
+
let mem;
|
|
63
|
+
try {
|
|
64
|
+
mem = await import('../lib/memory.mjs');
|
|
65
|
+
} catch {
|
|
66
|
+
return next();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const cfg = mem.loadConfig(dataRootOf(ctx));
|
|
71
|
+
if (cfg.enabled === false || cfg.captureSelf === false) return next();
|
|
72
|
+
|
|
73
|
+
const text = plainReply(ctx?.text);
|
|
74
|
+
if (!worthKeeping(text)) return next();
|
|
75
|
+
|
|
76
|
+
const scope = scopeOfTarget(ctx?.target);
|
|
77
|
+
if (!scope) return next();
|
|
78
|
+
|
|
79
|
+
const kind = PROMISE_RE.test(text) ? 'promise' : 'say';
|
|
80
|
+
const r = await mem.remember(dataRootOf(ctx), scope, {
|
|
81
|
+
text: text.slice(0, 200),
|
|
82
|
+
kind,
|
|
83
|
+
subj: '小蓝',
|
|
84
|
+
}, cfg);
|
|
85
|
+
if (r && r.ok) {
|
|
86
|
+
ctx?.logger?.debug?.(`[turn-memory] 记住自己说的(${kind}):${text.slice(0, 24)}`);
|
|
87
|
+
}
|
|
88
|
+
} catch (e) {
|
|
89
|
+
ctx?.logger?.debug?.(`[turn-memory] 跳过(${e instanceof Error ? e.message : String(e)})`);
|
|
90
|
+
}
|
|
91
|
+
return next();
|
|
92
|
+
}
|
|
@@ -1,120 +1,178 @@
|
|
|
1
|
-
import { existsSync as __existsSync } from 'node:fs'
|
|
2
|
-
const __repoRoot = (() => {
|
|
3
|
-
let cached = ''
|
|
4
|
-
const norm = (p) => String(p || '').split(String.fromCharCode(92)).join('/')
|
|
5
|
-
return () => {
|
|
6
|
-
if (cached) return cached
|
|
7
|
-
const cands = []
|
|
8
|
-
const env = String(process.env.QQBOT_ROOT || process.env.QQBOT_REPO_ROOT || '').trim()
|
|
9
|
-
if (env) cands.push(norm(env))
|
|
10
|
-
try {
|
|
11
|
-
let here = decodeURIComponent(new URL('.', import.meta.url).pathname)
|
|
12
|
-
if (here[0] === '/' && here[2] === ':') here = here.slice(1)
|
|
13
|
-
here = norm(here).replace(/\/+$/, '')
|
|
14
|
-
let d = here
|
|
15
|
-
for (let i = 0; i < 6; i += 1) {
|
|
16
|
-
if (!d) break
|
|
17
|
-
cands.push(d)
|
|
18
|
-
const k = d.lastIndexOf('/')
|
|
19
|
-
d = k > 0 ? d.slice(0, k) : ''
|
|
20
|
-
}
|
|
21
|
-
} catch { /* noop */ }
|
|
22
|
-
try { cands.push(norm(process.cwd())) } catch { /* noop */ }
|
|
23
|
-
for (const c of cands) {
|
|
24
|
-
try {
|
|
25
|
-
if (c && (__existsSync(c + '/qqbot-mod/muu-shared.mjs') || __existsSync(c + '/dshqqbot'))) { cached = c; return cached }
|
|
26
|
-
} catch { /* noop */ }
|
|
27
|
-
}
|
|
28
|
-
cached = norm(cands.find(Boolean) || '.')
|
|
29
|
-
return cached
|
|
30
|
-
}
|
|
31
|
-
})()
|
|
32
|
-
/**
|
|
33
|
-
* memory.mjs — 让她自己决定"记什么"(长期记忆的写入端)
|
|
34
|
-
*
|
|
35
|
-
* 为什么需要它:入站扩展只能沉淀**原话**(episode),真正的记忆应该是**提炼后的事实**。
|
|
36
|
-
* 让她在对话里主动记一条,比机器猜准得多。
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
1
|
+
import { existsSync as __existsSync } from 'node:fs'
|
|
2
|
+
const __repoRoot = (() => {
|
|
3
|
+
let cached = ''
|
|
4
|
+
const norm = (p) => String(p || '').split(String.fromCharCode(92)).join('/')
|
|
5
|
+
return () => {
|
|
6
|
+
if (cached) return cached
|
|
7
|
+
const cands = []
|
|
8
|
+
const env = String(process.env.QQBOT_ROOT || process.env.QQBOT_REPO_ROOT || '').trim()
|
|
9
|
+
if (env) cands.push(norm(env))
|
|
10
|
+
try {
|
|
11
|
+
let here = decodeURIComponent(new URL('.', import.meta.url).pathname)
|
|
12
|
+
if (here[0] === '/' && here[2] === ':') here = here.slice(1)
|
|
13
|
+
here = norm(here).replace(/\/+$/, '')
|
|
14
|
+
let d = here
|
|
15
|
+
for (let i = 0; i < 6; i += 1) {
|
|
16
|
+
if (!d) break
|
|
17
|
+
cands.push(d)
|
|
18
|
+
const k = d.lastIndexOf('/')
|
|
19
|
+
d = k > 0 ? d.slice(0, k) : ''
|
|
20
|
+
}
|
|
21
|
+
} catch { /* noop */ }
|
|
22
|
+
try { cands.push(norm(process.cwd())) } catch { /* noop */ }
|
|
23
|
+
for (const c of cands) {
|
|
24
|
+
try {
|
|
25
|
+
if (c && (__existsSync(c + '/qqbot-mod/muu-shared.mjs') || __existsSync(c + '/dshqqbot'))) { cached = c; return cached }
|
|
26
|
+
} catch { /* noop */ }
|
|
27
|
+
}
|
|
28
|
+
cached = norm(cands.find(Boolean) || '.')
|
|
29
|
+
return cached
|
|
30
|
+
}
|
|
31
|
+
})()
|
|
32
|
+
/**
|
|
33
|
+
* memory.mjs — 让她自己决定"记什么"(长期记忆的写入端)
|
|
34
|
+
*
|
|
35
|
+
* 为什么需要它:入站扩展只能沉淀**原话**(episode),真正的记忆应该是**提炼后的事实**。
|
|
36
|
+
* 让她在对话里主动记一条,比机器猜准得多。
|
|
37
|
+
*
|
|
38
|
+
* ★ 2026-12-04 修(主人反馈"调用工具整理记忆没什么用"):
|
|
39
|
+
* 旧版 add 只写公共收件箱、list 却是**全库混读** —— 她看不到"这个会话里记了什么",
|
|
40
|
+
* 也没法判断哪条记错了;收件箱要等下一句消息才归位,写完立刻 list 还看不到。
|
|
41
|
+
* 现在工具能从 env.caps.peer 拿到当前会话({ scope, peerId }),于是:
|
|
42
|
+
* · add → 直接写当前 scope,写完立刻能 list 到;拿不到会话才退回收件箱;
|
|
43
|
+
* · list → 默认只看**当前会话**(scope='all' 才看全库),每条带会话标签/时间;
|
|
44
|
+
* · forget→ 支持按 id 或按一段原文定位删除(只在当前会话里找,避免误删别人的)。
|
|
45
|
+
*/
|
|
46
|
+
import { loadConfig, pushInbox, listAll, forgetAnywhere, remember } from '../lib/memory.mjs';
|
|
47
|
+
import { scopeOfTarget } from '../lib/scope-util.mjs';
|
|
48
|
+
|
|
49
|
+
const DATA_ROOT_FALLBACK = __repoRoot() + "/dshqqbot";
|
|
50
|
+
|
|
51
|
+
function dataRoot(env) {
|
|
52
|
+
const capsRoot = String(env?.caps?.dataRoot || '').trim();
|
|
53
|
+
if (capsRoot) return capsRoot;
|
|
54
|
+
const envRoot = String(process.env.QQBOT_MEMORY_DATA_ROOT || '').trim();
|
|
55
|
+
if (envRoot) return envRoot;
|
|
56
|
+
try {
|
|
57
|
+
const cfg = loadConfig(DATA_ROOT_FALLBACK);
|
|
58
|
+
return cfg.dataRoot || DATA_ROOT_FALLBACK;
|
|
59
|
+
} catch {
|
|
60
|
+
return DATA_ROOT_FALLBACK;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 当前会话 scope(拿不到就空串) */
|
|
65
|
+
function currentScope(env) {
|
|
66
|
+
const peer = env?.caps?.peer;
|
|
67
|
+
// scope 为空时不猜群/私聊:猜错比不知道更糟,宁可退回收件箱/全库
|
|
68
|
+
if (!peer || !peer.scope || !peer.peerId) return '';
|
|
69
|
+
return scopeOfTarget({ scope: peer.scope, targetId: peer.peerId });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function scopeLabel(scope) {
|
|
73
|
+
const s = String(scope || '');
|
|
74
|
+
if (s === '__global__') return '全局'
|
|
75
|
+
if (s.startsWith('group:')) return '群聊'
|
|
76
|
+
if (s.startsWith('c2c:')) return '私聊'
|
|
77
|
+
if (s.startsWith('wechat:')) return '微信'
|
|
78
|
+
return s || '未知'
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function fmtTs(ts) {
|
|
82
|
+
const n = Number(ts) || 0;
|
|
83
|
+
if (!n) return '';
|
|
84
|
+
const d = new Date(n);
|
|
85
|
+
const p = (x) => String(x).padStart(2, '0');
|
|
86
|
+
return `${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const KINDS = new Set(['fact', 'event', 'preference', 'relation']);
|
|
90
|
+
|
|
91
|
+
export default {
|
|
92
|
+
name: 'memory',
|
|
93
|
+
// description 刻意写短 —— 工具 schema 是每请求的固定开销
|
|
94
|
+
description: '长期记忆:add=记住一条 / list=看这个会话记了什么 / forget=删掉记错的。'
|
|
95
|
+
+ '只记"以后会用到"的:人的事实、她的承诺、群里的约定、她对某人的态度。闲聊原话不用记。',
|
|
96
|
+
inputSchema: {
|
|
97
|
+
action: { type: 'string', required: true, description: 'add | list | forget' },
|
|
98
|
+
text: { type: 'string', description: 'add 必填:一句话,≤80 字,写成陈述句("妈妈养了只猫叫年糕");forget 可选:按原文片段找要删的那条' },
|
|
99
|
+
kind: { type: 'string', description: 'add 可选:fact=事实(默认) / event=发生过的事 / preference=偏好 / relation=对某人的态度' },
|
|
100
|
+
subj: { type: 'string', description: 'add 可选:这条记的是谁(昵称)' },
|
|
101
|
+
id: { type: 'string', description: 'forget 必填(或改用 text):记忆 id(list 里显示)' },
|
|
102
|
+
limit: { type: 'number', description: 'list 可选:最多几条,默认 20' },
|
|
103
|
+
scope: { type: 'string', description: "list 可选:here=只看当前会话(默认) / all=看全库" },
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
run: async (args, env) => {
|
|
107
|
+
const log = env?.logger || console;
|
|
108
|
+
const action = String(args?.action || '').trim();
|
|
109
|
+
const root = dataRoot(env);
|
|
110
|
+
const here = currentScope(env);
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
if (action === 'add') {
|
|
114
|
+
const text = String(args?.text || '').trim();
|
|
115
|
+
if (!text) return { ok: false, msg: 'text 不能为空' };
|
|
116
|
+
if (text.length > 120) return { ok: false, msg: `太长(${text.length} 字)—— 压到 80 字以内再记` };
|
|
117
|
+
const kind = KINDS.has(args?.kind) ? args.kind : 'fact';
|
|
118
|
+
const subj = String(args?.subj || '').trim();
|
|
119
|
+
// 知道当前会话就直接写进去(旧版一律进收件箱,要等下一条消息才归位)
|
|
120
|
+
if (here) {
|
|
121
|
+
const r = await remember(root, here, { text, kind, subj }, loadConfig(root));
|
|
122
|
+
if (r && r.ok) {
|
|
123
|
+
const dup = /dedup/i.test(String(r.reason || ''));
|
|
124
|
+
return { ok: true, msg: `${dup ? '这条之前记过,已加强' : '记下了'}(${scopeLabel(here)}·${kind}):${text}` };
|
|
125
|
+
}
|
|
126
|
+
// 写失败(如后端不可用)再退回收件箱,别把话吞掉
|
|
127
|
+
}
|
|
128
|
+
const r2 = pushInbox(root, { text, kind, subj });
|
|
129
|
+
if (!r2.ok) return { ok: false, msg: `没记住:${r2.reason}` };
|
|
130
|
+
return { ok: true, msg: `记下了(${kind}):${text}(下条消息归到当前会话)` };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (action === 'list') {
|
|
134
|
+
const limit = Math.max(1, Math.min(100, Number(args?.limit) || 20));
|
|
135
|
+
// 拿不到当前会话时只能看全库,并在抬头里说明,不能假装"这个会话"
|
|
136
|
+
const all = String(args?.scope || '').trim().toLowerCase() === 'all' || !here;
|
|
137
|
+
let items = listAll(root, { limit: all ? limit : Math.min(500, limit * 5), scopeFilter: all ? '' : here });
|
|
138
|
+
if (!all && here) items = items.filter((x) => x.scope === here);
|
|
139
|
+
items = items.slice(0, limit);
|
|
140
|
+
if (items.length === 0) {
|
|
141
|
+
return { ok: true, msg: all ? '记忆库还是空的。' : `这个会话还没有记忆(${here})。` };
|
|
142
|
+
}
|
|
143
|
+
const lines = items.map((x, i) => `${i + 1}. [${x.id}] ${scopeLabel(x.scope)}·${x.kind || '?'} ${fmtTs(x.ts)}${x.subj ? '(' + x.subj + ')' : ''}:${x.text}`);
|
|
144
|
+
const head = all
|
|
145
|
+
? `全库最近 ${items.length} 条${here ? '' : '(当前会话未知,按全库列出)'}:`
|
|
146
|
+
: `这个会话最近 ${items.length} 条(${here}):`;
|
|
147
|
+
return { ok: true, msg: head + '\n' + lines.join('\n') };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (action === 'forget') {
|
|
151
|
+
const id = String(args?.id || '').trim();
|
|
152
|
+
const needle = String(args?.text || '').trim();
|
|
153
|
+
if (!id && !needle) return { ok: false, msg: 'id 或 text 至少给一个(先 list 看)' };
|
|
154
|
+
if (id) {
|
|
155
|
+
const ok = forgetAnywhere(root, id);
|
|
156
|
+
return { ok, msg: ok ? `已删 ${id}` : `没找到 ${id}(先 list 看 id)` };
|
|
157
|
+
}
|
|
158
|
+
// 按原文找:只在本会话里找,避免误删别人的记忆
|
|
159
|
+
const pool = listAll(root, { limit: 500, scopeFilter: here });
|
|
160
|
+
const hit = pool.find((x) => String(x.text || '').includes(needle) && (!here || x.scope === here));
|
|
161
|
+
if (!hit) return { ok: false, msg: `本会话里没找到包含「${needle}」的记忆` };
|
|
162
|
+
const ok = forgetAnywhere(root, hit.id);
|
|
163
|
+
return { ok, msg: ok ? `已删 [${hit.id}] ${hit.text}` : `删除失败:${hit.id}` };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return { ok: false, msg: `不认识的 action: ${action}(add | list | forget)` };
|
|
167
|
+
} catch (e) {
|
|
168
|
+
log.warn?.(`[memory-tool] 异常: ${e instanceof Error ? e.message : String(e)}`);
|
|
169
|
+
return { ok: false, msg: '记忆操作出错了(已忽略,不影响聊天)' };
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/** 便于外部脚本复用:把一个 scope 的记忆直接灌进去(不做收件箱) */
|
|
175
|
+
export async function rememberInto(root, scope, text, kind = 'fact', subj = '') {
|
|
176
|
+
const cfg = loadConfig(root);
|
|
177
|
+
return remember(root, scope, { text, kind, subj }, cfg);
|
|
178
|
+
}
|