Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Show human-in-the-loop approve/reject buttons in both run views (SKY-12802) (#7652)
Celal Zamanoğlu495163fe6451
diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunHumanInteraction.test.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunHumanInteraction.test.tsxnew file mode 100644index 000000000..5a665da2b--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunHumanInteraction.test.tsx@@ -0,0 +1,121 @@+// @vitest-environment jsdom++vi.mock("@/api/AxiosClient", () => ({ getClient: vi.fn() }));+vi.mock("@/hooks/useCredentialGetter", () => ({+ useCredentialGetter: () => null,+}));+vi.mock("@/components/ui/use-toast", () => ({ toast: vi.fn() }));++const useWorkflowRunWithWorkflowQueryMock = vi.fn();+vi.mock("../hooks/useWorkflowRunWithWorkflowQuery", () => ({+ useWorkflowRunWithWorkflowQuery: (options: unknown) =>+ useWorkflowRunWithWorkflowQueryMock(options),+}));++import { cleanup, render, screen } from "@testing-library/react";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import { Status } from "@/api/types";+import type { WorkflowRunBlock } from "../types/workflowRunTypes";+import { WorkflowRunHumanInteraction } from "./WorkflowRunHumanInteraction";++function buildBlock(+ overrides: Partial<WorkflowRunBlock> = {},+): WorkflowRunBlock {+ return {+ workflow_run_block_id: "wrb_1",+ workflow_run_id: "wr_1",+ block_type: "human_interaction",+ // running = this block is the one currently awaiting interaction+ status: Status.Running,+ instructions: null,+ positive_descriptor: null,+ negative_descriptor: null,+ ...overrides,+ } as unknown as WorkflowRunBlock;+}++function renderInteraction(block: WorkflowRunBlock) {+ return render(+ <QueryClientProvider client={new QueryClient()}>+ <WorkflowRunHumanInteraction workflowRunBlock={block} />+ </QueryClientProvider>,+ );+}++describe("WorkflowRunHumanInteraction", () => {+ beforeEach(() => {+ useWorkflowRunWithWorkflowQueryMock.mockClear();+ useWorkflowRunWithWorkflowQueryMock.mockReturnValue({+ data: { workflow_run_id: "wr_1", status: Status.Paused },+ });+ });+ afterEach(cleanup);++ it("falls back to Approve/Reject when descriptors are empty", () => {+ renderInteraction(+ buildBlock({ positive_descriptor: null, negative_descriptor: "" }),+ );+ expect(screen.getByRole("button", { name: "Approve" })).toBeTruthy();+ expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();+ });++ it("uses custom descriptors when present", () => {+ renderInteraction(+ buildBlock({ positive_descriptor: "Yes", negative_descriptor: "No" }),+ );+ expect(screen.getByRole("button", { name: "Yes" })).toBeTruthy();+ expect(screen.getByRole("button", { name: "No" })).toBeTruthy();+ expect(screen.queryByRole("button", { name: "Approve" })).toBeNull();+ });++ it("resolves the run from the block id, not a route param", () => {+ useWorkflowRunWithWorkflowQueryMock.mockReturnValue({+ data: { workflow_run_id: "wr_studio", status: Status.Paused },+ });+ renderInteraction(+ buildBlock({ workflow_run_id: "wr_studio", status: Status.Running }),+ );+ expect(useWorkflowRunWithWorkflowQueryMock).toHaveBeenCalledWith({+ workflowRunId: "wr_studio",+ });+ expect(screen.getByRole("button", { name: "Approve" })).toBeTruthy();+ });++ it("renders nothing when the resolved run is not this block's run", () => {+ // keepPreviousData can briefly surface the prior (paused) run while switching;+ // the buttons must not act on a run that isn't this block's.+ useWorkflowRunWithWorkflowQueryMock.mockReturnValue({+ data: { workflow_run_id: "wr_other", status: Status.Paused },+ });+ const { container } = renderInteraction(+ buildBlock({ workflow_run_id: "wr_1", status: Status.Running }),+ );+ expect(container.textContent).toBe("");+ });++ it("shows a default message when instructions are empty", () => {+ renderInteraction(buildBlock({ instructions: null }));+ expect(+ screen.getByText("The agent is paused and waiting for your review."),+ ).toBeTruthy();+ });++ it("renders nothing when the run is not paused", () => {+ useWorkflowRunWithWorkflowQueryMock.mockReturnValue({+ data: { workflow_run_id: "wr_1", status: Status.Running },+ });+ const { container } = renderInteraction(buildBlock());+ expect(container.textContent).toBe("");+ });++ it("renders nothing for a resolved block while the run is paused elsewhere", () => {+ // Run paused at a later HITL block, but THIS block already resolved (completed).+ // Its buttons must NOT show, or a stale prompt would cancel the wrong pause.+ const { container } = renderInteraction(+ buildBlock({ status: Status.Completed }),+ );+ expect(container.textContent).toBe("");+ });+});diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunHumanInteraction.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunHumanInteraction.tsxindex f3f12136c..7b31a065a 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunHumanInteraction.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunHumanInteraction.tsx@@ -26,15 +26,25 @@ interface Props {export function WorkflowRunHumanInteraction({ workflowRunBlock }: Props) {const credentialGetter = useCredentialGetter();const queryClient = useQueryClient();- const { data: workflowRun } = useWorkflowRunWithWorkflowQuery();- const isPaused =- workflowRun && workflowRun.status === WorkflowRunStatus.Paused;+ // The studio run view carries the run id in a query param, not a route+ // param, so resolve the run from the block itself rather than the URL.+ const { data: workflowRun } = useWorkflowRunWithWorkflowQuery({+ workflowRunId: workflowRunBlock.workflow_run_id,+ });+ // Actionable only when the resolved run IS this block's run (keepPreviousData can+ // briefly return the prior run while switching), the run is paused, and this block+ // is still running — else a stale/historical prompt would resolve the wrong pause+ // (the continue/cancel mutations target the resolved run).+ const isAwaitingInteraction =+ workflowRun?.workflow_run_id === workflowRunBlock.workflow_run_id &&+ workflowRun?.status === WorkflowRunStatus.Paused &&+ workflowRunBlock.status === WorkflowRunStatus.Running;++ const positiveLabel = workflowRunBlock.positive_descriptor || "Approve";+ const negativeLabel = workflowRunBlock.negative_descriptor || "Reject";const buttonLayout =- (workflowRunBlock.positive_descriptor?.length ?? 0) < 8 &&- (workflowRunBlock.negative_descriptor?.length ?? 0) < 8- ? "inline"- : "stacked";+ positiveLabel.length < 8 && negativeLabel.length < 8 ? "inline" : "stacked";const [isDialogOpen, setIsDialogOpen] = useState(false);const [choice, setChoice] = useState<"approve" | "reject" | null>(null);@@ -58,8 +68,8 @@ export function WorkflowRunHumanInteraction({ workflowRunBlock }: Props) {toast({variant: "success",- title: `${workflowRunBlock.positive_descriptor}`,- description: `Successfully chose: ${workflowRunBlock.positive_descriptor}`,+ title: positiveLabel,+ description: `Successfully chose: ${positiveLabel}`,});},onError: (error) => {@@ -90,8 +100,8 @@ export function WorkflowRunHumanInteraction({ workflowRunBlock }: Props) {toast({variant: "success",- title: `${workflowRunBlock.negative_descriptor}`,- description: `Successfully chose: ${workflowRunBlock.negative_descriptor}`,+ title: negativeLabel,+ description: `Successfully chose: ${negativeLabel}`,});},onError: (error) => {@@ -103,7 +113,7 @@ export function WorkflowRunHumanInteraction({ workflowRunBlock }: Props) {},});- if (!isPaused) {+ if (!isAwaitingInteraction) {return null;}@@ -113,11 +123,13 @@ export function WorkflowRunHumanInteraction({ workflowRunBlock }: Props) {<DialogContent><DialogHeader><DialogTitle>- {choice === "approve"- ? workflowRunBlock.positive_descriptor- : workflowRunBlock.negative_descriptor}+ {choice === "approve" ? positiveLabel : negativeLabel}</DialogTitle>- <DialogDescription>Are you sure?</DialogDescription>+ <DialogDescription>+ {choice === "approve"+ ? "The agent will continue running from where it paused."+ : "The agent run will be stopped and can't be resumed."}+ </DialogDescription></DialogHeader><DialogFooter><DialogClose asChild>@@ -139,7 +151,10 @@ export function WorkflowRunHumanInteraction({ workflowRunBlock }: Props) {</DialogContent></Dialog>- <div className="text-sm">{workflowRunBlock.instructions}</div>+ <div className="text-sm">+ {workflowRunBlock.instructions ||+ "The agent is paused and waiting for your review."}+ </div><divclassName={cn("flex gap-2", {"justify-between": buttonLayout === "inline",@@ -153,7 +168,7 @@ export function WorkflowRunHumanInteraction({ workflowRunBlock }: Props) {setIsDialogOpen(true);}}>- <div>{workflowRunBlock.negative_descriptor}</div>+ <div>{negativeLabel}</div></Button><Buttonvariant="default"@@ -162,7 +177,7 @@ export function WorkflowRunHumanInteraction({ workflowRunBlock }: Props) {setIsDialogOpen(true);}}>- <div>{workflowRunBlock.positive_descriptor}</div>+ <div>{positiveLabel}</div></Button></div></div>