File tree Expand file tree Collapse file tree
src/lib/components/chat/Messages Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11<script >
2+ import { onDestroy } from ' svelte' ;
23 import { marked } from ' marked' ;
34 import { replaceTokens , processResponseContent } from ' $lib/utils' ;
45 import { user } from ' $lib/stores' ;
5354 ]
5455 });
5556
56- $: (async () => {
57+ const LEXER_THROTTLE_MS = 50 ;
58+ let lexerThrottleTimer;
59+
60+ function updateTokens () {
5761 if (content) {
5862 tokens = marked .lexer (
5963 replaceTokens (processResponseContent (content), model? .name , $user? .name )
6064 );
6165 }
62- })();
66+ }
67+
68+ $: if (content) {
69+ if (done) {
70+ // Final content, render now
71+ clearTimeout (lexerThrottleTimer);
72+ updateTokens ();
73+ } else {
74+ // Throttle during streaming
75+ if (! lexerThrottleTimer) {
76+ updateTokens ();
77+ }
78+ clearTimeout (lexerThrottleTimer);
79+ lexerThrottleTimer = setTimeout (() => {
80+ lexerThrottleTimer = null ;
81+ updateTokens ();
82+ }, LEXER_THROTTLE_MS );
83+ }
84+ }
85+
86+ onDestroy (() => {
87+ clearTimeout (lexerThrottleTimer);
88+ });
6389< / script>
6490
6591{#key id}
You can’t perform that action at this time.
0 commit comments