mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-27 16:33:49 +00:00
4.6.4-alpha (#569)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import React, { useCallback, useRef } from 'react';
|
||||
import Head from 'next/head';
|
||||
import { useRouter } from 'next/router';
|
||||
import { getInitChatSiteInfo, delChatRecordById, putChatHistory } from '@/web/core/chat/api';
|
||||
import { getInitChatInfo, putChatHistory } from '@/web/core/chat/api';
|
||||
import {
|
||||
Box,
|
||||
Flex,
|
||||
@@ -34,6 +34,7 @@ import { serviceSideProps } from '@/web/common/utils/i18n';
|
||||
import { useAppStore } from '@/web/core/app/store/useAppStore';
|
||||
import { checkChatSupportSelectFileByChatModels } from '@/web/core/chat/utils';
|
||||
import { chatContentReplaceBlock } from '@fastgpt/global/core/chat/utils';
|
||||
import { ChatStatusEnum } from '@fastgpt/global/core/chat/constants';
|
||||
|
||||
const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
const router = useRouter();
|
||||
@@ -49,13 +50,15 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
setLastChatAppId,
|
||||
lastChatId,
|
||||
setLastChatId,
|
||||
history,
|
||||
loadHistory,
|
||||
histories,
|
||||
loadHistories,
|
||||
pushHistory,
|
||||
updateHistory,
|
||||
delHistory,
|
||||
clearHistory,
|
||||
delOneHistory,
|
||||
clearHistories,
|
||||
chatData,
|
||||
setChatData
|
||||
setChatData,
|
||||
delOneHistoryItem
|
||||
} = useChatStore();
|
||||
const { myApps, loadMyApps } = useAppStore();
|
||||
const { userInfo } = useUserStore();
|
||||
@@ -85,7 +88,7 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
prompts[1]?.value?.slice(0, 20) ||
|
||||
'新对话';
|
||||
|
||||
// update history
|
||||
// new chat
|
||||
if (completionChatId !== chatId) {
|
||||
const newHistory: ChatHistoryItemType = {
|
||||
chatId: completionChatId,
|
||||
@@ -94,7 +97,7 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
appId,
|
||||
top: false
|
||||
};
|
||||
updateHistory(newHistory);
|
||||
pushHistory(newHistory);
|
||||
if (controller.signal.reason !== 'leave') {
|
||||
forbidRefresh.current = true;
|
||||
router.replace({
|
||||
@@ -105,7 +108,8 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const currentChat = history.find((item) => item.chatId === chatId);
|
||||
// update chat
|
||||
const currentChat = histories.find((item) => item.chatId === chatId);
|
||||
currentChat &&
|
||||
updateHistory({
|
||||
...currentChat,
|
||||
@@ -117,30 +121,12 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
setChatData((state) => ({
|
||||
...state,
|
||||
title: newTitle,
|
||||
history: ChatBoxRef.current?.getChatHistory() || state.history
|
||||
history: ChatBoxRef.current?.getChatHistories() || state.history
|
||||
}));
|
||||
|
||||
return { responseText, responseData, isNewChat: forbidRefresh.current };
|
||||
},
|
||||
[appId, chatId, history, router, setChatData, updateHistory]
|
||||
);
|
||||
|
||||
// del one chat content
|
||||
const delOneHistoryItem = useCallback(
|
||||
async ({ contentId, index }: { contentId?: string; index: number }) => {
|
||||
if (!chatId || !contentId) return;
|
||||
|
||||
try {
|
||||
setChatData((state) => ({
|
||||
...state,
|
||||
history: state.history.filter((_, i) => i !== index)
|
||||
}));
|
||||
await delChatRecordById({ chatId, contentId });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
},
|
||||
[chatId, setChatData]
|
||||
[appId, chatId, histories, pushHistory, router, setChatData, updateHistory]
|
||||
);
|
||||
|
||||
// get chat app info
|
||||
@@ -156,10 +142,10 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
}) => {
|
||||
try {
|
||||
loading && setIsLoading(true);
|
||||
const res = await getInitChatSiteInfo({ appId, chatId });
|
||||
const res = await getInitChatInfo({ appId, chatId });
|
||||
const history = res.history.map((item) => ({
|
||||
...item,
|
||||
status: 'finish' as any
|
||||
status: ChatStatusEnum.finish
|
||||
}));
|
||||
|
||||
setChatData({
|
||||
@@ -185,8 +171,13 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
});
|
||||
if (e?.code === 501) {
|
||||
router.replace('/app/list');
|
||||
} else {
|
||||
router.replace('/chat');
|
||||
} else if (chatId) {
|
||||
router.replace({
|
||||
query: {
|
||||
...router.query,
|
||||
chatId: ''
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
setIsLoading(false);
|
||||
@@ -250,7 +241,7 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
});
|
||||
});
|
||||
|
||||
useQuery(['loadHistory', appId], () => (appId ? loadHistory({ appId }) : null));
|
||||
useQuery(['loadHistories', appId], () => (appId ? loadHistories({ appId }) : null));
|
||||
|
||||
return (
|
||||
<Flex h={'100%'}>
|
||||
@@ -289,7 +280,7 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
appAvatar={chatData.app.avatar}
|
||||
activeChatId={chatId}
|
||||
onClose={onCloseSlider}
|
||||
history={history.map((item, i) => ({
|
||||
history={histories.map((item, i) => ({
|
||||
id: item.chatId,
|
||||
title: item.title,
|
||||
customTitle: item.customTitle,
|
||||
@@ -306,39 +297,24 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
onCloseSlider();
|
||||
}
|
||||
}}
|
||||
onDelHistory={delHistory}
|
||||
onDelHistory={delOneHistory}
|
||||
onClearHistory={() => {
|
||||
clearHistory(appId);
|
||||
clearHistories({ appId });
|
||||
router.replace({
|
||||
query: {
|
||||
appId
|
||||
}
|
||||
});
|
||||
}}
|
||||
onSetHistoryTop={async (e) => {
|
||||
try {
|
||||
await putChatHistory(e);
|
||||
const historyItem = history.find((item) => item.chatId === e.chatId);
|
||||
if (!historyItem) return;
|
||||
updateHistory({
|
||||
...historyItem,
|
||||
top: e.top
|
||||
});
|
||||
} catch (error) {}
|
||||
onSetHistoryTop={(e) => {
|
||||
updateHistory(e);
|
||||
}}
|
||||
onSetCustomTitle={async (e) => {
|
||||
try {
|
||||
putChatHistory({
|
||||
chatId: e.chatId,
|
||||
customTitle: e.title
|
||||
});
|
||||
const historyItem = history.find((item) => item.chatId === e.chatId);
|
||||
if (!historyItem) return;
|
||||
updateHistory({
|
||||
...historyItem,
|
||||
customTitle: e.title
|
||||
});
|
||||
} catch (error) {}
|
||||
updateHistory({
|
||||
chatId: e.chatId,
|
||||
title: e.title,
|
||||
customTitle: e.title
|
||||
});
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
@@ -372,7 +348,7 @@ const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
|
||||
feedbackType={'user'}
|
||||
onUpdateVariable={(e) => {}}
|
||||
onStartChat={startChat}
|
||||
onDelMessage={delOneHistoryItem}
|
||||
onDelMessage={(e) => delOneHistoryItem({ ...e, chatId })}
|
||||
/>
|
||||
</Box>
|
||||
</Flex>
|
||||
|
Reference in New Issue
Block a user