Skip to content

The platform for continuously improving AI agents.

5.2 KB
Raw Blame
// Adapted from AI SDK Agents step-inspector; see NOTICE.md.
import { type ReactNode, useState } from "react";

import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icons";
import { SearchableSelect } from "@/components/ui/searchable-select";
import { cn } from "@/lib/utils";

export const inspectorControl =
  "h-8 w-full min-w-0 rounded-sm border border-border bg-background px-2.5 text-sm leading-none transition-colors duration-150 ease-out hover:border-foreground/40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50 motion-reduce:transition-none";

export function InspectorSelect({
  label,
  value,
  options,
  disabled,
  onChange,
}: {
  label: string;
  value: string;
  options: { value: string; label: string }[];
  disabled: boolean;
  onChange: (value: string) => void;
}) {
  return (
    <SearchableSelect
      ariaLabel={label}
      disabled={disabled}
      onChange={onChange}
      options={options}
      placeholder="Choose an output"
      searchPlaceholder="Search outputs…"
      triggerClassName={inspectorControl}
      value={value}
    />
  );
}

export function InspectorField({
  label,
  children,
  action,
  optional,
  hint,
}: {
  label: string;
  children: ReactNode;
  action?: ReactNode;
  optional?: boolean;
  hint?: string;
}) {
  return (
    <div className="flex min-w-0 flex-col" data-slot="step-inspector-field">
      <div className="mb-1.5 flex min-h-5 min-w-0 items-center gap-2">
        <span className="min-w-0 flex-1 truncate text-xs leading-4 font-medium">
          {label}
          {optional && <span className="ml-1.5 font-normal text-muted-foreground">optional</span>}
        </span>
        {action}
      </div>
      {children}
      {hint && <p className="m-0 mt-1.5 text-xs leading-4 text-muted-foreground">{hint}</p>}
    </div>
  );
}

export function InspectorDisclosure({
  title,
  children,
  initialOpen = false,
}: {
  title: string;
  children: ReactNode;
  initialOpen?: boolean;
}) {
  const [open, setOpen] = useState(initialOpen);
  return (
    <div className="min-w-0">
      <button
        aria-expanded={open}
        className="flex h-6 w-full items-center gap-1.5 rounded-sm text-left text-xs text-muted-foreground outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/50"
        onClick={() => setOpen(!open)}
        type="button"
      >
        <Icon.chevronDown
          className={cn(
            "size-3 transition-transform duration-150 ease-out motion-reduce:transition-none",
            !open && "-rotate-90"
          )}
        />
        {title}
      </button>
      <div
        className={cn(
          "grid transition-[grid-template-rows,opacity] duration-200 ease-[cubic-bezier(0.25,1,0.5,1)] motion-reduce:transition-none",
          open ? "grid-rows-[1fr] opacity-100" : "grid-rows-[0fr] opacity-0"
        )}
        inert={!open}
      >
        <div className="-m-1 min-h-0 overflow-hidden p-1">
          <div className="flex min-w-0 flex-col gap-3 pt-2">{children}</div>
        </div>
      </div>
    </div>
  );
}

export function StepInspector({
  title,
  kind,
  icon,
  description,
  locked,
  onTitleChange,
  onClose,
  actions,
  children,
  footer,
}: {
  title: string;
  kind: string;
  icon: ReactNode;
  description?: string;
  locked: boolean;
  onTitleChange: (title: string) => void;
  onClose: () => void;
  actions?: ReactNode;
  children: ReactNode;
  footer?: ReactNode;
}) {
  return (
    <section
      aria-label={`${title} settings`}
      className="flex min-h-0 min-w-0 flex-col gap-3 bg-card p-3"
      data-slot="step-inspector"
    >
      <div className="flex shrink-0 flex-col gap-2" data-slot="step-inspector-header">
        <div className="flex h-6 min-w-0 items-center gap-2">
          <span className="inline-flex size-6 shrink-0 items-center justify-center rounded-md bg-cat-1/10 text-cat-1 [&_svg]:size-3.5">
            {icon}
          </span>
          <input
            aria-label="Step name"
            className="-mx-1 h-6 min-w-0 flex-1 truncate rounded-sm border-0 bg-transparent px-1 text-sm leading-none font-medium tracking-tight outline-none transition-colors duration-150 hover:bg-wash focus:bg-background focus-visible:ring-2 focus-visible:ring-ring/50 motion-reduce:transition-none"
            onChange={(event) => onTitleChange(event.target.value)}
            onKeyDown={(event) => {
              if (event.key === "Enter") event.currentTarget.blur();
            }}
            readOnly={locked}
            value={title}
          />
          <span className="rounded-sm bg-cat-1/10 px-1.5 py-0.5 text-xs leading-none text-cat-1">
            {kind}
          </span>
          {actions}
          <Button aria-label="Close editor" onClick={onClose} size="icon-xs" variant="ghost">
            <Icon.close />
          </Button>
        </div>
        {description && (
          <p className="m-0 pl-0.5 text-xs leading-4 text-muted-foreground">{description}</p>
        )}
      </div>
      <div className="-m-1 flex min-h-0 flex-col gap-3 overflow-y-auto scroll-fade-y p-1">
        {children}
      </div>
      {footer && <div className="flex min-h-6 shrink-0 items-center gap-2">{footer}</div>}
    </section>
  );
}