Skip to content

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

fix(sessions): Allow pretty printing JSON tool input / output (#18531)

Ben Bachem06eb4c50e88b
diff --git a/web/src/__tests__/codeblock-language.clienttest.ts b/web/src/__tests__/codeblock-language.clienttest.ts
new file mode 100644
index 0000000000..c5dafda64d
--- /dev/null
+++ b/web/src/__tests__/codeblock-language.clienttest.ts
@@ -0,0 +1,17 @@
+import { describe, expect, it } from "vitest";
+import { normalizeCodeblockLanguage } from "@/src/utils/normalizeCodeblockLanguage";
+
+describe("normalizeCodeblockLanguage", () => {
+ it("preserves supported languages and aliases", () => {
+ expect(normalizeCodeblockLanguage("typescript")).toBe("typescript");
+ expect(normalizeCodeblockLanguage("PYTHON")).toBe("python");
+ expect(normalizeCodeblockLanguage("shell")).toBe("shell");
+ expect(normalizeCodeblockLanguage("markup")).toBe("markup");
+ expect(normalizeCodeblockLanguage("tsx")).toBe("tsx");
+ });
+
+ it("falls back to plain text for unknown or missing language names", () => {
+ expect(normalizeCodeblockLanguage("unknown-language")).toBe("text");
+ expect(normalizeCodeblockLanguage("")).toBe("text");
+ });
+});
diff --git a/web/src/__tests__/format-json.clienttest.ts b/web/src/__tests__/format-json.clienttest.ts
new file mode 100644
index 0000000000..265c5cca8d
--- /dev/null
+++ b/web/src/__tests__/format-json.clienttest.ts
@@ -0,0 +1,27 @@
+import { describe, expect, it } from "vitest";
+import { formatJson } from "@/src/utils/formatJson";
+
+describe("formatJson", () => {
+ it("only changes whitespace, preserving numbers, escapes, and key order", () => {
+ const source =
+ '{"2":12345678901234567890,"1":1e400,"pattern":"\\\\u0061","nested":[{},[],"a, : { \\\"quoted\\\" }"],"unicode":"\\u4f60"}';
+ expect(formatJson(source)).toBe(
+ '{\n "2": 12345678901234567890,\n "1": 1e400,\n "pattern": "\\\\u0061",\n "nested": [\n {},\n [],\n "a, : { \\\"quoted\\\" }"\n ],\n "unicode": "\\u4f60"\n}',
+ );
+ });
+
+ it("formats JSON above the highlighting limit", () => {
+ const value = "x".repeat(10_001);
+ expect(formatJson(`{"value":"${value}"}`)).toBe(
+ `{\n "value": "${value}"\n}`,
+ );
+ });
+
+ it("keeps scalar JSON valid", () => {
+ expect(formatJson(" 12345678901234567890 ")).toBe("12345678901234567890");
+ });
+
+ it("does not format invalid JSON", () => {
+ expect(formatJson('{"broken":')).toBeNull();
+ });
+});
diff --git a/web/src/components/design-system/Codeblock/Codeblock.stories.tsx b/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
index cb09539a38..64111dbc64 100644
--- a/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
+++ b/web/src/components/design-system/Codeblock/Codeblock.stories.tsx
@@ -1,5 +1,5 @@
import preview from "../../../../.storybook/preview";
-import { expect } from "storybook/test";
+import { expect, spyOn, userEvent, within } from "storybook/test";
import { themes } from "prism-react-renderer";
import { Codeblock as CodeBlock } from "./Codeblock";
@@ -30,7 +30,7 @@ export const LightThemeTokenRendering = Default.extend({
name: "(Test) Light Theme Token Rendering",
play: async ({ args, canvasElement, canvas }) => {
const preview = canvasElement.querySelector("pre")!;
- await expect(preview).toHaveTextContent(args.value);
+ await expect(preview).toHaveTextContent(String(args.value));
const keyword = preview.querySelector(".token.keyword")!;
await expect(keyword).toHaveTextContent("const");
await expect(preview.querySelector(".token.string")).toHaveTextContent(
@@ -40,7 +40,14 @@ export const LightThemeTokenRendering = Default.extend({
const keywordStyle = theme.styles
.filter((rule) => {
if (!rule.types.includes("keyword")) return false;
- if (rule.languages && !rule.languages.includes(args.language)) {
+ if (
+ rule.languages &&
+ !rule.languages.includes(
+ typeof args.language === "string"
+ ? args.language
+ : (args.language?.value ?? "text"),
+ )
+ ) {
return false;
}
return true;
@@ -56,6 +63,160 @@ export const LightThemeTokenRendering = Default.extend({
},
});
+export const Json = meta.story({
+ name: "(Test) Copies Selected JSON Format",
+ args: {
+ label: "Example",
+ value: '{"items":[1,2],"message":"hello"}',
+ allowFormatting: true,
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const writeText = spyOn(
+ navigator.clipboard,
+ "writeText",
+ ).mockResolvedValue();
+ try {
+ const formattingButton = canvas.getByRole("button", {
+ name: "Pretty-print JSON",
+ });
+ await expect(formattingButton).toHaveAttribute("aria-pressed", "true");
+ await userEvent.click(canvas.getByRole("button", { name: "Copy code" }));
+ await expect(writeText).toHaveBeenLastCalledWith(
+ JSON.stringify(JSON.parse(String(args.value)), null, 2),
+ );
+ await expect(
+ canvas.getByRole("button", { name: "Copied" }),
+ ).toBeVisible();
+ await userEvent.click(formattingButton);
+ await expect(formattingButton).toHaveAttribute("aria-pressed", "false");
+ await userEvent.click(canvas.getByRole("button", { name: "Copied" }));
+ await expect(writeText).toHaveBeenLastCalledWith(args.value);
+ } finally {
+ writeText.mockRestore();
+ }
+ },
+});
+
+export const PlainText = meta.story({
+ args: {
+ label: "Example",
+ value: "hello world",
+ allowFormatting: true,
+ },
+});
+
+export const ConstrainedHeight = meta.story({
+ args: {
+ label: "Example",
+ value: { items: Array.from({ length: 100 }, (_, index) => index) },
+ allowFormatting: true,
+ },
+ render: (args) => (
+ <div className="grid max-h-48">
+ <CodeBlock {...args} />
+ </div>
+ ),
+});
+
+export const LargeValue = meta.story({
+ args: {
+ label: "Example",
+ value: "x".repeat(10_001),
+ allowFormatting: true,
+ },
+});
+
+export const LargeJson = meta.story({
+ name: "(Test) Formats Large JSON Without Highlighting",
+ args: {
+ label: "Example",
+ value: `{"value":"${"x".repeat(10_001)}"}`,
+ allowFormatting: true,
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const source = String(args.value);
+ await expect(
+ canvas.getByRole("button", { name: "Pretty-print JSON" }),
+ ).toHaveAttribute("aria-pressed", "true");
+ await expect(canvasElement.querySelector("pre")?.textContent).toBe(
+ `{\n "value": "${"x".repeat(10_001)}"\n}`,
+ );
+ await expect(canvasElement.querySelector("pre")?.childElementCount).toBe(0);
+ await userEvent.click(
+ canvas.getByRole("button", { name: "Pretty-print JSON" }),
+ );
+ await expect(canvasElement.querySelector("pre")?.textContent).toBe(source);
+ },
+});
+
+export const LosslessJson = meta.story({
+ name: "(Test) Copies Exact JSON Tokens In Both Formats",
+ args: {
+ value: '{"2":12345678901234567890,"1":"\\\\u0061"}',
+ allowFormatting: true,
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const writeText = spyOn(
+ navigator.clipboard,
+ "writeText",
+ ).mockResolvedValue();
+ try {
+ await userEvent.click(canvas.getByRole("button", { name: "Copy code" }));
+ await expect(writeText).toHaveBeenLastCalledWith(
+ '{\n "2": 12345678901234567890,\n "1": "\\\\u0061"\n}',
+ );
+ await userEvent.click(
+ canvas.getByRole("button", { name: "Pretty-print JSON" }),
+ );
+ await userEvent.click(canvas.getByRole("button", { name: "Copied" }));
+ await expect(writeText).toHaveBeenLastCalledWith(args.value);
+ } finally {
+ writeText.mockRestore();
+ }
+ },
+});
+
+export const ReadOnlyPlainText = ReadOnly.extend({
+ args: {
+ language: "text",
+ value:
+ "This intentionally long first line should scroll horizontally and remain clear of the copy control. ".repeat(
+ 8,
+ ),
+ },
+});
+
+export const UnsupportedLanguageCaption = meta.story({
+ args: {
+ language: { value: "text", label: "java" },
+ value: "public class Example {}",
+ },
+});
+
+export const InvalidJson = meta.story({
+ name: "(Test) Does Not Offer Formatting For Invalid JSON",
+ args: {
+ label: "Example",
+ value: '{"broken":',
+ allowFormatting: true,
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ await userEvent.click(
+ canvas.getByRole("combobox", { name: "Example language" }),
+ );
+ await userEvent.click(
+ within(document.body).getByRole("option", { name: "JSON" }),
+ );
+ await expect(
+ canvas.queryByRole("button", { name: "Pretty-print JSON" }),
+ ).not.toBeInTheDocument();
+ },
+});
+
export const DarkThemeTokenRendering = LightThemeTokenRendering.extend({
name: "(Test) Dark Theme Token Rendering",
args: { theme: "dark" },
diff --git a/web/src/components/design-system/Codeblock/Codeblock.tsx b/web/src/components/design-system/Codeblock/Codeblock.tsx
index ae4eb3e8f0..d0a2f0be4b 100644
--- a/web/src/components/design-system/Codeblock/Codeblock.tsx
+++ b/web/src/components/design-system/Codeblock/Codeblock.tsx
@@ -1,13 +1,85 @@
-import { IconButton } from "@/src/components/design-system/IconButton/IconButton";
-import { copyTextToClipboard } from "@/src/utils/clipboard";
-import { Check, Copy } from "lucide-react";
-import { type FC, memo, useState } from "react";
+import { memo, useMemo, useState } from "react";
+import { Check, Copy, ListTree } from "lucide-react";
+import hljs from "highlight.js/lib/core";
+import json from "highlight.js/lib/languages/json";
+import javascript from "highlight.js/lib/languages/javascript";
+import typescript from "highlight.js/lib/languages/typescript";
+import python from "highlight.js/lib/languages/python";
+import sql from "highlight.js/lib/languages/sql";
+import yaml from "highlight.js/lib/languages/yaml";
+import xml from "highlight.js/lib/languages/xml";
+import css from "highlight.js/lib/languages/css";
+import markdown from "highlight.js/lib/languages/markdown";
import { SyntaxHighlight } from "@/src/components/design-system/SyntaxHighlight/SyntaxHighlight";
+import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";
+import { IconButton } from "@/src/components/design-system/IconButton/IconButton";
+import { SelectDropdown } from "@/src/components/design-system/SelectDropdown/SelectDropdown";
+import { Tooltip } from "@/src/components/design-system/Tooltip/Tooltip";
+import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";
import { cn } from "@/src/utils/tailwind";
+import { formatJson } from "@/src/utils/formatJson";
+import { normalizeCodeblockLanguage } from "@/src/utils/normalizeCodeblockLanguage";
+
+export type CodeblockLanguage =
+ | "plain"
+ | "plaintext"
+ | "text"
+ | "txt"
+ | "markup"
+ | "html"
+ | "mathml"
+ | "svg"
+ | "xml"
+ | "ssml"
+ | "atom"
+ | "rss"
+ | "regex"
+ | "clike"
+ | "javascript"
+ | "js"
+ | "actionscript"
+ | "coffeescript"
+ | "coffee"
+ | "javadoclike"
+ | "css"
+ | "yaml"
+ | "yml"
+ | "markdown"
+ | "md"
+ | "graphql"
+ | "sql"
+ | "typescript"
+ | "ts"
+ | "jsdoc"
+ | "flow"
+ | "n4js"
+ | "n4jsd"
+ | "jsx"
+ | "tsx"
+ | "swift"
+ | "kotlin"
+ | "kt"
+ | "kts"
+ | "c"
+ | "objectivec"
+ | "objc"
+ | "reason"
+ | "rust"
+ | "go"
+ | "cpp"
+ | "python"
+ | "py"
+ | "json"
+ | "webmanifest"
+ | "bash"
+ | "shell";
-interface Props {
- language: string;
- value: string;
+interface CodeblockProps {
+ /** Use a labelled grammar when the source language has no matching grammar. */
+ language?: CodeblockLanguage | { value: CodeblockLanguage; label: string };
+ value: unknown;
+ label?: string;
+ allowFormatting?: boolean;
theme?: "light" | "dark";
borderless?: boolean;
/** Hide the language caption when the surrounding UI already states it. */
@@ -16,65 +88,207 @@ interface Props {
variant?: "default" | "read-only";
}
-const CodeBlock: FC<Props> = memo(
- ({
- language,
- value,
- theme,
- borderless = false,
- showLanguage = true,
- variant = "default",
- }) => {
- const [isCopied, setIsCopied] = useState(false);
- const handleCopy = () => {
- setIsCopied(true);
- copyTextToClipboard(value ?? "");
- setTimeout(() => setIsCopied(false), 1000);
- };
+const MAX_HIGHLIGHT_CHARACTERS = 10_000;
+const languageOptions = [
+ { value: "json", label: "JSON", grammar: json },
+ { value: "javascript", label: "JavaScript", grammar: javascript },
+ { value: "typescript", label: "TypeScript", grammar: typescript },
+ { value: "python", label: "Python", grammar: python },
+ { value: "sql", label: "SQL", grammar: sql },
+ { value: "yaml", label: "YAML", grammar: yaml },
+ { value: "xml", label: "HTML / XML", grammar: xml },
+ { value: "css", label: "CSS", grammar: css },
+ { value: "markdown", label: "Markdown", grammar: markdown },
+] as const;
+
+// Prism handles rendering but has no autodetection; only load detection grammars here.
+const detector = hljs.newInstance();
+for (const { value, grammar } of languageOptions) {
+ detector.registerLanguage(value, grammar);
+}
- const copyButton = (
- <IconButton
- icon={isCopied ? Check : Copy}
- label="Copy code"
- size="xs"
- variant={variant === "read-only" ? "ghost" : "subtle"}
- onClick={handleCopy}
- />
+export const Codeblock = memo(function Codeblock({
+ language: languageConfig,
+ value,
+ label = "Code",
+ allowFormatting = false,
+ theme,
+ borderless = false,
+ showLanguage = true,
+ variant = "default",
+}: CodeblockProps) {
+ const configuredLanguage =
+ typeof languageConfig === "string" ? languageConfig : languageConfig?.value;
+ const languageLabel =
+ typeof languageConfig === "string" ? undefined : languageConfig?.label;
+ const [selectedLanguage, setSelectedLanguage] = useState<
+ "auto" | CodeblockLanguage
+ >("auto");
+ const [format, setFormat] = useState<"original" | "pretty">("pretty");
+ const { copy, isCopied } = useCopyToClipboard();
+ const text = useMemo(() => {
+ if (typeof value === "string") return value;
+ return JSON.stringify(value, undefined, 2) ?? String(value);
+ }, [value]);
+ const formattedJson = useMemo(() => {
+ if (!allowFormatting) return null;
+ return formatJson(text);
+ }, [allowFormatting, text]);
+ const isLargePreview = text.length > MAX_HIGHLIGHT_CHARACTERS;
+ const detectedLanguage = useMemo(() => {
+ if (configuredLanguage !== undefined) return configuredLanguage;
+ if (formattedJson !== null) return "json";
+ if (isLargePreview) return "text";
+ if (typeof value !== "string") return "json";
+ return normalizeCodeblockLanguage(
+ detector.highlightAuto(text).language ?? "text",
);
+ }, [configuredLanguage, formattedJson, isLargePreview, value, text]);
+ const language =
+ configuredLanguage ??
+ (selectedLanguage === "auto" ? detectedLanguage : selectedLanguage);
+ const prettyPrintedText = language === "json" ? formattedJson : null;
+ const displayText =
+ format === "pretty" && prettyPrintedText !== null
+ ? prettyPrintedText
+ : text;
+ const detectedLabel =
+ languageOptions.find((option) => option.value === detectedLanguage)
+ ?.label ?? "Plain text";
+ const bypassHighlighting =
+ isLargePreview ||
+ displayText.length > MAX_HIGHLIGHT_CHARACTERS ||
+ language === "text";
+ const codePadding =
+ variant === "read-only" ? "0.75rem 2.5rem 0.75rem 0.75rem" : "0.75rem";
+ const codeClassName = cn(
+ "min-h-0 overflow-auto font-mono text-xs",
+ variant === "read-only"
+ ? "whitespace-pre"
+ : "break-all whitespace-pre-wrap",
+ );
- return (
- <div
- className={cn(
- "codeblock relative w-full overflow-hidden rounded font-sans",
- !borderless && "border",
- variant === "read-only" ? "bg-muted" : "bg-surface-code",
- )}
- >
- {variant === "default" ? (
- <div className="bg-surface-code-header flex w-full items-center justify-between px-2">
- {showLanguage ? (
- <span className="text-xs lowercase">{language}</span>
- ) : null}
- <div className="flex items-center py-1">{copyButton}</div>
- </div>
- ) : (
- <div className="text-muted-foreground absolute top-1.5 right-1.5">
- {copyButton}
- </div>
- )}
- <SyntaxHighlight theme={theme} code={value} language={language}>
+ const handleCopy = () =>
+ copy(displayText).catch((error: unknown) => {
+ console.error("Failed to copy code", error);
+ });
+ const handleFormatToggle = () => {
+ setFormat((previous) => (previous === "pretty" ? "original" : "pretty"));
+ };
+ const handleLanguageChange = (language: string) => {
+ if (language === "auto") {
+ setSelectedLanguage("auto");
+ return;
+ }
+ setSelectedLanguage(normalizeCodeblockLanguage(language));
+ };
+
+ return (
+ <div
+ className={cn(
+ "codeblock relative flex min-h-0 w-full min-w-0 flex-col overflow-hidden rounded-md font-sans",
+ !borderless && "border",
+ variant === "read-only" ? "bg-muted" : "bg-muted/30",
+ )}
+ >
+ {variant === "read-only" ? (
+ <div className="text-muted-foreground absolute top-1.5 right-1.5">
+ <IconButton
+ icon={isCopied ? Check : Copy}
+ label="Copy code"
+ size="xs"
+ variant="ghost"
+ onClick={handleCopy}
+ />
+ </div>
+ ) : (
+ <div className="flex shrink-0 flex-wrap items-center justify-end gap-2 border-b px-2 py-1">
+ {configuredLanguage !== undefined && showLanguage ? (
+ <span className="text-muted-foreground inline-flex items-center rounded px-1.5 py-1 text-xs">
+ {languageLabel ??
+ languageOptions.find(
+ (option) => option.value === configuredLanguage,
+ )?.label ??
+ configuredLanguage}
+ </span>
+ ) : null}
+ {configuredLanguage === undefined ? (
+ <SelectDropdown
+ aria-label={`${label} language`}
+ value={selectedLanguage}
+ onValueChange={handleLanguageChange}
+ options={[
+ { value: "auto", label: `Auto (${detectedLabel})` },
+ { value: "text", label: "Plain text" },
+ ...languageOptions.map(({ value, label }) => ({
+ value,
+ label,
+ })),
+ ]}
+ >
+ {({ getTriggerProps, selectedLabel }) => (
+ <button
+ {...getTriggerProps()}
+ className="text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:ring-ring inline-flex items-center gap-1 rounded px-1.5 py-1 text-xs focus-visible:ring-2 focus-visible:outline-hidden"
+ >
+ {selectedLabel}
+ <DropdownIndicator />
+ </button>
+ )}
+ </SelectDropdown>
+ ) : null}
+ {isLargePreview || displayText.length > MAX_HIGHLIGHT_CHARACTERS ? (
+ <span
+ className="text-muted-foreground text-xs"
+ title="Syntax highlighting is disabled for previews over 10,000 characters."
+ >
+ Plain text (large value)
+ </span>
+ ) : null}
+ {prettyPrintedText !== null ? (
+ <Tooltip label="Pretty-print JSON">
+ {({ getTriggerProps }) => (
+ <IconButton
+ {...getTriggerProps()}
+ label="Pretty-print JSON"
+ icon={ListTree}
+ variant="toolbar"
+ size="sm"
+ aria-pressed={format === "pretty"}
+ onClick={handleFormatToggle}
+ />
+ )}
+ </Tooltip>
+ ) : null}
+ <Tooltip label={isCopied ? "Copied" : "Copy code"}>
+ {({ getTriggerProps }) => (
+ <IconButton
+ {...getTriggerProps()}
+ label={isCopied ? "Copied" : "Copy code"}
+ icon={isCopied ? Check : Copy}
+ variant="toolbar"
+ size="sm"
+ onClick={handleCopy}
+ />
+ )}
+ </Tooltip>
+ </div>
+ )}
+ {bypassHighlighting ? (
+ <pre className={codeClassName} style={{ padding: codePadding }}>
+ {displayText}
+ </pre>
+ ) : (
+ <SyntaxHighlight theme={theme} code={displayText} language={language}>
{({ className, style, content }) => (
<pre
- className={className}
+ className={cn(className, codeClassName)}
style={{
...style,
margin: 0,
width: "100%",
background: "transparent",
- padding:
- variant === "read-only"
- ? "0.75rem 2.5rem 0.75rem 0.75rem"
- : "0.5rem",
+ padding: codePadding,
fontSize: "0.75rem",
fontFamily: "var(--font-mono)",
overflow: "auto",
@@ -84,10 +298,7 @@ const CodeBlock: FC<Props> = memo(
</pre>
)}
</SyntaxHighlight>
- </div>
- );
- },
-);
-CodeBlock.displayName = "CodeBlock";
-
-export { CodeBlock as Codeblock };
+ )}
+ </div>
+ );
+});
diff --git a/web/src/components/design-system/IconButton/IconButton.tsx b/web/src/components/design-system/IconButton/IconButton.tsx
index 7cb5a0c0ad..44dc9b7d97 100644
--- a/web/src/components/design-system/IconButton/IconButton.tsx
+++ b/web/src/components/design-system/IconButton/IconButton.tsx
@@ -1,6 +1,7 @@
import { cva } from "class-variance-authority";
import { type LucideIcon } from "lucide-react";
import { type ComponentProps, type Ref } from "react";
+import { cn } from "@/src/utils/tailwind";
const buttonVariants = cva(
"inline-flex items-center justify-center rounded-md ring-offset-background transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
@@ -11,6 +12,8 @@ const buttonVariants = cva(
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
subtle: "hover:bg-border aria-expanded:bg-border",
+ toolbar:
+ "text-muted-foreground hover:bg-accent hover:text-foreground aria-pressed:text-primary rounded-sm",
},
size: {
xs: "size-4 rounded-sm",
@@ -48,7 +51,7 @@ type IconButtonProps = NativeButtonProps & {
label: string;
ref?: Ref<HTMLButtonElement>;
size?: "xs" | "sm" | "md";
- variant?: "ghost" | "outline" | "subtle";
+ variant?: "ghost" | "outline" | "subtle" | "toolbar";
};
export function IconButton({
@@ -64,11 +67,17 @@ export function IconButton({
<button
{...buttonProps}
aria-label={label}
- className={buttonVariants({ size, variant })}
+ className={cn(buttonVariants({ size, variant }))}
ref={ref}
type={type}
>
- <Icon className={iconVariants({ size })} aria-hidden />
+ <Icon
+ className={cn(
+ iconVariants({ size }),
+ variant === "toolbar" && "icon-sm text-current",
+ )}
+ aria-hidden
+ />
</button>
);
}
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index 0ded7e7d63..59edbfb445 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -18,6 +18,7 @@ import ReactMarkdown, {
} from "react-markdown";
import remarkGfm from "remark-gfm";
import { Codeblock as CodeBlock } from "@/src/components/design-system/Codeblock/Codeblock";
+import { normalizeCodeblockLanguage } from "@/src/utils/normalizeCodeblockLanguage";
import { useTheme } from "next-themes";
import { ImageOff, Info } from "lucide-react";
import { MentionBadge } from "@/src/features/comments/components/MentionBadge";
@@ -242,12 +243,17 @@ function MarkdownCode({
const theme = forcedTheme ?? resolvedTheme;
const languageMatch = /language-(\w+)/.exec(className || "");
const language = languageMatch ? languageMatch[1] : "";
+ const codeLanguage = normalizeCodeblockLanguage(language);
const codeContent = String(children).replace(/\n$/, "");
const isMultiLine = codeContent.includes("\n");
return language || isMultiLine ? (
<CodeBlock
- language={language}
+ language={
+ codeLanguage === "text"
+ ? { value: codeLanguage, label: language }
+ : codeLanguage
+ }
value={codeContent}
theme={theme === "dark" ? "dark" : "light"}
/>
diff --git a/web/src/features/evals/v2/components/Evaluators/EvaluatorVersionHistorySheet/components/EvaluatorDefinitionView/EvaluatorDefinitionView.tsx b/web/src/features/evals/v2/components/Evaluators/EvaluatorVersionHistorySheet/components/EvaluatorDefinitionView/EvaluatorDefinitionView.tsx
index 14529dfbf4..1ff8609ea1 100644
--- a/web/src/features/evals/v2/components/Evaluators/EvaluatorVersionHistorySheet/components/EvaluatorDefinitionView/EvaluatorDefinitionView.tsx
+++ b/web/src/features/evals/v2/components/Evaluators/EvaluatorVersionHistorySheet/components/EvaluatorDefinitionView/EvaluatorDefinitionView.tsx
@@ -9,6 +9,8 @@ import {
} from "@langfuse/shared";
import { Codeblock as CodeBlock } from "@/src/components/design-system/Codeblock/Codeblock";
+import { normalizeCodeblockLanguage } from "@/src/utils/normalizeCodeblockLanguage";
+import { sourceCodeLanguageLabel } from "@/src/features/evals/v2/fns/evaluators/sourceCodeLanguageLabel";
import { Badge } from "@/src/components/ui/badge";
import { Label } from "@/src/components/ui/label";
import { MediaReferenceTag } from "@/src/components/ui/media/MediaReferenceTag";
@@ -26,7 +28,6 @@ import { VariableMapping } from "@/src/features/evals/v2/components/VariableMapp
import { DecisionModelQuestionSummary } from "@/src/features/evals/v2/components/Evaluators/DecisionModel/DecisionModelQuestionSummary/DecisionModelQuestionSummary";
import { evalVariableColumnLabel } from "@/src/features/evals/v2/fns/variableMapping/evalVariableColumnLabel";
import { formatMappingLabel } from "@/src/features/evals/v2/fns/variableMapping/segmentsToJsonPath";
-import { sourceCodeLanguageLabel } from "@/src/features/evals/v2/fns/evaluators/sourceCodeLanguageLabel";
import { toScoreOutputFormState } from "@/src/features/evals/v2/fns/scoreOutput/toScoreOutputFormState";
import type { JudgeModel } from "@/src/features/evals/v2/judgeModel";
@@ -87,7 +88,9 @@ function CodeEvaluatorDefinitionView({
<CodeBlock
language={
sourceCodeLanguage
- ? sourceCodeLanguageLabel(sourceCodeLanguage).toLowerCase()
+ ? normalizeCodeblockLanguage(
+ sourceCodeLanguageLabel(sourceCodeLanguage),
+ )
: "text"
}
value={sourceCode ?? ""}
diff --git a/web/src/features/in-app-agent/components/InAppAgentToolCallCard.stories.tsx b/web/src/features/in-app-agent/components/InAppAgentToolCallCard.stories.tsx
index d7ee5ea74f..abd2a4b170 100644
--- a/web/src/features/in-app-agent/components/InAppAgentToolCallCard.stories.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentToolCallCard.stories.tsx
@@ -282,9 +282,6 @@ export const EvaluatorToolCall = meta.story({
const canvas = await showToolCall(context);
await expect(canvas.getByText("sourceCode", { exact: true })).toBeVisible();
await expect(canvas.getByText("versions[0].sourceCode")).toBeVisible();
- await expect(
- canvas.getAllByRole("button", { name: "Copy code" }),
- ).toHaveLength(2);
await userEvent.click(
canvas.getAllByRole("button", { name: "View JSON" })[1],
);
@@ -323,9 +320,6 @@ export const EvaluatorToolCall = meta.story({
selectedCode.getByRole("button", { name: "Copy code" }),
);
await expect(copy).toHaveBeenLastCalledWith(toolErrorRateSource);
- await expect(
- canvas.getAllByRole("button", { name: "Copy code" }),
- ).toHaveLength(2);
} finally {
copy.mockRestore();
}
diff --git a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/SessionConversationTimelineTrace.stories.tsx b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/SessionConversationTimelineTrace.stories.tsx
index 160cf90734..beaf0be21f 100644
--- a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/SessionConversationTimelineTrace.stories.tsx
+++ b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/SessionConversationTimelineTrace.stories.tsx
@@ -347,8 +347,6 @@ export const ToolPreviewJsonHighlighting = meta.story({
controls.getByRole("combobox", { name: "Tool output language" }),
).toHaveTextContent("Auto (JSON)");
const [inputPreview, outputPreview] = row.querySelectorAll("pre");
- await expect(inputPreview).toHaveClass("max-h-48");
- await expect(outputPreview).toHaveClass("max-h-96");
await expect(
inputPreview.querySelector(".token.property"),
).toHaveTextContent('"query"');
@@ -392,12 +390,10 @@ export const LargeToolPreviews = meta.story({
? output
: JSON.stringify(output, undefined, 2);
await expect(outputPreview.textContent).toBe(expectedOutput);
- await expect(inputPreview).toHaveClass("max-h-48");
- await expect(outputPreview).toHaveClass("max-h-96");
await expect(outputPreview.childElementCount).toBe(0);
await expect(
- controls.queryByRole("combobox", { name: "Tool output language" }),
- ).toBeNull();
+ controls.getByRole("combobox", { name: "Tool output language" }),
+ ).toBeVisible();
if (name === "highlight-limit") {
await expect(expectedInput.length).toBe(10_000);
@@ -414,10 +410,6 @@ export const LargeToolPreviews = meta.story({
} else {
await expect(inputPreview.textContent).toBe(expectedInput);
await expect(inputPreview.childElementCount).toBe(0);
- await expect(controls.queryByRole("combobox")).toBeNull();
- await expect(
- controls.getAllByText("Plain text (large value)"),
- ).toHaveLength(2);
}
}
},
diff --git a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/components/SessionTranscriptContent/components/SessionTimelineToolRow/SessionTimelineToolRow.tsx b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/components/SessionTranscriptContent/components/SessionTimelineToolRow/SessionTimelineToolRow.tsx
index db828c479e..088356af8d 100644
--- a/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/components/SessionTranscriptContent/components/SessionTimelineToolRow/SessionTimelineToolRow.tsx
+++ b/web/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/components/SessionTranscriptContent/components/SessionTimelineToolRow/SessionTimelineToolRow.tsx
@@ -1,126 +1,12 @@
import { CircleAlert } from "lucide-react";
-import { type ReactNode, useMemo, useState } from "react";
-import hljs from "highlight.js/lib/core";
-import json from "highlight.js/lib/languages/json";
-import javascript from "highlight.js/lib/languages/javascript";
-import typescript from "highlight.js/lib/languages/typescript";
-import python from "highlight.js/lib/languages/python";
-import sql from "highlight.js/lib/languages/sql";
-import yaml from "highlight.js/lib/languages/yaml";
-import xml from "highlight.js/lib/languages/xml";
-import css from "highlight.js/lib/languages/css";
-import markdown from "highlight.js/lib/languages/markdown";
-import { SyntaxHighlight } from "@/src/components/design-system/SyntaxHighlight/SyntaxHighlight";
-import { SelectDropdown } from "@/src/components/design-system/SelectDropdown/SelectDropdown";
-import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";
+import { type ReactNode } from "react";
+import { Codeblock } from "@/src/components/design-system/Codeblock/Codeblock";
import { renderFilterIcon } from "@/src/components/ItemBadge";
import { SessionTimelineCollapsibleRow } from "@/src/features/sessions/components/ConnectedModernSessionBodyTimeline/components/ConnectedSessionConversationTimeline/components/SessionConversationalView/components/SessionConversationTimeline/components/SessionConversationTimelineTrace/components/SessionTranscriptContent/components/SessionTimelineCollapsibleRow/SessionTimelineCollapsibleRow";
-import { decodeUnicodeEscapesOnly } from "@/src/utils/unicode";
-import { cn } from "@/src/utils/tailwind";
-
-const toPreviewText = (value: unknown) => {
- const text =
- typeof value === "string"
- ? value
- : (JSON.stringify(value, undefined, 2) ?? String(value));
- return decodeUnicodeEscapesOnly(text, true);
-};
const hasPreviewValue = (value: unknown) =>
value !== null && value !== undefined && value !== "";
-const MAX_HIGHLIGHT_CHARACTERS = 10_000;
-
-const languageOptions = [
- { value: "json", label: "JSON", grammar: json },
- { value: "javascript", label: "JavaScript", grammar: javascript },
- { value: "typescript", label: "TypeScript", grammar: typescript },
- { value: "python", label: "Python", grammar: python },
- { value: "sql", label: "SQL", grammar: sql },
- { value: "yaml", label: "YAML", grammar: yaml },
- { value: "xml", label: "HTML / XML", grammar: xml },
- { value: "css", label: "CSS", grammar: css },
- { value: "markdown", label: "Markdown", grammar: markdown },
-];
-
-// Prism handles rendering but has no autodetection; only load detection grammars here.
-const detector = hljs.newInstance();
-for (const { value, grammar } of languageOptions) {
- detector.registerLanguage(value, grammar);
-}
-
-function SessionTimelineToolPreview({
- value,
- kind,
-}: {
- value: unknown;
- kind: "input" | "output";
-}) {
- const [selectedLanguage, setSelectedLanguage] = useState("auto");
- const text = useMemo(() => toPreviewText(value), [value]);
- const isLargePreview = text.length > MAX_HIGHLIGHT_CHARACTERS;
- const detectedLanguage = useMemo(() => {
- if (isLargePreview) return "text";
- if (typeof value !== "string") return "json";
- return detector.highlightAuto(text).language ?? "text";
- }, [isLargePreview, value, text]);
- const language =
- selectedLanguage === "auto" ? detectedLanguage : selectedLanguage;
- const detectedLabel =
- languageOptions.find((option) => option.value === detectedLanguage)
- ?.label ?? "Plain text";
- const previewClassName = cn(
- "bg-muted/30 overflow-auto rounded-md border p-3 font-mono text-xs break-all whitespace-pre-wrap",
- kind === "input" ? "max-h-48" : "max-h-96",
- );
-
- return (
- <div className="relative flex min-w-0 flex-col gap-1">
- <div className="flex items-center justify-between gap-2">
- <span className="text-muted-foreground font-mono text-[10px] font-bold uppercase">
- {kind === "input" ? "Input" : "Output"}
- </span>
- {isLargePreview ? (
- <span
- className="text-muted-foreground px-1.5 py-1 text-xs"
- title="Syntax highlighting is disabled for previews over 10,000 characters."
- >
- Plain text (large value)
- </span>
- ) : (
- <SelectDropdown
- aria-label={`Tool ${kind} language`}
- value={selectedLanguage}
- onValueChange={setSelectedLanguage}
- options={[
- { value: "auto", label: `Auto (${detectedLabel})` },
- { value: "text", label: "Plain text" },
- ...languageOptions.map(({ value, label }) => ({ value, label })),
- ]}
- >
- {({ getTriggerProps, selectedLabel }) => (
- <button
- {...getTriggerProps()}
- className="text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:ring-ring inline-flex items-center gap-1 rounded px-1.5 py-1 text-xs focus-visible:ring-2 focus-visible:outline-hidden"
- >
- {selectedLabel}
- <DropdownIndicator />
- </button>
- )}
- </SelectDropdown>
- )}
- </div>
- {isLargePreview || language === "text" ? (
- <pre className={previewClassName}>{text}</pre>
- ) : (
- <SyntaxHighlight code={text} language={language}>
- {({ content }) => <pre className={previewClassName}>{content}</pre>}
- </SyntaxHighlight>
- )}
- </div>
- );
-}
-
export function SessionTimelineToolRow({
name,
input,
@@ -159,10 +45,24 @@ export function SessionTimelineToolRow({
>
<div className="flex min-w-0 flex-col gap-3 pl-[22px]">
{hasPreviewValue(input) ? (
- <SessionTimelineToolPreview kind="input" value={input} />
+ <div className="flex min-w-0 flex-col gap-1">
+ <span className="text-muted-foreground font-mono text-[10px] font-bold uppercase">
+ Input
+ </span>
+ <div className="grid max-h-48 min-w-0">
+ <Codeblock label="Tool input" value={input} allowFormatting />
+ </div>
+ </div>
) : null}
{hasPreviewValue(output) ? (
- <SessionTimelineToolPreview kind="output" value={output} />
+ <div className="flex min-w-0 flex-col gap-1">
+ <span className="text-muted-foreground font-mono text-[10px] font-bold uppercase">
+ Output
+ </span>
+ <div className="grid max-h-96 min-w-0">
+ <Codeblock label="Tool output" value={output} allowFormatting />
+ </div>
+ </div>
) : null}
{!hasPreviewValue(input) && !hasPreviewValue(output) ? (
<div className="relative">
diff --git a/web/src/features/skills/components/SkillFilePreview.stories.tsx b/web/src/features/skills/components/SkillFilePreview.stories.tsx
index 926ec4f141..8589650969 100644
--- a/web/src/features/skills/components/SkillFilePreview.stories.tsx
+++ b/web/src/features/skills/components/SkillFilePreview.stories.tsx
@@ -96,7 +96,7 @@ export const InvalidFrontmatter = meta.story({
"Showing the original source",
);
await expect(canvasElement.querySelector("pre")?.textContent).toBe(
- args.content.replace(/\n/g, ""),
+ args.content,
);
},
});
diff --git a/web/src/features/skills/components/SkillFilePreview.tsx b/web/src/features/skills/components/SkillFilePreview.tsx
index 2f2bf9237f..426ec65241 100644
--- a/web/src/features/skills/components/SkillFilePreview.tsx
+++ b/web/src/features/skills/components/SkillFilePreview.tsx
@@ -1,5 +1,6 @@
import { isMap, parseDocument, stringify } from "yaml";
import { Codeblock } from "@/src/components/design-system/Codeblock/Codeblock";
+import { normalizeCodeblockLanguage } from "@/src/utils/normalizeCodeblockLanguage";
import { Tabs } from "@/src/components/design-system/Tabs/Tabs";
import { MarkdownView } from "@/src/components/ui/MarkdownViewer";
@@ -50,9 +51,15 @@ export function SkillFilePreview({
mdx: "text",
txt: "text",
};
+ const languageName = languageAliases[extension] ?? (extension || "text");
+ const language = normalizeCodeblockLanguage(languageName);
return (
<Codeblock
- language={languageAliases[extension] ?? (extension || "text")}
+ language={
+ language === "text"
+ ? { value: language, label: languageName }
+ : language
+ }
value={content}
/>
);
diff --git a/web/src/utils/formatJson.ts b/web/src/utils/formatJson.ts
new file mode 100644
index 0000000000..f7b1126424
--- /dev/null
+++ b/web/src/utils/formatJson.ts
@@ -0,0 +1,42 @@
+/** Indents valid JSON without changing number tokens, string escapes, or key order. */
+export function formatJson(text: string) {
+ try {
+ // Discard the parsed values: numbers may have lost precision during validation.
+ JSON.parse(text);
+ } catch {
+ return null;
+ }
+
+ const tokens =
+ text.match(/"(?:\\.|[^"\\])*"|[{}\[\],:]|[^\s{}\[\],:]+/g) ?? [];
+ const formatted: string[] = [];
+ let depth = 0;
+ for (const [index, token] of tokens.entries()) {
+ if (token === "{" || token === "[") {
+ formatted.push(token);
+ depth += 1;
+ if (tokens[index + 1] !== "}" && tokens[index + 1] !== "]") {
+ formatted.push("\n", " ".repeat(depth));
+ }
+ continue;
+ }
+ if (token === "}" || token === "]") {
+ depth -= 1;
+ if (tokens[index - 1] !== "{" && tokens[index - 1] !== "[") {
+ formatted.push("\n", " ".repeat(depth));
+ }
+ formatted.push(token);
+ continue;
+ }
+ if (token === ",") {
+ formatted.push(",\n", " ".repeat(depth));
+ continue;
+ }
+ if (token === ":") {
+ formatted.push(": ");
+ continue;
+ }
+ formatted.push(token);
+ }
+ return formatted.join("");
+}
diff --git a/web/src/utils/normalizeCodeblockLanguage.ts b/web/src/utils/normalizeCodeblockLanguage.ts
new file mode 100644
index 0000000000..82ca738b95
--- /dev/null
+++ b/web/src/utils/normalizeCodeblockLanguage.ts
@@ -0,0 +1,67 @@
+import type { CodeblockLanguage } from "@/src/components/design-system/Codeblock/Codeblock";
+
+// Prism's bundled grammars and aliases, plus shell labels used by existing callers.
+const supportedLanguages = [
+ "plain",
+ "plaintext",
+ "text",
+ "txt",
+ "markup",
+ "html",
+ "mathml",
+ "svg",
+ "xml",
+ "ssml",
+ "atom",
+ "rss",
+ "regex",
+ "clike",
+ "javascript",
+ "js",
+ "actionscript",
+ "coffeescript",
+ "coffee",
+ "javadoclike",
+ "css",
+ "yaml",
+ "yml",
+ "markdown",
+ "md",
+ "graphql",
+ "sql",
+ "typescript",
+ "ts",
+ "jsdoc",
+ "flow",
+ "n4js",
+ "n4jsd",
+ "jsx",
+ "tsx",
+ "swift",
+ "kotlin",
+ "kt",
+ "kts",
+ "c",
+ "objectivec",
+ "objc",
+ "reason",
+ "rust",
+ "go",
+ "cpp",
+ "python",
+ "py",
+ "json",
+ "webmanifest",
+ "bash",
+ "shell",
+] as const satisfies readonly CodeblockLanguage[];
+
+export function normalizeCodeblockLanguage(
+ language: string,
+): CodeblockLanguage {
+ return (
+ supportedLanguages.find(
+ (supported) => supported === language.toLowerCase(),
+ ) ?? "text"
+ );
+}