Skip to content

Commit

Permalink
refactor: Remove old message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alimtunc committed Oct 24, 2023
1 parent aebd100 commit 776a1d0
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 136 deletions.
7 changes: 0 additions & 7 deletions libs/components/hooks/useChat/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
IAvatarElement,
IMessage,
IMessageElement,
INestedMessage,
ITasklistElement
} from 'src/types';
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -46,12 +45,6 @@ export const messagesState = atom<IMessage[]>({
default: []
});

export const nestedMessagesState = atom<INestedMessage[]>({
key: 'NestedMessages',
dangerouslyAllowMutability: true,
default: []
});

export const tokenCountState = atom<number>({
key: 'TokenCount',
default: 0
Expand Down
15 changes: 3 additions & 12 deletions libs/components/hooks/useChat/useChatInteract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback } from 'react';
import { useRecoilValue, useResetRecoilState, useSetRecoilState } from 'recoil';
import { IAction, IFileElement, IMessage } from 'src/types';
import { addNestedMessage } from 'utils/message';
import { addMessages } from 'utils/message';

import {
actionState,
Expand All @@ -13,7 +13,6 @@ import {
firstUserMessageState,
loadingState,
messagesState,
nestedMessagesState,
sessionIdState,
sessionState,
tasklistState,
Expand All @@ -31,7 +30,6 @@ const useChatInteract = () => {
const setFirstUserMessage = useSetRecoilState(firstUserMessageState);
const setLoading = useSetRecoilState(loadingState);
const setMessages = useSetRecoilState(messagesState);
const setNestedMessages = useSetRecoilState(nestedMessagesState);
const setElements = useSetRecoilState(elementState);
const setAvatars = useSetRecoilState(avatarState);
const setTasklists = useSetRecoilState(tasklistState);
Expand All @@ -44,7 +42,6 @@ const useChatInteract = () => {
resetSessionId();
setFirstUserMessage(undefined);
setMessages([]);
setNestedMessages([]);
setElements([]);
setAvatars([]);
setTasklists([]);
Expand All @@ -56,10 +53,7 @@ const useChatInteract = () => {

const sendMessage = useCallback(
(message: IMessage, files?: IFileElement[]) => {
setMessages((oldMessages) => [...oldMessages, { ...message }]);
setNestedMessages((oldNestedMessages) =>
addNestedMessage(oldNestedMessages, message)
);
setMessages((oldMessages) => addMessages(oldMessages, message));

session?.socket.emit('ui_message', { message, files });
},
Expand All @@ -69,10 +63,7 @@ const useChatInteract = () => {
const replyMessage = useCallback(
(message: IMessage) => {
if (askUser) {
setMessages((oldMessages) => [...oldMessages, { ...message }]);
setNestedMessages((oldNestedMessages) =>
addNestedMessage(oldNestedMessages, message)
);
setMessages((oldMessages) => addMessages(oldMessages, message));
askUser.callback(message);
}
},
Expand Down
4 changes: 2 additions & 2 deletions libs/components/hooks/useChat/useChatMessages.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useRecoilValue } from 'recoil';

import { firstUserMessageState, nestedMessagesState } from './state';
import { firstUserMessageState, messagesState } from './state';

const useChatMessages = () => {
const messages = useRecoilValue(nestedMessagesState);
const messages = useRecoilValue(messagesState);
const firstUserMessage = useRecoilValue(firstUserMessageState);

return {
Expand Down
78 changes: 10 additions & 68 deletions libs/components/hooks/useChat/useChatSession.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import debounce from 'lodash/debounce';
import isEqual from 'lodash/isEqual';
import { useCallback } from 'react';
import {
useRecoilState,
Expand All @@ -11,7 +10,7 @@ import io from 'socket.io-client';
import { TFormInput } from 'src/inputs';
import { IAction, IElement, IMessage } from 'src/types';
import {
addNestedMessage,
addMessages,
deleteMessageById,
updateMessageById,
updateMessageContentById
Expand All @@ -27,7 +26,6 @@ import {
firstUserMessageState,
loadingState,
messagesState,
nestedMessagesState,
sessionIdState,
sessionState,
tasklistState,
Expand All @@ -44,7 +42,6 @@ const useChatSession = () => {
const setFirstUserMessage = useSetRecoilState(firstUserMessageState);
const setLoading = useSetRecoilState(loadingState);
const setMessages = useSetRecoilState(messagesState);
const setNestedMessages = useSetRecoilState(nestedMessagesState);
const setAskUser = useSetRecoilState(askUserState);
const setElements = useSetRecoilState(elementState);
const setAvatars = useSetRecoilState(avatarState);
Expand Down Expand Up @@ -105,93 +102,38 @@ const useChatSession = () => {
});

socket.on('new_message', (message: IMessage) => {
setMessages((oldMessages) => {
const index = oldMessages.findIndex((m) => isEqual(m.id, message.id));
if (index === -1) {
return [...oldMessages, message];
} else {
return [
...oldMessages.slice(0, index),
message,
...oldMessages.slice(index + 1)
];
}
});

setNestedMessages((oldNestedMessages) =>
addNestedMessage(oldNestedMessages, message)
);
setMessages((oldMessages) => addMessages(oldMessages, message));
});

socket.on('init_conversation', (message: IMessage) => {
setFirstUserMessage(message);
});

socket.on('update_message', (message: IMessageUpdate) => {
setNestedMessages((oldNestedMessages) =>
updateMessageById(oldNestedMessages, message.id, message)
setMessages((oldMessages) =>
updateMessageById(oldMessages, message.id, message)
);
});

socket.on('delete_message', (message: IMessage) => {
setMessages((oldMessages) => {
const index = oldMessages.findIndex((m) => isEqual(m.id, message.id));

if (index === -1) return oldMessages;
return [
...oldMessages.slice(0, index),
...oldMessages.slice(index + 1)
];
});

setNestedMessages((oldNestedMessages) =>
deleteMessageById(oldNestedMessages, message.id)
setMessages((oldMessages) =>
deleteMessageById(oldMessages, message.id)
);
});

socket.on('stream_start', (message: IMessage) => {
setMessages((oldMessages) => {
const index = oldMessages.findIndex((m) => m.id === message.id);
if (index === -1) {
return [...oldMessages, message];
}
return oldMessages;
});

setNestedMessages((oldNestedMessages) =>
addNestedMessage(oldNestedMessages, message)
);
setMessages((oldMessages) => addMessages(oldMessages, message));
});

socket.on('stream_token', ({ id, token, isSequence }: IToken) => {
setMessages((oldMessages) => {
const index = oldMessages.findIndex((m) => m.id === id);
if (index === -1) return oldMessages;
const oldMessage = oldMessages[index];
const newMessage = { ...oldMessage };
if (isSequence) {
newMessage.content = token;
} else {
newMessage.content += token;
}
return [
...oldMessages.slice(0, index),
newMessage,
...oldMessages.slice(index + 1)
];
});

setNestedMessages((oldNestedMessages) =>
updateMessageContentById(oldNestedMessages, id, token, isSequence)
setMessages((oldMessages) =>
updateMessageContentById(oldMessages, id, token, isSequence)
);
});

socket.on('ask', ({ msg, spec }, callback) => {
setAskUser({ spec, callback });
setMessages((oldMessages) => [...oldMessages, { ...msg }]);
setNestedMessages((oldNestedMessages) =>
addNestedMessage(oldNestedMessages, msg)
);
setMessages((oldMessages) => addMessages(oldMessages, msg));

setLoading(false);
});
Expand Down
4 changes: 2 additions & 2 deletions libs/components/src/messages/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { MessageContent } from './components/MessageContent';

import { IAction } from 'src/types/action';
import { IMessageElement } from 'src/types/element';
import { INestedMessage } from 'src/types/message';
import { IMessage } from 'src/types/message';

import { Messages } from './Messages';

interface Props {
message: INestedMessage;
message: IMessage;
elements: IMessageElement[];
actions: IAction[];
indent: number;
Expand Down
4 changes: 2 additions & 2 deletions libs/components/src/messages/MessageContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Box from '@mui/material/Box';

import { IAction } from 'src/types/action';
import { IMessageElement } from 'src/types/element';
import { INestedMessage } from 'src/types/message';
import { IMessage } from 'src/types/message';
import { IMessageContext } from 'src/types/messageContext';

import { Messages } from './Messages';
Expand All @@ -15,7 +15,7 @@ interface Props {
autoScroll?: boolean;
context: IMessageContext;
elements: IMessageElement[];
messages: INestedMessage[];
messages: IMessage[];
setAutoScroll?: (autoScroll: boolean) => void;
}

Expand Down
4 changes: 2 additions & 2 deletions libs/components/src/messages/Messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { isLastMessage } from 'utils/message';

import { IAction } from 'src/types/action';
import { IMessageElement } from 'src/types/element';
import { INestedMessage } from 'src/types/message';
import { IMessage } from 'src/types/message';

import { Message } from './Message';

interface Props {
messages: INestedMessage[];
messages: IMessage[];
elements: IMessageElement[];
actions: IAction[];
indent: number;
Expand Down
4 changes: 2 additions & 2 deletions libs/components/src/messages/components/DetailsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import ExpandLess from '@mui/icons-material/ExpandLess';
import ExpandMore from '@mui/icons-material/ExpandMore';
import CircularProgress from '@mui/material/CircularProgress';

import { INestedMessage } from 'src/types/message';
import { IMessage } from 'src/types/message';

interface Props {
message: INestedMessage;
message: IMessage;
opened: boolean;
loading?: boolean;
onClick: () => void;
Expand Down
6 changes: 3 additions & 3 deletions libs/components/src/messages/components/FeedbackButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ButtonGroup from '@mui/material/ButtonGroup';
import Stack from '@mui/material/Stack';
import Tooltip from '@mui/material/Tooltip';

import { nestedMessagesState } from 'hooks/useChat/state';
import { messagesState } from 'hooks/useChat/state';

import { IMessage } from 'src/types/message';

Expand All @@ -35,15 +35,15 @@ const FeedbackButtons = ({ message }: Props) => {
const UpIcon = feedback === 1 ? ThumbUpAlt : ThumbUpAltOutlined;
const [showFeedbackDialog, setShowFeedbackDialog] = useState<number>();
const [commentInput, setCommentInput] = useState<string>();
const setNestedMessages = useSetRecoilState(nestedMessagesState);
const setMessages = useSetRecoilState(messagesState);

const handleFeedbackChanged = (feedback: number, comment?: string) => {
onFeedbackUpdated &&
onFeedbackUpdated(
message.id,
feedback,
() =>
setNestedMessages((prev) =>
setMessages((prev) =>
updateMessageById(prev, message.id, {
...message,
humanFeedback: feedback,
Expand Down
3 changes: 0 additions & 3 deletions libs/components/src/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export interface IMessage {
prompt?: IPrompt;
streaming?: boolean;
waitForAnswer?: boolean;
}

export interface INestedMessage extends IMessage {
subMessages?: IMessage[];
}

Expand Down
Loading

0 comments on commit 776a1d0

Please sign in to comment.