🪢 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-markdown): fallback to json view if markdown parsing is unsucessful (#5386)
marliessophie03e80bac7652
diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 6f9f6fc902..c745ebaf9c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -682,6 +682,15 @@ importers:remark-math:specifier: ^6.0.0version: 6.0.0+ remark-parse:+ specifier: ^11.0.0+ version: 11.0.0+ remark-rehype:+ specifier: ^11.1.1+ version: 11.1.1+ remark-stringify:+ specifier: ^11.0.0+ version: 11.0.0sonner:specifier: ^1.4.41@@ -697,6 +706,9 @@ importers:tailwindcss-animate:specifier: ^1.0.7+ unified:+ specifier: ^11.0.5+ version: 11.0.5use-query-params:specifier: ^2.2.1@@ -10193,8 +10205,8 @@ packages:resolution: {integrity: sha512-FCxlKLNGknS5ba/1lmpYijMUzX2esxW5xQqjWxw2eHFfS2MSdaHVINFmhjo+qN1WhZhNimq0dZATN9pH0IDrpA==}- resolution: {integrity: sha512-z3tJrAs2kIs1AqIIy6pzHmAHlF1hWQ+OdY4/hv+Wxe35EhyLKcajL33iUEn3ScxtFox9nUvRufR/Zre8Q08H/g==}+ resolution: {integrity: sha512-g/osARvjkBXb6Wo0XvAeXQohVta8i84ACbenPpoSsxTOQH/Ae0/RGP4WZgnMH5pMLpsj4FG7OHmcIcXxpza8eQ==}resolution: {integrity: sha512-1OSmLd3awB/t8qdoEOMazZkNsfVTeY4fTsgzcQFdXNq8ToTN4ZGwrMnlda4K6smTFKD+GRV6O48i6Z4iKgPPpw==}@@ -23383,7 +23395,7 @@ snapshots:mdast-util-to-hast: 13.2.0react: 18.2.0remark-parse: 11.0.0- remark-rehype: 11.1.0+ remark-rehype: 11.1.1unified: 11.0.5unist-util-visit: 5.0.0vfile: 6.0.1@@ -23647,7 +23659,7 @@ snapshots:transitivePeerDependencies:- supports-colordependencies:'@types/hast': 3.0.4'@types/mdast': 4.0.4diff --git a/web/package.json b/web/package.jsonindex 0cdf35ccd4..fc930e3ad7 100644--- a/web/package.json+++ b/web/package.json@@ -143,11 +143,15 @@"react18-json-view": "^0.2.8-canary.6","remark-gfm": "^4.0.0","remark-math": "^6.0.0",+ "remark-parse": "^11.0.0",+ "remark-rehype": "^11.1.1",+ "remark-stringify": "^11.0.0","sonner": "^1.4.41","stripe": "^17.4.0","superjson": "2.2.1","tailwind-merge": "^2.5.2","tailwindcss-animate": "^1.0.7",+ "unified": "^11.0.5","use-query-params": "^2.2.1","uuid": "^9.0.1","vaul": "^0.9.1",diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsxindex ab2daf901b..8beec314df 100644--- a/web/src/components/ui/MarkdownViewer.tsx+++ b/web/src/components/ui/MarkdownViewer.tsx@@ -13,10 +13,12 @@ import ReactMarkdown, { type Options } from "react-markdown";import Link from "next/link";import remarkGfm from "remark-gfm";import remarkMath from "remark-math";+import { unified } from "unified";+import remarkParse from "remark-parse";import { CodeBlock } from "@/src/components/ui/Codeblock";import { useTheme } from "next-themes";import { Button } from "@/src/components/ui/button";-import { Check, Copy, ImageOff } from "lucide-react";+import { Check, Copy, ImageOff, Info } from "lucide-react";import { BsMarkdown } from "react-icons/bs";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";import { useMarkdownContext } from "@/src/features/theming/useMarkdownContext";@@ -32,6 +34,7 @@ import { type z } from "zod";import { ResizableImage } from "@/src/components/ui/resizable-image";import { LangfuseMediaView } from "@/src/components/ui/LangfuseMediaView";import { type MediaReturnType } from "@/src/features/media/validation";+import { JSONView } from "@/src/components/ui/CodeJsonViewer";type ReactMarkdownNode = ReactMarkdownExtraProps["node"];type ReactMarkdownNodeChildren = Exclude<@@ -87,130 +90,159 @@ function MarkdownRenderer({className?: string;customCodeHeaderClassName?: string;}) {- return (- <MemoizedReactMarkdown- className={cn("space-y-2 overflow-x-auto break-words text-sm", className)}- remarkPlugins={[remarkGfm, remarkMath]}- components={{- p({ children, node }) {- if (isImageNode(node)) {- return <>{children}</>;- }- return (- <p className="mb-2 whitespace-pre-wrap last:mb-0">{children}</p>- );- },- a({ children, href }) {- if (href)+ // Try to parse markdown content++ try {+ const parseMarkdown = () => {+ const processor = unified()+ .use(remarkParse)+ .use(remarkGfm)+ .use(remarkMath);+ return processor.parse(markdown);+ };+ parseMarkdown();++ // If parsing succeeds, render with ReactMarkdown+ return (+ <MemoizedReactMarkdown+ className={cn(+ "space-y-2 overflow-x-auto break-words text-sm",+ className,+ )}+ remarkPlugins={[remarkGfm, remarkMath]}+ components={{+ p({ children, node }) {+ if (isImageNode(node)) {+ return <>{children}</>;+ }return (- <Link href={href} className="underline" target="_blank">- {children}- </Link>+ <p className="mb-2 whitespace-pre-wrap last:mb-0">{children}</p>);- },- ul({ children }) {- if (isChecklist(children))- return <ul className="list-none">{children}</ul>;+ },+ a({ children, href }) {+ if (href)+ return (+ <Link href={href} className="underline" target="_blank">+ {children}+ </Link>+ );+ },+ ul({ children }) {+ if (isChecklist(children))+ return <ul className="list-none">{children}</ul>;- return <ul className="list-inside list-disc">{children}</ul>;- },- ol({ children }) {- return <ol className="list-inside list-decimal">{children}</ol>;- },- li({ children }) {- return (- <li className="mt-1 [&>ol]:pl-4 [&>ul]:pl-4">- {transformListItemChildren(children)}- </li>- );- },- pre({ children }) {- return <pre className="rounded p-2">{children}</pre>;- },- h1({ children }) {- return <h1 className="text-2xl font-bold">{children}</h1>;- },- h2({ children }) {- return <h2 className="text-xl font-bold">{children}</h2>;- },- h3({ children }) {- return <h3 className="text-lg font-bold">{children}</h3>;- },- h4({ children }) {- return <h4 className="text-base font-bold">{children}</h4>;- },- h5({ children }) {- return <h5 className="text-sm font-bold">{children}</h5>;- },- h6({ children }) {- return <h6 className="text-xs font-bold">{children}</h6>;- },- code({ children, className }) {- const languageMatch = /language-(\w+)/.exec(className || "");- const language = languageMatch ? languageMatch[1] : "";- const codeContent = String(children).replace(/\n$/, "");- const isMultiLine = codeContent.includes("\n");+ return <ul className="list-inside list-disc">{children}</ul>;+ },+ ol({ children }) {+ return <ol className="list-inside list-decimal">{children}</ol>;+ },+ li({ children }) {+ return (+ <li className="mt-1 [&>ol]:pl-4 [&>ul]:pl-4">+ {transformListItemChildren(children)}+ </li>+ );+ },+ pre({ children }) {+ return <pre className="rounded p-2">{children}</pre>;+ },+ h1({ children }) {+ return <h1 className="text-2xl font-bold">{children}</h1>;+ },+ h2({ children }) {+ return <h2 className="text-xl font-bold">{children}</h2>;+ },+ h3({ children }) {+ return <h3 className="text-lg font-bold">{children}</h3>;+ },+ h4({ children }) {+ return <h4 className="text-base font-bold">{children}</h4>;+ },+ h5({ children }) {+ return <h5 className="text-sm font-bold">{children}</h5>;+ },+ h6({ children }) {+ return <h6 className="text-xs font-bold">{children}</h6>;+ },+ code({ children, className }) {+ const languageMatch = /language-(\w+)/.exec(className || "");+ const language = languageMatch ? languageMatch[1] : "";+ const codeContent = String(children).replace(/\n$/, "");+ const isMultiLine = codeContent.includes("\n");- return language || isMultiLine ? (- // code block- <CodeBlock- key={Math.random()}- language={language}- value={codeContent}- theme={theme}- className={customCodeHeaderClassName}- />- ) : (- // inline code- <code className="rounded border bg-secondary px-0.5">- {codeContent}- </code>- );- },- blockquote({ children }) {- return (- <blockquote className="border-l-4 pl-4 italic">- {children}- </blockquote>- );- },- img({ src, alt }) {- return src ? <ResizableImage src={src} alt={alt} /> : null;- },- hr() {- return <hr className="my-4" />;- },- table({ children }) {- return (- <div className="overflow-x-auto rounded border text-xs">- <table className="min-w-full divide-y">{children}</table>- </div>- );- },- thead({ children }) {- return <thead>{children}</thead>;- },- tbody({ children }) {- return <tbody className="divide-y divide-border">{children}</tbody>;- },- tr({ children }) {- return <tr>{children}</tr>;- },- th({ children }) {- return (- <th className="px-4 py-2 text-left text-xs font-medium uppercase tracking-wider">- {children}- </th>- );- },- td({ children }) {- return <td className="whitespace-nowrap px-4 py-2">{children}</td>;- },- }}- >- {markdown}- </MemoizedReactMarkdown>- );+ return language || isMultiLine ? (+ // code block+ <CodeBlock+ key={Math.random()}+ language={language}+ value={codeContent}+ theme={theme}+ className={customCodeHeaderClassName}+ />+ ) : (+ // inline code+ <code className="rounded border bg-secondary px-0.5">+ {codeContent}+ </code>+ );+ },+ blockquote({ children }) {+ return (+ <blockquote className="border-l-4 pl-4 italic">+ {children}+ </blockquote>+ );+ },+ img({ src, alt }) {+ return src ? <ResizableImage src={src} alt={alt} /> : null;+ },+ hr() {+ return <hr className="my-4" />;+ },+ table({ children }) {+ return (+ <div className="overflow-x-auto rounded border text-xs">+ <table className="min-w-full divide-y">{children}</table>+ </div>+ );+ },+ thead({ children }) {+ return <thead>{children}</thead>;+ },+ tbody({ children }) {+ return <tbody className="divide-y divide-border">{children}</tbody>;+ },+ tr({ children }) {+ return <tr>{children}</tr>;+ },+ th({ children }) {+ return (+ <th className="px-4 py-2 text-left text-xs font-medium uppercase tracking-wider">+ {children}+ </th>+ );+ },+ td({ children }) {+ return <td className="whitespace-nowrap px-4 py-2">{children}</td>;+ },+ }}+ >+ {markdown}+ </MemoizedReactMarkdown>+ );+ } catch (error) {+ // fallback to JSON view if markdown parsing fails++ return (+ <>+ <div className="flex items-center gap-1 text-xs text-muted-foreground">+ <Info className="h-3 w-3" />+ Markdown parsing failed. Displaying raw JSON.+ </div>+ <JSONView json={markdown} className="min-w-0" />+ </>+ );+ }}const parseOpenAIContentParts = (content: z.infer<typeof OpenAIContentParts> | null,