dsh-dock 0.23.1 → 0.24.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 +32 -40
- package/client.js +599 -236
- package/features/animation/host.js +3 -2
- package/features/balance/host.js +3 -3
- package/features/dshtools/host.js +306 -0
- package/features/dshtools/view.jsx +403 -0
- package/features/mcpconfig/host.js +3 -3
- package/features/mcpconfig/view.js +8 -8
- package/features/mobile-relay/host.js +42 -9
- package/features/modelconfig/host.js +3 -3
- package/features/notify/host.js +3 -2
- package/features/notify/view.jsx +38 -46
- package/features/providerconfig/host.js +3 -3
- package/features/runstate/host.js +3 -2
- package/features/tokenlog/host.js +3 -2
- package/features/tts/host.js +3 -2
- package/index.js +5 -2
- package/package.json +5 -6
- package/src/client.jsx +5 -2
- package/src/host-core.js +52 -13
- package/screenshots/dock-animation.png +0 -0
- package/screenshots/dock-balance.png +0 -0
- package/screenshots/dock-games.png +0 -0
- package/screenshots/dock-home.png +0 -0
- package/screenshots/dock-modelconfig-provider-create.png +0 -0
- package/screenshots/dock-modelconfig-provider-edit.png +0 -0
- package/screenshots/dock-modelconfig-providers.png +0 -0
- package/screenshots/dock-modelconfig.png +0 -0
- package/screenshots/dock-notify.png +0 -0
- package/screenshots/dock-runstate.png +0 -0
- package/screenshots/dock-settings.png +0 -0
- package/screenshots/dock-tokenlog-charts-2.png +0 -0
- package/screenshots/dock-tokenlog-charts-3.png +0 -0
- package/screenshots/dock-tokenlog-charts.png +0 -0
- package/screenshots/dock-tokenlog.png +0 -0
|
@@ -61,9 +61,10 @@ export const feature = {
|
|
|
61
61
|
disposers.push(ctx.inject(['webServer'], (wsCtx) => {
|
|
62
62
|
wsCtx.effect(() => wsCtx.webServer.register({
|
|
63
63
|
kind: 'prefix',
|
|
64
|
+
auth: 'required',
|
|
64
65
|
path: '/dsh-dock/animation',
|
|
65
|
-
async handler(req, res) {
|
|
66
|
-
if (rejectUntrustedRequest(ctx, req, res)) return
|
|
66
|
+
async handler(req, res, peer) {
|
|
67
|
+
if (rejectUntrustedRequest(ctx, req, res, peer)) return
|
|
67
68
|
try {
|
|
68
69
|
const url = new URL(req.url || '/', 'http://dsh.internal')
|
|
69
70
|
const method = url.pathname.replace(/^\/dsh-dock\/animation\/?/, '').split('/')[0] || ''
|
package/features/balance/host.js
CHANGED
|
@@ -601,8 +601,8 @@ export const feature = {
|
|
|
601
601
|
setup(ctx) {
|
|
602
602
|
const webServer = ctx.get('webServer')
|
|
603
603
|
if (webServer === undefined) throw new Error('webServer 服务不可用,无法提供余额查询路由')
|
|
604
|
-
const handler = async (req, res) => {
|
|
605
|
-
if (rejectUntrustedRequest(ctx, req, res)) return
|
|
604
|
+
const handler = async (req, res, peer) => {
|
|
605
|
+
if (rejectUntrustedRequest(ctx, req, res, peer)) return
|
|
606
606
|
try {
|
|
607
607
|
sendJson(res, 200, await queryAll(ctx))
|
|
608
608
|
} catch (e) {
|
|
@@ -612,6 +612,6 @@ export const feature = {
|
|
|
612
612
|
})
|
|
613
613
|
}
|
|
614
614
|
}
|
|
615
|
-
return webServer.register({ kind: 'exact', path: '/dsh-dock/balance', handler })
|
|
615
|
+
return webServer.register({ kind: 'exact', auth: 'required', path: '/dsh-dock/balance', handler })
|
|
616
616
|
},
|
|
617
617
|
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
// dsh-dock · 功能模块【DSH功能】· 宿主半部
|
|
2
|
+
//
|
|
3
|
+
// 给 DSH 自带界面补充能力的开关集合。结构是「分组 × 勾选项」,和面板里其它功能一样
|
|
4
|
+
// 由功能开关统一启停;组内每一项都是独立勾选,不勾选就完全不介入(客户端不显示入口)。
|
|
5
|
+
//
|
|
6
|
+
// GET /dsh-dock/dshtools → { ok, groups:[{ id, name, description, items:[{ key, name, description, value }] }] }
|
|
7
|
+
// POST /dsh-dock/dshtools { group, key, value:boolean } → 同上(写回 settings 的 dshtools 段)
|
|
8
|
+
// GET /dsh-dock/download?path=/abs/file → 流式下载原文件(Content-Disposition: attachment)
|
|
9
|
+
//
|
|
10
|
+
// 为什么要单独一条下载路由:DSH 内置的右键「下载」走的是 Remote 整文件读取
|
|
11
|
+
// (workspaceFiles.readBytes),默认上限 maxFileBytes = 32 MiB,超过直接失败
|
|
12
|
+
// (workspace-file/too-large),前端只弹一句「无法保存文件」。这条路由按块
|
|
13
|
+
// readByteRange 流式发送,不受该上限影响,也不把整份文件读进内存。
|
|
14
|
+
//
|
|
15
|
+
// 新增一项能力:在 DSH_TOOL_GROUPS 对应分组下加一项(key/name/description/default),
|
|
16
|
+
// 客户端页面会自动多出这个勾选框;宿主按 settingValue(ctx, group, key) 决定行为。
|
|
17
|
+
import { isAbsolute } from 'node:path'
|
|
18
|
+
import { readBody, readDockRoot, mutateDockSection, rejectUntrustedRequest, sendJson } from '../../src/host-core.js'
|
|
19
|
+
|
|
20
|
+
/** 内置「下载」Remote 的整文件默认上限(packages/api/workspace-files 的 maxFileBytes)。 */
|
|
21
|
+
export const LARGE_FILE_BYTES = 32 * 1024 * 1024
|
|
22
|
+
|
|
23
|
+
/** 流式分块大小:1 MiB。足够小,16 路并发下载也只占十几 MiB 内存。 */
|
|
24
|
+
const CHUNK_BYTES = 1024 * 1024
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 分组清单:客户端页面渲染与宿主行为共用同一份键名。
|
|
28
|
+
* 默认值 = 首次运行(settings 里还没有记录)时的勾选状态。
|
|
29
|
+
*/
|
|
30
|
+
export const DSH_TOOL_GROUPS = [
|
|
31
|
+
{
|
|
32
|
+
id: 'fileContextMenu',
|
|
33
|
+
name: '文件右键',
|
|
34
|
+
description: '文件卡片、文件树右键菜单里的补充项。',
|
|
35
|
+
items: [
|
|
36
|
+
{
|
|
37
|
+
key: 'streamingDownload',
|
|
38
|
+
name: '大文件下载(流式)',
|
|
39
|
+
description: '右键「下载」改由本模块流式发送原文件,不再受 32 MiB 预览读取上限限制;大文件另提供「下载(大文件)」入口。',
|
|
40
|
+
default: true,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
/** 当前勾选值:清单里的每一项都有值(缺省用 default),未知键忽略。 */
|
|
47
|
+
export function readDshtoolsValues(ctx) {
|
|
48
|
+
const root = readDockRoot(ctx)
|
|
49
|
+
const saved = root && typeof root === 'object' && root.dshtools && typeof root.dshtools === 'object'
|
|
50
|
+
? root.dshtools
|
|
51
|
+
: {}
|
|
52
|
+
const values = {}
|
|
53
|
+
for (const group of DSH_TOOL_GROUPS) {
|
|
54
|
+
const row = saved[group.id] && typeof saved[group.id] === 'object' ? saved[group.id] : {}
|
|
55
|
+
const out = {}
|
|
56
|
+
for (const item of group.items) {
|
|
57
|
+
out[item.key] = typeof row[item.key] === 'boolean' ? row[item.key] : item.default
|
|
58
|
+
}
|
|
59
|
+
values[group.id] = out
|
|
60
|
+
}
|
|
61
|
+
return values
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 读单项勾选值。
|
|
66
|
+
* @param {object} ctx Cordis context
|
|
67
|
+
* @param {string} groupId 分组 id(DSH_TOOL_GROUPS[i].id)
|
|
68
|
+
* @param {string} key 功能项 key
|
|
69
|
+
* @returns {boolean|undefined} 未登记的分组/键返回 undefined
|
|
70
|
+
*/
|
|
71
|
+
export function settingValue(ctx, groupId, key) {
|
|
72
|
+
const row = readDshtoolsValues(ctx)[groupId]
|
|
73
|
+
return row ? row[key] : undefined
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 清单 + 当前值,客户端直接渲染这份结构。 */
|
|
77
|
+
function groupsPayload(ctx) {
|
|
78
|
+
const values = readDshtoolsValues(ctx)
|
|
79
|
+
return DSH_TOOL_GROUPS.map((group) => ({
|
|
80
|
+
id: group.id,
|
|
81
|
+
name: group.name,
|
|
82
|
+
description: group.description,
|
|
83
|
+
items: group.items.map((item) => ({
|
|
84
|
+
key: item.key,
|
|
85
|
+
name: item.name,
|
|
86
|
+
description: item.description,
|
|
87
|
+
value: values[group.id][item.key] === true,
|
|
88
|
+
})),
|
|
89
|
+
}))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 设置读写路由(GET 读、POST 单项切换)。 */
|
|
93
|
+
async function handleSettings(ctx, req, res) {
|
|
94
|
+
try {
|
|
95
|
+
if (req.method === 'GET') {
|
|
96
|
+
sendJson(res, 200, { ok: true, groups: groupsPayload(ctx) })
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
if (req.method === 'POST') {
|
|
100
|
+
const body = await readBody(req)
|
|
101
|
+
const groupId = body && typeof body.group === 'string' ? body.group : ''
|
|
102
|
+
const key = body && typeof body.key === 'string' ? body.key : ''
|
|
103
|
+
const value = body ? body.value : undefined
|
|
104
|
+
const group = DSH_TOOL_GROUPS.find((g) => g.id === groupId)
|
|
105
|
+
const item = group && group.items.find((i) => i.key === key)
|
|
106
|
+
if (!group || !item) {
|
|
107
|
+
sendJson(res, 400, { ok: false, error: '未知的分组或功能项' })
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
if (typeof value !== 'boolean') {
|
|
111
|
+
sendJson(res, 400, { ok: false, error: 'value 必须是布尔值' })
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
// 整段回写:清单里未知的旧键由 readDshtoolsValues 过滤,不会把设置写脏。
|
|
115
|
+
const current = readDshtoolsValues(ctx)
|
|
116
|
+
const next = {}
|
|
117
|
+
for (const g of DSH_TOOL_GROUPS) next[g.id] = { ...current[g.id] }
|
|
118
|
+
next[groupId][key] = value
|
|
119
|
+
await mutateDockSection(ctx, ['dshtools'], next)
|
|
120
|
+
sendJson(res, 200, { ok: true, groups: groupsPayload(ctx) })
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
sendJson(res, 405, { ok: false, error: `不支持的请求方法:${req.method}` })
|
|
124
|
+
} catch (error) {
|
|
125
|
+
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 取路径最后一段作建议文件名(兼容 Windows 分隔符)。 */
|
|
130
|
+
function baseName(path) {
|
|
131
|
+
const name = path.slice(Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')) + 1)
|
|
132
|
+
return name.length > 0 ? name : 'download'
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** RFC 5987 编码:非 ASCII 文件名(中文等)必须走 filename*。 */
|
|
136
|
+
function rfc5987(value) {
|
|
137
|
+
return encodeURIComponent(value).replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** attachment 头:ASCII 兜底 + filename* 真名。 */
|
|
141
|
+
function contentDisposition(name) {
|
|
142
|
+
const fallback = name.replace(/[^\x20-\x7e]/g, '_').replace(/["\\]/g, '_')
|
|
143
|
+
return `attachment; filename="${fallback}"; filename*=UTF-8''${rfc5987(name)}`
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 写响应并等背压解除;连接提前关闭也 resolve,避免挂死。 */
|
|
147
|
+
function drain(res) {
|
|
148
|
+
return new Promise((resolve) => {
|
|
149
|
+
const done = () => {
|
|
150
|
+
res.off('drain', done)
|
|
151
|
+
res.off('close', done)
|
|
152
|
+
resolve()
|
|
153
|
+
}
|
|
154
|
+
res.once('drain', done)
|
|
155
|
+
res.once('close', done)
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 已开始响应后的失败只能断开连接(头已发出,改不了状态码)。 */
|
|
160
|
+
function fail(res, status, message) {
|
|
161
|
+
if (res.headersSent) {
|
|
162
|
+
res.destroy()
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
sendJson(res, status, { ok: false, error: { code: 'download-failed', message } })
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* GET/HEAD /dsh-dock/download?path=/abs/file
|
|
170
|
+
* 流式发送原文件;路径可读性由 ctx.fs 的读取策略决定(与 DSH 的 /api/file 同一套)。
|
|
171
|
+
*/
|
|
172
|
+
async function handleDownload(ctx, req, res) {
|
|
173
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
174
|
+
res.writeHead(405, { allow: 'GET, HEAD', 'content-type': 'application/json; charset=utf-8' })
|
|
175
|
+
res.end(JSON.stringify({ ok: false, error: { code: 'method-not-allowed', message: '只支持 GET / HEAD' } }))
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
const fs = ctx.get('fs')
|
|
179
|
+
if (!fs || typeof fs.readByteRange !== 'function' || typeof fs.resolve !== 'function' || typeof fs.stat !== 'function') {
|
|
180
|
+
fail(res, 503, '文件系统服务不可用,无法下载')
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
let url
|
|
184
|
+
try {
|
|
185
|
+
url = new URL(req.url || '/', 'http://dsh.invalid')
|
|
186
|
+
} catch {
|
|
187
|
+
fail(res, 400, '请求地址无法解析')
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
// 客户端启动探针:只证明「路由已注册 + 过了信任栅栏」,不读任何文件。
|
|
191
|
+
// 探测通过后客户端才接管 DSH 自带的「下载」,宿主旧版本时保持原行为。
|
|
192
|
+
if (url.searchParams.has('probe')) {
|
|
193
|
+
res.writeHead(204, { 'cache-control': 'no-store' })
|
|
194
|
+
res.end()
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
const path = url.searchParams.get('path')
|
|
198
|
+
if (path === null || path.length === 0) {
|
|
199
|
+
fail(res, 400, '缺少 path 参数')
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
if (path.includes('\0') || !isAbsolute(path)) {
|
|
203
|
+
fail(res, 400, 'path 必须是绝对路径')
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
const abort = new AbortController()
|
|
207
|
+
let settled = false
|
|
208
|
+
const onClose = () => { if (!settled) abort.abort() }
|
|
209
|
+
res.on('close', onClose)
|
|
210
|
+
try {
|
|
211
|
+
const target = await fs.resolve(path, { signal: abort.signal })
|
|
212
|
+
const info = await fs.stat(target, abort.signal)
|
|
213
|
+
if (info === undefined) {
|
|
214
|
+
fail(res, 404, '文件不存在')
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
if (info.type !== 'file') {
|
|
218
|
+
fail(res, 403, '不是普通文件')
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
const size = typeof info.size === 'number' ? info.size : undefined
|
|
222
|
+
const headers = {
|
|
223
|
+
'content-type': 'application/octet-stream',
|
|
224
|
+
'content-disposition': contentDisposition(baseName(path)),
|
|
225
|
+
'cache-control': 'private, no-store',
|
|
226
|
+
'x-content-type-options': 'nosniff',
|
|
227
|
+
...(size === undefined ? {} : { 'content-length': String(size) }),
|
|
228
|
+
}
|
|
229
|
+
res.writeHead(200, headers)
|
|
230
|
+
if (req.method === 'HEAD') {
|
|
231
|
+
res.end()
|
|
232
|
+
return
|
|
233
|
+
}
|
|
234
|
+
if (size === 0) {
|
|
235
|
+
res.end()
|
|
236
|
+
return
|
|
237
|
+
}
|
|
238
|
+
let offset = 0
|
|
239
|
+
for (;;) {
|
|
240
|
+
if (abort.signal.aborted) break
|
|
241
|
+
const length = size === undefined ? CHUNK_BYTES : Math.min(CHUNK_BYTES, size - offset)
|
|
242
|
+
if (length <= 0) break
|
|
243
|
+
const chunk = await fs.readByteRange(target, { offset, length }, abort.signal)
|
|
244
|
+
if (!chunk || chunk.length === 0) break
|
|
245
|
+
const last = chunk.length < length
|
|
246
|
+
if (!res.write(chunk)) await drain(res)
|
|
247
|
+
offset += chunk.length
|
|
248
|
+
if (last) break
|
|
249
|
+
}
|
|
250
|
+
res.end()
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (abort.signal.aborted) {
|
|
253
|
+
res.destroy()
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
// 与 DSH 的 /api/file 同一套错误→状态码映射:读不到/越权不伪装成 500。
|
|
257
|
+
const code = error && typeof error === 'object' && 'code' in error ? error.code : ''
|
|
258
|
+
const status = code === 'FS_NOT_FOUND'
|
|
259
|
+
? 404
|
|
260
|
+
: code === 'FS_PERMISSION_DENIED' || code === 'FS_SANDBOX_DENIED'
|
|
261
|
+
? 403
|
|
262
|
+
: 500
|
|
263
|
+
fail(res, status, error instanceof Error ? error.message : String(error))
|
|
264
|
+
} finally {
|
|
265
|
+
settled = true
|
|
266
|
+
res.off('close', onClose)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** 功能注册表条目:index.js 组装 FEATURES 时使用。 */
|
|
271
|
+
export const feature = {
|
|
272
|
+
id: 'dshtools',
|
|
273
|
+
name: 'DSH功能',
|
|
274
|
+
description: '给 DSH 自带界面补充能力(文件右键等),按分组勾选启用',
|
|
275
|
+
defaultEnabled: true,
|
|
276
|
+
setup(ctx) {
|
|
277
|
+
const webServer = ctx.get('webServer')
|
|
278
|
+
if (webServer === undefined) throw new Error('webServer 服务不可用,无法提供 DSH功能路由')
|
|
279
|
+
const disposers = [
|
|
280
|
+
webServer.register({
|
|
281
|
+
kind: 'exact',
|
|
282
|
+
auth: 'required',
|
|
283
|
+
path: '/dsh-dock/dshtools',
|
|
284
|
+
// 信任栅栏必须挂在注册点旁边(静态约定见 scripts/test-route-trust-host.mjs)
|
|
285
|
+
handler: (req, res, peer) => {
|
|
286
|
+
if (rejectUntrustedRequest(ctx, req, res, peer)) return
|
|
287
|
+
void handleSettings(ctx, req, res)
|
|
288
|
+
},
|
|
289
|
+
}),
|
|
290
|
+
webServer.register({
|
|
291
|
+
kind: 'exact',
|
|
292
|
+
auth: 'required',
|
|
293
|
+
path: '/dsh-dock/download',
|
|
294
|
+
handler: (req, res, peer) => {
|
|
295
|
+
if (rejectUntrustedRequest(ctx, req, res, peer)) return
|
|
296
|
+
void handleDownload(ctx, req, res)
|
|
297
|
+
},
|
|
298
|
+
}),
|
|
299
|
+
]
|
|
300
|
+
return () => {
|
|
301
|
+
for (const dispose of disposers) {
|
|
302
|
+
try { dispose() } catch { /* 已卸载 */ }
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
}
|