Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Inbox (#6416, second half): read finished work moves into a Done line
milind-sonib0acbabbbd78
diff --git a/server/host/person-inbox.test.ts b/server/host/person-inbox.test.tsindex 16a1b1ca36..6934b6e60c 100644--- a/server/host/person-inbox.test.ts+++ b/server/host/person-inbox.test.ts@@ -184,6 +184,17 @@ describe("asks routed to a person", () => {describe("finished work in a person's inbox", () => {const report = (): Notification => ({ kind: "done", botId: "scout", botName: "Scout", threadId: "ben-private", title: "Scout finished", body: "Daily digest: 3 new leads." });+ it("takes a routine's report out of the inbox once the person has read it", () => {+ const f = fixture();+ f.team.route(report(), { routine: true });+ const person = key(BEN.email);+ const row = f.team.inbox.view(person)!.rows[0]!;+ expect(row).toMatchObject({ kind: "result", tab: "info" });+ f.team.inbox.markRead(person, row.id);+ expect(f.team.inbox.view(person)!.rows[0]!.tab).toBe("done");+ expect(f.team.inbox.view(person)!.rows.filter((one) => one.tab === "info"), "the inbox holds only what needs them").toEqual([]);+ });+it("points the line at the message it came from, so a client can open the conversation there", () => {const f = fixture();const said = f.said("ben-private", "Can you look at the deck?", BEN);diff --git a/server/host/person-inbox.ts b/server/host/person-inbox.tsindex a23d0e064b..770a360823 100644--- a/server/host/person-inbox.ts+++ b/server/host/person-inbox.ts@@ -250,10 +250,11 @@ export function createPersonInbox(deps: PersonInboxDeps) {const mark = (threadId: string) => { changedAt.set(threadId, Math.max(now(), (changedAt.get(threadId) ?? 0) + 1)); };const touch = (threadId: string) => { mark(threadId); deps.changed?.(threadId); };- /** The tab a line is listed in: an ask by its card where it was asked, anything else by its answer or the person's done. */+ /** The tab a line is listed in: an ask by its card where it was asked, anything else by its answer or the person's done.+ * Finished work the person has read needs nothing more from them, so it leaves the inbox. */function tabOf(item: Item, copy?: Message): InboxTab {if (item.kind === "ask" && item.requestId) return askWaits(item, copy) ? "needs-you" : "done";- return item.done || settled(item) ? "done" : "info";+ return item.done || settled(item) || (item.kind === "result" && item.seen === true) ? "done" : "info";}/** How many asks in this inbox wait on its person: questions, approvals and work handed on, open here and where theydiff --git a/server/host/place-inbox.test.ts b/server/host/place-inbox.test.tsindex f94301242d..5227401dbd 100644--- a/server/host/place-inbox.test.ts+++ b/server/host/place-inbox.test.ts@@ -113,6 +113,16 @@ describe("finished work reaches the inbox once", () => {expect(infoRows(reopened).map((row) => row.text).sort()).toEqual(["All three pages are live.", "It stopped in eli-pricing."]);});+ it("takes finished work out of the inbox once the person has read it", () => {+ const f = setup();+ f.inbox.route(routine, { routine: true });+ expect(infoRows(f)).toHaveLength(1);+ f.inbox.markRead(OWNER, f.inbox.view(OWNER).rows[0]!.id);+ expect(infoRows(f), "finished work the person has read leaves the inbox").toEqual([]);+ expect(f.inbox.view(OWNER).rows[0]!.tab).toBe("done");+ expect(f.inbox.count(OWNER), "nothing waits on the person any more").toBe(0);+ });+it("keeps a message that waits for an answer listed after the person read it", () => {const f = setup();expect(f.inbox.message("eli", "eli-main", "Can you look at the deck?").status).toBe(200);diff --git a/server/host/place-inbox.ts b/server/host/place-inbox.tsindex daeaa607d1..ba13dc9af6 100644--- a/server/host/place-inbox.ts+++ b/server/host/place-inbox.ts@@ -128,7 +128,9 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {return false;}- const lineTab = (line: Line): InboxRow["tab"] => (line.done || answeredThere(line) ? "done" : "info");+ /** Finished work the person has read needs nothing more from them: it leaves the inbox. */+ const readWork = (line: Line): boolean => line.kind === "result" && line.seen === true;+ const lineTab = (line: Line): InboxRow["tab"] => (line.done || answeredThere(line) || readWork(line) ? "done" : "info");const lineFor = (auth: RequestAuth, line: Line): boolean => {const viewer = deps.viewer(auth);if (viewer.kind === "nobody" || !deps.sees(auth, line.threadId)) return false;diff --git a/src/components/inbox/InboxActive.tsx b/src/components/inbox/InboxActive.tsxindex 2a89bf49ae..f3e0ad43e6 100644--- a/src/components/inbox/InboxActive.tsx+++ b/src/components/inbox/InboxActive.tsx@@ -35,13 +35,14 @@ export function inboxOpenThread(state: Pick<AppState, "activeView" | "selectedIdreturn group ? { kind: "group", groupId: group.id, threadId: group.threadId } : undefined;}-/** The conversations whose news lines have all been read: reading in the Inbox counts as reading them. */+/** The conversations whose news lines have all been read: reading in the Inbox counts as reading them. Finished work+ * the person has read moves to the Done line, and still counts. */export function readNewsThreads(view: InboxView | null): Set<string> {const read = new Set<string>();const unread = new Set<string>();for (const row of view?.rows ?? []) {const threadId = row.origin?.threadId;- if (!threadId || row.tab !== "info") continue;+ if (!threadId || !(row.tab === "info" || (row.tab === "done" && row.kind === "result"))) continue;(row.unread ? unread : read).add(threadId);}for (const threadId of unread) read.delete(threadId);diff --git a/src/components/inbox/InboxPanel.dom.test.ts b/src/components/inbox/InboxPanel.dom.test.tsindex 96aa616966..bf2df1bc18 100644--- a/src/components/inbox/InboxPanel.dom.test.ts+++ b/src/components/inbox/InboxPanel.dom.test.ts@@ -3,14 +3,16 @@ import { act, createElement, type ReactElement } from "react";import { createRoot, type Root } from "react-dom/client";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-const server = vi.hoisted(() => ({ calls: [] as Array<{ path: string; method: string; body?: unknown }>, view: null as null | { rows: Array<{ id: string; tab: string }> } }));+const server = vi.hoisted(() => ({ calls: [] as Array<{ path: string; method: string; body?: unknown }>, view: null as null | { rows: Array<{ id: string; tab: string; kind?: string; unread?: boolean }> } }));vi.mock("@/lib/use-owner-or-admin", () => ({ useOwnerOrAdmin: () => true }));vi.mock("../../state/api", () => ({api: vi.fn(async (path: string, init?: RequestInit) => {server.calls.push({ path, method: init?.method ?? "GET", ...(init?.body ? { body: JSON.parse(String(init.body)) } : {}) });const done = /^\/api\/inbox\/([\w-]+)\/done$/.exec(path);if (done && server.view) server.view = { ...server.view, rows: server.view.rows.map((row) => (row.id === done[1] ? { ...row, tab: "done", unread: false } : row)) };- if (path === "/api/inbox/read-all" && server.view) server.view = { ...server.view, rows: server.view.rows.map((row) => ({ ...row, unread: false })) };+ if (path === "/api/inbox/read-all" && server.view) server.view = { ...server.view, rows: server.view.rows.map((row) => (row.unread && row.kind === "result"+ ? { ...row, unread: false, tab: "done" }+ : { ...row, unread: false })) };return server.view;}),}));@@ -84,16 +86,16 @@ describe("the inbox panel", () => {await mount(createElement(InboxPanel));const rowIds = () => [...document.querySelectorAll<HTMLElement>("[data-inbox-row]")].map((row) => row.dataset.inboxRow);const unreadIds = () => [...document.querySelectorAll<HTMLElement>("[data-inbox-row][data-unread]")].map((row) => row.dataset.inboxRow);- expect(unreadIds()).toEqual([approvalRow.id, messageRow.id]);+ expect(unreadIds()).toEqual([approvalRow.id, messageRow.id, reportRow.id]);expect(document.querySelector("[data-inbox-mark-all]")!.textContent).toBe("Mark all as read");await click("[data-inbox-mark-all]");expect(server.calls).toContainEqual({ path: "/api/inbox/read-all", method: "POST" });expect(unreadIds()).toEqual([]);- expect(rowIds(), "every row is still listed").toEqual([approvalRow.id, questionRow.id, messageRow.id, reportRow.id]);+ expect(rowIds(), "the finished work leaves the list, the rest stays").toEqual([approvalRow.id, questionRow.id, messageRow.id]);expect(document.querySelector(`[data-inbox-row="${approvalRow.id}"] [data-inbox-action="allow"]`), "the ask still waits on its answer").not.toBeNull();expect(dispatch).not.toHaveBeenCalledWith(expect.objectContaining({ type: "decideRequest" }));expect(server.calls.filter((call) => /\/(done)$/.test(call.path))).toEqual([]);- expect(currentInbox()!.rows.map((row) => [row.id, row.tab])).toEqual(inboxView().rows.map((row) => [row.id, row.tab]));+ expect(currentInbox()!.rows.map((row) => [row.id, row.tab])).toEqual(server.view!.rows.map((row) => [row.id, row.tab]));expect(document.querySelector("[data-inbox-mark-all]"), "nothing is unread").toBeNull();});@@ -116,6 +118,16 @@ describe("the inbox panel", () => {expect(document.querySelector(`[data-inbox-reading="${notice.id}"]`), "it is not read in the panel").toBeNull();});+ it("keeps what it took out of the list where the person can find it again", async () => {+ shown(inboxView([approvalRow, { ...reportRow, tab: "done" }]));+ openInbox();+ await mount(createElement(InboxPanel));+ const listed = () => [...document.querySelectorAll<HTMLElement>("[data-inbox-row]")].map((row) => row.dataset.inboxRow);+ expect(listed(), "a settled line is not listed again").toEqual([approvalRow.id]);+ await click("[data-inbox-done-toggle]");+ expect(listed()).toEqual([approvalRow.id, reportRow.id]);+ });+it("is a sheet at phone width", async () => {vi.stubGlobal("matchMedia", (query: string) => ({ matches: query.includes("max-width"), addEventListener() {}, removeEventListener() {} }));openInbox();diff --git a/src/components/inbox/InboxPanel.tsx b/src/components/inbox/InboxPanel.tsxindex 5d6dd9d535..bb76510744 100644--- a/src/components/inbox/InboxPanel.tsx+++ b/src/components/inbox/InboxPanel.tsx@@ -1,6 +1,6 @@import { useEffect, useRef, useState, type KeyboardEvent } from "react";import { createPortal } from "react-dom";-import { Inbox, Maximize2, Minimize2, Search, X } from "lucide-react";+import { Check, Inbox, Maximize2, Minimize2, Search, X } from "lucide-react";import type { InboxRow } from "../../../shared/inbox";import { cn } from "@/lib/cn";import { useAdvancedMode } from "@/lib/interface-mode";@@ -15,7 +15,7 @@ import { InboxRowItem } from "./InboxRow";import { FinishedThreads } from "./FinishedThreads";import { PinnedThreads, pinnedThreadRows } from "./PinnedThreads";import { matchesSearch, nextAfter } from "./inbox-answer";-import { listedRows, waitingCount } from "./inbox-view";+import { listedRows, rowsIn, waitingCount } from "./inbox-view";import { useInboxActions } from "./use-inbox-actions";import { roundButtonCls } from "../bot-panel/rows";@@ -30,6 +30,7 @@ export function InboxPanel() {const { run, openInChat } = useInboxActions(view);const [now, setNow] = useState(() => Date.now());const [query, setQuery] = useState("");+ const [showDone, setShowDone] = useState(false);useEffect(() => {const timer = setInterval(() => setNow(Date.now()), 30_000);return () => clearInterval(timer);@@ -48,10 +49,10 @@ export function InboxPanel() {if (row.tab === "needs-you" || row.replies) { readInboxLine(row.id); return; }openInChat(row);};- // The notice list is upstream's: a read line stays until the person marks it done. Reading a conversation moves the- // conversation to Finished; it does not remove the notice it came from.+ // Finished work the person has read leaves the list for the Done line at the foot; its conversation moves to Finished.const listed = listedRows(view);const rows = listed.filter((row) => matchesSearch(row, query));+ const done = rowsIn(view, "done").filter((row) => matchesSearch(row, query)).sort((a, b) => b.at - a.at);const ids = rows.map((row) => row.id);const at = reading ? ids.indexOf(reading.id) : -1;@@ -71,7 +72,7 @@ export function InboxPanel() {const step = (by: -1 | 1) => {const row = rows[at + by];- if (row) { readInboxRow(row); readInboxLine(row.id); }+ if (row) open(row);};const pinnedThreads = new Set(pinnedThreadRows(state.bots, state.groups, state.pendingQueued).map((row) => row.task.threadId));const count = waitingCount(view) + unreadConversations(inboxAttentionEntries(state, view), view);@@ -129,6 +130,17 @@ export function InboxPanel() {{!query.trim() && <InboxActive view={view} current={openThread} exclude={pinnedThreads} />}{/* The finished, already-read conversations. */}{!query.trim() && <FinishedThreads bots={state.bots} groups={state.groups} queued={state.pendingQueued} readThreads={readNewsThreads(view)} exclude={pinnedThreads} now={now} current={openThread} onOpen={(row) => dispatch(attentionJumpAction(row))} />}+ {done.length > 0 && (+ <section data-inbox-done className="px-1.5 pb-1.5">+ <button type="button" data-inbox-done-toggle aria-expanded={showDone} onClick={() => setShowDone(!showDone)}+ className="flex w-full items-center gap-1.5 rounded-lg px-2 py-1.5 text-[12px] text-ink-tertiary hover:bg-raised/70 hover:text-ink-secondary">+ <Check size={13} aria-hidden="true" className="shrink-0" />+ <span className="font-medium">{t("common.done")}</span>+ <span className="tabular-nums">{done.length}</span>+ </button>+ {showDone && done.map((row) => <InboxRowItem key={row.id} row={row} now={now} onOpen={open} onAction={run} />)}+ </section>+ )}</div></>)}diff --git a/src/components/inbox/attention-read.test.ts b/src/components/inbox/attention-read.test.tsindex 079a4c7283..978c3dccd6 100644--- a/src/components/inbox/attention-read.test.ts+++ b/src/components/inbox/attention-read.test.ts@@ -37,6 +37,15 @@ describe("inboxAttentionEntries", () => {expect([...readNewsThreads(view(line(false)))]).toEqual(["eli-a"]);expect([...readNewsThreads(view(line(true)))]).toEqual([]);});++ it("still counts it read once its read result has moved to the Done line, so Mark all as read empties the active list", () => {+ const moved = [{ id: "l1", kind: "result", tab: "done", origin: { threadId: "eli-a" }, unread: false }];+ expect([...readNewsThreads(view(moved))]).toEqual(["eli-a"]);+ expect(inboxAttentionEntries(state(bots), view(moved))).toEqual([]);+ expect(unreadConversations(inboxAttentionEntries(state(bots), view(moved)), view(moved))).toBe(0);+ // A line the person only marked done is no reading of the conversation.+ expect([...readNewsThreads(view([{ id: "l2", kind: "message", tab: "done", origin: { threadId: "eli-a" }, unread: false }]))]).toEqual([]);+ });});const open = (selectedId: string, activeView = "chat") =>diff --git a/src/testing/inbox-fixture.ts b/src/testing/inbox-fixture.tsindex 00fe56579b..456651ef8b 100644--- a/src/testing/inbox-fixture.ts+++ b/src/testing/inbox-fixture.ts@@ -18,7 +18,7 @@ export const messageRow: InboxRow = {from: { name: "Cleo", email: "[email protected]" }, via: { botId: "scout", name: "Scout" }, text: "The launch moved to Friday. Can you check the numbers before then?",};export const reportRow: InboxRow = {- id: "line-report", kind: "result", tab: "info", unread: false, at: INBOX_NOW - 3 * 60 * 60_000,+ id: "line-report", kind: "result", tab: "info", unread: true, at: INBOX_NOW - 3 * 60 * 60_000,from: { name: "Reporter", botId: "reporter" }, origin: { botId: "reporter", threadId: "rep-daily", title: "Daily digest" }, text: "Daily digest: 3 new leads, 1 reply waiting.",};export const settledRow: InboxRow = {