Skip to content

Automate browser based workflows with AI

6.4 KB
Raw Blame
import { getClient } from "@/api/AxiosClient";
import { Action, ActionTypes } from "@/api/types";
import { BlockMarkdown } from "@/components/AgentMarkdown";
import { StatusPill } from "@/components/ui/status-pill";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { ScrollArea, ScrollAreaViewport } from "@/components/ui/scroll-area";
import { Separator } from "@/components/ui/separator";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { cn } from "@/util/utils";
import {
  CheckCircledIcon,
  CrossCircledIcon,
  DotFilledIcon,
  LightningBoltIcon,
} from "@radix-ui/react-icons";
import { useQueryClient } from "@tanstack/react-query";
import { ReactNode, useRef } from "react";
import { ActionTypePill } from "./ActionTypePill";

type Props = {
  data: Array<Action | null>;
  onActiveIndexChange: (index: number | "stream") => void;
  activeIndex: number | "stream";
  showStreamOption: boolean;
  taskDetails: {
    steps: number;
    actions: number;
    cost?: string;
  };
};

function ScrollableActionList({
  data,
  activeIndex,
  onActiveIndexChange,
  showStreamOption,
  taskDetails,
}: Props) {
  const queryClient = useQueryClient();
  const credentialGetter = useCredentialGetter();
  const refs = useRef<Array<HTMLDivElement | null>>(
    Array.from({ length: data.length + 1 }),
  );

  function prefetchStepArtifacts(stepId: string) {
    queryClient.prefetchQuery({
      queryKey: ["step", stepId, "artifacts"],
      queryFn: async () => {
        const client = await getClient(credentialGetter);
        return client
          .get(`/step/${stepId}/artifacts`)
          .then((response) => response.data);
      },
    });
  }

  function getReverseActions() {
    const elements: ReactNode[] = [];
    for (let i = data.length - 1; i >= 0; i--) {
      const action = data[i];
      if (!action) {
        continue;
      }
      const selected = activeIndex === i;
      elements.push(
        <div
          key={i}
          ref={(element) => {
            refs.current[i] = element;
          }}
          className={cn(
            "flex cursor-pointer rounded-lg border-2 bg-slate-elevation3 hover:border-slate-50",
            {
              "border-l-destructive": !action.success,
              "border-l-success": action.success,
              "border-slate-50": selected,
            },
          )}
          onClick={() => onActiveIndexChange(i)}
          onMouseEnter={() => prefetchStepArtifacts(action.stepId)}
        >
          <div className="flex-1 space-y-2 p-4 pl-5">
            <div className="flex justify-between">
              <div className="flex items-center gap-2">
                <span>#{i + 1}</span>
              </div>
              <div className="flex items-center gap-2">
                <ActionTypePill actionType={action.type} />
                {action.created_by === "script" && (
                  <TooltipProvider>
                    <Tooltip delayDuration={300}>
                      <TooltipTrigger asChild>
                        <StatusPill
                          icon={
                            <LightningBoltIcon className="h-4 w-4 text-[gold]" />
                          }
                        />
                      </TooltipTrigger>
                      <TooltipContent className="max-w-[250px]">
                        Code Execution
                      </TooltipContent>
                    </Tooltip>
                  </TooltipProvider>
                )}
                {action.success ? (
                  <StatusPill
                    icon={<CheckCircledIcon className="h-4 w-4 text-success" />}
                  />
                ) : (
                  <StatusPill
                    icon={
                      <CrossCircledIcon className="h-4 w-4 text-destructive" />
                    }
                  />
                )}
              </div>
            </div>
            {action.summary?.body && (
              <div className="break-words text-xs text-slate-400">
                {action.summary.body.isProse ? (
                  <BlockMarkdown text={action.summary.body.text} />
                ) : (
                  action.summary.body.text
                )}
              </div>
            )}
            {action.summary?.outcome && (
              <div className="break-words text-xs text-slate-400">
                <span className="text-slate-500">Result: </span>
                {action.summary.outcome}
              </div>
            )}
            {action.type === ActionTypes.InputText && (
              <>
                <Separator />
                <div className="max-h-24 overflow-y-auto break-words text-xs text-slate-400">
                  Input: {action.input}
                </div>
              </>
            )}
          </div>
        </div>,
      );
    }
    return elements;
  }

  return (
    <div className="h-[40rem] w-1/3 rounded border bg-slate-elevation1">
      <div className="flex items-center gap-2 p-4">
        <div className="flex h-8 flex-1 items-center justify-center rounded-sm bg-slate-700 px-3 text-xs text-gray-50">
          Actions: {taskDetails.actions}
        </div>
        <div className="flex h-8 flex-1 items-center justify-center rounded-sm bg-slate-700 px-3 text-xs text-gray-50">
          Steps: {taskDetails.steps}
        </div>
      </div>
      <Separator />
      <ScrollArea className="p-4">
        <ScrollAreaViewport className="max-h-[34rem]">
          <div className="space-y-4">
            {showStreamOption && (
              <div
                key="stream"
                ref={(element) => {
                  refs.current[data.length] = element;
                }}
                className={cn(
                  "flex cursor-pointer rounded-lg border-2 bg-slate-elevation3 p-4 hover:border-slate-50",
                  {
                    "border-slate-50": activeIndex === "stream",
                  },
                )}
                onClick={() => onActiveIndexChange("stream")}
              >
                <div className="flex items-center gap-2">
                  <DotFilledIcon className="h-6 w-6 text-destructive" />
                  <span>Live</span>
                </div>
              </div>
            )}
            {getReverseActions()}
          </div>
        </ScrollAreaViewport>
      </ScrollArea>
    </div>
  );
}

export { ScrollableActionList };