Skip to content

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

Inbox: list an automation's question once, not again for its Results-chat copy

AdityaUmalec76136148359
diff --git a/server/host/place-inbox.test.ts b/server/host/place-inbox.test.ts
index 5227401dbd..9ac8e97b16 100644
--- a/server/host/place-inbox.test.ts
+++ b/server/host/place-inbox.test.ts
@@ -185,6 +185,21 @@ describe("the inbox of a place that is no team's", () => {
expect(f.inbox.view(OWNER).rows).toEqual([]);
});
+ it("lists an automation's question once, though the run's Results chat holds a copy of its card", () => {
+ const f = setup();
+ const original = f.ask("eli-launch", "q");
+ f.cards.push({ ...original, threadId: "eli-pricing", id: "copy-q", at: original.at + 1, copyOf: original.id });
+ expect(f.inbox.view(OWNER).rows.map((row) => [row.id, row.origin?.threadId])).toEqual([["card-q", "eli-launch"]]);
+ expect(f.inbox.count(OWNER)).toBe(1);
+ });
+
+ it("still lists the Results chat's copy when the person can't see the run's own chat", () => {
+ const f = setup({ sees: (_auth, threadId) => threadId !== "eli-launch" });
+ const original = f.ask("eli-launch", "q");
+ f.cards.push({ ...original, threadId: "eli-pricing", id: "copy-q", at: original.at + 1, copyOf: original.id });
+ expect(f.inbox.view(OWNER).rows.map((row) => [row.id, row.origin?.threadId])).toEqual([["copy-q", "eli-pricing"]]);
+ });
+
it("drops a question the person wrote past, unless a running turn still waits on it", () => {
const live = new Set<string>();
const f = setup({ live: (_threadId, requestId) => live.has(requestId) });
diff --git a/server/host/place-inbox.ts b/server/host/place-inbox.ts
index ba13dc9af6..b20290d899 100644
--- a/server/host/place-inbox.ts
+++ b/server/host/place-inbox.ts
@@ -46,8 +46,9 @@ const lineSchema = z.object({
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>;
-/** An open card as the messages table holds it. */
-export interface OpenCardRow { threadId: string; id: string; at: number; card: string | null; fromBotId: string | null }
+/** An open card as the messages table holds it; `copyOf` names the original card when this one is an automation's copy of
+ * it in the run's Results chat. */
+export interface OpenCardRow { threadId: string; id: string; at: number; card: string | null; fromBotId: string | null; copyOf?: string | null }
/** Who is asking for the inbox: its owner (on a server with members, a member), a guest on someone's Cloud, or nobody it
* belongs to (a bot's shell on a Cloud, a local service). */
@@ -141,6 +142,7 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {
if (deps.viewer(auth).kind === "nobody") return [];
const rows = new Map<string, InboxRow>();
const passed = new Map<string, Set<string>>();
+ const copies = new Map<string, string>();
const overtaken = (threadId: string, messageId: string): boolean => {
let questions = passed.get(threadId);
if (!questions) passed.set(threadId, questions = overtakenQuestions(store.activePath(threadId), Boolean(store.groupByThread(threadId))));
@@ -156,7 +158,9 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {
const group = store.groupByThread(row.threadId);
const guest = deps.guestOf(row.threadId);
const handed = workingFor(row.threadId);
- rows.set(`${row.threadId}\u0000${card.requestId}`, {
+ const key = `${row.threadId}\u0000${card.requestId}`;
+ if (row.copyOf) copies.set(key, row.copyOf);
+ rows.set(key, {
id: row.id, kind: "ask", tab: "needs-you", from: { name: bot.name, botId: bot.id },
...(handed ? { workingFor: handed } : {}), ...(guest ? { askedBy: { name: deps.guestName(row.threadId) ?? "" } } : {}),
origin: { botId: bot.id, threadId: row.threadId, title: titleOf(bot.id, row.threadId), ...(group ? { groupId: group.id } : {}), messageId: row.id },
@@ -164,6 +168,7 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {
});
}
const listed = new Set([...rows.values()].map((row) => row.id));
+ for (const [key, original] of copies) if (listed.has(original)) rows.delete(key);
const viewer = deps.viewer(auth);
for (const wait of deps.waiting?.() ?? []) {
if (listed.has(wait.messageId) || !deps.sees(auth, wait.threadId)) continue;
diff --git a/server/message-db.test.ts b/server/message-db.test.ts
index b114db8f9a..e5879a8889 100644
--- a/server/message-db.test.ts
+++ b/server/message-db.test.ts
@@ -16,6 +16,7 @@ import { closeMessageDb,
latestMessageAts,
latestPersonLineAts,
optionCardsForTools,
+ openCardRows,
logNotification,
openRescueMessages,
LATEST_SAID_BY_BOT_SQL,
@@ -532,6 +533,13 @@ describe("start-up reads on their indexes", () => {
expect(optionCardsForTools(["your_computer", "ask_peer"]).map((row) => row.messageId)).toEqual(["second", "first", "other"]);
expect(optionCardsForTools([])).toEqual([]);
});
+
+ it("says which open card is an automation's copy of another one", () => {
+ const card = { title: "Allow?", subtitle: "", options: ["Approve", "Deny"], requestType: "question", requestId: "q" } as Message["card"];
+ insertMessage("run", { ...msg("asked", "", { at: 10, role: "bot" }), kind: "options", card });
+ insertMessage("results", { ...msg("copy", "", { at: 20, role: "bot" }), kind: "options", card, automation: { routineId: "r", runId: "run-1", needsYou: true, copyOf: "asked" } } as Message);
+ expect(openCardRows().map((row) => [row.threadId, row.id, row.copyOf])).toEqual([["run", "asked", null], ["results", "copy", "asked"]]);
+ });
});
describe("describeMissingFts5", () => {
diff --git a/server/message-db.ts b/server/message-db.ts
index a4ddb664ca..90d13e9c41 100644
--- a/server/message-db.ts
+++ b/server/message-db.ts
@@ -550,12 +550,12 @@ export function attentionRows(since: number): AttentionRow[] {
}
/** Every card still open, oldest first, whenever it was asked. */
-export function openCardRows(): Array<{ threadId: string; id: string; at: number; card: string | null; fromBotId: string | null }> {
+export function openCardRows(): Array<{ threadId: string; id: string; at: number; card: string | null; fromBotId: string | null; copyOf: string | null }> {
return db().prepare(
- `SELECT thread_id AS threadId, id, at, json_extract(json, '$.card') AS card, json_extract(json, '$.from.botId') AS fromBotId FROM messages INDEXED BY messages_work_at WHERE ${WORK_KINDS} AND kind = 'options' ` +
+ `SELECT thread_id AS threadId, id, at, json_extract(json, '$.card') AS card, json_extract(json, '$.from.botId') AS fromBotId, json_extract(json, '$.automation.copyOf') AS copyOf FROM messages INDEXED BY messages_work_at WHERE ${WORK_KINDS} AND kind = 'options' ` +
"AND json_extract(json, '$.card.requestId') IS NOT NULL AND json_extract(json, '$.card.answered') IS NULL AND coalesce(json_extract(json, '$.card.dismissed'), 0) = 0 " +
"AND coalesce(json_extract(json, '$.card.expired'), 0) = 0 AND json_extract(json, '$.helperIntro') IS NULL AND json_extract(json, '$.inbox') IS NULL ORDER BY at",
- ).all() as Array<{ threadId: string; id: string; at: number; card: string | null; fromBotId: string | null }>;
+ ).all() as Array<{ threadId: string; id: string; at: number; card: string | null; fromBotId: string | null; copyOf: string | null }>;
}
/** The conversations with a card asked since `since` that has not been answered, oldest first; the caller reads each card. */