Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): collapse an expanded system prompt from the message header (#18616)

Der Gerätd09027c5a5ce
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index 4f18ac0b00..48def24df3 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -39,10 +39,11 @@ import { LangfuseMediaView } from "@/src/components/ui/LangfuseMediaView";
import { type MediaReturnType } from "@/src/features/media";
import { JSONView } from "@/src/components/ui/CodeJsonViewer";
import { MarkdownJsonViewHeader } from "@/src/components/ui/MarkdownJsonView";
+import { SystemPromptCollapseButton } from "@/src/components/ui/SystemPromptCollapseButton";
import { copyTextToClipboard } from "@/src/utils/clipboard";
import { MENTION_USER_PREFIX } from "@/src/features/comments/lib/mentionParser";
import { useCollapsibleSystemPrompt } from "@/src/hooks/useCollapsibleSystemPrompt";
-import { Button } from "@/src/components/design-system/Button/Button";
+import { Button } from "@/src/components/ui/button";
import { getSafeImageUrl, getSafeLinkUrl } from "@/src/components/ui/safe-url";
import { env } from "@/src/env.mjs";
import {
@@ -62,6 +63,7 @@ import { exceedsMarkdownRenderLimits } from "@/src/components/ui/markdown-render
import { useMarkdownRenderCharacterLimit } from "@/src/hooks/useMarkdownRenderCharacterLimit";
import {
classifyMediaValue,
+ splitStringByMediaReferences,
type MediaDescriptor,
} from "@/src/components/ui/media/mediaUtils";
import { MediaReferenceTag } from "@/src/components/ui/media/MediaReferenceTag";
@@ -544,13 +546,17 @@ export function MarkdownView({
const standaloneMediaDescriptor: MediaDescriptor | null =
typeof markdown === "string" ? classifyMediaValue(markdown) : null;
- // Collapse preview is built from text parts only: serialized image/audio
- // parts (media-reference strings, base64 data URIs) neither survive the
- // generic markdown renderer nor belong in a first-lines text preview — and
- // media alone should not make a prompt collapsible.
+ // Collapse preview is built from text parts only: media references, base64
+ // data URIs and audio parts neither survive the generic markdown renderer nor
+ // belong in a first-lines text preview — and media alone should not make a
+ // prompt collapsible.
const collapsibleContent =
typeof markdown === "string"
- ? markdown
+ ? splitStringByMediaReferences(markdown)
+ .filter((s) => s.type === "text")
+ .map((s) => s.value)
+ .join("")
+ .trim()
: (markdown ?? [])
.filter(isOpenAITextContentPart)
.map((part) => part.text)
@@ -579,16 +585,30 @@ export function MarkdownView({
getRenderedInlineMediaIds({ markdown, audio }),
);
+ const handleCollapseInline = () => toggleCollapsed("inline");
+ const handleCollapseHeader = () => toggleCollapsed("header");
+
const collapseToggle = shouldBeCollapsible ? (
- <div className="flex">
- <Button
- variant="text"
- onClick={() => toggleCollapsed("inline")}
- text={isCollapsed ? "Expand system prompt" : "Collapse system prompt"}
- />
- </div>
+ <Button
+ variant="ghost"
+ size="xs"
+ onClick={handleCollapseInline}
+ className="w-fit text-xs underline"
+ >
+ {isCollapsed ? "Expand system prompt" : "Collapse system prompt"}
+ </Button>
) : null;
+ const headerControlButtons =
+ shouldBeCollapsible && !isCollapsed ? (
+ <>
+ <SystemPromptCollapseButton onCollapse={handleCollapseHeader} />
+ {controlButtons}
+ </>
+ ) : (
+ controlButtons
+ );
+
return (
<div className="group/iosection overflow-hidden" key={theme}>
{title ? (
@@ -597,7 +617,7 @@ export function MarkdownView({
titleIcon={titleIcon}
handleOnCopy={handleOnCopy}
hoverRevealControls
- controlButtons={controlButtons}
+ controlButtons={headerControlButtons}
/>
) : null}
{afterHeader}
diff --git a/web/src/components/ui/PrettyJsonView.tsx b/web/src/components/ui/PrettyJsonView.tsx
index 1cd457c07a..4a87ef4baf 100644
--- a/web/src/components/ui/PrettyJsonView.tsx
+++ b/web/src/components/ui/PrettyJsonView.tsx
@@ -15,10 +15,12 @@ import { Skeleton } from "@/src/components/ui/skeleton";
import { type MediaReturnType } from "@/src/features/media";
import { LangfuseMediaView } from "@/src/components/ui/LangfuseMediaView";
import { MarkdownJsonViewHeader } from "@/src/components/ui/MarkdownJsonView";
+import { SystemPromptCollapseButton } from "@/src/components/ui/SystemPromptCollapseButton";
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,
@@ -1156,6 +1158,8 @@ export function PrettyJsonView(props: {
[onExternalExpansionChange, actualExpansionState],
);
+ const handleCollapseHeader = () => toggleSystemPromptCollapsed("header");
+
const handleOnCopy = (event?: React.MouseEvent<HTMLButtonElement>) => {
if (event) {
event.preventDefault();
@@ -1191,6 +1195,16 @@ export function PrettyJsonView(props: {
: [];
const shouldRenderStandaloneMedia =
isMarkdownMode && standaloneMediaReferenceStrings.length > 0;
+
+ /** Collapse state for a nested MarkdownView that owns no header of its own. */
+ const {
+ shouldBeCollapsible: shouldCollapseSystemPrompt,
+ isCollapsed: isSystemPromptCollapsed,
+ toggleCollapsed: toggleSystemPromptCollapsed,
+ } = useCollapsibleSystemPrompt({
+ isSystemPrompt: Boolean(props.isSystemPrompt),
+ content: markdownContent ?? "",
+ });
const remainingMarkdownMedia = filterAlreadyRenderedMedia(
props.media,
getRenderedInlineMediaIds({
@@ -1429,6 +1443,14 @@ export function PrettyJsonView(props: {
hoverRevealControls
controlButtons={
<>
+ {/* Expanded only, and only when the nested MarkdownView that owns
+ the inline control is the thing being rendered. */}
+ {shouldCollapseSystemPrompt &&
+ !isSystemPromptCollapsed &&
+ isMarkdownMode &&
+ !shouldRenderStandaloneMedia ? (
+ <SystemPromptCollapseButton onCollapse={handleCollapseHeader} />
+ ) : null}
{expandCollapseButton}
{props.controlButtons}
</>
diff --git a/web/src/components/ui/SystemPromptCollapseButton.tsx b/web/src/components/ui/SystemPromptCollapseButton.tsx
new file mode 100644
index 0000000000..a2bdec1365
--- /dev/null
+++ b/web/src/components/ui/SystemPromptCollapseButton.tsx
@@ -0,0 +1,22 @@
+import { Button } from "@/src/components/ui/button";
+
+/** Header collapse control for long system prompts; rendered only while expanded. */
+export function SystemPromptCollapseButton({
+ onCollapse,
+}: {
+ onCollapse: () => void;
+}) {
+ return (
+ <Button
+ variant="ghost"
+ size="xs"
+ type="button"
+ onClick={onCollapse}
+ aria-label="Collapse system prompt"
+ title="Collapse system prompt"
+ className="text-muted-foreground hover:bg-border w-fit text-xs"
+ >
+ Collapse
+ </Button>
+ );
+}
diff --git a/web/src/features/traces/components/IOPreview/components/ChatMessage.clienttest.tsx b/web/src/features/traces/components/IOPreview/components/ChatMessage.clienttest.tsx
index 60f1369583..69bd501221 100644
--- a/web/src/features/traces/components/IOPreview/components/ChatMessage.clienttest.tsx
+++ b/web/src/features/traces/components/IOPreview/components/ChatMessage.clienttest.tsx
@@ -127,6 +127,14 @@ const collapseButtons = () =>
const expandButton = () => expandButtons()[0] ?? null;
const collapseButton = () => collapseButtons()[0] ?? null;
+// An expanded prompt offers one control at each end of the message: the header
+// (reachable without scrolling) and the inline one after the body.
+const isInHeader = (el: HTMLElement) =>
+ el.closest(".io-message-header") !== null;
+const headerCollapseButtons = () => collapseButtons().filter(isInHeader);
+const inlineCollapseButtons = () =>
+ collapseButtons().filter((el) => !isInHeader(el));
+
describe("ChatMessage system prompt collapse", () => {
beforeEach(() => {
vi.stubGlobal("localStorage", createMemoryStorage());
@@ -163,7 +171,10 @@ describe("ChatMessage system prompt collapse", () => {
{ shouldRenderMarkdown: false },
);
- expect(expandButton()).toBeInTheDocument();
+ // Same collapsed-state guarantee as the markdown path: exactly one control,
+ // and it sits with the prompt body rather than in the message header.
+ expect(expandButtons()).toHaveLength(1);
+ expect(expandButtons()[0].closest(".io-message-header")).toBeNull();
});
it("expands on click and persists the expanded preference", () => {
@@ -186,16 +197,59 @@ describe("ChatMessage system prompt collapse", () => {
content: longSystemPrompt,
} as ChatMlMessage);
- // rendered expanded, with a single collapse control still offered
+ // rendered expanded, with one collapse control at each end of the message:
+ // an expanded prompt is too tall for the inline one to be reachable.
expect(expandButtons()).toHaveLength(0);
- expect(collapseButtons()).toHaveLength(1);
+ expect(collapseButtons()).toHaveLength(2);
+ expect(headerCollapseButtons()).toHaveLength(1);
+ expect(inlineCollapseButtons()).toHaveLength(1);
- fireEvent.click(collapseButton()!);
+ // Click the INLINE control — the header test below covers the header path.
+ fireEvent.click(inlineCollapseButtons()[0]!);
expect(expandButtons().length).toBeGreaterThan(0);
expect(localStorage.getItem("collapseSystemPrompt")).toBe("true");
});
+ it("collapses from the header control without scrolling to the body", () => {
+ localStorage.setItem("collapseSystemPrompt", "false");
+
+ renderChatMessage({
+ role: "system",
+ content: longSystemPrompt,
+ } as ChatMlMessage);
+
+ fireEvent.click(headerCollapseButtons()[0]);
+
+ // back to the collapsed shape: one control, inline only
+ expect(expandButtons()).toHaveLength(1);
+ expect(expandButtons()[0].closest(".io-message-header")).toBeNull();
+ expect(collapseButtons()).toHaveLength(0);
+ expect(localStorage.getItem("collapseSystemPrompt")).toBe("true");
+ });
+
+ it("offers a header collapse control on the json/pretty path too", () => {
+ // PrettyJsonView delegates the body to a title-less MarkdownView, which
+ // renders no header of its own — so a fix confined to MarkdownViewer would
+ // leave this path collapsible only from the bottom.
+ localStorage.setItem("collapseSystemPrompt", "false");
+
+ renderChatMessage(
+ { role: "system", content: longSystemPrompt } as ChatMlMessage,
+ { shouldRenderMarkdown: false },
+ );
+
+ expect(collapseButtons()).toHaveLength(2);
+ expect(headerCollapseButtons()).toHaveLength(1);
+ expect(inlineCollapseButtons()).toHaveLength(1);
+
+ fireEvent.click(headerCollapseButtons()[0]);
+
+ expect(expandButtons()).toHaveLength(1);
+ expect(expandButtons()[0].closest(".io-message-header")).toBeNull();
+ expect(localStorage.getItem("collapseSystemPrompt")).toBe("true");
+ });
+
it("migrates the legacy expanded preference key and writes it through", () => {
// pre-existing "user expanded" choice under the old global key
localStorage.setItem("traceSystemPrompt:collapsed", "false");