Merge pull request #81 from nesquena/feat/raf-streaming-throttle
perf: rAF-throttled token streaming for smoother rendering
This commit is contained in:
@@ -103,14 +103,25 @@ async function send(){
|
|||||||
// ── Shared SSE handler wiring (used for initial connection and reconnect) ──
|
// ── Shared SSE handler wiring (used for initial connection and reconnect) ──
|
||||||
let _reconnectAttempted=false;
|
let _reconnectAttempted=false;
|
||||||
|
|
||||||
|
// rAF-throttled rendering: buffer tokens, render at most once per frame
|
||||||
|
let _renderPending=false;
|
||||||
|
function _scheduleRender(){
|
||||||
|
if(_renderPending) return;
|
||||||
|
_renderPending=true;
|
||||||
|
requestAnimationFrame(()=>{
|
||||||
|
_renderPending=false;
|
||||||
|
if(assistantBody) assistantBody.innerHTML=renderMd(assistantText);
|
||||||
|
scrollIfPinned();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function _wireSSE(source){
|
function _wireSSE(source){
|
||||||
source.addEventListener('token',e=>{
|
source.addEventListener('token',e=>{
|
||||||
if(!S.session||S.session.session_id!==activeSid) return;
|
if(!S.session||S.session.session_id!==activeSid) return;
|
||||||
const d=JSON.parse(e.data);
|
const d=JSON.parse(e.data);
|
||||||
assistantText+=d.text;
|
assistantText+=d.text;
|
||||||
ensureAssistantRow();
|
ensureAssistantRow();
|
||||||
assistantBody.innerHTML=renderMd(assistantText);
|
_scheduleRender();
|
||||||
scrollIfPinned();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
source.addEventListener('tool',e=>{
|
source.addEventListener('tool',e=>{
|
||||||
|
|||||||
Reference in New Issue
Block a user