2024-02-22 17:14:25 +08:00
|
|
|
|
import showDeleteConfirm from '@/components/deleting-confirm';
|
|
|
|
|
import { MessageType } from '@/constants/chat';
|
2024-02-26 18:38:54 +08:00
|
|
|
|
import { IConversation, IDialog } from '@/interfaces/database/chat';
|
2024-02-22 17:14:25 +08:00
|
|
|
|
import omit from 'lodash/omit';
|
2024-02-26 18:38:54 +08:00
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
2024-02-22 17:14:25 +08:00
|
|
|
|
import { useDispatch, useSearchParams, useSelector } from 'umi';
|
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
import { ChatSearchParams, EmptyConversationId } from './constants';
|
|
|
|
|
import {
|
|
|
|
|
IClientConversation,
|
|
|
|
|
IMessage,
|
|
|
|
|
VariableTableDataType,
|
|
|
|
|
} from './interface';
|
2024-02-26 18:38:54 +08:00
|
|
|
|
import { ChatModelState } from './model';
|
|
|
|
|
import { isConversationIdNotExist } from './utils';
|
2024-02-20 18:10:20 +08:00
|
|
|
|
|
|
|
|
|
export const useFetchDialogList = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const dialogList: IDialog[] = useSelector(
|
|
|
|
|
(state: any) => state.chatModel.dialogList,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
dispatch({ type: 'chatModel/listDialog' });
|
|
|
|
|
}, [dispatch]);
|
|
|
|
|
|
|
|
|
|
return dialogList;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSetDialog = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const setDialog = useCallback(
|
|
|
|
|
(payload: IDialog) => {
|
2024-02-22 17:14:25 +08:00
|
|
|
|
return dispatch<any>({ type: 'chatModel/setDialog', payload });
|
2024-02-20 18:10:20 +08:00
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return setDialog;
|
|
|
|
|
};
|
2024-02-22 17:14:25 +08:00
|
|
|
|
|
|
|
|
|
export const useFetchDialog = (dialogId: string, visible: boolean): IDialog => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const currentDialog: IDialog = useSelector(
|
|
|
|
|
(state: any) => state.chatModel.currentDialog,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const fetchDialog = useCallback(() => {
|
|
|
|
|
if (dialogId) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'chatModel/getDialog',
|
|
|
|
|
payload: { dialog_id: dialogId },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [dispatch, dialogId]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (dialogId && visible) {
|
|
|
|
|
fetchDialog();
|
|
|
|
|
}
|
|
|
|
|
}, [dialogId, fetchDialog, visible]);
|
|
|
|
|
|
|
|
|
|
return currentDialog;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSetCurrentDialog = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const currentDialog: IDialog = useSelector(
|
|
|
|
|
(state: any) => state.chatModel.currentDialog,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const setCurrentDialog = useCallback(
|
|
|
|
|
(dialogId: string) => {
|
|
|
|
|
if (dialogId) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'chatModel/setCurrentDialog',
|
|
|
|
|
payload: { id: dialogId },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { currentDialog, setCurrentDialog };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useResetCurrentDialog = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const resetCurrentDialog = useCallback(() => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'chatModel/setCurrentDialog',
|
|
|
|
|
payload: {},
|
|
|
|
|
});
|
|
|
|
|
}, [dispatch]);
|
|
|
|
|
|
|
|
|
|
return { resetCurrentDialog };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSelectPromptConfigParameters = (): VariableTableDataType[] => {
|
|
|
|
|
const currentDialog: IDialog = useSelector(
|
|
|
|
|
(state: any) => state.chatModel.currentDialog,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const finalParameters: VariableTableDataType[] = useMemo(() => {
|
|
|
|
|
const parameters = currentDialog?.prompt_config?.parameters ?? [];
|
|
|
|
|
if (!currentDialog.id) {
|
|
|
|
|
// The newly created chat has a default parameter
|
|
|
|
|
return [{ key: uuid(), variable: 'knowledge', optional: false }];
|
|
|
|
|
}
|
|
|
|
|
return parameters.map((x) => ({
|
|
|
|
|
key: uuid(),
|
|
|
|
|
variable: x.key,
|
|
|
|
|
optional: x.optional,
|
|
|
|
|
}));
|
|
|
|
|
}, [currentDialog]);
|
|
|
|
|
|
|
|
|
|
return finalParameters;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useRemoveDialog = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const removeDocument = (dialogIds: Array<string>) => () => {
|
|
|
|
|
return dispatch({
|
|
|
|
|
type: 'chatModel/removeDialog',
|
|
|
|
|
payload: {
|
|
|
|
|
dialog_ids: dialogIds,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onRemoveDialog = (dialogIds: Array<string>) => {
|
|
|
|
|
showDeleteConfirm({ onOk: removeDocument(dialogIds) });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { onRemoveDialog };
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-26 18:38:54 +08:00
|
|
|
|
export const useGetChatSearchParams = () => {
|
|
|
|
|
const [currentQueryParameters] = useSearchParams();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dialogId: currentQueryParameters.get(ChatSearchParams.DialogId) || '',
|
|
|
|
|
conversationId:
|
|
|
|
|
currentQueryParameters.get(ChatSearchParams.ConversationId) || '',
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSetCurrentConversation = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const setCurrentConversation = useCallback(
|
|
|
|
|
(currentConversation: IClientConversation) => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'chatModel/setCurrentConversation',
|
|
|
|
|
payload: currentConversation,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[dispatch],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return setCurrentConversation;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
|
export const useClickDialogCard = () => {
|
|
|
|
|
const [currentQueryParameters, setSearchParams] = useSearchParams();
|
|
|
|
|
|
|
|
|
|
const newQueryParameters: URLSearchParams = useMemo(() => {
|
2024-02-26 18:38:54 +08:00
|
|
|
|
return new URLSearchParams();
|
|
|
|
|
}, []);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
|
|
|
|
|
const handleClickDialog = useCallback(
|
|
|
|
|
(dialogId: string) => {
|
|
|
|
|
newQueryParameters.set(ChatSearchParams.DialogId, dialogId);
|
2024-02-26 18:38:54 +08:00
|
|
|
|
// newQueryParameters.set(
|
|
|
|
|
// ChatSearchParams.ConversationId,
|
|
|
|
|
// EmptyConversationId,
|
|
|
|
|
// );
|
2024-02-22 17:14:25 +08:00
|
|
|
|
setSearchParams(newQueryParameters);
|
|
|
|
|
},
|
|
|
|
|
[newQueryParameters, setSearchParams],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { handleClickDialog };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSelectFirstDialogOnMount = () => {
|
|
|
|
|
const dialogList = useFetchDialogList();
|
|
|
|
|
const { dialogId } = useGetChatSearchParams();
|
|
|
|
|
|
|
|
|
|
const { handleClickDialog } = useClickDialogCard();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (dialogList.length > 0 && !dialogId) {
|
|
|
|
|
handleClickDialog(dialogList[0].id);
|
|
|
|
|
}
|
|
|
|
|
}, [dialogList, handleClickDialog, dialogId]);
|
|
|
|
|
|
|
|
|
|
return dialogList;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//#region conversation
|
|
|
|
|
|
|
|
|
|
export const useCreateTemporaryConversation = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const { dialogId } = useGetChatSearchParams();
|
|
|
|
|
const { handleClickConversation } = useClickConversationCard();
|
|
|
|
|
let chatModel = useSelector((state: any) => state.chatModel);
|
2024-02-26 18:38:54 +08:00
|
|
|
|
|
|
|
|
|
const currentConversation: Pick<
|
2024-02-22 17:14:25 +08:00
|
|
|
|
IClientConversation,
|
|
|
|
|
'id' | 'message' | 'name' | 'dialog_id'
|
|
|
|
|
> = chatModel.currentConversation;
|
|
|
|
|
|
2024-02-26 18:38:54 +08:00
|
|
|
|
const conversationList: IClientConversation[] = chatModel.conversationList;
|
|
|
|
|
const currentDialog: IDialog = chatModel.currentDialog;
|
|
|
|
|
|
|
|
|
|
const setCurrentConversation = useSetCurrentConversation();
|
|
|
|
|
|
|
|
|
|
const createTemporaryConversation = useCallback(() => {
|
|
|
|
|
const firstConversation = conversationList[0];
|
|
|
|
|
const messages = [...(firstConversation?.message ?? [])];
|
2024-02-22 17:14:25 +08:00
|
|
|
|
if (messages.some((x) => x.id === EmptyConversationId)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-02-26 18:38:54 +08:00
|
|
|
|
messages.push({
|
2024-02-22 17:14:25 +08:00
|
|
|
|
id: EmptyConversationId,
|
2024-02-26 18:38:54 +08:00
|
|
|
|
content: currentDialog?.prompt_config?.prologue ?? '',
|
2024-02-22 17:14:25 +08:00
|
|
|
|
role: MessageType.Assistant,
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-26 18:38:54 +08:00
|
|
|
|
let nextCurrentConversation = currentConversation;
|
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
|
// It’s the back-end data.
|
|
|
|
|
if ('id' in currentConversation) {
|
2024-02-26 18:38:54 +08:00
|
|
|
|
nextCurrentConversation = { ...currentConversation, message: messages };
|
2024-02-22 17:14:25 +08:00
|
|
|
|
} else {
|
|
|
|
|
// client data
|
2024-02-26 18:38:54 +08:00
|
|
|
|
nextCurrentConversation = {
|
2024-02-22 17:14:25 +08:00
|
|
|
|
id: EmptyConversationId,
|
|
|
|
|
name: 'New conversation',
|
|
|
|
|
dialog_id: dialogId,
|
|
|
|
|
message: messages,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextConversationList = [...conversationList];
|
|
|
|
|
|
2024-02-26 18:38:54 +08:00
|
|
|
|
nextConversationList.unshift(
|
|
|
|
|
nextCurrentConversation as IClientConversation,
|
|
|
|
|
);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
|
2024-02-26 18:38:54 +08:00
|
|
|
|
setCurrentConversation(nextCurrentConversation as IClientConversation);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'chatModel/setConversationList',
|
|
|
|
|
payload: nextConversationList,
|
|
|
|
|
});
|
|
|
|
|
handleClickConversation(EmptyConversationId);
|
2024-02-26 18:38:54 +08:00
|
|
|
|
}, [
|
|
|
|
|
dispatch,
|
|
|
|
|
currentConversation,
|
|
|
|
|
dialogId,
|
|
|
|
|
setCurrentConversation,
|
|
|
|
|
handleClickConversation,
|
|
|
|
|
conversationList,
|
|
|
|
|
currentDialog,
|
|
|
|
|
]);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
|
|
|
|
|
return { createTemporaryConversation };
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-26 18:38:54 +08:00
|
|
|
|
export const useFetchConversationList = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const conversationList: any[] = useSelector(
|
|
|
|
|
(state: any) => state.chatModel.conversationList,
|
|
|
|
|
);
|
|
|
|
|
const { dialogId } = useGetChatSearchParams();
|
|
|
|
|
|
|
|
|
|
const fetchConversationList = useCallback(async () => {
|
|
|
|
|
if (dialogId) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'chatModel/listConversation',
|
|
|
|
|
payload: { dialog_id: dialogId },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [dispatch, dialogId]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchConversationList();
|
|
|
|
|
}, [fetchConversationList]);
|
|
|
|
|
|
|
|
|
|
return conversationList;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSelectConversationList = () => {
|
|
|
|
|
const [list, setList] = useState<Array<IConversation>>([]);
|
|
|
|
|
let chatModel: ChatModelState = useSelector((state: any) => state.chatModel);
|
|
|
|
|
const { conversationList, currentDialog } = chatModel;
|
|
|
|
|
const { dialogId } = useGetChatSearchParams();
|
|
|
|
|
const prologue = currentDialog?.prompt_config?.prologue ?? '';
|
|
|
|
|
|
|
|
|
|
const addTemporaryConversation = useCallback(() => {
|
|
|
|
|
setList(() => {
|
|
|
|
|
const nextList = [
|
|
|
|
|
{
|
|
|
|
|
id: '',
|
|
|
|
|
name: 'New conversation',
|
|
|
|
|
dialog_id: dialogId,
|
|
|
|
|
message: [
|
|
|
|
|
{
|
|
|
|
|
content: prologue,
|
|
|
|
|
role: MessageType.Assistant,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
} as IConversation,
|
|
|
|
|
...conversationList,
|
|
|
|
|
];
|
|
|
|
|
return nextList;
|
|
|
|
|
});
|
|
|
|
|
}, [conversationList, dialogId, prologue]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
addTemporaryConversation();
|
|
|
|
|
}, [addTemporaryConversation]);
|
|
|
|
|
|
|
|
|
|
return { list, addTemporaryConversation };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useClickConversationCard = () => {
|
|
|
|
|
const [currentQueryParameters, setSearchParams] = useSearchParams();
|
|
|
|
|
const newQueryParameters: URLSearchParams = useMemo(
|
|
|
|
|
() => new URLSearchParams(currentQueryParameters.toString()),
|
|
|
|
|
[currentQueryParameters],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleClickConversation = useCallback(
|
|
|
|
|
(conversationId: string) => {
|
|
|
|
|
newQueryParameters.set(ChatSearchParams.ConversationId, conversationId);
|
|
|
|
|
setSearchParams(newQueryParameters);
|
|
|
|
|
},
|
|
|
|
|
[newQueryParameters, setSearchParams],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { handleClickConversation };
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
|
export const useSetConversation = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const { dialogId } = useGetChatSearchParams();
|
|
|
|
|
|
|
|
|
|
const setConversation = (message: string) => {
|
|
|
|
|
return dispatch<any>({
|
|
|
|
|
type: 'chatModel/setConversation',
|
|
|
|
|
payload: {
|
|
|
|
|
// conversation_id: '',
|
|
|
|
|
dialog_id: dialogId,
|
|
|
|
|
name: message,
|
|
|
|
|
message: [
|
|
|
|
|
{
|
|
|
|
|
role: MessageType.Assistant,
|
|
|
|
|
content: message,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { setConversation };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useFetchConversation = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const { conversationId } = useGetChatSearchParams();
|
|
|
|
|
const conversation = useSelector(
|
|
|
|
|
(state: any) => state.chatModel.currentConversation,
|
|
|
|
|
);
|
2024-02-26 18:38:54 +08:00
|
|
|
|
const setCurrentConversation = useSetCurrentConversation();
|
2024-02-22 17:14:25 +08:00
|
|
|
|
|
|
|
|
|
const fetchConversation = useCallback(() => {
|
2024-02-26 18:38:54 +08:00
|
|
|
|
if (isConversationIdNotExist(conversationId)) {
|
|
|
|
|
dispatch<any>({
|
2024-02-22 17:14:25 +08:00
|
|
|
|
type: 'chatModel/getConversation',
|
|
|
|
|
payload: {
|
|
|
|
|
conversation_id: conversationId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-02-26 18:38:54 +08:00
|
|
|
|
} else {
|
|
|
|
|
setCurrentConversation({} as IClientConversation);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
}
|
2024-02-26 18:38:54 +08:00
|
|
|
|
}, [dispatch, conversationId, setCurrentConversation]);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchConversation();
|
|
|
|
|
}, [fetchConversation]);
|
|
|
|
|
|
|
|
|
|
return conversation;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSendMessage = () => {
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const { setConversation } = useSetConversation();
|
|
|
|
|
const { conversationId } = useGetChatSearchParams();
|
|
|
|
|
const conversation = useSelector(
|
|
|
|
|
(state: any) => state.chatModel.currentConversation,
|
|
|
|
|
);
|
|
|
|
|
const { handleClickConversation } = useClickConversationCard();
|
|
|
|
|
|
|
|
|
|
const sendMessage = (message: string, id?: string) => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'chatModel/completeConversation',
|
|
|
|
|
payload: {
|
|
|
|
|
conversation_id: id ?? conversationId,
|
|
|
|
|
messages: [
|
|
|
|
|
...(conversation?.message ?? []).map((x: IMessage) => omit(x, 'id')),
|
|
|
|
|
{
|
|
|
|
|
role: MessageType.User,
|
|
|
|
|
content: message,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSendMessage = async (message: string) => {
|
2024-02-26 18:38:54 +08:00
|
|
|
|
if (conversationId !== '') {
|
2024-02-22 17:14:25 +08:00
|
|
|
|
sendMessage(message);
|
|
|
|
|
} else {
|
|
|
|
|
const data = await setConversation(message);
|
|
|
|
|
if (data.retcode === 0) {
|
|
|
|
|
const id = data.data.id;
|
|
|
|
|
handleClickConversation(id);
|
|
|
|
|
sendMessage(message, id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { sendMessage: handleSendMessage };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//#endregion
|