Add workflow rename; Fix: userselect chatId unrefresh (#2672)

* feat: workflow node support rename

* perf: push data to training queue

* fix: userselect chatId unrefresh
This commit is contained in:
Archer 2024-09-11 15:27:47 +08:00 committed by GitHub
parent 11cbcca2d4
commit 02bf400bf3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 144 additions and 188 deletions

View File

@ -25,3 +25,4 @@ weight: 813
8. 优化 - 工作流嵌套层级限制 20 层,避免因编排不合理导致的无限死循环。 8. 优化 - 工作流嵌套层级限制 20 层,避免因编排不合理导致的无限死循环。
9. 优化 - 工作流 handler 性能优化。 9. 优化 - 工作流 handler 性能优化。
10. 修复 - 知识库选择权限问题。 10. 修复 - 知识库选择权限问题。
11. 修复 - 空 chatId 发起对话,首轮携带用户选择时会异常。

View File

@ -10,6 +10,7 @@ import { ClientSession } from '../../../common/mongo';
import { getLLMModel, getVectorModel } from '../../ai/model'; import { getLLMModel, getVectorModel } from '../../ai/model';
import { addLog } from '../../../common/system/log'; import { addLog } from '../../../common/system/log';
import { getCollectionWithDataset } from '../controller'; import { getCollectionWithDataset } from '../controller';
import { mongoSessionRun } from '../../../common/mongo/sessionRun';
export const lockTrainingDataByTeamId = async (teamId: string): Promise<any> => { export const lockTrainingDataByTeamId = async (teamId: string): Promise<any> => {
try { try {
@ -64,7 +65,7 @@ export async function pushDataListToTrainingQueue({
vectorModel: string; vectorModel: string;
session?: ClientSession; session?: ClientSession;
} & PushDatasetDataProps): Promise<PushDatasetDataResponse> { } & PushDatasetDataProps): Promise<PushDatasetDataResponse> {
const checkModelValid = async () => { const { model, maxToken, weight } = await (async () => {
const agentModelData = getLLMModel(agentModel); const agentModelData = getLLMModel(agentModel);
if (!agentModelData) { if (!agentModelData) {
return Promise.reject(`File model ${agentModel} is inValid`); return Promise.reject(`File model ${agentModel} is inValid`);
@ -91,9 +92,16 @@ export async function pushDataListToTrainingQueue({
} }
return Promise.reject(`Training mode "${trainingMode}" is inValid`); return Promise.reject(`Training mode "${trainingMode}" is inValid`);
}; })();
const { model, maxToken, weight } = await checkModelValid(); // filter repeat or equal content
const set = new Set();
const filterResult: Record<string, PushDatasetDataChunkProps[]> = {
success: [],
overToken: [],
repeat: [],
error: []
};
// format q and a, remove empty char // format q and a, remove empty char
data.forEach((item) => { data.forEach((item) => {
@ -108,19 +116,8 @@ export async function pushDataListToTrainingQueue({
}; };
}) })
.filter(Boolean); .filter(Boolean);
});
// filter repeat or equal content // filter repeat content
const set = new Set();
const filterResult: Record<string, PushDatasetDataChunkProps[]> = {
success: [],
overToken: [],
repeat: [],
error: []
};
// filter repeat content
data.forEach((item) => {
if (!item.q) { if (!item.q) {
filterResult.error.push(item); filterResult.error.push(item);
return; return;
@ -150,40 +147,55 @@ export async function pushDataListToTrainingQueue({
const failedDocuments: PushDatasetDataChunkProps[] = []; const failedDocuments: PushDatasetDataChunkProps[] = [];
// 使用 insertMany 批量插入 // 使用 insertMany 批量插入
try { const batchSize = 200;
await MongoDatasetTraining.insertMany( const insertData = async (startIndex: number, session: ClientSession) => {
filterResult.success.map((item) => ({ const list = filterResult.success.slice(startIndex, startIndex + batchSize);
teamId,
tmbId,
datasetId,
collectionId,
billId,
mode: trainingMode,
prompt,
model,
q: item.q,
a: item.a,
chunkIndex: item.chunkIndex ?? 0,
weight: weight ?? 0,
indexes: item.indexes
})),
{
session,
ordered: false
}
);
} catch (error: any) {
addLog.error(`Insert error`, error);
// 如果有错误,将失败的文档添加到失败列表中
error.writeErrors?.forEach((writeError: any) => {
failedDocuments.push(data[writeError.index]);
});
console.log('failed', failedDocuments);
}
// 对于失败的文档,尝试单独插入 if (list.length === 0) return;
for await (const item of failedDocuments) {
await MongoDatasetTraining.create(item); try {
await MongoDatasetTraining.insertMany(
list.map((item) => ({
teamId,
tmbId,
datasetId,
collectionId,
billId,
mode: trainingMode,
prompt,
model,
q: item.q,
a: item.a,
chunkIndex: item.chunkIndex ?? 0,
weight: weight ?? 0,
indexes: item.indexes
})),
{
session,
ordered: true
}
);
} catch (error: any) {
addLog.error(`Insert error`, error);
// 如果有错误,将失败的文档添加到失败列表中
error.writeErrors?.forEach((writeError: any) => {
failedDocuments.push(data[writeError.index]);
});
console.log('failed', failedDocuments);
}
console.log(startIndex, '===');
// 对于失败的文档,尝试单独插入
await MongoDatasetTraining.create(failedDocuments, { session });
return insertData(startIndex + batchSize, session);
};
if (session) {
await insertData(0, session);
} else {
await mongoSessionRun(async (session) => {
await insertData(0, session);
});
} }
delete filterResult.success; delete filterResult.success;

View File

@ -9,13 +9,11 @@ import { formatChatValue2InputType } from '../utils';
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants'; import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { ChatBoxContext } from '../Provider'; import { ChatBoxContext } from '../Provider';
import { useContextSelector } from 'use-context-selector'; import { useContextSelector } from 'use-context-selector';
import { SendPromptFnType } from '../type';
export type ChatControllerProps = { export type ChatControllerProps = {
isLastChild: boolean; isLastChild: boolean;
chat: ChatSiteItemType; chat: ChatSiteItemType;
showVoiceIcon?: boolean; showVoiceIcon?: boolean;
onSendMessage: SendPromptFnType;
onRetry?: () => void; onRetry?: () => void;
onDelete?: () => void; onDelete?: () => void;
onMark?: () => void; onMark?: () => void;

View File

@ -19,7 +19,6 @@ import { useCopyData } from '@/web/common/hooks/useCopyData';
import MyIcon from '@fastgpt/web/components/common/Icon'; import MyIcon from '@fastgpt/web/components/common/Icon';
import MyTooltip from '@fastgpt/web/components/common/MyTooltip'; import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
import { useTranslation } from 'next-i18next'; import { useTranslation } from 'next-i18next';
import { SendPromptFnType } from '../type';
import { AIChatItemValueItemType, ChatItemValueItemType } from '@fastgpt/global/core/chat/type'; import { AIChatItemValueItemType, ChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import { CodeClassNameEnum } from '@/components/Markdown/utils'; import { CodeClassNameEnum } from '@/components/Markdown/utils';
import { isEqual } from 'lodash'; import { isEqual } from 'lodash';
@ -51,7 +50,6 @@ type BasicProps = {
type Props = BasicProps & { type Props = BasicProps & {
type: ChatRoleEnum.Human | ChatRoleEnum.AI; type: ChatRoleEnum.Human | ChatRoleEnum.AI;
onSendMessage: SendPromptFnType;
}; };
const RenderQuestionGuide = ({ questionGuides }: { questionGuides: string[] }) => { const RenderQuestionGuide = ({ questionGuides }: { questionGuides: string[] }) => {
@ -80,14 +78,12 @@ const AIContentCard = React.memo(function AIContentCard({
dataId, dataId,
isLastChild, isLastChild,
isChatting, isChatting,
onSendMessage,
questionGuides questionGuides
}: { }: {
dataId: string; dataId: string;
chatValue: ChatItemValueItemType[]; chatValue: ChatItemValueItemType[];
isLastChild: boolean; isLastChild: boolean;
isChatting: boolean; isChatting: boolean;
onSendMessage: SendPromptFnType;
questionGuides: string[]; questionGuides: string[];
}) { }) {
return ( return (
@ -101,7 +97,6 @@ const AIContentCard = React.memo(function AIContentCard({
value={value} value={value}
isLastChild={isLastChild && i === chatValue.length - 1} isLastChild={isLastChild && i === chatValue.length - 1}
isChatting={isChatting} isChatting={isChatting}
onSendMessage={onSendMessage}
/> />
); );
})} })}
@ -113,16 +108,7 @@ const AIContentCard = React.memo(function AIContentCard({
}); });
const ChatItem = (props: Props) => { const ChatItem = (props: Props) => {
const { const { type, avatar, statusBoxData, children, isLastChild, questionGuides = [], chat } = props;
type,
avatar,
statusBoxData,
children,
isLastChild,
questionGuides = [],
onSendMessage,
chat
} = props;
const styleMap: BoxProps = const styleMap: BoxProps =
type === ChatRoleEnum.Human type === ChatRoleEnum.Human
@ -270,7 +256,6 @@ const ChatItem = (props: Props) => {
dataId={chat.dataId} dataId={chat.dataId}
isLastChild={isLastChild && i === splitAiResponseResults.length - 1} isLastChild={isLastChild && i === splitAiResponseResults.length - 1}
isChatting={isChatting} isChatting={isChatting}
onSendMessage={onSendMessage}
questionGuides={questionGuides} questionGuides={questionGuides}
/> />
)} )}

View File

@ -60,7 +60,7 @@ import dynamic from 'next/dynamic';
import type { StreamResponseType } from '@/web/common/api/fetch'; import type { StreamResponseType } from '@/web/common/api/fetch';
import { useContextSelector } from 'use-context-selector'; import { useContextSelector } from 'use-context-selector';
import { useSystem } from '@fastgpt/web/hooks/useSystem'; import { useSystem } from '@fastgpt/web/hooks/useSystem';
import { useCreation, useMemoizedFn, useThrottleFn, useTrackedEffect } from 'ahooks'; import { useCreation, useMemoizedFn, useThrottleFn } from 'ahooks';
import MyIcon from '@fastgpt/web/components/common/Icon'; import MyIcon from '@fastgpt/web/components/common/Icon';
const ResponseTags = dynamic(() => import('./components/ResponseTags')); const ResponseTags = dynamic(() => import('./components/ResponseTags'));
@ -832,12 +832,10 @@ const ChatBox = (
}; };
window.addEventListener('message', windowMessage); window.addEventListener('message', windowMessage);
eventBus.on(EventNameEnum.sendQuestion, ({ text }: { text: string }) => { const fn: SendPromptFnType = (e) => {
if (!text) return; sendPrompt(e);
sendPrompt({ };
text eventBus.on(EventNameEnum.sendQuestion, fn);
});
});
eventBus.on(EventNameEnum.editQuestion, ({ text }: { text: string }) => { eventBus.on(EventNameEnum.editQuestion, ({ text }: { text: string }) => {
if (!text) return; if (!text) return;
resetInputVal({ text }); resetInputVal({ text });
@ -881,7 +879,6 @@ const ChatBox = (
onRetry={retryInput(item.dataId)} onRetry={retryInput(item.dataId)}
onDelete={delOneMessage(item.dataId)} onDelete={delOneMessage(item.dataId)}
isLastChild={index === chatHistories.length - 1} isLastChild={index === chatHistories.length - 1}
onSendMessage={sendPrompt}
/> />
)} )}
{item.obj === ChatRoleEnum.AI && ( {item.obj === ChatRoleEnum.AI && (
@ -891,7 +888,6 @@ const ChatBox = (
avatar={appAvatar} avatar={appAvatar}
chat={item} chat={item}
isLastChild={index === chatHistories.length - 1} isLastChild={index === chatHistories.length - 1}
onSendMessage={sendPrompt}
{...{ {...{
showVoiceIcon, showVoiceIcon,
shareId, shareId,
@ -977,7 +973,6 @@ const ChatBox = (
outLinkUid, outLinkUid,
questionGuides, questionGuides,
retryInput, retryInput,
sendPrompt,
shareId, shareId,
showEmpty, showEmpty,
showMarkIcon, showMarkIcon,

View File

@ -2,7 +2,8 @@ import { ChatSiteItemType } from '@fastgpt/global/core/chat/type';
import { useCallback, useRef, useState } from 'react'; import { useCallback, useRef, useState } from 'react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { PluginRunBoxTabEnum } from './PluginRunBox/constants'; import { PluginRunBoxTabEnum } from './PluginRunBox/constants';
import { ComponentRef as ChatComponentRef } from './ChatBox/type'; import { ComponentRef as ChatComponentRef, SendPromptFnType } from './ChatBox/type';
import { eventBus, EventNameEnum } from '@/web/common/utils/eventbus';
export const useChat = () => { export const useChat = () => {
const ChatBoxRef = useRef<ChatComponentRef>(null); const ChatBoxRef = useRef<ChatComponentRef>(null);
@ -61,3 +62,5 @@ export const useChat = () => {
resetChatRecords resetChatRecords
}; };
}; };
export const onSendPrompt: SendPromptFnType = (e) => eventBus.emit(EventNameEnum.sendQuestion, e);

View File

@ -12,24 +12,20 @@ import {
import { ChatItemValueTypeEnum } from '@fastgpt/global/core/chat/constants'; import { ChatItemValueTypeEnum } from '@fastgpt/global/core/chat/constants';
import { import {
AIChatItemValueItemType, AIChatItemValueItemType,
ChatSiteItemType,
ToolModuleResponseItemType, ToolModuleResponseItemType,
UserChatItemValueItemType UserChatItemValueItemType
} from '@fastgpt/global/core/chat/type'; } from '@fastgpt/global/core/chat/type';
import React from 'react'; import React from 'react';
import MyIcon from '@fastgpt/web/components/common/Icon'; import MyIcon from '@fastgpt/web/components/common/Icon';
import Avatar from '@fastgpt/web/components/common/Avatar'; import Avatar from '@fastgpt/web/components/common/Avatar';
import { SendPromptFnType } from '../ChatContainer/ChatBox/type';
import { useContextSelector } from 'use-context-selector';
import { ChatBoxContext } from '../ChatContainer/ChatBox/Provider';
import { InteractiveNodeResponseItemType } from '@fastgpt/global/core/workflow/template/system/userSelect/type'; import { InteractiveNodeResponseItemType } from '@fastgpt/global/core/workflow/template/system/userSelect/type';
import { isEqual } from 'lodash'; import { isEqual } from 'lodash';
import { onSendPrompt } from '../ChatContainer/useChat';
type props = { type props = {
value: UserChatItemValueItemType | AIChatItemValueItemType; value: UserChatItemValueItemType | AIChatItemValueItemType;
isLastChild: boolean; isLastChild: boolean;
isChatting: boolean; isChatting: boolean;
onSendMessage?: SendPromptFnType;
}; };
const RenderText = React.memo(function RenderText({ const RenderText = React.memo(function RenderText({
@ -128,67 +124,51 @@ ${toolResponse}`}
}, },
(prevProps, nextProps) => isEqual(prevProps, nextProps) (prevProps, nextProps) => isEqual(prevProps, nextProps)
); );
const RenderInteractive = React.memo( const RenderInteractive = React.memo(function RenderInteractive({
function RenderInteractive({ interactive
isChatting, }: {
interactive, interactive: InteractiveNodeResponseItemType;
onSendMessage, }) {
chatHistories return (
}: { <>
isChatting: boolean; {interactive?.params?.description && <Markdown source={interactive.params.description} />}
interactive: InteractiveNodeResponseItemType; <Flex flexDirection={'column'} gap={2} w={'250px'}>
onSendMessage?: SendPromptFnType; {interactive.params.userSelectOptions?.map((option) => {
chatHistories: ChatSiteItemType[]; const selected = option.value === interactive?.params?.userSelectedVal;
}) {
return (
<>
{interactive?.params?.description && <Markdown source={interactive.params.description} />}
<Flex flexDirection={'column'} gap={2} w={'250px'}>
{interactive.params.userSelectOptions?.map((option) => {
const selected = option.value === interactive?.params?.userSelectedVal;
return ( return (
<Button <Button
key={option.key} key={option.key}
variant={'whitePrimary'} variant={'whitePrimary'}
whiteSpace={'pre-wrap'} whiteSpace={'pre-wrap'}
isDisabled={interactive?.params?.userSelectedVal !== undefined} isDisabled={interactive?.params?.userSelectedVal !== undefined}
{...(selected {...(selected
? { ? {
_disabled: { _disabled: {
cursor: 'default', cursor: 'default',
borderColor: 'primary.300', borderColor: 'primary.300',
bg: 'primary.50 !important', bg: 'primary.50 !important',
color: 'primary.600' color: 'primary.600'
}
} }
: {})} }
onClick={() => { : {})}
onSendMessage?.({ onClick={() => {
text: option.value, onSendPrompt({
isInteractivePrompt: true text: option.value,
}); isInteractivePrompt: true
}} });
> }}
{option.value} >
</Button> {option.value}
); </Button>
})} );
</Flex> })}
</> </Flex>
); </>
}, );
( });
prevProps,
nextProps // isChatting 更新时候onSendMessage 和 chatHistories 肯定都更新了,这里不需要额外的刷新
) =>
prevProps.isChatting === nextProps.isChatting &&
isEqual(prevProps.interactive, nextProps.interactive)
);
const AIResponseBox = ({ value, isLastChild, isChatting, onSendMessage }: props) => {
const chatHistories = useContextSelector(ChatBoxContext, (v) => v.chatHistories);
const AIResponseBox = ({ value, isLastChild, isChatting }: props) => {
if (value.type === ChatItemValueTypeEnum.text && value.text) if (value.type === ChatItemValueTypeEnum.text && value.text)
return <RenderText showAnimation={isChatting && isLastChild} text={value.text.content} />; return <RenderText showAnimation={isChatting && isLastChild} text={value.text.content} />;
if (value.type === ChatItemValueTypeEnum.tool && value.tools) if (value.type === ChatItemValueTypeEnum.tool && value.tools)
@ -198,14 +178,7 @@ const AIResponseBox = ({ value, isLastChild, isChatting, onSendMessage }: props)
value.interactive && value.interactive &&
value.interactive.type === 'userSelect' value.interactive.type === 'userSelect'
) )
return ( return <RenderInteractive interactive={value.interactive} />;
<RenderInteractive
isChatting={isChatting}
interactive={value.interactive}
onSendMessage={onSendMessage}
chatHistories={chatHistories}
/>
);
}; };
export default React.memo(AIResponseBox); export default React.memo(AIResponseBox);

View File

@ -53,7 +53,6 @@ const NodePluginConfig = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
selected={selected} selected={selected}
menuForbid={{ menuForbid={{
debug: true, debug: true,
rename: true,
copy: true, copy: true,
delete: true delete: true
}} }}

View File

@ -91,7 +91,6 @@ const NodePluginInput = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
minW={'300px'} minW={'300px'}
selected={selected} selected={selected}
menuForbid={{ menuForbid={{
rename: true,
copy: true, copy: true,
delete: true delete: true
}} }}

View File

@ -48,7 +48,6 @@ const NodePluginOutput = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
selected={selected} selected={selected}
menuForbid={{ menuForbid={{
debug: true, debug: true,
rename: true,
copy: true, copy: true,
delete: true delete: true
}} }}

View File

@ -53,7 +53,6 @@ const NodeUserGuide = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
selected={selected} selected={selected}
menuForbid={{ menuForbid={{
debug: true, debug: true,
rename: true,
copy: true, copy: true,
delete: true delete: true
}} }}

View File

@ -68,7 +68,6 @@ const NodeStart = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
minW={'240px'} minW={'240px'}
selected={selected} selected={selected}
menuForbid={{ menuForbid={{
rename: true,
copy: true, copy: true,
delete: true delete: true
}} }}

View File

@ -33,7 +33,6 @@ type Props = FlowNodeItemType & {
selected?: boolean; selected?: boolean;
menuForbid?: { menuForbid?: {
debug?: boolean; debug?: boolean;
rename?: boolean;
copy?: boolean; copy?: boolean;
delete?: boolean; delete?: boolean;
}; };
@ -154,37 +153,35 @@ const NodeCard = (props: Props) => {
<Box ml={3} fontSize={'md'} fontWeight={'medium'}> <Box ml={3} fontSize={'md'} fontWeight={'medium'}>
{t(name as any)} {t(name as any)}
</Box> </Box>
{!menuForbid?.rename && ( <MyIcon
<MyIcon className="controller-rename"
className="controller-rename" display={'none'}
display={'none'} name={'edit'}
name={'edit'} w={'14px'}
w={'14px'} cursor={'pointer'}
cursor={'pointer'} ml={1}
ml={1} color={'myGray.500'}
color={'myGray.500'} _hover={{ color: 'primary.600' }}
_hover={{ color: 'primary.600' }} onClick={() => {
onClick={() => { onOpenCustomTitleModal({
onOpenCustomTitleModal({ defaultVal: name,
defaultVal: name, onSuccess: (e) => {
onSuccess: (e) => { if (!e) {
if (!e) { return toast({
return toast({ title: t('app:modules.Title is required'),
title: t('app:modules.Title is required'), status: 'warning'
status: 'warning'
});
}
onChangeNode({
nodeId,
type: 'attr',
key: 'name',
value: e
}); });
} }
}); onChangeNode({
}} nodeId,
/> type: 'attr',
)} key: 'name',
value: e
});
}
});
}}
/>
<Box flex={1} /> <Box flex={1} />
{hasNewVersion && ( {hasNewVersion && (
<MyTooltip label={t('app:app.modules.click to update')}> <MyTooltip label={t('app:app.modules.click to update')}>

View File

@ -130,7 +130,6 @@ const Chat = ({
const completionChatId = chatId || getNanoid(); const completionChatId = chatId || getNanoid();
// Just send a user prompt // Just send a user prompt
const histories = messages.slice(-1); const histories = messages.slice(-1);
const { responseText, responseData } = await streamFetch({ const { responseText, responseData } = await streamFetch({
data: { data: {
messages: histories, messages: histories,
@ -146,10 +145,8 @@ const Chat = ({
const newTitle = getChatTitleFromChatMessage(GPTMessages2Chats(histories)[0]); const newTitle = getChatTitleFromChatMessage(GPTMessages2Chats(histories)[0]);
// new chat // new chat
if (completionChatId !== chatId) { if (completionChatId !== chatId && controller.signal.reason !== 'leave') {
if (controller.signal.reason !== 'leave') { onChangeChatId(completionChatId, true);
onChangeChatId(completionChatId, true);
}
} }
loadHistories(); loadHistories();