From eeca3e95fb555a1112c556c74206e552540d5038 Mon Sep 17 00:00:00 2001 From: kim Date: Thu, 8 Oct 2026 15:22:36 +0900 Subject: [PATCH] =?UTF-8?q?fix(katex-inline):=20=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EB=B0=8D=20=EC=9D=91=EB=8B=B5=EC=9D=98=20=EC=88=98?= =?UTF-8?q?=EC=8B=9D($\rightarrow$=20=EB=93=B1)=EC=9D=B4=20=EB=82=A0?= =?UTF-8?q?=EA=B2=83=EC=9C=BC=EB=A1=9C=20=EB=82=A8=EB=8D=98=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=E2=80=94=20#chat-msgs=20=EB=B3=80=ED=99=94?= =?UTF-8?q?=EB=A5=BC=20=EA=B0=90=EC=8B=9C=ED=95=B4=20350ms=20=EB=A9=88?= =?UTF-8?q?=EC=B6=94=EB=A9=B4=20=EB=A7=90=ED=92=8D=EC=84=A0=20=EB=A0=8C?= =?UTF-8?q?=EB=8D=94=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- web-ui/js/app/katex-inline.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/web-ui/js/app/katex-inline.js b/web-ui/js/app/katex-inline.js index 1a28a6f..8d5e2fe 100644 --- a/web-ui/js/app/katex-inline.js +++ b/web-ui/js/app/katex-inline.js @@ -82,4 +82,39 @@ } catch (e2) {} } }; + + // 스트리밍 응답 자동 렌더링: 앱들은 토큰이 올 때마다 bubble.innerHTML을 통째로 덮어쓰는데, + // renderKatexIn은 말풍선을 처음 만들 때(빈 상태) 한 번만 불려서 실시간 답변의 `$\rightarrow$`가 + // 날것으로 남았다(대화 기록 복원은 addMsg가 내용을 채워 만들어서 정상). 앱마다 스트림 루프를 + // 고치는 대신 채팅 영역의 DOM 변화를 지켜보다가, 변화가 잠시(350ms) 멈추면 그 말풍선을 렌더링한다. + // 스트리밍 중에는 매 토큰이 덮어쓰므로 중간 렌더링은 자연히 버려지고, 마지막 상태만 남는다. + function watchChat() { + var root = document.getElementById('chat-msgs'); + if (!root || !window.MutationObserver) return; + var pending = new Set(), timer = null; + var mo = new MutationObserver(function (muts) { + muts.forEach(function (m) { + var el = m.target.nodeType === 1 ? m.target : m.target.parentElement; + var b = el && el.closest && el.closest('[class*="-bubble"]'); + if (b && root.contains(b) && !b.closest('.user, .system')) pending.add(b); + }); + if (!pending.size) return; + clearTimeout(timer); + timer = setTimeout(flush, 350); + }); + function flush() { + var list = Array.from(pending); pending.clear(); + mo.disconnect(); // KaTeX가 DOM을 바꾸므로 자기 변경에 다시 반응하지 않게 끊고 렌더링한다. + try { + list.forEach(function (b) { + if (/\$|\\\(|\\\[/.test(b.textContent)) window.renderKatexIn(b); + }); + } finally { + mo.observe(root, { childList: true, subtree: true, characterData: true }); + } + } + mo.observe(root, { childList: true, subtree: true, characterData: true }); + } + if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', watchChat); + else watchChat(); })();