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.

5.9 KB
Raw Blame
import { PendingHumanReviewModel } from "@/app/api/__generated__/models/pendingHumanReviewModel";
import { Text } from "@/components/atoms/Text/Text";
import { Input } from "@/components/atoms/Input/Input";
import { Switch } from "@/components/atoms/Switch/Switch";
import { useEffect, useState } from "react";
import { JsonDataInput } from "./components/JsonDataInput/JsonDataInput";
import { ReviewInputFields } from "./components/ReviewInputFields/ReviewInputFields";
import { reviewFields } from "./components/ReviewInputFields/helpers";
import { useReviewBlockSchema } from "./components/ReviewInputFields/useReviewBlockSchema";

type ReviewPayload = PendingHumanReviewModel["payload"];

interface PendingReviewCardProps {
  review: PendingHumanReviewModel;
  onReviewDataChange: (nodeExecId: string, data: string) => void;
  autoApproveFuture?: boolean;
  onAutoApproveFutureChange?: (nodeExecId: string, enabled: boolean) => void;
  externalDataValue?: string;
  showAutoApprove?: boolean;
  nodeId?: string;
}

export function PendingReviewCard({
  review,
  onReviewDataChange,
  autoApproveFuture = false,
  onAutoApproveFutureChange,
  externalDataValue,
  showAutoApprove = true,
  nodeId,
}: PendingReviewCardProps) {
  const isDataEditable = review.editable;

  // A block's review names the block in `action`; a human-in-the-loop block's
  // instructions are the author's own words, shown above its data.
  const instructions = review.action ? undefined : review.instructions;
  const { schema, isLoading: isSchemaLoading } = useReviewBlockSchema(
    review.block_id,
  );

  // Render the payload as stored: it is exactly what the reviewer's decision
  // sends back as the block's input, so unwrapping any key here would show
  // one thing and execute another.
  const [currentData, setCurrentData] = useState<ReviewPayload>(review.payload);

  useEffect(() => {
    if (externalDataValue !== undefined) {
      try {
        const parsedData = JSON.parse(externalDataValue);
        setCurrentData(parsedData);
      } catch {}
    }
  }, [externalDataValue]);

  function handleDataChange(newValue: ReviewPayload) {
    setCurrentData(newValue);
    onReviewDataChange(review.node_exec_id, JSON.stringify(newValue, null, 2));
  }

  const renderFields = (readOnly: boolean) => {
    if (!isPlainObject(currentData) || isSchemaLoading) return null;
    return (
      <ReviewInputFields
        fields={reviewFields(currentData, schema)}
        values={currentData}
        onChange={(values) => handleDataChange(values as ReviewPayload)}
        readOnly={readOnly}
      />
    );
  };

  const renderDataInput = () => {
    const data = currentData;

    if (typeof data === "string") {
      return (
        <Input
          id="data-string"
          label="Value"
          hideLabel
          size="small"
          type="textarea"
          rows={3}
          value={data}
          onChange={(e) => handleDataChange(e.target.value)}
          placeholder="Enter text"
        />
      );
    } else if (typeof data === "number") {
      return (
        <Input
          id="data-number"
          label="Value"
          hideLabel
          size="small"
          type="number"
          value={data}
          onChange={(e) => handleDataChange(Number(e.target.value))}
          placeholder="Enter number"
        />
      );
    } else if (typeof data === "boolean") {
      return (
        <div className="flex items-center gap-3">
          <span className="text-sm text-gray-500">
            {data ? "Enabled" : "Disabled"}
          </span>
          <Switch
            className="ml-auto"
            checked={data}
            onCheckedChange={(checked: boolean) => handleDataChange(checked)}
          />
        </div>
      );
    } else {
      return (
        <JsonDataInput
          value={data}
          initialText={externalDataValue}
          onChange={(value) => handleDataChange(value as ReviewPayload)}
          onInvalidDraft={(text) =>
            onReviewDataChange(review.node_exec_id, text)
          }
        />
      );
    }
  };

  const getShortenedNodeId = (id: string) => {
    if (id.length <= 8) return id;
    return `${id.slice(0, 4)}...${id.slice(-4)}`;
  };

  return (
    <div className="space-y-4">
      {nodeId && (
        <Text variant="small" className="text-gray-500">
          Node #{getShortenedNodeId(nodeId)}
        </Text>
      )}

      <div className="space-y-3">
        {instructions && (
          <Text variant="body" className="font-semibold text-gray-900">
            {instructions}
          </Text>
        )}

        {isPlainObject(currentData) ? (
          renderFields(!isDataEditable || autoApproveFuture)
        ) : isDataEditable && !autoApproveFuture ? (
          renderDataInput()
        ) : (
          <div className="rounded-lg border border-gray-200 bg-white p-3">
            <Text variant="small" className="text-gray-600">
              {JSON.stringify(currentData, null, 2)}
            </Text>
          </div>
        )}
      </div>

      {/* Auto-approve toggle for this review */}
      {showAutoApprove && onAutoApproveFutureChange && (
        <div className="space-y-2 pt-2">
          <div className="flex items-center gap-3">
            <Switch
              checked={autoApproveFuture}
              onCheckedChange={(enabled: boolean) =>
                onAutoApproveFutureChange(review.node_exec_id, enabled)
              }
            />
            <Text variant="small" className="text-gray-700">
              Auto-approve future executions of this block
            </Text>
          </div>
          {autoApproveFuture && (
            <Text variant="small" className="pl-11 text-gray-500">
              Original data will be used for this and all future reviews from
              this block.
            </Text>
          )}
        </div>
      )}
    </div>
  );
}

function isPlainObject(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}