Skip to content

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

Mark as unread: hold it on the chat that is open

luishron1237c66550f1733
diff --git a/src/state/read-visible.test.ts b/src/state/read-visible.test.ts
index 40526d1a5b..7d36ec6ba6 100644
--- a/src/state/read-visible.test.ts
+++ b/src/state/read-visible.test.ts
@@ -1,6 +1,6 @@
-import { expect, it } from "vitest";
-import { markVisibleBotThreadsRead, markVisibleGroupThreadsRead } from "./read-visible";
-import type { AppState, BotAnnouncement, Group } from "./store";
+import { describe, expect, it } from "vitest";
+import { holdMarkedUnread, markVisibleBotThreadsRead, markVisibleGroupThreadsRead } from "./read-visible";
+import type { Action, AppState, BotAnnouncement, Group } from "./store";
it("marks both visible bot threads read while leaving a hidden task unread", () => {
const bot = { id: "bot", unread: true, tasks: [
@@ -27,3 +27,76 @@ it("marks the main room and its open fork read", () => {
expect(group.tasks?.map((task) => task.unread)).toEqual([false, false, true]);
expect(group.unread).toBe(true);
});
+
+describe("a conversation marked unread while it is open", () => {
+ const open = (updatedAt: number | null = 10) => ({ selectedId: "bot", activeView: "chat", forkPanel: null,
+ bots: [{ id: "bot", threadId: "main", tasks: [{ threadId: "main", updatedAt: updatedAt ?? undefined }] }, { id: "other", threadId: "elsewhere" }] }) as unknown as AppState;
+ const announced = (updatedAt = 10, id = "bot", threadId = "main") =>
+ ({ id, unread: true, tasks: [{ threadId, unread: true, updatedAt }] }) as unknown as BotAnnouncement;
+ const seen = (state: AppState, bot: BotAnnouncement) => {
+ const read: string[] = [];
+ markVisibleBotThreadsRead(state, bot, true, (_path, threadId) => read.push(threadId));
+ return { read, unread: bot.unread, thread: bot.tasks?.[0]?.unread };
+ };
+ const mark = (state: AppState, botId = "bot") => holdMarkedUnread({ type: "markUnread", botId }, state);
+ const act = (action: unknown, state: AppState) => holdMarkedUnread(action as Action, state);
+
+ it("stays unread however often the server announces it", () => {
+ const state = open();
+ mark(state);
+ expect(seen(state, announced())).toEqual({ read: [], unread: true, thread: true });
+ expect(seen(state, announced())).toEqual({ read: [], unread: true, thread: true });
+ act({ type: "select", id: "bot" }, state);
+ });
+
+ it("is read again once a newer message is seen", () => {
+ const state = open();
+ mark(state);
+ expect(seen(state, announced()).read).toEqual([]);
+ expect(seen(state, announced(11))).toEqual({ read: ["main"], unread: false, thread: false });
+ expect(seen(state, announced(11)).read).toEqual(["main"]);
+ });
+
+ it("takes the first announcement as its newest message when the app knows no time for it", () => {
+ const state = open(null);
+ mark(state);
+ expect(seen(state, announced(30)).read).toEqual([]);
+ expect(seen(state, announced(30)).read).toEqual([]);
+ expect(seen(state, announced(31)).read).toEqual(["main"]);
+ });
+
+ it.each([
+ ["the person opens another bot", { type: "select", id: "other" }],
+ ["the person opens it again", { type: "select", id: "bot" }],
+ ["the person switches conversation", { type: "switchTask", botId: "bot", threadId: "second" }],
+ ["the person starts a new conversation", { type: "newTask", botId: "bot" }],
+ ["the person writes in it", { type: "send", botId: "bot", text: "hello" }],
+ ])("is read as before once %s", (_what, action) => {
+ const state = open();
+ mark(state);
+ act(action, state);
+ expect(seen(state, announced())).toEqual({ read: ["main"], unread: false, thread: false });
+ });
+
+ it("keeps a bot announced without its conversations unread", () => {
+ const state = open();
+ mark(state);
+ const bot = { id: "bot", unread: true } as unknown as BotAnnouncement;
+ expect(seen(state, bot)).toMatchObject({ read: [], unread: true });
+ act({ type: "select", id: "bot" }, state);
+ expect(seen(state, { id: "bot", unread: true } as unknown as BotAnnouncement)).toMatchObject({ read: ["main"], unread: false });
+ });
+
+ it("holds nothing for a bot marked while another chat is open: opening it reads it", () => {
+ const state = open();
+ mark(state, "other");
+ const later = { ...state, selectedId: "other" } as AppState;
+ expect(seen(later, announced(10, "other", "elsewhere"))).toEqual({ read: ["elsewhere"], unread: false, thread: false });
+ });
+
+ it("holds nothing while the chat is not what the person is looking at", () => {
+ const state = { ...open(), activeView: "routines" } as unknown as AppState;
+ mark(state);
+ expect(seen(open(), announced()).read).toEqual(["main"]);
+ });
+});
diff --git a/src/state/read-visible.ts b/src/state/read-visible.ts
index 021536c706..0c66500139 100644
--- a/src/state/read-visible.ts
+++ b/src/state/read-visible.ts
@@ -1,5 +1,5 @@
import { botShowsUnread } from "@/lib/bot-unread";
-import type { AppState, BotAnnouncement, Group } from "./store";
+import type { Action, AppState, BotAnnouncement, Group } from "./store";
import { serverFetch } from "@/lib/relay-server";
type SendRead = (path: string, threadId: string, updatedAt: number) => void;
@@ -7,6 +7,21 @@ const sendRead: SendRead = (path, threadId) => {
void serverFetch(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ threadId }) }).catch(() => {});
};
+/** Conversations the person marked unread while looking at them, with their newest message then (unknown until the
+ * server next announces them). Being on screen reads none of them again until a newer message arrives. */
+const markedUnread = new Map<string, number | undefined>();
+const RESUMES_READING: ReadonlySet<Action["type"]> = new Set(["select", "openDataResult", "switchTask", "newTask", "send"]);
+
+/** "Mark as unread" on the open conversation holds until the person opens a conversation again, this one included, or
+ * writes in one. */
+export function holdMarkedUnread(action: Action, state: AppState): void {
+ if (RESUMES_READING.has(action.type)) return markedUnread.clear();
+ if (action.type !== "markUnread" || action.botId !== state.selectedId || state.activeView !== "chat") return;
+ const bot = state.bots.find((candidate) => candidate.id === action.botId);
+ const shown = bot?.switchingTo ?? bot?.threadId;
+ if (shown) markedUnread.set(shown, bot?.tasks?.find((task) => task.threadId === shown)?.updatedAt);
+}
+
function visibleThreadIds(state: AppState, kind: "bot" | "group", ownerId: string, shown?: string): string[] {
const panel = state.forkPanel;
return [...new Set([shown, panel?.kind === kind && panel.ownerId === ownerId ? panel.threadId : undefined])]
@@ -18,13 +33,24 @@ export function markVisibleBotThreadsRead(state: AppState, bot: BotAnnouncement,
const selected = state.bots.find((candidate) => candidate.id === bot.id);
const shown = selected?.switchingTo ?? selected?.threadId;
const visibleThreads = visibleThreadIds(state, "bot", bot.id, shown);
+ let held = false;
for (const threadId of visibleThreads) {
const task = bot.tasks?.find((candidate) => candidate.threadId === threadId);
if (!task?.unread && (bot.tasks || !bot.unread)) continue;
+ if (markedUnread.has(threadId)) {
+ const newest = task?.updatedAt ?? 0, marked = markedUnread.get(threadId) ?? newest;
+ if (newest <= marked) {
+ markedUnread.set(threadId, marked);
+ held = true;
+ continue;
+ }
+ markedUnread.delete(threadId);
+ }
if (task) task.unread = false;
post(`/api/bots/${bot.id}/read`, threadId, task?.updatedAt ?? 0);
}
- bot.unread = bot.tasks ? botShowsUnread(bot) : false;
+ if (bot.tasks) bot.unread = botShowsUnread(bot);
+ else if (!held) bot.unread = false;
}
export function markVisibleGroupThreadsRead(state: AppState, group: Partial<Group> & { id: string }, focused: boolean,
diff --git a/src/state/store.tsx b/src/state/store.tsx
index c025413e49..c35e2adde6 100644
--- a/src/state/store.tsx
+++ b/src/state/store.tsx
@@ -50,7 +50,7 @@ import {
import type { Routine, RoutineInput, RoutineRun, RoutineRunStatusFilter } from "@/lib/routines";
import type { WebhookAttempt, WebhookIngressStatus, WebhookTrigger } from "@/lib/webhooks";
import { duplicateBotProfile, restrictedCopyRequest } from "@/lib/bot-duplicate";
-import { markVisibleBotThreadsRead, markVisibleGroupThreadsRead } from "./read-visible";
+import { holdMarkedUnread, markVisibleBotThreadsRead, markVisibleGroupThreadsRead } from "./read-visible";
import { applyWindowPref } from "@/lib/window-prefs"; import { applyThemeFrame } from "@/lib/theme/runtime"; import { appCatalogAnnounced } from "@/lib/app-catalog-signal";
import { placeUnreadDivider, threadOpensUnread, threadReadCursor, unreadDividerFor, type UnreadDivider } from "@/lib/unread-divider";
import type { ComputerStart } from "@/lib/computer-start";
@@ -2193,6 +2193,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {
store.flushFrames(); if (["select", "newTask", "switchTask", "newGroupTask", "switchGroupTask", "showChat", "showRoutines", "showTeamMap", "showFiles", "showWebPanel", "showPublishedApps", "showSkillBurgers", "showTeamFiles", "showAnnouncements"].includes(action.type)) selectionRevision.current++;
// Pin before any await or optimistic state change.
action = pinBotThreadAction(action, stateRef.current.bots); if (runNewChat(action)) return;
+ holdMarkedUnread(action, stateRef.current);
if (action.type === "taskSwitched") action = { ...action, bot: withTaskWrites(action.bot) as Bot };
if (action.type === "botPatched") action = { ...action, bot: withTaskWrites(action.bot) };
if (action.type === "openForkPanel" && !action.loadId) action = { ...action, loadId: randomUuid() };
@@ -2556,9 +2557,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {
.finally(() => rawDispatch({ type: "botDeletionPending", botId: action.botId, on: false }));
break;
case "markUnread":
- api(`/api/bots/${action.botId}`, { method: "PATCH", body: JSON.stringify({ unread: true }) }).catch(
- () => {},
- );
+ api(`/api/bots/${action.botId}`, { method: "PATCH", body: JSON.stringify({ unread: true }) }).catch(() => {});
break;
case "openDataResult":
case "select": {