Skip to content

Automate browser based workflows with AI

13.7 KB
Raw Blame
import { ScrollArea, ScrollAreaViewport } from "@/components/ui/scroll-area";
import { useWorkflowPanelStore } from "@/store/WorkflowPanelStore";
import { useState, useRef, useEffect } from "react";
import { PlusIcon, MagnifyingGlassIcon } from "@radix-ui/react-icons";
import { WorkflowBlockTypes } from "../../types/workflowTypes";
import { WorkflowBlockNode } from "../nodes";
import { WorkflowBlockIcon } from "../nodes/WorkflowBlockIcon";
import { AddNodeProps } from "../Workspace";
import { Input } from "@/components/ui/input";

const enableCodeBlock =
  import.meta.env.VITE_ENABLE_CODE_BLOCK?.toLowerCase() === "true";

const nodeLibraryItems: Array<{
  nodeType: NonNullable<WorkflowBlockNode["type"]>;
  icon: JSX.Element;
  title: string;
  description: string;
}> = [
  {
    nodeType: "login",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Login}
        className="size-6"
      />
    ),
    title: "Login Block",
    description: "Login to a website",
  },
  {
    nodeType: "navigation",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Navigation}
        className="size-6"
      />
    ),
    title: "Browser Task Block",
    description: "Take actions to achieve a task.",
  },
  {
    nodeType: "action",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Action}
        className="size-6"
      />
    ),
    title: "Browser Action Block",
    description: "Take a single action",
  },
  {
    nodeType: "extraction",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Extraction}
        className="size-6"
      />
    ),
    title: "Extraction Block",
    description: "Extract data from a webpage",
  },
  {
    nodeType: "validation",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Validation}
        className="size-6"
      />
    ),
    title: "AI Validation Block",
    description: "Have an AI validate the state of the screen",
  },
  {
    nodeType: "human_interaction",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.HumanInteraction}
        className="size-6"
      />
    ),
    title: "Human Interaction Block",
    description: "Pause agent for human review and approval",
  },
  // Data export now lives inside the Extraction block's "Export" section
  // (SKY-15396). Existing workflows that already have a Data Export node keep
  // working -- this only hides it from new-block authoring.
  // {
  //   nodeType: "dataExport",
  //   icon: (
  //     <WorkflowBlockIcon
  //       workflowBlockType={WorkflowBlockTypes.DataExport}
  //       className="size-6"
  //     />
  //   ),
  //   title: "Data Export Block",
  //   description: "Write schema-defined records to a Parquet file",
  // },
  // {
  //   nodeType: "task",
  //   icon: (
  //     <WorkflowBlockIcon
  //       workflowBlockType={WorkflowBlockTypes.Task}
  //       className="size-6"
  //     />
  //   ),
  //   title: "Task Block",
  //   description: "Complete multi-step browser automation tasks",
  // },
  {
    nodeType: "url",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.URL}
        className="size-6"
      />
    ),
    title: "Go to URL Block",
    description: "Navigate to a specific URL",
  },
  {
    nodeType: "textPrompt",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.TextPrompt}
        className="size-6"
      />
    ),
    title: "Text Prompt Block",
    description: "Process text with LLM",
  },
  {
    nodeType: "conditional",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Conditional}
        className="size-6"
      />
    ),
    title: "Conditional Block",
    description: "Branch execution based on conditions",
  },
  {
    nodeType: "sendEmail",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.SendEmail}
        className="size-6"
      />
    ),
    title: "Send Email Block",
    description: "Send email notifications",
  },
  {
    nodeType: "loop",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.ForLoop}
        className="size-6"
      />
    ),
    title: "Loop Block",
    description: "Iterate over a list or while a condition stays true",
  },
  {
    nodeType: "codeBlock",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Code}
        className="size-6"
      />
    ),
    title: "Code Block",
    description: "Execute custom Python code",
  },
  {
    nodeType: "fileParser",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.FileURLParser}
        className="size-6"
      />
    ),
    title: "File Parser Block",
    description: "Parse PDFs, CSVs, Excel files, and Images",
  },
  // {
  //   nodeType: "pdfParser",
  //   icon: (
  //     <WorkflowBlockIcon
  //       workflowBlockType={WorkflowBlockTypes.PDFParser}
  //       className="size-6"
  //     />
  //   ),
  //   title: "PDF Parser Block",
  //   description: "Extract data from PDF files",
  // },
  //   nodeType: "upload",
  //   icon: (
  //     <WorkflowBlockIcon
  //       workflowBlockType={WorkflowBlockTypes.UploadToS3}
  //       className="size-6"
  //     />
  //   ),
  //   title: "Upload to S3 Block",
  //   description: "Upload files to AWS S3",
  // },
  // {
  //   nodeType: "download",
  //   icon: (
  //     <WorkflowBlockIcon
  //       workflowBlockType={WorkflowBlockTypes.DownloadToS3}
  //       className="size-6"
  //     />
  //   ),
  //   title: "Download from S3 Block",
  //   description: "Download files from AWS S3",
  // },
  {
    nodeType: "fileUpload",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.FileUpload}
        className="size-6"
      />
    ),
    title: "Cloud Storage Block",
    description: "Upload files to storage",
  },
  {
    nodeType: "fileDownload",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.FileDownload}
        className="size-6"
      />
    ),
    title: "File Download Block",
    description: "Download files from a website",
  },
  {
    nodeType: "wait",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Wait}
        className="size-6"
      />
    ),
    title: "Wait Block",
    description: "Wait for a specified amount of time",
  },
  {
    nodeType: "terminate",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.Terminate}
        className="size-6"
      />
    ),
    title: "Terminate Block",
    description: "End the run as terminated with a reason",
  },
  {
    nodeType: "web_search",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.WebSearch}
        className="size-6"
      />
    ),
    title: "Web Search Block",
    description: "Search Google or Exa, with an optional prompt",
  },
  {
    nodeType: "http_request",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.HttpRequest}
        className="size-6"
      />
    ),
    title: "HTTP Request Block",
    description: "Make HTTP API calls",
  },
  {
    nodeType: "printPage",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.PrintPage}
        className="size-6"
      />
    ),
    title: "Print Page Block",
    description: "Print current page to PDF",
  },
  {
    nodeType: "pdfFill",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.PDFFill}
        className="size-6"
      />
    ),
    title: "PDF Fill Block",
    description: "Fill a PDF form",
  },
  {
    nodeType: "splitPdf",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.SplitPDF}
        className="size-6"
      />
    ),
    title: "Split PDF",
    description:
      "Split one PDF into multiple PDFs based on a prompt and save each to S3.",
  },
  {
    nodeType: "workflowTrigger",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.WorkflowTrigger}
        className="size-6"
      />
    ),
    title: "Agent Trigger Block",
    description: "Trigger another agent",
  },
  {
    nodeType: "emailInbox",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.EmailInbox}
        className="size-6"
      />
    ),
    title: "Email Inbox",
    description: "Search Gmail or Outlook inboxes",
  },
  {
    nodeType: "googleSheetsRead",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.GoogleSheetsRead}
        className="size-6"
      />
    ),
    title: "Google Sheets Read",
    description: "Read data from a Google Sheet",
  },
  {
    nodeType: "googleSheetsWrite",
    icon: (
      <WorkflowBlockIcon
        workflowBlockType={WorkflowBlockTypes.GoogleSheetsWrite}
        className="size-6"
      />
    ),
    title: "Google Sheets Write",
    description: "Write data to a Google Sheet",
  },
];

type Props = {
  onNodeClick: (props: AddNodeProps) => void;
};

function WorkflowNodeLibraryPanel({ onNodeClick }: Props) {
  const workflowPanelData = useWorkflowPanelStore(
    (state) => state.workflowPanelState.data,
  );
  const workflowPanelActive = useWorkflowPanelStore(
    (state) => state.workflowPanelState.active,
  );
  const closeWorkflowPanel = useWorkflowPanelStore(
    (state) => state.closeWorkflowPanel,
  );
  const [search, setSearch] = useState("");
  const inputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    // Focus the input when the panel becomes active
    if (workflowPanelActive && inputRef.current) {
      // Use multiple approaches to ensure focus works
      const focusInput = () => {
        if (inputRef.current) {
          inputRef.current.focus();
          inputRef.current.select(); // Also select any existing text
        }
      };

      // Try immediate focus
      focusInput();

      // Also try with a small delay for animations/transitions
      const timeoutId = setTimeout(() => {
        focusInput();
      }, 100);

      // And try with a longer delay as backup
      const backupTimeoutId = setTimeout(() => {
        focusInput();
      }, 300);

      return () => {
        clearTimeout(timeoutId);
        clearTimeout(backupTimeoutId);
      };
    }
  }, [workflowPanelActive]);

  const filteredItems = nodeLibraryItems.filter((item) => {
    if (workflowPanelData?.disableLoop && item.nodeType === "loop") {
      return false;
    }
    if (!enableCodeBlock && item.nodeType === "codeBlock") {
      return false;
    }

    const term = search.toLowerCase();
    if (!term) {
      return true;
    }

    return (
      item.nodeType.toLowerCase().includes(term) ||
      item.title.toLowerCase().includes(term) ||
      item.description.toLowerCase().includes(term)
    );
  });

  return (
    <div className="flex h-full min-h-0 w-full min-w-0 flex-col gap-4">
      <div className="relative">
        <div className="absolute left-0 top-0 flex size-9 items-center justify-center">
          <MagnifyingGlassIcon className="size-5" />
        </div>
        <Input
          value={search}
          onChange={(event) => {
            setSearch(event.target.value);
          }}
          placeholder="Search blocks..."
          className="pl-9"
          ref={inputRef}
          autoFocus
          tabIndex={0}
        />
      </div>
      <ScrollArea className="min-h-0 min-w-0 flex-1">
        {/* Radix wraps viewport children in a display:table div by default;
        override it so narrow sidebar rows can shrink instead of widening the
        clipped editor shell. */}
        <ScrollAreaViewport className="h-full w-full [&>div]:!block [&>div]:!overflow-x-hidden">
          <div className="space-y-2">
            {filteredItems.length > 0 ? (
              filteredItems.map((item) => (
                <div
                  key={item.nodeType}
                  data-testid={`block-library-item-${item.nodeType}`}
                  className="flex min-w-0 cursor-pointer items-center justify-between rounded-sm bg-slate-elevation4 p-4 hover:bg-slate-elevation5"
                  onClick={() => {
                    onNodeClick({
                      nodeType: item.nodeType,
                      next: workflowPanelData?.next ?? null,
                      parent: workflowPanelData?.parent,
                      previous: workflowPanelData?.previous ?? null,
                      connectingEdgeType:
                        workflowPanelData?.connectingEdgeType ??
                        "edgeWithAddButton",
                      branch: workflowPanelData?.branchContext,
                    });
                    closeWorkflowPanel();
                  }}
                >
                  <div className="flex min-w-0 gap-2">
                    <div className="flex h-[2.75rem] w-[2.75rem] shrink-0 items-center justify-center rounded border border-border dark:border-slate-600">
                      {item.icon}
                    </div>
                    <div className="flex min-w-0 flex-col gap-1">
                      <span className="min-w-0 truncate text-base">
                        {item.title}
                      </span>
                      <span className="min-w-0 truncate text-xs text-slate-600 dark:text-slate-400">
                        {item.description}
                      </span>
                    </div>
                  </div>
                  <PlusIcon className="size-6 shrink-0" />
                </div>
              ))
            ) : (
              <div className="p-4 text-center text-sm text-muted-foreground">
                No results found
              </div>
            )}
          </div>
        </ScrollAreaViewport>
      </ScrollArea>
    </div>
  );
}

export { WorkflowNodeLibraryPanel };