Skip to content

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

fix(automations): let a run that waits on the person be cancelled beside Answer

AdityaUmalecfe06a9c9a5d
diff --git a/src/components/automations/actions.ts b/src/components/automations/actions.ts
index f5032ad369..8ae0869fe6 100644
--- a/src/components/automations/actions.ts
+++ b/src/components/automations/actions.ts
@@ -83,5 +83,11 @@ export function useAutomationActions() {
if (bot && run?.threadId) openBotThread(dispatch, bot, run.threadId, showThreads);
};
- return { busy, error, errorFor, setError, copied, toggle, runNow, remove, copyAddress, openRun };
+ /** Stops a run that waits on the person, for when the question it waits on was settled somewhere else. */
+ const cancelRun = (automation: Automation, run: RoutineRun) => attempt(`cancel:${automation.id}`, async () => {
+ const response = await api(`/api/routine-runs/${run.id}/cancel`, { method: "POST" });
+ if (response.run) dispatch({ type: "routineRunPatched", run: response.run });
+ });
+
+ return { busy, error, errorFor, setError, copied, toggle, runNow, remove, copyAddress, openRun, cancelRun };
}
diff --git a/src/components/automations/automations.test.ts b/src/components/automations/automations.test.ts
index cf45fb2706..71b7d0f745 100644
--- a/src/components/automations/automations.test.ts
+++ b/src/components/automations/automations.test.ts
@@ -332,3 +332,31 @@ describe("the secret, under More", () => {
expect(container.innerHTML).not.toContain(secret);
});
});
+
+describe("a run that waits on the person", () => {
+ const routine = { id: "routine-1", name: "Morning summary", prompt: "Send me a summary of my inbox.", target: "bot", botId: "bot-a", runOn: "maus", enabled: true,
+ schedule: { type: "daily", time: "09:00", weekdays: [0, 1, 2, 3, 4, 5, 6] }, durationMinutes: 30, nextRunAt: now + 3_600_000, createdAt: now, updatedAt: now };
+ const run = (status: string) => ({ id: "run-1", routineId: "routine-1", botId: "bot-a", status, scheduledFor: now - 60_000, startedAt: now - 60_000, createdAt: now - 60_000, threadId: "exec-1" });
+ const panel = (status: string) => {
+ const [automation] = automationsFrom({ routines: [routine] as never, webhooks: [], runs: [run(status)] as never });
+ return mount(createElement(AutomationPanel, { automation: automation!, sentence: "s", onClose: vi.fn(), onEdit: vi.fn(), onDeleted: vi.fn() }));
+ };
+ const cancel = (container: HTMLElement) => container.querySelector<HTMLButtonElement>("[data-automation-cancel-run]");
+
+ it("can be cancelled beside Answer, when its question was settled somewhere else", async () => {
+ const cancelled = { ...run("cancelled"), finishedAt: now };
+ fixture.api.mockResolvedValue({ run: cancelled });
+ const container = panel("waiting");
+ expect(button(container, "Answer")).toBeTruthy();
+ expect(cancel(container)!.textContent).toBe("Cancel run");
+ flushSync(() => cancel(container)!.click());
+ await vi.waitFor(() => expect(fixture.api).toHaveBeenCalledWith("/api/routine-runs/run-1/cancel", { method: "POST" }));
+ await vi.waitFor(() => expect(fixture.dispatch).toHaveBeenCalledWith({ type: "routineRunPatched", run: cancelled }));
+ });
+
+ it("offers no Cancel run when nothing waits", () => {
+ expect(cancel(panel("failed"))).toBeNull();
+ root?.unmount(); root = null; document.body.innerHTML = "";
+ expect(cancel(panel("completed"))).toBeNull();
+ });
+});
diff --git a/src/components/automations/parts.tsx b/src/components/automations/parts.tsx
index 823d4f76b7..f09365dea4 100644
--- a/src/components/automations/parts.tsx
+++ b/src/components/automations/parts.tsx
@@ -13,7 +13,7 @@ export function StatusMark({ status, className }: { status: AutomationStatus; cl
return <span aria-hidden="true" className={cn("size-2.5 shrink-0 rounded-full", status.state === "working" ? "bg-success" : "bg-ink-tertiary/60", className)} />;
}
-/** The one button that fixes what "Needs you" names. */
+/** The one button that fixes what "Needs you" names; a run that waits on the person can also be cancelled. */
export function FixButton({ automation, status, actions, onEdit, className }: {
automation: Automation;
status: AutomationStatus;
@@ -24,6 +24,7 @@ export function FixButton({ automation, status, actions, onEdit, className }: {
const { dispatch } = useStore();
if (status.state !== "needs-you" || !status.fix) return null;
const fix = status.fix;
+ const waiting = fix === "answer" ? automation.runs.find((run) => run.status === "waiting") : undefined;
const onClick = () => {
if (fix === "answer" || fix === "sign-in") actions.openRun(automation, automation.runs.find((run) => run.status !== "queued"));
else if (fix === "usage") dispatch({ type: "toggleAppSettings", open: true, section: "usage" });
@@ -32,8 +33,15 @@ export function FixButton({ automation, status, actions, onEdit, className }: {
else void actions.runNow(automation);
};
return (
- <button type="button" onClick={onClick} disabled={Boolean(actions.busy)} className={cn("shrink-0 rounded-lg bg-warning/15 px-2.5 py-1 text-[12px] font-medium text-warning hover:bg-warning/25 disabled:opacity-50", className)}>
- {t(AUTOMATION_FIX_LABEL[fix])}
- </button>
+ <>
+ <button type="button" onClick={onClick} disabled={Boolean(actions.busy)} className={cn("shrink-0 rounded-lg bg-warning/15 px-2.5 py-1 text-[12px] font-medium text-warning hover:bg-warning/25 disabled:opacity-50", className)}>
+ {t(AUTOMATION_FIX_LABEL[fix])}
+ </button>
+ {waiting && (
+ <button type="button" data-automation-cancel-run onClick={() => void actions.cancelRun(automation, waiting)} disabled={Boolean(actions.busy)} className={cn("shrink-0 rounded-lg px-2.5 py-1 text-[12px] text-ink-secondary hover:bg-raised hover:text-ink disabled:opacity-50", className)}>
+ {t("routines.drawer.cancelRun")}
+ </button>
+ )}
+ </>
);
}