Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Sidebar: a bot held in Unread leaves it when another of its conversations is open

luishron1235b83eb75465a
diff --git a/src/components/Sidebar.simple-layout.test.ts b/src/components/Sidebar.simple-layout.test.ts
index 6c3db2315b..8e9d737aba 100644
--- a/src/components/Sidebar.simple-layout.test.ts
+++ b/src/components/Sidebar.simple-layout.test.ts
@@ -464,6 +464,29 @@ describe("the sidebar's Unread section", () => {
expect(listedBots(tree)).toEqual(["atlas", "bea", "cora", "dodo", "eve"]);
});
+ it("sends the bot opened from it back to its own place once another conversation with that bot is open", () => {
+ const openDodo = () => {
+ withUnread();
+ render();
+ fixture.state = reducer(fixture.state, { type: "select", id: "dodo" });
+ expect(listedBots(render().tree)).toEqual(["cora", "dodo", "bea", "atlas", "eve"]);
+ };
+ openDodo();
+ fixture.state = reducer(fixture.state, { type: "newTask", botId: "dodo" });
+ expect(fixture.state.newChat?.botId).toBe("dodo");
+ let tree = render().tree;
+ expect(listedBots(tree)).toEqual(["cora", "bea", "atlas", "dodo", "eve"]);
+ expect(unreadHeader(tree)!.props.count).toBe(2);
+
+ openDodo();
+ fixture.state = { ...fixture.state, bots: fixture.state.bots.map((candidate) => (candidate.id === "dodo" ? { ...candidate, tasks: [...candidate.tasks!, { ...thread("dodo", 5, false), threadId: "dodo-other" }] } : candidate)) };
+ expect(listedBots(render().tree)).toEqual(["cora", "dodo", "bea", "atlas", "eve"]);
+ fixture.state = reducer(fixture.state, { type: "switchTask", botId: "dodo", threadId: "dodo-other" });
+ tree = render().tree;
+ expect(listedBots(tree)).toEqual(["cora", "bea", "atlas", "dodo", "eve"]);
+ expect(unreadHeader(tree)!.props.count).toBe(2);
+ });
+
it("keeps a held bot listed when it is marked unread or gets a new message, by the normal rule", () => {
withUnread();
render();
diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx
index 42d4b23df9..a6b0168255 100644
--- a/src/components/Sidebar.tsx
+++ b/src/components/Sidebar.tsx
@@ -1104,7 +1104,7 @@ export const Sidebar = memo(function Sidebar({ open, onClose, collapseToIcons =
const { pinnedBots, filed, unreadBots, unreadCount, sectionChiefs, sectionedBots, unsectionedBots, inPlace } = sidebarBotRows(saved, (bot) => {
const { waiting, working } = sidebarBotStatus(bot, state.pendingQueued);
return waiting || working;
- }, !q, useUnreadHold(state.activeView === "chat" ? state.selectedId : null, sideLists.bots));
+ }, !q, useUnreadHold(state.activeView === "chat" ? state.selectedId : null, sideLists.bots, state.newChat?.botId));
const { botChats, sectionedRooms, unsectionedRooms } = partitionSidebarGroups(visibleGroups);
const sectionNames = sectionsInUse([...(state.sections ?? []).filter((name) => !q || name.toLowerCase().includes(q.toLowerCase())), ...botDrag.pending], [...filed, ...sectionedRooms]);
diff --git a/src/components/unread-notice.ts b/src/components/unread-notice.ts
index b91667568c..765701f90e 100644
--- a/src/components/unread-notice.ts
+++ b/src/components/unread-notice.ts
@@ -2,6 +2,8 @@ import { useEffect, useState } from "react";
import { botShowsUnread } from "@/lib/bot-unread";
import { nextUnreadHold, unreadSinceByBot, type UnreadHold } from "@/lib/sidebar-layout";
+import { newChatThreadId } from "@/state/new-chat";
+import type { Bot } from "@/state/store";
import { EFFECTS } from "./CursorAvatar";
@@ -33,11 +35,14 @@ function sameTimes(a: ReadonlyMap<string, number>, b: ReadonlyMap<string, number
return a.size === b.size && [...a].every(([id, since]) => b.get(id) === since);
}
-/** The bot Unread keeps listed while its chat, `openId`, is the open one. It lasts for the session only. */
-export function useUnreadHold(openId: string | null, bots: Parameters<typeof unreadSinceByBot>[0]): UnreadHold | null {
+/** The bot Unread keeps listed while the conversation it was opened in is the open one: `openId`'s current thread, or its
+ * blank new chat when `newChatBotId` names it. It lasts for the session only. */
+export function useUnreadHold(openId: string | null, bots: readonly Bot[], newChatBotId?: string): UnreadHold | null {
const unread = unreadSinceByBot(bots);
+ const open = bots.find((bot) => bot.id === openId);
+ const chat = open && newChatBotId === open.id ? newChatThreadId(open.id) : open?.switchingTo ?? open?.threadId;
const [seen, setSeen] = useState<{ unread: ReadonlyMap<string, number>; held: UnreadHold | null }>({ unread, held: null });
- const held = nextUnreadHold(seen.held, openId, seen.unread);
+ const held = nextUnreadHold(seen.held, openId, seen.unread, chat);
if (held !== seen.held || !sameTimes(unread, seen.unread)) setSeen({ unread, held });
return held;
}
diff --git a/src/lib/sidebar-layout.test.ts b/src/lib/sidebar-layout.test.ts
index 5e857c7f72..706b98a9ad 100644
--- a/src/lib/sidebar-layout.test.ts
+++ b/src/lib/sidebar-layout.test.ts
@@ -425,6 +425,21 @@ describe("the unread section", () => {
expect(nextUnreadHold(held, "b", unreadSinceByBot(bots({ unread: true, at: 40 })))).toEqual({ id: "b", since: 40 });
});
+ it("ends the hold when another conversation of the same bot is open, and holds the bot again from one that was unread", () => {
+ const none = new Map<string, number>();
+ const held = nextUnreadHold(null, "b", new Map([["b", 20]]), "first");
+ expect(held).toEqual({ id: "b", since: 20, chat: "first" });
+ expect(nextUnreadHold(held, "b", none, "first")).toBe(held);
+ expect(nextUnreadHold(held, "b", none, "second")).toBeNull();
+ expect(nextUnreadHold(held, "b", none, "new-chat-b")).toBeNull();
+
+ const stillUnread = new Map([["b", 50]]);
+ const second = nextUnreadHold(held, "b", stillUnread, "second");
+ expect(second).toEqual({ id: "b", since: 50, chat: "second" });
+ expect(nextUnreadHold(second, "b", none, "second")).toBe(second);
+ expect(nextUnreadHold(second, "b", none, "first")).toBeNull();
+ });
+
it("leaves a held row out of what a drag or Alt+Up/Down arranges", () => {
const saved = { pinnedBots: [], sectionChiefs: [], sectionedBots: [read("w1"), read("w2")], unsectionedBots: [read("a"), read("u"), read("b")] };
const rows = sidebarBotRows(saved, () => false, true, { id: "u", since: 5 });
diff --git a/src/lib/sidebar-layout.ts b/src/lib/sidebar-layout.ts
index a0bcb18a09..55731b150f 100644
--- a/src/lib/sidebar-layout.ts
+++ b/src/lib/sidebar-layout.ts
@@ -168,8 +168,8 @@ type UnreadListBot = {
messages?: ReadonlyArray<{ at: number }>;
};
-/** The bot kept in Unread after it was opened from there, with the time it was listed by. */
-export type UnreadHold = { id: string; since: number };
+/** The bot kept in Unread after it was opened from there, with the time it was listed by and the conversation open then. */
+export type UnreadHold = { id: string; since: number; chat?: string };
function unreadSince(bot: UnreadListBot): number {
const threads = (bot.tasks ?? []).filter((task) => task.unread && !task.routineRunId && task.archivedAt === undefined);
@@ -182,13 +182,15 @@ export function unreadSinceByBot(bots: readonly UnreadListBot[]): Map<string, nu
return new Map(bots.filter(botShowsUnread).map((bot) => [bot.id, unreadSince(bot)]));
}
-/** A bot that showed unread when its chat was opened, or while it is open, stays in Unread where it was until another
- * chat or view is open, so a chat opened by mistake can be marked unread again without looking for its row. */
-export function nextUnreadHold(held: UnreadHold | null, openId: string | null, unreadBefore: ReadonlyMap<string, number>): UnreadHold | null {
+/** A bot that showed unread when its conversation `chat` was opened, or while it is open, stays in Unread where it was
+ * until another conversation or view is open, so one opened by mistake can be marked unread again without looking for its
+ * row. Another conversation of the same bot, or a blank new one, ends it too. */
+export function nextUnreadHold(held: UnreadHold | null, openId: string | null, unreadBefore: ReadonlyMap<string, number>, chat?: string): UnreadHold | null {
if (openId === null) return null;
+ const same = held?.id === openId && held.chat === chat;
const since = unreadBefore.get(openId);
- if (since !== undefined) return held?.id === openId && held.since === since ? held : { id: openId, since };
- return held?.id === openId ? held : null;
+ if (since !== undefined) return same && held.since === since ? held : { id: openId, since, chat };
+ return same ? held : null;
}
/** Bots showing unread leave their department or the main list for one list of their own, newest first, and go back