refactor(knowledge): restructure knowledge creation form and hooks

feat(knowledge): add GeneralForm and ChunkMethodForm components
This commit is contained in:
2025-10-13 18:25:44 +08:00
parent cedb0699f5
commit 34181cf025
9 changed files with 625 additions and 381 deletions

View File

@@ -0,0 +1,294 @@
import React from 'react';
import { type UseFormReturn } from 'react-hook-form';
import {
Box,
Typography,
FormControl,
InputLabel,
Select,
MenuItem,
Grid,
TextField,
FormHelperText,
Button,
CircularProgress,
} from '@mui/material';
import {
Save as SaveIcon,
} from '@mui/icons-material';
import { DOCUMENT_PARSER_TYPES, type DocumentParserType } from '@/constants/knowledge';
// 解析器选项配置
const parserOptions = [
{ value: DOCUMENT_PARSER_TYPES.Naive, label: '通用解析器', description: '适用于大多数文档类型' },
{ value: DOCUMENT_PARSER_TYPES.Qa, label: 'Q&A解析器', description: '适用于问答格式文档' },
{ value: DOCUMENT_PARSER_TYPES.Resume, label: '简历解析器', description: '专门用于解析简历文档' },
{ value: DOCUMENT_PARSER_TYPES.Manual, label: '手册解析器', description: '适用于技术手册和说明书' },
{ value: DOCUMENT_PARSER_TYPES.Table, label: '表格解析器', description: '专门处理表格数据' },
{ value: DOCUMENT_PARSER_TYPES.Paper, label: '论文解析器', description: '适用于学术论文' },
{ value: DOCUMENT_PARSER_TYPES.Book, label: '书籍解析器', description: '适用于书籍和长文档' },
{ value: DOCUMENT_PARSER_TYPES.Laws, label: '法律解析器', description: '专门处理法律文档' },
{ value: DOCUMENT_PARSER_TYPES.Presentation, label: '演示文稿解析器', description: '适用于PPT等演示文档' },
{ value: DOCUMENT_PARSER_TYPES.Picture, label: '图片解析器', description: '处理图片中的文字内容' },
{ value: DOCUMENT_PARSER_TYPES.One, label: '整体解析器', description: '将整个文档作为一个块处理' },
{ value: DOCUMENT_PARSER_TYPES.Audio, label: '音频解析器', description: '处理音频文件转录' },
{ value: DOCUMENT_PARSER_TYPES.Email, label: '邮件解析器', description: '专门处理邮件格式' },
{ value: DOCUMENT_PARSER_TYPES.Tag, label: '标签解析器', description: '基于标签的文档解析' },
{ value: DOCUMENT_PARSER_TYPES.KnowledgeGraph, label: '知识图谱解析器', description: '构建知识图谱结构' },
];
interface ConfigFormData {
parser_id: DocumentParserType;
chunk_token_count?: number;
layout_recognize?: boolean;
task_page_size?: number;
[key: string]: any;
}
interface ChunkMethodFormProps {
form: UseFormReturn<ConfigFormData>;
onSubmit: (data: ConfigFormData) => void;
isSubmitting?: boolean;
onCancel?: () => void;
disabled?: boolean;
submitButtonText?: string;
cancelButtonText?: string;
}
function ChunkMethodForm({
form,
onSubmit,
isSubmitting = false,
onCancel,
disabled = false,
submitButtonText = '提交',
cancelButtonText = '取消'
}: ChunkMethodFormProps) {
const selectedParser: DocumentParserType = form.watch('parser_id');
// 根据选择的解析器显示不同的配置选项
const renderParserSpecificConfig = () => {
switch (selectedParser) {
case DOCUMENT_PARSER_TYPES.Naive:
return (
<Grid container spacing={3}>
<Grid size={{xs:12,sm:6}}>
<TextField
fullWidth
type="number"
label="分块大小"
placeholder="512"
disabled={disabled}
{...form.register('chunk_token_num', {
valueAsNumber: true,
min: {
value: 50,
message: '分块大小不能小于50',
},
max: {
value: 2048,
message: '分块大小不能超过2048',
},
})}
error={!!form.formState.errors.chunk_token_num}
helperText={form.formState.errors.chunk_token_num?.message?.toString() || '设置每个文本块的最大token数量'}
/>
</Grid>
<Grid size={{xs:12,sm:6}}>
<TextField
fullWidth
label="分隔符"
placeholder="\\n"
disabled={disabled}
{...form.register('delimiter')}
helperText="用于分割文本的分隔符"
/>
</Grid>
</Grid>
);
case DOCUMENT_PARSER_TYPES.Table:
return (
<Grid container spacing={3}>
<Grid size={12}>
<Typography variant="body2" color="text.secondary">
</Typography>
</Grid>
</Grid>
);
case DOCUMENT_PARSER_TYPES.KnowledgeGraph:
return (
<Grid container spacing={3}>
<Grid size={12}>
<TextField
fullWidth
label="实体类型"
placeholder="person,organization,location"
disabled={disabled}
{...form.register('entity_types')}
helperText="指定要提取的实体类型,用逗号分隔"
/>
</Grid>
</Grid>
);
case DOCUMENT_PARSER_TYPES.Picture:
return (
<Grid container spacing={3}>
<Grid size={12}>
<TextField
fullWidth
multiline
rows={3}
label="系统提示词"
placeholder="请描述图片中的内容..."
disabled={disabled}
{...form.register('system_prompt')}
helperText="用于指导AI理解图片内容的提示词"
/>
</Grid>
</Grid>
);
default:
return (
<Grid container spacing={3}>
<Grid size={12}>
<Typography variant="body2" color="text.secondary">
使
</Typography>
</Grid>
</Grid>
);
}
};
return (
<Box>
<Typography variant="h6" gutterBottom>
</Typography>
<Grid container spacing={3}>
{/* 解析器选择 */}
<Grid size={12}>
<FormControl fullWidth disabled={disabled}>
<InputLabel></InputLabel>
<Select
label="解析器类型"
defaultValue={DOCUMENT_PARSER_TYPES.Naive}
{...form.register('parser_id')}
value={form.watch('parser_id') || DOCUMENT_PARSER_TYPES.Naive}
>
{parserOptions.map((option) => (
<MenuItem key={option.value} value={option.value}>
<Box>
<Typography variant="body1">{option.label}</Typography>
<Typography variant="caption" color="text.secondary">
{option.description}
</Typography>
</Box>
</MenuItem>
))}
</Select>
<FormHelperText>
</FormHelperText>
</FormControl>
</Grid>
{/* 解析器特定配置 */}
<Grid size={12}>
<Box sx={{ mt: 2 }}>
<Typography variant="subtitle2" gutterBottom>
</Typography>
{renderParserSpecificConfig()}
</Box>
</Grid>
{/* 通用配置 */}
<Grid size={12}>
<Box sx={{ mt: 2 }}>
<Typography variant="subtitle2" gutterBottom>
</Typography>
<Grid container spacing={3}>
<Grid size={{xs:12,sm:6}}>
<TextField
fullWidth
type="number"
label="自动关键词数量"
placeholder="0"
disabled={disabled}
{...form.register('auto_keywords', {
valueAsNumber: true,
min: {
value: 0,
message: '关键词数量不能小于0',
},
max: {
value: 10,
message: '关键词数量不能超过10',
},
})}
error={!!form.formState.errors.auto_keywords}
helperText={form.formState.errors.auto_keywords?.message?.toString() || '自动提取的关键词数量0表示不提取'}
/>
</Grid>
<Grid size={{xs:12,sm:6}}>
<TextField
fullWidth
type="number"
label="自动问题数量"
placeholder="0"
disabled={disabled}
{...form.register('auto_questions', {
valueAsNumber: true,
min: {
value: 0,
message: '问题数量不能小于0',
},
max: {
value: 10,
message: '问题数量不能超过10',
},
})}
error={!!form.formState.errors.auto_questions}
helperText={form.formState.errors.auto_questions?.message?.toString() || '自动生成的问题数量0表示不生成'}
/>
</Grid>
</Grid>
</Box>
</Grid>
{/* 操作按钮 */}
<Grid size={12}>
<Box sx={{ display: 'flex', gap: 2, justifyContent: 'flex-end', mt: 2 }}>
{onCancel && (
<Button
variant="outlined"
onClick={onCancel}
disabled={isSubmitting}
>
{cancelButtonText}
</Button>
)}
<Button
variant="contained"
onClick={form.handleSubmit(onSubmit)}
disabled={disabled || isSubmitting}
startIcon={isSubmitting ? <CircularProgress size={20} /> : <SaveIcon />}
>
{submitButtonText}
</Button>
</Box>
</Grid>
</Grid>
</Box>
);
}
export default ChunkMethodForm;

View File

@@ -0,0 +1,226 @@
import React, { useRef } from 'react';
import { type UseFormReturn } from 'react-hook-form';
import {
Box,
Typography,
TextField,
FormControl,
InputLabel,
Select,
MenuItem,
Grid,
Avatar,
Button,
IconButton,
CircularProgress,
} from '@mui/material';
import {
PhotoCamera as PhotoCameraIcon,
Delete as DeleteIcon,
Save as SaveIcon,
} from '@mui/icons-material';
interface BasicFormData {
name: string;
description: string;
permission: string;
avatar?: string;
}
interface GeneralFormProps {
form: UseFormReturn<BasicFormData>;
onSubmit: (data: BasicFormData) => void;
isSubmitting?: boolean;
onCancel?: () => void;
disabled?: boolean;
submitButtonText?: string;
cancelButtonText?: string;
}
function GeneralForm({
form,
onSubmit,
isSubmitting = false,
onCancel,
disabled = false,
submitButtonText = '提交',
cancelButtonText = '取消'
}: GeneralFormProps) {
const fileInputRef = useRef<HTMLInputElement>(null);
// 处理头像上传
const handleAvatarUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
// 检查文件类型
if (!file.type.startsWith('image/')) {
alert('请选择图片文件');
return;
}
// 检查文件大小 (限制为 2MB)
if (file.size > 2 * 1024 * 1024) {
alert('图片大小不能超过 2MB');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
const base64String = e.target?.result as string;
form.setValue('avatar', base64String);
};
reader.readAsDataURL(file);
}
};
// 删除头像
const handleAvatarDelete = () => {
form.setValue('avatar', undefined);
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
};
const avatarValue = form.watch('avatar');
return (
<Box>
<Typography variant="h6" gutterBottom>
</Typography>
<Grid container spacing={3}>
{/* 头像上传 */}
<Grid size={12}>
<Typography variant="subtitle2" gutterBottom>
</Typography>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 2 }}>
<Avatar
src={avatarValue}
sx={{ width: 80, height: 80 }}
>
{!avatarValue && form.watch('name')?.charAt(0)?.toUpperCase()}
</Avatar>
<Box>
<input
ref={fileInputRef}
type="file"
accept="image/*"
style={{ display: 'none' }}
onChange={handleAvatarUpload}
disabled={disabled}
/>
<Button
variant="outlined"
startIcon={<PhotoCameraIcon />}
onClick={() => fileInputRef.current?.click()}
disabled={disabled}
size="small"
sx={{ mr: 1 }}
>
</Button>
{avatarValue && (
<IconButton
onClick={handleAvatarDelete}
disabled={disabled}
size="small"
color="error"
>
<DeleteIcon />
</IconButton>
)}
</Box>
</Box>
<Typography variant="caption" color="text.secondary">
PNGJPG 2MB
</Typography>
</Grid>
{/* 知识库名称 */}
<Grid size={12}>
<TextField
fullWidth
label="知识库名称"
placeholder="请输入知识库名称"
disabled={disabled}
{...form.register('name', {
required: '请输入知识库名称',
minLength: {
value: 2,
message: '知识库名称至少需要2个字符',
},
maxLength: {
value: 50,
message: '知识库名称不能超过50个字符',
},
})}
error={!!form.formState.errors.name}
helperText={form.formState.errors.name?.message?.toString() || '请输入知识库名称'}
/>
</Grid>
{/* 描述 */}
<Grid size={12}>
<TextField
fullWidth
multiline
rows={4}
label="描述"
placeholder="请输入知识库描述"
disabled={disabled}
{...form.register('description', {
maxLength: {
value: 500,
message: '描述不能超过500个字符',
},
})}
error={!!form.formState.errors.description}
helperText={form.formState.errors.description?.message?.toString() || '请输入知识库描述'}
/>
</Grid>
{/* 权限设置 */}
<Grid size={{xs:12,sm:6}}>
<FormControl fullWidth disabled={disabled}>
<InputLabel></InputLabel>
<Select
label="权限设置"
{...form.register('permission')}
value={form.watch('permission') || 'me'}
>
<MenuItem value="me"></MenuItem>
<MenuItem value="team"></MenuItem>
</Select>
</FormControl>
</Grid>
{/* 操作按钮 */}
<Grid size={12}>
<Box sx={{ display: 'flex', gap: 2, justifyContent: 'flex-end', mt: 2 }}>
{onCancel && (
<Button
variant="outlined"
onClick={onCancel}
disabled={isSubmitting}
>
{cancelButtonText}
</Button>
)}
<Button
variant="contained"
onClick={form.handleSubmit(onSubmit)}
disabled={disabled || isSubmitting}
startIcon={isSubmitting ? <CircularProgress size={20} /> : <SaveIcon />}
>
{submitButtonText}
</Button>
</Box>
</Grid>
</Grid>
</Box>
);
}
export default GeneralForm;