TypeScript
fix(web): messages with quotes or links no longer collapse when short (#16624)
oliver6f83358ae6d8
diff --git a/apps/web/src/components/chat/AssistantCitationChip.tsx b/apps/web/src/components/chat/AssistantCitationChip.tsxindex e03872553a8..9f5d3a2e1e5 100644--- a/apps/web/src/components/chat/AssistantCitationChip.tsx+++ b/apps/web/src/components/chat/AssistantCitationChip.tsx@@ -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 },diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.tsindex 98339041f68..93fc89598be 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts@@ -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);+ });+});diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.tsindex 74f3991396e..4a81905eaa2 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts@@ -2,6 +2,10 @@ import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setuexport { 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+ );+}diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex 6145098f604..8fd9789d9d3 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -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;diff --git a/packages/shared/src/assistantCitations.ts b/packages/shared/src/assistantCitations.tsindex e27e35f352b..86ca0701f20 100644--- a/packages/shared/src/assistantCitations.ts+++ b/packages/shared/src/assistantCitations.ts@@ -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)})`;}