Skip to content

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

fix: join an event only into a waiting run with the same conversation setting

AdityaUmale6d183dc62a7e
diff --git a/server/app-trigger-host.ts b/server/app-trigger-host.ts
index 9ca346a9a2..b46d56403e 100644
--- a/server/app-trigger-host.ts
+++ b/server/app-trigger-host.ts
@@ -77,7 +77,7 @@ export function createAppTriggerHost(deps: AppTriggerHostDeps) {
botState: deps.botState,
runs: {
enqueue: (input) => deps.routines().enqueueWebhook(input),
- merge: (triggerId, eventId, merge) => deps.routines().mergeIntoQueuedRun(triggerId, eventId, merge),
+ merge: (triggerId, eventId, sameConversation, merge) => deps.routines().mergeIntoQueuedRun(triggerId, eventId, sameConversation, merge),
receipt: (triggerId, eventId) => deps.routines().webhookRunReceipt(triggerId, eventId),
pending: (triggerId) => deps.routines().activeWebhookRunCount(triggerId),
cancelQueued: (triggerId, message) => deps.routines().cancelQueuedWebhook(triggerId, message),
diff --git a/server/app-triggers.test.ts b/server/app-triggers.test.ts
index d924389307..1ea2bdc5b7 100644
--- a/server/app-triggers.test.ts
+++ b/server/app-triggers.test.ts
@@ -54,7 +54,7 @@ function manager(overrides: Partial<AppTriggerManagerOptions> = {}) {
if (failure === "after-save") throw new Error("crashed after the run was saved");
return run;
},
- merge: (triggerId, eventId, merge) => routines.mergeIntoQueuedRun(triggerId, eventId, merge),
+ merge: (triggerId, eventId, sameConversation, merge) => routines.mergeIntoQueuedRun(triggerId, eventId, sameConversation, merge),
receipt: (triggerId, eventId) => routines.webhookRunReceipt(triggerId, eventId),
pending: (triggerId) => routines.activeWebhookRunCount(triggerId),
cancelQueued: (triggerId, message) => routines.cancelQueuedWebhook(triggerId, message),
diff --git a/server/app-triggers.ts b/server/app-triggers.ts
index db8750f317..01e684cb50 100644
--- a/server/app-triggers.ts
+++ b/server/app-triggers.ts
@@ -60,7 +60,7 @@ export interface AppTriggerManagerOptions {
botState: (botId: string) => "ready" | "busy" | "missing";
runs: {
enqueue(input: { webhookId: string; webhookName: string; prompt: string; botId: string; runOn: RoutineRunOn; deliveryId: string; receivedAt: number; eventSource: "app"; sameConversation?: boolean }): { id: string };
- merge(triggerId: string, eventId: string, merge: (prompt: string) => string | null): { id: string } | null;
+ merge(triggerId: string, eventId: string, sameConversation: boolean, merge: (prompt: string) => string | null): { id: string } | null;
receipt(triggerId: string, eventId: string): { id: string } | null;
pending(triggerId: string): number;
cancelQueued(triggerId: string, message: string): void;
@@ -513,7 +513,7 @@ export class AppTriggerManager {
continue;
}
const block = eventBlock(trigger, { id: event.id, receivedAt: event.receivedAt, test: event.kind === "test", data });
- const joined = this.options.runs.merge(trigger.id, event.id, (prompt) => mergeEventBlock(prompt, block));
+ const joined = this.options.runs.merge(trigger.id, event.id, trigger.sameConversation === true, (prompt) => mergeEventBlock(prompt, block));
let runId = joined?.id;
if (!runId) {
if (this.options.runs.pending(trigger.id) >= DEFAULT_MAX_PENDING_RUNS) {
diff --git a/server/routines.test.ts b/server/routines.test.ts
index 2e2e9141fb..477a21b1d4 100644
--- a/server/routines.test.ts
+++ b/server/routines.test.ts
@@ -4152,4 +4152,14 @@ describe("event automations that keep one conversation", () => {
expect(h.started.map((turn) => turn.threadId)).toEqual(["thread-1", "thread-2"]);
expect(h.manager.listRuns().every((run) => run.sameConversation === undefined)).toBe(true);
});
+
+ it("joins a burst only into a waiting run with the same conversation setting", () => {
+ const h = harness();
+ const queued = event(h, "d1", false);
+ const add = (prompt: string) => `${prompt}\n+event`;
+ // The option was turned on after the first event: the next one must not ride along in the old run.
+ expect(h.manager.mergeIntoQueuedRun("hook", "d2", true, add)).toBeNull();
+ expect(h.manager.mergeIntoQueuedRun("hook", "d3", false, add)).toEqual({ id: queued.id });
+ expect(h.manager.listRuns().find((run) => run.id === queued.id)?.prompt).toBe("Handle it\n+event");
+ });
});
diff --git a/server/routines.ts b/server/routines.ts
index 0c2035b451..3bf4f63f1f 100644
--- a/server/routines.ts
+++ b/server/routines.ts
@@ -773,12 +773,12 @@ export class RoutineManager {
}
/** Adds an outside event to its trigger's run that has not started yet, so a burst becomes one run. Null when there
- * is no such run or `merge` declines (the run is full). */
- mergeIntoQueuedRun(webhookId: string, deliveryId: string, merge: (prompt: string) => string | null): { id: string } | null {
+ * is no such run with the same conversation setting, or `merge` declines (the run is full). */
+ mergeIntoQueuedRun(webhookId: string, deliveryId: string, sameConversation: boolean, merge: (prompt: string) => string | null): { id: string } | null {
this.assertWritable();
const existing = this.webhookRunReceipt(webhookId, deliveryId);
if (existing) return existing;
- const run = this.runs.findLast((candidate) => candidate.webhookId === webhookId && candidate.status === "queued" && !candidate.threadId);
+ const run = this.runs.findLast((candidate) => candidate.webhookId === webhookId && candidate.status === "queued" && !candidate.threadId && (candidate.sameConversation === true) === sameConversation);
const merged = run?.prompt === undefined ? null : merge(run.prompt);
if (!run || merged === null) return null;
const before = run.prompt;