Skip to content

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

Show the server's refusal reason on the burst card

AdityaUmale16d23a4eb9c8
diff --git a/src/components/BurstCard.refusal.dom.test.ts b/src/components/BurstCard.refusal.dom.test.ts
new file mode 100644
index 0000000000..6fe4d0f171
--- /dev/null
+++ b/src/components/BurstCard.refusal.dom.test.ts
@@ -0,0 +1,50 @@
+// @vitest-environment happy-dom
+import { act, createElement } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { setLocale } from "@/lib/i18n";
+import type { BurstCardData } from "../../shared/burst";
+
+const sent = vi.hoisted(() => ({ failure: null as unknown }));
+vi.mock("@/state/store", async () => {
+ const { ApiError } = await import("@/state/api");
+ return { ApiError, api: vi.fn(async () => { throw sent.failure; }), openThread: vi.fn(), useStore: () => ({ state: {}, dispatch: vi.fn() }) };
+});
+vi.mock("@/lib/thread-preferences", () => ({ useShowThreads: () => true }));
+
+const card: BurstCardData = {
+ version: 1, burstId: "b1", botId: "bot", task: "Research these companies", phase: "preview", total: 2, requested: 2, parallel: 2, modelName: "Fast 1",
+ estimateMinutes: 1, counts: { queued: 2, running: 0, done: 0, failed: 0, cancelled: 0 }, sample: ["Acme", "Globex"],
+};
+
+let container: HTMLDivElement, root: Root;
+beforeEach(() => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ setLocale("en");
+ container = document.createElement("div"); document.body.append(container); root = createRoot(container);
+});
+afterEach(async () => { await act(async () => root.unmount()); container.remove(); vi.unstubAllGlobals(); });
+
+async function pressRun(failure: unknown): Promise<string> {
+ sent.failure = failure;
+ const { BurstCard } = await import("./BurstCard");
+ await act(async () => { root.render(createElement(BurstCard, { card })); });
+ const run = [...container.querySelectorAll("button")].find((button) => button.textContent === "Run")!;
+ await act(async () => { run.click(); });
+ return container.textContent ?? "";
+}
+
+describe("a burst card press the server refuses", () => {
+ it("says why when the server names the reason, such as only the Cloud's owner may answer", async () => {
+ const { ApiError } = await import("@/state/api");
+ const body = { error: "refused", errorKey: "serverError.card.cloudOwnerOnly" };
+ const text = await pressRun(new ApiError("Only the owner of this Cloud can answer this card. Chats started by other people always ask the owner first, whatever the bot's access level.", 403, body));
+ expect(text).toContain("Only the owner of this Cloud can answer this card.");
+ expect(text).not.toContain("That didn't work. Try again in a moment.");
+ });
+
+ it("keeps the general try-again line when the server gives no reason", async () => {
+ const text = await pressRun(new TypeError("Failed to fetch"));
+ expect(text).toContain("That didn't work. Try again in a moment.");
+ });
+});
diff --git a/src/components/BurstCard.tsx b/src/components/BurstCard.tsx
index 1caed8beb0..860e127cd6 100644
--- a/src/components/BurstCard.tsx
+++ b/src/components/BurstCard.tsx
@@ -3,7 +3,8 @@ import { useEffect, useState } from "react";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
-import { api, openThread, useStore } from "@/state/store";
+import { serverErrorText } from "@/lib/server-error";
+import { api, ApiError, openThread, useStore } from "@/state/store";
import { useShowThreads } from "@/lib/thread-preferences";
import type { LocaleKey } from "@/locales";
import type { BurstCardData, BurstChildRow, BurstChildStatus, BurstView } from "../../shared/burst";
@@ -52,7 +53,7 @@ export function BurstCard({ card }: { card: BurstCardData }) {
const [open, setOpen] = useState(false);
const [view, setView] = useState<BurstView | null>(null);
const [busy, setBusy] = useState(false);
- const [error, setError] = useState(false);
+ const [error, setError] = useState<string>();
const moved = `${card.phase}:${card.counts.done}:${card.counts.running}:${card.counts.failed}:${card.counts.cancelled}`;
const live = open || editing;
@@ -65,12 +66,12 @@ export function BurstCard({ card }: { card: BurstCardData }) {
async function press(action: "run" | "cancel" | "retry" | "edit", body?: unknown): Promise<boolean> {
setBusy(true);
- setError(false);
+ setError(undefined);
try {
await api(`/api/bursts/${card.burstId}/${action}`, { method: "POST", ...(body === undefined ? {} : { body: JSON.stringify(body) }) });
return true;
- } catch {
- setError(true);
+ } catch (failure) {
+ setError(serverErrorText(failure instanceof ApiError ? failure.body : undefined, t("burst.error.action")));
return false;
} finally {
setBusy(false);
@@ -109,14 +110,14 @@ export function BurstCard({ card }: { card: BurstCardData }) {
)}
</>
)}
- {error && <p className="text-[12px] text-danger">{t("burst.error.action")}</p>}
+ {error && <p className="text-[12px] text-danger">{error}</p>}
{editing && view && <BurstEditor view={view} busy={busy} onBack={() => setEditing(false)} onSave={(plan) => { void press("edit", plan).then((ok) => { if (ok) setEditing(false); }); }} />}
{!editing && (
<div className="flex flex-wrap items-center gap-1.5 pt-1.5">
{preview ? (
<>
<button type="button" disabled={busy} onClick={() => { void press("run"); }} className={PRIMARY}>{t("burst.run")}</button>
- <button type="button" disabled={busy} onClick={() => { setError(false); setEditing(true); }} className={BUTTON}>{t("burst.edit")}</button>
+ <button type="button" disabled={busy} onClick={() => { setError(undefined); setEditing(true); }} className={BUTTON}>{t("burst.edit")}</button>
<button type="button" disabled={busy} onClick={() => { void press("cancel"); }} className={BUTTON}>{t("burst.cancel")}</button>
</>
) : (