Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(chat): "+ new chat" reuses an empty chat instead of spamming new

Louis Beaumont7222d42f2eb5
diff --git a/apps/screenpipe-app-tauri/app/home/page.tsx b/apps/screenpipe-app-tauri/app/home/page.tsx
index 9219199de..33be7024d 100644
--- a/apps/screenpipe-app-tauri/app/home/page.tsx
+++ b/apps/screenpipe-app-tauri/app/home/page.tsx
@@ -22,7 +22,7 @@ import {
X,
} from "lucide-react";
import { emit } from "@tauri-apps/api/event";
-import { useChatStore } from "@/lib/stores/chat-store";
+import { useChatStore, getOrCreateEmptyChatId } from "@/lib/stores/chat-store";
import { useOverlayData } from "@/app/shortcut-reminder/use-overlay-data";
import { cn } from "@/lib/utils";
import { AppSidebar, SidebarProvider, useSidebarContext } from "@/components/app-sidebar";
@@ -535,19 +535,25 @@ function HomeContent() {
// sidebar's "+ new chat" behaviour exactly so
// the two entry points stay in sync.
if (section.id === "home") {
- const id = crypto.randomUUID();
+ // Reuse an existing empty chat if there is one;
+ // otherwise create. Mirrors the sidebar's
+ // "+ new chat" handler so spamming the nav
+ // doesn't pile up empty rows.
+ const { id, isNew } = getOrCreateEmptyChatId();
const store = useChatStore.getState();
- store.actions.upsert({
- id,
- title: "new chat",
- preview: "",
- status: "idle",
- messageCount: 0,
- createdAt: Date.now(),
- updatedAt: Date.now(),
- pinned: false,
- unread: false,
- });
+ if (isNew) {
+ store.actions.upsert({
+ id,
+ title: "new chat",
+ preview: "",
+ status: "idle",
+ messageCount: 0,
+ createdAt: Date.now(),
+ updatedAt: Date.now(),
+ pinned: false,
+ unread: false,
+ });
+ }
store.actions.setCurrent(id);
void emit("chat-load-conversation", {
conversationId: id,
diff --git a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx b/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
index ad7a589bc..11fc8c09e 100644
--- a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
+++ b/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
@@ -39,6 +39,7 @@ import {
useChatStore,
useChatActions,
useOrderedSessions,
+ getOrCreateEmptyChatId,
type SessionRecord,
} from "@/lib/stores/chat-store";
import { updateConversationFlags } from "@/lib/chat-storage";
@@ -92,23 +93,23 @@ export function ChatSidebar({ className }: ChatSidebarProps) {
}, [sessions]);
const handleNew = () => {
- const id = crypto.randomUUID();
- // Optimistically add the row so the sidebar feels responsive even if
- // standalone-chat takes a tick to react. The router will pick up the
- // session if/when Pi starts emitting events for it. Critically, this
- // runs even when another session is mid-stream — multi-tab is the
- // whole point, so + new chat must be clickable at all times.
- actions.upsert({
- id,
- title: "new chat",
- preview: "",
- status: "idle",
- messageCount: 0,
- createdAt: Date.now(),
- updatedAt: Date.now(),
- pinned: false,
- unread: false,
- });
+ // Reuse an existing empty chat instead of spawning a fresh one
+ // every time. Spamming "+ new chat" otherwise floods the sidebar
+ // with rows the user never typed in.
+ const { id, isNew } = getOrCreateEmptyChatId();
+ if (isNew) {
+ actions.upsert({
+ id,
+ title: "new chat",
+ preview: "",
+ status: "idle",
+ messageCount: 0,
+ createdAt: Date.now(),
+ updatedAt: Date.now(),
+ pinned: false,
+ unread: false,
+ });
+ }
actions.setCurrent(id);
// chat-load-conversation with an unknown id is treated by
// standalone-chat's listener as "start a new chat with this id" —
diff --git a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts
index 706c5d044..25da374d0 100644
--- a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts
+++ b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts
@@ -12,6 +12,7 @@ import { beforeEach, describe, expect, it } from "vitest";
import {
useChatStore,
selectOrderedSessions,
+ getOrCreateEmptyChatId,
type SessionRecord,
} from "../stores/chat-store";
@@ -180,6 +181,62 @@ describe("chat-store: upsert preserves createdAt on re-insert", () => {
});
});
+describe("chat-store: getOrCreateEmptyChatId (no spam on +new)", () => {
+ beforeEach(reset);
+
+ it("creates a fresh id when there are no chats yet", () => {
+ const { id, isNew } = getOrCreateEmptyChatId();
+ expect(isNew).toBe(true);
+ expect(id).toMatch(/-/); // looks like a uuid
+ });
+
+ it("reuses the panel's current chat if it has no user message", () => {
+ useChatStore.setState({
+ sessions: { panelChat: baseRecord({ id: "panelChat", messages: [] }) },
+ currentId: null,
+ panelSessionId: "panelChat",
+ });
+ const { id, isNew } = getOrCreateEmptyChatId();
+ expect(id).toBe("panelChat");
+ expect(isNew).toBe(false);
+ });
+
+ it("creates a new id when the panel chat already has a user message", () => {
+ useChatStore.setState({
+ sessions: {
+ panelChat: baseRecord({
+ id: "panelChat",
+ messages: [{ id: "u1", role: "user", content: "hi", timestamp: 1 }],
+ }),
+ },
+ currentId: null,
+ panelSessionId: "panelChat",
+ });
+ const { id, isNew } = getOrCreateEmptyChatId();
+ expect(id).not.toBe("panelChat");
+ expect(isNew).toBe(true);
+ });
+
+ it("reuses any other empty chat (newest first) when the panel chat is full", () => {
+ useChatStore.setState({
+ sessions: {
+ full: baseRecord({
+ id: "full",
+ createdAt: 100,
+ messages: [{ id: "u", role: "user", content: "x", timestamp: 1 }],
+ }),
+ oldEmpty: baseRecord({ id: "oldEmpty", createdAt: 200, messages: [] }),
+ newEmpty: baseRecord({ id: "newEmpty", createdAt: 300, messages: [] }),
+ },
+ currentId: null,
+ panelSessionId: "full",
+ });
+ const { id, isNew } = getOrCreateEmptyChatId();
+ expect(id).toBe("newEmpty");
+ expect(isNew).toBe(false);
+ });
+});
+
describe("chat-store: setCurrent clears unread atomically", () => {
beforeEach(reset);
diff --git a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts b/apps/screenpipe-app-tauri/lib/stores/chat-store.ts
index b4dde13b0..50a48d0e6 100644
--- a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts
+++ b/apps/screenpipe-app-tauri/lib/stores/chat-store.ts
@@ -496,6 +496,40 @@ export const useChatStore = create<ChatStore>((set) => ({
* selector returns the same reference forever. Safe to use in deps. */
export const useChatActions = () => useChatStore((s) => s.actions);
+/**
+ * "+ new chat" semantics. If the user already has an empty chat
+ * (no user message sent yet), return its id instead of spawning a
+ * new one — repeatedly clicking the button otherwise floods the
+ * sidebar with empty rows. Picks the panel's current session first
+ * (most likely the one the user is staring at), then falls back to
+ * any other empty session newest-first.
+ *
+ * Returns `{ id, isNew }` so callers can decide whether to upsert.
+ */
+export function getOrCreateEmptyChatId(): { id: string; isNew: boolean } {
+ const state = useChatStore.getState();
+ const isEmpty = (s: SessionRecord) => {
+ const msgs = (s.messages as Array<{ role?: string }> | undefined) ?? [];
+ if (msgs.length === 0) return true;
+ return !msgs.some((m) => m?.role === "user");
+ };
+
+ // Prefer the chat the user is already on.
+ const panelId = state.panelSessionId;
+ if (panelId) {
+ const panel = state.sessions[panelId];
+ if (panel && isEmpty(panel)) return { id: panelId, isNew: false };
+ }
+
+ // Otherwise any other empty session, newest first by createdAt.
+ const empties = Object.values(state.sessions)
+ .filter(isEmpty)
+ .sort((a, b) => b.createdAt - a.createdAt);
+ if (empties.length > 0) return { id: empties[0].id, isNew: false };
+
+ return { id: crypto.randomUUID(), isNew: true };
+}
+
/**
* Sorted view: pinned first (most recent), then unpinned by updatedAt desc.
*
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
index 99885b303..884bb4828 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
-version = "2.4.73"
+version = "2.4.74"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""