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.

5.0 KB
Raw Blame
'use client';

import { createStaticStyles, cssVar, Flexbox } from '@lobehub/ui';
import {
  Activity,
  Award,
  BarChart3,
  Gauge,
  LoaderPinwheel,
  Server,
  Target,
  TrendingUp,
  Trophy,
  Volleyball,
  Zap,
} from 'lucide-react';
import { memo, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router';

import AsyncError from '@/components/AsyncError';
import { RouteLoading } from '@/components/Skeleton/RouteSegment';
import BenchmarkLeaderboard from '@/features/LobeBench/BenchmarkLeaderboard';
import { useIsLobeBenchBenchmark } from '@/features/LobeBench/useIsLobeBenchBenchmark';
import { datasetSelectors, runSelectors, useEvalStore } from '@/store/eval';

import BenchmarkHeader from './features/BenchmarkHeader';
import DatasetsTab from './features/DatasetsTab';
import RunsTab from './features/RunsTab';

const SYSTEM_ICONS = [
  LoaderPinwheel,
  Volleyball,
  Server,
  Target,
  Award,
  Trophy,
  Activity,
  BarChart3,
  TrendingUp,
  Gauge,
  Zap,
];

const getSystemIcon = (id: string) => {
  const hash = id.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
  return SYSTEM_ICONS[hash % SYSTEM_ICONS.length];
};

const styles = createStaticStyles(({ css }) => ({
  container: css`
    overflow-y: auto;
    padding-block: 24px;
    padding-inline: 32px;
  `,
  sectionTitle: css`
    margin: 0;
    font-size: ${cssVar.fontSizeLG};
    font-weight: 600;
    color: ${cssVar.colorText};
  `,
  tag: css`
    padding-block: 2px;
    padding-inline: 8px;
    border: 1px solid ${cssVar.colorBorder};
    border-radius: ${cssVar.borderRadiusXS};

    font-size: ${cssVar.fontSizeSM};
    color: ${cssVar.colorTextTertiary};

    background: transparent;
  `,
}));

const BenchmarkDetail = memo(() => {
  const { t } = useTranslation('eval');
  const { benchmarkId } = useParams<{ benchmarkId: string }>();
  const systemIcon = useMemo(
    () => (benchmarkId ? getSystemIcon(benchmarkId) : Server),
    [benchmarkId],
  );

  const useFetchBenchmarkDetail = useEvalStore((s) => s.useFetchBenchmarkDetail);
  const benchmark = useEvalStore((s) =>
    benchmarkId ? s.benchmarkDetailMap[benchmarkId] : undefined,
  );
  const useFetchDatasets = useEvalStore((s) => s.useFetchDatasets);
  const datasets = useEvalStore(datasetSelectors.datasetList(benchmarkId));
  const isLoadingDatasets = useEvalStore(datasetSelectors.isLoadingDatasetList(benchmarkId));
  const refreshDatasets = useEvalStore((s) => s.refreshDatasets);
  const useFetchRuns = useEvalStore((s) => s.useFetchRuns);
  const runList = useEvalStore(runSelectors.runList(benchmarkId));
  const lobeBench = useIsLobeBenchBenchmark(benchmarkId);

  const { error, isLoading, mutate } = useFetchBenchmarkDetail(benchmarkId);
  useFetchDatasets(benchmarkId);

  const handleRefreshDatasets = useCallback(async () => {
    if (benchmarkId) {
      await refreshDatasets(benchmarkId);
    }
  }, [benchmarkId, refreshDatasets]);

  const handleBenchmarkUpdate = useCallback(async () => {
    if (benchmarkId) {
      await refreshDatasets(benchmarkId);
    }
  }, [benchmarkId, refreshDatasets]);

  // Fetch all runs for this benchmark
  useFetchRuns(benchmarkId);

  const completedRuns = runList.filter((r) => r.status === 'completed');

  const totalCases = datasets.reduce((sum, ds) => sum + (ds.testCaseCount || 0), 0);

  if (!benchmark) {
    if (isLoading || !error) return <RouteLoading />;
    return <AsyncError error={error} variant={'page'} onRetry={() => void mutate()} />;
  }

  return (
    <Flexbox className={styles.container} gap={24} height="100%" width="100%">
      {/* Header + Stats */}
      <BenchmarkHeader
        benchmark={benchmark}
        completedRuns={completedRuns}
        datasets={datasets}
        runCount={runList.length}
        systemIcon={systemIcon}
        topLabel={lobeBench ? t('benchmark.detail.stats.topModels') : undefined}
        totalCases={totalCases}
        onBenchmarkUpdate={handleBenchmarkUpdate}
      />

      {/* A Lobe Bench benchmark ranks its contenders here too, as on its Lobe Bench page. */}
      {lobeBench && <BenchmarkLeaderboard benchmarkId={benchmarkId!} />}

      {/* Tags */}
      {(benchmark as any).tags && (benchmark as any).tags.length > 0 && (
        <Flexbox horizontal gap={8} style={{ flexWrap: 'wrap' }}>
          {(benchmark as any).tags.map((tag: string) => (
            <span className={styles.tag} key={tag}>
              {tag}
            </span>
          ))}
        </Flexbox>
      )}

      {/* Datasets Section */}
      <h3 className={styles.sectionTitle}>{t('benchmark.detail.tabs.datasets')}</h3>
      <DatasetsTab
        benchmarkId={benchmarkId!}
        datasets={datasets}
        loading={isLoadingDatasets}
        onImport={() => {}}
        onRefresh={handleRefreshDatasets}
      />

      {/* Evaluations Section */}
      <h3 className={styles.sectionTitle}>{t('benchmark.detail.tabs.runs')}</h3>
      <RunsTab benchmarkId={benchmarkId!} />
    </Flexbox>
  );
});

export default BenchmarkDetail;