🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(ui): collapse long system prompts from the message header (#16582)
Nikita Kabardin1aa538f94d04
diff --git a/web/src/components/ui/MarkdownJsonView.tsx b/web/src/components/ui/MarkdownJsonView.tsxindex 5900a6b6e5..3724770da2 100644--- a/web/src/components/ui/MarkdownJsonView.tsx+++ b/web/src/components/ui/MarkdownJsonView.tsx@@ -7,7 +7,7 @@ import { Button } from "@/src/components/ui/button";import { PrettyJsonView } from "@/src/components/ui/PrettyJsonView";import { MarkdownView } from "@/src/components/ui/MarkdownViewer";import { type MediaReturnType } from "@/src/features/media/validation";-import { Check, Copy } from "lucide-react";+import { Check, ChevronDown, Copy } from "lucide-react";import { useMemo, useState } from "react";import { type z } from "zod";import { useMarkdownRenderCharacterLimit } from "@/src/hooks/useMarkdownRenderCharacterLimit";@@ -20,6 +20,12 @@ type MarkdownJsonViewHeaderProps = {handleOnCopy: (event?: React.MouseEvent<HTMLButtonElement>) => void;canEnableMarkdown?: boolean;controlButtons?: React.ReactNode;+ /** When set, the header hosts expand/collapse so a long body is not the+ only place to find the control. */+ collapseControl?: {+ isCollapsed: boolean;+ onToggle: () => void;+ };inset?: boolean;};@@ -30,9 +36,37 @@ export function MarkdownJsonViewHeader({handleOnCopy,canEnableMarkdown: _canEnableMarkdown = true,controlButtons,+ collapseControl,inset = false,}: MarkdownJsonViewHeaderProps) {const [isCopied, setIsCopied] = useState(false);+ const collapseLabel = collapseControl+ ? collapseControl.isCollapsed+ ? "Expand system prompt"+ : "Collapse system prompt"+ : undefined;+ // Keep the visible title in the title-button name (WCAG 2.5.3). A generic+ // aria-label would hide message `name`s from assistive tech.+ const titleButtonLabel =+ typeof title === "string" && collapseLabel+ ? `${title}, ${collapseLabel}`+ : collapseLabel;++ const titleContent = (+ <>+ {collapseControl ? (+ <ChevronDown+ className={cn(+ "h-3.5 w-3.5 shrink-0 transition-transform",+ collapseControl.isCollapsed && "-rotate-90",+ )}+ aria-hidden+ />+ ) : null}+ {titleIcon}+ {title}+ </>+ );return (<div@@ -44,10 +78,33 @@ export function MarkdownJsonViewHeader({{/* Masked from session recordings: the title can be a customer-providedmessage `name` (or tool name) rather than a fixed role string. */}<div className="ph-no-capture flex items-center gap-2">- {titleIcon}- {title}+ {collapseControl ? (+ <button+ type="button"+ onClick={collapseControl.onToggle}+ aria-expanded={!collapseControl.isCollapsed}+ aria-label={titleButtonLabel}+ className="hover:text-foreground/80 flex items-center gap-1.5"+ >+ {titleContent}+ </button>+ ) : (+ titleContent+ )}</div><div className="mr-1 flex min-w-0 shrink flex-row items-center gap-1">+ {collapseControl ? (+ <Button+ variant="ghost"+ size="xs"+ type="button"+ onClick={collapseControl.onToggle}+ aria-label={collapseLabel}+ className="text-muted-foreground hover:bg-border w-fit text-xs"+ >+ {collapseControl.isCollapsed ? "Expand" : "Collapse"}+ </Button>+ ) : null}{controlButtons}<Buttontitle="Copy to clipboard"diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsxindex 0226407fb9..d638c171eb 100644--- a/web/src/components/ui/MarkdownViewer.tsx+++ b/web/src/components/ui/MarkdownViewer.tsx@@ -562,7 +562,7 @@ export function MarkdownView({<Buttonvariant="ghost"size="xs"- onClick={toggleCollapsed}+ onClick={() => toggleCollapsed("inline")}className="w-fit text-xs underline">{isCollapsed ? "Expand system prompt" : "Collapse system prompt"}@@ -579,6 +579,14 @@ export function MarkdownView({handleOnValueChange={handleOnValueChange}handleOnCopy={handleOnCopy}controlButtons={controlButtons}+ collapseControl={+ shouldBeCollapsible+ ? {+ isCollapsed,+ onToggle: () => toggleCollapsed("header"),+ }+ : undefined+ }/><div className="border-t" /></>diff --git a/web/src/components/ui/PrettyJsonView.gate.clienttest.tsx b/web/src/components/ui/PrettyJsonView.gate.clienttest.tsxindex 8546ff2457..6748f2c15a 100644--- a/web/src/components/ui/PrettyJsonView.gate.clienttest.tsx+++ b/web/src/components/ui/PrettyJsonView.gate.clienttest.tsx@@ -96,4 +96,16 @@ describe("PrettyJsonView large-string gate (LFE-10991)", () => {expect(screen.getByText(/Large string —/i)).toBeInTheDocument();expect(screen.queryByTestId("json-view")).not.toBeInTheDocument();});++ it("does not offer system-prompt collapse on a gated huge string", () => {+ // The header collapse path must not split() the multi-MB string the+ // large-string gate exists to keep off the main thread.+ const huge = "x".repeat(LARGE_STRING_RENDER_CHAR_LIMIT + 1);+ render(<PrettyJsonView json={huge} title="system" isSystemPrompt />);++ expect(screen.getByText(/Large string —/i)).toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: /system prompt/i }),+ ).not.toBeInTheDocument();+ });});diff --git a/web/src/components/ui/PrettyJsonView.tsx b/web/src/components/ui/PrettyJsonView.tsxindex 963407c3cd..2fd089034a 100644--- a/web/src/components/ui/PrettyJsonView.tsx+++ b/web/src/components/ui/PrettyJsonView.tsx@@ -11,6 +11,7 @@ import { copyTextToClipboard } from "@/src/utils/clipboard";import { JSONView } from "@/src/components/ui/CodeJsonViewer";import { Button } from "@/src/components/ui/button";import { useClickWithoutSelection } from "@/src/hooks/useClickWithoutSelection";+import { useCollapsibleSystemPrompt } from "@/src/hooks/useCollapsibleSystemPrompt";import {ChevronDown,ChevronRight,@@ -904,6 +905,28 @@ export function PrettyJsonView(props: {[parsedJson, largeStringValue, characterLimit],);+ // Nested MarkdownView is rendered without a title (this view owns the+ // header), so the header must host the same collapse control that+ // MarkdownView would show when it has a title. Skip gated large strings:+ // they render through LargeStringFallback, and splitting them for a+ // preview would undo the main-thread guard that gate exists for.+ const systemPromptCollapsibleContent =+ largeStringValue !== null+ ? ""+ : typeof markdownContent === "string"+ ? markdownContent+ : typeof parsedJson === "string"+ ? parsedJson+ : "";+ const {+ shouldBeCollapsible: shouldCollapseSystemPrompt,+ isCollapsed: isSystemPromptCollapsed,+ toggleCollapsed: toggleSystemPromptCollapsed,+ } = useCollapsibleSystemPrompt({+ isSystemPrompt: Boolean(props.isSystemPrompt),+ content: systemPromptCollapsibleContent,+ });+const baseTableData = useMemo(() => {try {if (@@ -1400,6 +1423,16 @@ export function PrettyJsonView(props: {canEnableMarkdown={false}handleOnValueChange={() => {}} // No-op, parent handles statehandleOnCopy={handleOnCopy}+ collapseControl={+ shouldCollapseSystemPrompt &&+ isMarkdownMode &&+ !shouldRenderStandaloneMedia+ ? {+ isCollapsed: isSystemPromptCollapsed,+ onToggle: () => toggleSystemPromptCollapsed("header"),+ }+ : undefined+ }inset={props.inset}controlButtons={<>diff --git a/web/src/features/posthog-analytics/usePostHogClientCapture.ts b/web/src/features/posthog-analytics/usePostHogClientCapture.tsindex bcc62fa713..07a6743929 100644--- a/web/src/features/posthog-analytics/usePostHogClientCapture.ts+++ b/web/src/features/posthog-analytics/usePostHogClientCapture.ts@@ -35,8 +35,9 @@ const events = {"graph_view_toggle",// Aggregated vs expanded graph build mode (LFE-10676)."graph_mode_switch",- // `source` distinguishes the inline expand/collapse button from the- // trace settings switch; `collapsed` is the new preference value.+ // `source` distinguishes the inline expand/collapse button, the message+ // header control, and the trace settings switch; `collapsed` is the new+ // preference value."system_prompt_collapse_toggle",// Fired from the tree, timeline, graph, and search-result click handlers;// `source` says which surface drove the navigation.diff --git a/web/src/features/traces/components/IOPreview/IOPreviewPretty.stories.tsx b/web/src/features/traces/components/IOPreview/IOPreviewPretty.stories.tsxindex 7c4ab2b81c..6d6a05eda4 100644--- a/web/src/features/traces/components/IOPreview/IOPreviewPretty.stories.tsx+++ b/web/src/features/traces/components/IOPreview/IOPreviewPretty.stories.tsx@@ -24,24 +24,27 @@ export const Default = meta.story({},});-export const Chat = meta.story({+export const LongSystemPrompt = meta.story({args: {input: {messages: [{role: "system",- content: "Answer questions using the product documentation.",+ content: Array.from(+ { length: 12 },+ (_, i) =>+ `system-prompt-line-${i + 1} lorem ipsum dolor sit amet consectetur adipiscing elit`,+ ).join("\n"),},{role: "user",- content: "How can I inspect a slow trace?",+ content: "Summarize the constraints above.",},],},output: {role: "assistant",- content:- "Open the trace detail and compare the **latency** of each observation.",+ content: "Stay within the listed role and constraints.",},},});diff --git a/web/src/features/traces/components/IOPreview/components/ChatMessage.clienttest.tsx b/web/src/features/traces/components/IOPreview/components/ChatMessage.clienttest.tsxindex 9c7a0c44ee..e0d627b927 100644--- a/web/src/features/traces/components/IOPreview/components/ChatMessage.clienttest.tsx+++ b/web/src/features/traces/components/IOPreview/components/ChatMessage.clienttest.tsx@@ -1,4 +1,4 @@-import { fireEvent, render, screen } from "@testing-library/react";+import { fireEvent, render, screen, within } from "@testing-library/react";import { vi } from "vitest";const { copyTextToClipboard } = vi.hoisted(() => ({@@ -119,10 +119,23 @@ describe("ChatMessage content modes", () => {// getByRole ignores elements hidden via display:none, so queries only see the// active render path (markdown vs json), even though ChatMessage mounts both.-const expandButton = () =>- screen.queryByRole("button", { name: /expand system prompt/i });-const collapseButton = () =>- screen.queryByRole("button", { name: /collapse system prompt/i });+// Header + inline controls share the same accessible name, so look them up+// as a set rather than assuming a single toggle.+const expandButtons = () =>+ screen.queryAllByRole("button", { name: /expand system prompt/i });+const collapseButtons = () =>+ screen.queryAllByRole("button", { name: /collapse system prompt/i });+const expandButton = () => expandButtons()[0] ?? null;+const collapseButton = () => collapseButtons()[0] ?? null;++const systemMessageHeader = () => {+ const headers = screen+ .getAllByText("system")+ .map((node) => node.closest(".io-message-header"))+ .filter((node): node is HTMLElement => node instanceof HTMLElement);+ expect(headers.length).toBeGreaterThan(0);+ return headers[0];+};describe("ChatMessage system prompt collapse", () => {beforeEach(() => {@@ -136,7 +149,17 @@ describe("ChatMessage system prompt collapse", () => {content: longSystemPrompt,} as ChatMlMessage);- expect(expandButton()).toBeInTheDocument();+ expect(expandButtons().length).toBeGreaterThan(0);+ expect(+ within(systemMessageHeader()).getAllByRole("button", {+ name: /expand system prompt/i,+ }).length,+ ).toBeGreaterThan(0);+ expect(+ within(systemMessageHeader()).getByRole("button", {+ name: /system, expand system prompt/i,+ }),+ ).toBeInTheDocument();});it("collapses a long system message that carries a name", () => {@@ -181,8 +204,44 @@ describe("ChatMessage system prompt collapse", () => {} as ChatMlMessage);// rendered expanded, with the option to collapse still offered- expect(expandButton()).not.toBeInTheDocument();- expect(collapseButton()).toBeInTheDocument();+ expect(expandButtons()).toHaveLength(0);+ expect(collapseButtons().length).toBeGreaterThan(0);+ });++ it("puts a collapse control on the system message header so it is reachable without scrolling", () => {+ // The inline "Collapse system prompt" control lives after the prompt body,+ // so an expanded long prompt hid it below the fold. The header must offer+ // the same action.+ localStorage.setItem("collapseSystemPrompt", "false");++ renderChatMessage({+ role: "system",+ content: longSystemPrompt,+ } as ChatMlMessage);++ expect(+ within(systemMessageHeader()).getAllByRole("button", {+ name: /collapse system prompt/i,+ }).length,+ ).toBeGreaterThan(0);+ });++ it("collapses from the system message header", () => {+ localStorage.setItem("collapseSystemPrompt", "false");++ renderChatMessage({+ role: "system",+ content: longSystemPrompt,+ } as ChatMlMessage);++ fireEvent.click(+ within(systemMessageHeader()).getAllByRole("button", {+ name: /collapse system prompt/i,+ })[0],+ );++ expect(expandButtons().length).toBeGreaterThan(0);+ expect(localStorage.getItem("collapseSystemPrompt")).toBe("true");});it("migrates the legacy expanded preference key and writes it through", () => {diff --git a/web/src/hooks/useCollapsibleSystemPrompt.ts b/web/src/hooks/useCollapsibleSystemPrompt.tsindex f68689f279..c1b8e790b0 100644--- a/web/src/hooks/useCollapsibleSystemPrompt.ts+++ b/web/src/hooks/useCollapsibleSystemPrompt.ts@@ -96,10 +96,12 @@ interface UseCollapsibleSystemPromptOptions {previewCharLimit?: number;}+type SystemPromptCollapseSource = "inline" | "header";+interface UseCollapsibleSystemPromptReturn {shouldBeCollapsible: boolean;isCollapsed: boolean;- toggleCollapsed: () => void;+ toggleCollapsed: (source?: SystemPromptCollapseSource) => void;truncatedContent: string;}@@ -131,13 +133,19 @@ export function useCollapsibleSystemPrompt({return null;}- const lines = content.split("\n");+ // Split only a bounded prefix. A full split("\n") over a multi-MB string+ // is the main-thread cost PrettyJsonView's large-string gate exists to+ // avoid; the preview never needs more than the first few lines anyway.+ const scanLimit = Math.max(previewCharLimit, 8_192);+ const scanText =+ content.length > scanLimit ? content.slice(0, scanLimit) : content;+ const lines = scanText.split("\n");const previewText = lines.slice(0, previewLines).join("\n");if (previewText.length > previewCharLimit) {return previewText.slice(0, previewCharLimit) + "...";}- if (lines.length > previewLines) {+ if (lines.length > previewLines || content.length > scanText.length) {return previewText + "\n...";}return null;@@ -146,14 +154,14 @@ export function useCollapsibleSystemPrompt({const shouldBeCollapsible = preview !== null;const isCollapsed = shouldBeCollapsible && collapsePreference;- const toggleCollapsed = () => {+ const toggleCollapsed = (source: SystemPromptCollapseSource = "inline") => {const next = !collapsePreference;// No trace analytics dimensions here on purpose: this fires from shared// components that also render outside trace contexts (e.g. session view),// and the event must keep one shape across sources.capture("trace_detail:system_prompt_collapse_toggle", {collapsed: next,- source: "inline",+ source,});setCollapsePreference(next);};