Skip to content

Commit 7c569e3

Browse files
committed
fix(web): messages with quotes or links no longer collapse when short
1 parent c9b21b2 commit 7c569e3

5 files changed

Lines changed: 78 additions & 17 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { AssistantCitation } from "@t3tools/contracts";
2-
import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
2+
import {
3+
assistantCitationLabel,
4+
serializeAssistantCitation,
5+
} from "@t3tools/shared/assistantCitations";
36
import { Link, useNavigate } from "@tanstack/react-router";
47
import { PencilIcon, QuoteIcon } from "lucide-react";
58
import {
@@ -94,8 +97,7 @@ export function AssistantCitationChip({
9497
},
9598
}
9699
: undefined;
97-
const preview = (citation.comment?.trim() || citation.text).replace(/\s+/g, " ");
98-
const label = preview.length > 64 ? `${preview.slice(0, 64)}…` : preview;
100+
const label = assistantCitationLabel(citation);
99101
const sourceLinkProps = {
100102
to: "/$environmentId/$threadId" as const,
101103
params: { environmentId: citation.environmentId, threadId: citation.threadId },

‎apps/web/src/components/chat/MessagesTimeline.logic.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import {
1717
import { makeStreamingTimelineFixture } from "../../test-fixtures";
1818
import type { TurnDiffSummary } from "../../types";
1919
import { describe, expect, it } from "vite-plus/test";
20-
import { MessageId, RunId } from "@t3tools/contracts";
20+
import { EnvironmentId, MessageId, RunId } from "@t3tools/contracts";
21+
import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
2122
import {
2223
computeStableMessagesTimelineRows,
2324
computeMessageDurationStart,
2425
deriveMessagesTimelineRows,
2526
deriveMessagesTimelineRowsWithState,
27+
shouldCollapseUserMessage,
2628
liveWorkEntryLabel,
2729
normalizeCompactToolLabel,
2830
resolveAssistantMessageCopyState,
@@ -4889,3 +4891,36 @@ describe("failed turn transcript", () => {
48894891
},
48904892
);
48914893
});
4894+
4895+
describe("shouldCollapseUserMessage", () => {
4896+
it("measures a quote chip by its label, not its encoded link", () => {
4897+
const quote = "A long assistant paragraph that the user quoted. ".repeat(40);
4898+
const citation = serializeAssistantCitation({
4899+
version: 1,
4900+
environmentId: EnvironmentId.make("environment"),
4901+
threadId: ThreadId.make("thread"),
4902+
messageId: MessageId.make("source"),
4903+
text: quote,
4904+
comment: "Why does this matter?",
4905+
start: 0,
4906+
end: quote.length,
4907+
prefix: "",
4908+
suffix: "",
4909+
});
4910+
4911+
expect(shouldCollapseUserMessage(`${citation} Can you expand on this?`)).toBe(false);
4912+
expect(shouldCollapseUserMessage(`${citation} ${"More text. ".repeat(60)}`)).toBe(true);
4913+
});
4914+
4915+
it("measures file links and context chips by their label", () => {
4916+
const links = Array.from(
4917+
{ length: 8 },
4918+
(_, index) =>
4919+
`[file${index}.ts](/workspace/projects/example/packages/some/deeply/nested/directory/file${index}.ts)`,
4920+
);
4921+
const text = `Compare ${links.join(", ")} with [terminal 1](t3-context://v1/terminal/${"a".repeat(36)}).`;
4922+
4923+
expect(text.length).toBeGreaterThan(600);
4924+
expect(shouldCollapseUserMessage(text)).toBe(false);
4925+
});
4926+
});

‎apps/web/src/components/chat/MessagesTimeline.logic.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup";
22
export { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup";
33
import * as Equal from "effect/Equal";
4+
import {
5+
assistantCitationLabel,
6+
collectAssistantCitations,
7+
} from "@t3tools/shared/assistantCitations";
48
import { shallow } from "zustand/vanilla/shallow";
59
import { renderCodexDirectivesForCopy } from "@t3tools/client-runtime/codex-markdown-directives";
610
import {
@@ -2101,3 +2105,30 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
21012105
}
21022106
}
21032107
}
2108+
2109+
const MAX_COLLAPSED_USER_MESSAGE_LINES = 8;
2110+
const MAX_COLLAPSED_USER_MESSAGE_LENGTH = 600;
2111+
const MARKDOWN_LINK = /!?\[([^\]\n]*)\]\((?:<[^>\n]*>|[^\s)]*)\)/g;
2112+
2113+
function visibleUserMessageText(text: string): string {
2114+
const linkLabels = (segment: string) => segment.replace(MARKDOWN_LINK, "$1");
2115+
let visible = "";
2116+
let cursor = 0;
2117+
for (const match of collectAssistantCitations(text)) {
2118+
visible += linkLabels(text.slice(cursor, match.start)) + assistantCitationLabel(match.citation);
2119+
cursor = match.end;
2120+
}
2121+
return visible + linkLabels(text.slice(cursor));
2122+
}
2123+
2124+
export function shouldCollapseUserMessage(text: string): boolean {
2125+
const visible = visibleUserMessageText(text);
2126+
if (visible.trim().length === 0) {
2127+
return false;
2128+
}
2129+
2130+
return (
2131+
visible.length > MAX_COLLAPSED_USER_MESSAGE_LENGTH ||
2132+
visible.split("\n").length > MAX_COLLAPSED_USER_MESSAGE_LINES
2133+
);
2134+
}

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ import {
205205
resolveTimelineMinimapNavigationInteractive,
206206
resolveTimelineMinimapTopPercent,
207207
resolveWorkGroupScrollIndex,
208+
shouldCollapseUserMessage,
208209
shouldFollowWorkGroupAppend,
209210
shouldPreserveAssistantLineBreaks,
210211
toolGroupAction,
@@ -4346,22 +4347,9 @@ function UserMessageContextReferenceChip(props: {
43464347
});
43474348
}
43484349

4349-
const MAX_COLLAPSED_USER_MESSAGE_LINES = 8;
4350-
const MAX_COLLAPSED_USER_MESSAGE_LENGTH = 600;
43514350
const COLLAPSED_USER_MESSAGE_FADE_HEIGHT_REM = 1.75;
43524351
const COLLAPSED_USER_MESSAGE_FADE_MASK = `linear-gradient(to bottom, black calc(100% - ${COLLAPSED_USER_MESSAGE_FADE_HEIGHT_REM}rem), transparent)`;
43534352

4354-
function shouldCollapseUserMessage(text: string): boolean {
4355-
if (text.trim().length === 0) {
4356-
return false;
4357-
}
4358-
4359-
return (
4360-
text.length > MAX_COLLAPSED_USER_MESSAGE_LENGTH ||
4361-
text.split("\n").length > MAX_COLLAPSED_USER_MESSAGE_LINES
4362-
);
4363-
}
4364-
43654353
const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(props: {
43664354
text: string;
43674355
renderContextReference: (reference: ChatMarkdownContextReference) => ReactNode;

‎packages/shared/src/assistantCitations.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ export function parseAssistantCitationHref(href: string): AssistantCitation | nu
9898
}
9999
}
100100

101+
export function assistantCitationLabel(citation: AssistantCitation): string {
102+
const preview = (citation.comment?.trim() || citation.text).replace(/\s+/g, " ");
103+
return preview.length > 64 ? `${preview.slice(0, 64)}…` : preview;
104+
}
105+
101106
export function serializeAssistantCitation(citation: AssistantCitation): string {
102107
return `[Assistant quote](${formatAssistantCitationHref(citation)})`;
103108
}

0 commit comments

Comments
 (0)