dsh-chat-comprehension 0.3.7 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -27
- package/client.js +441 -53
- package/lib/engines.mjs +174 -7
- package/lib/index.js +316 -35
- package/lib/models.mjs +166 -0
- package/lib/qq-cards.mjs +16 -8
- package/lib/qqbot-0.5.3-patches.json +60 -4
- package/lib/qqbot-patch.mjs +1 -1
- package/lib/video.mjs +1 -1
- package/package.json +2 -2
- package/vendor/dsh-agent-md/lib/chat-persona.js +29 -4
- package/vendor/dsh-agent-md/lib/index.js +80 -8
- package/vendor/dsh-agent-md/lib/qq.js +288 -44
- package/vendor/dsh-bili-scroll/index.js +25 -9
- package/vendor/dsh-meme/index.js +2 -2
- package/vendor/dsh-meme/memes.js +3 -1
- package/vendor/dsh-world/lib/qqScope.js +65 -17
- package/vendor/dsh-world/lib/runtime.js +17 -5
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
* 以真实路径解析,裸包名会解析失败)。
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
-
import {
|
|
24
|
+
import { randomUUID } from 'node:crypto'
|
|
25
|
+
import { spawnSync } from 'node:child_process'
|
|
26
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, writeFileSync } from 'node:fs'
|
|
25
27
|
import { homedir } from 'node:os'
|
|
26
28
|
import { dirname, join } from 'node:path'
|
|
27
29
|
|
|
@@ -58,73 +60,251 @@ export function bindingsPath() {
|
|
|
58
60
|
/**
|
|
59
61
|
* QQ 专属记忆的「会话清单」:dsh-world 的 QQ 作用域只认这里列出的会话。
|
|
60
62
|
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
+
* v2(2026-11-08):**按 QQ 聊天分组**——私聊、每个群各一组:
|
|
64
|
+
* { version: 2, peers: { <peerKey>: { current, sessions, updatedAt } } }
|
|
65
|
+
* 每组的 current 只读到本组 sessions 的记忆(以前是一张总表,私聊会读到群里的话)。
|
|
66
|
+
* 绑定时把「这一次的目标会话」和「绑定前的旧会话」记进**那个 peer 自己的组**。
|
|
63
67
|
*/
|
|
64
68
|
export function qqMemoryPath() {
|
|
65
69
|
return join(agentsRoot(), 'qq-memory.json')
|
|
66
70
|
}
|
|
67
71
|
|
|
68
|
-
/**
|
|
69
|
-
export function
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
/** 网关补丁 v4 记的「QQ 聊天 → 会话」表(每次网关确定会话都会写)。 */
|
|
73
|
+
export function qqPeersPath() {
|
|
74
|
+
return join(agentsRoot(), 'qq-peers.json')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 读 qq-peers.json:{ peerKey: { current, sessions[] } }。 */
|
|
78
|
+
export function readQqPeers() {
|
|
79
|
+
const raw = readJson(qqPeersPath())
|
|
80
|
+
const peers = raw && typeof raw.peers === 'object' && raw.peers !== null ? raw.peers : {}
|
|
81
|
+
const out = {}
|
|
82
|
+
for (const [key, row] of Object.entries(peers)) {
|
|
83
|
+
if (!row || typeof row !== 'object') continue
|
|
84
|
+
const sessions = Array.isArray(row.sessions) ? row.sessions.filter((id) => typeof id === 'string' && id !== '') : []
|
|
85
|
+
const current = typeof row.current === 'string' ? row.current : ''
|
|
86
|
+
if (current !== '' && !sessions.includes(current)) sessions.unshift(current)
|
|
87
|
+
out[key] = { current, sessions }
|
|
88
|
+
}
|
|
89
|
+
return out
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const uniq = (list) => {
|
|
93
|
+
const out = []
|
|
94
|
+
for (const id of list) {
|
|
95
|
+
const t = typeof id === 'string' ? id.trim() : ''
|
|
96
|
+
if (t !== '' && !out.includes(t)) out.push(t)
|
|
97
|
+
}
|
|
98
|
+
return out
|
|
73
99
|
}
|
|
74
100
|
|
|
75
101
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* `sessions` 是**归档来源**(当前 + 历史上用过的 QQ 对话),`current` 只放当前绑定的那一个:
|
|
79
|
-
* 记忆块只注入 current,旧对话只供阅读,不会被写入新的记忆块。
|
|
102
|
+
* 读成 v2 分组(v1 文件会按「哪个会话属于哪个 QQ 聊天」迁移;分不出来的留在 legacy 组,不再注入任何人)。
|
|
103
|
+
* @returns {{ version: 2, peers: Record<string, { current: string, sessions: string[], updatedAt?: string }> }}
|
|
80
104
|
*/
|
|
81
|
-
export function
|
|
105
|
+
export function readQqMemory() {
|
|
82
106
|
const raw = readJson(qqMemoryPath())
|
|
83
|
-
if (raw
|
|
84
|
-
|
|
107
|
+
if (raw && typeof raw.peers === 'object' && raw.peers !== null) {
|
|
108
|
+
const peers = {}
|
|
109
|
+
for (const [key, row] of Object.entries(raw.peers)) {
|
|
110
|
+
if (!row || typeof row !== 'object') continue
|
|
111
|
+
peers[key] = { current: typeof row.current === 'string' ? row.current : '', sessions: uniq(Array.isArray(row.sessions) ? row.sessions : []), ...(row.updatedAt ? { updatedAt: row.updatedAt } : {}) }
|
|
112
|
+
}
|
|
113
|
+
return { version: 2, peers }
|
|
114
|
+
}
|
|
115
|
+
if (raw && typeof raw === 'object' && (Array.isArray(raw.sessions) || typeof raw.current === 'string')) {
|
|
116
|
+
return migrateQqMemoryV1(raw)
|
|
117
|
+
}
|
|
118
|
+
return { version: 2, peers: {} }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** 某个会话属于哪个 QQ 聊天:先看 qq-peers.json,再看网关的 sessionIds 和绑定记录。 */
|
|
122
|
+
export function peerKeyOfSession(sessionId, hints = {}) {
|
|
123
|
+
const id = String(sessionId || '')
|
|
124
|
+
if (id === '') return ''
|
|
125
|
+
const peers = hints.peers || readQqPeers()
|
|
126
|
+
for (const [key, row] of Object.entries(peers)) if (row.current === id || row.sessions.includes(id)) return key
|
|
127
|
+
const ids = readQqPrefs().sessionIds
|
|
128
|
+
for (const [key, value] of Object.entries(ids)) if (String(value) === id) return key
|
|
129
|
+
for (const b of readBindings()) if (b.sessionId === id || b.originalSessionId === id) return b.peerKey
|
|
130
|
+
return ''
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** v1 → v2:每个会话按 peerKeyOfSession 归组;current 归到它自己的组。 */
|
|
134
|
+
export function migrateQqMemoryV1(raw) {
|
|
135
|
+
const peers = {}
|
|
136
|
+
const legacy = []
|
|
137
|
+
const sessions = uniq([typeof raw.current === 'string' ? raw.current : '', ...(Array.isArray(raw.sessions) ? raw.sessions : [])])
|
|
138
|
+
const hints = { peers: readQqPeers() }
|
|
139
|
+
for (const id of sessions) {
|
|
140
|
+
const key = peerKeyOfSession(id, hints)
|
|
141
|
+
if (key === '') { legacy.push(id); continue }
|
|
142
|
+
peers[key] = peers[key] || { current: '', sessions: [] }
|
|
143
|
+
if (!peers[key].sessions.includes(id)) peers[key].sessions.push(id)
|
|
144
|
+
}
|
|
145
|
+
const current = typeof raw.current === 'string' ? raw.current.trim() : ''
|
|
146
|
+
if (current !== '') {
|
|
147
|
+
const key = peerKeyOfSession(current, hints)
|
|
148
|
+
if (key !== '') peers[key].current = current
|
|
149
|
+
}
|
|
150
|
+
// 分不出来的旧会话:只有唯一的私聊 / 唯一的群时,按消息格式(群消息带「[昵称 (openid)]」)归过去;
|
|
151
|
+
// 不唯一就留在 legacy(不注入任何人,宁可少记也不串)
|
|
152
|
+
const known = Object.keys(readQqPrefs().sessionIds).concat(Object.keys(hints.peers))
|
|
153
|
+
const only = (scope) => { const ks = [...new Set(known.filter((k) => k.split(':')[2] === scope))]; return ks.length === 1 ? ks[0] : '' }
|
|
154
|
+
const rest = []
|
|
155
|
+
// 只有「确实只有一个私聊 / 一个群」时,猜出来的归属才有意义;而且最多读 8 个会话文件,
|
|
156
|
+
// 免得迁移时对着一堆旧文件挨个解压,把宿主启动拖住。
|
|
157
|
+
const canGuess = Boolean(only('c2c') || only('group'))
|
|
158
|
+
let probes = 0
|
|
159
|
+
for (const id of legacy) {
|
|
160
|
+
const kind = canGuess && probes < 8 ? (probes += 1, sessionLooksLike(id)) : ''
|
|
161
|
+
const key = kind ? only(kind) : ''
|
|
162
|
+
if (key === '') { rest.push(id); continue }
|
|
163
|
+
peers[key] = peers[key] || { current: '', sessions: [] }
|
|
164
|
+
if (!peers[key].sessions.includes(id)) peers[key].sessions.push(id)
|
|
165
|
+
}
|
|
166
|
+
if (rest.length) peers.legacy = { current: '', sessions: rest }
|
|
167
|
+
return { version: 2, peers, migratedFrom: 1 }
|
|
85
168
|
}
|
|
86
169
|
|
|
87
170
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
171
|
+
* 看一个旧会话像私聊还是群:群里的每条用户消息都带「[昵称 (openid)]」。
|
|
172
|
+
* 读不到(zstd 没装 / 会话被删)就返回 ''。只在迁移时用一次。
|
|
90
173
|
*/
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const trimmed = id.trim()
|
|
96
|
-
if (trimmed === '' || next.includes(trimmed)) continue
|
|
97
|
-
next.push(trimmed)
|
|
98
|
-
}
|
|
99
|
-
for (const id of readQqMemorySessions()) {
|
|
100
|
-
if (!next.includes(id)) next.push(id)
|
|
101
|
-
}
|
|
102
|
-
const kept = next.slice(0, 30)
|
|
103
|
-
const active = typeof current === 'string' ? current.trim() : readQqMemoryCurrent()
|
|
174
|
+
let zstdReady
|
|
175
|
+
let zstdProbes = 0
|
|
176
|
+
function hasZstd() {
|
|
177
|
+
if (zstdReady !== undefined) return zstdReady
|
|
104
178
|
try {
|
|
105
|
-
|
|
179
|
+
const out = spawnSync('zstd', ['--version'], { encoding: 'utf8', timeout: 3000, windowsHide: true })
|
|
180
|
+
zstdReady = out.status === 0
|
|
181
|
+
} catch {
|
|
182
|
+
zstdReady = false
|
|
183
|
+
}
|
|
184
|
+
return zstdReady
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function sessionLooksLike(sessionId) {
|
|
188
|
+
// 整个进程最多解压 8 个旧会话来判断归属:迁移失败也不能每 5 秒重试一次把宿主拖住
|
|
189
|
+
if (zstdProbes >= 8 || !hasZstd()) return ''
|
|
190
|
+
zstdProbes += 1
|
|
191
|
+
try {
|
|
192
|
+
const root = join(dirname(agentsRoot()), 'sessions')
|
|
193
|
+
const id = String(sessionId).replace(/^session-/, '')
|
|
194
|
+
let file = ''
|
|
195
|
+
for (const dir of readdirSync(root)) {
|
|
196
|
+
for (const name of [sessionId, 'session-' + id, id]) {
|
|
197
|
+
const candidate = join(root, dir, name, 'session.v4.jsonl.zstd')
|
|
198
|
+
if (existsSync(candidate)) { file = candidate; break }
|
|
199
|
+
}
|
|
200
|
+
if (file) break
|
|
201
|
+
}
|
|
202
|
+
if (!file) return ''
|
|
203
|
+
// 迁移只在启动时跑一次:单个会话最多读 4 秒,别让一个坏文件把宿主启动卡住
|
|
204
|
+
const out = spawnSync('zstd', ['-dcq', file], { encoding: 'utf8', timeout: 4000, maxBuffer: 32 * 1024 * 1024, windowsHide: true })
|
|
205
|
+
if (out.status !== 0 || !out.stdout) return ''
|
|
206
|
+
let group = 0
|
|
207
|
+
let plain = 0
|
|
208
|
+
for (const line of out.stdout.split('\n')) {
|
|
209
|
+
if (!line.includes('"kind":"user"') && !line.includes('"kind": "user"')) continue
|
|
210
|
+
const m = line.match(/"text":\s*"(.{0,200})/)
|
|
211
|
+
if (!m) continue
|
|
212
|
+
if (/\[[^\]]{1,40} \([0-9A-F]{16,}\)\]/.test(m[1])) group++
|
|
213
|
+
else plain++
|
|
214
|
+
}
|
|
215
|
+
if (group + plain < 3) return ''
|
|
216
|
+
return group >= plain * 2 ? 'group' : (plain >= group * 2 ? 'c2c' : '')
|
|
217
|
+
} catch {
|
|
218
|
+
return ''
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function writeQqMemory(memory) {
|
|
223
|
+
try {
|
|
224
|
+
writeJson(qqMemoryPath(), { version: 2, peers: memory.peers, updatedAt: new Date().toISOString() })
|
|
106
225
|
} catch {
|
|
107
226
|
// 记忆清单写不进去不能拦住绑定本身
|
|
108
227
|
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** 兼容旧调用:所有 QQ 聊天的会话合起来(只读用途)。 */
|
|
231
|
+
export function readQqMemorySessions() {
|
|
232
|
+
const out = []
|
|
233
|
+
for (const row of Object.values(readQqMemory().peers)) for (const id of row.sessions) if (!out.includes(id)) out.push(id)
|
|
234
|
+
return out
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** 兼容旧调用:私聊那一组的 current(没有就取第一个有 current 的组)。 */
|
|
238
|
+
export function readQqMemoryCurrent() {
|
|
239
|
+
const peers = Object.entries(readQqMemory().peers)
|
|
240
|
+
const c2c = peers.find(([key, row]) => key.split(':')[2] === 'c2c' && row.current)
|
|
241
|
+
if (c2c) return c2c[1].current
|
|
242
|
+
const any = peers.find(([, row]) => row.current)
|
|
243
|
+
return any ? any[1].current : ''
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 把若干会话记进**某个 QQ 聊天自己那一组**(去重、只留最近 30 个)。
|
|
248
|
+
* @param {string} peerKey `qqbot:<appId>:<c2c|group>:<id>`
|
|
249
|
+
* @returns {string[]} 写入后这一组的清单
|
|
250
|
+
*/
|
|
251
|
+
export function recordQqMemorySessions(ids, current, peerKey) {
|
|
252
|
+
const memory = readQqMemory()
|
|
253
|
+
const key = typeof peerKey === 'string' && peerKey !== '' ? peerKey : (peerKeyOfSession(current) || 'legacy')
|
|
254
|
+
const row = memory.peers[key] || { current: '', sessions: [] }
|
|
255
|
+
const kept = uniq([...(Array.isArray(ids) ? ids : []), ...row.sessions]).slice(0, 30)
|
|
256
|
+
const active = typeof current === 'string' ? current.trim() : row.current
|
|
257
|
+
memory.peers[key] = { current: active, sessions: kept, updatedAt: new Date().toISOString() }
|
|
258
|
+
writeQqMemory(memory)
|
|
109
259
|
return kept
|
|
110
260
|
}
|
|
111
261
|
|
|
112
|
-
/**
|
|
113
|
-
export function setQqMemoryCurrent(current) {
|
|
262
|
+
/** 解绑时把这个 QQ 聊天的「当前对话」清掉:它的历史会话仍留着,别的聊天不受影响。 */
|
|
263
|
+
export function setQqMemoryCurrent(current, peerKey) {
|
|
264
|
+
const memory = readQqMemory()
|
|
114
265
|
const active = typeof current === 'string' ? current.trim() : ''
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
updatedAt: new Date().toISOString(),
|
|
121
|
-
})
|
|
122
|
-
} catch {
|
|
123
|
-
// 同上
|
|
266
|
+
const keys = typeof peerKey === 'string' && peerKey !== '' ? [peerKey] : Object.keys(memory.peers)
|
|
267
|
+
for (const key of keys) {
|
|
268
|
+
if (!memory.peers[key]) continue
|
|
269
|
+
memory.peers[key].current = active
|
|
270
|
+
memory.peers[key].updatedAt = new Date().toISOString()
|
|
124
271
|
}
|
|
272
|
+
writeQqMemory(memory)
|
|
125
273
|
return active
|
|
126
274
|
}
|
|
127
275
|
|
|
276
|
+
/**
|
|
277
|
+
* 网关每确定一个会话就会更新 qq-peers.json;这里把它同步进记忆分组:
|
|
278
|
+
* 每个 QQ 聊天的 current = 它现在用的会话,sessions 并上它用过的会话。
|
|
279
|
+
* 已有的组不删;`legacy` 组(旧版分不出来的)不再注入。
|
|
280
|
+
* @returns {boolean} 有没有改动
|
|
281
|
+
*/
|
|
282
|
+
export function syncQqMemoryWithPeers() {
|
|
283
|
+
const peers = readQqPeers()
|
|
284
|
+
if (Object.keys(peers).length === 0) {
|
|
285
|
+
// 没有 peers 表时,至少把 v1 文件迁成 v2(不然旧的总表还会继续串)
|
|
286
|
+
const raw = readJson(qqMemoryPath())
|
|
287
|
+
if (raw && !raw.peers && (Array.isArray(raw.sessions) || typeof raw.current === 'string')) { writeQqMemory(readQqMemory()); return true }
|
|
288
|
+
return false
|
|
289
|
+
}
|
|
290
|
+
const memory = readQqMemory()
|
|
291
|
+
let changed = Boolean(memory.migratedFrom)
|
|
292
|
+
for (const [key, row] of Object.entries(peers)) {
|
|
293
|
+
const prev = memory.peers[key] || { current: '', sessions: [] }
|
|
294
|
+
const sessions = uniq([...row.sessions, ...prev.sessions]).slice(0, 30)
|
|
295
|
+
// 网关记的 current 才是这个聊天现在真正在用的会话(压缩后会换成子会话);
|
|
296
|
+
// 绑定记录只在网关还没说话(没有 current)时兜底,不然会话一压缩记忆就断了。
|
|
297
|
+
const bound = readBindings().find((b) => b.peerKey === key)
|
|
298
|
+
const current = row.current || (bound ? bound.sessionId : '')
|
|
299
|
+
if (prev.current !== current || sessions.join('|') !== prev.sessions.join('|')) {
|
|
300
|
+
memory.peers[key] = { current, sessions, updatedAt: new Date().toISOString() }
|
|
301
|
+
changed = true
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (changed) writeQqMemory(memory)
|
|
305
|
+
return changed
|
|
306
|
+
}
|
|
307
|
+
|
|
128
308
|
function readJson(file) {
|
|
129
309
|
try {
|
|
130
310
|
return JSON.parse(readFileSync(file, 'utf8'))
|
|
@@ -186,6 +366,59 @@ export function peerKeyFor(appId, scope, peerId) {
|
|
|
186
366
|
return `${KEY_PREFIX}${appId}:${scope}:${peerId}`
|
|
187
367
|
}
|
|
188
368
|
|
|
369
|
+
/**
|
|
370
|
+
* 把「这个 QQ 聊天用哪个模型」写进网关自己的 per-peer 覆盖表。
|
|
371
|
+
* 网关建会话/恢复会话时会读它,所以新建的群也能一开口就用对模型。
|
|
372
|
+
* @param {string} peerKey `qqbot:<appId>:<scope>:<peerId>`
|
|
373
|
+
* @param {{provider:string,model:string,reasoningEffort?:string}|null} route null 表示删掉覆盖
|
|
374
|
+
*/
|
|
375
|
+
export function setQqModelOverride(peerKey, route) {
|
|
376
|
+
const key = String(peerKey || '')
|
|
377
|
+
if (parsePeerKey(key) === null) return false
|
|
378
|
+
const prefs = readQqPrefs()
|
|
379
|
+
if (route && String(route.provider || '') !== '' && String(route.model || '') !== '') {
|
|
380
|
+
prefs.overrides[key] = {
|
|
381
|
+
provider: String(route.provider),
|
|
382
|
+
model: String(route.model),
|
|
383
|
+
...(String(route.reasoningEffort || '') !== '' ? { reasoningEffort: String(route.reasoningEffort) } : {}),
|
|
384
|
+
}
|
|
385
|
+
} else {
|
|
386
|
+
delete prefs.overrides[key]
|
|
387
|
+
}
|
|
388
|
+
writeQqPrefs(prefs)
|
|
389
|
+
return true
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* 让人格 / 工作区改动立刻作用到这个 QQ 聊天:
|
|
394
|
+
* 1. 网关补丁 v6 有 companionQq.resetSession 时,让它 fork 到新目录(保留历史);
|
|
395
|
+
* 2. 没有的话退回「换一个新的 sessionId + 重启网关」,下一条消息会按新工作区新建会话
|
|
396
|
+
* (更早的对话仍然在 QQ 记忆里,不会被忘掉)。
|
|
397
|
+
* @returns {Promise<{ok:boolean, rebuilt?:boolean, error?:string}>}
|
|
398
|
+
*/
|
|
399
|
+
export async function resetQqPeerSession(ctx, peerKey) {
|
|
400
|
+
const key = String(peerKey || '')
|
|
401
|
+
if (parsePeerKey(key) === null) return { ok: false, error: '不是合法的 QQ 会话 key' }
|
|
402
|
+
try {
|
|
403
|
+
const qq = ctx && typeof ctx.get === 'function' ? ctx.get('companionQq') : null
|
|
404
|
+
if (qq && typeof qq.resetSession === 'function') {
|
|
405
|
+
const r = await qq.resetSession(key)
|
|
406
|
+
if (r && r.ok) return r
|
|
407
|
+
}
|
|
408
|
+
} catch { /* 老补丁没有这个方法 */ }
|
|
409
|
+
try {
|
|
410
|
+
const prefs = readQqPrefs()
|
|
411
|
+
prefs.sessionIds[key] = randomUUID()
|
|
412
|
+
writeQqPrefs(prefs)
|
|
413
|
+
const { entry } = findQqEntry(ctx)
|
|
414
|
+
await stopGatewayEntry(entry)
|
|
415
|
+
const reload = await startGatewayEntry(ctx, entry)
|
|
416
|
+
return { ok: true, rebuilt: false, reload }
|
|
417
|
+
} catch (error) {
|
|
418
|
+
return { ok: false, error: String((error && error.message) || error) }
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
189
422
|
/**
|
|
190
423
|
* 列出网关已经记下的 QQ 账号(= prefs.sessionIds 的 key)。
|
|
191
424
|
*
|
|
@@ -195,11 +428,21 @@ export function peerKeyFor(appId, scope, peerId) {
|
|
|
195
428
|
export function listQqPeers(prefs) {
|
|
196
429
|
const table = (prefs || readQqPrefs()).sessionIds
|
|
197
430
|
const peers = []
|
|
431
|
+
const seen = new Set()
|
|
198
432
|
for (const [key, sessionId] of Object.entries(table)) {
|
|
199
433
|
const info = parsePeerKey(key)
|
|
200
434
|
if (info === null) continue
|
|
435
|
+
seen.add(info.key)
|
|
201
436
|
peers.push({ ...info, sessionId: typeof sessionId === 'string' ? sessionId : '' })
|
|
202
437
|
}
|
|
438
|
+
// 网关卡(补丁 v5 写)里有、但 prefs 还没登记的聊天(比如第一次说话的群)也要列出来,
|
|
439
|
+
// 否则页面里根本看不到这个群,人格/工作区/模型都没地方设。
|
|
440
|
+
for (const [key, row] of Object.entries(readQqPeers())) {
|
|
441
|
+
const info = parsePeerKey(key)
|
|
442
|
+
if (info === null || seen.has(info.key)) continue
|
|
443
|
+
seen.add(info.key)
|
|
444
|
+
peers.push({ ...info, sessionId: row && typeof row.current === 'string' ? row.current : '' })
|
|
445
|
+
}
|
|
203
446
|
peers.sort((a, b) => (a.scope === b.scope ? a.peerId.localeCompare(b.peerId) : a.scope === 'c2c' ? -1 : 1))
|
|
204
447
|
return peers
|
|
205
448
|
}
|
|
@@ -402,6 +645,7 @@ export function qqStatus(ctx) {
|
|
|
402
645
|
current: readQqMemoryCurrent(),
|
|
403
646
|
sessions: readQqMemorySessions(),
|
|
404
647
|
mirrorPath: join(agentsRoot(), 'qq-memory.md'),
|
|
648
|
+
peers: readQqMemory().peers,
|
|
405
649
|
},
|
|
406
650
|
bindings,
|
|
407
651
|
peers,
|
|
@@ -509,7 +753,7 @@ export async function bindQqPeer(ctx, options) {
|
|
|
509
753
|
await stopGatewayEntry(entry)
|
|
510
754
|
const applied = applyBinding(options)
|
|
511
755
|
// 记忆清单:当前目标 + 绑定前的旧会话(上一次 QQ 用的对话),只写这里列出的会话。
|
|
512
|
-
recordQqMemorySessions([options.sessionId, applied.previousSessionId].filter((id) => typeof id === 'string' && id !== ''), options.sessionId)
|
|
756
|
+
recordQqMemorySessions([options.sessionId, applied.previousSessionId].filter((id) => typeof id === 'string' && id !== ''), options.sessionId, options.peerKey || (applied.record && applied.record.peerKey))
|
|
513
757
|
const reload = await startGatewayEntry(ctx, entry)
|
|
514
758
|
return { ...applied, reload }
|
|
515
759
|
}
|
|
@@ -520,7 +764,7 @@ export async function unbindQqPeer(ctx, peerKey) {
|
|
|
520
764
|
await stopGatewayEntry(entry)
|
|
521
765
|
const removed = removeBinding(peerKey)
|
|
522
766
|
// 解绑后没有「当前 QQ 对话」了:历史仍保留,但不再往任何会话里注入记忆。
|
|
523
|
-
setQqMemoryCurrent('')
|
|
767
|
+
setQqMemoryCurrent('', peerKey)
|
|
524
768
|
const reload = await startGatewayEntry(ctx, entry)
|
|
525
769
|
return { ...removed, reload }
|
|
526
770
|
}
|
|
@@ -77,7 +77,7 @@ export const Config = !z ? undefined : z.object({
|
|
|
77
77
|
/** 关注关键词(空了用章程里默认那几组) */
|
|
78
78
|
keywords: z.array(z.string()).default([]).extra('volatile', true),
|
|
79
79
|
/** Python:指向 venv 里的那个最好 */
|
|
80
|
-
pythonPath: z.string().default('python').extra('volatile', true),
|
|
80
|
+
pythonPath: z.string().default(process.platform === 'win32' ? 'python' : 'python3').extra('volatile', true),
|
|
81
81
|
/** dsh 的 bin.js(不填就试自动探测,再退化成 shell 里的 dsh) */
|
|
82
82
|
dshBin: z.string().default('').extra('volatile', true),
|
|
83
83
|
/** headless 那一轮用哪个 profile */
|
|
@@ -106,7 +106,7 @@ export const Config = !z ? undefined : z.object({
|
|
|
106
106
|
/** 值班轮用的 model(留空 = 跟当前默认模型) */
|
|
107
107
|
roundModel: z.string().default('').extra('volatile', true),
|
|
108
108
|
/** 「人格」页对应的项目目录 */
|
|
109
|
-
personaCwd: z.string().default('
|
|
109
|
+
personaCwd: z.string().default('').extra('volatile', true),
|
|
110
110
|
/** 「别像 AI」那段文字(使用者自己写;留空就不加) */
|
|
111
111
|
extraPrompt: z.string().default('').extra('volatile', true),
|
|
112
112
|
/** 带上 QQ 对话的记忆摘要(<DSH_HOME>/agents/qq-memory.md) */
|
|
@@ -270,8 +270,8 @@ function reportMeta() {
|
|
|
270
270
|
personaPrompt: '',
|
|
271
271
|
perDay: 5, peakValley: false, noon: true, night: true, autoComment: false, transcribe: false, asrModel: 'small',
|
|
272
272
|
dmMode: 'only', dmUid: 0,
|
|
273
|
-
keywords: [], roundProvider: '', roundModel: '', commentPerRound: 1, mentionName: '', pythonPath: 'python', dshBin: '', headlessProfile: 'headless', overlayMcp: true, workspace: '', reportLevel: 'full', replyToComments: false, replyPerRound: 1, favFolderId: 0,
|
|
274
|
-
customTimes: '', timeSync: true, focusFollowings: true, focusLimit: 12, focusTopicsEnabled: false, focusTopics: '', personaFromPage: true, personaCwd: '
|
|
273
|
+
keywords: [], roundProvider: '', roundModel: '', commentPerRound: 1, mentionName: '', pythonPath: process.platform === 'win32' ? 'python' : 'python3', dshBin: '', headlessProfile: 'headless', overlayMcp: true, workspace: '', reportLevel: 'full', replyToComments: false, replyPerRound: 1, favFolderId: 0,
|
|
274
|
+
customTimes: '', timeSync: true, focusFollowings: true, focusLimit: 12, focusTopicsEnabled: false, focusTopics: '', personaFromPage: true, personaCwd: '', extraPrompt: '', memoryExcerpt: true,
|
|
275
275
|
}
|
|
276
276
|
const normalize = (src, base) => {
|
|
277
277
|
const out = { ...base }
|
|
@@ -473,7 +473,7 @@ function reportMeta() {
|
|
|
473
473
|
*/
|
|
474
474
|
const personaPageText = () => {
|
|
475
475
|
try {
|
|
476
|
-
const file = join(dshHomeDir(), 'agents', 'projects', `${slugForCwd(cfg.personaCwd)}.md`)
|
|
476
|
+
const file = join(dshHomeDir(), 'agents', 'projects', `${slugForCwd(cfg.personaCwd || cfg.workspace)}.md`)
|
|
477
477
|
let text = readFileSync(file, 'utf8')
|
|
478
478
|
text = text.replace(/<!--[\s\S]*?-->/g, '').trim()
|
|
479
479
|
const parts = text.split(/(?=【[^】]+】)/)
|
|
@@ -649,7 +649,7 @@ function reportMeta() {
|
|
|
649
649
|
if (!existsSync(script)) throw new Error(`家里没有 mcp_server.py(应有 ${script})—— 装上插件后重启一次 dsh 会自动铺好`)
|
|
650
650
|
mcpKey = mcpKeyNow()
|
|
651
651
|
mcp = new McpClient({
|
|
652
|
-
command: cfg.pythonPath || 'python',
|
|
652
|
+
command: cfg.pythonPath || (process.platform === 'win32' ? 'python' : 'python3'),
|
|
653
653
|
args: [script],
|
|
654
654
|
cwd: stateDir, // 凭证落在家里 ✓
|
|
655
655
|
// ⭐ 这个内置的 Python MCP 会往 stdout 打中文(ensure_ascii=False);
|
|
@@ -1512,18 +1512,34 @@ function reportMeta() {
|
|
|
1512
1512
|
// ⇒ overlay 里补一条 agent-default-model,**优先跟随用户当前选的模型**;拿不到就不写(保持原行为)✓
|
|
1513
1513
|
try {
|
|
1514
1514
|
const sel = ctx.get('agentDefaultModel')?.currentSelection?.() ?? null
|
|
1515
|
+
// companion 的「默认模型(全局)」:只作用于 QQ 和 B站 值班,绝不改 DSH 网页对话的默认模型。
|
|
1516
|
+
// (0.5.0 起不再通过改 agentDefaultModel 实现,所以在这里直接读那份文件。)
|
|
1517
|
+
const companion = (() => {
|
|
1518
|
+
try {
|
|
1519
|
+
const home = String(process.env.DSH_HOME || '').trim() || join(homedir(), '.dsh')
|
|
1520
|
+
const raw = readJson(join(home, 'agents', 'companion-model.json'), {}) || {}
|
|
1521
|
+
const provider = String(raw.provider || '').trim()
|
|
1522
|
+
const model = String(raw.model || '').trim()
|
|
1523
|
+
return provider !== '' && model !== '' ? { provider, model, reasoningEffort: String(raw.reasoningEffort || '').trim() } : null
|
|
1524
|
+
} catch { return null }
|
|
1525
|
+
})()
|
|
1515
1526
|
// ⭐ 2026-11-07(用户要求):值班轮可以有自己独立的模型,和 QQ / 网页默认互不干扰。
|
|
1516
1527
|
const cfgProvider = String(cfg.roundProvider || '').trim()
|
|
1517
1528
|
const cfgModel = String(cfg.roundModel || '').trim()
|
|
1518
|
-
|
|
1519
|
-
|
|
1529
|
+
// headless 值班 profile 里只保证有 deepseek-account 这条路由;companion 默认若是别的
|
|
1530
|
+
// (比如 packyapi),headless 里没有 adapter,照搬会直接 NO_ADAPTER。所以只在可用时沿用。
|
|
1531
|
+
const useCompanion = companion !== null && cfgProvider === '' && cfgModel === ''
|
|
1532
|
+
&& /^deepseek-account$/.test(String(companion.provider || ''))
|
|
1533
|
+
const selProvider = cfgProvider !== '' ? cfgProvider : (useCompanion ? companion.provider : (sel?.provider ?? sel?.providerId))
|
|
1534
|
+
const selModel = cfgModel !== '' ? cfgModel : (useCompanion ? companion.model : (sel?.model ?? sel?.modelId))
|
|
1535
|
+
const selEffort = useCompanion ? companion.reasoningEffort : String(sel?.reasoningEffort ?? '')
|
|
1520
1536
|
if (selProvider && selModel) {
|
|
1521
1537
|
yml.push(
|
|
1522
1538
|
'- id: agent-default-model',
|
|
1523
1539
|
' config:',
|
|
1524
1540
|
' provider: ' + String(selProvider),
|
|
1525
1541
|
' model: ' + String(selModel),
|
|
1526
|
-
...(
|
|
1542
|
+
...(selEffort ? [' reasoningEffort: ' + String(selEffort)] : []),
|
|
1527
1543
|
'',
|
|
1528
1544
|
)
|
|
1529
1545
|
}
|
package/vendor/dsh-meme/index.js
CHANGED
|
@@ -860,7 +860,7 @@ export function apply(ctx, config) {
|
|
|
860
860
|
})
|
|
861
861
|
const qq = ctx.get('companionQq')
|
|
862
862
|
const sendImage = qq !== undefined && typeof qq.sendImage === 'function'
|
|
863
|
-
? (path, caption) => qq.sendImage(path, caption)
|
|
863
|
+
? (path, caption, opts) => qq.sendImage(path, caption, opts)
|
|
864
864
|
: null
|
|
865
865
|
if (sendImage || urlPrefix) {
|
|
866
866
|
registerToolWhenReady(ctx, 'send_meme', () => {
|
|
@@ -877,7 +877,7 @@ export function apply(ctx, config) {
|
|
|
877
877
|
const qqNow = ctx.get('companionQq')
|
|
878
878
|
if (qqNow !== undefined && typeof qqNow.sendImage === 'function') {
|
|
879
879
|
registerToolWhenReady(ctx, 'send_meme', () => {
|
|
880
|
-
registerSendMemeTool(ctx, sampler, (path, caption) => qqNow.sendImage(path, caption), urlPrefix, imToolOptions())
|
|
880
|
+
registerSendMemeTool(ctx, sampler, (path, caption, opts) => qqNow.sendImage(path, caption, opts), urlPrefix, imToolOptions())
|
|
881
881
|
})
|
|
882
882
|
}
|
|
883
883
|
}
|
package/vendor/dsh-meme/memes.js
CHANGED
|
@@ -890,7 +890,9 @@ export function registerSendMemeTool(ctx, memes, sendImage, urlPrefix = null, op
|
|
|
890
890
|
const sendFailed = (error) => ({ ok: false, message: '发送失败: ' + (error instanceof Error ? error.message : String(error)) })
|
|
891
891
|
const sentOk = () => ({ ok: true, message: '已发送表情包', path })
|
|
892
892
|
try {
|
|
893
|
-
|
|
893
|
+
// 发回调用这个工具的那个 QQ 会话(私聊 / 群各是各的),找不到才退回最近活跃的聊天
|
|
894
|
+
const sessionId = exec && exec.agent && exec.agent.session ? String(exec.agent.session.id || '') : String((exec && exec.agent && exec.agent.id) || '')
|
|
895
|
+
const sending = sendImage(absolute, undefined, sessionId ? { sessionId } : undefined)
|
|
894
896
|
if (sending && typeof sending.then === 'function') return Promise.resolve(sending).then(sentOk, sendFailed)
|
|
895
897
|
return sentOk()
|
|
896
898
|
} catch (error) {
|
|
@@ -1,39 +1,87 @@
|
|
|
1
1
|
import { readFileSync, statSync } from 'node:fs';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* QQ 作用域的会话清单(dsh-agent-md 写、这里读)。
|
|
4
|
+
*
|
|
5
|
+
* v2(2026-11-08):按 QQ 聊天分组 —— `{ version: 2, peers: { <peerKey>: { current, sessions } } }`。
|
|
6
|
+
* 私聊和每个群各是一组:每组的 current 只读到**本组** sessions 的记忆,互不串。
|
|
7
|
+
* v1(`{ current, sessions }`)照旧能读,当成一组 `legacy`。
|
|
8
|
+
*/
|
|
9
|
+
const empty = (file) => ({ file, mtime: -1, groups: new Map(), bySession: new Map(), currents: new Set(), all: new Set() });
|
|
10
|
+
let cache = empty('');
|
|
11
|
+
const ids = (list) => (Array.isArray(list) ? list.filter((id) => typeof id === 'string' && id !== '') : []);
|
|
3
12
|
function snapshot(file) {
|
|
4
13
|
if (!file)
|
|
5
|
-
return
|
|
14
|
+
return empty('');
|
|
6
15
|
try {
|
|
7
16
|
const info = statSync(file);
|
|
8
17
|
if (cache.file === file && cache.mtime === info.mtimeMs)
|
|
9
18
|
return cache;
|
|
10
19
|
const raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
20
|
+
const next = empty(file);
|
|
21
|
+
next.mtime = info.mtimeMs;
|
|
22
|
+
const add = (peer, current, sessions) => {
|
|
23
|
+
const set = new Set(ids(sessions));
|
|
24
|
+
if (typeof current === 'string' && current !== '')
|
|
25
|
+
set.add(current);
|
|
26
|
+
const group = { peer, current: typeof current === 'string' ? current : '', sessions: set };
|
|
27
|
+
next.groups.set(peer, group);
|
|
28
|
+
for (const id of set) {
|
|
29
|
+
next.all.add(id);
|
|
30
|
+
if (!next.bySession.has(id))
|
|
31
|
+
next.bySession.set(id, group);
|
|
32
|
+
}
|
|
33
|
+
if (group.current !== '') {
|
|
34
|
+
next.currents.add(group.current);
|
|
35
|
+
next.bySession.set(group.current, group);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
if (raw && typeof raw.peers === 'object' && raw.peers !== null) {
|
|
39
|
+
for (const [peer, row] of Object.entries(raw.peers))
|
|
40
|
+
if (row && typeof row === 'object')
|
|
41
|
+
add(peer, row.current, row.sessions);
|
|
42
|
+
}
|
|
43
|
+
else if (raw && typeof raw === 'object') {
|
|
44
|
+
add('legacy', raw.current, raw.sessions);
|
|
45
|
+
}
|
|
46
|
+
cache = next;
|
|
14
47
|
return cache;
|
|
15
48
|
}
|
|
16
49
|
catch {
|
|
17
|
-
return
|
|
50
|
+
return empty(file);
|
|
18
51
|
}
|
|
19
52
|
}
|
|
20
|
-
/** Every conversation whose messages belong to this world's memory (current +
|
|
53
|
+
/** Every conversation whose messages belong to this world's memory (all QQ chats, current + earlier). */
|
|
21
54
|
export function qqScopeSessions(file) {
|
|
22
|
-
return snapshot(file).
|
|
55
|
+
return snapshot(file).all;
|
|
23
56
|
}
|
|
24
|
-
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
57
|
+
/** The conversations QQ currently speaks through — one per QQ chat. */
|
|
58
|
+
export function qqScopeCurrents(file) {
|
|
59
|
+
return snapshot(file).currents;
|
|
60
|
+
}
|
|
61
|
+
/** Kept for callers that expect one current: the first group's current ('' when none). */
|
|
28
62
|
export function qqScopeCurrent(file) {
|
|
29
|
-
|
|
63
|
+
for (const id of snapshot(file).currents)
|
|
64
|
+
return id;
|
|
65
|
+
return '';
|
|
30
66
|
}
|
|
31
|
-
/** Whether this session is one of the QQ-bound conversations (
|
|
67
|
+
/** Whether this session is one of the QQ-bound conversations (any chat, current or earlier). */
|
|
32
68
|
export function inQqScope(sessionId, file) {
|
|
33
69
|
return qqScopeSessions(file).has(String(sessionId));
|
|
34
70
|
}
|
|
35
|
-
/** Whether this session is the conversation QQ currently speaks through. */
|
|
71
|
+
/** Whether this session is the conversation some QQ chat currently speaks through. */
|
|
36
72
|
export function isQqCurrent(sessionId, file) {
|
|
37
|
-
|
|
38
|
-
|
|
73
|
+
return snapshot(file).currents.has(String(sessionId));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The conversations whose memory this session may read: its own QQ chat's group only.
|
|
77
|
+
* A session outside every group reads nothing (empty set).
|
|
78
|
+
*/
|
|
79
|
+
export function qqGroupOf(sessionId, file) {
|
|
80
|
+
const group = snapshot(file).bySession.get(String(sessionId));
|
|
81
|
+
return group ? group.sessions : new Set();
|
|
82
|
+
}
|
|
83
|
+
/** Which QQ chat (peerKey) this session belongs to; '' when none. */
|
|
84
|
+
export function qqPeerOf(sessionId, file) {
|
|
85
|
+
const group = snapshot(file).bySession.get(String(sessionId));
|
|
86
|
+
return group ? group.peer : '';
|
|
39
87
|
}
|