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
fix(ai): creating new thread from sidebar doesn't work (#10538)
Shahmir Varqhafb45323498c5
diff --git a/frontend/src/components/chat/__tests__/use-chat-controller-id.test.tsx b/frontend/src/components/chat/__tests__/use-chat-controller-id.test.tsxnew file mode 100644index 0000000000..5eb8ecf506--- /dev/null+++ b/frontend/src/components/chat/__tests__/use-chat-controller-id.test.tsx@@ -0,0 +1,39 @@+/* Copyright 2026 Marimo. All rights reserved. */++import { act, renderHook } from "@testing-library/react";+import { describe, expect, it } from "vitest";+import type { ChatId } from "@/core/ai/state";+import { useChatControllerId } from "../use-chat-controller-id";++describe("useChatControllerId", () => {+ it("creates a new ID when starting another unsaved chat", () => {+ const activeChatId = "existing-chat" as ChatId;+ const initialProps: { activeId: ChatId | undefined } = {+ activeId: undefined,+ };+ const { result, rerender } = renderHook(+ ({ activeId }: { activeId: ChatId | undefined }) =>+ useChatControllerId(activeId),+ { initialProps },+ );++ const initialDraftId = result.current.chatControllerId;+ rerender({ activeId: activeChatId });++ expect(result.current.chatControllerId).toBe(activeChatId);++ act(() => result.current.renewDraftChatId());+ rerender({ activeId: undefined });++ expect(result.current.chatControllerId).not.toBe(initialDraftId);+ });++ it("renews the ID when the current chat is already unsaved", () => {+ const { result } = renderHook(() => useChatControllerId(undefined));+ const initialDraftId = result.current.chatControllerId;++ act(() => result.current.renewDraftChatId());++ expect(result.current.chatControllerId).not.toBe(initialDraftId);+ });+});diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsxindex c9e815f422..9181f4e875 100644--- a/frontend/src/components/chat/chat-panel.tsx+++ b/frontend/src/components/chat/chat-panel.tsx@@ -110,6 +110,7 @@ import {import { getCodes } from "@/core/codemirror/copilot/getCodes";import { focusInputAndMoveToEnd } from "@/core/codemirror/utils";import ScrollToBottomButton from "./acp/scroll-to-bottom-button";+import { useChatControllerId } from "./use-chat-controller-id";// Default mode for the AIconst DEFAULT_MODE = "manual";@@ -554,6 +555,8 @@ const ChatPanelBody = () => {const { openModal, closeModal } = useImperativeModal();const activeChatId = activeChat?.id;+ const { chatControllerId, renewDraftChatId } =+ useChatControllerId(activeChatId);const store = useStore();const { addStagedCell } = useStagedAICellsActions();@@ -577,7 +580,7 @@ const ChatPanelBody = () => {addToolApprovalResponse,id: chatId,} = useChat({- id: activeChatId,+ id: chatControllerId,throttle: AI_SDK_UI_THROTTLE_MS,sendAutomaticallyWhen: ({ messages }) => hasPendingToolCalls(messages),messages: activeChat?.messages || [], // initial messages@@ -816,6 +819,7 @@ const ChatPanelBody = () => {);const handleNewChat = useEvent(() => {+ renewDraftChatId();setActiveChat(null);setInput("");setNewThreadInput("");diff --git a/frontend/src/components/chat/use-chat-controller-id.ts b/frontend/src/components/chat/use-chat-controller-id.tsnew file mode 100644index 0000000000..42b58cd81e--- /dev/null+++ b/frontend/src/components/chat/use-chat-controller-id.ts@@ -0,0 +1,24 @@+/* Copyright 2026 Marimo. All rights reserved. */++import { useState } from "react";+import useEvent from "react-use-event-hook";+import type { ChatId } from "@/core/ai/state";+import { generateUUID } from "@/utils/uuid";++function generateChatId(): ChatId {+ return generateUUID() as ChatId;+}++/**+ * Keep an ID for an unsaved chat so `useChat` can distinguish it from the+ * previously active chat. Empty chats are not added to chat history.+ */+export function useChatControllerId(activeChatId?: ChatId) {+ const [draftChatId, setDraftChatId] = useState(generateChatId);+ const renewDraftChatId = useEvent(() => setDraftChatId(generateChatId()));++ return {+ chatControllerId: activeChatId ?? draftChatId,+ renewDraftChatId,+ };+}