Skip to content

Commit

Permalink
perf: code (#3232)
Browse files Browse the repository at this point in the history
* perf: code

* update doc
  • Loading branch information
c121914yu committed Nov 25, 2024
1 parent a61118c commit 06df2d5
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 82 deletions.
12 changes: 8 additions & 4 deletions docSite/content/zh-cn/docs/development/upgrading/4814.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ weight: 810
1. 新增 - 工作流支持进入聊天框/点击开始对话后,自动触发一轮对话。
2. 新增 - 重写 chatContext,对话测试也会有日志,并且刷新后不会丢失对话。
3. 新增 - 分享链接支持配置是否允许查看原文。
4. 优化 - 工作流 ui 细节。
5. 优化 - 应用编辑记录采用 diff 存储,避免浏览器溢出。
6. 修复 - 分块策略,四级标题会被丢失。 同时新增了五级标题的支持。
7. 修复 - MongoDB 知识库集合唯一索引。
4. 新增 - 新的 doc2x 插件。
5. 优化 - 工作流 ui 细节。
6. 优化 - 应用编辑记录采用 diff 存储,避免浏览器溢出。
7. 修复 - 分块策略,四级标题会被丢失。 同时新增了五级标题的支持。
8. 修复 - MongoDB 知识库集合唯一索引。
9. 修复 - 反选知识库引用后可能会报错。
10. 修复 - 简易模式转工作流,不是使用最新编辑记录进行转移。
11. 修复 - 表单输入的说明文字不显示。
3 changes: 2 additions & 1 deletion packages/service/core/workflow/dispatch/chat/oneapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { getLLMModel, ModelTypeEnum } from '../../../ai/model';
import type { SearchDataResponseItemType } from '@fastgpt/global/core/dataset/type';
import { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { getHistories } from '../utils';
import { checkQuoteQAValue, getHistories } from '../utils';
import { filterSearchResultsByMaxChars } from '../../utils';
import { getHistoryPreview } from '@fastgpt/global/core/chat/utils';
import { computedMaxToken, llmCompletionsBodyFormat } from '../../../ai/utils';
Expand Down Expand Up @@ -91,6 +91,7 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp
stream = stream && isResponseAnswerText;

const chatHistories = getHistories(history, histories);
quoteQA = checkQuoteQAValue(quoteQA);

const modelConstantsData = getLLMModel(model);
if (!modelConstantsData) {
Expand Down
29 changes: 8 additions & 21 deletions packages/service/core/workflow/dispatch/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { responseWrite } from '../../../common/response';
import { NextApiResponse } from 'next';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { getNanoid } from '@fastgpt/global/common/string/tools';
import { SearchDataResponseItemType } from '@fastgpt/global/core/dataset/type';

export const getWorkflowResponseWrite = ({
res,
Expand Down Expand Up @@ -87,27 +88,6 @@ export const filterToolNodeIdByEdges = ({
.map((edge) => edge.target);
};

// export const checkTheModuleConnectedByTool = (
// modules: StoreNodeItemType[],
// node: StoreNodeItemType
// ) => {
// let sign = false;
// const toolModules = modules.filter((item) => item.flowNodeType === FlowNodeTypeEnum.tools);

// toolModules.forEach((item) => {
// const toolOutput = item.outputs.find(
// (output) => output.key === NodeOutputKeyEnum.selectedTools
// );
// toolOutput?.targets.forEach((target) => {
// if (target.moduleId === node.moduleId) {
// sign = true;
// }
// });
// });

// return sign;
// };

export const getHistories = (history?: ChatItemType[] | number, histories: ChatItemType[] = []) => {
if (!history) return [];

Expand Down Expand Up @@ -149,6 +129,13 @@ export const valueTypeFormat = (value: any, type?: WorkflowIOValueTypeEnum) => {
return value;
};

export const checkQuoteQAValue = (quoteQA: SearchDataResponseItemType[] = []) => {
if (quoteQA.some((item) => !item.q || !item.datasetId)) {
return undefined;
}
return quoteQA;
};

/* remove system variable */
export const removeSystemVariable = (variables: Record<string, any>) => {
const copyVariables = { ...variables };
Expand Down
7 changes: 5 additions & 2 deletions packages/web/components/common/MySelect/MultipleRowSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ export const MultipleRowSelect = ({
const newValue = [...cloneValue];

if (item.value === selectedValue) {
setCloneValue([undefined]);
onSelect(undefined);
for (let i = index; i < newValue.length; i++) {
newValue[i] = undefined;
}
setCloneValue(newValue);
onSelect(newValue);
} else {
newValue[index] = item.value;
setCloneValue(newValue);
Expand Down
2 changes: 1 addition & 1 deletion packages/web/components/common/MySelect/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type MultipleSelectProps = {
list: ListItemType[];
emptyTip?: string;
maxH?: number;
onSelect: (val: any[] | undefined) => void;
onSelect: (val: any[]) => void;
styles?: ButtonProps;
popDirection?: 'top' | 'bottom';
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ enum FeedbackTypeEnum {

type Props = OutLinkChatAuthProps &
ChatProviderProps & {
isReady?: boolean;
feedbackType?: `${FeedbackTypeEnum}`;
showMarkIcon?: boolean; // admin mark dataset
showVoiceIcon?: boolean;
Expand All @@ -97,6 +98,7 @@ type Props = OutLinkChatAuthProps &
};

const ChatBox = ({
isReady = true,
feedbackType = FeedbackTypeEnum.hidden,
showMarkIcon = false,
showVoiceIcon = true,
Expand Down Expand Up @@ -811,7 +813,7 @@ const ChatBox = ({
setQuestionGuide([]);
setValue('chatStarted', false);
abortRequest('leave');
}, [router.query, setValue, chatId, abortRequest]);
}, [router.query, setValue, chatId]);

// add listener
useEffect(() => {
Expand Down Expand Up @@ -840,6 +842,22 @@ const ChatBox = ({
};
}, [resetInputVal, sendPrompt]);

// Auto send prompt
useEffect(() => {
if (
isReady &&
autoExecute.open &&
chatStarted &&
chatRecords.length === 0 &&
isChatRecordsLoaded
) {
sendPrompt({
text: autoExecute.defaultPrompt || 'AUTO_EXECUTE',
hideInUI: true
});
}
}, [isReady, chatStarted, autoExecute?.open, chatRecords, isChatRecordsLoaded]);

// output data
useImperativeHandle(ChatBoxRef, () => ({
restartChat() {
Expand All @@ -854,16 +872,6 @@ const ChatBox = ({
}
}));

// Auto send prompt
useEffect(() => {
if (autoExecute.open && chatStarted && chatRecords.length === 0 && isChatRecordsLoaded) {
sendPrompt({
text: autoExecute.defaultPrompt || 'AUTO_EXECUTE',
hideInUI: true
});
}
}, [sendPrompt, chatStarted, autoExecute, chatRecords, isChatRecordsLoaded]);

const RenderRecords = useMemo(() => {
return (
<ScrollData
Expand Down
1 change: 0 additions & 1 deletion projects/app/src/global/core/app/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export type AppUpdateParams = {
};

export type PostPublishAppProps = {
type: AppTypeEnum;
nodes: AppSchema['modules'];
edges: AppSchema['edges'];
chatConfig: AppSchema['chatConfig'];
Expand Down
4 changes: 2 additions & 2 deletions projects/app/src/pages/api/core/app/transitionWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ async function handler(
});

return { id: appId };
} else {
await MongoApp.findByIdAndUpdate(appId, { type: AppTypeEnum.workflow });
}

await MongoApp.findByIdAndUpdate(appId, { type: AppTypeEnum.workflow });

return {};
}

Expand Down
27 changes: 13 additions & 14 deletions projects/app/src/pages/api/core/app/version/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import { beforeUpdateAppFormat } from '@fastgpt/service/core/app/controller';
import { getNextTimeByCronStringAndTimezone } from '@fastgpt/global/common/string/time';
import { PostPublishAppProps } from '@/global/core/app/api';
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
import { ApiRequestProps } from '@fastgpt/service/type/next';

async function handler(req: NextApiRequest, res: NextApiResponse<any>): Promise<{}> {
async function handler(
req: ApiRequestProps<PostPublishAppProps>,
res: NextApiResponse<any>
): Promise<{}> {
const { appId } = req.query as { appId: string };
const {
nodes = [],
edges = [],
chatConfig,
type,
isPublish,
versionName
} = req.body as PostPublishAppProps;
const { nodes = [], edges = [], chatConfig, isPublish, versionName } = req.body;

const { tmbId } = await authApp({ appId, req, per: WritePermissionVal, authToken: true });

Expand Down Expand Up @@ -50,11 +47,13 @@ async function handler(req: NextApiRequest, res: NextApiResponse<any>): Promise<
chatConfig,
updateTime: new Date(),
version: 'v2',
type,
scheduledTriggerConfig: chatConfig?.scheduledTriggerConfig,
scheduledTriggerNextTime: chatConfig?.scheduledTriggerConfig?.cronString
? getNextTimeByCronStringAndTimezone(chatConfig.scheduledTriggerConfig)
: null,
// 只有发布才会更新定时器
...(isPublish && {
scheduledTriggerConfig: chatConfig?.scheduledTriggerConfig,
scheduledTriggerNextTime: chatConfig?.scheduledTriggerConfig?.cronString
? getNextTimeByCronStringAndTimezone(chatConfig.scheduledTriggerConfig)
: null
}),
'pluginData.nodeVersion': _id
},
{
Expand Down
26 changes: 5 additions & 21 deletions projects/app/src/pages/app/detail/components/SimpleApp/AppCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import MyMenu from '@fastgpt/web/components/common/MyMenu';
import MyModal from '@fastgpt/web/components/common/MyModal';
import { useRequest2 } from '@fastgpt/web/hooks/useRequest';
import { postTransition2Workflow } from '@/web/core/app/api/app';
import { AppTypeEnum } from '@fastgpt/global/core/app/constants';
import { form2AppWorkflow } from '@/web/core/app/utils';
import { formatTime2YMDHMS } from '@fastgpt/global/common/string/time';
import { SimpleAppSnapshotType } from './useSnapshots';

const AppCard = ({
Expand All @@ -38,7 +36,6 @@ const AppCard = ({
const { t } = useTranslation();
const onSaveApp = useContextSelector(AppContext, (v) => v.onSaveApp);
const appDetail = useContextSelector(AppContext, (v) => v.appDetail);
const setAppDetail = useContextSelector(AppContext, (v) => v.setAppDetail);
const onOpenInfoEdit = useContextSelector(AppContext, (v) => v.onOpenInfoEdit);
const onDelApp = useContextSelector(AppContext, (v) => v.onDelApp);

Expand All @@ -55,22 +52,11 @@ const AppCard = ({
nodes,
edges,
chatConfig: appForm.chatConfig,
type: AppTypeEnum.simple,
isPublish: true,
versionName: formatTime2YMDHMS(new Date())
isPublish: false,
versionName: t('app:transition_to_workflow')
});
setPast((prevPast) =>
prevPast.map((item, index) =>
index === 0
? {
...item,
isSaved: true
}
: item
)
);

return await postTransition2Workflow({ appId, createNew: transitionCreateNew });
return postTransition2Workflow({ appId, createNew: transitionCreateNew });
},
{
onSuccess: ({ id }) => {
Expand All @@ -81,10 +67,8 @@ const AppCard = ({
}
});
} else {
setAppDetail((state) => ({
...state,
type: AppTypeEnum.workflow
}));
setPast([]);
router.reload();
}
},
successToast: t('common:common.Success')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const ChatTest = ({ appForm }: Props) => {

const { ChatContainer, restartChat, loading } = useChatTest({
...workflowData,
chatConfig: appForm.chatConfig
chatConfig: appForm.chatConfig,
isReady: true
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ const Header = ({
nodes,
edges,
chatConfig: appForm.chatConfig,
type: AppTypeEnum.simple,
isPublish,
versionName
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const ChatTest = ({ isOpen, nodes = [], edges = [], onClose }: Props) => {
const { restartChat, ChatContainer, loading } = useChatTest({
nodes,
edges,
chatConfig: appDetail.chatConfig
chatConfig: appDetail.chatConfig,
isReady: isOpen
});
const pluginRunTab = useContextSelector(ChatItemContext, (v) => v.pluginRunTab);
const setPluginRunTab = useContextSelector(ChatItemContext, (v) => v.setPluginRunTab);
Expand Down
5 changes: 4 additions & 1 deletion projects/app/src/pages/app/detail/components/useChatTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ const PluginRunBox = dynamic(() => import('@/components/core/chat/ChatContainer/
export const useChatTest = ({
nodes,
edges,
chatConfig
chatConfig,
isReady
}: {
nodes: StoreNodeItemType[];
edges: StoreEdgeItemType[];
chatConfig: AppChatConfigType;
isReady: boolean;
}) => {
const { t } = useTranslation();
const { userInfo } = useUserStore();
Expand Down Expand Up @@ -130,6 +132,7 @@ export const useChatTest = ({
</Box>
) : (
<ChatBox
isReady={isReady}
appId={appId}
chatId={chatId}
showMarkIcon
Expand Down

0 comments on commit 06df2d5

Please sign in to comment.