Skip to content

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

feat(trace-ui): collapse long system prompts (#10198)

Nimare36f69cee41a
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index 4a9e18bc8c..edfd1a3712 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -36,6 +36,8 @@ import { MarkdownJsonViewHeader } from "@/src/components/ui/MarkdownJsonView";
import { copyTextToClipboard } from "@/src/utils/clipboard";
import DOMPurify from "dompurify";
import { MENTION_USER_PREFIX } from "@/src/features/comments/lib/mentionParser";
+import { useCollapsibleSystemPrompt } from "@/src/hooks/useCollapsibleSystemPrompt";
+import { Button } from "@/src/components/ui/button";
type ReactMarkdownNode = ReactMarkdownExtraProps["node"];
type ReactMarkdownNodeChildren = Exclude<
@@ -333,12 +335,21 @@ export function MarkdownView({
const { resolvedTheme: theme } = useTheme();
const { setIsMarkdownEnabled } = useMarkdownContext();
+ const markdownContent =
+ typeof markdown === "string" ? markdown : parseOpenAIContentParts(markdown);
+
+ const {
+ shouldBeCollapsible,
+ isCollapsed,
+ toggleCollapsed,
+ truncatedContent,
+ } = useCollapsibleSystemPrompt({
+ role: title ?? "",
+ content: markdownContent,
+ });
+
const handleOnCopy = () => {
- const rawText =
- typeof markdown === "string"
- ? markdown
- : parseOpenAIContentParts(markdown);
- void copyTextToClipboard(rawText);
+ void copyTextToClipboard(markdownContent);
};
const handleOnValueChange = () => {
@@ -373,11 +384,27 @@ export function MarkdownView({
>
{typeof markdown === "string" ? (
// plain string
- <MarkdownRenderer
- markdown={markdown}
- theme={theme}
- customCodeHeaderClassName={customCodeHeaderClassName}
- />
+ <>
+ <MarkdownRenderer
+ markdown={
+ shouldBeCollapsible && isCollapsed ? truncatedContent : markdown
+ }
+ theme={theme}
+ customCodeHeaderClassName={customCodeHeaderClassName}
+ />
+ {shouldBeCollapsible && (
+ <Button
+ variant="ghost"
+ size="xs"
+ onClick={toggleCollapsed}
+ className="w-fit text-xs underline"
+ >
+ {isCollapsed
+ ? "Expand system prompt"
+ : "Collapse system prompt"}
+ </Button>
+ )}
+ </>
) : (
// content parts (multi-modal)
(markdown ?? []).map((content, index) =>
diff --git a/web/src/hooks/useCollapsibleSystemPrompt.ts b/web/src/hooks/useCollapsibleSystemPrompt.ts
new file mode 100644
index 0000000000..014ba0c0fa
--- /dev/null
+++ b/web/src/hooks/useCollapsibleSystemPrompt.ts
@@ -0,0 +1,86 @@
+import { useState, useMemo } from "react";
+
+const COLLAPSE_CHAR_THRESHOLD = 250;
+const STORAGE_KEY = "traceSystemPrompt:collapsed";
+
+interface UseCollapsibleSystemPromptOptions {
+ role: string;
+ content: string;
+}
+
+interface UseCollapsibleSystemPromptReturn {
+ shouldBeCollapsible: boolean;
+ isCollapsed: boolean;
+ toggleCollapsed: () => void;
+ truncatedContent: string;
+}
+
+/**
+ * Auto-collapses system prompts >250 chars.
+ * Saves user preference when manually expanded to localStorage.
+ */
+export function useCollapsibleSystemPrompt({
+ role,
+ content,
+}: UseCollapsibleSystemPromptOptions): UseCollapsibleSystemPromptReturn {
+ const shouldBeCollapsible = useMemo(() => {
+ if (role !== "system") return false;
+ if (!content || typeof content !== "string") return false;
+ return content.length > COLLAPSE_CHAR_THRESHOLD;
+ }, [role, content]);
+
+ // Default: collapsed if long
+ // Override: expanded if user previously expanded (localStorage = false)
+ const [isCollapsed, setIsCollapsed] = useState(() => {
+ if (!shouldBeCollapsible) return false;
+
+ try {
+ const stored = localStorage.getItem(STORAGE_KEY);
+ // No preference = use default (collapsed)
+ // false = user expanded it
+ return stored !== null ? JSON.parse(stored) : true;
+ } catch {
+ return true;
+ }
+ });
+
+ const toggleCollapsed = () => {
+ setIsCollapsed((prev: boolean) => {
+ const newValue = !prev;
+
+ try {
+ if (newValue) {
+ // delete key on collapse to get default behavior
+ localStorage.removeItem(STORAGE_KEY);
+ } else {
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(false));
+ }
+ } catch {
+ // Ignore localStorage errors
+ }
+
+ return newValue;
+ });
+ };
+
+ // Truncated preview (first 4 lines or 250 chars)
+ const truncatedContent = useMemo(() => {
+ if (!shouldBeCollapsible || !content) return content;
+
+ const lines = content.split("\n");
+ const preview = lines.slice(0, 4).join("\n");
+ const hasMore = lines.length > 4;
+ const tooLong = preview.length > COLLAPSE_CHAR_THRESHOLD;
+
+ return tooLong
+ ? preview.slice(0, COLLAPSE_CHAR_THRESHOLD) + "..."
+ : preview + (hasMore ? "\n..." : "");
+ }, [shouldBeCollapsible, content]);
+
+ return {
+ shouldBeCollapsible,
+ isCollapsed,
+ toggleCollapsed,
+ truncatedContent,
+ };
+}