dsh-session-superagent 0.9.269 → 0.9.271
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/CHANGELOG.md +62 -0
- package/README.md +3 -3
- package/client.js +103 -16
- package/lib/index.js +86 -13
- package/lib/update.js +328 -0
- package/package.json +2 -1
- package/ui-host-primitives.js +3 -0
- package/ui-orbit-popover.js +33 -9
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,68 @@
|
|
|
2
2
|
|
|
3
3
|
格式参照 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
4
4
|
|
|
5
|
+
## [0.9.271] - 2026-11-06 —— 界面内安装更新 + Toast/响应式真实验收 39/39
|
|
6
|
+
|
|
7
|
+
### 设置页可以直接安装更新
|
|
8
|
+
|
|
9
|
+
「版本与更新」不再只给复制命令:hasUpdate 时显示官方 primary「安装更新」,
|
|
10
|
+
经 DSH Modal 确认后 POST `/session-superagent/update`,busy 防重复,成功/失败均有反馈。
|
|
11
|
+
保留复制命令作手动回退,不自动重启。
|
|
12
|
+
|
|
13
|
+
后端使用受 `guardWrite` 保护的固定 argv:`dsh plugin --profile <profile> add dsh-session-superagent@<version>`。
|
|
14
|
+
严格 semver、execFile/spawn(shell:false)、CLI 解析、并发锁、超时、输出截断与非零处理均已覆盖。
|
|
15
|
+
响应明确区分 installedVersion/runningVersion/needsReload,安装成功不冒充运行中代码已切换。
|
|
16
|
+
|
|
17
|
+
### Toast / Modal / 响应式 / 双主题真实验收
|
|
18
|
+
|
|
19
|
+
新增独立 Chromium 验收:Toast 6/6、Modal 9/9、响应式 11/11、双主题 9/9,
|
|
20
|
+
连同环境 4/4,总计 **39/39**。Toast 3 秒 hold + 1 秒 fade、连续通知 id guard、
|
|
21
|
+
Modal 焦点圈/Escape/焦点归还、真实 resize 与官方主题 DOM/token 均通过。
|
|
22
|
+
|
|
23
|
+
真实浏览器发现固定 368px 在 320/360px 视口左溢出;现在先缩到 `视口 - 16px`
|
|
24
|
+
再交给 DSH clamp,实测 360→344px、320→304px,均零溢出。
|
|
25
|
+
|
|
26
|
+
### 证据
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
更新路由 19/19;更新 UI 5/5;窄屏回归 2/2
|
|
30
|
+
真实 Chromium 156.0.8078.74 → 39/39
|
|
31
|
+
npm test → 570/570
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
真实认证 DSH WebView 的会话 slot、物理读屏、touch 与真实 watcher 请求仍列入走查。
|
|
35
|
+
|
|
36
|
+
## [0.9.270] - 2026-11-05 —— 真实 Chromium 验收 27/27,Orbit 焦点链修复
|
|
37
|
+
|
|
38
|
+
独立 `ui-host-primitives.js` 的 Tooltip 需要 `React.cloneElement` 合并 trigger ref。
|
|
39
|
+
老宿主缺少该能力时,现在直接给出可读错误:
|
|
40
|
+
|
|
41
|
+
```text
|
|
42
|
+
Tooltip 需要宿主 React.cloneElement 以合并 trigger ref
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
不再在渲染深处抛「React.cloneElement is not a function」。
|
|
46
|
+
主面板已有独立 forwardRef 回退,不受这次模块防线影响。
|
|
47
|
+
|
|
48
|
+
### Orbit 真实焦点链
|
|
49
|
+
|
|
50
|
+
真实 Chromium 首先发现并复验修复了三个测试桩未覆盖的问题:
|
|
51
|
+
首帧 hidden 聚焦失败、Orbit cleanup 覆盖 Modal autofocus、外点被强制回圆环。
|
|
52
|
+
现在 visible 后每次 open 只聚焦一次;outside/confirm 不抢焦点;Escape 回圆环;
|
|
53
|
+
确认关闭后才显式回圆环。独立适配器与 UMD 实现同步,confirm reason 可跨父组件关闭路径传递。
|
|
54
|
+
|
|
55
|
+
### 证据
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
test/host-primitives-adapter.test.mjs → 12/12(新增缺 cloneElement 用例)
|
|
59
|
+
Orbit 独立适配器 → 13/13;UMD 集成 → 8/8
|
|
60
|
+
适配器同源审计 → 84 项结构化断言 + 4 条测试,含 holdMs/openOnClick 变异反例
|
|
61
|
+
真实 Chromium 156.0.8078.74 → 27/27,含 Tooltip/Orbit/Modal 焦点与事件
|
|
62
|
+
npm test → 544/544
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
T61/T62/T64 均已完成;真实 DSH WebView 的宿主 slot、读屏与 watcher 请求仍列入走查。
|
|
66
|
+
|
|
5
67
|
## [0.9.269] - 2026-11-05 —— Tooltip 不再丢焦点,Orbit 触发按钮去 inline 化
|
|
6
68
|
|
|
7
69
|
### Tooltip scroll-close 保留焦点
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<img src="assets/icon-180.png" width="88" height="88" alt="">
|
|
4
4
|
|
|
5
5
|
[](LICENSE)
|
|
6
|
-
[](https://github.com/MYCF711/dsh-session-superagent/releases)
|
|
7
7
|
[](https://www.npmjs.com/package/dsh-session-superagent)
|
|
8
8
|
[](https://github.com/topics/dsh-plugin)
|
|
9
9
|
[](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin)
|
|
@@ -148,7 +148,7 @@ Gitee 私有仓库要在「设置 → 凭证」填私人令牌;公开仓库不
|
|
|
148
148
|
## 开发
|
|
149
149
|
|
|
150
150
|
```sh
|
|
151
|
-
npm test #
|
|
151
|
+
npm test # 570 项单测(带离线开关;裸 node --test 会真去 spawn gh,两分半)
|
|
152
152
|
```
|
|
153
153
|
|
|
154
154
|
### 工具输出:文本给人和 agent,JSON 给程序
|
|
@@ -208,7 +208,7 @@ dsh plugin --profile <profile> add dsh-session-superagent@latest
|
|
|
208
208
|
|
|
209
209
|
|
|
210
210
|
```bash
|
|
211
|
-
npm test #
|
|
211
|
+
npm test # 570 项单测(带离线开关,约 1.6 秒)
|
|
212
212
|
node scripts/ui-audit.mjs # 11 项 UI 指标(写死颜色 / 自造 token / 官方结构采用 …)
|
|
213
213
|
node scripts/preflight.mjs # 41 项门禁(含上面两条,外加 profile 一致性、路由面、包内容)
|
|
214
214
|
node scripts/render-preview.mjs preview.html # 双主题静态预览,直接浏览器打开
|
package/client.js
CHANGED
|
@@ -135,6 +135,8 @@ window.__ModuleLoader__.load({
|
|
|
135
135
|
// createOrbitPopover({React, primitives}) 的参数、OrbitPopover/useOrbitConfirm 调用面与来源模块一致;
|
|
136
136
|
// React.createPortal 由调用方用宿主 require('react-dom').createPortal 注入,不伪造 primitives.createPortal。
|
|
137
137
|
const DEFAULT_ORBIT_Z_INDEX = 1100
|
|
138
|
+
// confirm 由父组件的 useOrbitConfirm 直接关闭 orbit,不经过 OrbitPopover.close;按 ref 关联原因,避免新增公开参数。
|
|
139
|
+
const orbitCloseReasons = new WeakMap()
|
|
138
140
|
|
|
139
141
|
function focusIfConnected(node) {
|
|
140
142
|
if (!node || typeof node.focus !== 'function') return false
|
|
@@ -168,6 +170,10 @@ window.__ModuleLoader__.load({
|
|
|
168
170
|
|
|
169
171
|
const requestConfirm = HostReact.useCallback((detail = {}) => {
|
|
170
172
|
restoreFocusRef.current = 'orbit'
|
|
173
|
+
const ref = orbitRefRef.current
|
|
174
|
+
if (ref && (typeof ref === 'object' || typeof ref === 'function')) {
|
|
175
|
+
orbitCloseReasons.set(ref, 'confirm')
|
|
176
|
+
}
|
|
171
177
|
if (typeof onCloseOrbitRef.current === 'function') {
|
|
172
178
|
onCloseOrbitRef.current({ reason: 'confirm' })
|
|
173
179
|
}
|
|
@@ -244,10 +250,14 @@ window.__ModuleLoader__.load({
|
|
|
244
250
|
const returnFocusRef = orbitRef || anchorRef
|
|
245
251
|
const onCloseRef = HostReact.useRef(onClose)
|
|
246
252
|
onCloseRef.current = onClose
|
|
253
|
+
const didFocusRef = HostReact.useRef(false)
|
|
254
|
+
const closeReasonRef = HostReact.useRef(null)
|
|
247
255
|
|
|
248
256
|
const close = HostReact.useCallback((detail) => {
|
|
257
|
+
const reason = closeReason(detail)
|
|
258
|
+
closeReasonRef.current = reason.reason
|
|
249
259
|
const callback = onCloseRef.current
|
|
250
|
-
if (typeof callback === 'function') callback(
|
|
260
|
+
if (typeof callback === 'function') callback(reason)
|
|
251
261
|
}, [])
|
|
252
262
|
|
|
253
263
|
// 与来源适配器一致:宿主 hooks 和本地 effect 都在 open 分支前无条件调用。
|
|
@@ -270,8 +280,18 @@ window.__ModuleLoader__.load({
|
|
|
270
280
|
: (typeof document !== 'undefined' ? document : null)
|
|
271
281
|
|
|
272
282
|
HostReact.useEffect(() => {
|
|
273
|
-
if (!open
|
|
274
|
-
|
|
283
|
+
if (!open) {
|
|
284
|
+
didFocusRef.current = false
|
|
285
|
+
closeReasonRef.current = null
|
|
286
|
+
orbitCloseReasons.delete(returnFocusRef)
|
|
287
|
+
return undefined
|
|
288
|
+
}
|
|
289
|
+
// 首帧 position=null 时 surface 是 hidden,focus() 会被浏览器拒绝;等可见后只尝试一次。
|
|
290
|
+
if (position && !didFocusRef.current) {
|
|
291
|
+
didFocusRef.current = true
|
|
292
|
+
focusIfConnected(panelRef.current)
|
|
293
|
+
}
|
|
294
|
+
if (!ownerDocument || typeof ownerDocument.addEventListener !== 'function') return undefined
|
|
275
295
|
const onKeyDown = (event) => {
|
|
276
296
|
if (!event || event.key !== 'Escape' || event.repeat) return
|
|
277
297
|
if (typeof event.preventDefault === 'function') event.preventDefault()
|
|
@@ -281,9 +301,11 @@ window.__ModuleLoader__.load({
|
|
|
281
301
|
ownerDocument.addEventListener('keydown', onKeyDown, true)
|
|
282
302
|
return () => {
|
|
283
303
|
ownerDocument.removeEventListener('keydown', onKeyDown, true)
|
|
284
|
-
|
|
304
|
+
closeReasonRef.current = closeReasonRef.current || orbitCloseReasons.get(returnFocusRef) || null
|
|
305
|
+
if (closeReasonRef.current === 'escape') focusIfConnected(returnFocusRef && returnFocusRef.current)
|
|
306
|
+
closeReasonRef.current = null
|
|
285
307
|
}
|
|
286
|
-
}, [open, close, ownerDocument, panelRef, returnFocusRef])
|
|
308
|
+
}, [open, position, close, ownerDocument, panelRef, returnFocusRef])
|
|
287
309
|
|
|
288
310
|
if (!open) return null
|
|
289
311
|
if (!anchorRef || typeof anchorRef !== 'object') {
|
|
@@ -295,6 +317,9 @@ window.__ModuleLoader__.load({
|
|
|
295
317
|
if (!portalTarget) {
|
|
296
318
|
throw new Error('OrbitPopover portal target 不可用;不会退化为非 portal surface')
|
|
297
319
|
}
|
|
320
|
+
// 与独立适配器一致:先缩到视口内,再交给 DSH 计算坐标。
|
|
321
|
+
const viewportWidth = typeof window !== 'undefined' && Number.isFinite(window.innerWidth) ? window.innerWidth : width + 16
|
|
322
|
+
const effectiveWidth = Math.min(width, Math.max(1, viewportWidth - 16))
|
|
298
323
|
|
|
299
324
|
const surface = hostH(MenuSurface, {
|
|
300
325
|
ref: panelRef,
|
|
@@ -307,7 +332,7 @@ window.__ModuleLoader__.load({
|
|
|
307
332
|
position: 'fixed',
|
|
308
333
|
left: position ? position.left : 0,
|
|
309
334
|
top: position ? position.top : 0,
|
|
310
|
-
width,
|
|
335
|
+
width: effectiveWidth,
|
|
311
336
|
zIndex,
|
|
312
337
|
visibility: position ? 'visible' : 'hidden',
|
|
313
338
|
},
|
|
@@ -320,10 +345,13 @@ window.__ModuleLoader__.load({
|
|
|
320
345
|
|
|
321
346
|
// 老宿主没有这个映射时 require 会抛错;可选探测失败必须完整保留本地实现。
|
|
322
347
|
let hostUi = null
|
|
348
|
+
let hostButton = null
|
|
323
349
|
let hostTooltip = null
|
|
324
350
|
let hostOrbit = null
|
|
325
351
|
try {
|
|
326
352
|
const primitives = require('@deepseek-ai/dsh-client-ui-primitives')
|
|
353
|
+
// Button 独立探测:老宿主缺 Modal/Toast 时仍可复用官方按钮;缺 Button 则由 UpdateButton 回退 Btn。
|
|
354
|
+
hostButton = primitives.Button || null
|
|
327
355
|
if (primitives && primitives.Modal && primitives.Toast && primitives.IconWarningOutlineMedium) {
|
|
328
356
|
hostUi = createHostUi({ React, primitives })
|
|
329
357
|
// Tooltip 独立探测:缺少它只回退 InfoTip,不把已接入的 Modal/Toast 一起降级。
|
|
@@ -331,6 +359,7 @@ window.__ModuleLoader__.load({
|
|
|
331
359
|
}
|
|
332
360
|
} catch {
|
|
333
361
|
hostUi = null
|
|
362
|
+
hostButton = null
|
|
334
363
|
hostTooltip = null
|
|
335
364
|
}
|
|
336
365
|
|
|
@@ -350,7 +379,7 @@ window.__ModuleLoader__.load({
|
|
|
350
379
|
}
|
|
351
380
|
|
|
352
381
|
// 客户端版本探针:每次模块加载写一次,用来确认浏览器里跑的是哪一版
|
|
353
|
-
const CLIENT_VERSION = '0.9.
|
|
382
|
+
const CLIENT_VERSION = '0.9.271'
|
|
354
383
|
let BASE = '/session-superagent'
|
|
355
384
|
/**
|
|
356
385
|
* 挑「最新的那个服务端实例」。
|
|
@@ -588,6 +617,9 @@ window.__ModuleLoader__.load({
|
|
|
588
617
|
children)
|
|
589
618
|
}
|
|
590
619
|
|
|
620
|
+
// 安装动作优先用 DSH Button;老宿主缺该导出时完整保留同源 Btn 的尺寸、variant 与交互。
|
|
621
|
+
const UpdateButton = hostButton || Btn
|
|
622
|
+
|
|
591
623
|
function Tag({ tone = 'outline', title, children }) {
|
|
592
624
|
return h('span', { className: 'dt-tag', 'data-tone': tone, title }, children)
|
|
593
625
|
}
|
|
@@ -1104,7 +1136,7 @@ window.__ModuleLoader__.load({
|
|
|
1104
1136
|
const PanelModal = hostUi ? hostUi.Modal : Modal
|
|
1105
1137
|
|
|
1106
1138
|
/** 确认层与触发器分离:DSH orbit 关闭后,同一个本地 Modal 仍能由 StatusOrbit 持有。 */
|
|
1107
|
-
function ConfirmDialog({ open, label, confirmLabel = '确定执行', title = confirmLabel, message, onConfirm, onClose, modalContext = 'panel' }) {
|
|
1139
|
+
function ConfirmDialog({ open, label, confirmLabel = '确定执行', title = confirmLabel, message, onConfirm, onClose, modalContext = 'panel', ButtonComponent = Btn, danger = true }) {
|
|
1108
1140
|
const [busy, setBusy] = useState(false)
|
|
1109
1141
|
const busyRef = useRef(false)
|
|
1110
1142
|
const confirm = async () => {
|
|
@@ -1130,10 +1162,10 @@ window.__ModuleLoader__.load({
|
|
|
1130
1162
|
onClose,
|
|
1131
1163
|
children: null,
|
|
1132
1164
|
footer: h(React.Fragment, null,
|
|
1133
|
-
h(
|
|
1134
|
-
h(
|
|
1165
|
+
h(ButtonComponent, { variant: 'ghost', onClick: onClose }, '取消'),
|
|
1166
|
+
h(ButtonComponent, {
|
|
1135
1167
|
variant: 'primary',
|
|
1136
|
-
className: cx('dt-danger', busy && 'dt-btn-busy'),
|
|
1168
|
+
className: cx(danger && 'dt-danger', busy && 'dt-btn-busy'),
|
|
1137
1169
|
disabled: busy,
|
|
1138
1170
|
'aria-label': confirmLabel,
|
|
1139
1171
|
'aria-busy': busy ? 'true' : undefined,
|
|
@@ -1143,11 +1175,11 @@ window.__ModuleLoader__.load({
|
|
|
1143
1175
|
}
|
|
1144
1176
|
|
|
1145
1177
|
/** 官方 ConfirmButton 动线:触发按钮只开 Modal,取消不执行,确认期间 busy 防重复。 */
|
|
1146
|
-
function ConfirmButton({ label, confirmLabel = '确定执行', title = confirmLabel, message, onConfirm, variant = 'ghost', icon, size, modalContext = 'panel' }) {
|
|
1178
|
+
function ConfirmButton({ label, confirmLabel = '确定执行', title = confirmLabel, message, onConfirm, variant = 'ghost', icon, size, modalContext = 'panel', ButtonComponent = Btn, danger = true }) {
|
|
1147
1179
|
const [open, setOpen] = useState(false)
|
|
1148
1180
|
const close = useCallback(() => setOpen(false), [])
|
|
1149
1181
|
return h(React.Fragment, null,
|
|
1150
|
-
h(
|
|
1182
|
+
h(ButtonComponent, {
|
|
1151
1183
|
variant,
|
|
1152
1184
|
size,
|
|
1153
1185
|
icon,
|
|
@@ -1155,7 +1187,7 @@ window.__ModuleLoader__.load({
|
|
|
1155
1187
|
'aria-haspopup': 'dialog',
|
|
1156
1188
|
onClick: () => setOpen(true),
|
|
1157
1189
|
}, label),
|
|
1158
|
-
h(ConfirmDialog, { open, label, confirmLabel, title, message, onConfirm, onClose: close, modalContext }))
|
|
1190
|
+
h(ConfirmDialog, { open, label, confirmLabel, title, message, onConfirm, onClose: close, modalContext, ButtonComponent, danger }))
|
|
1159
1191
|
}
|
|
1160
1192
|
|
|
1161
1193
|
const watcherId = (w) => (w && w.id) || String((w && w.repo) || '') + '#' + String((w && w.sessionId) || '')
|
|
@@ -1774,6 +1806,8 @@ window.__ModuleLoader__.load({
|
|
|
1774
1806
|
const [giteeToken, setGiteeToken] = useState('')
|
|
1775
1807
|
// 判定页内联追溯结果(官方 Thread 详情最小吸收):不再只塞进一条 Toast。
|
|
1776
1808
|
const [traceResult, setTraceResult] = useState(null)
|
|
1809
|
+
// 安装回执与版本检查分开:POST 失败不能冲掉当前版本和命令回退。
|
|
1810
|
+
const [updateResult, setUpdateResult] = useState(null)
|
|
1777
1811
|
// 去设置页时必须同时定位分区;只切 Tab 会把人留在上次看的分区(官方 settingsRoute(group) 的最小等价)。
|
|
1778
1812
|
const openSettings = (section) => {
|
|
1779
1813
|
setSettingsSection(section)
|
|
@@ -2710,12 +2744,28 @@ window.__ModuleLoader__.load({
|
|
|
2710
2744
|
h(KeyValues, { rows: [
|
|
2711
2745
|
['当前版本', (update && update.current) || CLIENT_VERSION],
|
|
2712
2746
|
['npm 最新', !update ? '检测中…' : update.error ? '检测失败 · ' + update.error : (update.latest || '未知')],
|
|
2713
|
-
['状态',
|
|
2747
|
+
['状态', updateResult
|
|
2748
|
+
? (updateResult.needsReload ? '安装完成,需要重载才生效' : '安装完成')
|
|
2749
|
+
: !update ? '…' : update.error ? '检查失败' : update.hasUpdate ? '有新版本,可以升级' : '已是最新'],
|
|
2750
|
+
updateResult ? ['安装副本(installedVersion)', updateResult.installedVersion] : null,
|
|
2751
|
+
updateResult ? ['运行版本(runningVersion)', updateResult.runningVersion] : null,
|
|
2752
|
+
updateResult ? ['重载要求(needsReload)', updateResult.needsReload ? 'true · 需要重载才生效' : 'false · 当前无需重载'] : null,
|
|
2714
2753
|
] }),
|
|
2715
2754
|
update && update.hasUpdate
|
|
2716
2755
|
? h('div', null,
|
|
2717
2756
|
h('code', { className: 'dt-code' }, update.command),
|
|
2718
2757
|
h('div', { className: 'dt-actions' },
|
|
2758
|
+
h(ConfirmButton, {
|
|
2759
|
+
label: '安装更新',
|
|
2760
|
+
confirmLabel: '确认安装',
|
|
2761
|
+
title: '安装更新',
|
|
2762
|
+
message: '将安装 ' + update.latest + '(当前 ' + (update.current || CLIENT_VERSION) + ')。这会修改安装副本;安装完成后需要重载宿主才生效,不会自动重启。',
|
|
2763
|
+
variant: 'primary',
|
|
2764
|
+
size: 'sm',
|
|
2765
|
+
ButtonComponent: UpdateButton,
|
|
2766
|
+
danger: false,
|
|
2767
|
+
onConfirm: installUpdate,
|
|
2768
|
+
}),
|
|
2719
2769
|
h(Btn, {
|
|
2720
2770
|
size: 'sm',
|
|
2721
2771
|
// 审核 T24 W2:以前点了零反馈,writeText 的 rejection 也没人接 —— 客户不知道复制没成。
|
|
@@ -2725,7 +2775,7 @@ window.__ModuleLoader__.load({
|
|
|
2725
2775
|
: null,
|
|
2726
2776
|
h('div', { className: 'dt-actions' },
|
|
2727
2777
|
h(Btn, { size: 'sm', variant: 'ghost', onClick: () => checkUpdate(true), disabled: checking }, checking ? '检查中…' : '重新检查')),
|
|
2728
|
-
h(Hint, null, '
|
|
2778
|
+
h(Hint, null, '安装会修改当前安装副本,不会自动重启;安装成功后需要重载宿主才生效。复制命令保留作手动回退。'),
|
|
2729
2779
|
h(Hint, null, '反馈问题时把下面这份诊断贴给对方:里面有版本、挂载状态和每条守的最近错误(凭证已脱敏)。'),
|
|
2730
2780
|
h('div', { className: 'dt-actions' },
|
|
2731
2781
|
h(Btn, {
|
|
@@ -2793,13 +2843,50 @@ window.__ModuleLoader__.load({
|
|
|
2793
2843
|
try {
|
|
2794
2844
|
const value = await call('/update' + (force ? '?refresh=1' : ''))
|
|
2795
2845
|
setUpdate(value)
|
|
2846
|
+
return true
|
|
2796
2847
|
} catch (error) {
|
|
2797
2848
|
setUpdate({ current: CLIENT_VERSION, latest: null, hasUpdate: false, error: String(error && error.message || error) })
|
|
2849
|
+
return false
|
|
2798
2850
|
} finally {
|
|
2799
2851
|
setChecking(false)
|
|
2800
2852
|
}
|
|
2801
2853
|
}, [])
|
|
2802
2854
|
|
|
2855
|
+
async function installUpdate() {
|
|
2856
|
+
const version = update && update.latest
|
|
2857
|
+
if (!version) {
|
|
2858
|
+
const text = '安装更新失败:缺少目标版本,请先重新检查'
|
|
2859
|
+
setNotice({ ok: false, text })
|
|
2860
|
+
notify(text, 'error')
|
|
2861
|
+
return false
|
|
2862
|
+
}
|
|
2863
|
+
setNotice(null)
|
|
2864
|
+
try {
|
|
2865
|
+
const result = await call('/update', {
|
|
2866
|
+
method: 'POST',
|
|
2867
|
+
body: JSON.stringify({ version }),
|
|
2868
|
+
})
|
|
2869
|
+
if (!result || result.ok === false) throw new Error((result && result.error) || '服务端没有返回安装结果')
|
|
2870
|
+
const receipt = {
|
|
2871
|
+
installedVersion: String(result.installedVersion || '未知'),
|
|
2872
|
+
runningVersion: String(result.runningVersion || '未知'),
|
|
2873
|
+
needsReload: result.needsReload === true,
|
|
2874
|
+
}
|
|
2875
|
+
setUpdateResult(receipt)
|
|
2876
|
+
notify('安装完成:installedVersion ' + receipt.installedVersion
|
|
2877
|
+
+ ',runningVersion ' + receipt.runningVersion
|
|
2878
|
+
+ (receipt.needsReload ? ',需要重载才生效' : ''), 'success')
|
|
2879
|
+
// 安装只改副本;重新检查由 GET 完成,不触发重启,也不推断 runningVersion 已变化。
|
|
2880
|
+
await checkUpdate(true)
|
|
2881
|
+
return true
|
|
2882
|
+
} catch (error) {
|
|
2883
|
+
const text = '安装更新失败:' + String((error && error.message) || error)
|
|
2884
|
+
setNotice({ ok: false, text })
|
|
2885
|
+
notify(text, 'error')
|
|
2886
|
+
return false
|
|
2887
|
+
}
|
|
2888
|
+
}
|
|
2889
|
+
|
|
2803
2890
|
useEffect(() => {
|
|
2804
2891
|
// 初始检查与手动「重新检查」共用一条错误路径:失败要显示「检查失败」,不能吞掉后永远检测中。
|
|
2805
2892
|
checkUpdate(false)
|
package/lib/index.js
CHANGED
|
@@ -26,6 +26,7 @@ import { decodeBody, truncateUtf8 } from './body.js'
|
|
|
26
26
|
import { quotaDeferText, isOfflineMode } from './gh-format.js'
|
|
27
27
|
import { listGiteeItems, fetchGiteeLastComment, addGiteeLabel, addGiteeComment, ensureGiteeLabel, giteeWhoami, giteeRepo, mapGiteeRepoAccess, DEFAULT_ACK_LABEL_GITEE, giteeLabels, giteeHasLabel, isValidGiteeLabel, suggestGiteeLabel } from './gitee.js'
|
|
28
28
|
import { migrateState, CURRENT_STATE_SCHEMA } from './state-migration.js'
|
|
29
|
+
import { isStrictSemver, runExclusiveUpdate, spawnPluginUpdate, validateUpdateBody } from './update.js'
|
|
29
30
|
import {
|
|
30
31
|
effectiveIntervalMs, planWakeSettlement, advanceSeen, applyWatcherAction, batchFresh, decideWake, DEFAULT_ACK_LABEL, DEFAULT_PRIORITY, formatWatchText,
|
|
31
32
|
recordPendingWake, clearPending, pendingRetry,
|
|
@@ -47,7 +48,7 @@ import {
|
|
|
47
48
|
|
|
48
49
|
export const name = 'dsh-session-superagent'
|
|
49
50
|
/** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
|
|
50
|
-
export const SERVER_VERSION = '0.9.
|
|
51
|
+
export const SERVER_VERSION = '0.9.271'
|
|
51
52
|
export const inject = ['tools', 'agents', 'webServer']
|
|
52
53
|
/** 一条会话每小时最多被唤醒几次(跨所有守累计)。单守上限挡不住多守叠加。 */
|
|
53
54
|
// 本进程启动时刻:用来区分「刚重启还没轮到」与「定时器真的丢了」
|
|
@@ -1644,7 +1645,11 @@ async function pollWithItems(ctx, entry, baseMs, parsed) {
|
|
|
1644
1645
|
}, wakeQueueKey)
|
|
1645
1646
|
}
|
|
1646
1647
|
|
|
1647
|
-
export function apply(ctx) {
|
|
1648
|
+
export function apply(ctx, options = {}) {
|
|
1649
|
+
// 更新 runner 可注入:路由测试绝不发起真实安装;生产默认走 shell:false 的 CLI runner。
|
|
1650
|
+
const updateRunner = options.updateRunner || spawnPluginUpdate
|
|
1651
|
+
const updateTimeoutMs = options.updateTimeoutMs
|
|
1652
|
+
const readInstalledVersion = options.readInstalledVersion || installedVersion
|
|
1648
1653
|
// 宿主升级(如 0.2.2-rc.1)时最可能变的不是我们的逻辑,而是服务名/形状。
|
|
1649
1654
|
// 一条守挂了不该拖垮整个插件树:缺服务就只打一条日志,其余能力继续挂。
|
|
1650
1655
|
if (!ctx || !ctx.tools || typeof ctx.tools.register !== 'function') {
|
|
@@ -1755,8 +1760,8 @@ export function apply(ctx) {
|
|
|
1755
1760
|
}
|
|
1756
1761
|
|
|
1757
1762
|
/**
|
|
1758
|
-
*
|
|
1759
|
-
*
|
|
1763
|
+
* 版本检查:问 npm registry 最新版是多少,和本实例版本比。
|
|
1764
|
+
* GET 只读;POST 仅在 guardWrite 通过后复用这里的 latest 并交给受控更新 runner。
|
|
1760
1765
|
* 10 分钟内存缓存,避免面板每次刷新都打 registry。
|
|
1761
1766
|
*/
|
|
1762
1767
|
// /state 的体积预算:每守最多 6 条事件、全局最多 240 条(约 100 KB 内)
|
|
@@ -1981,7 +1986,8 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
|
|
|
1981
1986
|
// 审核 T19:64KB 以前只是「读完再截断」—— 48MB 的请求体会先把 RSS 从 105MB 顶到 333MB。
|
|
1982
1987
|
// 现在边读边算,超了立刻 destroy 并返回 413。
|
|
1983
1988
|
const MAX_BODY_BYTES = 64 * 1024
|
|
1984
|
-
const
|
|
1989
|
+
const INVALID_JSON_BODY = Symbol('invalid-json-body')
|
|
1990
|
+
const readBody = (req, res, strict = false) => new Promise((resolve) => {
|
|
1985
1991
|
const chunks = []
|
|
1986
1992
|
let bytes = 0
|
|
1987
1993
|
let aborted = false
|
|
@@ -2002,10 +2008,15 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
|
|
|
2002
2008
|
try {
|
|
2003
2009
|
// 按字符边界截断:直接 subarray 会切开汉字,剩下的半截被 decodeBody 当成 GBK 整段重解(T16)
|
|
2004
2010
|
const raw = decodeBody(truncateUtf8(Buffer.concat(chunks), 64 * 1024))
|
|
2005
|
-
|
|
2006
|
-
|
|
2011
|
+
const parsed = JSON.parse(raw || '{}')
|
|
2012
|
+
if (strict && (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))) {
|
|
2013
|
+
resolve(INVALID_JSON_BODY)
|
|
2014
|
+
return
|
|
2015
|
+
}
|
|
2016
|
+
resolve(parsed)
|
|
2017
|
+
} catch { resolve(strict ? INVALID_JSON_BODY : {}) }
|
|
2007
2018
|
})
|
|
2008
|
-
req.on('error', () => resolve({}))
|
|
2019
|
+
req.on('error', () => resolve(strict ? INVALID_JSON_BODY : {}))
|
|
2009
2020
|
})
|
|
2010
2021
|
const json = (res, code, body) => {
|
|
2011
2022
|
res.statusCode = code
|
|
@@ -2325,15 +2336,77 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
|
|
|
2325
2336
|
},
|
|
2326
2337
|
})
|
|
2327
2338
|
|
|
2328
|
-
//
|
|
2339
|
+
// GET 保持只读版本检查;POST 走同一套写来源守卫,执行固定 argv 的插件更新。
|
|
2329
2340
|
addRoute({
|
|
2330
2341
|
kind: 'exact',
|
|
2331
2342
|
path: '/session-superagent/update',
|
|
2332
2343
|
handler: async (req, res) => {
|
|
2333
|
-
if (req.method
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2344
|
+
if (req.method === 'GET') {
|
|
2345
|
+
const url = new URL(String(req.url || '/'), 'http://127.0.0.1')
|
|
2346
|
+
const value = await checkUpdate(url.searchParams.get('refresh') === '1')
|
|
2347
|
+
return json(res, 200, value)
|
|
2348
|
+
}
|
|
2349
|
+
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
2350
|
+
if (!guardWrite(req, res)) return
|
|
2351
|
+
const body = await readBody(req, res, true)
|
|
2352
|
+
if (body === null) return // 已回 413
|
|
2353
|
+
if (body === INVALID_JSON_BODY) return json(res, 400, { ok: false, code: 'invalid_body', error: '请求体必须是合法 JSON 对象' })
|
|
2354
|
+
const selected = validateUpdateBody(body)
|
|
2355
|
+
if (!selected.ok) return json(res, 400, selected)
|
|
2356
|
+
|
|
2357
|
+
const versionState = () => {
|
|
2358
|
+
const installed = readInstalledVersion()
|
|
2359
|
+
return {
|
|
2360
|
+
installedVersion: installed,
|
|
2361
|
+
runningVersion: SERVER_VERSION,
|
|
2362
|
+
needsReload: installed !== null && installed !== SERVER_VERSION,
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
let version = selected.version
|
|
2366
|
+
if (version === undefined) {
|
|
2367
|
+
const checked = await checkUpdate(false)
|
|
2368
|
+
if (!isStrictSemver(checked.latest)) {
|
|
2369
|
+
return json(res, 503, {
|
|
2370
|
+
...versionState(),
|
|
2371
|
+
ok: false,
|
|
2372
|
+
code: 'latest_unavailable',
|
|
2373
|
+
error: '没有取得严格 semver 的 latest 版本,本次没有执行更新' + (checked.error ? ':' + redact(String(checked.error)) : ''),
|
|
2374
|
+
})
|
|
2375
|
+
}
|
|
2376
|
+
version = checked.latest
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2379
|
+
const result = await runExclusiveUpdate({ version, runner: updateRunner, timeoutMs: updateTimeoutMs })
|
|
2380
|
+
const state = versionState()
|
|
2381
|
+
const common = {
|
|
2382
|
+
...state,
|
|
2383
|
+
// 成功安装了别的版本时,即使测试替身没有改磁盘,也必须提示运行中代码尚未切换。
|
|
2384
|
+
needsReload: state.needsReload || (result.ok === true && version !== SERVER_VERSION),
|
|
2385
|
+
version,
|
|
2386
|
+
profile: result.profile || null,
|
|
2387
|
+
stdout: redact(String(result.stdout || '')),
|
|
2388
|
+
stderr: redact(String(result.stderr || '')),
|
|
2389
|
+
stdoutTruncated: result.stdoutTruncated === true,
|
|
2390
|
+
stderrTruncated: result.stderrTruncated === true,
|
|
2391
|
+
}
|
|
2392
|
+
if (result.ok === true && result.code === 0) {
|
|
2393
|
+
return json(res, 200, {
|
|
2394
|
+
...common,
|
|
2395
|
+
ok: true,
|
|
2396
|
+
code: 0,
|
|
2397
|
+
exitCode: 0,
|
|
2398
|
+
message: 'DSH CLI 更新命令已以退出码 0 结束;运行中代码仍为 ' + SERVER_VERSION + ',needsReload 表示是否还需重载后才能使用新版本',
|
|
2399
|
+
})
|
|
2400
|
+
}
|
|
2401
|
+
const code = result.code || 'update_failed'
|
|
2402
|
+
const status = code === 'update_in_flight' ? 409
|
|
2403
|
+
: code === 'timeout' ? 504
|
|
2404
|
+
: ['cli_not_found', 'cli_unsupported', 'profile_unavailable', 'invalid_profile'].includes(code) ? 503
|
|
2405
|
+
: 500
|
|
2406
|
+
const error = result.error || (typeof code === 'number'
|
|
2407
|
+
? 'DSH CLI 更新命令以非零退出码 ' + code + ' 结束,未确认安装成功'
|
|
2408
|
+
: '插件更新失败,未确认安装成功')
|
|
2409
|
+
return json(res, status, { ...common, ok: false, code, exitCode: typeof code === 'number' ? code : null, error })
|
|
2337
2410
|
},
|
|
2338
2411
|
})
|
|
2339
2412
|
|
package/lib/update.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { statSync } from 'node:fs'
|
|
3
|
+
import { delimiter, dirname, extname, join, resolve } from 'node:path'
|
|
4
|
+
|
|
5
|
+
export const UPDATE_PACKAGE_NAME = 'dsh-session-superagent'
|
|
6
|
+
export const DEFAULT_UPDATE_TIMEOUT_MS = 120_000
|
|
7
|
+
export const UPDATE_OUTPUT_LIMIT_BYTES = 16 * 1024
|
|
8
|
+
|
|
9
|
+
// Official semver grammar: no leading "v", ranges, tags, or option-like values.
|
|
10
|
+
const SEMVER_RE = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/
|
|
11
|
+
const PROFILE_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
|
|
12
|
+
const UPDATE_LOCK_KEY = Symbol.for('dsh-session-superagent.pluginUpdateInFlight')
|
|
13
|
+
|
|
14
|
+
function updateError(code, message) {
|
|
15
|
+
const error = new Error(message)
|
|
16
|
+
error.code = code
|
|
17
|
+
return error
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isStrictSemver(value) {
|
|
21
|
+
return typeof value === 'string' && value.length <= 160 && SEMVER_RE.test(value)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The POST body is deliberately closed: no command, args, cwd, env, or profile overrides. */
|
|
25
|
+
export function validateUpdateBody(body) {
|
|
26
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
|
27
|
+
return { ok: false, code: 'invalid_body', error: '请求体必须是 JSON 对象,且只允许可选字段 version' }
|
|
28
|
+
}
|
|
29
|
+
const keys = Object.keys(body)
|
|
30
|
+
if (keys.some((key) => key !== 'version')) {
|
|
31
|
+
return { ok: false, code: 'invalid_body', error: '请求体只允许可选字段 version' }
|
|
32
|
+
}
|
|
33
|
+
if (body.version !== undefined && !isStrictSemver(body.version)) {
|
|
34
|
+
return { ok: false, code: 'invalid_version', error: 'version 必须是严格 semver(例如 0.9.271),不接受 latest、范围或命令参数' }
|
|
35
|
+
}
|
|
36
|
+
return { ok: true, version: body.version }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function resolveProfile(env = process.env) {
|
|
40
|
+
const names = ['DSH_PROFILE', 'DSH_PROFILE_NAME', 'DSH_ACTIVE_PROFILE']
|
|
41
|
+
for (const name of names) {
|
|
42
|
+
const value = typeof env[name] === 'string' ? env[name].trim() : ''
|
|
43
|
+
if (!value) continue
|
|
44
|
+
if (!PROFILE_RE.test(value)) {
|
|
45
|
+
throw updateError('invalid_profile', name + ' 含有不支持的字符;profile 只能包含字母、数字、点、下划线和连字符,且不能以连字符开头')
|
|
46
|
+
}
|
|
47
|
+
return value
|
|
48
|
+
}
|
|
49
|
+
throw updateError('profile_unavailable', '无法确定要更新的 DSH profile:宿主环境未提供 DSH_PROFILE(也支持 DSH_PROFILE_NAME / DSH_ACTIVE_PROFILE)')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function pluginAddArguments(profile, version) {
|
|
53
|
+
if (!PROFILE_RE.test(String(profile || ''))) throw updateError('invalid_profile', 'profile 格式无效')
|
|
54
|
+
if (!isStrictSemver(version)) throw updateError('invalid_version', 'version 必须是严格 semver')
|
|
55
|
+
return ['plugin', '--profile', profile, 'add', UPDATE_PACKAGE_NAME + '@' + version]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isFile(path) {
|
|
59
|
+
try { return statSync(path).isFile() } catch { return false }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function pathCandidates(env, platform) {
|
|
63
|
+
const pathValue = typeof env.PATH === 'string' ? env.PATH : ''
|
|
64
|
+
const names = platform === 'win32' ? ['dsh.exe', 'dsh.cmd', 'dsh'] : ['dsh']
|
|
65
|
+
const dirs = pathValue.split(delimiter).map((part) => part.trim()).filter(Boolean)
|
|
66
|
+
const found = []
|
|
67
|
+
for (const dir of dirs) {
|
|
68
|
+
for (const name of names) found.push(join(dir, name))
|
|
69
|
+
}
|
|
70
|
+
return found
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function layoutCandidates(resourcesPath, execPath, platform) {
|
|
74
|
+
const roots = []
|
|
75
|
+
for (const root of [resourcesPath, execPath ? join(dirname(execPath), 'resources') : null]) {
|
|
76
|
+
if (root && !roots.includes(root)) roots.push(root)
|
|
77
|
+
}
|
|
78
|
+
const name = platform === 'win32' ? 'dsh.cmd' : 'dsh'
|
|
79
|
+
return roots.map((root) => join(root, 'runtime', 'cli', 'bin', name))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Convert the standard Windows dsh.cmd wrapper into its direct Electron form.
|
|
84
|
+
* This preserves shell:false even on Node versions that refuse to spawn .cmd.
|
|
85
|
+
*/
|
|
86
|
+
function standardWindowsCommand(cmdPath, fileExists) {
|
|
87
|
+
const binDir = dirname(cmdPath)
|
|
88
|
+
const desktopDir = resolve(binDir, '..', '..', '..', '..')
|
|
89
|
+
const resourcesDir = resolve(binDir, '..', '..', '..')
|
|
90
|
+
const executable = join(desktopDir, 'DeepSeek Harness.exe')
|
|
91
|
+
const asar = join(resourcesDir, 'app.asar')
|
|
92
|
+
if (!fileExists(executable) || !fileExists(asar)) return null
|
|
93
|
+
return {
|
|
94
|
+
file: executable,
|
|
95
|
+
args: [
|
|
96
|
+
'--expose-internals',
|
|
97
|
+
join(asar, 'dsh', 'node_modules', '@deepseek-ai', 'dsh-desktop-host', 'lib', 'cli.js'),
|
|
98
|
+
],
|
|
99
|
+
env: { ELECTRON_RUN_AS_NODE: '1' },
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Resolution order is explicit and deterministic:
|
|
105
|
+
* 1. DSH_CLI_PATH
|
|
106
|
+
* 2. each PATH entry
|
|
107
|
+
* 3. resources/runtime/cli/bin/dsh[.cmd] from the DSH installation layout
|
|
108
|
+
* Existing but unusable candidates do not become a false success.
|
|
109
|
+
*/
|
|
110
|
+
export function resolveDshCommand(options = {}) {
|
|
111
|
+
const env = options.env || process.env
|
|
112
|
+
const platform = options.platform || process.platform
|
|
113
|
+
const execPath = options.execPath || process.execPath
|
|
114
|
+
const resourcesPath = options.resourcesPath === undefined ? process.resourcesPath : options.resourcesPath
|
|
115
|
+
const fileExists = options.fileExists || isFile
|
|
116
|
+
const candidates = []
|
|
117
|
+
const add = (path, source) => {
|
|
118
|
+
if (path && !candidates.some((item) => item.path === path)) candidates.push({ path, source })
|
|
119
|
+
}
|
|
120
|
+
if (typeof env.DSH_CLI_PATH === 'string' && env.DSH_CLI_PATH.trim()) add(env.DSH_CLI_PATH.trim(), 'DSH_CLI_PATH')
|
|
121
|
+
for (const path of pathCandidates(env, platform)) add(path, 'PATH')
|
|
122
|
+
for (const path of layoutCandidates(resourcesPath, execPath, platform)) add(path, 'DSH installation')
|
|
123
|
+
|
|
124
|
+
let unsupported = false
|
|
125
|
+
for (const candidate of candidates) {
|
|
126
|
+
if (!fileExists(candidate.path)) continue
|
|
127
|
+
const extension = extname(candidate.path).toLowerCase()
|
|
128
|
+
if (['.js', '.mjs', '.cjs'].includes(extension)) {
|
|
129
|
+
return {
|
|
130
|
+
file: execPath,
|
|
131
|
+
args: [candidate.path],
|
|
132
|
+
env: {},
|
|
133
|
+
source: candidate.source,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (platform === 'win32' && ['.cmd', '.bat'].includes(extension)) {
|
|
137
|
+
const converted = standardWindowsCommand(candidate.path, fileExists)
|
|
138
|
+
if (converted) return { ...converted, source: candidate.source }
|
|
139
|
+
unsupported = true
|
|
140
|
+
continue
|
|
141
|
+
}
|
|
142
|
+
return { file: candidate.path, args: [], env: {}, source: candidate.source }
|
|
143
|
+
}
|
|
144
|
+
if (unsupported) {
|
|
145
|
+
throw updateError('cli_unsupported', '找到了 Windows dsh.cmd/dsh.bat,但它不属于可直接执行的标准 DSH 安装布局;为避免 shell 注入,本次没有执行。请把 DSH_CLI_PATH 指向标准安装中的 dsh.cmd、dsh.exe 或 cli.js')
|
|
146
|
+
}
|
|
147
|
+
throw updateError('cli_not_found', '找不到可执行的 DSH CLI:已按 DSH_CLI_PATH → PATH → DSH 安装布局顺序查找。请把 DSH_CLI_PATH 指向 dsh 可执行文件或 cli.js')
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function outputCollector(limit) {
|
|
151
|
+
const chunks = []
|
|
152
|
+
let bytes = 0
|
|
153
|
+
let truncated = false
|
|
154
|
+
return {
|
|
155
|
+
append(chunk) {
|
|
156
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk ?? ''), 'utf8')
|
|
157
|
+
const remaining = Math.max(0, limit - bytes)
|
|
158
|
+
if (remaining > 0) chunks.push(buffer.subarray(0, Math.min(remaining, buffer.length)))
|
|
159
|
+
bytes += Math.min(remaining, buffer.length)
|
|
160
|
+
if (buffer.length > remaining) truncated = true
|
|
161
|
+
},
|
|
162
|
+
finish() {
|
|
163
|
+
return { text: Buffer.concat(chunks, bytes).toString('utf8'), truncated }
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function normalizeTimeout(value) {
|
|
169
|
+
const number = Number(value)
|
|
170
|
+
return Number.isFinite(number) && number > 0 ? Math.floor(number) : DEFAULT_UPDATE_TIMEOUT_MS
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Production runner: argv is an array and shell is always false. */
|
|
174
|
+
export function createSpawnRunner(options = {}) {
|
|
175
|
+
const spawnImpl = options.spawn || spawn
|
|
176
|
+
return async function spawnRunner(request) {
|
|
177
|
+
const timeoutMs = normalizeTimeout(request.timeoutMs ?? options.timeoutMs)
|
|
178
|
+
const env = options.env || process.env
|
|
179
|
+
const command = (options.resolveCommand || resolveDshCommand)({
|
|
180
|
+
env,
|
|
181
|
+
platform: options.platform,
|
|
182
|
+
execPath: options.execPath,
|
|
183
|
+
resourcesPath: options.resourcesPath,
|
|
184
|
+
fileExists: options.fileExists,
|
|
185
|
+
})
|
|
186
|
+
const args = [...command.args, ...pluginAddArguments(request.profile, request.version)]
|
|
187
|
+
|
|
188
|
+
return new Promise((resolveResult) => {
|
|
189
|
+
let child
|
|
190
|
+
try {
|
|
191
|
+
child = spawnImpl(command.file, args, {
|
|
192
|
+
env: { ...env, ...(command.env || {}) },
|
|
193
|
+
windowsHide: true,
|
|
194
|
+
shell: false,
|
|
195
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
196
|
+
})
|
|
197
|
+
} catch (error) {
|
|
198
|
+
resolveResult({ code: 'spawn_error', error: String((error && error.message) || error), stdout: '', stderr: '' })
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const stdout = outputCollector(UPDATE_OUTPUT_LIMIT_BYTES)
|
|
203
|
+
const stderr = outputCollector(UPDATE_OUTPUT_LIMIT_BYTES)
|
|
204
|
+
let settled = false
|
|
205
|
+
const finish = (result) => {
|
|
206
|
+
if (settled) return
|
|
207
|
+
settled = true
|
|
208
|
+
clearTimeout(timer)
|
|
209
|
+
const out = stdout.finish()
|
|
210
|
+
const err = stderr.finish()
|
|
211
|
+
resolveResult({
|
|
212
|
+
...result,
|
|
213
|
+
stdout: out.text,
|
|
214
|
+
stderr: err.text,
|
|
215
|
+
stdoutTruncated: out.truncated,
|
|
216
|
+
stderrTruncated: err.truncated,
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
const timer = setTimeout(() => {
|
|
220
|
+
try { child.kill('SIGKILL') } catch {}
|
|
221
|
+
finish({ code: 'timeout', error: 'DSH CLI 更新命令超过 ' + timeoutMs + 'ms,已终止该 CLI 子进程;未确认安装成功' })
|
|
222
|
+
}, timeoutMs)
|
|
223
|
+
|
|
224
|
+
child.stdout?.on('data', stdout.append)
|
|
225
|
+
child.stderr?.on('data', stderr.append)
|
|
226
|
+
child.on('error', (error) => {
|
|
227
|
+
finish({ code: 'spawn_error', error: String((error && error.message) || error) })
|
|
228
|
+
})
|
|
229
|
+
child.on('close', (code, signal) => {
|
|
230
|
+
if (typeof code === 'number') finish({ code })
|
|
231
|
+
else finish({ code: 'spawn_error', error: 'DSH CLI 未给出退出码' + (signal ? '(signal=' + signal + ')' : '') })
|
|
232
|
+
})
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export const spawnPluginUpdate = createSpawnRunner()
|
|
238
|
+
|
|
239
|
+
function capInjectedOutput(value) {
|
|
240
|
+
const text = String(value ?? '')
|
|
241
|
+
const buffer = Buffer.from(text, 'utf8')
|
|
242
|
+
if (buffer.length <= UPDATE_OUTPUT_LIMIT_BYTES) return { text, truncated: false }
|
|
243
|
+
// Back off at most three bytes so the cap never splits a UTF-8 continuation sequence.
|
|
244
|
+
let end = UPDATE_OUTPUT_LIMIT_BYTES
|
|
245
|
+
let back = 0
|
|
246
|
+
while (end > 0 && back < 4 && (buffer[end] & 0b1100_0000) === 0b1000_0000) {
|
|
247
|
+
end -= 1
|
|
248
|
+
back += 1
|
|
249
|
+
}
|
|
250
|
+
return { text: buffer.subarray(0, end).toString('utf8'), truncated: true }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function normalizeRunnerResult(result) {
|
|
254
|
+
const stdout = capInjectedOutput(result && result.stdout)
|
|
255
|
+
const stderr = capInjectedOutput(result && result.stderr)
|
|
256
|
+
return {
|
|
257
|
+
...(result || {}),
|
|
258
|
+
stdout: stdout.text,
|
|
259
|
+
stderr: stderr.text,
|
|
260
|
+
stdoutTruncated: Boolean(result && result.stdoutTruncated) || stdout.truncated,
|
|
261
|
+
stderrTruncated: Boolean(result && result.stderrTruncated) || stderr.truncated,
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function normalizeFailure(error) {
|
|
266
|
+
const rawCode = String((error && error.code) || '')
|
|
267
|
+
const code = rawCode.toLowerCase() === 'cli_not_found' ? 'cli_not_found'
|
|
268
|
+
: rawCode.toLowerCase() === 'cli_unsupported' ? 'cli_unsupported'
|
|
269
|
+
: rawCode || 'update_failed'
|
|
270
|
+
return {
|
|
271
|
+
ok: false,
|
|
272
|
+
code,
|
|
273
|
+
error: String((error && error.message) || error || '更新失败').slice(0, 1000),
|
|
274
|
+
stdout: '',
|
|
275
|
+
stderr: '',
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Single-process/cross-module update gate plus an outer timeout for injected runners.
|
|
281
|
+
* After an outer timeout the lock stays held until a late runner actually settles,
|
|
282
|
+
* so a hung runner can never cause a second installation to overlap the first.
|
|
283
|
+
*/
|
|
284
|
+
export async function runExclusiveUpdate(request) {
|
|
285
|
+
if (!isStrictSemver(request && request.version)) return normalizeFailure(updateError('invalid_version', 'version 必须是严格 semver'))
|
|
286
|
+
let profile
|
|
287
|
+
try { profile = resolveProfile(request.env || process.env) } catch (error) { return normalizeFailure(error) }
|
|
288
|
+
if (globalThis[UPDATE_LOCK_KEY]) {
|
|
289
|
+
return { ok: false, code: 'update_in_flight', error: '已有一个插件更新命令在执行,本次没有重复启动', stdout: '', stderr: '' }
|
|
290
|
+
}
|
|
291
|
+
globalThis[UPDATE_LOCK_KEY] = true
|
|
292
|
+
const runner = request.runner || spawnPluginUpdate
|
|
293
|
+
const timeoutMs = normalizeTimeout(request.timeoutMs)
|
|
294
|
+
let timedOut = false
|
|
295
|
+
let released = false
|
|
296
|
+
const release = () => {
|
|
297
|
+
if (released) return
|
|
298
|
+
released = true
|
|
299
|
+
if (globalThis[UPDATE_LOCK_KEY]) delete globalThis[UPDATE_LOCK_KEY]
|
|
300
|
+
}
|
|
301
|
+
const work = Promise.resolve().then(() => runner({
|
|
302
|
+
version: request.version,
|
|
303
|
+
profile,
|
|
304
|
+
timeoutMs,
|
|
305
|
+
}))
|
|
306
|
+
// Attach both handlers now: a late rejection after the HTTP timeout is still handled.
|
|
307
|
+
work.then(() => { if (timedOut) release() }, () => { if (timedOut) release() })
|
|
308
|
+
let timer
|
|
309
|
+
let outerTimeoutError = null
|
|
310
|
+
try {
|
|
311
|
+
const result = await new Promise((resolveResult, rejectResult) => {
|
|
312
|
+
outerTimeoutError = updateError('timeout', '等待更新命令超过 ' + timeoutMs + 'ms;未确认安装成功,请核对磁盘版本后再试')
|
|
313
|
+
timer = setTimeout(() => rejectResult(outerTimeoutError), timeoutMs)
|
|
314
|
+
work.then(resolveResult, rejectResult)
|
|
315
|
+
})
|
|
316
|
+
const normalized = normalizeRunnerResult(result)
|
|
317
|
+
return { ...normalized, ok: normalized.code === 0, profile, version: request.version }
|
|
318
|
+
} catch (error) {
|
|
319
|
+
if (error === outerTimeoutError) {
|
|
320
|
+
timedOut = true
|
|
321
|
+
return { ...normalizeFailure(error), code: 'timeout', profile, version: request.version }
|
|
322
|
+
}
|
|
323
|
+
return { ...normalizeFailure(error), profile, version: request.version }
|
|
324
|
+
} finally {
|
|
325
|
+
clearTimeout(timer)
|
|
326
|
+
if (!timedOut) release()
|
|
327
|
+
}
|
|
328
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-session-superagent",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.271",
|
|
4
4
|
"description": "Session-level superagent for DeepSeek Harness: watch a GitHub repository and wake a session when something needs doing. Credentials never reach the model.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"lib/body.js",
|
|
22
22
|
"lib/gitee.js",
|
|
23
23
|
"lib/state-migration.js",
|
|
24
|
+
"lib/update.js",
|
|
24
25
|
"cordis.patch.yml",
|
|
25
26
|
"README.md",
|
|
26
27
|
"SETUP.md",
|
package/ui-host-primitives.js
CHANGED
|
@@ -71,6 +71,9 @@ export function createHostUi({ React, primitives } = {}) {
|
|
|
71
71
|
|
|
72
72
|
function Tooltip({ text, icon = 'question', warn = false, children } = {}) {
|
|
73
73
|
const HostTooltip = primitive('Tooltip')
|
|
74
|
+
if (typeof React.cloneElement !== 'function') {
|
|
75
|
+
throw new TypeError('Tooltip 需要宿主 React.cloneElement 以合并 trigger ref')
|
|
76
|
+
}
|
|
74
77
|
const [remountKey, setRemountKey] = React.useState(0)
|
|
75
78
|
const triggerNodeRef = React.useRef(null)
|
|
76
79
|
const restoreFocusRef = React.useRef(false)
|
package/ui-orbit-popover.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
// 这里不包含 watcher 轮询、行数据、删除请求、确认 Modal 或另一套定位实现。
|
|
4
4
|
|
|
5
5
|
const DEFAULT_ORBIT_Z_INDEX = 1100
|
|
6
|
+
// confirm 由父组件的 useOrbitConfirm 直接关闭 orbit,不经过 OrbitPopover.close;按 ref 关联原因,避免新增公开参数。
|
|
7
|
+
const orbitCloseReasons = new WeakMap()
|
|
6
8
|
|
|
7
9
|
function focusIfConnected(node) {
|
|
8
10
|
if (!node || typeof node.focus !== 'function') return false
|
|
@@ -81,11 +83,15 @@ export function createOrbitPopover({ React, primitives } = {}) {
|
|
|
81
83
|
const returnFocusRef = orbitRef || anchorRef
|
|
82
84
|
const onCloseRef = React.useRef(onClose)
|
|
83
85
|
onCloseRef.current = onClose
|
|
86
|
+
const didFocusRef = React.useRef(false)
|
|
87
|
+
const closeReasonRef = React.useRef(null)
|
|
84
88
|
|
|
85
89
|
// stable close:父级轮询重渲染只更新 ref,不重建外点 effect 或焦点 effect。
|
|
86
90
|
const close = React.useCallback((detail) => {
|
|
91
|
+
const reason = closeReason(detail)
|
|
92
|
+
closeReasonRef.current = reason.reason
|
|
87
93
|
const callback = onCloseRef.current
|
|
88
|
-
if (typeof callback === 'function') callback(
|
|
94
|
+
if (typeof callback === 'function') callback(reason)
|
|
89
95
|
}, [])
|
|
90
96
|
|
|
91
97
|
// 两个宿主 hooks 与本地 effect 都在 if (!open) return 之前无条件调用。
|
|
@@ -107,11 +113,20 @@ export function createOrbitPopover({ React, primitives } = {}) {
|
|
|
107
113
|
? portalTarget.ownerDocument
|
|
108
114
|
: (typeof document !== 'undefined' ? document : null)
|
|
109
115
|
|
|
116
|
+
// effect 在 portal mount 后运行;非 Modal dialog 自身接收初始焦点,不做焦点陷阱。
|
|
110
117
|
React.useEffect(() => {
|
|
111
|
-
if (!open
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
118
|
+
if (!open) {
|
|
119
|
+
didFocusRef.current = false
|
|
120
|
+
closeReasonRef.current = null
|
|
121
|
+
orbitCloseReasons.delete(returnFocusRef)
|
|
122
|
+
return undefined
|
|
123
|
+
}
|
|
124
|
+
// 首帧 position=null 时 surface 是 hidden,focus() 会被浏览器拒绝;等可见后只尝试一次。
|
|
125
|
+
if (position && !didFocusRef.current) {
|
|
126
|
+
didFocusRef.current = true
|
|
127
|
+
focusIfConnected(panelRef.current)
|
|
128
|
+
}
|
|
129
|
+
if (!ownerDocument || typeof ownerDocument.addEventListener !== 'function') return undefined
|
|
115
130
|
const onKeyDown = (event) => {
|
|
116
131
|
if (!event || event.key !== 'Escape' || event.repeat) return
|
|
117
132
|
if (typeof event.preventDefault === 'function') event.preventDefault()
|
|
@@ -121,10 +136,12 @@ export function createOrbitPopover({ React, primitives } = {}) {
|
|
|
121
136
|
ownerDocument.addEventListener('keydown', onKeyDown, true)
|
|
122
137
|
return () => {
|
|
123
138
|
ownerDocument.removeEventListener('keydown', onKeyDown, true)
|
|
124
|
-
|
|
125
|
-
|
|
139
|
+
closeReasonRef.current = closeReasonRef.current || orbitCloseReasons.get(returnFocusRef) || null
|
|
140
|
+
// 外点要把焦点留给用户点击的目标;确认要让 Modal 持有焦点。只有 Escape 显式回圆环。
|
|
141
|
+
if (closeReasonRef.current === 'escape') focusIfConnected(returnFocusRef && returnFocusRef.current)
|
|
142
|
+
closeReasonRef.current = null
|
|
126
143
|
}
|
|
127
|
-
}, [open, close, ownerDocument, panelRef, returnFocusRef])
|
|
144
|
+
}, [open, position, close, ownerDocument, panelRef, returnFocusRef])
|
|
128
145
|
|
|
129
146
|
if (!open) return null
|
|
130
147
|
if (!anchorRef || typeof anchorRef !== 'object') {
|
|
@@ -136,6 +153,9 @@ export function createOrbitPopover({ React, primitives } = {}) {
|
|
|
136
153
|
if (!portalTarget) {
|
|
137
154
|
throw new Error('OrbitPopover portal target 不可用;不会退化为非 portal surface')
|
|
138
155
|
}
|
|
156
|
+
// DSH 只 clamp 坐标,不会替你缩宽;368px 在 320/360px 视口会把 left 挤成负数。
|
|
157
|
+
const viewportWidth = typeof window !== 'undefined' && Number.isFinite(window.innerWidth) ? window.innerWidth : width + 16
|
|
158
|
+
const effectiveWidth = Math.min(width, Math.max(1, viewportWidth - 16))
|
|
139
159
|
|
|
140
160
|
const surface = h(MenuSurface, {
|
|
141
161
|
ref: panelRef,
|
|
@@ -148,7 +168,7 @@ export function createOrbitPopover({ React, primitives } = {}) {
|
|
|
148
168
|
position: 'fixed',
|
|
149
169
|
left: position ? position.left : 0,
|
|
150
170
|
top: position ? position.top : 0,
|
|
151
|
-
width,
|
|
171
|
+
width: effectiveWidth,
|
|
152
172
|
zIndex,
|
|
153
173
|
// useAnchoredPosition 首帧尚未测量时不在 0,0 闪现;不自行计算另一套坐标。
|
|
154
174
|
visibility: position ? 'visible' : 'hidden',
|
|
@@ -172,6 +192,10 @@ export function createOrbitPopover({ React, primitives } = {}) {
|
|
|
172
192
|
|
|
173
193
|
const requestConfirm = React.useCallback((detail = {}) => {
|
|
174
194
|
restoreFocusRef.current = 'orbit'
|
|
195
|
+
const ref = orbitRefRef.current
|
|
196
|
+
if (ref && (typeof ref === 'object' || typeof ref === 'function')) {
|
|
197
|
+
orbitCloseReasons.set(ref, 'confirm')
|
|
198
|
+
}
|
|
175
199
|
if (typeof onCloseOrbitRef.current === 'function') {
|
|
176
200
|
onCloseOrbitRef.current({ reason: 'confirm' })
|
|
177
201
|
}
|