feat: let the messages I send appear immediately in the chat window and remove rewrite configuration from nginx proxy (#86)

* feat: remove rewrite configuration from nginx proxy

* feat: let the messages I send appear immediately in the chat window
This commit is contained in:
balibabu
2024-02-29 14:26:59 +08:00
committed by GitHub
parent 0429107e80
commit 14633df880
2 changed files with 117 additions and 70 deletions

View File

@ -10,10 +10,8 @@ import reactStringReplace from 'react-string-replace';
import { import {
useFetchConversationOnMount, useFetchConversationOnMount,
useGetFileIcon, useGetFileIcon,
useScrollToBottom,
useSendMessage, useSendMessage,
} from '../hooks'; } from '../hooks';
import { IClientConversation } from '../interface';
import Image from '@/components/image'; import Image from '@/components/image';
import NewDocumentLink from '@/components/new-document-link'; import NewDocumentLink from '@/components/new-document-link';
@ -187,17 +185,24 @@ const MessageItem = ({
const ChatContainer = () => { const ChatContainer = () => {
const [value, setValue] = useState(''); const [value, setValue] = useState('');
const conversation: IClientConversation = useFetchConversationOnMount(); const {
ref,
currentConversation: conversation,
addNewestConversation,
} = useFetchConversationOnMount();
const { sendMessage } = useSendMessage(); const { sendMessage } = useSendMessage();
const loading = useOneNamespaceEffectsLoading('chatModel', [ const loading = useOneNamespaceEffectsLoading('chatModel', [
'completeConversation', 'completeConversation',
]); ]);
const ref = useScrollToBottom();
useGetFileIcon(); useGetFileIcon();
const handlePressEnter = () => { const handlePressEnter = () => {
setValue(''); if (!loading) {
sendMessage(value); setValue('');
addNewestConversation(value);
sendMessage(value);
}
}; };
const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => { const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {

View File

@ -374,32 +374,64 @@ export const useSetConversation = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const { dialogId } = useGetChatSearchParams(); const { dialogId } = useGetChatSearchParams();
const setConversation = (message: string) => { const setConversation = useCallback(
return dispatch<any>({ (message: string) => {
type: 'chatModel/setConversation', return dispatch<any>({
payload: { type: 'chatModel/setConversation',
// conversation_id: '', payload: {
dialog_id: dialogId, // conversation_id: '',
name: message, dialog_id: dialogId,
message: [ name: message,
{ message: [
role: MessageType.Assistant, {
content: message, role: MessageType.Assistant,
}, content: message,
], },
}, ],
}); },
}; });
},
[dispatch, dialogId],
);
return { setConversation }; return { setConversation };
}; };
export const useSelectCurrentConversation = () => { export const useSelectCurrentConversation = () => {
const [currentConversation, setCurrentConversation] =
useState<IClientConversation>({} as IClientConversation);
const conversation: IClientConversation = useSelector( const conversation: IClientConversation = useSelector(
(state: any) => state.chatModel.currentConversation, (state: any) => state.chatModel.currentConversation,
); );
return conversation; const addNewestConversation = useCallback((message: string) => {
setCurrentConversation((pre) => {
return {
...pre,
message: [
...pre.message,
{
role: MessageType.User,
content: message,
id: uuid(),
} as IMessage,
],
};
});
}, []);
useEffect(() => {
console.info('useSelectCurrentConversation: 1', currentConversation);
}, [currentConversation]);
useEffect(() => {
console.info('useSelectCurrentConversation: 2', conversation);
setCurrentConversation(conversation);
}, [conversation]);
return { currentConversation, addNewestConversation };
}; };
export const useFetchConversation = () => { export const useFetchConversation = () => {
@ -421,11 +453,30 @@ export const useFetchConversation = () => {
return fetchConversation; return fetchConversation;
}; };
export const useScrollToBottom = (currentConversation: IClientConversation) => {
const ref = useRef<HTMLDivElement>(null);
const scrollToBottom = useCallback(() => {
console.info('useScrollToBottom');
if (currentConversation.id) {
ref.current?.scrollIntoView({ behavior: 'instant' });
}
}, [currentConversation]);
useEffect(() => {
scrollToBottom();
}, [scrollToBottom]);
return ref;
};
export const useFetchConversationOnMount = () => { export const useFetchConversationOnMount = () => {
const { conversationId } = useGetChatSearchParams(); const { conversationId } = useGetChatSearchParams();
const conversation = useSelectCurrentConversation();
const setCurrentConversation = useSetCurrentConversation(); const setCurrentConversation = useSetCurrentConversation();
const fetchConversation = useFetchConversation(); const fetchConversation = useFetchConversation();
const { currentConversation, addNewestConversation } =
useSelectCurrentConversation();
const ref = useScrollToBottom(currentConversation);
const fetchConversationOnMount = useCallback(() => { const fetchConversationOnMount = useCallback(() => {
if (isConversationIdExist(conversationId)) { if (isConversationIdExist(conversationId)) {
@ -439,68 +490,59 @@ export const useFetchConversationOnMount = () => {
fetchConversationOnMount(); fetchConversationOnMount();
}, [fetchConversationOnMount]); }, [fetchConversationOnMount]);
return conversation; return { currentConversation, addNewestConversation, ref };
}; };
export const useSendMessage = () => { export const useSendMessage = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const { setConversation } = useSetConversation(); const { setConversation } = useSetConversation();
const { conversationId } = useGetChatSearchParams(); const { conversationId } = useGetChatSearchParams();
const conversation = useSelector( const conversation: IClientConversation = useSelector(
(state: any) => state.chatModel.currentConversation, (state: any) => state.chatModel.currentConversation,
); );
const { handleClickConversation } = useClickConversationCard(); const { handleClickConversation } = useClickConversationCard();
const sendMessage = (message: string, id?: string) => { const sendMessage = useCallback(
dispatch({ (message: string, id?: string) => {
type: 'chatModel/completeConversation', dispatch({
payload: { type: 'chatModel/completeConversation',
conversation_id: id ?? conversationId, payload: {
messages: [ conversation_id: id ?? conversationId,
...(conversation?.message ?? []).map((x: IMessage) => omit(x, 'id')), messages: [
{ ...(conversation?.message ?? []).map((x: IMessage) =>
role: MessageType.User, omit(x, 'id'),
content: message, ),
}, {
], role: MessageType.User,
}, content: message,
}); },
}; ],
},
});
},
[dispatch, conversation?.message, conversationId],
);
const handleSendMessage = async (message: string) => { const handleSendMessage = useCallback(
if (conversationId !== '') { async (message: string) => {
sendMessage(message); if (conversationId !== '') {
} else { sendMessage(message);
const data = await setConversation(message); } else {
if (data.retcode === 0) { const data = await setConversation(message);
const id = data.data.id; if (data.retcode === 0) {
handleClickConversation(id); const id = data.data.id;
sendMessage(message, id); handleClickConversation(id);
sendMessage(message, id);
}
} }
} },
}; [conversationId, handleClickConversation, setConversation, sendMessage],
);
return { sendMessage: handleSendMessage }; return { sendMessage: handleSendMessage };
}; };
export const useScrollToBottom = () => {
const ref = useRef<HTMLDivElement>(null);
let chatModel: ChatModelState = useSelector((state: any) => state.chatModel);
const { currentConversation } = chatModel;
const scrollToBottom = useCallback(() => {
if (currentConversation.id) {
ref.current?.scrollIntoView({ behavior: 'instant' });
}
}, [currentConversation]);
useEffect(() => {
scrollToBottom();
}, [scrollToBottom]);
return ref;
};
export const useGetFileIcon = () => { export const useGetFileIcon = () => {
// const req = require.context('@/assets/svg/file-icon'); // const req = require.context('@/assets/svg/file-icon');
// const ret = req.keys().map(req); // const ret = req.keys().map(req);