From f49588756e6ae5e855567c74b7902a22089ab8b0 Mon Sep 17 00:00:00 2001 From: balibabu Date: Thu, 3 Apr 2025 18:04:40 +0800 Subject: [PATCH] Feat: Load the dialog page, prohibit calling the dialog/get interface #6798 (#6799) ### What problem does this PR solve? Feat: Load the dialog page, prohibit calling the dialog/get interface #6798 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- web/src/hooks/chat-hooks.ts | 14 ++++++++------ web/src/pages/chat/hooks.ts | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/web/src/hooks/chat-hooks.ts b/web/src/hooks/chat-hooks.ts index 93a55421..599944d2 100644 --- a/web/src/hooks/chat-hooks.ts +++ b/web/src/hooks/chat-hooks.ts @@ -84,7 +84,7 @@ export const useGetChatSearchParams = () => { //#region dialog -export const useFetchNextDialogList = () => { +export const useFetchNextDialogList = (pureFetch = false) => { const { handleClickDialog } = useClickDialogCard(); const { dialogId } = useGetChatSearchParams(); @@ -103,12 +103,14 @@ export const useFetchNextDialogList = () => { if (data.code === 0) { const list: IDialog[] = data.data; - if (list.length > 0) { - if (list.every((x) => x.id !== dialogId)) { - handleClickDialog(data.data[0].id); + if (!pureFetch) { + if (list.length > 0) { + if (list.every((x) => x.id !== dialogId)) { + handleClickDialog(data.data[0].id); + } + } else { + history.push('/chat'); } - } else { - history.push('/chat'); } } diff --git a/web/src/pages/chat/hooks.ts b/web/src/pages/chat/hooks.ts index e93206aa..962781e9 100644 --- a/web/src/pages/chat/hooks.ts +++ b/web/src/pages/chat/hooks.ts @@ -6,6 +6,7 @@ import { useFetchNextConversation, useFetchNextConversationList, useFetchNextDialog, + useFetchNextDialogList, useGetChatSearchParams, useRemoveNextConversation, useRemoveNextDialog, @@ -202,15 +203,24 @@ export const useEditDialog = () => { //#region conversation +const useFindPrologueFromDialogList = () => { + const { dialogId } = useGetChatSearchParams(); + const { data: dialogList } = useFetchNextDialogList(true); + const prologue = useMemo(() => { + return dialogList.find((x) => x.id === dialogId)?.prompt_config.prologue; + }, [dialogId, dialogList]); + + return prologue; +}; + export const useSelectDerivedConversationList = () => { const { t } = useTranslate('chat'); const [list, setList] = useState>([]); - const { data: currentDialog } = useFetchNextDialog(); const { data: conversationList, loading } = useFetchNextConversationList(); const { dialogId } = useGetChatSearchParams(); - const prologue = currentDialog?.prompt_config?.prologue ?? ''; const { setNewConversationRouteParams } = useSetNewConversationRouteParams(); + const prologue = useFindPrologueFromDialogList(); const addTemporaryConversation = useCallback(() => { const conversationId = getConversationId(); @@ -291,13 +301,11 @@ export const useSelectNextMessages = () => { removeMessagesAfterCurrentMessage, } = useSelectDerivedMessages(); const { data: conversation, loading } = useFetchNextConversation(); - const { data: dialog } = useFetchNextDialog(); const { conversationId, dialogId, isNew } = useGetChatSearchParams(); + const prologue = useFindPrologueFromDialogList(); const addPrologue = useCallback(() => { if (dialogId !== '' && isNew === 'true') { - const prologue = dialog.prompt_config?.prologue; - const nextMessage = { role: MessageType.Assistant, content: prologue, @@ -306,7 +314,7 @@ export const useSelectNextMessages = () => { setDerivedMessages([nextMessage]); } - }, [isNew, dialog, dialogId, setDerivedMessages]); + }, [dialogId, isNew, prologue, setDerivedMessages]); useEffect(() => { addPrologue();