Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions apps/web/src/components/chat/AssistantCitationChip.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 },
Expand Down
37 changes: 36 additions & 1 deletion apps/web/src/components/chat/MessagesTimeline.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
});
31 changes: 31 additions & 0 deletions apps/web/src/components/chat/MessagesTimeline.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Comment thread
flamboh marked this conversation as resolved.

function visibleUserMessageText(text: string): string {
const linkLabels = (segment: string) => segment.replace(MARKDOWN_LINK, "$1");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
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));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

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
);
}
14 changes: 1 addition & 13 deletions apps/web/src/components/chat/MessagesTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ import {
resolveTimelineMinimapNavigationInteractive,
resolveTimelineMinimapTopPercent,
resolveWorkGroupScrollIndex,
shouldCollapseUserMessage,
shouldFollowWorkGroupAppend,
shouldPreserveAssistantLineBreaks,
threadReadLabelPrefix,
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/assistantCitations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)})`;
}
Expand Down
Loading