Skip to content

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

fix(place-inbox): keep a room message open until the person replies to it or taps Done

AdityaUmaleb48721a58170
diff --git a/server/host/place-inbox.test.ts b/server/host/place-inbox.test.ts
index 247e27fc85..8e32da14b1 100644
--- a/server/host/place-inbox.test.ts
+++ b/server/host/place-inbox.test.ts
@@ -1,4 +1,4 @@
-import { mkdtempSync, rmSync } from "node:fs";
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
@@ -7,7 +7,7 @@ import type { Notification } from "../../shared/notification.ts";
import type { PersonWait } from "../../shared/person-wait.ts";
import type { OptionCardData } from "../../shared/wire.ts";
import type { RequestAuth } from "../request-auth.ts";
-import { createPlaceInbox, PLACE_MESSAGES_HOURLY, placeInboxViewer, type InboxViewer, type OpenCardRow, type PlaceInboxDeps } from "./place-inbox.ts";
+import { createPlaceInbox, PLACE_INBOX_FILE, PLACE_MESSAGES_HOURLY, placeInboxViewer, type InboxViewer, type OpenCardRow, type PlaceInboxDeps } from "./place-inbox.ts";
const OWNER = { kind: "owner-session" } as unknown as RequestAuth;
const GUEST = { kind: "guest-session" } as unknown as RequestAuth;
@@ -17,7 +17,7 @@ const viewers = new Map<RequestAuth, InboxViewer>([[OWNER, { kind: "owner" }], [
const dirs: string[] = [];
afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); });
-type Said = { id: string; at: number; role: string; peerAsk?: boolean; relayed?: boolean; via?: string; text?: string; inbox?: unknown };
+type Said = { id: string; at: number; role: string; peerAsk?: boolean; relayed?: boolean; via?: string; text?: string; inbox?: unknown; replyToId?: string };
function setup(over: Partial<PlaceInboxDeps> = {}, dataDir = mkdtempSync(join(tmpdir(), "place-inbox-"))) {
dirs.push(dataDir);
@@ -32,12 +32,13 @@ function setup(over: Partial<PlaceInboxDeps> = {}, dataDir = mkdtempSync(join(tm
eli: { id: "eli", name: "Eli", threadId: "eli-main", color: "blue" }, mira: { id: "mira", name: "Mira", threadId: "mira-main", color: "red" },
};
const titles: Record<string, string> = { "eli-main": "Eli", "eli-pricing": "Pricing work", "eli-launch": "Launch work", "guest-chat": "Guest chat" };
+ const rooms: Record<string, { id: string; name: string; memberIds: string[] }> = { "room-main": { id: "room", name: "Team room", memberIds: ["eli", "mira"] } };
const listeners: Array<(change: { type: string; threadId: string }) => void> = [];
const store = {
bot: (id: string) => bots[id],
botByThread: (threadId: string) => (threadId.startsWith("eli") || threadId === "guest-chat" ? bots.eli : threadId.startsWith("mira") ? bots.mira : undefined),
taskByThread: (_botId: string, threadId: string) => (titles[threadId] ? { threadId, title: titles[threadId] } : undefined),
- groupByThread: () => undefined,
+ groupByThread: (threadId: string) => rooms[threadId],
appendMessage: (threadId: string, line: Omit<Said, "id" | "at">) => {
const posted = { ...line, id: `m${++serial}`, at: clock };
said.set(threadId, [...(said.get(threadId) ?? []), posted]);
@@ -247,6 +248,47 @@ describe("the inbox of a place that is no team's", () => {
expect(f.inbox.count(OWNER)).toBe(0);
});
+ it("keeps a bot's message from a room open until the person replies to that line or taps Done, since the room's other talk isn't their answer", () => {
+ const f = setup();
+ const sent = f.inbox.message("eli", "room-main", "The report is ready.");
+ expect(sent).toMatchObject({ status: 200, text: expect.stringContaining("posted in this room") });
+ const [row] = f.inbox.view(OWNER).rows;
+ expect(row).toMatchObject({ kind: "message", tab: "info", unread: true, origin: { threadId: "room-main", groupId: "room", title: "Team room" } });
+
+ f.tick(10);
+ f.store.appendMessage("room-main", { role: "user", text: "@Mira can you check the build?" });
+ f.store.appendMessage("room-main", { role: "bot", text: "On it." });
+ expect(f.inbox.view(OWNER).rows[0]!.tab, "the person talking to others in the room hasn't answered it").toBe("info");
+ f.store.appendMessage("room-main", { role: "user", text: "Thanks", replyToId: "some-other-line" });
+ expect(f.inbox.view(OWNER).rows[0]!.tab, "nor has a reply to another line").toBe("info");
+ f.store.appendMessage("room-main", { role: "user", text: "Thanks", replyToId: row!.id });
+ expect(f.inbox.view(OWNER).rows[0]!.tab).toBe("done");
+
+ const other = f.inbox.message("mira", "room-main", "Build is green.");
+ expect(other.status).toBe(200);
+ const next = f.inbox.view(OWNER).rows.find((one) => one.tab === "info")!;
+ f.store.appendMessage("room-main", { role: "user", text: "ok everyone" });
+ expect(f.inbox.view(OWNER).rows.find((one) => one.id === next.id)!.tab).toBe("info");
+ expect(f.inbox.markDone(OWNER, next.id)).toBe("done");
+ expect(f.inbox.view(OWNER).rows.find((one) => one.id === next.id)!.tab).toBe("done");
+ });
+
+ it("leaves a room message saved before this rule as it was: the person's next words there still settle it", () => {
+ const dataDir = mkdtempSync(join(tmpdir(), "place-inbox-"));
+ writeFileSync(join(dataDir, PLACE_INBOX_FILE), `${JSON.stringify({ version: 1, seenAsks: [], lines: [
+ { id: "old-line", botId: "eli", threadId: "room-main", groupId: "room", kind: "message", from: "Eli", text: "Earlier news", at: 500 },
+ ] })}\n`);
+ const f = setup({}, dataDir);
+ expect(f.inbox.view(OWNER).rows[0]).toMatchObject({ id: "old-line", tab: "info" });
+ f.store.appendMessage("room-main", { role: "user", text: "ok" });
+ expect(f.inbox.view(OWNER).rows[0]!.tab).toBe("done");
+ });
+
+ it("tells a bot in its own chat that the message is in the Inbox, with no room named", () => {
+ const f = setup();
+ expect(f.inbox.message("eli", "eli-pricing", "The report is ready.").text).not.toContain("room");
+ });
+
it("stops a loop of messages from one conversation, and refuses a conversation that isn't the bot's", () => {
const f = setup();
for (let index = 0; index < PLACE_MESSAGES_HOURLY; index++) expect(f.inbox.message("eli", "eli-main", `m${index}`).status).toBe(200);
diff --git a/server/host/place-inbox.ts b/server/host/place-inbox.ts
index 4f53a135e7..f8eb2cb8c1 100644
--- a/server/host/place-inbox.ts
+++ b/server/host/place-inbox.ts
@@ -39,6 +39,8 @@ const lineSchema = z.object({
text: z.string().max(NOTICE_TEXT_MAX), at: z.number(), seen: z.boolean().optional(), done: z.boolean().optional(),
/** The conversation this result is news for: its next unread result replaces this line, so one job keeps one row. */
job: id.optional(),
+ /** A message posted in a room: only a reply to it (or Done) answers it, not the person's other talk there. */
+ untilReply: z.boolean().optional(),
});
const fileSchema = z.object({ version: z.literal(1), lines: z.array(lineSchema).max(MAX_LINES), seenAsks: z.array(id).max(MAX_SEEN_ASKS) });
type Line = z.infer<typeof lineSchema>;
@@ -110,13 +112,14 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {
const titleOf = (botId: string, threadId: string): string => store.groupByThread(threadId)?.name ?? store.taskByThread(botId, threadId)?.title ?? store.bot(botId)?.name ?? "";
- /** The person wrote in the conversation after a bot's message reached them: that is their answer. */
+ /** The person wrote in the conversation after a bot's message reached them: that is their answer. In a room they keep
+ * talking to the others, so there only a reply to that very line answers it (or Done in the Inbox). */
function answeredThere(line: Line): boolean {
if (line.kind !== "message") return false;
const said = store.messagesFor(line.threadId);
for (let index = said.length - 1; index >= 0 && said[index]!.at >= line.at; index--) {
const one = said[index]!;
- if (one.role === "user" && !one.peerAsk && !one.relayed && one.via !== "api") return true;
+ if (one.role === "user" && !one.peerAsk && !one.relayed && one.via !== "api" && (!line.untilReply || one.replyToId === line.id)) return true;
}
return false;
}
@@ -204,14 +207,16 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {
const wire: InboxItem = { kind: "message", from: { name: bot.name, botId: bot.id }, replies: true };
const posted = store.appendMessage(threadId, { role: "bot", kind: "text", text, inbox: wire, ...(group ? { from: { botId: bot.id, name: bot.name, color: bot.color } } : {}) });
addLine({ id: posted.id, botId: bot.id, threadId, ...(group ? { groupId: group.id } : {}), kind: "message", from: bot.name.slice(0, 200), ...(handed ? { workingFor: handed } : {}),
- ...(guest ? { guest } : {}), text: clip(text, NOTICE_TEXT_MAX), at: now() });
+ ...(guest ? { guest } : {}), ...(group ? { untilReply: true } : {}), text: clip(text, NOTICE_TEXT_MAX), at: now() });
if (group) store.patchGroup(group.id, { unread: true });
else store.patchTask(bot.id, threadId, { unread: true });
if (!guest && bot.notifications !== false) {
deps.send({ kind: "mention", botId: bot.id, botName: bot.name, threadId, title: handed ? serverLine(conversationLanguage(threadId), "server.notify.workingFor", { who: bot.name, other: handed.name }) : serverLine(conversationLanguage(threadId), "server.notify.message", { who: bot.name }), body: oneLine(text, 200),
...(group ? { groupId: group.id } : {}), ...(bot.avatarUrl ? { avatarUrl: bot.avatarUrl } : {}) });
}
- return { status: 200, text: "It is in their Inbox and they were notified. Their answer comes back here, and you carry on from it." };
+ return { status: 200, text: group
+ ? "It is posted in this room and in their Inbox, and they were notified; it stays open there until they reply to it or mark it done. Their answer comes back here, and you carry on from it."
+ : "It is in their Inbox and they were notified. Their answer comes back here, and you carry on from it." };
}
/** A routine's report, the answer that sums up work handed out, or a failure of work nobody is watching gets a line; the