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: made ai chat persist when clicking other tabs in side panel (#5118)

Joaquin Coromina06d094050c09
diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsx
index 16d8592ea6..dfc9b466b6 100644
--- a/frontend/src/components/chat/chat-panel.tsx
+++ b/frontend/src/components/chat/chat-panel.tsx
@@ -25,6 +25,7 @@ import {
type SetStateAction,
type Dispatch,
memo,
+ useEffect,
} from "react";
import { generateUUID } from "@/utils/uuid";
import { type Message, useChat } from "ai/react";
@@ -247,6 +248,7 @@ const ChatPanelBody = () => {
const [newThreadInput, setNewThreadInput] = useState("");
const newThreadInputRef = useRef<ReactCodeMirrorRef>(null);
const newMessageInputRef = useRef<ReactCodeMirrorRef>(null);
+ const scrollContainerRef = useRef<HTMLDivElement>(null);
const messagesEndRef = useRef<HTMLDivElement>(null);
const { theme } = useTheme();
@@ -262,6 +264,7 @@ const ChatPanelBody = () => {
reload,
stop,
} = useChat({
+ id: chatState.activeChatId || undefined,
keepLastMessageOnError: true,
// Throttle the messages and data updates to 100ms
experimental_throttle: 100,
@@ -278,10 +281,6 @@ const ChatPanelBody = () => {
},
streamProtocol: "text",
onFinish: (message) => {
- if (!chatState.activeChatId) {
- Logger.warn("No active chat");
- return;
- }
setChatState((prev) =>
addMessageToChat(prev, prev.activeChatId, "assistant", message.content),
);
@@ -296,6 +295,18 @@ const ChatPanelBody = () => {
const isLoading = status === "submitted" || status === "streaming";
+ // Scroll to the latest chat message at the bottom
+ useEffect(() => {
+ const scrollToBottom = () => {
+ if (scrollContainerRef.current) {
+ const container = scrollContainerRef.current;
+ container.scrollTop = container.scrollHeight;
+ }
+ };
+
+ requestAnimationFrame(scrollToBottom);
+ }, [chatState.activeChatId]);
+
const createNewThread = (initialMessage: string) => {
const newChat: Chat = {
id: generateUUID(),
@@ -376,7 +387,10 @@ const ChatPanelBody = () => {
/>
</TooltipProvider>
- <div className="flex-1 px-3 bg-[var(--slate-1)] gap-4 py-3 flex flex-col overflow-y-auto">
+ <div
+ className="flex-1 px-3 bg-[var(--slate-1)] gap-4 py-3 flex flex-col overflow-y-auto"
+ ref={scrollContainerRef}
+ >
{(!messages || messages.length === 0) && (
<div className="flex rounded-md border px-1 bg-background">
<PromptInput
diff --git a/frontend/src/core/ai/chat-utils.ts b/frontend/src/core/ai/chat-utils.ts
index f5a0192c5f..0cfcfc6a6f 100644
--- a/frontend/src/core/ai/chat-utils.ts
+++ b/frontend/src/core/ai/chat-utils.ts
@@ -1,4 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
+import { Logger } from "@/utils/Logger";
import type { ChatState } from "./state";
export const addMessageToChat = (
@@ -8,6 +9,7 @@ export const addMessageToChat = (
content: string,
): ChatState => {
if (!chatId) {
+ Logger.warn("No active chat");
return chatState;
}
return {