Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

8.0 KB
Raw Blame
"use client";

import { CircleHelp } from "lucide-react";
import React, { useEffect, useId } from "react";
import {
  useController,
  useWatch,
  type Control,
  type ControllerRenderProps,
  type RegisterOptions,
} from "react-hook-form";
import { Field, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import {
  getLoggingOnlyScopeOptions,
  modeIncludesLoggingOnly,
  normalizeLoggingOnlyScopeChoice,
  type LoggingOnlyScopeChoice,
} from "./guardrail_info_helpers";

export interface GuardrailCriterion {
  name: string;
  weight: number | string;
  description?: string;
}

export interface GuardrailFormValues extends Record<string, unknown> {
  criteria?: GuardrailCriterion[];
  logging_only_scope_choice?: LoggingOnlyScopeChoice;
  logging_only_continue_on_input_failure?: boolean;
}
export type GuardrailFormControl = Control<GuardrailFormValues>;
export type GuardrailFieldRules = Pick<RegisterOptions<GuardrailFormValues, string>, "validate">;

export type GuardrailFieldControlProps = ControllerRenderProps<GuardrailFormValues, string> & {
  id: string;
  "aria-invalid": true | undefined;
  "aria-describedby": string | undefined;
};

const isBlank = (value: unknown): boolean =>
  value === undefined || value === null || value === "" || (Array.isArray(value) && value.length === 0);

export const requiredRule = (message: string): GuardrailFieldRules => ({
  validate: (value: unknown) => (isBlank(value) ? message : true),
});

export const asText = (value: unknown): string => {
  if (typeof value === "string") return value;
  if (typeof value === "number") return String(value);
  return "";
};

const LOGGING_ONLY_SCOPE_CHOICES: ReadonlySet<string> = new Set(
  getLoggingOnlyScopeOptions(true).map(({ value }) => value),
);

const isLoggingOnlyScopeChoice = (value: unknown): value is LoggingOnlyScopeChoice =>
  typeof value === "string" && LOGGING_ONLY_SCOPE_CHOICES.has(value);

export const asStringArray = (value: unknown): string[] => {
  if (Array.isArray(value)) return value.filter((entry): entry is string => typeof entry === "string");
  if (typeof value === "string" && value !== "") return [value];
  return [];
};

export const readRecord = (source: unknown, key: string): unknown =>
  source !== null && typeof source === "object" ? (source as Record<string, unknown>)[key] : undefined;

export const labelWithHint = (label: React.ReactNode, hint: string): React.ReactNode => (
  <>
    {label}
    <Tooltip>
      <TooltipTrigger render={<CircleHelp className="size-3.5 shrink-0 cursor-help text-muted-foreground" />} />
      <TooltipContent className="max-w-xs">{hint}</TooltipContent>
    </Tooltip>
  </>
);

export interface GuardrailFieldProps {
  control: GuardrailFormControl;
  name: string;
  label?: React.ReactNode;
  description?: React.ReactNode;
  rules?: GuardrailFieldRules;
  defaultValue?: unknown;
  className?: string;
  children: (control: GuardrailFieldControlProps) => React.ReactNode;
}

export const GuardrailField: React.FC<GuardrailFieldProps> = ({
  control,
  name,
  label,
  description,
  rules,
  defaultValue,
  className,
  children,
}) => {
  const reactId = useId();
  const controlId = `${reactId}-control`;
  const descriptionId = `${reactId}-description`;
  const errorId = `${reactId}-error`;
  const { field, fieldState } = useController({ control, name, rules, defaultValue });
  const invalid = fieldState.error !== undefined;
  const describedBy =
    [description !== undefined ? descriptionId : undefined, invalid ? errorId : undefined]
      .filter((id): id is string => id !== undefined)
      .join(" ") || undefined;

  return (
    <Field data-invalid={invalid || undefined} className={className}>
      {label !== undefined && <FieldLabel htmlFor={controlId}>{label}</FieldLabel>}
      {children({ ...field, id: controlId, "aria-invalid": invalid || undefined, "aria-describedby": describedBy })}
      {description !== undefined && <FieldDescription id={descriptionId}>{description}</FieldDescription>}
      <FieldError id={errorId} errors={[fieldState.error]} />
    </Field>
  );
};

const SKIP_MESSAGE_ITEMS = [
  { label: "Use global default", value: "inherit" },
  { label: "Yes — exclude from guardrail scan", value: "yes" },
  { label: "No — always include in scan", value: "no" },
];

export const SkipMessageSelect: React.FC<{ control: GuardrailFieldControlProps }> = ({ control }) => {
  const { id, value, onChange, "aria-invalid": ariaInvalid, "aria-describedby": ariaDescribedBy } = control;

  return (
    <Select items={SKIP_MESSAGE_ITEMS} value={asText(value) || null} onValueChange={onChange}>
      <SelectTrigger id={id} aria-invalid={ariaInvalid} aria-describedby={ariaDescribedBy} className="w-full">
        <SelectValue placeholder="Select an option" />
      </SelectTrigger>
      <SelectContent>
        {SKIP_MESSAGE_ITEMS.map((item) => (
          <SelectItem key={item.value} value={item.value}>
            {item.label}
          </SelectItem>
        ))}
      </SelectContent>
    </Select>
  );
};

export const LoggingOnlyScopeSelect: React.FC<{
  control: GuardrailFieldControlProps;
  directionalScopeSupported: boolean;
}> = ({ control, directionalScopeSupported }) => {
  const { id, value, onChange, "aria-invalid": ariaInvalid, "aria-describedby": ariaDescribedBy } = control;
  const items = getLoggingOnlyScopeOptions(directionalScopeSupported);

  useEffect(() => {
    const currentChoice = isLoggingOnlyScopeChoice(value) ? value : "default";
    const choice = normalizeLoggingOnlyScopeChoice(currentChoice, directionalScopeSupported);
    if (choice !== value) onChange(choice);
  }, [value, directionalScopeSupported, onChange]);

  return (
    <Select items={items} value={asText(value) || "default"} onValueChange={onChange}>
      <SelectTrigger id={id} aria-invalid={ariaInvalid} aria-describedby={ariaDescribedBy} className="w-full">
        <SelectValue placeholder="Select an option" />
      </SelectTrigger>
      <SelectContent>
        {items.map((item) => (
          <SelectItem key={item.value} value={item.value}>
            {item.label}
          </SelectItem>
        ))}
      </SelectContent>
    </Select>
  );
};

export const LoggingOnlyScopeField: React.FC<{
  control: GuardrailFormControl;
  mode: unknown;
  directionalScopeSupported: boolean;
}> = ({ control, mode, directionalScopeSupported }) => {
  const scopeChoice = useWatch({ control, name: "logging_only_scope_choice" });
  const continueDisabled = scopeChoice === "input" || scopeChoice === "output";
  if (!modeIncludesLoggingOnly(mode)) return null;

  return (
    <>
      <GuardrailField
        control={control}
        name="logging_only_scope_choice"
        label={labelWithHint(
          "Logging only scope",
          "Which direction a logging_only scan observes. Observe-only scans never block; pre_call and post_call on this guardrail still block.",
        )}
      >
        {(fieldControl) => (
          <LoggingOnlyScopeSelect control={fieldControl} directionalScopeSupported={directionalScopeSupported} />
        )}
      </GuardrailField>
      <GuardrailField
        control={control}
        name="logging_only_continue_on_input_failure"
        label={labelWithHint(
          "Continue observing the response after a flagged request",
          "When on, a flagged or failing request scan is logged and the response is still scanned, so both verdicts land.",
        )}
      >
        {(fieldControl) => (
          <Switch
            id={fieldControl.id}
            checked={!continueDisabled && fieldControl.value === true}
            disabled={continueDisabled}
            onCheckedChange={fieldControl.onChange}
            aria-invalid={fieldControl["aria-invalid"]}
            aria-describedby={fieldControl["aria-describedby"]}
            aria-label="Continue observing the response after a flagged request"
          />
        )}
      </GuardrailField>
    </>
  );
};