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
package/lib/engines.mjs
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
* 本插件还跳过挂载 ⇒ 页面所有引擎都“没加载”。
|
|
8
8
|
* - 现在只认 **当前 profile 的 dsh.profile.bundles** 或 `cordis.patch.yml` 里的
|
|
9
9
|
* 引擎行;都没有才从 vendor/ 挂载。repair 强制重挂,并先 dispose 旧挂载。
|
|
10
|
+
*
|
|
11
|
+
* 2026-11-08:挂载时不再一律传 `{}`。dsh-world 的 `scope: qq` / 会话清单 / 镜像路径
|
|
12
|
+
* 只在插件配置里,传空会让它退回默认(= QQ 记忆整个不工作)。这里从旧 profile 的
|
|
13
|
+
* `cordis.patch.yml` 把该引擎那一行 config 读出来合并,并补上 QQ 作用域的默认值。
|
|
10
14
|
*/
|
|
11
15
|
import { existsSync, readFileSync, readdirSync } from 'node:fs'
|
|
12
16
|
import { homedir } from 'node:os'
|
|
@@ -15,7 +19,7 @@ import { join } from 'node:path'
|
|
|
15
19
|
export const VENDORED_ENGINES = [
|
|
16
20
|
{ id: 'agent-md', pkg: 'dsh-agent-md', entry: '../vendor/dsh-agent-md/lib/index.js', config: {} },
|
|
17
21
|
{ id: 'bili-scroll', pkg: 'dsh-bili-scroll', entry: '../vendor/dsh-bili-scroll/index.js', config: {} },
|
|
18
|
-
{ id: 'world', pkg: 'dsh-world', entry: '../vendor/dsh-world/lib/index.js', config:
|
|
22
|
+
{ id: 'world', pkg: 'dsh-world', entry: '../vendor/dsh-world/lib/index.js', config: () => worldEngineConfig() },
|
|
19
23
|
{ id: 'meme', pkg: 'dsh-meme', entry: '../vendor/dsh-meme/index.js', config: {} },
|
|
20
24
|
{ id: 'video-storyboard', pkg: 'dsh-video-storyboard', entry: '../vendor/dsh-video-storyboard/src/index.js', config: {} },
|
|
21
25
|
]
|
|
@@ -39,6 +43,136 @@ function readText(file) {
|
|
|
39
43
|
try { return readFileSync(file, 'utf8') } catch { return '' }
|
|
40
44
|
}
|
|
41
45
|
|
|
46
|
+
/** 去掉 YAML 行尾注释:引号里的 # 不算注释。 */
|
|
47
|
+
function stripYamlComment(text) {
|
|
48
|
+
let quote = ''
|
|
49
|
+
for (let i = 0; i < text.length; i++) {
|
|
50
|
+
const ch = text[i]
|
|
51
|
+
if (quote !== '') {
|
|
52
|
+
if (ch === '\\' && quote === '"') { i++; continue }
|
|
53
|
+
if (ch === quote) quote = ''
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
if (ch === '"' || ch === "'") { quote = ch; continue }
|
|
57
|
+
if (ch === '#' && (i === 0 || /\s/.test(text[i - 1]))) return text.slice(0, i)
|
|
58
|
+
}
|
|
59
|
+
return text
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** YAML 标量:引号字符串 / 布尔 / null / 数字 / 行内数组;其它原样当字符串。 */
|
|
63
|
+
function parseScalar(raw) {
|
|
64
|
+
const value = String(raw).trim()
|
|
65
|
+
if (value === '') return ''
|
|
66
|
+
if (value.startsWith("'") && value.endsWith("'") && value.length >= 2) return value.slice(1, -1).replace(/''/g, "'")
|
|
67
|
+
if (value.startsWith('"') && value.endsWith('"') && value.length >= 2) {
|
|
68
|
+
try { return JSON.parse(value) } catch { return value.slice(1, -1) }
|
|
69
|
+
}
|
|
70
|
+
if (/^(true|false)$/i.test(value)) return value.toLowerCase() === 'true'
|
|
71
|
+
if (/^(null|~)$/i.test(value)) return null
|
|
72
|
+
if (/^-?\d+(\.\d+)?$/.test(value)) return Number(value)
|
|
73
|
+
if (value.startsWith('[') && value.endsWith(']')) {
|
|
74
|
+
const inner = value.slice(1, -1).trim()
|
|
75
|
+
if (inner === '') return []
|
|
76
|
+
return inner.split(',').map((part) => parseScalar(part))
|
|
77
|
+
}
|
|
78
|
+
return value
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 读一段 `config:` 下面的标量键(只认第一层;更深的对象/多行文本跳过)。
|
|
83
|
+
* 导出是为了能从 profile 补丁里把旧引擎配置迁移到内置挂载。
|
|
84
|
+
*/
|
|
85
|
+
export function parseConfigLines(lines, start, rowIndent) {
|
|
86
|
+
const out = {}
|
|
87
|
+
let childIndent = -1
|
|
88
|
+
for (let i = start; i < lines.length; i++) {
|
|
89
|
+
const line = lines[i]
|
|
90
|
+
if (line.trim() === '' || /^\s*#/.test(line)) continue
|
|
91
|
+
const indent = (line.match(/^ */) || [''])[0].length
|
|
92
|
+
if (indent <= rowIndent) break
|
|
93
|
+
if (/^\s*- /.test(line)) {
|
|
94
|
+
if (childIndent >= 0 && indent <= childIndent) break
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
const match = line.match(/^( *)([A-Za-z0-9_$.-]+):(?:[ \t]*(.*))?$/)
|
|
98
|
+
if (!match) continue
|
|
99
|
+
const keyIndent = match[1].length
|
|
100
|
+
if (childIndent < 0) childIndent = keyIndent
|
|
101
|
+
if (keyIndent < childIndent) break
|
|
102
|
+
if (keyIndent > childIndent) continue
|
|
103
|
+
const raw = stripYamlComment(match[3] || '').trim()
|
|
104
|
+
// 嵌套对象 / 多行文本(>- | 等)不迁移:宁可少给一项,也别把 YAML 记号塞进配置
|
|
105
|
+
if (raw === '' || /^[>|]/.test(raw) || raw.startsWith('&') || raw.startsWith('*')) { out[match[2]] = undefined; continue }
|
|
106
|
+
out[match[2]] = parseScalar(raw)
|
|
107
|
+
}
|
|
108
|
+
return out
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 从 profile 的 `cordis.patch.yml` 里找 `name: <pkg>` 那一行,读出它的 `config:`。
|
|
113
|
+
* 引擎改成本插件内置挂载以后,用户以前写在 profile 里的引擎配置不会自动跟过来;
|
|
114
|
+
* 这里把它读出来继续用(多个 profile 时,后读到的覆盖先读到的)。
|
|
115
|
+
*/
|
|
116
|
+
export function readProfileEngineConfig(pkg, dirs = activeProfileDirs()) {
|
|
117
|
+
const out = {}
|
|
118
|
+
const escaped = String(pkg).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
119
|
+
const nameRe = new RegExp('^\\s*name:\\s*[\'"]?' + escaped + '[\'"]?\\s*(#.*)?$')
|
|
120
|
+
for (const dir of dirs) {
|
|
121
|
+
const text = readText(join(dir, 'cordis.patch.yml'))
|
|
122
|
+
if (!text) continue
|
|
123
|
+
const lines = text.split(/\r?\n/)
|
|
124
|
+
for (let i = 0; i < lines.length; i++) {
|
|
125
|
+
if (!nameRe.test(lines[i])) continue
|
|
126
|
+
const rowIndent = (lines[i].match(/^ */) || [''])[0].length
|
|
127
|
+
for (let j = i + 1; j < lines.length; j++) {
|
|
128
|
+
const line = lines[j]
|
|
129
|
+
if (line.trim() === '' || /^\s*#/.test(line)) continue
|
|
130
|
+
const indent = (line.match(/^ */) || [''])[0].length
|
|
131
|
+
if (indent < rowIndent) break
|
|
132
|
+
if (indent !== rowIndent) continue
|
|
133
|
+
if (/^\s*- /.test(line)) break
|
|
134
|
+
const match = line.match(/^ *config:(?:[ \t]*(.*))?$/)
|
|
135
|
+
if (!match) continue
|
|
136
|
+
const inline = stripYamlComment(match[1] || '').trim()
|
|
137
|
+
if (inline !== '') break
|
|
138
|
+
Object.assign(out, parseConfigLines(lines, j + 1, rowIndent))
|
|
139
|
+
break
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return out
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 内置挂载 dsh-world 时用的配置:QQ 作用域 + 本机的记忆清单 / 镜像路径,
|
|
148
|
+
* 再叠加用户旧 profile 里配过的项(名字、life、路径…)。
|
|
149
|
+
*/
|
|
150
|
+
const WORLD_CONFIG_KEYS = new Set([
|
|
151
|
+
'worldId', 'agentPreset', 'timeZone', 'scope', 'qqSessionsFile', 'qqMirrorFile',
|
|
152
|
+
'companionName', 'partnerName', 'archiveScope', 'life', 'diaryDir', 'fallbackWakeTime',
|
|
153
|
+
'selfFile', 'worldPromptFile', 'alarmTemplateFile', 'personaFile', 'outlets', 'bridge',
|
|
154
|
+
'wechat', 'wechatAdapter', 'toast', 'allowedHosts', 'qqBridgeDir', 'qqBridgePort',
|
|
155
|
+
'qqBridgeNode', 'qqBridgeAutoStart', 'qqBridgeBindHomeSession',
|
|
156
|
+
])
|
|
157
|
+
|
|
158
|
+
export function worldEngineConfig(home = dshHome()) {
|
|
159
|
+
const agents = join(home, 'agents')
|
|
160
|
+
const config = {
|
|
161
|
+
scope: 'qq',
|
|
162
|
+
qqSessionsFile: join(agents, 'qq-memory.json'),
|
|
163
|
+
qqMirrorFile: join(agents, 'qq-memory.md'),
|
|
164
|
+
}
|
|
165
|
+
let saved = {}
|
|
166
|
+
try { saved = readProfileEngineConfig('dsh-world') } catch { saved = {} }
|
|
167
|
+
for (const [key, value] of Object.entries(saved)) {
|
|
168
|
+
if (!WORLD_CONFIG_KEYS.has(key)) continue
|
|
169
|
+
if (value === undefined || value === null) continue
|
|
170
|
+
if (typeof value === 'string' && value.trim() === '') continue
|
|
171
|
+
config[key] = value
|
|
172
|
+
}
|
|
173
|
+
return config
|
|
174
|
+
}
|
|
175
|
+
|
|
42
176
|
/** 这个引擎是否真的被当前 profile 的 bundles 或 patch 行加载。 */
|
|
43
177
|
export function engineInstalled(pkg) {
|
|
44
178
|
for (const dir of activeProfileDirs()) {
|
|
@@ -75,8 +209,36 @@ function selfBaseUrl() {
|
|
|
75
209
|
return 'http://127.0.0.1:' + (process.env.PORT || 3080)
|
|
76
210
|
}
|
|
77
211
|
|
|
78
|
-
/**
|
|
79
|
-
|
|
212
|
+
/**
|
|
213
|
+
* 这个引擎是不是作为 profile 的独立行被加载了(老用户的「单独安装」)。
|
|
214
|
+
*
|
|
215
|
+
* 为什么不能只靠 HTTP 探测:新版 DSH 给所有请求都加了登录校验,未登录时任何路径都可能回
|
|
216
|
+
* 401/403;把 401 当成「已加载」会让全新环境里本来该从 vendor 挂载的引擎被跳过。
|
|
217
|
+
*/
|
|
218
|
+
export function loadedFromProfile(ctx, pkg) {
|
|
219
|
+
try {
|
|
220
|
+
const loader = ctx && typeof ctx.get === 'function' ? ctx.get('loader') : null
|
|
221
|
+
const entries = loader && typeof loader.entries === 'function' ? loader.entries() : []
|
|
222
|
+
for (const entry of Array.isArray(entries) ? entries : []) {
|
|
223
|
+
const name = entry && entry.options && entry.options.name
|
|
224
|
+
if (typeof name !== 'string') continue
|
|
225
|
+
if (name !== pkg && !name.startsWith(pkg + '/')) continue
|
|
226
|
+
if (entry.disabled === true) continue
|
|
227
|
+
const fiber = entry.fiber
|
|
228
|
+
if (fiber === null || fiber === undefined) continue
|
|
229
|
+
// Cordis fiber.state:2 = active;老版本没有 state 时退回 runtime 是否就绪
|
|
230
|
+
if (typeof fiber.state === 'number') {
|
|
231
|
+
if (fiber.state !== 2) continue
|
|
232
|
+
} else if (fiber.runtime === null || fiber.runtime === undefined) continue
|
|
233
|
+
return true
|
|
234
|
+
}
|
|
235
|
+
} catch { /* 老宿主没有 loader 服务 */ }
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** 真正有没有跑起来:先看 profile 行,再看 tools 注册,最后用 HTTP 路由的 JSON 应答兜底。 */
|
|
240
|
+
export async function engineLoaded(ctx, engine) {
|
|
241
|
+
if (loadedFromProfile(ctx, engine.pkg)) return true
|
|
80
242
|
const tools = ctx.get('tools')
|
|
81
243
|
const names = TOOL_NAMES[engine.id]
|
|
82
244
|
if (names && tools && typeof tools.list === 'function') {
|
|
@@ -90,8 +252,11 @@ async function engineLoaded(ctx, engine) {
|
|
|
90
252
|
if (route) {
|
|
91
253
|
try {
|
|
92
254
|
const r = await fetch(selfBaseUrl() + route, { signal: AbortSignal.timeout(3000) })
|
|
93
|
-
// 401/403
|
|
94
|
-
|
|
255
|
+
// 只认「真的返回了 JSON」:401/403(宿主登录校验)和 SPA 的 HTML 兜底都不能算已加载。
|
|
256
|
+
if (r.status !== 200) return false
|
|
257
|
+
if (!/json/i.test(String(r.headers.get('content-type') || ''))) return false
|
|
258
|
+
const body = await r.json()
|
|
259
|
+
return Boolean(body && typeof body === 'object')
|
|
95
260
|
} catch { return false }
|
|
96
261
|
}
|
|
97
262
|
return false
|
|
@@ -114,8 +279,10 @@ export async function mountVendoredEngines(ctx, { force = false } = {}) {
|
|
|
114
279
|
try {
|
|
115
280
|
const mod = await import(new URL(engine.entry, import.meta.url))
|
|
116
281
|
if (typeof ctx.plugin !== 'function') throw new Error('当前宿主没有 ctx.plugin')
|
|
117
|
-
const
|
|
282
|
+
const config = typeof engine.config === 'function' ? engine.config() : engine.config
|
|
283
|
+
const fiber = ctx.plugin(mod, config)
|
|
118
284
|
mounted.set(engine.id, fiber)
|
|
285
|
+
if (engine.id === 'world') console.log('[companion] dsh-world 以 QQ 作用域挂载:清单 ' + config.qqSessionsFile + ' / 镜像 ' + config.qqMirrorFile)
|
|
119
286
|
results.push({ id: engine.id, pkg: engine.pkg, installed: false, mounted: true })
|
|
120
287
|
console.log(`[companion] 已内置挂载引擎 ${engine.pkg}`)
|
|
121
288
|
} catch (error) {
|
|
@@ -129,7 +296,7 @@ export async function mountVendoredEngines(ctx, { force = false } = {}) {
|
|
|
129
296
|
/** 一键修复:先卸掉上次 vendor 挂载,再强制全部重挂(不动 profile 里已单独加载的引擎)。 */
|
|
130
297
|
export async function repairVendoredEngines(ctx) {
|
|
131
298
|
for (const fiber of mounted.values()) {
|
|
132
|
-
try { fiber?.dispose?.() } catch { /* 旧挂载清理失败不影响重挂 */ }
|
|
299
|
+
try { await fiber?.dispose?.() } catch { /* 旧挂载清理失败不影响重挂 */ }
|
|
133
300
|
}
|
|
134
301
|
mounted.clear()
|
|
135
302
|
return mountVendoredEngines(ctx, { force: false })
|