4.6.4-alpha (#569)

This commit is contained in:
Archer
2023-12-07 13:43:08 +08:00
committed by GitHub
parent 71afe71192
commit e01c38efe0
80 changed files with 1401 additions and 1109 deletions

View File

@@ -1,7 +1,7 @@
import { sseResponseEventEnum } from '@fastgpt/service/common/response/constant';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { parseStreamChunk, SSEParseData } from '@/utils/sse';
import type { ChatHistoryItemResType } from '@fastgpt/global/core/chat/api.d';
import type { ChatHistoryItemResType } from '@fastgpt/global/core/chat/type.d';
import { StartChatFnProps } from '@/components/ChatBox';
import { getToken } from '@/web/support/user/auth';
import { ModuleOutputKeyEnum } from '@fastgpt/global/core/module/constants';

View File

@@ -346,7 +346,7 @@ export const appTemplates: (AppItemType & {
{
key: 'similarity',
type: 'slider',
label: '相度',
label: '相度',
value: 0.4,
min: 0,
max: 1,
@@ -1398,7 +1398,7 @@ export const appTemplates: (AppItemType & {
{
key: 'similarity',
type: 'slider',
label: '相度',
label: '相度',
value: 0.76,
min: 0,
max: 1,

View File

@@ -1,42 +1,53 @@
import { GET, POST, DELETE, PUT } from '@/web/common/api/request';
import type { ChatHistoryItemType } from '@fastgpt/global/core/chat/type.d';
import type { InitChatResponse } from '@fastgpt/global/core/chat/api.d';
import type { RequestPaging } from '@/types';
import { UpdateHistoryProps } from '@fastgpt/global/core/chat/api.d';
import type { AdminUpdateFeedbackParams } from '@fastgpt/global/core/chat/api.d';
import { GetChatSpeechProps } from '@/global/core/chat/api.d';
import type {
InitChatProps,
InitChatResponse,
InitOutLinkChatProps,
getHistoriesProps
} from '@/global/core/chat/api.d';
import type {
AdminUpdateFeedbackParams,
ClearHistoriesProps,
DelHistoryProps,
DeleteChatItemProps,
UpdateHistoryProps
} from '@/global/core/chat/api.d';
/**
* 获取初始化聊天内容
*/
export const getInitChatSiteInfo = (data: { appId: string; chatId?: string }) =>
export const getInitChatInfo = (data: InitChatProps) =>
GET<InitChatResponse>(`/core/chat/init`, data);
export const getInitOutLinkChatInfo = (data: InitOutLinkChatProps) =>
GET<InitChatResponse>(`/core/chat/outLink/init`, data);
/**
* 获取历史记录
* get current window history(appid or shareId)
*/
export const getChatHistory = (data: RequestPaging & { appId: string }) =>
POST<ChatHistoryItemType[]>('/core/chat/list', data);
export const getChatHistories = (data: getHistoriesProps) =>
POST<ChatHistoryItemType[]>('/core/chat/getHistories', data);
/**
* 删除一条历史记录
* delete one history
*/
export const delChatHistoryById = (chatId: string) => DELETE(`/core/chat/delete`, { chatId });
export const delChatHistoryById = (data: DelHistoryProps) => DELETE(`/core/chat/delHistory`, data);
/**
* clear all history by appid
*/
export const clearChatHistoryByAppId = (appId: string) => DELETE(`/core/chat/delete`, { appId });
export const clearChatHistoryByAppId = (data: ClearHistoriesProps) =>
DELETE(`/core/chat/clearHistories`, data);
/**
* 删除一句对话
* delete one chat record
*/
export const delChatRecordById = (data: { chatId: string; contentId: string }) =>
export const delChatRecordById = (data: DeleteChatItemProps) =>
DELETE(`/core/chat/item/delete`, data);
/**
* 修改历史记录: 标题/置顶
*/
export const putChatHistory = (data: UpdateHistoryProps) => PUT('/core/chat/update', data);
export const putChatHistory = (data: UpdateHistoryProps) => PUT('/core/chat/updateHistory', data);
export const userUpdateChatFeedback = (data: { chatItemId: string; userFeedback?: string }) =>
POST('/core/chat/feedback/userUpdate', data);

View File

@@ -2,35 +2,36 @@ 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 } from '@fastgpt/global/core/chat/api';
import { delChatHistoryById, getChatHistory, clearChatHistoryByAppId } from '@/web/core/chat/api';
import type {
InitChatResponse,
getHistoriesProps,
ClearHistoriesProps,
DelHistoryProps,
UpdateHistoryProps
} 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 = {
history: ChatHistoryItemType[];
loadHistory: (data: { appId: string }) => Promise<null>;
delHistory(history: string): Promise<void>;
clearHistory(appId: string): Promise<void>;
updateHistory: (history: ChatHistoryItemType) => void;
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;
};
const defaultChatData: InitChatResponse = {
chatId: '',
appId: '',
app: {
name: 'Loading',
avatar: '/icon/logo.svg',
intro: '',
canUse: false
},
title: '新对话',
variables: {},
history: []
delOneHistoryItem: (e: { chatId: string; contentId?: string; index: number }) => Promise<any>;
};
export const useChatStore = create<State>()(
@@ -49,49 +50,62 @@ export const useChatStore = create<State>()(
state.lastChatId = id;
});
},
history: [],
async loadHistory({ appId }) {
const oneHistory = get().history[0];
if (oneHistory && oneHistory.appId === appId) return null;
const data = await getChatHistory({
appId,
pageNum: 1,
pageSize: 20
});
histories: [],
async loadHistories(e) {
const data = await getChatHistories(e);
set((state) => {
state.history = data;
state.histories = data;
});
return null;
},
async delHistory(chatId) {
async delOneHistory(props) {
set((state) => {
state.history = state.history.filter((item) => item.chatId !== chatId);
state.histories = state.histories.filter((item) => item.chatId !== props.chatId);
});
await delChatHistoryById(chatId);
await delChatHistoryById(props);
},
async clearHistory(appId) {
async clearHistories(data) {
set((state) => {
state.history = [];
state.histories = [];
});
await clearChatHistoryByAppId(appId);
await clearChatHistoryByAppId(data);
},
updateHistory(history) {
const index = get().history.findIndex((item) => item.chatId === history.chatId);
pushHistory(history) {
set((state) => {
const newHistory = (() => {
if (index > -1) {
return [
history,
...get().history.slice(0, index),
...get().history.slice(index + 1)
];
} else {
return [history, ...state.history];
}
})();
state.histories = [history, ...state.histories];
});
},
async updateHistory(props) {
const { chatId, customTitle, top, title, updateTime } = props;
const index = get().histories.findIndex((item) => item.chatId === chatId);
state.history = newHistory;
});
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) {
@@ -104,6 +118,19 @@ export const useChatStore = create<State>()(
state.chatData = e;
});
}
},
async delOneHistoryItem({ chatId, contentId, index }) {
if (!chatId || !contentId) return;
try {
get().setChatData((state) => ({
...state,
history: state.history.filter((_, i) => i !== index)
}));
await delChatRecordById({ chatId, contentId });
} catch (err) {
console.log(err);
}
}
})),
{

View File

@@ -1,142 +1,39 @@
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import type {
ShareChatHistoryItemType,
ShareChatType
} from '@fastgpt/global/support/outLink/api.d';
import type { ChatSiteItemType } from '@fastgpt/global/core/chat/type.d';
import { HUMAN_ICON } from '@fastgpt/global/core/chat/constants';
import { chatContentReplaceBlock } from '@fastgpt/global/core/chat/utils';
import type { ChatHistoryItemType } from '@fastgpt/global/core/chat/type.d';
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZ1234567890_',
24
);
type State = {
shareChatData: ShareChatType;
setShareChatData: (e: ShareChatType | ((e: ShareChatType) => ShareChatType)) => void;
shareChatHistory: ShareChatHistoryItemType[];
saveChatResponse: (e: {
chatId: string;
prompts: ChatSiteItemType[];
variables: Record<string, any>;
shareId: string;
}) => void;
delOneShareHistoryByChatId: (chatId: string) => void;
delShareChatHistoryItemById: (e: { chatId: string; contentId?: string; index: number }) => void;
delManyShareChatHistoryByShareId: (shareId?: string) => void;
};
export const defaultHistory: ShareChatHistoryItemType = {
chatId: `${Date.now()}`,
updateTime: new Date(),
title: '新对话',
shareId: '',
chats: []
};
const defaultShareChatData: ShareChatType = {
userAvatar: HUMAN_ICON,
app: {
name: '',
avatar: '/icon/logo.svg',
intro: ''
},
history: defaultHistory
localUId: string;
shareChatHistory: (ChatHistoryItemType & { delete?: boolean })[];
clearLocalHistory: (shareId?: string) => void;
};
export const useShareChatStore = create<State>()(
devtools(
persist(
immer((set, get) => ({
shareChatData: defaultShareChatData,
setShareChatData(e) {
const val = (() => {
if (typeof e === 'function') {
return e(get().shareChatData);
} else {
return e;
}
})();
localUId: `shareChat-${Date.now()}-${nanoid()}`,
shareChatHistory: [], // old version field
clearLocalHistory() {
// abandon
set((state) => {
state.shareChatData = val;
// update history
state.shareChatHistory = state.shareChatHistory.map((item) =>
item.chatId === val.history.chatId ? val.history : item
);
});
},
shareChatHistory: [],
saveChatResponse({ chatId, prompts, variables, shareId }) {
const chatHistory = get().shareChatHistory.find((item) => item.chatId === chatId);
const newTitle =
chatContentReplaceBlock(prompts[prompts.length - 2]?.value).slice(0, 20) ||
prompts[prompts.length - 1]?.value?.slice(0, 20) ||
'Chat';
const historyList = (() => {
if (chatHistory) {
return get().shareChatHistory.map((item) =>
item.chatId === chatId
? {
...item,
title: newTitle,
updateTime: new Date(),
chats: chatHistory.chats.concat(prompts).slice(-30),
variables
}
: item
);
}
return get().shareChatHistory.concat({
chatId,
shareId,
title: newTitle,
updateTime: new Date(),
chats: prompts,
variables
});
})();
// @ts-ignore
historyList.sort((a, b) => new Date(b.updateTime) - new Date(a.updateTime));
set((state) => {
state.shareChatHistory = historyList.slice(0, 50);
});
},
delOneShareHistoryByChatId(chatId: string) {
set((state) => {
state.shareChatHistory = state.shareChatHistory.filter(
(item) => item.chatId !== chatId
);
});
},
delShareChatHistoryItemById({ chatId, contentId }) {
set((state) => {
// update history store
const newHistoryList = state.shareChatHistory.map((item) =>
item.chatId === chatId
? {
...item,
chats: item.chats.filter((item) => item.dataId !== contentId)
}
: item
);
state.shareChatHistory = newHistoryList;
});
},
delManyShareChatHistoryByShareId(shareId?: string) {
set((state) => {
if (shareId) {
state.shareChatHistory = state.shareChatHistory.filter(
(item) => item.shareId !== shareId
);
} else {
state.shareChatHistory = [];
}
state.shareChatHistory = state.shareChatHistory.map((item) => ({
...item,
delete: true
}));
});
}
})),
{
name: 'shareChatStore',
partialize: (state) => ({
localUId: state.localUId,
shareChatHistory: state.shareChatHistory
})
}

View File

@@ -79,8 +79,7 @@ export const useDatasetStore = create<State>()(
item._id === data.id
? {
...item,
...data,
tags: data.tags || []
...data
}
: item
);

View File

@@ -1,13 +1,6 @@
import { GET, POST, DELETE } from '@/web/common/api/request';
import type { InitShareChatResponse } from '@fastgpt/global/support/outLink/api.d';
import type { OutLinkEditType, OutLinkSchema } from '@fastgpt/global/support/outLink/type.d';
/**
* 初始化分享聊天
*/
export const initShareChatInfo = (data: { shareId: string; authToken?: string }) =>
GET<InitShareChatResponse>(`/support/outLink/init`, data);
/**
* create a shareChat
*/

View File

@@ -1,7 +1,5 @@
import { GET } from '@/web/common/api/request';
import type { PaySchema } from '@fastgpt/global/support/wallet/pay/type.d';
import { delay } from '@fastgpt/global/common/system/utils';
export const getPayOrders = () => GET<PaySchema[]>(`/plusApi/support/wallet/pay/getPayOrders`);
export const getPayCode = (amount: number) =>
@@ -11,15 +9,9 @@ export const getPayCode = (amount: number) =>
}>(`/plusApi/support/wallet/pay/getPayCode`, { amount });
export const checkPayResult = (payId: string) =>
GET<number>(`/plusApi/support/wallet/pay/checkPayResult`, { payId }).then(() => {
async function startQueue() {
try {
await GET('/common/system/unlockTask');
} catch (error) {
await delay(1000);
startQueue();
}
}
startQueue();
return 'success';
GET<string>(`/plusApi/support/wallet/pay/checkPayResult`, { payId }).then((data) => {
try {
GET('/common/system/unlockTask');
} catch (error) {}
return data;
});