Skip to content

Commit 50ebc95

Browse files
committed
fix(web): preserve text in image-only thought previews
1 parent be5591d commit 50ebc95

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.test.tsx‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,16 +1656,37 @@ describe("MessagesTimeline", () => {
16561656
expect(markup).not.toContain("tool call failed");
16571657
});
16581658

1659-
it.each([false, true])(
1660-
"shows plain Markdown previews for thoughts, streaming=%s",
1661-
async (streaming) => {
1659+
it.each(
1660+
(
1661+
[
1662+
[
1663+
"**Viewing image first** with *care*, ~~old~~ `code` and [context](https://example.com)",
1664+
"Viewing image first with care, old code and context",
1665+
1,
1666+
],
1667+
["first paragraph\n\nsecond paragraph", "first paragraph second paragraph", 0],
1668+
["- first\n- second", "first second", 0],
1669+
["first \nsecond", "first second", 0],
1670+
["![image description](image.png)", "image description", 0],
1671+
["![](image.png)", "Thought", 0],
1672+
["---", "Thought", 0],
1673+
] as const
1674+
).flatMap(([markdown, expected, strongCount]) =>
1675+
[false, true].map((streaming) => ({
1676+
markdown,
1677+
expected,
1678+
strongCount,
1679+
streaming,
1680+
})),
1681+
),
1682+
)(
1683+
"shows a plain thought preview for $markdown, streaming=$streaming",
1684+
async ({ markdown, expected, strongCount, streaming }) => {
16621685
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
16631686
vi.stubGlobal("requestAnimationFrame", () => 0);
16641687
vi.stubGlobal("cancelAnimationFrame", () => {});
16651688
const turnId = TurnId.make("turn-thought");
1666-
const thought = buildAssistantTimelineEntry(
1667-
"**Viewing image first** with *care*, ~~old~~ `code` and [context](https://example.com)",
1668-
);
1689+
const thought = buildAssistantTimelineEntry(markdown);
16691690
let renderer: ReactTestRenderer | undefined;
16701691
try {
16711692
await act(() => {
@@ -1709,14 +1730,16 @@ describe("MessagesTimeline", () => {
17091730
.flatMap((node) => node.children)
17101731
.filter((child) => typeof child === "string")
17111732
.join(""),
1712-
).toBe("Viewing image first with care, old code and context".repeat(streaming ? 2 : 1));
1733+
).toBe(
1734+
(streaming && expected === "Thought" ? "Thinking" : expected).repeat(streaming ? 2 : 1),
1735+
);
17131736
expect(
17141737
preview.findAll((node) =>
17151738
["strong", "em", "del", "code", "a"].includes(String(node.type)),
17161739
),
17171740
).toHaveLength(0);
17181741
await act(() => preview.props.onClick());
1719-
expect(renderer!.root.findAllByType("strong")).toHaveLength(1);
1742+
expect(renderer!.root.findAllByType("strong")).toHaveLength(strongCount);
17201743
await act(() => preview.props.onClick());
17211744
expect(renderer!.root.findAllByType("strong")).toHaveLength(0);
17221745
} finally {

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import { PREFERRED_HIGHLIGHTER } from "../../lib/syntaxHighlighting";
105105
import ChatMarkdown, { ChatMarkdownAssetImage } from "../ChatMarkdown";
106106
import ReactMarkdown from "react-markdown";
107107
import remarkGfm from "remark-gfm";
108+
import type { Root, RootContent } from "mdast";
108109
import { T3Wordmark } from "../T3Wordmark";
109110
import {
110111
BotIcon,
@@ -2691,6 +2692,28 @@ function ThinkingTimelineRow() {
26912692
);
26922693
}
26932694

2695+
function remarkThoughtPreview(fallback: string) {
2696+
return (tree: Root) => {
2697+
const plainText = (node: Root | RootContent): string => {
2698+
if (node.type === "html" || node.type === "definition") return "";
2699+
if ("alt" in node) return node.alt ?? "";
2700+
if ("value" in node) return node.value;
2701+
if ("children" in node) {
2702+
const separator = ["root", "blockquote", "list", "listItem", "table", "tableRow"].includes(
2703+
node.type,
2704+
)
2705+
? " "
2706+
: "";
2707+
return node.children.map(plainText).join(separator);
2708+
}
2709+
return node.type === "break" ? " " : "";
2710+
};
2711+
tree.children = [
2712+
{ type: "text", value: plainText(tree).replace(/\s+/g, " ").trim() || fallback },
2713+
];
2714+
};
2715+
}
2716+
26942717
/**
26952718
* Thinking inside a tool group has its own disclosure, preserved across recycling.
26962719
* A group whose row already reads "Thought" (no visible tool) skips the header.
@@ -2727,7 +2750,7 @@ function ReasoningTraceBlock({
27272750
const headerText = expanded ? (
27282751
label
27292752
) : (
2730-
<ReactMarkdown allowedElements={[]} unwrapDisallowed skipHtml remarkPlugins={[remarkGfm]}>
2753+
<ReactMarkdown remarkPlugins={[remarkGfm, [remarkThoughtPreview, label]]}>
27312754
{collapsedPreview ?? label}
27322755
</ReactMarkdown>
27332756
);

0 commit comments

Comments
 (0)