Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Thread tabs: show the strip with one thread, so + is always there
milind-sonibedefaed45f4
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsxindex b388c5aaa9..3c999a3e58 100644--- a/src/components/ChatView.tsx+++ b/src/components/ChatView.tsx@@ -737,7 +737,7 @@ export const ChatView = memo(function ChatView({ bot: profile, inForkPanel = falconst advanced = useAdvancedMode(), activity = useChatActivity(), skillBurger = useFeature("skillBurger");const phone = useMediaQuery(PHONE_LAYOUT);const phoneMenu = phone && !remoteClient;- const simpleHead = !advanced && !remoteClient, headTabs = useThreadTabsShown(bot.id) && !inForkPanel; useTabsFrameOnRoot(headTabs ? (simpleHead ? "simple" : "advanced") : undefined);+ const simpleHead = !advanced && !remoteClient, headTabs = useThreadTabsShown() && !inForkPanel; useTabsFrameOnRoot(headTabs ? (simpleHead ? "simple" : "advanced") : undefined);const canWrite = useCanWriteIn(bot.threadId, bot.yourAccess), mayWrite = canWrite !== false;const computerStarting = computerStartLine(state.computerStart, bot.name); useScreensWhile(Boolean(computerStarting));diff --git a/src/components/HeaderThreadTabs.dom.test.ts b/src/components/HeaderThreadTabs.dom.test.tsindex 5dc8d5db02..f4c752272a 100644--- a/src/components/HeaderThreadTabs.dom.test.ts+++ b/src/components/HeaderThreadTabs.dom.test.ts@@ -11,7 +11,7 @@ const store = vi.hoisted(() => ({ state: { bots: [] as unknown[], newChat: nullvi.mock("@/lib/use-feature", () => ({ useFeature: (name: string) => name === "threadTabs" && feature.on }));vi.mock("@/state/store-select", () => ({ useStoreDispatch: () => dispatch, useStoreSelector: (select: (state: unknown) => unknown) => select(store.state) }));-import { HeaderThreadTabs } from "./HeaderThreadTabs";+import { HeaderThreadTabs, useBotHasThreads, useThreadTabsShown } from "./HeaderThreadTabs";(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;@@ -36,16 +36,32 @@ beforeEach(() => { feature.on = true; dispatch.mockClear(); });afterEach(() => { act(() => root?.unmount()); root = null; document.body.innerHTML = ""; });describe("thread tab strip", () => {- it("is not there while Thread tabs is off, or for a bot with one thread", () => {+ it("is not there while Thread tabs is off, even for a bot with many threads", () => {feature.on = false;render(kiwi(FIVE, "launch"));expect(document.querySelector("[data-thread-tabs]")).toBeNull();act(() => root!.unmount());- feature.on = true;render(kiwi([task("main", "Only", 1)], "main"));expect(document.querySelector("[data-thread-tabs]")).toBeNull();});+ it("shows for a bot with one thread once Thread tabs is on: one tab with no close, and the + for a new one", () => {+ render(kiwi([task("main", "Only", 1)], "main"));+ expect(document.querySelector("[data-thread-tabs]")).not.toBeNull();+ expect(tabs().map((element) => element.getAttribute("data-tab-id"))).toEqual(["main"]);+ expect(tab("Only").getAttribute("aria-selected")).toBe("true");+ expect(closeOf("Only")).toBeNull();+ act(() => document.querySelector<HTMLButtonElement>("[data-thread-tabs-new]")!.click());+ expect(sent()).toEqual([{ type: "newTask", botId: "kiwi" }]);+ });++ it("shows a brand-new bot's open thread as a tab before it has a thread row", () => {+ render(kiwi([], "main"));+ expect(tabs().map((element) => element.getAttribute("data-tab-id"))).toEqual(["main"]);+ expect(tab("Untitled chat").getAttribute("aria-selected")).toBe("true");+ expect(document.querySelector("[data-thread-tabs-new]")).not.toBeNull();+ });+it("lists pinned threads first, then by recent use, with the open one selected", () => {render(kiwi(FIVE, "launch"));expect(tabs().map((element) => element.querySelector("[data-tab-label] > span")?.textContent)).toEqual(["Launch plan", "Weekly sales", "Invoice for Acme", "Berlin trip", "Call notes"]);@@ -111,3 +127,26 @@ describe("thread tab strip", () => {expect(sent()).toEqual([{ type: "renameTask", botId: "kiwi", threadId: "notes", title: "Call with Dana" }]);});});++describe("whether the header shows tabs, and Simple's pill switcher", () => {+ const Probe = () => createElement("output", { "data-tabs": String(useThreadTabsShown()), "data-pill": String(useBotHasThreads("kiwi")) });+ const render2 = (element: ReturnType<typeof createElement>) => {+ root = createRoot(document.body.appendChild(document.createElement("div")));+ act(() => root!.render(element));+ };+ const probe = (bot: Bot) => {+ store.state = { bots: [bot], newChat: null };+ render2(createElement(Probe));+ const out = document.querySelector("output")!;+ return { tabs: out.dataset.tabs, pill: out.dataset.pill };+ };++ it("shows tabs for one thread with Thread tabs on, and keeps the pill switcher for two threads or more either way", () => {+ expect(probe(kiwi([task("main", "Only", 1)], "main"))).toEqual({ tabs: "true", pill: "false" });+ act(() => root!.unmount());+ feature.on = false;+ expect(probe(kiwi([task("main", "Only", 1)], "main"))).toEqual({ tabs: "false", pill: "false" });+ act(() => root!.unmount());+ expect(probe(kiwi(FIVE, "launch"))).toEqual({ tabs: "false", pill: "true" });+ });+});diff --git a/src/components/HeaderThreadTabs.tsx b/src/components/HeaderThreadTabs.tsxindex 0019001504..32fba26430 100644--- a/src/components/HeaderThreadTabs.tsx+++ b/src/components/HeaderThreadTabs.tsx@@ -11,7 +11,7 @@ import type { Bot } from "@/state/store";import { useStoreDispatch, useStoreSelector } from "@/state/store-select";import { BotAvatar } from "./Avatar";import { reducedMotion } from "./MenuMotion";-import { botShowsTabs, neighbourOf, openTabId, sameTabs, showsTabs, threadTabs, type ThreadTab } from "./thread-tabs";+import { botShowsTabs, neighbourOf, openTabId, sameTabs, stripTabs, type ThreadTab } from "./thread-tabs";import { useSlidingIndicator, useTabMotion } from "./thread-tab-motion";import { UndoToast, useUndoToast } from "./UndoToast";@@ -19,24 +19,23 @@ const TAB = "thread-tab group/tab relative z-[1] flex h-8.5 min-w-24 max-w-42 shconst CLOSE = "pointer-events-none ms-1 flex size-5 shrink-0 items-center justify-center rounded-full text-ink-tertiary opacity-0 transition-opacity duration-[120ms] hover:bg-ink/[0.08] hover:text-ink group-hover/tab:pointer-events-auto group-hover/tab:opacity-100 group-focus-within/tab:pointer-events-auto group-focus-within/tab:opacity-100 touch:pointer-events-auto touch:opacity-100 motion-reduce:transition-none";const FADE = 24;-/** Whether the chat header shows this bot's threads as tabs in place of its pill and New thread, in windows wide enough- * for them. */-export function useThreadTabsShown(botId: string): boolean {- const on = useFeature("threadTabs");- return useBotHasThreads(botId) && on;+/** Whether the chat header shows the thread tab strip in place of the bot's pill and New thread, in windows wide enough+ * for it: whenever Thread tabs is on, so a bot with one thread still has the strip's + to make another. */+export function useThreadTabsShown(): boolean {+ return useFeature("threadTabs");}-/** Whether this bot has two threads or more to switch between, its blank New thread chat counted. */+/** Whether this bot has two threads or more to switch between, its blank New thread chat counted: Simple's pill+ * switcher shows for these, whether or not Thread tabs is on. */export function useBotHasThreads(botId: string): boolean {return useStoreSelector((state) => botShowsTabs(state.bots.find((bot) => bot.id === botId), state.newChat));}-/** A bot's threads as tabs in the chat header, once Thread tabs is on and the bot has two threads or more. */+/** A bot's threads as tabs in the chat header, with a + for a new one, once Thread tabs is on. */export function HeaderThreadTabs({ bot }: { bot: Bot }) {const on = useFeature("threadTabs");- const tabs = threadTabs(bot);- if (!on || !showsTabs(tabs)) return null;- return <Strip bot={bot} tabs={tabs} active={openTabId(bot)} />;+ if (!on) return null;+ return <Strip bot={bot} tabs={stripTabs(bot)} active={openTabId(bot)} />;}/** The mask that fades a sideways-scrolling row's edges where more of it is hidden, none when it all fits. */diff --git a/src/components/NewThreadHeaderButton.tsx b/src/components/NewThreadHeaderButton.tsxindex 102aa6ca34..f603d7f94c 100644--- a/src/components/NewThreadHeaderButton.tsx+++ b/src/components/NewThreadHeaderButton.tsx@@ -7,7 +7,7 @@ import { useBotHasThreads, useThreadTabsShown } from "./HeaderThreadTabs";export function NewThreadHeaderButton({ botId }: { botId: string }) {const dispatch = useStoreDispatch();- const tabs = useThreadTabsShown(botId);+ const tabs = useThreadTabsShown();const simplePill = !useAdvancedMode() && window.ogb?.remoteClient?.active !== true;const pillHasIt = useBotHasThreads(botId) && simplePill;if (pillHasIt) return null;diff --git a/src/components/thread-tabs.test.ts b/src/components/thread-tabs.test.tsindex b16a8bcf43..200aab3383 100644--- a/src/components/thread-tabs.test.ts+++ b/src/components/thread-tabs.test.ts@@ -1,7 +1,7 @@import { describe, expect, it } from "vitest";import type { Task } from "@/state/store";import { newChatThreadId } from "@/state/new-chat";-import { botShowsTabs, neighbourOf, showsTabs, threadTabs, type TabsBot } from "./thread-tabs";+import { botShowsTabs, neighbourOf, showsTabs, stripTabs, threadTabs, type TabsBot } from "./thread-tabs";const task = (threadId: string, at: number, extra: Partial<Task> = {}): Task => ({ threadId, title: threadId, createdAt: at, lastThreadOrderAt: at, ...extra });@@ -45,6 +45,21 @@ describe("thread tabs", () => {expect(botShowsTabs(one, { session: 1, botId: "other", held: [] })).toBe(false);});+ it("gives the strip a tab for a bot's one thread, with no close", () => {+ const one = bot([task("only", 1)], { mainThreadId: "elsewhere" });+ expect(stripTabs(one)).toEqual(threadTabs(one));+ expect(stripTabs(one)).toEqual([expect.objectContaining({ threadId: "only", closable: false, draft: false })]);+ });++ it("gives the strip a tab for the open thread before it has a thread row, after the pinned ones", () => {+ expect(stripTabs(bot([], { threadId: "fresh", busy: true }))).toEqual([+ { threadId: "fresh", title: "New thread", pinned: false, working: true, draft: false, closable: false, renamable: false },+ ]);+ const tabs = stripTabs(bot([task("pin", 1, { pinned: true }), task("a", 2)], { threadId: "fresh" }));+ expect(tabs.map((tab) => tab.threadId)).toEqual(["pin", "fresh", "a"]);+ expect(stripTabs(bot([], { threadId: "" }))).toEqual([]);+ });+it("puts the blank New thread tab where its thread will land: after the pinned ones", () => {const tabs = threadTabs(bot([task("pin", 1, { pinned: true }), task("a", 3), task("b", 2)], { threadId: newChatThreadId("kiwi") }));expect(tabs.map((tab) => tab.threadId)).toEqual(["pin", newChatThreadId("kiwi"), "a", "b"]);diff --git a/src/components/thread-tabs.ts b/src/components/thread-tabs.tsindex 3eef90c090..0fd3656e06 100644--- a/src/components/thread-tabs.ts+++ b/src/components/thread-tabs.ts@@ -3,6 +3,9 @@ import type { NewChat } from "@/state/new-chat";import { isNewChatThread, newChatThreadId } from "@/state/new-chat";import { isArchived, isSnoozed, isWorking, orderedThreadList } from "./SidebarThreadRow";+/** The factory title a thread starts with, shown localized. */+const UNTITLED = "New thread";+export interface ThreadTab {threadId: string;title: string;@@ -58,9 +61,23 @@ export function sameTabs(a: readonly ThreadTab[], b: readonly ThreadTab[]): boolreturn a === b || (a.length === b.length && a.every((tab, i) => sameTab(tab, b[i]!)));}-/** The strip shows for a bot with two threads or more, its blank New thread chat counted. */+/** Whether a bot has two threads or more to switch between, its blank New thread chat counted: Simple's pill switcher+ * shows for these. The tab strip shows whenever Thread tabs is on, see stripTabs. */export const showsTabs = (tabs: readonly ThreadTab[]): boolean => tabs.length > 1;+/** The thread tab strip's tabs: the bot's threads, and the open one before it has a thread row of its own, so a bot+ * with one thread, or none yet, still gets a tab to sit on beside the strip's +. */+export function stripTabs(bot: TabsBot): ThreadTab[] {+ const tabs = threadTabs(bot);+ const open = openTabId({ threadId: bot.threadId, switchingTo: bot.switchingTo });+ if (!open || tabs.some((tab) => tab.threadId === open)) return tabs;+ const at = tabs.findIndex((tab) => !tab.pinned);+ const tab: ThreadTab = {+ threadId: open, title: UNTITLED, pinned: false, working: open === bot.threadId && Boolean(bot.busy), draft: false, closable: false, renamable: false,+ };+ return [...tabs.slice(0, at < 0 ? tabs.length : at), tab, ...tabs.slice(at < 0 ? tabs.length : at)];+}+/** Where closing a tab goes when it was the open one: the tab after it, else the one before. */export function neighbourOf(tabs: readonly ThreadTab[], threadId: string): string | undefined {const at = tabs.findIndex((tab) => tab.threadId === threadId);@@ -68,7 +85,7 @@ export function neighbourOf(tabs: readonly ThreadTab[], threadId: string): strinreturn (tabs[at + 1] ?? tabs[at - 1])?.threadId;}-/** Whether the strip shows for this bot in the chat: the header's New thread plus gives way to the strip's own. */+/** Whether this bot has two threads or more in the chat, its blank New thread chat counted, as showsTabs. */export function botShowsTabs(bot: TabsBot | undefined, newChat: NewChat | null | undefined): boolean {if (!bot) return false;const shown = newChat?.botId === bot.id ? { ...bot, threadId: newChatThreadId(bot.id), switchingTo: undefined } : bot;