This commit is contained in:
Archer
2023-09-26 14:31:37 +08:00
committed by GitHub
parent 38d4db5d5f
commit f6552d0d4f
48 changed files with 536 additions and 399 deletions

View File

@@ -1,58 +0,0 @@
// 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, Chat } from '@/service/mongo';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await authUser({ req, authRoot: true });
await connectToDatabase();
const { limit = 1000 } = req.body as { limit: number };
let skip = 0;
const total = await Chat.countDocuments({
chatId: { $exists: false }
});
let promise = Promise.resolve();
console.log(total);
for (let i = 0; i < total; i += limit) {
const skipVal = skip;
skip += limit;
promise = promise
.then(() => init(limit, skipVal))
.then(() => {
console.log(skipVal);
});
}
await promise;
jsonRes(res, {});
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}
async function init(limit: number, skip: number) {
// 遍历 app
const chats = await Chat.find(
{
chatId: { $exists: false }
},
'_id'
).limit(limit);
await Promise.all(
chats.map((chat) =>
Chat.findByIdAndUpdate(chat._id, {
chatId: String(chat._id),
source: 'online'
})
)
);
}

View File

@@ -1,98 +0,0 @@
// 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, Chat, ChatItem } from '@/service/mongo';
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 24);
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await authUser({ req, authRoot: true });
await connectToDatabase();
const { limit = 100 } = req.body as { limit: number };
let skip = 0;
const total = await Chat.countDocuments({
content: { $exists: true, $not: { $size: 0 } },
isInit: { $ne: true }
});
const totalChat = await Chat.aggregate([
{
$project: {
contentLength: { $size: '$content' }
}
},
{
$group: {
_id: null,
totalLength: { $sum: '$contentLength' }
}
}
]);
console.log('chatLen:', total, totalChat);
let promise = Promise.resolve();
for (let i = 0; i < total; i += limit) {
const skipVal = skip;
skip += limit;
promise = promise
.then(() => init(limit))
.then(() => {
console.log(skipVal);
});
}
await promise;
jsonRes(res, {});
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}
async function init(limit: number) {
// 遍历 app
const chats = await Chat.find(
{
content: { $exists: true, $not: { $size: 0 } },
isInit: { $ne: true }
},
'_id userId appId chatId content'
)
.sort({ updateTime: -1 })
.limit(limit);
await Promise.all(
chats.map(async (chat) => {
const inserts = chat.content
.map((item) => ({
dataId: nanoid(),
chatId: chat.chatId,
userId: chat.userId,
appId: chat.appId,
obj: item.obj,
value: item.value,
responseData: item.responseData
}))
.filter((item) => item.chatId && item.userId && item.appId && item.obj && item.value);
try {
await Promise.all(inserts.map((item) => ChatItem.create(item)));
await Chat.findByIdAndUpdate(chat._id, {
isInit: true
});
} catch (error) {
console.log(error);
await ChatItem.deleteMany({ chatId: chat.chatId });
}
})
);
}

View File

@@ -1,27 +0,0 @@
// 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, OutLink } from '@/service/mongo';
import { OutLinkTypeEnum } from '@/constants/chat';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await authUser({ req, authRoot: true });
await connectToDatabase();
await OutLink.updateMany(
{},
{
$set: { type: OutLinkTypeEnum.share }
}
);
jsonRes(res, {});
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}

View File

@@ -0,0 +1,104 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { authUser } from '@/service/utils/auth';
import { connectToDatabase, App } from '@/service/mongo';
import { FlowInputItemTypeEnum, FlowModuleTypeEnum } from '@/constants/flow';
import { SystemInputEnum } from '@/constants/app';
const limit = 300;
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await connectToDatabase();
await authUser({ req, authRoot: true });
const totalApps = await App.countDocuments();
// init app
await App.updateMany({}, { $set: { inited: false } });
for (let i = 0; i < totalApps; i += limit) {
await initVariable();
console.log(i + limit);
}
jsonRes(res, {
data: {
total: totalApps
}
});
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}
async function initVariable(): Promise<any> {
try {
const apps = await App.find({ inited: false }).limit(limit);
await Promise.all(
apps.map(async (app) => {
const jsonAPP = app.toObject();
// @ts-ignore
app.inited = true;
const modules = jsonAPP.modules;
// 找到 variable
const variable = modules.find((item) => item.flowType === FlowModuleTypeEnum.variable);
if (!variable) return await app.save();
// 找到 guide 模块
const userGuideModule = modules.find(
(item) => item.flowType === FlowModuleTypeEnum.userGuide
);
if (userGuideModule) {
userGuideModule.inputs = [
userGuideModule.inputs[0],
{
key: SystemInputEnum.variables,
type: FlowInputItemTypeEnum.systemInput,
label: '对话框变量',
value: variable.inputs[0]?.value
}
];
} else {
modules.unshift({
moduleId: 'userGuide',
flowType: FlowModuleTypeEnum.userGuide,
name: '用户引导',
position: {
x: 447.98520778293346,
y: 721.4016845336229
},
inputs: [
{
key: SystemInputEnum.welcomeText,
type: FlowInputItemTypeEnum.input,
label: '开场白'
},
{
key: SystemInputEnum.variables,
type: FlowInputItemTypeEnum.systemInput,
label: '对话框变量',
value: variable.inputs[0]?.value
}
],
outputs: []
});
}
jsonAPP.modules = jsonAPP.modules.filter(
(item) => item.flowType !== FlowModuleTypeEnum.variable
);
app.modules = JSON.parse(JSON.stringify(jsonAPP.modules));
await app.save();
})
);
} catch (error) {
return initVariable();
}
}

View File

@@ -6,7 +6,7 @@ import { authUser } from '@/service/utils/auth';
import { ChatItemType } from '@/types/chat';
import { authApp } from '@/service/utils/auth';
import type { ChatSchema } from '@/types/mongoSchema';
import { getSpecialModule, getChatModelNameList } from '@/components/ChatBox/utils';
import { getGuideModules, getChatModelNameList } from '@/components/ChatBox/utils';
import { TaskResponseKeyEnum } from '@/constants/chat';
/* 初始化我的聊天框,需要身份验证 */
@@ -81,7 +81,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
chatId,
appId,
app: {
...getSpecialModule(app.modules),
...getGuideModules(app.modules),
chatModels: getChatModelNameList(app.modules),
name: app.name,
avatar: app.avatar,

View File

@@ -18,7 +18,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await collection.updateMany(
{
_id: { $in: fileIds.map((id) => new Types.ObjectId(id)) },
_id: { $in: fileIds.filter((id) => !!id).map((id) => new Types.ObjectId(id)) },
['metadata.userId']: userId
},
{

View File

@@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { authBalanceByUid, authUser } from '@/service/utils/auth';
import { withNextCors } from '@/service/utils/tools';
import { getAIChatApi, axiosConfig } from '@/service/lib/openai';
import { getAIChatApi, axiosConfig } from '@fastgpt/core/aiApi/config';
import { pushGenerateVectorBill } from '@/service/common/bill/push';
type Props = {

View File

@@ -14,12 +14,14 @@ import {
dispatchContentExtract,
dispatchHttpRequest
} from '@/service/moduleDispatch';
import type { CreateChatCompletionRequest } from 'openai';
import type {
CreateChatCompletionRequest,
ChatCompletionRequestMessage
} from '@fastgpt/core/aiApi/type';
import { gptMessage2ChatType, textAdaptGptResponse } from '@/utils/adapt';
import { getChatHistory } from './getHistory';
import { saveChat } from '@/service/utils/chat/saveChat';
import { sseResponse } from '@/service/utils/tools';
import { type ChatCompletionRequestMessage } from 'openai';
import { TaskResponseKeyEnum } from '@/constants/chat';
import { FlowModuleTypeEnum, initModuleType } from '@/constants/flow';
import { AppModuleItemType, RunningModuleItemType } from '@/types/app';

View File

@@ -4,7 +4,7 @@ import { connectToDatabase, OutLink, User } from '@/service/mongo';
import type { InitShareChatResponse } from '@/api/response/chat';
import { authApp } from '@/service/utils/auth';
import { HUMAN_ICON } from '@/constants/chat';
import { getChatModelNameList, getSpecialModule } from '@/components/ChatBox/utils';
import { getChatModelNameList, getGuideModules } from '@/components/ChatBox/utils';
import { authShareChatInit } from '@/service/support/outLink/auth';
/* init share chat window */
@@ -46,7 +46,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
data: {
userAvatar: user?.avatar || HUMAN_ICON,
app: {
...getSpecialModule(app.modules),
...getGuideModules(app.modules),
chatModels: getChatModelNameList(app.modules),
name: app.name,
avatar: app.avatar,

View File

@@ -5,7 +5,7 @@ import { User } from '@/service/models/user';
import { connectToDatabase } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { UserUpdateParams } from '@/types/user';
import { axiosConfig, getAIChatApi, openaiBaseUrl } from '@/service/lib/openai';
import { axiosConfig, getAIChatApi, openaiBaseUrl } from '@fastgpt/core/aiApi/config';
/* update user info */
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
@@ -34,7 +34,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
...axiosConfig(openaiAccount)
}
);
if (!response?.data?.choices?.[0]?.message?.content) {
if (response?.data?.choices?.[0]?.message?.content === undefined) {
throw new Error(JSON.stringify(response?.data));
}
}

View File

@@ -15,7 +15,7 @@ import { streamFetch } from '@/api/fetch';
import MyTooltip from '@/components/MyTooltip';
import { useUserStore } from '@/store/user';
import ChatBox, { type ComponentRef, type StartChatFnProps } from '@/components/ChatBox';
import { getSpecialModule } from '@/components/ChatBox/utils';
import { getGuideModules } from '@/components/ChatBox/utils';
export type ChatTestComponentRef = {
resetChatTest: () => void;
@@ -114,7 +114,7 @@ const ChatTest = (
appAvatar={app.avatar}
userAvatar={userInfo?.avatar}
showMarkIcon
{...getSpecialModule(modules)}
{...getGuideModules(modules)}
onStartChat={startChat}
onDelMessage={() => {}}
/>

View File

@@ -1,17 +1,49 @@
import React, { useMemo } from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import { NodeProps } from 'reactflow';
import { Box, Flex, Textarea } from '@chakra-ui/react';
import {
Box,
Flex,
Textarea,
Button,
Table,
Thead,
Tbody,
Tr,
Th,
Td,
TableContainer
} from '@chakra-ui/react';
import { QuestionOutlineIcon } from '@chakra-ui/icons';
import NodeCard from '../modules/NodeCard';
import { FlowModuleItemType } from '@/types/flow';
import Container from '../modules/Container';
import { SystemInputEnum } from '@/constants/app';
import { welcomeTextTip, variableTip } from '@/constants/flow/ModuleTemplate';
import VariableEditModal, { addVariable } from '../../../VariableEditModal';
import MyIcon from '@/components/Icon';
import MyTooltip from '@/components/MyTooltip';
import { welcomeTextTip } from '@/constants/flow/ModuleTemplate';
import Container from '../modules/Container';
import NodeCard from '../modules/NodeCard';
import { VariableItemType } from '@/types/app';
const NodeUserGuide = ({ data }: NodeProps<FlowModuleItemType>) => {
return (
<>
<NodeCard minW={'300px'} {...data}>
<Container borderTop={'2px solid'} borderTopColor={'myGray.200'}>
<WelcomeText data={data} />
<Box mt={3}>
<ChatStartVariable data={data} />
</Box>
</Container>
</NodeCard>
</>
);
};
export default React.memo(NodeUserGuide);
export function WelcomeText({ data }: { data: FlowModuleItemType }) {
const { inputs, moduleId, onChangeNode } = data;
const welcomeText = useMemo(
() => inputs.find((item) => item.key === SystemInputEnum.welcomeText),
[inputs]
@@ -19,41 +51,148 @@ const NodeUserGuide = ({ data }: NodeProps<FlowModuleItemType>) => {
return (
<>
<NodeCard minW={'300px'} {...data}>
<Container borderTop={'2px solid'} borderTopColor={'myGray.200'}>
<>
<Flex mb={1} alignItems={'center'}>
<MyIcon name={'welcomeText'} mr={2} w={'16px'} color={'#E74694'} />
<Box></Box>
<MyTooltip label={welcomeTextTip} forceShow>
<QuestionOutlineIcon display={['none', 'inline']} ml={1} />
</MyTooltip>
</Flex>
{welcomeText && (
<Textarea
className="nodrag"
rows={6}
resize={'both'}
defaultValue={welcomeText.value}
bg={'myWhite.500'}
placeholder={welcomeTextTip}
onChange={(e) => {
onChangeNode({
moduleId,
key: SystemInputEnum.welcomeText,
type: 'inputs',
value: {
...welcomeText,
value: e.target.value
}
});
}}
/>
)}
</>
</Container>
</NodeCard>
<Flex mb={1} alignItems={'center'}>
<MyIcon name={'welcomeText'} mr={2} w={'16px'} color={'#E74694'} />
<Box></Box>
<MyTooltip label={welcomeTextTip} forceShow>
<QuestionOutlineIcon display={['none', 'inline']} ml={1} />
</MyTooltip>
</Flex>
{welcomeText && (
<Textarea
className="nodrag"
rows={6}
resize={'both'}
defaultValue={welcomeText.value}
bg={'myWhite.500'}
placeholder={welcomeTextTip}
onChange={(e) => {
onChangeNode({
moduleId,
key: SystemInputEnum.welcomeText,
type: 'inputs',
value: {
...welcomeText,
value: e.target.value
}
});
}}
/>
)}
</>
);
};
export default React.memo(NodeUserGuide);
}
function ChatStartVariable({ data }: { data: FlowModuleItemType }) {
const { inputs, moduleId, onChangeNode } = data;
const variables = useMemo(
() =>
(inputs.find((item) => item.key === SystemInputEnum.variables)
?.value as VariableItemType[]) || [],
[inputs]
);
const [editVariable, setEditVariable] = useState<VariableItemType>();
const updateVariables = useCallback(
(value: VariableItemType[]) => {
onChangeNode({
moduleId,
key: SystemInputEnum.variables,
type: 'inputs',
value: {
...inputs.find((item) => item.key === SystemInputEnum.variables),
value
}
});
},
[inputs, onChangeNode, moduleId]
);
const onclickSubmit = useCallback(
({ variable }: { variable: VariableItemType }) => {
updateVariables(variables.map((item) => (item.id === variable.id ? variable : item)));
setEditVariable(undefined);
},
[updateVariables, variables]
);
return (
<>
<Flex mb={1} alignItems={'center'}>
<MyIcon name={'variable'} mr={2} w={'16px'} color={'#fb7c3d'} />
<Box></Box>
<MyTooltip label={variableTip} forceShow>
<QuestionOutlineIcon display={['none', 'inline']} ml={1} />
</MyTooltip>
<Box flex={1} />
<Flex
ml={2}
textAlign={'right'}
cursor={'pointer'}
px={3}
py={'2px'}
borderRadius={'md'}
_hover={{ bg: 'myGray.200' }}
onClick={() => {
const newVariable = addVariable();
updateVariables(variables.concat(newVariable));
setEditVariable(newVariable);
}}
>
+&ensp;
</Flex>
</Flex>
{variables.length > 0 && (
<TableContainer borderWidth={'1px'} borderBottom="none" borderRadius={'lg'}>
<Table>
<Thead>
<Tr>
<Th></Th>
<Th> key</Th>
<Th></Th>
<Th></Th>
</Tr>
</Thead>
<Tbody>
{variables.map((item, index) => (
<Tr key={index}>
<Td>{item.label} </Td>
<Td>{item.key}</Td>
<Td>{item.required ? '✔' : ''}</Td>
<Td>
<MyIcon
mr={3}
name={'settingLight'}
w={'16px'}
cursor={'pointer'}
onClick={() => {
setEditVariable(item);
}}
/>
<MyIcon
name={'delete'}
w={'16px'}
cursor={'pointer'}
onClick={() =>
updateVariables(variables.filter((variable) => variable.id !== item.id))
}
/>
</Td>
</Tr>
))}
</Tbody>
</Table>
</TableContainer>
)}
{!!editVariable && (
<VariableEditModal
defaultVariable={editVariable}
onClose={() => setEditVariable(undefined)}
onSubmit={onclickSubmit}
/>
)}
</>
);
}

View File

@@ -109,7 +109,7 @@ const ModuleTemplateList = ({
<Avatar src={item.logo} w={'34px'} objectFit={'contain'} borderRadius={'0'} />
<Box ml={5} flex={'1 0 0'}>
<Box color={'black'}>{item.name}</Box>
<Box color={'myGray.500'} fontSize={'sm'}>
<Box className="textEllipsis3" color={'myGray.500'} fontSize={'sm'}>
{item.intro}
</Box>
</Box>

View File

@@ -46,7 +46,7 @@ import { useToast } from '@/hooks/useToast';
import { AppSchema } from '@/types/mongoSchema';
import { delModelById } from '@/api/app';
import { useTranslation } from 'react-i18next';
import { getSpecialModule } from '@/components/ChatBox/utils';
import { getGuideModules } from '@/components/ChatBox/utils';
import dynamic from 'next/dynamic';
import MySelect from '@/components/Select';
@@ -410,20 +410,22 @@ const Settings = ({ appId }: { appId: string }) => {
<Flex alignItems={'center'} mt={5}>
<Box {...LabelStyles}></Box>
<MySelect
width={['100%', '300px']}
value={getValues('chatModel.model')}
list={chatModelSelectList}
onchange={(val: any) => {
setValue('chatModel.model', val);
const maxToken =
chatModelList.find((item) => item.model === getValues('chatModel.model'))
?.contextMaxToken || 4000;
const token = maxToken / 2;
setValue('chatModel.maxToken', token);
setRefresh(!refresh);
}}
/>
<Box flex={'1 0 0'}>
<MySelect
width={'100%'}
value={getValues('chatModel.model')}
list={chatModelSelectList}
onchange={(val: any) => {
setValue('chatModel.model', val);
const maxToken =
chatModelList.find((item) => item.model === getValues('chatModel.model'))
?.contextMaxToken || 4000;
const token = maxToken / 2;
setValue('chatModel.maxToken', token);
setRefresh(!refresh);
}}
/>
</Box>
</Flex>
<Flex alignItems={'center'} my={10}>
<Box {...LabelStyles}></Box>
@@ -676,7 +678,7 @@ const ChatTest = ({ appId }: { appId: string }) => {
appAvatar={appDetail.avatar}
userAvatar={userInfo?.avatar}
showMarkIcon
{...getSpecialModule(modules)}
{...getGuideModules(modules)}
onStartChat={startChat}
onDelMessage={() => {}}
/>

View File

@@ -29,27 +29,24 @@ const Home = ({ homeUrl = '/' }: { homeUrl: string }) => {
<Head>
<title>{feConfigs?.systemTitle || 'FastGPT'}</title>
</Head>
{homeUrl === '/' ? (
<Box id="home" bg={'myWhite.600'} h={'100vh'} overflowY={'auto'} overflowX={'hidden'}>
<Box position={'fixed'} zIndex={10} top={0} left={0} right={0}>
<Navbar />
</Box>
<Box maxW={'1200px'} pt={'70px'} m={'auto'}>
<Hero />
<Ability />
<Box my={[4, 6]}>
<Choice />
</Box>
</Box>
{feConfigs?.show_git && (
<Box bg={'white'}>
<Footer />
</Box>
)}
<Box id="home" bg={'myWhite.600'} h={'100vh'} overflowY={'auto'} overflowX={'hidden'}>
<Box position={'fixed'} zIndex={10} top={0} left={0} right={0}>
<Navbar />
</Box>
) : (
<Loading />
)}
<Box maxW={'1200px'} pt={'70px'} m={'auto'}>
<Hero />
<Ability />
<Box my={[4, 6]}>
<Choice />
</Box>
</Box>
{feConfigs?.show_git && (
<Box bg={'white'}>
<Footer />
</Box>
)}
</Box>
{homeUrl !== '/' && <Loading bg={'white'} />}
</>
);
};

View File

@@ -113,7 +113,7 @@ const ChunkImport = ({ kbId }: { kbId: string }) => {
chunks: splitRes.chunks.map((chunk) => ({
a: '',
source: file.filename,
file_id: file.id,
file_id: file.chunks[0]?.file_id,
q: chunk
}))
};

View File

@@ -20,7 +20,7 @@ const CreateFileModal = ({
});
return (
<MyModal title={t('file.Create File')} isOpen onClose={() => {}} w={'600px'} top={'15vh'}>
<MyModal title={t('file.Create File')} isOpen w={'600px'} top={'15vh'}>
<ModalBody>
<Box mb={1} fontSize={'sm'}>

View File

@@ -113,7 +113,7 @@ const QAImport = ({ kbId }: { kbId: string }) => {
chunks: splitRes.chunks.map((chunk) => ({
a: '',
source: file.filename,
file_id: file.id,
file_id: file.chunks[0]?.file_id,
q: chunk
}))
};