@vincentbanana/bananacli 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/ui-server.js +95 -15
- package/dist/tools/todo.js +5 -2
- package/dist/ui/app.js +567 -106
- package/dist/ui/index.html +5 -5
- package/dist/ui/style.css +87 -6
- package/package.json +1 -1
package/dist/ui/app.js
CHANGED
|
@@ -80,14 +80,26 @@ const $ = (id) => document.getElementById(id);
|
|
|
80
80
|
|
|
81
81
|
// ---------------- Markdown(够用即可) ----------------
|
|
82
82
|
const escapeHtml = (text) =>
|
|
83
|
-
text
|
|
83
|
+
text
|
|
84
|
+
.replace(/&/g, '&')
|
|
85
|
+
.replace(/</g, '<')
|
|
86
|
+
.replace(/>/g, '>')
|
|
87
|
+
// 这些值也会进 href/alt 之类的属性,引号必须一起转义。
|
|
88
|
+
.replace(/"/g, '"')
|
|
89
|
+
.replace(/'/g, ''');
|
|
84
90
|
|
|
85
91
|
function inline(text) {
|
|
86
|
-
|
|
87
|
-
|
|
92
|
+
const codes = [];
|
|
93
|
+
// 先占位行内代码,避免 `a**b**c` 里的 ** 被粗体规则吃掉。
|
|
94
|
+
let value = escapeHtml(text).replace(/`([^`\n]+)`/g, (_, code) => {
|
|
95
|
+
codes.push(code);
|
|
96
|
+
return `\u0000${codes.length - 1}\u0000`;
|
|
97
|
+
});
|
|
98
|
+
value = value
|
|
88
99
|
.replace(/\*\*([^*\n]+)\*\*/g, '<strong>$1</strong>')
|
|
89
100
|
.replace(/(^|[\s(])\*([^*\n]+)\*/g, '$1<em>$2</em>')
|
|
90
101
|
.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)/g, '<a href="$2" target="_blank" rel="noreferrer">$1</a>');
|
|
102
|
+
return value.replace(/\u0000(\d+)\u0000/g, (_, index) => `<code>${codes[Number(index)] ?? ''}</code>`);
|
|
91
103
|
}
|
|
92
104
|
|
|
93
105
|
/**
|
|
@@ -161,6 +173,31 @@ function markdown(source) {
|
|
|
161
173
|
continue;
|
|
162
174
|
}
|
|
163
175
|
if (/^\s*$/.test(line)) { i += 1; continue; }
|
|
176
|
+
// 简单表格:| a | b | + | --- | --- |
|
|
177
|
+
if (line.includes('|') && i + 1 < lines.length && lines[i + 1].includes('|')) {
|
|
178
|
+
const cells = (row) => row.trim().replace(/^\|/, '').replace(/\|$/, '').split('|').map((cell) => cell.trim());
|
|
179
|
+
const separator = cells(lines[i + 1]);
|
|
180
|
+
const isSeparator = separator.length > 0 && separator.every((cell) => /^:?-{2,}:?$/.test(cell));
|
|
181
|
+
if (isSeparator) {
|
|
182
|
+
const header = cells(line);
|
|
183
|
+
const rows = [];
|
|
184
|
+
i += 2;
|
|
185
|
+
while (i < lines.length && lines[i].includes('|') && !/^\s*$/.test(lines[i])) {
|
|
186
|
+
rows.push(cells(lines[i]));
|
|
187
|
+
i += 1;
|
|
188
|
+
}
|
|
189
|
+
out.push(
|
|
190
|
+
'<div class="table-wrap"><table><thead><tr>' +
|
|
191
|
+
header.map((cell) => `<th>${inline(cell)}</th>`).join('') +
|
|
192
|
+
'</tr></thead><tbody>' +
|
|
193
|
+
rows
|
|
194
|
+
.map((row) => `<tr>${header.map((_, index) => `<td>${inline(row[index] ?? '')}</td>`).join('')}</tr>`)
|
|
195
|
+
.join('') +
|
|
196
|
+
'</tbody></table></div>',
|
|
197
|
+
);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
164
201
|
if (/^###\s/.test(line)) { out.push(`<h3>${inline(line.replace(/^###\s/, ''))}</h3>`); i += 1; continue; }
|
|
165
202
|
if (/^##\s/.test(line)) { out.push(`<h2>${inline(line.replace(/^##\s/, ''))}</h2>`); i += 1; continue; }
|
|
166
203
|
if (/^#\s/.test(line)) { out.push(`<h1>${inline(line.replace(/^#\s/, ''))}</h1>`); i += 1; continue; }
|
|
@@ -321,6 +358,8 @@ function cameraTick() {
|
|
|
321
358
|
function setCameraFollow(follow) {
|
|
322
359
|
camera.follow = Boolean(follow);
|
|
323
360
|
state.stickToBottom = camera.follow;
|
|
361
|
+
// 暂停跟随时恢复浏览器滚动锚定,用户在读旧内容时不会被上方新内容推走。
|
|
362
|
+
THREAD.style.overflowAnchor = camera.follow ? 'none' : 'auto';
|
|
324
363
|
if (!camera.follow) {
|
|
325
364
|
stopCamera();
|
|
326
365
|
if (THREAD.querySelector('.msg')) $('jumpLatest').hidden = false;
|
|
@@ -370,6 +409,10 @@ let switching = false;
|
|
|
370
409
|
*/
|
|
371
410
|
async function switchSession(ref) {
|
|
372
411
|
if (switching) return;
|
|
412
|
+
if (state.busy) {
|
|
413
|
+
toast('正在生成,先停止当前任务再切换会话');
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
373
416
|
switching = true;
|
|
374
417
|
document.body.classList.add('switching');
|
|
375
418
|
try {
|
|
@@ -377,9 +420,14 @@ async function switchSession(ref) {
|
|
|
377
420
|
if (result?.info) state.info = result.info;
|
|
378
421
|
if (result?.sessions) state.sessions = result.sessions;
|
|
379
422
|
state.changes = null;
|
|
423
|
+
// 附件属于当前草稿,切会话时不能带到另一个会话里。
|
|
424
|
+
state.attachments = [];
|
|
425
|
+
renderAttachments();
|
|
380
426
|
renderHeader();
|
|
381
427
|
renderSidebar();
|
|
382
428
|
renderThread();
|
|
429
|
+
$('input').value = '';
|
|
430
|
+
grow();
|
|
383
431
|
$('filesTitle').textContent = `项目文件 · ${state.info?.workspace ?? ''}`;
|
|
384
432
|
setActiveTab(state.info?.session?.id ?? null);
|
|
385
433
|
if (state.review.open && state.review.mode === 'changes') void loadChanges(true);
|
|
@@ -393,6 +441,35 @@ async function switchSession(ref) {
|
|
|
393
441
|
}
|
|
394
442
|
}
|
|
395
443
|
|
|
444
|
+
/** 新建会话。命令 /new 和顶部 + 共用同一条路径,避免两套逻辑漂移。 */
|
|
445
|
+
async function createSession(title = '') {
|
|
446
|
+
if (state.busy) {
|
|
447
|
+
toast('正在生成,先停止当前任务再新建对话');
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
try {
|
|
451
|
+
const result = await post('session', { action: 'new', ...(title ? { title } : {}) });
|
|
452
|
+
if (result?.info) state.info = result.info;
|
|
453
|
+
if (result?.sessions) state.sessions = result.sessions;
|
|
454
|
+
state.changes = null;
|
|
455
|
+
state.attachments = [];
|
|
456
|
+
renderAttachments();
|
|
457
|
+
renderHeader();
|
|
458
|
+
renderSidebar();
|
|
459
|
+
renderThread();
|
|
460
|
+
if (state.dock.open) termClose();
|
|
461
|
+
closeSidebarIfOverlay();
|
|
462
|
+
setActiveTab(state.info?.session?.id ?? null);
|
|
463
|
+
$('input').value = '';
|
|
464
|
+
grow();
|
|
465
|
+
$('input').focus();
|
|
466
|
+
return result;
|
|
467
|
+
} catch (error) {
|
|
468
|
+
toast('新建对话失败:' + (error?.message ?? error));
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
396
473
|
// ---------------- 会话标签条 ----------------
|
|
397
474
|
function tabTitleFor(id) {
|
|
398
475
|
if (state.info?.session?.id === id && state.info.session.title) return state.info.session.title;
|
|
@@ -483,6 +560,10 @@ function renderTabs() {
|
|
|
483
560
|
}
|
|
484
561
|
|
|
485
562
|
function closeTab(id) {
|
|
563
|
+
if (state.busy && id === state.activeTab) {
|
|
564
|
+
toast('正在生成,先停止当前任务再关闭标签');
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
486
567
|
const index = state.tabs.findIndex((tab) => tab.id === id);
|
|
487
568
|
if (index < 0) return;
|
|
488
569
|
state.tabs.splice(index, 1);
|
|
@@ -558,18 +639,58 @@ function renderSidebar() {
|
|
|
558
639
|
`<span class="s-title">${escapeHtml(item.title || '新对话')}</span>` +
|
|
559
640
|
`<span class="s-meta">${item.turns} 轮 · ${relativeTime(item.updatedAt)}</span>`;
|
|
560
641
|
|
|
642
|
+
node.tabIndex = 0;
|
|
643
|
+
node.setAttribute('role', 'button');
|
|
561
644
|
node.addEventListener('click', (event) => {
|
|
562
645
|
if (event.target.closest('[data-del], [data-menu]')) return;
|
|
563
646
|
switchSession(item.id);
|
|
564
647
|
});
|
|
648
|
+
node.addEventListener('keydown', (event) => {
|
|
649
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
650
|
+
event.preventDefault();
|
|
651
|
+
switchSession(item.id);
|
|
652
|
+
}
|
|
653
|
+
});
|
|
565
654
|
|
|
566
|
-
node.querySelector('[data-del]').addEventListener('click', (event) => {
|
|
655
|
+
node.querySelector('[data-del]').addEventListener('click', async (event) => {
|
|
567
656
|
event.stopPropagation();
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
657
|
+
if (state.busy) {
|
|
658
|
+
toast('正在生成,先停止当前任务再删除会话');
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
const confirmed = await openAppDialog({
|
|
662
|
+
title: '删除这个会话?',
|
|
663
|
+
body: `「${item.title || '新对话'}」会从本地删除,无法恢复。`,
|
|
664
|
+
confirmLabel: '删除',
|
|
665
|
+
danger: true,
|
|
572
666
|
});
|
|
667
|
+
if (!confirmed) return;
|
|
668
|
+
try {
|
|
669
|
+
const wasActive = item.id === state.info?.session?.id;
|
|
670
|
+
const result = await post('session', { action: 'delete', ref: item.id });
|
|
671
|
+
state.tabs = state.tabs.filter((tab) => tab.id !== item.id);
|
|
672
|
+
if (result?.sessions) state.sessions = result.sessions;
|
|
673
|
+
if (wasActive) {
|
|
674
|
+
state.changes = null;
|
|
675
|
+
if (result?.info) state.info = result.info;
|
|
676
|
+
saveTabs();
|
|
677
|
+
state.attachments = [];
|
|
678
|
+
renderAttachments();
|
|
679
|
+
$('input').value = '';
|
|
680
|
+
grow();
|
|
681
|
+
renderHeader();
|
|
682
|
+
renderSidebar();
|
|
683
|
+
renderThread();
|
|
684
|
+
setActiveTab(state.info?.session?.id ?? null);
|
|
685
|
+
} else {
|
|
686
|
+
saveTabs();
|
|
687
|
+
renderSidebar();
|
|
688
|
+
renderTabs();
|
|
689
|
+
}
|
|
690
|
+
toast('已删除会话');
|
|
691
|
+
} catch (error) {
|
|
692
|
+
toast('删除失败:' + (error?.message ?? error));
|
|
693
|
+
}
|
|
573
694
|
});
|
|
574
695
|
|
|
575
696
|
node.querySelector('[data-menu]').addEventListener('click', (event) => {
|
|
@@ -587,7 +708,7 @@ function openSessionMenu(item, anchor) {
|
|
|
587
708
|
const pop = document.createElement('div');
|
|
588
709
|
pop.className = 'popover';
|
|
589
710
|
pop.innerHTML = `
|
|
590
|
-
<button data-act="switch"
|
|
711
|
+
<button data-act="switch">打开会话</button>
|
|
591
712
|
<button data-act="rename">重命名</button>
|
|
592
713
|
<button data-act="md">导出 Markdown</button>
|
|
593
714
|
<button data-act="json">导出 JSON</button>`;
|
|
@@ -618,20 +739,43 @@ function openSessionMenu(item, anchor) {
|
|
|
618
739
|
if (action === 'switch') {
|
|
619
740
|
await switchSession(item.id);
|
|
620
741
|
} else if (action === 'rename') {
|
|
621
|
-
|
|
622
|
-
if (title && title.trim()) {
|
|
623
|
-
await post('session', { action: 'switch', ref: item.id });
|
|
624
|
-
await post('session', { action: 'rename', title: title.trim() });
|
|
625
|
-
await refresh(false);
|
|
626
|
-
toast('已重命名');
|
|
627
|
-
}
|
|
742
|
+
await renameSession(item);
|
|
628
743
|
} else {
|
|
629
|
-
await
|
|
630
|
-
downloadSession(action === 'json' ? 'json' : 'md', item.id);
|
|
744
|
+
await downloadSession(action === 'json' ? 'json' : 'md', item.id);
|
|
631
745
|
}
|
|
632
746
|
});
|
|
633
747
|
}
|
|
634
748
|
|
|
749
|
+
/** 重命名指定会话。不会像以前那样先切过去,避免打断当前阅读/生成。 */
|
|
750
|
+
async function renameSession(item) {
|
|
751
|
+
if (item.id === state.info?.session?.id && state.busy) {
|
|
752
|
+
toast('正在生成,先停止当前任务再重命名');
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
const title = await openAppDialog({
|
|
756
|
+
title: '重命名会话',
|
|
757
|
+
value: item.title || '',
|
|
758
|
+
placeholder: '输入新的标题',
|
|
759
|
+
confirmLabel: '保存',
|
|
760
|
+
});
|
|
761
|
+
if (title === null) return;
|
|
762
|
+
if (!title) {
|
|
763
|
+
toast('标题不能为空');
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
try {
|
|
767
|
+
const result = await post('session', { action: 'rename', ref: item.id, title });
|
|
768
|
+
if (result?.info) state.info = result.info;
|
|
769
|
+
if (result?.sessions) state.sessions = result.sessions;
|
|
770
|
+
renderHeader();
|
|
771
|
+
renderSidebar();
|
|
772
|
+
renderTabs();
|
|
773
|
+
toast('已重命名');
|
|
774
|
+
} catch (error) {
|
|
775
|
+
toast('重命名失败:' + (error?.message ?? error));
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
635
779
|
function sessionToMarkdown(messages, title) {
|
|
636
780
|
const lines = [`# ${title || '对话'}`, '', `导出时间:${new Date().toLocaleString()}`, ''];
|
|
637
781
|
for (const message of messages) {
|
|
@@ -649,8 +793,19 @@ function sessionToMarkdown(messages, title) {
|
|
|
649
793
|
return lines.join('\n');
|
|
650
794
|
}
|
|
651
795
|
|
|
652
|
-
function downloadSession(format, id) {
|
|
653
|
-
|
|
796
|
+
async function downloadSession(format, id) {
|
|
797
|
+
let session = state.info?.session;
|
|
798
|
+
if (!session) return;
|
|
799
|
+
// 导出别的会话时只读取,不切换当前会话。
|
|
800
|
+
if (id && id !== session.id) {
|
|
801
|
+
try {
|
|
802
|
+
const result = await post('session', { action: 'load', ref: id });
|
|
803
|
+
if (result?.session) session = result.session;
|
|
804
|
+
} catch (error) {
|
|
805
|
+
toast('导出失败:' + (error?.message ?? error));
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
654
809
|
const blob =
|
|
655
810
|
format === 'json'
|
|
656
811
|
? JSON.stringify(session, null, 2)
|
|
@@ -681,16 +836,27 @@ function ensureToolOutput(row, initial = '', isError = false) {
|
|
|
681
836
|
out.classList.toggle('is-error', isError);
|
|
682
837
|
if (initial) out.textContent = initial;
|
|
683
838
|
row.classList.add('is-clickable');
|
|
839
|
+
row.tabIndex = 0;
|
|
840
|
+
row.setAttribute('role', 'button');
|
|
841
|
+
row.setAttribute('aria-expanded', String(!out.hidden));
|
|
684
842
|
row.title = '点击展开命令输出';
|
|
685
843
|
if (!row.dataset.bound) {
|
|
686
844
|
row.dataset.bound = '1';
|
|
687
|
-
|
|
845
|
+
const toggle = () => {
|
|
688
846
|
const box = row.nextElementSibling;
|
|
689
847
|
if (!box?.classList.contains('tool-output')) return;
|
|
690
848
|
const open = box.hidden;
|
|
691
849
|
box.hidden = !open;
|
|
692
850
|
row.classList.toggle('is-open', open);
|
|
851
|
+
row.setAttribute('aria-expanded', String(open));
|
|
693
852
|
row.title = open ? '点击收起命令输出' : '点击展开命令输出';
|
|
853
|
+
};
|
|
854
|
+
row.addEventListener('click', toggle);
|
|
855
|
+
row.addEventListener('keydown', (event) => {
|
|
856
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
857
|
+
event.preventDefault();
|
|
858
|
+
toggle();
|
|
859
|
+
}
|
|
694
860
|
});
|
|
695
861
|
}
|
|
696
862
|
return out;
|
|
@@ -739,9 +905,10 @@ function reasoningDetails(text) {
|
|
|
739
905
|
const details = document.createElement('details');
|
|
740
906
|
details.className = 'think';
|
|
741
907
|
const summary = document.createElement('summary');
|
|
908
|
+
const length = text.length > 999 ? `${(text.length / 1000).toFixed(1)}k 字` : `${text.length} 字`;
|
|
742
909
|
summary.innerHTML =
|
|
743
910
|
'<span class="think-title">思考</span>' +
|
|
744
|
-
`<span class="think-len">${
|
|
911
|
+
`<span class="think-len">${length}</span>`;
|
|
745
912
|
const body = document.createElement('div');
|
|
746
913
|
body.className = 'think-body';
|
|
747
914
|
body.innerHTML = markdown(text);
|
|
@@ -836,7 +1003,7 @@ function renderEmpty() {
|
|
|
836
1003
|
btn.addEventListener('click', () => { $('input').value = btn.textContent; $('input').focus(); grow(); });
|
|
837
1004
|
});
|
|
838
1005
|
const setup = tips.querySelector('#emptySetup');
|
|
839
|
-
if (setup) setup.addEventListener('click', () => openSettings());
|
|
1006
|
+
if (setup) setup.addEventListener('click', () => openSettings('model'));
|
|
840
1007
|
}
|
|
841
1008
|
return wrap;
|
|
842
1009
|
}
|
|
@@ -844,8 +1011,7 @@ function renderEmpty() {
|
|
|
844
1011
|
function renderThread() {
|
|
845
1012
|
stopCamera();
|
|
846
1013
|
THREAD.innerHTML = '';
|
|
847
|
-
|
|
848
|
-
state.stickToBottom = true;
|
|
1014
|
+
setCameraFollow(true);
|
|
849
1015
|
$('jumpLatest').hidden = true;
|
|
850
1016
|
const messages = state.info?.session?.messages ?? [];
|
|
851
1017
|
if (messages.length === 0) THREAD.appendChild(renderEmpty());
|
|
@@ -879,10 +1045,7 @@ function renderTurnMeta() {
|
|
|
879
1045
|
}
|
|
880
1046
|
|
|
881
1047
|
function scrollToEnd(smooth = true, force = false) {
|
|
882
|
-
if (force)
|
|
883
|
-
camera.follow = true;
|
|
884
|
-
state.stickToBottom = true;
|
|
885
|
-
}
|
|
1048
|
+
if (force) setCameraFollow(true);
|
|
886
1049
|
if (!camera.follow) return;
|
|
887
1050
|
if (!smooth || prefersReducedMotion()) {
|
|
888
1051
|
stopCamera();
|
|
@@ -980,9 +1143,12 @@ async function refresh(path) {
|
|
|
980
1143
|
state.info = info;
|
|
981
1144
|
const { sessions } = await get('sessions');
|
|
982
1145
|
state.sessions = sessions;
|
|
983
|
-
|
|
1146
|
+
// 即使标签条被清空,也要把服务端当前会话补回来,否则删掉当前会话后会变成无标签状态。
|
|
1147
|
+
if (state.info?.session?.id) {
|
|
984
1148
|
upsertTab(state.info.session.id, state.info.session.title);
|
|
985
1149
|
state.activeTab = state.info.session.id;
|
|
1150
|
+
} else {
|
|
1151
|
+
state.activeTab = null;
|
|
986
1152
|
}
|
|
987
1153
|
renderHeader();
|
|
988
1154
|
renderSidebar();
|
|
@@ -991,6 +1157,24 @@ async function refresh(path) {
|
|
|
991
1157
|
if (path) renderThread();
|
|
992
1158
|
}
|
|
993
1159
|
|
|
1160
|
+
/** 首轮结束后异步生成标题,不再阻塞 done/用时统计。 */
|
|
1161
|
+
async function requestAutoTitle(sessionId) {
|
|
1162
|
+
if (!sessionId) return;
|
|
1163
|
+
try {
|
|
1164
|
+
const result = await post('title', { session: sessionId });
|
|
1165
|
+
if (!result?.title) return;
|
|
1166
|
+
if (result.sessions) state.sessions = result.sessions;
|
|
1167
|
+
if (state.info?.session?.id === sessionId) {
|
|
1168
|
+
state.info.session.title = result.title;
|
|
1169
|
+
renderHeader();
|
|
1170
|
+
}
|
|
1171
|
+
renderTabs();
|
|
1172
|
+
renderSidebar();
|
|
1173
|
+
} catch {
|
|
1174
|
+
// 标题失败不影响对话;保留首条消息截断的标题即可。
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
|
|
994
1178
|
/** 设置面板里的改动要立刻反映在面板上,所以改完重画面板内容。 */
|
|
995
1179
|
async function refreshSettings() {
|
|
996
1180
|
await refresh(false);
|
|
@@ -1011,13 +1195,94 @@ function hideAnimated(element, className = 'is-closing', ms = 180) {
|
|
|
1011
1195
|
});
|
|
1012
1196
|
}
|
|
1013
1197
|
|
|
1198
|
+
let appDialog = null;
|
|
1199
|
+
/**
|
|
1200
|
+
* 轻量自定义对话框。Electron 不实现 window.prompt,重命名/删除不能依赖原生弹窗。
|
|
1201
|
+
* 返回值:有输入框时是字符串或 null;纯确认时是 true 或 null。
|
|
1202
|
+
*/
|
|
1203
|
+
function openAppDialog({ title, body = '', value = null, placeholder = '', confirmLabel = '确定', danger = false }) {
|
|
1204
|
+
return new Promise((resolve) => {
|
|
1205
|
+
appDialog?.remove();
|
|
1206
|
+
const wrap = document.createElement('div');
|
|
1207
|
+
wrap.className = 'modal';
|
|
1208
|
+
wrap.id = 'appDialog';
|
|
1209
|
+
const card = document.createElement('div');
|
|
1210
|
+
card.className = 'modal-card dialog-card';
|
|
1211
|
+
card.setAttribute('role', 'dialog');
|
|
1212
|
+
card.setAttribute('aria-modal', 'true');
|
|
1213
|
+
const heading = document.createElement('div');
|
|
1214
|
+
heading.className = 'modal-head';
|
|
1215
|
+
heading.textContent = title;
|
|
1216
|
+
const copy = document.createElement('p');
|
|
1217
|
+
copy.className = 'dialog-text';
|
|
1218
|
+
copy.textContent = body;
|
|
1219
|
+
copy.hidden = !body;
|
|
1220
|
+
const input = document.createElement('input');
|
|
1221
|
+
input.className = 'dialog-input';
|
|
1222
|
+
input.type = 'text';
|
|
1223
|
+
input.placeholder = placeholder;
|
|
1224
|
+
input.value = value ?? '';
|
|
1225
|
+
input.hidden = value === null;
|
|
1226
|
+
const actions = document.createElement('div');
|
|
1227
|
+
actions.className = 'modal-actions';
|
|
1228
|
+
const cancel = document.createElement('button');
|
|
1229
|
+
cancel.type = 'button';
|
|
1230
|
+
cancel.className = 'btn';
|
|
1231
|
+
cancel.textContent = '取消';
|
|
1232
|
+
const ok = document.createElement('button');
|
|
1233
|
+
ok.type = 'button';
|
|
1234
|
+
ok.className = `btn ${danger ? 'danger' : 'primary'}`;
|
|
1235
|
+
ok.textContent = confirmLabel;
|
|
1236
|
+
actions.append(cancel, ok);
|
|
1237
|
+
card.append(heading, copy, input, actions);
|
|
1238
|
+
wrap.appendChild(card);
|
|
1239
|
+
document.body.appendChild(wrap);
|
|
1240
|
+
syncOverlayState();
|
|
1241
|
+
|
|
1242
|
+
const finish = (result) => {
|
|
1243
|
+
wrap.remove();
|
|
1244
|
+
document.removeEventListener('keydown', onKey, true);
|
|
1245
|
+
if (appDialog === wrap) appDialog = null;
|
|
1246
|
+
syncOverlayState();
|
|
1247
|
+
resolve(result);
|
|
1248
|
+
};
|
|
1249
|
+
const confirm = () => finish(value === null ? true : input.value.trim());
|
|
1250
|
+
const onKey = (event) => {
|
|
1251
|
+
if (event.key === 'Escape') {
|
|
1252
|
+
event.preventDefault();
|
|
1253
|
+
event.stopPropagation();
|
|
1254
|
+
finish(null);
|
|
1255
|
+
} else if (event.key === 'Enter') {
|
|
1256
|
+
event.preventDefault();
|
|
1257
|
+
confirm();
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
1260
|
+
appDialog = wrap;
|
|
1261
|
+
cancel.addEventListener('click', () => finish(null));
|
|
1262
|
+
ok.addEventListener('click', confirm);
|
|
1263
|
+
wrap.addEventListener('mousedown', (event) => {
|
|
1264
|
+
if (event.target === wrap) finish(null);
|
|
1265
|
+
});
|
|
1266
|
+
document.addEventListener('keydown', onKey, true);
|
|
1267
|
+
requestAnimationFrame(() => {
|
|
1268
|
+
if (!input.hidden) {
|
|
1269
|
+
input.focus();
|
|
1270
|
+
input.select();
|
|
1271
|
+
} else {
|
|
1272
|
+
ok.focus();
|
|
1273
|
+
}
|
|
1274
|
+
});
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1014
1278
|
function syncOverlayState() {
|
|
1015
1279
|
const open =
|
|
1016
1280
|
!$('commandCenter').hidden ||
|
|
1017
1281
|
!$('codeViewer').hidden ||
|
|
1018
1282
|
!$('attachmentViewer').hidden ||
|
|
1019
1283
|
!$('settingsSheet').hidden ||
|
|
1020
|
-
!$('approvalModal').hidden
|
|
1284
|
+
!$('approvalModal').hidden ||
|
|
1285
|
+
Boolean($('appDialog'));
|
|
1021
1286
|
document.body.classList.toggle('has-overlay', open);
|
|
1022
1287
|
}
|
|
1023
1288
|
|
|
@@ -1135,6 +1400,7 @@ const REASONING_HINTS = {
|
|
|
1135
1400
|
};
|
|
1136
1401
|
|
|
1137
1402
|
function formatFileSize(bytes) {
|
|
1403
|
+
if (bytes === 0) return '0 B';
|
|
1138
1404
|
if (!bytes) return '';
|
|
1139
1405
|
if (bytes < 1024) return `${bytes} B`;
|
|
1140
1406
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
@@ -1374,6 +1640,8 @@ function onReasoningOutside(event) {
|
|
|
1374
1640
|
function onReasoningKey(event) {
|
|
1375
1641
|
if (event.key === 'Escape') {
|
|
1376
1642
|
event.preventDefault();
|
|
1643
|
+
// capture 阶段就消费掉,否则事件继续冒泡到全局 Esc,会把正在生成的 turn 停掉。
|
|
1644
|
+
event.stopPropagation();
|
|
1377
1645
|
closeReasoningPop();
|
|
1378
1646
|
$('reasoningBtn')?.focus();
|
|
1379
1647
|
}
|
|
@@ -1450,6 +1718,8 @@ function grow() {
|
|
|
1450
1718
|
input.style.height = Math.min(input.scrollHeight, 220) + 'px';
|
|
1451
1719
|
const send = $('sendBtn');
|
|
1452
1720
|
if (!state.busy) send.disabled = input.value.trim() === '';
|
|
1721
|
+
// composer 长高会把最后几行挤出视口;跟随状态下重新贴底。
|
|
1722
|
+
if (camera.follow) requestAnimationFrame(() => scrollToEnd());
|
|
1453
1723
|
}
|
|
1454
1724
|
|
|
1455
1725
|
function stopTurn() {
|
|
@@ -1464,6 +1734,8 @@ function setBusy(busy) {
|
|
|
1464
1734
|
const send = $('sendBtn');
|
|
1465
1735
|
const composer = $('composer');
|
|
1466
1736
|
composer?.classList.toggle('is-busy', busy);
|
|
1737
|
+
composer?.setAttribute('aria-busy', String(busy));
|
|
1738
|
+
THREAD.setAttribute('aria-busy', String(busy));
|
|
1467
1739
|
// 发送键在原位切换成停止键,不再让用户去右上角找
|
|
1468
1740
|
send.innerHTML = busy ? svg('stop') : svg('send');
|
|
1469
1741
|
send.title = busy ? '停止生成' : '发送';
|
|
@@ -1499,9 +1771,9 @@ async function send() {
|
|
|
1499
1771
|
const text = input.value.trim();
|
|
1500
1772
|
if (!text || state.busy) return;
|
|
1501
1773
|
if (!state.info?.session) return;
|
|
1774
|
+
const runSessionId = state.info.session.id;
|
|
1502
1775
|
stopCamera();
|
|
1503
|
-
|
|
1504
|
-
state.stickToBottom = true;
|
|
1776
|
+
setCameraFollow(true);
|
|
1505
1777
|
input.value = '';
|
|
1506
1778
|
grow();
|
|
1507
1779
|
closePalette();
|
|
@@ -1557,26 +1829,32 @@ async function send() {
|
|
|
1557
1829
|
workBody.className = 'tool-group-body';
|
|
1558
1830
|
work.append(workSummary, workBody);
|
|
1559
1831
|
work.classList.add('is-running');
|
|
1832
|
+
// 工具默认只显示一行活动摘要;用户主动展开或失败时才看详情,避免面板反复撑高再收起。
|
|
1833
|
+
workSummary.addEventListener('click', () => {
|
|
1834
|
+
work.dataset.user = '1';
|
|
1835
|
+
});
|
|
1560
1836
|
|
|
1561
|
-
//
|
|
1837
|
+
// 思考过程默认折叠成一行实时摘要:用户点开才看全文,避免收起时整页跳动。
|
|
1562
1838
|
const reasoning = document.createElement('details');
|
|
1563
1839
|
reasoning.className = 'think is-live';
|
|
1564
1840
|
reasoning.hidden = true;
|
|
1565
|
-
reasoning.open =
|
|
1841
|
+
reasoning.open = false;
|
|
1566
1842
|
const reasonSummary = document.createElement('summary');
|
|
1567
|
-
reasonSummary.innerHTML =
|
|
1843
|
+
reasonSummary.innerHTML =
|
|
1844
|
+
'<span class="think-title">思考中…</span><span class="think-peek"></span><span class="think-len"></span>';
|
|
1568
1845
|
const reasonBody = document.createElement('div');
|
|
1569
1846
|
reasonBody.className = 'think-body';
|
|
1570
1847
|
const reasoningStream = createMarkdownStream(reasonBody);
|
|
1571
1848
|
reasoning.append(reasonSummary, reasonBody);
|
|
1572
|
-
//
|
|
1849
|
+
// 用户手动展开后,后续流式内容不要再强行收起。
|
|
1573
1850
|
reasonSummary.addEventListener('click', () => {
|
|
1574
1851
|
reasoning.dataset.user = '1';
|
|
1575
1852
|
});
|
|
1576
1853
|
|
|
1577
1854
|
const body = document.createElement('div');
|
|
1578
1855
|
body.className = 'msg-body caret';
|
|
1579
|
-
|
|
1856
|
+
// 流式内容每几十毫秒重绘一次,aria-live 会反复播报;完成时由状态区统一提示。
|
|
1857
|
+
body.setAttribute('aria-live', 'off');
|
|
1580
1858
|
const answerStream = createMarkdownStream(body);
|
|
1581
1859
|
const meta = document.createElement('div');
|
|
1582
1860
|
meta.className = 'turn-meta';
|
|
@@ -1603,11 +1881,22 @@ async function send() {
|
|
|
1603
1881
|
else reasoningStream.render(text);
|
|
1604
1882
|
const len = reasonSummary.querySelector('.think-len');
|
|
1605
1883
|
if (len) len.textContent = text.length > 999 ? `${(text.length / 1000).toFixed(1)}k 字` : `${text.length} 字`;
|
|
1884
|
+
const peek = reasonSummary.querySelector('.think-peek');
|
|
1885
|
+
if (peek) {
|
|
1886
|
+
const line = text
|
|
1887
|
+
.split(/\r?\n/)
|
|
1888
|
+
.map((item) => item.replace(/^[#>*\-\s]+/, '').trim())
|
|
1889
|
+
.filter(Boolean)
|
|
1890
|
+
.pop();
|
|
1891
|
+
peek.textContent = final ? '' : line ? `· ${line.slice(0, 80)}` : '';
|
|
1892
|
+
}
|
|
1606
1893
|
if (final) reasoning.classList.remove('is-live');
|
|
1607
|
-
//
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1894
|
+
// 只有用户展开并没有上滚时才跟随最新思考
|
|
1895
|
+
if (reasoning.open) {
|
|
1896
|
+
const nearBottom = reasonBody.scrollHeight - reasonBody.scrollTop - reasonBody.clientHeight < 48;
|
|
1897
|
+
if (nearBottom) reasonBody.scrollTop = reasonBody.scrollHeight;
|
|
1898
|
+
}
|
|
1899
|
+
scrollToEnd();
|
|
1611
1900
|
};
|
|
1612
1901
|
const scheduleReasoning = () => {
|
|
1613
1902
|
if (!reasonPending) {
|
|
@@ -1650,7 +1939,7 @@ async function send() {
|
|
|
1650
1939
|
toolCount === 1 ? '完成 1 个操作' : `完成 ${toolCount} 个操作`;
|
|
1651
1940
|
workSummary.querySelector('.wg-state').textContent = `${seconds}s${toolErrors ? ' · 有失败' : ''}`;
|
|
1652
1941
|
work.classList.toggle('has-error', toolErrors);
|
|
1653
|
-
if (!toolErrors) work.open = false;
|
|
1942
|
+
if (!toolErrors && work.dataset.user !== '1') work.open = false;
|
|
1654
1943
|
};
|
|
1655
1944
|
|
|
1656
1945
|
state.attachments = [];
|
|
@@ -1673,7 +1962,7 @@ async function send() {
|
|
|
1673
1962
|
answerStream.render(buffer);
|
|
1674
1963
|
// 流式过程中保持光标
|
|
1675
1964
|
if (!streamEnded) body.classList.add('caret');
|
|
1676
|
-
scrollToEnd(
|
|
1965
|
+
scrollToEnd();
|
|
1677
1966
|
};
|
|
1678
1967
|
const schedule = () => {
|
|
1679
1968
|
if (!pending) pending = requestAnimationFrame(paint);
|
|
@@ -1698,7 +1987,7 @@ async function send() {
|
|
|
1698
1987
|
message: text,
|
|
1699
1988
|
images,
|
|
1700
1989
|
attachments: files,
|
|
1701
|
-
session:
|
|
1990
|
+
session: runSessionId,
|
|
1702
1991
|
}),
|
|
1703
1992
|
signal: controller.signal,
|
|
1704
1993
|
});
|
|
@@ -1726,23 +2015,23 @@ async function send() {
|
|
|
1726
2015
|
buffer += event.chunk;
|
|
1727
2016
|
schedule();
|
|
1728
2017
|
} else if (event.type === 'reasoning') {
|
|
1729
|
-
if (
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
2018
|
+
if (state.info?.showReasoning !== false) {
|
|
2019
|
+
if (!reasoningStarted) {
|
|
2020
|
+
reasoningStarted = Date.now();
|
|
2021
|
+
reasoning.hidden = false;
|
|
2022
|
+
// 默认只显示一行实时摘要,点开才看全文。
|
|
2023
|
+
reasoning.open = reasoning.dataset.user === '1';
|
|
2024
|
+
}
|
|
2025
|
+
reasoningText += event.chunk;
|
|
2026
|
+
scheduleReasoning();
|
|
1733
2027
|
}
|
|
1734
|
-
reasoning.hidden = false;
|
|
1735
|
-
if (reasoning.dataset.user !== '1') reasoning.open = true;
|
|
1736
|
-
reasoningText += event.chunk;
|
|
1737
2028
|
setWorking('正在思考…');
|
|
1738
2029
|
setStatus('思考中…');
|
|
1739
2030
|
working.hidden = false;
|
|
1740
|
-
scheduleReasoning();
|
|
1741
2031
|
} else if (event.type === 'tool_start') {
|
|
1742
2032
|
if (!toolStarted) toolStarted = Date.now();
|
|
1743
2033
|
toolCount += 1;
|
|
1744
2034
|
work.hidden = false;
|
|
1745
|
-
work.open = true;
|
|
1746
2035
|
workSummary.querySelector('.wg-title').textContent =
|
|
1747
2036
|
toolCount === 1 ? '正在执行 1 个操作' : `正在执行 ${toolCount} 个操作`;
|
|
1748
2037
|
workSummary.querySelector('.wg-state').textContent = '';
|
|
@@ -1770,22 +2059,17 @@ async function send() {
|
|
|
1770
2059
|
setWorking(`正在${toolLabel(event.name)}…`);
|
|
1771
2060
|
setStatus(`正在${toolLabel(event.name)}…`);
|
|
1772
2061
|
working.hidden = false;
|
|
1773
|
-
scrollToEnd(
|
|
2062
|
+
scrollToEnd();
|
|
1774
2063
|
} else if (event.type === 'tool_output') {
|
|
1775
2064
|
const row = toolRows.get(event.id);
|
|
1776
2065
|
if (row) {
|
|
1777
2066
|
row._out = `${row._out ?? ''}${event.chunk}`;
|
|
1778
2067
|
if (row._out.length > 12000) row._out = row._out.slice(-12000);
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
out.hidden = false;
|
|
1782
|
-
row.classList.add('is-open');
|
|
1783
|
-
}
|
|
1784
|
-
out.scrollTop = out.scrollHeight;
|
|
2068
|
+
// 实时输出先缓存在隐藏面板里,默认不把页面撑到 300px 高。
|
|
2069
|
+
ensureToolOutput(row, row._out);
|
|
1785
2070
|
const state = row.querySelector('.t-state');
|
|
1786
2071
|
if (state && !state.querySelector('.spin')) state.innerHTML = '<span class="spin"></span>';
|
|
1787
2072
|
setStatus(`${row.querySelector('.t-name')?.textContent ?? '命令'} · 正在输出…`);
|
|
1788
|
-
scrollToEnd(false);
|
|
1789
2073
|
}
|
|
1790
2074
|
} else if (event.type === 'tool_end') {
|
|
1791
2075
|
const row = toolRows.get(event.id) ?? [...toolRows.values()].pop();
|
|
@@ -1811,7 +2095,7 @@ async function send() {
|
|
|
1811
2095
|
} else {
|
|
1812
2096
|
setWorking('正在整理结果…');
|
|
1813
2097
|
}
|
|
1814
|
-
scrollToEnd(
|
|
2098
|
+
scrollToEnd();
|
|
1815
2099
|
}
|
|
1816
2100
|
} else if (event.type === 'step') {
|
|
1817
2101
|
setStatus('正在处理…');
|
|
@@ -1826,7 +2110,7 @@ async function send() {
|
|
|
1826
2110
|
} else if (event.type === 'todos') {
|
|
1827
2111
|
if (state.info?.session) state.info.session.todos = event.todos ?? [];
|
|
1828
2112
|
renderTodos();
|
|
1829
|
-
scrollToEnd(
|
|
2113
|
+
scrollToEnd();
|
|
1830
2114
|
} else if (event.type === 'approval') {
|
|
1831
2115
|
const allowed = await askApproval(event);
|
|
1832
2116
|
await post('approve', { id: event.id, allowed });
|
|
@@ -1857,17 +2141,28 @@ async function send() {
|
|
|
1857
2141
|
metaLine('用时', (event.ms / 1000).toFixed(1) + 's');
|
|
1858
2142
|
if (event.steps > 0) metaLine('过程', `${event.steps} 步`);
|
|
1859
2143
|
if (event.cost && event.cost !== '≈$0') metaLine('花费', event.cost);
|
|
1860
|
-
|
|
1861
|
-
if (event.
|
|
1862
|
-
|
|
2144
|
+
if (event.stopReason === 'max_steps') metaLine('状态', '达到步数上限');
|
|
2145
|
+
else if (event.stopReason === 'aborted') metaLine('状态', '已停止');
|
|
2146
|
+
// 防御性校验:只有这一轮仍属于当前会话时才更新标题/正文状态。
|
|
2147
|
+
const stillCurrent = state.info?.session?.id === runSessionId;
|
|
2148
|
+
if (stillCurrent && event.info) {
|
|
2149
|
+
state.info = event.info;
|
|
2150
|
+
if (event.todos) state.info.session.todos = event.todos;
|
|
2151
|
+
if (event.plan) state.info.session.plan = event.plan;
|
|
2152
|
+
}
|
|
1863
2153
|
const { sessions } = await get('sessions');
|
|
1864
2154
|
state.sessions = sessions;
|
|
1865
2155
|
renderSidebar();
|
|
1866
2156
|
renderTabs();
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
2157
|
+
if (stillCurrent) {
|
|
2158
|
+
renderHeader();
|
|
2159
|
+
renderTodos();
|
|
2160
|
+
if (event.plan?.status === 'pending') renderPendingPlan(event.plan);
|
|
2161
|
+
if (state.review.open && state.review.mode === 'changes') void loadChanges(true);
|
|
2162
|
+
}
|
|
2163
|
+
if (event.firstTurn && event.titleSource !== 'user' && event.stopReason === 'done') {
|
|
2164
|
+
setTimeout(() => void requestAutoTitle(runSessionId), 200);
|
|
2165
|
+
}
|
|
1871
2166
|
}
|
|
1872
2167
|
}
|
|
1873
2168
|
}
|
|
@@ -1878,7 +2173,7 @@ async function send() {
|
|
|
1878
2173
|
finishWork();
|
|
1879
2174
|
body.classList.remove('caret');
|
|
1880
2175
|
if (!buffer.trim() && workBody.childElementCount === 0) {
|
|
1881
|
-
answerStream.setError(markdown('
|
|
2176
|
+
answerStream.setError(markdown('这一轮没有返回内容。可以重试,或按发送键停止后换个说法。'));
|
|
1882
2177
|
}
|
|
1883
2178
|
await refresh();
|
|
1884
2179
|
}
|
|
@@ -2079,9 +2374,111 @@ async function requestFileComplete(value) {
|
|
|
2079
2374
|
}, 90);
|
|
2080
2375
|
}
|
|
2081
2376
|
|
|
2082
|
-
|
|
2377
|
+
// GUI 已经有对应界面的命令才放出来;其余命令由 runCommand 明确提示,避免“显示成功但没生效”。
|
|
2378
|
+
const GUI_COMMANDS = new Set([
|
|
2379
|
+
'help', 'h', '?', 'new', 'list', 'ls', 'use', 'switch', 'resume', 'rename',
|
|
2380
|
+
'session', 'history', 'provider', 'model', 'perm', 'system', 'cwd', 'undo', 'save', 'clear',
|
|
2381
|
+
]);
|
|
2382
|
+
|
|
2383
|
+
function leaveHomeMode() {
|
|
2384
|
+
THREAD.querySelector('.empty')?.remove();
|
|
2385
|
+
$('chatCard').classList.remove('home-mode');
|
|
2386
|
+
$('homeTips').hidden = true;
|
|
2387
|
+
}
|
|
2388
|
+
|
|
2389
|
+
async function clearCurrentSession() {
|
|
2390
|
+
if (state.busy) {
|
|
2391
|
+
toast('正在生成,先停止当前任务再清空');
|
|
2392
|
+
return;
|
|
2393
|
+
}
|
|
2394
|
+
const confirmed = await openAppDialog({
|
|
2395
|
+
title: '清空当前对话?',
|
|
2396
|
+
body: '消息、任务清单和计划都会被清空,这个操作无法撤回。',
|
|
2397
|
+
confirmLabel: '清空',
|
|
2398
|
+
danger: true,
|
|
2399
|
+
});
|
|
2400
|
+
if (!confirmed) return;
|
|
2401
|
+
try {
|
|
2402
|
+
const result = await post('session', { action: 'clear' });
|
|
2403
|
+
if (result?.info) state.info = result.info;
|
|
2404
|
+
if (result?.sessions) state.sessions = result.sessions;
|
|
2405
|
+
state.changes = null;
|
|
2406
|
+
renderHeader();
|
|
2407
|
+
renderSidebar();
|
|
2408
|
+
renderThread();
|
|
2409
|
+
renderTodos();
|
|
2410
|
+
setActiveTab(state.info?.session?.id ?? null);
|
|
2411
|
+
toast('已清空当前对话');
|
|
2412
|
+
} catch (error) {
|
|
2413
|
+
toast('清空失败:' + (error?.message ?? error));
|
|
2414
|
+
}
|
|
2415
|
+
}
|
|
2416
|
+
|
|
2417
|
+
async function renameCurrentSession(title) {
|
|
2418
|
+
const session = state.info?.session;
|
|
2419
|
+
if (!session) return;
|
|
2420
|
+
if (state.busy) {
|
|
2421
|
+
toast('正在生成,先停止当前任务再重命名');
|
|
2422
|
+
return;
|
|
2423
|
+
}
|
|
2424
|
+
let value = String(title ?? '').trim();
|
|
2425
|
+
if (!value) {
|
|
2426
|
+
const input = await openAppDialog({
|
|
2427
|
+
title: '重命名会话',
|
|
2428
|
+
value: session.title || '',
|
|
2429
|
+
placeholder: '输入新的标题',
|
|
2430
|
+
confirmLabel: '保存',
|
|
2431
|
+
});
|
|
2432
|
+
if (input === null) return;
|
|
2433
|
+
value = input;
|
|
2434
|
+
}
|
|
2435
|
+
if (!value) {
|
|
2436
|
+
toast('标题不能为空');
|
|
2437
|
+
return;
|
|
2438
|
+
}
|
|
2439
|
+
try {
|
|
2440
|
+
const result = await post('session', { action: 'rename', ref: session.id, title: value });
|
|
2441
|
+
if (result?.info) state.info = result.info;
|
|
2442
|
+
if (result?.sessions) state.sessions = result.sessions;
|
|
2443
|
+
renderHeader();
|
|
2444
|
+
renderSidebar();
|
|
2445
|
+
renderTabs();
|
|
2446
|
+
toast('已重命名');
|
|
2447
|
+
} catch (error) {
|
|
2448
|
+
toast('重命名失败:' + (error?.message ?? error));
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
/** 执行命令:会话类命令直接调用界面的同一套逻辑,避免前后端状态脱节。 */
|
|
2083
2453
|
async function runCommand(text) {
|
|
2084
2454
|
closePalette();
|
|
2455
|
+
const raw = text.replace(/^\//, '').trim();
|
|
2456
|
+
const [name = '', ...rest] = raw.split(/\s+/);
|
|
2457
|
+
const arg = rest.join(' ').trim();
|
|
2458
|
+
|
|
2459
|
+
if (name === 'new') {
|
|
2460
|
+
await createSession(arg);
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2463
|
+
if (name === 'use' || name === 'switch' || name === 'resume') {
|
|
2464
|
+
if (!arg) return toast('用法:/use <序号、id 前缀或标题>');
|
|
2465
|
+
await switchSession(arg);
|
|
2466
|
+
return;
|
|
2467
|
+
}
|
|
2468
|
+
if (name === 'clear') {
|
|
2469
|
+
await clearCurrentSession();
|
|
2470
|
+
return;
|
|
2471
|
+
}
|
|
2472
|
+
if (name === 'rename') {
|
|
2473
|
+
await renameCurrentSession(arg);
|
|
2474
|
+
return;
|
|
2475
|
+
}
|
|
2476
|
+
if (!GUI_COMMANDS.has(name)) {
|
|
2477
|
+
toast(`GUI 里请直接用界面操作;/${name} 在这里不会生效`);
|
|
2478
|
+
return;
|
|
2479
|
+
}
|
|
2480
|
+
|
|
2481
|
+
leaveHomeMode();
|
|
2085
2482
|
const userMsg = document.createElement('article');
|
|
2086
2483
|
userMsg.className = 'msg user cmd';
|
|
2087
2484
|
userMsg.innerHTML = '<div class="msg-role">你</div>';
|
|
@@ -2089,8 +2486,12 @@ async function runCommand(text) {
|
|
|
2089
2486
|
userBody.className = 'msg-body';
|
|
2090
2487
|
userBody.textContent = text;
|
|
2091
2488
|
userMsg.appendChild(userBody);
|
|
2489
|
+
if (!THREAD.querySelector('.thread-divider')) {
|
|
2490
|
+
const divider = document.createElement('div');
|
|
2491
|
+
divider.className = 'thread-divider';
|
|
2492
|
+
THREAD.appendChild(divider);
|
|
2493
|
+
}
|
|
2092
2494
|
THREAD.appendChild(userMsg);
|
|
2093
|
-
updateHomeMode();
|
|
2094
2495
|
|
|
2095
2496
|
const botMsg = document.createElement('article');
|
|
2096
2497
|
botMsg.className = 'msg assistant enter';
|
|
@@ -2106,15 +2507,26 @@ async function runCommand(text) {
|
|
|
2106
2507
|
|
|
2107
2508
|
try {
|
|
2108
2509
|
const result = await post('command', { input: text });
|
|
2109
|
-
|
|
2510
|
+
// CLI 在 TTY 下会输出 ANSI 颜色;GUI 里统一去掉,避免出现 [36m 之类的乱码。
|
|
2511
|
+
const cleaned = String(result.output ?? '').replace(/\u001b\[[0-9;?]*[ -/]*[@-~]/g, '');
|
|
2512
|
+
out.textContent = cleaned || (result.ok ? '(没有输出)' : '');
|
|
2110
2513
|
out.classList.toggle('is-error', !result.ok);
|
|
2111
2514
|
if (result.error) out.textContent += (out.textContent ? '\n' : '') + result.error;
|
|
2515
|
+
if ((name === 'help' || name === 'h' || name === '?') && !result.error) {
|
|
2516
|
+
out.textContent += '\nGUI 提示:/tools /compact /cls /exit /image /delete /setup 已隐藏,请用界面里的对应功能。';
|
|
2517
|
+
}
|
|
2518
|
+
const beforeId = state.info?.session?.id;
|
|
2112
2519
|
if (result.info) state.info = result.info;
|
|
2113
2520
|
if (result.sessions) state.sessions = result.sessions;
|
|
2521
|
+
if (result.info && beforeId !== state.info.session.id) {
|
|
2522
|
+
renderThread();
|
|
2523
|
+
setActiveTab(state.info.session.id);
|
|
2524
|
+
}
|
|
2114
2525
|
// \u001b[clear 之类的终端指令:清空当前视图
|
|
2115
2526
|
if ((result.output || '').includes('\u001b[clear]')) renderThread();
|
|
2116
2527
|
renderHeader();
|
|
2117
2528
|
renderSidebar();
|
|
2529
|
+
renderTodos();
|
|
2118
2530
|
} catch (error) {
|
|
2119
2531
|
out.textContent = error.message;
|
|
2120
2532
|
out.classList.add('is-error');
|
|
@@ -2191,6 +2603,7 @@ function onModelPopOutside(event) {
|
|
|
2191
2603
|
function onModelPopKey(event) {
|
|
2192
2604
|
if (event.key === 'Escape') {
|
|
2193
2605
|
event.preventDefault();
|
|
2606
|
+
event.stopPropagation();
|
|
2194
2607
|
closeModelPop();
|
|
2195
2608
|
$('modelBtn')?.focus();
|
|
2196
2609
|
}
|
|
@@ -2299,7 +2712,7 @@ function openSettings(category) {
|
|
|
2299
2712
|
if (category) state.settingsCategory = category;
|
|
2300
2713
|
state.settingsScroll = 0;
|
|
2301
2714
|
state.settingsReturn = document.activeElement;
|
|
2302
|
-
renderSettings();
|
|
2715
|
+
renderSettings({ resetScroll: true });
|
|
2303
2716
|
const sheet = $('settingsSheet');
|
|
2304
2717
|
sheet.classList.remove('is-closing');
|
|
2305
2718
|
sheet.hidden = false;
|
|
@@ -2434,12 +2847,13 @@ async function saveSetting(action, payload) {
|
|
|
2434
2847
|
}
|
|
2435
2848
|
}
|
|
2436
2849
|
|
|
2437
|
-
function renderSettings() {
|
|
2850
|
+
function renderSettings({ resetScroll = false } = {}) {
|
|
2438
2851
|
const info = state.info;
|
|
2439
2852
|
if (!info) return;
|
|
2440
2853
|
const body = $('settingsBody');
|
|
2441
2854
|
const nav = $('settingsNav');
|
|
2442
|
-
|
|
2855
|
+
// 切换分类必须回到顶部;保存设置时保留当前位置。
|
|
2856
|
+
const scroll = resetScroll ? 0 : body.scrollTop || state.settingsScroll || 0;
|
|
2443
2857
|
nav.innerHTML = '';
|
|
2444
2858
|
for (const item of SETTINGS_NAV) {
|
|
2445
2859
|
const button = document.createElement('button');
|
|
@@ -2449,8 +2863,7 @@ function renderSettings() {
|
|
|
2449
2863
|
if (state.settingsCategory === item.id) return;
|
|
2450
2864
|
state.settingsCategory = item.id;
|
|
2451
2865
|
state.settingsScroll = 0;
|
|
2452
|
-
renderSettings();
|
|
2453
|
-
body.scrollTop = 0;
|
|
2866
|
+
renderSettings({ resetScroll: true });
|
|
2454
2867
|
});
|
|
2455
2868
|
nav.appendChild(button);
|
|
2456
2869
|
}
|
|
@@ -2647,7 +3060,7 @@ function renderTerminalSettings(body) {
|
|
|
2647
3060
|
if (isTermOpen) termClose();
|
|
2648
3061
|
else termOpen();
|
|
2649
3062
|
}, true);
|
|
2650
|
-
card.appendChild(settingsRow('终端面板', '
|
|
3063
|
+
card.appendChild(settingsRow('终端面板', '在底部 dock 打开,不会挡住输入框。快捷键 Ctrl+`。', openBtn));
|
|
2651
3064
|
section.appendChild(card);
|
|
2652
3065
|
|
|
2653
3066
|
const help = settingsSection('常用快捷键', '');
|
|
@@ -2888,7 +3301,7 @@ function renderAboutSettings(body) {
|
|
|
2888
3301
|
const info = state.info;
|
|
2889
3302
|
const section = settingsSection('关于 Banana', '一个把 CLI 内核完整搬进图形界面的本地 agent。');
|
|
2890
3303
|
const card = settingsCard();
|
|
2891
|
-
card.appendChild(settingsRow('版本', 'CLI 与 GUI 使用同一套 agent 循环和工具。', pluginBadge('v' + (info.version ?? '0.6.0'))));
|
|
3304
|
+
card.appendChild(settingsRow('CLI 版本', 'CLI 与 GUI 使用同一套 agent 循环和工具。', pluginBadge('v' + (info.version ?? '0.6.0'))));
|
|
2892
3305
|
card.appendChild(settingsRow('工作区', info.workspace ?? '', null));
|
|
2893
3306
|
card.appendChild(settingsRow('工具数量', `${info.tools?.length ?? 0} 个`, null));
|
|
2894
3307
|
card.appendChild(settingsRow('平台', info.platform === 'win32' ? 'Windows' : info.platform === 'darwin' ? 'macOS' : 'Linux', null));
|
|
@@ -3463,7 +3876,8 @@ async function termLoadShells() {
|
|
|
3463
3876
|
select.addEventListener('change', () => {
|
|
3464
3877
|
localStorage.setItem(TERM_STORAGE.shell, select.value);
|
|
3465
3878
|
closeTermMenu();
|
|
3466
|
-
|
|
3879
|
+
// 这里只记录“新标签”用哪个 shell;真正新建走 + 按钮。
|
|
3880
|
+
toast('新标签将使用 ' + (term.shells.find((shell) => shell.id === select.value)?.name ?? select.value));
|
|
3467
3881
|
});
|
|
3468
3882
|
} catch {
|
|
3469
3883
|
term.shells = [{ id: 'cmd', name: '终端' }];
|
|
@@ -3479,7 +3893,17 @@ function termRenderTabs() {
|
|
|
3479
3893
|
for (const tab of term.tabs) {
|
|
3480
3894
|
const node = document.createElement('div');
|
|
3481
3895
|
node.className = 'term-tab' + (tab.id === term.activeId ? ' active' : '');
|
|
3896
|
+
node.tabIndex = 0;
|
|
3897
|
+
node.setAttribute('role', 'tab');
|
|
3898
|
+
node.setAttribute('aria-selected', String(tab.id === term.activeId));
|
|
3482
3899
|
node.innerHTML = `<span class="term-tab-name">${escapeHtml(tab.name)}</span><button class="term-tab-close" title="关闭标签" aria-label="关闭标签"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button>`;
|
|
3900
|
+
node.addEventListener('keydown', (event) => {
|
|
3901
|
+
if (event.target.closest('.term-tab-close')) return;
|
|
3902
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
3903
|
+
event.preventDefault();
|
|
3904
|
+
termActivate(tab.id);
|
|
3905
|
+
}
|
|
3906
|
+
});
|
|
3483
3907
|
node.addEventListener('click', (event) => {
|
|
3484
3908
|
if (event.target.closest('.term-tab-close')) {
|
|
3485
3909
|
event.stopPropagation();
|
|
@@ -3552,6 +3976,8 @@ function termConnect(tab) {
|
|
|
3552
3976
|
} else if (message.type === 'data') {
|
|
3553
3977
|
tab.terminal.write(message.data);
|
|
3554
3978
|
} else if (message.type === 'exit') {
|
|
3979
|
+
// 用户主动 exit 后不要自动重连,否则关闭的 shell 会悄悄重启。
|
|
3980
|
+
tab.alive = false;
|
|
3555
3981
|
tab.terminal.write(`\r\n\x1b[90m[进程已退出,代码 ${message.code ?? 0}]\x1b[0m\r\n`);
|
|
3556
3982
|
} else if (message.type === 'error') {
|
|
3557
3983
|
tab.terminal.write(`\r\n\x1b[31m${message.message}\x1b[0m\r\n`);
|
|
@@ -3811,6 +4237,7 @@ function bindTerminal() {
|
|
|
3811
4237
|
function closeTermMenu() {
|
|
3812
4238
|
const menu = $('termMenu');
|
|
3813
4239
|
if (menu) menu.hidden = true;
|
|
4240
|
+
$('termMore')?.setAttribute('aria-expanded', 'false');
|
|
3814
4241
|
}
|
|
3815
4242
|
|
|
3816
4243
|
// ---------------- 命令中心(Ctrl+K) ----------------
|
|
@@ -3821,7 +4248,7 @@ function ccActions() {
|
|
|
3821
4248
|
{ title: '新建对话', sub: '开始一个干净的会话', icon: 'plus', run: () => $('newChat').click() },
|
|
3822
4249
|
{ title: '打开项目文件', sub: '浏览、全屏查看、引用文件', icon: 'panel', run: openFiles },
|
|
3823
4250
|
{ title: '查看本会话改动', sub: '按文件查看增删行,审阅 agent 的修改', icon: 'diff', run: openChanges },
|
|
3824
|
-
{ title: '打开终端', sub: '
|
|
4251
|
+
{ title: '打开终端', sub: '底部 dock,不遮挡输入框', icon: 'terminal', run: termOpen },
|
|
3825
4252
|
{ title: '打开控制中心', sub: '模型、外观、终端、工作区、插件', icon: 'sliders', run: () => openSettings() },
|
|
3826
4253
|
{ title: '插件工坊', sub: '启用、停用、生成模板、试运行插件', icon: 'plug', run: () => openSettings('plugins') },
|
|
3827
4254
|
{ title: '切换主题', sub: '浅色 / 深色 / 跟随系统', icon: 'sun', run: cycleTheme },
|
|
@@ -4027,6 +4454,8 @@ function initTooltips() {
|
|
|
4027
4454
|
const title = node.getAttribute('title');
|
|
4028
4455
|
if (title) {
|
|
4029
4456
|
node.dataset.tip = title;
|
|
4457
|
+
// 移除原生 title 前保留可访问名称,避免 hover 一次后读屏软件失去描述。
|
|
4458
|
+
if (!node.hasAttribute('aria-label')) node.setAttribute('aria-label', title);
|
|
4030
4459
|
node.removeAttribute('title');
|
|
4031
4460
|
}
|
|
4032
4461
|
const text = node.dataset.tip;
|
|
@@ -4189,23 +4618,8 @@ function bind() {
|
|
|
4189
4618
|
hydrate();
|
|
4190
4619
|
loadTheme();
|
|
4191
4620
|
|
|
4192
|
-
$('newChat').addEventListener('click',
|
|
4193
|
-
|
|
4194
|
-
// 直接用返回的会话,不再 refresh:多一次 lastActive 解析就多一次串回旧对话的机会
|
|
4195
|
-
const result = await post('session', { action: 'new' });
|
|
4196
|
-
if (result?.info) state.info = result.info;
|
|
4197
|
-
if (result?.sessions) state.sessions = result.sessions;
|
|
4198
|
-
state.changes = null;
|
|
4199
|
-
renderHeader();
|
|
4200
|
-
renderSidebar();
|
|
4201
|
-
renderThread();
|
|
4202
|
-
if (state.dock.open) termClose();
|
|
4203
|
-
closeSidebarIfOverlay();
|
|
4204
|
-
setActiveTab(state.info?.session?.id ?? null);
|
|
4205
|
-
$('input').focus();
|
|
4206
|
-
} catch (error) {
|
|
4207
|
-
toast('新建对话失败:' + (error?.message ?? error));
|
|
4208
|
-
}
|
|
4621
|
+
$('newChat').addEventListener('click', () => {
|
|
4622
|
+
void createSession();
|
|
4209
4623
|
});
|
|
4210
4624
|
$('openSettings').addEventListener('click', () => openSettings());
|
|
4211
4625
|
$('closeSettings').addEventListener('click', closeSettings);
|
|
@@ -4258,10 +4672,18 @@ function bind() {
|
|
|
4258
4672
|
$('approvalYes').addEventListener('click', () => closeApproval(true));
|
|
4259
4673
|
|
|
4260
4674
|
$('undoBtn').addEventListener('click', async () => {
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4675
|
+
if (state.busy) {
|
|
4676
|
+
toast('正在生成,先停止当前任务再撤销');
|
|
4677
|
+
return;
|
|
4678
|
+
}
|
|
4679
|
+
try {
|
|
4680
|
+
const result = await post('undo');
|
|
4681
|
+
toast(result.had ? `已回滚:恢复 ${result.restored} 个文件` : '没有可回滚的改动');
|
|
4682
|
+
state.changes = null;
|
|
4683
|
+
if (state.review.open && state.review.mode === 'changes') void loadChanges(true);
|
|
4684
|
+
} catch (error) {
|
|
4685
|
+
toast('撤销失败:' + (error?.message ?? error));
|
|
4686
|
+
}
|
|
4265
4687
|
});
|
|
4266
4688
|
|
|
4267
4689
|
$('composer').addEventListener('submit', (event) => { event.preventDefault(); send(); });
|
|
@@ -4326,8 +4748,21 @@ function bind() {
|
|
|
4326
4748
|
},
|
|
4327
4749
|
{ passive: true },
|
|
4328
4750
|
);
|
|
4751
|
+
// 拖动滚动条时不会触发 wheel;点中滚动条区域就暂停跟随。
|
|
4752
|
+
THREAD.addEventListener('pointerdown', (event) => {
|
|
4753
|
+
if (event.target !== THREAD) return;
|
|
4754
|
+
if (event.offsetX >= THREAD.clientWidth) {
|
|
4755
|
+
setCameraFollow(false);
|
|
4756
|
+
camera.lastUserScroll = Date.now();
|
|
4757
|
+
}
|
|
4758
|
+
});
|
|
4329
4759
|
THREAD.addEventListener('scroll', () => {
|
|
4330
4760
|
const top = THREAD.scrollTop;
|
|
4761
|
+
// 没有相机动画时,向上滚动(滚动条、键盘、查找)也要暂停跟随。
|
|
4762
|
+
if (!camera.running && top < camera.lastTop - 4) {
|
|
4763
|
+
setCameraFollow(false);
|
|
4764
|
+
camera.lastUserScroll = Date.now();
|
|
4765
|
+
}
|
|
4331
4766
|
camera.lastTop = top;
|
|
4332
4767
|
const atBottom = cameraMax() - top < 70;
|
|
4333
4768
|
if (atBottom && !camera.follow && Date.now() - camera.lastUserScroll > 300) {
|
|
@@ -4336,6 +4771,13 @@ function bind() {
|
|
|
4336
4771
|
const hasMessages = Boolean(THREAD.querySelector('.msg'));
|
|
4337
4772
|
$('jumpLatest').hidden = camera.follow || !hasMessages;
|
|
4338
4773
|
});
|
|
4774
|
+
document.addEventListener('keydown', (event) => {
|
|
4775
|
+
if (event.target instanceof Element && event.target.closest('input, textarea, select')) return;
|
|
4776
|
+
if (!['PageUp', 'Home', 'ArrowUp'].includes(event.key)) return;
|
|
4777
|
+
if (!THREAD.querySelector('.msg')) return;
|
|
4778
|
+
setCameraFollow(false);
|
|
4779
|
+
camera.lastUserScroll = Date.now();
|
|
4780
|
+
});
|
|
4339
4781
|
$('input').addEventListener('input', () => {
|
|
4340
4782
|
grow();
|
|
4341
4783
|
const value = $('input').value;
|
|
@@ -4414,6 +4856,23 @@ function bind() {
|
|
|
4414
4856
|
}
|
|
4415
4857
|
if (event.key === 'Escape') {
|
|
4416
4858
|
if (!$('commandPalette').hidden) { closePalette(); return; }
|
|
4859
|
+
// 弹层优先:Esc 只负责关最上层,不能顺手把正在生成的回答停掉。
|
|
4860
|
+
if (reasoningPop) {
|
|
4861
|
+
event.preventDefault();
|
|
4862
|
+
closeReasoningPop();
|
|
4863
|
+
return;
|
|
4864
|
+
}
|
|
4865
|
+
if ($('modelPop')) {
|
|
4866
|
+
event.preventDefault();
|
|
4867
|
+
closeModelPop();
|
|
4868
|
+
$('modelBtn')?.focus();
|
|
4869
|
+
return;
|
|
4870
|
+
}
|
|
4871
|
+
if (document.querySelector('.popover')) {
|
|
4872
|
+
event.preventDefault();
|
|
4873
|
+
document.querySelector('.popover')?.remove();
|
|
4874
|
+
return;
|
|
4875
|
+
}
|
|
4417
4876
|
// 终端里的 Esc 属于 shell,不要把工作台一起关掉
|
|
4418
4877
|
const inTerminal = event.target instanceof Element && event.target.closest('#terminalPanel');
|
|
4419
4878
|
if (inTerminal && $('settingsSheet').hidden && $('codeViewer').hidden && $('commandCenter').hidden && $('approvalModal').hidden) return;
|
|
@@ -4449,7 +4908,9 @@ async function boot() {
|
|
|
4449
4908
|
bind();
|
|
4450
4909
|
// 命令表来自服务端(与 CLI 同一份),拉不到也不影响对话
|
|
4451
4910
|
try {
|
|
4452
|
-
|
|
4911
|
+
const all = (await get('commands')).commands ?? [];
|
|
4912
|
+
// 只暴露 GUI 真正能执行的命令;否则会出现“显示成功但没生效”。
|
|
4913
|
+
commandList = all.filter((item) => GUI_COMMANDS.has(item.name));
|
|
4453
4914
|
} catch {
|
|
4454
4915
|
commandList = [];
|
|
4455
4916
|
}
|