Skip to content

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

fix(trace): Improve `MarkdownViewer` styling (#15129)

Ben Bachem848947ea3a82
diff --git a/web/.storybook/preview.tsx b/web/.storybook/preview.tsx
index 22c269f09c..f0bfb4059a 100644
--- a/web/.storybook/preview.tsx
+++ b/web/.storybook/preview.tsx
@@ -12,6 +12,7 @@ import {
type ReactNode,
} from "react";
import { TooltipProvider } from "../src/components/ui/tooltip";
+import { ThemeProvider } from "../src/features/theming/ThemeProvider";
import { MarkdownContextProvider } from "../src/features/theming/useMarkdownContext";
import { LAYER_ORDER } from "../src/components/ui/layer";
import "../src/styles/globals.css";
@@ -24,6 +25,7 @@ import "streamdown/styles.css";
function StorybookThemeProvider({
children,
fullHeight,
+ theme,
}: {
children?: ReactNode;
/**
@@ -33,6 +35,7 @@ function StorybookThemeProvider({
* content. (LFE-10549)
*/
fullHeight: boolean;
+ theme: "light" | "dark";
}) {
// Overlay layer containers, declared exactly like _document.tsx: a
// <div data-overlay-root> holding one <div data-layer={name}/> per
@@ -66,13 +69,20 @@ function StorybookThemeProvider({
// _app.tsx). `#__next` is given a real viewport height so the `height: 100%`
// chain has something to resolve against.
return (
- <div
- id="__next"
- className="bg-background text-foreground"
- style={{ height: fullHeight ? "100vh" : "auto" }}
+ <ThemeProvider
+ attribute="class"
+ forcedTheme={theme}
+ enableSystem={false}
+ disableTransitionOnChange
>
- <div>{children}</div>
- </div>
+ <div
+ id="__next"
+ className="bg-background text-foreground"
+ style={{ height: fullHeight ? "100vh" : "auto" }}
+ >
+ <div>{children}</div>
+ </div>
+ </ThemeProvider>
);
}
@@ -140,7 +150,10 @@ export default definePreview({
},
decorators: [
(Story, context) => (
- <StorybookThemeProvider fullHeight={context.viewMode !== "docs"}>
+ <StorybookThemeProvider
+ fullHeight={context.viewMode !== "docs"}
+ theme={context.globals.theme === "dark" ? "dark" : "light"}
+ >
{/* MarkdownContextProvider mirrors the app: pages render inside it so
the JSON/IO viewers (CodeJsonViewer's JSONView calls
useMarkdownContext) work identically to production. Without it,
diff --git a/web/src/components/design-system/Codeblock/Codeblock.stories.tsx b/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
index 650f042892..fdee9e591b 100644
--- a/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
+++ b/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
@@ -12,12 +12,3 @@ export const Default = meta.story({
theme: "light",
},
});
-
-export const CardHeader = meta.story({
- args: {
- language: "typescript",
- value: 'const greeting = "Hello, Langfuse!";',
- theme: "light",
- variant: "card",
- },
-});
diff --git a/web/src/components/design-system/Codeblock/Codeblock.tsx b/web/src/components/design-system/Codeblock/Codeblock.tsx
index 36dd7b605d..0ced7bc943 100644
--- a/web/src/components/design-system/Codeblock/Codeblock.tsx
+++ b/web/src/components/design-system/Codeblock/Codeblock.tsx
@@ -1,4 +1,3 @@
-import { cva, type VariantProps } from "class-variance-authority";
import { Button } from "@/src/components/ui/button";
import { copyTextToClipboard } from "@/src/utils/clipboard";
import { Check, Copy } from "lucide-react";
@@ -6,88 +5,74 @@ import { type FC, memo, useState } from "react";
import { Highlight, themes } from "prism-react-renderer";
import { useTheme } from "next-themes";
-const headerVariants = cva("flex w-full items-center justify-between px-2", {
- variants: {
- variant: {
- default: "bg-secondary",
- card: "bg-card",
- },
- },
- defaultVariants: {
- variant: "default",
- },
-});
-
-interface Props extends VariantProps<typeof headerVariants> {
+interface Props {
language: string;
value: string;
theme?: "light" | "dark";
}
-const CodeBlock: FC<Props> = memo(
- ({ language, value, theme, variant = "default" }) => {
- const [isCopied, setIsCopied] = useState(false);
- const { resolvedTheme } = useTheme();
- const appliedTheme = theme ?? resolvedTheme;
- const handleCopy = () => {
- setIsCopied(true);
- copyTextToClipboard(value ?? "");
- setTimeout(() => setIsCopied(false), 1000);
- };
+const CodeBlock: FC<Props> = memo(({ language, value, theme }) => {
+ const [isCopied, setIsCopied] = useState(false);
+ const { resolvedTheme } = useTheme();
+ const appliedTheme = theme ?? resolvedTheme;
+ const handleCopy = () => {
+ setIsCopied(true);
+ copyTextToClipboard(value ?? "");
+ setTimeout(() => setIsCopied(false), 1000);
+ };
- return (
- <div className="codeblock dark:bg-surface-code relative w-full overflow-hidden rounded border font-sans">
- <div className={headerVariants({ variant })}>
- <span className="text-xs lowercase">{language}</span>
- <div className="flex items-center py-1">
- <Button
- variant="ghost"
- size="xs"
- className="hover:bg-border text-xs focus-visible:ring-1 focus-visible:ring-offset-0"
- onClick={handleCopy}
- >
- {isCopied ? (
- <Check className="h-3 w-3" />
- ) : (
- <Copy className="h-3 w-3" />
- )}
- <span className="sr-only">Copy code</span>
- </Button>
- </div>
+ return (
+ <div className="codeblock bg-surface-code relative w-full overflow-hidden rounded border font-sans">
+ <div className="bg-surface-code-header flex w-full items-center justify-between px-2">
+ <span className="text-xs lowercase">{language}</span>
+ <div className="flex items-center py-1">
+ <Button
+ variant="ghost"
+ size="xs"
+ className="hover:bg-border text-xs focus-visible:ring-1 focus-visible:ring-offset-0"
+ onClick={handleCopy}
+ >
+ {isCopied ? (
+ <Check className="h-3 w-3" />
+ ) : (
+ <Copy className="h-3 w-3" />
+ )}
+ <span className="sr-only">Copy code</span>
+ </Button>
</div>
- <Highlight
- theme={appliedTheme === "dark" ? themes.vsDark : themes.github}
- code={value}
- language={language}
- >
- {({ className, style, tokens, getLineProps, getTokenProps }) => (
- <pre
- className={className}
- style={{
- ...style,
- margin: 0,
- width: "100%",
- background: "transparent",
- padding: "0.5rem",
- fontSize: "0.75rem",
- fontFamily: "var(--font-mono)",
- overflow: "auto",
- }}
- >
- {tokens.map((line, i) => (
- <div key={i} {...getLineProps({ line })}>
- {line.map((token, key) => (
- <span key={key} {...getTokenProps({ token })} />
- ))}
- </div>
- ))}
- </pre>
- )}
- </Highlight>
</div>
- );
- },
-);
+ <Highlight
+ theme={appliedTheme === "dark" ? themes.vsDark : themes.github}
+ code={value}
+ language={language}
+ >
+ {({ className, style, tokens, getLineProps, getTokenProps }) => (
+ <pre
+ className={className}
+ style={{
+ ...style,
+ margin: 0,
+ width: "100%",
+ background: "transparent",
+ padding: "0.5rem",
+ fontSize: "0.75rem",
+ fontFamily: "var(--font-mono)",
+ overflow: "auto",
+ }}
+ >
+ {tokens.map((line, i) => (
+ <div key={i} {...getLineProps({ line })}>
+ {line.map((token, key) => (
+ <span key={key} {...getTokenProps({ token })} />
+ ))}
+ </div>
+ ))}
+ </pre>
+ )}
+ </Highlight>
+ </div>
+ );
+});
CodeBlock.displayName = "CodeBlock";
export { CodeBlock };
diff --git a/web/src/components/trace/components/IOPreview/IOPreviewPretty.stories.tsx b/web/src/components/trace/components/IOPreview/IOPreviewPretty.stories.tsx
new file mode 100644
index 0000000000..7c4ab2b81c
--- /dev/null
+++ b/web/src/components/trace/components/IOPreview/IOPreviewPretty.stories.tsx
@@ -0,0 +1,146 @@
+import preview from "../../../../../.storybook/preview";
+import { IOPreviewPretty } from "./IOPreviewPretty";
+
+const meta = preview.meta({
+ component: IOPreviewPretty,
+ args: {
+ projectId: "storybook-project",
+ traceId: "storybook-trace",
+ // Keep this false: the correction editor uses private tRPC APIs and is not isolated for Storybook.
+ showCorrections: false,
+ },
+});
+
+export const Default = meta.story({
+ args: {
+ input: {
+ customerId: "customer-123",
+ question: "How do I reduce API latency?",
+ },
+ output: {
+ recommendation: "Enable prompt caching and stream the response.",
+ latencyMs: 842,
+ },
+ },
+});
+
+export const Chat = meta.story({
+ args: {
+ input: {
+ messages: [
+ {
+ role: "system",
+ content: "Answer questions using the product documentation.",
+ },
+ {
+ role: "user",
+ content: "How can I inspect a slow trace?",
+ },
+ ],
+ },
+ output: {
+ role: "assistant",
+ content:
+ "Open the trace detail and compare the **latency** of each observation.",
+ },
+ },
+});
+
+export const MarkdownWithCodeBlock = meta.story({
+ args: {
+ input: {
+ messages: [
+ {
+ role: "system",
+ content: `Use this tracing configuration:
+
+\`\`\`typescript
+const trace = langfuse.trace({
+ name: "support-workflow",
+});
+\`\`\``,
+ },
+ {
+ role: "user",
+ content: "How can I record a generation?",
+ },
+ ],
+ },
+ output: {
+ role: "assistant",
+ content: `Use the Langfuse SDK to wrap the model call:
+
+\`\`\`typescript
+const generation = trace.generation({
+ name: "support-answer",
+ model: "example-model",
+});
+
+generation.end({ output: "Your response" });
+\`\`\``,
+ },
+ },
+});
+
+export const ToolCall = meta.story({
+ args: {
+ input: {
+ messages: [
+ {
+ role: "system",
+ content: "Use tools when they can provide a precise answer.",
+ tools: [
+ {
+ name: "search_docs",
+ description: "Search the product documentation.",
+ parameters: {
+ type: "object",
+ properties: {
+ query: { type: "string" },
+ },
+ required: ["query"],
+ },
+ },
+ ],
+ },
+ {
+ role: "user",
+ content: "Where can I find trace latency?",
+ },
+ ],
+ },
+ output: {
+ role: "assistant",
+ tool_calls: [
+ {
+ id: "call-search-docs",
+ name: "search_docs",
+ arguments: JSON.stringify({ query: "trace latency" }),
+ },
+ ],
+ },
+ },
+});
+
+export const WithMetadata = meta.story({
+ args: {
+ input: "Summarize this support conversation.",
+ output: "The customer needs help configuring prompt caching.",
+ metadata: {
+ model: "example-model",
+ region: "eu-west-1",
+ cached: true,
+ },
+ showMetadata: true,
+ },
+});
+
+export const Loading = meta.story({
+ args: {
+ input: {
+ question: "How do I configure evaluations?",
+ },
+ output: null,
+ isLoading: true,
+ },
+});
diff --git a/web/src/components/trace/components/IOPreview/components/ChatMessage.tsx b/web/src/components/trace/components/IOPreview/components/ChatMessage.tsx
index 9e7ecdc2ba..1ecf8bb966 100644
--- a/web/src/components/trace/components/IOPreview/components/ChatMessage.tsx
+++ b/web/src/components/trace/components/IOPreview/components/ChatMessage.tsx
@@ -91,7 +91,6 @@ export function ChatMessage({
<MarkdownJsonView
title="Placeholder"
content={message.name || "Unnamed placeholder"}
- customCodeHeaderVariant="card"
/>
</div>
<div style={{ display: shouldRenderMarkdown ? "none" : "block" }}>
@@ -182,11 +181,6 @@ export function ChatMessage({
</>
);
- let customCodeHeaderVariant: "card" | undefined;
- if (message.role === "system") {
- customCodeHeaderVariant = "card";
- }
-
return (
<div className="hover:bg-muted transition-colors">
{/* Markdown view */}
@@ -194,7 +188,6 @@ export function ChatMessage({
<MarkdownJsonView
title={title}
content={message.content || ""}
- customCodeHeaderVariant={customCodeHeaderVariant}
audio={message.audio}
controlButtons={passthroughToggleButton}
afterHeader={thinkingBlocks}
diff --git a/web/src/components/ui/MarkdownJsonView.tsx b/web/src/components/ui/MarkdownJsonView.tsx
index 5458a2450e..47e9fa24a7 100644
--- a/web/src/components/ui/MarkdownJsonView.tsx
+++ b/web/src/components/ui/MarkdownJsonView.tsx
@@ -83,7 +83,6 @@ export function MarkdownJsonView({
title,
titleIcon,
className,
- customCodeHeaderVariant,
audio,
media,
controlButtons,
@@ -94,7 +93,6 @@ export function MarkdownJsonView({
title?: string;
titleIcon?: React.ReactNode;
className?: string;
- customCodeHeaderVariant?: "card";
audio?: OpenAIOutputAudioType;
media?: MediaReturnType[];
controlButtons?: React.ReactNode;
@@ -123,7 +121,6 @@ export function MarkdownJsonView({
}
title={title}
titleIcon={titleIcon}
- customCodeHeaderVariant={customCodeHeaderVariant}
audio={audio}
media={media}
controlButtons={controlButtons}
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index ae10c0d1cc..37466c01a5 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -221,12 +221,10 @@ function MarkdownRenderer({
markdown,
theme,
className,
- customCodeHeaderVariant,
}: {
markdown: string;
theme?: string;
className?: string;
- customCodeHeaderVariant?: "card";
}) {
const promptReferenceProjectId = usePromptReferenceProjectId();
@@ -383,7 +381,6 @@ function MarkdownRenderer({
language={language}
value={codeContent}
theme={theme === "dark" ? "dark" : "light"}
- variant={customCodeHeaderVariant}
/>
) : (
// inline code
@@ -479,7 +476,6 @@ export function MarkdownView({
markdown,
title,
titleIcon,
- customCodeHeaderVariant,
audio,
media,
className,
@@ -490,7 +486,6 @@ export function MarkdownView({
markdown: string | z.infer<typeof OpenAIContentSchema>;
title?: string;
titleIcon?: React.ReactNode;
- customCodeHeaderVariant?: "card";
audio?: OpenAIOutputAudioType;
media?: MediaReturnType[];
className?: string;
@@ -503,7 +498,8 @@ export function MarkdownView({
isSystemPrompt?: boolean;
}) {
const capture = usePostHogClientCapture();
- const { resolvedTheme: theme } = useTheme();
+ const { forcedTheme, resolvedTheme } = useTheme();
+ const theme = forcedTheme ?? resolvedTheme;
const { setIsMarkdownEnabled } = useMarkdownContext();
const markdownContent =
@@ -601,7 +597,6 @@ export function MarkdownView({
<MarkdownRenderer
markdown={isCollapsed ? truncatedContent : markdown}
theme={theme}
- customCodeHeaderVariant={customCodeHeaderVariant}
/>
{collapseToggle}
</>
@@ -610,11 +605,7 @@ export function MarkdownView({
// content parts (multi-modal); collapsed = preview of the joined text
<>
{isCollapsed ? (
- <MarkdownRenderer
- markdown={truncatedContent}
- theme={theme}
- customCodeHeaderVariant={customCodeHeaderVariant}
- />
+ <MarkdownRenderer markdown={truncatedContent} theme={theme} />
) : (
(markdown ?? []).map((content, index) => {
if (isOpenAITextContentPart(content)) {
@@ -623,7 +614,6 @@ export function MarkdownView({
key={index}
markdown={content.text}
theme={theme}
- customCodeHeaderVariant={customCodeHeaderVariant}
/>
);
}
@@ -672,7 +662,6 @@ export function MarkdownView({
<MarkdownRenderer
markdown={audio.transcript ? "[Audio] \n" + audio.transcript : ""}
theme={theme}
- customCodeHeaderVariant={customCodeHeaderVariant}
/>
<LangfuseMediaView
mediaReferenceString={audio.data.referenceString}
diff --git a/web/src/styles/globals.css b/web/src/styles/globals.css
index a427a429b7..5aed1bf5ae 100644
--- a/web/src/styles/globals.css
+++ b/web/src/styles/globals.css
@@ -65,6 +65,7 @@
--color-control-border: hsl(var(--control-border));
--color-foreground-tertiary: hsl(var(--foreground-tertiary));
--color-surface-code: hsl(var(--surface-code));
+ --color-surface-code-header: hsl(var(--surface-code-header));
--color-muted-magenta: hsl(var(--muted-magenta));
--color-muted-blue: hsl(var(--muted-blue));
--color-muted-green: hsl(var(--muted-green));
@@ -243,8 +244,9 @@
on white — flagged in review, kept per design preference for now. */
--foreground-tertiary: 215 12% 62%;
/* Code blocks sit a step BELOW their surrounding surface ("code is a
- well"), matching the docs site's --surface-code convention. */
- --surface-code: 0 0% 98%;
+ well"), matching the docs site's --surface-code convention. */
+ --surface-code: 0 0% 92%;
+ --surface-code-header: 0 0% 86%;
--popover: 0 0% 100%;
--popover-foreground: var(--foreground);
@@ -411,6 +413,7 @@
--muted-foreground: 0 0% 54%;
--foreground-tertiary: 0 0% 40%;
--surface-code: 0 0% 2.3%;
+ --surface-code-header: 0 0% 8.2%;
--popover: 0 0% 12%;
--popover-foreground: var(--foreground);