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
Upgrade AI sdk to v5 (#6242)
Shahmir Varqha5880ac134e15
diff --git a/frontend/package.json b/frontend/package.jsonindex 886bcbe2de..5c5867f33d 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -17,7 +17,7 @@"./unstable_internal/*": "./src/*"},"dependencies": {- "@ai-sdk/react": "^1.2.12",+ "@ai-sdk/react": "^2.0.30","@anywidget/types": "^0.2.0","@codemirror/autocomplete": "^6.18.6","@codemirror/commands": "^6.8.1",@@ -102,7 +102,7 @@"@xterm/addon-attach": "^0.11.0","@xterm/addon-fit": "^0.10.0","@xterm/xterm": "^5.5.0",- "ai": "^4.3.16",+ "ai": "^5.0.30","ansi_up": "^6.0.6","class-variance-authority": "^0.7.1","clsx": "^2.1.1",diff --git a/frontend/src/__tests__/chat-utils.test.ts b/frontend/src/__tests__/chat-utils.test.tsindex 91ec485b98..d60c7011b1 100644--- a/frontend/src/__tests__/chat-utils.test.ts+++ b/frontend/src/__tests__/chat-utils.test.ts@@ -1,22 +1,17 @@/* Copyright 2024 Marimo. All rights reserved. */+import type { UIMessage } from "ai";import { describe, expect, it } from "vitest";import { Maps } from "@/utils/maps";-import { addMessageToChat } from "../core/ai/chat-utils";+import { replaceMessagesInChat } from "../core/ai/chat-utils";import type { Chat, ChatId, ChatState } from "../core/ai/state";const CHAT_1 = "chat-1" as ChatId;-const CHAT_2 = "chat-2" as ChatId;--function first(map: Map<ChatId, Chat>) {- return [...map.values()][0];-}function asMap(list: Iterable<Chat>) {return Maps.keyBy(list, (c) => c.id);}--describe("addMessageToChat", () => {+describe("replaceMessagesInChat", () => {const mockChatState: ChatState = {chats: asMap([{@@ -26,223 +21,43 @@ describe("addMessageToChat", () => {{id: "msg-1",role: "user",- content: "Hello",- timestamp: 1000,- },- {- id: "msg-2",- role: "assistant",- content: "Hi there!",- timestamp: 2000,+ parts: [{ type: "text", text: "Hello" }],+ metadata: { timestamp: 1000 },},],createdAt: 1000,updatedAt: 2000,},- {- id: CHAT_2,- title: "Test Chat 2",- messages: [- {- id: "msg-3",- role: "user",- content: "How are you?",- timestamp: 3000,- },- ],- createdAt: 3000,- updatedAt: 3000,- },]),activeChatId: CHAT_1,};- it("should add a new message to an existing chat", () => {- const result = addMessageToChat({- chatState: mockChatState,- chatId: CHAT_1,- messageId: "msg-4",- role: "user",- content: "New message",- });-- expect(result.chats).toHaveLength(2);- const updatedChat = result.chats.get(CHAT_1);- expect(updatedChat?.messages).toHaveLength(3);- expect(updatedChat?.messages[2]).toEqual({- id: "msg-4",- role: "user",- content: "New message",- timestamp: expect.any(Number),- });- expect(updatedChat?.updatedAt).toBeGreaterThan(- first(mockChatState.chats).updatedAt,- );- });-- it("should update an existing message", () => {- const result = addMessageToChat({+ it("replaces messages in a chat", () => {+ const newMessages: UIMessage[] = [+ {+ id: "msg-2",+ role: "assistant",+ parts: [{ type: "text", text: "Hi there!" }],+ metadata: { timestamp: 2000 },+ },+ ];+ const result = replaceMessagesInChat({chatState: mockChatState,chatId: CHAT_1,- messageId: "msg-1",- role: "user",- content: "Updated content",+ messages: newMessages,});-- expect(result.chats).toHaveLength(2);- const updatedChat = result.chats.get(CHAT_1);- expect(updatedChat?.messages).toHaveLength(2);- expect(updatedChat?.messages[0]).toEqual({- id: "msg-1",- role: "user",- content: "Updated content",- timestamp: 1000,- });- expect(updatedChat?.updatedAt).toBeGreaterThan(- first(mockChatState.chats).updatedAt,+ expect(result.chats.get(CHAT_1)?.messages).toEqual(newMessages);+ expect(result.chats.get(CHAT_1)?.updatedAt).toBeGreaterThan(+ mockChatState.chats.get(CHAT_1)?.updatedAt ?? 0,);});- it("should handle message parts", () => {- const parts = [{ type: "text" as const, text: "Part content" }];- const result = addMessageToChat({- chatState: mockChatState,- chatId: CHAT_1,- messageId: "msg-5",- role: "assistant",- content: "Message with parts",- parts,- });-- const updatedChat = result.chats.get(CHAT_1);- expect(updatedChat?.messages[2].parts).toEqual(parts);- });-- it("should update message parts", () => {- const originalParts = [{ type: "text" as const, text: "Original" }];- const updatedParts = [{ type: "text" as const, text: "Updated" }];- const chats = [...mockChatState.chats.values()];-- const stateWithParts: ChatState = {- ...mockChatState,- chats: asMap([- {- ...chats[0],- messages: [- {- ...chats[0].messages[0],- parts: originalParts,- },- chats[0].messages[1],- ],- },- chats[1],- ]),- };-- const result = addMessageToChat({- chatState: stateWithParts,- chatId: CHAT_1,- messageId: "msg-1",- role: "user",- content: "Updated content",- parts: updatedParts,- });-- const updatedChat = result.chats.get(CHAT_1);- expect(updatedChat?.messages[0].parts).toEqual(updatedParts);- });-- it("should return unchanged state when chatId is null", () => {- const result = addMessageToChat({+ it("returns unchanged state if chatId is null", () => {+ const result = replaceMessagesInChat({chatState: mockChatState,chatId: null,- messageId: "msg-4",- role: "user",- content: "New message",+ messages: [],});-expect(result).toEqual(mockChatState);});-- it("should return unchanged state when chatId does not exist", () => {- const result = addMessageToChat({- chatState: mockChatState,- chatId: "non-existent-chat" as ChatId,- messageId: "msg-4",- role: "user",- content: "New message",- });-- expect(result).toEqual(mockChatState);- });-- it("should not modify other chats when updating a specific chat", () => {- const result = addMessageToChat({- chatState: mockChatState,- chatId: CHAT_1,- messageId: "msg-4",- role: "user",- content: "New message",- });-- const unchangedChat = result.chats.get(CHAT_2);- expect(unchangedChat).toEqual([...mockChatState.chats.values()][1]);- });-- it("should preserve message order when adding new messages", () => {- const result = addMessageToChat({- chatState: mockChatState,- chatId: CHAT_1,- messageId: "msg-4",- role: "user",- content: "New message",- });-- const updatedChat = result.chats.get(CHAT_1);- expect(updatedChat?.messages[0].id).toBe("msg-1");- expect(updatedChat?.messages[1].id).toBe("msg-2");- expect(updatedChat?.messages[2].id).toBe("msg-4");- });-- it("should handle empty chat messages array", () => {- const chatId = "empty-chat" as ChatId;- const emptyChatState: ChatState = {- chats: asMap([- {- id: chatId,- title: "Empty Chat",- messages: [],- createdAt: 1000,- updatedAt: 1000,- },- ]),- activeChatId: chatId,- };-- const result = addMessageToChat({- chatState: emptyChatState,- chatId: chatId,- messageId: "msg-1",- role: "user",- content: "First message",- });-- const updatedChat = result.chats.get(chatId);- expect(updatedChat?.messages).toHaveLength(1);- expect(updatedChat?.messages[0].content).toBe("First message");- });-- it("should handle different message roles", () => {- const result = addMessageToChat({- chatState: mockChatState,- chatId: CHAT_1,- messageId: "msg-4",- role: "assistant",- content: "Assistant response",- });-- const updatedChat = result.chats.get(CHAT_1);- expect(updatedChat?.messages[2].role).toBe("assistant");- });});diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsxindex 0e54db8098..f7e2cb1e99 100644--- a/frontend/src/components/chat/chat-panel.tsx+++ b/frontend/src/components/chat/chat-panel.tsx@@ -1,10 +1,15 @@/* Copyright 2024 Marimo. All rights reserved. */+import type { UIMessage } from "@ai-sdk/react";import { useChat } from "@ai-sdk/react";import { storePrompt } from "@marimo-team/codemirror-ai";import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";-import type { Message } from "ai/react";-import { useAtom, useAtomValue } from "jotai";+import {+ DefaultChatTransport,+ lastAssistantMessageIsCompleteWithToolCalls,+ type ToolUIPart,+} from "ai";+import { useAtom, useAtomValue, useSetAtom } from "jotai";import {BotMessageSquareIcon,ClockIcon,@@ -15,15 +20,7 @@ import {SettingsIcon,SquareIcon,} from "lucide-react";-import {- type Dispatch,- memo,- type SetStateAction,- useEffect,- useMemo,- useRef,- useState,-} from "react";+import { memo, useEffect, useMemo, useRef, useState } from "react";import useEvent from "react-use-event-hook";import { Button } from "@/components/ui/button";import {@@ -40,17 +37,15 @@ import {SelectLabel,SelectTrigger,} from "@/components/ui/select";-import { addMessageToChat } from "@/core/ai/chat-utils";+import { replaceMessagesInChat } from "@/core/ai/chat-utils";import { useModelChange } from "@/core/ai/config";import { AiModelId, type ProviderId } from "@/core/ai/ids/ids";import {activeChatAtom,type Chat,type ChatId,- type ChatState,chatStateAtom,} from "@/core/ai/state";-import type { ChatAttachment } from "@/core/ai/types";import { aiAtom, aiEnabledAtom } from "@/core/config/config";import { DEFAULT_AI_MODEL } from "@/core/config/config-schema";import { FeatureFlagged } from "@/core/config/feature-flag";@@ -59,9 +54,7 @@ import { useRuntimeManager } from "@/core/runtime/config";import { ErrorBanner } from "@/plugins/impl/common/error-banner";import { cn } from "@/utils/cn";import { timeAgo } from "@/utils/dates";-import { blobToString } from "@/utils/fileToBase64";import { Logger } from "@/utils/Logger";-import { generateUUID } from "@/utils/uuid";import { AIModelDropdown } from "../ai/ai-model-dropdown";import { useOpenSettingsToTab } from "../app-config/state";import { PromptInput } from "../editor/ai/add-cell-with-ai";@@ -72,6 +65,11 @@ import { Input } from "../ui/input";import { Tooltip, TooltipProvider } from "../ui/tooltip";import { toast } from "../ui/use-toast";import { AttachmentRenderer, FileAttachmentPill } from "./chat-components";+import {+ convertToFileUIPart,+ generateChatTitle,+ isLastMessageReasoning,+} from "./chat-utils";import { MarkdownRenderer } from "./markdown-renderer";import { ReasoningAccordion } from "./reasoning-accordion";import { ToolCallAccordion } from "./tool-call-accordion";@@ -89,16 +87,20 @@ interface ChatHeaderProps {onNewChat: () => void;activeChatId: ChatId | undefined;setActiveChat: (id: ChatId | null) => void;- chats: Chat[];}const ChatHeader: React.FC<ChatHeaderProps> = ({onNewChat,activeChatId,setActiveChat,- chats,}) => {const { handleClick } = useOpenSettingsToTab();+ const chatState = useAtomValue(chatStateAtom);+ const chats = useMemo(() => {+ return [...chatState.chats.values()].sort(+ (a, b) => b.updatedAt - a.updatedAt,+ );+ }, [chatState.chats]);return (<div className="flex border-b px-2 py-1 justify-between shrink-0 items-center">@@ -164,31 +166,32 @@ const ChatHeader: React.FC<ChatHeaderProps> = ({};interface ChatMessageProps {- message: Message;+ message: UIMessage;index: number;onEdit: (index: number, newValue: string) => void;- setChatState: Dispatch<SetStateAction<ChatState>>;- chatState: ChatState;isStreamingReasoning: boolean;isLast: boolean;}+function isToolPart(part: UIMessage["parts"][number]): part is ToolUIPart {+ return part.type.startsWith("tool-");+}+const ChatMessage: React.FC<ChatMessageProps> = memo(- ({ message, index, onEdit, isStreamingReasoning, isLast }) => (- <div- className={cn(- "flex group relative",- message.role === "user" ? "justify-end" : "justify-start",- )}- >- {message.role === "user" ? (+ ({ message, index, onEdit, isStreamingReasoning, isLast }) => {+ const renderUserMessage = (message: UIMessage) => {+ const textParts = message.parts?.filter((p) => p.type === "text");+ const content = textParts?.map((p) => p.text).join("\n");+ const fileParts = message.parts?.filter((p) => p.type === "file");++ return (<div className="w-[95%] bg-background border p-1 rounded-sm">- {message.experimental_attachments?.map((attachment, idx) => (- <AttachmentRenderer attachment={attachment} key={idx} />+ {fileParts?.map((filePart, idx) => (+ <AttachmentRenderer attachment={filePart} key={idx} />))}<PromptInputkey={message.id}- value={message.content}+ value={content}placeholder="Type your message..."onChange={() => {// noop@@ -204,12 +207,31 @@ const ChatMessage: React.FC<ChatMessageProps> = memo(}}/></div>- ) : (+ );+ };++ const renderOtherMessage = (message: UIMessage) => {+ const textParts = message.parts.filter((p) => p.type === "text");+ const content = textParts.map((p) => p.text).join("\n");++ return (<div className="w-[95%] break-words"><div className="absolute right-1 top-1 opacity-0 group-hover:opacity-100 transition-opacity">- <CopyClipboardIcon className="h-3 w-3" value={message.content} />+ <CopyClipboardIcon className="h-3 w-3" value={content || ""} /></div>- {message.parts?.map((part, i) => {+ {message.parts.map((part, i) => {+ if (isToolPart(part)) {+ return (+ <ToolCallAccordion+ key={i}+ index={i}+ toolName={part.type}+ result={part.output}+ state={part.state}+ />+ );+ }+switch (part.type) {case "text":return <MarkdownRenderer key={i} content={part.text} />;@@ -217,42 +239,62 @@ const ChatMessage: React.FC<ChatMessageProps> = memo(case "reasoning":return (<ReasoningAccordion- reasoning={part.reasoning}+ reasoning={part.text}key={i}index={i}isStreaming={isLast &&isStreamingReasoning &&// If there are multiple reasoning parts, only show the last one- i === (message.parts?.length || 0) - 1+ i === (message.parts.length || 0) - 1}/>);- case "tool-invocation":+ case "dynamic-tool":return (<ToolCallAccordionkey={i}index={i}- toolName={part.toolInvocation.toolName}- result={- part.toolInvocation.state === "result"- ? part.toolInvocation.result- : null- }- state={part.toolInvocation.state}+ toolName={part.type}+ result={part.output}+ state={part.state}/>);/* handle other part types … */default:- return null;+ Logger.error("Unhandled part type:", part.type);+ try {+ return (+ <MarkdownRenderer+ key={i}+ content={JSON.stringify(part, null, 2)}+ />+ );+ } catch (error) {+ Logger.error("Error rendering part:", part.type, error);+ return null;+ }}})}</div>- )}- </div>- ),+ );+ };++ return (+ <div+ className={cn(+ "flex group relative",+ message.role === "user" ? "justify-end" : "justify-start",+ )}+ >+ {message.role === "user"+ ? renderUserMessage(message)+ : renderOtherMessage(message)}+ </div>+ );+ },);ChatMessage.displayName = "ChatMessage";@@ -416,7 +458,7 @@ const ChatInput: React.FC<ChatInputProps> = memo(});return (- <div className="border-t relative shrink-0 min-h-[80px] flex flex-col">+ <div className="relative shrink-0 min-h-[80px] flex flex-col"><div className={cn("px-2 py-3 flex-1", inputClassName)}><PromptInputinputRef={inputRef}@@ -465,8 +507,9 @@ const ChatPanel = () => {};const ChatPanelBody = () => {- const [chatState, setChatState] = useAtom(chatStateAtom);+ const setChatState = useSetAtom(chatStateAtom);const [activeChat, setActiveChat] = useAtom(activeChatAtom);+ const [input, setInput] = useState("");const [newThreadInput, setNewThreadInput] = useState("");const [files, setFiles] = useState<File[]>();const newThreadInputRef = useRef<ReactCodeMirrorRef>(null);@@ -477,56 +520,62 @@ const ChatPanelBody = () => {const runtimeManager = useRuntimeManager();const { invokeAiTool } = useRequestClient();+ const activeChatId = activeChat?.id;+const {messages,- input,- setInput,- setMessages,- append,- handleSubmit,+ sendMessage,error,status,- reload,+ regenerate,stop,+ addToolResult,+ id: chatId,} = useChat({- id: activeChat?.id,- maxSteps: 10,- initialMessages: activeChat?.messages || [],- keepLastMessageOnError: true,- // Throttle the messages and data updates to 100ms- // experimental_throttle: 100,- api: runtimeManager.getAiURL("chat").toString(),- headers: runtimeManager.headers(),- experimental_prepareRequestBody: (options) => {- const completionBody = getAICompletionBody({- input: options.messages.map((m) => m.content).join("\n"),- });-- // Backend accepts attachments, so we convert the key- const newOptions = {- ...options,- messages: options.messages.map((m) => ({- ...m,- attachments: m.experimental_attachments,- experimental_attachments: undefined,- })),- };+ id: activeChatId,+ // Automatically submit when all tool results are available+ sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,+ messages: activeChat?.messages || [], // initial messages+ transport: new DefaultChatTransport({+ api: runtimeManager.getAiURL("chat").toString(),+ headers: runtimeManager.headers(),+ prepareSendMessagesRequest: (options) => {+ let input = "";+ for (const message of options.messages) {+ const textParts = message.parts.filter((p) => p.type === "text");+ input += textParts.map((p) => p.text).join("\n");+ }+ const completionBody = getAICompletionBody({ input });- return {- ...newOptions,- ...completionBody,- };- },- onFinish: (message) => {+ return {+ body: {+ ...options,+ ...completionBody,+ messages: options.messages.map((m) => ({+ ...m,+ attachments: m.parts.flatMap((p) =>+ p.type === "file"+ ? {+ contentType: p.mediaType,+ name: p.filename,+ url: p.url,+ }+ : [],+ ),+ content: m.parts+ ?.map((p) => ("text" in p ? p.text : ""))+ .join("\n"),+ })),+ },+ };+ },+ }),+ onFinish: ({ messages }) => {setChatState((prev) => {- return addMessageToChat({+ return replaceMessagesInChat({chatState: prev,chatId: prev.activeChatId,- messageId: message.id,- role: "assistant",- content: message.content,- parts: message.parts,- attachments: message.experimental_attachments,+ messages: messages,});});},@@ -534,22 +583,25 @@ const ChatPanelBody = () => {try {const response = await invokeAiTool({toolName: toolCall.toolName,- arguments: toolCall.args as Record<string, never>,+ arguments: toolCall.input as Record<string, never>,+ });+ addToolResult({+ tool: toolCall.toolName,+ toolCallId: toolCall.toolCallId,+ output: response.result || response.error,});-- // This response triggers the onFinish callback- return response.result || response.error;} catch (error) {Logger.error("Tool call failed:", error);- return `Error: ${error instanceof Error ? error.message : String(error)}`;+ addToolResult({+ tool: toolCall.toolName,+ toolCallId: toolCall.toolCallId,+ output: `Error: ${error instanceof Error ? error.message : String(error)}`,+ });}},onError: (error) => {Logger.error("An error occurred:", error);},- onResponse: (response) => {- Logger.debug("Received HTTP response from server:", response);- },});const handleFileChange = useEvent(@@ -584,58 +636,6 @@ const ChatPanelBody = () => {const isLoading = status === "submitted" || status === "streaming";- // Sync user messages from useChat to storage when they become available- // Only when we are done loading, for performance.- useEffect(() => {- if (!chatState.activeChatId || messages.length === 0 || isLoading) {- return;- }-- // Only sync if the last message is from a user- const lastMessage = messages[messages.length - 1];- if (lastMessage?.role !== "user") {- return;- }-- const currentChat = chatState.chats.get(chatState.activeChatId);- if (!currentChat) {- return;- }-- const storedMessageIds = new Set(currentChat.messages.map((m) => m.id));-- // Find user messages from useChat that aren't in storage yet- const missingUserMessages = messages.filter(- (m) => m.role === "user" && !storedMessageIds.has(m.id),- );-- if (missingUserMessages.length > 0) {- setChatState((prev) => {- let result = prev;-- for (const userMessage of missingUserMessages) {- result = addMessageToChat({- chatState: result,- chatId: prev.activeChatId,- messageId: userMessage.id,- role: "user",- content: userMessage.content,- parts: userMessage.parts,- attachments: userMessage.experimental_attachments,- });- }-- return result;- });- }- }, [- messages,- chatState.activeChatId,- chatState.chats,- setChatState,- isLoading,- ]);-// Check if we're currently streaming reasoning in the latest messageconst isStreamingReasoning =isLoading && messages.length > 0 && isLastMessageReasoning(messages);@@ -650,22 +650,19 @@ const ChatPanelBody = () => {};requestAnimationFrame(scrollToBottom);- }, [chatState.activeChatId]);+ }, [activeChatId]);const createNewThread = async (initialMessage: string,initialAttachments?: File[],) => {- const CURRENT_TIME = Date.now();+ const now = Date.now();const newChat: Chat = {- id: generateUUID() as ChatId,- title:- initialMessage.length > 50- ? `${initialMessage.slice(0, 50)}...`- : initialMessage,- messages: [], // Don't pre-populate - let useChat handle it and sync back- createdAt: CURRENT_TIME,- updatedAt: CURRENT_TIME,+ id: chatId as ChatId,+ title: generateChatTitle(initialMessage),+ messages: [],+ createdAt: now,+ updatedAt: now,};// Create new chat and set as active@@ -680,17 +677,21 @@ const ChatPanelBody = () => {return newState;});- const attachments =+ const fileParts =initialAttachments && initialAttachments.length > 0- ? await convertToChatAttachments(initialAttachments)+ ? await convertToFileUIPart(initialAttachments): undefined;// Trigger AI conversation with append- append({- id: generateUUID(),+ sendMessage({role: "user",- content: initialMessage,- experimental_attachments: attachments,+ parts: [+ {+ type: "text" as const,+ text: initialMessage,+ },+ ...(fileParts ?? []),+ ],});setFiles(undefined);setInput("");@@ -705,34 +706,13 @@ const ChatPanelBody = () => {const handleMessageEdit = useEvent((index: number, newValue: string) => {const editedMessage = messages[index];- const attachments = editedMessage.experimental_attachments;-- // Truncate both useChat and storage- setMessages((messages) => messages.slice(0, index));- const activeChatId = chatState.activeChatId;- if (activeChatId) {- setChatState((prev) => {- const nextChats = new Map(prev.chats);- const activeChat = chatState.chats.get(activeChatId);- if (activeChat) {- nextChats.set(activeChat.id, {- ...activeChat,- messages: activeChat.messages.slice(0, index),- updatedAt: Date.now(),- });- }-- return {- ...prev,- chats: nextChats,- };- });- }+ const fileParts = editedMessage.parts?.filter((p) => p.type === "file");- append({+ const messageId = editedMessage.id;+ sendMessage({+ messageId: messageId, // replace the messagerole: "user",- content: newValue,- experimental_attachments: attachments,+ parts: [{ type: "text", text: newValue }, ...fileParts],});});@@ -744,19 +724,20 @@ const ChatPanelBody = () => {if (newMessageInputRef.current?.view) {storePrompt(newMessageInputRef.current.view);}- const attachments = files- ? await convertToChatAttachments(files)- : undefined;+ const fileParts = files ? await convertToFileUIPart(files) : undefined;- handleSubmit(e, {- experimental_attachments: attachments,+ e?.preventDefault();+ sendMessage({+ text: newValue,+ files: fileParts,});+ setInput("");setFiles(undefined);},);const handleReload = () => {- reload();+ regenerate();};const handleNewThreadSubmit = useEvent(() => {@@ -771,12 +752,6 @@ const ChatPanelBody = () => {const handleOnCloseThread = () => newThreadInputRef.current?.editor?.blur();- const sortedChats = useMemo(() => {- return [...chatState.chats.values()].sort(- (a, b) => b.updatedAt - a.updatedAt,- );- }, [chatState.chats]);-const isNewThread = messages.length === 0;const chatInput = isNewThread ? (<ChatInput@@ -831,7 +806,6 @@ const ChatPanelBody = () => {onNewChat={handleNewChat}activeChatId={activeChat?.id}setActiveChat={setActiveChat}- chats={sortedChats}/></TooltipProvider>@@ -852,8 +826,6 @@ const ChatPanelBody = () => {message={message}index={idx}onEdit={handleMessageEdit}- setChatState={setChatState}- chatState={chatState}isStreamingReasoning={isStreamingReasoning}isLast={idx === messages.length - 1}/>@@ -896,44 +868,4 @@ const ChatPanelBody = () => {);};-function isLastMessageReasoning(messages: Message[]): boolean {- if (messages.length === 0) {- return false;- }-- const lastMessage = messages.at(-1);- if (!lastMessage) {- return false;- }-- if (lastMessage.role !== "assistant" || !lastMessage.parts) {- return false;- }-- const parts = lastMessage.parts;- if (parts.length === 0) {- return false;- }-- // Check if the last part is reasoning- const lastPart = parts[parts.length - 1];- return lastPart.type === "reasoning";-}--async function convertToChatAttachments(- files: File[],-): Promise<ChatAttachment[]> {- const attachments = await Promise.all(- files.map(async (file) => {- return {- name: file.name,- url: await blobToString(file, "dataUrl"),- contentType: file.type,- };- }),- );-- return attachments;-}-export default ChatPanel;diff --git a/frontend/src/components/chat/chat-utils.ts b/frontend/src/components/chat/chat-utils.tsnew file mode 100644index 0000000000..30ac6a5769--- /dev/null+++ b/frontend/src/components/chat/chat-utils.ts@@ -0,0 +1,50 @@+/* Copyright 2024 Marimo. All rights reserved. */++import type { FileUIPart, UIMessage } from "ai";+import { blobToString } from "@/utils/fileToBase64";++export function generateChatTitle(message: string): string {+ return message.length > 50 ? `${message.slice(0, 50)}...` : message;+}++export async function convertToFileUIPart(+ files: File[],+): Promise<FileUIPart[]> {+ const fileUIParts = await Promise.all(+ files.map(async (file) => {+ const part: FileUIPart = {+ type: "file" as const,+ mediaType: file.type,+ filename: file.name,+ url: await blobToString(file, "dataUrl"),+ };+ return part;+ }),+ );++ return fileUIParts;+}++export function isLastMessageReasoning(messages: UIMessage[]): boolean {+ if (messages.length === 0) {+ return false;+ }++ const lastMessage = messages.at(-1);+ if (!lastMessage) {+ return false;+ }++ if (lastMessage.role !== "assistant" || !lastMessage.parts) {+ return false;+ }++ const parts = lastMessage.parts;+ if (parts.length === 0) {+ return false;+ }++ // Check if the last part is reasoning+ const lastPart = parts[parts.length - 1];+ return lastPart.type === "reasoning";+}diff --git a/frontend/src/components/chat/tool-call-accordion.tsx b/frontend/src/components/chat/tool-call-accordion.tsxindex 72364a5ef7..f41706c8d7 100644--- a/frontend/src/components/chat/tool-call-accordion.tsx+++ b/frontend/src/components/chat/tool-call-accordion.tsx@@ -1,5 +1,6 @@/* Copyright 2024 Marimo. All rights reserved. */+import type { ToolUIPart } from "ai";import {CheckCircleIcon,Loader2,@@ -17,10 +18,10 @@ import { cn } from "@/utils/cn";interface ToolCallAccordionProps {toolName: string;- result?: string | null;+ result: unknown;error?: string;index?: number;- state?: "partial-call" | "call" | "result";+ state?: ToolUIPart["state"];}export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({@@ -30,7 +31,7 @@ export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({index = 0,state,}) => {- const hasResult = state === "result" && (result || error);+ const hasResult = state === "output-available" && (result || error);const status = error ? "error" : hasResult ? "success" : "loading";const getStatusIcon = () => {@@ -84,8 +85,7 @@ export const ToolCallAccordion: React.FC<ToolCallAccordionProps> = ({{/* Only show content when tool is complete */}{hasResult && (<div className="space-y-3">- {/* Result */}- {result && (+ {result !== undefined && result !== null && (<div><div className="text-xs font-medium text-muted-foreground mt-2 mb-1">Result:diff --git a/frontend/src/components/editor/ai/add-cell-with-ai.tsx b/frontend/src/components/editor/ai/add-cell-with-ai.tsxindex 1711722b2f..94550f3d37 100644--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx@@ -1,5 +1,6 @@/* Copyright 2024 Marimo. All rights reserved. */+import { useCompletion } from "@ai-sdk/react";import {autocompletion,type Completion,@@ -16,7 +17,6 @@ import ReactCodeMirror, {minimalSetup,type ReactCodeMirrorRef,} from "@uiw/react-codemirror";-import { useCompletion } from "ai/react";import { useAtom, useAtomValue, useStore } from "jotai";import { atomWithStorage } from "jotai/utils";import {diff --git a/frontend/src/core/ai/chat-utils.ts b/frontend/src/core/ai/chat-utils.tsindex f6b9e0656e..18baa13296 100644--- a/frontend/src/core/ai/chat-utils.ts+++ b/frontend/src/core/ai/chat-utils.ts@@ -1,29 +1,29 @@/* Copyright 2024 Marimo. All rights reserved. */-import type { Message as AIMessage } from "@ai-sdk/react";+import type { UIMessage } from "@ai-sdk/react";import { Logger } from "@/utils/Logger";import type { ChatId, ChatState } from "./state";-import type { ChatAttachment } from "./types";-interface AddMessageToChatParams {+interface ReplaceMessagesInChatParams {+ /**+ * The state of the chats+ */chatState: ChatState;+ /**+ * The chat to replace the messages in+ */chatId: ChatId | null;- messageId: string;- role: "user" | "assistant";- content: string;- parts?: AIMessage["parts"];- attachments?: ChatAttachment[];+ /**+ * The messages to replace in the chat+ */+ messages: UIMessage[];}-export const addMessageToChat = ({+export const replaceMessagesInChat = ({chatState,chatId,- messageId,- role,- content,- parts,- attachments,-}: AddMessageToChatParams): ChatState => {+ messages,+}: ReplaceMessagesInChatParams): ChatState => {if (!chatId) {Logger.warn("No active chat");return chatState;@@ -34,46 +34,15 @@ export const addMessageToChat = ({return chatState;}- const messageIndex = chat.messages.findIndex(- (message) => message.id === messageId,- );-// Create copy of chats to modifyconst newChats = new Map(chatState.chats);const timestamp = Date.now();- if (messageIndex === -1) {- // Handle new message- newChats.set(chatId, {- ...chat,- messages: [- ...chat.messages,- {- id: messageId,- role,- content,- timestamp: timestamp,- parts,- attachments,- },- ],- updatedAt: timestamp,- });- } else {- // Handle update message- const newMessages = [...chat.messages];- newMessages[messageIndex] = {- ...newMessages[messageIndex],- content,- parts,- attachments,- };- newChats.set(chat.id, {- ...chat,- messages: newMessages,- updatedAt: timestamp,- });- }+ newChats.set(chat.id, {+ ...chat,+ messages: messages,+ updatedAt: timestamp,+ });return {...chatState,diff --git a/frontend/src/core/ai/state.ts b/frontend/src/core/ai/state.tsindex c4a8123de3..c478ece0b4 100644--- a/frontend/src/core/ai/state.ts+++ b/frontend/src/core/ai/state.ts@@ -1,6 +1,6 @@/* Copyright 2024 Marimo. All rights reserved. */-import type { Message as AIMessage } from "@ai-sdk/react";+import type { UIMessage } from "@ai-sdk/react";import { atom } from "jotai";import { atomWithStorage } from "jotai/utils";import { adaptForLocalStorage } from "@/utils/storage";@@ -8,7 +8,7 @@ import type { TypedString } from "@/utils/typed";import type { CellId } from "../cells/ids";import type { ChatAttachment } from "./types";-const KEY = "marimo:ai:chatState:v4";+const KEY = "marimo:ai:chatState:v5";export type ChatId = TypedString<"ChatId">;@@ -28,14 +28,14 @@ export interface Message {role: "user" | "assistant" | "data" | "system";content: string;timestamp: number;- parts?: AIMessage["parts"];+ parts?: UIMessage["parts"];attachments?: ChatAttachment[];}export interface Chat {id: ChatId;title: string;- messages: Message[];+ messages: UIMessage[];createdAt: number;updatedAt: number;}@@ -45,6 +45,12 @@ export interface ChatState {activeChatId: ChatId | null;}+function removeEmptyChats(chatState: Map<ChatId, Chat>): Map<ChatId, Chat> {+ return new Map(+ [...chatState.entries()].filter(([_, chat]) => chat.messages.length > 0),+ );+}+export const chatStateAtom = atomWithStorage<ChatState>(KEY,{@@ -53,7 +59,7 @@ export const chatStateAtom = atomWithStorage<ChatState>(},adaptForLocalStorage({toSerializable: (value: ChatState) => ({- chats: [...value.chats.entries()],+ chats: [...removeEmptyChats(value.chats).entries()],activeChatId: value.activeChatId,}),fromSerializable: (value) => ({diff --git a/frontend/src/plugins/impl/chat/chat-ui.tsx b/frontend/src/plugins/impl/chat/chat-ui.tsxindex c3d0aafac9..af029f30b6 100644--- a/frontend/src/plugins/impl/chat/chat-ui.tsx+++ b/frontend/src/plugins/impl/chat/chat-ui.tsx@@ -1,10 +1,10 @@/* Copyright 2024 Marimo. All rights reserved. */-import { useChat } from "@ai-sdk/react";+import { type UIMessage, useChat } from "@ai-sdk/react";import { ChatBubbleIcon } from "@radix-ui/react-icons";import { PopoverAnchor } from "@radix-ui/react-popover";import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";-import type { Message } from "ai/react";+import { DefaultChatTransport } from "ai";import { startCase } from "lodash-es";import {BotMessageSquareIcon,@@ -18,6 +18,7 @@ import {X,} from "lucide-react";import React, { useEffect, useRef, useState } from "react";+import { convertToFileUIPart } from "@/components/chat/chat-utils";import {type AdditionalCompletions,PromptInput,@@ -50,7 +51,7 @@ import { Logger } from "@/utils/Logger";import { Objects } from "@/utils/objects";import { ErrorBanner } from "../common/error-banner";import type { PluginFunctions } from "./ChatPlugin";-import type { ChatConfig, ChatMessage, ChatRole } from "./types";+import type { ChatConfig, ChatMessage } from "./types";interface Props extends PluginFunctions {prompts: string[];@@ -63,8 +64,9 @@ interface Props extends PluginFunctions {}export const Chatbot: React.FC<Props> = (props) => {+ const [input, setInput] = useState("");const [config, setConfig] = useState<ChatConfig>(props.config);- const [files, setFiles] = useState<FileList | undefined>(undefined);+ const [files, setFiles] = useState<File[] | undefined>(undefined);const fileInputRef = useRef<HTMLInputElement>(null);const formRef = useRef<HTMLFormElement>(null);const codeMirrorInputRef = useRef<ReactCodeMirrorRef>(null);@@ -72,75 +74,75 @@ export const Chatbot: React.FC<Props> = (props) => {const { data: initialMessages } = useAsyncData(async () => {const chatMessages = await props.get_chat_history({});- const messages: Message[] = chatMessages.messages.map((message, idx) => ({+ const messages: UIMessage[] = chatMessages.messages.map((message, idx) => ({id: idx.toString(),role: message.role,- content: message.content,- experimental_attachments: message.attachments,+ parts: [{ type: "text", text: message.content }],}));return messages;}, []);const {messages,+ sendMessage,setMessages,- input,- setInput,- handleSubmit,status,stop,error,- reload,+ regenerate,} = useChat({- keepLastMessageOnError: true,- streamProtocol: "text",- fetch: async (_url, request) => {- const body = JSON.parse(request?.body as string) as {- messages: Message[];- };- try {- const response = await props.send_prompt({- messages: body.messages.map((m) => ({- role: m.role as ChatRole,- content: m.content,- attachments: m.experimental_attachments,- })),- config: {- max_tokens: config.max_tokens,- temperature: config.temperature,- top_p: config.top_p,- top_k: config.top_k,- frequency_penalty: config.frequency_penalty,- presence_penalty: config.presence_penalty,- },- });- return new Response(response);- // eslint-disable-next-line @typescript-eslint/no-explicit-any- } catch (error: any) {- // HACK: strip the error message to clean up the response- const strippedError = error.message- .split("failed with exception ")- .pop();- return new Response(strippedError, { status: 400 });- }- },- initialMessages: initialMessages,- onFinish: (message, { usage, finishReason }) => {+ transport: new DefaultChatTransport({+ fetch: async (+ request: RequestInfo | URL,+ init: RequestInit | undefined,+ ) => {+ if (init === undefined) {+ return fetch(request);+ }++ const body = JSON.parse(init.body as unknown as string) as {+ messages: UIMessage[];+ };+ try {+ const response = await props.send_prompt({+ messages: body.messages.map((m) => ({+ role: m.role,+ content: m.parts+ ?.map((p) => ("text" in p ? p.text : ""))+ .join("\n"),+ })),+ config: {+ max_tokens: config.max_tokens,+ temperature: config.temperature,+ top_p: config.top_p,+ top_k: config.top_k,+ frequency_penalty: config.frequency_penalty,+ presence_penalty: config.presence_penalty,+ },+ });+ return new Response(response);+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ } catch (error: any) {+ // HACK: strip the error message to clean up the response+ const strippedError = error.message+ .split("failed with exception ")+ .pop();+ return new Response(strippedError, { status: 400 });+ }+ },+ }),+ messages: initialMessages,+ onFinish: (message) => {setFiles(undefined);if (fileInputRef.current) {fileInputRef.current.value = "";}Logger.debug("Finished streaming message:", message);- Logger.debug("Token usage:", usage);- Logger.debug("Finish reason:", finishReason);},onError: (error) => {Logger.error("An error occurred:", error);},- onResponse: (response) => {- Logger.debug("Received HTTP response from server:", response);- },});const isLoading = status === "submitted" || status === "streaming";@@ -178,13 +180,15 @@ export const Chatbot: React.FC<Props> = (props) => {);};- const renderMessage = (message: Message) => {+ const renderMessage = (message: UIMessage) => {+ const textParts = message.parts?.filter((p) => p.type === "text");+ const textContent = textParts?.map((p) => p.text).join("\n");const content =message.role === "assistant"- ? renderHTML({ html: message.content })- : message.content;+ ? renderHTML({ html: textContent })+ : textContent;- const attachments = message.experimental_attachments;+ const attachments = message.parts?.filter((p) => p.type === "file");return (<>@@ -200,7 +204,7 @@ export const Chatbot: React.FC<Props> = (props) => {size: "icon",})}href={attachment.url}- download={attachment.name}+ download={attachment.filename}><DownloadIcon className="size-3" /></a>@@ -270,50 +274,59 @@ export const Chatbot: React.FC<Props> = (props) => {</p></div>)}- {messages.map((message) => (- <div- key={message.id}- className={cn(- "flex flex-col group gap-2",- message.role === "user" ? "items-end" : "items-start",- )}- >+ {messages.map((message) => {+ const textContent = message.parts+ ?.filter((p) => p.type === "text")+ .map((p) => p.text)+ .join("\n");++ return (<div- className={`max-w-[80%] p-3 rounded-lg ${- message.role === "user"- ? "bg-(--sky-11) text-(--slate-1)"- : "bg-(--slate-4) text-(--slate-12)"- }`}+ key={message.id}+ className={cn(+ "flex flex-col group gap-2",+ message.role === "user" ? "items-end" : "items-start",+ )}>- <p- className={cn(message.role === "user" && "whitespace-pre-wrap")}- >- {renderMessage(message)}- </p>- </div>- <div className="flex justify-end text-xs gap-2 invisible group-hover:visible">- <button- type="button"- onClick={async () => {- await copyToClipboard(message.content);- toast({- title: "Copied to clipboard",- });- }}- className="text-xs text-(--slate-9) hover:text-(--slate-11)"+ <div+ className={`max-w-[80%] p-3 rounded-lg ${+ message.role === "user"+ ? "bg-(--sky-11) text-(--slate-1)"+ : "bg-(--slate-4) text-(--slate-12)"+ }`}>- <ClipboardIcon className="h-3 w-3" />- </button>- <button- type="button"- onClick={() => handleDelete(message.id)}- className="text-xs text-(--slate-9) hover:text-(--slate-11)"- >- <Trash2Icon className="h-3 w-3 text-(--red-9)" />- </button>+ <p+ className={cn(+ message.role === "user" && "whitespace-pre-wrap",+ )}+ >+ {renderMessage(message)}+ </p>+ </div>+ <div className="flex justify-end text-xs gap-2 invisible group-hover:visible">+ <button+ type="button"+ onClick={async () => {+ await copyToClipboard(textContent);+ toast({+ title: "Copied to clipboard",+ });+ }}+ className="text-xs text-(--slate-9) hover:text-(--slate-11)"+ >+ <ClipboardIcon className="h-3 w-3" />+ </button>+ <button+ type="button"+ onClick={() => handleDelete(message.id)}+ className="text-xs text-(--slate-9) hover:text-(--slate-11)"+ >+ <Trash2Icon className="h-3 w-3 text-(--red-9)" />+ </button>+ </div></div>- </div>- ))}+ );+ })}{isLoading && (<div className="flex items-center justify-center space-x-2 mb-4">@@ -332,17 +345,22 @@ export const Chatbot: React.FC<Props> = (props) => {{error && (<div className="flex items-center justify-center space-x-2 mb-4"><ErrorBanner error={error} />- <Button variant="outline" size="sm" onClick={() => reload()}>+ <Button variant="outline" size="sm" onClick={() => regenerate()}>Retry</Button></div>)}</div>-<form- onSubmit={(evt) => {- handleSubmit(evt, {- experimental_attachments: files,+ onSubmit={async (evt) => {+ const fileParts = files+ ? await convertToFileUIPart(files)+ : undefined;++ evt.preventDefault();+ sendMessage({+ role: "user",+ parts: [{ type: "text", text: input }, ...(fileParts ?? [])],});}}ref={formRef}@@ -435,7 +453,7 @@ export const Chatbot: React.FC<Props> = (props) => {}onChange={(event) => {if (event.target.files) {- setFiles(event.target.files);+ setFiles([...event.target.files]);}}}/>diff --git a/frontend/src/plugins/impl/chat/types.ts b/frontend/src/plugins/impl/chat/types.tsindex 2c035bdcc7..d286e06c33 100644--- a/frontend/src/plugins/impl/chat/types.ts+++ b/frontend/src/plugins/impl/chat/types.ts@@ -1,12 +1,10 @@/* Copyright 2024 Marimo. All rights reserved. */-import type { ChatAttachment } from "@/core/ai/types";export type ChatRole = "system" | "user" | "assistant";export interface ChatMessage {role: ChatRole;content: string;- attachments?: ChatAttachment[];}export interface SendMessageRequest {diff --git a/marimo/_ai/_convert.py b/marimo/_ai/_convert.pyindex a9c250ae3e..1d76d7c40a 100644--- a/marimo/_ai/_convert.py+++ b/marimo/_ai/_convert.py@@ -3,11 +3,13 @@ from __future__ import annotationsimport base64import json+import uuidfrom dataclasses import asdict-from typing import TYPE_CHECKING, Any, Literal, Union, cast+from typing import TYPE_CHECKING, Any, Literal, Optional, Union, castfrom marimo._ai._types import (ChatMessage,+ ChatPart,ReasoningPart,TextPart,ToolInvocationPart,@@ -31,7 +33,7 @@ if TYPE_CHECKING:# Message conversionsdef get_openai_messages_from_parts(role: Literal["system", "user", "assistant"],- parts: list[Union[TextPart, ReasoningPart, ToolInvocationPart]],+ parts: list[ChatPart],) -> list[dict[str, Any]]:messages: list[dict[str, Any]] = []for part in parts:@@ -45,12 +47,12 @@ def get_openai_messages_from_parts("content": None,"tool_calls": [{- "id": part.tool_invocation.tool_call_id,+ "id": part.tool_call_id,"type": "function","function": {- "name": part.tool_invocation.tool_name,- "arguments": str(part.tool_invocation.args)- if part.tool_invocation.args+ "name": part.tool_name,+ "arguments": str(part.input)+ if part.inputelse "{}",},}@@ -59,9 +61,9 @@ def get_openai_messages_from_parts(messages.append(assistant_message)tool_result_message = {"role": "tool",- "tool_call_id": part.tool_invocation.tool_call_id,- "name": part.tool_invocation.tool_name,- "content": str(part.tool_invocation.result),+ "tool_call_id": part.tool_call_id,+ "name": part.tool_name,+ "content": str(part.output),}messages.append(tool_result_message)return messages@@ -118,7 +120,7 @@ def convert_to_openai_messages(def get_anthropic_messages_from_parts(role: Literal["system", "user", "assistant"],- parts: list[Union[TextPart, ReasoningPart, ToolInvocationPart]],+ parts: list[ChatPart],) -> list[dict[Any, Any]]:messages: list[dict[Any, Any]] = []content_parts: list[dict[str, Any]] = []@@ -144,9 +146,9 @@ def get_anthropic_messages_from_parts(content_parts.append({"type": "tool_use",- "id": part.tool_invocation.tool_call_id,- "name": part.tool_invocation.tool_name,- "input": part.tool_invocation.args,+ "id": part.tool_call_id,+ "name": part.tool_name,+ "input": part.input,})@@ -167,8 +169,8 @@ def get_anthropic_messages_from_parts("content": [{"type": "tool_result",- "tool_use_id": part.tool_invocation.tool_call_id,- "content": str(part.tool_invocation.result),+ "tool_use_id": part.tool_call_id,+ "content": str(part.output),}],}@@ -274,7 +276,7 @@ def convert_to_groq_messages(def get_google_messages_from_parts(role: Literal["system", "user", "assistant"],- parts: list[Union[TextPart, ReasoningPart, ToolInvocationPart]],+ parts: list[ChatPart],) -> list[ContentDict]:messages: list[ContentDict] = []@@ -301,8 +303,8 @@ def get_google_messages_from_parts("parts": [{"function_call": {- "name": part.tool_invocation.tool_name,- "args": part.tool_invocation.args or {},+ "name": part.tool_name,+ "args": part.input or {},}}],@@ -315,10 +317,8 @@ def get_google_messages_from_parts("parts": [{"function_response": {- "name": part.tool_invocation.tool_name,- "response": {- "result": str(part.tool_invocation.result)- },+ "name": part.tool_name,+ "response": {"result": str(part.output)},}}],@@ -420,7 +420,11 @@ def convert_to_ai_sdk_messages(content_text: Union[str, dict[str, Any]],content_type: Literal["text",+ "text_start",+ "text_end","reasoning",+ "reasoning_start",+ "reasoning_end","reasoning_signature","tool_call_start","tool_call_delta",@@ -429,59 +433,77 @@ def convert_to_ai_sdk_messages("finish_reason","error",],+ text_id: Optional[str] = None,) -> str:"""- Format text events for the AI SDK stream protocol.- This follows the data-stream v1 protocol- See: https://v4.ai-sdk.dev/docs/ai-sdk-ui/stream-protocol+ Format events for the AI SDK v5 stream protocol using Server-Sent Events.+ This follows the data-stream v1 protocol with SSE format.+ See: https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol"""- TEXT_PREFIX = "0:"- REASON_PREFIX = "g:"- REASON_SIGNATURE_PREFIX = "j:"- TOOL_CALL_START_PREFIX = "b:"- TOOL_CALL_DELTA_PREFIX = "c:"- TOOL_CALL_PREFIX = "9:"- TOOL_RESULT_PREFIX = "a:"- FINISH_REASON_PREFIX = "d:"- ERROR_PREFIX = "3:"-- # Text events++ # Text events - use start/delta/end pattern with unique IDsif content_type == "text" and isinstance(content_text, str):- return f"{TEXT_PREFIX}{json.dumps(content_text)}\n"+ if text_id is None:+ text_id = f"text_{uuid.uuid4().hex}"+ return f"data: {json.dumps({'type': 'text-delta', 'id': text_id, 'delta': content_text})}\n\n"++ elif content_type == "text_start":+ if text_id is None:+ text_id = f"text_{uuid.uuid4().hex}"+ return f"data: {json.dumps({'type': 'text-start', 'id': text_id})}\n\n"++ elif content_type == "text_end" and text_id is not None:+ return f"data: {json.dumps({'type': 'text-end', 'id': text_id})}\n\n"++ # Reasoning events - use start/delta/end pattern with unique IDselif content_type == "reasoning" and isinstance(content_text, str):- return f"{REASON_PREFIX}{json.dumps(content_text)}\n"+ if text_id is None:+ text_id = f"reasoning_{uuid.uuid4().hex}"+ return f"data: {json.dumps({'type': 'reasoning-delta', 'id': text_id, 'delta': content_text})}\n\n"++ elif content_type == "reasoning_start":+ if text_id is None:+ text_id = f"reasoning_{uuid.uuid4().hex}"+ return f"data: {json.dumps({'type': 'reasoning-start', 'id': text_id})}\n\n"++ elif content_type == "reasoning_end" and text_id is not None:+ return (+ f"data: {json.dumps({'type': 'reasoning-end', 'id': text_id})}\n\n"+ )# Tool use eventselif content_type == "tool_call_start" and isinstance(content_text, dict):- return f"{TOOL_CALL_START_PREFIX}{json.dumps(content_text)}\n"+ return f"data: {json.dumps({'type': 'tool-input-start', **content_text})}\n\n"+elif content_type == "tool_call_delta" and isinstance(content_text, dict):- return f"{TOOL_CALL_DELTA_PREFIX}{json.dumps(content_text)}\n"+ return f"data: {json.dumps({'type': 'tool-input-delta', **content_text})}\n\n"+elif content_type == "tool_call_end" and isinstance(content_text, dict):- return f"{TOOL_CALL_PREFIX}{json.dumps(content_text)}\n"+ return f"data: {json.dumps({'type': 'tool-input-available', **content_text})}\n\n"+elif content_type == "tool_result" and isinstance(content_text, dict):- return f"{TOOL_RESULT_PREFIX}{json.dumps(content_text)}\n"+ return f"data: {json.dumps({'type': 'tool-output-available', **content_text})}\n\n"++ # Finish events+ elif content_type == "finish_reason":+ return f"data: {json.dumps({'type': 'finish'})}\n\n"- # Other events- elif content_type == "finish_reason" and content_text in [- "tool_calls",- "stop",- ]:- # Emit the finishReason as a JSON object with usage (default 0s)- # TODO: Add usage (promptTokens, completionTokens)- return f'{FINISH_REASON_PREFIX}{{"finishReason": "{content_text}", "usage": {{"promptTokens": 0, "completionTokens": 0}}}}\n'+ # Error events+ elif content_type == "error" and isinstance(content_text, str):+ return f"data: {json.dumps({'type': 'error', 'errorText': content_text})}\n\n"+ # Reasoning signature (for Anthropic thinking models)elif content_type == "reasoning_signature" and isinstance(content_text, dict):- return f"{REASON_SIGNATURE_PREFIX}{json.dumps(content_text)}\n"-- # Error events- elif content_type == "error" and isinstance(content_text, str):- return f"{ERROR_PREFIX}{json.dumps(content_text)}\n"+ # This might be handled differently in the new protocol+ return f"data: {json.dumps({'type': 'data-reasoning-signature', 'data': content_text})}\n\n"else:- # Default to text for unknown types- return f"{TEXT_PREFIX}{json.dumps(content_text)}\n"+ # Default to text delta for unknown types+ if text_id is None:+ text_id = f"text_{uuid.uuid4().hex}"+ return f"data: {json.dumps({'type': 'text-delta', 'id': text_id, 'delta': str(content_text)})}\n\n"# Tool conversionsdiff --git a/marimo/_ai/_types.py b/marimo/_ai/_types.pyindex 2dea099675..c2d03095e4 100644--- a/marimo/_ai/_types.py+++ b/marimo/_ai/_types.py@@ -30,27 +30,22 @@ class ReasoningDetailsDict(TypedDict):signature: Optional[str]-class ToolInvocationResultDict(TypedDict):- state: Literal["result"]- result: Any+class ToolInvocationPartDict(TypedDict):+ type: strtool_call_id: str- tool_name: str- step: int- args: dict[str, Any]+ state: str+ input: dict[str, Any]+ output: Optional[Any]-class ToolInvocationPartDict(TypedDict):- type: Literal["tool-invocation"]- tool_invocation: ToolInvocationResultDict+ChatPartDict = Union[TextPartDict, ReasoningPartDict, ToolInvocationPartDict]class ChatMessageDict(TypedDict):role: Literal["user", "assistant", "system"]content: strattachments: Optional[list[ChatAttachmentDict]]- parts: Optional[- list[Union[TextPartDict, ReasoningPartDict, ToolInvocationPartDict]]- ]+ parts: Optional[list[ChatPartDict]]class ChatModelConfigDict(TypedDict, total=False):@@ -110,23 +105,29 @@ class ReasoningDetails:@dataclass-class ToolInvocationResult:- """Represents a tool invocation result part from the AI SDK."""+class ToolInvocationPart:+ """Represents a tool invocation part from the AI SDK."""- state: Literal["result"]- result: Any+ type: str # Starts with "tool-"tool_call_id: str- tool_name: str- step: int- args: dict[str, Any]+ state: Union[str, Literal["output-available"]]+ input: dict[str, Any]+ output: Optional[Any]++ @property+ def tool_name(self) -> str:+ return self.type.split("-", 1)[1]@dataclass-class ToolInvocationPart:- """Represents a tool invocation part from the AI SDK."""+class FilePart:+ type: Literal["file"]+ media_type: Optional[str]+ filename: Optional[str]+ url: str+- type: Literal["tool-invocation"]- tool_invocation: ToolInvocationResult+ChatPart = Union[TextPart, ReasoningPart, ToolInvocationPart, FilePart]@dataclass@@ -145,9 +146,7 @@ class ChatMessage:attachments: Optional[list[ChatAttachment]] = None# Optional parts from AI SDK. (see types above)- parts: Optional[- list[Union[TextPart, ReasoningPart, ToolInvocationPart]]- ] = None+ parts: Optional[list[ChatPart]] = None@dataclassdiff --git a/marimo/_plugins/ui/_impl/chat/utils.py b/marimo/_plugins/ui/_impl/chat/utils.pyindex 6debd4a2e1..a42d5212a4 100644--- a/marimo/_plugins/ui/_impl/chat/utils.py+++ b/marimo/_plugins/ui/_impl/chat/utils.py@@ -1,17 +1,20 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations-from typing import Optional, Union+from typing import Optional, castfrom marimo._ai._types import (ChatAttachment,ChatMessage,ChatMessageDict,+ ChatPart,ReasoningDetails,ReasoningPart,+ ReasoningPartDict,TextPart,+ TextPartDict,ToolInvocationPart,- ToolInvocationResult,+ ToolInvocationPartDict,)@@ -35,18 +38,18 @@ def from_chat_message_dict(d: ChatMessageDict) -> ChatMessage:# Handle partsparts_dict = d.get("parts", None)- parts: Optional[- list[Union[TextPart, ReasoningPart, ToolInvocationPart]]- ] = None+ parts: Optional[list[ChatPart]] = Noneif parts_dict is not None:parts = []for part_dict in parts_dict:if part_dict["type"] == "text":+ part_dict = cast(TextPartDict, part_dict)parts.append(TextPart(type="text", text=part_dict["text"]))elif part_dict["type"] == "reasoning":+ part_dict = cast(ReasoningPartDict, part_dict)# Handle ReasoningDetails if presentdetails_list = part_dict.get("details", [])- details = []+ details: list[ReasoningDetails] = []if details_list:for details_dict in details_list:@@ -74,25 +77,24 @@ def from_chat_message_dict(d: ChatMessageDict) -> ChatMessage:details=details,))- elif part_dict["type"] == "tool-invocation":- tool_inv = part_dict["tool_invocation"]- if tool_inv["state"] == "result":- tool_call = ToolInvocationResult(- state=tool_inv["state"],- tool_call_id=tool_inv["tool_call_id"],- tool_name=tool_inv["tool_name"],- step=tool_inv["step"],- args=tool_inv["args"],- result=tool_inv["result"],- )- parts.append(- ToolInvocationPart(- type="tool-invocation", tool_invocation=tool_call- )+ elif part_dict["type"].startswith("tool-"):+ part_dict = cast(ToolInvocationPartDict, part_dict)+ tool_inv = ToolInvocationPart(+ type=part_dict["type"],+ tool_call_id=part_dict["tool_call_id"],+ state=part_dict["state"],+ input=part_dict["input"],+ output=part_dict.get("output"),+ )+ parts.append(+ ToolInvocationPart(+ type=tool_inv.type,+ tool_call_id=tool_inv.tool_call_id,+ state=tool_inv.state,+ input=tool_inv.input,+ output=tool_inv.output,)- else:- # Skip unsupported tool invocation states- continue+ )return ChatMessage(role=d["role"],diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.pyindex 15d89292bf..bd67021f7d 100644--- a/marimo/_server/ai/providers.py+++ b/marimo/_server/ai/providers.py@@ -138,7 +138,11 @@ class CompletionProvider(Generic[ResponseT, StreamT], ABC):content_text, content_type = contentif content_type in ["text",+ "text_start",+ "text_end","reasoning",+ "reasoning_start",+ "reasoning_end","reasoning_signature","tool_call_start","tool_call_delta",@@ -231,6 +235,12 @@ class CompletionProvider(Generic[ResponseT, StreamT], ABC):# Finish reason collected from the last chunkfinish_reason: Optional[FinishReason] = None+ # Text block tracking for start/delta/end pattern+ current_text_id: Optional[str] = None+ current_reasoning_id: Optional[str] = None+ has_text_started = False+ has_reasoning_started = False+async for chunk in response:# Always check for finish reason first, before checking content# Some chunks (like RawMessageDeltaEvent) contain finish reasons but no extractable content@@ -246,6 +256,46 @@ class CompletionProvider(Generic[ResponseT, StreamT], ABC):if options.text_only and content_type != "text":continue+ # Handle text content with start/delta/end pattern+ if (+ content_type == "text"+ and isinstance(content_data, str)+ and options.format_stream+ ):+ if not has_text_started:+ # Emit text-start event+ current_text_id = f"text_{uuid.uuid4().hex}"+ yield convert_to_ai_sdk_messages(+ "", "text_start", current_text_id+ )+ has_text_started = True++ # Emit text-delta event with the actual content+ yield convert_to_ai_sdk_messages(+ content_data, "text", current_text_id+ )+ continue++ # Handle reasoning content with start/delta/end pattern+ elif (+ content_type == "reasoning"+ and isinstance(content_data, str)+ and options.format_stream+ ):+ if not has_reasoning_started:+ # Emit reasoning-start event+ current_reasoning_id = f"reasoning_{uuid.uuid4().hex}"+ yield convert_to_ai_sdk_messages(+ "", "reasoning_start", current_reasoning_id+ )+ has_reasoning_started = True++ # Emit reasoning-delta event with the actual content+ yield convert_to_ai_sdk_messages(+ content_data, "reasoning", current_reasoning_id+ )+ continue+# Tool handlingif content_type == "tool_call_start" and isinstance(content_data, dict@@ -274,7 +324,7 @@ class CompletionProvider(Generic[ResponseT, StreamT], ABC):# based on https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#tool-call-delta-partcontent_data = {"toolCallId": tool_call_id,- "argsTextDelta": content_data,+ "inputTextDelta": content_data,}content_str = self._content_to_string(content_data)@@ -291,12 +341,26 @@ class CompletionProvider(Generic[ResponseT, StreamT], ABC):yield bufferbuffer = ""+ # Emit text-end event if we started a text block+ if has_text_started and current_text_id and options.format_stream:+ yield convert_to_ai_sdk_messages("", "text_end", current_text_id)++ # Emit reasoning-end event if we started a reasoning block+ if (+ has_reasoning_started+ and current_reasoning_id+ and options.format_stream+ ):+ yield convert_to_ai_sdk_messages(+ "", "reasoning_end", current_reasoning_id+ )+# Handle tool call end after the stream is completeif tool_call_id and tool_call_name and not options.text_only:content_data = {"toolCallId": tool_call_id,"toolName": tool_call_name,- "args": self.validate_tool_call_args(tool_call_args)+ "input": self.validate_tool_call_args(tool_call_args)or {}, # empty object if tool doesnt have args}content_type = "tool_call_end"diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 90eb050668..507fd036e1 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -32,8 +32,8 @@ importers:frontend:dependencies:'@ai-sdk/react':- specifier: ^1.2.12- version: 1.2.12([email protected])([email protected])+ specifier: ^2.0.30+ version: 2.0.30([email protected])([email protected])'@anywidget/types':specifier: ^0.2.0version: 0.2.0@@ -287,8 +287,8 @@ importers:specifier: ^5.5.0version: 5.5.0ai:- specifier: ^4.3.16- version: 4.3.19([email protected])([email protected])+ specifier: ^5.0.30+ version: 5.0.30([email protected])ansi_up:specifier: ^6.0.6version: 6.0.6@@ -720,32 +720,32 @@ packages:'@adobe/[email protected]':resolution: {integrity: sha512-VQKMkwriZbaOgVCby1UDY/LDk5fIjhQicCvVPFqfe+69fWaPWydbWJ3wRt59/YzIwda1I81loas3oCoHxnqvdA==}- '@ai-sdk/[email protected]':- resolution: {integrity: sha512-fqhG+4sCVv8x7nFzYnFo19ryhAa3w096Kmc3hWxMQfW/TubPOmt3A6tYZhl4mUfQWWQMsuSkLrtjlWuXBVSGQA==}+ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-xySXoQ29+KbGuGfmDnABx+O6vc7Gj7qugmj1kGpn0rW0rQNn6UKUuvscKMzWyv1Uv05GyC1vqHq8ZhEOLfXscQ==}engines: {node: '>=18'}peerDependencies:- zod: ^3.23.8+ zod: ^3.25.76 || ^4- '@ai-sdk/[email protected]':- resolution: {integrity: sha512-qZMxYJ0qqX/RfnuIaab+zp8UAeJn/ygXXAffR5I4N0n1IrvA6qBsjc8hXLmBiMV2zoXlifkacF7sEFnYnjBcqg==}+ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-o3BS5/t8KnBL3ubP8k3w77AByOypLm+pkIL/DCw0qKkhDbvhCy+L3hRTGPikpdb8WHcylAeKsjgwOxhj4cqTUA==}+ engines: {node: '>=18'}+ peerDependencies:+ zod: ^3.25.76 || ^4++ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-6o7Y2SeO9vFKB8lArHXehNuusnpddKPk7xqL7T2/b+OvXMRIXUO1rR4wcv1hAFUAT9avGZshty3Wlua/XA7TvA==}engines: {node: '>=18'}- '@ai-sdk/[email protected]':- resolution: {integrity: sha512-jK1IZZ22evPZoQW3vlkZ7wvjYGYF+tRBKXtrcolduIkQ/m/sOAVcVeVDUDvh1T91xCnWCdUGCPZg2avZ90mv3g==}+ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-3DepYWZqR4RgowpVYllsIWDCiYl3lHWN1w4fp+uLIvyqlWnTZqa3PWdEWjHbxNt98Y9Vh0VpVQKyAN7t3+to1Q==}engines: {node: '>=18'}peerDependencies:react: ^18 || ^19 || ^19.0.0-rc- zod: ^3.23.8+ zod: ^3.25.76 || ^4peerDependenciesMeta:zod:optional: true- '@ai-sdk/[email protected]':- resolution: {integrity: sha512-3zcwCc8ezzFlwp3ZD15wAPjf2Au4s3vAbKsXQVyhxODHcmu0iyPO2Eua6D/vicq/AUm/BAo60r97O6HU+EI0+w==}- engines: {node: '>=18'}- peerDependencies:- zod: ^3.23.8-'@alloc/[email protected]':resolution: {integrity: sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==}engines: {node: '>=10'}@@ -3407,6 +3407,9 @@ packages:'@sourcegraph/[email protected]':resolution: {integrity: sha512-EJLq/ni66glk3xYyOZtUIEbjTCw8kMI6RvO0YQtPd+4um2+aTSM1LfN4NrsiVrRkG7EG/U2OkFlKqT8mGo6w4Q==}+ '@standard-schema/[email protected]':+ resolution: {integrity: sha512-m2bOd0f2RT9k8QJx1JN85cZYyH1RqFBdlwtkSlf4tBDYLCiiZnv1fIIwacK6cqwXavOydf0NPToMQgpKq+dVlA==}+'@storybook/[email protected]':resolution: {integrity: sha512-iCzuHRyUgir2+ExqPO4ouxm90zW+6dkNuB4lyyFwNU10slJhVT8yGPk8PVOT6LhXMIii+7Hqc4dB0tj+kLOW/A==}peerDependencies:@@ -3834,9 +3837,6 @@ packages:'@types/[email protected]':resolution: {integrity: sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==}- '@types/[email protected]':- resolution: {integrity: sha512-xFdR6tkm0MWvBfO8xXCSsinYxHcqkQUlcHeSpMC2ukzOb6lwQAfDmW+Qt0AvlGd8HpsS28qKsB+oPeJn9I39jg==}-'@types/[email protected]':resolution: {integrity: sha512-eOIHzCUSH7SMfonMG1LsC2f8vxBFtho6NGBznK41R84YzPuvSBzrhEps33IsQiOW9+VL6NQ9DbjQJznk/S4uRA==}@@ -4282,15 +4282,11 @@ packages:resolution: {integrity: sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==}engines: {node: '>= 14'}- resolution: {integrity: sha512-dIE2bfNpqHN3r6IINp9znguYdhIOheKW2LDigAMrgt/upT3B8eBGPSCblENvaZGoq+hxaN9fSMzjWpbqloP+7Q==}+ resolution: {integrity: sha512-u7WdsDde9BasP+h8Q64CtU32GFShCYmxVtBa2h5dxM1f0w/AMKwzpmIDI1t3M3ean+L6uBiwOtRs8B2KA+OHgQ==}engines: {node: '>=18'}peerDependencies:- react: ^18 || ^19 || ^19.0.0-rc- zod: ^3.23.8- peerDependenciesMeta:- react:- optional: true+ zod: ^3.25.76 || ^4resolution: {integrity: sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==}@@ -4643,10 +4639,6 @@ packages:resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==}engines: {node: '>=10'}- resolution: {integrity: sha512-zgVZuo2WcZgfUEmsn6eO3kINexW8RAE4maiQ8QNs8CtpPCSyMiYsULR3HQYkm3w8FIA3SberyMJMSldGsW+U3w==}- engines: {node: ^12.17.0 || ^14.13 || >=16.0.0}-resolution: {integrity: sha512-HRQyTk2/YPEkt9TnUPbOpr64Uw3KOicFWPVBb+xiHvd6eBx/qPr9xqfBFDT8P2vWsvvz4jbEkfDe71W3VyNu2w==}@@ -5354,9 +5346,6 @@ packages:resolution: {integrity: sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA==}- resolution: {integrity: sha512-IayShXAgj/QMXgB0IWmKx+rOPuGMhqm5w6jvFxmVenXKIzRqTAAsbBPT3kWQeGANj3jGgvcvv4yK6SxqYmikgw==}-resolution: {integrity: sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==}engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}@@ -6674,11 +6663,6 @@ packages:engines: {node: '>=6'}hasBin: true- resolution: {integrity: sha512-3QItJOXp2AP1uv7waBkao5nCvhEv+QmJAd38Ybq7wNI74Q+BBmnLn4EDKz6yI9xGAIQoUF87qHt+kc1IVxB4zQ==}- engines: {node: ^18.0.0 || >=20.0.0}- hasBin: true-resolution: {integrity: sha512-2/Ki0GcmuqSrgFyelQq9M05y7PS0mEwuIzrf3f1fPqkVDVRvZrPZtVSMHxdgo8Aq0sxAOb/cr2aqqA3LeWHVPg==}@@ -8400,9 +8384,6 @@ packages:resolution: {integrity: sha512-Gn/JaSk/Mt9gYubxTtSn/QCV4em9mpAPiR1rqy/Ocu19u/G9J5WWdNoUT4SiV6mFC3y6cxyFcFwdzPM3FgxGAQ==}engines: {node: '>= 10.13.0'}- resolution: {integrity: sha512-6aU+Rwsezw7VR8/nyvKTx8QpWH9FrcYiXXlqC4z5d5XQBDRqtbfsRjnwGyqbi3gddNtWHuEk9OANUotL26qKUw==}-resolution: {integrity: sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==}hasBin: true@@ -9894,34 +9875,33 @@ snapshots:'@adobe/[email protected]': {}- '@ai-sdk/[email protected]([email protected])':+ '@ai-sdk/[email protected]([email protected])':dependencies:- '@ai-sdk/provider': 1.1.3- nanoid: 3.3.11- secure-json-parse: 2.7.0+ '@ai-sdk/provider': 2.0.0+ '@ai-sdk/provider-utils': 3.0.7([email protected])+ zod: 3.25.76++ '@ai-sdk/[email protected]([email protected])':+ dependencies:+ '@ai-sdk/provider': 2.0.0+ '@standard-schema/spec': 1.0.0+ eventsource-parser: 3.0.5zod: 3.25.76- '@ai-sdk/[email protected]':+ '@ai-sdk/[email protected]':dependencies:json-schema: 0.4.0dependencies:- '@ai-sdk/provider-utils': 2.2.8([email protected])- '@ai-sdk/ui-utils': 1.2.11([email protected])+ '@ai-sdk/provider-utils': 3.0.7([email protected])+ ai: 5.0.30([email protected])react: 19.1.0swr: 2.3.4([email protected])throttleit: 2.1.0optionalDependencies:zod: 3.25.76- '@ai-sdk/[email protected]([email protected])':- dependencies:- '@ai-sdk/provider': 1.1.3- '@ai-sdk/provider-utils': 2.2.8([email protected])- zod: 3.25.76- zod-to-json-schema: 3.24.6([email protected])-'@alloc/[email protected]': {}'@ampproject/[email protected]':@@ -13396,6 +13376,8 @@ snapshots:dependencies:vscode-jsonrpc: 4.0.0+ '@standard-schema/[email protected]': {}+'@storybook/[email protected](@types/[email protected])([email protected](@testing-library/[email protected])([email protected](@types/[email protected])([email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])))':dependencies:'@mdx-js/react': 3.1.0(@types/[email protected])([email protected])@@ -13853,8 +13835,6 @@ snapshots:'@types/[email protected]': {}- '@types/[email protected]': {}-'@types/[email protected]': {}'@types/[email protected]':@@ -14420,17 +14400,13 @@ snapshots:dependencies:- '@ai-sdk/provider': 1.1.3- '@ai-sdk/provider-utils': 2.2.8([email protected])- '@ai-sdk/react': 1.2.12([email protected])([email protected])- '@ai-sdk/ui-utils': 1.2.11([email protected])+ '@ai-sdk/gateway': 1.0.15([email protected])+ '@ai-sdk/provider': 2.0.0+ '@ai-sdk/provider-utils': 3.0.7([email protected])'@opentelemetry/api': 1.9.0- jsondiffpatch: 0.6.0zod: 3.25.76- optionalDependencies:- react: 19.1.0optionalDependencies:@@ -14794,8 +14770,6 @@ snapshots:ansi-styles: 4.3.0supports-color: 7.2.0- [email protected]: {}-@@ -15551,8 +15525,6 @@ snapshots:dependencies:dequal: 2.0.3- [email protected]: {}-@@ -17103,12 +17075,6 @@ snapshots:- dependencies:- '@types/diff-match-patch': 1.0.36- chalk: 5.4.1- diff-match-patch: 1.0.5-@@ -19369,8 +19335,6 @@ snapshots:ajv-formats: 2.1.1([email protected])ajv-keywords: 5.1.0([email protected])- [email protected]: {}-diff --git a/tests/_ai/test_chat_convert.py b/tests/_ai/test_chat_convert.pyindex 52ad53f038..5f7917f024 100644--- a/tests/_ai/test_chat_convert.py+++ b/tests/_ai/test_chat_convert.py@@ -25,7 +25,6 @@ from marimo._ai._types import (ReasoningPart,TextPart,ToolInvocationPart,- ToolInvocationResult,)from marimo._plugins.ui._impl.chat.utils import from_chat_message_dictfrom marimo._server.ai.tools import Tool@@ -110,15 +109,11 @@ def test_convert_to_openai_messages_with_parts_and_attachments():],),ToolInvocationPart(- type="tool-invocation",- tool_invocation=ToolInvocationResult(- state="result",- result={"answer": 4},- tool_call_id="call_123",- tool_name="calculator",- step=1,- args={"expression": "2 + 2"},- ),+ type="tool-calculator",+ tool_call_id="call_123",+ state="output-available",+ input={"expression": "2 + 2"},+ output={"answer": 4},),],),@@ -137,15 +132,11 @@ def test_convert_to_openai_messages_with_parts_and_attachments():],},{- "type": "tool-invocation",- "tool_invocation": {- "state": "result",- "result": {"answer": 4},- "tool_call_id": "call_123",- "tool_name": "calculator",- "step": 1,- "args": {"expression": "2 + 2"},- },+ "type": "tool-calculator",+ "tool_call_id": "call_123",+ "state": "output-available",+ "input": {"expression": "2 + 2"},+ "output": {"answer": 4},},{"type": "image_url",@@ -367,15 +358,11 @@ def test_from_chat_message_dict():"content": "Here's the tool result.","parts": [{- "type": "tool-invocation",- "tool_invocation": {- "state": "result",- "result": {"temperature": "72°F", "condition": "sunny"},- "tool_call_id": "call_123",- "tool_name": "weather_tool",- "step": 1,- "args": {"location": "New York"},- },+ "type": "tool-weather_tool",+ "tool_call_id": "call_123",+ "state": "output-available",+ "input": {"location": "New York"},+ "output": {"temperature": "72°F", "condition": "sunny"},}],}@@ -386,22 +373,15 @@ def test_from_chat_message_dict():assert result_tool_result.role == "assistant"assert result_tool_result.content == "Here's the tool result."assert len(result_tool_result.parts) == 1- assert result_tool_result.parts[0].type == "tool-invocation"- assert result_tool_result.parts[0].tool_invocation.state == "result"- assert result_tool_result.parts[0].tool_invocation.result == {+ assert result_tool_result.parts[0].type == "tool-weather_tool"+ assert result_tool_result.parts[0].state == "output-available"+ assert result_tool_result.parts[0].output == {"temperature": "72°F","condition": "sunny",}- assert (- result_tool_result.parts[0].tool_invocation.tool_call_id == "call_123"- )- assert (- result_tool_result.parts[0].tool_invocation.tool_name == "weather_tool"- )- assert result_tool_result.parts[0].tool_invocation.step == 1- assert result_tool_result.parts[0].tool_invocation.args == {- "location": "New York"- }+ assert result_tool_result.parts[0].tool_call_id == "call_123"+ assert result_tool_result.parts[0].tool_name == "weather_tool"+ assert result_tool_result.parts[0].input == {"location": "New York"}# Test case 6: Existing ChatMessage input (should return as-is)existing_message = ChatMessage(@@ -479,63 +459,110 @@ def test_convert_to_google_tools(sample_tools):def test_convert_to_ai_sdk_messages():# Test text typetext = "hello world"- result = convert_to_ai_sdk_messages(text, "text")- assert result == f"0:{json.dumps(text)}\n"+ result = convert_to_ai_sdk_messages(text, "text", text_id="test_text_id")+ expected = f"data: {json.dumps({'type': 'text-delta', 'id': 'test_text_id', 'delta': text})}\n\n"+ assert result == expected# Test reasoning typereasoning = "step by step"- result = convert_to_ai_sdk_messages(reasoning, "reasoning")- assert result == f"g:{json.dumps(reasoning)}\n"+ result = convert_to_ai_sdk_messages(+ reasoning, "reasoning", text_id="test_reasoning_id"+ )+ expected = f"data: {json.dumps({'type': 'reasoning-delta', 'id': 'test_reasoning_id', 'delta': reasoning})}\n\n"+ assert result == expected# Test reasoning_signature typereasoning_signature = {"signature": "encrypted_signature_string"}result = convert_to_ai_sdk_messages(reasoning_signature, "reasoning_signature")- assert result == f"j:{json.dumps(reasoning_signature)}\n"+ expected = f"data: {json.dumps({'type': 'data-reasoning-signature', 'data': reasoning_signature})}\n\n"+ assert result == expected# Test tool_call_start typetool_call_start = {"toolCallId": "123", "toolName": "test_tool"}result = convert_to_ai_sdk_messages(tool_call_start, "tool_call_start")- assert result == f"b:{json.dumps(tool_call_start)}\n"+ expected = f"data: {json.dumps({'type': 'tool-input-start', 'toolCallId': '123', 'toolName': 'test_tool'})}\n\n"+ assert result == expected# Test tool_call_delta type- tool_call_delta = {"toolCallId": "123", "argsTextDelta": "partial args"}+ tool_call_delta = {"toolCallId": "123", "inputTextDelta": "partial args"}result = convert_to_ai_sdk_messages(tool_call_delta, "tool_call_delta")- assert result == f"c:{json.dumps(tool_call_delta)}\n"+ expected = f"data: {json.dumps({'type': 'tool-input-delta', 'toolCallId': '123', 'inputTextDelta': 'partial args'})}\n\n"+ assert result == expected# Test tool_call_end typetool_call_end = {"toolCallId": "123","toolName": "test_tool",- "args": {"param": "value"},+ "input": {"param": "value"},}result = convert_to_ai_sdk_messages(tool_call_end, "tool_call_end")- assert result == f"9:{json.dumps(tool_call_end)}\n"+ expected_data = {+ "type": "tool-input-available",+ "toolCallId": "123",+ "toolName": "test_tool",+ "input": {"param": "value"},+ }+ expected = f"data: {json.dumps(expected_data)}\n\n"+ assert result == expected# Test tool_result type- tool_result = {"toolCallId": "123", "result": "success"}+ tool_result = {"toolCallId": "123", "output": "success"}result = convert_to_ai_sdk_messages(tool_result, "tool_result")- assert result == f"a:{json.dumps(tool_result)}\n"+ expected = f"data: {json.dumps({'type': 'tool-output-available', 'toolCallId': '123', 'output': 'success'})}\n\n"+ assert result == expected- # Test finish_reason type with "tool_calls"+ # Test finish_reason typeresult = convert_to_ai_sdk_messages("tool_calls", "finish_reason")- expected = 'd:{"finishReason": "tool_calls", "usage": {"promptTokens": 0, "completionTokens": 0}}\n'+ expected = f"data: {json.dumps({'type': 'finish'})}\n\n"assert result == expected- # Test finish_reason type with "stop"+ # Test finish_reason type with "stop" - same as aboveresult = convert_to_ai_sdk_messages("stop", "finish_reason")- expected = 'd:{"finishReason": "stop", "usage": {"promptTokens": 0, "completionTokens": 0}}\n'+ expected = f"data: {json.dumps({'type': 'finish'})}\n\n"assert result == expected# Test error typeerror_message = "Model not found"result = convert_to_ai_sdk_messages(error_message, "error")- assert result == f"3:{json.dumps(error_message)}\n"+ expected = f"data: {json.dumps({'type': 'error', 'errorText': error_message})}\n\n"+ assert result == expected++ # Test text_start type+ result = convert_to_ai_sdk_messages(+ "", "text_start", text_id="test_text_start_id"+ )+ expected = f"data: {json.dumps({'type': 'text-start', 'id': 'test_text_start_id'})}\n\n"+ assert result == expected- # Test unknown type defaults to text (using type ignore for testing)- result = convert_to_ai_sdk_messages("fallback", "unknown") # type: ignore- assert result == f"0:{json.dumps('fallback')}\n"+ # Test text_end type+ result = convert_to_ai_sdk_messages(+ "", "text_end", text_id="test_text_end_id"+ )+ expected = f"data: {json.dumps({'type': 'text-end', 'id': 'test_text_end_id'})}\n\n"+ assert result == expected++ # Test reasoning_start type+ result = convert_to_ai_sdk_messages(+ "", "reasoning_start", text_id="test_reasoning_start_id"+ )+ expected = f"data: {json.dumps({'type': 'reasoning-start', 'id': 'test_reasoning_start_id'})}\n\n"+ assert result == expected++ # Test reasoning_end type+ result = convert_to_ai_sdk_messages(+ "", "reasoning_end", text_id="test_reasoning_end_id"+ )+ expected = f"data: {json.dumps({'type': 'reasoning-end', 'id': 'test_reasoning_end_id'})}\n\n"+ assert result == expected++ # Test unknown type defaults to text-delta (using type ignore for testing)+ result = convert_to_ai_sdk_messages(+ "fallback", "unknown", text_id="test_fallback_id"+ ) # type: ignore+ expected = f"data: {json.dumps({'type': 'text-delta', 'id': 'test_fallback_id', 'delta': 'fallback'})}\n\n"+ assert result == expected# Tests for helper functions that convert parts to provider-specific formats@@ -555,19 +582,14 @@ def test_get_openai_messages_from_parts_text_only():def test_get_openai_messages_from_parts_with_tool_invocation():"""Test converting ToolInvocationPart to OpenAI format."""- tool_invocation = ToolInvocationResult(- state="result",- tool_call_id="call_123",- tool_name="weather_tool",- step=1,- args={"location": "New York"},- result={"temperature": "72°F", "condition": "sunny"},- )-parts = [TextPart(type="text", text="Let me check the weather"),ToolInvocationPart(- type="tool-invocation", tool_invocation=tool_invocation+ type="tool-weather_tool",+ tool_call_id="call_123",+ state="output-available",+ input={"location": "New York"},+ output={"temperature": "72°F", "condition": "sunny"},),]@@ -706,19 +728,14 @@ def test_get_anthropic_messages_from_parts_reasoning_empty_details():def test_get_anthropic_messages_from_parts_with_tool_invocation():"""Test converting ToolInvocationPart to Anthropic format."""- tool_invocation = ToolInvocationResult(- state="result",- tool_call_id="call_123",- tool_name="search_tool",- step=1,- args={"query": "Python tutorials"},- result={"results": ["tutorial1", "tutorial2"]},- )-parts = [TextPart(type="text", text="I'll search for you"),ToolInvocationPart(- type="tool-invocation", tool_invocation=tool_invocation+ type="tool-search_tool",+ tool_call_id="call_123",+ state="output-available",+ input={"query": "Python tutorials"},+ output={"results": ["tutorial1", "tutorial2"]},),]@@ -819,18 +836,13 @@ def test_get_google_messages_from_parts_with_reasoning():def test_get_google_messages_from_parts_with_tool_invocation():"""Test converting ToolInvocationPart to Google function call format."""- tool_invocation = ToolInvocationResult(- state="result",- tool_call_id="call_456",- tool_name="calculator",- step=1,- args={"expression": "2 + 2"},- result={"answer": 4},- )-parts = [ToolInvocationPart(- type="tool-invocation", tool_invocation=tool_invocation+ type="tool-calculator",+ tool_call_id="call_456",+ state="output-available",+ input={"expression": "2 + 2"},+ output={"answer": 4},),]