🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
'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 TestCaseEditContentProps {
formId: string;
onLoadingChange?: (loading: boolean) => void;
onSuccess?: (datasetId: string) => void;
testCase: any;
}
const TestCaseEditContent: FC<TestCaseEditContentProps> = ({
formId,
onLoadingChange,
onSuccess,
testCase,
}) => {
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.updateTestCase({
content: {
category: values.category || undefined,
expected: values.expected,
input: values.input,
},
evalConfig:
(values.evalMode === 'llm-rubric' || values.evalMode === 'rule-checklist') &&
values.evalConfig?.judgePrompt
? values.evalConfig
: null,
evalMode: values.evalMode || null,
id: testCase.id,
metadata: {
...(values.difficulty ? { difficulty: values.difficulty } : {}),
...(tags ? { tags } : {}),
},
});
await onSuccess?.(testCase.datasetId);
toast.success(t('testCase.edit.success'));
close();
} catch (cause) {
console.error('Failed to update eval test case:', cause);
setError(toInlineError(t('testCase.edit.failedRetry'), cause));
} finally {
onLoadingChange?.(false);
}
};
const form = useForm({
initialValues: {
category: testCase.content?.category,
difficulty: testCase.metadata?.difficulty,
evalConfig: { judgePrompt: testCase.evalConfig?.judgePrompt },
evalMode: testCase.evalMode || undefined,
expected: testCase.content?.expected,
input: testCase.content?.input,
tags: testCase.metadata?.tags?.join(', '),
},
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('table.columns.category')}
name="category"
style={{ paddingBlock: 0 }}
>
<Input placeholder={t('dataset.import.categoryDesc')} />
</Form.Field>
<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 TestCaseEditContent;