@devries-cameron20 / t3code-1 / commits / 2dc07510e2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): long reasoning previews format in linear time (600 ms → 1.3 ms median) (#30038)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Andrei committed Nov 9, 2026 at 22:33 UTC 2dc07510e211573f1ca6a1385c5e09c3476bd504
2 files changed +81 -18
apps/web/src/components/chat/MessagesTimeline.browser.test.tsx
+72 -14
@@ -1263,21 +1263,79 @@ describe("MessagesTimeline thread find", () => {
1263 });
1264
1265 describe("compact reasoning math", () => {
1266 - it("keeps formulas while flattening compact reasoning previews", () => {
1267 - const tree = parseMarkdownWithMath("## Reasoning\n\nUse **norm** $|x|$ and \\[x^2\\]");
1266 + it.each([
1267 + [
1268 + "## Reasoning\n\nUse **norm** $|x|$ and \\[x^2\\]",
1269 + [
1270 + ["text", "Reasoning Use norm "],
1271 + ["inlineMath", "|x|"],
1272 + ["text", " and "],
1273 + ["inlineMath", "x^2"],
1274 + ],
1275 + ],
1276 + [
1277 + " Lead **bold**\n\n *em* `code` \n\n- one\n- two\n\n> quote\n> more\n\ntrail ",
1278 + [["text", "Lead bold em code one two quote more trail"]],
1279 + ],
1280 + [
1281 + "$a$ starts, ends with $b$",
1282 + [
1283 + ["inlineMath", "a"],
1284 + ["text", " starts, ends with "],
1285 + ["inlineMath", "b"],
1286 + ],
1287 + ],
1288 + [
1289 + "before $x$\n\n$$\ny\n$$\n\n after",
1290 + [
1291 + ["text", "before "],
1292 + ["inlineMath", "x"],
1293 + ["text", " "],
1294 + ["inlineMath", "y"],
1295 + ["text", " after"],
1296 + ],
1297 + ],
1298 + [
1299 + "a *b*$c$**d** e",
1300 + [
1301 + ["text", "a b"],
1302 + ["inlineMath", "c"],
1303 + ["text", "d e"],
1304 + ],
1305 + ],
1306 + [
1307 + "$a$![](x.png)$b$",
1308 + [
1309 + ["inlineMath", "a"],
1310 + ["text", ""],
1311 + ["inlineMath", "b"],
1312 + ],
1313 + ],
1314 + [
1315 + "| a | b |\n| - | - |\n| $x$ | y z |",
1316 + [
1317 + ["text", "a b "],
1318 + ["inlineMath", "x"],
1319 + ["text", " y z"],
1320 + ],
1321 + ],
1322 + [
1323 + 'See [a link](https://example.com "t") ![alt text](img.png) <b>bold</b> <!-- c -->\n\n[ref]: https://example.com\n\n:::note\nbody\n:::',
1324 + [["text", "See a link alt text bold :::note body :::"]],
1325 + ],
1326 + [
1327 + "line one\r\nline two\r\n\r\nnext  nbsp wide 😀𝑥
end",
1328 + [["text", "line one line two next nbsp wide 😀𝑥 end"]],
1329 + ],
1330 + ["\\$5 and \\$6, \\*not em\\*", [["text", "$5 and $6, *not em*"]]],
1331 + ["hard \nbreak\\\nagain", [["text", "hard break again"]]],
1332 + ["<div>only html</div>", [["text", "Reasoning"]]],
1333 + ])("keeps formulas while flattening compact reasoning previews: %j", (source, expected) => {
1334 + const tree = parseMarkdownWithMath(source);
1335 remarkThoughtPreview("Reasoning")(tree);
1269 - expect(tree.children.map((node) => node.type)).toEqual([
1270 - "text",
1271 - "inlineMath",
1272 - "text",
1273 - "inlineMath",
1274 - ]);
1275 - expect(tree.children.map((node) => ("value" in node ? node.value : null))).toEqual([
1276 - "Reasoning Use norm ",
1277 - "|x|",
1278 - " and ",
1279 - "x^2",
1280 - ]);
1336 + expect(tree.children.map((node) => [node.type, "value" in node ? node.value : null])).toEqual(
1337 + expected,
1338 + );
1339 });
1340 });
1341
apps/web/src/components/chat/TimelineWorkEntryRow.logic.ts
+9 -4
@@ -196,15 +196,19 @@ export function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconNa
196 export function remarkThoughtPreview(fallback: string) {
197 return (tree: Root) => {
198 const children: RootContent[] = [];
199 + let run: string[] = [];
200 const append = (value: string) => {
200 - const last = children.at(-1);
201 - const text = value.replace(/\s+/g, " ");
202 - if (last?.type === "text") last.value = (last.value + text).replace(/\s+/g, " ");
203 - else children.push({ type: "text", value: text });
201 + run.push(value);
202 + };
203 + const flush = () => {
204 + if (run.length === 0) return;
205 + children.push({ type: "text", value: run.join("").replace(/\s+/g, " ") });
206 + run = [];
207 };
208 const visit = (node: Root | RootContent) => {
209 if (node.type === "html" || node.type === "definition") return;
210 if (node.type === "math" || node.type === "inlineMath") {
211 + flush();
212 children.push({
213 type: "inlineMath",
214 value: node.value,
@@ -229,6 +233,7 @@ export function remarkThoughtPreview(fallback: string) {
233 } else if (node.type === "break") append(" ");
234 };
235 visit(tree);
236 + flush();
237 const first = children[0];
238 const last = children.at(-1);
239 if (first?.type === "text") first.value = first.value.trimStart();