@bolloon/bolloon-agent 0.1.33 → 0.1.34

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.
@@ -29,6 +29,62 @@ let reconnectAttempts = new Map(); // channelId -> attempts
29
29
  let reconnectTimers = new Map(); // channelId -> timer
30
30
  let heartbeatTimers = new Map(); // channelId -> setInterval handle (防止泄漏)
31
31
  let lastUserCommand = ''; // 防止用户消息重复显示
32
+
33
+ // 2026-06-10: P2P peer-group 折叠状态持久化 (跨刷新)
34
+ // key = bolloon.p2p.collapsedPeers, value = JSON array of publicKey hex
35
+ const COLLAPSED_PEERS_KEY = 'bolloon.p2p.collapsedPeers';
36
+ const SEEN_PEERS_KEY = 'bolloon.p2p.seenPeers';
37
+ let collapsedPeers = (function loadCollapsed() {
38
+ try {
39
+ const raw = localStorage.getItem(COLLAPSED_PEERS_KEY);
40
+ return new Set(raw ? JSON.parse(raw) : []);
41
+ } catch { return new Set(); }
42
+ })();
43
+ let seenPeers = (function loadSeen() {
44
+ try {
45
+ const raw = localStorage.getItem(SEEN_PEERS_KEY);
46
+ return new Set(raw ? JSON.parse(raw) : []);
47
+ } catch { return new Set(); }
48
+ })();
49
+ function saveCollapsedPeers() {
50
+ try { localStorage.setItem(COLLAPSED_PEERS_KEY, JSON.stringify([...collapsedPeers])); } catch {}
51
+ }
52
+ function saveSeenPeers() {
53
+ try { localStorage.setItem(SEEN_PEERS_KEY, JSON.stringify([...seenPeers])); } catch {}
54
+ }
55
+ function togglePeerCollapsed(peerPk) {
56
+ if (collapsedPeers.has(peerPk)) {
57
+ collapsedPeers.delete(peerPk);
58
+ } else {
59
+ collapsedPeers.add(peerPk);
60
+ }
61
+ saveCollapsedPeers();
62
+ renderRemoteChannels();
63
+ // 2026-06-10: 通知 header 切换按钮同步图标
64
+ if (typeof window.__syncP2PToggleAllBtn === 'function') window.__syncP2PToggleAllBtn();
65
+ }
66
+ // 2026-06-10: 一键展开/折叠所有 P2P peer (header 按钮调用)
67
+ function expandAllPeers() {
68
+ // 从 remoteChannels + knownPeers 收集所有 publicKey
69
+ const allPks = new Set([
70
+ ...knownPeers.map(p => p.publicKey),
71
+ ...remoteChannels.map(g => g.peerId)
72
+ ]);
73
+ for (const pk of allPks) collapsedPeers.delete(pk);
74
+ saveCollapsedPeers();
75
+ renderRemoteChannels();
76
+ if (typeof window.__syncP2PToggleAllBtn === 'function') window.__syncP2PToggleAllBtn();
77
+ }
78
+ function collapseAllPeers() {
79
+ const allPks = new Set([
80
+ ...knownPeers.map(p => p.publicKey),
81
+ ...remoteChannels.map(g => g.peerId)
82
+ ]);
83
+ for (const pk of allPks) collapsedPeers.add(pk);
84
+ saveCollapsedPeers();
85
+ renderRemoteChannels();
86
+ if (typeof window.__syncP2PToggleAllBtn === 'function') window.__syncP2PToggleAllBtn();
87
+ }
32
88
  let lastAiContent = ''; // 防止 AI 消息重复显示
33
89
  let messagesContainers = new Map(); // channelId -> messages container div
34
90
  let sessionMessages = new Map(); // channelId:sessionId -> messages array
@@ -100,6 +156,8 @@ async function loadChannels() {
100
156
  channels.forEach((ch, i) => {
101
157
  console.log(` [${i}] ${ch.name} - did: "${ch.did}"`);
102
158
  });
159
+ // 2026-06-11: 全部默认不展开 (用户需要手动点 caret 展开 session 列表)
160
+ // 之前默认展开第一个会喧宾夺主, 用户看不到完整 channel 列表
103
161
  renderChannels();
104
162
  } catch (err) {
105
163
  console.error('[加载频道] 失败:', err);
@@ -116,20 +174,34 @@ function startV3GlobalSSE() {
116
174
  try {
117
175
  const msg = JSON.parse(e.data);
118
176
  if (msg.type === 'remote-chat-reply') {
119
- // 找到当前打开的远端 chat modal log
177
+ // 2026-06-10: 复用本地 addMessage 渲染 — 自动 marked + 剥 think/env + 主题样式
178
+ // 之前是 textContent 硬编码灰底, 跟 Step 3 重写的 modal 风格不一致,
179
+ // 而且 SSE 异步回到时 modal 可能已被切到 thinking 占满, 用户看不到 reply.
120
180
  const log = document.getElementById('rcm-log');
121
181
  const thinkingEl = document.getElementById('rcm-thinking');
122
182
  if (thinkingEl) thinkingEl.style.display = 'none'; // 思考结束, 隐藏
183
+ // 也清掉 "对方正在思考..." 行 (流式 token 留下的)
184
+ const liveThinking = document.getElementById('rcm-thinking-live');
185
+ if (liveThinking) liveThinking.remove();
123
186
  if (log) {
124
- const bubble = document.createElement('div');
125
- bubble.style.cssText = 'padding:8px 10px;margin:4px 0;border-radius:6px;font-size:13px;line-height:1.4;max-width:80%;word-wrap:break-word;background:#e5e7eb;color:#111;';
126
- bubble.textContent = msg.text || '(空回复)';
127
187
  if (msg.error) {
128
- bubble.textContent = '(错误: ' + msg.error + ')';
129
- bubble.style.background = '#fca5a5';
188
+ // 错误用 sysmsg 样式 (跟 modal 风格一致)
189
+ const errEl = document.createElement('div');
190
+ errEl.className = 'remote-chat-sysmsg remote-chat-sysmsg-error';
191
+ errEl.textContent = `❌ 对方回复出错: ${msg.error}`;
192
+ log.appendChild(errEl);
193
+ } else {
194
+ // 走本地 addMessage, 跟主聊天框完全一致 (marked + think/env 折叠 + 主题色)
195
+ const prefix = `🤖 远端 AI 回复\n\n`;
196
+ addMessage(prefix + (msg.text || '(空回复)'), 'ai', false, log);
130
197
  }
131
- log.appendChild(bubble);
132
198
  log.scrollTop = log.scrollHeight;
199
+ } else {
200
+ // modal 没开 → 用右下 toast 提示用户"对方回了, 打开聊天看"
201
+ if (typeof showSimpleToast === 'function') {
202
+ const preview = (msg.text || '').slice(0, 50);
203
+ showSimpleToast(`💬 远端 channel 有新回复: ${preview}${msg.text && msg.text.length > 50 ? '…' : ''}`);
204
+ }
133
205
  }
134
206
  } else if (msg.type === 'remote-chat-thinking') {
135
207
  // v3 新增: B 端实时显示 A 节点的思考过程
@@ -182,6 +254,44 @@ function startV3GlobalSSE() {
182
254
  log.appendChild(toast);
183
255
  log.scrollTop = log.scrollHeight;
184
256
  }
257
+ } else if (msg.type === 'remote-channel-update') {
258
+ // v3 新增: 远端节点发来新分享 / 删除 / 改名, 立即更新本地 cache
259
+ const peerId = msg.peerId;
260
+ const channels = msg.channels || [];
261
+ const peerName = msg.peerName || null; // 2026-06-10: 同步接收对方名字
262
+ let group = remoteChannels.find(g => g.peerId === peerId);
263
+ if (!group) {
264
+ group = { peerId, channels: [], peerName: peerName || ('peer-' + peerId.substring(0, 8)) };
265
+ remoteChannels.push(group);
266
+ } else if (peerName) {
267
+ group.peerName = peerName; // 更新名字
268
+ }
269
+ group.channels = channels;
270
+ // 2026-06-10: 如果对面告知名字, 同步刷新 knownPeers 列表, 避免陌生 peer 状态
271
+ if (peerName && !knownPeers.find(p => p.publicKey === peerId)) {
272
+ knownPeers.push({
273
+ publicKey: peerId,
274
+ name: peerName,
275
+ addedAt: new Date().toISOString(),
276
+ lastConnectedAt: new Date().toISOString(),
277
+ });
278
+ console.log(`[v3] 远端 ${peerId.substring(0,12)}... 自报名字 = ${peerName}, 已加到 knownPeers`);
279
+ }
280
+ renderRemoteChannels();
281
+ console.log(`[v3] 收到远端 ${peerId.substring(0,12)}... 的 ${channels.length} 个 channel 更新 (name=${peerName || '?'})`);
282
+ } else if (msg.type === 'friend-request') {
283
+ // v3 新增: 收到好友申请
284
+ showFriendRequestModal(msg);
285
+ } else if (msg.type === 'friend-request-ack') {
286
+ // 2026-06-10: 收到对方 ack, 给发送方提示"已送达"
287
+ const pending = window.__pendingFriendRequests;
288
+ if (pending && msg.requestId && pending.has(msg.requestId)) {
289
+ const { name } = pending.get(msg.requestId);
290
+ pending.delete(msg.requestId);
291
+ console.log(`[v3-friend] ✅ ack 收到: ${name} 已收到好友申请`);
292
+ // 简短 toast (右下角), 不阻塞
293
+ showSimpleToast(`📬 ${name} 已收到你的好友申请, 等对方接受`);
294
+ }
185
295
  }
186
296
  } catch (err) {
187
297
  console.error('[v3] 全局 SSE 解析失败:', err);
@@ -489,6 +599,50 @@ function toggleAgentExpand(channelId, e) {
489
599
  renderChannels();
490
600
  }
491
601
 
602
+ /**
603
+ * 2026-06-11 性能优化: 切 channel 时用轻量 patch, 不重建整个 sidebar 列表
604
+ * 只更新: (1) active class (2) 当前 session label + count (3) expanded 状态
605
+ * 避免每次切 channel 都 innerHTML='' + 重建 ~10 个 channel 节点
606
+ */
607
+ function renderChannelsLite(activeChannelId, activeSessionId) {
608
+ if (!channelList) return;
609
+ // 1. 更新所有 .agent-row 的 active class
610
+ channelList.querySelectorAll('.agent-row').forEach(row => {
611
+ const li = row.closest('.agent-group');
612
+ const chId = li?.dataset.channelId;
613
+ row.classList.toggle('active', chId === activeChannelId);
614
+ });
615
+ // 2. 当前 channel 的展开状态: 强制展开, 其他不动
616
+ if (activeChannelId) expandedAgents.add(activeChannelId);
617
+ // 3. 当前 channel 行展开 + 只切 session-item 的 active class (不再 innerHTML 重渲!)
618
+ // 原因: 重渲 innerHTML 会清掉原始 renderChannels 绑的 session-item click handler,
619
+ // 即使补绑也会因为 lite HTML 结构 (.session-dot + .session-msg-count) 跟原始不同
620
+ // 导致"第 1 次点不动 (原始), 第 2 次点才能用 (lite)" 现象
621
+ // 修法: 完全不动 session-list DOM, 只 toggle .active
622
+ const activeLi = channelList.querySelector(`.agent-group[data-channel-id="${activeChannelId}"]`);
623
+ if (activeLi) {
624
+ activeLi.classList.add('expanded');
625
+ // 只切 active class, 不动 innerHTML (避免清掉原始 click handler)
626
+ const ch = channels.find(c => c.id === activeChannelId);
627
+ // 2026-06-11: 原始 renderChannels 已经给 session-item 加了 data-session-id (line 791),
628
+ // 这里先清空所有 .active 再设新的, 避免多个 active 共存 (因为 renderChannels 初始 DOM
629
+ // 上会有一个 active 标记旧 session, 新切 session 容易出现两个 active)
630
+ activeLi.querySelectorAll('.session-item').forEach(sessLi => {
631
+ const sessId = sessLi.dataset.sessionId;
632
+ const shouldBeActive = sessId === activeSessionId;
633
+ sessLi.classList.toggle('active', shouldBeActive);
634
+ });
635
+ // 更新顶部 current session label
636
+ if (ch) {
637
+ const currentSess = Array.isArray(ch.sessions) ? ch.sessions.find(s => s.id === activeSessionId) : null;
638
+ const labelEl = activeLi.querySelector('.agent-current-session');
639
+ if (labelEl) {
640
+ labelEl.textContent = currentSess ? '· ' + formatSessionName(currentSess) : '';
641
+ }
642
+ }
643
+ }
644
+ }
645
+
492
646
  function renderChannels() {
493
647
  if (!channelList) return;
494
648
  channelList.innerHTML = '';
@@ -532,12 +686,9 @@ function renderChannels() {
532
686
  const currentSessLabel = currentSess ? formatSessionName(currentSess) : '';
533
687
  const sessionCount = Array.isArray(ch.sessions) ? ch.sessions.length : 0;
534
688
 
535
- const walletBadge = ch.walletAddress
536
- ? `<span class="agent-wallet-badge" title="已绑定钱包: ${escapeHtml(ch.walletAddress)}">⛓</span>`
537
- : '';
538
- const toolsBadge = ch.autoInvokeTools
539
- ? `<span class="agent-tools-badge" title="自动工具调用已开启">⚡</span>`
540
- : '';
689
+ // 2026-06-10: 隐藏 channel 行右侧的勋章 (钱包 / 工具) — UI 简洁
690
+ const walletBadge = '';
691
+ const toolsBadge = '';
541
692
 
542
693
  row.innerHTML = `
543
694
  <svg class="agent-caret" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
@@ -621,6 +772,7 @@ function renderChannels() {
621
772
  const sessLi = document.createElement('li');
622
773
  const isActive = ch.id === currentChannelId && sess.id === ch.currentSessionId;
623
774
  sessLi.className = `session-item ${isActive ? 'active' : ''}`;
775
+ sessLi.dataset.sessionId = sess.id; // 2026-06-11: 给 session-item 加上 data-session-id, renderChannelsLite 才能 toggle active class
624
776
  sessLi.innerHTML = `
625
777
  <span class="session-name" title="${escapeHtml(formatSessionName(sess))}">${escapeHtml(formatSessionName(sess))}</span>
626
778
  <button class="session-delete" title="删除会话">×</button>
@@ -731,7 +883,11 @@ async function selectChannel(channelId, targetSessionId = null) {
731
883
  console.log('[selectChannel] 频道:', channel.name, 'session:', currentSessionId);
732
884
  }
733
885
 
734
- renderChannels();
886
+ // 2026-06-11 提速: 切 channel 时 sidebar 渲染降级 — 只更新 active 样式, 不重渲整列表
887
+ // renderChannels() 仍然要调 (current session label 等可能变了), 但加一层判断: 如果只是切 channel (没增删), 走 patch 路径
888
+ const t0 = performance.now();
889
+ renderChannelsLite(channelId, currentSessionId);
890
+ console.log(`[selectChannel] renderChannelsLite 耗时 ${(performance.now() - t0).toFixed(1)}ms`);
735
891
 
736
892
  // 确保该频道有消息容器
737
893
  const container = ensureMessageContainer(channelId);
@@ -750,10 +906,19 @@ async function selectChannel(channelId, targetSessionId = null) {
750
906
  try {
751
907
  const res = await fetch(`/sessions/${channelId}?sessionId=${encodeURIComponent(currentSessionId)}`);
752
908
  const session = await res.json();
753
- if (session.messages && session.messages.length > 0) {
754
- session.messages.forEach(msg => {
755
- addMessage(msg.content, msg.type, false, container);
756
- });
909
+ const msgs = session.messages || [];
910
+ if (msgs.length > 0) {
911
+ // 2026-06-11 提速: 用 DocumentFragment 一次性 append 避免多次 reflow
912
+ const frag = document.createDocumentFragment();
913
+ const tmpContainer = document.createElement('div');
914
+ tmpContainer.style.display = 'none';
915
+ for (const msg of msgs) {
916
+ addMessage(msg.content, msg.type, false, tmpContainer);
917
+ }
918
+ while (tmpContainer.firstChild) {
919
+ frag.appendChild(tmpContainer.firstChild);
920
+ }
921
+ container.appendChild(frag);
757
922
  } else {
758
923
  addMessage('你好!我是 Bolloon Agent。有什么我可以帮你的吗?', 'ai', false, container);
759
924
  }
@@ -1364,7 +1529,7 @@ function handleWorkflowLoopEvent(data, container) {
1364
1529
  // 用户命令可视化 - 当用户发送命令时调用
1365
1530
  let userCommandDisplayEl = null;
1366
1531
 
1367
- function showUserCommand(command, container) {
1532
+ function showUserCommand(command, container, opts) {
1368
1533
  const msgContainer = container || messagesContainers.get(currentChannelId) || messagesEl;
1369
1534
  // 先移除之前的消息中的 user bubble(如果有重复的话)
1370
1535
  const existingUserBubbles = msgContainer.querySelectorAll('.message-user');
@@ -1378,12 +1543,17 @@ function showUserCommand(command, container) {
1378
1543
  // 创建美化版本的命令显示
1379
1544
  userCommandDisplayEl = document.createElement('div');
1380
1545
  userCommandDisplayEl.className = 'message message-user';
1546
+ // v3 新增: 远端访客消息加 tag (source === 'remote' 表示是 B 通过 P2P 发来的)
1547
+ const sourceTag = (opts && opts.source === 'remote')
1548
+ ? `<div style="font-size:10px;color:#6b7280;margin-bottom:2px;">🌐 远端访客${opts.fromPublicKey ? ' (' + opts.fromPublicKey.substring(0, 8) + '…)' : ''} → A 的 channel</div>`
1549
+ : '';
1381
1550
  userCommandDisplayEl.innerHTML = `
1382
1551
  <div class="user-command-display">
1383
1552
  <div class="command-prompt">
1384
1553
  <span class="prompt-icon">›</span>
1385
1554
  <span class="prompt-text">${command}</span>
1386
1555
  </div>
1556
+ ${sourceTag}
1387
1557
  </div>
1388
1558
  `;
1389
1559
 
@@ -1490,7 +1660,19 @@ function connect(channelId) {
1490
1660
  const container = messagesContainers.get(msgChannelId) || messagesEl;
1491
1661
 
1492
1662
  if (data.type === 'user') {
1493
- showUserCommand(data.content, container);
1663
+ // 2026-06-11 修: 不再走 showUserCommand ( 装饰条) 路径, 因为:
1664
+ // 1. sendMessage 已经在客户端 addMessage(text, 'user', true) 渲染成 .bubble-user 气泡
1665
+ // 2. SSE 推 user 又调 showUserCommand → 同时出现气泡 + 装饰条 (双重显示)
1666
+ // 3. 第二次切 channel 时, showUserCommand 会 remove 已有 .message-user 元素 (line 1477),
1667
+ // 但 .bubble-user class 不是 .message-user → 残留装饰条, 表现"模式变了"
1668
+ // 改法: SSE 收到 user 后, 跳过显示 (lastUserCommand 已经匹配, addMessage(save=true) 内部去重)
1669
+ // 但要确保 lastUserCommand 已经设过 — sendMessage 调 addMessage(true) 时会设
1670
+ // 远端 user (source === 'remote') 不会被 sendMessage 渲染, 需要走 addMessage 一次
1671
+ if (data.source === 'remote') {
1672
+ // 远端访客 (B 通过 P2P 发来的), sendMessage 没渲染它, 这里补上气泡
1673
+ addMessage(data.content, 'user', true, container);
1674
+ }
1675
+ // 本地 user 已经由 sendMessage 渲染 + 去重, 这里不再显示
1494
1676
  } else if (data.type === 'ai') {
1495
1677
  addMessage(data.content, 'ai', true, container);
1496
1678
  hideTyping();
@@ -1543,6 +1725,14 @@ async function sendMessage() {
1543
1725
  const text = input.value.trim();
1544
1726
  if (!text) return;
1545
1727
 
1728
+ // 2026-06-11: 立即把用户消息渲染成气泡上屏 (走 .bubble-user, 跟本地聊天一致)
1729
+ // 之前只靠 SSE `type: user` 回调显示, 但 addMessage(user) 默认 save=true 走去重, 容易跟 SSE 二次显示冲突/丢失
1730
+ // 现在: sendMessage 自己上屏, SSE `user` 回调来时因为 lastUserCommand 已匹配, 自动跳过 → 不重复
1731
+ const container = messagesContainers.get(currentChannelId) || messagesEl;
1732
+ addMessage(text, 'user', true, container);
1733
+ // 滚动到底
1734
+ if (container) container.scrollTop = container.scrollHeight;
1735
+
1546
1736
  input.value = '';
1547
1737
  showTyping();
1548
1738
 
@@ -1731,13 +1921,23 @@ function applyMention(channel) {
1731
1921
  }
1732
1922
 
1733
1923
  function updateMentionDropdown() {
1734
- if (!mentionChannels.length) return;
1924
+ // 2026-06-10 修: 数组空时主动刷一次, 不再静默 return
1925
+ // 之前 `if (!mentionChannels.length) return` 导致初始化 0-8s 窗口按 @ 看不到任何 item
1926
+ if (!mentionChannels.length) {
1927
+ refreshMentionChannels().then(() => {
1928
+ // 拉完再重试一次 (异步, 不阻塞当前键击)
1929
+ if (mentionChannels.length) updateMentionDropdown();
1930
+ });
1931
+ return;
1932
+ }
1735
1933
  const m = getCurrentMentionQuery();
1736
1934
  if (!m) { closeMentionDropdown(); return; }
1737
1935
  // 只在 dropdown 刚打开时设置 anchor (blockEnd 跟着 insert 走)
1738
1936
  if (mentionAnchor === -1) {
1739
1937
  mentionAnchor = m.anchor;
1740
1938
  mentionBlockEnd = m.anchor + 1 + (m.query || '').length;
1939
+ // dropdown 首次打开 → 强制刷一次, 保证 remote 列表最新
1940
+ refreshMentionChannels();
1741
1941
  }
1742
1942
  mentionQuery = m.query;
1743
1943
  const q = m.query.toLowerCase();
@@ -1876,12 +2076,20 @@ function setupMentionAutocomplete(inputEl) {
1876
2076
  }
1877
2077
 
1878
2078
  function update() {
1879
- if (!mentionChannels.length) return;
2079
+ // 2026-06-10 修: 与主输入框同步 — 数组空时主动刷新, 首次打开 dropdown 强制刷新
2080
+ if (!mentionChannels.length) {
2081
+ refreshMentionChannels().then(() => {
2082
+ if (mentionChannels.length) update();
2083
+ });
2084
+ return;
2085
+ }
1880
2086
  const m = detectQuery();
1881
2087
  if (!m) { closeLocal(); return; }
1882
2088
  if (localAnchor === -1) {
1883
2089
  localAnchor = m.anchor;
1884
2090
  localBlockEnd = m.anchor + 1 + (m.query || '').length;
2091
+ // dropdown 首次打开 → 强制刷一次保证 remote 最新
2092
+ refreshMentionChannels();
1885
2093
  }
1886
2094
  localQuery = m.query;
1887
2095
  const q = m.query.toLowerCase();
@@ -2519,7 +2727,6 @@ function renderJudgmentItems(items, opts) {
2519
2727
  <input type="checkbox" class="judgment-select-cb" data-id="${escapeHtml(j.id)}" style="cursor:pointer;" onclick="event.stopPropagation();">
2520
2728
  </label>
2521
2729
  <div class="task-item-title">
2522
- <span>🛡️</span>
2523
2730
  <span class="judgment-decision">${escapeHtml(j.decision)}</span>
2524
2731
  </div>
2525
2732
  <span class="task-item-status completed">${stakes}</span>
@@ -2905,11 +3112,33 @@ let knownPeers = []; // { name, publicKey, lastConnectedAt, addedAt }
2905
3112
 
2906
3113
  async function loadRemoteChannels() {
2907
3114
  try {
3115
+ // 1) 拉 known peers (好友列表)
2908
3116
  const res = await fetch('/api/p2p-peers');
2909
- if (!res.ok) return;
2910
- const data = await res.json();
2911
- knownPeers = Array.isArray(data.peers) ? data.peers : [];
3117
+ if (res.ok) {
3118
+ const data = await res.json();
3119
+ knownPeers = Array.isArray(data.peers) ? data.peers : [];
3120
+ }
3121
+ // 2) 2026-06-10 修: 同时拉 /api/remote-channels, 兜底 SSE 推送漏掉的情况
3122
+ // (页面刷新后 remoteChannels[] = [], 必须主动拉一次才有数据)
3123
+ const r2 = await fetch('/api/remote-channels');
3124
+ if (r2.ok) {
3125
+ const data2 = await r2.json();
3126
+ const peers = Array.isArray(data2.peers) ? data2.peers : [];
3127
+ // 合并到 remoteChannels[]: 按 peerId 覆盖
3128
+ for (const p of peers) {
3129
+ let group = remoteChannels.find(g => g.peerId === p.peerId);
3130
+ if (!group) {
3131
+ group = { peerId: p.peerId, channels: [], peerName: ('peer-' + p.peerId.substring(0, 8)) };
3132
+ remoteChannels.push(group);
3133
+ }
3134
+ group.channels = p.channels || [];
3135
+ }
3136
+ }
2912
3137
  renderRemoteChannels();
3138
+ // 3) 远端数据可能变化, 同步 @-mention 列表
3139
+ if (typeof refreshMentionChannels === 'function') {
3140
+ refreshMentionChannels();
3141
+ }
2913
3142
  } catch (err) {
2914
3143
  console.error('[v3] loadRemoteChannels 失败:', err);
2915
3144
  }
@@ -2919,30 +3148,60 @@ function renderRemoteChannels() {
2919
3148
  const list = document.getElementById('remote-channel-list');
2920
3149
  if (!list) return;
2921
3150
 
2922
- // Phase 3 重做: 好友列表 + 收到的 channel 分组
2923
- if (knownPeers.length === 0) {
2924
- list.innerHTML = '<li style="color:var(--text-muted);font-size:11px;padding:8px 4px;text-align:center;">(暂无好友, 点 + 添加)</li>';
2925
- return;
2926
- }
2927
-
2928
3151
  // 按 peerId 分组 channels
2929
3152
  const channelsByPeer = {};
2930
3153
  for (const p of remoteChannels) {
2931
3154
  channelsByPeer[p.peerId] = p.channels || [];
2932
3155
  }
2933
3156
 
2934
- const html = knownPeers.map(peer => {
3157
+ // 2026-06-10 修: 之前 UI 只渲染 knownPeers, 但对面 publicKey 可能跟本机 known_peers 不匹配
3158
+ // (例如对面重启 / 换 role / 第一次相连还没加为好友), 导致 remoteChannels 里有数据 UI 却空白.
3159
+ // 修法: 把 remoteChannels 里的 "陌生 peer" (不在 known_peers 里) 也渲染出来, 标记为未加好友.
3160
+ const knownPks = new Set(knownPeers.map(p => p.publicKey));
3161
+ const strangerPeers = remoteChannels
3162
+ .filter(p => !knownPks.has(p.peerId))
3163
+ .map(p => ({
3164
+ publicKey: p.peerId,
3165
+ name: p.peerName || ('未授权 ' + p.peerId.substring(0, 8)),
3166
+ lastConnectedAt: null,
3167
+ _isStranger: true
3168
+ }));
3169
+ const allPeers = [...knownPeers, ...strangerPeers];
3170
+
3171
+ if (allPeers.length === 0) {
3172
+ list.innerHTML = '<li style="color:var(--text-muted);font-size:11px;padding:8px 4px;text-align:center;">(暂无好友, 点 + 添加)</li>';
3173
+ return;
3174
+ }
3175
+
3176
+ const html = allPeers.map(peer => {
2935
3177
  const peerChannels = channelsByPeer[peer.publicKey] || [];
2936
3178
  const lastConn = peer.lastConnectedAt
2937
3179
  ? new Date(peer.lastConnectedAt).toLocaleDateString()
2938
- : '从未连接';
3180
+ : (peer._isStranger ? '陌生 peer' : '从未连接');
3181
+ const strangerStyle = peer._isStranger ? 'border:1px dashed var(--border-light);' : '';
3182
+ const strangerIcon = peer._isStranger ? '❔' : '👤';
3183
+ // 2026-06-11: 折叠逻辑 (全不展开)
3184
+ // - 所有 peer 首次见都默认 *折叠* (包括 known_peers 第一个) — 用户一进来看到完整 peer 列表
3185
+ // - 标题栏右侧 "X ch" 提示有内容, 用户点 caret 展开
3186
+ // - 已见过: 沿用 collapsedPeers (用户上次选择)
3187
+ // - "全部展开/折叠" 按钮在 P2P header (id=p2p-toggle-all-btn)
3188
+ if (!seenPeers.has(peer.publicKey)) {
3189
+ seenPeers.add(peer.publicKey);
3190
+ collapsedPeers.add(peer.publicKey); // 全部默认折叠
3191
+ saveSeenPeers();
3192
+ saveCollapsedPeers();
3193
+ }
3194
+ const isCollapsed = collapsedPeers.has(peer.publicKey);
3195
+ const caretChar = '▾'; // CSS rotate -90deg 处理折叠态
2939
3196
  return `
2940
- <li class="remote-peer-group" style="margin-bottom:10px;">
3197
+ <li class="remote-peer-group ${isCollapsed ? 'collapsed' : ''}" style="margin-bottom:10px;${strangerStyle}">
2941
3198
  <div class="remote-peer-header" data-peer-name="${escapeHtml(peer.name)}" data-peer-pk="${escapeHtml(peer.publicKey)}"
2942
- style="display:flex;align-items:center;gap:6px;padding:4px 6px;background:var(--bg-hover);border-radius:4px;cursor:pointer;">
2943
- <span style="font-size:12px;">👤</span>
2944
- <span style="flex:1;font-size:12px;font-weight:600;" title="${escapeHtml(peer.publicKey)}">${escapeHtml(peer.name)}</span>
2945
- <span style="font-size:9px;color:var(--text-muted);">${lastConn}</span>
3199
+ style="display:flex;align-items:center;gap:6px;padding:6px 8px;background:var(--bg-hover);border-radius:4px;cursor:pointer;">
3200
+ <button class="peer-caret-btn" data-toggle-peer="${escapeHtml(peer.publicKey)}" title="折叠/展开"
3201
+ style="background:var(--bg-active);border:1px solid var(--border);color:var(--text);cursor:pointer;width:22px;height:22px;border-radius:4px;font-size:12px;line-height:1;padding:0;display:flex;align-items:center;justify-content:center;flex:0 0 auto;">${caretChar}</button>
3202
+ <span style="font-size:13px;">${strangerIcon}</span>
3203
+ <span style="flex:1;font-size:12px;font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="${escapeHtml(peer.publicKey)}">${escapeHtml(peer.name)}</span>
3204
+ <span style="font-size:9px;color:var(--text-muted);">${peerChannels.length > 0 ? `${peerChannels.length} ch · ` : ''}${lastConn}</span>
2946
3205
  </div>
2947
3206
  <div class="remote-peer-channels" style="margin-top:4px;margin-left:8px;">
2948
3207
  ${peerChannels.length === 0
@@ -2952,7 +3211,6 @@ function renderRemoteChannels() {
2952
3211
  style="display:flex;align-items:center;gap:6px;padding:4px 6px;cursor:pointer;border-radius:4px;font-size:12px;">
2953
3212
  <span>🤖</span>
2954
3213
  <span style="flex:1;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="${escapeHtml(c.name || '')}">${escapeHtml(c.name || '(未命名)')}</span>
2955
- <span title="对方 judgment 数 (不会同步到本地)" style="font-size:9px;color:var(--text-muted);">🧠${c.boundJudgmentCount || 0}</span>
2956
3214
  </div>
2957
3215
  `).join('')
2958
3216
  }
@@ -2962,6 +3220,15 @@ function renderRemoteChannels() {
2962
3220
  }).join('');
2963
3221
  list.innerHTML = html;
2964
3222
 
3223
+ // 2026-06-10: 折叠按钮点击 → 切折叠 (stopPropagation 防止冒泡触发 header 的分享 modal)
3224
+ list.querySelectorAll('.peer-caret-btn[data-toggle-peer]').forEach(btn => {
3225
+ btn.addEventListener('click', (e) => {
3226
+ e.stopPropagation();
3227
+ const pk = btn.getAttribute('data-toggle-peer');
3228
+ togglePeerCollapsed(pk);
3229
+ });
3230
+ });
3231
+
2965
3232
  // 绑定: 点击 channel → 弹聊天窗口
2966
3233
  list.querySelectorAll('.remote-channel-row').forEach(row => {
2967
3234
  row.addEventListener('click', () => {
@@ -2974,15 +3241,104 @@ function renderRemoteChannels() {
2974
3241
  });
2975
3242
  // 绑定: 点击 peer 头部 → 弹分享 modal (让 A 决定分享本机哪些 channel 给这个 peer)
2976
3243
  list.querySelectorAll('.remote-peer-header').forEach(row => {
2977
- row.addEventListener('click', () => {
3244
+ row.addEventListener('click', (e) => {
3245
+ // 2026-06-10: 防御 — 点 caret 时已 stopPropagation, 但万一冒泡逃逸再挡一道
3246
+ if (e.target.closest('.peer-caret')) return;
2978
3247
  const peerName = row.dataset.peerName;
2979
3248
  const peerPk = row.dataset.peerPk;
2980
3249
  openShareToPeerModal(peerName, peerPk);
2981
3250
  });
2982
3251
  });
3252
+
3253
+ // 2026-06-10: 每个 peer 头部双击 → 改名字 / 改备注
3254
+ list.querySelectorAll('.remote-peer-header').forEach(row => {
3255
+ row.addEventListener('dblclick', (e) => {
3256
+ if (e.target.closest('.peer-caret-btn')) return;
3257
+ const peerName = row.dataset.peerName;
3258
+ const peerPk = row.dataset.peerPk;
3259
+ openEditPeerModal(peerName, peerPk);
3260
+ });
3261
+ });
3262
+
3263
+ // 2026-06-10: 渲染完成后同步 header 切换按钮图标
3264
+ if (typeof window.__syncP2PToggleAllBtn === 'function') window.__syncP2PToggleAllBtn();
3265
+ }
3266
+
3267
+ /** v3: 改 peer 名字 / 备注 modal (持久化到 known_peers.json) */
3268
+ async function openEditPeerModal(peerName, peerPublicKey) {
3269
+ document.getElementById('edit-peer-modal')?.remove();
3270
+ // 先读 known_peers 拿到现有 notes
3271
+ let currentNotes = '';
3272
+ let currentName = peerName;
3273
+ try {
3274
+ const r = await fetch('/api/p2p-peers');
3275
+ if (r.ok) {
3276
+ const d = await r.json();
3277
+ const entry = (d.peers || []).find(p => p.publicKey === peerPublicKey);
3278
+ if (entry) {
3279
+ currentName = entry.name || peerName;
3280
+ currentNotes = entry.notes || '';
3281
+ }
3282
+ }
3283
+ } catch {}
3284
+ const html = `
3285
+ <div id="edit-peer-modal" class="friend-req-overlay">
3286
+ <div class="friend-req-shell" style="width:520px;">
3287
+ <div class="friend-req-header">
3288
+ <span style="font-size:18px;">✏️</span>
3289
+ <div style="flex:1;min-width:0;">
3290
+ <div class="friend-req-title">编辑好友</div>
3291
+ <div class="friend-req-meta">publicKey: ${escapeHtml(peerPublicKey.substring(0,16))}…</div>
3292
+ </div>
3293
+ </div>
3294
+ <div class="friend-req-body">
3295
+ <label style="display:block;margin-bottom:6px;font-size:12px;color:var(--text-secondary);">显示名字</label>
3296
+ <input id="epm-name" type="text" value="${escapeHtml(currentName)}"
3297
+ style="width:100%;padding:8px 10px;border:1px solid var(--border);border-radius:4px;background:var(--bg-main);color:var(--text);font-family:inherit;font-size:13px;box-sizing:border-box;margin-bottom:12px;">
3298
+ <label style="display:block;margin-bottom:6px;font-size:12px;color:var(--text-secondary);">备注 (自由文本, 例如合作领域 / 怎么认识的)</label>
3299
+ <textarea id="epm-notes" rows="4" placeholder="例如: 2026-06 合作 LLM 代发验证"
3300
+ style="width:100%;padding:8px 10px;border:1px solid var(--border);border-radius:4px;background:var(--bg-main);color:var(--text);font-family:inherit;font-size:13px;box-sizing:border-box;resize:vertical;">${escapeHtml(currentNotes)}</textarea>
3301
+ </div>
3302
+ <div class="friend-req-actions">
3303
+ <button id="epm-cancel" class="friend-req-btn-deny">取消</button>
3304
+ <button id="epm-save" class="friend-req-btn-accept">保存</button>
3305
+ </div>
3306
+ </div>
3307
+ </div>
3308
+ `;
3309
+ document.body.insertAdjacentHTML('beforeend', html);
3310
+ const close = () => document.getElementById('edit-peer-modal')?.remove();
3311
+ document.getElementById('epm-cancel').onclick = close;
3312
+ document.getElementById('epm-save').onclick = async () => {
3313
+ const newName = document.getElementById('epm-name').value.trim() || currentName;
3314
+ const newNotes = document.getElementById('epm-notes').value;
3315
+ try {
3316
+ const r = await fetch(`/api/p2p-peers/${encodeURIComponent(peerName)}`, {
3317
+ method: 'PATCH',
3318
+ headers: { 'Content-Type': 'application/json' },
3319
+ body: JSON.stringify({ name: newName, notes: newNotes })
3320
+ });
3321
+ const data = await r.json();
3322
+ if (!r.ok) throw new Error(data.error || 'save failed');
3323
+ console.log('[v3] 改 peer 成功:', newName, '备注:', newNotes);
3324
+ showSimpleToast(`✅ 已保存 ${newName}`);
3325
+ close();
3326
+ // 重新拉 known_peers + 远程 channels 重新渲染
3327
+ const r2 = await fetch('/api/p2p-peers');
3328
+ if (r2.ok) {
3329
+ const d2 = await r2.json();
3330
+ knownPeers = Array.isArray(d2.peers) ? d2.peers : [];
3331
+ }
3332
+ renderRemoteChannels();
3333
+ } catch (err) {
3334
+ console.error('[v3] 保存 peer 失败:', err);
3335
+ alert('保存失败: ' + (err.message || err));
3336
+ }
3337
+ };
2983
3338
  }
2984
3339
 
2985
3340
  /** v3: 分享 channel 给指定 peer 的 modal (A 侧用) */
3341
+ /** v3: 分享 channel 给指定 peer 的 modal (A 侧用) — 2026-06-11 改用 Step 3 风格 class */
2986
3342
  async function openShareToPeerModal(peerName, peerPublicKey) {
2987
3343
  document.getElementById('share-to-peer-modal')?.remove();
2988
3344
  let allChannels = [];
@@ -2991,36 +3347,37 @@ async function openShareToPeerModal(peerName, peerPublicKey) {
2991
3347
  if (res.ok) allChannels = await res.json();
2992
3348
  } catch (err) { console.error('openShareToPeerModal:', err); }
2993
3349
  const rows = allChannels.length === 0
2994
- ? '<div style="color:#6b7280;padding:12px;text-align:center;">还没有 channel</div>'
3350
+ ? '<div class="share-modal-empty">还没有 channel</div>'
2995
3351
  : allChannels.map(ch => {
2996
3352
  const isShared = Array.isArray(ch.shared_with_peers) && ch.shared_with_peers.includes(peerPublicKey);
2997
3353
  return `
2998
- <label style="display:flex;align-items:flex-start;gap:8px;padding:6px 4px;cursor:pointer;border-bottom:1px solid #f3f4f6;">
2999
- <input type="checkbox" data-cid="${escapeHtml(ch.id)}" ${isShared ? 'checked' : ''} style="margin-top:4px;cursor:pointer;">
3000
- <div style="flex:1;min-width:0;">
3001
- <div style="font-size:13px;font-weight:500;">${escapeHtml(ch.name)}</div>
3002
- <div style="font-size:10px;color:#9ca3af;margin-top:2px;">
3003
- ${isShared ? '✓ 已分享' : '未分享'} · ${ch.id}
3354
+ <label class="share-modal-row">
3355
+ <input type="checkbox" data-cid="${escapeHtml(ch.id)}" ${isShared ? 'checked' : ''} class="share-modal-cb">
3356
+ <div class="share-modal-row-info">
3357
+ <div class="share-modal-row-name">${escapeHtml(ch.name)}</div>
3358
+ <div class="share-modal-row-meta">
3359
+ ${isShared ? '✓ 已分享' : '未分享'} · ${escapeHtml(ch.id.slice(0, 24))}
3004
3360
  </div>
3005
3361
  </div>
3006
3362
  </label>
3007
3363
  `;
3008
3364
  }).join('');
3009
3365
  const html = `
3010
- <div id="share-to-peer-modal" style="position:fixed;inset:0;background:rgba(0,0,0,0.4);z-index:10003;display:flex;align-items:center;justify-content:center;">
3011
- <div style="background:#fff;border-radius:8px;width:480px;max-width:92vw;max-height:80vh;display:flex;flex-direction:column;box-shadow:0 10px 40px rgba(0,0,0,0.2);">
3012
- <div style="padding:12px 16px;border-bottom:1px solid #e5e7eb;display:flex;align-items:center;justify-content:space-between;">
3013
- <div>
3014
- <div style="font-size:15px;font-weight:600;">分享 channel 给 ${escapeHtml(peerName)}</div>
3015
- <div style="font-size:11px;color:#6b7280;margin-top:2px;">${escapeHtml(peerPublicKey.substring(0,16))}…</div>
3366
+ <div id="share-to-peer-modal" class="friend-req-overlay">
3367
+ <div class="friend-req-shell share-modal-shell">
3368
+ <div class="friend-req-header">
3369
+ <span style="font-size:18px;">📤</span>
3370
+ <div style="flex:1;min-width:0;">
3371
+ <div class="friend-req-title">分享 channel 给 ${escapeHtml(peerName)}</div>
3372
+ <div class="friend-req-meta">${escapeHtml(peerPublicKey.substring(0,16))}…</div>
3016
3373
  </div>
3017
- <button id="spm-close" style="background:none;border:none;font-size:20px;color:#6b7280;cursor:pointer;">×</button>
3374
+ <button id="spm-close" class="friend-req-btn-close">×</button>
3018
3375
  </div>
3019
- <div style="padding:8px 12px;background:#f9fafb;font-size:12px;color:#6b7280;">勾选要分享的 channel, 对方才能看到</div>
3020
- <div id="spm-list" style="flex:1;overflow-y:auto;padding:8px 16px;">${rows}</div>
3021
- <div style="padding:12px 20px;border-top:1px solid #e5e7eb;display:flex;justify-content:flex-end;gap:8px;">
3022
- <button id="spm-cancel" style="padding:6px 14px;border:1px solid #d1d5db;background:#fff;border-radius:4px;cursor:pointer;font-size:13px;">取消</button>
3023
- <button id="spm-save" style="padding:6px 14px;background:#2563eb;color:#fff;border:none;border-radius:4px;cursor:pointer;font-size:13px;">保存分享</button>
3376
+ <div class="share-modal-hint">勾选要分享的 channel, 对方才能看到</div>
3377
+ <div id="spm-list" class="share-modal-list">${rows}</div>
3378
+ <div class="friend-req-actions">
3379
+ <button id="spm-cancel" class="friend-req-btn-deny">取消</button>
3380
+ <button id="spm-save" class="friend-req-btn-accept">保存分享</button>
3024
3381
  </div>
3025
3382
  </div>
3026
3383
  </div>
@@ -3036,7 +3393,7 @@ async function openShareToPeerModal(peerName, peerPublicKey) {
3036
3393
  for (const ch of allChannels) {
3037
3394
  const shouldShare = checkedIds.includes(ch.id);
3038
3395
  const wasShared = Array.isArray(ch.shared_with_peers) && ch.shared_with_peers.includes(peerPublicKey);
3039
- if (shouldShare === wasShared) continue; // 没变化跳过
3396
+ if (shouldShare === wasShared) continue;
3040
3397
  const newList = (ch.shared_with_peers || []).filter((p) => p !== peerPublicKey);
3041
3398
  if (shouldShare) newList.push(peerPublicKey);
3042
3399
  try {
@@ -3048,34 +3405,36 @@ async function openShareToPeerModal(peerName, peerPublicKey) {
3048
3405
  if (res.ok) ok++; else fail++;
3049
3406
  } catch { fail++; }
3050
3407
  }
3051
- alert(`分享更新完成: 成功 ${ok}, 失败 ${fail}`);
3408
+ showSimpleToast(`分享更新完成: 成功 ${ok}, 失败 ${fail}`, ok > 0 ? 'info' : (fail > 0 ? 'error' : 'info'));
3052
3409
  overlay.remove();
3053
3410
  };
3054
3411
  }
3055
3412
 
3056
- /** v3: 跟远端 channel 聊天的简易弹窗 */
3413
+ /** v3: 跟远端 channel 聊天的简易弹窗
3414
+ * 2026-06-10 重写: UI 完全对齐本地聊天 (复用 addMessage / .messages / .bubble 整套样式),
3415
+ * marked.parse + cleanThink + cleanEnv 自动生效, 不再裸文本.
3416
+ */
3057
3417
  function openRemoteChannelChat(peerPublicKey, channelId, channelName) {
3058
3418
  // 移除已有 modal
3059
3419
  document.getElementById('remote-chat-modal')?.remove();
3060
3420
  const html = `
3061
- <div id="remote-chat-modal" style="position:fixed;inset:0;background:rgba(0,0,0,0.5);z-index:10002;display:flex;align-items:center;justify-content:center;">
3062
- <div style="background:#fff;border-radius:8px;width:560px;max-width:92vw;max-height:80vh;display:flex;flex-direction:column;box-shadow:0 10px 40px rgba(0,0,0,0.2);">
3063
- <div style="padding:12px 16px;border-bottom:1px solid #e5e7eb;display:flex;align-items:center;justify-content:space-between;">
3421
+ <div id="remote-chat-modal" class="remote-chat-overlay">
3422
+ <div class="remote-chat-shell">
3423
+ <div class="remote-chat-header">
3064
3424
  <div style="flex:1;min-width:0;">
3065
- <div style="font-size:15px;font-weight:600;">跟 ${escapeHtml(channelName)} 聊天</div>
3066
- <div style="font-size:11px;color:#6b7280;margin-top:2px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">远端 peer: ${escapeHtml(peerPublicKey.substring(0,16))}… · ${escapeHtml(channelId)}</div>
3425
+ <div class="remote-chat-title">🌐 ${escapeHtml(channelName)} 聊天</div>
3426
+ <div class="remote-chat-meta">远端 peer: ${escapeHtml(peerPublicKey.substring(0,16))}… · ${escapeHtml(channelId)}</div>
3067
3427
  </div>
3068
- <button id="rcm-refresh-history" title="重新拉历史" style="background:none;border:1px solid var(--border);color:#6b7280;cursor:pointer;padding:2px 8px;border-radius:4px;font-size:11px;margin-right:6px;">↻ 历史</button>
3069
- <button id="rcm-close" style="background:none;border:none;font-size:20px;color:#6b7280;cursor:pointer;">×</button>
3428
+ <button id="rcm-refresh-history" title="重新拉历史" class="remote-chat-btn-secondary">↻ 历史</button>
3429
+ <button id="rcm-close" class="remote-chat-btn-close">×</button>
3070
3430
  </div>
3071
- <div id="rcm-thinking" style="display:none;padding:8px 16px;background:#fef3c7;color:#92400e;font-size:12px;border-bottom:1px solid #e5e7eb;">
3431
+ <div id="rcm-thinking" class="remote-chat-thinking" style="display:none;">
3072
3432
  📥 正在从远端拉历史 + 判断力…
3073
3433
  </div>
3074
- <div id="rcm-log" style="flex:1;overflow-y:auto;padding:12px 16px;min-height:240px;max-height:60vh;background:#f9fafb;"></div>
3075
- <div style="padding:10px 12px;border-top:1px solid #e5e7eb;display:flex;gap:6px;">
3076
- <input id="rcm-input" type="text" placeholder="输入消息, 发送到远端 channel..."
3077
- style="flex:1;padding:8px 10px;border:1px solid #d1d5db;border-radius:4px;font-size:13px;">
3078
- <button id="rcm-send" style="padding:8px 14px;background:#2563eb;color:#fff;border:none;border-radius:4px;cursor:pointer;font-size:13px;">发送</button>
3434
+ <div id="rcm-log" class="messages remote-chat-log"></div>
3435
+ <div class="remote-chat-input-row">
3436
+ <input id="rcm-input" type="text" placeholder="输入消息, 发送到远端 channel..." class="remote-chat-input">
3437
+ <button id="rcm-send" class="remote-chat-btn-send">发送</button>
3079
3438
  </div>
3080
3439
  </div>
3081
3440
  </div>
@@ -3086,40 +3445,51 @@ function openRemoteChannelChat(peerPublicKey, channelId, channelName) {
3086
3445
  const inputEl = document.getElementById('rcm-input');
3087
3446
  const sendBtn = document.getElementById('rcm-send');
3088
3447
  const thinkingEl = document.getElementById('rcm-thinking');
3089
- document.getElementById('rcm-close').onclick = () => document.getElementById('remote-chat-modal').remove();
3090
- document.getElementById('rcm-refresh-history').onclick = () => loadHistory();
3448
+ let historyRefreshTimer = null;
3449
+ document.getElementById('rcm-close').onclick = () => {
3450
+ if (historyRefreshTimer) { clearInterval(historyRefreshTimer); historyRefreshTimer = null; }
3451
+ document.getElementById('remote-chat-modal').remove();
3452
+ };
3453
+ document.getElementById('rcm-refresh-history').onclick = () => loadHistory(false);
3091
3454
 
3455
+ // 2026-06-10 改: 直接复用本地 addMessage, 自动获得 marked + think 折叠 + env 折叠 + 主题变量
3092
3456
  const append = (text, role) => {
3093
- const bubble = document.createElement('div');
3094
- const isUser = role === 'user';
3095
- bubble.style.cssText = `padding:8px 10px;margin:4px 0;border-radius:6px;font-size:13px;line-height:1.4;max-width:80%;word-wrap:break-word;${
3096
- isUser ? 'background:#2563eb;color:#fff;margin-left:auto;text-align:left;'
3097
- : 'background:#e5e7eb;color:#111;'
3098
- }`;
3099
- bubble.textContent = text;
3100
- log.appendChild(bubble);
3457
+ addMessage(text, role === 'user' ? 'user' : 'ai', false, log);
3101
3458
  log.scrollTop = log.scrollHeight;
3102
3459
  };
3103
3460
 
3461
+ // 系统提示用更轻量的样式 (不走 addMessage, 避免被当聊天记录裁剪)
3104
3462
  const appendSystem = (text, kind = 'info') => {
3105
3463
  const el = document.createElement('div');
3106
- const colors = {
3107
- info: { bg: '#dbeafe', fg: '#1e40af' },
3108
- warn: { bg: '#fef3c7', fg: '#92400e' },
3109
- error: { bg: '#fca5a5', fg: '#7f1d1d' }
3110
- };
3111
- const c = colors[kind] || colors.info;
3112
- el.style.cssText = `margin:6px 0;padding:6px 10px;background:${c.bg};color:${c.fg};border-radius:4px;font-size:11px;text-align:center;`;
3464
+ el.className = `remote-chat-sysmsg remote-chat-sysmsg-${kind}`;
3113
3465
  el.textContent = text;
3114
3466
  log.appendChild(el);
3115
3467
  log.scrollTop = log.scrollHeight;
3116
3468
  };
3117
3469
 
3118
3470
  // v3 新增: 拉 A 端的 channel 历史 (含 messages + judgments)
3119
- async function loadHistory() {
3471
+ async function loadHistory(isSilent) {
3472
+ if (!document.getElementById('remote-chat-modal')) return; // modal 已关闭
3473
+
3474
+ if (isSilent) {
3475
+ try {
3476
+ const res = await fetch(`/api/remote-channels/chat-history?targetPublicKey=${encodeURIComponent(peerPublicKey)}&channelId=${encodeURIComponent(channelId)}`);
3477
+ if (!res.ok || !document.getElementById('remote-chat-modal')) return;
3478
+ const data = await res.json();
3479
+ const newMsgs = data.messages || [];
3480
+ const oldCount = log.querySelectorAll('.message').length;
3481
+ if (newMsgs.length === oldCount) return;
3482
+ const scrollWasAtBottom = log.scrollTop + log.clientHeight >= log.scrollHeight - 30;
3483
+ renderHistory(data);
3484
+ if (scrollWasAtBottom) {
3485
+ setTimeout(() => { log.scrollTop = log.scrollHeight; }, 50);
3486
+ }
3487
+ } catch (_) { /* 静默失败 */ }
3488
+ return;
3489
+ }
3490
+
3120
3491
  thinkingEl.style.display = 'block';
3121
3492
  log.innerHTML = '';
3122
- appendSystem('正在拉取远端 channel 的历史和判断力...', 'info');
3123
3493
  try {
3124
3494
  const res = await fetch(`/api/remote-channels/chat-history?targetPublicKey=${encodeURIComponent(peerPublicKey)}&channelId=${encodeURIComponent(channelId)}`);
3125
3495
  const data = await res.json();
@@ -3128,63 +3498,55 @@ function openRemoteChannelChat(peerPublicKey, channelId, channelName) {
3128
3498
  thinkingEl.style.display = 'none';
3129
3499
  return;
3130
3500
  }
3131
- // 清掉 loading
3132
- log.innerHTML = '';
3133
-
3134
- // 1. 显示 judgment 依据 (header)
3135
- const judgments = data.judgments || { bound: [], candidates: [] };
3136
- if (judgments.bound && judgments.bound.length > 0) {
3137
- const jh = document.createElement('div');
3138
- jh.style.cssText = 'margin:0 0 8px;padding:8px 10px;background:#fef3c7;border-left:3px solid #f59e0b;border-radius:4px;font-size:12px;';
3139
- let h = `<div style="font-weight:600;color:#92400e;margin-bottom:4px;">🛡️ 对方 channel 绑定的判断力 (${judgments.bound.length} 条硬约束)</div>`;
3140
- for (const j of judgments.bound) {
3141
- h += `<div style="margin:3px 0;padding-left:8px;">• <b>${escapeHtml((j.decision || '').slice(0, 100))}</b>${j.domain ? `<span style="color:#a16207;font-size:10px;"> [${escapeHtml(j.domain)}${j.stakes ? '/' + escapeHtml(j.stakes) : ''}]</span>` : ''}${j.reasons && j.reasons.length ? '<br><span style="color:#92400e;font-size:11px;">理由: ' + escapeHtml(j.reasons.join('; ').slice(0, 100)) + '</span>' : ''}</div>`;
3142
- }
3143
- if (judgments.candidates && judgments.candidates.length > 0) {
3144
- h += `<div style="margin-top:6px;color:#92400e;font-size:11px;">+ ${judgments.candidates.length} 条候选判断力 (LLM 可自选参考)</div>`;
3145
- }
3146
- jh.innerHTML = h;
3147
- log.appendChild(jh);
3501
+ renderHistory(data);
3502
+ } catch (err) {
3503
+ appendSystem(`拉取异常: ${err.message}`, 'error');
3504
+ } finally {
3505
+ thinkingEl.style.display = 'none';
3506
+ }
3507
+ }
3508
+
3509
+ function renderHistory(data) {
3510
+ log.innerHTML = '';
3511
+
3512
+ // 1. 显示 judgment 依据 (header) — 保留, 但用 class 化样式
3513
+ const judgments = data.judgments || { bound: [], candidates: [] };
3514
+ if (judgments.bound && judgments.bound.length > 0) {
3515
+ const jh = document.createElement('div');
3516
+ jh.className = 'remote-chat-judgments';
3517
+ let h = `<div class="remote-chat-judgments-title">🛡️ 对方 channel 绑定的判断力 (${judgments.bound.length} 条硬约束)</div>`;
3518
+ for (const j of judgments.bound) {
3519
+ h += `<div class="remote-chat-judgment-item">• <b>${escapeHtml((j.decision || '').slice(0, 100))}</b>${j.domain ? `<span class="remote-chat-judgment-tag"> [${escapeHtml(j.domain)}${j.stakes ? '/' + escapeHtml(j.stakes) : ''}]</span>` : ''}${j.reasons && j.reasons.length ? '<br><span class="remote-chat-judgment-reason">理由: ' + escapeHtml(j.reasons.join('; ').slice(0, 100)) + '</span>' : ''}</div>`;
3520
+ }
3521
+ if (judgments.candidates && judgments.candidates.length > 0) {
3522
+ h += `<div class="remote-chat-judgments-foot">+ ${judgments.candidates.length} 条候选判断力 (LLM 可自选参考)</div>`;
3148
3523
  }
3524
+ jh.innerHTML = h;
3525
+ log.appendChild(jh);
3526
+ }
3149
3527
 
3150
- // 2. 显示历史 messages (带 source 标签: 内部 owner vs 远端访客)
3151
- const msgs = data.messages || [];
3152
- if (msgs.length === 0) {
3153
- appendSystem('还没有历史消息, 在下面发第一条吧', 'info');
3154
- } else {
3155
- appendSystem(`从远端拉到 ${msgs.length} 条历史消息 (A 内部 owner + B 远端访客 都会显示)`, 'info');
3156
- for (const m of msgs) {
3157
- const isUser = m.type === 'user';
3158
- // v3: source 标签
3159
- let tag = '';
3160
- if (isUser) {
3161
- if (m.source === 'remote') {
3162
- tag = `<div style="font-size:10px;color:#6b7280;margin-bottom:2px;">🌐 远端访客${m.fromPublicKey ? ' (' + m.fromPublicKey.substring(0, 8) + '…)' : ''} → A 的 channel</div>`;
3163
- } else {
3164
- tag = `<div style="font-size:10px;color:#6b7280;margin-bottom:2px;">👤 A (内部 owner) → A 的 channel</div>`;
3165
- }
3528
+ // 2. 显示历史 messages 完全复用本地 addMessage 渲染
3529
+ const msgs = data.messages || [];
3530
+ if (msgs.length === 0) {
3531
+ appendSystem('还没有历史消息, 在下面发第一条吧', 'info');
3532
+ } else {
3533
+ for (const m of msgs) {
3534
+ // 远端 owner user 消息 vs 远端访客 (B) 的 user 消息 vs A 的 LLM 回复
3535
+ // 全部走 addMessage, marked/think/env 自动处理. 来源用一个小 prefix 标记.
3536
+ const type = m.type === 'user' ? 'user' : 'ai';
3537
+ let prefix = '';
3538
+ if (m.type === 'user') {
3539
+ if (m.source === 'remote') {
3540
+ prefix = `🌐 远端访客${m.fromPublicKey ? ' (' + m.fromPublicKey.substring(0, 8) + '…)' : ''}\n\n`;
3166
3541
  } else {
3167
- tag = `<div style="font-size:10px;color:#6b7280;margin-bottom:2px;">🤖 A 的 LLM ( A 节点上跑)</div>`;
3542
+ prefix = `👤 A (内部 owner)\n\n`;
3168
3543
  }
3169
- const bubble = document.createElement('div');
3170
- const isRemoteUser = isUser && m.source === 'remote';
3171
- bubble.style.cssText = `padding:8px 10px;margin:4px 0;border-radius:6px;font-size:13px;line-height:1.4;max-width:80%;word-wrap:break-word;${
3172
- isUser
3173
- ? (isRemoteUser
3174
- ? 'background:#dbeafe;color:#1e3a8a;margin-right:auto;text-align:left;border:1px solid #93c5fd;'
3175
- : 'background:#f3f4f6;color:#374151;margin-left:auto;text-align:left;border:1px solid #d1d5db;')
3176
- : 'background:#e5e7eb;color:#111;'
3177
- }`;
3178
- bubble.innerHTML = tag + `<div>${escapeHtml(m.content || '')}</div>`;
3179
- log.appendChild(bubble);
3544
+ } else {
3545
+ prefix = `🤖 A LLM\n\n`;
3180
3546
  }
3181
- // 滚到底部
3182
- setTimeout(() => { log.scrollTop = log.scrollHeight; }, 50);
3547
+ addMessage(prefix + (m.content || ''), type, false, log);
3183
3548
  }
3184
- } catch (err) {
3185
- appendSystem(`拉取异常: ${err.message}`, 'error');
3186
- } finally {
3187
- thinkingEl.style.display = 'none';
3549
+ setTimeout(() => { log.scrollTop = log.scrollHeight; }, 50);
3188
3550
  }
3189
3551
  }
3190
3552
 
@@ -3203,9 +3565,9 @@ function openRemoteChannelChat(peerPublicKey, channelId, channelName) {
3203
3565
  });
3204
3566
  const data = await res.json();
3205
3567
  if (!res.ok) throw new Error(data.error || 'send failed');
3206
- appendSystem('已发送, 等待对方回复...', 'info');
3568
+ // 不再 appendSystem('已发送...') —— 用户看到自己消息已上屏就知道, 系统提示是噪音
3207
3569
  } catch (err) {
3208
- append('(发送失败: ' + (err.message || err) + ')', 'ai');
3570
+ appendSystem('发送失败: ' + (err.message || err), 'error');
3209
3571
  } finally {
3210
3572
  sendBtn.disabled = false;
3211
3573
  sendBtn.textContent = '发送';
@@ -3219,7 +3581,10 @@ function openRemoteChannelChat(peerPublicKey, channelId, channelName) {
3219
3581
  startV3GlobalSSE();
3220
3582
 
3221
3583
  // 打开时立即拉历史
3222
- loadHistory();
3584
+ loadHistory(false);
3585
+
3586
+ // 每 15 秒自动静默刷新, 同步远端 owner 或其他访客的新消息
3587
+ historyRefreshTimer = setInterval(() => loadHistory(true), 15000);
3223
3588
  }
3224
3589
 
3225
3590
  // Phase 3: 我的 ID 按钮 → 真 modal (避免 confirm 在某些环境被禁用)
@@ -3305,20 +3670,166 @@ if (addPeerBtn) {
3305
3670
  return;
3306
3671
  }
3307
3672
  try {
3308
- // p2p-connect (会 joinPeer + 持久化)
3309
- const res = await fetch('/api/remote-channels/p2p-connect', {
3673
+ // v3 新增: 改用 friend-request RPC — 不光 joinPeer, 还发申请到对方
3674
+ // 对方会收到 SSE friend-request 事件, 弹一个申请 modal
3675
+ const res = await fetch('/api/friend-request', {
3310
3676
  method: 'POST',
3311
3677
  headers: { 'Content-Type': 'application/json' },
3312
- body: JSON.stringify({ targetPublicKey: publicKey, name, persist: true })
3678
+ body: JSON.stringify({ targetPublicKey: publicKey, name, message: '想加你为 P2P 好友, 共享 channel 协作' })
3313
3679
  });
3314
3680
  const data = await res.json();
3681
+ if (res.status === 502) {
3682
+ // 2026-06-10: 区分"对方不在线"和"写失败" — 让用户知道是否需要重试
3683
+ const reason = data.code === 'NO_CONN' ? '对方未在线或 P2P 握手超时' : '写入 P2P 通道失败';
3684
+ alert(`好友申请发送失败: ${reason}\n\n本地已记住对方 publicKey (${publicKey.substring(0,8)}...), 等对方上线后可在 P2P 面板手动重试.`);
3685
+ await loadRemoteChannels();
3686
+ return;
3687
+ }
3315
3688
  if (!res.ok) throw new Error(data.error || 'connect failed');
3316
- alert(`已添加好友: ${name} (${publicKey.substring(0, 12)}...)\n重启后会自动重连`);
3689
+ // 成功 — 但不阻塞地等 ack (ack 经 SSE 'friend-request-ack' 推回, 由 v3GlobalEventSource 处理)
3690
+ window.__pendingFriendRequests = window.__pendingFriendRequests || new Map();
3691
+ if (data.requestId) {
3692
+ window.__pendingFriendRequests.set(data.requestId, { name, publicKey, at: Date.now() });
3693
+ // 8s 后还没 ack → 提示用户对方可能跑旧版 (无 ack 协议)
3694
+ setTimeout(() => {
3695
+ if (window.__pendingFriendRequests.has(data.requestId)) {
3696
+ window.__pendingFriendRequests.delete(data.requestId);
3697
+ console.warn(`[v3-friend] 申请超时未收到 ack (requestId=${data.requestId.substring(0,8)})`);
3698
+ showSimpleToast(`⚠️ 对方未确认收到 (可能是旧版客户端, 申请已发出但无法验证)`, 'warn');
3699
+ }
3700
+ }, 8000);
3701
+ }
3702
+ alert(`已发送好友申请给 ${name} (${publicKey.substring(0, 12)}...)\n对方收到后自己端弹申请 modal, 接受后会出现在 P2P 好友区.`);
3317
3703
  await loadRemoteChannels();
3318
3704
  } catch (err) {
3319
- alert('添加失败: ' + (err.message || err));
3705
+ alert('申请失败: ' + (err.message || err));
3706
+ }
3707
+ });
3708
+ }
3709
+
3710
+ /**
3711
+ * v3 新增: 收到好友申请时, 弹一个 modal 让用户接受或拒绝
3712
+ */
3713
+ function showFriendRequestModal(req) {
3714
+ // 移除已有 modal
3715
+ document.getElementById('friend-request-modal')?.remove();
3716
+ // 2026-06-10: 同 Step 3 远端 chat modal 一样, 改用 class + CSS 变量, 跟本地风格统一
3717
+ const html = `
3718
+ <div id="friend-request-modal" class="friend-req-overlay">
3719
+ <div class="friend-req-shell">
3720
+ <div class="friend-req-header">
3721
+ <span style="font-size:20px;">🤝</span>
3722
+ <div style="flex:1;min-width:0;">
3723
+ <div class="friend-req-title">好友申请</div>
3724
+ <div class="friend-req-meta">来自 ${escapeHtml(req.fromName)} (${escapeHtml(req.fromPublicKey.substring(0, 16))}…)</div>
3725
+ </div>
3726
+ </div>
3727
+ <div class="friend-req-body">
3728
+ <p style="margin:0 0 8px;">${escapeHtml(req.message || '想加你为 P2P 好友')}</p>
3729
+ <p style="margin:0;color:var(--text-muted);font-size:11px;">接受后: 双方互加好友, 对方分享的 channel 会自动出现在 P2P 好友区.</p>
3730
+ </div>
3731
+ <div class="friend-req-actions">
3732
+ <button id="frm-deny" class="friend-req-btn-deny">拒绝</button>
3733
+ <button id="frm-accept" class="friend-req-btn-accept">接受</button>
3734
+ </div>
3735
+ </div>
3736
+ </div>
3737
+ `;
3738
+ document.body.insertAdjacentHTML('beforeend', html);
3739
+ const close = () => document.getElementById('friend-request-modal')?.remove();
3740
+ document.getElementById('frm-deny').onclick = close;
3741
+ document.getElementById('frm-accept').onclick = async () => {
3742
+ close();
3743
+ try {
3744
+ const res = await fetch('/api/friend-accept', {
3745
+ method: 'POST',
3746
+ headers: { 'Content-Type': 'application/json' },
3747
+ body: JSON.stringify({ fromPublicKey: req.fromPublicKey, name: req.fromName })
3748
+ });
3749
+ const data = await res.json();
3750
+ if (!res.ok) throw new Error(data.error || 'accept failed');
3751
+ console.log('[v3-friend] 接受了好友申请:', req.fromName);
3752
+ // 立刻拉一次 — 对方刚 accept, ta 的 channel 列表会被推到我们这
3753
+ setTimeout(loadRemoteChannels, 1000);
3754
+ showSimpleToast(`✅ 已接受 ${req.fromName} 的好友申请`);
3755
+ } catch (err) {
3756
+ console.error('[v3-friend] accept 失败:', err);
3757
+ alert('接受失败: ' + (err.message || err));
3758
+ }
3759
+ };
3760
+ }
3761
+
3762
+ /**
3763
+ * 2026-06-10: 简单的右下 toast, 3s 自动消失. 用于 ack / 接受好友 等非阻塞反馈
3764
+ */
3765
+ function showSimpleToast(text, kind = 'info') {
3766
+ const containerId = 'simple-toast-container';
3767
+ let container = document.getElementById(containerId);
3768
+ if (!container) {
3769
+ container = document.createElement('div');
3770
+ container.id = containerId;
3771
+ container.style.cssText = 'position:fixed;right:16px;bottom:16px;z-index:10005;display:flex;flex-direction:column;gap:8px;max-width:320px;';
3772
+ document.body.appendChild(container);
3773
+ }
3774
+ const el = document.createElement('div');
3775
+ el.className = `simple-toast simple-toast-${kind}`;
3776
+ el.style.cssText = `background:var(--bg-sidebar);color:var(--text);border:1px solid var(--border);padding:10px 14px;border-radius:6px;font-size:12px;box-shadow:0 4px 16px rgba(0,0,0,0.3);font-family:inherit;animation:toast-in .2s ease-out;`;
3777
+ el.textContent = text;
3778
+ container.appendChild(el);
3779
+ setTimeout(() => {
3780
+ el.style.transition = 'opacity .3s, transform .3s';
3781
+ el.style.opacity = '0';
3782
+ el.style.transform = 'translateX(20px)';
3783
+ setTimeout(() => el.remove(), 320);
3784
+ }, 3000);
3785
+ }
3786
+
3787
+ // 2026-06-10: P2P 全部展开/折叠切换按钮 (单按钮, 根据当前多数态切换)
3788
+ const p2pToggleAllBtn = document.getElementById('p2p-toggle-all-btn');
3789
+ if (p2pToggleAllBtn) {
3790
+ // 同步图标/文字: 多数 peer 折叠 → 显示 "⊞ 展开"; 多数展开 → 显示 "⊟ 折叠"
3791
+ function syncToggleAllBtn() {
3792
+ const allPks = new Set([
3793
+ ...knownPeers.map(p => p.publicKey),
3794
+ ...remoteChannels.map(g => g.peerId)
3795
+ ]);
3796
+ if (allPks.size === 0) {
3797
+ p2pToggleAllBtn.textContent = '⊞ 展开';
3798
+ p2pToggleAllBtn.title = '切换全部展开/折叠';
3799
+ return;
3800
+ }
3801
+ let collapsedCount = 0;
3802
+ for (const pk of allPks) if (collapsedPeers.has(pk)) collapsedCount++;
3803
+ const majorityCollapsed = collapsedCount >= allPks.size / 2;
3804
+ if (majorityCollapsed) {
3805
+ p2pToggleAllBtn.textContent = '⊞ 展开';
3806
+ p2pToggleAllBtn.title = '点击展开所有 P2P 好友';
3807
+ } else {
3808
+ p2pToggleAllBtn.textContent = '⊟ 折叠';
3809
+ p2pToggleAllBtn.title = '点击折叠所有 P2P 好友';
3810
+ }
3811
+ }
3812
+ p2pToggleAllBtn.addEventListener('click', (e) => {
3813
+ e.stopPropagation();
3814
+ const allPks = new Set([
3815
+ ...knownPeers.map(p => p.publicKey),
3816
+ ...remoteChannels.map(g => g.peerId)
3817
+ ]);
3818
+ if (allPks.size === 0) return;
3819
+ // 多数折叠 → 全展开; 否则全折叠
3820
+ let collapsedCount = 0;
3821
+ for (const pk of allPks) if (collapsedPeers.has(pk)) collapsedCount++;
3822
+ const majorityCollapsed = collapsedCount >= allPks.size / 2;
3823
+ if (majorityCollapsed) {
3824
+ expandAllPeers();
3825
+ } else {
3826
+ collapseAllPeers();
3320
3827
  }
3828
+ syncToggleAllBtn();
3321
3829
  });
3830
+ // 暴露给 renderRemoteChannels 渲染后调用 (保持图标跟实际状态一致)
3831
+ window.__syncP2PToggleAllBtn = syncToggleAllBtn;
3832
+ syncToggleAllBtn(); // 首次同步
3322
3833
  }
3323
3834
 
3324
3835
  // v3 双向刷新: 主动向所有好友发 agent.meta.list, 拿到 ta 们分享给我的 channel
@@ -3349,6 +3860,8 @@ if (refreshSharedBtn) {
3349
3860
  // 启动时拉一次 + 定期轮询 (SSE 接收 P2P reply 后也会更新)
3350
3861
  loadRemoteChannels();
3351
3862
  setInterval(loadRemoteChannels, 8000);
3863
+ // 全局 SSE — 接收 remote-channel-update / remote-chat-reply / friend-request
3864
+ startV3GlobalSSE();
3352
3865
 
3353
3866
  // ============ v3: 折叠 + 拖拽分隔线 ============
3354
3867