A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
styles: tool calls styling and streamdown (#6411)
Myles Scolnick98f8ae3334dc
diff --git a/frontend/package.json b/frontend/package.jsonindex 2f05ce09c3..6cf533c6a5 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -155,7 +155,7 @@"remark-gfm": "^4.0.1","rpc-anywhere": "^1.7.0","sql-formatter": "^15.6.6",- "streamdown": "^1.2.0",+ "streamdown": "^1.3.0","string-dedent": "^3.0.2","swiper": "^11.2.10","tailwind-merge": "^2.6.0",diff --git a/frontend/src/components/chat/acp/agent-panel.css b/frontend/src/components/chat/acp/agent-panel.cssindex 803f0d2fb6..ce085ca674 100644--- a/frontend/src/components/chat/acp/agent-panel.css+++ b/frontend/src/components/chat/acp/agent-panel.css@@ -1,23 +1,5 @@-@source "../node_modules/streamdown/dist/index.js";-.mo-agent-panel {- /* Make headers a bit smaller */- --text-2xl: 1rem;- --text-3xl: 1rem;-pre {width: 100%;}-- [data-code-block-container="true"] {- margin-top: 0 !important;- }-- [data-code-block-header="true"] {- padding: 4px !important;- }-- code {- font-size: inherit !important;- }}diff --git a/frontend/src/components/chat/acp/blocks.tsx b/frontend/src/components/chat/acp/blocks.tsxindex 4d85e0cd41..ed2b113b35 100644--- a/frontend/src/components/chat/acp/blocks.tsx+++ b/frontend/src/components/chat/acp/blocks.tsx@@ -20,7 +20,6 @@ import {XIcon,} from "lucide-react";import React from "react";-import { Streamdown } from "streamdown";import { mergeToolCalls } from "use-acp";import { JsonOutput } from "@/components/editor/output/JsonOutput";import { Button } from "@/components/ui/button";@@ -33,6 +32,7 @@ import { logNever } from "@/utils/assertNever";import { cn } from "@/utils/cn";import { type Base64String, base64ToDataURL } from "@/utils/json/base64";import { Strings } from "@/utils/strings";+import { MarkdownRenderer } from "../markdown-renderer";import { SimpleAccordion } from "./common";import type {AgentNotificationEvent,@@ -308,7 +308,7 @@ export const AgentMessagesBlock = (props: {export const ContentBlocks = (props: { data: ContentBlock[] }) => {const renderBlock = (block: ContentBlock) => {if (block.type === "text") {- return <Streamdown>{block.text}</Streamdown>;+ return <MarkdownRenderer content={block.text} />;}if (block.type === "image") {return <ImageBlock data={block} />;@@ -372,7 +372,7 @@ export const ResourceBlock = (props: { data: ContentBlockOf<"resource"> }) => {</span></PopoverTrigger><PopoverContent className="max-h-96 overflow-y-auto scrollbar-thin">- <Streamdown>{props.data.resource.text}</Streamdown>+ <MarkdownRenderer content={props.data.resource.text} /></PopoverContent></Popover>);diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsxindex 951b5218a8..b439c3b977 100644--- a/frontend/src/components/chat/chat-panel.tsx+++ b/frontend/src/components/chat/chat-panel.tsx@@ -234,6 +234,7 @@ const ChatMessageDisplay: React.FC<ChatMessageProps> = memo(index={i}toolName={part.type}result={part.output}+ className="my-2"state={part.state}/>);@@ -266,6 +267,7 @@ const ChatMessageDisplay: React.FC<ChatMessageProps> = memo(toolName={part.type}result={part.output}state={part.state}+ className="my-2"/>);@@ -283,10 +285,11 @@ const ChatMessageDisplay: React.FC<ChatMessageProps> = memo(Logger.error("Unhandled part type:", part.type);try {return (- <MarkdownRenderer- key={i}- content={JSON.stringify(part, null, 2)}- />+ <div className="text-xs text-muted-foreground" key={i}>+ <MarkdownRenderer+ content={JSON.stringify(part, null, 2)}+ />+ </div>);} catch (error) {Logger.error("Error rendering part:", part.type, error);diff --git a/frontend/src/components/chat/markdown-renderer.css b/frontend/src/components/chat/markdown-renderer.cssindex b4c2455add..880ae37ffd 100644--- a/frontend/src/components/chat/markdown-renderer.css+++ b/frontend/src/components/chat/markdown-renderer.css@@ -1,4 +1,10 @@+@source "../../../node_modules/streamdown/dist/index.js";+.mo-markdown-renderer {+ /* Make headers a bit smaller */+ --text-2xl: 1rem;+ --text-3xl: 1.2rem;+h1,h2,h3,@@ -12,4 +18,37 @@margin-top: 8px;margin-bottom: 12px;}++ pre {+ width: 100%;+ }++ [data-code-block-container="true"] {+ margin-top: 0 !important;+ }++ [data-code-block-header="true"] {+ padding: 4px !important;+ }++ code {+ font-size: inherit !important;+ }++ li.task-list-item {+ list-style-type: none;+ }++ li {+ padding-left: 6px;++ > input {+ margin-right: 6px;+ }+ }++ ul,+ ol {+ padding-left: 10px;+ }}diff --git a/frontend/src/components/chat/markdown-renderer.tsx b/frontend/src/components/chat/markdown-renderer.tsxindex 2a29573340..2a26159007 100644--- a/frontend/src/components/chat/markdown-renderer.tsx+++ b/frontend/src/components/chat/markdown-renderer.tsx@@ -3,10 +3,8 @@import { EditorView } from "@codemirror/view";import { useAtomValue } from "jotai";import { BetweenHorizontalStartIcon } from "lucide-react";-import { marked } from "marked";-import { memo, Suspense, useEffect, useMemo, useState } from "react";-import Markdown, { type Components } from "react-markdown";-import remarkGfm from "remark-gfm";+import { memo, Suspense, useEffect, useState } from "react";+import { Streamdown, type StreamdownProps } from "streamdown";import { Button, type ButtonProps } from "@/components/ui/button";import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";import { useCellActions } from "@/core/cells/cells";@@ -171,6 +169,8 @@ const CopyButton: React.FC<ButtonProps> = ({ onClick, ...props }) => {);};+type Components = StreamdownProps["components"];+const COMPONENTS: Components = {code: ({ children, className }) => {const language = className?.replace("language-", "");@@ -186,42 +186,11 @@ const COMPONENTS: Components = {},};-function parseMarkdownIntoBlocks(markdown: string): string[] {- const tokens = marked.lexer(markdown);- return tokens.map((token) => token.raw);-}--const PLUGINS = [remarkGfm];--const MemoizedMarkdownBlock = memo(- ({ content }: { content: string }) => {- return (- <Markdown- components={COMPONENTS}- remarkPlugins={PLUGINS}- className="mo-markdown-renderer prose dark:prose-invert max-w-none prose-pre:pl-0"- >- {content}- </Markdown>- );- },- (prevProps, nextProps) => prevProps.content === nextProps.content,-);--MemoizedMarkdownBlock.displayName = "MemoizedMarkdownBlock";-export const MarkdownRenderer = memo(({ content }: { content: string }) => {- const blocks = useMemo(() => parseMarkdownIntoBlocks(content), [content]);-return (- <>- {blocks.map((block, index) => (- <MemoizedMarkdownBlock- content={block}- key={`markdown-block-${index}`}- />- ))}- </>+ <Streamdown components={COMPONENTS} className="mo-markdown-renderer">+ {content}+ </Streamdown>);});MarkdownRenderer.displayName = "MarkdownRenderer";diff --git a/frontend/src/components/chat/tool-call-accordion.tsx b/frontend/src/components/chat/tool-call-accordion.tsxindex 87cb20e3af..07d05799e5 100644--- a/frontend/src/components/chat/tool-call-accordion.tsx+++ b/frontend/src/components/chat/tool-call-accordion.tsx@@ -1,13 +1,16 @@/* Copyright 2024 Marimo. All rights reserved. */import type { ToolUIPart } from "ai";+import { isEmpty } from "lodash-es";import {CheckCircleIcon,+ InfoIcon,Loader2,WrenchIcon,XCircleIcon,} from "lucide-react";import React from "react";+import { z } from "zod";import {Accordion,AccordionContent,@@ -16,12 +19,101 @@ import {} from "@/components/ui/accordion";import { cn } from "@/utils/cn";+// Zod schema matching the Python SuccessResult dataclass+const SuccessResultSchema = z+ .object({+ status: z.string().default("success"),+ auth_required: z.boolean().default(false),+ action_url: z.any(),+ next_steps: z.any(),+ meta: z.any(),+ message: z.string().nullish(),+ })+ .passthrough();++type SuccessResult = z.infer<typeof SuccessResultSchema>;++const PrettySuccessResult: React.FC<{ data: SuccessResult }> = ({ data }) => {+ const {+ status,+ auth_required,+ action_url: _action_url,+ meta: _meta,+ next_steps: _next_steps,+ message,+ ...rest+ } = data;++ return (+ <div className="py-1 flex flex-col gap-1">+ {/* Status */}+ <div className="flex items-center gap-2">+ <span className="text-xs font-medium text-[var(--grass-11)] capitalize">+ {status}+ </span>+ {auth_required && (+ <span className="text-xs px-2 py-0.5 bg-[var(--amber-2)] text-[var(--amber-11)] rounded-full">+ Auth Required+ </span>+ )}+ </div>++ {/* Message */}+ {message && (+ <div className="flex items-start gap-2">+ <InfoIcon className="h-3 w-3 text-[var(--blue-11)] mt-0.5 flex-shrink-0" />+ <div className="text-xs text-foreground">{message}</div>+ </div>+ )}++ {/* Data */}+ {rest && (+ <div className="flex flex-col gap-1">+ {Object.entries(rest).map(([key, value]) => {+ if (isEmpty(value)) {+ return null;+ }+ return (+ <div key={key}>+ <div className="text-xs font-medium text-muted-foreground mb-1 capitalize">+ {key}:+ </div>+ <pre className="bg-[var(--slate-2)] p-1 text-muted-foreground border border-[var(--slate-4)] rounded text-xs overflow-auto scrollbar-thin max-h-64">+ {JSON.stringify(value, null, 2)}+ </pre>+ </div>+ );+ })}+ </div>+ )}+ </div>+ );+};++const ResultRenderer: React.FC<{ result: unknown }> = ({ result }) => {+ // Try to parse the result with our Zod schema+ const parseResult = SuccessResultSchema.safeParse(result);++ if (parseResult.success) {+ // If it matches the SuccessResult schema, show the pretty UI+ return <PrettySuccessResult data={parseResult.data} />;+ }++ // Otherwise, fall back to the current JSON viewer+ return (+ <div className="text-xs font-medium text-muted-foreground mb-1">+ {typeof result === "string" ? result : JSON.stringify(result, null, 2)}+ </div>+ );+};+interface ToolCallAccordionProps {toolName: string;result: unknown;error?: string;index?: number;state?: ToolUIPart["state"];+ className?: string;}export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({@@ -30,6 +122,7 @@ export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({error,index = 0,state,+ className,}) => {const hasResult = state === "output-available" && (result || error);const status = error ? "error" : hasResult ? "success" : "loading";@@ -39,9 +132,9 @@ export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({case "loading":return <Loader2 className="h-3 w-3 animate-spin" />;case "error":- return <XCircleIcon className="h-3 w-3 text-destructive" />;+ return <XCircleIcon className="h-3 w-3 text-[var(--red-11)]" />;case "success":- return <CheckCircleIcon className="h-3 w-3 text-green-600" />;+ return <CheckCircleIcon className="h-3 w-3 text-[var(--grass-11)]" />;default:return <WrenchIcon className="h-3 w-3" />;}@@ -49,13 +142,13 @@ export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({const getStatusText = () => {if (status === "loading") {- return "Running: ";+ return "Running";}if (error) {- return "Failed: ";+ return "Failed";}if (hasResult) {- return "Done: ";+ return "Done";}return "Tool call";};@@ -65,20 +158,22 @@ export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({key={`tool-${index}`}type="single"collapsible={true}- className="w-full"+ className={cn("w-full", className)}><AccordionItem value="tool-call" className="border-0"><AccordionTriggerclassName={cn(- "h-6 text-xs border-border shadow-none! ring-0! bg-muted hover:bg-muted/30 py-0 px-2 gap-1 rounded-sm [&[data-state=open]>svg]:rotate-180",- status === "error" && "text-destructive/80",- status === "success" && "text-green-600/80",+ "h-6 text-xs border-border shadow-none! ring-0! bg-muted/60 hover:bg-muted py-0 px-2 gap-1 rounded-sm [&[data-state=open]>svg]:rotate-180 hover:no-underline",+ status === "error" && "text-[var(--red-11)]/80",+ status === "success" && "text-[var(--grass-11)]/80",)}><span className="flex items-center gap-1">{getStatusIcon()}- {getStatusText()}:{" "}- <code className="font-mono text-xs">{toolName}</code>+ {getStatusText()}:+ <code className="font-mono text-xs">+ {formatToolName(toolName)}+ </code></span></AccordionTrigger><AccordionContent className="pb-2 px-2">@@ -86,25 +181,16 @@ export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({{hasResult && (<div className="space-y-3">{result !== undefined && result !== null && (- <div>- <div className="text-xs font-medium text-muted-foreground mt-2 mb-1">- Result:- </div>- <div className="text-xs font-medium text-muted-foreground mb-1">- {typeof result === "string"- ? result- : JSON.stringify(result, null, 2)}- </div>- </div>+ <ResultRenderer result={result} />)}{/* Error */}{error && (<div>- <div className="text-xs font-medium text-destructive mb-1">+ <div className="text-xs font-medium text-[var(--red-11)] mb-1">Error:</div>- <div className="bg-destructive/10 border border-destructive/20 rounded-md p-3 text-sm text-destructive">+ <div className="bg-[var(--red-2)] border border-[var(--red-6)] rounded-md p-3 text-sm text-[var(--red-11)]">{error}</div></div>@@ -116,3 +202,7 @@ export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({</Accordion>);};++function formatToolName(toolName: string) {+ return toolName.replace("tool-", "");+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 4a3dde1014..ef16f0e2d6 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -446,8 +446,8 @@ importers:specifier: ^15.6.6version: 15.6.6streamdown:- specifier: ^1.2.0- version: 1.2.0(@types/[email protected])([email protected])+ specifier: ^1.3.0+ version: 1.3.0(@types/[email protected])([email protected])string-dedent:specifier: ^3.0.2version: 3.0.2@@ -6177,12 +6177,18 @@ packages:resolution: {integrity: sha512-wkQCkSYoOGCRKERFWcxMVMOcYE2K1AaNLU8DXS9arxnLOUEWbOXKXiJUNzEpqZ3JOKpnha3jkFrumEjVliDe7A==}+ resolution: {integrity: sha512-Y8/SBAHkZGoNkpzqqfCldijcuUKh7/su31kEBp67cFY09Wy0mTRgtsLYsiIxMJxlu0f6AA5SUTbDR8K0rxnbUw==}+resolution: {integrity: sha512-OguPdidb+fbHQSU4Q4ZiLKnzWo8Wwsf5bZfbvu7//a9oTYoqD/fWpe96NuHkoS9h0ccGOTe0C4NGXdtS0iObOw==}resolution: {integrity: sha512-zl6s8LwNyo1P9uw+XJGvZtdFF1GdAkOg8ujOw+4Pyb76874fLps4ueHXDhXWdk6YHQ6OgUtinliG7RsYvCbbBg==}+ resolution: {integrity: sha512-3KKrV5ZVI8if87DVSi1vDeByYrkGzg4mEfeu4alwgmmIeARiBLKCZS2uw5Gb6nU9x9Yufyj3iudm6i7nl52PFw==}+resolution: {integrity: sha512-KK6y/BN8lbaq654j7JgBydev7wuNMcID54lkRav1P0CaE1e47P72AWWPiGKXTJU271ooYzcvTAn/Zt0REnvc7A==}@@ -7941,6 +7947,9 @@ packages:resolution: {integrity: sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==}+ resolution: {integrity: sha512-PgTgs/BlvHxOu8QuEN7wi5A0OmXaBcHpmCSTehcs6Uuu9IkDIEo13Hy7n898RHfrQ49vKCoGeWZSaAK01nwVig==}+resolution: {integrity: sha512-TwEZ+X+yCJmYfL7TPUOcvBZ4QfoT5YenQiJuX//0th53DE6w0xxLEtfK3iyryQFddXuvkIk51EEgrJQ0WJkOmQ==}@@ -8332,6 +8341,9 @@ packages:resolution: {integrity: sha512-OiM2wrZ/wuhKkigASodFoo8wimG3H12LWQaH8qSPVJn9apWKFSH3YOCtbKpBorTVw/eI7cuT21XBbvwEswbIOA==}+ resolution: {integrity: sha512-/aE8hCfKlQeA8LmyeyQvQF3eBiLRGNlfBJEvWH7ivp9sBqs7TNqBL5X3v157rM4IFETqDnIOO+z5M/biZbo9Ww==}+resolution: {integrity: sha512-1quofZ2RQ9EWdeN34S79+KExV1764+wCUGop5CPL1WGdD0ocPpu91lzPGbwWMECpEpd42kJGQwzRfyov9j4yNg==}@@ -8666,8 +8678,8 @@ packages:resolution: {integrity: sha512-76ORR0DO1o1hlKwTbi/DM3EXWGf3ZJYO8cXX5RJwnul2DEg2oyoZyjLNoQM8WsvZiFKCRfC1O0J7iCvie3RZmQ==}- resolution: {integrity: sha512-FBRV8mXgFQx+MbLgVKjEpGzlmm5H/3VDh7mebd9g9xSOnqi1UyrqR4na917jecTcCCNQuyRPTRPIB9DH5X8pDg==}+ resolution: {integrity: sha512-vFZdoWKUeagzKwGGOcEqkV1fcgXOJOQqrNBor5/hbaAE/e/ULxZoIHHJJd5KEuaSddCM9KuYtIuZi3WSttXTEA==}peerDependencies:react: ^18.0.0 || ^19.0.0@@ -16744,6 +16756,22 @@ snapshots:dependencies:'@types/hast': 3.0.4+ dependencies:+ '@types/hast': 3.0.4+ '@types/unist': 3.0.3+ '@ungap/structured-clone': 1.3.0+ hast-util-from-parse5: 8.0.3+ hast-util-to-parse5: 8.0.0+ html-void-elements: 3.0.0+ mdast-util-to-hast: 13.2.0+ parse5: 7.3.0+ unist-util-position: 5.0.0+ unist-util-visit: 5.0.0+ vfile: 6.0.3+ web-namespaces: 2.0.1+ zwitch: 2.0.4+dependencies:'@types/hast': 3.0.4@@ -16778,6 +16806,16 @@ snapshots:transitivePeerDependencies:- supports-color+ dependencies:+ '@types/hast': 3.0.4+ comma-separated-tokens: 2.0.3+ devlop: 1.1.0+ property-information: 6.5.0+ space-separated-tokens: 2.0.2+ web-namespaces: 2.0.1+ zwitch: 2.0.4+dependencies:'@types/hast': 3.0.4@@ -18788,6 +18826,8 @@ snapshots:object-assign: 4.1.1react-is: 16.13.1+ [email protected]: {}+@@ -19408,6 +19448,12 @@ snapshots:unist-util-visit-parents: 6.0.1vfile: 6.0.3+ dependencies:+ '@types/hast': 3.0.4+ hast-util-raw: 9.1.0+ vfile: 6.0.3+dependencies:'@types/mdast': 4.0.4@@ -19869,7 +19915,7 @@ snapshots:dependencies:clsx: 2.1.1harden-react-markdown: 1.0.5([email protected](@types/[email protected])([email protected]))([email protected])@@ -19880,6 +19926,7 @@ snapshots:react: 19.1.1react-markdown: 10.1.0(@types/[email protected])([email protected])rehype-katex: 7.0.1+ rehype-raw: 7.0.0remark-gfm: 4.0.1remark-math: 6.0.0shiki: 3.12.2