fix(web): messages with quotes or links no longer collapse when short (#16624)

This commit is contained in:
oliver authored and GitHub committed 2026-10-08 01:18:01 -03:00
1 parent 0f09627a11
commit 6f83358ae6
5 files changed
+78 -17

No files matched your search

@@ -1,5 +1,8 @@
import type { AssistantCitation } from "@t3tools/contracts";
import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
import {
assistantCitationLabel,
serializeAssistantCitation,
} from "@t3tools/shared/assistantCitations";
import { Link, useNavigate } from "@tanstack/react-router";
import { PencilIcon, QuoteIcon } from "lucide-react";
import {
@@ -94,8 +97,7 @@ export function AssistantCitationChip({
},
}
: undefined;
const preview = (citation.comment?.trim() || citation.text).replace(/\s+/g, " ");
const label = preview.length > 64 ? `${preview.slice(0, 64)}…` : preview;
const label = assistantCitationLabel(citation);
const sourceLinkProps = {
to: "/$environmentId/$threadId" as const,
params: { environmentId: citation.environmentId, threadId: citation.threadId },
@@ -21,12 +21,14 @@ import {
import { makeStreamingTimelineFixture } from "../../test-fixtures";
import type { TurnDiffSummary } from "../../types";
import { describe, expect, it } from "vite-plus/test";
import { MessageId, RunId } from "@t3tools/contracts";
import { EnvironmentId, MessageId, RunId } from "@t3tools/contracts";
import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
import {
computeStableMessagesTimelineRows,
computeMessageDurationStart,
deriveMessagesTimelineRows,
deriveMessagesTimelineRowsWithState,
shouldCollapseUserMessage,
liveWorkEntryLabel,
normalizeCompactToolLabel,
resolveAssistantMessageCopyState,
@@ -5162,3 +5164,36 @@ describe("live subagents after their parent turn settles", () => {
expect(rows.map((row) => row.id)).toEqual([`attempt-fold:${attempt.id}`, "child"]);
});
});
describe("shouldCollapseUserMessage", () => {
it("measures a quote chip by its label, not its encoded link", () => {
const quote = "A long assistant paragraph that the user quoted. ".repeat(40);
const citation = serializeAssistantCitation({
version: 1,
environmentId: EnvironmentId.make("environment"),
threadId: ThreadId.make("thread"),
messageId: MessageId.make("source"),
text: quote,
comment: "Why does this matter?",
start: 0,
end: quote.length,
prefix: "",
suffix: "",
});
expect(shouldCollapseUserMessage(`${citation} Can you expand on this?`)).toBe(false);
expect(shouldCollapseUserMessage(`${citation} ${"More text. ".repeat(60)}`)).toBe(true);
});
it("measures file links and context chips by their label", () => {
const links = Array.from(
{ length: 8 },
(_, index) =>
`[file${index}.ts](/workspace/projects/example/packages/some/deeply/nested/directory/file${index}.ts)`,
);
const text = `Compare ${links.join(", ")} with [terminal 1](t3-context://v1/terminal/${"a".repeat(36)}).`;
expect(text.length).toBeGreaterThan(600);
expect(shouldCollapseUserMessage(text)).toBe(false);
});
});
@@ -2,6 +2,10 @@ import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setu
export { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup";
import { isLiveSubagentTurnItem } from "@t3tools/client-runtime/state/subagentRuntime";
import * as Equal from "effect/Equal";
import {
assistantCitationLabel,
collectAssistantCitations,
} from "@t3tools/shared/assistantCitations";
import { shallow } from "zustand/vanilla/shallow";
import { renderCodexDirectivesForCopy } from "@t3tools/client-runtime/codex-markdown-directives";
import {
@@ -2226,3 +2230,30 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
}
}
}
const MAX_COLLAPSED_USER_MESSAGE_LINES = 8;
const MAX_COLLAPSED_USER_MESSAGE_LENGTH = 600;
const MARKDOWN_LINK = /!?\[([^\]\n]*)\]\((?:<[^>\n]*>|[^\s)]*)\)/g;
function visibleUserMessageText(text: string): string {
const linkLabels = (segment: string) => segment.replace(MARKDOWN_LINK, "$1");
let visible = "";
let cursor = 0;
for (const match of collectAssistantCitations(text)) {
visible += linkLabels(text.slice(cursor, match.start)) + assistantCitationLabel(match.citation);
cursor = match.end;
}
return visible + linkLabels(text.slice(cursor));
}
export function shouldCollapseUserMessage(text: string): boolean {
const visible = visibleUserMessageText(text);
if (visible.trim().length === 0) {
return false;
}
return (
visible.length > MAX_COLLAPSED_USER_MESSAGE_LENGTH ||
visible.split("\n").length > MAX_COLLAPSED_USER_MESSAGE_LINES
);
}
@@ -207,6 +207,7 @@ import {
resolveTimelineMinimapNavigationInteractive,
resolveTimelineMinimapTopPercent,
resolveWorkGroupScrollIndex,
shouldCollapseUserMessage,
shouldFollowWorkGroupAppend,
shouldPreserveAssistantLineBreaks,
threadReadLabelPrefix,
@@ -4522,22 +4523,9 @@ function UserMessageContextReferenceChip(props: {
});
}
const MAX_COLLAPSED_USER_MESSAGE_LINES = 8;
const MAX_COLLAPSED_USER_MESSAGE_LENGTH = 600;
const COLLAPSED_USER_MESSAGE_FADE_HEIGHT_REM = 1.75;
const COLLAPSED_USER_MESSAGE_FADE_MASK = `linear-gradient(to bottom, black calc(100% - ${COLLAPSED_USER_MESSAGE_FADE_HEIGHT_REM}rem), transparent)`;
function shouldCollapseUserMessage(text: string): boolean {
if (text.trim().length === 0) {
return false;
}
return (
text.length > MAX_COLLAPSED_USER_MESSAGE_LENGTH ||
text.split("\n").length > MAX_COLLAPSED_USER_MESSAGE_LINES
);
}
const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(props: {
text: string;
renderContextReference: (reference: ChatMarkdownContextReference) => ReactNode;
@@ -98,6 +98,11 @@ export function parseAssistantCitationHref(href: string): AssistantCitation | nu
}
}
export function assistantCitationLabel(citation: AssistantCitation): string {
const preview = (citation.comment?.trim() || citation.text).replace(/\s+/g, " ");
return preview.length > 64 ? `${preview.slice(0, 64)}…` : preview;
}
export function serializeAssistantCitation(citation: AssistantCitation): string {
return `[Assistant quote](${formatAssistantCitationHref(citation)})`;
}