@vincentbanana/bananacli 0.6.0 → 0.7.0

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/ui/app.js CHANGED
@@ -8,6 +8,7 @@ const ICONS = {
8
8
  send: '<path d="M12 19V5M6 11l6-6 6 6"/>',
9
9
  stop: '<rect x="7" y="7" width="10" height="10" rx="1.5"/>',
10
10
  chip: '<rect x="7" y="7" width="10" height="10" rx="2"/><path d="M10 4v3M14 4v3M10 17v3M14 17v3M4 10h3M4 14h3M17 10h3M17 14h3"/>',
11
+ chevronDown: '<path d="M6 9l6 6 6-6"/>',
11
12
  sliders: '<path d="M4 8h8M16 8h4M4 16h4M12 16h8"/><circle cx="14" cy="8" r="2"/><circle cx="10" cy="16" r="2"/>',
12
13
  undo: '<path d="M9 8H4m0 0 3.5-3.5M4 8h9a6 6 0 0 1 0 12h-4"/>',
13
14
  trash: '<path d="M5 7h14M10 7V5h4v2M7 7l1 13h8l1-13"/>',
@@ -42,6 +43,9 @@ const ICONS = {
42
43
  restore: '<rect x="5" y="8" width="9" height="9" rx="1"/><path d="M9 5h9v9"/>',
43
44
  diff: '<path d="M6 4v16M18 4v16M3 8h6M15 16h6M3 16h6M15 8h6"/>',
44
45
  refresh: '<path d="M20 11a8 8 0 1 0-2.3 5.7"/><path d="M20 5v6h-6"/>',
46
+ history: '<path d="M3.5 12a8.5 8.5 0 1 0 2.8-6.3"/><path d="M3.5 4.5v5h5"/><path d="M12 8v4.5l3 1.8"/>',
47
+ attach: '<path d="M20 11.5l-7.8 7.8a5 5 0 0 1-7-7L13 4.5a3.4 3.4 0 0 1 4.8 4.8L10 17a1.8 1.8 0 0 1-2.5-2.5l6.7-6.7"/>',
48
+ arrowDown: '<path d="M12 5v14M6 13l6 6 6-6"/>',
45
49
  };
46
50
 
47
51
  const TOOL_ICON = {
@@ -180,6 +184,9 @@ const state = {
180
184
  activeTab: null,
181
185
  review: { open: false, mode: 'changes', width: 520 },
182
186
  sidebar: { open: true, width: 344 },
187
+ sessionQuery: '',
188
+ status: '',
189
+ stickToBottom: true,
183
190
  dock: { open: false, height: 280 },
184
191
  changes: null,
185
192
  changesFile: null,
@@ -220,7 +227,7 @@ function relativeTime(iso) {
220
227
 
221
228
  /** 窄屏下侧边栏是覆盖层,选完会话必须收起来,否则挡住对话。 */
222
229
  function closeSidebarIfOverlay() {
223
- if (window.matchMedia('(max-width: 900px)').matches) $('sidebar').classList.remove('open');
230
+ if (window.matchMedia('(max-width: 900px)').matches && state.sidebar.open) setSidebar(false);
224
231
  }
225
232
 
226
233
  let switching = false;
@@ -297,8 +304,10 @@ function renderTabs() {
297
304
  node.draggable = true;
298
305
  node.dataset.id = tab.id;
299
306
  const title = tabTitleFor(tab.id) || tab.title || '新对话';
307
+ const isNew = title === '新对话';
308
+ node.classList.toggle('is-running', state.busy && tab.id === state.activeTab);
300
309
  node.innerHTML =
301
- `<span class="tab-avatar">${escapeHtml(title.slice(0, 1))}</span>` +
310
+ `<span class="tab-avatar${isNew ? ' is-new' : ''}">${isNew ? '' : escapeHtml(title.slice(0, 1))}</span>` +
302
311
  `<span class="tab-title">${escapeHtml(title)}</span>` +
303
312
  `<span class="tab-close" title="关闭标签"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></span>`;
304
313
  node.addEventListener('pointerdown', (event) => {
@@ -391,7 +400,11 @@ function renderSidebar() {
391
400
  if (value >= start - 86_400_000) return '昨天';
392
401
  return '更早';
393
402
  };
394
- state.sessions.forEach((item, index) => {
403
+ const sessions = state.sessions.filter((item) => {
404
+ const q = state.sessionQuery.trim().toLowerCase();
405
+ return !q || String(item.title ?? '').toLowerCase().includes(q);
406
+ });
407
+ sessions.forEach((item, index) => {
395
408
  const label = dayOf(item.updatedAt);
396
409
  if (label !== group) {
397
410
  group = label;
@@ -518,6 +531,50 @@ function downloadSession(format, id) {
518
531
  toast(`已导出 ${format === 'json' ? 'JSON' : 'Markdown'}`);
519
532
  }
520
533
 
534
+ function toolSummaryText(call) {
535
+ const input = call.input ?? {};
536
+ if (typeof input.command === 'string') return input.command;
537
+ return JSON.stringify(input).slice(0, 110);
538
+ }
539
+
540
+ function ensureToolOutput(row, initial = '', isError = false) {
541
+ let out = row.nextElementSibling;
542
+ if (!out || !out.classList.contains('tool-output')) {
543
+ out = document.createElement('div');
544
+ out.className = 'tool-output';
545
+ out.hidden = true;
546
+ row.after(out);
547
+ }
548
+ out.classList.toggle('is-error', isError);
549
+ if (initial) out.textContent = initial;
550
+ row.classList.add('is-clickable');
551
+ row.title = '点击展开命令输出';
552
+ if (!row.dataset.bound) {
553
+ row.dataset.bound = '1';
554
+ row.addEventListener('click', () => {
555
+ const box = row.nextElementSibling;
556
+ if (!box?.classList.contains('tool-output')) return;
557
+ const open = box.hidden;
558
+ box.hidden = !open;
559
+ row.classList.toggle('is-open', open);
560
+ row.title = open ? '点击收起命令输出' : '点击展开命令输出';
561
+ });
562
+ }
563
+ return out;
564
+ }
565
+
566
+ function toolRowNode(name, detail, stateHtml, ms) {
567
+ const row = document.createElement('div');
568
+ row.className = 'tool-row';
569
+ row.innerHTML =
570
+ `<span class="t-ico">${svg(TOOL_ICON[name] ?? 'file')}</span>` +
571
+ `<span class="t-name">${escapeHtml(name)}</span>` +
572
+ `<span class="t-detail">${escapeHtml(detail)}</span>` +
573
+ `<span class="t-state">${stateHtml}</span>` +
574
+ (ms ? `<span class="t-time">${(ms / 1000).toFixed(1)}s</span>` : '');
575
+ return row;
576
+ }
577
+
521
578
  function toolGroup(calls, results) {
522
579
  const wrap = document.createElement('details');
523
580
  wrap.className = 'work-group';
@@ -532,25 +589,47 @@ function toolGroup(calls, results) {
532
589
  body.className = 'tool-group-body';
533
590
  for (const call of calls) {
534
591
  const result = results.get(call.id);
535
- const row = document.createElement('div');
536
- row.className = `tool-row${result && result.isError ? ' is-error' : ''}`;
537
- const detail = JSON.stringify(call.input ?? {}).slice(0, 90);
538
- row.innerHTML =
539
- `<span class="t-ico">${svg(TOOL_ICON[call.name] ?? 'file')}</span>` +
540
- `<span class="t-name">${escapeHtml(call.name)}</span>` +
541
- `<span class="t-detail">${escapeHtml(detail)}</span>` +
542
- `<span class="t-state">${result ? (result.isError ? '失败' : escapeHtml(result.content?.slice(0, 40) ?? '完成')) : '已调用'}</span>`;
543
- if (result?.content) {
544
- row.classList.add('is-clickable');
545
- row.title = '点击展开输出';
546
- row.addEventListener('click', () => toggleToolOutput(row, result.content, Boolean(result.isError)));
547
- }
592
+ const row = toolRowNode(
593
+ call.name,
594
+ toolSummaryText(call),
595
+ result ? (result.isError ? '失败' : '完成') : '已调用',
596
+ );
597
+ row.classList.toggle('is-error', Boolean(result?.isError));
598
+ if (result?.content) ensureToolOutput(row, result.content, Boolean(result.isError));
548
599
  body.appendChild(row);
549
600
  }
550
601
  wrap.append(summary, body);
551
602
  return wrap;
552
603
  }
553
604
 
605
+ function reasoningDetails(text) {
606
+ const details = document.createElement('details');
607
+ details.className = 'think';
608
+ const summary = document.createElement('summary');
609
+ summary.innerHTML =
610
+ '<span class="think-title">思考</span>' +
611
+ `<span class="think-len">${Math.max(1, Math.round(text.length / 1000))}k 字</span>`;
612
+ const body = document.createElement('div');
613
+ body.className = 'think-body';
614
+ body.innerHTML = markdown(text);
615
+ details.append(summary, body);
616
+ return details;
617
+ }
618
+
619
+ function messageActions(node, text, role) {
620
+ const actions = document.createElement('div');
621
+ actions.className = `msg-actions ${role === 'user' ? 'is-user' : 'is-assistant'}`;
622
+ const button = document.createElement('button');
623
+ button.type = 'button';
624
+ button.className = 'msg-action';
625
+ button.title = '复制';
626
+ button.setAttribute('aria-label', '复制');
627
+ button.innerHTML = svg('copy');
628
+ button.addEventListener('click', () => copyText(text ?? '', '已复制'));
629
+ actions.appendChild(button);
630
+ node.appendChild(actions);
631
+ }
632
+
554
633
  function renderMessage(message) {
555
634
  if (message.role === 'tool') return null;
556
635
  const node = document.createElement('article');
@@ -564,6 +643,9 @@ function renderMessage(message) {
564
643
  body.textContent = message.content;
565
644
  } else {
566
645
  body.innerHTML = markdown(message.content || '');
646
+ if (message.reasoning && state.info?.showReasoning !== false) {
647
+ body.insertBefore(reasoningDetails(message.reasoning), body.firstChild);
648
+ }
567
649
  if (message.toolCalls?.length) {
568
650
  const results = new Map(
569
651
  (state.info?.session?.messages ?? []).filter((m) => m.role === 'tool').map((m) => [m.toolCallId, m]),
@@ -572,6 +654,7 @@ function renderMessage(message) {
572
654
  }
573
655
  }
574
656
  node.append(role, body);
657
+ if (message.content?.trim()) messageActions(node, message.content, message.role);
575
658
  return node;
576
659
  }
577
660
 
@@ -619,6 +702,8 @@ function renderEmpty() {
619
702
 
620
703
  function renderThread() {
621
704
  THREAD.innerHTML = '';
705
+ state.stickToBottom = true;
706
+ $('jumpLatest').hidden = true;
622
707
  const messages = state.info?.session?.messages ?? [];
623
708
  if (messages.length === 0) THREAD.appendChild(renderEmpty());
624
709
  else {
@@ -638,6 +723,7 @@ function renderThread() {
638
723
 
639
724
  function renderTurnMeta() {
640
725
  const meta = $('threadMeta');
726
+ if (state.busy && state.status) { meta.textContent = state.status; return; }
641
727
  const usage = state.info?.session?.usage;
642
728
  if (!usage || (!usage.inputTokens && !usage.outputTokens)) { meta.textContent = ''; return; }
643
729
  const parts = [`↑${usage.inputTokens} ↓${usage.outputTokens}`];
@@ -645,19 +731,37 @@ function renderTurnMeta() {
645
731
  meta.textContent = parts.join(' · ');
646
732
  }
647
733
 
648
- function scrollToEnd(smooth = true) {
734
+ function scrollToEnd(smooth = true, force = false) {
735
+ if (!force && state.busy && !state.stickToBottom) return;
649
736
  requestAnimationFrame(() => {
650
737
  THREAD.scrollTo({ top: THREAD.scrollHeight, behavior: smooth ? 'smooth' : 'auto' });
651
738
  });
652
739
  }
653
740
 
741
+ function setStatus(text) {
742
+ state.status = text || '';
743
+ const hint = $('composerHint');
744
+ if (hint) {
745
+ hint.textContent = text || workspaceHint();
746
+ hint.dataset.kind = text ? 'live' : 'idle';
747
+ }
748
+ const meta = $('threadMeta');
749
+ if (meta && text && state.busy) meta.textContent = text;
750
+ }
751
+
752
+ function workspaceHint() {
753
+ const info = state.info;
754
+ if (!info) return '';
755
+ return `工作区 ${info.workspace} · ${info.tools.length} 个工具 · shell=${info.permissions.shell} write=${info.permissions.write} network=${info.permissions.network}`;
756
+ }
757
+
654
758
  function renderHeader() {
655
759
  const info = state.info;
656
760
  if (!info) return;
657
761
  $('threadTitle').textContent = info.session.title || '新对话';
658
762
  $('modelLabel').textContent = `${info.provider.name} / ${info.provider.model}`;
659
- $('composerHint').textContent =
660
- `工作区 ${info.workspace} · ${info.tools.length} 个工具 · shell=${info.permissions.shell} write=${info.permissions.write} network=${info.permissions.network}`;
763
+ if (!state.busy) $('composerHint').textContent = workspaceHint();
764
+ else if (state.status) $('composerHint').textContent = state.status;
661
765
  renderTurnMeta();
662
766
  }
663
767
 
@@ -848,14 +952,31 @@ function grow() {
848
952
  const input = $('input');
849
953
  input.style.height = 'auto';
850
954
  input.style.height = Math.min(input.scrollHeight, 220) + 'px';
851
- $('sendBtn').disabled = state.busy || input.value.trim() === '';
955
+ const send = $('sendBtn');
956
+ if (!state.busy) send.disabled = input.value.trim() === '';
957
+ }
958
+
959
+ function stopTurn() {
960
+ if (!state.busy) return;
961
+ setStatus('正在停止…');
962
+ controller?.abort();
963
+ void post('abort').catch(() => {});
852
964
  }
853
965
 
854
966
  function setBusy(busy) {
855
967
  state.busy = busy;
856
- $('abortBtn').hidden = !busy;
857
- $('sendBtn').disabled = busy;
968
+ const send = $('sendBtn');
969
+ const composer = $('composer');
970
+ composer?.classList.toggle('is-busy', busy);
971
+ // 发送键在原位切换成停止键,不再让用户去右上角找
972
+ send.innerHTML = busy ? svg('stop') : svg('send');
973
+ send.title = busy ? '停止生成' : '发送';
974
+ send.setAttribute('aria-label', busy ? '停止生成' : '发送');
975
+ send.dataset.mode = busy ? 'stop' : 'send';
976
+ send.disabled = busy ? false : $('input').value.trim() === '';
977
+ $('abortBtn').hidden = true;
858
978
  document.body.style.cursor = busy ? 'progress' : '';
979
+ if (state.tabs?.length) renderTabs();
859
980
  }
860
981
 
861
982
  function askApproval(payload) {
@@ -882,6 +1003,7 @@ async function send() {
882
1003
  const input = $('input');
883
1004
  const text = input.value.trim();
884
1005
  if (!text || state.busy) return;
1006
+ if (!state.info?.session) return;
885
1007
  input.value = '';
886
1008
  grow();
887
1009
  closePalette();
@@ -892,11 +1014,17 @@ async function send() {
892
1014
  return;
893
1015
  }
894
1016
 
895
- if (state.info.session.messages.length === 0 && state.info.session.title === '新对话') {
1017
+ const messages = state.info.session.messages ?? (state.info.session.messages = []);
1018
+ if (messages.length === 0 && state.info.session.title === '新对话') {
896
1019
  state.info.session.title = text.slice(0, 40);
897
1020
  renderHeader();
898
1021
  }
899
- renderEmpty();
1022
+
1023
+ // 首页空状态整块移除,否则第一条消息会被追加在 wordmark 下面
1024
+ THREAD.querySelector('.empty')?.remove();
1025
+ $('chatCard').classList.remove('home-mode');
1026
+ $('homeTips').hidden = true;
1027
+ messages.push({ role: 'user', content: text });
900
1028
 
901
1029
  const userMsg = document.createElement('article');
902
1030
  userMsg.className = 'msg user enter';
@@ -905,8 +1033,12 @@ async function send() {
905
1033
  userBody.className = 'msg-body';
906
1034
  userBody.textContent = text;
907
1035
  userMsg.appendChild(userBody);
1036
+ messageActions(userMsg, text, 'user');
1037
+ const divider = document.createElement('div');
1038
+ divider.className = 'thread-divider';
1039
+ if (!THREAD.querySelector('.thread-divider')) THREAD.appendChild(divider);
908
1040
  THREAD.appendChild(userMsg);
909
- updateHomeMode();
1041
+ scrollToEnd(false);
910
1042
 
911
1043
  const botMsg = document.createElement('article');
912
1044
  botMsg.className = 'msg assistant enter';
@@ -922,39 +1054,78 @@ async function send() {
922
1054
  const workBody = document.createElement('div');
923
1055
  workBody.className = 'tool-group-body';
924
1056
  work.append(workSummary, workBody);
1057
+ work.classList.add('is-running');
925
1058
 
926
- // 思考过程:流式展开,回答开始后自动折叠
1059
+ // 思考过程:流式时就保持结构化,随时可折叠
927
1060
  const reasoning = document.createElement('details');
928
- reasoning.className = 'think';
1061
+ reasoning.className = 'think is-live';
929
1062
  reasoning.hidden = true;
930
1063
  reasoning.open = true;
931
1064
  const reasonSummary = document.createElement('summary');
932
- reasonSummary.innerHTML = '<span class="think-title">思考中…</span>';
1065
+ reasonSummary.innerHTML = '<span class="think-title">思考中…</span><span class="think-len"></span>';
933
1066
  const reasonBody = document.createElement('div');
934
1067
  reasonBody.className = 'think-body';
935
1068
  reasoning.append(reasonSummary, reasonBody);
1069
+ // 用户手动收起后,后续流式内容不再强行展开
1070
+ reasonSummary.addEventListener('click', () => {
1071
+ reasoning.dataset.user = '1';
1072
+ });
936
1073
 
937
1074
  const body = document.createElement('div');
938
1075
  body.className = 'msg-body caret';
1076
+ body.setAttribute('aria-live', 'polite');
939
1077
  const meta = document.createElement('div');
940
1078
  meta.className = 'turn-meta';
941
- botMsg.append(work, reasoning, body, meta);
1079
+ const working = document.createElement('div');
1080
+ working.className = 'working-row';
1081
+ working.innerHTML = '<span class="wg-dot"></span><span class="shimmer working-text">Banana 正在运行</span>';
1082
+ botMsg.append(working, work, reasoning, body, meta);
942
1083
  THREAD.appendChild(botMsg);
943
1084
 
944
1085
  let reasoningStarted = 0;
1086
+ let reasoningText = '';
1087
+ let reasonPending = 0;
945
1088
  let toolStarted = 0;
946
1089
  let toolCount = 0;
947
1090
  let toolErrors = false;
1091
+ const toolRows = new Map();
1092
+
1093
+ const paintReasoning = (final = false) => {
1094
+ reasonPending = 0;
1095
+ const text = reasoningText;
1096
+ reasonBody.innerHTML = markdown(text);
1097
+ const len = reasonSummary.querySelector('.think-len');
1098
+ if (len) len.textContent = text.length > 999 ? `${(text.length / 1000).toFixed(1)}k 字` : `${text.length} 字`;
1099
+ if (final) reasoning.classList.remove('is-live');
1100
+ // 只有用户没有上滚时才跟随最新思考
1101
+ const nearBottom = reasonBody.scrollHeight - reasonBody.scrollTop - reasonBody.clientHeight < 40;
1102
+ if (nearBottom) reasonBody.scrollTop = reasonBody.scrollHeight;
1103
+ };
1104
+ const scheduleReasoning = () => {
1105
+ if (!reasonPending) reasonPending = requestAnimationFrame(() => paintReasoning(false));
1106
+ };
1107
+
948
1108
  const finishReasoning = () => {
949
1109
  if (reasoning.hidden || reasoning.dataset.done === '1') return;
950
1110
  reasoning.dataset.done = '1';
951
- reasoning.open = false;
1111
+ reasoning.classList.remove('is-live');
1112
+ if (reasonPending) cancelAnimationFrame(reasonPending);
1113
+ paintReasoning(true);
1114
+ if (reasoning.dataset.user !== '1') reasoning.open = false;
952
1115
  const seconds = reasoningStarted ? ((Date.now() - reasoningStarted) / 1000).toFixed(1) : '0';
953
1116
  reasonSummary.querySelector('.think-title').textContent = `思考 · ${seconds}s`;
954
1117
  };
1118
+
1119
+ const setWorking = (text) => {
1120
+ const node = working.querySelector('.working-text');
1121
+ if (node) node.textContent = text;
1122
+ working.hidden = false;
1123
+ };
1124
+
955
1125
  const finishWork = () => {
956
1126
  if (work.hidden || work.dataset.done === '1') return;
957
1127
  work.dataset.done = '1';
1128
+ work.classList.remove('is-running');
958
1129
  const seconds = toolStarted ? ((Date.now() - toolStarted) / 1000).toFixed(1) : '0';
959
1130
  workSummary.querySelector('.wg-title').textContent = `运行了 ${toolCount} 个工具`;
960
1131
  workSummary.querySelector('.wg-state').textContent = `${seconds}s${toolErrors ? ' · 有失败' : ''}`;
@@ -968,17 +1139,33 @@ async function send() {
968
1139
 
969
1140
  let buffer = '';
970
1141
  let pending = 0;
1142
+ let lastPaint = 0;
971
1143
  let streamEnded = false;
1144
+ const renderStream = () => markdown(buffer);
1145
+
972
1146
  const paint = () => {
973
1147
  pending = 0;
974
- body.textContent = buffer;
975
- scrollToEnd(false);
1148
+ const now = performance.now();
1149
+ // 流式 markdown 限制到约 12fps,避免长回答每帧重排 + 高亮
1150
+ if (!streamEnded && now - lastPaint < 80) {
1151
+ schedule();
1152
+ return;
1153
+ }
1154
+ lastPaint = now;
1155
+ const atBottom = THREAD.scrollHeight - THREAD.scrollTop - THREAD.clientHeight < 90;
1156
+ body.innerHTML = renderStream();
1157
+ // 流式过程中保持光标
1158
+ if (!streamEnded) body.classList.add('caret');
1159
+ if (atBottom) scrollToEnd(false);
976
1160
  };
977
1161
  const schedule = () => {
978
1162
  if (!pending) pending = requestAnimationFrame(paint);
979
1163
  };
980
1164
 
981
1165
  setBusy(true);
1166
+ const composer = $('composer');
1167
+ composer.classList.add('is-busy');
1168
+ setStatus('正在连接模型…');
982
1169
  controller = new AbortController();
983
1170
  const metaLine = (label, value) => {
984
1171
  const span = document.createElement('span');
@@ -1012,6 +1199,8 @@ async function send() {
1012
1199
  const event = JSON.parse(line.slice(6));
1013
1200
  if (event.type === 'text') {
1014
1201
  finishReasoning();
1202
+ working.hidden = true;
1203
+ setStatus('正在输出…');
1015
1204
  buffer += event.chunk;
1016
1205
  schedule();
1017
1206
  } else if (event.type === 'reasoning') {
@@ -1021,7 +1210,12 @@ async function send() {
1021
1210
  reasoning.open = true;
1022
1211
  }
1023
1212
  reasoning.hidden = false;
1024
- reasonBody.appendChild(document.createTextNode(event.chunk));
1213
+ if (reasoning.dataset.user !== '1') reasoning.open = true;
1214
+ reasoningText += event.chunk;
1215
+ setWorking('正在思考…');
1216
+ setStatus('思考中…');
1217
+ working.hidden = false;
1218
+ scheduleReasoning();
1025
1219
  } else if (event.type === 'tool_start') {
1026
1220
  if (!toolStarted) toolStarted = Date.now();
1027
1221
  toolCount += 1;
@@ -1029,32 +1223,80 @@ async function send() {
1029
1223
  work.open = true;
1030
1224
  workSummary.querySelector('.wg-title').textContent = `正在运行 ${toolCount} 个工具`;
1031
1225
  workSummary.querySelector('.wg-state').textContent = '';
1032
- const row = document.createElement('div');
1033
- row.className = 'tool-row';
1034
- row.innerHTML =
1035
- `<span class="t-ico">${svg(TOOL_ICON[event.name] ?? 'file')}</span>` +
1036
- `<span class="t-name">${escapeHtml(event.name)}</span>` +
1037
- `<span class="t-detail">${escapeHtml(event.detail)}</span>` +
1038
- '<span class="t-state"><span class="spin"></span></span>';
1226
+ work.classList.add('is-updating');
1227
+ setTimeout(() => work.classList.remove('is-updating'), 220);
1228
+ const command = typeof event.input?.command === 'string' ? event.input.command : event.detail;
1229
+ const row = toolRowNode(
1230
+ event.name,
1231
+ command,
1232
+ '<span class="spin"></span>',
1233
+ );
1234
+ row.dataset.id = event.id;
1235
+ row.dataset.started = String(Date.now());
1236
+ const time = document.createElement('span');
1237
+ time.className = 't-time';
1238
+ time.textContent = '0.0s';
1239
+ row.appendChild(time);
1240
+ row._timer = setInterval(() => {
1241
+ const began = Number(row.dataset.started);
1242
+ time.textContent = `${((Date.now() - began) / 1000).toFixed(1)}s`;
1243
+ }, 400);
1244
+ toolRows.set(event.id, row);
1039
1245
  workBody.appendChild(row);
1246
+ if (command) ensureToolOutput(row, `$ ${command}\n`, false);
1247
+ setWorking(`正在运行 ${event.name}…`);
1248
+ setStatus(`运行工具 ${event.name}…`);
1249
+ working.hidden = false;
1040
1250
  scrollToEnd(false);
1251
+ } else if (event.type === 'tool_output') {
1252
+ const row = toolRows.get(event.id);
1253
+ if (row) {
1254
+ row._out = `${row._out ?? ''}${event.chunk}`;
1255
+ if (row._out.length > 12000) row._out = row._out.slice(-12000);
1256
+ const out = ensureToolOutput(row, row._out);
1257
+ if (out.hidden) {
1258
+ out.hidden = false;
1259
+ row.classList.add('is-open');
1260
+ }
1261
+ out.scrollTop = out.scrollHeight;
1262
+ const state = row.querySelector('.t-state');
1263
+ if (state && !state.querySelector('.spin')) state.innerHTML = '<span class="spin"></span>';
1264
+ setStatus(`输出中 · ${row.querySelector('.t-name')?.textContent ?? ''}`);
1265
+ }
1041
1266
  } else if (event.type === 'tool_end') {
1042
- const rows = workBody.querySelectorAll('.tool-row');
1043
- const row = rows[rows.length - 1];
1267
+ const row = toolRows.get(event.id) ?? [...toolRows.values()].pop();
1044
1268
  if (row) {
1269
+ clearInterval(row._timer);
1270
+ row.dataset.done = '1';
1045
1271
  row.querySelector('.t-state').innerHTML = event.ok ? svg('check') : svg('x');
1046
1272
  row.classList.toggle('is-error', !event.ok);
1047
1273
  if (!event.ok) toolErrors = true;
1048
- if (event.detail) {
1049
- row.classList.add('is-clickable');
1050
- row.title = '点击展开输出';
1051
- row.addEventListener('click', () => toggleToolOutput(row, event.detail, !event.ok));
1274
+ const time = row.querySelector('.t-time');
1275
+ if (time && event.ms) time.textContent = `${(event.ms / 1000).toFixed(1)}s`;
1276
+ const outText = row._out || event.detail || '';
1277
+ if (outText) {
1278
+ const out = ensureToolOutput(row, outText, !event.ok);
1279
+ if (!event.ok) {
1280
+ out.hidden = false;
1281
+ row.classList.add('is-open');
1282
+ }
1283
+ }
1284
+ if (!event.ok) {
1285
+ work.open = true;
1286
+ setWorking('工具执行失败,已展开输出');
1287
+ } else {
1288
+ setWorking('正在继续…');
1052
1289
  }
1053
1290
  }
1291
+ } else if (event.type === 'step') {
1292
+ setStatus(`第 ${event.step} 步 · 处理中`);
1293
+ setWorking(`第 ${event.step} 步 · 处理中`);
1294
+ working.hidden = false;
1054
1295
  } else if (event.type === 'status') {
1055
1296
  if (event.message) {
1056
- $('composerHint').textContent = event.message;
1057
- $('threadMeta').textContent = event.message;
1297
+ setStatus(event.message);
1298
+ setWorking(event.message);
1299
+ working.hidden = false;
1058
1300
  }
1059
1301
  } else if (event.type === 'approval') {
1060
1302
  const allowed = await askApproval(event);
@@ -1071,11 +1313,18 @@ async function send() {
1071
1313
  } else if (event.type === 'done') {
1072
1314
  finishReasoning();
1073
1315
  finishWork();
1316
+ if (pending) {
1317
+ cancelAnimationFrame(pending);
1318
+ pending = 0;
1319
+ }
1074
1320
  body.classList.remove('caret');
1075
1321
  streamEnded = true;
1076
1322
  if (event.error) body.innerHTML = markdown(`**请求失败** ${escapeHtml(event.error)}`);
1077
1323
  else if (!buffer.trim()) body.innerHTML = markdown('这一轮没有返回内容。');
1078
1324
  else body.innerHTML = markdown(buffer);
1325
+ if (buffer.trim() && !botMsg.querySelector('.msg-actions')) {
1326
+ messageActions(botMsg, buffer, 'assistant');
1327
+ }
1079
1328
  metaLine('步数', event.steps);
1080
1329
  metaLine('用时', (event.ms / 1000).toFixed(1) + 's');
1081
1330
  if (event.cost && event.cost !== '≈$0') metaLine('花费', event.cost);
@@ -1084,6 +1333,7 @@ async function send() {
1084
1333
  state.info.session.messages[state.info.session.messages.length - 1] = {
1085
1334
  role: 'assistant',
1086
1335
  content: buffer,
1336
+ reasoning: reasoningText || undefined,
1087
1337
  };
1088
1338
  const { sessions } = await get('sessions');
1089
1339
  state.sessions = sessions;
@@ -1107,14 +1357,20 @@ async function send() {
1107
1357
  }
1108
1358
  } catch (error) {
1109
1359
  body.classList.remove('caret');
1110
- body.innerHTML = markdown(`**中断** ${escapeHtml(error.message)}`);
1360
+ if (!buffer.trim()) body.innerHTML = markdown(`**中断** ${escapeHtml(error.message)}`);
1111
1361
  await refresh();
1112
1362
  } finally {
1113
1363
  finishReasoning();
1114
1364
  finishWork();
1365
+ for (const row of toolRows.values()) clearInterval(row._timer);
1366
+ working.hidden = true;
1367
+ body.classList.remove('caret');
1368
+ controller = null;
1115
1369
  setBusy(false);
1116
- scrollToEnd();
1370
+ $('composer').classList.remove('is-busy');
1371
+ setStatus(null);
1117
1372
  renderHeader();
1373
+ scrollToEnd();
1118
1374
  if (state.review.open && state.review.mode === 'changes') void loadChanges(true);
1119
1375
  }
1120
1376
  }
@@ -1362,9 +1618,130 @@ const SETTINGS_NAV = [
1362
1618
  { id: 'about', label: '关于', icon: 'info' },
1363
1619
  ];
1364
1620
 
1621
+ function closeModelPop() {
1622
+ const pop = $('modelPop');
1623
+ if (!pop) return;
1624
+ pop.remove();
1625
+ document.removeEventListener('click', onModelPopOutside, true);
1626
+ document.removeEventListener('keydown', onModelPopKey, true);
1627
+ }
1628
+
1629
+ function onModelPopOutside(event) {
1630
+ if (event.target.closest('#modelPop, #modelBtn')) return;
1631
+ closeModelPop();
1632
+ }
1633
+
1634
+ function onModelPopKey(event) {
1635
+ if (event.key === 'Escape') {
1636
+ event.preventDefault();
1637
+ closeModelPop();
1638
+ $('modelBtn')?.focus();
1639
+ }
1640
+ }
1641
+
1642
+ function toggleModelPop() {
1643
+ if ($('modelPop')) return closeModelPop();
1644
+ const info = state.info;
1645
+ if (!info) return;
1646
+ const pop = document.createElement('div');
1647
+ pop.className = 'model-pop';
1648
+ pop.id = 'modelPop';
1649
+ pop.setAttribute('role', 'dialog');
1650
+ pop.setAttribute('aria-label', '切换模型');
1651
+ const head = document.createElement('div');
1652
+ head.className = 'mp-head';
1653
+ head.innerHTML = '<span>模型</span><span class="mp-sub">切换后新消息生效</span>';
1654
+ pop.appendChild(head);
1655
+
1656
+ for (const provider of info.providers ?? []) {
1657
+ const row = document.createElement('button');
1658
+ row.type = 'button';
1659
+ row.className = `mp-row${provider.current ? ' is-on' : ''}`;
1660
+ row.innerHTML =
1661
+ `<span class="mp-name">${escapeHtml(provider.name)}</span>` +
1662
+ `<span class="mp-model">${escapeHtml(provider.model)}</span>` +
1663
+ (provider.current ? `<span class="mp-check">${svg('check')}</span>` : '');
1664
+ row.addEventListener('click', async () => {
1665
+ if (provider.current) return closeModelPop();
1666
+ try {
1667
+ await saveSetting('provider', { name: provider.name });
1668
+ await refresh(false);
1669
+ closeModelPop();
1670
+ toast(`已切换到 ${provider.name}`);
1671
+ } catch (error) {
1672
+ toast('切换失败:' + error.message);
1673
+ }
1674
+ });
1675
+ pop.appendChild(row);
1676
+ }
1677
+
1678
+ const form = document.createElement('div');
1679
+ form.className = 'mp-form';
1680
+ const input = document.createElement('input');
1681
+ input.type = 'text';
1682
+ input.value = info.provider.model;
1683
+ input.placeholder = '模型名';
1684
+ input.setAttribute('aria-label', '模型名');
1685
+ const save = document.createElement('button');
1686
+ save.type = 'button';
1687
+ save.className = 'btn tiny primary';
1688
+ save.textContent = '保存';
1689
+ save.addEventListener('click', async () => {
1690
+ const value = input.value.trim();
1691
+ if (!value) return toast('请填写模型名');
1692
+ try {
1693
+ await saveSetting('model', { value });
1694
+ await refresh(false);
1695
+ closeModelPop();
1696
+ toast('模型已更新');
1697
+ } catch (error) {
1698
+ toast('保存失败:' + error.message);
1699
+ }
1700
+ });
1701
+ input.addEventListener('keydown', (event) => {
1702
+ if (event.key === 'Enter') {
1703
+ event.preventDefault();
1704
+ save.click();
1705
+ }
1706
+ });
1707
+ form.append(input, save);
1708
+ pop.appendChild(form);
1709
+
1710
+ const more = document.createElement('button');
1711
+ more.type = 'button';
1712
+ more.className = 'mp-more';
1713
+ more.textContent = '更多模型与 Key 设置';
1714
+ more.addEventListener('click', () => {
1715
+ closeModelPop();
1716
+ openSettings('model');
1717
+ });
1718
+ pop.appendChild(more);
1719
+ document.body.appendChild(pop);
1720
+
1721
+ const rect = $('modelBtn').getBoundingClientRect();
1722
+ const width = Math.min(340, window.innerWidth - 16);
1723
+ pop.style.width = `${width}px`;
1724
+ pop.style.left = `${Math.max(8, Math.min(rect.left, window.innerWidth - width - 8))}px`;
1725
+ if (rect.top > 300) {
1726
+ pop.style.bottom = `${Math.max(8, window.innerHeight - rect.top + 8)}px`;
1727
+ pop.style.maxHeight = `${Math.max(180, rect.top - 16)}px`;
1728
+ } else {
1729
+ pop.style.top = `${Math.max(8, rect.bottom + 8)}px`;
1730
+ pop.style.maxHeight = `${Math.max(180, window.innerHeight - rect.bottom - 16)}px`;
1731
+ }
1732
+ pop.style.overflowY = 'auto';
1733
+ void pop.offsetHeight;
1734
+ pop.classList.add('is-on');
1735
+ setTimeout(() => {
1736
+ document.addEventListener('click', onModelPopOutside, true);
1737
+ document.addEventListener('keydown', onModelPopKey, true);
1738
+ }, 0);
1739
+ }
1740
+
1365
1741
  function openSettings(category) {
1366
1742
  if (category) state.settingsCategory = category;
1367
1743
  state.settingsScroll = 0;
1744
+ state.settingsReturn = document.activeElement;
1368
1745
  renderSettings();
1369
1746
  const sheet = $('settingsSheet');
1370
1747
  sheet.classList.remove('is-closing');
@@ -1381,7 +1758,32 @@ function closeSettings() {
1381
1758
  if (sheet.hidden) return;
1382
1759
  state.settingsScroll = $('settingsBody').scrollTop;
1383
1760
  void hideAnimated(sheet, 'is-closing', 170).then(syncOverlayState);
1384
- $('input')?.focus();
1761
+ const back = state.settingsReturn;
1762
+ state.settingsReturn = null;
1763
+ if (back && back.isConnected) back.focus();
1764
+ else $('input')?.focus();
1765
+ }
1766
+
1767
+ function trapSettingsFocus(event) {
1768
+ const sheet = $('settingsSheet');
1769
+ if (sheet.hidden) return false;
1770
+ const focusable = [...sheet.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])')]
1771
+ .filter((node) => !node.disabled && node.offsetParent !== null);
1772
+ if (!focusable.length) return false;
1773
+ const first = focusable[0];
1774
+ const last = focusable[focusable.length - 1];
1775
+ const active = document.activeElement;
1776
+ if (event.shiftKey && (active === first || !sheet.contains(active))) {
1777
+ event.preventDefault();
1778
+ last.focus();
1779
+ return true;
1780
+ }
1781
+ if (!event.shiftKey && (active === last || !sheet.contains(active))) {
1782
+ event.preventDefault();
1783
+ first.focus();
1784
+ return true;
1785
+ }
1786
+ return false;
1385
1787
  }
1386
1788
 
1387
1789
  function settingsCard() {
@@ -1945,7 +2347,7 @@ function renderAboutSettings(body) {
1945
2347
  // ---------------- 外壳:侧栏 / Review / 终端 dock ----------------
1946
2348
  const SHELL_STORAGE = {
1947
2349
  sidebarWidth: 'banana.sidebar.width',
1948
- sidebarOpen: 'banana.sidebar.open',
2350
+ sidebarOpen: 'banana.sidebar.open.v2',
1949
2351
  reviewWidth: 'banana.review.width',
1950
2352
  reviewMode: 'banana.review.mode',
1951
2353
  tabState: 'banana.tabs.v1',
@@ -1961,16 +2363,23 @@ function applySidebarWidth(width) {
1961
2363
 
1962
2364
  function setSidebar(open) {
1963
2365
  state.sidebar.open = Boolean(open);
1964
- $('sidebar').classList.toggle('open', state.sidebar.open && window.matchMedia('(max-width: 900px)').matches);
1965
- $('sidebar').hidden = !state.sidebar.open && !window.matchMedia('(max-width: 900px)').matches;
1966
- if (window.matchMedia('(max-width: 900px)').matches) $('sidebar').hidden = false;
2366
+ const narrow = window.matchMedia('(max-width: 900px)').matches;
2367
+ const sidebar = $('sidebar');
2368
+ sidebar.classList.toggle('open', state.sidebar.open && narrow);
2369
+ // 桌面端收起 = 真正隐藏;窄屏收起 = 平移出屏,但仍需 inert
2370
+ sidebar.hidden = !state.sidebar.open && !narrow;
2371
+ sidebar.inert = !state.sidebar.open;
2372
+ sidebar.setAttribute('aria-hidden', String(!state.sidebar.open));
2373
+ $('toggleSidebar').classList.toggle('is-on', state.sidebar.open);
2374
+ $('toggleSidebar').setAttribute('aria-expanded', String(state.sidebar.open));
1967
2375
  try {
1968
2376
  localStorage.setItem(SHELL_STORAGE.sidebarOpen, state.sidebar.open ? '1' : '0');
1969
2377
  } catch {}
1970
2378
  }
1971
2379
 
1972
2380
  function toggleSidebar() {
1973
- setSidebar(!state.sidebar.open || (window.matchMedia('(max-width: 900px)').matches && !$('sidebar').classList.contains('open')));
2381
+ const narrow = window.matchMedia('(max-width: 900px)').matches;
2382
+ setSidebar(!state.sidebar.open || (narrow && !$('sidebar').classList.contains('open')));
1974
2383
  }
1975
2384
 
1976
2385
  function applyReviewWidth(width) {
@@ -1997,13 +2406,22 @@ function setReviewMode(mode) {
1997
2406
  }
1998
2407
  }
1999
2408
 
2409
+ function syncReviewScrim() {
2410
+ const scrim = $('reviewScrim');
2411
+ if (!scrim) return;
2412
+ const overlay = window.matchMedia('(max-width: 1180px)').matches;
2413
+ scrim.hidden = !(state.review.open && overlay);
2414
+ }
2415
+
2000
2416
  function openReview(mode = 'changes') {
2001
2417
  state.review.open = true;
2418
+ state.reviewRestore = document.activeElement;
2002
2419
  const panel = $('reviewPanel');
2003
2420
  panel.classList.remove('is-closing');
2004
2421
  panel.hidden = false;
2005
2422
  applyReviewWidth(state.review.width);
2006
2423
  setReviewMode(mode);
2424
+ syncReviewScrim();
2007
2425
  syncOverlayState();
2008
2426
  }
2009
2427
 
@@ -2012,12 +2430,16 @@ function closeReview() {
2012
2430
  if (!state.review.open) return;
2013
2431
  state.review.open = false;
2014
2432
  panel.classList.add('is-closing');
2433
+ $('reviewScrim').hidden = true;
2015
2434
  setTimeout(() => {
2016
2435
  if (state.review.open) return;
2017
2436
  panel.hidden = true;
2018
2437
  panel.classList.remove('is-closing');
2019
2438
  syncOverlayState();
2020
- $('input')?.focus();
2439
+ const back = state.reviewRestore;
2440
+ state.reviewRestore = null;
2441
+ if (back && back.isConnected) back.focus();
2442
+ else $('input')?.focus();
2021
2443
  termFitActive();
2022
2444
  }, 200);
2023
2445
  }
@@ -2033,7 +2455,8 @@ function initShell() {
2033
2455
  const dh = Number(localStorage.getItem(SHELL_STORAGE.dockHeight));
2034
2456
  if (dh) state.dock.height = Math.max(120, Math.min(dh, window.innerHeight * 0.6));
2035
2457
  const openSidebar = localStorage.getItem(SHELL_STORAGE.sidebarOpen);
2036
- state.sidebar.open = openSidebar === null ? window.innerWidth > 900 : openSidebar !== '0';
2458
+ // 历史栏默认收起:顶部标签才是主入口
2459
+ state.sidebar.open = openSidebar === '1';
2037
2460
  } catch {
2038
2461
  applySidebarWidth(344);
2039
2462
  applyReviewWidth(520);
@@ -2042,6 +2465,7 @@ function initShell() {
2042
2465
  $('reviewTabChanges').addEventListener('click', () => setReviewMode('changes'));
2043
2466
  $('reviewTabFiles').addEventListener('click', () => setReviewMode('files'));
2044
2467
  $('reviewClose').addEventListener('click', closeReview);
2468
+ $('reviewScrim').addEventListener('click', closeReview);
2045
2469
  $('reviewRefresh').addEventListener('click', () => {
2046
2470
  state.changes = null;
2047
2471
  if (state.review.mode === 'changes') void loadChanges(true);
@@ -2079,6 +2503,8 @@ function initShell() {
2079
2503
  });
2080
2504
  window.addEventListener('resize', () => {
2081
2505
  applyReviewWidth(state.review.width);
2506
+ syncReviewScrim();
2507
+ setSidebar(state.sidebar.open);
2082
2508
  termFitActive();
2083
2509
  });
2084
2510
  }
@@ -2743,7 +3169,30 @@ function bindTerminal() {
2743
3169
  });
2744
3170
  $('termMore').addEventListener('click', (event) => {
2745
3171
  event.stopPropagation();
2746
- $('termMenu').hidden = !$('termMenu').hidden;
3172
+ const menu = $('termMenu');
3173
+ const willOpen = menu.hidden;
3174
+ menu.hidden = !willOpen;
3175
+ if (willOpen) {
3176
+ // fixed 定位:终端 dock 有 overflow:hidden,菜单贴着头部放会被裁掉
3177
+ const rect = $('termMore').getBoundingClientRect();
3178
+ const below = window.innerHeight - rect.bottom - 12;
3179
+ const above = rect.top - 12;
3180
+ menu.style.position = 'fixed';
3181
+ menu.style.right = `${Math.max(8, window.innerWidth - rect.right)}px`;
3182
+ if (below >= 150 || below >= above) {
3183
+ menu.style.top = `${Math.round(rect.bottom + 4)}px`;
3184
+ menu.style.bottom = 'auto';
3185
+ menu.style.maxHeight = `${Math.max(90, below)}px`;
3186
+ } else {
3187
+ menu.style.top = 'auto';
3188
+ menu.style.bottom = `${Math.max(8, Math.round(window.innerHeight - rect.top + 4))}px`;
3189
+ menu.style.maxHeight = `${Math.max(90, above)}px`;
3190
+ }
3191
+ menu.style.overflowY = 'auto';
3192
+ $('termMore').setAttribute('aria-expanded', 'true');
3193
+ } else {
3194
+ $('termMore').setAttribute('aria-expanded', 'false');
3195
+ }
2747
3196
  });
2748
3197
  $('termAnalyze').addEventListener('click', () => {
2749
3198
  closeTermMenu();
@@ -3126,8 +3575,21 @@ function bind() {
3126
3575
  $('searchBtn').innerHTML = svg('search');
3127
3576
  $('sendBtn').innerHTML = svg('send');
3128
3577
  $('abortBtn').innerHTML = svg('stop');
3129
- $('toggleSidebar').innerHTML = '<svg class="brand-mark" viewBox="0 0 24 24" aria-hidden="true"><path d="M7 4.6c2.6-.4 4.9.5 6.3 2.6M12.3 7.2c1.4-2.1 3.7-3 6.3-2.6-1.1 1.6-1.2 3.1-.4 4.6.8 1.5.8 3-.1 4.5-1.1 2-3.3 3.5-5.9 4.3-2.3.7-4.7.7-7.1 0"/><path d="M7.2 18c-1.1-2-1.2-4-.4-6 .9-2.3 2.5-3.7 4.7-4.2"/></svg>';
3130
3578
  $('closeSettings').innerHTML = svg('x');
3579
+ $('toggleSidebar').innerHTML = svg('history');
3580
+ $('toggleSidebar').title = '历史会话(Ctrl+B)';
3581
+ $('attachBtn').innerHTML = svg('attach');
3582
+ $('jumpLatest').innerHTML = svg('arrowDown');
3583
+ $('abortBtn').addEventListener('click', stopTurn);
3584
+ $('modelBtn').addEventListener('click', (event) => {
3585
+ event.stopPropagation();
3586
+ toggleModelPop();
3587
+ });
3588
+ $('attachBtn').addEventListener('click', () => $('attachInput').click());
3589
+ $('attachInput').addEventListener('change', async () => {
3590
+ for (const file of $('attachInput').files ?? []) await attach(file);
3591
+ $('attachInput').value = '';
3592
+ });
3131
3593
  $('reviewClose').innerHTML = svg('x');
3132
3594
  $('reviewRefresh').innerHTML = svg('refresh');
3133
3595
  $('filesBtn').innerHTML = svg('panel');
@@ -3152,14 +3614,14 @@ function bind() {
3152
3614
  renderHeader();
3153
3615
  renderSidebar();
3154
3616
  renderThread();
3617
+ if (state.dock.open) termClose();
3618
+ closeSidebarIfOverlay();
3155
3619
  setActiveTab(state.info?.session?.id ?? null);
3156
3620
  $('input').focus();
3157
3621
  } catch (error) {
3158
3622
  toast('新建对话失败:' + (error?.message ?? error));
3159
3623
  }
3160
3624
  });
3161
- $('abortBtn').addEventListener('click', async () => { controller?.abort(); await post('abort'); });
3162
- $('modelBtn').addEventListener('click', () => openSettings());
3163
3625
  $('openSettings').addEventListener('click', () => openSettings());
3164
3626
  $('closeSettings').addEventListener('click', closeSettings);
3165
3627
  $('settingsSheet').addEventListener('click', (event) => {
@@ -3169,6 +3631,10 @@ function bind() {
3169
3631
  if (event.target === $('settingsSheet')) closeSettings();
3170
3632
  });
3171
3633
  $('toggleSidebar').addEventListener('click', toggleSidebar);
3634
+ $('sessionSearch').addEventListener('input', () => {
3635
+ state.sessionQuery = $('sessionSearch').value;
3636
+ renderSidebar();
3637
+ });
3172
3638
  $('filesBtn').addEventListener('click', toggleFiles);
3173
3639
  $('reviewBtn').addEventListener('click', () => {
3174
3640
  if (state.review.open && state.review.mode === 'changes') closeReview();
@@ -3211,6 +3677,20 @@ function bind() {
3211
3677
  });
3212
3678
 
3213
3679
  $('composer').addEventListener('submit', (event) => { event.preventDefault(); send(); });
3680
+ $('sendBtn').addEventListener('click', () => {
3681
+ if (state.busy) stopTurn();
3682
+ else send();
3683
+ });
3684
+ $('jumpLatest').addEventListener('click', () => {
3685
+ state.stickToBottom = true;
3686
+ scrollToEnd(true);
3687
+ });
3688
+ THREAD.addEventListener('scroll', () => {
3689
+ const atBottom = THREAD.scrollHeight - THREAD.scrollTop - THREAD.clientHeight < 90;
3690
+ state.stickToBottom = atBottom;
3691
+ const hasMessages = Boolean(THREAD.querySelector('.msg'));
3692
+ $('jumpLatest').hidden = atBottom || !hasMessages;
3693
+ });
3214
3694
  $('input').addEventListener('input', () => {
3215
3695
  grow();
3216
3696
  const value = $('input').value;
@@ -3260,12 +3740,28 @@ function bind() {
3260
3740
  });
3261
3741
 
3262
3742
  document.addEventListener('keydown', (event) => {
3743
+ if (event.key === 'Tab' && trapSettingsFocus(event)) return;
3263
3744
  if ((event.ctrlKey || event.metaKey) && String(event.key).toLowerCase() === 'k') {
3264
3745
  event.preventDefault();
3265
3746
  if ($('commandCenter').hidden) openCommandCenter();
3266
3747
  else closeCommandCenter();
3267
3748
  return;
3268
3749
  }
3750
+ if ((event.ctrlKey || event.metaKey) && !event.shiftKey && String(event.key).toLowerCase() === 'b') {
3751
+ event.preventDefault();
3752
+ toggleSidebar();
3753
+ return;
3754
+ }
3755
+ if (event.ctrlKey && event.key === 'Tab') {
3756
+ event.preventDefault();
3757
+ const list = state.tabs ?? [];
3758
+ if (list.length > 1) {
3759
+ const index = list.findIndex((tab) => tab.id === state.activeTab);
3760
+ const next = list[(index + (event.shiftKey ? -1 : 1) + list.length) % list.length];
3761
+ if (next) void switchSession(next.id);
3762
+ }
3763
+ return;
3764
+ }
3269
3765
  if ((event.ctrlKey || event.metaKey) && event.shiftKey) {
3270
3766
  const key = String(event.key).toLowerCase();
3271
3767
  if (key === 'f') { event.preventDefault(); openFiles(); return; }
@@ -3286,7 +3782,7 @@ function bind() {
3286
3782
  if (!window.matchMedia('(max-width: 900px)').matches) return;
3287
3783
  if (!$('sidebar').classList.contains('open')) return;
3288
3784
  if (event.target.closest('#sidebar, #toggleSidebar')) return;
3289
- $('sidebar').classList.remove('open');
3785
+ setSidebar(false);
3290
3786
  });
3291
3787
 
3292
3788
  $('input').addEventListener('paste', (event) => {
@@ -3312,7 +3808,8 @@ async function boot() {
3312
3808
  $('workspaceLabel').textContent = state.info?.workspace ?? '';
3313
3809
  updateHomeMode();
3314
3810
  try {
3315
- if (localStorage.getItem(SHELL_STORAGE.dockOpen) === '1') termOpen();
3811
+ const hasMessages = Boolean(state.info?.session?.messages?.length);
3812
+ if (hasMessages && localStorage.getItem(SHELL_STORAGE.dockOpen) === '1') termOpen();
3316
3813
  } catch {}
3317
3814
  grow();
3318
3815
  $('input').focus();