|
381 | 381 | saveChatHandler(_chatId, history); |
382 | 382 | }; |
383 | 383 |
|
| 384 | + // Batch reactive history updates to animation frames during streaming. |
| 385 | + // Data is accumulated on every socket event (cheap mutation), but the |
| 386 | + // Svelte reactivity trigger fires at most once per frame. |
| 387 | + let _pendingFlushFrame = null; |
| 388 | + let _pendingFlushIds = new Set(); |
| 389 | +
|
| 390 | + const scheduleHistoryFlush = (messageId) => { |
| 391 | + _pendingFlushIds.add(messageId); |
| 392 | + if (!_pendingFlushFrame) { |
| 393 | + _pendingFlushFrame = requestAnimationFrame(() => { |
| 394 | + for (const id of _pendingFlushIds) { |
| 395 | + history.messages[id] = history.messages[id]; |
| 396 | + } |
| 397 | + _pendingFlushIds.clear(); |
| 398 | + _pendingFlushFrame = null; |
| 399 | +
|
| 400 | + if (autoScroll) { |
| 401 | + scrollToBottom(); |
| 402 | + } |
| 403 | + }); |
| 404 | + } |
| 405 | + }; |
| 406 | +
|
| 407 | + const flushHistoryNow = () => { |
| 408 | + if (_pendingFlushFrame) { |
| 409 | + cancelAnimationFrame(_pendingFlushFrame); |
| 410 | + _pendingFlushFrame = null; |
| 411 | + } |
| 412 | + for (const id of _pendingFlushIds) { |
| 413 | + history.messages[id] = history.messages[id]; |
| 414 | + } |
| 415 | + _pendingFlushIds.clear(); |
| 416 | + }; |
| 417 | +
|
384 | 418 | const chatEventHandler = async (event, cb) => { |
385 | 419 | console.log(event); |
386 | 420 |
|
|
508 | 542 | console.log('Unknown message type', data); |
509 | 543 | } |
510 | 544 |
|
511 | | - history.messages[event.message_id] = message; |
| 545 | + scheduleHistoryFlush(event.message_id); |
512 | 546 | } |
513 | 547 | } |
514 | 548 | }; |
|
1546 | 1580 | message.usage = usage; |
1547 | 1581 | } |
1548 | 1582 |
|
1549 | | - history.messages[message.id] = message; |
1550 | | -
|
1551 | 1583 | if (done) { |
1552 | 1584 | message.done = true; |
1553 | 1585 |
|
|
1582 | 1614 | }) |
1583 | 1615 | ); |
1584 | 1616 |
|
| 1617 | + flushHistoryNow(); |
1585 | 1618 | history.messages[message.id] = message; |
1586 | 1619 |
|
1587 | 1620 | await tick(); |
|
0 commit comments