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.

3.3 KB
Raw Blame
'use client';

import { cssVar, Input, TextArea, toast, useModalContext } from '@lobehub/ui';
import { Form, useForm, useWatch } from '@lobehub/ui/form';
import { type FC, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';

import InlineErrorText from '@/components/InlineErrorText';
import { type InlineErrorState, toInlineError } from '@/components/InlineErrorText/errorDetail';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useEvalStore } from '@/store/eval';

const toIdentifier = (name: string) =>
  name
    .trim()
    .toLowerCase()
    .replaceAll(/\s+/g, '-')
    .replaceAll(/[^\da-z-]/g, '');

export interface CreateBenchmarkContentProps {
  formId: string;
  onLoadingChange?: (loading: boolean) => void;
}

const CreateBenchmarkContent: FC<CreateBenchmarkContentProps> = ({ formId, onLoadingChange }) => {
  const { t } = useTranslation('eval');
  const { close } = useModalContext();

  const navigate = useWorkspaceAwareNavigate();
  const [identifierTouched, setIdentifierTouched] = useState(false);
  const createBenchmark = useEvalStore((s) => s.createBenchmark);

  /** Submit failure shown below the form; the modal stays open with the user's input. */
  const [error, setError] = useState<InlineErrorState | null>(null);

  const handleFinish = async (values: any) => {
    setError(null);
    onLoadingChange?.(true);
    try {
      const result = await createBenchmark({
        description: values.description?.trim() || undefined,
        identifier: values.identifier.trim(),
        name: values.name.trim(),
      });
      toast.success(t('benchmark.create.success'));
      close();
      if (result?.id) {
        navigate(`/eval/bench/${result.id}`);
      }
    } catch (cause) {
      console.error('Failed to create eval benchmark:', cause);
      setError(toInlineError(t('benchmark.create.failedRetry'), cause));
    } finally {
      onLoadingChange?.(false);
    }
  };

  const form = useForm({
    onSubmit: handleFinish,
    onValuesChange: () => setError(null),
  });
  const nameValue = useWatch(form, 'name');

  useEffect(() => {
    if (!identifierTouched && nameValue) {
      form.setValue('identifier', toIdentifier(nameValue));
    }
  }, [nameValue, identifierTouched, form]);

  return (
    <Form form={form} id={formId} layout="vertical">
      <Form.Field
        label={t('benchmark.create.name.label')}
        name="name"
        required={t('benchmark.create.nameRequired')}
      >
        <Input autoFocus placeholder={t('benchmark.create.name.placeholder')} />
      </Form.Field>

      <Form.Field
        label={t('benchmark.create.identifier.label')}
        name="identifier"
        required={t('benchmark.create.identifierRequired')}
      >
        <Input
          placeholder={t('benchmark.create.identifier.placeholder')}
          style={{ fontFamily: cssVar.fontFamilyCode }}
          onChange={() => setIdentifierTouched(true)}
        />
      </Form.Field>

      <Form.Field label={t('benchmark.create.description.label')} name="description">
        <TextArea placeholder={t('benchmark.create.description.placeholder')} rows={3} />
      </Form.Field>
      {error && <InlineErrorText detail={error.detail}>{error.summary}</InlineErrorText>}
    </Form>
  );
};

export default CreateBenchmarkContent;