dsh-qqbot-muu 0.1.15 → 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.
Files changed (171) hide show
  1. package/CHANGELOG.md +168 -85
  2. package/client/qqbot-settings.js +13401 -12024
  3. package/config/modules.json +212 -0
  4. package/dist/channel-tools.js +21 -10
  5. package/dist/config.d.ts +14 -0
  6. package/dist/config.js +573 -449
  7. package/dist/features/contextless-store.js +247 -247
  8. package/dist/features/contextless-trim.js +455 -34
  9. package/dist/features/four-source.js +338 -336
  10. package/dist/features/group-join-request.js +289 -287
  11. package/dist/features/local-models.js +1494 -0
  12. package/dist/features/rotate-summarizer.js +438 -0
  13. package/dist/features/scenario-detect.js +321 -0
  14. package/dist/features/scheduler.js +8 -1
  15. package/dist/features/session-rotate.js +499 -0
  16. package/dist/features/session-title.js +199 -0
  17. package/dist/features/style-layer.js +501 -0
  18. package/dist/features/thinking-log.js +71 -69
  19. package/dist/gateway/bootstrap.js +1704 -1388
  20. package/dist/gateway/data-root.js +77 -3
  21. package/dist/gateway/middleware-setup.js +6 -3
  22. package/dist/index.js +4 -2
  23. package/dist/model/settings-reader.js +193 -102
  24. package/dist/session/session-manager.js +1730 -1587
  25. package/dist/shared/paths.js +128 -0
  26. package/dist/shared/settings-io.js +79 -0
  27. package/dist/transport/inbound.js +20 -1
  28. package/dist/transport/outbound.js +56 -4
  29. package/docs/RELEASE_CHECKLIST.md +24 -2
  30. package/dshqqbot/.qqbot-extensions/hooks/interview-panel.mjs +41 -1
  31. package/dshqqbot/.qqbot-extensions/inbound/00-vision.mjs +111 -0
  32. package/dshqqbot/.qqbot-extensions/inbound/aa-memory-recall.mjs +802 -689
  33. package/dshqqbot/.qqbot-extensions/inbound/ac-image-see.mjs +1 -1
  34. package/dshqqbot/.qqbot-extensions/inbound/az-feedback.mjs +113 -115
  35. package/dshqqbot/.qqbot-extensions/inbound/local-answer.mjs +689 -568
  36. package/dshqqbot/.qqbot-extensions/lib/.pet-form.json +1 -1
  37. package/dshqqbot/.qqbot-extensions/lib/draw-core.mjs +238 -7
  38. package/dshqqbot/.qqbot-extensions/lib/local-llm.mjs +455 -394
  39. package/dshqqbot/.qqbot-extensions/lib/memory-agent.mjs +930 -928
  40. package/dshqqbot/.qqbot-extensions/lib/memory-core.mjs +537 -537
  41. package/dshqqbot/.qqbot-extensions/lib/memory-db.mjs +2476 -2245
  42. package/dshqqbot/.qqbot-extensions/lib/memory-hippo.mjs +18 -0
  43. package/dshqqbot/.qqbot-extensions/lib/memory.mjs +191 -186
  44. package/dshqqbot/.qqbot-extensions/lib/plugin-dir.mjs +14 -4
  45. package/dshqqbot/.qqbot-extensions/lib/scope-util.mjs +84 -0
  46. package/dshqqbot/.qqbot-extensions/lib/vision.mjs +1 -1
  47. package/dshqqbot/.qqbot-extensions/outbound/aa-sanitize.mjs +158 -154
  48. package/dshqqbot/.qqbot-extensions/outbound/ac-media-fix.mjs +5 -1
  49. package/dshqqbot/.qqbot-extensions/outbound/memory-agent.mjs +121 -130
  50. package/dshqqbot/.qqbot-extensions/outbound/split-reply.mjs +18 -2
  51. package/dshqqbot/.qqbot-extensions/outbound/turn-memory.mjs +92 -101
  52. package/dshqqbot/.qqbot-extensions/tools/draw.mjs +22 -1
  53. package/dshqqbot/.qqbot-extensions/tools/memory.mjs +178 -120
  54. package/dshqqbot/.qqbot-extensions/tools/pet_card.mjs +3 -2
  55. package/dshqqbot/.qqbot-extensions/tools/storyboard.mjs +3 -1
  56. package/dshqqbot/.qqbot-extensions/vendor/abogus/test-abogus.mjs +128 -126
  57. package/package.json +119 -111
  58. package/scripts/__pycache__/fan-mode.cpython-311.pyc +0 -0
  59. package/scripts/__pycache__/memory-audit.cpython-311.pyc +0 -0
  60. package/scripts/__pycache__/qq-ip-sync.cpython-311.pyc +0 -0
  61. package/scripts/__pycache__/qq-whitelist.cpython-311.pyc +0 -0
  62. package/scripts/_add_maintenance_cfg.py +20 -0
  63. package/scripts/_bench_keeplease.sh +10 -0
  64. package/scripts/_dbg_purge.py +15 -0
  65. package/scripts/_elev.sh +6 -0
  66. package/scripts/_fix_all_admin.cmd +16 -0
  67. package/scripts/_livecheck-image.mjs +33 -0
  68. package/scripts/_livecheck-llm.mjs +34 -0
  69. package/scripts/_livecheck-tts.mjs +37 -0
  70. package/scripts/_patch_curve.py +88 -0
  71. package/scripts/_patch_fan_thermal.py +24 -0
  72. package/scripts/_tag_tmp.py +13 -0
  73. package/scripts/adb-broker-client.mjs +87 -0
  74. package/scripts/adb-broker.mjs +205 -0
  75. package/scripts/ai-mode.cmd +6 -0
  76. package/scripts/ai-mode.py +387 -0
  77. package/scripts/ai-toolkit.py +607 -0
  78. package/scripts/apply-fan-gpu45.py +51 -0
  79. package/scripts/bench-fans.py +515 -0
  80. package/scripts/bench-load-direct.py +63 -0
  81. package/scripts/boot-tasks-hidden.vbs +20 -0
  82. package/scripts/boot-tasks.cmd +277 -0
  83. package/scripts/chat-archive/__pycache__/build-sft-v4.cpython-311.pyc +0 -0
  84. package/scripts/chat-archive/__pycache__/build-sft.cpython-311.pyc +0 -0
  85. package/scripts/chat-archive/build-sft-v4.py +566 -0
  86. package/scripts/chat-archive/build-sft.py +390 -0
  87. package/scripts/chat-archive/eval-sft.py +155 -0
  88. package/scripts/chat-archive/extract-memory.py +143 -0
  89. package/scripts/chat-archive/inventory.py +182 -0
  90. package/scripts/chat-archive/merge-sft.py +114 -0
  91. package/scripts/chat-archive/qce-pull.py +217 -0
  92. package/scripts/chat-archive/qqheader.py +110 -0
  93. package/scripts/chat-archive/qqkey2.py +113 -0
  94. package/scripts/chat-archive/qqprobe.py +153 -0
  95. package/scripts/chat-archive/screen-collect.py +163 -0
  96. package/scripts/chat-archive/sqlcipher_probe.py +444 -0
  97. package/scripts/comfy_gen.py +209 -0
  98. package/scripts/compose-video.py +168 -0
  99. package/scripts/dl_models.py +45 -0
  100. package/scripts/draw-now.cmd +18 -0
  101. package/scripts/dsh-instance-guard.mjs +20 -2
  102. package/scripts/elev-extra.cmd +25 -0
  103. package/scripts/episode.py +529 -0
  104. package/scripts/fan-curve.py +218 -0
  105. package/scripts/fan-guard.cmd +8 -0
  106. package/scripts/fan-mode.py +380 -0
  107. package/scripts/fan_manual_safe.py +33 -0
  108. package/scripts/fan_restore_manual.py +46 -0
  109. package/scripts/fc_curves.py +359 -0
  110. package/scripts/fix-fan-and-timers.cmd +39 -0
  111. package/scripts/fix_admin_all.py +94 -0
  112. package/scripts/flows.mjs +224 -0
  113. package/scripts/gateway.cmd +126 -0
  114. package/scripts/gen-image.py +420 -0
  115. package/scripts/gen-video.py +186 -0
  116. package/scripts/git-askpass.cmd +2 -2
  117. package/scripts/gpu-mode.py +195 -0
  118. package/scripts/hw-profile.py +175 -0
  119. package/scripts/install-schedule.cmd +37 -0
  120. package/scripts/install-watch.cmd +29 -0
  121. package/scripts/matte-cutout.py +106 -0
  122. package/scripts/media-enhance.py +402 -0
  123. package/scripts/memory-audit.py +581 -0
  124. package/scripts/migrate-scope.mjs +140 -139
  125. package/scripts/model-registry.py +519 -0
  126. package/scripts/openid-audit.mjs +219 -0
  127. package/scripts/pull-gguf.py +127 -0
  128. package/scripts/purge-standby.py +151 -0
  129. package/scripts/qq-connect-login.py +133 -0
  130. package/scripts/qq-ip-sync-hidden.vbs +5 -0
  131. package/scripts/qq-ip-sync.py +300 -0
  132. package/scripts/qq-login-once.cmd +20 -0
  133. package/scripts/qq-login-qr.py +72 -0
  134. package/scripts/qq-whitelist-fix.cmd +27 -0
  135. package/scripts/qq-whitelist-login.cmd +26 -0
  136. package/scripts/qq-whitelist.cmd +12 -0
  137. package/scripts/qq-whitelist.py +1452 -0
  138. package/scripts/quality-compare.py +134 -0
  139. package/scripts/release-all.mjs +636 -430
  140. package/scripts/release-check.mjs +244 -226
  141. package/scripts/resume-coder.cmd +38 -0
  142. package/scripts/secure-room/index.html +334 -0
  143. package/scripts/secure-room/server.py +582 -0
  144. package/scripts/security-check.mjs +164 -0
  145. package/scripts/selftest-local-llm.mjs +20 -0
  146. package/scripts/selftest-local-models.mjs +754 -0
  147. package/scripts/selftest-rotate-style.mjs +308 -0
  148. package/scripts/shutdown-s5-optional.cmd +21 -0
  149. package/scripts/sleep-2325.cmd +75 -0
  150. package/scripts/start-comfyui.cmd +5 -0
  151. package/scripts/start-strata.cmd +17 -0
  152. package/scripts/start-strata.vbs +11 -0
  153. package/scripts/start-watch-hidden.vbs +3 -0
  154. package/scripts/stickers/auto-tag.mjs +161 -0
  155. package/scripts/storage.mjs +236 -0
  156. package/scripts/sync-plugin-copies.mjs +98 -23
  157. package/scripts/tests/test-qq-whitelist.py +192 -0
  158. package/scripts/train/__pycache__/train-lora.cpython-311.pyc +0 -0
  159. package/scripts/train/gguf-quantize.py +92 -0
  160. package/scripts/train/gguf-to-hf.py +112 -0
  161. package/scripts/train/merge-lora.py +51 -0
  162. package/scripts/train/probe-mirrors.sh +12 -0
  163. package/scripts/train/probe-mirrors2.sh +20 -0
  164. package/scripts/train/setup-wsl.sh +23 -0
  165. package/scripts/train/setup-wsl2.sh +25 -0
  166. package/scripts/train/setup-wsl3.sh +22 -0
  167. package/scripts/train/setup-wsl4.sh +22 -0
  168. package/scripts/train/train-lora.py +319 -0
  169. package/scripts/vram-free.py +104 -0
  170. package/scripts/wechat-full-pull.mjs +1 -1
  171. package/settings-host.js +1047 -12
@@ -0,0 +1,501 @@
1
+ /**
2
+ * style-layer.js — 「去鱼味 / 去 AI 味 / 违禁词 / 梗指南」风格层(2026-11-25 任务 C)
3
+ *
4
+ * 三件事:
5
+ * 1) 出站后处理:把「首先/其次/综上所述/希望对你有帮助/很高兴为你/话不多说/值得一提的是/总的来说/这个嘛」
6
+ * 这类八股腔、客服腔、AI 腔**削掉**(只削腔调,不按语义改写;代码块/链接/媒体标记不动)。
7
+ * 2) 违禁词:默认表 + 用户表,落 `{dataRoot}/.qqbot/style/banned.json`、`filler.json`,按 mtime 热加载;
8
+ * 命中 `replace` 就替换、`strip` 就抹掉、`flag` 只记账(交给提示词侧避免,不硬改正文)。
9
+ * 3) 梗指南:`{dataRoot}/.qqbot/style/memes.json`(梗/出处/适用场景/慎用),
10
+ * `findMemes` 按当前情境打分检索,`buildStylePrompt` 只注入 1~2 条候选,不全量灌。
11
+ *
12
+ * 设计原则:
13
+ * · 后处理**非破坏性**:保护 ```代码块```、`行内代码`、markdown 图、[MEDIA:]/[STICKER:]/[rf:]/[RECALL]、URL,
14
+ * 一个都不碰;只动自然语言散文段。
15
+ * · 没命中词表时原样返回(不产生空改动,免得日志噪音)。
16
+ * · 全部离线、纯 node、无外部依赖 —— 可以直接在自测脚本里 import。
17
+ */
18
+ import { mkdirSync, readFileSync, statSync, writeFileSync, renameSync } from 'node:fs';
19
+ import { join } from 'node:path';
20
+
21
+ /** 风格数据目录(相对数据根) */
22
+ export const STYLE_SUBDIR = join('.qqbot', 'style');
23
+ export const STYLE_FILES = ['banned.json', 'filler.json', 'memes.json'];
24
+
25
+ /** 默认「八股/客服/AI 腔」软化表(只在后处理里删/换,不改语义) */
26
+ export const DEFAULT_FILLERS = [
27
+ // 逻辑连接词(结构性词,人设里已禁)
28
+ { p: '首先,', to: '', note: '去掉「首先」' },
29
+ { p: '首先', to: '' },
30
+ { p: '其次,', to: '' },
31
+ { p: '其次', to: '' },
32
+ { p: '再者,', to: '还有,' },
33
+ { p: '综上所述,', to: '' },
34
+ { p: '综上所述', to: '' },
35
+ { p: '总的来说,', to: '' },
36
+ { p: '总的来说', to: '' },
37
+ { p: '总而言之,', to: '反正' },
38
+ { p: '值得一提的是,', to: '' },
39
+ { p: '值得一提的是', to: '' },
40
+ { p: '需要注意的是,', to: '' },
41
+ { p: '需要注意的是', to: '' },
42
+ { p: '话不多说,', to: '' },
43
+ { p: '话不多说', to: '' },
44
+ { p: '具体来说,', to: '' },
45
+ { p: '简单来说,', to: '就是说,' },
46
+ { p: '与此同时,', to: '' },
47
+ { p: '另外,', to: '还有,' },
48
+ { p: '最后,', to: '' },
49
+ { p: '最后', to: '' },
50
+ // 客服腔
51
+ { p: '希望对你有帮助', to: '' },
52
+ { p: '希望对你有所帮助', to: '' },
53
+ { p: '很高兴为你服务', to: '' },
54
+ { p: '很高兴为您服务', to: '' },
55
+ { p: '很高兴为你解答', to: '' },
56
+ { p: '很高兴为您解答', to: '' },
57
+ { p: '很高兴为你', to: '' },
58
+ { p: '很高兴为您', to: '' },
59
+ { p: '有什么可以帮你', to: '咋了' },
60
+ { p: '有什么可以帮您', to: '咋了' },
61
+ { p: '有什么可以帮你吗', to: '咋了' },
62
+ { p: '有什么可以帮您吗', to: '咋了' },
63
+ { p: '还需要什么帮助吗', to: '' },
64
+ { p: '请问还有什么', to: '' },
65
+ { p: '这个嘛', to: '唔' },
66
+ { p: '这个嘛,', to: '唔,' },
67
+ { p: '需要注意的是', to: '' },
68
+ { p: '希望能帮到你', to: '' },
69
+ { p: '如果你需要的话,我可以', to: '要我' },
70
+ { p: '如果你有任何问题', to: '有啥问题' },
71
+ { p: '请随时告诉我', to: '跟我说' },
72
+ { p: '如有任何疑问', to: '有啥不懂的' },
73
+ // AI 腔
74
+ { p: '作为一个AI', to: '' },
75
+ { p: '作为一个 AI', to: '' },
76
+ { p: '作为AI', to: '' },
77
+ { p: '作为 AI', to: '' },
78
+ { p: '我理解你的感受', to: '啊这' },
79
+ { p: '让我们一起来', to: '' },
80
+ { p: '不妨我们', to: '' },
81
+ ];
82
+
83
+ /** 默认违禁词(AI 自曝 + 客服模板;默认仅这两类,别误伤正常聊天) */
84
+ export const DEFAULT_BANNED = [
85
+ { w: '作为一个AI', to: '', mode: 'strip', note: 'AI 自曝' },
86
+ { w: '作为一个 AI', to: '', mode: 'strip' },
87
+ { w: '作为AI', to: '', mode: 'strip' },
88
+ { w: '作为 AI', to: '', mode: 'strip' },
89
+ { w: '我是AI', to: '', mode: 'strip' },
90
+ { w: '我是 AI', to: '', mode: 'strip' },
91
+ { w: '人工智能', to: '', mode: 'strip', note: '容易显得不是人' },
92
+ { w: '语言模型', to: '', mode: 'strip' },
93
+ { w: '大语言模型', to: '', mode: 'strip' },
94
+ { w: 'AI助手', to: '', mode: 'strip' },
95
+ { w: 'AI 助手', to: '', mode: 'strip' },
96
+ { w: '智能助手', to: '', mode: 'strip' },
97
+ { w: '聊天机器人', to: '', mode: 'strip' },
98
+ { w: '虚拟助手', to: '', mode: 'strip' },
99
+ { w: '系统提示词', to: '', mode: 'strip' },
100
+ { w: '系统提示', to: '', mode: 'strip', note: '内部词' },
101
+ { w: '提示词', to: '', mode: 'flag', note: '技术话题里可能是正常词 → 只记账' },
102
+ { w: '上下文窗口', to: '', mode: 'flag' },
103
+ { w: '作为一个语言模型', to: '我', mode: 'replace' },
104
+ { w: '别难过,会好起来的', to: '', mode: 'flag', note: '空话,交给提示词侧' },
105
+ ];
106
+
107
+ /** 起步梗库(中文互联网常用、不冒犯;risk=low 可放心用,mid 要看人) */
108
+ export const DEFAULT_MEMES = [
109
+ {
110
+ id: 'pofang', meme: '破防了', origin: '直播间/电竞圈,指心理防线被击穿、绷不住',
111
+ when: ['被戳中', '绷不住', '难受', '破防'], risk: 'low', tags: ['情绪'],
112
+ },
113
+ {
114
+ id: 'nanbeng', meme: '难绷', origin: '抽象话,忍不住笑 / 实在看不下去',
115
+ when: ['离谱', '好笑', '无语', '绷不住'], risk: 'low', tags: ['反应'],
116
+ },
117
+ {
118
+ id: 'bengbu', meme: '蚌埠住了', origin: '谐音「绷不住了」,贴吧热梗',
119
+ when: ['好笑', '绷不住', '离谱'], risk: 'low', tags: ['反应'],
120
+ },
121
+ {
122
+ id: 'azhe', meme: '啊这', origin: '万能无语反应,接不住话时的台阶',
123
+ when: ['无语', '尴尬', '接不住', '沉默'], risk: 'low', tags: ['反应'],
124
+ },
125
+ {
126
+ id: 'zhengbuhui', meme: '整不会了', origin: '东北话,「把我整懵了」',
127
+ when: ['懵', '搞不懂', '意外', '迷惑'], risk: 'low', tags: ['反应'],
128
+ },
129
+ {
130
+ id: 'taidian', meme: '太典了', origin: '贴吧「典」,形容典型到离谱;带一点阴阳',
131
+ when: ['套路', '双标', '典型'], risk: 'mid', tags: ['阴阳'],
132
+ },
133
+ ];
134
+
135
+ const RISK_ORDER = { low: 0, mid: 1, high: 2 };
136
+
137
+ /** {dataRoot}/.qqbot/style */
138
+ export function styleDirOf(dataRoot) {
139
+ return join(String(dataRoot ?? '.'), STYLE_SUBDIR);
140
+ }
141
+
142
+ function readJson(path, fallback) {
143
+ try {
144
+ return JSON.parse(readFileSync(path, 'utf8'));
145
+ }
146
+ catch {
147
+ return fallback;
148
+ }
149
+ }
150
+
151
+ function writeJsonIfAbsent(path, value, created) {
152
+ try {
153
+ statSync(path);
154
+ return false;
155
+ }
156
+ catch { /* 不存在才写 */ }
157
+ try {
158
+ // 原子写:设置页可能同时在读这份文件,直接覆盖会读到半个 JSON
159
+ const tmp = `${path}.${process.pid}.tmp`;
160
+ writeFileSync(tmp, JSON.stringify({ v: 1, updatedAt: new Date().toISOString(), ...value }, null, 2) + '\n', 'utf8');
161
+ renameSync(tmp, path);
162
+ created.push(path);
163
+ return true;
164
+ }
165
+ catch {
166
+ return false;
167
+ }
168
+ }
169
+
170
+ /** 首次使用把默认表落盘(已存在绝不覆盖 —— 用户改过的东西不能被默认值盖掉) */
171
+ export function ensureStyleFiles(dataRoot) {
172
+ const dir = styleDirOf(dataRoot);
173
+ const created = [];
174
+ try {
175
+ mkdirSync(dir, { recursive: true });
176
+ }
177
+ catch (err) {
178
+ return { dir, created, error: err instanceof Error ? err.message : String(err) };
179
+ }
180
+ writeJsonIfAbsent(join(dir, 'filler.json'), { softeners: DEFAULT_FILLERS.map((x) => ({ p: x.p, to: x.to })) }, created);
181
+ writeJsonIfAbsent(join(dir, 'banned.json'), { banned: DEFAULT_BANNED.map((x) => ({ w: x.w, to: x.to, mode: x.mode })) }, created);
182
+ writeJsonIfAbsent(join(dir, 'memes.json'), { memes: DEFAULT_MEMES }, created);
183
+ return { dir, created };
184
+ }
185
+
186
+ /** 归一化三张表:容忍用户只写了数组 / 少字段 */
187
+ function normalizeTables(fillerRaw, bannedRaw, memesRaw) {
188
+ const fillerList = Array.isArray(fillerRaw?.softeners) ? fillerRaw.softeners
189
+ : Array.isArray(fillerRaw) ? fillerRaw : [];
190
+ const bannedList = Array.isArray(bannedRaw?.banned) ? bannedRaw.banned
191
+ : Array.isArray(bannedRaw) ? bannedRaw : [];
192
+ const memesList = Array.isArray(memesRaw?.memes) ? memesRaw.memes
193
+ : Array.isArray(memesRaw) ? memesRaw : [];
194
+ const softeners = fillerList
195
+ .map((x) => ({ p: String(x?.p ?? x?.w ?? '').trim(), to: String(x?.to ?? '') }))
196
+ .filter((x) => x.p);
197
+ const banned = bannedList
198
+ .map((x) => ({
199
+ w: String(x?.w ?? x?.p ?? '').trim(),
200
+ to: String(x?.to ?? ''),
201
+ mode: ['replace', 'strip', 'flag'].includes(String(x?.mode)) ? String(x.mode) : 'strip',
202
+ }))
203
+ .filter((x) => x.w);
204
+ const memes = memesList
205
+ .map((x) => ({
206
+ id: String(x?.id ?? x?.meme ?? '').trim(),
207
+ meme: String(x?.meme ?? x?.name ?? '').trim(),
208
+ origin: String(x?.origin ?? ''),
209
+ when: Array.isArray(x?.when) ? x.when.map((s) => String(s)) : String(x?.when ?? '').split(/[,,|]/).map((s) => s.trim()).filter(Boolean),
210
+ risk: ['low', 'mid', 'high'].includes(String(x?.risk)) ? String(x.risk) : 'low',
211
+ tags: Array.isArray(x?.tags) ? x.tags.map((s) => String(s)) : [],
212
+ }))
213
+ .filter((x) => x.id && x.meme);
214
+ return { softeners, banned, memes };
215
+ }
216
+
217
+ /** 热加载缓存:按三个文件的 mtime 判失效 */
218
+ const _cache = new Map(); // dir -> {stamp, tables}
219
+ function stampOf(dir) {
220
+ const out = [];
221
+ for (const f of STYLE_FILES) {
222
+ try {
223
+ const st = statSync(join(dir, f));
224
+ out.push(f + ':' + st.mtimeMs + ':' + st.size);
225
+ }
226
+ catch {
227
+ out.push(f + ':-');
228
+ }
229
+ }
230
+ return out.join('|');
231
+ }
232
+
233
+ /**
234
+ * 读三张表(带 mtime 热加载;文件缺失自动补默认,不覆盖已存在文件)。
235
+ * @returns {{dir:string, softeners:Array, banned:Array, memes:Array, reloaded:boolean, created:string[]}}
236
+ */
237
+ export function loadStyleTables(dataRoot) {
238
+ const dir = styleDirOf(dataRoot);
239
+ const stamp = stampOf(dir);
240
+ const hit = _cache.get(dir);
241
+ if (hit && hit.stamp === stamp)
242
+ return { ...hit.tables, dir, reloaded: false, created: [] };
243
+ const { created } = ensureStyleFiles(dataRoot);
244
+ const fillerRaw = readJson(join(dir, 'filler.json'), { softeners: DEFAULT_FILLERS });
245
+ const bannedRaw = readJson(join(dir, 'banned.json'), { banned: DEFAULT_BANNED });
246
+ const memesRaw = readJson(join(dir, 'memes.json'), { memes: DEFAULT_MEMES });
247
+ const t = normalizeTables(fillerRaw, bannedRaw, memesRaw);
248
+ const tables = {
249
+ softeners: t.softeners.length > 0 ? t.softeners : DEFAULT_FILLERS.map((x) => ({ p: x.p, to: x.to })),
250
+ banned: t.banned.length > 0 ? t.banned : DEFAULT_BANNED.map((x) => ({ w: x.w, to: x.to, mode: x.mode })),
251
+ memes: t.memes.length > 0 ? t.memes : DEFAULT_MEMES,
252
+ };
253
+ // 用户在内联配置里补的词表(bannedWordsExtra)也并进来(不落盘,热生效)
254
+ _cache.set(dir, { stamp, tables });
255
+ return { ...tables, dir, reloaded: !!hit, created };
256
+ }
257
+
258
+ /** 把配置里内联的额外违禁词并进表(每次调用现算,避免写盘) */
259
+ function withExtraBanned(tables, config) {
260
+ const extra = Array.isArray(config?.bannedWordsExtra) ? config.bannedWordsExtra : [];
261
+ if (extra.length === 0)
262
+ return tables;
263
+ const seen = new Set(tables.banned.map((b) => b.w));
264
+ const add = [];
265
+ for (const w of extra) {
266
+ const word = String(w ?? '').trim();
267
+ if (!word || seen.has(word))
268
+ continue;
269
+ seen.add(word);
270
+ add.push({ w: word, to: '', mode: 'strip', note: 'config.bannedWordsExtra' });
271
+ }
272
+ return { ...tables, banned: tables.banned.concat(add) };
273
+ }
274
+
275
+ /* ───────────── 保护段:代码块/行内码/图/媒体标记/URL 一律不动 ───────────── */
276
+ const PROTECT_RES = [
277
+ /```[\s\S]*?```/g,
278
+ /`[^`\n]+`/g,
279
+ /!\[[^\]]*\]\([^)\s]+\)/g,
280
+ /\[MEDIA:[^\]]*\]/gi,
281
+ /\[STICKER:[^\]]*\]/gi,
282
+ /\[rf:[^\]]*\]/gi,
283
+ /\[RECALL\]/g,
284
+ /https?:\/\/[^\s))]+/g,
285
+ ];
286
+
287
+ function protect(text) {
288
+ const saved = [];
289
+ let out = text;
290
+ for (const re of PROTECT_RES) {
291
+ out = out.replace(re, (m) => {
292
+ saved.push(m);
293
+ return '\u0000' + (saved.length - 1) + '\u0000';
294
+ });
295
+ }
296
+ return { out, saved };
297
+ }
298
+
299
+ function restore(text, saved) {
300
+ return text.replace(/\u0000(\d+)\u0000/g, (_m, i) => saved[Number(i)] ?? '');
301
+ }
302
+
303
+ function countOccurrences(hay, needle) {
304
+ if (!needle)
305
+ return 0;
306
+ let n = 0;
307
+ let i = 0;
308
+ for (;;) {
309
+ const j = hay.indexOf(needle, i);
310
+ if (j < 0)
311
+ return n;
312
+ n += 1;
313
+ i = j + needle.length;
314
+ }
315
+ }
316
+
317
+ /** 削完腔调后的清场:去空标点、去多余空白、去行首逗号 */
318
+ function tidy(s) {
319
+ let t = s;
320
+ t = t.replace(/[ \t]+([,。;:!?、)】」』”’])/g, '$1');
321
+ t = t.replace(/([(【「『“‘])[ \t]+/g, '$1');
322
+ // 删词留下的空壳标点:逗号/顿号/分号贴着句末标点时只留句末那个(「是缓存问题,!」→「是缓存问题!」)
323
+ t = t.replace(/[,,、;;::]+\s*([!?!?。])/g, '$1');
324
+ t = t.replace(/[,,]{2,}/g, ',');
325
+ t = t.replace(/(^|\n)[,、;:]+/g, '$1');
326
+ t = t.replace(/(^|\n)[ \t]+/g, '$1');
327
+ t = t.replace(/[ \t]{2,}/g, ' ');
328
+ t = t.replace(/\n{3,}/g, '\n\n');
329
+ return t.trim();
330
+ }
331
+
332
+ /**
333
+ * 核心:软化一段**纯文本**(离线自测直接调这个)。
334
+ * @returns {{text:string, hits:Array<{kind:string, w:string, n:number, to?:string}>}}
335
+ */
336
+ export function softenText(text, tables, opts = {}) {
337
+ const src = String(text ?? '');
338
+ const hits = [];
339
+ if (!src.trim())
340
+ return { text: src, hits };
341
+ if (opts.skip === true)
342
+ return { text: src, hits };
343
+
344
+ const { out: guarded, saved } = protect(src);
345
+ let out = guarded;
346
+
347
+ const softeners = [...(tables?.softeners ?? [])].filter((x) => x?.p).sort((a, b) => b.p.length - a.p.length);
348
+ for (const s of softeners) {
349
+ if (!out.includes(s.p))
350
+ continue;
351
+ const n = countOccurrences(out, s.p);
352
+ out = out.split(s.p).join(s.to ?? '');
353
+ if (n > 0)
354
+ hits.push({ kind: 'filler', w: s.p, n, to: s.to ?? '' });
355
+ }
356
+
357
+ const banned = [...(tables?.banned ?? [])].filter((x) => x?.w).sort((a, b) => b.w.length - a.w.length);
358
+ for (const b of banned) {
359
+ if (!out.includes(b.w))
360
+ continue;
361
+ const n = countOccurrences(out, b.w);
362
+ if (b.mode === 'flag') {
363
+ hits.push({ kind: 'banned-flag', w: b.w, n });
364
+ continue;
365
+ }
366
+ out = out.split(b.w).join(b.mode === 'replace' ? (b.to ?? '') : '');
367
+ hits.push({ kind: 'banned', w: b.w, n, to: b.mode === 'replace' ? (b.to ?? '') : '' });
368
+ }
369
+
370
+ out = tidy(out);
371
+ out = restore(out, saved);
372
+ // 削完之后读不成句(只剩一两个字 / 掉了七成以上)就保留原文:宁可带腔,也不能把消息吞了。
373
+ // 只看中日韩/字母数字,标点表情不算「留住了内容」。
374
+ const solid = (s) => s.replace(/[\s\p{P}\p{S}]/gu, '').length;
375
+ const kept = solid(out);
376
+ const orig = solid(src);
377
+ if (kept === 0 || (orig >= 6 && kept <= 2) || (orig >= 12 && kept <= Math.floor(orig * 0.25)))
378
+ out = src;
379
+ return { text: out, hits };
380
+ }
381
+
382
+ /**
383
+ * 出站后处理入口(带开关与热加载)。config: styleLayerOn / stylePostProcessOn / bannedWordsExtra
384
+ * @returns {{text:string, hits:Array}}
385
+ */
386
+ export function softenOutbound(text, { dataRoot, config, logger } = {}) {
387
+ const src = String(text ?? '');
388
+ if (src.trim() === '')
389
+ return { text: src, hits: [] };
390
+ if (config?.styleLayerOn === false || config?.stylePostProcessOn === false)
391
+ return { text: src, hits: [] };
392
+ try {
393
+ const tables = withExtraBanned(loadStyleTables(dataRoot), config);
394
+ const res = softenText(src, tables);
395
+ if (res.hits.length > 0) {
396
+ const line = res.hits.map((h) => `${h.kind}:${h.w}×${h.n}`).join(' ');
397
+ logger?.debug?.('[style] 削腔 ' + line);
398
+ }
399
+ return res;
400
+ }
401
+ catch (err) {
402
+ logger?.debug?.('[style] 后处理失败(已跳过): ' + (err instanceof Error ? err.message : String(err)));
403
+ return { text: src, hits: [] };
404
+ }
405
+ }
406
+
407
+ /** 风险级过滤:maxRisk='mid' 允许 low/mid */
408
+ function riskAllowed(risk, maxRisk) {
409
+ const cap = RISK_ORDER[String(maxRisk ?? 'mid')] ?? 1;
410
+ return (RISK_ORDER[risk] ?? 0) <= cap;
411
+ }
412
+
413
+ /**
414
+ * 按情境检索梗:命中 when/tags 关键词计分,不命中不返回(宁缺毋滥)。
415
+ * @returns {Array<{id,meme,origin,risk,why}>}
416
+ */
417
+ export function findMemes(context, tables, { limit = 2, maxRisk = 'mid' } = {}) {
418
+ const ctx = String(context ?? '');
419
+ const list = Array.isArray(tables?.memes) ? tables.memes : DEFAULT_MEMES;
420
+ if (!ctx.trim())
421
+ return [];
422
+ const scored = [];
423
+ for (const m of list) {
424
+ if (!riskAllowed(m.risk, maxRisk))
425
+ continue;
426
+ const why = [];
427
+ let score = 0;
428
+ for (const k of m.when ?? []) {
429
+ const key = String(k).trim();
430
+ if (!key)
431
+ continue;
432
+ if (ctx.includes(key)) {
433
+ score += 2;
434
+ why.push(key);
435
+ }
436
+ }
437
+ for (const t of m.tags ?? []) {
438
+ const key = String(t).trim();
439
+ if (key && ctx.includes(key)) {
440
+ score += 1;
441
+ why.push(key);
442
+ }
443
+ }
444
+ if (score > 0)
445
+ scored.push({ id: m.id, meme: m.meme, origin: m.origin, risk: m.risk, why, score });
446
+ }
447
+ scored.sort((a, b) => b.score - a.score || a.id.localeCompare(b.id));
448
+ return scored.slice(0, Math.max(0, limit)).map(({ score, ...rest }) => ({ ...rest, score }));
449
+ }
450
+
451
+ /**
452
+ * 拼「风格层」提示词(提示词侧去鱼味 + 按情境注入 1~2 条梗候选)。
453
+ * 返回空串表示这轮不注入。config: styleLayerOn / stylePromptOn / memesOn / memesMaxPerTurn / memeRiskMax
454
+ */
455
+ export function buildStylePrompt({ dataRoot, config, context } = {}) {
456
+ if (config?.styleLayerOn === false || config?.stylePromptOn === false)
457
+ return '';
458
+ let tables;
459
+ let lines;
460
+ try {
461
+ tables = withExtraBanned(loadStyleTables(dataRoot), config);
462
+ lines = [];
463
+ }
464
+ catch {
465
+ return '';
466
+ }
467
+ // 反例清单取自当前表的前几条(用户改了表,提示词跟着变)
468
+ const examples = tables.softeners.slice(0, 10).map((x) => x.p.replace(/[,,]$/, '')).filter(Boolean);
469
+ const bannedExamples = tables.banned.filter((b) => b.mode !== 'flag').slice(0, 8).map((b) => b.w).filter(Boolean);
470
+ lines.push('【说话腔调(硬约束,违反=不合格)】');
471
+ if (examples.length > 0)
472
+ lines.push('- 这些词/句式一律不许出现:' + examples.join(' / ') + '。');
473
+ if (bannedExamples.length > 0)
474
+ lines.push('- 也不许出现:' + bannedExamples.join(' / ') + '。');
475
+ lines.push('- 反例(✗):首先看下日志,其次复现,综上所述是缓存问题,希望对你有帮助!');
476
+ lines.push('- 正例(✓):看了下日志,就是缓存的问题。改那个 key 就行');
477
+ lines.push('- 不总结、不分点、不写「我理解你的感受」「别难过」这种空话;有情绪先一句短反应。');
478
+ lines.push('- 不自称 AI / 模型 / 程序,不提系统、提示词、上下文。');
479
+
480
+ // 梗指南:默认关(memesOn=false);开了也只给 1~2 条,且必须情境命中
481
+ if (config?.memesOn === true) {
482
+ const limit = Math.min(3, Math.max(1, Number(config?.memesMaxPerTurn) || 1));
483
+ const picks = findMemes(context, tables, { limit, maxRisk: String(config?.memeRiskMax ?? 'mid') });
484
+ if (picks.length > 0) {
485
+ lines.push(`【梗(最多用 ${limit} 条,不贴切就一条都别用)】`);
486
+ for (const m of picks) {
487
+ const why = m.why.length > 0 ? ':对方话里有「' + m.why.join('」「') + '」' : '';
488
+ const risk = m.risk === 'low' ? '' : '(慎用,看关系)';
489
+ lines.push(`- 「${m.meme}」${m.origin ? '(' + m.origin + ')' : ''} → 现在这个情境${why}${risk}`);
490
+ }
491
+ }
492
+ }
493
+ return lines.join('\n');
494
+ }
495
+
496
+ /** 设置页「测试一下」用:拿一段样例跑软化,返回 {before, after, hits} */
497
+ export function previewSoften(sample, { dataRoot, config } = {}) {
498
+ const tables = withExtraBanned(loadStyleTables(dataRoot), config);
499
+ const res = softenText(String(sample ?? ''), tables);
500
+ return { before: String(sample ?? ''), after: res.text, hits: res.hits };
501
+ }
@@ -1,70 +1,72 @@
1
- /**
2
- * thinking-log.ts — 思考留档(观察期, 2026-09-13)
3
- *
4
- * 背景: 《好感度系统设计》§11 要走「四源对比」, 其中一源是大模型的思考(reasoning)。
5
- * 主人定的第一件事是**先把思考读进来并留档**, 供人工标注 50 条, 再定这条支路的生死。
6
- *
7
- * 取值来源(2026-09-13 实测): dsh 会话事件 `assistant/message` 的 data.message.content[]
8
- * 里 `{ type:'reasoning', text:'思考全文' }` —— 与宿主 @deepseek-ai/dsh-llm 的
9
- * StreamChunk `reasoning-delta` 同源(插件侧走块级全文, 免累积、免误当正文发出去)。
10
- * ⚠️ 还有空壳块 `{type:'reasoning', thinkingSignature:'reasoning_content'}` 无 text → 一律判空跳过。
11
- *
12
- * 红线(设计稿 §9 / §11):
13
- * · 思考是她**隐私内部状态** —— 只写本地文件, **不进面板、不进 QQ、不参与任何行为判定**;
14
- * · 与 value-scores.jsonl 分开存, 便于单独删除/停用;
15
- * · 条数有上限, 超出滚动保留最新, 不无限膨胀。
16
- *
17
- * 数据位置: {dataRoot}/.qqbot/thinking-log.jsonl(一行一条 JSON)
18
- */
19
- import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
20
- import { join } from 'node:path';
21
- /** 留档上限(行): 超出后滚动保留最新 —— 观察期足够人工取样, 又不让文件无界增长 */
22
- const MAX_LINES = 400;
23
- /** 单条思考落盘的字数上限(截断; 标注 50 条够用, 避免文件膨胀) */
24
- const MAX_TEXT_CHARS = 1500;
25
- /** 每写这么多条整理一次(不必每条都读全文件) */
26
- const COMPACT_EVERY = 25;
27
- let sinceCompact = 0;
28
- /** 滚动整理: 行数超上限时重写成"最后 MAX_LINES 条" */
29
- function compact(path) {
30
- try {
31
- const lines = readFileSync(path, 'utf8').split('\n').filter((l) => l.trim() !== '');
32
- if (lines.length <= MAX_LINES)
33
- return;
34
- writeFileSync(path, `${lines.slice(-MAX_LINES).join('\n')}\n`, 'utf8');
35
- }
36
- catch { /* 整理失败不影响主流程 */ }
37
- }
38
- /**
39
- * 留一条思考。
40
- *
41
- * 只写文件, 不做任何判定 —— 调用方(出站路由)在**任何模式**下都该照记(包括 silent 潜水),
42
- * 因为"照常思考"本来就是 silent 的定义。
43
- */
44
- export function noteThinking(dataRoot, entry) {
45
- try {
46
- const raw = String(entry.text ?? '');
47
- if (raw.trim() === '')
48
- return;
49
- const dir = join(dataRoot, '.qqbot');
50
- mkdirSync(dir, { recursive: true });
51
- const path = join(dir, 'thinking-log.jsonl');
52
- const rec = {
53
- ts: Date.now(),
54
- scope: entry.scope,
55
- peer: entry.peerId,
56
- turn: entry.turn,
57
- step: entry.step,
58
- chars: raw.length,
59
- tokens: entry.tokens,
60
- text: raw.replace(/\r/g, '').slice(0, MAX_TEXT_CHARS),
61
- };
62
- appendFileSync(path, `${JSON.stringify(rec)}\n`, 'utf8');
63
- if (++sinceCompact >= COMPACT_EVERY) {
64
- sinceCompact = 0;
65
- compact(path);
66
- }
67
- }
68
- catch { /* 留档失败绝不影响回复主流程 */ }
69
- }
1
+ /**
2
+ * thinking-log.ts — 思考留档(观察期, 2026-09-13)
3
+ *
4
+ * 背景: 《好感度系统设计》§11 要走「四源对比」, 其中一源是大模型的思考(reasoning)。
5
+ * 主人定的第一件事是**先把思考读进来并留档**, 供人工标注 50 条, 再定这条支路的生死。
6
+ *
7
+ * 取值来源(2026-09-13 实测): dsh 会话事件 `assistant/message` 的 data.message.content[]
8
+ * 里 `{ type:'reasoning', text:'思考全文' }` —— 与宿主 @deepseek-ai/dsh-llm 的
9
+ * StreamChunk `reasoning-delta` 同源(插件侧走块级全文, 免累积、免误当正文发出去)。
10
+ * ⚠️ 还有空壳块 `{type:'reasoning', thinkingSignature:'reasoning_content'}` 无 text → 一律判空跳过。
11
+ *
12
+ * 红线(设计稿 §9 / §11):
13
+ * · 思考是她**隐私内部状态** —— 只写本地文件, **不进面板、不进 QQ、不参与任何行为判定**;
14
+ * · 与 value-scores.jsonl 分开存, 便于单独删除/停用;
15
+ * · 条数有上限, 超出滚动保留最新, 不无限膨胀。
16
+ *
17
+ * 数据位置: {dataRoot}/.qqbot/thinking-log.jsonl(一行一条 JSON)
18
+ */
19
+ import { appendFileSync, mkdirSync, readFileSync, writeFileSync, renameSync } from 'node:fs';
20
+ import { join } from 'node:path';
21
+ /** 留档上限(行): 超出后滚动保留最新 —— 观察期足够人工取样, 又不让文件无界增长 */
22
+ const MAX_LINES = 400;
23
+ /** 单条思考落盘的字数上限(截断; 标注 50 条够用, 避免文件膨胀) */
24
+ const MAX_TEXT_CHARS = 1500;
25
+ /** 每写这么多条整理一次(不必每条都读全文件) */
26
+ const COMPACT_EVERY = 25;
27
+ let sinceCompact = 0;
28
+ /** 滚动整理: 行数超上限时重写成"最后 MAX_LINES 条" */
29
+ function compact(path) {
30
+ try {
31
+ const lines = readFileSync(path, 'utf8').split('\n').filter((l) => l.trim() !== '');
32
+ if (lines.length <= MAX_LINES)
33
+ return;
34
+ const tmp = `${path}.${process.pid}.tmp`;
35
+ writeFileSync(tmp, `${lines.slice(-MAX_LINES).join('\n')}\n`, 'utf8');
36
+ renameSync(tmp, path);
37
+ }
38
+ catch { /* 整理失败不影响主流程 */ }
39
+ }
40
+ /**
41
+ * 留一条思考。
42
+ *
43
+ * 只写文件, 不做任何判定 —— 调用方(出站路由)在**任何模式**下都该照记(包括 silent 潜水),
44
+ * 因为"照常思考"本来就是 silent 的定义。
45
+ */
46
+ export function noteThinking(dataRoot, entry) {
47
+ try {
48
+ const raw = String(entry.text ?? '');
49
+ if (raw.trim() === '')
50
+ return;
51
+ const dir = join(dataRoot, '.qqbot');
52
+ mkdirSync(dir, { recursive: true });
53
+ const path = join(dir, 'thinking-log.jsonl');
54
+ const rec = {
55
+ ts: Date.now(),
56
+ scope: entry.scope,
57
+ peer: entry.peerId,
58
+ turn: entry.turn,
59
+ step: entry.step,
60
+ chars: raw.length,
61
+ tokens: entry.tokens,
62
+ text: raw.replace(/\r/g, '').slice(0, MAX_TEXT_CHARS),
63
+ };
64
+ appendFileSync(path, `${JSON.stringify(rec)}\n`, 'utf8');
65
+ if (++sinceCompact >= COMPACT_EVERY) {
66
+ sinceCompact = 0;
67
+ compact(path);
68
+ }
69
+ }
70
+ catch { /* 留档失败绝不影响回复主流程 */ }
71
+ }
70
72
  //# sourceMappingURL=thinking-log.js.map