Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Sidebar: a bot opened from Unread stays listed there until another chat is open
luishron123da3578b7465e
diff --git a/src/components/Sidebar.simple-layout.test.ts b/src/components/Sidebar.simple-layout.test.tsindex 53623f29b6..6c3db2315b 100644--- a/src/components/Sidebar.simple-layout.test.ts+++ b/src/components/Sidebar.simple-layout.test.ts@@ -434,26 +434,73 @@ describe("the sidebar's Unread section", () => {expect(headers(tree).map((header) => [header.props.name, "count" in header.props])).toEqual([["Unread", true], ["Bots", false], ["Group chats", false], ["Work", false]]);});- it("sends a bot back to its own place once it is opened, and brings it back when it is marked unread", () => {+ it("keeps the bot opened from it listed in its place, and sends it back to its own place once another chat is open", () => {withUnread();+ render();fixture.state = reducer(fixture.state, { type: "select", id: "dodo" });let tree = render().tree;- expect(listedBots(tree)).toEqual(["cora", "bea", "atlas", "dodo", "eve"]);+ expect(listedBots(tree)).toEqual(["cora", "dodo", "bea", "atlas", "eve"]);expect(unreadHeader(tree)!.props.count).toBe(2);+ expect(tree.find((node) => (node.props.bot as Bot | undefined)?.id === "dodo")!.props.botDrag).toBeUndefined();++ fixture.state = reducer(fixture.state, { type: "select", id: "cora" });+ tree = render().tree;+ expect(listedBots(tree)).toEqual(["cora", "bea", "atlas", "dodo", "eve"]);+ expect(unreadHeader(tree)!.props.count).toBe(1);- fixture.state = reducer(reducer(fixture.state, { type: "select", id: "cora" }), { type: "select", id: "bea" });+ fixture.state = reducer(fixture.state, { type: "select", id: "room" });+ tree = render().tree;+ expect(listedBots(tree)).toEqual(["bea", "atlas", "cora", "dodo", "eve"]);++ fixture.state = reducer(fixture.state, { type: "select", id: "bea" });+ tree = render().tree;+ expect(sectionOrder(tree)).toEqual([UNREAD_SECTION_ID, BOTS_SECTION_ID, CHANNELS_SECTION_ID, userSectionId("Work")]);+ expect(listedBots(tree)).toEqual(["bea", "atlas", "cora", "dodo", "eve"]);+ expect("count" in unreadHeader(tree)!.props).toBe(false);++ fixture.state = reducer(reducer(fixture.state, { type: "select", id: "bea" }), { type: "showFiles" });tree = render().tree;expect(sectionOrder(tree)).toEqual([BOTS_SECTION_ID, CHANNELS_SECTION_ID, userSectionId("Work")]);expect(listedBots(tree)).toEqual(["atlas", "bea", "cora", "dodo", "eve"]);+ });++ it("keeps a held bot listed when it is marked unread or gets a new message, by the normal rule", () => {+ withUnread();+ render();+ fixture.state = reducer(fixture.state, { type: "select", id: "bea" });+ expect(listedBots(render().tree)).toEqual(["cora", "dodo", "bea", "atlas", "eve"]);++ fixture.state = reducer(fixture.state, { type: "markUnread", botId: "bea" });+ fixture.state = { ...fixture.state, bots: fixture.state.bots.map((candidate) => (candidate.id === "bea" ? { ...candidate, tasks: [thread("bea", 20)] } : candidate)) };+ let tree = render().tree;+ expect(listedBots(tree)).toEqual(["cora", "dodo", "bea", "atlas", "eve"]);+ expect(unreadHeader(tree)!.props.count).toBe(3);- fixture.state = { ...fixture.state, bots: fixture.state.bots.map((candidate) => (candidate.id === "eve" ? { ...candidate, tasks: undefined } : candidate)) };- fixture.state = reducer(fixture.state, { type: "markUnread", botId: "eve" });+ fixture.state = { ...fixture.state, bots: fixture.state.bots.map((candidate) => (candidate.id === "bea" ? { ...candidate, tasks: [thread("bea", 90)] } : candidate)) };tree = render().tree;+ expect(listedBots(tree)).toEqual(["bea", "cora", "dodo", "atlas", "eve"]);+ });++ it("brings a read bot to it when it is marked unread", () => {+ fixture.state = reducer(fixture.state, { type: "markUnread", botId: "dodo" });+ const { tree } = render();expect(sectionOrder(tree)[0]).toBe(UNREAD_SECTION_ID);- expect(listedBots(tree)).toEqual(["eve", "atlas", "bea", "cora", "dodo"]);+ expect(listedBots(tree)).toEqual(["dodo", "atlas", "bea"]);expect(unreadHeader(tree)!.props.count).toBe(1);});+ it("arranges the rows left in place around a held one with Alt+arrow", () => {+ vi.stubGlobal("requestAnimationFrame", () => 0);+ vi.stubGlobal("fetch", () => new Promise(() => {}));+ fixture.state = { ...fixture.state, bots: [bot("atlas"), bot("bea", { unread: true }), bot("cora"), bot("dodo", { section: "Work" })] };+ render();+ fixture.state = reducer(fixture.state, { type: "select", id: "bea" });+ const tree = render().tree;+ expect(listedBots(tree)).toEqual(["bea", "atlas", "cora", "dodo"]);+ rowOf(tree, "atlas").botDrag.step("atlas", 1);+ expect(fixture.dispatch).toHaveBeenCalledWith({ type: "botsOrdered", botIds: ["cora", "atlas"] });+ });+it("keeps a department whose bots are all unread, and takes no drag on its own rows", () => {withUnread();fixture.state = { ...fixture.state, sections: [], bots: fixture.state.bots.filter((candidate) => candidate.id !== "eve") };diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsxindex 141d1fe6a1..42d4b23df9 100644--- a/src/components/Sidebar.tsx+++ b/src/components/Sidebar.tsx@@ -86,7 +86,7 @@ import { PHONE_TOP_BUTTON, SIMPLE_CORNER_BUTTON, SIMPLE_ROW_FOCUS, SIMPLE_TOP_BUimport { isSidebarToggle } from "@/lib/keyboard-shortcuts";import { SidebarRailSeparator, SimpleRailFooter } from "./SidebarSimpleRail";import { useShowThreads } from "@/lib/thread-preferences";-import { useBotUnread } from "./unread-notice";+import { useBotUnread, useUnreadHold } from "./unread-notice";import { attentionHasRunningWork, attentionJumpAction, attentionUnpinAction, crossBotAttentionThreads, crossBotPinnedThreads, runningRoutineRun, SidebarBotActivity, sidebarBotActivityTasks, sidebarBotStatus } from "./SidebarBotActivity";import { SidebarNoBots, SidebarTileGrid } from "./SidebarTiles";import { BotContextMenu } from "./SidebarBotMenu";@@ -1101,10 +1101,10 @@ export const Sidebar = memo(function Sidebar({ open, onClose, collapseToIcons =const saved = simpleDesk ? withPrimaryTile(parts, myDefaultBot(state.bots, viewerEmail)?.id) : { ...parts, primary: null };// Shown order only: drag and Alt+Up/Down work on the saved order of the rows left in place, never on this one.const { primary, unsectionedChief } = saved;- const { pinnedBots, filed, unreadBots, sectionChiefs, sectionedBots, unsectionedBots, inPlace } = sidebarBotRows(saved, (bot) => {+ const { pinnedBots, filed, unreadBots, unreadCount, sectionChiefs, sectionedBots, unsectionedBots, inPlace } = sidebarBotRows(saved, (bot) => {const { waiting, working } = sidebarBotStatus(bot, state.pendingQueued);return waiting || working;- }, !q);+ }, !q, useUnreadHold(state.activeView === "chat" ? state.selectedId : null, sideLists.bots));const { botChats, sectionedRooms, unsectionedRooms } = partitionSidebarGroups(visibleGroups);const sectionNames = sectionsInUse([...(state.sections ?? []).filter((name) => !q || name.toLowerCase().includes(q.toLowerCase())), ...botDrag.pending], [...filed, ...sectionedRooms]);@@ -1434,7 +1434,7 @@ export const Sidebar = memo(function Sidebar({ open, onClose, collapseToIcons =collapsed={collapsed}attention={attention}onToggle={layoutInteractive ? () => toggleSection(id) : undefined}- {...layout.header(id)} {...(id === UNREAD_SECTION_ID && { count: unreadBots.length, reorderable: false })} naming={drops.naming(sectionName)} dropInto={drops.into(sectionName)}+ {...layout.header(id)} {...(id === UNREAD_SECTION_ID && { ...(unreadCount > 0 && { count: unreadCount }), reorderable: false })} naming={drops.naming(sectionName)} dropInto={drops.into(sectionName)}onDelete={!simple && !remoteClient && sectionName ? () => setDeletingTeam(sectionName) : undefined} onShare={!simple && !remoteClient && sectionName ? () => setShareTeam(sectionName) : undefined}/>)}diff --git a/src/components/unread-notice.ts b/src/components/unread-notice.tsindex 9aff535ee6..b91667568c 100644--- a/src/components/unread-notice.ts+++ b/src/components/unread-notice.ts@@ -1,6 +1,7 @@import { useEffect, useState } from "react";import { botShowsUnread } from "@/lib/bot-unread";+import { nextUnreadHold, unreadSinceByBot, type UnreadHold } from "@/lib/sidebar-layout";import { EFFECTS } from "./CursorAvatar";@@ -27,3 +28,16 @@ export function useBotUnread(bot: Parameters<typeof botShowsUnread>[0]): { unreaconst unread = botShowsUnread(bot);return { unread, noticing: useUnreadNotice(unread) };}++function sameTimes(a: ReadonlyMap<string, number>, b: ReadonlyMap<string, number>): boolean {+ 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 {+ const unread = unreadSinceByBot(bots);+ const [seen, setSeen] = useState<{ unread: ReadonlyMap<string, number>; held: UnreadHold | null }>({ unread, held: null });+ const held = nextUnreadHold(seen.held, openId, seen.unread);+ 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.tsindex b55f870c0c..5e857c7f72 100644--- a/src/lib/sidebar-layout.test.ts+++ b/src/lib/sidebar-layout.test.ts@@ -23,7 +23,9 @@ import {simpleSidebarSections,sectionsInUse,sidebarBotRows,+ nextUnreadHold,unreadBotsApart,+ unreadSinceByBot,userSectionId,userSectionName,withPrimaryTile,@@ -391,6 +393,47 @@ describe("the unread section", () => {expect(ids(searching.inPlace.unsectionedBots)).toEqual(["a", "u", "b"]);});+ it("keeps the bot opened from Unread listed there, in its place, until another chat is open", () => {+ const bots = [{ id: "a", unread: true, messages: [{ at: 30 }] }, { id: "b", unread: true, messages: [{ at: 20 }] }, { id: "c", unread: true, messages: [{ at: 10 }] }];+ const before = unreadSinceByBot(bots);+ expect([...before]).toEqual([["a", 30], ["b", 20], ["c", 10]]);++ const opened = bots.map((bot) => (bot.id === "b" ? { ...bot, unread: false, messages: [{ at: 20 }, { at: 99 }] } : bot));+ const held = nextUnreadHold(null, "b", before);+ expect(held).toEqual({ id: "b", since: 20 });+ const parts = { sectionChiefs: [], sectionedBots: [], unsectionedBots: opened };+ const apart = unreadBotsApart(parts, true, held);+ expect(apart.unreadBots.map((bot) => bot.id)).toEqual(["a", "b", "c"]);+ expect(apart.unsectionedBots).toEqual([]);+ expect(unreadBotsApart(parts).unreadBots.map((bot) => bot.id)).toEqual(["a", "c"]);++ const after = unreadSinceByBot(opened);+ expect(nextUnreadHold(held, "b", after)).toBe(held);+ expect(nextUnreadHold(held, "a", after)).toEqual({ id: "a", since: 30 });+ expect(nextUnreadHold(held, "room", after)).toBeNull();+ expect(nextUnreadHold(held, null, after)).toBeNull();+ expect(nextUnreadHold(null, "b", after)).toBeNull();+ });++ it("lists a held bot by the normal rule again once it is marked unread or a new message arrives", () => {+ const held = { id: "b", since: 20 };+ const bots = (b: { unread: boolean; at: number }) => [{ id: "a", unread: true, messages: [{ at: 30 }] }, { id: "b", unread: b.unread, messages: [{ at: b.at }] }];+ const listed = (b: { unread: boolean; at: number }) => unreadBotsApart({ sectionChiefs: [], sectionedBots: [], unsectionedBots: bots(b) }, true, held).unreadBots.map((bot) => bot.id);+ expect(listed({ unread: false, at: 20 })).toEqual(["a", "b"]);+ expect(listed({ unread: true, at: 20 })).toEqual(["a", "b"]);+ expect(listed({ unread: true, at: 40 })).toEqual(["b", "a"]);+ expect(nextUnreadHold(held, "b", unreadSinceByBot(bots({ unread: true, at: 40 })))).toEqual({ id: "b", since: 40 });+ });++ 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 });+ const ids = (bots: { id: string }[]) => bots.map((bot) => bot.id);+ expect(ids(rows.unreadBots)).toEqual(["u"]);+ expect([ids(rows.unsectionedBots), ids(rows.inPlace.unsectionedBots), ids(rows.inPlace.sectionedBots)]).toEqual([["a", "b"], ["a", "b"], ["w1", "w2"]]);+ expect(ids(sidebarBotRows(saved, () => false, false, { id: "u", since: 5 }).inPlace.unsectionedBots)).toEqual(["a", "u", "b"]);+ });+it("lists a department that only a shown bot or room names, after the given ones and once", () => {expect(sectionsInUse(["Work"], [{ section: "Ops" }, { section: "Work" }, { section: "Ops" }, {}, { section: null }])).toEqual(["Work", "Ops"]);});diff --git a/src/lib/sidebar-layout.ts b/src/lib/sidebar-layout.tsindex 1286cbe108..a0bcb18a09 100644--- a/src/lib/sidebar-layout.ts+++ b/src/lib/sidebar-layout.ts@@ -162,37 +162,60 @@ export function sectionsInUse(names: readonly string[], filed: ReadonlyArray<{ s}type UnreadListBot = {+ id: string;unread?: boolean;tasks?: ReadonlyArray<{ unread?: boolean; routineRunId?: string; archivedAt?: number; updatedAt?: number; createdAt?: number }> | null;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 };+function unreadSince(bot: UnreadListBot): number {const threads = (bot.tasks ?? []).filter((task) => task.unread && !task.routineRunId && task.archivedAt === undefined);if (threads.length === 0) return bot.messages?.at(-1)?.at ?? 0;return Math.max(...threads.map((task) => task.updatedAt ?? task.createdAt ?? 0));}+/** Each bot showing unread, with the time Unread lists it by. */+export function unreadSinceByBot(bots: readonly UnreadListBot[]): Map<string, number> {+ 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 {+ if (openId === null) return null;+ 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;+}+/** Bots showing unread leave their department or the main list for one list of their own, newest first, and go back- * once read. Pinned bots and the main Chief keep their place above it. Only how the list is shown changes. */-export function unreadBotsApart<T extends UnreadListBot>(parts: { sectionChiefs: T[]; sectionedBots: T[]; unsectionedBots: T[] }, enabled = true) {+ * once read; `held` stays listed where it was. Pinned bots and the main Chief keep their place above it. Only how the+ * list is shown changes. */+export function unreadBotsApart<T extends UnreadListBot>(parts: { sectionChiefs: T[]; sectionedBots: T[]; unsectionedBots: T[] }, enabled = true, held: UnreadHold | null = null) {if (!enabled) return { ...parts, unreadBots: [] as T[] };- const read = (bots: T[]) => bots.filter((bot) => !botShowsUnread(bot));- const unreadBots = [...parts.unsectionedBots, ...parts.sectionChiefs, ...parts.sectionedBots].filter(botShowsUnread)- .map((bot) => ({ bot, since: unreadSince(bot) })).sort((a, b) => b.since - a.since).map(({ bot }) => bot);- return { unreadBots, sectionChiefs: read(parts.sectionChiefs), sectionedBots: read(parts.sectionedBots), unsectionedBots: read(parts.unsectionedBots) };+ const listed = (bot: T) => botShowsUnread(bot) || bot.id === held?.id;+ const inPlace = (bots: T[]) => bots.filter((bot) => !listed(bot));+ const unreadBots = [...parts.unsectionedBots, ...parts.sectionChiefs, ...parts.sectionedBots].filter(listed)+ .map((bot) => ({ bot, since: held && !botShowsUnread(bot) ? held.since : unreadSince(bot) })).sort((a, b) => b.since - a.since).map(({ bot }) => bot);+ return { unreadBots, sectionChiefs: inPlace(parts.sectionChiefs), sectionedBots: inPlace(parts.sectionedBots), unsectionedBots: inPlace(parts.unsectionedBots) };}/** The rows of the bot list as shown, busy bots first and unread ones in a list of their own, with `filed`, every* shown bot that belongs to a department. `inPlace` is the saved order of the rows left where they belong: what a drag- * or Alt+Up/Down arranges, so neither stores the shown order nor counts a row that Unread is listing. */-export function sidebarBotRows<T extends UnreadListBot>(saved: { pinnedBots: T[]; sectionChiefs: T[]; sectionedBots: T[]; unsectionedBots: T[] }, busy: (bot: T) => boolean, unreadApart = true) {+ * or Alt+Up/Down arranges, so neither stores the shown order nor counts a row that Unread is listing. `unreadCount`+ * leaves out a row Unread only holds. */+export function sidebarBotRows<T extends UnreadListBot>(saved: { pinnedBots: T[]; sectionChiefs: T[]; sectionedBots: T[]; unsectionedBots: T[] }, busy: (bot: T) => boolean, unreadApart = true, held: UnreadHold | null = null) {const ordered = { ...saved, ...activeBotsFirst(saved, busy) };+ const shown = unreadBotsApart(ordered, unreadApart, held);return {pinnedBots: ordered.pinnedBots,filed: [...ordered.sectionedBots, ...ordered.sectionChiefs],- ...unreadBotsApart(ordered, unreadApart),- inPlace: unreadBotsApart(saved, unreadApart),+ ...shown,+ unreadCount: shown.unreadBots.filter(botShowsUnread).length,+ inPlace: unreadBotsApart(saved, unreadApart, held),};}