4.8.5 test (#1819)

This commit is contained in:
Archer
2024-06-21 18:32:05 +08:00
committed by GitHub
parent 5cc01b8509
commit 24596a6e21
40 changed files with 908 additions and 1058 deletions

View File

@@ -43,7 +43,7 @@ export const delChatHistoryById = (data: DelHistoryProps) => DELETE(`/core/chat/
/**
* clear all history by appid
*/
export const clearChatHistoryByAppId = (data: ClearHistoriesProps) =>
export const delClearChatHistories = (data: ClearHistoriesProps) =>
DELETE(`/core/chat/clearHistories`, data);
/**

View File

@@ -0,0 +1,151 @@
import { useRequest2 } from '@fastgpt/web/hooks/useRequest';
import { useRouter } from 'next/router';
import React, { ReactNode, useCallback, useEffect, useRef } from 'react';
import { createContext } from 'use-context-selector';
import { delClearChatHistories, delChatHistoryById, putChatHistory } from '../api';
import { ChatHistoryItemType } from '@fastgpt/global/core/chat/type';
import { ClearHistoriesProps, DelHistoryProps, UpdateHistoryProps } from '@/global/core/chat/api';
import { useDisclosure } from '@chakra-ui/react';
import { useChatStore } from './storeChat';
type ChatContextValueType = {
histories: ChatHistoryItemType[];
loadHistories: () => Promise<ChatHistoryItemType[]>;
};
type ChatContextType = ChatContextValueType & {
chatId: string;
onUpdateHistory: (data: UpdateHistoryProps) => void;
onDelHistory: (data: DelHistoryProps) => Promise<undefined>;
onClearHistories: (data: ClearHistoriesProps) => Promise<undefined>;
isOpenSlider: boolean;
onCloseSlider: () => void;
onOpenSlider: () => void;
forbidLoadChat: React.MutableRefObject<boolean>;
onChangeChatId: (chatId?: string, forbid?: boolean) => void;
onChangeAppId: (appId: string) => void;
isLoading: boolean;
};
export const ChatContext = createContext<ChatContextType>({
chatId: '',
// forbidLoadChat: undefined,
histories: [],
loadHistories: function (): Promise<ChatHistoryItemType[]> {
throw new Error('Function not implemented.');
},
onUpdateHistory: function (data: UpdateHistoryProps): void {
throw new Error('Function not implemented.');
},
onDelHistory: function (data: DelHistoryProps): Promise<undefined> {
throw new Error('Function not implemented.');
},
onClearHistories: function (data: ClearHistoriesProps): Promise<undefined> {
throw new Error('Function not implemented.');
},
isOpenSlider: false,
onCloseSlider: function (): void {
throw new Error('Function not implemented.');
},
onOpenSlider: function (): void {
throw new Error('Function not implemented.');
},
forbidLoadChat: { current: false },
onChangeChatId: function (chatId?: string | undefined, forbid?: boolean | undefined): void {
throw new Error('Function not implemented.');
},
onChangeAppId: function (appId: string): void {
throw new Error('Function not implemented.');
},
isLoading: false
});
const ChatContextProvider = ({
children,
histories,
loadHistories
}: ChatContextValueType & { children: ReactNode }) => {
const router = useRouter();
const { chatId = '' } = router.query as { chatId: string };
const isSystemChat = router.pathname === '/chat';
const forbidLoadChat = useRef(false);
const { isOpen: isOpenSlider, onClose: onCloseSlider, onOpen: onOpenSlider } = useDisclosure();
const { setLastChatId } = useChatStore();
const onChangeChatId = useCallback(
(changeChatId = '', forbid = false) => {
if (chatId !== changeChatId) {
forbidLoadChat.current = forbid;
setLastChatId(changeChatId);
router.replace({
query: {
...router.query,
chatId: changeChatId || ''
}
});
}
onCloseSlider();
},
[chatId, onCloseSlider, router, setLastChatId]
);
useEffect(() => {
setLastChatId(chatId);
}, [chatId, setLastChatId]);
const onChangeAppId = useCallback(
(appId: string) => {
router.replace({
query: {
...router.query,
chatId: '',
appId
}
});
onCloseSlider();
},
[onCloseSlider, router]
);
const { runAsync: onUpdateHistory, loading: isUpdatingHistory } = useRequest2(putChatHistory, {
onSuccess() {
loadHistories();
}
});
const { runAsync: onDelHistory, loading: isDeletingHistory } = useRequest2(delChatHistoryById, {
onSuccess() {
loadHistories();
}
});
const { runAsync: onClearHistories, loading: isClearingHistory } = useRequest2(
delClearChatHistories,
{
onSuccess() {
loadHistories();
},
onFinally() {
onChangeChatId('');
}
}
);
const isLoading = isUpdatingHistory || isDeletingHistory || isClearingHistory;
const contextValue = {
chatId,
histories,
loadHistories,
onUpdateHistory,
onDelHistory,
onClearHistories,
isOpenSlider,
onCloseSlider,
onOpenSlider,
forbidLoadChat,
onChangeChatId,
onChangeAppId,
isLoading
};
return <ChatContext.Provider value={contextValue}>{children}</ChatContext.Provider>;
};
export default ChatContextProvider;

View File

@@ -0,0 +1,39 @@
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import type { DeleteChatItemProps } from '@/global/core/chat/api';
type State = {
lastChatAppId: string;
setLastChatAppId: (id: string) => void;
lastChatId: string;
setLastChatId: (id: string) => void;
};
export const useChatStore = create<State>()(
devtools(
persist(
immer((set, get) => ({
lastChatAppId: '',
setLastChatAppId(id: string) {
set((state) => {
state.lastChatAppId = id;
});
},
lastChatId: '',
setLastChatId(id: string) {
set((state) => {
state.lastChatId = id;
});
}
})),
{
name: 'chatStore',
partialize: (state) => ({
lastChatAppId: state.lastChatAppId,
lastChatId: state.lastChatId
})
}
)
)
);

View File

@@ -1,147 +0,0 @@
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import type { ChatHistoryItemType } from '@fastgpt/global/core/chat/type.d';
import type {
InitChatResponse,
GetHistoriesProps,
ClearHistoriesProps,
DelHistoryProps,
UpdateHistoryProps,
DeleteChatItemProps
} from '@/global/core/chat/api';
import {
delChatHistoryById,
getChatHistories,
clearChatHistoryByAppId,
delChatRecordById,
putChatHistory
} from '@/web/core/chat/api';
import { defaultChatData } from '@/global/core/chat/constants';
type State = {
histories: ChatHistoryItemType[];
loadHistories: (data: GetHistoriesProps) => Promise<null>;
delOneHistory(data: DelHistoryProps): Promise<void>;
clearHistories(data: ClearHistoriesProps): Promise<void>;
pushHistory: (history: ChatHistoryItemType) => void;
updateHistory: (e: UpdateHistoryProps & { updateTime?: Date; title?: string }) => Promise<any>;
chatData: InitChatResponse;
setChatData: (e: InitChatResponse | ((e: InitChatResponse) => InitChatResponse)) => void;
lastChatAppId: string;
setLastChatAppId: (id: string) => void;
lastChatId: string;
setLastChatId: (id: string) => void;
delOneHistoryItem: (e: DeleteChatItemProps) => Promise<any>;
};
export const useChatStore = create<State>()(
devtools(
persist(
immer((set, get) => ({
lastChatAppId: '',
setLastChatAppId(id: string) {
set((state) => {
state.lastChatAppId = id;
});
},
lastChatId: '',
setLastChatId(id: string) {
set((state) => {
state.lastChatId = id;
});
},
histories: [],
async loadHistories(e) {
const data = await getChatHistories(e);
set((state) => {
state.histories = data;
});
return null;
},
async delOneHistory(props) {
set((state) => {
state.histories = state.histories.filter((item) => item.chatId !== props.chatId);
});
await delChatHistoryById(props);
},
async clearHistories(data) {
set((state) => {
state.histories = [];
});
await clearChatHistoryByAppId(data);
},
pushHistory(history) {
set((state) => {
state.histories = [history, ...state.histories];
});
},
async updateHistory(props) {
const { chatId, customTitle, top, title, updateTime } = props;
const index = get().histories.findIndex((item) => item.chatId === chatId);
if (index > -1) {
const newHistory = {
...get().histories[index],
...(title && { title }),
...(updateTime && { updateTime }),
...(customTitle !== undefined && { customTitle }),
...(top !== undefined && { top })
};
if (customTitle !== undefined || top !== undefined) {
try {
putChatHistory(props);
} catch (error) {}
}
set((state) => {
const newHistories = (() => {
return [
newHistory,
...get().histories.slice(0, index),
...get().histories.slice(index + 1)
];
})();
state.histories = newHistories;
});
}
},
chatData: defaultChatData,
setChatData(e = defaultChatData) {
if (typeof e === 'function') {
set((state) => {
state.chatData = e(state.chatData);
});
} else {
set((state) => {
state.chatData = e;
});
}
},
async delOneHistoryItem(props) {
const { chatId, contentId } = props;
if (!chatId || !contentId) return;
try {
get().setChatData((state) => ({
...state,
history: state.history.filter((item) => item.dataId !== contentId)
}));
await delChatRecordById(props);
} catch (err) {
console.log(err);
}
}
})),
{
name: 'chatStore',
partialize: (state) => ({
lastChatAppId: state.lastChatAppId,
lastChatId: state.lastChatId
})
}
)
)
);

View File

@@ -10,32 +10,16 @@ const nanoid = customAlphabet(
type State = {
localUId: string;
shareChatHistory: (ChatHistoryItemType & { delete?: boolean })[];
clearLocalHistory: (shareId?: string) => void;
};
export const useShareChatStore = create<State>()(
devtools(
persist(
immer((set, get) => ({
localUId: `shareChat-${Date.now()}-${nanoid()}`,
shareChatHistory: [], // old version field
clearLocalHistory() {
// abandon
set((state) => {
state.shareChatHistory = state.shareChatHistory.map((item) => ({
...item,
delete: true
}));
});
}
localUId: `shareChat-${Date.now()}-${nanoid()}`
})),
{
name: 'shareChatStore',
partialize: (state) => ({
localUId: state.localUId,
shareChatHistory: state.shareChatHistory
})
name: 'shareChatStore'
}
)
)