Refactor CurrentMessage component and ChatStore
- Updated the CurrentMessage component to use the Message model from ChatStore. - Replaced chat.lastMessage with the last message in the chat.messages array. - Updated the useEffect hooks to create a new Message object and pass it to the newMessage and updateLastMessage actions. - Updated the chatPanel function in ChatPanel.tsx to create a new Message object before calling the newMessage action. - Removed the lastMessage field from the ChatStore model. - Updated the startResponsing action to accept a string parameter. - Updated the updateLastMessage action to accept a string parameter and update the message field of the last message in the chat.messages array.
This commit is contained in:
parent
1fe45ef5e0
commit
5eefb6b32c
@ -5,6 +5,7 @@ import { Container, Text } from "@mantine/core";
|
||||
import CodeBlock from "@/views/components/CodeBlock";
|
||||
import { observer } from "mobx-react-lite";
|
||||
import { useMst } from "@/views/stores/RootStore";
|
||||
import { Message } from "@/views/stores/ChatStore";
|
||||
|
||||
|
||||
const MessageBlink = observer(() => {
|
||||
@ -50,7 +51,7 @@ const CurrentMessage = observer((props: any) => {
|
||||
|
||||
// split blocks
|
||||
const messageBlocks = getBlocks(chat.currentMessage);
|
||||
const lastMessageBlocks = getBlocks(chat.lastMessage?.message);
|
||||
const lastMessageBlocks = getBlocks(chat.messages[chat.messages.length - 1]?.message);
|
||||
const fixedCount = lastMessageBlocks.length;
|
||||
const receivedCount = messageBlocks.length;
|
||||
const renderBlocks = messageBlocks.splice(-1);
|
||||
@ -58,13 +59,14 @@ const CurrentMessage = observer((props: any) => {
|
||||
useEffect(() => {
|
||||
if (chat.generating) {
|
||||
// new a bot message
|
||||
chat.newMessage({ type: 'bot', message: chat.currentMessage });
|
||||
const messageItem = Message.create({ type: 'bot', message: chat.currentMessage });
|
||||
chat.newMessage(messageItem);
|
||||
}
|
||||
}, [chat.generating]);
|
||||
|
||||
useEffect(() => {
|
||||
if (receivedCount - fixedCount >= 1 || !chat.responsed) {
|
||||
chat.updateLastMessage({ type: 'bot', message: chat.currentMessage });
|
||||
chat.updateLastMessage(chat.currentMessage);
|
||||
}
|
||||
}, [chat.currentMessage, chat.responsed]);
|
||||
|
||||
|
@ -60,7 +60,7 @@ const chatPanel = observer(() => {
|
||||
timer.start();
|
||||
});
|
||||
messageUtil.registerHandler('receiveMessage', (message: { text: string; isError: boolean, hash }) => {
|
||||
const messageItem = Message.create({ message: message.text, hash: message.hash });
|
||||
const messageItem = Message.create({ type: 'bot', message: message.text, hash: message.hash });
|
||||
chat.stopGenerating(true, messageItem);
|
||||
if (message.isError) {
|
||||
chat.happendError(message.text);
|
||||
@ -68,7 +68,8 @@ const chatPanel = observer(() => {
|
||||
});
|
||||
|
||||
messageUtil.registerHandler('systemMessage', (message: { text: string }) => {
|
||||
chat.newMessage({ type: 'system', message: message.text });
|
||||
const messageItem = Message.create({ type: 'system', message: message.text });
|
||||
chat.newMessage(messageItem);
|
||||
// start generating
|
||||
chat.startSystemMessage();
|
||||
// Clear the input field
|
||||
|
@ -66,7 +66,6 @@ export const Message = types.model({
|
||||
export const ChatStore = types.model('Chat', {
|
||||
generating: false,
|
||||
responsed: false,
|
||||
lastMessage: types.maybe(Message),
|
||||
currentMessage: '',
|
||||
hasDone: false,
|
||||
errorMessage: '',
|
||||
@ -131,17 +130,17 @@ export const ChatStore = types.model('Chat', {
|
||||
}
|
||||
}
|
||||
},
|
||||
startResponsing: (action) => {
|
||||
startResponsing: (message: string) => {
|
||||
self.responsed = true;
|
||||
self.currentMessage = action.payload;
|
||||
self.currentMessage = message;
|
||||
},
|
||||
newMessage: (message: IMessage) => {
|
||||
self.messages.push(message);
|
||||
self.lastMessage = message;
|
||||
},
|
||||
updateLastMessage: (message: IMessage) => {
|
||||
self.messages[self.messages.length - 1] = message;
|
||||
self.lastMessage = message;
|
||||
updateLastMessage: (message: string) => {
|
||||
if (self.messages.length > 0) {
|
||||
self.messages[self.messages.length - 1].message = message;
|
||||
}
|
||||
},
|
||||
shiftMessage: () => {
|
||||
self.messages.splice(0, 1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user