From 99e47849f5d1b54328c76278a0fd04a71c13e4b7 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Mon, 12 Jun 2023 21:59:30 +0800 Subject: [PATCH] perf: kb test --- client/src/api/chat.ts | 1 + client/src/api/plugins/kb.ts | 3 +- .../api/chat/history/updateHistoryQuote.ts | 10 +- .../src/pages/chat/components/QuoteModal.tsx | 8 +- client/src/pages/kb/components/DataCard.tsx | 27 ++-- client/src/pages/kb/components/Detail.tsx | 5 +- client/src/pages/kb/components/Info.tsx | 6 +- .../pages/kb/components/InputDataModal.tsx | 66 ++++++--- client/src/pages/kb/components/Test.tsx | 138 ++++++++++++++---- client/src/store/kb.ts | 14 +- client/src/types/plugin.d.ts | 1 + 11 files changed, 213 insertions(+), 66 deletions(-) diff --git a/client/src/api/chat.ts b/client/src/api/chat.ts index 2abc47c98..f9aee72c7 100644 --- a/client/src/api/chat.ts +++ b/client/src/api/chat.ts @@ -38,6 +38,7 @@ export const updateHistoryQuote = (params: { chatId: string; historyId: string; quoteId: string; + sourceText: string; }) => GET(`/chat/history/updateHistoryQuote`, params); /** diff --git a/client/src/api/plugins/kb.ts b/client/src/api/plugins/kb.ts index ff64df3b3..1ddf3cf01 100644 --- a/client/src/api/plugins/kb.ts +++ b/client/src/api/plugins/kb.ts @@ -2,6 +2,7 @@ import { GET, POST, PUT, DELETE } from '../request'; import type { KbItemType } from '@/types/plugin'; import { RequestPaging } from '@/types/index'; import { TrainingModeEnum } from '@/constants/plugin'; +import { type QuoteItemType } from '@/pages/api/openapi/kb/appKbSearch'; import { Props as PushDataProps, Response as PushDateResponse @@ -59,7 +60,7 @@ export const getTrainingData = (data: { kbId: string; init: boolean }) => }>(`/plugins/kb/data/getTrainingData`, data); export const getKbDataItemById = (dataId: string) => - GET(`/plugins/kb/data/getDataById`, { dataId }); + GET(`/plugins/kb/data/getDataById`, { dataId }); /** * 直接push数据 diff --git a/client/src/pages/api/chat/history/updateHistoryQuote.ts b/client/src/pages/api/chat/history/updateHistoryQuote.ts index 2b2e9aba9..30b698da3 100644 --- a/client/src/pages/api/chat/history/updateHistoryQuote.ts +++ b/client/src/pages/api/chat/history/updateHistoryQuote.ts @@ -6,10 +6,16 @@ import { Types } from 'mongoose'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { - let { chatId, historyId, quoteId } = req.query as { + let { + chatId, + historyId, + quoteId, + sourceText = '' + } = req.query as { chatId: string; historyId: string; quoteId: string; + sourceText: string; }; await connectToDatabase(); @@ -27,7 +33,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) }, { $set: { - 'content.$.quote.$[quoteElem].source': '手动修改' + 'content.$.quote.$[quoteElem].source': sourceText } }, { diff --git a/client/src/pages/chat/components/QuoteModal.tsx b/client/src/pages/chat/components/QuoteModal.tsx index b1510268c..a8246b43c 100644 --- a/client/src/pages/chat/components/QuoteModal.tsx +++ b/client/src/pages/chat/components/QuoteModal.tsx @@ -76,13 +76,14 @@ const QuoteModal = ({ * update kbData, update mongo status and reload quotes */ const updateQuoteStatus = useCallback( - async (quoteId: string) => { + async (quoteId: string, sourceText: string) => { setIsLoading(true); try { await updateHistoryQuote({ chatId, historyId, - quoteId + quoteId, + sourceText }); // reload quote refetch(); @@ -163,7 +164,8 @@ const QuoteModal = ({ {editDataItem && ( setEditDataItem(undefined)} - onSuccess={() => updateQuoteStatus(editDataItem.dataId)} + onSuccess={() => updateQuoteStatus(editDataItem.dataId, '手动修改')} + onDelete={() => updateQuoteStatus(editDataItem.dataId, '已删除')} kbId="" defaultValues={editDataItem} /> diff --git a/client/src/pages/kb/components/DataCard.tsx b/client/src/pages/kb/components/DataCard.tsx index c3f7ce189..d7d3648dd 100644 --- a/client/src/pages/kb/components/DataCard.tsx +++ b/client/src/pages/kb/components/DataCard.tsx @@ -208,7 +208,7 @@ const DataCard = ({ kbId }: { kbId: string }) => { /> @@ -216,7 +216,7 @@ const DataCard = ({ kbId }: { kbId: string }) => { { }) } > - + + + {item.q} + + {item.a} + + {item.source?.trim()} @@ -252,12 +265,6 @@ const DataCard = ({ kbId }: { kbId: string }) => { }} /> - - - {item.q} - - {item.a} - ))} @@ -271,7 +278,7 @@ const DataCard = ({ kbId }: { kbId: string }) => { kbId={kbId} defaultValues={editInputData} onClose={() => setEditInputData(undefined)} - onSuccess={refetchData} + onSuccess={() => refetchData()} /> )} {isOpenSelectFileModal && ( diff --git a/client/src/pages/kb/components/Detail.tsx b/client/src/pages/kb/components/Detail.tsx index 7d749d916..cd0bf0ac2 100644 --- a/client/src/pages/kb/components/Detail.tsx +++ b/client/src/pages/kb/components/Detail.tsx @@ -8,7 +8,7 @@ import { useUserStore } from '@/store/user'; import { KbItemType } from '@/types/plugin'; import { useScreen } from '@/hooks/useScreen'; import { getErrText } from '@/utils/tools'; -import { type ComponentRef } from './Info'; +import Info, { type ComponentRef } from './Info'; import Tabs from '@/components/Tabs'; import dynamic from 'next/dynamic'; import DataCard from './DataCard'; @@ -16,9 +16,6 @@ import DataCard from './DataCard'; const Test = dynamic(() => import('./Test'), { ssr: false }); -const Info = dynamic(() => import('./Info'), { - ssr: false -}); enum TabEnum { data = 'data', diff --git a/client/src/pages/kb/components/Info.tsx b/client/src/pages/kb/components/Info.tsx index 8cfd26018..85e00b961 100644 --- a/client/src/pages/kb/components/Info.tsx +++ b/client/src/pages/kb/components/Info.tsx @@ -214,8 +214,10 @@ const Info = ( aria-label={''} variant={'outline'} size={'sm'} - colorScheme={'red'} - color={'red.500'} + _hover={{ + color: 'red.600', + borderColor: 'red.600' + }} onClick={openConfirm(onclickDelKb)} /> diff --git a/client/src/pages/kb/components/InputDataModal.tsx b/client/src/pages/kb/components/InputDataModal.tsx index d74020b70..14c3092ad 100644 --- a/client/src/pages/kb/components/InputDataModal.tsx +++ b/client/src/pages/kb/components/InputDataModal.tsx @@ -8,19 +8,22 @@ import { ModalContent, ModalHeader, ModalCloseButton, - Textarea + Textarea, + IconButton } from '@chakra-ui/react'; import { useForm } from 'react-hook-form'; -import { postKbDataFromList, putKbDataById } from '@/api/plugins/kb'; +import { postKbDataFromList, putKbDataById, delOneKbDataByDataId } from '@/api/plugins/kb'; import { useToast } from '@/hooks/useToast'; import { TrainingModeEnum } from '@/constants/plugin'; import { getErrText } from '@/utils/tools'; +import MyIcon from '@/components/Icon'; export type FormData = { dataId?: string; a: string; q: string }; const InputDataModal = ({ onClose, onSuccess, + onDelete, kbId, defaultValues = { a: '', @@ -28,7 +31,8 @@ const InputDataModal = ({ } }: { onClose: () => void; - onSuccess: () => void; + onSuccess: (data: FormData) => void; + onDelete?: () => void; kbId: string; defaultValues?: FormData; }) => { @@ -54,16 +58,15 @@ const InputDataModal = ({ setLoading(true); try { + const data = { + a: e.a, + q: e.q, + source: '手动录入' + }; const { insertLen } = await postKbDataFromList({ kbId, mode: TrainingModeEnum.index, - data: [ - { - a: e.a, - q: e.q, - source: '手动录入' - } - ] + data: [data] }); if (insertLen === 0) { @@ -82,7 +85,7 @@ const InputDataModal = ({ }); } - onSuccess(); + onSuccess(data); } catch (err: any) { toast({ title: getErrText(err, '出现了点意外~'), @@ -101,12 +104,13 @@ const InputDataModal = ({ if (e.a !== defaultValues.a || e.q !== defaultValues.q) { setLoading(true); try { - await putKbDataById({ + const data = { dataId: e.dataId, a: e.a, q: e.q === defaultValues.q ? '' : e.q - }); - onSuccess(); + }; + await putKbDataById(data); + onSuccess(data); } catch (error) {} setLoading(false); } @@ -169,9 +173,37 @@ const InputDataModal = ({ - - -