dsh-app-controls 0.0.0-stage → 0.5.2
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/LICENSE +21 -0
- package/README.md +249 -2
- package/cordis.patch.yml +4 -0
- package/icon.svg +6 -0
- package/lib/cache.js +491 -0
- package/lib/client.js +1476 -0
- package/lib/index.js +647 -0
- package/lib/settings.js +186 -0
- package/lib/update.js +518 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +69 -4
- package/tray/dsh-tray.py +975 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-app-controls — host half.
|
|
3
|
+
*
|
|
4
|
+
* Web routes below `/api` (authenticated by Connection):
|
|
5
|
+
* POST /api/dsh-app-controls/quit
|
|
6
|
+
* GET|POST /api/dsh-app-controls/update
|
|
7
|
+
* GET /api/dsh-app-controls/update-progress
|
|
8
|
+
* GET /api/dsh-app-controls/state
|
|
9
|
+
*
|
|
10
|
+
* A local Unix-socket control channel for the KDE tray helper
|
|
11
|
+
* (`~/.local/state/dsh-app-controls/control.sock`, mode 0600) exposes the
|
|
12
|
+
* same operations without storing browser credentials in the helper:
|
|
13
|
+
* status | check-update | update | quit
|
|
14
|
+
*
|
|
15
|
+
* Quit/update requests reach the open Web page through the `state` route's
|
|
16
|
+
* `quitRequested` flag, so the tray can close the window as well as stop the
|
|
17
|
+
* service (and restart it later).
|
|
18
|
+
*/
|
|
19
|
+
import { appendFileSync, chmodSync, mkdirSync, unlinkSync } from 'node:fs'
|
|
20
|
+
import net from 'node:net'
|
|
21
|
+
import os from 'node:os'
|
|
22
|
+
import path from 'node:path'
|
|
23
|
+
import { CACHE_CATEGORIES, cleanCache, packageManagerBusy, scanCache } from './cache.js'
|
|
24
|
+
import { checkUpdates, locateInstall, updateTo } from './update.js'
|
|
25
|
+
import { applyTraySettings, normalizeSettings, readSettings, writeSettings } from './settings.js'
|
|
26
|
+
|
|
27
|
+
export const name = 'dsh-app-controls'
|
|
28
|
+
|
|
29
|
+
/** Wait for Connection before registering routes; it owns the `/api` carrier. */
|
|
30
|
+
export const inject = ['connection']
|
|
31
|
+
|
|
32
|
+
const STATE_DIR = path.join(
|
|
33
|
+
process.env.XDG_STATE_HOME ?? path.join(os.homedir(), '.local', 'state'),
|
|
34
|
+
'dsh-app-controls',
|
|
35
|
+
)
|
|
36
|
+
const LOG_FILE = path.join(STATE_DIR, 'app-controls.log')
|
|
37
|
+
const CONTROL_SOCKET = path.join(STATE_DIR, 'control.sock')
|
|
38
|
+
const CONTROL_DISABLED = process.env.DSH_APP_CONTROLS_DISABLE_SOCKET === '1'
|
|
39
|
+
|
|
40
|
+
const QUIT_PATH = '/api/dsh-app-controls/quit'
|
|
41
|
+
const UPDATE_PATH = '/api/dsh-app-controls/update'
|
|
42
|
+
const PROGRESS_PATH = '/api/dsh-app-controls/update-progress'
|
|
43
|
+
const STATE_PATH = '/api/dsh-app-controls/state'
|
|
44
|
+
const SETTINGS_PATH = '/api/dsh-app-controls/settings'
|
|
45
|
+
const CACHE_PATH = '/api/dsh-app-controls/cache'
|
|
46
|
+
const CACHE_CLEAN_PATH = '/api/dsh-app-controls/cache/clean'
|
|
47
|
+
|
|
48
|
+
const UPDATE_CACHE_TTL_MS = 5 * 60 * 1000
|
|
49
|
+
const UPDATE_EXIT_DELAY_MS = 3000
|
|
50
|
+
const TRAY_UPDATE_EXIT_DELAY_MS = 4500
|
|
51
|
+
const QUIT_EXIT_DELAY_MS = 800
|
|
52
|
+
const TRAY_QUIT_EXIT_DELAY_MS = 4500
|
|
53
|
+
const SERVER_STARTED_AT = Date.now()
|
|
54
|
+
let cacheRunning = false
|
|
55
|
+
|
|
56
|
+
function describe(value) {
|
|
57
|
+
if (typeof value === 'string') return value
|
|
58
|
+
if (value instanceof Error) return value.message
|
|
59
|
+
try {
|
|
60
|
+
return JSON.stringify(value)
|
|
61
|
+
} catch {
|
|
62
|
+
return String(value)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function log(message, detail) {
|
|
67
|
+
const suffix = detail === undefined ? '' : ` ${describe(detail)}`
|
|
68
|
+
const line = `${new Date().toISOString()} ${message}${suffix}`
|
|
69
|
+
try {
|
|
70
|
+
mkdirSync(STATE_DIR, { recursive: true, mode: 0o700 })
|
|
71
|
+
appendFileSync(LOG_FILE, `${line}\n`)
|
|
72
|
+
} catch {
|
|
73
|
+
// Logging must never break a quit/update request.
|
|
74
|
+
}
|
|
75
|
+
console.log(`[dsh-app-controls] ${line}`)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function json(status, body) {
|
|
79
|
+
return new Response(JSON.stringify(body), {
|
|
80
|
+
status,
|
|
81
|
+
headers: {
|
|
82
|
+
'content-type': 'application/json; charset=utf-8',
|
|
83
|
+
'cache-control': 'no-store',
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Refuse destructive actions from deployments that accept non-loopback authorities. */
|
|
89
|
+
function remoteRefusal(ctx) {
|
|
90
|
+
if (ctx.connection.allowsRemoteAuthorities) {
|
|
91
|
+
return json(403, {
|
|
92
|
+
ok: false,
|
|
93
|
+
error: 'remote-access-not-allowed',
|
|
94
|
+
message: '当前部署允许远程访问:界面退出/更新仅限本机使用。',
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
return undefined
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The launcher-provided exit service; optional so embedded hosts answer cleanly. */
|
|
101
|
+
function exitHook(ctx) {
|
|
102
|
+
const appExit = ctx.get('appExit')
|
|
103
|
+
return typeof appExit === 'function' ? appExit : undefined
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function scheduleExit(ctx, appExit, delayMs) {
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
try {
|
|
109
|
+
appExit(0)
|
|
110
|
+
} catch (error) {
|
|
111
|
+
log('appExit threw', error)
|
|
112
|
+
}
|
|
113
|
+
}, delayMs)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function messageForUpdateError(result) {
|
|
117
|
+
switch (result.error) {
|
|
118
|
+
case 'update-timeout':
|
|
119
|
+
return '更新超时:下载或安装耗时过长,请检查网络后重试。'
|
|
120
|
+
case 'copy-failed':
|
|
121
|
+
return `准备更新副本失败:${result.detail || '未知错误'}`
|
|
122
|
+
case 'swap-failed':
|
|
123
|
+
return `切换新版本失败:${result.detail || '未知错误'}`
|
|
124
|
+
case 'version-unchanged':
|
|
125
|
+
return '安装后版本未变化,更新未生效。'
|
|
126
|
+
case 'version-unreadable':
|
|
127
|
+
return '无法读取新版本信息,更新未生效。'
|
|
128
|
+
default:
|
|
129
|
+
return `更新失败:${result.detail || result.error}`
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** One process-local update job; the Web UI/tray polls its snapshot. */
|
|
134
|
+
const job = {
|
|
135
|
+
started: false,
|
|
136
|
+
active: false,
|
|
137
|
+
done: false,
|
|
138
|
+
error: null,
|
|
139
|
+
source: null,
|
|
140
|
+
phase: 'idle',
|
|
141
|
+
percent: 0,
|
|
142
|
+
message: '',
|
|
143
|
+
activity: '',
|
|
144
|
+
startedAt: 0,
|
|
145
|
+
finishedAt: 0,
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function jobSnapshot() {
|
|
149
|
+
const now = Date.now()
|
|
150
|
+
const elapsedMs = job.startedAt === 0
|
|
151
|
+
? 0
|
|
152
|
+
: (job.finishedAt !== 0 ? job.finishedAt : now) - job.startedAt
|
|
153
|
+
return {
|
|
154
|
+
ok: true,
|
|
155
|
+
started: job.started,
|
|
156
|
+
active: job.active,
|
|
157
|
+
done: job.done,
|
|
158
|
+
error: job.error,
|
|
159
|
+
source: job.source,
|
|
160
|
+
phase: job.phase,
|
|
161
|
+
percent: job.percent,
|
|
162
|
+
message: job.message,
|
|
163
|
+
activity: job.activity,
|
|
164
|
+
elapsedMs,
|
|
165
|
+
from: job.from,
|
|
166
|
+
to: job.to,
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const updateCache = { checkedAt: 0, value: null }
|
|
171
|
+
const quitState = { requested: false }
|
|
172
|
+
|
|
173
|
+
function installedVersion() {
|
|
174
|
+
try {
|
|
175
|
+
return locateInstall(process.argv[1]).version
|
|
176
|
+
} catch {
|
|
177
|
+
return null
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function serverFacts(ctx) {
|
|
182
|
+
try {
|
|
183
|
+
const webServer = ctx.get('webServer')
|
|
184
|
+
if (webServer !== undefined && webServer !== null) {
|
|
185
|
+
return { host: webServer.host ?? null, port: webServer.port ?? null }
|
|
186
|
+
}
|
|
187
|
+
} catch {
|
|
188
|
+
// the listener may not be bound yet
|
|
189
|
+
}
|
|
190
|
+
return { host: null, port: null }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function statusSnapshot(ctx) {
|
|
194
|
+
const facts = serverFacts(ctx)
|
|
195
|
+
return {
|
|
196
|
+
ok: true,
|
|
197
|
+
running: true,
|
|
198
|
+
pid: process.pid,
|
|
199
|
+
version: installedVersion(),
|
|
200
|
+
host: facts.host,
|
|
201
|
+
port: facts.port,
|
|
202
|
+
uptimeMs: Math.max(0, Date.now() - SERVER_STARTED_AT),
|
|
203
|
+
quitRequested: quitState.requested,
|
|
204
|
+
job: jobSnapshot(),
|
|
205
|
+
update: updateCache.value,
|
|
206
|
+
updateCheckedAt: updateCache.checkedAt || null,
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function getUpdateStatus(force) {
|
|
211
|
+
if (!force && updateCache.value !== null && Date.now() - updateCache.checkedAt < UPDATE_CACHE_TTL_MS) {
|
|
212
|
+
return updateCache.value
|
|
213
|
+
}
|
|
214
|
+
const value = await checkUpdates()
|
|
215
|
+
updateCache.value = value
|
|
216
|
+
updateCache.checkedAt = Date.now()
|
|
217
|
+
return value
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function requestQuit(ctx, appExit, source, delayMs) {
|
|
221
|
+
quitState.requested = true
|
|
222
|
+
log(`quit requested (${source})`)
|
|
223
|
+
scheduleExit(ctx, appExit, delayMs)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function runUpdateJob(ctx, appExit, status, source) {
|
|
227
|
+
Object.assign(job, {
|
|
228
|
+
started: true,
|
|
229
|
+
active: true,
|
|
230
|
+
done: false,
|
|
231
|
+
error: null,
|
|
232
|
+
source,
|
|
233
|
+
phase: 'copy',
|
|
234
|
+
percent: 1,
|
|
235
|
+
message: '正在准备更新…',
|
|
236
|
+
activity: '',
|
|
237
|
+
startedAt: Date.now(),
|
|
238
|
+
finishedAt: 0,
|
|
239
|
+
from: status.current,
|
|
240
|
+
to: status.candidate,
|
|
241
|
+
})
|
|
242
|
+
let exitScheduled = false
|
|
243
|
+
try {
|
|
244
|
+
const result = await updateTo(status.candidate, undefined, (event) => {
|
|
245
|
+
job.phase = event.phase
|
|
246
|
+
job.percent = Math.max(0, Math.min(100, Math.round(event.percent ?? 0)))
|
|
247
|
+
job.message = event.message ?? ''
|
|
248
|
+
job.activity = event.activity ?? ''
|
|
249
|
+
})
|
|
250
|
+
if (!result.ok) {
|
|
251
|
+
job.error = result.error
|
|
252
|
+
job.message = messageForUpdateError(result)
|
|
253
|
+
log('update failed', result)
|
|
254
|
+
} else {
|
|
255
|
+
job.phase = 'done'
|
|
256
|
+
job.percent = 100
|
|
257
|
+
job.message = `更新完成:DeepSeek Harness ${result.to}`
|
|
258
|
+
job.to = result.to
|
|
259
|
+
job.error = null
|
|
260
|
+
exitScheduled = true
|
|
261
|
+
if (source === 'tray') quitState.requested = true
|
|
262
|
+
log(`updated ${result.from} -> ${result.to}; stopping to apply the new version`)
|
|
263
|
+
}
|
|
264
|
+
} catch (error) {
|
|
265
|
+
job.error = 'update-failed'
|
|
266
|
+
job.message = `更新失败:${describe(error)}`
|
|
267
|
+
log('update crashed', error)
|
|
268
|
+
} finally {
|
|
269
|
+
job.active = false
|
|
270
|
+
job.done = true
|
|
271
|
+
job.finishedAt = Date.now()
|
|
272
|
+
if (exitScheduled) {
|
|
273
|
+
scheduleExit(ctx, appExit, source === 'tray' ? TRAY_UPDATE_EXIT_DELAY_MS : UPDATE_EXIT_DELAY_MS)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/* ── local control socket for the KDE tray helper ───────────────────────── */
|
|
279
|
+
|
|
280
|
+
function writeControlResponse(socket, payload) {
|
|
281
|
+
try {
|
|
282
|
+
socket.write(`${JSON.stringify(payload)}\n`)
|
|
283
|
+
} catch {
|
|
284
|
+
// the tray may have gone away
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async function handleControlCommand(ctx, appExit, request) {
|
|
289
|
+
const base = {}
|
|
290
|
+
if (request.id !== undefined) base.id = request.id
|
|
291
|
+
const command = typeof request.command === 'string' ? request.command : ''
|
|
292
|
+
try {
|
|
293
|
+
switch (command) {
|
|
294
|
+
case 'status':
|
|
295
|
+
return { ...base, ...statusSnapshot(ctx) }
|
|
296
|
+
case 'check-update': {
|
|
297
|
+
const value = await getUpdateStatus(true)
|
|
298
|
+
return { ...base, ok: true, update: value }
|
|
299
|
+
}
|
|
300
|
+
case 'update': {
|
|
301
|
+
if (job.active) {
|
|
302
|
+
return { ...base, ok: false, error: 'update-in-progress', message: '已有更新正在进行。' }
|
|
303
|
+
}
|
|
304
|
+
if (cacheRunning) {
|
|
305
|
+
return { ...base, ok: false, error: 'cache-clean-in-progress', message: '正在清理缓存,请稍后再更新。' }
|
|
306
|
+
}
|
|
307
|
+
if (appExit === undefined) {
|
|
308
|
+
return { ...base, ok: false, error: 'no-exit-hook', message: '当前宿主没有退出接口。' }
|
|
309
|
+
}
|
|
310
|
+
const value = await getUpdateStatus(true)
|
|
311
|
+
if (!value.updateAvailable) {
|
|
312
|
+
return { ...base, ok: false, error: 'up-to-date', update: value, message: `已是最新版本(${value.current})。` }
|
|
313
|
+
}
|
|
314
|
+
log(`tray started update ${value.current} -> ${value.candidate}`)
|
|
315
|
+
void runUpdateJob(ctx, appExit, value, 'tray')
|
|
316
|
+
return { ...base, ok: true, started: true, from: value.current, to: value.candidate }
|
|
317
|
+
}
|
|
318
|
+
case 'quit': {
|
|
319
|
+
if (job.active) {
|
|
320
|
+
return { ...base, ok: false, error: 'update-in-progress', message: '正在更新,请等待完成。' }
|
|
321
|
+
}
|
|
322
|
+
if (appExit === undefined) {
|
|
323
|
+
return { ...base, ok: false, error: 'no-exit-hook', message: '当前宿主没有退出接口。' }
|
|
324
|
+
}
|
|
325
|
+
requestQuit(ctx, appExit, 'tray', TRAY_QUIT_EXIT_DELAY_MS)
|
|
326
|
+
return { ...base, ok: true, message: '正在停止 DeepSeek Harness。' }
|
|
327
|
+
}
|
|
328
|
+
default:
|
|
329
|
+
return { ...base, ok: false, error: 'unknown-command', message: `未知命令:${command}` }
|
|
330
|
+
}
|
|
331
|
+
} catch (error) {
|
|
332
|
+
log('control command failed', error)
|
|
333
|
+
return { ...base, ok: false, error: 'command-failed', message: describe(error) }
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function createControlServer(ctx, getAppExit) {
|
|
338
|
+
if (CONTROL_DISABLED) return () => {}
|
|
339
|
+
try {
|
|
340
|
+
mkdirSync(STATE_DIR, { recursive: true, mode: 0o700 })
|
|
341
|
+
chmodSync(STATE_DIR, 0o700)
|
|
342
|
+
} catch {
|
|
343
|
+
// the directory may already exist with the right permissions
|
|
344
|
+
}
|
|
345
|
+
const server = net.createServer((socket) => {
|
|
346
|
+
socket.setEncoding('utf8')
|
|
347
|
+
let buffer = ''
|
|
348
|
+
let chain = Promise.resolve()
|
|
349
|
+
socket.on('data', (chunk) => {
|
|
350
|
+
buffer += chunk
|
|
351
|
+
if (buffer.length > 1_000_000) {
|
|
352
|
+
buffer = ''
|
|
353
|
+
socket.destroy()
|
|
354
|
+
return
|
|
355
|
+
}
|
|
356
|
+
let index = buffer.indexOf('\n')
|
|
357
|
+
while (index >= 0) {
|
|
358
|
+
const line = buffer.slice(0, index).trim()
|
|
359
|
+
buffer = buffer.slice(index + 1)
|
|
360
|
+
if (line !== '') {
|
|
361
|
+
chain = chain.then(async () => {
|
|
362
|
+
let request = {}
|
|
363
|
+
try {
|
|
364
|
+
request = JSON.parse(line)
|
|
365
|
+
} catch {
|
|
366
|
+
request = {}
|
|
367
|
+
}
|
|
368
|
+
const response = await handleControlCommand(ctx, getAppExit(), request)
|
|
369
|
+
writeControlResponse(socket, response)
|
|
370
|
+
}).catch(() => {})
|
|
371
|
+
}
|
|
372
|
+
index = buffer.indexOf('\n')
|
|
373
|
+
}
|
|
374
|
+
})
|
|
375
|
+
socket.on('error', () => {})
|
|
376
|
+
})
|
|
377
|
+
server.on('error', (error) => log('control socket error', error))
|
|
378
|
+
try {
|
|
379
|
+
unlinkSync(CONTROL_SOCKET)
|
|
380
|
+
} catch {
|
|
381
|
+
// no stale socket
|
|
382
|
+
}
|
|
383
|
+
try {
|
|
384
|
+
server.listen(CONTROL_SOCKET, () => {
|
|
385
|
+
try {
|
|
386
|
+
chmodSync(CONTROL_SOCKET, 0o600)
|
|
387
|
+
} catch {
|
|
388
|
+
// best effort
|
|
389
|
+
}
|
|
390
|
+
log('control socket listening')
|
|
391
|
+
})
|
|
392
|
+
} catch (error) {
|
|
393
|
+
log('control socket listen failed', error)
|
|
394
|
+
}
|
|
395
|
+
return () => {
|
|
396
|
+
try {
|
|
397
|
+
server.close()
|
|
398
|
+
} catch {
|
|
399
|
+
// already closed
|
|
400
|
+
}
|
|
401
|
+
try {
|
|
402
|
+
unlinkSync(CONTROL_SOCKET)
|
|
403
|
+
} catch {
|
|
404
|
+
// already gone
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export function apply(ctx) {
|
|
410
|
+
ctx.connection.fetch.register({
|
|
411
|
+
path: QUIT_PATH,
|
|
412
|
+
methods: ['POST'],
|
|
413
|
+
requestBody: 'buffered',
|
|
414
|
+
fetch: async () => {
|
|
415
|
+
const refusal = remoteRefusal(ctx)
|
|
416
|
+
if (refusal !== undefined) return refusal
|
|
417
|
+
if (job.active) {
|
|
418
|
+
return json(409, {
|
|
419
|
+
ok: false,
|
|
420
|
+
error: 'update-in-progress',
|
|
421
|
+
message: '正在更新 DeepSeek Harness,请等待更新完成。',
|
|
422
|
+
})
|
|
423
|
+
}
|
|
424
|
+
const appExit = exitHook(ctx)
|
|
425
|
+
if (appExit === undefined) {
|
|
426
|
+
return json(501, {
|
|
427
|
+
ok: false,
|
|
428
|
+
error: 'no-exit-hook',
|
|
429
|
+
message: '当前宿主没有提供退出接口,无法从界面退出。',
|
|
430
|
+
})
|
|
431
|
+
}
|
|
432
|
+
requestQuit(ctx, appExit, 'web', QUIT_EXIT_DELAY_MS)
|
|
433
|
+
return json(202, { ok: true, message: 'DeepSeek Harness 正在退出。' })
|
|
434
|
+
},
|
|
435
|
+
})
|
|
436
|
+
|
|
437
|
+
ctx.connection.fetch.register({
|
|
438
|
+
path: UPDATE_PATH,
|
|
439
|
+
methods: ['GET', 'POST'],
|
|
440
|
+
requestBody: 'buffered',
|
|
441
|
+
fetch: async (request) => {
|
|
442
|
+
if (request.method === 'GET') {
|
|
443
|
+
try {
|
|
444
|
+
return json(200, await getUpdateStatus(false))
|
|
445
|
+
} catch (error) {
|
|
446
|
+
log('update check failed', error)
|
|
447
|
+
return json(502, {
|
|
448
|
+
ok: false,
|
|
449
|
+
error: 'check-failed',
|
|
450
|
+
message: `检查更新失败:${describe(error)}`,
|
|
451
|
+
})
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const refusal = remoteRefusal(ctx)
|
|
456
|
+
if (refusal !== undefined) return refusal
|
|
457
|
+
const appExit = exitHook(ctx)
|
|
458
|
+
if (appExit === undefined) {
|
|
459
|
+
return json(501, {
|
|
460
|
+
ok: false,
|
|
461
|
+
error: 'no-exit-hook',
|
|
462
|
+
message: '当前宿主没有提供退出接口,无法在更新后重启。',
|
|
463
|
+
})
|
|
464
|
+
}
|
|
465
|
+
if (job.active) {
|
|
466
|
+
return json(409, {
|
|
467
|
+
ok: false,
|
|
468
|
+
error: 'update-in-progress',
|
|
469
|
+
message: '已有更新正在进行,请稍候。',
|
|
470
|
+
})
|
|
471
|
+
}
|
|
472
|
+
if (cacheRunning) {
|
|
473
|
+
return json(409, {
|
|
474
|
+
ok: false,
|
|
475
|
+
error: 'cache-clean-in-progress',
|
|
476
|
+
message: '正在清理缓存,请稍后再更新。',
|
|
477
|
+
})
|
|
478
|
+
}
|
|
479
|
+
try {
|
|
480
|
+
const status = await getUpdateStatus(true)
|
|
481
|
+
if (!status.updateAvailable) {
|
|
482
|
+
return json(409, {
|
|
483
|
+
ok: false,
|
|
484
|
+
error: 'up-to-date',
|
|
485
|
+
current: status.current,
|
|
486
|
+
message: `已是最新版本(${status.current})。`,
|
|
487
|
+
})
|
|
488
|
+
}
|
|
489
|
+
log(`starting update ${status.current} -> ${status.candidate}`)
|
|
490
|
+
void runUpdateJob(ctx, appExit, status, 'web')
|
|
491
|
+
return json(202, {
|
|
492
|
+
ok: true,
|
|
493
|
+
started: true,
|
|
494
|
+
from: status.current,
|
|
495
|
+
to: status.candidate,
|
|
496
|
+
})
|
|
497
|
+
} catch (error) {
|
|
498
|
+
log('update start failed', error)
|
|
499
|
+
return json(502, {
|
|
500
|
+
ok: false,
|
|
501
|
+
error: 'update-start-failed',
|
|
502
|
+
message: `无法开始更新:${describe(error)}`,
|
|
503
|
+
})
|
|
504
|
+
}
|
|
505
|
+
},
|
|
506
|
+
})
|
|
507
|
+
|
|
508
|
+
ctx.connection.fetch.register({
|
|
509
|
+
path: PROGRESS_PATH,
|
|
510
|
+
methods: ['GET'],
|
|
511
|
+
requestBody: 'buffered',
|
|
512
|
+
fetch: async () => {
|
|
513
|
+
return json(200, jobSnapshot())
|
|
514
|
+
},
|
|
515
|
+
})
|
|
516
|
+
|
|
517
|
+
ctx.connection.fetch.register({
|
|
518
|
+
path: STATE_PATH,
|
|
519
|
+
methods: ['GET'],
|
|
520
|
+
requestBody: 'buffered',
|
|
521
|
+
fetch: async () => {
|
|
522
|
+
return json(200, statusSnapshot(ctx))
|
|
523
|
+
},
|
|
524
|
+
})
|
|
525
|
+
|
|
526
|
+
ctx.connection.fetch.register({
|
|
527
|
+
path: SETTINGS_PATH,
|
|
528
|
+
methods: ['GET', 'POST'],
|
|
529
|
+
requestBody: 'buffered',
|
|
530
|
+
fetch: async (request) => {
|
|
531
|
+
if (request.method === 'GET') {
|
|
532
|
+
return json(200, { ok: true, settings: readSettings() })
|
|
533
|
+
}
|
|
534
|
+
const refusal = remoteRefusal(ctx)
|
|
535
|
+
if (refusal !== undefined) return refusal
|
|
536
|
+
let patch
|
|
537
|
+
try {
|
|
538
|
+
patch = await request.json()
|
|
539
|
+
} catch {
|
|
540
|
+
return json(400, { ok: false, error: 'invalid-json', message: '设置内容不是合法 JSON。' })
|
|
541
|
+
}
|
|
542
|
+
if (patch === null || typeof patch !== 'object' || Array.isArray(patch)) {
|
|
543
|
+
return json(400, { ok: false, error: 'invalid-settings', message: '设置内容必须是对象。' })
|
|
544
|
+
}
|
|
545
|
+
const current = readSettings()
|
|
546
|
+
const next = normalizeSettings({
|
|
547
|
+
...current,
|
|
548
|
+
...patch,
|
|
549
|
+
features: { ...current.features, ...(patch.features ?? {}) },
|
|
550
|
+
window: { ...current.window, ...(patch.window ?? {}) },
|
|
551
|
+
})
|
|
552
|
+
writeSettings(next)
|
|
553
|
+
applyTraySettings(next, log)
|
|
554
|
+
log('settings updated')
|
|
555
|
+
return json(200, { ok: true, settings: next })
|
|
556
|
+
},
|
|
557
|
+
})
|
|
558
|
+
|
|
559
|
+
ctx.connection.fetch.register({
|
|
560
|
+
path: CACHE_PATH,
|
|
561
|
+
methods: ['GET'],
|
|
562
|
+
requestBody: 'buffered',
|
|
563
|
+
fetch: async () => {
|
|
564
|
+
try {
|
|
565
|
+
return json(200, await scanCache())
|
|
566
|
+
} catch (error) {
|
|
567
|
+
log('cache scan failed', error)
|
|
568
|
+
return json(500, {
|
|
569
|
+
ok: false,
|
|
570
|
+
error: 'cache-scan-failed',
|
|
571
|
+
message: `读取缓存信息失败:${describe(error)}`,
|
|
572
|
+
})
|
|
573
|
+
}
|
|
574
|
+
},
|
|
575
|
+
})
|
|
576
|
+
|
|
577
|
+
ctx.connection.fetch.register({
|
|
578
|
+
path: CACHE_CLEAN_PATH,
|
|
579
|
+
methods: ['POST'],
|
|
580
|
+
requestBody: 'buffered',
|
|
581
|
+
fetch: async (request) => {
|
|
582
|
+
const refusal = remoteRefusal(ctx)
|
|
583
|
+
if (refusal !== undefined) return refusal
|
|
584
|
+
if (job.active) {
|
|
585
|
+
return json(409, {
|
|
586
|
+
ok: false,
|
|
587
|
+
error: 'update-in-progress',
|
|
588
|
+
message: '正在更新 DeepSeek Harness,请等待更新完成。',
|
|
589
|
+
})
|
|
590
|
+
}
|
|
591
|
+
if (cacheRunning) {
|
|
592
|
+
return json(409, {
|
|
593
|
+
ok: false,
|
|
594
|
+
error: 'cache-clean-in-progress',
|
|
595
|
+
message: '已有缓存清理正在执行,请稍候。',
|
|
596
|
+
})
|
|
597
|
+
}
|
|
598
|
+
let body
|
|
599
|
+
try {
|
|
600
|
+
body = await request.json()
|
|
601
|
+
} catch {
|
|
602
|
+
return json(400, { ok: false, error: 'invalid-json', message: '清理请求不是合法 JSON。' })
|
|
603
|
+
}
|
|
604
|
+
const categories = body !== null && typeof body === 'object' && Array.isArray(body.categories)
|
|
605
|
+
? [...new Set(body.categories.filter((id) => typeof id === 'string'))]
|
|
606
|
+
: []
|
|
607
|
+
if (categories.length === 0) {
|
|
608
|
+
return json(400, { ok: false, error: 'invalid-categories', message: '请至少选择一种缓存类型。' })
|
|
609
|
+
}
|
|
610
|
+
const unknown = categories.filter((id) => !CACHE_CATEGORIES.includes(id))
|
|
611
|
+
if (unknown.length > 0) {
|
|
612
|
+
return json(400, {
|
|
613
|
+
ok: false,
|
|
614
|
+
error: 'invalid-categories',
|
|
615
|
+
message: `未知缓存类型:${unknown.join(', ')}`,
|
|
616
|
+
})
|
|
617
|
+
}
|
|
618
|
+
const packageCategories = new Set(['npx', 'npm', 'pnpm'])
|
|
619
|
+
if (categories.some((id) => packageCategories.has(id)) && packageManagerBusy()) {
|
|
620
|
+
return json(409, {
|
|
621
|
+
ok: false,
|
|
622
|
+
error: 'package-manager-busy',
|
|
623
|
+
message: '有 npm/pnpm/npx 进程正在运行,请等待其结束后再清理包缓存。',
|
|
624
|
+
})
|
|
625
|
+
}
|
|
626
|
+
cacheRunning = true
|
|
627
|
+
try {
|
|
628
|
+
const result = await cleanCache(categories)
|
|
629
|
+
const inventory = await scanCache()
|
|
630
|
+
log('cache cleaned', { categories, freedBytes: result.freedBytes })
|
|
631
|
+
return json(200, { ...result, inventory })
|
|
632
|
+
} catch (error) {
|
|
633
|
+
log('cache clean failed', error)
|
|
634
|
+
return json(409, {
|
|
635
|
+
ok: false,
|
|
636
|
+
error: error.code ?? 'cache-clean-failed',
|
|
637
|
+
message: `缓存清理失败:${describe(error)}`,
|
|
638
|
+
})
|
|
639
|
+
} finally {
|
|
640
|
+
cacheRunning = false
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
})
|
|
644
|
+
|
|
645
|
+
ctx.effect(() => createControlServer(ctx, () => exitHook(ctx)), 'dsh-app-controls: control socket')
|
|
646
|
+
applyTraySettings(readSettings(), log)
|
|
647
|
+
}
|