Skip to content

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

Place the unread divider once scrollback reaches the boundary

AdityaUmalec2dea5a3874f
diff --git a/src/lib/unread-divider.test.ts b/src/lib/unread-divider.test.ts
index e84dbc9c7c..ce2f31d410 100644
--- a/src/lib/unread-divider.test.ts
+++ b/src/lib/unread-divider.test.ts
@@ -120,6 +120,21 @@ describe("the divider in the store", () => {
expect(reducer(read, { type: "select", id: "room" }).unreadDivider).toEqual({ threadId: "room-t", messageId: "b3" });
});
+ it("waits above a short switch page until scrollback reaches the boundary", () => {
+ const tail = [line("b4", "bot"), line("b5", "bot")];
+ const short = { ...base, bots: [bot("other"), bot("pepper", { unread: true, hasMore: true, messages: tail,
+ tasks: [{ threadId: "pepper-a", title: "A", createdAt: 1, unread: true, lastReadMessageId: "b1" }] })],
+ groups: [{ ...room, hasMore: true, messages: tail, lastReadMessageId: "b1" }] };
+ for (const [id, threadId] of [["pepper", "pepper-a"], ["room", "room-t"]] as const) {
+ const opened = reducer(short, { type: "select", id });
+ expect(opened.unreadDivider?.messageId ?? null).toBeNull();
+ const partial = reducer(opened, { type: "olderMessages", threadId, generation: 0, messages: [line("b3", "bot")], hasMore: true });
+ expect(partial.unreadDivider?.messageId ?? null).toBeNull();
+ const reached = reducer(partial, { type: "olderMessages", threadId, generation: 0, messages: [line("b1", "bot"), line("b2", "bot")], hasMore: false });
+ expect(reached.unreadDivider).toEqual({ threadId, messageId: "b2" });
+ }
+ });
+
it("is dropped once done, only for its own conversation", () => {
const opened = reducer(base, { type: "select", id: "pepper" });
expect(reducer(opened, { type: "unreadDividerDone", threadId: "elsewhere" })).toBe(opened);
diff --git a/src/lib/unread-divider.ts b/src/lib/unread-divider.ts
index 5516a9a833..dee74b65a1 100644
--- a/src/lib/unread-divider.ts
+++ b/src/lib/unread-divider.ts
@@ -12,6 +12,20 @@ export function firstUnreadMessageId(messages: readonly Line[], lastReadMessageI
return seen < 0 ? null : messages[seen + 1]?.id ?? null;
}
+export type UnreadDivider = { threadId: string; messageId: string | null; lastReadMessageId?: string };
+
+// A short switch page can end below the boundary: keep the cursor so a scrollback page can place it.
+export function unreadDividerFor(threadId: string, messages: readonly Line[], lastReadMessageId: string | undefined, hasMore?: boolean): UnreadDivider | null {
+ const messageId = firstUnreadMessageId(messages, lastReadMessageId);
+ if (messageId) return { threadId, messageId };
+ return hasMore ? { threadId, messageId: null, lastReadMessageId } : null;
+}
+
+export function placeUnreadDivider(divider: UnreadDivider | null, threadId: string, messages: readonly Line[], hasMore: boolean): UnreadDivider | null {
+ if (divider?.threadId !== threadId || divider.messageId) return divider;
+ return unreadDividerFor(threadId, messages, divider.lastReadMessageId, hasMore);
+}
+
export function threadReadCursor(bot: {
threadId: string;
tasks?: ReadonlyArray<{ threadId: string; lastReadMessageId?: string }> | null;
diff --git a/src/state/chat-switch-page.test.ts b/src/state/chat-switch-page.test.ts
index 5cacd36aad..64eccfa2ed 100644
--- a/src/state/chat-switch-page.test.ts
+++ b/src/state/chat-switch-page.test.ts
@@ -1,5 +1,4 @@
// @vitest-environment happy-dom
-// Opening a chat fetches a short tail so big chats switch quickly; older messages load on scroll.
import { createElement, type Dispatch } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
diff --git a/src/state/store.tsx b/src/state/store.tsx
index f5052c87cc..e672711638 100644
--- a/src/state/store.tsx
+++ b/src/state/store.tsx
@@ -54,7 +54,7 @@ import type { Routine, RoutineInput, RoutineRun, RoutineRunStatusFilter } from "
import type { WebhookAttempt, WebhookIngressStatus, WebhookTrigger } from "@/lib/webhooks";
import { duplicateBotProfile, restrictedCopyRequest } from "@/lib/bot-duplicate";
import { botShowsUnread } from "@/lib/bot-unread";
-import { firstUnreadMessageId, threadOpensUnread, threadReadCursor } from "@/lib/unread-divider";
+import { placeUnreadDivider, threadOpensUnread, threadReadCursor, unreadDividerFor, type UnreadDivider } from "@/lib/unread-divider";
import type { ComputerStart } from "@/lib/computer-start";
import { parseComputerHelp } from "@/lib/computer-control";
import { answerResponse, dismissResponse } from "@/lib/card-answer";
@@ -399,10 +399,8 @@ function taskPatchFields(patch: TaskUpdatePatch): Partial<Task> {
...(link === undefined ? {} : { link: link ?? undefined }) };
}
-function openedUnreadDivider(state: AppState, threadId: string, unread: boolean, messages: readonly Message[], lastReadMessageId?: string): AppState["unreadDivider"] {
- if (!unread) return state.unreadDivider?.threadId === threadId ? state.unreadDivider : null;
- const messageId = firstUnreadMessageId(messages, lastReadMessageId);
- return messageId ? { threadId, messageId } : null;
+function openedUnreadDivider(state: AppState, threadId: string, unread: boolean, messages: readonly Message[], lastReadMessageId?: string, hasMore?: boolean): AppState["unreadDivider"] {
+ return unread ? unreadDividerFor(threadId, messages, lastReadMessageId, hasMore) : state.unreadDivider?.threadId === threadId ? state.unreadDivider : null;
}
import type { BrowserEngineSummary, BrowserProfile, ConfigStatus, ManagedPolicySummary } from "./config-status";
@@ -610,7 +608,7 @@ export interface AppState {
signInFocus: { botId: string; messageId: string; url: string; site: string; place: import("../../shared/wire").Surface; requestId: number; consumed: boolean } | null;
dataView: import("@/lib/composer-attachments").DataViewContext | null;
focusMessage: { threadId: string; messageId: string; matchText?: string; nonce: number; consumed: boolean } | null;
- unreadDivider: { threadId: string; messageId: string } | null;
+ unreadDivider: UnreadDivider | null;
connected: boolean;
hydrated?: true;
error: string | null;
@@ -956,12 +954,11 @@ export function reducer(state: AppState, action: Action): AppState {
hasMore: action.hasMore,
};
};
- return {
- ...state,
- loadingOlder,
- bots: state.bots.map((bot) => (bot.threadId === action.threadId ? prepend(bot) : bot)),
- groups: state.groups.map((group) => (group.threadId === action.threadId ? prepend(group) : group)),
- };
+ const bots = state.bots.map((bot) => (bot.threadId === action.threadId ? prepend(bot) : bot));
+ const groups = state.groups.map((group) => (group.threadId === action.threadId ? prepend(group) : group));
+ const owner = [...bots, ...groups].find((candidate) => candidate.threadId === action.threadId);
+ const unreadDivider = placeUnreadDivider(state.unreadDivider, action.threadId, owner ? visibleMessages(owner) : [], action.hasMore);
+ return { ...state, loadingOlder, bots, groups, unreadDivider };
}
case "sectionDeleted":
return {
@@ -1083,7 +1080,7 @@ export function reducer(state: AppState, action: Action): AppState {
if (room) {
return {
...(action.id === state.selectedId && state.activeView === "chat" ? state : closeUnpinnedTray(state)),
- unreadDivider: openedUnreadDivider(state, room.threadId, room.unread, room.messages, room.lastReadMessageId),
+ unreadDivider: openedUnreadDivider(state, room.threadId, room.unread, room.messages, room.lastReadMessageId, room.hasMore),
activeView: "chat",
selectedId: action.id,
groups: state.groups.map((g) => (g.id === action.id ? { ...g, unread: false } : g)),
@@ -1095,7 +1092,7 @@ export function reducer(state: AppState, action: Action): AppState {
{
...(action.id === state.selectedId && state.activeView === "chat" ? state : closeUnpinnedTray(state)),
unreadDivider: opened
- ? openedUnreadDivider(state, opened.threadId, threadOpensUnread(opened), visibleMessages(opened), threadReadCursor(opened))
+ ? openedUnreadDivider(state, opened.threadId, threadOpensUnread(opened), visibleMessages(opened), threadReadCursor(opened), opened.hasMore)
: state.unreadDivider,
activeView: "chat",
selectedId: action.id,
@@ -1772,7 +1769,7 @@ export function reducer(state: AppState, action: Action): AppState {
switched = { ...switched, backgroundThreadEvents };
const opened = switched.bots.find((bot) => bot.id === action.bot.id);
if (opened && opened.id === switched.selectedId) {
- switched = { ...switched, unreadDivider: openedUnreadDivider(switched, opened.threadId, threadOpensUnread(opened), visibleMessages(opened), threadReadCursor(opened)) };
+ switched = { ...switched, unreadDivider: openedUnreadDivider(switched, opened.threadId, threadOpensUnread(opened), visibleMessages(opened), threadReadCursor(opened), opened.hasMore) };
// The snapshot predates the switch's own announcement and must not put the unread dot back.
if (switched.activeView === "chat") switched = updateBot(switched, opened.id, (bot) => ({ ...bot,
unread: Boolean(bot.tasks?.some((task) => task.threadId !== bot.threadId && task.unread)),
@@ -1921,7 +1918,7 @@ export { api, ApiError, liveCallFromFrame, nextLiveCallLookup };
/** Messages per snapshot thread and per scrollback page: the server's own page maximum. */
export const MESSAGE_PAGE_SIZE = 200;
-export const SWITCH_MESSAGE_COUNT = 80; // a chat switch loads a short tail so big chats open fast; older messages load on scroll
+export const SWITCH_MESSAGE_COUNT = 80;
type TrustedApprovalBridge = {
setMode(