@xiangyixie/dsh-superagent 0.14.2 → 0.14.3

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.
@@ -0,0 +1,1110 @@
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>任务看板 · dsh-superagent</title>
7
+ <style>
8
+ :root {
9
+ color-scheme: light;
10
+ --bg: #f6f7f9;
11
+ --panel: #ffffff;
12
+ --panel-2: #fbfbfc;
13
+ --ink: #1b1f24;
14
+ --muted: #626b76;
15
+ --faint: #8a939e;
16
+ --line: #e4e7eb;
17
+ --line-strong: #d3d8de;
18
+ --accent: #2f6feb;
19
+ --accent-soft: #eaf1ff;
20
+ --ok: #1f8a5b;
21
+ --ok-soft: #e8f6ef;
22
+ --warn: #a8690b;
23
+ --warn-soft: #fdf3e2;
24
+ --bad: #c0362c;
25
+ --bad-soft: #fdeceb;
26
+ --idle: #6b7480;
27
+ --idle-soft: #eff1f4;
28
+ --radius: 14px;
29
+ --radius-sm: 10px;
30
+ --shadow: 0 1px 2px rgba(20, 26, 33, .05), 0 8px 24px rgba(20, 26, 33, .05);
31
+ --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
32
+ }
33
+ * { box-sizing: border-box; }
34
+ html, body { height: 100%; }
35
+ body {
36
+ margin: 0;
37
+ background: var(--bg);
38
+ color: var(--ink);
39
+ font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
40
+ -webkit-font-smoothing: antialiased;
41
+ }
42
+ button, input, textarea, select { font: inherit; color: inherit; }
43
+ button { cursor: pointer; }
44
+ a { color: var(--accent); }
45
+
46
+ .shell { max-width: 1280px; margin: 0 auto; padding: 20px 24px 56px; }
47
+ header.top {
48
+ display: flex; align-items: center; gap: 14px;
49
+ padding: 10px 2px 18px;
50
+ }
51
+ .brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
52
+ .logo {
53
+ width: 30px; height: 30px; border-radius: 9px; flex: none;
54
+ background: linear-gradient(145deg, #2f6feb, #7aa2f7);
55
+ box-shadow: inset 0 0 0 1px rgba(255,255,255,.35);
56
+ position: relative;
57
+ }
58
+ .logo::after {
59
+ content: ""; position: absolute; inset: 8px; border-radius: 4px;
60
+ border: 2px solid rgba(255,255,255,.9); border-right-color: transparent; border-bottom-color: transparent;
61
+ transform: rotate(45deg);
62
+ }
63
+ .brand h1 { font-size: 17px; margin: 0; letter-spacing: .2px; white-space: nowrap; }
64
+ .brand p { margin: 0; color: var(--muted); font-size: 12px; white-space: nowrap; }
65
+ .top-spacer { flex: 1; }
66
+ .conn {
67
+ display: inline-flex; align-items: center; gap: 7px;
68
+ padding: 6px 11px; border-radius: 999px; background: var(--panel);
69
+ border: 1px solid var(--line); color: var(--muted); font-size: 12px;
70
+ }
71
+ .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--idle); }
72
+ .conn.live .dot { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
73
+ .conn.down .dot { background: var(--bad); box-shadow: 0 0 0 3px var(--bad-soft); }
74
+ .conn.connecting .dot { background: var(--warn); animation: pulse 1s infinite; }
75
+ @keyframes pulse { 50% { opacity: .35; } }
76
+
77
+ nav.tabs {
78
+ display: inline-flex; gap: 3px; padding: 4px; border: 1px solid var(--line);
79
+ background: var(--panel); border-radius: 12px; box-shadow: var(--shadow);
80
+ }
81
+ nav.tabs button {
82
+ border: 0; background: transparent; padding: 8px 16px; border-radius: 9px;
83
+ color: var(--muted); font-weight: 500; display: inline-flex; align-items: center; gap: 8px;
84
+ }
85
+ nav.tabs button:hover { background: var(--panel-2); color: var(--ink); }
86
+ nav.tabs button[aria-selected="true"] { background: var(--ink); color: #fff; }
87
+ nav.tabs button[aria-selected="true"] .badge { background: #fff; color: var(--ink); }
88
+ .badge {
89
+ min-width: 19px; height: 19px; padding: 0 6px; border-radius: 999px;
90
+ background: var(--accent-soft); color: var(--accent);
91
+ font-size: 11px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center;
92
+ }
93
+ .badge.hot { background: var(--warn-soft); color: var(--warn); }
94
+
95
+ main { margin-top: 18px; }
96
+ .view[hidden] { display: none !important; }
97
+
98
+ .board-grid { display: grid; grid-template-columns: 310px minmax(0, 1fr); gap: 16px; align-items: start; }
99
+ @media (max-width: 940px) { .board-grid { grid-template-columns: 1fr; } }
100
+
101
+ .panel {
102
+ background: var(--panel); border: 1px solid var(--line);
103
+ border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
104
+ }
105
+ .panel-head {
106
+ display: flex; align-items: center; gap: 10px;
107
+ padding: 14px 16px; border-bottom: 1px solid var(--line);
108
+ }
109
+ .panel-head h2 { margin: 0; font-size: 13px; font-weight: 600; letter-spacing: .3px; color: var(--muted); text-transform: none; }
110
+ .panel-head .hint { margin-left: auto; color: var(--faint); font-size: 12px; }
111
+
112
+ .run-list { max-height: calc(100vh - 230px); overflow: auto; }
113
+ .run-item {
114
+ display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line);
115
+ background: transparent; padding: 13px 16px; transition: background .12s ease;
116
+ }
117
+ .run-item:last-child { border-bottom: 0; }
118
+ .run-item:hover { background: var(--panel-2); }
119
+ .run-item[aria-current="true"] { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
120
+ .run-item .goal {
121
+ font-weight: 600; font-size: 13.5px; line-height: 1.4; margin: 0 0 7px;
122
+ display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
123
+ }
124
+ .run-meta { display: flex; flex-wrap: wrap; gap: 6px 10px; color: var(--faint); font-size: 11.5px; align-items: center; }
125
+
126
+ .pill {
127
+ display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px;
128
+ border-radius: 999px; font-size: 11.5px; font-weight: 600; white-space: nowrap;
129
+ background: var(--idle-soft); color: var(--idle);
130
+ }
131
+ .pill.ok { background: var(--ok-soft); color: var(--ok); }
132
+ .pill.run { background: var(--accent-soft); color: var(--accent); }
133
+ .pill.warn { background: var(--warn-soft); color: var(--warn); }
134
+ .pill.bad { background: var(--bad-soft); color: var(--bad); }
135
+ .pill.idle { background: var(--idle-soft); color: var(--idle); }
136
+ .pill .mini-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
137
+ .pill.run .mini-dot { animation: pulse 1.2s infinite; }
138
+
139
+ .detail { min-height: 320px; }
140
+ .detail-head { padding: 18px 20px 16px; border-bottom: 1px solid var(--line); }
141
+ .detail-head .goal { margin: 0 0 10px; font-size: 16px; line-height: 1.45; word-break: break-word; }
142
+ .detail-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
143
+ .stat-row { display: flex; flex-wrap: wrap; gap: 8px 22px; margin-top: 12px; color: var(--muted); font-size: 12.5px; }
144
+ .stat-row b { color: var(--ink); font-weight: 600; }
145
+ .stat-row .mono { font-family: var(--mono); font-size: 12px; }
146
+
147
+ .cards { display: flex; flex-direction: column; gap: 10px; padding: 14px; }
148
+ .card {
149
+ border: 1px solid var(--line); border-radius: 12px; background: var(--panel);
150
+ padding: 14px 15px; transition: border-color .12s ease;
151
+ }
152
+ .card:hover { border-color: var(--line-strong); }
153
+ .card.running { border-color: #b9cdf5; box-shadow: 0 0 0 3px #eef4ff; }
154
+ .card.failed, .card.unverified { border-color: #edc9c5; }
155
+ .card-top { display: flex; align-items: flex-start; gap: 12px; }
156
+ .card-index {
157
+ flex: none; width: 26px; height: 26px; border-radius: 8px; background: var(--panel-2);
158
+ border: 1px solid var(--line); color: var(--muted);
159
+ font: 600 12px/24px var(--mono); text-align: center;
160
+ }
161
+ .card-title { flex: 1; min-width: 0; }
162
+ .card-title h3 { margin: 0; font-size: 14px; line-height: 1.45; font-weight: 600; word-break: break-word; }
163
+ .card-sub { display: flex; flex-wrap: wrap; gap: 5px 14px; margin-top: 6px; color: var(--faint); font-size: 12px; }
164
+ .card-sub span { display: inline-flex; gap: 5px; align-items: center; }
165
+ .card-sub .mono { font-family: var(--mono); }
166
+ .card-actions { display: flex; gap: 6px; flex: none; }
167
+
168
+ .btn {
169
+ border: 1px solid var(--line-strong); background: var(--panel); color: var(--ink);
170
+ border-radius: 9px; padding: 6px 12px; font-size: 12.5px; font-weight: 500;
171
+ transition: background .12s ease, border-color .12s ease, transform .06s ease;
172
+ }
173
+ .btn:hover { background: var(--panel-2); border-color: #b9c0c8; }
174
+ .btn:active { transform: translateY(1px); }
175
+ .btn:disabled { opacity: .45; cursor: not-allowed; }
176
+ .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
177
+ .btn.primary:hover { background: #245fd0; }
178
+ .btn.danger { color: var(--bad); border-color: #e7c2be; background: #fffafa; }
179
+ .btn.danger:hover { background: var(--bad-soft); }
180
+ .btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
181
+ .btn.ghost:hover { background: var(--panel-2); color: var(--ink); }
182
+
183
+ .explain {
184
+ margin-top: 12px; padding: 12px 13px; border-radius: 10px;
185
+ background: var(--warn-soft); border: 1px solid #f0dcb8;
186
+ }
187
+ .explain.bad { background: var(--bad-soft); border-color: #efcbc7; }
188
+ .explain.ok { background: var(--ok-soft); border-color: #c6e7d5; }
189
+ .explain h4 { margin: 0 0 4px; font-size: 13px; }
190
+ .explain p { margin: 0; color: #57483a; font-size: 12.5px; }
191
+ .explain.bad p { color: #6b3b37; }
192
+ .explain .why { margin-top: 4px; color: var(--muted); }
193
+ .explain .acts { display: flex; gap: 8px; margin-top: 9px; flex-wrap: wrap; }
194
+ details.raw { margin-top: 9px; }
195
+ details.raw summary { cursor: pointer; color: var(--faint); font-size: 11.5px; }
196
+ details.raw pre {
197
+ margin: 7px 0 0; padding: 10px; border-radius: 8px; background: #1e2228; color: #dfe4ea;
198
+ font: 11.5px/1.5 var(--mono); white-space: pre-wrap; word-break: break-word; max-height: 260px; overflow: auto;
199
+ }
200
+ .empty {
201
+ padding: 46px 20px; text-align: center; color: var(--muted);
202
+ }
203
+ .empty strong { display: block; color: var(--ink); font-size: 14px; margin-bottom: 4px; }
204
+ .empty .emoji { font-size: 22px; margin-bottom: 8px; }
205
+
206
+ .inbox-list { display: flex; flex-direction: column; gap: 12px; }
207
+ .inbox-card {
208
+ background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
209
+ box-shadow: var(--shadow); padding: 18px 20px;
210
+ }
211
+ .inbox-card header { display: flex; gap: 10px; align-items: flex-start; }
212
+ .inbox-card h3 { margin: 0; font-size: 15px; line-height: 1.45; flex: 1; }
213
+ .inbox-card .why { margin: 10px 0 0; color: var(--muted); font-size: 13px; }
214
+ .inbox-card .impact {
215
+ margin-top: 10px; padding: 9px 11px; border-radius: 9px; background: var(--panel-2);
216
+ border: 1px dashed var(--line-strong); color: var(--muted); font-size: 12.5px;
217
+ }
218
+ .answer-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 13px; }
219
+ .choice {
220
+ border: 1px solid var(--line-strong); background: var(--panel); border-radius: 9px;
221
+ padding: 7px 13px; font-size: 13px;
222
+ }
223
+ .choice:hover { border-color: var(--accent); color: var(--accent); }
224
+ .choice[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
225
+ .answer-box { margin-top: 12px; display: flex; gap: 8px; align-items: flex-end; }
226
+ .answer-box textarea {
227
+ flex: 1; min-height: 42px; max-height: 160px; resize: vertical; padding: 9px 11px;
228
+ border: 1px solid var(--line-strong); border-radius: 9px; background: var(--panel-2);
229
+ }
230
+ .answer-box textarea:focus { outline: 2px solid #c8d9fb; border-color: var(--accent); background: #fff; }
231
+
232
+ .tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; }
233
+ .tpl-card {
234
+ background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
235
+ box-shadow: var(--shadow); padding: 18px; display: flex; flex-direction: column; gap: 10px;
236
+ cursor: pointer; transition: border-color .12s ease, box-shadow .12s ease;
237
+ text-align: left;
238
+ }
239
+ .tpl-card:hover { border-color: #b9cdf5; box-shadow: 0 2px 4px rgba(20,26,33,.05), 0 14px 32px rgba(47,111,235,.10); }
240
+ .tpl-card[aria-current="true"] { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
241
+ .tpl-card h3 { margin: 0; font-size: 15px; line-height: 1.4; }
242
+ .tpl-card p { margin: 0; color: var(--muted); font-size: 12.5px; }
243
+ .tpl-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; }
244
+ .tag { padding: 3px 8px; border-radius: 7px; background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); font-size: 11.5px; }
245
+
246
+ .tpl-form { margin-top: 16px; }
247
+ .field { margin-top: 13px; }
248
+ .field label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 5px; }
249
+ .field .desc { color: var(--faint); font-size: 12px; margin: 0 0 6px; }
250
+ .field input, .field textarea {
251
+ width: 100%; padding: 9px 11px; border: 1px solid var(--line-strong);
252
+ border-radius: 9px; background: var(--panel-2);
253
+ }
254
+ .field textarea { min-height: 74px; resize: vertical; }
255
+ .field input:focus, .field textarea:focus { outline: 2px solid #c8d9fb; border-color: var(--accent); background: #fff; }
256
+ .form-foot { display: flex; gap: 10px; align-items: center; margin-top: 16px; }
257
+ .form-foot .note { color: var(--faint); font-size: 12px; }
258
+
259
+ .modal-backdrop {
260
+ position: fixed; inset: 0; background: rgba(20, 26, 33, .38); backdrop-filter: blur(2px);
261
+ display: flex; align-items: center; justify-content: center; padding: 24px; z-index: 40;
262
+ }
263
+ .modal-backdrop[hidden] { display: none; }
264
+ .modal {
265
+ width: min(860px, 100%); max-height: 86vh; display: flex; flex-direction: column;
266
+ background: var(--panel); border-radius: 16px; box-shadow: 0 24px 64px rgba(10, 15, 22, .28);
267
+ overflow: hidden;
268
+ }
269
+ .modal header { display: flex; align-items: center; gap: 12px; padding: 15px 18px; border-bottom: 1px solid var(--line); }
270
+ .modal header h3 { margin: 0; font-size: 14px; flex: 1; }
271
+ .modal .body { padding: 16px 18px 20px; overflow: auto; }
272
+ .modal .body h4 { margin: 16px 0 6px; font-size: 13px; color: var(--muted); }
273
+ .modal .body h4:first-child { margin-top: 0; }
274
+ .modal pre {
275
+ margin: 0; padding: 12px; border-radius: 10px; background: #f2f4f7; border: 1px solid var(--line);
276
+ font: 12px/1.55 var(--mono); white-space: pre-wrap; word-break: break-word; max-height: 320px; overflow: auto;
277
+ }
278
+ .audit-row { display: flex; gap: 10px; padding: 7px 0; border-bottom: 1px dashed var(--line); font-size: 12px; }
279
+ .audit-row:last-child { border-bottom: 0; }
280
+ .audit-row time { flex: none; color: var(--faint); font-family: var(--mono); font-size: 11px; width: 138px; }
281
+ .audit-row .what { flex: 1; color: var(--muted); word-break: break-word; }
282
+ .audit-row .what b { color: var(--ink); font-weight: 600; }
283
+
284
+ .toast {
285
+ position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%) translateY(10px);
286
+ background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px;
287
+ font-size: 13px; box-shadow: 0 12px 32px rgba(10,15,22,.24); opacity: 0;
288
+ pointer-events: none; transition: opacity .18s ease, transform .18s ease; z-index: 60;
289
+ max-width: 80vw;
290
+ }
291
+ .toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
292
+ .toast.bad { background: var(--bad); }
293
+
294
+ .gate { min-height: 70vh; display: flex; align-items: center; justify-content: center; }
295
+ .gate .panel { width: min(520px, 100%); padding: 28px; }
296
+ .gate h2 { margin: 0 0 6px; font-size: 18px; }
297
+ .gate p { color: var(--muted); font-size: 13px; margin: 0 0 16px; }
298
+ .gate input {
299
+ width: 100%; padding: 10px 12px; border: 1px solid var(--line-strong); border-radius: 9px;
300
+ background: var(--panel-2); font-family: var(--mono); font-size: 12.5px;
301
+ }
302
+ .gate .row { display: flex; gap: 8px; margin-top: 12px; }
303
+ .gate .err { color: var(--bad); font-size: 12.5px; margin-top: 10px; min-height: 18px; }
304
+ .sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }
305
+ </style>
306
+ </head>
307
+ <body>
308
+ <div class="shell" id="shell">
309
+ <header class="top">
310
+ <div class="brand">
311
+ <div class="logo" aria-hidden="true"></div>
312
+ <div>
313
+ <h1>任务看板</h1>
314
+ <p>dsh-superagent · 谁在做什么,什么在等你</p>
315
+ </div>
316
+ </div>
317
+ <div class="top-spacer"></div>
318
+ <div class="conn connecting" id="conn" role="status" aria-live="polite">
319
+ <span class="dot"></span><span id="conn-text">正在连接</span>
320
+ </div>
321
+ </header>
322
+
323
+ <nav class="tabs" role="tablist" aria-label="页面">
324
+ <button role="tab" id="tab-board" aria-controls="view-board" aria-selected="true" data-tab="board">看板</button>
325
+ <button role="tab" id="tab-inbox" aria-controls="view-inbox" aria-selected="false" data-tab="inbox">
326
+ 待办 <span class="badge" id="inbox-count" hidden>0</span>
327
+ </button>
328
+ <button role="tab" id="tab-templates" aria-controls="view-templates" aria-selected="false" data-tab="templates">模板</button>
329
+ </nav>
330
+
331
+ <main>
332
+ <section class="view" id="view-board" role="tabpanel" aria-labelledby="tab-board">
333
+ <div class="board-grid">
334
+ <aside class="panel">
335
+ <div class="panel-head"><h2>最近的运行</h2><span class="hint" id="run-count"></span></div>
336
+ <div class="run-list" id="run-list"></div>
337
+ </aside>
338
+ <section class="panel detail" id="detail">
339
+ <div class="empty"><strong>正在读取运行</strong>看板会自己刷新,不需要手动点。</div>
340
+ </section>
341
+ </div>
342
+ </section>
343
+
344
+ <section class="view" id="view-inbox" role="tabpanel" aria-labelledby="tab-inbox" hidden>
345
+ <div id="inbox"></div>
346
+ </section>
347
+
348
+ <section class="view" id="view-templates" role="tabpanel" aria-labelledby="tab-templates" hidden>
349
+ <div class="tpl-grid" id="tpl-grid"></div>
350
+ <section class="panel tpl-form" id="tpl-form-panel" hidden>
351
+ <div class="panel-head">
352
+ <h2 id="tpl-form-title">发起运行</h2>
353
+ <span class="hint">填完直接启动,然后跳到看板</span>
354
+ </div>
355
+ <div style="padding: 18px 20px 22px">
356
+ <p id="tpl-purpose" style="margin:0;color:var(--muted);font-size:13px"></p>
357
+ <div id="tpl-fields"></div>
358
+ <div class="form-foot">
359
+ <button class="btn primary" id="tpl-start">启动这次运行</button>
360
+ <span class="note" id="tpl-note"></span>
361
+ </div>
362
+ </div>
363
+ </section>
364
+ </section>
365
+ </main>
366
+ </div>
367
+
368
+ <div class="gate" id="gate" hidden>
369
+ <div class="panel">
370
+ <h2>需要访问口令</h2>
371
+ <p>看板只接受本机访问。打开插件状态目录里的 <code>board.json</code>,把 <code>token</code> 粘到下面,或直接打开带 <code>?token=</code> 的地址。</p>
372
+ <label class="sr-only" for="gate-token">访问口令</label>
373
+ <input id="gate-token" placeholder="粘贴 token" autocomplete="off" spellcheck="false">
374
+ <div class="row"><button class="btn primary" id="gate-submit">连接看板</button></div>
375
+ <div class="err" id="gate-err"></div>
376
+ </div>
377
+ </div>
378
+
379
+ <div class="modal-backdrop" id="modal" hidden>
380
+ <div class="modal" role="dialog" aria-modal="true" aria-labelledby="modal-title">
381
+ <header>
382
+ <h3 id="modal-title">详情</h3>
383
+ <button class="btn ghost" id="modal-close">关闭</button>
384
+ </header>
385
+ <div class="body" id="modal-body"></div>
386
+ </div>
387
+ </div>
388
+
389
+ <div class="toast" id="toast" role="status" aria-live="polite"></div>
390
+
391
+ <script>
392
+ (() => {
393
+ 'use strict';
394
+
395
+ const $ = (id) => document.getElementById(id);
396
+ const state = {
397
+ token: '',
398
+ snapshot: null,
399
+ selectedRunId: null,
400
+ detail: null,
401
+ detailSig: '',
402
+ tab: 'board',
403
+ templateId: null,
404
+ inboxDraft: new Map(),
405
+ source: null,
406
+ retry: 0,
407
+ timer: null,
408
+ busy: new Set(),
409
+ };
410
+
411
+ const RUN_PILL = {
412
+ queued: 'idle', planning: 'run', running: 'run', aggregating: 'run',
413
+ completed: 'ok', partial: 'warn', failed: 'bad', cancelled: 'idle', interrupted: 'warn',
414
+ };
415
+ const SUB_PILL = {
416
+ pending: 'idle', waiting: 'idle', running: 'run', completed: 'ok',
417
+ failed: 'bad', skipped: 'idle', unverified: 'warn', cancelled: 'idle', blocked: 'warn',
418
+ };
419
+
420
+ function esc(value) {
421
+ return String(value == null ? '' : value)
422
+ .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
423
+ .replace(/"/g, '&quot;').replace(/'/g, '&#39;');
424
+ }
425
+ function pill(status, label, kind) {
426
+ return `<span class="pill ${kind || ''}"><span class="mini-dot"></span>${esc(label || status)}</span>`;
427
+ }
428
+ function runPill(run) { return pill(run.status, run.statusLabel, RUN_PILL[run.status] || 'idle'); }
429
+ function subPill(subtask) { return pill(subtask.status, subtask.statusLabel, SUB_PILL[subtask.status] || 'idle'); }
430
+ function fmtDuration(ms) {
431
+ if (ms == null || !Number.isFinite(ms)) return '—';
432
+ const s = Math.max(0, Math.round(ms / 1000));
433
+ if (s < 60) return s + ' 秒';
434
+ const m = Math.floor(s / 60), h = Math.floor(m / 60);
435
+ if (h > 0) return h + ' 小时 ' + (m % 60) + ' 分';
436
+ return m + ' 分 ' + (s % 60) + ' 秒';
437
+ }
438
+ function fmtTime(iso) {
439
+ if (!iso) return '—';
440
+ const d = new Date(iso);
441
+ if (Number.isNaN(d.getTime())) return '—';
442
+ return d.toLocaleString('zh-CN', { month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit' });
443
+ }
444
+ function fmtTokens(n) {
445
+ if (n == null || !Number.isFinite(n)) return '—';
446
+ return n >= 1000 ? (n / 1000).toFixed(1) + 'k' : String(n);
447
+ }
448
+ function fmtCost(cost) { return cost && cost.text ? cost.text : '—'; }
449
+
450
+ function toast(message, bad = false) {
451
+ const el = $('toast');
452
+ el.textContent = message;
453
+ el.className = 'toast show' + (bad ? ' bad' : '');
454
+ clearTimeout(el._timer);
455
+ el._timer = setTimeout(() => { el.className = 'toast' + (bad ? ' bad' : ''); }, 3200);
456
+ }
457
+
458
+ function tokenFromUrl() {
459
+ const url = new URL(location.href);
460
+ const value = url.searchParams.get('token');
461
+ if (!value) return '';
462
+ url.searchParams.delete('token');
463
+ history.replaceState(null, '', url.pathname + (url.search ? url.search : '') + url.hash);
464
+ return value;
465
+ }
466
+
467
+ function readToken() {
468
+ const fromUrl = tokenFromUrl();
469
+ if (fromUrl) {
470
+ try { localStorage.setItem('dsh.board.token', fromUrl); } catch {}
471
+ return fromUrl;
472
+ }
473
+ try { return localStorage.getItem('dsh.board.token') || ''; } catch { return ''; }
474
+ }
475
+
476
+ function showGate(message) {
477
+ $('gate').hidden = false;
478
+ $('shell').style.display = 'none';
479
+ if (message) $('gate-err').textContent = message;
480
+ $('gate-token').focus();
481
+ }
482
+ function hideGate() {
483
+ $('gate').hidden = true;
484
+ $('shell').style.display = '';
485
+ }
486
+
487
+ async function api(path, options = {}) {
488
+ const headers = { authorization: 'Bearer ' + state.token };
489
+ if (options.body !== undefined) headers['content-type'] = 'application/json';
490
+ const res = await fetch(path, {
491
+ method: options.method || 'GET',
492
+ headers,
493
+ body: options.body !== undefined ? JSON.stringify(options.body) : undefined,
494
+ });
495
+ let payload = null;
496
+ try { payload = await res.json(); } catch {}
497
+ if (res.status === 401 || res.status === 403 && payload?.error?.id === 'unauthorized') {
498
+ try { localStorage.removeItem('dsh.board.token'); } catch {}
499
+ showGate('口令不正确或已过期,请重新粘贴。');
500
+ throw new Error('访问口令不正确');
501
+ }
502
+ if (!res.ok) {
503
+ const message = payload?.error?.summary || payload?.error?.message || ('请求失败(' + res.status + ')');
504
+ const hint = payload?.error?.hint;
505
+ throw new Error(hint ? message + ' ' + hint : message);
506
+ }
507
+ return payload?.data ?? payload;
508
+ }
509
+
510
+ // ---------- connection ----------
511
+
512
+ function setConn(kind, text) {
513
+ const el = $('conn');
514
+ el.className = 'conn ' + kind;
515
+ $('conn-text').textContent = text;
516
+ }
517
+
518
+ function connect() {
519
+ if (!state.token) { showGate(); return; }
520
+ hideGate();
521
+ if (state.source) { try { state.source.close(); } catch {} }
522
+ setConn('connecting', '正在连接');
523
+ const source = new EventSource('/api/board/events?token=' + encodeURIComponent(state.token));
524
+ state.source = source;
525
+ source.addEventListener('board', (event) => {
526
+ let frame = null;
527
+ try { frame = JSON.parse(event.data); } catch { return; }
528
+ if (frame.type === 'snapshot' && frame.data) {
529
+ state.retry = 0;
530
+ setConn('live', '实时更新中');
531
+ applySnapshot(frame.data);
532
+ } else if (frame.type === 'error') {
533
+ toast(frame.message || '看板读取失败', true);
534
+ }
535
+ });
536
+ source.onopen = () => { state.retry = 0; setConn('live', '实时更新中'); };
537
+ source.onerror = () => {
538
+ setConn('down', '连接断开,正在重连');
539
+ if (source.readyState === EventSource.CLOSED) {
540
+ scheduleReconnect();
541
+ }
542
+ };
543
+ }
544
+
545
+ function scheduleReconnect() {
546
+ clearTimeout(state.timer);
547
+ const delay = Math.min(15000, 700 * Math.pow(1.6, state.retry++));
548
+ state.timer = setTimeout(connect, delay);
549
+ }
550
+
551
+ // ---------- render ----------
552
+
553
+ function applySnapshot(snapshot) {
554
+ state.snapshot = snapshot;
555
+ renderRunList();
556
+ renderInbox();
557
+ renderTemplates();
558
+ renderDetailIfNeeded();
559
+ const count = snapshot.inbox ? snapshot.inbox.length : 0;
560
+ const badge = $('inbox-count');
561
+ badge.textContent = String(count);
562
+ badge.hidden = count === 0;
563
+ badge.className = 'badge' + (count > 0 ? ' hot' : '');
564
+ $('run-count').textContent = snapshot.runs.length + ' 次';
565
+ if (!state.selectedRunId && snapshot.runs[0]) {
566
+ state.selectedRunId = snapshot.runs[0].id;
567
+ renderRunList();
568
+ loadDetail();
569
+ }
570
+ if (!state.snapshot.capabilities.start) {
571
+ $('tpl-note').textContent = snapshot.capabilities.reason || '当前宿主不能从看板发起运行';
572
+ }
573
+ }
574
+
575
+ function renderRunList() {
576
+ const list = $('run-list');
577
+ const runs = state.snapshot?.runs || [];
578
+ if (runs.length === 0) {
579
+ list.innerHTML = '<div class="empty"><strong>还没有运行</strong>去「模板」发起第一次,或者让助手调用 superagent_start。</div>';
580
+ return;
581
+ }
582
+ list.innerHTML = runs.map(run => `
583
+ <button class="run-item" aria-current="${run.id === state.selectedRunId}" data-run="${esc(run.id)}">
584
+ <p class="goal">${esc(run.goal)}</p>
585
+ <div class="run-meta">
586
+ ${runPill(run)}
587
+ <span>${esc(fmtDuration(run.elapsedMs))}</span>
588
+ <span>${run.counts.completed || 0}/${run.counts.total} 完成</span>
589
+ ${run.attentionCount ? '<span>· ' + run.attentionCount + ' 个待答</span>' : ''}
590
+ </div>
591
+ </button>`).join('');
592
+ list.querySelectorAll('[data-run]').forEach(button => {
593
+ button.addEventListener('click', () => {
594
+ state.selectedRunId = button.dataset.run;
595
+ state.detail = null;
596
+ state.detailSig = '';
597
+ renderRunList();
598
+ renderDetailIfNeeded(true);
599
+ loadDetail();
600
+ });
601
+ });
602
+ }
603
+
604
+ function renderDetailIfNeeded(force) {
605
+ const run = (state.snapshot?.runs || []).find(item => item.id === state.selectedRunId);
606
+ if (!run) {
607
+ $('detail').innerHTML = '<div class="empty"><strong>还没有选中运行</strong>左侧点一次运行,这里会显示每条子任务的进度。</div>';
608
+ return;
609
+ }
610
+ const sig = JSON.stringify(run.subtasks.map(s => [s.key, s.status, s.attempts, s.tokens, s.finishedAt]));
611
+ if (!force && state.detail && state.detailSig === sig) return;
612
+ state.detailSig = sig;
613
+ renderDetail(run);
614
+ loadDetail();
615
+ }
616
+
617
+ function renderDetail(run) {
618
+ const detail = state.detail && state.detail.run.id === run.id ? state.detail : null;
619
+ const capacity = state.snapshot?.capabilities || {};
620
+ const explanation = run.explanation;
621
+ const parts = [];
622
+ parts.push(`<div class="detail-head">
623
+ <p class="goal">${esc(run.goal)}</p>
624
+ <div class="detail-actions">
625
+ ${runPill(run)}
626
+ ${run.active && capacity.answer ? '<button class="btn danger" id="run-cancel">停止这次运行</button>' : ''}
627
+ ${!run.active && capacity.answer ? '<button class="btn" id="run-resume">继续未完成的工作</button>' : ''}
628
+ <button class="btn ghost" id="run-report">${detail && detail.report ? '查看最终报告' : '刷新详情'}</button>
629
+ </div>
630
+ <div class="stat-row">
631
+ <span>建立于 <b>${esc(fmtTime(run.createdAt))}</b></span>
632
+ <span>耗时 <b>${esc(fmtDuration(run.elapsedMs))}</b></span>
633
+ <span>tokens <b class="mono">${esc(fmtTokens(run.tokens))}</b></span>
634
+ <span>花费 <b class="mono">${esc(fmtCost(run.cost))}</b></span>
635
+ <span>子任务 <b>${run.counts.completed || 0}/${run.counts.total} 完成</b></span>
636
+ </div>
637
+ ${run.preflight && run.preflight.blocked ? `<div class="explain bad" style="margin-top:14px"><h4>有 ${run.preflight.blocked} 条子任务缺少能力,没有启动</h4><p>它们在下面标成「缺少能力,未启动」,补上工具后可以单独重跑。</p></div>` : ''}
638
+ </div>`);
639
+
640
+ if (explanation && run.status !== 'completed') {
641
+ parts.push(errorBlock(explanation, run, null));
642
+ }
643
+
644
+ if (!run.subtasks.length) {
645
+ parts.push('<div class="empty"><strong>还没有拆出子任务</strong>运行可能还在规划,或规划没有产出可用步骤。</div>');
646
+ } else {
647
+ parts.push('<div class="cards">' + run.subtasks.map(subtask => cardHtml(run, subtask, capacity)).join('') + '</div>');
648
+ }
649
+ const el = $('detail');
650
+ el.innerHTML = parts.join('');
651
+ wireDetail(run);
652
+ }
653
+
654
+ function errorBlock(explanation, run, subtask) {
655
+ const tone = explanation.code === 'needs-review' || explanation.code === 'blocked' ? '' : ' bad';
656
+ const subKey = subtask?.key || explanation.subtaskKey || '';
657
+ return `<div class="explain${tone}" style="margin:12px 14px 0">
658
+ <h4>${esc(explanation.title)}</h4>
659
+ <p>${esc(explanation.what)}</p>
660
+ <p class="why">${esc(explanation.why)}</p>
661
+ <div class="acts">
662
+ ${actionButton(explanation.action, run, subKey)}
663
+ <button class="btn ghost" data-diagnostics="${esc(run.id)}" data-sub="${esc(subKey)}">复制诊断信息</button>
664
+ </div>
665
+ ${explanation.detail ? `<details class="raw"><summary>原始错误(给技术同学看)</summary><pre>${esc(explanation.detail)}</pre></details>` : ''}
666
+ </div>`;
667
+ }
668
+
669
+ function actionButton(action, run, subKey) {
670
+ if (!action) return '';
671
+ const map = {
672
+ retry: { label: action.label || '重试', attrs: `data-act="retry" data-run="${esc(run.id)}" data-sub="${esc(subKey)}"` },
673
+ rerun: { label: action.label || '重跑这一条', attrs: `data-act="rerun" data-run="${esc(run.id)}" data-sub="${esc(subKey)}"` },
674
+ config: { label: action.label || '查看配置说明', attrs: 'data-act="config"' },
675
+ model: { label: action.label || '检查模型配置', attrs: 'data-act="config"' },
676
+ review: { label: action.label || '去验收', attrs: 'data-act="review"' },
677
+ };
678
+ const item = map[action.kind];
679
+ if (!item) return '';
680
+ return `<button class="btn primary" ${item.attrs}>${esc(item.label)}</button>`;
681
+ }
682
+
683
+ function cardHtml(run, subtask, capacity) {
684
+ const deps = subtask.dependsOn.length
685
+ ? `<span>依赖 ${subtask.dependsOn.map(esc).join('、')}</span>` : '';
686
+ const waiting = subtask.waitingOn.length
687
+ ? `<span>等待 ${subtask.waitingOn.map(esc).join('、')}</span>` : '';
688
+ const usage = [
689
+ subtask.tokens != null ? `<span>tokens <b class="mono">${esc(fmtTokens(subtask.tokens))}</b></span>` : '',
690
+ subtask.cost ? `<span>花费 <b class="mono">${esc(fmtCost(subtask.cost))}</b></span>` : '',
691
+ subtask.attempts ? `<span>尝试 <b>${subtask.attempts}</b> 次</span>` : '',
692
+ subtask.startedAt ? `<span>开始 ${esc(fmtTime(subtask.startedAt))}</span>` : '',
693
+ subtask.elapsedMs != null ? `<span>耗时 <b>${esc(fmtDuration(subtask.elapsedMs))}</b></span>` : '',
694
+ ].join('');
695
+ return `<article class="card ${esc(subtask.status)}" data-sub-card="${esc(subtask.key)}">
696
+ <div class="card-top">
697
+ <div class="card-index">${esc(subtask.index ?? '·')}</div>
698
+ <div class="card-title">
699
+ <h3>${esc(subtask.title)}</h3>
700
+ <div class="card-sub">
701
+ ${subPill(subtask)}
702
+ ${subtask.childId ? `<span class="mono">${esc(subtask.childId)}</span>` : ''}
703
+ ${deps}${waiting}
704
+ ${subtask.hasResult ? '<span>有输出</span>' : ''}
705
+ </div>
706
+ ${usage ? `<div class="card-sub">${usage}</div>` : ''}
707
+ </div>
708
+ <div class="card-actions">
709
+ <button class="btn ghost" data-act="output" data-run="${esc(run.id)}" data-sub="${esc(subtask.key)}">输出</button>
710
+ ${subtask.canCancel && capacity.answer ? `<button class="btn danger" data-act="cancel" data-run="${esc(run.id)}" data-sub="${esc(subtask.key)}">取消</button>` : ''}
711
+ ${subtask.canRerun && capacity.answer ? `<button class="btn" data-act="rerun" data-run="${esc(run.id)}" data-sub="${esc(subtask.key)}">重跑</button>` : ''}
712
+ </div>
713
+ </div>
714
+ ${subtask.skipReason ? `<div class="explain"><h4>为什么没跑</h4><p>${esc(subtask.skipReason)}</p></div>` : ''}
715
+ ${subtask.explanation ? errorBlock(subtask.explanation, run, subtask) : ''}
716
+ </article>`;
717
+ }
718
+
719
+ function wireDetail(run) {
720
+ const el = $('detail');
721
+ el.querySelectorAll('[data-act]').forEach(button => {
722
+ button.addEventListener('click', () => onAction(button.dataset.act, button.dataset.run || run.id, button.dataset.sub || ''));
723
+ });
724
+ const cancel = $('run-cancel');
725
+ if (cancel) cancel.addEventListener('click', () => cancelRun(run.id));
726
+ const resume = $('run-resume');
727
+ if (resume) resume.addEventListener('click', () => resumeRun(run.id));
728
+ const report = $('run-report');
729
+ if (report) report.addEventListener('click', () => showReport(run.id));
730
+ }
731
+
732
+ function onAction(act, runId, subKey) {
733
+ if (act === 'cancel') return cancelSubtask(runId, subKey);
734
+ if (act === 'rerun' || act === 'retry') return rerunSubtask(runId, subKey);
735
+ if (act === 'output') return showOutput(runId, subKey);
736
+ if (act === 'config') return showConfigHelp(runId, subKey);
737
+ if (act === 'review') { switchTab('inbox'); return; }
738
+ if (act === 'diagnostics') return copyDiagnostics(runId, subKey);
739
+ }
740
+
741
+ async function loadDetail() {
742
+ const runId = state.selectedRunId;
743
+ if (!runId) return;
744
+ try {
745
+ const detail = await api('/api/board/runs/' + encodeURIComponent(runId));
746
+ if (state.selectedRunId !== runId) return;
747
+ state.detail = detail;
748
+ const run = (state.snapshot?.runs || []).find(item => item.id === runId);
749
+ if (run) renderDetail(run);
750
+ } catch (error) {
751
+ toast(error.message, true);
752
+ }
753
+ }
754
+
755
+ async function post(path, body) {
756
+ const data = await api(path, { method: 'POST', body });
757
+ await refresh();
758
+ return data;
759
+ }
760
+
761
+ async function refresh() {
762
+ try {
763
+ const snapshot = await api('/api/board/state');
764
+ applySnapshot(snapshot);
765
+ if (state.selectedRunId) await loadDetail();
766
+ } catch (error) {
767
+ toast(error.message, true);
768
+ }
769
+ }
770
+
771
+ async function cancelSubtask(runId, subKey) {
772
+ if (!subKey) { toast('先选中一条子任务再取消', true); return; }
773
+ const key = 'cancel:' + runId + ':' + subKey;
774
+ if (state.busy.has(key)) return;
775
+ state.busy.add(key);
776
+ try {
777
+ await post('/api/board/runs/' + encodeURIComponent(runId) + '/subtasks/' + encodeURIComponent(subKey) + '/cancel', {});
778
+ toast('已取消这一条,其他子任务继续。');
779
+ } catch (error) {
780
+ toast(error.message, true);
781
+ } finally {
782
+ state.busy.delete(key);
783
+ }
784
+ }
785
+
786
+ async function rerunSubtask(runId, subKey) {
787
+ if (!subKey) { toast('这条失败不属于某一条子任务,先点「继续未完成的工作」', true); return; }
788
+ const key = 'rerun:' + runId + ':' + subKey;
789
+ if (state.busy.has(key)) return;
790
+ state.busy.add(key);
791
+ try {
792
+ await post('/api/board/runs/' + encodeURIComponent(runId) + '/subtasks/' + encodeURIComponent(subKey) + '/rerun', {});
793
+ toast('已重新排队这一条,其他已完成的不重跑。');
794
+ } catch (error) {
795
+ toast(error.message, true);
796
+ } finally {
797
+ state.busy.delete(key);
798
+ }
799
+ }
800
+
801
+ async function cancelRun(runId) {
802
+ try {
803
+ // The board only knows per-subtask cancel; a whole-run stop is the sum of
804
+ // its open subtasks, done one by one so the audit trail stays readable.
805
+ const detail = state.detail && state.detail.run.id === runId ? state.detail : null;
806
+ const open = (detail?.subtasks || []).filter(subtask => subtask.canCancel);
807
+ if (!open.length) { toast('这次运行没有可停止的子任务', true); return; }
808
+ for (const subtask of open) {
809
+ await api('/api/board/runs/' + encodeURIComponent(runId) + '/subtasks/' + encodeURIComponent(subtask.key) + '/cancel', { method: 'POST', body: {} });
810
+ }
811
+ await refresh();
812
+ toast('已停止未完成的子任务。');
813
+ } catch (error) {
814
+ toast(error.message, true);
815
+ }
816
+ }
817
+
818
+ async function resumeRun(runId) {
819
+ try {
820
+ await post('/api/board/runs/' + encodeURIComponent(runId) + '/resume', {});
821
+ toast('已继续未完成的工作。');
822
+ } catch (error) {
823
+ toast(error.message, true);
824
+ }
825
+ }
826
+
827
+ function showReport(runId) {
828
+ const detail = state.detail && state.detail.run.id === runId ? state.detail : null;
829
+ if (!detail || !detail.report) {
830
+ loadDetail().then(() => showReport(runId));
831
+ return;
832
+ }
833
+ openModal('最终报告', '<pre>' + esc(detail.report) + '</pre>');
834
+ }
835
+
836
+ function showOutput(runId, subKey) {
837
+ const detail = state.detail && state.detail.run.id === runId ? state.detail : null;
838
+ if (!detail) { loadDetail().then(() => showOutput(runId, subKey)); return; }
839
+ const subtask = (detail.subtasks || []).find(item => item.key === subKey);
840
+ if (!subtask) { toast('这条子任务的详情暂时读不到', true); return; }
841
+ const audit = (subtask.audit || []).slice().reverse().map(entry => auditHtml(entry)).join('');
842
+ openModal(subtask.title + ' · 输出与审计', [
843
+ '<h4>它拿到的任务</h4><pre>' + esc(subtask.prompt || '(没有单独的任务说明)') + '</pre>',
844
+ '<h4>输出</h4><pre>' + esc(subtask.result || '(还没有输出)') + '</pre>',
845
+ subtask.deliverables.length ? '<h4>交付物</h4><pre>' + esc(subtask.deliverables.join('\n')) + '</pre>' : '',
846
+ '<h4>审计记录</h4>' + (audit || '<p style="color:var(--muted);font-size:12.5px">这条子任务还没有审计记录。</p>'),
847
+ ].join(''));
848
+ }
849
+
850
+ function auditHtml(entry) {
851
+ return `<div class="audit-row"><time>${esc(fmtTime(entry.at))}</time>
852
+ <div class="what"><b>${esc(entry.kind || entry.action || '事件')}</b>
853
+ ${entry.actor ? ' · ' + esc(entry.actor) : ''}
854
+ ${entry.decision ? ' · ' + esc(entry.decision) : ''}
855
+ ${entry.result ? ' · ' + esc(entry.result) : ''}
856
+ ${entry.reason ? '<br>' + esc(entry.reason) : ''}</div></div>`;
857
+ }
858
+
859
+ function showConfigHelp(runId, subKey) {
860
+ const run = (state.snapshot?.runs || []).find(item => item.id === runId);
861
+ const subtask = run?.subtasks.find(item => item.key === subKey);
862
+ const explanation = subtask?.explanation || run?.explanation;
863
+ const lines = [
864
+ '<h4>先看这一句</h4><p>' + esc(explanation?.what || '没有读到具体原因,先复制诊断信息。') + '</p>',
865
+ '<h4>可以检查的地方</h4>',
866
+ '<ul style="margin:0;padding-left:18px;color:var(--muted);font-size:13px;line-height:1.8">',
867
+ '<li>宿主的工具/权限配置:缺能力、spawn EPERM、沙箱写入被拒,都来自这里。</li>',
868
+ '<li>模型配置:地址、密钥、模型名、额度和超时。</li>',
869
+ '<li>工作目录:子任务只能写父会话可写的目录。</li>',
870
+ '</ul>',
871
+ '<h4>诊断信息</h4><pre>' + esc(diagnostics(runId, subKey)) + '</pre>',
872
+ ];
873
+ openModal('配置说明', lines.join(''));
874
+ }
875
+
876
+ function diagnostics(runId, subKey) {
877
+ const run = (state.snapshot?.runs || []).find(item => item.id === runId);
878
+ const subtask = run?.subtasks.find(item => item.key === subKey);
879
+ return JSON.stringify({
880
+ runId,
881
+ subtask: subKey || null,
882
+ runStatus: run?.status,
883
+ runError: run?.error,
884
+ subtaskStatus: subtask?.status,
885
+ subtaskError: subtask?.explanation?.detail || null,
886
+ missingCapabilities: subtask?.missingCapabilities,
887
+ requires: subtask?.requires,
888
+ at: new Date().toISOString(),
889
+ }, null, 2);
890
+ }
891
+
892
+ async function copyDiagnostics(runId, subKey) {
893
+ const text = diagnostics(runId, subKey);
894
+ try {
895
+ await navigator.clipboard.writeText(text);
896
+ toast('诊断信息已复制。');
897
+ } catch {
898
+ openModal('诊断信息', '<pre>' + esc(text) + '</pre>');
899
+ }
900
+ }
901
+
902
+ function openModal(title, html) {
903
+ $('modal-title').textContent = title;
904
+ $('modal-body').innerHTML = html;
905
+ $('modal').hidden = false;
906
+ }
907
+ function closeModal() { $('modal').hidden = true; }
908
+
909
+ // ---------- inbox ----------
910
+
911
+ function renderInbox() {
912
+ const items = state.snapshot?.inbox || [];
913
+ const root = $('inbox');
914
+ if (items.length === 0) {
915
+ root.innerHTML = `<div class="panel"><div class="empty"><div class="emoji">✓</div><strong>当前没有需要你处理的事</strong>运行会自己往前走;出问题时这里会亮起来。</div></div>`;
916
+ return;
917
+ }
918
+ root.innerHTML = '<div class="inbox-list">' + items.map(item => {
919
+ const draft = state.inboxDraft.get(item.id) || { text: '', choice: '' };
920
+ const choices = item.options.map(option =>
921
+ `<button class="choice" aria-pressed="${draft.choice === option}" data-choice="${esc(option)}" data-item="${esc(item.id)}">${esc(option)}</button>`
922
+ ).join('');
923
+ return `<article class="inbox-card" data-inbox="${esc(item.id)}">
924
+ <header>
925
+ ${pill(item.kind, item.kindLabel, item.kind === 'review' ? 'warn' : item.kind === 'permission' ? 'bad' : 'run')}
926
+ <h3>${esc(item.title)}</h3>
927
+ </header>
928
+ <p class="why">${esc(item.what)}</p>
929
+ <p class="why">${esc(item.why)}</p>
930
+ <div class="impact">${esc(item.impact)}</div>
931
+ ${choices ? '<div class="answer-row">' + choices + '</div>' : ''}
932
+ <div class="answer-box">
933
+ <textarea data-answer="${esc(item.id)}" placeholder="也可以直接写你的回答或补充说明"></textarea>
934
+ <button class="btn primary" data-send="${esc(item.id)}">提交回答</button>
935
+ </div>
936
+ </article>`;
937
+ }).join('') + '</div>';
938
+
939
+ root.querySelectorAll('[data-choice]').forEach(button => {
940
+ button.addEventListener('click', () => {
941
+ const id = button.dataset.item;
942
+ const draft = state.inboxDraft.get(id) || { text: '', choice: '' };
943
+ draft.choice = button.dataset.choice;
944
+ state.inboxDraft.set(id, draft);
945
+ const card = root.querySelector('[data-inbox="' + cssEscape(id) + '"]');
946
+ if (card) card.querySelectorAll('[data-choice]').forEach(other => {
947
+ other.setAttribute('aria-pressed', String(other.dataset.choice === draft.choice));
948
+ });
949
+ const area = root.querySelector('[data-answer="' + cssEscape(id) + '"]');
950
+ if (area && !area.value) area.value = draft.choice;
951
+ });
952
+ });
953
+ root.querySelectorAll('[data-send]').forEach(button => {
954
+ button.addEventListener('click', () => sendInbox(button.dataset.send));
955
+ });
956
+ }
957
+
958
+ function cssEscape(value) {
959
+ return String(value).replace(/["\\]/g, '\\$&');
960
+ }
961
+
962
+ async function sendInbox(itemId) {
963
+ const card = document.querySelector('[data-inbox="' + cssEscape(itemId) + '"]');
964
+ const area = card?.querySelector('[data-answer]');
965
+ const draft = state.inboxDraft.get(itemId) || { choice: '' };
966
+ const answer = (area?.value || draft.choice || '').trim();
967
+ if (!answer) { toast('先选一个选项,或写一句你的回答', true); return; }
968
+ const key = 'answer:' + itemId;
969
+ if (state.busy.has(key)) return;
970
+ state.busy.add(key);
971
+ if (area) area.disabled = true;
972
+ try {
973
+ const data = await post('/api/board/inbox/' + encodeURIComponent(itemId) + '/answer', { answer });
974
+ state.inboxDraft.delete(itemId);
975
+ toast(data?.message || '已提交,运行会从这里继续。');
976
+ } catch (error) {
977
+ toast(error.message, true);
978
+ } finally {
979
+ state.busy.delete(key);
980
+ if (area) area.disabled = false;
981
+ }
982
+ }
983
+
984
+ // ---------- templates ----------
985
+
986
+ function renderTemplates() {
987
+ const templates = state.snapshot?.templates || [];
988
+ const grid = $('tpl-grid');
989
+ grid.innerHTML = templates.map(template => `
990
+ <button class="tpl-card" data-tpl="${esc(template.id)}" aria-current="${state.templateId === template.id}">
991
+ <h3>${esc(template.title)}</h3>
992
+ <p>${esc(template.purpose)}</p>
993
+ <div class="tpl-tags">
994
+ <span class="tag">约 ${template.estimate} 个子任务</span>
995
+ <span class="tag">${template.params.length} 个参数</span>
996
+ <span class="tag">${esc(template.bestFor)}</span>
997
+ </div>
998
+ </button>`).join('');
999
+ grid.querySelectorAll('[data-tpl]').forEach(button => {
1000
+ button.addEventListener('click', () => selectTemplate(button.dataset.tpl));
1001
+ });
1002
+ if (state.templateId) renderTemplateForm();
1003
+ }
1004
+
1005
+ function selectTemplate(id) {
1006
+ state.templateId = id;
1007
+ renderTemplates();
1008
+ renderTemplateForm();
1009
+ $('tpl-form-panel').scrollIntoView({ behavior: 'smooth', block: 'nearest' });
1010
+ }
1011
+
1012
+ function renderTemplateForm() {
1013
+ const template = (state.snapshot?.templates || []).find(item => item.id === state.templateId);
1014
+ const panel = $('tpl-form-panel');
1015
+ if (!template) { panel.hidden = true; return; }
1016
+ panel.hidden = false;
1017
+ $('tpl-form-title').textContent = template.title;
1018
+ $('tpl-purpose').textContent = template.purpose + '。预计约 ' + template.estimate + ' 个子任务,规划器可能会合并步骤。';
1019
+ $('tpl-fields').innerHTML = template.params.map(param => `
1020
+ <div class="field">
1021
+ <label for="p-${esc(param.name)}">${esc(param.label)}</label>
1022
+ <p class="desc">${esc(param.hint)}${param.description ? ' · ' + esc(param.description) : ''}</p>
1023
+ <textarea id="p-${esc(param.name)}" data-param="${esc(param.name)}" rows="3"></textarea>
1024
+ </div>`).join('');
1025
+ $('tpl-start').disabled = !(state.snapshot?.capabilities.start);
1026
+ if (!state.snapshot?.capabilities.start) {
1027
+ $('tpl-note').textContent = state.snapshot?.capabilities.reason || '当前宿主不能从看板发起运行';
1028
+ } else {
1029
+ $('tpl-note').textContent = '启动后会直接跳到看板,实时显示每条子任务。';
1030
+ }
1031
+ }
1032
+
1033
+ async function startTemplate() {
1034
+ const template = (state.snapshot?.templates || []).find(item => item.id === state.templateId);
1035
+ if (!template) return;
1036
+ const values = {};
1037
+ const missing = [];
1038
+ document.querySelectorAll('[data-param]').forEach(input => {
1039
+ const value = input.value.trim();
1040
+ values[input.dataset.param] = value;
1041
+ if (!value) missing.push(input.previousElementSibling?.previousElementSibling?.textContent || input.dataset.param);
1042
+ });
1043
+ if (missing.length) { toast('还差:' + missing.join('、'), true); return; }
1044
+ const button = $('tpl-start');
1045
+ button.disabled = true;
1046
+ try {
1047
+ const data = await post('/api/board/templates/' + encodeURIComponent(template.id) + '/start', { values });
1048
+ if (data?.runId) {
1049
+ state.selectedRunId = data.runId;
1050
+ state.detail = null;
1051
+ state.detailSig = '';
1052
+ switchTab('board');
1053
+ await refresh();
1054
+ toast('已启动,跳到看板了。');
1055
+ }
1056
+ } catch (error) {
1057
+ toast(error.message, true);
1058
+ } finally {
1059
+ button.disabled = false;
1060
+ }
1061
+ }
1062
+
1063
+ // ---------- tabs ----------
1064
+
1065
+ function switchTab(tab) {
1066
+ state.tab = tab;
1067
+ document.querySelectorAll('nav.tabs button').forEach(button => {
1068
+ button.setAttribute('aria-selected', String(button.dataset.tab === tab));
1069
+ });
1070
+ document.querySelectorAll('.view').forEach(view => {
1071
+ view.hidden = view.id !== 'view-' + tab;
1072
+ });
1073
+ }
1074
+
1075
+ // ---------- boot ----------
1076
+
1077
+ function boot() {
1078
+ document.querySelectorAll('nav.tabs button').forEach(button => {
1079
+ button.addEventListener('click', () => switchTab(button.dataset.tab));
1080
+ });
1081
+ $('gate-submit').addEventListener('click', () => {
1082
+ const value = $('gate-token').value.trim();
1083
+ if (!value) { $('gate-err').textContent = '先粘贴 token。'; return; }
1084
+ try { localStorage.setItem('dsh.board.token', value); } catch {}
1085
+ state.token = value;
1086
+ $('gate-err').textContent = '';
1087
+ connect();
1088
+ });
1089
+ $('gate-token').addEventListener('keydown', event => {
1090
+ if (event.key === 'Enter') $('gate-submit').click();
1091
+ });
1092
+ $('modal-close').addEventListener('click', closeModal);
1093
+ $('modal').addEventListener('click', event => {
1094
+ if (event.target === $('modal')) closeModal();
1095
+ });
1096
+ document.addEventListener('keydown', event => {
1097
+ if (event.key === 'Escape') closeModal();
1098
+ });
1099
+ $('tpl-start').addEventListener('click', startTemplate);
1100
+
1101
+ state.token = readToken();
1102
+ if (!state.token) { showGate(); return; }
1103
+ connect();
1104
+ }
1105
+
1106
+ boot();
1107
+ })();
1108
+ </script>
1109
+ </body>
1110
+ </html>