Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

10.9 KB
Raw Blame
import { useEffect, useState } from "react";
import { Controller, useFormContext } from "react-hook-form";

import { TSecretRotationV2Form } from "@app/components/secret-rotations-v2/forms/schemas";
import { FieldLabelWithTooltip } from "@app/components/secret-rotations-v2/forms/shared";
import {
  Combobox,
  Field,
  FieldError,
  Input,
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
  TextArea
} from "@app/components/v3";
import { useDebounce } from "@app/hooks";
import {
  useListLiteLLMConnectionModels,
  useListLiteLLMConnectionTeams,
  useListLiteLLMConnectionUsers
} from "@app/hooks/api/appConnections/litellm";
import { TLiteLLMTeam, TLiteLLMUser } from "@app/hooks/api/appConnections/litellm/types";
import { SecretRotation } from "@app/hooks/api/secretRotationsV2";

/** Max length for the key name (matches backend schema). */
const LITELLM_API_KEY_NAME_MAX_LENGTH = 100;

/** Starter set of common LiteLLM key options; users can edit or clear these. Must be valid JSON. */
const DEFAULT_LITELLM_OPTIONS = `{
  "max_budget": null,
  "tpm_limit": null,
  "rpm_limit": null,
  "metadata": {}
}`;

enum ParameterTab {
  General = "general",
  Advanced = "advanced"
}

type LiteLLMOption = { id: string; name: string };

/**
 * Resolves the option to display for a stored id. Because the list is fetched with
 * server-side search, the selected record may not be in the current page, so we fall
 * back to the locally-captured selection, then to a bare id so the field never blanks.
 */
const resolveSelectedOption = <T extends LiteLLMOption>(
  options: T[] | undefined,
  selected: T | null,
  id: string | undefined
): T | null => {
  if (!id) return null;
  if (selected?.id === id) return selected;
  return options?.find((option) => option.id === id) ?? ({ id, name: id } as T);
};

/** Ensures the selected option is present in the list so its label always renders. */
const withSelectedOption = <T extends LiteLLMOption>(
  options: T[] | undefined,
  selected: T | null
): T[] => {
  const base = options ?? [];
  if (selected && !base.some((option) => option.id === selected.id)) {
    return [selected, ...base];
  }
  return base;
};

export const LiteLLMApiKeyRotationParametersFields = () => {
  const { control, watch } = useFormContext<
    TSecretRotationV2Form & {
      type: SecretRotation.LiteLLMApiKey;
    }
  >();

  const connectionId = watch("connection.id");
  const userId = watch("parameters.userId");
  const teamId = watch("parameters.teamId");

  const [userSearch, setUserSearch] = useState("");
  const [debouncedUserSearch] = useDebounce(userSearch);
  const [selectedUser, setSelectedUser] = useState<TLiteLLMUser | null>(null);

  const [teamSearch, setTeamSearch] = useState("");
  const [debouncedTeamSearch] = useDebounce(teamSearch);
  const [selectedTeam, setSelectedTeam] = useState<TLiteLLMTeam | null>(null);

  const { data: users, isFetching: isUsersFetching } = useListLiteLLMConnectionUsers(
    connectionId,
    debouncedUserSearch.trim(),
    {
      enabled: Boolean(connectionId)
    }
  );

  const { data: teams, isFetching: isTeamsFetching } = useListLiteLLMConnectionTeams(
    connectionId,
    debouncedTeamSearch.trim(),
    {
      enabled: Boolean(connectionId)
    }
  );

  const { data: models, isPending: isModelsPending } = useListLiteLLMConnectionModels(
    connectionId,
    {
      enabled: Boolean(connectionId)
    }
  );

  // Seed the locally-captured selection from fetched records (e.g. when editing an
  // existing rotation) so the label survives even after the search list changes.
  useEffect(() => {
    if (userId && selectedUser?.id !== userId) {
      const match = users?.find((user) => user.id === userId);
      if (match) setSelectedUser(match);
    }
  }, [users, userId, selectedUser]);

  useEffect(() => {
    if (teamId && selectedTeam?.id !== teamId) {
      const match = teams?.find((team) => team.id === teamId);
      if (match) setSelectedTeam(match);
    }
  }, [teams, teamId, selectedTeam]);

  return (
    <Tabs defaultValue={ParameterTab.General}>
      <TabsList variant="project" className="w-full justify-start">
        <TabsTrigger value={ParameterTab.General}>General</TabsTrigger>
        <TabsTrigger value={ParameterTab.Advanced}>Advanced</TabsTrigger>
      </TabsList>
      <TabsContent value={ParameterTab.General} className="space-y-4">
        <Controller
          name="parameters.name"
          control={control}
          render={({ field: { value, onChange, onBlur, ref }, fieldState: { error } }) => (
            <Field data-invalid={Boolean(error)}>
              <FieldLabelWithTooltip
                htmlFor="litellm-key-name"
                tooltip="A descriptive name for the generated key. Infisical appends a timestamp so each rotated key stays unique and records its creation time."
              >
                Key Name
              </FieldLabelWithTooltip>
              <Input
                ref={ref}
                id="litellm-key-name"
                value={value}
                onBlur={onBlur}
                onChange={onChange}
                placeholder="my-rotated-key"
                maxLength={LITELLM_API_KEY_NAME_MAX_LENGTH}
                isError={Boolean(error)}
              />
              <FieldError>{error?.message}</FieldError>
            </Field>
          )}
        />
        <Controller
          name="parameters.userId"
          control={control}
          render={({ field: { value, onChange, onBlur }, fieldState: { error } }) => {
            const selectedValue = resolveSelectedOption(users, selectedUser, value);
            return (
              <Field data-invalid={Boolean(error)}>
                <FieldLabelWithTooltip
                  htmlFor="litellm-user"
                  tooltip="Associate the generated key with a LiteLLM user. Search by email."
                >
                  User <span className="font-normal text-muted">(optional)</span>
                </FieldLabelWithTooltip>
                <Combobox
                  id="litellm-user"
                  isLoading={
                    (userSearch !== debouncedUserSearch || isUsersFetching) && Boolean(connectionId)
                  }
                  isDisabled={!connectionId}
                  value={selectedValue}
                  onBlur={onBlur}
                  onValueChange={(newValue) => {
                    setSelectedUser(newValue);
                    onChange(newValue?.id);
                  }}
                  options={withSelectedOption(users, selectedValue)}
                  onSearchChange={setUserSearch}
                  placeholder="Search for a user..."
                  getOptionLabel={(option) => option.name}
                  getOptionValue={(option) => option.id}
                  isError={Boolean(error)}
                />
                <FieldError>{error?.message}</FieldError>
              </Field>
            );
          }}
        />
        <Controller
          name="parameters.teamId"
          control={control}
          render={({ field: { value, onChange, onBlur }, fieldState: { error } }) => {
            const selectedValue = resolveSelectedOption(teams, selectedTeam, value);
            return (
              <Field data-invalid={Boolean(error)}>
                <FieldLabelWithTooltip
                  htmlFor="litellm-team"
                  tooltip="Associate the generated key with a LiteLLM team. Search by team alias."
                >
                  Team <span className="font-normal text-muted">(optional)</span>
                </FieldLabelWithTooltip>
                <Combobox
                  id="litellm-team"
                  isLoading={
                    (teamSearch !== debouncedTeamSearch || isTeamsFetching) && Boolean(connectionId)
                  }
                  isDisabled={!connectionId}
                  value={selectedValue}
                  onBlur={onBlur}
                  onValueChange={(newValue) => {
                    setSelectedTeam(newValue);
                    onChange(newValue?.id);
                  }}
                  options={withSelectedOption(teams, selectedValue)}
                  onSearchChange={setTeamSearch}
                  placeholder="Search for a team..."
                  getOptionLabel={(option) => option.name}
                  getOptionValue={(option) => option.id}
                  isError={Boolean(error)}
                />
                <FieldError>{error?.message}</FieldError>
              </Field>
            );
          }}
        />
        <Controller
          name="parameters.models"
          control={control}
          render={({ field: { value, onChange, onBlur }, fieldState: { error } }) => (
            <Field data-invalid={Boolean(error)}>
              <FieldLabelWithTooltip
                htmlFor="litellm-models"
                tooltip="Restrict the generated key to specific models. Leave empty to allow all models."
              >
                Models <span className="font-normal text-muted">(optional)</span>
              </FieldLabelWithTooltip>
              <Combobox
                id="litellm-models"
                multiple
                isLoading={isModelsPending && Boolean(connectionId)}
                isDisabled={!connectionId}
                value={models?.filter((model) => (value ?? []).includes(model.id)) ?? []}
                onBlur={onBlur}
                onValueChange={(option) => onChange(option.map((model) => model.id))}
                options={models}
                placeholder="Select models..."
                getOptionLabel={(option) => option.name}
                getOptionValue={(option) => option.id}
                getOptionKeywords={(option) => [option.id]}
                isError={Boolean(error)}
              />
              <FieldError>{error?.message}</FieldError>
            </Field>
          )}
        />
      </TabsContent>
      <TabsContent value={ParameterTab.Advanced} className="space-y-4">
        <Controller
          name="parameters.additionalOptions"
          control={control}
          defaultValue={DEFAULT_LITELLM_OPTIONS}
          render={({ field, fieldState: { error } }) => (
            <Field data-invalid={Boolean(error)}>
              <FieldLabelWithTooltip tooltip="Common fields: max_budget (number), tpm_limit / rpm_limit (numbers), metadata (object). user_id, team_id and models are set on the general tab; key_alias, auto_rotate, rotation_interval, duration, send_invite_email and key_type are managed by Infisical and cannot be set here.">
                Additional Key Options (JSON){" "}
                <span className="font-normal text-muted">(optional)</span>
              </FieldLabelWithTooltip>
              <TextArea
                {...field}
                className="resize-none font-mono text-sm"
                rows={10}
                placeholder={DEFAULT_LITELLM_OPTIONS}
                isError={Boolean(error)}
              />
              <FieldError>{error?.message}</FieldError>
            </Field>
          )}
        />
      </TabsContent>
    </Tabs>
  );
};