import React, { useState, useCallback } from 'react'; import { Box, Flex, Button, Textarea, IconButton, BoxProps } from '@chakra-ui/react'; import { useForm } from 'react-hook-form'; import { insertData2Kb, putKbDataById, delOneKbDataByDataId } from '@/api/plugins/kb'; import { getFileViewUrl } from '@/api/support/file'; import { useToast } from '@/hooks/useToast'; import { getErrText } from '@/utils/tools'; import MyIcon from '@/components/Icon'; import MyModal from '@/components/MyModal'; import MyTooltip from '@/components/MyTooltip'; import { QuestionOutlineIcon } from '@chakra-ui/icons'; import { useQuery } from '@tanstack/react-query'; import { DatasetItemType } from '@/types/plugin'; import { useTranslation } from 'react-i18next'; import { useDatasetStore } from '@/store/dataset'; export type FormData = { dataId?: string } & DatasetItemType; const InputDataModal = ({ onClose, onSuccess, onDelete, kbId, defaultValues = { a: '', q: '' } }: { onClose: () => void; onSuccess: (data: FormData) => void; onDelete?: () => void; kbId: string; defaultValues?: FormData; }) => { const { t } = useTranslation(); const [loading, setLoading] = useState(false); const { toast } = useToast(); const { kbDetail, getKbDetail } = useDatasetStore(); const { getValues, register, handleSubmit, reset } = useForm({ defaultValues }); const maxToken = kbDetail.vectorModel?.maxToken || 2000; /** * 确认导入新数据 */ const sureImportData = useCallback( async (e: FormData) => { if (e.q.length >= maxToken) { toast({ title: '总长度超长了', status: 'warning' }); return; } setLoading(true); try { const data = { dataId: '', a: e.a, q: e.q, source: '手动录入' }; data.dataId = await insertData2Kb({ kbId, data }); toast({ title: '导入数据成功,需要一段时间训练', status: 'success' }); reset({ a: '', q: '' }); onSuccess(data); } catch (err: any) { toast({ title: getErrText(err, '出现了点意外~'), status: 'error' }); } setLoading(false); }, [kbId, maxToken, onSuccess, reset, toast] ); const updateData = useCallback( async (e: FormData) => { if (!e.dataId) return; if (e.a !== defaultValues.a || e.q !== defaultValues.q) { setLoading(true); try { const data = { dataId: e.dataId, kbId, a: e.a, q: e.q === defaultValues.q ? '' : e.q }; await putKbDataById(data); onSuccess(data); } catch (err) { toast({ status: 'error', title: getErrText(err, '更新数据失败') }); } setLoading(false); } toast({ title: '修改数据成功', status: 'success' }); onClose(); }, [defaultValues.a, defaultValues.q, kbId, onClose, onSuccess, toast] ); useQuery(['getKbDetail'], () => { if (kbDetail._id === kbId) return null; return getKbDetail(kbId); }); return ( {'匹配的知识点'}