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.

6.0 KB
Raw Blame
'use client';

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

import InlineErrorText from '@/components/InlineErrorText';
import { type InlineErrorState, toInlineError } from '@/components/InlineErrorText/errorDetail';
import { agentEvalService } from '@/services/agentEval';

const styles = createStaticStyles(({ css }) => ({
  sectionLabel: css`
    margin-block-end: 12px;
    font-size: ${cssVar.fontSizeSM};
    font-weight: 500;
    color: ${cssVar.colorTextSecondary};
  `,
}));

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

const TestCaseCreateContent: FC<TestCaseCreateContentProps> = ({
  datasetId,
  formId,
  onLoadingChange,
  onSuccess,
}) => {
  const { t } = useTranslation('eval');
  const { close } = useModalContext();

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

  const handleFinish = async (values: any) => {
    setError(undefined);
    onLoadingChange?.(true);
    try {
      const tags = values.tags
        ? values.tags
            .split(',')
            .map((t: string) => t.trim())
            .filter(Boolean)
        : undefined;

      await agentEvalService.createTestCase({
        content: {
          expected: values.expected,
          input: values.input,
        },
        datasetId,
        evalConfig:
          (values.evalMode === 'llm-rubric' || values.evalMode === 'rule-checklist') &&
          values.evalConfig?.judgePrompt
            ? values.evalConfig
            : undefined,
        evalMode: values.evalMode || undefined,
        metadata: {
          ...(values.difficulty ? { difficulty: values.difficulty } : {}),
          ...(tags ? { tags } : {}),
        },
      });

      setTimeout(() => {
        toast.success(t('testCase.create.success'));
      }, 0);
      close();
      onSuccess?.(datasetId);
    } catch (cause) {
      console.error('Failed to create eval test case:', cause);
      setError(toInlineError(t('testCase.create.failedRetry'), cause));
    } finally {
      onLoadingChange?.(false);
    }
  };

  const form = useForm({ onSubmit: handleFinish, onValuesChange: () => setError(undefined) });
  const evalModeValue = useWatch(form, 'evalMode');

  return (
    <Form form={form} id={formId} layout="vertical">
      <div className={styles.sectionLabel}>{t('caseDetail.section.testCase')}</div>
      <Form.Field required label={t('testCase.create.input.label')} name="input">
        <TextArea
          autoSize={{ maxRows: 6, minRows: 3 }}
          placeholder={t('testCase.create.input.placeholder')}
        />
      </Form.Field>
      <Form.Field
        label={t('testCase.create.expected.label')}
        name="expected"
        required={t('testCase.create.expected.required')}
      >
        <TextArea
          autoSize={{ maxRows: 6, minRows: 2 }}
          placeholder={t('testCase.create.expected.placeholder')}
        />
      </Form.Field>
      <div className={styles.sectionLabel} style={{ marginBlockStart: 4 }}>
        {t('caseDetail.section.scoring')}
      </div>
      <Form.Field label={t('evalMode.label')} name="evalMode">
        <Select
          allowClear
          placeholder={t('evalMode.placeholder')}
          optionRender={(option) => (
            <Flexbox gap={4} style={{ paddingBlock: 4 }}>
              <div>{option.label}</div>
              <Text fontSize={12} type="secondary">
                {t(`evalMode.${option.value}.desc` as any)}
              </Text>
            </Flexbox>
          )}
          options={[
            { label: t('evalMode.equals'), value: 'equals' },
            { label: t('evalMode.contains'), value: 'contains' },
            { label: t('evalMode.llm-rubric'), value: 'llm-rubric' },
            { label: t('evalMode.rule-checklist'), value: 'rule-checklist' },
          ]}
        />
      </Form.Field>
      {(evalModeValue === 'llm-rubric' || evalModeValue === 'rule-checklist') && (
        <Form.Field label={t('evalMode.prompt.label')} name="evalConfig.judgePrompt">
          <TextArea
            autoSize={{ maxRows: 8, minRows: 3 }}
            placeholder={t('evalMode.prompt.placeholder')}
          />
        </Form.Field>
      )}
      <Accordion
        keepMounted
        indicatorPlacement="inline"
        styles={{ header: { paddingBlock: 8, paddingInline: 4 } }}
        items={[
          {
            children: (
              <Flexbox gap={16} style={{ paddingBlockStart: 8 }}>
                <Form.Field
                  label={t('testCase.create.difficulty.label')}
                  name="difficulty"
                  style={{ paddingBlock: 0 }}
                >
                  <Select
                    allowClear
                    placeholder={t('testCase.create.difficulty.label')}
                    options={[
                      { label: t('difficulty.easy'), value: 'easy' },
                      { label: t('difficulty.medium'), value: 'medium' },
                      { label: t('difficulty.hard'), value: 'hard' },
                    ]}
                  />
                </Form.Field>
                <Form.Field
                  label={t('testCase.create.tags.label')}
                  name="tags"
                  style={{ paddingBlock: 0 }}
                >
                  <Input placeholder={t('testCase.create.tags.placeholder')} />
                </Form.Field>
              </Flexbox>
            ),
            key: 'advanced',
            title: t('testCase.create.advanced'),
          },
        ]}
      />
      {error && <InlineErrorText detail={error.detail}>{error.summary}</InlineErrorText>}
    </Form>
  );
};

export default TestCaseCreateContent;