dsh-rig 0.1.3 → 0.1.4
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 +34 -0
- package/README.en.md +23 -6
- package/README.md +23 -5
- package/docs/DESIGN.md +69 -0
- package/docs/screenshot-panel.png +0 -0
- package/icon.svg +12 -6
- package/lib/client.js +384 -387
- package/lib/http/routes.js +56 -9
- package/lib/index.js +6 -2
- package/lib/lab/constants.js +1 -1
- package/lib/lab/drive-guard.js +64 -0
- package/lib/lab/ownership.js +51 -0
- package/lib/lab/registry.js +4 -0
- package/lib/lab/session-title.js +65 -0
- package/lib/lab/supervisor.js +17 -17
- package/lib/tools/lab-tool.js +18 -6
- package/lib/tools/lab-ui-tool.js +8 -1
- package/lib/tools/mount.js +3 -3
- package/package.json +2 -2
package/lib/client.js
CHANGED
|
@@ -1,143 +1,109 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* dsh-rig · client half — 右栏「插件实验室」面板
|
|
3
3
|
*
|
|
4
|
-
* ⚠️ 这个文件是**一个 ModuleLoader bundle**:协议上没法拆成多文件(factory 只能 require
|
|
5
|
-
*
|
|
6
|
-
* 分区顺序固定:常量 → 样式 → 通信 → hook → 原子组件 → 容器 → 注册。
|
|
4
|
+
* ⚠️ 这个文件是**一个 ModuleLoader bundle**:协议上没法拆成多文件(factory 只能 require 宿主导出的模块)。
|
|
5
|
+
* 所以「组件化」靠文件内分区保证,顺序固定:常量 → 样式 → 图标 → 通信 → 页级 store → hook → 原子组件 → 三带 → 容器 → 注册。
|
|
7
6
|
* 结构守卫(test/structure.test.mjs)盯着它的行长上限与 API 前缀对拍。
|
|
8
7
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* 数据面走宿主 webServer 的 `/dsh-rig/api`:事件流用 fetch 手工解 SSE(EventSource 发不了
|
|
13
|
-
* 自定义头),命令走 POST。iframe 是跨源的,所以「锁定」只能挡输入,不能碰它内部状态。
|
|
8
|
+
* 四件事:事件流是页级单例(面板没开时也要能收到 `open-lab`);iframe 指向宿主稳定入口而非实验室裸地址;
|
|
9
|
+
* 占用靠「动手才 claim」;锁定只挡输入,不是安全边界。
|
|
14
10
|
*/
|
|
15
11
|
window.__ModuleLoader__.load({
|
|
16
12
|
id: 'dsh-rig',
|
|
17
13
|
factory(require) {
|
|
18
14
|
const React = require('react')
|
|
19
15
|
const h = React.createElement
|
|
20
|
-
const { useState, useEffect, useRef
|
|
16
|
+
const { useState, useEffect, useRef } = React
|
|
21
17
|
|
|
22
|
-
// ── 1.
|
|
18
|
+
// ── 1. 常量与文案 ──────────────────────────────────────────────
|
|
23
19
|
const TAB_ID = 'dsh-rig'
|
|
24
20
|
const TAB_KIND = 'dsh-rig:lab'
|
|
25
21
|
const GUIDE_ORDER = 45
|
|
26
|
-
|
|
27
|
-
/** 与 host 半 `lib/lab/constants.js` 的 API_PREFIX 必须一致,由结构守卫对拍。 */
|
|
22
|
+
/** 与宿主侧 `lib/lab/constants.js` 的 API_PREFIX 一致,由结构守卫 4b 对拍。 */
|
|
28
23
|
const API = '/dsh-rig/api'
|
|
29
24
|
const HEADERS = { 'x-dsh-rig': '1' }
|
|
30
25
|
const LOG_TAIL = 80
|
|
31
26
|
const LOG_KEEP = 400
|
|
27
|
+
const ICON_MASK = 'dsh-rig-mask'
|
|
32
28
|
|
|
33
29
|
const T = {
|
|
34
30
|
tab: '实验室',
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
panel: '插件实验室',
|
|
32
|
+
guide: { title: '插件实验室', desc: '开一个真实、隔离的 DSH,看它跑起来' },
|
|
33
|
+
empty: { title: '还没有实验室', desc: '建一个之后,这里会嵌进来一个真实的 DSH 进程。', action: '创建实验室', busy: '创建中…' },
|
|
34
|
+
act: {
|
|
35
|
+
start: '启动', stop: '停止', takeover: '接手', more: '更多操作', restart: '重启', refresh: '刷新',
|
|
36
|
+
popout: '弹出到窗口', version: '切换版本', about: '关于隔离边界', destroy: '销毁', confirm: '确认销毁',
|
|
37
|
+
cancel: '取消', back: '返回', hostVersion: '跟随宿主', ok: '知道了',
|
|
38
|
+
},
|
|
39
|
+
status: { created: '未启动', starting: '启动中', running: '运行中', stopped: '已停止', crashed: '已崩溃' },
|
|
40
|
+
owner: { me: '本会话', free: '空闲', other: '会话', freeTitle: '没有会话在驾驶它' },
|
|
41
|
+
lock: { on: '已锁定', off: '可介入', hint: '锁定只挡输入,不是安全边界' },
|
|
46
42
|
agent: {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
off: '未授权',
|
|
50
|
-
hintOff: '授权后智能体才能点按钮、打字、截图(工具只在授权期间存在)',
|
|
51
|
-
hintOn: '智能体现在可以操作这个实验室的界面',
|
|
52
|
-
noBackend: '本机没有可用浏览器后端(装 playwright 后可用)',
|
|
43
|
+
on: '智能体可操作', off: '智能体未授权', noBackend: '本机没有可用浏览器后端',
|
|
44
|
+
hintOn: '授权期间智能体才能点按钮、打字、截图', hintOff: '授权后智能体才能操作界面(工具只在授权期间存在)',
|
|
53
45
|
},
|
|
54
|
-
lockBadge: '已锁定 · 点「可介入」解锁',
|
|
55
|
-
viewport: { idle: '实验室未运行', hint: '点「启动」后在这里看它', starting: '启动中…' },
|
|
56
46
|
logs: { title: '日志', empty: '(暂无日志)' },
|
|
47
|
+
view: { idle: '实验室未运行', starting: '启动中…', hint: '点「启动」后在这里看它', taken: '由别的会话驾驶 · 只读', crashed: '实验室崩了' },
|
|
57
48
|
conn: { on: '已连接', off: '未连接' },
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
about: '实验室是独立进程 + 独立 DSH_HOME:它崩了这边不会崩。锁定只挡输入,不是安全边界。',
|
|
50
|
+
destroyWarn: '销毁会删掉实验室的目录与 profile,不能撤销。',
|
|
51
|
+
loadingVersions: '读取版本清单…',
|
|
61
52
|
}
|
|
62
53
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
starting: 'var(--dsw-alias-state-warn-primary)',
|
|
67
|
-
running: 'var(--dsw-alias-state-success-primary)',
|
|
68
|
-
crashed: 'var(--dsw-alias-state-error-primary)',
|
|
69
|
-
}
|
|
54
|
+
/** 状态 → 令牌名,一句 `ink()` 就能取色,不散落九个变量名。 */
|
|
55
|
+
const STATE_INK = { created: 'idle', stopped: 'idle', starting: 'warn', running: 'success', crashed: 'error' }
|
|
56
|
+
const ink = (status) => `var(--dsw-alias-state-${STATE_INK[status] || 'idle'}-primary)`
|
|
70
57
|
|
|
71
58
|
// ── 2. 样式(全部走主题令牌,浅色/深色都跟宿主) ──────────────────
|
|
59
|
+
const BOX = '1px solid var(--dsw-alias-border-l1)'
|
|
72
60
|
const S = {
|
|
73
|
-
panel: {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
},
|
|
78
|
-
|
|
61
|
+
panel: { display: 'flex', flexDirection: 'column', height: '100%', minHeight: 0, boxSizing: 'border-box', color: 'var(--dsw-alias-label-primary)', fontSize: 13, lineHeight: 1.5 },
|
|
62
|
+
head: { display: 'flex', alignItems: 'center', gap: 6, padding: '10px 12px 6px', flex: 'none', minWidth: 0 },
|
|
63
|
+
band: { display: 'flex', alignItems: 'center', gap: 6, padding: '0 12px 8px', flex: 'none', flexWrap: 'wrap' },
|
|
64
|
+
body: { flex: 1, minHeight: 120, display: 'flex', padding: '0 12px' },
|
|
65
|
+
foot: { display: 'flex', alignItems: 'center', gap: 6, padding: '8px 12px 10px', flex: 'none', flexWrap: 'wrap' },
|
|
66
|
+
grow: { flex: 1, minWidth: 4 },
|
|
79
67
|
title: { fontSize: 13, fontWeight: 600, letterSpacing: 0.2, whiteSpace: 'nowrap' },
|
|
80
|
-
pill: {
|
|
81
|
-
display: 'inline-flex', alignItems: 'center', gap: 6, padding: '2px 8px', borderRadius: 999,
|
|
82
|
-
border: '1px solid var(--dsw-alias-border-l1)', background: 'var(--dsw-alias-bg-layer-1)',
|
|
83
|
-
fontSize: 11, color: 'var(--dsw-alias-label-secondary)', whiteSpace: 'nowrap',
|
|
84
|
-
},
|
|
85
|
-
dot: (color) => ({ width: 7, height: 7, borderRadius: '50%', background: color, flex: 'none' }),
|
|
86
|
-
spacer: { flex: 1 },
|
|
87
68
|
meta: { fontSize: 11, color: 'var(--dsw-alias-label-secondary)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' },
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},
|
|
69
|
+
chip: { display: 'inline-flex', alignItems: 'center', gap: 5, padding: '2px 8px', borderRadius: 999, border: BOX, background: 'var(--dsw-alias-bg-layer-1)', color: 'var(--dsw-alias-label-secondary)', fontSize: 11, whiteSpace: 'nowrap', maxWidth: '100%', overflow: 'hidden', textOverflow: 'ellipsis', font: 'inherit' },
|
|
70
|
+
chipOff: { cursor: 'not-allowed', opacity: 0.55 },
|
|
71
|
+
dot: (color) => ({ width: 7, height: 7, borderRadius: '50%', background: color, flex: 'none' }),
|
|
72
|
+
btn: { appearance: 'none', display: 'inline-flex', alignItems: 'center', gap: 5, border: BOX, background: 'var(--dsw-alias-bg-layer-1)', color: 'var(--dsw-alias-label-primary)', borderRadius: 8, padding: '5px 10px', fontSize: 12, cursor: 'pointer', font: 'inherit', whiteSpace: 'nowrap' },
|
|
93
73
|
btnPrimary: { borderColor: 'var(--dsw-alias-brand-primary)', color: 'var(--dsw-alias-brand-primary)' },
|
|
94
|
-
btnDanger: { color: 'var(--dsw-alias-state-error-primary)' },
|
|
95
|
-
btnOff: { cursor: 'not-allowed', color: 'var(--dsw-alias-state-idle-primary)' },
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
},
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
flex: 'none', maxHeight: 150, overflow: 'auto', border: '1px solid var(--dsw-alias-border-l1)',
|
|
123
|
-
borderRadius: 8, background: 'var(--dsw-alias-bg-layer-1)', padding: '6px 8px',
|
|
124
|
-
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace', fontSize: 11, lineHeight: 1.45,
|
|
125
|
-
},
|
|
126
|
-
logLine: { whiteSpace: 'pre-wrap', wordBreak: 'break-all', color: 'var(--dsw-alias-label-secondary)' },
|
|
127
|
-
logErr: { color: 'var(--dsw-alias-state-error-primary)' },
|
|
128
|
-
errorBar: {
|
|
129
|
-
flex: 'none', fontSize: 12, padding: '6px 8px', borderRadius: 8,
|
|
130
|
-
border: '1px solid var(--dsw-alias-state-error-primary)', color: 'var(--dsw-alias-state-error-primary)',
|
|
131
|
-
},
|
|
132
|
-
hint: { fontSize: 11, color: 'var(--dsw-alias-label-secondary)', flex: 'none' },
|
|
133
|
-
headerButton: {
|
|
134
|
-
appearance: 'none', display: 'inline-flex', alignItems: 'center', gap: 6, border: '1px solid var(--dsw-alias-border-l1)',
|
|
135
|
-
background: 'var(--dsw-alias-bg-layer-1)', color: 'var(--dsw-alias-label-secondary)', borderRadius: 999,
|
|
136
|
-
padding: '3px 10px', fontSize: 12, cursor: 'pointer', font: 'inherit', flex: 'none',
|
|
137
|
-
},
|
|
74
|
+
btnDanger: { borderColor: 'var(--dsw-alias-state-error-primary)', color: 'var(--dsw-alias-state-error-primary)' },
|
|
75
|
+
btnOff: { cursor: 'not-allowed', color: 'var(--dsw-alias-state-idle-primary)', borderColor: 'var(--dsw-alias-border-l1)' },
|
|
76
|
+
pill: { display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: 11, color: 'var(--dsw-alias-label-secondary)', whiteSpace: 'nowrap', fontVariantNumeric: 'tabular-nums' },
|
|
77
|
+
viewport: { position: 'relative', flex: 1, minHeight: 140, border: '1px solid var(--dsw-alias-border-l2)', borderRadius: 10, overflow: 'hidden', background: 'var(--dsw-alias-bg-base)', display: 'flex' },
|
|
78
|
+
frame: { width: '100%', height: '100%', border: 'none', display: 'block', background: 'transparent', flex: 1 },
|
|
79
|
+
idle: { position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 6, textAlign: 'center', padding: 16, color: 'var(--dsw-alias-label-secondary)', fontSize: 12 },
|
|
80
|
+
shield: { position: 'absolute', inset: 0, zIndex: 2, cursor: 'not-allowed', background: 'color-mix(in srgb, var(--dsw-alias-bg-base) 10%, transparent)', display: 'flex', alignItems: 'flex-start', justifyContent: 'center', paddingTop: 8 },
|
|
81
|
+
shieldTag: { fontSize: 11, padding: '2px 8px', borderRadius: 999, border: '1px solid var(--dsw-alias-border-l2)', background: 'var(--dsw-alias-bg-overlay)', color: 'var(--dsw-alias-label-secondary)' },
|
|
82
|
+
drawer: { flex: 'none', maxHeight: 140, overflow: 'auto', margin: '8px 12px 0', border: BOX, borderRadius: 8, background: 'var(--dsw-alias-bg-layer-1)', padding: '6px 8px', fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace', fontSize: 11, lineHeight: 1.45 },
|
|
83
|
+
line: { whiteSpace: 'pre-wrap', wordBreak: 'break-all', color: 'var(--dsw-alias-label-secondary)' },
|
|
84
|
+
lineErr: { color: 'var(--dsw-alias-state-error-primary)' },
|
|
85
|
+
note: { fontSize: 11, color: 'var(--dsw-alias-label-secondary)', padding: '2px 0' },
|
|
86
|
+
error: { margin: '0 12px 8px', flex: 'none', fontSize: 12, padding: '6px 8px', borderRadius: 8, border: '1px solid var(--dsw-alias-state-error-primary)', color: 'var(--dsw-alias-state-error-primary)' },
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ── 3. 图标:icon.svg 的内联版(双层窗口 = 面板里套了一个真东西,绿点 = 它在跑) ──
|
|
90
|
+
function LabIcon(props) {
|
|
91
|
+
const size = props && props.size ? props.size : 24
|
|
92
|
+
return h(
|
|
93
|
+
'svg',
|
|
94
|
+
{ className: props && props.className, width: size, height: size, viewBox: '0 0 36 36', fill: 'none', 'aria-hidden': true },
|
|
95
|
+
h('defs', null, h('mask', { id: ICON_MASK },
|
|
96
|
+
h('rect', { width: 36, height: 36, fill: '#fff' }),
|
|
97
|
+
h('rect', { x: 11.2, y: 13.2, width: 21.6, height: 17.6, rx: 5, fill: '#000' }))),
|
|
98
|
+
h('rect', { x: 5, y: 5.5, width: 19, height: 14.5, rx: 4, stroke: '#7fb2d9', strokeWidth: 2, mask: `url(#${ICON_MASK})` }),
|
|
99
|
+
h('rect', { x: 13, y: 15, width: 18, height: 14, rx: 4, stroke: '#247bbf', strokeWidth: 2 }),
|
|
100
|
+
h('circle', { cx: 17.4, cy: 19.2, r: 1.7, fill: '#37b26d' }),
|
|
101
|
+
)
|
|
138
102
|
}
|
|
139
103
|
|
|
140
|
-
// ──
|
|
104
|
+
// ── 4. 通信(唯一的 HTTP 出入口) ────────────────────────────────
|
|
105
|
+
const message = (err) => (err && err.message ? err.message : String(err ?? ''))
|
|
106
|
+
|
|
141
107
|
async function request(path, init) {
|
|
142
108
|
const res = await fetch(API + path, { ...init, headers: { ...HEADERS, ...((init && init.headers) || {}) } })
|
|
143
109
|
if (!res.ok) {
|
|
@@ -152,7 +118,7 @@ window.__ModuleLoader__.load({
|
|
|
152
118
|
return res.json()
|
|
153
119
|
}
|
|
154
120
|
|
|
155
|
-
/** 手工解 SSE
|
|
121
|
+
/** 手工解 SSE(EventSource 发不了自定义头)。返回时说明流结束了,不是错误。 */
|
|
156
122
|
async function consumeEvents(onEvent, signal) {
|
|
157
123
|
const res = await fetch(`${API}/events`, { headers: { ...HEADERS, accept: 'text/event-stream' }, signal })
|
|
158
124
|
if (!res.ok || !res.body) throw new Error(`事件流 HTTP ${res.status}`)
|
|
@@ -179,323 +145,362 @@ window.__ModuleLoader__.load({
|
|
|
179
145
|
}
|
|
180
146
|
}
|
|
181
147
|
|
|
182
|
-
// ──
|
|
183
|
-
function
|
|
184
|
-
const
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const [
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
148
|
+
// ── 5. 页级 store:一条 SSE 常驻,面板只是订阅者 ─────────────────
|
|
149
|
+
function createStore() {
|
|
150
|
+
const state = { labs: [], logs: {}, connected: false, error: '', uiMode: false, ui: null, frameSig: '', frameGen: 0 }
|
|
151
|
+
const listeners = new Set()
|
|
152
|
+
let controller = null
|
|
153
|
+
let retry = null
|
|
154
|
+
let watchdog = null
|
|
155
|
+
let onOpenLab = () => {}
|
|
156
|
+
|
|
157
|
+
// 订阅方自己炸了不该拖垮事件流,所以逐个 try。
|
|
158
|
+
const notify = () => { for (const fn of [...listeners]) { try { fn() } catch { /* 忽略 */ } } }
|
|
159
|
+
const set = (patch) => {
|
|
160
|
+
Object.assign(state, patch)
|
|
161
|
+
notify()
|
|
162
|
+
}
|
|
163
|
+
const apply = (data) => set({
|
|
164
|
+
labs: data.labs || state.labs,
|
|
165
|
+
...(data.diagnostics ? { diagnostics: data.diagnostics } : {}),
|
|
166
|
+
...(typeof data.uiMode === 'boolean' ? { uiMode: data.uiMode } : {}),
|
|
167
|
+
...(data.ui ? { ui: data.ui } : {}),
|
|
168
|
+
...(typeof data.frameSig === 'string' ? { frameSig: data.frameSig } : {}),
|
|
169
|
+
})
|
|
170
|
+
const pushLog = (id, entry) => {
|
|
171
|
+
const list = (state.logs[id] || []).concat(entry)
|
|
172
|
+
state.logs = { ...state.logs, [id]: list.length > LOG_KEEP ? list.slice(list.length - LOG_KEEP) : list }
|
|
173
|
+
notify()
|
|
174
|
+
}
|
|
175
|
+
const refresh = () => request('/state').then(apply).catch(() => {})
|
|
176
|
+
|
|
177
|
+
const onEvent = (event) => {
|
|
178
|
+
if (event.type === 'snapshot') apply(event)
|
|
179
|
+
else if (event.type === 'ui-mode') set({ uiMode: Boolean(event.enabled) })
|
|
180
|
+
else if (event.type === 'url') {
|
|
181
|
+
// 实验室换了地址(启动/重启):自增一代,逼 iframe 重新走一次稳定入口拿新 token。
|
|
182
|
+
set({ frameGen: state.frameGen + 1 })
|
|
183
|
+
refresh()
|
|
184
|
+
} else if (event.type === 'log') pushLog(event.id, event.entry)
|
|
185
|
+
else if (event.type === 'open-lab') onOpenLab(event.sessionId || null)
|
|
186
|
+
else if (event.type === 'status' || event.type === 'labs') refresh()
|
|
187
|
+
}
|
|
197
188
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
189
|
+
const loop = () => {
|
|
190
|
+
if (!controller || controller.signal.aborted) return
|
|
191
|
+
set({ connected: true })
|
|
192
|
+
consumeEvents(onEvent, controller.signal)
|
|
193
|
+
.then(() => {
|
|
194
|
+
set({ connected: false })
|
|
195
|
+
if (!controller.signal.aborted) retry = setTimeout(loop, 2_000)
|
|
202
196
|
})
|
|
197
|
+
.catch((err) => {
|
|
198
|
+
set({ connected: false })
|
|
199
|
+
if (controller.signal.aborted) return
|
|
200
|
+
set({ error: `事件流断开:${message(err)}` })
|
|
201
|
+
retry = setTimeout(loop, 3_000)
|
|
202
|
+
})
|
|
203
|
+
}
|
|
203
204
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (typeof event.uiMode === 'boolean') setUiMode(event.uiMode)
|
|
222
|
-
if (event.ui) setUi(event.ui)
|
|
223
|
-
if (typeof event.frameSig === 'string') setFrameSig(event.frameSig)
|
|
224
|
-
} else if (event.type === 'ui-mode') {
|
|
225
|
-
setUiMode(Boolean(event.enabled))
|
|
226
|
-
} else if (event.type === 'url') {
|
|
227
|
-
// 实验室换了地址(启动/重启):让 iframe 重新走一次稳定入口,拿新 token。
|
|
228
|
-
// 只把 key 绑在入口地址上不够 —— 入口地址本身不变,必须由这次自增触发导航。
|
|
229
|
-
setFrameGen((n) => n + 1)
|
|
230
|
-
refresh()
|
|
231
|
-
} else if (event.type === 'log') {
|
|
232
|
-
pushLog(event.id, event.entry)
|
|
233
|
-
} else if (event.type === 'status' || event.type === 'labs') {
|
|
234
|
-
refresh()
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const loop = () => {
|
|
239
|
-
if (controller.signal.aborted) return
|
|
240
|
-
setConnected(true)
|
|
241
|
-
consumeEvents(applyEvent, controller.signal)
|
|
242
|
-
.then(() => {
|
|
243
|
-
setConnected(false)
|
|
244
|
-
if (!controller.signal.aborted) retry = setTimeout(loop, 2_000)
|
|
205
|
+
return {
|
|
206
|
+
get: () => state,
|
|
207
|
+
subscribe(fn) {
|
|
208
|
+
listeners.add(fn)
|
|
209
|
+
return () => listeners.delete(fn)
|
|
210
|
+
},
|
|
211
|
+
set,
|
|
212
|
+
refresh,
|
|
213
|
+
/** 补历史:事件流只推「之后」的行,面板后开时前面那些得自己拉一次环形缓冲。 */
|
|
214
|
+
seedLogs(id) {
|
|
215
|
+
if (id === '' || (state.logs[id] || []).length > 0) return Promise.resolve()
|
|
216
|
+
return request(`/logs?id=${encodeURIComponent(id)}`)
|
|
217
|
+
.then((data) => {
|
|
218
|
+
const entries = (data && data.entries) || []
|
|
219
|
+
if (entries.length === 0) return
|
|
220
|
+
state.logs = { ...state.logs, [id]: entries.slice(-LOG_KEEP) }
|
|
221
|
+
notify()
|
|
245
222
|
})
|
|
246
|
-
.catch((
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
223
|
+
.catch(() => {})
|
|
224
|
+
},
|
|
225
|
+
configure(handlers) {
|
|
226
|
+
if (handlers && typeof handlers.onOpenLab === 'function') onOpenLab = handlers.onOpenLab
|
|
227
|
+
},
|
|
228
|
+
start() {
|
|
229
|
+
if (controller) return
|
|
230
|
+
controller = new AbortController()
|
|
231
|
+
request('/state')
|
|
232
|
+
.then((data) => {
|
|
233
|
+
apply(data)
|
|
234
|
+
set({ error: '' })
|
|
235
|
+
loop()
|
|
251
236
|
})
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
.
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* 看门狗:事件流可能**安静地挂住**(不报错、也不再送数据),面板就停在旧状态。
|
|
264
|
-
* 25 秒无事件则主动拉一次状态:不解决根因,但保证「旧一拍」自愈。
|
|
265
|
-
*/
|
|
266
|
-
const watchdog = setInterval(() => {
|
|
267
|
-
if (controller.signal.aborted) return
|
|
268
|
-
if (Date.now() - lastEventAt > 25_000) refresh()
|
|
269
|
-
}, 10_000)
|
|
270
|
-
|
|
271
|
-
return () => {
|
|
272
|
-
controller.abort()
|
|
237
|
+
.catch((err) => set({ error: `拿不到实验室状态:${message(err)}(宿主可能没有 HTTP 面)` }))
|
|
238
|
+
// 每 10 秒对一次状态,两个理由:
|
|
239
|
+
// 1. 占用者标题会**随对话实时变**(官方 session-titles 插件按全部消息重算),而事件流不会告诉我这件事;
|
|
240
|
+
// 2. 流可能安静地挂住(不报错也不送数据)—— 这一拍顺便就把「旧一拍」顶回真相,比单设看门狗简单。
|
|
241
|
+
watchdog = setInterval(refresh, 10_000)
|
|
242
|
+
},
|
|
243
|
+
stop() {
|
|
244
|
+
if (controller) controller.abort()
|
|
245
|
+
controller = null
|
|
273
246
|
if (retry) clearTimeout(retry)
|
|
274
|
-
clearInterval(watchdog)
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
247
|
+
if (watchdog) clearInterval(watchdog)
|
|
248
|
+
retry = null
|
|
249
|
+
watchdog = null
|
|
250
|
+
},
|
|
251
|
+
}
|
|
279
252
|
}
|
|
280
253
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
254
|
+
const labStore = createStore()
|
|
255
|
+
|
|
256
|
+
/** 订阅 store:用一个计数器触发重渲,不引入 useSyncExternalStore 的版本差异。 */
|
|
257
|
+
function useLab() {
|
|
258
|
+
const [, bump] = useState(0)
|
|
259
|
+
useEffect(() => labStore.subscribe(() => bump((n) => n + 1)), [])
|
|
260
|
+
return labStore.get()
|
|
284
261
|
}
|
|
285
262
|
|
|
263
|
+
// ── 6. 原子组件(只吃 props,不认识 ctx / 网络) ──────────────────
|
|
286
264
|
function Btn({ children, onClick, disabled, tone, title }) {
|
|
287
265
|
const off = Boolean(disabled)
|
|
288
266
|
const style = { ...S.btn, ...(tone === 'primary' ? S.btnPrimary : {}), ...(tone === 'danger' ? S.btnDanger : {}), ...(off ? S.btnOff : {}) }
|
|
289
267
|
return h('button', { type: 'button', disabled: off, title: title || children, style, onClick: off ? undefined : onClick }, children)
|
|
290
268
|
}
|
|
291
269
|
|
|
292
|
-
function
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// ── 6. 业务组件(每个只负责一件事) ──────────────────────────────
|
|
297
|
-
function LabModeSwitch({ locked, onChange }) {
|
|
270
|
+
function Chip({ children, onClick, active, disabled, title }) {
|
|
271
|
+
const off = Boolean(disabled)
|
|
298
272
|
return h(
|
|
299
|
-
'
|
|
300
|
-
{
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
),
|
|
307
|
-
h('span', { style: S.meta }, locked ? T.modeHint.lock : T.modeHint.unlock),
|
|
273
|
+
'button',
|
|
274
|
+
{
|
|
275
|
+
type: 'button', disabled: off, title: title || undefined,
|
|
276
|
+
style: { ...S.chip, ...(active ? S.btnPrimary : {}), ...(off ? S.chipOff : {}) },
|
|
277
|
+
onClick: off ? undefined : onClick,
|
|
278
|
+
},
|
|
279
|
+
children,
|
|
308
280
|
)
|
|
309
281
|
}
|
|
310
282
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
{ style: S.row },
|
|
321
|
-
h(
|
|
322
|
-
'button',
|
|
323
|
-
{
|
|
324
|
-
type: 'button',
|
|
325
|
-
disabled: !usable,
|
|
326
|
-
style: { ...S.btn, ...(enabled ? S.btnPrimary : {}), ...(usable ? {} : S.btnOff) },
|
|
327
|
-
title: usable ? '' : T.agent.noBackend,
|
|
328
|
-
onClick: () => usable && onChange(!enabled),
|
|
329
|
-
},
|
|
330
|
-
`${T.agent.label}:${enabled ? T.agent.on : T.agent.off}`,
|
|
331
|
-
),
|
|
332
|
-
h(
|
|
333
|
-
'span',
|
|
334
|
-
{ style: S.meta },
|
|
335
|
-
usable ? (enabled ? T.agent.hintOn : T.agent.hintOff) : T.agent.noBackend,
|
|
336
|
-
),
|
|
337
|
-
)
|
|
283
|
+
const pad2 = (n) => String(n).padStart(2, '0')
|
|
284
|
+
|
|
285
|
+
/** 运行时长:启动至今。单独一个组件,所以每秒重渲的只有这一格。 */
|
|
286
|
+
function Elapsed({ startedAt }) {
|
|
287
|
+
const [, tick] = useState(0)
|
|
288
|
+
useEffect(() => { const id = setInterval(() => tick((n) => n + 1), 1_000); return () => clearInterval(id) }, [])
|
|
289
|
+
const t0 = Date.parse(startedAt || '')
|
|
290
|
+
const seconds = Number.isFinite(t0) ? Math.max(0, Math.floor((Date.now() - t0) / 1_000)) : 0
|
|
291
|
+
return h('span', null, `${pad2(Math.floor(seconds / 60))}:${pad2(seconds % 60)}`)
|
|
338
292
|
}
|
|
339
293
|
|
|
340
|
-
function
|
|
341
|
-
const
|
|
294
|
+
function StatusPill({ status, startedAt, exitAt }) {
|
|
295
|
+
const crashedAgo = () => {
|
|
296
|
+
const t0 = Date.parse(exitAt || '')
|
|
297
|
+
if (!Number.isFinite(t0)) return T.status.crashed
|
|
298
|
+
const minutes = Math.floor((Date.now() - t0) / 60_000)
|
|
299
|
+
return minutes <= 0 ? '刚刚崩溃' : `${minutes} 分钟前崩溃`
|
|
300
|
+
}
|
|
342
301
|
return h(
|
|
343
|
-
'
|
|
344
|
-
{ style: S.
|
|
345
|
-
h(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
h(Btn, { disabled: !lab || !lab.url, onClick: onPopout }, T.actions.popout),
|
|
349
|
-
h(Btn, { disabled: any || !live, onClick: () => onAction('stop') }, T.actions.stop),
|
|
350
|
-
h(Btn, { tone: 'danger', disabled: any || live, onClick: () => onAction('destroy') }, T.actions.destroy),
|
|
302
|
+
'span',
|
|
303
|
+
{ style: S.pill },
|
|
304
|
+
h('span', { style: S.dot(ink(status)) }),
|
|
305
|
+
status === 'crashed' ? crashedAgo() : T.status[status] || status,
|
|
306
|
+
status === 'running' && startedAt ? h(Elapsed, { startedAt }) : null,
|
|
351
307
|
)
|
|
352
308
|
}
|
|
353
309
|
|
|
354
|
-
function
|
|
355
|
-
|
|
310
|
+
function OwnerChip({ owner, ownerTitle, since, sessionId }) {
|
|
311
|
+
if (!owner) return h('span', { style: S.chip, title: T.owner.freeTitle }, T.owner.free)
|
|
312
|
+
const mine = owner === sessionId
|
|
313
|
+
const t0 = Date.parse(since || '')
|
|
314
|
+
const at = Number.isFinite(t0) ? `${pad2(new Date(t0).getHours())}:${pad2(new Date(t0).getMinutes())} 起` : ''
|
|
315
|
+
// 标题优先(与左侧栏同一份,随对话实时变);拿不到才退回短 id,且要跳过 `session-` 前缀
|
|
316
|
+
// —— 真实 id 前 8 位人人相同(2026-11-21 真机上被智能体吐槽才发现)。
|
|
317
|
+
const bare = owner.startsWith('session-') ? owner.slice(8) : owner
|
|
318
|
+
const title = typeof ownerTitle === 'string' ? ownerTitle.trim() : ''
|
|
319
|
+
const who = title === '' ? `${T.owner.other} ${bare.slice(0, 8)}` : title.length > 16 ? `${title.slice(0, 16)}…` : title
|
|
320
|
+
return h('span', { style: { ...S.chip, ...(mine ? S.btnPrimary : {}) }, title: `${mine ? '本会话在驾驶它' : '占用者'} · ${owner}` }, mine ? T.owner.me : `${who}${at ? ` · ${at}` : ''}`)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// ── 7. 三带组件 ────────────────────────────────────────────────
|
|
324
|
+
function Viewport({ frameUrl, status, locked, taken }) {
|
|
356
325
|
return h(
|
|
357
326
|
'div',
|
|
358
|
-
{ style: S.
|
|
327
|
+
{ style: S.viewport },
|
|
359
328
|
frameUrl
|
|
360
329
|
? h('iframe', {
|
|
361
330
|
// key 绑在地址上:地址一变就强制重建 iframe,避免沿用旧文档。
|
|
362
331
|
key: frameUrl,
|
|
363
332
|
src: frameUrl,
|
|
364
333
|
style: { ...S.frame, pointerEvents: locked ? 'none' : 'auto' },
|
|
365
|
-
title: T.
|
|
334
|
+
title: T.panel,
|
|
335
|
+
'aria-hidden': locked ? 'true' : undefined,
|
|
366
336
|
allow: 'clipboard-write',
|
|
367
337
|
})
|
|
368
|
-
: h(
|
|
369
|
-
'div',
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
h('div', null, T.viewport.hint),
|
|
373
|
-
),
|
|
374
|
-
locked && frameUrl ? h('div', { style: S.lockShield }, h('span', { style: S.lockBadge }, T.lockBadge)) : null,
|
|
338
|
+
: h('div', { style: S.idle },
|
|
339
|
+
h('div', null, status === 'starting' ? T.view.starting : status === 'crashed' ? T.view.crashed : T.view.idle),
|
|
340
|
+
h('div', null, T.view.hint)),
|
|
341
|
+
locked && frameUrl ? h('div', { style: S.shield, role: 'note' }, h('span', { style: S.shieldTag }, taken ? T.view.taken : T.lock.hint)) : null,
|
|
375
342
|
)
|
|
376
343
|
}
|
|
377
344
|
|
|
378
|
-
function
|
|
345
|
+
function LogDrawer({ entries }) {
|
|
379
346
|
const ref = useRef(null)
|
|
380
347
|
useEffect(() => {
|
|
381
348
|
const el = ref.current
|
|
382
349
|
if (el) el.scrollTop = el.scrollHeight
|
|
383
350
|
}, [entries.length])
|
|
384
|
-
if (!entries.length) return h('div', { style: { ...S.
|
|
385
|
-
return h(
|
|
386
|
-
'div',
|
|
387
|
-
{ style: S.logBox, ref },
|
|
388
|
-
...entries.slice(-LOG_TAIL).map((entry) =>
|
|
389
|
-
h('div', { key: entry.seq, style: { ...S.logLine, ...(entry.stream === 'err' ? S.logErr : {}) } }, entry.text),
|
|
390
|
-
),
|
|
391
|
-
)
|
|
351
|
+
if (!entries.length) return h('div', { ref, style: { ...S.drawer, color: 'var(--dsw-alias-label-secondary)' } }, T.logs.empty)
|
|
352
|
+
return h('div', { ref, style: S.drawer },
|
|
353
|
+
...entries.slice(-LOG_TAIL).map((entry) => h('div', { key: entry.seq, style: { ...S.line, ...(entry.stream === 'err' ? S.lineErr : {}) } }, entry.text)))
|
|
392
354
|
}
|
|
393
355
|
|
|
394
|
-
function
|
|
395
|
-
return h(
|
|
396
|
-
'div',
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
'div',
|
|
400
|
-
{
|
|
401
|
-
h('div', { style: { fontWeight: 600, color: 'var(--dsw-alias-label-primary)' } }, T.emptyTitle),
|
|
402
|
-
h('div', null, T.emptyDesc),
|
|
403
|
-
h(Btn, { tone: 'primary', onClick: onCreate, disabled: busy !== '' }, busy === 'create' ? T.creating : T.create),
|
|
404
|
-
),
|
|
405
|
-
)
|
|
356
|
+
function EmptyCard({ busy, onCreate }) {
|
|
357
|
+
return h('div', { style: S.viewport },
|
|
358
|
+
h('div', { style: S.idle },
|
|
359
|
+
h(LabIcon, { size: 36 }),
|
|
360
|
+
h('div', { style: { fontWeight: 600, color: 'var(--dsw-alias-label-primary)' } }, T.empty.title),
|
|
361
|
+
h('div', null, T.empty.desc),
|
|
362
|
+
h(Btn, { tone: 'primary', onClick: onCreate, disabled: busy !== '' }, busy === 'create' ? T.empty.busy : T.empty.action)))
|
|
406
363
|
}
|
|
407
364
|
|
|
408
|
-
// ──
|
|
409
|
-
function LabPanel() {
|
|
410
|
-
const
|
|
411
|
-
const
|
|
365
|
+
// ── 8. 容器:只做接线(状态 → 组件),不写样式细节 ─────────────
|
|
366
|
+
function LabPanel(props) {
|
|
367
|
+
const sessionId = props && props.sessionId ? String(props.sessionId) : ''
|
|
368
|
+
const view = useLab()
|
|
369
|
+
// 占用者标题从**客户端会话列表**拿(`useSessions` 是槽位标准 prop,左侧栏渲染的就是这份):
|
|
370
|
+
// 它覆盖全部会话、随改题实时变;宿主的 `sessions.get(id)` 只认正在跑的会话。拿不到再用 /state 的兜底。
|
|
371
|
+
const byId = props && typeof props.useSessions === 'function' ? props.useSessions((state) => state.byId) : null
|
|
372
|
+
// 默认**不锁**:这个面板的全部意义就是让你当手边这台机器用,锁着等于每次先点两下。
|
|
373
|
+
// 锁是给「我只想看着、别手滑点到」的那种时刻准备的,chip 一键就能切。
|
|
374
|
+
const [locked, setLocked] = useState(false)
|
|
412
375
|
const [busy, setBusy] = useState('')
|
|
413
376
|
const [nonce, setNonce] = useState(0)
|
|
377
|
+
const [menu, setMenu] = useState('')
|
|
378
|
+
const [versions, setVersions] = useState(null)
|
|
379
|
+
const [confirming, setConfirming] = useState(false)
|
|
380
|
+
const [drawer, setDrawer] = useState(false)
|
|
414
381
|
|
|
415
|
-
const lab = labs.length > 0 ? labs[0] : null
|
|
416
|
-
const entries = lab ? logs[lab.id] || [] : []
|
|
382
|
+
const lab = view.labs.length > 0 ? view.labs[0] : null
|
|
383
|
+
const entries = lab ? view.logs[lab.id] || [] : []
|
|
417
384
|
const status = lab ? lab.status : 'created'
|
|
418
385
|
const live = status === 'running' || status === 'starting'
|
|
419
|
-
//
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
|
|
386
|
+
// 没有 lab、没有会话 id(拿不到身份)、或主人就是自己 → 都算「我能动」。
|
|
387
|
+
const mine = !lab || !lab.ownerSessionId || sessionId === '' || lab.ownerSessionId === sessionId
|
|
388
|
+
const canView = Boolean(lab && lab.url)
|
|
389
|
+
// iframe 指向宿主的稳定入口而不是实验室裸地址:它每次重启都换端口与 token,
|
|
390
|
+
// 直连会让面板卡在 `dsh web authentication required`(2026-11-21 撞上过一次)。
|
|
391
|
+
// 没在跑时**不挂 iframe**:入口会回一张 503 的 HTML,那张页有自己的居中文字,
|
|
392
|
+
// 会把面板自己的占位说明顶掉(截图里一眼能看出是两张页叠在一起)。
|
|
393
|
+
const frameUrl = canView && view.frameSig ? `${API}/frame?id=${encodeURIComponent(lab.id)}&sig=${view.frameSig}&n=${nonce + view.frameGen}` : null
|
|
394
|
+
|
|
395
|
+
const run = async (label, fn) => {
|
|
396
|
+
setBusy(label)
|
|
397
|
+
labStore.set({ error: '' })
|
|
398
|
+
try {
|
|
399
|
+
await fn()
|
|
400
|
+
} catch (err) {
|
|
401
|
+
labStore.set({ error: `${label}失败:${message(err)}` })
|
|
402
|
+
} finally {
|
|
403
|
+
setBusy('')
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const post = (path, body) => request(path, { method: 'POST', body: JSON.stringify(body) })
|
|
407
|
+
const drive = (action, extra) => run(action, () => post(`/labs/${lab.id}`, { action, sessionId, ...extra }))
|
|
408
|
+
const takeover = () => run('接手', () => post(`/labs/${lab.id}/claim`, { sessionId, force: true }))
|
|
409
|
+
const create = () => run('create', () => post('/labs', { sessionId }))
|
|
410
|
+
const toggleAgent = (next) => run('授权', async () => {
|
|
411
|
+
labStore.set({ uiMode: next })
|
|
412
|
+
await post('/ui-mode', { enabled: next })
|
|
413
|
+
})
|
|
414
|
+
const setVersion = (version) => run('版本', () => post(`/labs/${lab.id}/version`, { version, sessionId }))
|
|
415
|
+
const openVersions = () => {
|
|
416
|
+
setMenu('version')
|
|
417
|
+
if (!versions) request('/versions').then(setVersions).catch(() => setVersions({ cached: [] }))
|
|
418
|
+
}
|
|
438
419
|
|
|
439
|
-
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
const
|
|
420
|
+
// 面板后开 / 日志抽屉打开:补一次历史(事件流只推「之后」的行)。
|
|
421
|
+
const labId = lab ? lab.id : ''
|
|
422
|
+
useEffect(() => {
|
|
423
|
+
void labStore.seedLogs(labId)
|
|
424
|
+
}, [labId, drawer])
|
|
425
|
+
|
|
426
|
+
const primary = !mine
|
|
427
|
+
? { label: T.act.takeover, tone: 'primary', run: takeover }
|
|
428
|
+
: live
|
|
429
|
+
? { label: T.act.stop, run: () => drive('stop') }
|
|
430
|
+
: { label: T.act.start, tone: 'primary', run: () => drive('start') }
|
|
431
|
+
|
|
432
|
+
let menuBody = null
|
|
433
|
+
if (lab && confirming) {
|
|
434
|
+
menuBody = [
|
|
435
|
+
h('span', { key: 'w', style: S.note }, T.destroyWarn),
|
|
436
|
+
h(Btn, { key: 'y', tone: 'danger', onClick: () => { setConfirming(false); setMenu(''); void drive('destroy') } }, T.act.confirm),
|
|
437
|
+
h(Btn, { key: 'n', onClick: () => setConfirming(false) }, T.act.cancel),
|
|
438
|
+
]
|
|
439
|
+
} else if (lab && menu === 'more') {
|
|
440
|
+
menuBody = [
|
|
441
|
+
h(Btn, { key: 'r', disabled: !mine || !live || busy !== '', onClick: () => void drive('restart') }, T.act.restart),
|
|
442
|
+
h(Btn, { key: 'f', disabled: !canView, onClick: () => setNonce((n) => n + 1) }, T.act.refresh),
|
|
443
|
+
h(Btn, { key: 'p', disabled: !canView, onClick: () => window.open(lab.url, '_blank', 'noopener') }, T.act.popout),
|
|
444
|
+
h(Btn, { key: 'v', disabled: !mine, onClick: openVersions }, T.act.version),
|
|
445
|
+
h(Btn, { key: 'a', onClick: () => setMenu('about') }, T.act.about),
|
|
446
|
+
h(Btn, { key: 'd', tone: 'danger', disabled: !mine || busy !== '', onClick: () => setConfirming(true) }, T.act.destroy),
|
|
447
|
+
]
|
|
448
|
+
} else if (lab && menu === 'version') {
|
|
449
|
+
menuBody = [
|
|
450
|
+
h(Btn, { key: 'h', tone: lab.version === 'host' ? 'primary' : undefined, disabled: !mine, onClick: () => setVersion('host') }, T.act.hostVersion),
|
|
451
|
+
...(versions
|
|
452
|
+
? versions.cached.map((item) => h(Btn, { key: item.version, tone: item.version === lab.version ? 'primary' : undefined, disabled: !mine, onClick: () => setVersion(item.version) }, item.version))
|
|
453
|
+
: [h('span', { key: 'loading', style: S.note }, T.loadingVersions)]),
|
|
454
|
+
h(Btn, { key: 'b', onClick: () => setMenu('more') }, T.act.back),
|
|
455
|
+
]
|
|
456
|
+
} else if (menu === 'about') {
|
|
457
|
+
menuBody = [h('span', { key: 't', style: S.note }, T.about), h(Btn, { key: 'o', onClick: () => setMenu('') }, T.act.ok)]
|
|
458
|
+
}
|
|
446
459
|
|
|
447
|
-
return h(
|
|
448
|
-
'div',
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
{ style: S.
|
|
453
|
-
h(
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
h('
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
onChange: toggleAgent,
|
|
479
|
-
}),
|
|
480
|
-
h(LabViewport, { lab, locked, frameUrl }),
|
|
481
|
-
h(MetaRow, { left: `${T.logs.title}(${entries.length})` }),
|
|
482
|
-
h(LabLogPanel, { entries }),
|
|
483
|
-
),
|
|
484
|
-
h('div', { style: S.hint }, T.hint),
|
|
485
|
-
)
|
|
460
|
+
return h('div', { style: S.panel },
|
|
461
|
+
h('div', { style: S.head },
|
|
462
|
+
h(LabIcon, { size: 16 }),
|
|
463
|
+
h('span', { style: S.title }, T.panel),
|
|
464
|
+
lab ? h(StatusPill, { status, startedAt: lab.startedAt, exitAt: lab.exit && lab.exit.at }) : null,
|
|
465
|
+
h('span', { style: S.grow }),
|
|
466
|
+
lab ? h(OwnerChip, { owner: lab.ownerSessionId, ownerTitle: (byId && byId[lab.ownerSessionId] && byId[lab.ownerSessionId].title) || lab.ownerTitle, since: lab.ownerSince, sessionId }) : null,
|
|
467
|
+
h('span', { style: S.dot(view.connected ? 'var(--dsw-alias-state-success-primary)' : 'var(--dsw-alias-state-idle-primary)'), title: view.connected ? T.conn.on : T.conn.off })),
|
|
468
|
+
lab
|
|
469
|
+
? h('div', { style: S.band },
|
|
470
|
+
h(Btn, { tone: primary.tone, disabled: busy !== '' || (!mine && false), onClick: primary.run, title: primary.label }, primary.label),
|
|
471
|
+
h(Btn, { disabled: menu === 'more' || confirming, onClick: () => setMenu(menu === 'more' ? '' : 'more'), title: T.act.more }, '⋯'),
|
|
472
|
+
h('span', { style: S.grow }),
|
|
473
|
+
h('span', { style: S.meta }, lab.port ? `:${lab.port}` : lab.version === 'host' ? T.act.hostVersion : lab.version))
|
|
474
|
+
: null,
|
|
475
|
+
menuBody ? h('div', { style: S.band }, ...menuBody) : null,
|
|
476
|
+
view.error ? h('div', { style: S.error }, view.error) : null,
|
|
477
|
+
h('div', { style: S.body }, lab
|
|
478
|
+
? h(Viewport, { frameUrl, status, locked: !mine || locked, taken: !mine })
|
|
479
|
+
: h(EmptyCard, { busy, onCreate: create })),
|
|
480
|
+
drawer && lab ? h(LogDrawer, { entries }) : null,
|
|
481
|
+
h('div', { style: S.foot },
|
|
482
|
+
lab ? h(Chip, { active: locked, disabled: !mine, title: T.lock.hint, onClick: () => setLocked((v) => !v) }, locked ? T.lock.on : T.lock.off) : null,
|
|
483
|
+
h(Chip, {
|
|
484
|
+
active: view.uiMode,
|
|
485
|
+
disabled: !view.ui || view.ui.available === false,
|
|
486
|
+
title: !view.ui || view.ui.available === false ? T.agent.noBackend : view.uiMode ? T.agent.hintOn : T.agent.hintOff,
|
|
487
|
+
onClick: () => void toggleAgent(!view.uiMode),
|
|
488
|
+
}, view.uiMode ? T.agent.on : T.agent.off),
|
|
489
|
+
h('span', { style: S.grow }),
|
|
490
|
+
lab ? h(Chip, { active: drawer, onClick: () => setDrawer((v) => !v) }, `${T.logs.title} · ${entries.length}`) : null))
|
|
486
491
|
}
|
|
487
492
|
|
|
488
493
|
/** tab 标题:文字 + 状态点。 */
|
|
489
494
|
function LabTabTitle() {
|
|
490
|
-
return h(
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
h('span', { style: S.dot('var(--dsw-alias-state-idle-primary)') }),
|
|
494
|
-
T.tab,
|
|
495
|
-
)
|
|
495
|
+
return h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: 6 } },
|
|
496
|
+
h(LabIcon, { size: 14 }),
|
|
497
|
+
T.tab)
|
|
496
498
|
}
|
|
497
499
|
|
|
498
|
-
// ──
|
|
500
|
+
// ── 9. 注册(只接线:拿服务、注册槽位) ────────────────────────
|
|
501
|
+
/** 同一个会话只自动展开一次:智能体连续调用时不反复抢焦点。 */
|
|
502
|
+
const autoOpened = new Set()
|
|
503
|
+
|
|
499
504
|
function service(ctx, key) {
|
|
500
505
|
try {
|
|
501
506
|
return typeof ctx?.get === 'function' ? ctx.get(key) : null
|
|
@@ -522,6 +527,13 @@ window.__ModuleLoader__.load({
|
|
|
522
527
|
}
|
|
523
528
|
}
|
|
524
529
|
|
|
530
|
+
function autoOpen(ctx, sessionId) {
|
|
531
|
+
const key = sessionId || 'any'
|
|
532
|
+
if (autoOpened.has(key)) return
|
|
533
|
+
autoOpened.add(key)
|
|
534
|
+
openLab(ctx)
|
|
535
|
+
}
|
|
536
|
+
|
|
525
537
|
function collector() {
|
|
526
538
|
const list = []
|
|
527
539
|
return {
|
|
@@ -554,7 +566,8 @@ window.__ModuleLoader__.load({
|
|
|
554
566
|
kind: TAB_KIND,
|
|
555
567
|
priority: 'extension',
|
|
556
568
|
title: () => T.tab,
|
|
557
|
-
|
|
569
|
+
// 开始页那张卡片的图标:内联自 icon.svg,不用 emoji。
|
|
570
|
+
guide: [{ id: TAB_ID, order: GUIDE_ORDER, title: () => T.guide.title, description: () => T.guide.desc, icon: LabIcon }],
|
|
558
571
|
}),
|
|
559
572
|
)
|
|
560
573
|
inner.add(
|
|
@@ -576,30 +589,6 @@ window.__ModuleLoader__.load({
|
|
|
576
589
|
} catch (e) {
|
|
577
590
|
console.warn('[dsh-rig] inject(sidebarRightTabs) 不可用', e)
|
|
578
591
|
}
|
|
579
|
-
|
|
580
|
-
try {
|
|
581
|
-
const slots = service(ctx, 'slots')
|
|
582
|
-
if (slots && typeof slots.inject === 'function') {
|
|
583
|
-
disposers.add(
|
|
584
|
-
slots.inject('conversation.session.header.actions', () =>
|
|
585
|
-
slots.register(
|
|
586
|
-
{ name: 'conversation.session.header.actions', id: TAB_ID, order: HEADER_ACTION_ORDER, label: T.headerAction },
|
|
587
|
-
function HeaderButton() {
|
|
588
|
-
return h(
|
|
589
|
-
'button',
|
|
590
|
-
{ type: 'button', style: S.headerButton, title: T.headerAction, onClick: () => openLab(ctx) },
|
|
591
|
-
h('span', { 'aria-hidden': true }, '🧪'),
|
|
592
|
-
h('span', null, T.headerAction),
|
|
593
|
-
)
|
|
594
|
-
},
|
|
595
|
-
),
|
|
596
|
-
),
|
|
597
|
-
)
|
|
598
|
-
}
|
|
599
|
-
} catch (e) {
|
|
600
|
-
console.warn('[dsh-rig] 头部入口注册失败', e)
|
|
601
|
-
}
|
|
602
|
-
|
|
603
592
|
return () => disposers.disposeAll()
|
|
604
593
|
}
|
|
605
594
|
|
|
@@ -607,8 +596,16 @@ window.__ModuleLoader__.load({
|
|
|
607
596
|
inject: ['slots'],
|
|
608
597
|
apply(ctx) {
|
|
609
598
|
try {
|
|
599
|
+
// 页级连接先起来:面板没打开时也要能收到 `open-lab`,否则「自动展开」无从触发。
|
|
600
|
+
labStore.configure({ onOpenLab: (sessionId) => autoOpen(ctx, sessionId) })
|
|
601
|
+
labStore.start()
|
|
610
602
|
const dispose = registerInto(ctx)
|
|
611
|
-
if (typeof ctx.effect === 'function')
|
|
603
|
+
if (typeof ctx.effect === 'function') {
|
|
604
|
+
ctx.effect(() => () => {
|
|
605
|
+
dispose()
|
|
606
|
+
labStore.stop()
|
|
607
|
+
}, 'dsh-rig registration')
|
|
608
|
+
}
|
|
612
609
|
} catch (e) {
|
|
613
610
|
console.warn('[dsh-rig] apply 失败', e)
|
|
614
611
|
}
|