Skip to content

Commit 58745f8

Browse files
authored
4.8.14 test (labring#3164)
* perf: match base 64 image * perf: register plugins
1 parent f699061 commit 58745f8

File tree

9 files changed

+76
-44
lines changed

9 files changed

+76
-44
lines changed

packages/global/common/string/markdown.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { batchRun } from '../fn/utils';
2-
import { simpleText } from './tools';
2+
import { getNanoid, simpleText } from './tools';
3+
import type { ImageType } from '../../../service/worker/readFile/type';
34

45
/* Delete redundant text in markdown */
56
export const simpleMarkdownText = (rawText: string) => {
@@ -92,3 +93,25 @@ export const markdownProcess = async ({
9293

9394
return simpleMarkdownText(imageProcess);
9495
};
96+
97+
export const matchMdImgTextAndUpload = (text: string) => {
98+
const base64Regex = /"(data:image\/[^;]+;base64[^"]+)"/g;
99+
const imageList: ImageType[] = [];
100+
const images = Array.from(text.match(base64Regex) || []);
101+
for (const image of images) {
102+
const uuid = `IMAGE_${getNanoid(12)}_IMAGE`;
103+
const mime = image.split(';')[0].split(':')[1];
104+
const base64 = image.split(',')[1];
105+
text = text.replace(image, uuid);
106+
imageList.push({
107+
uuid,
108+
base64,
109+
mime
110+
});
111+
}
112+
113+
return {
114+
text,
115+
imageList
116+
};
117+
};

packages/plugins/register.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export const getCommunityPlugins = () => {
4646
};
4747

4848
export const getSystemPluginTemplates = () => {
49+
if (!global.systemPlugins) return [];
50+
4951
const oldPlugins = global.communityPlugins ?? [];
5052
return [...oldPlugins, ...cloneDeep(global.systemPlugins)];
5153
};

packages/service/common/file/read/utils.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import FormData from 'form-data';
44

55
import { WorkerNameEnum, runWorker } from '../../../worker/utils';
66
import fs from 'fs';
7-
import { detectFileEncoding } from '@fastgpt/global/common/file/tools';
87
import type { ReadFileResponse } from '../../../worker/readFile/type';
98
import axios from 'axios';
109
import { addLog } from '../../system/log';
1110
import { batchRun } from '@fastgpt/global/common/fn/utils';
1211
import { addHours } from 'date-fns';
12+
import { matchMdImgTextAndUpload } from '@fastgpt/global/common/string/markdown';
1313

1414
export type readRawTextByLocalFileParams = {
1515
teamId: string;
@@ -79,6 +79,7 @@ export const readRawContentByFileBuffer = async ({
7979
data: {
8080
page: number;
8181
markdown: string;
82+
duration: number;
8283
};
8384
}>(customReadfileUrl, data, {
8485
timeout: 600000,
@@ -90,10 +91,12 @@ export const readRawContentByFileBuffer = async ({
9091
addLog.info(`Use custom read file service, time: ${Date.now() - start}ms`);
9192

9293
const rawText = response.data.markdown;
94+
const { text, imageList } = matchMdImgTextAndUpload(rawText);
9395

9496
return {
95-
rawText,
96-
formatText: rawText
97+
rawText: text,
98+
formatText: rawText,
99+
imageList
97100
};
98101
};
99102

@@ -120,6 +123,9 @@ export const readRawContentByFileBuffer = async ({
120123
}
121124
});
122125
rawText = rawText.replace(item.uuid, src);
126+
if (formatText) {
127+
formatText = formatText.replace(item.uuid, src);
128+
}
123129
});
124130
}
125131

@@ -128,7 +134,7 @@ export const readRawContentByFileBuffer = async ({
128134
if (isQAImport) {
129135
rawText = rawText || '';
130136
} else {
131-
rawText = formatText || '';
137+
rawText = formatText || rawText;
132138
}
133139
}
134140

packages/service/worker/htmlStr2Md/utils.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import TurndownService from 'turndown';
22
import { ImageType } from '../readFile/type';
3+
import { matchMdImgTextAndUpload } from '@fastgpt/global/common/string/markdown';
34
// @ts-ignore
45
const turndownPluginGfm = require('joplin-turndown-plugin-gfm');
56

@@ -24,23 +25,10 @@ export const html2md = (
2425
turndownService.remove(['i', 'script', 'iframe', 'style']);
2526
turndownService.use(turndownPluginGfm.gfm);
2627

27-
const base64Regex = /"(data:image\/[^;]+;base64[^"]+)"/g;
28-
const imageList: ImageType[] = [];
29-
const images = Array.from(html.match(base64Regex) || []);
30-
for (const image of images) {
31-
const uuid = crypto.randomUUID();
32-
const mime = image.split(';')[0].split(':')[1];
33-
const base64 = image.split(',')[1];
34-
html = html.replace(image, uuid);
35-
imageList.push({
36-
uuid,
37-
base64,
38-
mime
39-
});
40-
}
28+
const { text, imageList } = matchMdImgTextAndUpload(html);
4129

4230
return {
43-
rawText: turndownService.turndown(html),
31+
rawText: turndownService.turndown(text),
4432
imageList
4533
};
4634
} catch (error) {

projects/app/src/components/support/user/inform/SystemMsgModal.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { useCallback } from 'react';
22
import MyModal from '@fastgpt/web/components/common/MyModal';
33
import { useUserStore } from '@/web/support/user/useUserStore';
4-
import { useQuery } from '@tanstack/react-query';
54
import { Button, ModalBody, ModalFooter, useDisclosure } from '@chakra-ui/react';
65
import { useTranslation } from 'next-i18next';
76
import { LOGO_ICON } from '@fastgpt/global/common/system/constants';
87
import { getSystemMsgModalData } from '@/web/support/user/inform/api';
98
import dynamic from 'next/dynamic';
9+
import { useRequest2 } from '@fastgpt/web/hooks/useRequest';
1010
const Markdown = dynamic(() => import('@/components/Markdown'), { ssr: false });
1111

1212
const SystemMsgModal = ({}: {}) => {
@@ -15,7 +15,9 @@ const SystemMsgModal = ({}: {}) => {
1515

1616
const { isOpen, onOpen, onClose } = useDisclosure();
1717

18-
const { data } = useQuery(['initSystemMsgModal', systemMsgReadId], getSystemMsgModalData, {
18+
const { data } = useRequest2(getSystemMsgModalData, {
19+
refreshDeps: [systemMsgReadId],
20+
manual: false,
1921
onSuccess(res) {
2022
if (res?.content && (!systemMsgReadId || res.id !== systemMsgReadId)) {
2123
onOpen();

projects/app/src/pages/app/detail/components/WorkflowComponents/Flow/nodes/render/Handle/ConnectionHandle.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
66
import { useContextSelector } from 'use-context-selector';
77
import { WorkflowContext } from '../../../../context';
88
import { WorkflowNodeEdgeContext } from '../../../../context/workflowInitContext';
9+
import { FlowNodeItemType } from '@fastgpt/global/core/workflow/type/node';
910

1011
export const ConnectionSourceHandle = ({
1112
nodeId,
@@ -141,15 +142,23 @@ export const ConnectionTargetHandle = React.memo(function ConnectionTargetHandle
141142
const { connectingEdge, nodeList } = useContextSelector(WorkflowContext, (ctx) => ctx);
142143

143144
const { LeftHandle, rightHandle, topHandle, bottomHandle } = useMemo(() => {
144-
const node = nodeList.find((node) => node.nodeId === nodeId);
145-
const connectingNode = nodeList.find((node) => node.nodeId === connectingEdge?.nodeId);
145+
let node: FlowNodeItemType | undefined = undefined,
146+
connectingNode: FlowNodeItemType | undefined = undefined;
147+
for (const item of nodeList) {
148+
if (item.nodeId === nodeId) {
149+
node = item;
150+
}
151+
if (item.nodeId === connectingEdge?.nodeId) {
152+
connectingNode = item;
153+
}
154+
if (node && (connectingNode || !connectingEdge?.nodeId)) break;
155+
}
146156

147-
const connectingNodeSourceNodeIdMap = new Map<string, number>();
148157
let forbidConnect = false;
149-
edges.forEach((edge) => {
150-
if (edge.target === connectingNode?.nodeId) {
151-
connectingNodeSourceNodeIdMap.set(edge.source, 1);
152-
} else if (edge.target === nodeId) {
158+
for (const edge of edges) {
159+
if (forbidConnect) break;
160+
161+
if (edge.target === nodeId) {
153162
// Node has be connected tool, it cannot be connect by other handle
154163
if (edge.targetHandle === NodeOutputKeyEnum.selectedTools) {
155164
forbidConnect = true;
@@ -163,7 +172,7 @@ export const ConnectionTargetHandle = React.memo(function ConnectionTargetHandle
163172
forbidConnect = true;
164173
}
165174
}
166-
});
175+
}
167176

168177
const showHandle = (() => {
169178
if (forbidConnect) return false;
@@ -178,9 +187,6 @@ export const ConnectionTargetHandle = React.memo(function ConnectionTargetHandle
178187
// Not the same parent node
179188
if (connectingNode && connectingNode?.parentNodeId !== node?.parentNodeId) return false;
180189

181-
// Unable to connect to the source node
182-
if (connectingNodeSourceNodeIdMap.has(nodeId)) return false;
183-
184190
return true;
185191
})();
186192

projects/app/src/service/common/system/volumnMongoWatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getSystemPluginCb, getSystemPlugins } from '@/service/core/app/plugin';
1+
import { getSystemPluginCb } from '@/service/core/app/plugin';
22
import { initSystemConfig } from '.';
33
import { createDatasetTrainingMongoWatch } from '@/service/core/dataset/training/utils';
44
import { MongoSystemConfigs } from '@fastgpt/service/common/system/config/schema';
@@ -29,7 +29,7 @@ const refetchSystemPlugins = () => {
2929
changeStream.on('change', async (change) => {
3030
setTimeout(() => {
3131
try {
32-
getSystemPlugins(true);
32+
getSystemPluginCb(true);
3333
} catch (error) {}
3434
}, 5000);
3535
});

projects/app/src/service/core/app/plugin.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const getCommercialPlugins = () => {
1111
return GET<SystemPluginTemplateItemType[]>('/core/app/plugin/getSystemPlugins');
1212
};
1313
export const getSystemPlugins = async (refresh = false) => {
14-
if (isProduction && global.systemPlugins && !refresh) return cloneDeep(global.systemPlugins);
14+
if (isProduction && global.systemPlugins && global.systemPlugins.length > 0 && !refresh)
15+
return cloneDeep(global.systemPlugins);
1516

1617
try {
1718
if (!global.systemPlugins) {
@@ -22,8 +23,6 @@ export const getSystemPlugins = async (refresh = false) => {
2223

2324
addLog.info(`Load system plugin successfully: ${global.systemPlugins.length}`);
2425

25-
getSystemPluginCb();
26-
2726
return cloneDeep(global.systemPlugins);
2827
} catch (error) {
2928
//@ts-ignore
@@ -56,16 +55,22 @@ const getCommercialCb = async () => {
5655
{}
5756
);
5857
};
59-
export const getSystemPluginCb = async () => {
60-
if (isProduction && global.systemPluginCb) return global.systemPluginCb;
58+
59+
export const getSystemPluginCb = async (refresh = false) => {
60+
if (
61+
isProduction &&
62+
global.systemPluginCb &&
63+
Object.keys(global.systemPluginCb).length > 0 &&
64+
!refresh
65+
)
66+
return global.systemPluginCb;
6167

6268
try {
6369
global.systemPluginCb = {};
70+
await getSystemPlugins();
6471
global.systemPluginCb = FastGPTProUrl ? await getCommercialCb() : await getCommunityCb();
6572
return global.systemPluginCb;
6673
} catch (error) {
67-
//@ts-ignore
68-
global.systemPluginCb = undefined;
6974
return Promise.reject(error);
7075
}
7176
};

projects/app/src/service/mongo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { startMongoWatch } from './common/system/volumnMongoWatch';
1212
import { startTrainingQueue } from './core/dataset/training/utils';
1313
import { systemStartCb } from '@fastgpt/service/common/system/tools';
1414
import { addLog } from '@fastgpt/service/common/system/log';
15-
import { getSystemPlugins } from './core/app/plugin';
15+
import { getSystemPluginCb } from './core/app/plugin';
1616

1717
/**
1818
* This function is equivalent to the entry to the service
@@ -34,7 +34,7 @@ export function connectToDatabase() {
3434
//init system config;init vector database;init root user
3535
await Promise.all([getInitConfig(), initVectorStore(), initRootUser()]);
3636

37-
getSystemPlugins();
37+
getSystemPluginCb();
3838
startMongoWatch();
3939
// cron
4040
startCron();

0 commit comments

Comments
 (0)