Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.2 KB
Raw Blame
import { cx, Flexbox, Icon, Tag, Tooltip } from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
import { BoltIcon, RotateCwIcon } from 'lucide-react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { type FileParsingTask } from '@/types/asyncTask';
import { AsyncTaskErrorType, AsyncTaskStatus } from '@/types/asyncTask';

const styles = stylex.create({
  errorReason: {
    padding: '4px',
    borderRadius: '4px',
    fontFamily: 'monospace',
    fontSize: '12px',
    backgroundColor: cssVar.colorFillTertiary,
    backgroundImage: 'none',
  },
});

interface EmbeddingStatusProps extends FileParsingTask {
  className?: string;
  onClick?: (status: AsyncTaskStatus) => void;
  onErrorClick?: (task: 'chunking' | 'embedding') => void;
}

const EmbeddingStatus = memo<EmbeddingStatusProps>(
  ({ chunkCount, embeddingStatus, embeddingError, onClick, onErrorClick, className }) => {
    const { t } = useTranslation(['components', 'common']);

    switch (embeddingStatus) {
      case AsyncTaskStatus.Processing: {
        return (
          <Flexbox horizontal>
            <Tooltip
              title={t('FileParsingStatus.chunks.embeddingStatus.processing')}
              styles={{
                root: { pointerEvents: 'none' },
              }}
            >
              <Tag
                className={cx('chunk-tag', className)}
                color={'processing'}
                icon={<Icon spin icon={BoltIcon} />}
                style={{ cursor: 'pointer' }}
                variant={'filled'}
              >
                {chunkCount}
              </Tag>
            </Tooltip>
          </Flexbox>
        );
      }

      case AsyncTaskStatus.Error: {
        // A full vector quota is not a failure retrying can fix by itself: name the quota
        // and how to free it instead of showing the raw error as a generic failure.
        const isQuotaExceeded = embeddingError?.name === AsyncTaskErrorType.EmbeddingQuotaExceeded;

        return (
          <Tooltip
            styles={{
              root: { maxWidth: 340, pointerEvents: 'none' },
            }}
            title={
              isQuotaExceeded ? (
                t('FileParsingStatus.chunks.embeddingStatus.quotaExceededResult')
              ) : (
                <Flexbox gap={4}>
                  {t('FileParsingStatus.chunks.embeddingStatus.errorResult')}
                  {embeddingError && (
                    <Flexbox className={stylex.props(styles.errorReason).className}>
                      [{embeddingError.name}]:{' '}
                      {embeddingError.body && typeof embeddingError.body !== 'string'
                        ? embeddingError.body.detail
                        : embeddingError.body}
                    </Flexbox>
                  )}
                </Flexbox>
              )
            }
          >
            <Tag className={className} color={'error'} variant={'filled'}>
              {t(
                isQuotaExceeded
                  ? 'FileParsingStatus.chunks.embeddingStatus.quotaExceeded'
                  : 'FileParsingStatus.chunks.embeddingStatus.error',
              )}{' '}
              <Icon
                icon={RotateCwIcon}
                style={{ cursor: 'pointer' }}
                title={t('retry', { ns: 'common' })}
                onClick={() => {
                  onErrorClick?.('embedding');
                }}
              />
            </Tag>
          </Tooltip>
        );
      }

      case AsyncTaskStatus.Success: {
        return (
          <Flexbox horizontal>
            <Tooltip
              styles={{ root: { pointerEvents: 'none' } }}
              title={t('FileParsingStatus.chunks.embeddingStatus.success')}
            >
              <Tag
                className={cx('chunk-tag', className)}
                color={'purple'}
                icon={<Icon icon={BoltIcon} />}
                style={{ cursor: 'pointer' }}
                variant={'filled'}
                onClick={() => {
                  onClick?.(AsyncTaskStatus.Success);
                }}
              >
                {chunkCount}
              </Tag>
            </Tooltip>
          </Flexbox>
        );
      }
    }
  },
);

export default EmbeddingStatus;