Skip to content

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

Webhooks and app events report into a chat of their own on every home

milind-soni5a403e1529b2
diff --git a/docs/verification/automation-reports.md b/docs/verification/automation-reports.md
index 8c1f833903..f6c6c752dc 100644
--- a/docs/verification/automation-reports.md
+++ b/docs/verification/automation-reports.md
@@ -31,9 +31,10 @@ Only a run working in a conversation of its own is told that only its last
reply leaves it (`NOTHING NEW`, `NEEDS INPUT:`). A "when my limit resets"
continuation works in the person's own chat and is not.
-On a Cloud home, every run of a webhook or app-event automation reports into
-one "<name> · Results" chat of its own, and a routine someone other than the
-owner wrote keeps its own results chat, so no outside words reach the owner's
+Every run of a webhook or app-event automation reports into one
+"<name> · Results" chat of its own, on every home, so outside events never pile
+into a chat a person works in. On a Cloud home, a routine someone other than the
+owner wrote keeps its own results chat too, so no outside words reach the owner's
other chats or a lent Mac.
## The concurrency rule
diff --git a/server/automation-destinations.test.ts b/server/automation-destinations.test.ts
index 4b59f20321..22c0e52b3a 100644
--- a/server/automation-destinations.test.ts
+++ b/server/automation-destinations.test.ts
@@ -4,7 +4,7 @@ import { createAutomationDestinations, type AutomationDestinationDeps } from "./
import { CLOUD_OUTSIDE_KEY } from "./cloud-sharing.ts";
import type { Routine } from "./routines.ts";
-function world(writer: string) {
+function world(writer: string, cloud = true, earlier: Array<{ webhookId: string; botId: string; resultsThreadId: string }> = []) {
const tasks = new Map([["person-chat", { threadId: "person-chat", title: "Person" }], ["hook-chat", { threadId: "hook-chat", title: "Hook · Results" }]]);
const opened: Array<[string, string]> = [];
const outside: string[] = [];
@@ -15,9 +15,9 @@ function world(writer: string) {
messagesFor: () => [],
taskByThread: (_botId: string, threadId: string) => tasks.get(threadId),
} as unknown as AutomationDestinationDeps["store"];
- const destinations = createAutomationDestinations({ store, mainThread: () => "main", runs: () => [],
- cloud: { owner: "owner", opener: () => writer, reportFromOthers: () => false, opened: (threadId, person) => { opened.push([threadId, person]); }, outside: (threadId) => { outside.push(threadId); } } });
- return { destinations, opened, outside };
+ const destinations = createAutomationDestinations({ store, mainThread: () => "main", runs: () => earlier,
+ cloud: cloud ? { owner: "owner", opener: () => writer, reportFromOthers: () => false, opened: (threadId, person) => { opened.push([threadId, person]); }, outside: (threadId) => { outside.push(threadId); } } : null });
+ return { destinations, opened, outside, tasks };
}
const routine = { id: "r", name: "Daily", botId: "bot", resultsThreadId: "person-chat" } as Routine;
@@ -45,3 +45,24 @@ describe("where an automation reports on a Cloud", () => {
expect(w.outside).toEqual(["made-1"]);
});
});
+
+describe("where an outside event reports on a desktop or self-hosted home", () => {
+ const event = { webhookId: "w", webhookName: "Hook", botId: "bot" };
+
+ it("opens a chat of its own instead of piling into the main chat", () => {
+ const w = world("owner", false);
+ expect(w.destinations.event(event)).toBe("made-1");
+ expect(w.tasks.get("made-1")).toMatchObject({ title: "Hook · Results" });
+ expect(w.outside).toEqual([]);
+ });
+
+ it("keeps using that chat for the webhook's later runs", () => {
+ const w = world("owner", false, [{ webhookId: "w", botId: "bot", resultsThreadId: "hook-chat" }]);
+ expect(w.destinations.event(event)).toBe("hook-chat");
+ });
+
+ it("moves off a person's chat that an older run reported into", () => {
+ const w = world("owner", false, [{ webhookId: "w", botId: "bot", resultsThreadId: "person-chat" }]);
+ expect(w.destinations.event(event)).toBe("made-1");
+ });
+});
diff --git a/server/automation-destinations.ts b/server/automation-destinations.ts
index 94fb2ebe79..5e663aacfd 100644
--- a/server/automation-destinations.ts
+++ b/server/automation-destinations.ts
@@ -1,6 +1,6 @@
-// Where an automation's runs report: the chat a routine chose, else the bot's main chat. On a Cloud home a chat the
-// owner writes in never gets words they did not choose, so a Mac they lend stays theirs there: a routine someone else
-// wrote, and every outside event, report into a chat of their own.
+// Where an automation's runs report: the chat a routine chose, else the bot's main chat; every outside event, into a
+// chat of its own. On a Cloud home a chat the owner writes in never gets words they did not choose, so a Mac they lend
+// stays theirs there: a routine someone else wrote reports into a chat of its own too.
import { CLOUD_OUTSIDE_KEY } from "./cloud-sharing.ts";
import type { Routine, RoutineRun } from "./routines.ts";
import { threadTitleFrom, type Message, type Store, type TaskRecord } from "./store.ts";
@@ -27,9 +27,9 @@ function legacyResultsThread(routineName: string, task: TaskRecord): boolean {
export function createAutomationDestinations(deps: AutomationDestinationDeps) {
const { store, cloud } = deps;
- const chatOfItsOwn = (botId: string, name: string, person: string) => {
+ const chatOfItsOwn = (botId: string, name: string, person?: string) => {
const threadId = store.createTask(botId, `${name} · Results`, false)?.threadId;
- if (threadId) cloud?.opened(threadId, person);
+ if (threadId && person) cloud?.opened(threadId, person);
return threadId;
};
return {
@@ -53,12 +53,12 @@ export function createAutomationDestinations(deps: AutomationDestinationDeps) {
event({ webhookId, webhookName, botId }: { webhookId: string; webhookName: string; botId: string }): string | undefined {
const bot = store.bot(botId);
if (!bot || bot.hidden) return undefined;
- if (!cloud) return deps.mainThread(botId);
const earlier = deps.runs().find((run) => run.webhookId === webhookId && run.botId === botId && run.resultsThreadId)?.resultsThreadId;
const kept = earlier ? store.taskByThread(botId, earlier) : undefined;
const lines = kept && !kept.routineRunId ? store.messagesFor(kept.threadId) : undefined;
- const threadId = kept && lines && (lines.length === 0 || lines.some(cloud.reportFromOthers)) ? kept.threadId : chatOfItsOwn(botId, webhookName, cloud.owner);
- if (threadId) cloud.outside(threadId);
+ const stillItsOwn = kept && lines && (cloud ? lines.length === 0 || lines.some(cloud.reportFromOthers) : legacyResultsThread(webhookName, kept));
+ const threadId = stillItsOwn ? kept.threadId : chatOfItsOwn(botId, webhookName, cloud?.owner);
+ if (threadId) cloud?.outside(threadId);
return threadId;
},
};
diff --git a/server/automation-reports.e2e.test.ts b/server/automation-reports.e2e.test.ts
index dd45604690..e365dcafe4 100644
--- a/server/automation-reports.e2e.test.ts
+++ b/server/automation-reports.e2e.test.ts
@@ -293,7 +293,7 @@ it("hands the chat's own session the newest report of each automation, however m
expect((await s.messages(s.main)).findLast((message) => message.role === "bot" && message.kind === "text")?.text).toBe("Sent.");
}, 120_000);
-it("reports an outside event's run into the main chat too, from a conversation of its own that stays out of the way", async () => {
+it("reports an outside event's run into a chat of its own, not the main chat", async () => {
const s = await server();
s.script([{ key: "lead", match: ["Draft a follow-up"], reply: "Drafted a follow-up for Ada.", repeat: true }]);
const { webhook, credential } = await s.api("POST", "/api/webhooks", { name: "Sales form leads", prompt: "Draft a follow-up for the lead.", botId: s.bot.id }, 201);
@@ -303,12 +303,20 @@ it("reports an outside event's run into the main chat too, from a conversation o
await s.settled(runId);
await s.reported(runId, "posted");
const done = await s.run(runId);
- expect(done).toMatchObject({ webhookId: webhook.id, resultsThreadId: s.main, triggerSource: "webhook" });
- expect(await s.lines()).toEqual([expect.objectContaining({ text: "Drafted a follow-up for Ada.", automation: expect.objectContaining({ trigger: "webhook", name: "Sales form leads" }) })]);
+ expect(done).toMatchObject({ webhookId: webhook.id, triggerSource: "webhook" });
+ expect(done.resultsThreadId).not.toBe(s.main);
+ expect(await s.lines()).toEqual([]);
+ expect(await s.lines(done.resultsThreadId)).toEqual([expect.objectContaining({ text: "Drafted a follow-up for Ada.", automation: expect.objectContaining({ trigger: "webhook", name: "Sales form leads" }) })]);
expect(s.toldOnlyReplyLeaves("lead")).toBe(true);
const saved = await s.savedBot();
expect(saved.threadId).toBe(s.main);
+ expect(saved.tasks.find((task: any) => task.threadId === done.resultsThreadId)).toMatchObject({ title: "Sales form leads · Results" });
expect(saved.tasks.find((task: any) => task.threadId === done.threadId)).toMatchObject({ routineRunId: runId });
+ const again = await fetch(credential.url, { method: "POST", headers: { "content-type": "application/json", "idempotency-key": "lead-2" }, body: JSON.stringify({ lead: "Bo" }) });
+ const { runId: second } = await again.json() as any;
+ await s.settled(second);
+ await s.reported(second, "posted");
+ expect(await s.run(second)).toMatchObject({ resultsThreadId: done.resultsThreadId });
expect(existsSync(join(s.dataDir, "room-plan.json"))).toBe(true);
}, 120_000);