mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-29 17:55:24 +00:00
new framwork
This commit is contained in:
228
client/src/store/chat.ts
Normal file
228
client/src/store/chat.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools, persist } from 'zustand/middleware';
|
||||
import { immer } from 'zustand/middleware/immer';
|
||||
import { OpenAiChatEnum } from '@/constants/model';
|
||||
|
||||
import {
|
||||
ChatSiteItemType,
|
||||
HistoryItemType,
|
||||
ShareChatHistoryItemType,
|
||||
ChatType,
|
||||
ShareChatType
|
||||
} from '@/types/chat';
|
||||
import { getChatHistory } from '@/api/chat';
|
||||
import { HUMAN_ICON } from '@/constants/chat';
|
||||
|
||||
type SetShareChatHistoryItem = {
|
||||
historyId: string;
|
||||
shareId: string;
|
||||
title: string;
|
||||
latestChat: string;
|
||||
chats: ChatSiteItemType[];
|
||||
};
|
||||
|
||||
type State = {
|
||||
history: HistoryItemType[];
|
||||
loadHistory: (data: { pageNum: number; init?: boolean }) => Promise<null>;
|
||||
forbidLoadChatData: boolean;
|
||||
setForbidLoadChatData: (val: boolean) => void;
|
||||
chatData: ChatType;
|
||||
setChatData: (e?: ChatType | ((e: ChatType) => ChatType)) => void;
|
||||
lastChatModelId: string;
|
||||
setLastChatModelId: (id: string) => void;
|
||||
lastChatId: string;
|
||||
setLastChatId: (id: string) => void;
|
||||
|
||||
shareChatData: ShareChatType;
|
||||
setShareChatData: (e?: ShareChatType | ((e: ShareChatType) => ShareChatType)) => void;
|
||||
password: string;
|
||||
setPassword: (val: string) => void;
|
||||
shareChatHistory: ShareChatHistoryItemType[];
|
||||
setShareChatHistory: (e: SetShareChatHistoryItem) => void;
|
||||
delShareHistoryById: (historyId: string) => void;
|
||||
delShareChatHistoryItemById: (historyId: string, index: number) => void;
|
||||
delShareChatHistory: (shareId?: string) => void;
|
||||
};
|
||||
|
||||
const defaultChatData = {
|
||||
chatId: 'chatId',
|
||||
modelId: 'modelId',
|
||||
model: {
|
||||
name: '',
|
||||
avatar: '/icon/logo.png',
|
||||
intro: '',
|
||||
canUse: false
|
||||
},
|
||||
chatModel: OpenAiChatEnum.GPT35,
|
||||
history: []
|
||||
};
|
||||
const defaultShareChatData: ShareChatType = {
|
||||
maxContext: 5,
|
||||
userAvatar: HUMAN_ICON,
|
||||
model: {
|
||||
name: '',
|
||||
avatar: '/icon/logo.png',
|
||||
intro: ''
|
||||
},
|
||||
chatModel: 'gpt-3.5-turbo',
|
||||
history: []
|
||||
};
|
||||
|
||||
export const useChatStore = create<State>()(
|
||||
devtools(
|
||||
persist(
|
||||
immer((set, get) => ({
|
||||
lastChatModelId: '',
|
||||
setLastChatModelId(id: string) {
|
||||
set((state) => {
|
||||
state.lastChatModelId = id;
|
||||
});
|
||||
},
|
||||
lastChatId: '',
|
||||
setLastChatId(id: string) {
|
||||
set((state) => {
|
||||
state.lastChatId = id;
|
||||
});
|
||||
},
|
||||
history: [],
|
||||
async loadHistory({ pageNum, init = false }: { pageNum: number; init?: boolean }) {
|
||||
if (get().history.length > 0 && !init) return null;
|
||||
const data = await getChatHistory({
|
||||
pageNum,
|
||||
pageSize: 20
|
||||
});
|
||||
set((state) => {
|
||||
state.history = data;
|
||||
});
|
||||
return null;
|
||||
},
|
||||
forbidLoadChatData: false,
|
||||
setForbidLoadChatData(val: boolean) {
|
||||
set((state) => {
|
||||
state.forbidLoadChatData = val;
|
||||
});
|
||||
},
|
||||
chatData: defaultChatData,
|
||||
setChatData(e: ChatType | ((e: ChatType) => ChatType) = defaultChatData) {
|
||||
if (typeof e === 'function') {
|
||||
set((state) => {
|
||||
state.chatData = e(state.chatData);
|
||||
});
|
||||
} else {
|
||||
set((state) => {
|
||||
state.chatData = e;
|
||||
});
|
||||
}
|
||||
},
|
||||
shareChatData: defaultShareChatData,
|
||||
setShareChatData(
|
||||
e: ShareChatType | ((e: ShareChatType) => ShareChatType) = defaultShareChatData
|
||||
) {
|
||||
if (typeof e === 'function') {
|
||||
set((state) => {
|
||||
state.shareChatData = e(state.shareChatData);
|
||||
});
|
||||
} else {
|
||||
set((state) => {
|
||||
state.shareChatData = e;
|
||||
});
|
||||
}
|
||||
},
|
||||
password: '',
|
||||
setPassword(val: string) {
|
||||
set((state) => {
|
||||
state.password = val;
|
||||
});
|
||||
},
|
||||
shareChatHistory: [],
|
||||
setShareChatHistory({
|
||||
historyId,
|
||||
shareId,
|
||||
title,
|
||||
latestChat,
|
||||
chats = []
|
||||
}: SetShareChatHistoryItem) {
|
||||
set((state) => {
|
||||
const history = state.shareChatHistory.find((item) => item._id === historyId);
|
||||
let historyList: ShareChatHistoryItemType[] = [];
|
||||
if (history) {
|
||||
historyList = state.shareChatHistory.map((item) =>
|
||||
item._id === historyId
|
||||
? {
|
||||
...item,
|
||||
title,
|
||||
latestChat,
|
||||
updateTime: new Date(),
|
||||
chats
|
||||
}
|
||||
: item
|
||||
);
|
||||
} else {
|
||||
historyList = [
|
||||
...state.shareChatHistory,
|
||||
{
|
||||
_id: historyId,
|
||||
shareId,
|
||||
title,
|
||||
latestChat,
|
||||
updateTime: new Date(),
|
||||
chats
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
historyList.sort((a, b) => new Date(b.updateTime) - new Date(a.updateTime));
|
||||
|
||||
state.shareChatHistory = historyList.slice(0, 30);
|
||||
});
|
||||
},
|
||||
delShareHistoryById(historyId: string) {
|
||||
set((state) => {
|
||||
state.shareChatHistory = state.shareChatHistory.filter(
|
||||
(item) => item._id !== historyId
|
||||
);
|
||||
});
|
||||
},
|
||||
delShareChatHistoryItemById(historyId: string, index: number) {
|
||||
set((state) => {
|
||||
// update history store
|
||||
const newHistoryList = state.shareChatHistory.map((item) =>
|
||||
item._id === historyId
|
||||
? {
|
||||
...item,
|
||||
chats: [...item.chats.slice(0, index), ...item.chats.slice(index + 1)]
|
||||
}
|
||||
: item
|
||||
);
|
||||
state.shareChatHistory = newHistoryList;
|
||||
|
||||
// update chatData
|
||||
state.shareChatData.history =
|
||||
newHistoryList.find((item) => item._id === historyId)?.chats || [];
|
||||
});
|
||||
},
|
||||
delShareChatHistory(shareId?: string) {
|
||||
set((state) => {
|
||||
if (shareId) {
|
||||
state.shareChatHistory = state.shareChatHistory.filter(
|
||||
(item) => item.shareId !== shareId
|
||||
);
|
||||
} else {
|
||||
state.shareChatHistory = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
})),
|
||||
{
|
||||
name: 'chatStore',
|
||||
partialize: (state) => ({
|
||||
lastChatModelId: state.lastChatModelId,
|
||||
lastChatId: state.lastChatId,
|
||||
password: state.password,
|
||||
shareChatHistory: state.shareChatHistory
|
||||
})
|
||||
}
|
||||
)
|
||||
)
|
||||
);
|
49
client/src/store/global.ts
Normal file
49
client/src/store/global.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { immer } from 'zustand/middleware/immer';
|
||||
import type { InitDateResponse } from '@/pages/api/system/getInitData';
|
||||
import { getInitData } from '@/api/system';
|
||||
|
||||
type State = {
|
||||
initData: InitDateResponse;
|
||||
loadInitData: () => Promise<void>;
|
||||
loading: boolean;
|
||||
setLoading: (val: boolean) => null;
|
||||
screenWidth: number;
|
||||
setScreenWidth: (val: number) => void;
|
||||
isPc: boolean;
|
||||
};
|
||||
|
||||
export const useGlobalStore = create<State>()(
|
||||
devtools(
|
||||
immer((set, get) => ({
|
||||
initData: {
|
||||
beianText: '',
|
||||
googleVerKey: ''
|
||||
},
|
||||
async loadInitData() {
|
||||
try {
|
||||
const res = await getInitData();
|
||||
set((state) => {
|
||||
state.initData = res;
|
||||
});
|
||||
} catch (error) {}
|
||||
},
|
||||
loading: false,
|
||||
setLoading: (val: boolean) => {
|
||||
set((state) => {
|
||||
state.loading = val;
|
||||
});
|
||||
return null;
|
||||
},
|
||||
screenWidth: 600,
|
||||
setScreenWidth(val: number) {
|
||||
set((state) => {
|
||||
state.screenWidth = val;
|
||||
state.isPc = val < 900 ? false : true;
|
||||
});
|
||||
},
|
||||
isPc: false
|
||||
}))
|
||||
)
|
||||
);
|
155
client/src/store/user.ts
Normal file
155
client/src/store/user.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools, persist } from 'zustand/middleware';
|
||||
import { immer } from 'zustand/middleware/immer';
|
||||
import type { UserType, UserUpdateParams } from '@/types/user';
|
||||
import { getMyModels, getModelById } from '@/api/model';
|
||||
import { formatPrice } from '@/utils/user';
|
||||
import { getTokenLogin } from '@/api/user';
|
||||
import { defaultModel } from '@/constants/model';
|
||||
import { ModelListItemType } from '@/types/model';
|
||||
import { KbItemType } from '@/types/plugin';
|
||||
import { getKbList, getKbById } from '@/api/plugins/kb';
|
||||
import { defaultKbDetail } from '@/constants/kb';
|
||||
import type { ModelSchema } from '@/types/mongoSchema';
|
||||
|
||||
type State = {
|
||||
userInfo: UserType | null;
|
||||
initUserInfo: () => Promise<UserType>;
|
||||
setUserInfo: (user: UserType | null) => void;
|
||||
updateUserInfo: (user: UserUpdateParams) => void;
|
||||
// model
|
||||
lastModelId: string;
|
||||
setLastModelId: (id: string) => void;
|
||||
myModels: ModelListItemType[];
|
||||
myCollectionModels: ModelListItemType[];
|
||||
loadMyModels: (init?: boolean) => Promise<null>;
|
||||
modelDetail: ModelSchema;
|
||||
loadModelDetail: (id: string, init?: boolean) => Promise<ModelSchema>;
|
||||
refreshModel: {
|
||||
freshMyModels(): void;
|
||||
updateModelDetail(model: ModelSchema): void;
|
||||
removeModelDetail(modelId: string): void;
|
||||
};
|
||||
// kb
|
||||
lastKbId: string;
|
||||
setLastKbId: (id: string) => void;
|
||||
myKbList: KbItemType[];
|
||||
loadKbList: (init?: boolean) => Promise<KbItemType[]>;
|
||||
kbDetail: KbItemType;
|
||||
getKbDetail: (id: string, init?: boolean) => Promise<KbItemType>;
|
||||
};
|
||||
|
||||
export const useUserStore = create<State>()(
|
||||
devtools(
|
||||
persist(
|
||||
immer((set, get) => ({
|
||||
userInfo: null,
|
||||
async initUserInfo() {
|
||||
const res = await getTokenLogin();
|
||||
get().setUserInfo(res);
|
||||
return res;
|
||||
},
|
||||
setUserInfo(user: UserType | null) {
|
||||
set((state) => {
|
||||
state.userInfo = user
|
||||
? {
|
||||
...user,
|
||||
balance: formatPrice(user.balance)
|
||||
}
|
||||
: null;
|
||||
});
|
||||
},
|
||||
updateUserInfo(user: UserUpdateParams) {
|
||||
set((state) => {
|
||||
if (!state.userInfo) return;
|
||||
state.userInfo = {
|
||||
...state.userInfo,
|
||||
...user
|
||||
};
|
||||
});
|
||||
},
|
||||
lastModelId: '',
|
||||
setLastModelId(id: string) {
|
||||
set((state) => {
|
||||
state.lastModelId = id;
|
||||
});
|
||||
},
|
||||
myModels: [],
|
||||
myCollectionModels: [],
|
||||
async loadMyModels(init = false) {
|
||||
if (get().myModels.length > 0 && !init) return null;
|
||||
const res = await getMyModels();
|
||||
set((state) => {
|
||||
state.myModels = res.myModels;
|
||||
state.myCollectionModels = res.myCollectionModels;
|
||||
});
|
||||
return null;
|
||||
},
|
||||
modelDetail: defaultModel,
|
||||
async loadModelDetail(id: string, init = false) {
|
||||
if (id === get().modelDetail._id && !init) return get().modelDetail;
|
||||
|
||||
const res = await getModelById(id);
|
||||
set((state) => {
|
||||
state.modelDetail = res;
|
||||
});
|
||||
return res;
|
||||
},
|
||||
refreshModel: {
|
||||
freshMyModels() {
|
||||
get().loadMyModels(true);
|
||||
},
|
||||
updateModelDetail(model: ModelSchema) {
|
||||
set((state) => {
|
||||
state.modelDetail = model;
|
||||
});
|
||||
get().loadMyModels(true);
|
||||
},
|
||||
removeModelDetail(modelId: string) {
|
||||
if (modelId === get().modelDetail._id) {
|
||||
set((state) => {
|
||||
state.modelDetail = defaultModel;
|
||||
state.lastModelId = '';
|
||||
});
|
||||
}
|
||||
get().loadMyModels(true);
|
||||
}
|
||||
},
|
||||
lastKbId: '',
|
||||
setLastKbId(id: string) {
|
||||
set((state) => {
|
||||
state.lastKbId = id;
|
||||
});
|
||||
},
|
||||
myKbList: [],
|
||||
async loadKbList(init = false) {
|
||||
if (get().myKbList.length > 0 && !init) return get().myKbList;
|
||||
const res = await getKbList();
|
||||
set((state) => {
|
||||
state.myKbList = res;
|
||||
});
|
||||
return res;
|
||||
},
|
||||
kbDetail: defaultKbDetail,
|
||||
async getKbDetail(id: string, init = false) {
|
||||
if (id === get().kbDetail._id && !init) return get().kbDetail;
|
||||
|
||||
const data = await getKbById(id);
|
||||
|
||||
set((state) => {
|
||||
state.kbDetail = data;
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
})),
|
||||
{
|
||||
name: 'userStore',
|
||||
partialize: (state) => ({
|
||||
lastModelId: state.lastModelId,
|
||||
lastKbId: state.lastKbId
|
||||
})
|
||||
}
|
||||
)
|
||||
)
|
||||
);
|
Reference in New Issue
Block a user