Skip to content

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

Unread: the launch move to a bot's main thread keeps the reload hold, and a held thread keeps its owner unread

luishron123f69dd4b78a01
diff --git a/src/state/read-visible.test.ts b/src/state/read-visible.test.ts
index fe6b8290c4..e1263ec92a 100644
--- a/src/state/read-visible.test.ts
+++ b/src/state/read-visible.test.ts
@@ -194,3 +194,42 @@ describe("a conversation marked unread while it is open", () => {
});
});
});
+
+describe("a conversation a reloaded page put back on screen unread", () => {
+ const reloaded = async () => { vi.resetModules(); return import("./read-visible"); };
+ const tasks = [{ threadId: "side", unread: true, updatedAt: 10 }, { threadId: "main", unread: true, updatedAt: 20 }];
+ const showing = (threadId: string) => ({ selectedId: "bot", activeView: "chat", forkPanel: null, groups: [],
+ bots: [{ id: "bot", threadId, mainThreadId: "main", tasks }] }) as unknown as AppState;
+
+ it("stays held when the page itself moves to the bot's main thread at launch", async () => {
+ const page = await reloaded();
+ page.holdRestoredUnread(showing("side"));
+ page.launchNavigation(() => page.holdMarkedUnread({ type: "switchTask", botId: "bot", threadId: "main" }, showing("side")));
+ expect(page.shownChatHeldUnread(showing("main"))).toBe(true);
+ const read: string[] = [];
+ page.markVisibleBotThreadsRead(showing("main"), { id: "bot", unread: true, tasks } as unknown as BotAnnouncement, true, (_path, threadId) => read.push(threadId));
+ expect(read).toEqual([]);
+ page.holdMarkedUnread({ type: "switchTask", botId: "bot", threadId: "main" }, showing("main"));
+ expect(page.shownChatHeldUnread(showing("main")), "the person opening it ends the hold").toBe(false);
+ });
+
+ it("keeps a bot announced without its conversations unread when a thread beside it is read", async () => {
+ const page = await reloaded();
+ const state = { selectedId: "bot", activeView: "chat", groups: [], forkPanel: { kind: "bot", ownerId: "bot", threadId: "fork" },
+ bots: [{ id: "bot", threadId: "main", unread: true }] } as unknown as AppState;
+ page.holdRestoredUnread(state);
+ const bot = { id: "bot", unread: true } as unknown as BotAnnouncement;
+ page.markVisibleBotThreadsRead(state, bot, true, () => {});
+ expect(bot.unread).toBe(true);
+ });
+
+ it("keeps a room announced without its threads unread when a thread beside it is read", async () => {
+ const page = await reloaded();
+ const state = { selectedId: "room", activeView: "chat", bots: [], config: {}, forkPanel: { kind: "group", ownerId: "room", threadId: "fork" },
+ groups: [{ id: "room", threadId: "main", unread: true }] } as unknown as AppState;
+ page.holdRestoredUnread(state);
+ const group = { id: "room", unread: true } as Partial<Group> & { id: string };
+ page.markVisibleGroupThreadsRead(state, group, true, () => {});
+ expect(group.unread).toBe(true);
+ });
+});
diff --git a/src/state/read-visible.ts b/src/state/read-visible.ts
index 8b6fd1ebdb..4d07dc786b 100644
--- a/src/state/read-visible.ts
+++ b/src/state/read-visible.ts
@@ -25,6 +25,22 @@ let restoreSettled = false;
* stays unread until the person opens a conversation here. */
let restoredUnread: string | undefined;
+/** Whether the page is opening, by itself, the conversation a bot opens to at launch. */
+let launching = false;
+
+/** Runs the page's own move at launch to the conversation a bot opens to: it is not the person opening one, so a
+ * conversation restored unread stays held, now on the thread the page moved to. */
+export function launchNavigation(navigate: () => void): void {
+ launching = true;
+ try { navigate(); } finally { launching = false; }
+}
+
+function threadIsUnread(state: AppState, botId: string, threadId: string): boolean {
+ const bot = state.bots.find((candidate) => candidate.id === botId);
+ const task = bot?.tasks?.find((candidate) => candidate.threadId === threadId);
+ return Boolean(task ? task.unread : bot?.unread);
+}
+
function shownBotThread(state: AppState): string | undefined {
if (state.activeView !== "chat" || (state.newChat && state.newChat.botId === state.selectedId)) return undefined;
const bot = state.bots.find((candidate) => candidate.id === state.selectedId);
@@ -43,6 +59,11 @@ function openedBotThread(action: Action, state: AppState): string | undefined {
* this one included, write in one, or act from anywhere else in the app. The server's own mark ends only for the
* conversation they open or write in. */
export function holdMarkedUnread(action: Action, state: AppState): void {
+ if (launching) {
+ // The page moving to the bot's main thread by itself at launch: the hold follows it, since nobody chose to look.
+ if (action.type === "switchTask") restoredUnread = threadIsUnread(state, action.botId, action.threadId) ? action.threadId : undefined;
+ return;
+ }
if (OPENS_A_CONVERSATION.has(action.type) || OPENS_A_ROOM_THREAD.has(action.type)) {
restoreSettled = true;
restoredUnread = undefined;
@@ -124,8 +145,9 @@ export function markVisibleBotThreadsRead(state: AppState, bot: BotAnnouncement,
if (task) task.unread = false;
post(`/api/bots/${bot.id}/read`, threadId, task?.updatedAt ?? 0, task?.heldUnread !== undefined ? "open" : "seen");
}
+ // Without per-thread state the aggregate mark is the shown thread's, so a restored hold on it keeps the bot unread.
if (bot.tasks) bot.unread = botShowsUnread(bot);
- else if (!held && shown) bot.unread = false;
+ else if (!held && shown && shown !== restoredUnread) bot.unread = false;
}
export function markVisibleGroupThreadsRead(state: AppState, group: Partial<Group> & { id: string }, focused: boolean,
@@ -142,5 +164,5 @@ export function markVisibleGroupThreadsRead(state: AppState, group: Partial<Grou
post(`/api/groups/${group.id}/read`, threadId, task?.updatedAt ?? 0, "seen");
}
if (group.tasks) group.unread = group.tasks.some((task) => task.unread);
- else if (shown) group.unread = false;
+ else if (shown && shown !== restoredUnread) group.unread = false;
}
diff --git a/src/state/store.tsx b/src/state/store.tsx
index 772b51392c..8e62311f4c 100644
--- a/src/state/store.tsx
+++ b/src/state/store.tsx
@@ -19,7 +19,7 @@ import { isNotificationFeedAction, reduceNotificationFeed, type NotificationFeed
import { bumpThreadUpdatedAt, mergeTaskStamps, rewindThreadUpdatedAt } from "./thread-stamps";
import { botOpensTo, closeTrayUnlessShown, closeUnpinnedTray, openBot, openThread, visibleNotificationThread, withRememberedTrayPin } from "./thread-navigation";
import { patchGroupTask } from "./thread-edits"; import { unreadBeyondFrame, wholeBotFrame } from "./bot-thread-frame";
-import type { EngineInstall } from "./engine-install"; import { holdRestoredUnread } from "./read-visible";
+import type { EngineInstall } from "./engine-install"; import { holdRestoredUnread, launchNavigation } from "./read-visible";
import { optimisticMessageId, optimisticUserMessage, sendArrived, sendReconciled } from "./optimistic-send";
import { patchedGroups, roomAnswers, snapshotPatch } from "./room-frames";
import { savedStateAction, type SavedOwner, type SavedSnapshot } from "./saved-state";
@@ -2827,7 +2827,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {
if (!state.hydrated || !state.config || launched.current) return;
launched.current = true;
const bot = state.bots.find((candidate) => candidate.id === state.selectedId);
- if (bot && state.activeView === "chat" && botOpensTo(bot, showThreads) !== bot.threadId) openBot(dispatch, bot, showThreads);
+ if (bot && state.activeView === "chat" && botOpensTo(bot, showThreads) !== bot.threadId) launchNavigation(() => openBot(dispatch, bot, showThreads));
}, [state.hydrated, state.config, state.bots, state.selectedId, state.activeView, showThreads, dispatch]);
useEffect(() => {