|
660 | 660 | // SSE |
661 | 661 | // ============================================================================================== |
662 | 662 | function startSSE() { |
663 | | - const es = new EventSource('/events'); |
664 | | - es.onopen = () => setConnected(true); |
665 | | - es.onerror = () => { setConnected(false); setTimeout(startSSE, 3000); es.close(); }; |
666 | | - |
667 | | - es.onmessage = async (e) => { |
668 | | - const ev = JSON.parse(e.data); |
669 | | - const p = ev.payload; |
670 | | - |
671 | | - if (ev.type === 'msg.new') { |
672 | | - if (p.thread_id === activeThreadId) await loadNewMessages(); |
673 | | - await refreshThreads(); // update sidebar counts |
674 | | - } |
675 | | - if (ev.type === 'msg.edit' && p.thread_id === activeThreadId) { |
676 | | - // Update the bubble content in-place and show/refresh the "edited" indicator |
677 | | - const row = document.querySelector(`[data-msg-id="${p.msg_id}"]`); |
678 | | - if (row) { |
679 | | - const bubbleEl = row.querySelector('.bubble-v2'); |
680 | | - if (bubbleEl) { |
681 | | - bubbleEl.innerHTML = ''; |
682 | | - window.AcbMessageRenderer.renderMessageContent(bubbleEl, p.content, null); |
683 | | - } |
684 | | - let indicator = row.querySelector('.msg-edited-indicator'); |
685 | | - if (!indicator) { |
686 | | - indicator = document.createElement('span'); |
687 | | - indicator.className = 'msg-edited-indicator'; |
688 | | - indicator.setAttribute('onclick', 'window.AcbMsgEditHistory(this)'); |
689 | | - indicator.setAttribute('data-msg-id', p.msg_id); |
690 | | - const tail = row.querySelector('acb-message-tail-meta') || row.querySelector('.msg-reactions'); |
691 | | - if (tail) tail.before(indicator); |
692 | | - } |
693 | | - indicator.title = `Edited (v${p.version})`; |
694 | | - indicator.textContent = 'edited'; |
695 | | - } |
696 | | - } |
697 | | - if (ev.type === 'thread.new' || ev.type === 'thread.state' || ev.type === 'thread.closed' || ev.type === 'thread.archived' || ev.type === 'thread.deleted') { |
698 | | - await refreshThreads(); |
699 | | - if (ev.type === 'thread.deleted' && p.thread_id === activeThreadId) { |
700 | | - activeThreadId = null; |
701 | | - document.getElementById('msg-pane').innerHTML = ''; |
702 | | - const hdr = document.getElementById('thread-header'); |
703 | | - if (hdr) hdr.textContent = ''; |
704 | | - } |
705 | | - } |
706 | | - if (ev.type === 'agent.online' || ev.type === 'agent.offline') { |
707 | | - await refreshAgents(); |
708 | | - } |
709 | | - if (ev.type === 'agent.typing' && p.thread_id === activeThreadId) { |
710 | | - if (p.is_typing) showTyping(p.agent_id); |
711 | | - else hideTyping(p.agent_id); |
712 | | - } |
713 | | - }; |
714 | 663 | return window.AcbSSE.startSSE({ |
715 | 664 | getActiveThreadId: () => activeThreadId, |
716 | 665 | onMsgNew: async () => loadNewMessages(), |
|
0 commit comments