Skip to content

The open alternative to Salesforce, designed for AI.

4.5 KB
Raw Blame
import { getChipLabel } from '@/ui/field/display/utils/getChipLabel';
import { styled } from '@linaria/react';
import { t } from '@lingui/core/macro';
import { Chip } from 'twenty-ui/primitives/data-display';
import {
  IconBolt,
  IconBuildingSkyscraper,
  IconFlag,
  IconTag,
  IconUsers,
  IconWindow,
  type IconComponent,
} from 'twenty-ui/icon';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';

import { type AiModelSummary } from '@/settings/ai/types/AiModelSummary';
import { getDataResidencyDisplay } from '@/ai/utils/getDataResidencyDisplay';
import { getModelIcon } from '@/ai/utils/getModelIcon';
import { SettingsTableCard } from '@/settings/components/SettingsTableCard';
import { isDefined } from 'twenty-shared/utils';
import { formatNumber } from '@/localization/utils/formatNumber';

const StyledHoverCardWrapper = styled.div`
  border-radius: ${themeCssVariables.border.radius.md};
  box-shadow: ${themeCssVariables.boxShadow.strong};
  overflow: hidden;
  width: 320px;
`;

const StyledChipContainer = styled.div`
  max-width: 100%;
  overflow: hidden;
`;

const StyledValueText = styled.span`
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
`;

const formatCost = (
  inputCost?: number | null,
  outputCost?: number | null,
): string => {
  if (!isDefined(inputCost) && !isDefined(outputCost)) {
    return '—';
  }

  const parts: string[] = [];

  if (isDefined(inputCost)) {
    parts.push(t`$${inputCost} in`);
  }

  if (isDefined(outputCost)) {
    parts.push(t`$${outputCost} out`);
  }

  return parts.join(' / ');
};

type SettingsAiModelHoverCardProps = {
  model: AiModelSummary;
};

type HoverCardItem = {
  Icon: IconComponent;
  label: string;
  value: React.ReactNode;
};

export const SettingsAiModelHoverCard = ({
  model,
}: SettingsAiModelHoverCardProps) => {
  const theme = useTheme();

  const ModelIcon = getModelIcon(model.modelFamily, model.providerName);
  const providerLabel = model.providerLabel ?? model.providerName ?? '—';

  const items: HoverCardItem[] = [
    {
      Icon: IconTag,
      label: t`Name`,
      value: (
        <StyledChipContainer>
          <Chip
            size="sm"
            color="primary"
            variant="soft"
            startElement={
              <ModelIcon
                size={theme.icon.size.sm}
                stroke={theme.icon.stroke.sm}
              />
            }
            endElement={null}
          >
            {getChipLabel(model.label).content}
          </Chip>
        </StyledChipContainer>
      ),
    },
    {
      Icon: IconBuildingSkyscraper,
      label: t`Provider`,
      value: <StyledValueText>{providerLabel}</StyledValueText>,
    },
    ...(isDefined(model.inputCostPerMillionTokens) ||
    isDefined(model.outputCostPerMillionTokens)
      ? [
          {
            Icon: IconUsers,
            label: t`Cost per 1M tokens`,
            value: (
              <StyledValueText>
                {formatCost(
                  model.inputCostPerMillionTokens,
                  model.outputCostPerMillionTokens,
                )}
              </StyledValueText>
            ),
          },
        ]
      : []),
    ...(isDefined(model.contextWindowTokens)
      ? [
          {
            Icon: IconWindow,
            label: t`Context`,
            value: (
              <StyledValueText>
                {t`${formatNumber(model.contextWindowTokens, {
                  abbreviate: true,
                  decimals: 1,
                })} tokens`}
              </StyledValueText>
            ),
          },
        ]
      : []),
    ...(isDefined(model.maxOutputTokens)
      ? [
          {
            Icon: IconBolt,
            label: t`Max output`,
            value: (
              <StyledValueText>
                {t`${formatNumber(model.maxOutputTokens, {
                  abbreviate: true,
                  decimals: 1,
                })} tokens`}
              </StyledValueText>
            ),
          },
        ]
      : []),
    ...(isDefined(model.dataResidency)
      ? [
          {
            Icon: IconFlag,
            label: t`Data residency`,
            value: (
              <StyledValueText>
                {getDataResidencyDisplay(model.dataResidency)}
              </StyledValueText>
            ),
          },
        ]
      : []),
  ];

  return (
    <StyledHoverCardWrapper>
      <SettingsTableCard
        items={items}
        gridAutoColumns={`${themeCssVariables.spacing[30]} 1fr`}
      />
    </StyledHoverCardWrapper>
  );
};