balibabu
		
	commited on
		
		
					Commit 
							
							·
						
						7d8f5e6
	
1
								Parent(s):
							
							cf772f7
								
feat: Delete message by id #2088 (#2155)
Browse files### What problem does this PR solve?
feat: Delete message by id #2088
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- web/src/components/message-item/group-button.tsx +14 -5
- web/src/components/message-item/hooks.ts +21 -1
- web/src/components/message-item/index.tsx +4 -1
- web/src/hooks/chat-hooks.ts +4 -0
- web/src/hooks/logic-hooks.ts +30 -0
- web/src/pages/chat/chat-container/index.tsx +2 -0
- web/src/pages/chat/hooks.ts +9 -2
    	
        web/src/components/message-item/group-button.tsx
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 1 | 
             
            import CopyToClipboard from '@/components/copy-to-clipboard';
         | 
| 2 | 
             
            import { useSetModalState } from '@/hooks/common-hooks';
         | 
|  | |
| 3 | 
             
            import {
         | 
| 4 | 
             
              DeleteOutlined,
         | 
| 5 | 
             
              DislikeOutlined,
         | 
| @@ -11,7 +12,7 @@ import { Radio } from 'antd'; | |
| 11 | 
             
            import { useCallback } from 'react';
         | 
| 12 | 
             
            import SvgIcon from '../svg-icon';
         | 
| 13 | 
             
            import FeedbackModal from './feedback-modal';
         | 
| 14 | 
            -
            import { useSendFeedback } from './hooks';
         | 
| 15 | 
             
            import PromptModal from './prompt-modal';
         | 
| 16 |  | 
| 17 | 
             
            interface IProps {
         | 
| @@ -77,12 +78,20 @@ export const AssistantGroupButton = ({ | |
| 77 | 
             
              );
         | 
| 78 | 
             
            };
         | 
| 79 |  | 
| 80 | 
            -
            interface UserGroupButtonProps {
         | 
| 81 | 
             
              messageId: string;
         | 
| 82 | 
             
              content: string;
         | 
| 83 | 
             
            }
         | 
| 84 |  | 
| 85 | 
            -
            export const UserGroupButton = ({ | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 86 | 
             
              return (
         | 
| 87 | 
             
                <Radio.Group size="small">
         | 
| 88 | 
             
                  <Radio.Button value="a">
         | 
| @@ -91,8 +100,8 @@ export const UserGroupButton = ({ content }: UserGroupButtonProps) => { | |
| 91 | 
             
                  <Radio.Button value="b">
         | 
| 92 | 
             
                    <SyncOutlined />
         | 
| 93 | 
             
                  </Radio.Button>
         | 
| 94 | 
            -
                  <Radio.Button value="c">
         | 
| 95 | 
            -
                    <DeleteOutlined />
         | 
| 96 | 
             
                  </Radio.Button>
         | 
| 97 | 
             
                </Radio.Group>
         | 
| 98 | 
             
              );
         | 
|  | |
| 1 | 
             
            import CopyToClipboard from '@/components/copy-to-clipboard';
         | 
| 2 | 
             
            import { useSetModalState } from '@/hooks/common-hooks';
         | 
| 3 | 
            +
            import { IRemoveMessageById } from '@/hooks/logic-hooks';
         | 
| 4 | 
             
            import {
         | 
| 5 | 
             
              DeleteOutlined,
         | 
| 6 | 
             
              DislikeOutlined,
         | 
|  | |
| 12 | 
             
            import { useCallback } from 'react';
         | 
| 13 | 
             
            import SvgIcon from '../svg-icon';
         | 
| 14 | 
             
            import FeedbackModal from './feedback-modal';
         | 
| 15 | 
            +
            import { useRemoveMessage, useSendFeedback } from './hooks';
         | 
| 16 | 
             
            import PromptModal from './prompt-modal';
         | 
| 17 |  | 
| 18 | 
             
            interface IProps {
         | 
|  | |
| 78 | 
             
              );
         | 
| 79 | 
             
            };
         | 
| 80 |  | 
| 81 | 
            +
            interface UserGroupButtonProps extends IRemoveMessageById {
         | 
| 82 | 
             
              messageId: string;
         | 
| 83 | 
             
              content: string;
         | 
| 84 | 
             
            }
         | 
| 85 |  | 
| 86 | 
            +
            export const UserGroupButton = ({
         | 
| 87 | 
            +
              content,
         | 
| 88 | 
            +
              messageId,
         | 
| 89 | 
            +
              removeMessageById,
         | 
| 90 | 
            +
            }: UserGroupButtonProps) => {
         | 
| 91 | 
            +
              const { onRemoveMessage, loading } = useRemoveMessage(
         | 
| 92 | 
            +
                messageId,
         | 
| 93 | 
            +
                removeMessageById,
         | 
| 94 | 
            +
              );
         | 
| 95 | 
             
              return (
         | 
| 96 | 
             
                <Radio.Group size="small">
         | 
| 97 | 
             
                  <Radio.Button value="a">
         | 
|  | |
| 100 | 
             
                  <Radio.Button value="b">
         | 
| 101 | 
             
                    <SyncOutlined />
         | 
| 102 | 
             
                  </Radio.Button>
         | 
| 103 | 
            +
                  <Radio.Button value="c" onClick={onRemoveMessage} disabled={loading}>
         | 
| 104 | 
            +
                    <DeleteOutlined spin={loading} />
         | 
| 105 | 
             
                  </Radio.Button>
         | 
| 106 | 
             
                </Radio.Group>
         | 
| 107 | 
             
              );
         | 
    	
        web/src/components/message-item/hooks.ts
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 1 | 
            -
            import { useFeedback } from '@/hooks/chat-hooks';
         | 
| 2 | 
             
            import { useSetModalState } from '@/hooks/common-hooks';
         | 
|  | |
| 3 | 
             
            import { IFeedbackRequestBody } from '@/interfaces/request/chat';
         | 
| 4 | 
             
            import { getMessagePureId } from '@/utils/chat';
         | 
| 5 | 
             
            import { useCallback } from 'react';
         | 
| @@ -30,3 +31,22 @@ export const useSendFeedback = (messageId: string) => { | |
| 30 | 
             
                showModal,
         | 
| 31 | 
             
              };
         | 
| 32 | 
             
            };
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import { useDeleteMessage, useFeedback } from '@/hooks/chat-hooks';
         | 
| 2 | 
             
            import { useSetModalState } from '@/hooks/common-hooks';
         | 
| 3 | 
            +
            import { IRemoveMessageById } from '@/hooks/logic-hooks';
         | 
| 4 | 
             
            import { IFeedbackRequestBody } from '@/interfaces/request/chat';
         | 
| 5 | 
             
            import { getMessagePureId } from '@/utils/chat';
         | 
| 6 | 
             
            import { useCallback } from 'react';
         | 
|  | |
| 31 | 
             
                showModal,
         | 
| 32 | 
             
              };
         | 
| 33 | 
             
            };
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            export const useRemoveMessage = (
         | 
| 36 | 
            +
              messageId: string,
         | 
| 37 | 
            +
              removeMessageById: IRemoveMessageById['removeMessageById'],
         | 
| 38 | 
            +
            ) => {
         | 
| 39 | 
            +
              const { deleteMessage, loading } = useDeleteMessage();
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              const onRemoveMessage = useCallback(async () => {
         | 
| 42 | 
            +
                const pureId = getMessagePureId(messageId);
         | 
| 43 | 
            +
                if (pureId) {
         | 
| 44 | 
            +
                  const retcode = await deleteMessage(pureId);
         | 
| 45 | 
            +
                  if (retcode === 0) {
         | 
| 46 | 
            +
                    removeMessageById(messageId);
         | 
| 47 | 
            +
                  }
         | 
| 48 | 
            +
                }
         | 
| 49 | 
            +
              }, [deleteMessage, messageId, removeMessageById]);
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              return { onRemoveMessage, loading };
         | 
| 52 | 
            +
            };
         | 
    	
        web/src/components/message-item/index.tsx
    CHANGED
    
    | @@ -11,6 +11,7 @@ import { | |
| 11 | 
             
              useFetchDocumentInfosByIds,
         | 
| 12 | 
             
              useFetchDocumentThumbnailsByIds,
         | 
| 13 | 
             
            } from '@/hooks/document-hooks';
         | 
|  | |
| 14 | 
             
            import { IMessage } from '@/pages/chat/interface';
         | 
| 15 | 
             
            import MarkdownContent from '@/pages/chat/markdown-content';
         | 
| 16 | 
             
            import { getExtension, isImage } from '@/utils/document-util';
         | 
| @@ -23,7 +24,7 @@ import styles from './index.less'; | |
| 23 |  | 
| 24 | 
             
            const { Text } = Typography;
         | 
| 25 |  | 
| 26 | 
            -
            interface IProps {
         | 
| 27 | 
             
              item: IMessage;
         | 
| 28 | 
             
              reference: IReference;
         | 
| 29 | 
             
              loading?: boolean;
         | 
| @@ -40,6 +41,7 @@ const MessageItem = ({ | |
| 40 | 
             
              avatar = '',
         | 
| 41 | 
             
              clickDocumentButton,
         | 
| 42 | 
             
              index,
         | 
|  | |
| 43 | 
             
            }: IProps) => {
         | 
| 44 | 
             
              const isAssistant = item.role === MessageType.Assistant;
         | 
| 45 | 
             
              const isUser = item.role === MessageType.User;
         | 
| @@ -125,6 +127,7 @@ const MessageItem = ({ | |
| 125 | 
             
                            <UserGroupButton
         | 
| 126 | 
             
                              content={item.content}
         | 
| 127 | 
             
                              messageId={item.id}
         | 
|  | |
| 128 | 
             
                            ></UserGroupButton>
         | 
| 129 | 
             
                          )}
         | 
| 130 |  | 
|  | |
| 11 | 
             
              useFetchDocumentInfosByIds,
         | 
| 12 | 
             
              useFetchDocumentThumbnailsByIds,
         | 
| 13 | 
             
            } from '@/hooks/document-hooks';
         | 
| 14 | 
            +
            import { IRemoveMessageById } from '@/hooks/logic-hooks';
         | 
| 15 | 
             
            import { IMessage } from '@/pages/chat/interface';
         | 
| 16 | 
             
            import MarkdownContent from '@/pages/chat/markdown-content';
         | 
| 17 | 
             
            import { getExtension, isImage } from '@/utils/document-util';
         | 
|  | |
| 24 |  | 
| 25 | 
             
            const { Text } = Typography;
         | 
| 26 |  | 
| 27 | 
            +
            interface IProps extends IRemoveMessageById {
         | 
| 28 | 
             
              item: IMessage;
         | 
| 29 | 
             
              reference: IReference;
         | 
| 30 | 
             
              loading?: boolean;
         | 
|  | |
| 41 | 
             
              avatar = '',
         | 
| 42 | 
             
              clickDocumentButton,
         | 
| 43 | 
             
              index,
         | 
| 44 | 
            +
              removeMessageById,
         | 
| 45 | 
             
            }: IProps) => {
         | 
| 46 | 
             
              const isAssistant = item.role === MessageType.Assistant;
         | 
| 47 | 
             
              const isUser = item.role === MessageType.User;
         | 
|  | |
| 127 | 
             
                            <UserGroupButton
         | 
| 128 | 
             
                              content={item.content}
         | 
| 129 | 
             
                              messageId={item.id}
         | 
| 130 | 
            +
                              removeMessageById={removeMessageById}
         | 
| 131 | 
             
                            ></UserGroupButton>
         | 
| 132 | 
             
                          )}
         | 
| 133 |  | 
    	
        web/src/hooks/chat-hooks.ts
    CHANGED
    
    | @@ -314,6 +314,10 @@ export const useDeleteMessage = () => { | |
| 314 | 
             
                    conversationId,
         | 
| 315 | 
             
                  });
         | 
| 316 |  | 
|  | |
|  | |
|  | |
|  | |
| 317 | 
             
                  return data.retcode;
         | 
| 318 | 
             
                },
         | 
| 319 | 
             
              });
         | 
|  | |
| 314 | 
             
                    conversationId,
         | 
| 315 | 
             
                  });
         | 
| 316 |  | 
| 317 | 
            +
                  if (data.retcode === 0) {
         | 
| 318 | 
            +
                    message.success(i18n.t(`message.deleted`));
         | 
| 319 | 
            +
                  }
         | 
| 320 | 
            +
             | 
| 321 | 
             
                  return data.retcode;
         | 
| 322 | 
             
                },
         | 
| 323 | 
             
              });
         | 
    	
        web/src/hooks/logic-hooks.ts
    CHANGED
    
    | @@ -5,8 +5,10 @@ import { ResponseType } from '@/interfaces/database/base'; | |
| 5 | 
             
            import { IAnswer } from '@/interfaces/database/chat';
         | 
| 6 | 
             
            import { IKnowledgeFile } from '@/interfaces/database/knowledge';
         | 
| 7 | 
             
            import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
         | 
|  | |
| 8 | 
             
            import api from '@/utils/api';
         | 
| 9 | 
             
            import { getAuthorization } from '@/utils/authorization-util';
         | 
|  | |
| 10 | 
             
            import { PaginationProps } from 'antd';
         | 
| 11 | 
             
            import { FormInstance } from 'antd/lib';
         | 
| 12 | 
             
            import axios from 'axios';
         | 
| @@ -306,6 +308,34 @@ export const useHandleMessageInputChange = () => { | |
| 306 | 
             
              };
         | 
| 307 | 
             
            };
         | 
| 308 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 309 | 
             
            // #endregion
         | 
| 310 |  | 
| 311 | 
             
            /**
         | 
|  | |
| 5 | 
             
            import { IAnswer } from '@/interfaces/database/chat';
         | 
| 6 | 
             
            import { IKnowledgeFile } from '@/interfaces/database/knowledge';
         | 
| 7 | 
             
            import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
         | 
| 8 | 
            +
            import { IClientConversation } from '@/pages/chat/interface';
         | 
| 9 | 
             
            import api from '@/utils/api';
         | 
| 10 | 
             
            import { getAuthorization } from '@/utils/authorization-util';
         | 
| 11 | 
            +
            import { getMessagePureId } from '@/utils/chat';
         | 
| 12 | 
             
            import { PaginationProps } from 'antd';
         | 
| 13 | 
             
            import { FormInstance } from 'antd/lib';
         | 
| 14 | 
             
            import axios from 'axios';
         | 
|  | |
| 308 | 
             
              };
         | 
| 309 | 
             
            };
         | 
| 310 |  | 
| 311 | 
            +
            export interface IRemoveMessageById {
         | 
| 312 | 
            +
              removeMessageById(messageId: string): void;
         | 
| 313 | 
            +
            }
         | 
| 314 | 
            +
             | 
| 315 | 
            +
            export const useRemoveMessageById = (
         | 
| 316 | 
            +
              setCurrentConversation: (
         | 
| 317 | 
            +
                callback: (state: IClientConversation) => IClientConversation,
         | 
| 318 | 
            +
              ) => void,
         | 
| 319 | 
            +
            ) => {
         | 
| 320 | 
            +
              const removeMessageById = useCallback(
         | 
| 321 | 
            +
                (messageId: string) => {
         | 
| 322 | 
            +
                  setCurrentConversation((pre) => {
         | 
| 323 | 
            +
                    const nextMessages =
         | 
| 324 | 
            +
                      pre.message?.filter(
         | 
| 325 | 
            +
                        (x) => getMessagePureId(x.id) !== getMessagePureId(messageId),
         | 
| 326 | 
            +
                      ) ?? [];
         | 
| 327 | 
            +
                    return {
         | 
| 328 | 
            +
                      ...pre,
         | 
| 329 | 
            +
                      message: nextMessages,
         | 
| 330 | 
            +
                    };
         | 
| 331 | 
            +
                  });
         | 
| 332 | 
            +
                },
         | 
| 333 | 
            +
                [setCurrentConversation],
         | 
| 334 | 
            +
              );
         | 
| 335 | 
            +
             | 
| 336 | 
            +
              return { removeMessageById };
         | 
| 337 | 
            +
            };
         | 
| 338 | 
            +
             | 
| 339 | 
             
            // #endregion
         | 
| 340 |  | 
| 341 | 
             
            /**
         | 
    	
        web/src/pages/chat/chat-container/index.tsx
    CHANGED
    
    | @@ -27,6 +27,7 @@ const ChatContainer = () => { | |
| 27 | 
             
                addNewestAnswer,
         | 
| 28 | 
             
                conversationId,
         | 
| 29 | 
             
                loading,
         | 
|  | |
| 30 | 
             
              } = useFetchConversationOnMount();
         | 
| 31 | 
             
              const {
         | 
| 32 | 
             
                handleInputChange,
         | 
| @@ -69,6 +70,7 @@ const ChatContainer = () => { | |
| 69 | 
             
                                reference={buildMessageItemReference(conversation, message)}
         | 
| 70 | 
             
                                clickDocumentButton={clickDocumentButton}
         | 
| 71 | 
             
                                index={i}
         | 
|  | |
| 72 | 
             
                              ></MessageItem>
         | 
| 73 | 
             
                            );
         | 
| 74 | 
             
                          })}
         | 
|  | |
| 27 | 
             
                addNewestAnswer,
         | 
| 28 | 
             
                conversationId,
         | 
| 29 | 
             
                loading,
         | 
| 30 | 
            +
                removeMessageById,
         | 
| 31 | 
             
              } = useFetchConversationOnMount();
         | 
| 32 | 
             
              const {
         | 
| 33 | 
             
                handleInputChange,
         | 
|  | |
| 70 | 
             
                                reference={buildMessageItemReference(conversation, message)}
         | 
| 71 | 
             
                                clickDocumentButton={clickDocumentButton}
         | 
| 72 | 
             
                                index={i}
         | 
| 73 | 
            +
                                removeMessageById={removeMessageById}
         | 
| 74 | 
             
                              ></MessageItem>
         | 
| 75 | 
             
                            );
         | 
| 76 | 
             
                          })}
         | 
    	
        web/src/pages/chat/hooks.ts
    CHANGED
    
    | @@ -17,7 +17,10 @@ import { | |
| 17 | 
             
              useShowDeleteConfirm,
         | 
| 18 | 
             
              useTranslate,
         | 
| 19 | 
             
            } from '@/hooks/common-hooks';
         | 
| 20 | 
            -
            import { | 
|  | |
|  | |
|  | |
| 21 | 
             
            import {
         | 
| 22 | 
             
              IAnswer,
         | 
| 23 | 
             
              IConversation,
         | 
| @@ -251,6 +254,7 @@ export const useSelectCurrentConversation = () => { | |
| 251 | 
             
              const { data: conversation, loading } = useFetchNextConversation();
         | 
| 252 | 
             
              const { data: dialog } = useFetchNextDialog();
         | 
| 253 | 
             
              const { conversationId, dialogId } = useGetChatSearchParams();
         | 
|  | |
| 254 |  | 
| 255 | 
             
              // Show the entered message in the conversation immediately after sending the message
         | 
| 256 | 
             
              const addNewestConversation = useCallback(
         | 
| @@ -348,6 +352,7 @@ export const useSelectCurrentConversation = () => { | |
| 348 | 
             
                addNewestConversation,
         | 
| 349 | 
             
                removeLatestMessage,
         | 
| 350 | 
             
                addNewestAnswer,
         | 
|  | |
| 351 | 
             
                loading,
         | 
| 352 | 
             
              };
         | 
| 353 | 
             
            };
         | 
| @@ -376,6 +381,7 @@ export const useFetchConversationOnMount = () => { | |
| 376 | 
             
                removeLatestMessage,
         | 
| 377 | 
             
                addNewestAnswer,
         | 
| 378 | 
             
                loading,
         | 
|  | |
| 379 | 
             
              } = useSelectCurrentConversation();
         | 
| 380 | 
             
              const ref = useScrollToBottom(currentConversation);
         | 
| 381 |  | 
| @@ -387,6 +393,7 @@ export const useFetchConversationOnMount = () => { | |
| 387 | 
             
                addNewestAnswer,
         | 
| 388 | 
             
                conversationId,
         | 
| 389 | 
             
                loading,
         | 
|  | |
| 390 | 
             
              };
         | 
| 391 | 
             
            };
         | 
| 392 |  | 
| @@ -408,7 +415,7 @@ export const useHandleMessageInputChange = () => { | |
| 408 |  | 
| 409 | 
             
            export const useSendMessage = (
         | 
| 410 | 
             
              conversation: IClientConversation,
         | 
| 411 | 
            -
              addNewestConversation: (message:  | 
| 412 | 
             
              removeLatestMessage: () => void,
         | 
| 413 | 
             
              addNewestAnswer: (answer: IAnswer) => void,
         | 
| 414 | 
             
            ) => {
         | 
|  | |
| 17 | 
             
              useShowDeleteConfirm,
         | 
| 18 | 
             
              useTranslate,
         | 
| 19 | 
             
            } from '@/hooks/common-hooks';
         | 
| 20 | 
            +
            import {
         | 
| 21 | 
            +
              useRemoveMessageById,
         | 
| 22 | 
            +
              useSendMessageWithSse,
         | 
| 23 | 
            +
            } from '@/hooks/logic-hooks';
         | 
| 24 | 
             
            import {
         | 
| 25 | 
             
              IAnswer,
         | 
| 26 | 
             
              IConversation,
         | 
|  | |
| 254 | 
             
              const { data: conversation, loading } = useFetchNextConversation();
         | 
| 255 | 
             
              const { data: dialog } = useFetchNextDialog();
         | 
| 256 | 
             
              const { conversationId, dialogId } = useGetChatSearchParams();
         | 
| 257 | 
            +
              const { removeMessageById } = useRemoveMessageById(setCurrentConversation);
         | 
| 258 |  | 
| 259 | 
             
              // Show the entered message in the conversation immediately after sending the message
         | 
| 260 | 
             
              const addNewestConversation = useCallback(
         | 
|  | |
| 352 | 
             
                addNewestConversation,
         | 
| 353 | 
             
                removeLatestMessage,
         | 
| 354 | 
             
                addNewestAnswer,
         | 
| 355 | 
            +
                removeMessageById,
         | 
| 356 | 
             
                loading,
         | 
| 357 | 
             
              };
         | 
| 358 | 
             
            };
         | 
|  | |
| 381 | 
             
                removeLatestMessage,
         | 
| 382 | 
             
                addNewestAnswer,
         | 
| 383 | 
             
                loading,
         | 
| 384 | 
            +
                removeMessageById,
         | 
| 385 | 
             
              } = useSelectCurrentConversation();
         | 
| 386 | 
             
              const ref = useScrollToBottom(currentConversation);
         | 
| 387 |  | 
|  | |
| 393 | 
             
                addNewestAnswer,
         | 
| 394 | 
             
                conversationId,
         | 
| 395 | 
             
                loading,
         | 
| 396 | 
            +
                removeMessageById,
         | 
| 397 | 
             
              };
         | 
| 398 | 
             
            };
         | 
| 399 |  | 
|  | |
| 415 |  | 
| 416 | 
             
            export const useSendMessage = (
         | 
| 417 | 
             
              conversation: IClientConversation,
         | 
| 418 | 
            +
              addNewestConversation: (message: Message, answer?: string) => void,
         | 
| 419 | 
             
              removeLatestMessage: () => void,
         | 
| 420 | 
             
              addNewestAnswer: (answer: IAnswer) => void,
         | 
| 421 | 
             
            ) => {
         |