@xiangyixie/dsh-superagent 0.14.7 → 0.14.8
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 +16 -0
- package/README.en.md +4 -4
- package/README.md +4 -4
- package/README.zh.md +4 -4
- package/lib/console.js +138 -6
- package/lib/index.js +244 -0
- package/package.json +1 -1
- package/public/console.html +635 -65
package/public/console.html
CHANGED
|
@@ -87,6 +87,8 @@
|
|
|
87
87
|
}
|
|
88
88
|
.btn:hover { color: var(--text); border-color: #35464f; background: #172028; }
|
|
89
89
|
.btn:disabled { opacity: .45; cursor: default; }
|
|
90
|
+
.btn.danger { color: var(--red); border-color: rgba(255, 123, 114, .3); }
|
|
91
|
+
.btn.danger:hover { background: rgba(255, 123, 114, .08); border-color: rgba(255, 123, 114, .45); }
|
|
90
92
|
|
|
91
93
|
.panel {
|
|
92
94
|
min-height: 0; display: flex; flex-direction: column;
|
|
@@ -148,6 +150,14 @@
|
|
|
148
150
|
@keyframes caret { 50% { opacity: 0; } }
|
|
149
151
|
.msg .meta { margin: 5px 4px 0; color: var(--faint); font-size: 11px; }
|
|
150
152
|
.msg .meta .failed { color: var(--red); }
|
|
153
|
+
.msg-actions { display: flex; gap: 4px; margin: 5px 4px 0; }
|
|
154
|
+
.msg.user .msg-actions { justify-content: flex-end; }
|
|
155
|
+
.msg-actions[hidden] { display: none; }
|
|
156
|
+
.msg-actions button {
|
|
157
|
+
padding: 1px 4px; border: 0; border-radius: 5px; background: transparent;
|
|
158
|
+
color: var(--faint); font: 11px var(--sans); cursor: pointer;
|
|
159
|
+
}
|
|
160
|
+
.msg-actions button:hover { color: var(--text); background: rgba(255, 255, 255, .06); }
|
|
151
161
|
.reasoning { margin-top: 8px; border-top: 1px dashed var(--line); padding-top: 7px; }
|
|
152
162
|
.reasoning summary { color: var(--faint); font-size: 11px; cursor: pointer; }
|
|
153
163
|
.reasoning pre { margin: 6px 0 0; color: var(--muted); font: 12px/1.6 var(--mono); white-space: pre-wrap; }
|
|
@@ -292,6 +302,26 @@
|
|
|
292
302
|
}
|
|
293
303
|
.options button:hover { background: rgba(246, 193, 119, .18); }
|
|
294
304
|
.options button:disabled { opacity: .45; }
|
|
305
|
+
.approval-card .options button.allow-tool {
|
|
306
|
+
border-color: rgba(94, 234, 212, .38); background: rgba(94, 234, 212, .1); color: #c2efe7;
|
|
307
|
+
}
|
|
308
|
+
.approval-card .call { margin-top: 7px; }
|
|
309
|
+
.approval-card .call summary {
|
|
310
|
+
color: #cbb794; font-size: 11px; cursor: pointer; list-style: none;
|
|
311
|
+
}
|
|
312
|
+
.approval-card .call summary::-webkit-details-marker { display: none; }
|
|
313
|
+
.approval-card .call summary::before { content: '▸ '; }
|
|
314
|
+
.approval-card .call[open] summary::before { content: '▾ '; }
|
|
315
|
+
.approval-card .call pre {
|
|
316
|
+
max-height: 240px; overflow: auto; margin: 6px 0 0; padding: 8px 9px;
|
|
317
|
+
border-radius: 7px; border: 1px solid rgba(246, 193, 119, .22);
|
|
318
|
+
background: rgba(10, 15, 18, .62); color: #e9dcc4;
|
|
319
|
+
font: 11px/1.55 var(--mono); white-space: pre-wrap; word-break: break-word;
|
|
320
|
+
}
|
|
321
|
+
.approval-card .call .note { margin: 5px 0 0; color: #b9a789; font-size: 10.5px; }
|
|
322
|
+
.approval-card .expiry { margin-top: 6px; color: #b9a789; font-size: 11px; }
|
|
323
|
+
.approval-card .expiry.soon { color: var(--amber); }
|
|
324
|
+
.approval-card .expiry.over { color: var(--red); }
|
|
295
325
|
.free { display: flex; gap: 6px; margin-top: 7px; }
|
|
296
326
|
.free input {
|
|
297
327
|
flex: 1; min-width: 0; height: 29px; padding: 0 8px;
|
|
@@ -493,6 +523,26 @@
|
|
|
493
523
|
margin: 0; padding: 13px; border-radius: 9px; border: 1px solid var(--line-soft);
|
|
494
524
|
background: #0a0f12; font: 12px/1.65 var(--mono); white-space: pre-wrap; word-break: break-word;
|
|
495
525
|
}
|
|
526
|
+
.dialog-report { font-size: 12.5px; line-height: 1.7; }
|
|
527
|
+
.dialog-report h1, .dialog-report h2, .dialog-report h3, .dialog-report h4 {
|
|
528
|
+
margin: 16px 0 8px; font-size: 14px; line-height: 1.45; color: var(--text);
|
|
529
|
+
}
|
|
530
|
+
.dialog-report h1 { font-size: 17px; }
|
|
531
|
+
.dialog-report h2 { font-size: 15px; }
|
|
532
|
+
.dialog-report p { margin: 0 0 9px; }
|
|
533
|
+
.dialog-report ul, .dialog-report ol { margin: 0 0 10px; padding-left: 22px; }
|
|
534
|
+
.dialog-report li { margin: 3px 0; }
|
|
535
|
+
.dialog-report code {
|
|
536
|
+
padding: 1px 5px; border-radius: 5px; background: rgba(94, 234, 212, .1);
|
|
537
|
+
font: 11.5px var(--mono); color: var(--text);
|
|
538
|
+
}
|
|
539
|
+
.dialog-report pre { margin: 10px 0; }
|
|
540
|
+
.dialog-report pre code { padding: 0; background: transparent; font-size: 11.5px; }
|
|
541
|
+
.dialog-report a { color: var(--teal); }
|
|
542
|
+
.dialog-report iframe { width: 100%; height: min(62vh, 620px); border: 1px solid var(--line-soft); border-radius: 10px; background: #fff; }
|
|
543
|
+
.dialog-report .image-preview { max-width: 100%; border-radius: 10px; border: 1px solid var(--line-soft); }
|
|
544
|
+
.dialog-actions { display: flex; gap: 8px; margin-top: 14px; }
|
|
545
|
+
.dialog-actions .btn { text-decoration: none; }
|
|
496
546
|
|
|
497
547
|
/* ---- conversation-first stage ---- */
|
|
498
548
|
.stage { flex: 1; min-height: 0; display: flex; justify-content: center; padding: 0 18px 18px; }
|
|
@@ -523,6 +573,15 @@
|
|
|
523
573
|
.lane-goal { font-size: 12.5px; font-weight: 600; line-height: 1.45; word-break: break-word; }
|
|
524
574
|
.lane-now { display: flex; align-items: center; gap: 6px; margin-top: 3px; color: var(--muted); font-size: 11px; }
|
|
525
575
|
.lane-now .spark { flex: none; width: 5px; height: 5px; border-radius: 50%; background: var(--amber); animation: blink 1.1s infinite; }
|
|
576
|
+
.lane-now .lane-tool { color: var(--faint); font-family: var(--mono); font-size: 10.5px; }
|
|
577
|
+
.lane-time { flex: none; padding-top: 1px; color: var(--faint); font: 10.5px var(--mono); white-space: nowrap; }
|
|
578
|
+
.lane-stop {
|
|
579
|
+
flex: none; height: 22px; padding: 0 9px; border-radius: 999px; cursor: pointer;
|
|
580
|
+
border: 1px solid rgba(255, 123, 114, .3); background: rgba(255, 123, 114, .06);
|
|
581
|
+
color: var(--red); font-size: 10.5px;
|
|
582
|
+
}
|
|
583
|
+
.lane-stop:hover { background: rgba(255, 123, 114, .13); }
|
|
584
|
+
.lane-stop:disabled { opacity: .55; cursor: default; }
|
|
526
585
|
.steps { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
|
|
527
586
|
.step { display: flex; align-items: flex-start; gap: 7px; color: var(--faint); font-size: 11px; line-height: 1.5; }
|
|
528
587
|
.step .sdot { flex: none; width: 6px; height: 6px; margin-top: 4px; border-radius: 50%; background: #3a4a55; }
|
|
@@ -544,7 +603,8 @@
|
|
|
544
603
|
.artifact {
|
|
545
604
|
display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
|
|
546
605
|
padding: 4px 9px; border-radius: 8px; text-decoration: none;
|
|
547
|
-
border: 1px solid rgba(94, 234, 212, .3); background: rgba(94, 234, 212, .07); color: #bdf3e8;
|
|
606
|
+
border: 1px solid rgba(94, 234, 212, .3); background: rgba(94, 234, 212, .07); color: #bdf3e8;
|
|
607
|
+
font-size: 11.5px; font-family: inherit; text-align: left; cursor: pointer;
|
|
548
608
|
}
|
|
549
609
|
.artifact:hover { background: rgba(94, 234, 212, .14); }
|
|
550
610
|
.artifact .ak { color: var(--faint); font: 10px var(--mono); }
|
|
@@ -591,6 +651,8 @@
|
|
|
591
651
|
color: var(--text); text-align: left; cursor: pointer;
|
|
592
652
|
}
|
|
593
653
|
.hist-item:hover { border-color: #35464f; }
|
|
654
|
+
.hist-item.current { border-color: #3a5a52; background: #101b1a; cursor: default; }
|
|
655
|
+
.hist-state + .hist-state { margin-left: 6px; }
|
|
594
656
|
.hist-top { display: flex; align-items: center; gap: 8px; color: var(--faint); font-size: 10.5px; }
|
|
595
657
|
.hist-state { margin-left: auto; }
|
|
596
658
|
.hist-text { margin-top: 4px; font-size: 12px; line-height: 1.55; word-break: break-word; }
|
|
@@ -619,6 +681,8 @@
|
|
|
619
681
|
</div>
|
|
620
682
|
<div class="topbar-actions">
|
|
621
683
|
<span id="conn" class="conn"><i></i><span id="connText">连接中</span></span>
|
|
684
|
+
<button id="stopAll" class="btn danger" type="button" hidden>停止这一轮</button>
|
|
685
|
+
<button id="newChatTop" class="btn" type="button" disabled>新对话</button>
|
|
622
686
|
<button id="historyOpen" class="btn" type="button">历史</button>
|
|
623
687
|
<button id="advancedOpen" class="btn" type="button">高级</button>
|
|
624
688
|
</div>
|
|
@@ -659,7 +723,7 @@
|
|
|
659
723
|
<div class="drawer-head">
|
|
660
724
|
<div>
|
|
661
725
|
<span class="kicker">历史</span>
|
|
662
|
-
<h2
|
|
726
|
+
<h2>你的对话</h2>
|
|
663
727
|
</div>
|
|
664
728
|
<button class="drawer-close" data-close="history" type="button" aria-label="关闭">×</button>
|
|
665
729
|
</div>
|
|
@@ -679,7 +743,6 @@
|
|
|
679
743
|
<h3>会话</h3>
|
|
680
744
|
<div id="advSession" class="adv-kv"></div>
|
|
681
745
|
<div class="adv-actions">
|
|
682
|
-
<button id="newChat" class="btn tiny" type="button" disabled>开始新对话</button>
|
|
683
746
|
<button id="wizardOpen" class="btn tiny" type="button">首次使用向导</button>
|
|
684
747
|
</div>
|
|
685
748
|
</section>
|
|
@@ -737,7 +800,8 @@
|
|
|
737
800
|
</div>
|
|
738
801
|
<div class="dialog-body">
|
|
739
802
|
<div id="dialogMeta" class="dialog-meta"></div>
|
|
740
|
-
<
|
|
803
|
+
<div id="dialogReport" class="dialog-report"></div>
|
|
804
|
+
<div id="dialogActions" class="dialog-actions" hidden></div>
|
|
741
805
|
</div>
|
|
742
806
|
</dialog>
|
|
743
807
|
|
|
@@ -758,7 +822,8 @@
|
|
|
758
822
|
const sessionLabelEl = $('sessionLabel')
|
|
759
823
|
const connEl = $('conn')
|
|
760
824
|
const connTextEl = $('connText')
|
|
761
|
-
const newChatEl = $('
|
|
825
|
+
const newChatEl = $('newChatTop')
|
|
826
|
+
const stopAllEl = $('stopAll')
|
|
762
827
|
const gateEl = $('gate')
|
|
763
828
|
const gateFormEl = $('gateForm')
|
|
764
829
|
const gateErrorEl = $('gateError')
|
|
@@ -768,6 +833,7 @@
|
|
|
768
833
|
const dialogTitleEl = $('dialogTitle')
|
|
769
834
|
const dialogMetaEl = $('dialogMeta')
|
|
770
835
|
const dialogReportEl = $('dialogReport')
|
|
836
|
+
const dialogActionsEl = $('dialogActions')
|
|
771
837
|
const wizardDialogEl = $('wizardDialog')
|
|
772
838
|
const wizardStepsEl = $('wizardSteps')
|
|
773
839
|
const wizardBodyEl = $('wizardBody')
|
|
@@ -807,6 +873,12 @@
|
|
|
807
873
|
runAnchors: new Map(),
|
|
808
874
|
attentionSeen: new Set(),
|
|
809
875
|
approvals: new Map(),
|
|
876
|
+
sessions: [],
|
|
877
|
+
sessionsLoadedAt: 0,
|
|
878
|
+
sessionsBusy: false,
|
|
879
|
+
sessionNote: null,
|
|
880
|
+
cancelRequested: new Set(),
|
|
881
|
+
stopAllBusy: false,
|
|
810
882
|
}
|
|
811
883
|
|
|
812
884
|
const WIZ = {
|
|
@@ -908,6 +980,7 @@
|
|
|
908
980
|
modeTextEl.textContent = chat ? '已就绪' : mode === 'loading' ? '连接中' : '未连接'
|
|
909
981
|
newChatEl.disabled = !chat
|
|
910
982
|
inputEl.disabled = !chat
|
|
983
|
+
syncStopAll()
|
|
911
984
|
updateSend()
|
|
912
985
|
}
|
|
913
986
|
|
|
@@ -987,15 +1060,82 @@
|
|
|
987
1060
|
if (message.failed) meta.append(el('span', 'failed', ' · 发送失败'))
|
|
988
1061
|
if (message.interrupted) meta.append(el('span', '', ' · 已中断'))
|
|
989
1062
|
|
|
990
|
-
|
|
1063
|
+
const actions = el('div', 'msg-actions')
|
|
1064
|
+
actions.hidden = !message.text
|
|
1065
|
+
if (message.role === 'assistant') {
|
|
1066
|
+
const copy = el('button', '', '复制')
|
|
1067
|
+
copy.type = 'button'
|
|
1068
|
+
copy.title = '复制这条回复的完整文本'
|
|
1069
|
+
copy.addEventListener('click', () => copyMessage(message, copy))
|
|
1070
|
+
actions.append(copy)
|
|
1071
|
+
} else {
|
|
1072
|
+
const edit = el('button', '', '编辑并重发')
|
|
1073
|
+
edit.type = 'button'
|
|
1074
|
+
edit.title = '把这条原文放回输入框,改完再发送'
|
|
1075
|
+
edit.addEventListener('click', () => editMessage(message))
|
|
1076
|
+
actions.append(edit)
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
article.append(who, bubble, meta, actions)
|
|
991
1080
|
message.el = article
|
|
992
1081
|
message.textEl = text
|
|
993
1082
|
message.bubbleEl = bubble
|
|
994
1083
|
message.toolsEl = tools
|
|
995
1084
|
message.metaEl = meta
|
|
1085
|
+
message.actionsEl = actions
|
|
996
1086
|
return article
|
|
997
1087
|
}
|
|
998
1088
|
|
|
1089
|
+
function formatTextOf(message) {
|
|
1090
|
+
return typeof message.text === 'string' ? message.text : ''
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
async function copyText(value) {
|
|
1094
|
+
const text = typeof value === 'string' ? value : ''
|
|
1095
|
+
if (!text) return false
|
|
1096
|
+
if (navigator.clipboard?.writeText) {
|
|
1097
|
+
try {
|
|
1098
|
+
await navigator.clipboard.writeText(text)
|
|
1099
|
+
return true
|
|
1100
|
+
} catch {
|
|
1101
|
+
// The page may not be a secure context; fall through to the old path.
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
const area = document.createElement('textarea')
|
|
1105
|
+
area.value = text
|
|
1106
|
+
area.setAttribute('readonly', '')
|
|
1107
|
+
area.style.position = 'fixed'
|
|
1108
|
+
area.style.top = '-1000px'
|
|
1109
|
+
document.body.append(area)
|
|
1110
|
+
area.select()
|
|
1111
|
+
let copied = false
|
|
1112
|
+
try { copied = document.execCommand('copy') } catch { copied = false }
|
|
1113
|
+
area.remove()
|
|
1114
|
+
return copied
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
async function copyMessage(message, button) {
|
|
1118
|
+
const copied = await copyText(formatTextOf(message))
|
|
1119
|
+
if (!copied) {
|
|
1120
|
+
pushToast('复制失败,可以手动选中文字。', 'err')
|
|
1121
|
+
return
|
|
1122
|
+
}
|
|
1123
|
+
pushToast('已复制这条回复。', 'ok')
|
|
1124
|
+
button.textContent = '已复制'
|
|
1125
|
+
setTimeout(() => { button.textContent = '复制' }, 1600)
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/** Put the original text back in the composer; sending stays the person's call. */
|
|
1129
|
+
function editMessage(message) {
|
|
1130
|
+
const text = formatTextOf(message)
|
|
1131
|
+
if (!text) return
|
|
1132
|
+
inputEl.value = text
|
|
1133
|
+
inputEl.dispatchEvent(new Event('input'))
|
|
1134
|
+
inputEl.focus()
|
|
1135
|
+
try { inputEl.setSelectionRange(text.length, text.length) } catch { /* not focusable yet */ }
|
|
1136
|
+
pushToast('原文已放回输入框,改完点发送。', 'ok')
|
|
1137
|
+
}
|
|
1138
|
+
|
|
999
1139
|
function summarizeArgs(value) {
|
|
1000
1140
|
if (value === undefined || value === null) return ''
|
|
1001
1141
|
let text
|
|
@@ -1038,6 +1178,7 @@
|
|
|
1038
1178
|
if (message.failed) message.metaEl.append(el('span', 'failed', ' · 发送失败'))
|
|
1039
1179
|
if (message.interrupted) message.metaEl.append(el('span', '', ' · 已中断'))
|
|
1040
1180
|
}
|
|
1181
|
+
if (message.actionsEl) message.actionsEl.hidden = !message.text
|
|
1041
1182
|
}
|
|
1042
1183
|
|
|
1043
1184
|
function addMessage(role, text, options = {}) {
|
|
@@ -1128,9 +1269,10 @@
|
|
|
1128
1269
|
}
|
|
1129
1270
|
|
|
1130
1271
|
function laneStatusText(run) {
|
|
1272
|
+
if (STATE.cancelRequested.has(run.id) && !isTerminal(run.status)) return '停止中'
|
|
1131
1273
|
if (run.status === 'queued') return '排队'
|
|
1132
1274
|
if (run.status === 'completed') return '完成'
|
|
1133
|
-
if (isTerminal(run.status)) return '失败'
|
|
1275
|
+
if (isTerminal(run.status)) return run.status === 'cancelled' ? '已取消' : '失败'
|
|
1134
1276
|
return '进行中'
|
|
1135
1277
|
}
|
|
1136
1278
|
|
|
@@ -1143,12 +1285,81 @@
|
|
|
1143
1285
|
return `${(minutes / 60).toFixed(1)} 小时`
|
|
1144
1286
|
}
|
|
1145
1287
|
|
|
1288
|
+
/** Per-lane clock, fine-grained enough to visibly tick. */
|
|
1289
|
+
function formatClock(ms) {
|
|
1290
|
+
if (!Number.isFinite(ms) || ms < 0) return ''
|
|
1291
|
+
const total = Math.floor(ms / 1000)
|
|
1292
|
+
const minutes = Math.floor(total / 60)
|
|
1293
|
+
const seconds = total % 60
|
|
1294
|
+
return minutes > 0 ? `${minutes}分${String(seconds).padStart(2, '0')}秒` : `${seconds}秒`
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
function laneElapsed(run) {
|
|
1298
|
+
const start = Date.parse(run.createdAt)
|
|
1299
|
+
const end = isTerminal(run.status) ? Date.parse(run.finishedAt) : Date.now()
|
|
1300
|
+
if (!Number.isFinite(start) || !Number.isFinite(end)) return ''
|
|
1301
|
+
return formatClock(end - start)
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
function tickLaneClocks() {
|
|
1305
|
+
for (const node of messagesEl.querySelectorAll('.lane-time')) {
|
|
1306
|
+
const started = Number(node.dataset.started)
|
|
1307
|
+
if (!Number.isFinite(started) || node.dataset.started === '') continue
|
|
1308
|
+
const finished = node.dataset.finished ? Number(node.dataset.finished) : Date.now()
|
|
1309
|
+
if (!Number.isFinite(finished)) continue
|
|
1310
|
+
node.textContent = formatClock(finished - started)
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
function syncStopAll() {
|
|
1315
|
+
const active = STATE.runs.filter(run => !isTerminal(run.status))
|
|
1316
|
+
stopAllEl.hidden = active.length === 0
|
|
1317
|
+
stopAllEl.disabled = STATE.mode !== 'chat' || active.length === 0 || STATE.stopAllBusy
|
|
1318
|
+
stopAllEl.textContent = STATE.stopAllBusy ? '正在停止…' : '停止这一轮'
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
async function stopRun(runId) {
|
|
1322
|
+
if (!runId || STATE.cancelRequested.has(runId)) return
|
|
1323
|
+
STATE.cancelRequested.add(runId)
|
|
1324
|
+
renderLanes()
|
|
1325
|
+
try {
|
|
1326
|
+
await api(`/api/console/runs/${encodeURIComponent(runId)}/cancel`, { method: 'POST' })
|
|
1327
|
+
pushToast('已请求停止,等宿主确认。', 'ok')
|
|
1328
|
+
} catch (error) {
|
|
1329
|
+
STATE.cancelRequested.delete(runId)
|
|
1330
|
+
renderLanes()
|
|
1331
|
+
pushToast(`无法停止这条任务线:${messageOf(error)}`, 'err')
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
async function stopAllRuns() {
|
|
1336
|
+
const active = STATE.runs.filter(run => !isTerminal(run.status) && !STATE.cancelRequested.has(run.id))
|
|
1337
|
+
if (active.length === 0) return
|
|
1338
|
+
STATE.stopAllBusy = true
|
|
1339
|
+
syncStopAll()
|
|
1340
|
+
for (const run of active) STATE.cancelRequested.add(run.id)
|
|
1341
|
+
renderLanes()
|
|
1342
|
+
for (const run of active) {
|
|
1343
|
+
try {
|
|
1344
|
+
await api(`/api/console/runs/${encodeURIComponent(run.id)}/cancel`, { method: 'POST' })
|
|
1345
|
+
} catch (error) {
|
|
1346
|
+
STATE.cancelRequested.delete(run.id)
|
|
1347
|
+
pushToast(`有任务线没停下来:${messageOf(error)}`, 'err')
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
STATE.stopAllBusy = false
|
|
1351
|
+
renderLanes()
|
|
1352
|
+
syncStopAll()
|
|
1353
|
+
pushToast('已请求停止这一轮。', 'ok')
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1146
1356
|
function stepLabel(subtask) {
|
|
1147
1357
|
return subtask.title || `步骤 ${subtask.index ?? ''}`.trim()
|
|
1148
1358
|
}
|
|
1149
1359
|
|
|
1150
1360
|
function currentAction(run) {
|
|
1151
1361
|
const subtasks = Array.isArray(run.subtasks) ? run.subtasks : []
|
|
1362
|
+
if (STATE.cancelRequested.has(run.id) && !isTerminal(run.status)) return '已请求停止,等宿主确认'
|
|
1152
1363
|
if (run.status === 'queued') return '排队,等前一条任务线让出位置'
|
|
1153
1364
|
if (run.status === 'planning') return '正在拆步骤'
|
|
1154
1365
|
if (run.status === 'aggregating') return '正在汇总各步骤的结果'
|
|
@@ -1162,14 +1373,22 @@
|
|
|
1162
1373
|
return '正在推进'
|
|
1163
1374
|
}
|
|
1164
1375
|
|
|
1376
|
+
/** Tools the running steps report right now; empty when unknown, never guessed. */
|
|
1377
|
+
function runningToolsOf(run) {
|
|
1378
|
+
const subtasks = Array.isArray(run.subtasks) ? run.subtasks : []
|
|
1379
|
+
return [...new Set(subtasks
|
|
1380
|
+
.filter(subtask => subtask.status === 'running' && typeof subtask.tool === 'string' && subtask.tool)
|
|
1381
|
+
.map(subtask => subtask.tool))]
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1165
1384
|
function laneSignature(run) {
|
|
1166
1385
|
const steps = Array.isArray(run.subtasks)
|
|
1167
|
-
? run.subtasks.map(subtask => `${subtask.status}:${subtask.title}`).join(',')
|
|
1386
|
+
? run.subtasks.map(subtask => `${subtask.status}:${subtask.title}:${subtask.tool || ''}`).join(',')
|
|
1168
1387
|
: ''
|
|
1169
1388
|
const attention = Array.isArray(run.attention)
|
|
1170
1389
|
? run.attention.map(item => `${item.id}:${item.question}`).join(',')
|
|
1171
1390
|
: ''
|
|
1172
|
-
return [run.status, steps, attention, run.report ? run.report.length : 0, run.finishedAt || ''].join('|')
|
|
1391
|
+
return [run.status, steps, attention, run.report ? run.report.length : 0, run.finishedAt || '', STATE.cancelRequested.has(run.id) ? 'stopping' : ''].join('|')
|
|
1173
1392
|
}
|
|
1174
1393
|
|
|
1175
1394
|
/**
|
|
@@ -1195,20 +1414,203 @@
|
|
|
1195
1414
|
return items.slice(0, 12)
|
|
1196
1415
|
}
|
|
1197
1416
|
|
|
1417
|
+
function artifactName(item) {
|
|
1418
|
+
return item.href.replace(/[\\/]+$/, '').split(/[\\/]/).pop() || item.href
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
function artifactUrl(item) {
|
|
1422
|
+
if (/^https?:\/\//i.test(item.href)) return item.href
|
|
1423
|
+
return `/api/console/artifact?path=${encodeURIComponent(item.href)}&runId=${encodeURIComponent(item.runId)}&token=${encodeURIComponent(STATE.token)}`
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1198
1426
|
function artifactElement(item) {
|
|
1199
1427
|
const isUrl = /^https?:\/\//i.test(item.href)
|
|
1200
|
-
const
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1428
|
+
const button = el('button', 'artifact')
|
|
1429
|
+
button.type = 'button'
|
|
1430
|
+
button.title = item.href
|
|
1431
|
+
button.append(el('span', 'ak', isUrl ? '页面' : '文件'))
|
|
1432
|
+
button.append(el('span', 'an', artifactName(item)))
|
|
1433
|
+
button.addEventListener('click', () => previewArtifact(item))
|
|
1434
|
+
return button
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
function escapeHtml(value) {
|
|
1438
|
+
return String(value).replace(/[&<>"']/g, ch => (
|
|
1439
|
+
{ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[ch]
|
|
1440
|
+
))
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
/** Inline spans a report actually uses: code, bold, http links. */
|
|
1444
|
+
function inlineMarkdown(text) {
|
|
1445
|
+
return escapeHtml(text)
|
|
1446
|
+
.replace(/`([^`]+)`/g, '<code>$1</code>')
|
|
1447
|
+
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
|
1448
|
+
.replace(/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g, '<a href="$2" target="_blank" rel="noopener">$1</a>')
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
/**
|
|
1452
|
+
* Minimal Markdown for run reports: headings, lists, fenced code, paragraphs.
|
|
1453
|
+
* Everything is escaped before markup is added, so a report cannot inject HTML.
|
|
1454
|
+
*/
|
|
1455
|
+
function renderMarkdownInto(container, text) {
|
|
1456
|
+
container.textContent = ''
|
|
1457
|
+
const lines = String(text || '').replace(/\r\n?/g, '\n').split('\n')
|
|
1458
|
+
let list = null
|
|
1459
|
+
let listTag = ''
|
|
1460
|
+
let buffer = []
|
|
1461
|
+
const closeList = () => { list = null; listTag = '' }
|
|
1462
|
+
const flushParagraph = () => {
|
|
1463
|
+
if (buffer.length === 0) return
|
|
1464
|
+
const para = el('p')
|
|
1465
|
+
para.innerHTML = inlineMarkdown(buffer.join('\n')).replace(/\n/g, '<br>')
|
|
1466
|
+
container.append(para)
|
|
1467
|
+
buffer = []
|
|
1468
|
+
}
|
|
1469
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
1470
|
+
const line = lines[i]
|
|
1471
|
+
const fence = line.match(/^\s*```(\S*)\s*$/)
|
|
1472
|
+
if (fence) {
|
|
1473
|
+
flushParagraph()
|
|
1474
|
+
closeList()
|
|
1475
|
+
const code = []
|
|
1476
|
+
i += 1
|
|
1477
|
+
while (i < lines.length && !/^\s*```\s*$/.test(lines[i])) {
|
|
1478
|
+
code.push(lines[i])
|
|
1479
|
+
i += 1
|
|
1480
|
+
}
|
|
1481
|
+
const pre = el('pre')
|
|
1482
|
+
pre.append(el('code', '', code.join('\n')))
|
|
1483
|
+
container.append(pre)
|
|
1484
|
+
continue
|
|
1485
|
+
}
|
|
1486
|
+
if (!line.trim()) {
|
|
1487
|
+
flushParagraph()
|
|
1488
|
+
closeList()
|
|
1489
|
+
continue
|
|
1490
|
+
}
|
|
1491
|
+
if (/^\s*(?:---+|\*\*\*+)\s*$/.test(line)) {
|
|
1492
|
+
flushParagraph()
|
|
1493
|
+
closeList()
|
|
1494
|
+
container.append(el('hr'))
|
|
1495
|
+
continue
|
|
1496
|
+
}
|
|
1497
|
+
const heading = line.match(/^(#{1,4})\s+(.*)$/)
|
|
1498
|
+
if (heading) {
|
|
1499
|
+
flushParagraph()
|
|
1500
|
+
closeList()
|
|
1501
|
+
const node = el(`h${heading[1].length}`)
|
|
1502
|
+
node.innerHTML = inlineMarkdown(heading[2])
|
|
1503
|
+
container.append(node)
|
|
1504
|
+
continue
|
|
1505
|
+
}
|
|
1506
|
+
const bullet = line.match(/^\s*[-*+]\s+(.*)$/)
|
|
1507
|
+
const ordered = line.match(/^\s*\d+[.)]\s+(.*)$/)
|
|
1508
|
+
if (bullet || ordered) {
|
|
1509
|
+
flushParagraph()
|
|
1510
|
+
const tag = bullet ? 'ul' : 'ol'
|
|
1511
|
+
if (!list || listTag !== tag) {
|
|
1512
|
+
list = el(tag)
|
|
1513
|
+
listTag = tag
|
|
1514
|
+
container.append(list)
|
|
1515
|
+
}
|
|
1516
|
+
const item = el('li')
|
|
1517
|
+
item.innerHTML = inlineMarkdown((bullet || ordered)[1])
|
|
1518
|
+
list.append(item)
|
|
1519
|
+
continue
|
|
1520
|
+
}
|
|
1521
|
+
buffer.push(line)
|
|
1522
|
+
}
|
|
1523
|
+
flushParagraph()
|
|
1524
|
+
closeList()
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
function previewFrame(src) {
|
|
1528
|
+
const frame = el('iframe')
|
|
1529
|
+
frame.setAttribute('sandbox', 'allow-scripts allow-popups allow-forms')
|
|
1530
|
+
frame.setAttribute('referrerpolicy', 'no-referrer')
|
|
1531
|
+
frame.src = src
|
|
1532
|
+
return frame
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
function setDialogActions(nodes) {
|
|
1536
|
+
dialogActionsEl.textContent = ''
|
|
1537
|
+
const items = Array.isArray(nodes) ? nodes : []
|
|
1538
|
+
for (const node of items) dialogActionsEl.append(node)
|
|
1539
|
+
dialogActionsEl.hidden = items.length === 0
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
function externalArtifactLink(item) {
|
|
1543
|
+
const link = el('a', 'btn', '新标签打开')
|
|
1544
|
+
link.href = artifactUrl(item)
|
|
1204
1545
|
link.target = '_blank'
|
|
1205
1546
|
link.rel = 'noopener'
|
|
1206
|
-
link.title = item.href
|
|
1207
|
-
link.append(el('span', 'ak', isUrl ? '页面' : '文件'))
|
|
1208
|
-
link.append(el('span', 'an', item.href.replace(/[\\/]+$/, '').split(/[\\/]/).pop() || item.href))
|
|
1209
1547
|
return link
|
|
1210
1548
|
}
|
|
1211
1549
|
|
|
1550
|
+
/**
|
|
1551
|
+
* Show an artifact in place. Markdown is rendered, HTML runs in a sandboxed
|
|
1552
|
+
* frame (the route already ships a sandbox CSP), images and PDFs render
|
|
1553
|
+
* natively, and anything else falls back to its text. The original file stays
|
|
1554
|
+
* one click away from the dialog footer.
|
|
1555
|
+
*/
|
|
1556
|
+
async function previewArtifact(item) {
|
|
1557
|
+
const isUrl = /^https?:\/\//i.test(item.href)
|
|
1558
|
+
const name = artifactName(item)
|
|
1559
|
+
const ext = (name.split('.').pop() || '').toLowerCase()
|
|
1560
|
+
dialogTitleEl.textContent = name
|
|
1561
|
+
dialogMetaEl.textContent = ''
|
|
1562
|
+
dialogMetaEl.append(el('span', '', isUrl ? item.href : `产物 · ${item.href}`))
|
|
1563
|
+
dialogReportEl.textContent = ''
|
|
1564
|
+
setDialogActions([externalArtifactLink(item)])
|
|
1565
|
+
const url = artifactUrl(item)
|
|
1566
|
+
if (isUrl) {
|
|
1567
|
+
dialogReportEl.append(previewFrame(url))
|
|
1568
|
+
reportDialogEl.showModal()
|
|
1569
|
+
return
|
|
1570
|
+
}
|
|
1571
|
+
if (ext === 'html' || ext === 'htm') {
|
|
1572
|
+
dialogReportEl.append(previewFrame(url))
|
|
1573
|
+
reportDialogEl.showModal()
|
|
1574
|
+
return
|
|
1575
|
+
}
|
|
1576
|
+
if (['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg'].includes(ext)) {
|
|
1577
|
+
const image = el('img', 'image-preview')
|
|
1578
|
+
image.src = url
|
|
1579
|
+
image.alt = name
|
|
1580
|
+
dialogReportEl.append(image)
|
|
1581
|
+
reportDialogEl.showModal()
|
|
1582
|
+
return
|
|
1583
|
+
}
|
|
1584
|
+
if (ext === 'pdf') {
|
|
1585
|
+
dialogReportEl.append(previewFrame(url))
|
|
1586
|
+
reportDialogEl.showModal()
|
|
1587
|
+
return
|
|
1588
|
+
}
|
|
1589
|
+
const loading = el('p', '', '正在读取…')
|
|
1590
|
+
dialogReportEl.append(loading)
|
|
1591
|
+
reportDialogEl.showModal()
|
|
1592
|
+
let text = ''
|
|
1593
|
+
try {
|
|
1594
|
+
const response = await fetch(url)
|
|
1595
|
+
if (!response.ok) {
|
|
1596
|
+
throw new Error(response.status === 415
|
|
1597
|
+
? '这个类型不能在控制台里预览'
|
|
1598
|
+
: `读取失败(HTTP ${response.status})`)
|
|
1599
|
+
}
|
|
1600
|
+
text = await response.text()
|
|
1601
|
+
} catch (error) {
|
|
1602
|
+
dialogReportEl.textContent = `读取失败:${messageOf(error)}`
|
|
1603
|
+
return
|
|
1604
|
+
}
|
|
1605
|
+
dialogReportEl.textContent = ''
|
|
1606
|
+
if (ext === 'md' || ext === 'markdown') renderMarkdownInto(dialogReportEl, text)
|
|
1607
|
+
else {
|
|
1608
|
+
const pre = el('pre')
|
|
1609
|
+
pre.append(el('code', '', text))
|
|
1610
|
+
dialogReportEl.append(pre)
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1212
1614
|
function resetLanes() {
|
|
1213
1615
|
STATE.laneEls.clear()
|
|
1214
1616
|
STATE.runAnchors.clear()
|
|
@@ -1280,57 +1682,91 @@
|
|
|
1280
1682
|
next.focus()
|
|
1281
1683
|
try { next.setSelectionRange(next.value.length, next.value.length) } catch {}
|
|
1282
1684
|
}
|
|
1685
|
+
if (artifacts.length > 0) {
|
|
1686
|
+
const chips = el('div', 'artifacts')
|
|
1687
|
+
for (const artifact of artifacts) chips.append(artifactElement(artifact))
|
|
1688
|
+
item.append(chips)
|
|
1689
|
+
}
|
|
1690
|
+
item.addEventListener('click', event => {
|
|
1691
|
+
if (event.target.closest('a')) return
|
|
1692
|
+
drawerClose()
|
|
1693
|
+
entry.message.el?.scrollIntoView({ block: 'center', behavior: 'smooth' })
|
|
1694
|
+
entry.message.el?.classList.add('flash')
|
|
1695
|
+
setTimeout(() => entry.message.el?.classList.remove('flash'), 1200)
|
|
1696
|
+
})
|
|
1697
|
+
historyListEl.append(item)
|
|
1283
1698
|
}
|
|
1284
1699
|
renderAdvancedRuns()
|
|
1700
|
+
for (const id of [...STATE.cancelRequested]) {
|
|
1701
|
+
const run = STATE.runs.find(item => item.id === id)
|
|
1702
|
+
if (!run || isTerminal(run.status)) STATE.cancelRequested.delete(id)
|
|
1703
|
+
}
|
|
1704
|
+
syncStopAll()
|
|
1285
1705
|
renderHistory()
|
|
1286
1706
|
}
|
|
1287
1707
|
|
|
1708
|
+
async function loadSessions() {
|
|
1709
|
+
if (STATE.sessionsBusy) return
|
|
1710
|
+
STATE.sessionsBusy = true
|
|
1711
|
+
renderHistory()
|
|
1712
|
+
try {
|
|
1713
|
+
const data = await api('/api/console/sessions')
|
|
1714
|
+
STATE.sessions = Array.isArray(data.sessions) ? data.sessions : []
|
|
1715
|
+
STATE.sessionsLoadedAt = Date.now()
|
|
1716
|
+
STATE.sessionNote = data.reason || null
|
|
1717
|
+
} catch (error) {
|
|
1718
|
+
STATE.sessions = []
|
|
1719
|
+
STATE.sessionNote = messageOf(error)
|
|
1720
|
+
} finally {
|
|
1721
|
+
STATE.sessionsBusy = false
|
|
1722
|
+
renderHistory()
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
|
|
1726
|
+
/**
|
|
1727
|
+
* Reopen a conversation the host already has. The page reloads so the normal
|
|
1728
|
+
* bootstrap/follow path rebuilds the thread, cards and artifacts — the same
|
|
1729
|
+
* code that serves a fresh page load, not a second renderer.
|
|
1730
|
+
*/
|
|
1731
|
+
async function switchToSession(sessionId) {
|
|
1732
|
+
if (!sessionId || sessionId === STATE.sessionId) {
|
|
1733
|
+
drawerClose()
|
|
1734
|
+
return
|
|
1735
|
+
}
|
|
1736
|
+
try {
|
|
1737
|
+
await api('/api/console/session/switch', { method: 'POST', body: { sessionId } })
|
|
1738
|
+
window.location.reload()
|
|
1739
|
+
} catch (error) {
|
|
1740
|
+
pushToast(`无法打开那条对话:${messageOf(error)}`, 'err')
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1288
1744
|
function renderHistory() {
|
|
1289
1745
|
if (historyDrawerEl.hidden) return
|
|
1290
1746
|
historyListEl.replaceChildren()
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
})
|
|
1747
|
+
if (STATE.sessionsBusy && STATE.sessions.length === 0) {
|
|
1748
|
+
historyListEl.append(el('p', 'adv-note', '正在读取对话…'))
|
|
1749
|
+
return
|
|
1750
|
+
}
|
|
1751
|
+
if (STATE.sessionNote) {
|
|
1752
|
+
historyListEl.append(el('p', 'adv-note', `无法读取历史:${STATE.sessionNote}`))
|
|
1298
1753
|
}
|
|
1299
|
-
if (
|
|
1300
|
-
historyListEl.append(el('p', 'adv-note', '
|
|
1754
|
+
if (STATE.sessions.length === 0) {
|
|
1755
|
+
historyListEl.append(el('p', 'adv-note', '还没有可回看的对话。'))
|
|
1301
1756
|
return
|
|
1302
1757
|
}
|
|
1303
|
-
for (const
|
|
1304
|
-
const
|
|
1758
|
+
for (const session of STATE.sessions) {
|
|
1759
|
+
const current = session.sessionId === STATE.sessionId
|
|
1760
|
+
const item = el('div', `hist-item${current ? ' current' : ''}`)
|
|
1305
1761
|
const top = el('div', 'hist-top')
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
}
|
|
1762
|
+
const when = session.updatedAt ? timeLabel(new Date(session.updatedAt).toISOString()) : '时间未知'
|
|
1763
|
+
top.append(el('span', '', when))
|
|
1764
|
+
if (current) top.append(el('span', 'hist-state', '当前'))
|
|
1765
|
+
else if (session.running) top.append(el('span', 'hist-state', '进行中'))
|
|
1766
|
+
if (session.runs > 0) top.append(el('span', 'hist-state', `${session.runs} 轮 · ${session.artifacts} 个产物`))
|
|
1311
1767
|
item.append(top)
|
|
1312
|
-
item.append(el('div', 'hist-text',
|
|
1313
|
-
|
|
1314
|
-
const seen = new Set()
|
|
1315
|
-
for (const run of entry.runs) {
|
|
1316
|
-
for (const artifact of artifactsOf(run)) {
|
|
1317
|
-
if (seen.has(artifact.href)) continue
|
|
1318
|
-
seen.add(artifact.href)
|
|
1319
|
-
artifacts.push(artifact)
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
if (artifacts.length > 0) {
|
|
1323
|
-
const chips = el('div', 'artifacts')
|
|
1324
|
-
for (const artifact of artifacts) chips.append(artifactElement(artifact))
|
|
1325
|
-
item.append(chips)
|
|
1326
|
-
}
|
|
1327
|
-
item.addEventListener('click', event => {
|
|
1328
|
-
if (event.target.closest('a')) return
|
|
1329
|
-
drawerClose()
|
|
1330
|
-
entry.message.el?.scrollIntoView({ block: 'center', behavior: 'smooth' })
|
|
1331
|
-
entry.message.el?.classList.add('flash')
|
|
1332
|
-
setTimeout(() => entry.message.el?.classList.remove('flash'), 1200)
|
|
1333
|
-
})
|
|
1768
|
+
item.append(el('div', 'hist-text', session.first || session.title || '(这条对话还没有内容)'))
|
|
1769
|
+
item.addEventListener('click', () => switchToSession(session.sessionId))
|
|
1334
1770
|
historyListEl.append(item)
|
|
1335
1771
|
}
|
|
1336
1772
|
}
|
|
@@ -1386,11 +1822,31 @@
|
|
|
1386
1822
|
const action = currentAction(run)
|
|
1387
1823
|
if (action) {
|
|
1388
1824
|
const line = el('div', 'lane-now')
|
|
1389
|
-
if (!isTerminal(run.status)) line.append(el('span', 'spark'))
|
|
1825
|
+
if (!isTerminal(run.status) && !STATE.cancelRequested.has(run.id)) line.append(el('span', 'spark'))
|
|
1390
1826
|
line.append(el('span', '', action))
|
|
1827
|
+
const tools = runningToolsOf(run)
|
|
1828
|
+
if (tools.length > 0) line.append(el('span', 'lane-tool', ` · 工具 ${tools.join('、')}`))
|
|
1391
1829
|
title.append(line)
|
|
1392
1830
|
}
|
|
1393
1831
|
head.append(title)
|
|
1832
|
+
const clock = el('span', 'lane-time', laneElapsed(run))
|
|
1833
|
+
clock.dataset.started = Number.isFinite(Date.parse(run.createdAt)) ? String(Date.parse(run.createdAt)) : ''
|
|
1834
|
+
clock.dataset.finished = isTerminal(run.status) && Number.isFinite(Date.parse(run.finishedAt))
|
|
1835
|
+
? String(Date.parse(run.finishedAt))
|
|
1836
|
+
: ''
|
|
1837
|
+
if (!clock.textContent) clock.hidden = true
|
|
1838
|
+
head.append(clock)
|
|
1839
|
+
if (!isTerminal(run.status)) {
|
|
1840
|
+
const stopping = STATE.cancelRequested.has(run.id)
|
|
1841
|
+
const stop = el('button', `lane-stop${stopping ? ' stopping' : ''}`, stopping ? '停止中…' : '停止')
|
|
1842
|
+
stop.type = 'button'
|
|
1843
|
+
stop.disabled = stopping
|
|
1844
|
+
stop.addEventListener('click', event => {
|
|
1845
|
+
event.stopPropagation()
|
|
1846
|
+
stopRun(run.id)
|
|
1847
|
+
})
|
|
1848
|
+
head.append(stop)
|
|
1849
|
+
}
|
|
1394
1850
|
head.append(el('span', `status-pill ${statusClass(run.status)}`, laneStatusText(run)))
|
|
1395
1851
|
card.append(head)
|
|
1396
1852
|
|
|
@@ -1576,6 +2032,56 @@
|
|
|
1576
2032
|
return approval.source || '这条对话'
|
|
1577
2033
|
}
|
|
1578
2034
|
|
|
2035
|
+
/** Raw arguments of the blocked call, from the request or the streamed chip. */
|
|
2036
|
+
function approvalArguments(approval) {
|
|
2037
|
+
const candidates = [approval.callArguments]
|
|
2038
|
+
const call = approval.callId ? STATE.toolCalls.get(approval.callId) : null
|
|
2039
|
+
if (call && call.arguments) candidates.push(call.arguments)
|
|
2040
|
+
for (const raw of candidates) {
|
|
2041
|
+
if (typeof raw !== 'string' || !raw.trim()) continue
|
|
2042
|
+
try {
|
|
2043
|
+
return { parsed: JSON.parse(raw), text: raw }
|
|
2044
|
+
} catch {
|
|
2045
|
+
return { parsed: null, text: raw }
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
return null
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
const CALL_PREVIEW_LIMIT = 4000
|
|
2052
|
+
|
|
2053
|
+
/**
|
|
2054
|
+
* Spell out what the blocked call would actually do: a write shows the text
|
|
2055
|
+
* it would put on disk, a shell call shows the full command, everything else
|
|
2056
|
+
* shows the raw arguments.
|
|
2057
|
+
*/
|
|
2058
|
+
function approvalCallPreview(approval) {
|
|
2059
|
+
const args = approvalArguments(approval)
|
|
2060
|
+
if (!args) return null
|
|
2061
|
+
const parsed = args.parsed && typeof args.parsed === 'object' ? args.parsed : null
|
|
2062
|
+
const pick = (...keys) => {
|
|
2063
|
+
if (!parsed) return ''
|
|
2064
|
+
for (const key of keys) {
|
|
2065
|
+
if (typeof parsed[key] === 'string' && parsed[key].trim()) return parsed[key]
|
|
2066
|
+
}
|
|
2067
|
+
return ''
|
|
2068
|
+
}
|
|
2069
|
+
const tool = `${approval.toolName || ''}`.toLowerCase()
|
|
2070
|
+
const clip = value => (value.length > CALL_PREVIEW_LIMIT
|
|
2071
|
+
? { text: `${value.slice(0, CALL_PREVIEW_LIMIT)}\n…`, note: `共 ${value.length} 字符,这里先看前 ${CALL_PREVIEW_LIMIT} 字符。` }
|
|
2072
|
+
: { text: value, note: '' })
|
|
2073
|
+
if (/write|edit|patch|apply|create|append/.test(tool)) {
|
|
2074
|
+
const body = pick('content', 'contents', 'text', 'new_string', 'newText', 'patch', 'diff')
|
|
2075
|
+
if (body) return { label: '要写入的内容', ...clip(body) }
|
|
2076
|
+
}
|
|
2077
|
+
if (/shell|bash|command|exec|terminal|cmd|powershell|run/.test(tool)) {
|
|
2078
|
+
const command = pick('command', 'cmd', 'script', 'input')
|
|
2079
|
+
|| (typeof args.parsed === 'string' ? args.parsed : '')
|
|
2080
|
+
if (command) return { label: '要执行的命令', ...clip(command) }
|
|
2081
|
+
}
|
|
2082
|
+
return { label: '调用参数', ...clip(args.text) }
|
|
2083
|
+
}
|
|
2084
|
+
|
|
1579
2085
|
function approvalCard(approval) {
|
|
1580
2086
|
const card = el('div', 'approval-card')
|
|
1581
2087
|
card.dataset.approvalId = approval.id
|
|
@@ -1588,28 +2094,65 @@
|
|
|
1588
2094
|
if (target) card.append(el('div', 'where', `要动的位置:${target}`))
|
|
1589
2095
|
const reason = approval.displayReason?.zh || approval.reason
|
|
1590
2096
|
card.append(el('p', 'why', reason || '宿主的工具调用被拦住了,需要你明确决定这一次。'))
|
|
2097
|
+
const preview = approvalCallPreview(approval)
|
|
2098
|
+
if (preview) {
|
|
2099
|
+
const details = el('details', 'call')
|
|
2100
|
+
details.append(el('summary', '', preview.label))
|
|
2101
|
+
details.append(el('pre', '', preview.text))
|
|
2102
|
+
if (preview.note) details.append(el('div', 'note', preview.note))
|
|
2103
|
+
card.append(details)
|
|
2104
|
+
}
|
|
2105
|
+
const expiresAt = Date.parse(approval.expiresAt || '')
|
|
2106
|
+
if (Number.isFinite(expiresAt)) {
|
|
2107
|
+
const expiry = el('div', 'expiry')
|
|
2108
|
+
expiry.dataset.expiresAt = String(expiresAt)
|
|
2109
|
+
card.append(expiry)
|
|
2110
|
+
updateApprovalClock(expiry)
|
|
2111
|
+
}
|
|
1591
2112
|
card.append(el('div', 'state', '等待你的决定…'))
|
|
1592
2113
|
const options = el('div', 'options')
|
|
1593
2114
|
const allow = el('button', 'allow', '允许这一次')
|
|
1594
2115
|
allow.type = 'button'
|
|
1595
2116
|
allow.addEventListener('click', () => decideApproval(approval.id, 'allow'))
|
|
2117
|
+
const allowTool = el('button', 'allow-tool', `本会话内允许 ${approval.toolName || '这个工具'}`)
|
|
2118
|
+
allowTool.type = 'button'
|
|
2119
|
+
allowTool.title = '这一轮对话里同一个工具不再逐次询问;换对话或新对话后恢复询问。'
|
|
2120
|
+
allowTool.addEventListener('click', () => decideApproval(approval.id, 'allow-tool'))
|
|
1596
2121
|
const reject = el('button', 'reject', '拒绝')
|
|
1597
2122
|
reject.type = 'button'
|
|
1598
2123
|
reject.addEventListener('click', () => decideApproval(approval.id, 'reject'))
|
|
1599
|
-
options.append(allow, reject)
|
|
2124
|
+
options.append(allow, allowTool, reject)
|
|
1600
2125
|
card.append(options)
|
|
1601
2126
|
return card
|
|
1602
2127
|
}
|
|
1603
2128
|
|
|
2129
|
+
/** Keeping the deadline visible beats letting the ask silently expire. */
|
|
2130
|
+
function updateApprovalClock(node) {
|
|
2131
|
+
const expiresAt = Number(node.dataset.expiresAt)
|
|
2132
|
+
if (!Number.isFinite(expiresAt)) return
|
|
2133
|
+
const left = expiresAt - Date.now()
|
|
2134
|
+
node.classList.toggle('soon', left > 0 && left <= 60_000)
|
|
2135
|
+
node.classList.toggle('over', left <= 0)
|
|
2136
|
+
if (left <= 0) node.textContent = `已超过有效期(${timeLabel(expiresAt)}),宿主会按拒绝处理;再发起会重新询问。`
|
|
2137
|
+
else node.textContent = `剩余有效时间 ${formatClock(left)}(约到 ${timeLabel(expiresAt)})`
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2140
|
+
function updateApprovalClocks() {
|
|
2141
|
+
for (const node of messagesEl.querySelectorAll('.approval-card .expiry')) updateApprovalClock(node)
|
|
2142
|
+
}
|
|
2143
|
+
|
|
1604
2144
|
function settleApprovalNode(entry) {
|
|
1605
2145
|
if (!entry || !entry.node) return
|
|
1606
2146
|
entry.node.querySelector('.options')?.remove()
|
|
1607
2147
|
const state = entry.node.querySelector('.state')
|
|
1608
2148
|
if (!state) return
|
|
1609
2149
|
if (entry.decided === 'allow') state.textContent = '已允许这一次(只对这一条调用生效)'
|
|
2150
|
+
else if (entry.decided === 'allow-tool') state.textContent = `已允许,并在本会话内放行 ${entry.approval?.toolName || '这个工具'}(换对话后恢复询问)`
|
|
1610
2151
|
else if (entry.decided === 'reject') state.textContent = '已拒绝'
|
|
2152
|
+
else if (entry.decided === 'expired') state.textContent = '已过有效期,宿主按拒绝处理'
|
|
1611
2153
|
else if (entry.decided === 'cancelled') state.textContent = '没有回答,按拒绝处理'
|
|
1612
2154
|
else state.textContent = '等待你的决定…'
|
|
2155
|
+
if (entry.decided) entry.node.querySelector('.expiry')?.remove()
|
|
1613
2156
|
}
|
|
1614
2157
|
|
|
1615
2158
|
function seatApproval(approval) {
|
|
@@ -1646,15 +2189,18 @@
|
|
|
1646
2189
|
if (!entry || entry.decided) return
|
|
1647
2190
|
const state = entry.node.querySelector('.state')
|
|
1648
2191
|
for (const button of entry.node.querySelectorAll('button')) button.disabled = true
|
|
1649
|
-
|
|
2192
|
+
const pending = { allow: '正在允许这一次…', 'allow-tool': '正在允许这个工具…', reject: '正在拒绝…' }
|
|
2193
|
+
if (state) state.textContent = pending[decision] || '正在提交…'
|
|
1650
2194
|
try {
|
|
1651
2195
|
await api('/api/console/approval', { method: 'POST', body: { id, decision } })
|
|
1652
2196
|
entry.decided = decision
|
|
1653
2197
|
settleApprovalNode(entry)
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
'
|
|
1657
|
-
|
|
2198
|
+
const done = {
|
|
2199
|
+
allow: '已允许这一次;只对这一条调用生效。',
|
|
2200
|
+
'allow-tool': '已允许:本会话内同一个工具不再询问(换对话后恢复)。',
|
|
2201
|
+
reject: '已拒绝这一条调用。',
|
|
2202
|
+
}
|
|
2203
|
+
pushToast(done[decision] || '已提交。', 'ok')
|
|
1658
2204
|
} catch (error) {
|
|
1659
2205
|
for (const button of entry.node.querySelectorAll('button')) button.disabled = false
|
|
1660
2206
|
if (state) state.textContent = `没有提交成功:${messageOf(error)}`
|
|
@@ -1674,7 +2220,12 @@
|
|
|
1674
2220
|
data.finishedAt ? `结束 ${timeLabel(data.finishedAt)}` : null,
|
|
1675
2221
|
].filter(Boolean)
|
|
1676
2222
|
for (const value of meta) dialogMetaEl.append(el('span', '', value))
|
|
1677
|
-
|
|
2223
|
+
setDialogActions([])
|
|
2224
|
+
if (data.report) renderMarkdownInto(dialogReportEl, data.report)
|
|
2225
|
+
else {
|
|
2226
|
+
dialogReportEl.textContent = ''
|
|
2227
|
+
dialogReportEl.append(el('p', '', '(这个 run 还没有报告)'))
|
|
2228
|
+
}
|
|
1678
2229
|
reportDialogEl.showModal()
|
|
1679
2230
|
} catch (error) {
|
|
1680
2231
|
pushToast(`读取报告失败:${messageOf(error)}`, 'err')
|
|
@@ -1735,12 +2286,16 @@
|
|
|
1735
2286
|
if (frame.action === 'asked' && approval && approval.id) {
|
|
1736
2287
|
seatApproval(approval)
|
|
1737
2288
|
pushToast('有一个工具调用在等你决定。', 'warn')
|
|
2289
|
+
} else if (frame.action === 'auto') {
|
|
2290
|
+
pushToast(`已按本会话的许可放行 ${frame.toolName || '这个工具'},没有再打断你。`, 'ok')
|
|
1738
2291
|
} else if ((frame.action === 'decided' || frame.action === 'expired') && frame.id) {
|
|
1739
2292
|
const entry = STATE.approvals.get(frame.id)
|
|
1740
2293
|
if (entry) {
|
|
1741
2294
|
entry.decided = frame.action === 'expired'
|
|
1742
|
-
? '
|
|
1743
|
-
: frame.
|
|
2295
|
+
? 'expired'
|
|
2296
|
+
: frame.scope === 'tool' ? 'allow-tool'
|
|
2297
|
+
: frame.outcome === 'allowed-once' ? 'allow'
|
|
2298
|
+
: frame.outcome === 'rejected' ? 'reject' : 'cancelled'
|
|
1744
2299
|
settleApprovalNode(entry)
|
|
1745
2300
|
}
|
|
1746
2301
|
}
|
|
@@ -1753,6 +2308,12 @@
|
|
|
1753
2308
|
case 'answered':
|
|
1754
2309
|
pushToast(`已提交 ${frame.attentionId || '决定'}。`, 'ok')
|
|
1755
2310
|
break
|
|
2311
|
+
case 'canceling':
|
|
2312
|
+
if (frame.runId) {
|
|
2313
|
+
STATE.cancelRequested.add(frame.runId)
|
|
2314
|
+
renderLanes()
|
|
2315
|
+
}
|
|
2316
|
+
break
|
|
1756
2317
|
case 'error':
|
|
1757
2318
|
if (frame.code === 'follow-failed' || frame.code === 'state-failed') {
|
|
1758
2319
|
setConn('warn', '流中断')
|
|
@@ -2697,6 +3258,7 @@
|
|
|
2697
3258
|
if (STATE.streamAbort) STATE.streamAbort.abort()
|
|
2698
3259
|
setSession(data.sessionId)
|
|
2699
3260
|
STATE.approvals.clear()
|
|
3261
|
+
STATE.cancelRequested.clear()
|
|
2700
3262
|
clearMessages()
|
|
2701
3263
|
STATE.runs = []
|
|
2702
3264
|
renderLanes()
|
|
@@ -2753,7 +3315,10 @@
|
|
|
2753
3315
|
advancedDrawerEl.hidden = target !== advancedDrawerEl
|
|
2754
3316
|
scrimEl.hidden = false
|
|
2755
3317
|
target.hidden = false
|
|
2756
|
-
if (which === 'history')
|
|
3318
|
+
if (which === 'history') {
|
|
3319
|
+
renderHistory()
|
|
3320
|
+
loadSessions()
|
|
3321
|
+
}
|
|
2757
3322
|
else renderAdvancedRuns()
|
|
2758
3323
|
}
|
|
2759
3324
|
|
|
@@ -2797,6 +3362,11 @@
|
|
|
2797
3362
|
}
|
|
2798
3363
|
|
|
2799
3364
|
newChatEl.addEventListener('click', newConversation)
|
|
3365
|
+
stopAllEl.addEventListener('click', stopAllRuns)
|
|
3366
|
+
setInterval(() => {
|
|
3367
|
+
tickLaneClocks()
|
|
3368
|
+
updateApprovalClocks()
|
|
3369
|
+
}, 1000)
|
|
2800
3370
|
$('historyOpen').addEventListener('click', () => drawerOpen('history'))
|
|
2801
3371
|
$('advancedOpen').addEventListener('click', () => drawerOpen('advanced'))
|
|
2802
3372
|
for (const button of document.querySelectorAll('[data-close]')) {
|