Skip to content

Commit e100827

Browse files
committed
perf: quote auth
1 parent 8934191 commit e100827

File tree

11 files changed

+140
-86
lines changed

11 files changed

+140
-86
lines changed

packages/web/i18n/en/common.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"Warning": "Warning",
3737
"add_new": "Add New",
3838
"add_new_param": "Add new param",
39-
"all_quotes": "Quote all",
39+
"all_quotes": "All quotes",
4040
"app.templateMarket.templateTags.Image_generation": "Image generation",
4141
"app.templateMarket.templateTags.Office_services": "Office Services",
4242
"app.templateMarket.templateTags.Roleplay": "role play",

projects/app/src/components/Markdown/A.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { getCollectionSourceData } from '@fastgpt/global/core/dataset/collection
2222
import Markdown from '.';
2323
import { getSourceNameIcon } from '@fastgpt/global/core/dataset/utils';
2424

25-
const A = ({ children, ...props }: any) => {
25+
const A = ({ children, chatAuthData, ...props }: any) => {
2626
const { t } = useTranslation();
2727

2828
const { isOpen, onOpen, onClose } = useDisclosure();
@@ -52,7 +52,7 @@ const A = ({ children, ...props }: any) => {
5252
data: quoteData,
5353
loading,
5454
runAsync: getQuoteDataById
55-
} = useRequest2((id: string) => getQuoteData({ id, ...props.metadata }), {
55+
} = useRequest2((id: string) => getQuoteData({ id, ...chatAuthData }), {
5656
manual: true
5757
});
5858
const sourceData = useMemo(
@@ -149,4 +149,4 @@ const A = ({ children, ...props }: any) => {
149149
return <Link {...props}>{children}</Link>;
150150
};
151151

152-
export default A;
152+
export default React.memo(A);

projects/app/src/components/Markdown/index.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import dynamic from 'next/dynamic';
1212

1313
import { Box } from '@chakra-ui/react';
1414
import { CodeClassNameEnum, mdTextFormat } from './utils';
15+
import { useCreation } from 'ahooks';
16+
import { OutLinkChatAuthProps } from '@fastgpt/global/support/permission/chat';
1517

1618
const CodeLight = dynamic(() => import('./codeBlock/CodeLight'), { ssr: false });
1719
const MermaidCodeBlock = dynamic(() => import('./img/MermaidCodeBlock'), { ssr: false });
@@ -31,14 +33,11 @@ type Props = {
3133
showAnimation?: boolean;
3234
isDisabled?: boolean;
3335
forbidZhFormat?: boolean;
34-
metadata?: {
36+
chatAuthData?: {
3537
appId: string;
3638
chatId: string;
37-
shareId?: string;
38-
outLinkUid?: string;
39-
teamId?: string;
40-
teamToken?: string;
41-
};
39+
chatItemDataId: string;
40+
} & OutLinkChatAuthProps;
4241
};
4342
const Markdown = (props: Props) => {
4443
const source = props.source || '';
@@ -54,17 +53,16 @@ const MarkdownRender = ({
5453
showAnimation,
5554
isDisabled,
5655
forbidZhFormat,
57-
metadata
56+
chatAuthData
5857
}: Props) => {
59-
const components = useMemo<any>(
60-
() => ({
58+
const components = useCreation(() => {
59+
return {
6160
img: Image,
6261
pre: RewritePre,
6362
code: Code,
64-
a: (props: any) => <A {...props} metadata={metadata} />
65-
}),
66-
[metadata]
67-
);
63+
a: (props: any) => <A {...props} chatAuthData={chatAuthData} />
64+
};
65+
}, [chatAuthData]);
6866

6967
const formatSource = useMemo(() => {
7068
if (showAnimation || forbidZhFormat) return source;

projects/app/src/components/Markdown/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ export const mdTextFormat = (text: string) => {
3333
// /([\u4e00-\u9fa5\u3000-\u303f])([a-zA-Z0-9])|([a-zA-Z0-9])([\u4e00-\u9fa5\u3000-\u303f])/g,
3434
// '$1$3 $2$4'
3535
// )
36-
// 处理 [quote:id] 格式引用,将 [quote:675934a198f46329dfc6d05a] 转换为 [675934a198f46329dfc6d05a](QUOTE)
37-
.replace(/\[quote:?\s*([a-f0-9]{24})\](?!\()/gi, '[$1](QUOTE)')
36+
// 处理 格式引用,将 [675934a198f46329dfc6d05a] 转换为 [675934a198f46329dfc6d05a](QUOTE)
3837
.replace(/\[([a-f0-9]{24})\](?!\()/g, '[$1](QUOTE)');
3938

4039
// 处理链接后的中文标点符号,增加空格

projects/app/src/components/core/chat/ChatContainer/ChatBox/Provider.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { VariableInputEnum } from '@fastgpt/global/core/workflow/constants';
2222
import { getChatResData } from '@/web/core/chat/api';
2323
import { ChatItemContext } from '@/web/core/chat/context/chatItemContext';
2424
import { ChatRecordContext } from '@/web/core/chat/context/chatRecordContext';
25+
import { useCreation } from 'ahooks';
2526

2627
export type ChatProviderProps = {
2728
appId: string;
@@ -128,13 +129,17 @@ export const ChatBoxContext = createContext<useChatStoreType>({
128129
const Provider = ({
129130
appId,
130131
chatId,
131-
outLinkAuthData = {},
132+
outLinkAuthData,
132133
chatType = 'chat',
133134
children,
134135
...props
135136
}: ChatProviderProps & {
136137
children: React.ReactNode;
137138
}) => {
139+
const formatOutLinkAuth = useCreation(() => {
140+
return outLinkAuthData || {};
141+
}, [outLinkAuthData]);
142+
138143
const welcomeText = useContextSelector(
139144
ChatItemContext,
140145
(v) => v.chatBoxData?.app?.chatConfig?.welcomeText ?? ''
@@ -187,7 +192,7 @@ const Provider = ({
187192
} = useAudioPlay({
188193
appId,
189194
ttsConfig,
190-
...outLinkAuthData
195+
...formatOutLinkAuth
191196
});
192197

193198
const autoTTSResponse =
@@ -209,15 +214,15 @@ const Provider = ({
209214
appId: appId,
210215
chatId: chatId,
211216
dataId,
212-
...outLinkAuthData
217+
...formatOutLinkAuth
213218
});
214219
setChatRecords((state) =>
215220
state.map((item) => (item.dataId === dataId ? { ...item, responseData: resData } : item))
216221
);
217222
return resData;
218223
}
219224
},
220-
[chatRecords, chatId, appId, outLinkAuthData, setChatRecords]
225+
[chatRecords, chatId, appId, formatOutLinkAuth, setChatRecords]
221226
);
222227
const value: useChatStoreType = {
223228
...props,
@@ -243,7 +248,7 @@ const Provider = ({
243248
chatInputGuide,
244249
appId,
245250
chatId,
246-
outLinkAuthData,
251+
outLinkAuthData: formatOutLinkAuth,
247252
getHistoryResponseData,
248253
chatType
249254
};

projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ChatItem.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const AIContentCard = React.memo(function AIContentCard({
100100

101101
return (
102102
<AIResponseBox
103+
chatItemDataId={dataId}
103104
key={key}
104105
value={value}
105106
isLastResponseValue={isLastChild && i === chatValue.length - 1}

projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderOutput.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const RenderOutput = () => {
3535
const key = `${histories[1].dataId}-ai-${i}`;
3636
return (
3737
<AIResponseBox
38+
chatItemDataId={histories[1].dataId}
3839
key={key}
3940
value={value}
4041
isLastResponseValue={true}

projects/app/src/components/core/chat/components/AIResponseBox.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { extractDeepestInteractive } from '@fastgpt/global/core/workflow/runtime
3232
import { useContextSelector } from 'use-context-selector';
3333
import { ChatItemContext } from '@/web/core/chat/context/chatItemContext';
3434
import { ChatBoxContext } from '../ChatContainer/ChatBox/Provider';
35+
import { useCreation } from 'ahooks';
3536

3637
const accordionButtonStyle = {
3738
w: 'auto',
@@ -88,10 +89,12 @@ const RenderResoningContent = React.memo(function RenderResoningContent({
8889
});
8990
const RenderText = React.memo(function RenderText({
9091
showAnimation,
91-
text
92+
text,
93+
chatItemDataId
9294
}: {
9395
showAnimation: boolean;
9496
text: string;
97+
chatItemDataId: string;
9598
}) {
9699
const isResponseDetail = useContextSelector(ChatItemContext, (v) => v.isResponseDetail);
97100

@@ -103,19 +106,16 @@ const RenderText = React.memo(function RenderText({
103106
if (!text) return '';
104107

105108
// Remove quote references if not showing response detail
106-
return isResponseDetail ? text : text.replace(/\[[a-f0-9]{24}\]\(QUOTE\)/g, '');
109+
return isResponseDetail
110+
? text
111+
: text.replace(/\[([a-f0-9]{24})\]\(QUOTE\)/g, '').replace(/\[([a-f0-9]{24})\](?!\()/g, '');
107112
}, [text, isResponseDetail]);
108113

109-
// First empty line
110-
// if (!source && !isLastChild) return null;
114+
const chatAuthData = useCreation(() => {
115+
return { appId, chatId, chatItemDataId, ...outLinkAuthData };
116+
}, [appId, chatId, chatItemDataId, outLinkAuthData]);
111117

112-
return (
113-
<Markdown
114-
source={source}
115-
showAnimation={showAnimation}
116-
metadata={{ appId, chatId, ...outLinkAuthData }}
117-
/>
118-
);
118+
return <Markdown source={source} showAnimation={showAnimation} chatAuthData={chatAuthData} />;
119119
});
120120

121121
const RenderTool = React.memo(
@@ -241,17 +241,23 @@ const RenderUserFormInteractive = React.memo(function RenderFormInput({
241241
});
242242

243243
const AIResponseBox = ({
244+
chatItemDataId,
244245
value,
245246
isLastResponseValue,
246247
isChatting
247248
}: {
249+
chatItemDataId: string;
248250
value: UserChatItemValueItemType | AIChatItemValueItemType;
249251
isLastResponseValue: boolean;
250252
isChatting: boolean;
251253
}) => {
252254
if (value.type === ChatItemValueTypeEnum.text && value.text) {
253255
return (
254-
<RenderText showAnimation={isChatting && isLastResponseValue} text={value.text.content} />
256+
<RenderText
257+
chatItemDataId={chatItemDataId}
258+
showAnimation={isChatting && isLastResponseValue}
259+
text={value.text.content}
260+
/>
255261
);
256262
}
257263
if (value.type === ChatItemValueTypeEnum.reasoning && value.reasoning) {

projects/app/src/pages/api/core/chat/quote/getQuote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function handler(req: ApiRequestProps<GetQuoteDataProps>): Promise<GetQuot
3636
datasetDataIdList
3737
} = req.body;
3838

39-
const [chat, { chatItem }] = await Promise.all([
39+
const [{ chat, responseDetail }, { chatItem }] = await Promise.all([
4040
authChatCrud({
4141
req,
4242
authToken: true,
@@ -49,7 +49,7 @@ async function handler(req: ApiRequestProps<GetQuoteDataProps>): Promise<GetQuot
4949
}),
5050
authCollectionInChat({ appId, chatId, chatItemDataId, collectionIds: collectionIdList })
5151
]);
52-
if (!chat) return Promise.reject(ChatErrEnum.unAuthChat);
52+
if (!chat || !responseDetail) return Promise.reject(ChatErrEnum.unAuthChat);
5353

5454
const list = await MongoDatasetData.find(
5555
{ _id: { $in: datasetDataIdList }, collectionId: { $in: collectionIdList } },

0 commit comments

Comments
 (0)