Skip to content

Automate browser based workflows with AI

Show the billing error and a Go to Billing link when Discover hits a 402 (SKY-15589) (#8434)

Marc Kelechavac25fc3fc5657
diff --git a/skyvern-frontend/src/routes/tasks/create/PromptBox.test.tsx b/skyvern-frontend/src/routes/tasks/create/PromptBox.test.tsx
index f33a10f1d..c8c177e53 100644
--- a/skyvern-frontend/src/routes/tasks/create/PromptBox.test.tsx
+++ b/skyvern-frontend/src/routes/tasks/create/PromptBox.test.tsx
@@ -9,6 +9,7 @@ import {
import {
act,
createRef,
+ isValidElement,
type ButtonHTMLAttributes,
type InputHTMLAttributes,
type Ref,
@@ -18,7 +19,9 @@ import {
} from "react";
import { afterEach, describe, expect, test, vi } from "vitest";
+import { ToastAction } from "@/components/ui/toast";
import { toast } from "@/components/ui/use-toast";
+import { Link } from "react-router-dom";
import { PromptBox, type PromptBoxHandle } from "./PromptBox";
@@ -367,4 +370,71 @@ describe("PromptBox", () => {
sessionStorage.getItem("skyvern.discoverCopilotHandoff:wpid_studio"),
).toBe("Build this in studio");
});
+
+ // SKY-15589: the axios message ("Request failed with status code 402") hid
+ // the server's billing guidance behind a generic toast.
+ test.each([
+ { handoff: false, status: 402 },
+ { handoff: true, status: 402 },
+ { handoff: false, status: 422 },
+ { handoff: true, status: 422 },
+ ])(
+ "surfaces the server detail on a $status (handoff=$handoff)",
+ async ({ handoff, status }) => {
+ const detail =
+ status === 402
+ ? "Credits exhausted. Upgrade your plan in Billing."
+ : "Prompt must be at least 10 characters.";
+ mockPost.mockRejectedValue({
+ isAxiosError: true,
+ message: `Request failed with status code ${status}`,
+ response: { status, data: { detail } },
+ });
+
+ renderPromptBox(handoff);
+ fireEvent.change(screen.getByPlaceholderText("Enter your prompt..."), {
+ target: { value: "Visit the docs" },
+ });
+ fireEvent.click(screen.getByLabelText("submit-prompt"));
+
+ await waitFor(() =>
+ expect(vi.mocked(toast)).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ description: detail,
+ }),
+ ),
+ );
+ const args = vi.mocked(toast).mock.calls[0]![0];
+ if (status === 402) {
+ expect(args.title).toBe("Not enough credits");
+ const action = args.action;
+ if (
+ !isValidElement<{
+ altText: string;
+ asChild?: boolean;
+ children: unknown;
+ }>(action)
+ ) {
+ throw new Error("402 toast has no action element");
+ }
+ expect(action.type).toBe(ToastAction);
+ expect(action.props.altText).toBe("Go to Billing");
+ expect(action.props.asChild).toBe(true);
+ const link = action.props.children;
+ if (!isValidElement<{ to: string; children: unknown }>(link)) {
+ throw new Error("402 toast action has no link child");
+ }
+ expect(link.type).toBe(Link);
+ expect(link.props.to).toBe("/billing");
+ expect(link.props.children).toBe("Go to Billing");
+ } else {
+ expect(args.title).toBe(
+ handoff ? "Error creating agent" : "Error creating agent from prompt",
+ );
+ expect(args.action).toBeUndefined();
+ }
+ expect(mockNavigate).not.toHaveBeenCalled();
+ },
+ );
});
diff --git a/skyvern-frontend/src/routes/tasks/create/PromptBox.tsx b/skyvern-frontend/src/routes/tasks/create/PromptBox.tsx
index a2aafa151..ef9ba0265 100644
--- a/skyvern-frontend/src/routes/tasks/create/PromptBox.tsx
+++ b/skyvern-frontend/src/routes/tasks/create/PromptBox.tsx
@@ -1,4 +1,5 @@
import { getClient } from "@/api/AxiosClient";
+import { isPaymentRequiredError } from "@/api/paymentRequired";
import { Createv2TaskRequest, ProxyLocation } from "@/api/types";
import { stringify as convertToYAML } from "yaml";
import { WorkflowCreateYAMLRequest } from "@/routes/workflows/types/workflowYamlTypes";
@@ -14,6 +15,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { KeyValueInput } from "@/components/KeyValueInput";
import { Switch } from "@/components/ui/switch";
+import { ToastAction } from "@/components/ui/toast";
import { toast } from "@/components/ui/use-toast";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { WorkflowApiResponse } from "@/routes/workflows/types/workflowTypes";
@@ -34,7 +36,7 @@ import {
useRef,
useState,
} from "react";
-import { useNavigate } from "react-router-dom";
+import { Link, useNavigate } from "react-router-dom";
import {
generatePhoneNumber,
generateUniqueEmail,
@@ -48,6 +50,7 @@ import { useAutoplayStore } from "@/store/useAutoplayStore";
import { TestWebhookDialog } from "@/components/TestWebhookDialog";
import { ImprovePrompt } from "@/components/ImprovePrompt";
import { SpeechInputButton } from "@/components/SpeechInputButton";
+import { getErrorDetail } from "@/util/getErrorDetail";
import { cn } from "@/util/utils";
import { useSpeechToTextField } from "@/hooks/useSpeechToTextField";
import { useWorkflowStudioEnabled } from "@/hooks/useWorkflowStudioEnabled";
@@ -181,6 +184,23 @@ function describeResponseEnvelope(response: AxiosResponse<unknown>): string {
return `status=${response.status} content_type=${contentType} body_length=${bodyLength} parsed_as_json=${parsedAsJson}`;
}
+function showCreateErrorToast(title: string, error: unknown) {
+ if (isPaymentRequiredError(error)) {
+ toast({
+ variant: "destructive",
+ title: "Not enough credits",
+ description: getErrorDetail(error),
+ action: (
+ <ToastAction altText="Go to Billing" asChild>
+ <Link to="/billing">Go to Billing</Link>
+ </ToastAction>
+ ),
+ });
+ return;
+ }
+ toast({ variant: "destructive", title, description: getErrorDetail(error) });
+}
+
function PromptBoxImpl(
{ enableCopilotHandoff = false }: PromptBoxProps,
ref: ForwardedRef<PromptBoxHandle>,
@@ -308,11 +328,7 @@ function PromptBoxImpl(
);
},
onError: (error: Error) => {
- toast({
- variant: "destructive",
- title: "Error creating agent from prompt",
- description: error.message,
- });
+ showCreateErrorToast("Error creating agent from prompt", error);
},
onSettled: () => {
submitInFlightRef.current = false;
@@ -364,11 +380,7 @@ function PromptBoxImpl(
);
},
onError: (error: AxiosError) => {
- toast({
- variant: "destructive",
- title: "Error creating agent",
- description: error.message,
- });
+ showCreateErrorToast("Error creating agent", error);
},
onSettled: () => {
submitInFlightRef.current = false;