Skip to content

Commit 69c07f6

Browse files
committed
Revert "perf: throttle markdown lexing during streaming and reduce redundant re-renders"
This reverts commit 40e8a21.
1 parent 40e8a21 commit 69c07f6

3 files changed

Lines changed: 32 additions & 67 deletions

File tree

src/lib/components/chat/Messages/ContentRenderer.svelte

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,6 @@
4040
export let onTaskClick = (e) => {};
4141
export let onAddMessages = (e) => {};
4242
43-
// Memoize sourceIds to avoid creating a new array reference on every render,
44-
// which would cause the entire Markdown/MarkdownTokens tree to re-render
45-
$: computedSourceIds = (sources ?? []).reduce((acc, source) => {
46-
let ids = [];
47-
source.document.forEach((document, index) => {
48-
if (model?.info?.meta?.capabilities?.citations == false) {
49-
ids.push('N/A');
50-
return ids;
51-
}
52-
53-
const metadata = source.metadata?.[index];
54-
const id = metadata?.source ?? 'N/A';
55-
56-
if (metadata?.name) {
57-
ids.push(metadata.name);
58-
return ids;
59-
}
60-
61-
if (id.startsWith('http://') || id.startsWith('https://')) {
62-
ids.push(id);
63-
} else {
64-
ids.push(source?.source?.name ?? id);
65-
}
66-
67-
return ids;
68-
});
69-
70-
acc = [...acc, ...ids];
71-
72-
// remove duplicates
73-
return acc.filter((item, index) => acc.indexOf(item) === index);
74-
}, []);
75-
7643
let contentContainerElement;
7744
let floatingButtonsElement;
7845
@@ -176,7 +143,36 @@
176143
{done}
177144
{editCodeBlock}
178145
{topPadding}
179-
sourceIds={computedSourceIds}
146+
sourceIds={(sources ?? []).reduce((acc, source) => {
147+
let ids = [];
148+
source.document.forEach((document, index) => {
149+
if (model?.info?.meta?.capabilities?.citations == false) {
150+
ids.push('N/A');
151+
return ids;
152+
}
153+
154+
const metadata = source.metadata?.[index];
155+
const id = metadata?.source ?? 'N/A';
156+
157+
if (metadata?.name) {
158+
ids.push(metadata.name);
159+
return ids;
160+
}
161+
162+
if (id.startsWith('http://') || id.startsWith('https://')) {
163+
ids.push(id);
164+
} else {
165+
ids.push(source?.source?.name ?? id);
166+
}
167+
168+
return ids;
169+
});
170+
171+
acc = [...acc, ...ids];
172+
173+
// remove duplicates
174+
return acc.filter((item, index) => acc.indexOf(item) === index);
175+
}, [])}
180176
{onSourceClick}
181177
{onTaskClick}
182178
{onSave}

src/lib/components/chat/Messages/Markdown.svelte

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
</script>
2525

2626
<script>
27-
import { onDestroy } from 'svelte';
2827
import { replaceTokens, processResponseContent } from '$lib/utils';
2928
import { user } from '$lib/stores';
3029
@@ -53,40 +52,11 @@
5352
5453
let tokens = [];
5554
56-
const THROTTLE_MS = 100;
57-
let throttleTimer = null;
58-
let lastLexTime = 0;
59-
60-
function lexContent(content) {
55+
$: if (content) {
6156
tokens = marked.lexer(
6257
replaceTokens(processResponseContent(content), model?.name, $user?.name)
6358
);
64-
lastLexTime = Date.now();
6559
}
66-
67-
$: if (content) {
68-
if (done) {
69-
// When streaming is complete, always lex immediately
70-
clearTimeout(throttleTimer);
71-
throttleTimer = null;
72-
lexContent(content);
73-
} else {
74-
// During streaming, throttle to reduce processing
75-
const elapsed = Date.now() - lastLexTime;
76-
if (elapsed >= THROTTLE_MS) {
77-
lexContent(content);
78-
} else if (!throttleTimer) {
79-
throttleTimer = setTimeout(() => {
80-
throttleTimer = null;
81-
lexContent(content);
82-
}, THROTTLE_MS - elapsed);
83-
}
84-
}
85-
}
86-
87-
onDestroy(() => {
88-
clearTimeout(throttleTimer);
89-
});
9060
</script>
9161
9262
{#key id}

src/lib/components/chat/Messages/Markdown/MarkdownTokens.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@
325325
{@const textContent = decode(token.text || '')
326326
.replace(/<summary>.*?<\/summary>/gi, '')
327327
.trim()}
328-
{@const detailsTokens = textContent.length > 0 ? marked.lexer(decode(token.text)) : []}
329328

330329
{#if token?.attributes?.type === 'tool_calls'}
331330
<!-- Tool calls have dedicated handling with ToolCallDisplay component -->
@@ -346,7 +345,7 @@
346345
<div class=" mb-1.5" slot="content">
347346
<svelte:self
348347
id={`${id}-${tokenIdx}-d`}
349-
tokens={detailsTokens}
348+
tokens={marked.lexer(decode(token.text))}
350349
attributes={token?.attributes}
351350
{done}
352351
{editCodeBlock}

0 commit comments

Comments
 (0)