Skip to content

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.

feat: add ai reasoning tokens and other parts persistence between sessions (#5191)

Joaquin Coromina589b85286929
diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsx
index fdd612ae76..3fb291b24b 100644
--- a/frontend/src/components/chat/chat-panel.tsx
+++ b/frontend/src/components/chat/chat-panel.tsx
@@ -292,10 +292,11 @@ const ChatPanelBody = () => {
id: activeChat?.id,
initialMessages: useMemo(() => {
return activeChat
- ? activeChat.messages.map(({ role, content, timestamp }) => ({
+ ? activeChat.messages.map(({ role, content, timestamp, parts }) => ({
role,
content,
id: timestamp.toString(),
+ parts,
}))
: [];
}, [activeChat]),
@@ -315,7 +316,13 @@ const ChatPanelBody = () => {
},
onFinish: (message) => {
setChatState((prev) =>
- addMessageToChat(prev, prev.activeChatId, "assistant", message.content),
+ addMessageToChat(
+ prev,
+ prev.activeChatId,
+ "assistant",
+ message.content,
+ message.parts,
+ ),
);
},
onError: (error) => {
diff --git a/frontend/src/core/ai/chat-utils.ts b/frontend/src/core/ai/chat-utils.ts
index 0cfcfc6a6f..feceb0e3cf 100644
--- a/frontend/src/core/ai/chat-utils.ts
+++ b/frontend/src/core/ai/chat-utils.ts
@@ -1,12 +1,14 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { Logger } from "@/utils/Logger";
import type { ChatState } from "./state";
+import type { Message as AIMessage } from "@ai-sdk/react";
export const addMessageToChat = (
chatState: ChatState,
chatId: string | null,
role: "user" | "assistant",
content: string,
+ parts?: AIMessage["parts"],
): ChatState => {
if (!chatId) {
Logger.warn("No active chat");
@@ -24,6 +26,7 @@ export const addMessageToChat = (
role,
content,
timestamp: Date.now(),
+ parts,
},
],
updatedAt: Date.now(),
diff --git a/frontend/src/core/ai/state.ts b/frontend/src/core/ai/state.ts
index 413e102c02..22737685a6 100644
--- a/frontend/src/core/ai/state.ts
+++ b/frontend/src/core/ai/state.ts
@@ -2,8 +2,9 @@
import { atom } from "jotai";
import { atomWithStorage } from "jotai/utils";
import type { CellId } from "../cells/ids";
+import type { Message as AIMessage } from "@ai-sdk/react";
-const KEY = "marimo:ai:chatState:v1";
+const KEY = "marimo:ai:chatState:v2";
export const aiCompletionCellAtom = atom<{
cellId: CellId;
@@ -15,6 +16,7 @@ export interface Message {
role: "user" | "assistant" | "data" | "system";
content: string;
timestamp: number;
+ parts?: AIMessage["parts"];
}
export interface Chat {