feat: dataset folder
This commit is contained in:
parent
971c9cb291
commit
0b0f184dd1
1
client/public/imgs/files/folder.svg
Normal file
1
client/public/imgs/files/folder.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1694141197423" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4891" xmlns:xlink="http://www.w3.org/1999/xlink" width="64" height="64"><path d="M855.04 385.024q19.456 2.048 38.912 10.24t33.792 23.04 21.504 37.376 2.048 54.272q-2.048 8.192-8.192 40.448t-14.336 74.24-18.432 86.528-19.456 76.288q-5.12 18.432-14.848 37.888t-25.088 35.328-36.864 26.112-51.2 10.24l-567.296 0q-21.504 0-44.544-9.216t-42.496-26.112-31.744-40.96-12.288-53.76l0-439.296q0-62.464 33.792-97.792t95.232-35.328l503.808 0q22.528 0 46.592 8.704t43.52 24.064 31.744 35.84 12.288 44.032l0 11.264-53.248 0q-40.96 0-95.744-0.512t-116.736-0.512-115.712-0.512-92.672-0.512l-47.104 0q-26.624 0-41.472 16.896t-23.04 44.544q-8.192 29.696-18.432 62.976t-18.432 61.952q-10.24 33.792-20.48 65.536-2.048 8.192-2.048 13.312 0 17.408 11.776 29.184t29.184 11.776q31.744 0 43.008-39.936l54.272-198.656q133.12 1.024 243.712 1.024l286.72 0z" fill="#FFCC66" p-id="4892"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.1 KiB |
@ -2,6 +2,10 @@
|
|||||||
"App": "App",
|
"App": "App",
|
||||||
"Cancel": "No",
|
"Cancel": "No",
|
||||||
"Confirm": "Yes",
|
"Confirm": "Yes",
|
||||||
|
"Create New": "Create",
|
||||||
|
"Dataset": "Dataset",
|
||||||
|
"Folder": "Folder",
|
||||||
|
"Name": "Name",
|
||||||
"Running": "Running",
|
"Running": "Running",
|
||||||
"Select value is empty": "Select value is empty",
|
"Select value is empty": "Select value is empty",
|
||||||
"UnKnow": "UnKnow",
|
"UnKnow": "UnKnow",
|
||||||
@ -74,12 +78,14 @@
|
|||||||
"Copy Successful": "Copy Successful",
|
"Copy Successful": "Copy Successful",
|
||||||
"Course": "",
|
"Course": "",
|
||||||
"Delete": "Delete",
|
"Delete": "Delete",
|
||||||
|
"Delete Warning": "Warning",
|
||||||
"Filed is repeat": "Filed is repeated",
|
"Filed is repeat": "Filed is repeated",
|
||||||
"Filed is repeated": "",
|
"Filed is repeated": "",
|
||||||
"Input": "Input",
|
"Input": "Input",
|
||||||
"Output": "Output",
|
"Output": "Output",
|
||||||
"export": "",
|
"Password inconsistency": "Password inconsistency",
|
||||||
"Password inconsistency": "Password inconsistency"
|
"Rename": "Rename",
|
||||||
|
"export": ""
|
||||||
},
|
},
|
||||||
"dataset": {
|
"dataset": {
|
||||||
"Confirm to delete the data": "Confirm to delete the data?",
|
"Confirm to delete the data": "Confirm to delete the data?",
|
||||||
@ -148,6 +154,13 @@
|
|||||||
"desc": "AI knowledge base question and answer platform based on LLM large model",
|
"desc": "AI knowledge base question and answer platform based on LLM large model",
|
||||||
"slogan": "Let the AI know more about you"
|
"slogan": "Let the AI know more about you"
|
||||||
},
|
},
|
||||||
|
"kb": {
|
||||||
|
"Create Folder": "Create Folder",
|
||||||
|
"Edit Folder": "Edit Folder",
|
||||||
|
"Folder Name": "Input folder name",
|
||||||
|
"deleteDatasetTips": "Are you sure to delete the knowledge base? Data cannot be recovered after deletion, please confirm!",
|
||||||
|
"deleteFolderTips": "Are you sure to delete this folder and all the knowledge bases it contains? Data cannot be recovered after deletion, please confirm!"
|
||||||
|
},
|
||||||
"navbar": {
|
"navbar": {
|
||||||
"Account": "Account",
|
"Account": "Account",
|
||||||
"Apps": "Apps",
|
"Apps": "Apps",
|
||||||
|
|||||||
@ -2,6 +2,10 @@
|
|||||||
"App": "应用",
|
"App": "应用",
|
||||||
"Cancel": "取消",
|
"Cancel": "取消",
|
||||||
"Confirm": "确认",
|
"Confirm": "确认",
|
||||||
|
"Create New": "新建",
|
||||||
|
"Dataset": "知识库",
|
||||||
|
"Folder": "文件夹",
|
||||||
|
"Name": "名称",
|
||||||
"Running": "运行中",
|
"Running": "运行中",
|
||||||
"Select value is empty": "选择的内容为空",
|
"Select value is empty": "选择的内容为空",
|
||||||
"UnKnow": "未知",
|
"UnKnow": "未知",
|
||||||
@ -74,12 +78,14 @@
|
|||||||
"Copy Successful": "复制成功",
|
"Copy Successful": "复制成功",
|
||||||
"Course": "",
|
"Course": "",
|
||||||
"Delete": "删除",
|
"Delete": "删除",
|
||||||
|
"Delete Warning": "删除警告",
|
||||||
"Filed is repeat": "",
|
"Filed is repeat": "",
|
||||||
"Filed is repeated": "字段重复了",
|
"Filed is repeated": "字段重复了",
|
||||||
"Input": "输入",
|
"Input": "输入",
|
||||||
"Output": "输出",
|
"Output": "输出",
|
||||||
"export": "",
|
"Password inconsistency": "两次密码不一致",
|
||||||
"Password inconsistency": "两次密码不一致"
|
"Rename": "重命名",
|
||||||
|
"export": ""
|
||||||
},
|
},
|
||||||
"dataset": {
|
"dataset": {
|
||||||
"Confirm to delete the data": "确认删除该数据?",
|
"Confirm to delete the data": "确认删除该数据?",
|
||||||
@ -148,6 +154,13 @@
|
|||||||
"desc": "基于 LLM 大模型的 AI 知识库问答平台",
|
"desc": "基于 LLM 大模型的 AI 知识库问答平台",
|
||||||
"slogan": "让 AI 更懂你的知识"
|
"slogan": "让 AI 更懂你的知识"
|
||||||
},
|
},
|
||||||
|
"kb": {
|
||||||
|
"Create Folder": "创建文件夹",
|
||||||
|
"Edit Folder": "编辑文件夹",
|
||||||
|
"Folder Name": "输入文件夹名称",
|
||||||
|
"deleteDatasetTips": "确认删除该知识库?删除后数据无法恢复,请确认!",
|
||||||
|
"deleteFolderTips": "确认删除该文件夹及其包含的所有知识库?删除后数据无法恢复,请确认!"
|
||||||
|
},
|
||||||
"navbar": {
|
"navbar": {
|
||||||
"Account": "账号",
|
"Account": "账号",
|
||||||
"Apps": "应用",
|
"Apps": "应用",
|
||||||
|
|||||||
@ -16,7 +16,8 @@ import type { KbUpdateParams, CreateKbParams } from '../request/kb';
|
|||||||
import { QuoteItemType } from '@/types/chat';
|
import { QuoteItemType } from '@/types/chat';
|
||||||
|
|
||||||
/* knowledge base */
|
/* knowledge base */
|
||||||
export const getKbList = () => GET<KbListItemType[]>(`/plugins/kb/list`);
|
export const getKbList = (parentId?: string) =>
|
||||||
|
GET<KbListItemType[]>(`/plugins/kb/list`, { parentId });
|
||||||
|
|
||||||
export const getKbById = (id: string) => GET<KbItemType>(`/plugins/kb/detail?id=${id}`);
|
export const getKbById = (id: string) => GET<KbItemType>(`/plugins/kb/detail?id=${id}`);
|
||||||
|
|
||||||
|
|||||||
11
client/src/api/request/kb.d.ts
vendored
11
client/src/api/request/kb.d.ts
vendored
@ -1,12 +1,15 @@
|
|||||||
|
import { KbTypeEnum } from '@/constants/kb';
|
||||||
export type KbUpdateParams = {
|
export type KbUpdateParams = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
tags?: string;
|
||||||
tags: string;
|
name?: string;
|
||||||
avatar: string;
|
avatar?: string;
|
||||||
};
|
};
|
||||||
export type CreateKbParams = {
|
export type CreateKbParams = {
|
||||||
|
parentId?: string;
|
||||||
name: string;
|
name: string;
|
||||||
tags: string[];
|
tags: string[];
|
||||||
avatar: string;
|
avatar: string;
|
||||||
vectorModel: string;
|
vectorModel?: string;
|
||||||
|
type: `${KbTypeEnum}`;
|
||||||
};
|
};
|
||||||
|
|||||||
51
client/src/components/MyMenu/index.tsx
Normal file
51
client/src/components/MyMenu/index.tsx
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { Menu, MenuList, MenuItem } from '@chakra-ui/react';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
width: number;
|
||||||
|
offset?: [number, number];
|
||||||
|
Button: React.ReactNode;
|
||||||
|
menuList: {
|
||||||
|
isActive?: boolean;
|
||||||
|
child: React.ReactNode;
|
||||||
|
onClick: () => void;
|
||||||
|
}[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const MyMenu = ({ width, offset = [0, 10], Button, menuList }: Props) => {
|
||||||
|
const menuItemStyles = {
|
||||||
|
borderRadius: 'sm',
|
||||||
|
py: 3,
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
_hover: {
|
||||||
|
backgroundColor: 'myWhite.600',
|
||||||
|
color: 'hover.blue'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Menu offset={offset} autoSelect={false} isLazy>
|
||||||
|
{Button}
|
||||||
|
<MenuList
|
||||||
|
minW={`${width}px !important`}
|
||||||
|
p={'6px'}
|
||||||
|
border={'1px solid #fff'}
|
||||||
|
boxShadow={'0px 2px 4px rgba(161, 167, 179, 0.25), 0px 0px 1px rgba(121, 141, 159, 0.25);'}
|
||||||
|
>
|
||||||
|
{menuList.map((item, i) => (
|
||||||
|
<MenuItem
|
||||||
|
key={i}
|
||||||
|
{...menuItemStyles}
|
||||||
|
onClick={item.onClick}
|
||||||
|
color={item.isActive ? 'hover.blue' : ''}
|
||||||
|
>
|
||||||
|
{item.child}
|
||||||
|
</MenuItem>
|
||||||
|
))}
|
||||||
|
</MenuList>
|
||||||
|
</Menu>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MyMenu;
|
||||||
@ -14,3 +14,19 @@ export const defaultKbDetail: KbItemType = {
|
|||||||
maxToken: 3000
|
maxToken: 3000
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum KbTypeEnum {
|
||||||
|
folder = 'folder',
|
||||||
|
dataset = 'dataset'
|
||||||
|
}
|
||||||
|
|
||||||
|
export const KbTypeMap = {
|
||||||
|
[KbTypeEnum.folder]: {
|
||||||
|
name: 'folder'
|
||||||
|
},
|
||||||
|
[KbTypeEnum.dataset]: {
|
||||||
|
name: 'dataset'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const FolderAvatarSrc = '/imgs/files/folder.svg';
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useRef } from 'react';
|
import { useCallback, useRef, useState } from 'react';
|
||||||
import {
|
import {
|
||||||
AlertDialog,
|
AlertDialog,
|
||||||
AlertDialogBody,
|
AlertDialogBody,
|
||||||
@ -11,21 +11,25 @@ import {
|
|||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import { useTranslation } from 'next-i18next';
|
import { useTranslation } from 'next-i18next';
|
||||||
|
|
||||||
export const useConfirm = (props: { title?: string; content: string }) => {
|
export const useConfirm = (props: { title?: string | null; content?: string | null }) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { title = t('Warning'), content } = props;
|
const { title = t('Warning'), content } = props;
|
||||||
|
const [customContent, setCustomContent] = useState(content);
|
||||||
|
|
||||||
const { isOpen, onOpen, onClose } = useDisclosure();
|
const { isOpen, onOpen, onClose } = useDisclosure();
|
||||||
|
|
||||||
const cancelRef = useRef(null);
|
const cancelRef = useRef(null);
|
||||||
const confirmCb = useRef<any>();
|
const confirmCb = useRef<any>();
|
||||||
const cancelCb = useRef<any>();
|
const cancelCb = useRef<any>();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
openConfirm: useCallback(
|
openConfirm: useCallback(
|
||||||
(confirm?: any, cancel?: any) => {
|
(confirm?: any, cancel?: any, customContent?: string) => {
|
||||||
confirmCb.current = confirm;
|
confirmCb.current = confirm;
|
||||||
cancelCb.current = cancel;
|
cancelCb.current = cancel;
|
||||||
|
|
||||||
|
customContent && setCustomContent(customContent);
|
||||||
|
|
||||||
return onOpen;
|
return onOpen;
|
||||||
},
|
},
|
||||||
[onOpen]
|
[onOpen]
|
||||||
@ -44,7 +48,7 @@ export const useConfirm = (props: { title?: string; content: string }) => {
|
|||||||
{title}
|
{title}
|
||||||
</AlertDialogHeader>
|
</AlertDialogHeader>
|
||||||
|
|
||||||
<AlertDialogBody>{content}</AlertDialogBody>
|
<AlertDialogBody>{customContent}</AlertDialogBody>
|
||||||
|
|
||||||
<AlertDialogFooter>
|
<AlertDialogFooter>
|
||||||
<Button
|
<Button
|
||||||
@ -70,7 +74,7 @@ export const useConfirm = (props: { title?: string; content: string }) => {
|
|||||||
</AlertDialogOverlay>
|
</AlertDialogOverlay>
|
||||||
</AlertDialog>
|
</AlertDialog>
|
||||||
),
|
),
|
||||||
[content, isOpen, onClose, title]
|
[customContent, isOpen, onClose, t, title]
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback, useRef } from 'react';
|
import React, { useCallback, useRef, useState } from 'react';
|
||||||
import { ModalFooter, ModalBody, Input, useDisclosure, Button } from '@chakra-ui/react';
|
import { ModalFooter, ModalBody, Input, useDisclosure, Button } from '@chakra-ui/react';
|
||||||
import MyModal from '@/components/MyModal';
|
import MyModal from '@/components/MyModal';
|
||||||
|
|
||||||
|
|||||||
62
client/src/pages/api/admin/initv44.ts
Normal file
62
client/src/pages/api/admin/initv44.ts
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { authUser } from '@/service/utils/auth';
|
||||||
|
import { connectToDatabase, KB } from '@/service/mongo';
|
||||||
|
import { KbTypeMap } from '@/constants/kb';
|
||||||
|
|
||||||
|
const limit = 50;
|
||||||
|
let success = 0;
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
await connectToDatabase();
|
||||||
|
await authUser({ req, authRoot: true });
|
||||||
|
|
||||||
|
await initKb();
|
||||||
|
|
||||||
|
jsonRes(res, {});
|
||||||
|
} catch (error) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function initKb(): Promise<any> {
|
||||||
|
try {
|
||||||
|
// 找到所有 type 不存在的 kb
|
||||||
|
const kbList = await KB.find({ type: { $exists: false } }).limit(limit);
|
||||||
|
|
||||||
|
if (kbList.length === 0) return;
|
||||||
|
|
||||||
|
await Promise.allSettled(
|
||||||
|
kbList.map(async (kb) => {
|
||||||
|
let id = '';
|
||||||
|
try {
|
||||||
|
// 创建一组以 kb 的 name,userId 相同文件夹类型的数据
|
||||||
|
const result = await KB.create({
|
||||||
|
parentId: null,
|
||||||
|
userId: kb.userId,
|
||||||
|
avatar: KbTypeMap.folder.avatar,
|
||||||
|
name: kb.name,
|
||||||
|
type: 'folder'
|
||||||
|
});
|
||||||
|
id = result._id;
|
||||||
|
// 将现有的 kb 挂载到这个文件夹下
|
||||||
|
await KB.findByIdAndUpdate(kb._id, {
|
||||||
|
parentId: result._id,
|
||||||
|
type: 'manualData'
|
||||||
|
});
|
||||||
|
console.log(++success);
|
||||||
|
} catch (error) {
|
||||||
|
await KB.findByIdAndDelete(id);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return initKb();
|
||||||
|
} catch (error) {
|
||||||
|
return initKb();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,11 +6,7 @@ import type { CreateKbParams } from '@/api/request/kb';
|
|||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
try {
|
try {
|
||||||
const { name, tags, avatar, vectorModel } = req.body as CreateKbParams;
|
const { name, tags, avatar, vectorModel, parentId, type } = req.body as CreateKbParams;
|
||||||
|
|
||||||
if (!name || !vectorModel) {
|
|
||||||
throw new Error('缺少参数');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 凭证校验
|
// 凭证校验
|
||||||
const { userId } = await authUser({ req, authToken: true });
|
const { userId } = await authUser({ req, authToken: true });
|
||||||
@ -22,7 +18,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
userId,
|
userId,
|
||||||
tags,
|
tags,
|
||||||
vectorModel,
|
vectorModel,
|
||||||
avatar
|
avatar,
|
||||||
|
parentId: parentId || null,
|
||||||
|
type
|
||||||
});
|
});
|
||||||
|
|
||||||
jsonRes(res, { data: _id });
|
jsonRes(res, { data: _id });
|
||||||
|
|||||||
@ -33,9 +33,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
_id: data._id,
|
_id: data._id,
|
||||||
avatar: data.avatar,
|
avatar: data.avatar,
|
||||||
name: data.name,
|
name: data.name,
|
||||||
userId: data.userId,
|
userId: data.userId
|
||||||
vectorModel: getVectorModel(data.vectorModel),
|
|
||||||
tags: data.tags.join(' ')
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@ -2,29 +2,25 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase, KB } from '@/service/mongo';
|
import { connectToDatabase, KB } from '@/service/mongo';
|
||||||
import { authUser } from '@/service/utils/auth';
|
import { authUser } from '@/service/utils/auth';
|
||||||
import { KbListItemType } from '@/types/plugin';
|
|
||||||
import { getVectorModel } from '@/service/utils/data';
|
import { getVectorModel } from '@/service/utils/data';
|
||||||
|
import { KbListItemType } from '@/types/plugin';
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
try {
|
try {
|
||||||
|
const { parentId } = req.query as { parentId: string };
|
||||||
// 凭证校验
|
// 凭证校验
|
||||||
const { userId } = await authUser({ req, authToken: true });
|
const { userId } = await authUser({ req, authToken: true });
|
||||||
|
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
const kbList = await KB.find(
|
const kbList = await KB.find({
|
||||||
{
|
userId,
|
||||||
userId
|
parentId: parentId || null
|
||||||
},
|
}).sort({ updateTime: -1 });
|
||||||
'_id avatar name tags vectorModel'
|
|
||||||
).sort({ updateTime: -1 });
|
|
||||||
|
|
||||||
const data = await Promise.all(
|
const data = await Promise.all(
|
||||||
kbList.map(async (item) => ({
|
kbList.map(async (item) => ({
|
||||||
_id: item._id,
|
...item.toJSON(),
|
||||||
avatar: item.avatar,
|
|
||||||
name: item.name,
|
|
||||||
tags: item.tags,
|
|
||||||
vectorModel: getVectorModel(item.vectorModel)
|
vectorModel: getVectorModel(item.vectorModel)
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import type { KbUpdateParams } from '@/api/request/kb';
|
|||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||||
try {
|
try {
|
||||||
const { id, name, tags, avatar } = req.body as KbUpdateParams;
|
const { id, name, avatar, tags = '' } = req.body as KbUpdateParams;
|
||||||
|
|
||||||
if (!id || !name) {
|
if (!id || !name) {
|
||||||
throw new Error('缺少参数');
|
throw new Error('缺少参数');
|
||||||
@ -23,8 +23,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
userId
|
userId
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
avatar,
|
...(name && { name }),
|
||||||
name,
|
...(avatar && { avatar }),
|
||||||
tags: tags.split(' ').filter((item) => item)
|
tags: tags.split(' ').filter((item) => item)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@ -11,10 +11,9 @@ import {
|
|||||||
Textarea
|
Textarea
|
||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import Avatar from '@/components/Avatar';
|
import Avatar from '@/components/Avatar';
|
||||||
import { KbListItemType } from '@/types/plugin';
|
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { QuestionOutlineIcon } from '@chakra-ui/icons';
|
import { QuestionOutlineIcon } from '@chakra-ui/icons';
|
||||||
import type { SelectedKbType } from '@/types/plugin';
|
import type { KbListItemType, SelectedKbType } from '@/types/plugin';
|
||||||
import { useGlobalStore } from '@/store/global';
|
import { useGlobalStore } from '@/store/global';
|
||||||
import { useToast } from '@/hooks/useToast';
|
import { useToast } from '@/hooks/useToast';
|
||||||
import MySlider from '@/components/Slider';
|
import MySlider from '@/components/Slider';
|
||||||
|
|||||||
@ -18,7 +18,7 @@ import MySelect from '@/components/Select';
|
|||||||
import { QuestionOutlineIcon } from '@chakra-ui/icons';
|
import { QuestionOutlineIcon } from '@chakra-ui/icons';
|
||||||
import Tag from '@/components/Tag';
|
import Tag from '@/components/Tag';
|
||||||
|
|
||||||
const CreateModal = ({ onClose }: { onClose: () => void }) => {
|
const CreateModal = ({ onClose, parentId }: { onClose: () => void; parentId?: string }) => {
|
||||||
const [refresh, setRefresh] = useState(false);
|
const [refresh, setRefresh] = useState(false);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -28,7 +28,9 @@ const CreateModal = ({ onClose }: { onClose: () => void }) => {
|
|||||||
avatar: '/icon/logo.svg',
|
avatar: '/icon/logo.svg',
|
||||||
name: '',
|
name: '',
|
||||||
tags: [],
|
tags: [],
|
||||||
vectorModel: vectorModelList[0].model
|
vectorModel: vectorModelList[0].model,
|
||||||
|
type: 'dataset',
|
||||||
|
parentId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const InputRef = useRef<HTMLInputElement>(null);
|
const InputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|||||||
85
client/src/pages/kb/list/component/EditFolderModal.tsx
Normal file
85
client/src/pages/kb/list/component/EditFolderModal.tsx
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
import React, { useMemo, useRef } from 'react';
|
||||||
|
import { ModalFooter, ModalBody, Input, Button } from '@chakra-ui/react';
|
||||||
|
import MyModal from '@/components/MyModal';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { useRequest } from '@/hooks/useRequest';
|
||||||
|
import { postCreateKb, putKbById } from '@/api/plugins/kb';
|
||||||
|
import { FolderAvatarSrc, KbTypeEnum } from '@/constants/kb';
|
||||||
|
|
||||||
|
const EditFolderModal = ({
|
||||||
|
onClose,
|
||||||
|
onSuccess,
|
||||||
|
id,
|
||||||
|
parentId,
|
||||||
|
name
|
||||||
|
}: {
|
||||||
|
onClose: () => void;
|
||||||
|
onSuccess: () => void;
|
||||||
|
id?: string;
|
||||||
|
parentId?: string;
|
||||||
|
name?: string;
|
||||||
|
}) => {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
const typeMap = useMemo(
|
||||||
|
() =>
|
||||||
|
id
|
||||||
|
? {
|
||||||
|
title: t('kb.Edit Folder')
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
title: t('kb.Create Folder')
|
||||||
|
},
|
||||||
|
[id, t]
|
||||||
|
);
|
||||||
|
|
||||||
|
const { mutate: onSave, isLoading } = useRequest({
|
||||||
|
mutationFn: () => {
|
||||||
|
const val = inputRef.current?.value;
|
||||||
|
if (!val) return Promise.resolve('');
|
||||||
|
if (id) {
|
||||||
|
return putKbById({
|
||||||
|
id,
|
||||||
|
name: val
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return postCreateKb({
|
||||||
|
parentId,
|
||||||
|
name: val,
|
||||||
|
type: KbTypeEnum.folder,
|
||||||
|
avatar: FolderAvatarSrc,
|
||||||
|
tags: []
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onSuccess: (res) => {
|
||||||
|
if (!res) return;
|
||||||
|
onSuccess();
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<MyModal isOpen onClose={onClose} title={typeMap.title}>
|
||||||
|
<ModalBody>
|
||||||
|
<Input
|
||||||
|
ref={inputRef}
|
||||||
|
defaultValue={name}
|
||||||
|
placeholder={t('kb.Folder Name') || ''}
|
||||||
|
autoFocus
|
||||||
|
maxLength={20}
|
||||||
|
/>
|
||||||
|
</ModalBody>
|
||||||
|
<ModalFooter>
|
||||||
|
<Button mr={3} variant={'base'} onClick={onClose}>
|
||||||
|
{t('common.Cancel')}
|
||||||
|
</Button>
|
||||||
|
<Button isLoading={isLoading} onClick={onSave}>
|
||||||
|
{t('Confirm')}
|
||||||
|
</Button>
|
||||||
|
</ModalFooter>
|
||||||
|
</MyModal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditFolderModal;
|
||||||
@ -1,13 +1,14 @@
|
|||||||
import React, { useCallback } from 'react';
|
import React, { useCallback, useRef, useState } from 'react';
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
Card,
|
|
||||||
Flex,
|
Flex,
|
||||||
Grid,
|
Grid,
|
||||||
useTheme,
|
useTheme,
|
||||||
Button,
|
useDisclosure,
|
||||||
|
Card,
|
||||||
IconButton,
|
IconButton,
|
||||||
useDisclosure
|
MenuButton,
|
||||||
|
Image
|
||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { useUserStore } from '@/store/user';
|
import { useUserStore } from '@/store/user';
|
||||||
@ -17,21 +18,34 @@ import { AddIcon } from '@chakra-ui/icons';
|
|||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { useToast } from '@/hooks/useToast';
|
import { useToast } from '@/hooks/useToast';
|
||||||
import { delKbById } from '@/api/plugins/kb';
|
import { delKbById } from '@/api/plugins/kb';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
import Avatar from '@/components/Avatar';
|
import Avatar from '@/components/Avatar';
|
||||||
import MyIcon from '@/components/Icon';
|
import MyIcon from '@/components/Icon';
|
||||||
import Tag from '@/components/Tag';
|
|
||||||
import { serviceSideProps } from '@/utils/i18n';
|
import { serviceSideProps } from '@/utils/i18n';
|
||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
|
import { FolderAvatarSrc, KbTypeEnum } from '@/constants/kb';
|
||||||
|
import Tag from '@/components/Tag';
|
||||||
|
import MyMenu from '@/components/MyMenu';
|
||||||
|
import { getErrText } from '@/utils/tools';
|
||||||
|
|
||||||
const CreateModal = dynamic(() => import('./component/CreateModal'), { ssr: false });
|
const CreateModal = dynamic(() => import('./component/CreateModal'), { ssr: false });
|
||||||
|
const EditFolderModal = dynamic(() => import('./component/EditFolderModal'), { ssr: false });
|
||||||
|
|
||||||
const Kb = () => {
|
const Kb = () => {
|
||||||
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { parentId } = router.query as { parentId: string };
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
|
const DeleteTipsMap = useRef({
|
||||||
|
[KbTypeEnum.folder]: t('kb.deleteFolderTips'),
|
||||||
|
[KbTypeEnum.dataset]: t('kb.deleteDatasetTips')
|
||||||
|
});
|
||||||
|
|
||||||
const { openConfirm, ConfirmModal } = useConfirm({
|
const { openConfirm, ConfirmModal } = useConfirm({
|
||||||
title: '删除提示',
|
title: t('common.Delete Warning'),
|
||||||
content: '确认删除该知识库?知识库相关的文件、记录将永久删除,无法恢复!'
|
content: ''
|
||||||
});
|
});
|
||||||
const { myKbList, loadKbList, setKbList } = useUserStore();
|
const { myKbList, loadKbList, setKbList } = useUserStore();
|
||||||
|
|
||||||
@ -40,8 +54,12 @@ const Kb = () => {
|
|||||||
onOpen: onOpenCreateModal,
|
onOpen: onOpenCreateModal,
|
||||||
onClose: onCloseCreateModal
|
onClose: onCloseCreateModal
|
||||||
} = useDisclosure();
|
} = useDisclosure();
|
||||||
|
const [editFolderData, setEditFolderData] = useState<{
|
||||||
|
id?: string;
|
||||||
|
name?: string;
|
||||||
|
}>();
|
||||||
|
|
||||||
const { refetch } = useQuery(['loadKbList'], () => loadKbList());
|
const { refetch } = useQuery(['loadKbList', parentId], () => loadKbList(parentId));
|
||||||
|
|
||||||
/* 点击删除 */
|
/* 点击删除 */
|
||||||
const onclickDelKb = useCallback(
|
const onclickDelKb = useCallback(
|
||||||
@ -55,7 +73,7 @@ const Kb = () => {
|
|||||||
setKbList(myKbList.filter((item) => item._id !== id));
|
setKbList(myKbList.filter((item) => item._id !== id));
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
toast({
|
toast({
|
||||||
title: err?.message || '删除失败',
|
title: getErrText(err, '删除失败'),
|
||||||
status: 'error'
|
status: 'error'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -69,9 +87,49 @@ const Kb = () => {
|
|||||||
<Box flex={1} className="textlg" letterSpacing={1} fontSize={'24px'} fontWeight={'bold'}>
|
<Box flex={1} className="textlg" letterSpacing={1} fontSize={'24px'} fontWeight={'bold'}>
|
||||||
我的知识库
|
我的知识库
|
||||||
</Box>
|
</Box>
|
||||||
<Button leftIcon={<AddIcon />} variant={'base'} onClick={onOpenCreateModal}>
|
<MyMenu
|
||||||
新建
|
offset={[-30, 10]}
|
||||||
</Button>
|
width={120}
|
||||||
|
Button={
|
||||||
|
<MenuButton
|
||||||
|
_hover={{
|
||||||
|
color: 'myBlue.600'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Flex
|
||||||
|
alignItems={'center'}
|
||||||
|
border={theme.borders.base}
|
||||||
|
px={5}
|
||||||
|
py={2}
|
||||||
|
borderRadius={'md'}
|
||||||
|
cursor={'pointer'}
|
||||||
|
>
|
||||||
|
<AddIcon mr={2} />
|
||||||
|
<Box>{t('Create New')}</Box>
|
||||||
|
</Flex>
|
||||||
|
</MenuButton>
|
||||||
|
}
|
||||||
|
menuList={[
|
||||||
|
{
|
||||||
|
child: (
|
||||||
|
<Flex>
|
||||||
|
<Image src={FolderAvatarSrc} alt={''} w={'20px'} mr={1} />
|
||||||
|
{t('Folder')}
|
||||||
|
</Flex>
|
||||||
|
),
|
||||||
|
onClick: () => setEditFolderData({})
|
||||||
|
},
|
||||||
|
{
|
||||||
|
child: (
|
||||||
|
<Flex>
|
||||||
|
<Image src={'/imgs/module/db.png'} alt={''} w={'20px'} mr={1} />
|
||||||
|
{t('Dataset')}
|
||||||
|
</Flex>
|
||||||
|
),
|
||||||
|
onClick: onOpenCreateModal
|
||||||
|
}
|
||||||
|
]}
|
||||||
|
/>
|
||||||
</Flex>
|
</Flex>
|
||||||
<Grid
|
<Grid
|
||||||
p={5}
|
p={5}
|
||||||
@ -86,7 +144,7 @@ const Kb = () => {
|
|||||||
py={4}
|
py={4}
|
||||||
px={5}
|
px={5}
|
||||||
cursor={'pointer'}
|
cursor={'pointer'}
|
||||||
h={'140px'}
|
h={'130px'}
|
||||||
border={theme.borders.md}
|
border={theme.borders.md}
|
||||||
boxShadow={'none'}
|
boxShadow={'none'}
|
||||||
userSelect={'none'}
|
userSelect={'none'}
|
||||||
@ -98,14 +156,23 @@ const Kb = () => {
|
|||||||
display: 'block'
|
display: 'block'
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onClick={() =>
|
onClick={() => {
|
||||||
|
if (kb.type === KbTypeEnum.folder) {
|
||||||
|
router.push({
|
||||||
|
pathname: '/kb/list',
|
||||||
|
query: {
|
||||||
|
parentId: kb._id
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (kb.type === KbTypeEnum.dataset) {
|
||||||
router.push({
|
router.push({
|
||||||
pathname: '/kb/detail',
|
pathname: '/kb/detail',
|
||||||
query: {
|
query: {
|
||||||
kbId: kb._id
|
kbId: kb._id
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Flex alignItems={'center'} h={'38px'}>
|
<Flex alignItems={'center'} h={'38px'}>
|
||||||
<Avatar src={kb.avatar} borderRadius={'lg'} w={'28px'} />
|
<Avatar src={kb.avatar} borderRadius={'lg'} w={'28px'} />
|
||||||
@ -126,7 +193,11 @@ const Kb = () => {
|
|||||||
}}
|
}}
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
openConfirm(() => onclickDelKb(kb._id))();
|
openConfirm(
|
||||||
|
() => onclickDelKb(kb._id),
|
||||||
|
undefined,
|
||||||
|
DeleteTipsMap.current[kb.type]
|
||||||
|
)();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Flex>
|
</Flex>
|
||||||
@ -140,8 +211,14 @@ const Kb = () => {
|
|||||||
</Flex>
|
</Flex>
|
||||||
</Box>
|
</Box>
|
||||||
<Flex justifyContent={'flex-end'} alignItems={'center'} fontSize={'sm'}>
|
<Flex justifyContent={'flex-end'} alignItems={'center'} fontSize={'sm'}>
|
||||||
|
{kb.type === KbTypeEnum.folder ? (
|
||||||
|
<Box color={'myGray.500'}>{t('Folder')}</Box>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
<MyIcon mr={1} name="kbTest" w={'12px'} />
|
<MyIcon mr={1} name="kbTest" w={'12px'} />
|
||||||
<Box color={'myGray.500'}>{kb.vectorModel.name}</Box>
|
<Box color={'myGray.500'}>{kb.vectorModel.name}</Box>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</Flex>
|
</Flex>
|
||||||
</Card>
|
</Card>
|
||||||
))}
|
))}
|
||||||
@ -155,7 +232,15 @@ const Kb = () => {
|
|||||||
</Flex>
|
</Flex>
|
||||||
)}
|
)}
|
||||||
<ConfirmModal />
|
<ConfirmModal />
|
||||||
{isOpenCreateModal && <CreateModal onClose={onCloseCreateModal} />}
|
{isOpenCreateModal && <CreateModal onClose={onCloseCreateModal} parentId={parentId} />}
|
||||||
|
{!!editFolderData && (
|
||||||
|
<EditFolderModal
|
||||||
|
onClose={() => setEditFolderData(undefined)}
|
||||||
|
onSuccess={refetch}
|
||||||
|
parentId={parentId}
|
||||||
|
{...editFolderData}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</PageContainer>
|
</PageContainer>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,7 +1,13 @@
|
|||||||
import { Schema, model, models, Model } from 'mongoose';
|
import { Schema, model, models, Model } from 'mongoose';
|
||||||
import { kbSchema as SchemaType } from '@/types/mongoSchema';
|
import { kbSchema as SchemaType } from '@/types/mongoSchema';
|
||||||
|
import { KbTypeMap } from '@/constants/kb';
|
||||||
|
|
||||||
const kbSchema = new Schema({
|
const kbSchema = new Schema({
|
||||||
|
parentId: {
|
||||||
|
type: Schema.Types.ObjectId,
|
||||||
|
ref: 'kb',
|
||||||
|
default: null
|
||||||
|
},
|
||||||
userId: {
|
userId: {
|
||||||
type: Schema.Types.ObjectId,
|
type: Schema.Types.ObjectId,
|
||||||
ref: 'user',
|
ref: 'user',
|
||||||
@ -24,6 +30,11 @@ const kbSchema = new Schema({
|
|||||||
required: true,
|
required: true,
|
||||||
default: 'text-embedding-ada-002'
|
default: 'text-embedding-ada-002'
|
||||||
},
|
},
|
||||||
|
type: {
|
||||||
|
type: String,
|
||||||
|
enum: Object.keys(KbTypeMap),
|
||||||
|
required: true
|
||||||
|
},
|
||||||
tags: {
|
tags: {
|
||||||
type: [String],
|
type: [String],
|
||||||
default: []
|
default: []
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import { getTokenLogin, putUserInfo } from '@/api/user';
|
|||||||
import { defaultApp } from '@/constants/model';
|
import { defaultApp } from '@/constants/model';
|
||||||
import { AppListItemType, AppUpdateParams } from '@/types/app';
|
import { AppListItemType, AppUpdateParams } from '@/types/app';
|
||||||
import type { KbItemType, KbListItemType } from '@/types/plugin';
|
import type { KbItemType, KbListItemType } from '@/types/plugin';
|
||||||
import { getKbList, getKbById } from '@/api/plugins/kb';
|
import { getKbList, getKbById, putKbById } from '@/api/plugins/kb';
|
||||||
import { defaultKbDetail } from '@/constants/kb';
|
import { defaultKbDetail } from '@/constants/kb';
|
||||||
import type { AppSchema } from '@/types/mongoSchema';
|
import type { AppSchema } from '@/types/mongoSchema';
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ type State = {
|
|||||||
clearAppModules(): void;
|
clearAppModules(): void;
|
||||||
// kb
|
// kb
|
||||||
myKbList: KbListItemType[];
|
myKbList: KbListItemType[];
|
||||||
loadKbList: () => Promise<any>;
|
loadKbList: (parentId: string) => Promise<any>;
|
||||||
setKbList(val: KbListItemType[]): void;
|
setKbList(val: KbListItemType[]): void;
|
||||||
kbDetail: KbItemType;
|
kbDetail: KbItemType;
|
||||||
getKbDetail: (id: string, init?: boolean) => Promise<KbItemType>;
|
getKbDetail: (id: string, init?: boolean) => Promise<KbItemType>;
|
||||||
@ -108,14 +108,14 @@ export const useUserStore = create<State>()(
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
myKbList: [],
|
myKbList: [],
|
||||||
async loadKbList() {
|
async loadKbList(parentId) {
|
||||||
const res = await getKbList();
|
const res = await getKbList(parentId);
|
||||||
set((state) => {
|
set((state) => {
|
||||||
state.myKbList = res;
|
state.myKbList = res;
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
setKbList(val: KbListItemType[]) {
|
setKbList(val) {
|
||||||
set((state) => {
|
set((state) => {
|
||||||
state.myKbList = val;
|
state.myKbList = val;
|
||||||
});
|
});
|
||||||
|
|||||||
7
client/src/types/mongoSchema.d.ts
vendored
7
client/src/types/mongoSchema.d.ts
vendored
@ -6,6 +6,7 @@ import { TrainingModeEnum } from '@/constants/plugin';
|
|||||||
import type { AppModuleItemType } from './app';
|
import type { AppModuleItemType } from './app';
|
||||||
import { ChatSourceEnum, OutLinkTypeEnum } from '@/constants/chat';
|
import { ChatSourceEnum, OutLinkTypeEnum } from '@/constants/chat';
|
||||||
import { AppTypeEnum } from '@/constants/app';
|
import { AppTypeEnum } from '@/constants/app';
|
||||||
|
import { KbTypeEnum } from '@/constants/kb';
|
||||||
|
|
||||||
export interface UserModelSchema {
|
export interface UserModelSchema {
|
||||||
_id: string;
|
_id: string;
|
||||||
@ -166,15 +167,17 @@ export interface OutLinkSchema {
|
|||||||
type: `${OutLinkTypeEnum}`;
|
type: `${OutLinkTypeEnum}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface kbSchema {
|
export type kbSchema = {
|
||||||
_id: string;
|
_id: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
|
parentId: string;
|
||||||
updateTime: Date;
|
updateTime: Date;
|
||||||
avatar: string;
|
avatar: string;
|
||||||
name: string;
|
name: string;
|
||||||
vectorModel: string;
|
vectorModel: string;
|
||||||
tags: string[];
|
tags: string[];
|
||||||
}
|
type: `${KbTypeEnum}`;
|
||||||
|
};
|
||||||
|
|
||||||
export interface informSchema {
|
export interface informSchema {
|
||||||
_id: string;
|
_id: string;
|
||||||
|
|||||||
7
client/src/types/plugin.d.ts
vendored
7
client/src/types/plugin.d.ts
vendored
@ -3,13 +3,10 @@ import type { kbSchema } from './mongoSchema';
|
|||||||
|
|
||||||
export type SelectedKbType = { kbId: string; vectorModel: VectorModelItemType }[];
|
export type SelectedKbType = { kbId: string; vectorModel: VectorModelItemType }[];
|
||||||
|
|
||||||
export type KbListItemType = {
|
export type KbListItemType = Omit<kbSchema, 'vectorModel'> & {
|
||||||
_id: string;
|
|
||||||
avatar: string;
|
|
||||||
name: string;
|
|
||||||
tags: string[];
|
|
||||||
vectorModel: VectorModelItemType;
|
vectorModel: VectorModelItemType;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* kb type */
|
/* kb type */
|
||||||
export interface KbItemType {
|
export interface KbItemType {
|
||||||
_id: string;
|
_id: string;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user