TypeScript
fix(web): render assistant images inline in chat (#9126)
Theo Browne652515a34974
diff --git a/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx b/apps/web/src/components/ChatMarkdown.workspace-images.test.tsxindex 172793bacb0..39be0eedafe 100644--- a/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx+++ b/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx@@ -145,6 +145,25 @@ describe("ChatMarkdown workspace images", () => {expect(html).not.toContain("Image unavailable");});+ it("loads a POSIX absolute path and file URI through a signed asset URL", () => {+ const html = renderToStaticMarkup(+ <ChatMarkdown+ cwd="/workspace/project"+ threadRef={threadRef}+ text={[+ "",+ "",+ ].join("\n\n")}+ />,+ );++ expect(testState.resources).toEqual([+ { _tag: "media-file", threadId: threadRef.threadId, path: "/tmp/embed-test/2.png" },+ { _tag: "media-file", threadId: threadRef.threadId, path: "/tmp/embed-test/5.png" },+ ]);+ expect(html).not.toContain("Image unavailable");+ });+it("normalizes a drive-absolute src in raw image HTML", () => {const html = render(String.raw`<img src="D:\screens\workspace-image.svg" alt="raw">`);diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.tsindex a198c3edc0d..e19c2fdb192 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts@@ -12,6 +12,7 @@ import {shouldPreserveAssistantLineBreaks,type MessagesTimelineRow,workEntryDisplayLabel,+ workEntryIsVisibleInGroup,} from "./MessagesTimeline.logic";describe("expanded tool group scrolling", () => {@@ -471,6 +472,22 @@ describe("resolveAssistantMessageCopyState", () => {});});+describe("workEntryIsVisibleInGroup", () => {+ it("keeps an in-progress image preview row visible outside a group", () => {+ const entry = {+ id: "work-image",+ createdAt: "2026-01-01T00:00:04Z",+ turnId: "turn-1" as never,+ label: "Image view",+ detail: "screenshots/result.png",+ itemType: "image_view" as const,+ tone: "tool" as const,+ toolLifecycleStatus: "inProgress" as const,+ };+ expect(workEntryIsVisibleInGroup(entry, false)).toBe(true);+ });+});+describe("deriveMessagesTimelineRows", () => {it("only enables assistant copy for the terminal assistant message in a turn", () => {const rows = deriveMessagesTimelineRows({@@ -834,6 +851,125 @@ describe("deriveMessagesTimelineRows", () => {});});+ it("keeps an image preview row visible after the turn settles", () => {+ const timelineEntries = [+ {+ id: "work-command-entry",+ kind: "work" as const,+ createdAt: "2026-01-01T00:00:02Z",+ entry: {+ id: "work-command",+ createdAt: "2026-01-01T00:00:02Z",+ turnId: "turn-1" as never,+ label: "Ran command",+ tone: "tool" as const,+ },+ },+ {+ id: "work-image-entry",+ kind: "work" as const,+ createdAt: "2026-01-01T00:00:04Z",+ entry: {+ id: "work-image",+ createdAt: "2026-01-01T00:00:04Z",+ turnId: "turn-1" as never,+ label: "Image view",+ detail: "screenshots/result.png",+ itemType: "image_view" as const,+ tone: "tool" as const,+ },+ },+ {+ id: "assistant-final-entry",+ kind: "message" as const,+ createdAt: "2026-01-01T00:00:06Z",+ message: {+ id: "assistant-final" as never,+ role: "assistant" as const,+ text: "Here is the screenshot.",+ turnId: "turn-1" as never,+ createdAt: "2026-01-01T00:00:06Z",+ updatedAt: "2026-01-01T00:00:07Z",+ streaming: false,+ },+ },+ ];++ const rows = deriveMessagesTimelineRows({+ timelineEntries,+ isWorking: false,+ activeTurnStartedAt: null,+ turnDiffSummaryByAssistantMessageId: new Map(),+ revertTurnCountByUserMessageId: new Map(),+ });++ // The command row folds, the image row stays as its own visible row rather+ // than collapsing into a tool group summary.+ expect(rows.map((row) => row.id)).toEqual([+ "turn-fold:turn-1",+ "work-image-entry",+ "assistant-final-entry",+ ]);+ expect(rows.find((row) => row.id === "work-image-entry")?.kind).toBe("work");+ });++ it("keeps a trailing image preview row visible while the turn is still working", () => {+ const rows = deriveMessagesTimelineRows({+ timelineEntries: [+ {+ id: "user-entry",+ kind: "message" as const,+ createdAt: "2026-01-01T00:00:00Z",+ message: {+ id: "user-1" as never,+ role: "user" as const,+ text: "show me the result",+ turnId: null,+ createdAt: "2026-01-01T00:00:00Z",+ updatedAt: "2026-01-01T00:00:00Z",+ streaming: false,+ },+ },+ {+ id: "work-command-entry",+ kind: "work" as const,+ createdAt: "2026-01-01T00:00:02Z",+ entry: {+ id: "work-command",+ createdAt: "2026-01-01T00:00:02Z",+ turnId: "turn-1" as never,+ label: "Ran command",+ tone: "tool" as const,+ },+ },+ {+ id: "work-image-entry",+ kind: "work" as const,+ createdAt: "2026-01-01T00:00:04Z",+ entry: {+ id: "work-image",+ createdAt: "2026-01-01T00:00:04Z",+ turnId: "turn-1" as never,+ label: "Image view",+ detail: "screenshots/result.png",+ itemType: "image_view" as const,+ tone: "tool" as const,+ },+ },+ ],+ isWorking: true,+ activeTurnStartedAt: "2026-01-01T00:00:01Z",+ turnDiffSummaryByAssistantMessageId: new Map(),+ revertTurnCountByUserMessageId: new Map(),+ });++ // The live activity row must not swallow the image: it renders only a+ // label, which would hide the image until the turn settles.+ const imageRow = rows.find((row) => row.id === "work-image-entry");+ expect(imageRow?.kind).toBe("work");+ expect(rows.some((row) => row.kind === "work-live")).toBe(false);+ });+it("folds all assistant messages before the terminal message", () => {const timelineEntries = [{diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.tsindex 4b4540a27e2..c661636821a 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts@@ -9,6 +9,7 @@ import {summarizeToolGroup,toolGroupAction,toolGroupSummaryKind,+ workEntryViewedImagePath,type ToolGroupSummaryKind,} from "@t3tools/client-runtime/work-log/presentation";export {@@ -89,6 +90,15 @@ export function liveWorkEntryLabel(return workEntryDisplayLabel(entry, workspaceRoot);}+/**+ * Rows that preview an image the agent viewed or produced. They stay out of+ * tool groups and out of the settled-turn fold: the image is the answer the+ * user asked for, not tool noise to hide behind "Worked for ...".+ */+function workEntryRendersImagePreview(entry: WorkLogEntry): boolean {+ return workEntryViewedImagePath(entry) !== null;+}+export function workEntryIsVisibleInGroup(entry: WorkLogEntry,expandedToolGroupEntry = false,@@ -97,6 +107,9 @@ export function workEntryIsVisibleInGroup((expandedToolGroupEntry &&(entry.toolLifecycleStatus === "inProgress" ||entry.sourceActivityKind === "task.progress")) ||+ // An image row stands alone outside any group, so the neutral filter+ // would leave an empty gap while its tool is still in progress.+ workEntryRendersImagePreview(entry) ||!workEntryIndicatesToolNeutralStatus(entry));}@@ -591,6 +604,9 @@ function deriveTurnFolds(input: {if (entry.kind === "work" && entry.entry.agentSpawn !== undefined) {continue;}+ if (entry.kind === "work" && workEntryRendersImagePreview(entry.entry)) {+ continue;+ }hiddenEntryIds.add(entry.id);}if (hiddenEntryIds.size === 0) {@@ -787,7 +803,8 @@ export function deriveMessagesTimelineRows(input: {!entryBelongsToActiveTurn(entry, index) ||entry.kind !== "work" ||entry.entry.agentSpawn !== undefined ||- entry.entry.tone === "error"+ entry.entry.tone === "error" ||+ workEntryRendersImagePreview(entry.entry)) {break;}@@ -890,7 +907,11 @@ export function deriveMessagesTimelineRows(input: {}if (timelineEntry.kind === "work") {- if (timelineEntry.entry.agentSpawn !== undefined || timelineEntry.entry.tone === "error") {+ if (+ timelineEntry.entry.agentSpawn !== undefined ||+ timelineEntry.entry.tone === "error" ||+ workEntryRendersImagePreview(timelineEntry.entry)+ ) {nextRows.push({kind: "work",id: timelineEntry.id,@@ -909,6 +930,7 @@ export function deriveMessagesTimelineRows(input: {nextEntry.kind !== "work" ||nextEntry.entry.agentSpawn !== undefined ||nextEntry.entry.tone === "error" ||+ workEntryRendersImagePreview(nextEntry.entry) ||activeWorkEntryIds.has(nextEntry.id) ||collapsedEntryIds.has(nextEntry.id) ||foldsByAnchorEntryId.has(nextEntry.id)diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex 98622b043f3..c85f85cac12 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -3159,25 +3159,29 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: {</span></div></div>+ {viewedImage && threadRef ? (+ <div+ className="mt-1 ms-7 cursor-default"+ onClick={stopRowToggle}+ onPointerDown={stopRowToggle}+ >+ <ChatMarkdownAssetImage+ environmentId={threadRef.environmentId}+ resource={viewedImage.resource}+ alt={viewedImage.alt}+ srcFragment={viewedImage.srcFragment}+ workspaceRoot={workspaceRoot}+ style={{ maxHeight: "16rem" }}+ onImageExpand={onImageExpand}+ />+ </div>+ ) : null}{expanded && canExpand && expandedBody ? (<divclassName="mt-1 ms-7 cursor-default border-s border-border/45 ps-3 pt-0.5"onClick={stopRowToggle}onPointerDown={stopRowToggle}>- {viewedImage && threadRef ? (- <div className="mb-1.5">- <ChatMarkdownAssetImage- environmentId={threadRef.environmentId}- resource={viewedImage.resource}- alt={viewedImage.alt}- srcFragment={viewedImage.srcFragment}- workspaceRoot={workspaceRoot}- style={{ maxHeight: "16rem" }}- onImageExpand={onImageExpand}- />- </div>- ) : null}<pre className={toolCallExpandedBodyClassName}>{expandedBody}</pre></div>) : null}