Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(frontend/library): Add toast on agent execution request failure (#9689)

Zamil Majdy33299070d386
diff --git a/autogpt_platform/frontend/src/app/profile/(user)/credits/page.tsx b/autogpt_platform/frontend/src/app/profile/(user)/credits/page.tsx
index 7b76b70951..b3852c2465 100644
--- a/autogpt_platform/frontend/src/app/profile/(user)/credits/page.tsx
+++ b/autogpt_platform/frontend/src/app/profile/(user)/credits/page.tsx
@@ -4,7 +4,7 @@ import { Button } from "@/components/ui/button";
import useCredits from "@/hooks/useCredits";
import { useBackendAPI } from "@/lib/autogpt-server-api/context";
import { useSearchParams, useRouter } from "next/navigation";
-import { useToast } from "@/components/ui/use-toast";
+import { useToast, useToastOnFail } from "@/components/ui/use-toast";
import { RefundModal } from "./RefundModal";
import { CreditTransaction } from "@/lib/autogpt-server-api";
@@ -38,20 +38,7 @@ export default function CreditsPage() {
const searchParams = useSearchParams();
const topupStatus = searchParams.get("topup") as "success" | "cancel" | null;
const { toast } = useToast();
-
- const toastOnFail = useCallback(
- (action: string, fn: () => Promise<any>) => {
- return fn().catch((e) => {
- toast({
- title: `Unable to ${action}`,
- description: e.message,
- variant: "destructive",
- duration: 10000,
- });
- });
- },
- [toast],
- );
+ const toastOnFail = useToastOnFail();
const [isRefundModalOpen, setIsRefundModalOpen] = useState(false);
const [topUpTransactions, setTopUpTransactions] = useState<
@@ -63,42 +50,44 @@ export default function CreditsPage() {
setIsRefundModalOpen(true);
});
};
- const refundCredits = async (transaction_key: string, reason: string) =>
- toastOnFail("refund transaction", async () => {
- const amount = await refundTopUp(transaction_key, reason);
- if (amount > 0) {
- toast({
- title: "Refund approved! 🎉",
- description: `Your refund has been automatically processed. Based on your remaining balance, the amount of ${formatCredits(amount)} will be credited to your account.`,
- });
- } else {
- toast({
- title: "Refund Request Received",
- description:
- "We have received your refund request. A member of our team will review it and reach out via email shortly.",
- });
- }
- });
+ const refundCredits = (transaction_key: string, reason: string) =>
+ refundTopUp(transaction_key, reason)
+ .then((amount) => {
+ if (amount > 0) {
+ toast({
+ title: "Refund approved! 🎉",
+ description: `Your refund has been automatically processed. Based on your remaining balance, the amount of ${formatCredits(amount)} will be credited to your account.`,
+ });
+ } else {
+ toast({
+ title: "Refund Request Received",
+ description:
+ "We have received your refund request. A member of our team will review it and reach out via email shortly.",
+ });
+ }
+ })
+ .catch(toastOnFail("refund transaction"));
useEffect(() => {
if (api && topupStatus === "success") {
- toastOnFail("fulfill checkout", () => api.fulfillCheckout());
+ api.fulfillCheckout().catch(toastOnFail("fulfill checkout"));
}
}, [api, topupStatus, toastOnFail]);
- const openBillingPortal = async () => {
- toastOnFail("open billing portal", async () => {
- const portal = await api.getUserPaymentPortalLink();
- router.push(portal.url);
- });
- };
+ const openBillingPortal = () =>
+ api
+ .getUserPaymentPortalLink()
+ .then((portal) => {
+ router.push(portal.url);
+ })
+ .catch(toastOnFail("open billing portal"));
const submitTopUp = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const form = e.currentTarget;
const amount =
parseInt(new FormData(form).get("topUpAmount") as string) * 100;
- toastOnFail("request top-up", () => requestTopUp(amount));
+ requestTopUp(amount).catch(toastOnFail("request top-up"));
};
const submitAutoTopUpConfig = (e: React.FormEvent<HTMLFormElement>) => {
@@ -107,11 +96,11 @@ export default function CreditsPage() {
const formData = new FormData(form);
const amount = parseInt(formData.get("topUpAmount") as string) * 100;
const threshold = parseInt(formData.get("threshold") as string) * 100;
- toastOnFail("update auto top-up config", () =>
- updateAutoTopUpConfig(amount, threshold).then(() => {
+ updateAutoTopUpConfig(amount, threshold)
+ .then(() => {
toast({ title: "Auto top-up config updated! 🎉" });
- }),
- );
+ })
+ .catch(toastOnFail("update auto top-up config"));
};
return (
diff --git a/autogpt_platform/frontend/src/components/agents/agent-run-details-view.tsx b/autogpt_platform/frontend/src/components/agents/agent-run-details-view.tsx
index fae3632ba8..e19d744194 100644
--- a/autogpt_platform/frontend/src/components/agents/agent-run-details-view.tsx
+++ b/autogpt_platform/frontend/src/components/agents/agent-run-details-view.tsx
@@ -12,6 +12,7 @@ import {
import type { ButtonAction } from "@/components/agptui/types";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { IconRefresh, IconSquare } from "@/components/ui/icons";
+import { useToastOnFail } from "@/components/ui/use-toast";
import { Button } from "@/components/agptui/Button";
import { Input } from "@/components/ui/input";
@@ -38,6 +39,8 @@ export default function AgentRunDetailsView({
[run],
);
+ const toastOnFail = useToastOnFail();
+
const infoStats: { label: string; value: React.ReactNode }[] = useMemo(() => {
if (!run) return [];
return [
@@ -79,14 +82,16 @@ export default function AgentRunDetailsView({
const runAgain = useCallback(
() =>
agentRunInputs &&
- api.executeGraph(
- graph.id,
- graph.version,
- Object.fromEntries(
- Object.entries(agentRunInputs).map(([k, v]) => [k, v.value]),
- ),
- ),
- [api, graph, agentRunInputs],
+ api
+ .executeGraph(
+ graph.id,
+ graph.version,
+ Object.fromEntries(
+ Object.entries(agentRunInputs).map(([k, v]) => [k, v.value]),
+ ),
+ )
+ .catch(toastOnFail("execute agent")),
+ [api, graph, agentRunInputs, toastOnFail],
);
const stopRun = useCallback(
diff --git a/autogpt_platform/frontend/src/components/agents/agent-run-draft-view.tsx b/autogpt_platform/frontend/src/components/agents/agent-run-draft-view.tsx
index ac77bbf42a..eadb84cd87 100644
--- a/autogpt_platform/frontend/src/components/agents/agent-run-draft-view.tsx
+++ b/autogpt_platform/frontend/src/components/agents/agent-run-draft-view.tsx
@@ -7,6 +7,7 @@ import { GraphExecutionID, GraphMeta } from "@/lib/autogpt-server-api";
import type { ButtonAction } from "@/components/agptui/types";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { LocalValuedInput } from "@/components/ui/input";
+import { useToastOnFail } from "@/components/ui/use-toast";
import { Pencil2Icon } from "@radix-ui/react-icons";
import { Textarea } from "@/components/ui/textarea";
import { IconPlay } from "@/components/ui/icons";
@@ -28,6 +29,7 @@ export default function AgentRunDraftView({
agentActions: ButtonAction[];
}): React.ReactNode {
const api = useBackendAPI();
+ const toastOnFail = useToastOnFail();
const agentInputs = graph.input_schema.properties;
const [inputValues, setInputValues] = useState<Record<string, any>>({});
@@ -57,8 +59,9 @@ export default function AgentRunDraftView({
() =>
api
.executeGraph(graph.id, graph.version, inputValues)
- .then((newRun) => onRun(newRun.graph_exec_id)),
- [api, graph, inputValues, onRun],
+ .then((newRun) => onRun(newRun.graph_exec_id))
+ .catch(toastOnFail("execute agent")),
+ [api, graph, inputValues, onRun, toastOnFail],
);
const runActions: ButtonAction[] = useMemo(
diff --git a/autogpt_platform/frontend/src/components/agents/agent-schedule-details-view.tsx b/autogpt_platform/frontend/src/components/agents/agent-schedule-details-view.tsx
index 520188fad4..5cc783d9ed 100644
--- a/autogpt_platform/frontend/src/components/agents/agent-schedule-details-view.tsx
+++ b/autogpt_platform/frontend/src/components/agents/agent-schedule-details-view.tsx
@@ -11,6 +11,7 @@ import { useBackendAPI } from "@/lib/autogpt-server-api/context";
import type { ButtonAction } from "@/components/agptui/types";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { AgentRunStatus } from "@/components/agents/agent-run-status-chip";
+import { useToastOnFail } from "@/components/ui/use-toast";
import { Button } from "@/components/agptui/Button";
import { Input } from "@/components/ui/input";
@@ -29,6 +30,8 @@ export default function AgentScheduleDetailsView({
const selectedRunStatus: AgentRunStatus = "scheduled";
+ const toastOnFail = useToastOnFail();
+
const infoStats: { label: string; value: React.ReactNode }[] = useMemo(() => {
return [
{
@@ -67,8 +70,9 @@ export default function AgentScheduleDetailsView({
() =>
api
.executeGraph(graph.id, graph.version, schedule.input_data)
- .then((run) => onForcedRun(run.graph_exec_id)),
- [api, graph, schedule, onForcedRun],
+ .then((run) => onForcedRun(run.graph_exec_id))
+ .catch(toastOnFail("execute agent")),
+ [api, graph, schedule, onForcedRun, toastOnFail],
);
const runActions: { label: string; callback: () => void }[] = useMemo(
diff --git a/autogpt_platform/frontend/src/components/ui/use-toast.tsx b/autogpt_platform/frontend/src/components/ui/use-toast.tsx
index 6555e795c1..6135989113 100644
--- a/autogpt_platform/frontend/src/components/ui/use-toast.tsx
+++ b/autogpt_platform/frontend/src/components/ui/use-toast.tsx
@@ -188,4 +188,26 @@ function useToast() {
};
}
-export { useToast, toast };
+interface ToastOnFailOptions {
+ rethrow?: boolean;
+}
+
+function useToastOnFail() {
+ return React.useCallback(
+ (action: string, { rethrow = false }: ToastOnFailOptions = {}) =>
+ (error: any) => {
+ toast({
+ title: `Unable to ${action}`,
+ description: (error as Error)?.message ?? "Something went wrong",
+ variant: "destructive",
+ duration: 10000,
+ });
+ if (rethrow) {
+ throw error;
+ }
+ },
+ [],
+ );
+}
+
+export { useToast, toast, useToastOnFail };