@vincentbanana/bananacli 0.8.0 → 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 +808 -137
- package/dist/ui/index.html +5 -5
- package/dist/ui/style.css +205 -6
- package/package.json +1 -1
package/dist/ui/app.js
CHANGED
|
@@ -54,6 +54,24 @@ const TOOL_ICON = {
|
|
|
54
54
|
web_fetch: 'globe', todo_write: 'listCheck', todo_read: 'listCheck',
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
+
const TOOL_LABEL = {
|
|
58
|
+
read_file: '读取文件',
|
|
59
|
+
write_file: '写入文件',
|
|
60
|
+
edit_file: '修改文件',
|
|
61
|
+
list_dir: '查看目录',
|
|
62
|
+
glob: '查找文件',
|
|
63
|
+
grep: '搜索代码',
|
|
64
|
+
shell: '运行命令',
|
|
65
|
+
web_search: '搜索网页',
|
|
66
|
+
web_fetch: '读取网页',
|
|
67
|
+
todo_write: '更新任务',
|
|
68
|
+
todo_read: '读取任务',
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
function toolLabel(name) {
|
|
72
|
+
return TOOL_LABEL[name] ?? name;
|
|
73
|
+
}
|
|
74
|
+
|
|
57
75
|
function svg(name) {
|
|
58
76
|
return `<svg viewBox="0 0 24 24" aria-hidden="true">${ICONS[name] ?? ICONS.file}</svg>`;
|
|
59
77
|
}
|
|
@@ -62,14 +80,80 @@ const $ = (id) => document.getElementById(id);
|
|
|
62
80
|
|
|
63
81
|
// ---------------- Markdown(够用即可) ----------------
|
|
64
82
|
const escapeHtml = (text) =>
|
|
65
|
-
text
|
|
83
|
+
text
|
|
84
|
+
.replace(/&/g, '&')
|
|
85
|
+
.replace(/</g, '<')
|
|
86
|
+
.replace(/>/g, '>')
|
|
87
|
+
// 这些值也会进 href/alt 之类的属性,引号必须一起转义。
|
|
88
|
+
.replace(/"/g, '"')
|
|
89
|
+
.replace(/'/g, ''');
|
|
66
90
|
|
|
67
91
|
function inline(text) {
|
|
68
|
-
|
|
69
|
-
|
|
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
|
|
70
99
|
.replace(/\*\*([^*\n]+)\*\*/g, '<strong>$1</strong>')
|
|
71
100
|
.replace(/(^|[\s(])\*([^*\n]+)\*/g, '$1<em>$2</em>')
|
|
72
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>`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 流式 Markdown 的稳定切分:空白行之前已经完成的内容不再重绘,
|
|
107
|
+
* 只更新最后一段未完成内容,避免思考/回答在流式时整体高度反复跳。
|
|
108
|
+
*/
|
|
109
|
+
function splitStableMarkdown(source) {
|
|
110
|
+
const lines = source.replace(/\r\n/g, '\n').split('\n');
|
|
111
|
+
let inFence = false;
|
|
112
|
+
let offset = 0;
|
|
113
|
+
let boundary = 0;
|
|
114
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
115
|
+
const line = lines[index];
|
|
116
|
+
if (/^```/.test(line)) inFence = !inFence;
|
|
117
|
+
offset += line.length + (index < lines.length - 1 ? 1 : 0);
|
|
118
|
+
if (!inFence && /^\s*$/.test(line)) boundary = offset;
|
|
119
|
+
}
|
|
120
|
+
return [source.slice(0, boundary), source.slice(boundary)];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function createMarkdownStream(container) {
|
|
124
|
+
const stable = document.createElement('div');
|
|
125
|
+
stable.className = 'stream-stable';
|
|
126
|
+
const tail = document.createElement('div');
|
|
127
|
+
tail.className = 'stream-tail';
|
|
128
|
+
container.replaceChildren(stable, tail);
|
|
129
|
+
let lastHead = '';
|
|
130
|
+
let lastTail = '';
|
|
131
|
+
let finished = false;
|
|
132
|
+
const paint = (text) => {
|
|
133
|
+
const [head, tailText] = splitStableMarkdown(text);
|
|
134
|
+
if (head !== lastHead) {
|
|
135
|
+
stable.innerHTML = markdown(head);
|
|
136
|
+
lastHead = head;
|
|
137
|
+
}
|
|
138
|
+
if (tailText !== lastTail) {
|
|
139
|
+
tail.innerHTML = markdown(tailText);
|
|
140
|
+
lastTail = tailText;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
return {
|
|
144
|
+
render(text) {
|
|
145
|
+
if (!finished) paint(text);
|
|
146
|
+
},
|
|
147
|
+
finish(text) {
|
|
148
|
+
if (finished) return;
|
|
149
|
+
paint(text);
|
|
150
|
+
finished = true;
|
|
151
|
+
},
|
|
152
|
+
setError(html) {
|
|
153
|
+
finished = true;
|
|
154
|
+
container.innerHTML = html;
|
|
155
|
+
},
|
|
156
|
+
};
|
|
73
157
|
}
|
|
74
158
|
|
|
75
159
|
function markdown(source) {
|
|
@@ -89,6 +173,31 @@ function markdown(source) {
|
|
|
89
173
|
continue;
|
|
90
174
|
}
|
|
91
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
|
+
}
|
|
92
201
|
if (/^###\s/.test(line)) { out.push(`<h3>${inline(line.replace(/^###\s/, ''))}</h3>`); i += 1; continue; }
|
|
93
202
|
if (/^##\s/.test(line)) { out.push(`<h2>${inline(line.replace(/^##\s/, ''))}</h2>`); i += 1; continue; }
|
|
94
203
|
if (/^#\s/.test(line)) { out.push(`<h1>${inline(line.replace(/^#\s/, ''))}</h1>`); i += 1; continue; }
|
|
@@ -196,6 +305,67 @@ const state = {
|
|
|
196
305
|
|
|
197
306
|
const THREAD = $('thread');
|
|
198
307
|
|
|
308
|
+
// 单一滚动相机:始终追底,用缓动逼近,避免每个 chunk 都重启 smooth 动画。
|
|
309
|
+
const camera = {
|
|
310
|
+
raf: 0,
|
|
311
|
+
running: false,
|
|
312
|
+
follow: true,
|
|
313
|
+
lastTop: 0,
|
|
314
|
+
programmaticAt: 0,
|
|
315
|
+
lastUserScroll: 0,
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
function prefersReducedMotion() {
|
|
319
|
+
return (
|
|
320
|
+
document.documentElement.dataset.reduceMotion === '1' ||
|
|
321
|
+
window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function stopCamera() {
|
|
326
|
+
camera.running = false;
|
|
327
|
+
if (camera.raf) cancelAnimationFrame(camera.raf);
|
|
328
|
+
camera.raf = 0;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function cameraMax() {
|
|
332
|
+
return Math.max(0, THREAD.scrollHeight - THREAD.clientHeight);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function cameraTick() {
|
|
336
|
+
if (!camera.running) return;
|
|
337
|
+
if (!camera.follow) {
|
|
338
|
+
stopCamera();
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const target = cameraMax();
|
|
342
|
+
const current = THREAD.scrollTop;
|
|
343
|
+
const distance = target - current;
|
|
344
|
+
if (distance <= 0.8) {
|
|
345
|
+
THREAD.scrollTop = target;
|
|
346
|
+
camera.lastTop = target;
|
|
347
|
+
camera.programmaticAt = Date.now();
|
|
348
|
+
stopCamera();
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const step = Math.min(64, Math.max(1, distance * 0.12));
|
|
352
|
+
THREAD.scrollTop = current + step;
|
|
353
|
+
camera.lastTop = THREAD.scrollTop;
|
|
354
|
+
camera.programmaticAt = Date.now();
|
|
355
|
+
camera.raf = requestAnimationFrame(cameraTick);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function setCameraFollow(follow) {
|
|
359
|
+
camera.follow = Boolean(follow);
|
|
360
|
+
state.stickToBottom = camera.follow;
|
|
361
|
+
// 暂停跟随时恢复浏览器滚动锚定,用户在读旧内容时不会被上方新内容推走。
|
|
362
|
+
THREAD.style.overflowAnchor = camera.follow ? 'none' : 'auto';
|
|
363
|
+
if (!camera.follow) {
|
|
364
|
+
stopCamera();
|
|
365
|
+
if (THREAD.querySelector('.msg')) $('jumpLatest').hidden = false;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
199
369
|
// 历史消息 / 新回复渲染完成后统一高亮,避免流式过程中反复解析
|
|
200
370
|
let highlightFrame = 0;
|
|
201
371
|
const highlightObserver = new MutationObserver(() => {
|
|
@@ -239,6 +409,10 @@ let switching = false;
|
|
|
239
409
|
*/
|
|
240
410
|
async function switchSession(ref) {
|
|
241
411
|
if (switching) return;
|
|
412
|
+
if (state.busy) {
|
|
413
|
+
toast('正在生成,先停止当前任务再切换会话');
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
242
416
|
switching = true;
|
|
243
417
|
document.body.classList.add('switching');
|
|
244
418
|
try {
|
|
@@ -246,9 +420,14 @@ async function switchSession(ref) {
|
|
|
246
420
|
if (result?.info) state.info = result.info;
|
|
247
421
|
if (result?.sessions) state.sessions = result.sessions;
|
|
248
422
|
state.changes = null;
|
|
423
|
+
// 附件属于当前草稿,切会话时不能带到另一个会话里。
|
|
424
|
+
state.attachments = [];
|
|
425
|
+
renderAttachments();
|
|
249
426
|
renderHeader();
|
|
250
427
|
renderSidebar();
|
|
251
428
|
renderThread();
|
|
429
|
+
$('input').value = '';
|
|
430
|
+
grow();
|
|
252
431
|
$('filesTitle').textContent = `项目文件 · ${state.info?.workspace ?? ''}`;
|
|
253
432
|
setActiveTab(state.info?.session?.id ?? null);
|
|
254
433
|
if (state.review.open && state.review.mode === 'changes') void loadChanges(true);
|
|
@@ -262,6 +441,35 @@ async function switchSession(ref) {
|
|
|
262
441
|
}
|
|
263
442
|
}
|
|
264
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
|
+
|
|
265
473
|
// ---------------- 会话标签条 ----------------
|
|
266
474
|
function tabTitleFor(id) {
|
|
267
475
|
if (state.info?.session?.id === id && state.info.session.title) return state.info.session.title;
|
|
@@ -352,6 +560,10 @@ function renderTabs() {
|
|
|
352
560
|
}
|
|
353
561
|
|
|
354
562
|
function closeTab(id) {
|
|
563
|
+
if (state.busy && id === state.activeTab) {
|
|
564
|
+
toast('正在生成,先停止当前任务再关闭标签');
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
355
567
|
const index = state.tabs.findIndex((tab) => tab.id === id);
|
|
356
568
|
if (index < 0) return;
|
|
357
569
|
state.tabs.splice(index, 1);
|
|
@@ -427,18 +639,58 @@ function renderSidebar() {
|
|
|
427
639
|
`<span class="s-title">${escapeHtml(item.title || '新对话')}</span>` +
|
|
428
640
|
`<span class="s-meta">${item.turns} 轮 · ${relativeTime(item.updatedAt)}</span>`;
|
|
429
641
|
|
|
642
|
+
node.tabIndex = 0;
|
|
643
|
+
node.setAttribute('role', 'button');
|
|
430
644
|
node.addEventListener('click', (event) => {
|
|
431
645
|
if (event.target.closest('[data-del], [data-menu]')) return;
|
|
432
646
|
switchSession(item.id);
|
|
433
647
|
});
|
|
648
|
+
node.addEventListener('keydown', (event) => {
|
|
649
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
650
|
+
event.preventDefault();
|
|
651
|
+
switchSession(item.id);
|
|
652
|
+
}
|
|
653
|
+
});
|
|
434
654
|
|
|
435
|
-
node.querySelector('[data-del]').addEventListener('click', (event) => {
|
|
655
|
+
node.querySelector('[data-del]').addEventListener('click', async (event) => {
|
|
436
656
|
event.stopPropagation();
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
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,
|
|
441
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
|
+
}
|
|
442
694
|
});
|
|
443
695
|
|
|
444
696
|
node.querySelector('[data-menu]').addEventListener('click', (event) => {
|
|
@@ -456,7 +708,7 @@ function openSessionMenu(item, anchor) {
|
|
|
456
708
|
const pop = document.createElement('div');
|
|
457
709
|
pop.className = 'popover';
|
|
458
710
|
pop.innerHTML = `
|
|
459
|
-
<button data-act="switch"
|
|
711
|
+
<button data-act="switch">打开会话</button>
|
|
460
712
|
<button data-act="rename">重命名</button>
|
|
461
713
|
<button data-act="md">导出 Markdown</button>
|
|
462
714
|
<button data-act="json">导出 JSON</button>`;
|
|
@@ -487,20 +739,43 @@ function openSessionMenu(item, anchor) {
|
|
|
487
739
|
if (action === 'switch') {
|
|
488
740
|
await switchSession(item.id);
|
|
489
741
|
} else if (action === 'rename') {
|
|
490
|
-
|
|
491
|
-
if (title && title.trim()) {
|
|
492
|
-
await post('session', { action: 'switch', ref: item.id });
|
|
493
|
-
await post('session', { action: 'rename', title: title.trim() });
|
|
494
|
-
await refresh(false);
|
|
495
|
-
toast('已重命名');
|
|
496
|
-
}
|
|
742
|
+
await renameSession(item);
|
|
497
743
|
} else {
|
|
498
|
-
await
|
|
499
|
-
downloadSession(action === 'json' ? 'json' : 'md', item.id);
|
|
744
|
+
await downloadSession(action === 'json' ? 'json' : 'md', item.id);
|
|
500
745
|
}
|
|
501
746
|
});
|
|
502
747
|
}
|
|
503
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
|
+
|
|
504
779
|
function sessionToMarkdown(messages, title) {
|
|
505
780
|
const lines = [`# ${title || '对话'}`, '', `导出时间:${new Date().toLocaleString()}`, ''];
|
|
506
781
|
for (const message of messages) {
|
|
@@ -518,8 +793,19 @@ function sessionToMarkdown(messages, title) {
|
|
|
518
793
|
return lines.join('\n');
|
|
519
794
|
}
|
|
520
795
|
|
|
521
|
-
function downloadSession(format, id) {
|
|
522
|
-
|
|
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
|
+
}
|
|
523
809
|
const blob =
|
|
524
810
|
format === 'json'
|
|
525
811
|
? JSON.stringify(session, null, 2)
|
|
@@ -550,16 +836,27 @@ function ensureToolOutput(row, initial = '', isError = false) {
|
|
|
550
836
|
out.classList.toggle('is-error', isError);
|
|
551
837
|
if (initial) out.textContent = initial;
|
|
552
838
|
row.classList.add('is-clickable');
|
|
839
|
+
row.tabIndex = 0;
|
|
840
|
+
row.setAttribute('role', 'button');
|
|
841
|
+
row.setAttribute('aria-expanded', String(!out.hidden));
|
|
553
842
|
row.title = '点击展开命令输出';
|
|
554
843
|
if (!row.dataset.bound) {
|
|
555
844
|
row.dataset.bound = '1';
|
|
556
|
-
|
|
845
|
+
const toggle = () => {
|
|
557
846
|
const box = row.nextElementSibling;
|
|
558
847
|
if (!box?.classList.contains('tool-output')) return;
|
|
559
848
|
const open = box.hidden;
|
|
560
849
|
box.hidden = !open;
|
|
561
850
|
row.classList.toggle('is-open', open);
|
|
851
|
+
row.setAttribute('aria-expanded', String(open));
|
|
562
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
|
+
}
|
|
563
860
|
});
|
|
564
861
|
}
|
|
565
862
|
return out;
|
|
@@ -570,7 +867,7 @@ function toolRowNode(name, detail, stateHtml, ms) {
|
|
|
570
867
|
row.className = 'tool-row';
|
|
571
868
|
row.innerHTML =
|
|
572
869
|
`<span class="t-ico">${svg(TOOL_ICON[name] ?? 'file')}</span>` +
|
|
573
|
-
`<span class="t-name">${escapeHtml(name)}</span>` +
|
|
870
|
+
`<span class="t-name">${escapeHtml(toolLabel(name))}</span>` +
|
|
574
871
|
`<span class="t-detail">${escapeHtml(detail)}</span>` +
|
|
575
872
|
`<span class="t-state">${stateHtml}</span>` +
|
|
576
873
|
(ms ? `<span class="t-time">${(ms / 1000).toFixed(1)}s</span>` : '');
|
|
@@ -585,7 +882,7 @@ function toolGroup(calls, results) {
|
|
|
585
882
|
wrap.classList.toggle('has-error', hasError);
|
|
586
883
|
const summary = document.createElement('summary');
|
|
587
884
|
summary.innerHTML =
|
|
588
|
-
`<span class="wg-dot"></span><span class="wg-title"
|
|
885
|
+
`<span class="wg-dot"></span><span class="wg-title">完成 ${calls.length} 个操作</span>` +
|
|
589
886
|
`<span class="wg-state">${hasError ? '有失败' : ''}</span>`;
|
|
590
887
|
const body = document.createElement('div');
|
|
591
888
|
body.className = 'tool-group-body';
|
|
@@ -608,9 +905,10 @@ function reasoningDetails(text) {
|
|
|
608
905
|
const details = document.createElement('details');
|
|
609
906
|
details.className = 'think';
|
|
610
907
|
const summary = document.createElement('summary');
|
|
908
|
+
const length = text.length > 999 ? `${(text.length / 1000).toFixed(1)}k 字` : `${text.length} 字`;
|
|
611
909
|
summary.innerHTML =
|
|
612
910
|
'<span class="think-title">思考</span>' +
|
|
613
|
-
`<span class="think-len">${
|
|
911
|
+
`<span class="think-len">${length}</span>`;
|
|
614
912
|
const body = document.createElement('div');
|
|
615
913
|
body.className = 'think-body';
|
|
616
914
|
body.innerHTML = markdown(text);
|
|
@@ -705,14 +1003,15 @@ function renderEmpty() {
|
|
|
705
1003
|
btn.addEventListener('click', () => { $('input').value = btn.textContent; $('input').focus(); grow(); });
|
|
706
1004
|
});
|
|
707
1005
|
const setup = tips.querySelector('#emptySetup');
|
|
708
|
-
if (setup) setup.addEventListener('click', () => openSettings());
|
|
1006
|
+
if (setup) setup.addEventListener('click', () => openSettings('model'));
|
|
709
1007
|
}
|
|
710
1008
|
return wrap;
|
|
711
1009
|
}
|
|
712
1010
|
|
|
713
1011
|
function renderThread() {
|
|
1012
|
+
stopCamera();
|
|
714
1013
|
THREAD.innerHTML = '';
|
|
715
|
-
|
|
1014
|
+
setCameraFollow(true);
|
|
716
1015
|
$('jumpLatest').hidden = true;
|
|
717
1016
|
const messages = state.info?.session?.messages ?? [];
|
|
718
1017
|
if (messages.length === 0) THREAD.appendChild(renderEmpty());
|
|
@@ -731,7 +1030,7 @@ function renderThread() {
|
|
|
731
1030
|
renderTodos();
|
|
732
1031
|
updateHomeMode();
|
|
733
1032
|
// 切换会话时瞬间定位到底部;平滑滚动只用于新消息
|
|
734
|
-
scrollToEnd(false);
|
|
1033
|
+
scrollToEnd(false, true);
|
|
735
1034
|
}
|
|
736
1035
|
|
|
737
1036
|
function renderTurnMeta() {
|
|
@@ -739,16 +1038,26 @@ function renderTurnMeta() {
|
|
|
739
1038
|
if (state.busy && state.status) { meta.textContent = state.status; return; }
|
|
740
1039
|
const usage = state.info?.session?.usage;
|
|
741
1040
|
if (!usage || (!usage.inputTokens && !usage.outputTokens)) { meta.textContent = ''; return; }
|
|
742
|
-
const
|
|
743
|
-
|
|
1041
|
+
const total = usage.inputTokens + usage.outputTokens;
|
|
1042
|
+
const parts = [total >= 1000 ? `${(total / 1000).toFixed(1)}k tokens` : `${total} tokens`];
|
|
1043
|
+
if (usage.costUsd) parts.push(`$${usage.costUsd.toFixed(4)}`);
|
|
744
1044
|
meta.textContent = parts.join(' · ');
|
|
745
1045
|
}
|
|
746
1046
|
|
|
747
1047
|
function scrollToEnd(smooth = true, force = false) {
|
|
748
|
-
if (
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
1048
|
+
if (force) setCameraFollow(true);
|
|
1049
|
+
if (!camera.follow) return;
|
|
1050
|
+
if (!smooth || prefersReducedMotion()) {
|
|
1051
|
+
stopCamera();
|
|
1052
|
+
THREAD.scrollTop = cameraMax();
|
|
1053
|
+
camera.lastTop = THREAD.scrollTop;
|
|
1054
|
+
camera.programmaticAt = Date.now();
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
1057
|
+
if (!camera.running) {
|
|
1058
|
+
camera.running = true;
|
|
1059
|
+
camera.raf = requestAnimationFrame(cameraTick);
|
|
1060
|
+
}
|
|
752
1061
|
}
|
|
753
1062
|
|
|
754
1063
|
function setStatus(text) {
|
|
@@ -765,7 +1074,7 @@ function setStatus(text) {
|
|
|
765
1074
|
function workspaceHint() {
|
|
766
1075
|
const info = state.info;
|
|
767
1076
|
if (!info) return '';
|
|
768
|
-
return `工作区 ${info.workspace}
|
|
1077
|
+
return `工作区 ${info.workspace}`;
|
|
769
1078
|
}
|
|
770
1079
|
|
|
771
1080
|
function renderHeader() {
|
|
@@ -834,9 +1143,12 @@ async function refresh(path) {
|
|
|
834
1143
|
state.info = info;
|
|
835
1144
|
const { sessions } = await get('sessions');
|
|
836
1145
|
state.sessions = sessions;
|
|
837
|
-
|
|
1146
|
+
// 即使标签条被清空,也要把服务端当前会话补回来,否则删掉当前会话后会变成无标签状态。
|
|
1147
|
+
if (state.info?.session?.id) {
|
|
838
1148
|
upsertTab(state.info.session.id, state.info.session.title);
|
|
839
1149
|
state.activeTab = state.info.session.id;
|
|
1150
|
+
} else {
|
|
1151
|
+
state.activeTab = null;
|
|
840
1152
|
}
|
|
841
1153
|
renderHeader();
|
|
842
1154
|
renderSidebar();
|
|
@@ -845,6 +1157,24 @@ async function refresh(path) {
|
|
|
845
1157
|
if (path) renderThread();
|
|
846
1158
|
}
|
|
847
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
|
+
|
|
848
1178
|
/** 设置面板里的改动要立刻反映在面板上,所以改完重画面板内容。 */
|
|
849
1179
|
async function refreshSettings() {
|
|
850
1180
|
await refresh(false);
|
|
@@ -865,13 +1195,94 @@ function hideAnimated(element, className = 'is-closing', ms = 180) {
|
|
|
865
1195
|
});
|
|
866
1196
|
}
|
|
867
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
|
+
|
|
868
1278
|
function syncOverlayState() {
|
|
869
1279
|
const open =
|
|
870
1280
|
!$('commandCenter').hidden ||
|
|
871
1281
|
!$('codeViewer').hidden ||
|
|
872
1282
|
!$('attachmentViewer').hidden ||
|
|
873
1283
|
!$('settingsSheet').hidden ||
|
|
874
|
-
!$('approvalModal').hidden
|
|
1284
|
+
!$('approvalModal').hidden ||
|
|
1285
|
+
Boolean($('appDialog'));
|
|
875
1286
|
document.body.classList.toggle('has-overlay', open);
|
|
876
1287
|
}
|
|
877
1288
|
|
|
@@ -989,6 +1400,7 @@ const REASONING_HINTS = {
|
|
|
989
1400
|
};
|
|
990
1401
|
|
|
991
1402
|
function formatFileSize(bytes) {
|
|
1403
|
+
if (bytes === 0) return '0 B';
|
|
992
1404
|
if (!bytes) return '';
|
|
993
1405
|
if (bytes < 1024) return `${bytes} B`;
|
|
994
1406
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
@@ -1228,6 +1640,8 @@ function onReasoningOutside(event) {
|
|
|
1228
1640
|
function onReasoningKey(event) {
|
|
1229
1641
|
if (event.key === 'Escape') {
|
|
1230
1642
|
event.preventDefault();
|
|
1643
|
+
// capture 阶段就消费掉,否则事件继续冒泡到全局 Esc,会把正在生成的 turn 停掉。
|
|
1644
|
+
event.stopPropagation();
|
|
1231
1645
|
closeReasoningPop();
|
|
1232
1646
|
$('reasoningBtn')?.focus();
|
|
1233
1647
|
}
|
|
@@ -1304,6 +1718,8 @@ function grow() {
|
|
|
1304
1718
|
input.style.height = Math.min(input.scrollHeight, 220) + 'px';
|
|
1305
1719
|
const send = $('sendBtn');
|
|
1306
1720
|
if (!state.busy) send.disabled = input.value.trim() === '';
|
|
1721
|
+
// composer 长高会把最后几行挤出视口;跟随状态下重新贴底。
|
|
1722
|
+
if (camera.follow) requestAnimationFrame(() => scrollToEnd());
|
|
1307
1723
|
}
|
|
1308
1724
|
|
|
1309
1725
|
function stopTurn() {
|
|
@@ -1318,6 +1734,8 @@ function setBusy(busy) {
|
|
|
1318
1734
|
const send = $('sendBtn');
|
|
1319
1735
|
const composer = $('composer');
|
|
1320
1736
|
composer?.classList.toggle('is-busy', busy);
|
|
1737
|
+
composer?.setAttribute('aria-busy', String(busy));
|
|
1738
|
+
THREAD.setAttribute('aria-busy', String(busy));
|
|
1321
1739
|
// 发送键在原位切换成停止键,不再让用户去右上角找
|
|
1322
1740
|
send.innerHTML = busy ? svg('stop') : svg('send');
|
|
1323
1741
|
send.title = busy ? '停止生成' : '发送';
|
|
@@ -1353,6 +1771,9 @@ async function send() {
|
|
|
1353
1771
|
const text = input.value.trim();
|
|
1354
1772
|
if (!text || state.busy) return;
|
|
1355
1773
|
if (!state.info?.session) return;
|
|
1774
|
+
const runSessionId = state.info.session.id;
|
|
1775
|
+
stopCamera();
|
|
1776
|
+
setCameraFollow(true);
|
|
1356
1777
|
input.value = '';
|
|
1357
1778
|
grow();
|
|
1358
1779
|
closePalette();
|
|
@@ -1403,41 +1824,51 @@ async function send() {
|
|
|
1403
1824
|
work.hidden = true;
|
|
1404
1825
|
const workSummary = document.createElement('summary');
|
|
1405
1826
|
workSummary.innerHTML =
|
|
1406
|
-
'<span class="wg-dot"></span><span class="wg-title"
|
|
1827
|
+
'<span class="wg-dot"></span><span class="wg-title">正在处理</span><span class="wg-state"></span>';
|
|
1407
1828
|
const workBody = document.createElement('div');
|
|
1408
1829
|
workBody.className = 'tool-group-body';
|
|
1409
1830
|
work.append(workSummary, workBody);
|
|
1410
1831
|
work.classList.add('is-running');
|
|
1832
|
+
// 工具默认只显示一行活动摘要;用户主动展开或失败时才看详情,避免面板反复撑高再收起。
|
|
1833
|
+
workSummary.addEventListener('click', () => {
|
|
1834
|
+
work.dataset.user = '1';
|
|
1835
|
+
});
|
|
1411
1836
|
|
|
1412
|
-
//
|
|
1837
|
+
// 思考过程默认折叠成一行实时摘要:用户点开才看全文,避免收起时整页跳动。
|
|
1413
1838
|
const reasoning = document.createElement('details');
|
|
1414
1839
|
reasoning.className = 'think is-live';
|
|
1415
1840
|
reasoning.hidden = true;
|
|
1416
|
-
reasoning.open =
|
|
1841
|
+
reasoning.open = false;
|
|
1417
1842
|
const reasonSummary = document.createElement('summary');
|
|
1418
|
-
reasonSummary.innerHTML =
|
|
1843
|
+
reasonSummary.innerHTML =
|
|
1844
|
+
'<span class="think-title">思考中…</span><span class="think-peek"></span><span class="think-len"></span>';
|
|
1419
1845
|
const reasonBody = document.createElement('div');
|
|
1420
1846
|
reasonBody.className = 'think-body';
|
|
1847
|
+
const reasoningStream = createMarkdownStream(reasonBody);
|
|
1421
1848
|
reasoning.append(reasonSummary, reasonBody);
|
|
1422
|
-
//
|
|
1849
|
+
// 用户手动展开后,后续流式内容不要再强行收起。
|
|
1423
1850
|
reasonSummary.addEventListener('click', () => {
|
|
1424
1851
|
reasoning.dataset.user = '1';
|
|
1425
1852
|
});
|
|
1426
1853
|
|
|
1427
1854
|
const body = document.createElement('div');
|
|
1428
1855
|
body.className = 'msg-body caret';
|
|
1429
|
-
|
|
1856
|
+
// 流式内容每几十毫秒重绘一次,aria-live 会反复播报;完成时由状态区统一提示。
|
|
1857
|
+
body.setAttribute('aria-live', 'off');
|
|
1858
|
+
const answerStream = createMarkdownStream(body);
|
|
1430
1859
|
const meta = document.createElement('div');
|
|
1431
1860
|
meta.className = 'turn-meta';
|
|
1432
1861
|
const working = document.createElement('div');
|
|
1433
1862
|
working.className = 'working-row';
|
|
1434
|
-
working.innerHTML = '<span class="wg-dot"></span><span class="shimmer working-text"
|
|
1863
|
+
working.innerHTML = '<span class="wg-dot"></span><span class="shimmer working-text">正在准备…</span>';
|
|
1435
1864
|
botMsg.append(working, work, reasoning, body, meta);
|
|
1436
1865
|
THREAD.appendChild(botMsg);
|
|
1866
|
+
scrollToEnd(false);
|
|
1437
1867
|
|
|
1438
1868
|
let reasoningStarted = 0;
|
|
1439
1869
|
let reasoningText = '';
|
|
1440
1870
|
let reasonPending = 0;
|
|
1871
|
+
let lastReasonPaint = 0;
|
|
1441
1872
|
let toolStarted = 0;
|
|
1442
1873
|
let toolCount = 0;
|
|
1443
1874
|
let toolErrors = false;
|
|
@@ -1446,16 +1877,40 @@ async function send() {
|
|
|
1446
1877
|
const paintReasoning = (final = false) => {
|
|
1447
1878
|
reasonPending = 0;
|
|
1448
1879
|
const text = reasoningText;
|
|
1449
|
-
|
|
1880
|
+
if (final) reasoningStream.finish(text);
|
|
1881
|
+
else reasoningStream.render(text);
|
|
1450
1882
|
const len = reasonSummary.querySelector('.think-len');
|
|
1451
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
|
+
}
|
|
1452
1893
|
if (final) reasoning.classList.remove('is-live');
|
|
1453
|
-
//
|
|
1454
|
-
|
|
1455
|
-
|
|
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();
|
|
1456
1900
|
};
|
|
1457
1901
|
const scheduleReasoning = () => {
|
|
1458
|
-
if (!reasonPending)
|
|
1902
|
+
if (!reasonPending) {
|
|
1903
|
+
reasonPending = requestAnimationFrame(() => {
|
|
1904
|
+
// 思考也限速,避免每个 token 都重排
|
|
1905
|
+
if (Date.now() - lastReasonPaint < 60) {
|
|
1906
|
+
reasonPending = 0;
|
|
1907
|
+
scheduleReasoning();
|
|
1908
|
+
return;
|
|
1909
|
+
}
|
|
1910
|
+
lastReasonPaint = Date.now();
|
|
1911
|
+
paintReasoning(false);
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1459
1914
|
};
|
|
1460
1915
|
|
|
1461
1916
|
const finishReasoning = () => {
|
|
@@ -1466,7 +1921,7 @@ async function send() {
|
|
|
1466
1921
|
paintReasoning(true);
|
|
1467
1922
|
if (reasoning.dataset.user !== '1') reasoning.open = false;
|
|
1468
1923
|
const seconds = reasoningStarted ? ((Date.now() - reasoningStarted) / 1000).toFixed(1) : '0';
|
|
1469
|
-
reasonSummary.querySelector('.think-title').textContent =
|
|
1924
|
+
reasonSummary.querySelector('.think-title').textContent = `思考了 ${seconds}s`;
|
|
1470
1925
|
};
|
|
1471
1926
|
|
|
1472
1927
|
const setWorking = (text) => {
|
|
@@ -1480,10 +1935,11 @@ async function send() {
|
|
|
1480
1935
|
work.dataset.done = '1';
|
|
1481
1936
|
work.classList.remove('is-running');
|
|
1482
1937
|
const seconds = toolStarted ? ((Date.now() - toolStarted) / 1000).toFixed(1) : '0';
|
|
1483
|
-
workSummary.querySelector('.wg-title').textContent =
|
|
1938
|
+
workSummary.querySelector('.wg-title').textContent =
|
|
1939
|
+
toolCount === 1 ? '完成 1 个操作' : `完成 ${toolCount} 个操作`;
|
|
1484
1940
|
workSummary.querySelector('.wg-state').textContent = `${seconds}s${toolErrors ? ' · 有失败' : ''}`;
|
|
1485
1941
|
work.classList.toggle('has-error', toolErrors);
|
|
1486
|
-
if (!toolErrors) work.open = false;
|
|
1942
|
+
if (!toolErrors && work.dataset.user !== '1') work.open = false;
|
|
1487
1943
|
};
|
|
1488
1944
|
|
|
1489
1945
|
state.attachments = [];
|
|
@@ -1493,22 +1949,20 @@ async function send() {
|
|
|
1493
1949
|
let pending = 0;
|
|
1494
1950
|
let lastPaint = 0;
|
|
1495
1951
|
let streamEnded = false;
|
|
1496
|
-
const renderStream = () => markdown(buffer);
|
|
1497
1952
|
|
|
1498
1953
|
const paint = () => {
|
|
1499
1954
|
pending = 0;
|
|
1500
1955
|
const now = performance.now();
|
|
1501
1956
|
// 流式 markdown 限制到约 12fps,避免长回答每帧重排 + 高亮
|
|
1502
|
-
if (!streamEnded && now - lastPaint <
|
|
1957
|
+
if (!streamEnded && now - lastPaint < 50) {
|
|
1503
1958
|
schedule();
|
|
1504
1959
|
return;
|
|
1505
1960
|
}
|
|
1506
1961
|
lastPaint = now;
|
|
1507
|
-
|
|
1508
|
-
body.innerHTML = renderStream();
|
|
1962
|
+
answerStream.render(buffer);
|
|
1509
1963
|
// 流式过程中保持光标
|
|
1510
1964
|
if (!streamEnded) body.classList.add('caret');
|
|
1511
|
-
|
|
1965
|
+
scrollToEnd();
|
|
1512
1966
|
};
|
|
1513
1967
|
const schedule = () => {
|
|
1514
1968
|
if (!pending) pending = requestAnimationFrame(paint);
|
|
@@ -1533,7 +1987,7 @@ async function send() {
|
|
|
1533
1987
|
message: text,
|
|
1534
1988
|
images,
|
|
1535
1989
|
attachments: files,
|
|
1536
|
-
session:
|
|
1990
|
+
session: runSessionId,
|
|
1537
1991
|
}),
|
|
1538
1992
|
signal: controller.signal,
|
|
1539
1993
|
});
|
|
@@ -1557,28 +2011,29 @@ async function send() {
|
|
|
1557
2011
|
if (event.type === 'text') {
|
|
1558
2012
|
finishReasoning();
|
|
1559
2013
|
working.hidden = true;
|
|
1560
|
-
setStatus('
|
|
2014
|
+
setStatus('正在回答…');
|
|
1561
2015
|
buffer += event.chunk;
|
|
1562
2016
|
schedule();
|
|
1563
2017
|
} else if (event.type === 'reasoning') {
|
|
1564
|
-
if (
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
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();
|
|
1568
2027
|
}
|
|
1569
|
-
reasoning.hidden = false;
|
|
1570
|
-
if (reasoning.dataset.user !== '1') reasoning.open = true;
|
|
1571
|
-
reasoningText += event.chunk;
|
|
1572
2028
|
setWorking('正在思考…');
|
|
1573
2029
|
setStatus('思考中…');
|
|
1574
2030
|
working.hidden = false;
|
|
1575
|
-
scheduleReasoning();
|
|
1576
2031
|
} else if (event.type === 'tool_start') {
|
|
1577
2032
|
if (!toolStarted) toolStarted = Date.now();
|
|
1578
2033
|
toolCount += 1;
|
|
1579
2034
|
work.hidden = false;
|
|
1580
|
-
|
|
1581
|
-
|
|
2035
|
+
workSummary.querySelector('.wg-title').textContent =
|
|
2036
|
+
toolCount === 1 ? '正在执行 1 个操作' : `正在执行 ${toolCount} 个操作`;
|
|
1582
2037
|
workSummary.querySelector('.wg-state').textContent = '';
|
|
1583
2038
|
work.classList.add('is-updating');
|
|
1584
2039
|
setTimeout(() => work.classList.remove('is-updating'), 220);
|
|
@@ -1601,24 +2056,20 @@ async function send() {
|
|
|
1601
2056
|
toolRows.set(event.id, row);
|
|
1602
2057
|
workBody.appendChild(row);
|
|
1603
2058
|
if (command) ensureToolOutput(row, `$ ${command}\n`, false);
|
|
1604
|
-
setWorking(
|
|
1605
|
-
setStatus(
|
|
2059
|
+
setWorking(`正在${toolLabel(event.name)}…`);
|
|
2060
|
+
setStatus(`正在${toolLabel(event.name)}…`);
|
|
1606
2061
|
working.hidden = false;
|
|
1607
|
-
scrollToEnd(
|
|
2062
|
+
scrollToEnd();
|
|
1608
2063
|
} else if (event.type === 'tool_output') {
|
|
1609
2064
|
const row = toolRows.get(event.id);
|
|
1610
2065
|
if (row) {
|
|
1611
2066
|
row._out = `${row._out ?? ''}${event.chunk}`;
|
|
1612
2067
|
if (row._out.length > 12000) row._out = row._out.slice(-12000);
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
out.hidden = false;
|
|
1616
|
-
row.classList.add('is-open');
|
|
1617
|
-
}
|
|
1618
|
-
out.scrollTop = out.scrollHeight;
|
|
2068
|
+
// 实时输出先缓存在隐藏面板里,默认不把页面撑到 300px 高。
|
|
2069
|
+
ensureToolOutput(row, row._out);
|
|
1619
2070
|
const state = row.querySelector('.t-state');
|
|
1620
2071
|
if (state && !state.querySelector('.spin')) state.innerHTML = '<span class="spin"></span>';
|
|
1621
|
-
setStatus(
|
|
2072
|
+
setStatus(`${row.querySelector('.t-name')?.textContent ?? '命令'} · 正在输出…`);
|
|
1622
2073
|
}
|
|
1623
2074
|
} else if (event.type === 'tool_end') {
|
|
1624
2075
|
const row = toolRows.get(event.id) ?? [...toolRows.values()].pop();
|
|
@@ -1640,14 +2091,15 @@ async function send() {
|
|
|
1640
2091
|
}
|
|
1641
2092
|
if (!event.ok) {
|
|
1642
2093
|
work.open = true;
|
|
1643
|
-
setWorking('
|
|
2094
|
+
setWorking('有个步骤失败了,已展开详情');
|
|
1644
2095
|
} else {
|
|
1645
|
-
setWorking('
|
|
2096
|
+
setWorking('正在整理结果…');
|
|
1646
2097
|
}
|
|
2098
|
+
scrollToEnd();
|
|
1647
2099
|
}
|
|
1648
2100
|
} else if (event.type === 'step') {
|
|
1649
|
-
setStatus(
|
|
1650
|
-
setWorking(
|
|
2101
|
+
setStatus('正在处理…');
|
|
2102
|
+
setWorking('正在处理…');
|
|
1651
2103
|
working.hidden = false;
|
|
1652
2104
|
} else if (event.type === 'status') {
|
|
1653
2105
|
if (event.message) {
|
|
@@ -1658,6 +2110,7 @@ async function send() {
|
|
|
1658
2110
|
} else if (event.type === 'todos') {
|
|
1659
2111
|
if (state.info?.session) state.info.session.todos = event.todos ?? [];
|
|
1660
2112
|
renderTodos();
|
|
2113
|
+
scrollToEnd();
|
|
1661
2114
|
} else if (event.type === 'approval') {
|
|
1662
2115
|
const allowed = await askApproval(event);
|
|
1663
2116
|
await post('approve', { id: event.id, allowed });
|
|
@@ -1666,7 +2119,7 @@ async function send() {
|
|
|
1666
2119
|
finishReasoning();
|
|
1667
2120
|
finishWork();
|
|
1668
2121
|
body.classList.remove('caret');
|
|
1669
|
-
|
|
2122
|
+
answerStream.setError(markdown(`**${escapeHtml(event.message ?? '出错了')}**`));
|
|
1670
2123
|
if (event.hint) metaLine('提示', event.hint);
|
|
1671
2124
|
if (event.code === 'no_api_key') openSettings();
|
|
1672
2125
|
streamEnded = true;
|
|
@@ -1679,26 +2132,37 @@ async function send() {
|
|
|
1679
2132
|
}
|
|
1680
2133
|
body.classList.remove('caret');
|
|
1681
2134
|
streamEnded = true;
|
|
1682
|
-
if (event.error)
|
|
1683
|
-
else if (!buffer.trim())
|
|
1684
|
-
else
|
|
2135
|
+
if (event.error) answerStream.setError(markdown(`**请求失败** ${escapeHtml(event.error)}`));
|
|
2136
|
+
else if (!buffer.trim()) answerStream.setError(markdown('这一轮没有返回内容。'));
|
|
2137
|
+
else answerStream.finish(buffer);
|
|
1685
2138
|
if (buffer.trim() && !botMsg.querySelector('.msg-actions')) {
|
|
1686
2139
|
messageActions(botMsg, buffer, 'assistant');
|
|
1687
2140
|
}
|
|
1688
|
-
metaLine('步数', event.steps);
|
|
1689
2141
|
metaLine('用时', (event.ms / 1000).toFixed(1) + 's');
|
|
2142
|
+
if (event.steps > 0) metaLine('过程', `${event.steps} 步`);
|
|
1690
2143
|
if (event.cost && event.cost !== '≈$0') metaLine('花费', event.cost);
|
|
1691
|
-
|
|
1692
|
-
if (event.
|
|
1693
|
-
|
|
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
|
+
}
|
|
1694
2153
|
const { sessions } = await get('sessions');
|
|
1695
2154
|
state.sessions = sessions;
|
|
1696
2155
|
renderSidebar();
|
|
1697
2156
|
renderTabs();
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
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
|
+
}
|
|
1702
2166
|
}
|
|
1703
2167
|
}
|
|
1704
2168
|
}
|
|
@@ -1708,8 +2172,8 @@ async function send() {
|
|
|
1708
2172
|
finishReasoning();
|
|
1709
2173
|
finishWork();
|
|
1710
2174
|
body.classList.remove('caret');
|
|
1711
|
-
if (
|
|
1712
|
-
|
|
2175
|
+
if (!buffer.trim() && workBody.childElementCount === 0) {
|
|
2176
|
+
answerStream.setError(markdown('这一轮没有返回内容。可以重试,或按发送键停止后换个说法。'));
|
|
1713
2177
|
}
|
|
1714
2178
|
await refresh();
|
|
1715
2179
|
}
|
|
@@ -1717,11 +2181,11 @@ async function send() {
|
|
|
1717
2181
|
body.classList.remove('caret');
|
|
1718
2182
|
const aborted = controller?.signal.aborted || /abort/i.test(String(error?.message ?? ''));
|
|
1719
2183
|
if (aborted) {
|
|
1720
|
-
|
|
2184
|
+
answerStream.setError(markdown(buffer.trim() || '已停止生成。'));
|
|
1721
2185
|
metaLine('状态', '已停止');
|
|
1722
2186
|
streamEnded = true;
|
|
1723
2187
|
} else if (!buffer.trim()) {
|
|
1724
|
-
|
|
2188
|
+
answerStream.setError(markdown(`**中断** ${escapeHtml(error.message)}`));
|
|
1725
2189
|
}
|
|
1726
2190
|
await refresh();
|
|
1727
2191
|
} finally {
|
|
@@ -1910,9 +2374,111 @@ async function requestFileComplete(value) {
|
|
|
1910
2374
|
}, 90);
|
|
1911
2375
|
}
|
|
1912
2376
|
|
|
1913
|
-
|
|
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
|
+
/** 执行命令:会话类命令直接调用界面的同一套逻辑,避免前后端状态脱节。 */
|
|
1914
2453
|
async function runCommand(text) {
|
|
1915
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();
|
|
1916
2482
|
const userMsg = document.createElement('article');
|
|
1917
2483
|
userMsg.className = 'msg user cmd';
|
|
1918
2484
|
userMsg.innerHTML = '<div class="msg-role">你</div>';
|
|
@@ -1920,8 +2486,12 @@ async function runCommand(text) {
|
|
|
1920
2486
|
userBody.className = 'msg-body';
|
|
1921
2487
|
userBody.textContent = text;
|
|
1922
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
|
+
}
|
|
1923
2494
|
THREAD.appendChild(userMsg);
|
|
1924
|
-
updateHomeMode();
|
|
1925
2495
|
|
|
1926
2496
|
const botMsg = document.createElement('article');
|
|
1927
2497
|
botMsg.className = 'msg assistant enter';
|
|
@@ -1937,15 +2507,26 @@ async function runCommand(text) {
|
|
|
1937
2507
|
|
|
1938
2508
|
try {
|
|
1939
2509
|
const result = await post('command', { input: text });
|
|
1940
|
-
|
|
2510
|
+
// CLI 在 TTY 下会输出 ANSI 颜色;GUI 里统一去掉,避免出现 [36m 之类的乱码。
|
|
2511
|
+
const cleaned = String(result.output ?? '').replace(/\u001b\[[0-9;?]*[ -/]*[@-~]/g, '');
|
|
2512
|
+
out.textContent = cleaned || (result.ok ? '(没有输出)' : '');
|
|
1941
2513
|
out.classList.toggle('is-error', !result.ok);
|
|
1942
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;
|
|
1943
2519
|
if (result.info) state.info = result.info;
|
|
1944
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
|
+
}
|
|
1945
2525
|
// \u001b[clear 之类的终端指令:清空当前视图
|
|
1946
2526
|
if ((result.output || '').includes('\u001b[clear]')) renderThread();
|
|
1947
2527
|
renderHeader();
|
|
1948
2528
|
renderSidebar();
|
|
2529
|
+
renderTodos();
|
|
1949
2530
|
} catch (error) {
|
|
1950
2531
|
out.textContent = error.message;
|
|
1951
2532
|
out.classList.add('is-error');
|
|
@@ -2022,6 +2603,7 @@ function onModelPopOutside(event) {
|
|
|
2022
2603
|
function onModelPopKey(event) {
|
|
2023
2604
|
if (event.key === 'Escape') {
|
|
2024
2605
|
event.preventDefault();
|
|
2606
|
+
event.stopPropagation();
|
|
2025
2607
|
closeModelPop();
|
|
2026
2608
|
$('modelBtn')?.focus();
|
|
2027
2609
|
}
|
|
@@ -2130,7 +2712,7 @@ function openSettings(category) {
|
|
|
2130
2712
|
if (category) state.settingsCategory = category;
|
|
2131
2713
|
state.settingsScroll = 0;
|
|
2132
2714
|
state.settingsReturn = document.activeElement;
|
|
2133
|
-
renderSettings();
|
|
2715
|
+
renderSettings({ resetScroll: true });
|
|
2134
2716
|
const sheet = $('settingsSheet');
|
|
2135
2717
|
sheet.classList.remove('is-closing');
|
|
2136
2718
|
sheet.hidden = false;
|
|
@@ -2265,12 +2847,13 @@ async function saveSetting(action, payload) {
|
|
|
2265
2847
|
}
|
|
2266
2848
|
}
|
|
2267
2849
|
|
|
2268
|
-
function renderSettings() {
|
|
2850
|
+
function renderSettings({ resetScroll = false } = {}) {
|
|
2269
2851
|
const info = state.info;
|
|
2270
2852
|
if (!info) return;
|
|
2271
2853
|
const body = $('settingsBody');
|
|
2272
2854
|
const nav = $('settingsNav');
|
|
2273
|
-
|
|
2855
|
+
// 切换分类必须回到顶部;保存设置时保留当前位置。
|
|
2856
|
+
const scroll = resetScroll ? 0 : body.scrollTop || state.settingsScroll || 0;
|
|
2274
2857
|
nav.innerHTML = '';
|
|
2275
2858
|
for (const item of SETTINGS_NAV) {
|
|
2276
2859
|
const button = document.createElement('button');
|
|
@@ -2280,8 +2863,7 @@ function renderSettings() {
|
|
|
2280
2863
|
if (state.settingsCategory === item.id) return;
|
|
2281
2864
|
state.settingsCategory = item.id;
|
|
2282
2865
|
state.settingsScroll = 0;
|
|
2283
|
-
renderSettings();
|
|
2284
|
-
body.scrollTop = 0;
|
|
2866
|
+
renderSettings({ resetScroll: true });
|
|
2285
2867
|
});
|
|
2286
2868
|
nav.appendChild(button);
|
|
2287
2869
|
}
|
|
@@ -2478,7 +3060,7 @@ function renderTerminalSettings(body) {
|
|
|
2478
3060
|
if (isTermOpen) termClose();
|
|
2479
3061
|
else termOpen();
|
|
2480
3062
|
}, true);
|
|
2481
|
-
card.appendChild(settingsRow('终端面板', '
|
|
3063
|
+
card.appendChild(settingsRow('终端面板', '在底部 dock 打开,不会挡住输入框。快捷键 Ctrl+`。', openBtn));
|
|
2482
3064
|
section.appendChild(card);
|
|
2483
3065
|
|
|
2484
3066
|
const help = settingsSection('常用快捷键', '');
|
|
@@ -2719,7 +3301,7 @@ function renderAboutSettings(body) {
|
|
|
2719
3301
|
const info = state.info;
|
|
2720
3302
|
const section = settingsSection('关于 Banana', '一个把 CLI 内核完整搬进图形界面的本地 agent。');
|
|
2721
3303
|
const card = settingsCard();
|
|
2722
|
-
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'))));
|
|
2723
3305
|
card.appendChild(settingsRow('工作区', info.workspace ?? '', null));
|
|
2724
3306
|
card.appendChild(settingsRow('工具数量', `${info.tools?.length ?? 0} 个`, null));
|
|
2725
3307
|
card.appendChild(settingsRow('平台', info.platform === 'win32' ? 'Windows' : info.platform === 'darwin' ? 'macOS' : 'Linux', null));
|
|
@@ -3294,7 +3876,8 @@ async function termLoadShells() {
|
|
|
3294
3876
|
select.addEventListener('change', () => {
|
|
3295
3877
|
localStorage.setItem(TERM_STORAGE.shell, select.value);
|
|
3296
3878
|
closeTermMenu();
|
|
3297
|
-
|
|
3879
|
+
// 这里只记录“新标签”用哪个 shell;真正新建走 + 按钮。
|
|
3880
|
+
toast('新标签将使用 ' + (term.shells.find((shell) => shell.id === select.value)?.name ?? select.value));
|
|
3298
3881
|
});
|
|
3299
3882
|
} catch {
|
|
3300
3883
|
term.shells = [{ id: 'cmd', name: '终端' }];
|
|
@@ -3310,7 +3893,17 @@ function termRenderTabs() {
|
|
|
3310
3893
|
for (const tab of term.tabs) {
|
|
3311
3894
|
const node = document.createElement('div');
|
|
3312
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));
|
|
3313
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
|
+
});
|
|
3314
3907
|
node.addEventListener('click', (event) => {
|
|
3315
3908
|
if (event.target.closest('.term-tab-close')) {
|
|
3316
3909
|
event.stopPropagation();
|
|
@@ -3383,6 +3976,8 @@ function termConnect(tab) {
|
|
|
3383
3976
|
} else if (message.type === 'data') {
|
|
3384
3977
|
tab.terminal.write(message.data);
|
|
3385
3978
|
} else if (message.type === 'exit') {
|
|
3979
|
+
// 用户主动 exit 后不要自动重连,否则关闭的 shell 会悄悄重启。
|
|
3980
|
+
tab.alive = false;
|
|
3386
3981
|
tab.terminal.write(`\r\n\x1b[90m[进程已退出,代码 ${message.code ?? 0}]\x1b[0m\r\n`);
|
|
3387
3982
|
} else if (message.type === 'error') {
|
|
3388
3983
|
tab.terminal.write(`\r\n\x1b[31m${message.message}\x1b[0m\r\n`);
|
|
@@ -3642,6 +4237,7 @@ function bindTerminal() {
|
|
|
3642
4237
|
function closeTermMenu() {
|
|
3643
4238
|
const menu = $('termMenu');
|
|
3644
4239
|
if (menu) menu.hidden = true;
|
|
4240
|
+
$('termMore')?.setAttribute('aria-expanded', 'false');
|
|
3645
4241
|
}
|
|
3646
4242
|
|
|
3647
4243
|
// ---------------- 命令中心(Ctrl+K) ----------------
|
|
@@ -3652,7 +4248,7 @@ function ccActions() {
|
|
|
3652
4248
|
{ title: '新建对话', sub: '开始一个干净的会话', icon: 'plus', run: () => $('newChat').click() },
|
|
3653
4249
|
{ title: '打开项目文件', sub: '浏览、全屏查看、引用文件', icon: 'panel', run: openFiles },
|
|
3654
4250
|
{ title: '查看本会话改动', sub: '按文件查看增删行,审阅 agent 的修改', icon: 'diff', run: openChanges },
|
|
3655
|
-
{ title: '打开终端', sub: '
|
|
4251
|
+
{ title: '打开终端', sub: '底部 dock,不遮挡输入框', icon: 'terminal', run: termOpen },
|
|
3656
4252
|
{ title: '打开控制中心', sub: '模型、外观、终端、工作区、插件', icon: 'sliders', run: () => openSettings() },
|
|
3657
4253
|
{ title: '插件工坊', sub: '启用、停用、生成模板、试运行插件', icon: 'plug', run: () => openSettings('plugins') },
|
|
3658
4254
|
{ title: '切换主题', sub: '浅色 / 深色 / 跟随系统', icon: 'sun', run: cycleTheme },
|
|
@@ -3858,6 +4454,8 @@ function initTooltips() {
|
|
|
3858
4454
|
const title = node.getAttribute('title');
|
|
3859
4455
|
if (title) {
|
|
3860
4456
|
node.dataset.tip = title;
|
|
4457
|
+
// 移除原生 title 前保留可访问名称,避免 hover 一次后读屏软件失去描述。
|
|
4458
|
+
if (!node.hasAttribute('aria-label')) node.setAttribute('aria-label', title);
|
|
3861
4459
|
node.removeAttribute('title');
|
|
3862
4460
|
}
|
|
3863
4461
|
const text = node.dataset.tip;
|
|
@@ -4020,23 +4618,8 @@ function bind() {
|
|
|
4020
4618
|
hydrate();
|
|
4021
4619
|
loadTheme();
|
|
4022
4620
|
|
|
4023
|
-
$('newChat').addEventListener('click',
|
|
4024
|
-
|
|
4025
|
-
// 直接用返回的会话,不再 refresh:多一次 lastActive 解析就多一次串回旧对话的机会
|
|
4026
|
-
const result = await post('session', { action: 'new' });
|
|
4027
|
-
if (result?.info) state.info = result.info;
|
|
4028
|
-
if (result?.sessions) state.sessions = result.sessions;
|
|
4029
|
-
state.changes = null;
|
|
4030
|
-
renderHeader();
|
|
4031
|
-
renderSidebar();
|
|
4032
|
-
renderThread();
|
|
4033
|
-
if (state.dock.open) termClose();
|
|
4034
|
-
closeSidebarIfOverlay();
|
|
4035
|
-
setActiveTab(state.info?.session?.id ?? null);
|
|
4036
|
-
$('input').focus();
|
|
4037
|
-
} catch (error) {
|
|
4038
|
-
toast('新建对话失败:' + (error?.message ?? error));
|
|
4039
|
-
}
|
|
4621
|
+
$('newChat').addEventListener('click', () => {
|
|
4622
|
+
void createSession();
|
|
4040
4623
|
});
|
|
4041
4624
|
$('openSettings').addEventListener('click', () => openSettings());
|
|
4042
4625
|
$('closeSettings').addEventListener('click', closeSettings);
|
|
@@ -4089,10 +4672,18 @@ function bind() {
|
|
|
4089
4672
|
$('approvalYes').addEventListener('click', () => closeApproval(true));
|
|
4090
4673
|
|
|
4091
4674
|
$('undoBtn').addEventListener('click', async () => {
|
|
4092
|
-
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
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
|
+
}
|
|
4096
4687
|
});
|
|
4097
4688
|
|
|
4098
4689
|
$('composer').addEventListener('submit', (event) => { event.preventDefault(); send(); });
|
|
@@ -4117,14 +4708,75 @@ function bind() {
|
|
|
4117
4708
|
});
|
|
4118
4709
|
$('attachmentCopyPath').addEventListener('click', () => copyText(state.attachmentPath ?? '', '已复制附件路径'));
|
|
4119
4710
|
$('jumpLatest').addEventListener('click', () => {
|
|
4120
|
-
|
|
4121
|
-
|
|
4711
|
+
setCameraFollow(true);
|
|
4712
|
+
$('jumpLatest').hidden = true;
|
|
4713
|
+
scrollToEnd(true, true);
|
|
4714
|
+
});
|
|
4715
|
+
THREAD.addEventListener(
|
|
4716
|
+
'wheel',
|
|
4717
|
+
(event) => {
|
|
4718
|
+
if (event.deltaY < 0) {
|
|
4719
|
+
setCameraFollow(false);
|
|
4720
|
+
camera.lastUserScroll = Date.now();
|
|
4721
|
+
return;
|
|
4722
|
+
}
|
|
4723
|
+
const atBottom = cameraMax() - THREAD.scrollTop < 90;
|
|
4724
|
+
if (atBottom) {
|
|
4725
|
+
setCameraFollow(true);
|
|
4726
|
+
$('jumpLatest').hidden = true;
|
|
4727
|
+
}
|
|
4728
|
+
},
|
|
4729
|
+
{ passive: true },
|
|
4730
|
+
);
|
|
4731
|
+
let touchY = 0;
|
|
4732
|
+
THREAD.addEventListener(
|
|
4733
|
+
'touchstart',
|
|
4734
|
+
(event) => {
|
|
4735
|
+
touchY = event.touches[0]?.clientY ?? 0;
|
|
4736
|
+
},
|
|
4737
|
+
{ passive: true },
|
|
4738
|
+
);
|
|
4739
|
+
THREAD.addEventListener(
|
|
4740
|
+
'touchmove',
|
|
4741
|
+
(event) => {
|
|
4742
|
+
const nextY = event.touches[0]?.clientY ?? touchY;
|
|
4743
|
+
if (nextY > touchY + 4) {
|
|
4744
|
+
setCameraFollow(false);
|
|
4745
|
+
camera.lastUserScroll = Date.now();
|
|
4746
|
+
}
|
|
4747
|
+
touchY = nextY;
|
|
4748
|
+
},
|
|
4749
|
+
{ passive: true },
|
|
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
|
+
}
|
|
4122
4758
|
});
|
|
4123
4759
|
THREAD.addEventListener('scroll', () => {
|
|
4124
|
-
const
|
|
4125
|
-
|
|
4760
|
+
const top = THREAD.scrollTop;
|
|
4761
|
+
// 没有相机动画时,向上滚动(滚动条、键盘、查找)也要暂停跟随。
|
|
4762
|
+
if (!camera.running && top < camera.lastTop - 4) {
|
|
4763
|
+
setCameraFollow(false);
|
|
4764
|
+
camera.lastUserScroll = Date.now();
|
|
4765
|
+
}
|
|
4766
|
+
camera.lastTop = top;
|
|
4767
|
+
const atBottom = cameraMax() - top < 70;
|
|
4768
|
+
if (atBottom && !camera.follow && Date.now() - camera.lastUserScroll > 300) {
|
|
4769
|
+
setCameraFollow(true);
|
|
4770
|
+
}
|
|
4126
4771
|
const hasMessages = Boolean(THREAD.querySelector('.msg'));
|
|
4127
|
-
$('jumpLatest').hidden =
|
|
4772
|
+
$('jumpLatest').hidden = camera.follow || !hasMessages;
|
|
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();
|
|
4128
4780
|
});
|
|
4129
4781
|
$('input').addEventListener('input', () => {
|
|
4130
4782
|
grow();
|
|
@@ -4204,6 +4856,23 @@ function bind() {
|
|
|
4204
4856
|
}
|
|
4205
4857
|
if (event.key === 'Escape') {
|
|
4206
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
|
+
}
|
|
4207
4876
|
// 终端里的 Esc 属于 shell,不要把工作台一起关掉
|
|
4208
4877
|
const inTerminal = event.target instanceof Element && event.target.closest('#terminalPanel');
|
|
4209
4878
|
if (inTerminal && $('settingsSheet').hidden && $('codeViewer').hidden && $('commandCenter').hidden && $('approvalModal').hidden) return;
|
|
@@ -4239,7 +4908,9 @@ async function boot() {
|
|
|
4239
4908
|
bind();
|
|
4240
4909
|
// 命令表来自服务端(与 CLI 同一份),拉不到也不影响对话
|
|
4241
4910
|
try {
|
|
4242
|
-
|
|
4911
|
+
const all = (await get('commands')).commands ?? [];
|
|
4912
|
+
// 只暴露 GUI 真正能执行的命令;否则会出现“显示成功但没生效”。
|
|
4913
|
+
commandList = all.filter((item) => GUI_COMMANDS.has(item.name));
|
|
4243
4914
|
} catch {
|
|
4244
4915
|
commandList = [];
|
|
4245
4916
|
}
|