Skip to content

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

Thread tabs: every bot with two open threads gets its switcher, and Ctrl+Tab keeps to the pill's order

milind-soniec270fc6bd0f
diff --git a/src/components/ThreadSwitcherPill.dom.test.ts b/src/components/ThreadSwitcherPill.dom.test.ts
index 39d819492b..1ad16066b8 100644
--- a/src/components/ThreadSwitcherPill.dom.test.ts
+++ b/src/components/ThreadSwitcherPill.dom.test.ts
@@ -5,10 +5,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { Action, Bot, Task } from "@/state/store";
const dispatch = vi.hoisted(() => vi.fn());
-vi.mock("@/state/store-select", () => ({ useStoreDispatch: () => dispatch, useStoreSelector: (select: (state: unknown) => unknown) => select({ settingsOpen: false, pendingQueued: {}, bots: [], newChat: null }) }));
+const store = vi.hoisted(() => ({ state: {} as Record<string, unknown> }));
+vi.mock("@/state/store-select", () => ({ useStoreDispatch: () => dispatch, useStoreSelector: (select: (state: unknown) => unknown) => select(store.state) }));
vi.mock("./Avatar", () => ({ BotAvatar: () => null }));
import { ChatHeadPill } from "./ChatHeadPill";
+import { useThreadCycleKeys } from "./thread-cycle";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -18,9 +20,15 @@ const THREADS = [task("sales", "Weekly sales", 5, { busy: true }), task("launch"
let root: Root | null = null;
const onToggle = vi.fn();
+/** Simple mode's header pill, with the app's Ctrl+Tab keys listening as they do in the app. */
+function Header({ bot }: { bot: Bot }) {
+ useThreadCycleKeys();
+ return createElement(ChatHeadPill, { bot, avatar: {} as never, privacy: null, onToggle, switcher: true });
+}
const render = (bot: Bot) => {
- root = createRoot(document.body.appendChild(document.createElement("div")));
- act(() => root!.render(createElement(ChatHeadPill, { bot, avatar: {} as never, privacy: null, onToggle, switcher: true })));
+ store.state = { settingsOpen: false, pendingQueued: {}, bots: [bot], selectedId: bot.id, activeView: "chat", shortcutsOpen: false, newChat: null };
+ if (!root) root = createRoot(document.body.appendChild(document.createElement("div")));
+ act(() => root!.render(createElement(Header, { bot })));
};
const q = <T extends Element = HTMLElement>(selector: string) => document.querySelector<T & HTMLElement>(selector);
const chips = () => [...document.querySelectorAll<HTMLElement>("[role=option]")];
@@ -75,6 +83,32 @@ describe("the pill's thread switcher", () => {
expect(sent()).toEqual([{ type: "newTask", botId: "pepper" }]);
});
+ it("shows for a bot whose second open thread another bot opened, beside its main thread", () => {
+ const lead = { botId: "lead", name: "Lead", at: 1 };
+ render(pepper([task("sales", "Weekly sales", 5), task("brief", "Launch brief", 3, { openedBy: lead })], "sales"));
+ act(() => q("[data-chathead-threads]")!.click());
+ expect(chips().map((chip) => chip.textContent)).toEqual(["Weekly sales", "Launch brief"]);
+ });
+
+ it("Ctrl+Tab walks the threads in the order the pill lists them, wrapping", () => {
+ const helper = task("brief", "Launch brief", 4, { openedBy: { botId: "lead", name: "Lead", at: 1 } });
+ const threads = [...THREADS, helper];
+ render(pepper(threads, "launch"));
+ act(() => q("[data-chathead-threads]")!.click());
+ const order = chips().map((chip) => chip.dataset.threadChip!);
+ expect(order).toEqual(["launch", "sales", "brief", "trip"]);
+ const visited: string[] = [];
+ let open = order[0]!;
+ for (let step = 0; step < order.length; step += 1) {
+ act(() => { document.body.dispatchEvent(new KeyboardEvent("keydown", { key: "Tab", ctrlKey: true, bubbles: true, cancelable: true })); });
+ const action = sent().at(-1) as Extract<Action, { type: "switchTask" }>;
+ open = action.threadId;
+ visited.push(open);
+ render(pepper(threads, open));
+ }
+ expect(visited).toEqual([...order.slice(1), order[0]]);
+ });
+
it("stays the plain pill for a bot with one thread", () => {
render(pepper([task("sales", "Weekly sales", 5)], "sales"));
expect(q("[data-chathead-threads]")).toBeNull();
diff --git a/src/components/thread-cycle.test.ts b/src/components/thread-cycle.test.ts
index 5b37d2c5e6..22449e5104 100644
--- a/src/components/thread-cycle.test.ts
+++ b/src/components/thread-cycle.test.ts
@@ -25,13 +25,13 @@ describe("Ctrl+Tab through a bot's threads", () => {
expect(threadCycleStep(key({ defaultPrevented: true }))).toBeNull();
});
- it("goes in the header tabs' order, pinned first, skipping archived, snoozed, closed, routine and bot-opened threads", () => {
+ it("goes in the header tabs' order, pinned first, skipping archived, snoozed, closed, routine and burst threads", () => {
const tasks = [thread("main", 1), thread("new", 9), thread("pin", 2, { pinned: true }), thread("mid", 5), thread("gone", 8, { archivedAt: 1 }),
thread("later", 8, { snoozedUntil: 0 }), thread("done", 8, { closedBy: { botId: "x", name: "X", at: 1 } }), thread("run", 8, { routineRunId: "r" }),
- thread("helper", 8, { openedBy: { botId: "x", name: "X", at: 1 } })];
+ thread("burst", 8, { openedBy: { botId: "x", name: "X", at: 1, burstId: "z" } }), thread("helper", 7, { openedBy: { botId: "x", name: "X", at: 1 } })];
const bot = { id: "b", threadId: "main", tasks };
expect(threadCycleOrder(bot)).toEqual(threadTabs(bot).map((tab) => tab.threadId));
- expect(threadCycleOrder(bot)).toEqual(["pin", "new", "mid", "main"]);
+ expect(threadCycleOrder(bot)).toEqual(["pin", "new", "helper", "mid", "main"]);
});
it("leaves out the blank New thread chat's tab", () => {
diff --git a/src/components/thread-tabs.test.ts b/src/components/thread-tabs.test.ts
index 1b001f6315..b16a8bcf43 100644
--- a/src/components/thread-tabs.test.ts
+++ b/src/components/thread-tabs.test.ts
@@ -14,16 +14,29 @@ describe("thread tabs", () => {
expect(tabs[0].pinned).toBe(true);
});
- it("leaves out archived, snoozed, closed and bot-started threads, unless one is open", () => {
+ it("leaves out archived, snoozed, closed, routine and burst threads, unless one is open", () => {
const tasks = [
task("mine", 9), task("gone", 8, { archivedAt: 1 }), task("later", 7, { snoozedUntil: 0 }), task("run", 6, { routineRunId: "r1" }),
- task("helper", 5, { openedBy: { botId: "b", name: "Rio", at: 1 } }), task("closed", 4, { closedBy: { botId: "b", name: "Rio", at: 1 } }),
+ task("burst", 5, { openedBy: { botId: "b", name: "Rio", at: 1, burstId: "x" } }), task("closed", 4, { closedBy: { botId: "b", name: "Rio", at: 1 } }),
];
expect(threadTabs(bot(tasks, { threadId: "mine" })).map((tab) => tab.threadId)).toEqual(["mine"]);
expect(threadTabs(bot(tasks, { threadId: "gone" })).map((tab) => tab.threadId)).toEqual(["mine", "gone"]);
expect(threadTabs(bot(tasks, { threadId: "mine", switchingTo: "run" })).map((tab) => tab.threadId)).toEqual(["mine", "run"]);
});
+ it("counts a thread another bot opened while it is still going, so a bot whose work comes from teammates gets its switcher", () => {
+ const lead = { botId: "lead", name: "Lead", at: 1 };
+ const tasks = [task("main", 1, { openedBy: lead }), task("steer", 3, { openedBy: lead }), task("gemini", 2, { openedBy: lead }), task("done", 4, { openedBy: lead, closedBy: lead })];
+ const helper = bot(tasks, { threadId: "main", mainThreadId: "main" });
+ expect(threadTabs(helper).map((tab) => tab.threadId)).toEqual(["steer", "gemini", "main"]);
+ expect(botShowsTabs(helper, null)).toBe(true);
+ });
+
+ it("keeps the main thread whoever opened it, even when another thread is on screen", () => {
+ const tasks = [task("main", 1, { routineRunId: "r" }), task("other", 2)];
+ expect(threadTabs(bot(tasks, { threadId: "other", mainThreadId: "main" })).map((tab) => tab.threadId)).toEqual(["other", "main"]);
+ });
+
it("shows the strip only for two threads or more, the blank New thread chat counted", () => {
const one = bot([task("only", 1)]);
expect(showsTabs(threadTabs(one))).toBe(false);
diff --git a/src/components/thread-tabs.ts b/src/components/thread-tabs.ts
index ec008259e2..3eef90c090 100644
--- a/src/components/thread-tabs.ts
+++ b/src/components/thread-tabs.ts
@@ -1,7 +1,6 @@
import type { Bot, Task } from "@/state/store";
import type { NewChat } from "@/state/new-chat";
import { isNewChatThread, newChatThreadId } from "@/state/new-chat";
-import { ownThread } from "./sidebar-thread-labels";
import { isArchived, isSnoozed, isWorking, orderedThreadList } from "./SidebarThreadRow";
export interface ThreadTab {
@@ -20,8 +19,11 @@ export type TabsBot = Pick<Bot, "id" | "threadId"> & Partial<Pick<Bot, "mainThre
/** The open thread: where a switch is going, or the blank New thread chat. */
export const openTabId = (bot: Pick<Bot, "threadId" | "switchingTo">): string => bot.switchingTo ?? bot.threadId;
-const tabbed = (task: Task, open: string) =>
- task.threadId === open || task.pinned === true || (ownThread(task) && !isArchived(task) && !isSnoozed(task) && !task.closedBy);
+/** A thread someone is still talking in, whoever opened it: not a routine's run or a burst's, and not ended, archived or snoozed. */
+const stillOpen = (task: Task) => !task.routineRunId && !task.openedBy?.burstId && !isArchived(task) && !isSnoozed(task) && !task.closedBy;
+
+const tabbed = (task: Task, open: string, main: string | undefined) =>
+ task.threadId === open || task.threadId === main || task.pinned === true || stillOpen(task);
/**
* A bot's threads as tabs, in its sidebar order: pinned first, then by recent use. The blank New thread chat sits where
@@ -29,7 +31,7 @@ const tabbed = (task: Task, open: string) =>
*/
export function threadTabs(bot: TabsBot): ThreadTab[] {
const open = openTabId({ threadId: bot.threadId, switchingTo: bot.switchingTo });
- const tasks = orderedThreadList((bot.tasks ?? []).filter((task) => tabbed(task, open)));
+ const tasks = orderedThreadList((bot.tasks ?? []).filter((task) => tabbed(task, open, bot.mainThreadId)));
const main = bot.mainThreadId;
const real = tasks.length;
const tabs: ThreadTab[] = tasks.map((task) => {