Skip to content

Commit

Permalink
feat: Regenerate chat message infiniflow#2088 (infiniflow#2166)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

feat: Regenerate chat message infiniflow#2088
### Type of change

- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
cike8899 authored Aug 29, 2024
1 parent 7c77caa commit 73d3128
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 28 deletions.
28 changes: 23 additions & 5 deletions web/src/components/message-item/group-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
SoundOutlined,
SyncOutlined,
} from '@ant-design/icons';
import { Radio } from 'antd';
import { Radio, Tooltip } from 'antd';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import SvgIcon from '../svg-icon';
import FeedbackModal from './feedback-modal';
import { useRemoveMessage, useSendFeedback } from './hooks';
Expand All @@ -33,6 +34,7 @@ export const AssistantGroupButton = ({
hideModal: hidePromptModal,
showModal: showPromptModal,
} = useSetModalState();
const { t } = useTranslation();

const handleLike = useCallback(() => {
onFeedbackOk({ thumbup: true });
Expand All @@ -45,7 +47,9 @@ export const AssistantGroupButton = ({
<CopyToClipboard text={content}></CopyToClipboard>
</Radio.Button>
<Radio.Button value="b">
<SoundOutlined />
<Tooltip title={t('chat.read')}>
<SoundOutlined />
</Tooltip>
</Radio.Button>
<Radio.Button value="c" onClick={handleLike}>
<LikeOutlined />
Expand Down Expand Up @@ -81,27 +85,41 @@ export const AssistantGroupButton = ({
interface UserGroupButtonProps extends IRemoveMessageById {
messageId: string;
content: string;
regenerateMessage(): void;
sendLoading: boolean;
}

export const UserGroupButton = ({
content,
messageId,
sendLoading,
removeMessageById,
regenerateMessage,
}: UserGroupButtonProps) => {
const { onRemoveMessage, loading } = useRemoveMessage(
messageId,
removeMessageById,
);
const { t } = useTranslation();

return (
<Radio.Group size="small">
<Radio.Button value="a">
<CopyToClipboard text={content}></CopyToClipboard>
</Radio.Button>
<Radio.Button value="b">
<SyncOutlined />
<Radio.Button
value="b"
onClick={regenerateMessage}
disabled={sendLoading}
>
<Tooltip title={t('chat.regenerate')}>
<SyncOutlined spin={sendLoading} />
</Tooltip>
</Radio.Button>
<Radio.Button value="c" onClick={onRemoveMessage} disabled={loading}>
<DeleteOutlined spin={loading} />
<Tooltip title={t('common.delete')}>
<DeleteOutlined spin={loading} />
</Tooltip>
</Radio.Button>
</Radio.Group>
);
Expand Down
13 changes: 11 additions & 2 deletions web/src/components/message-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
useFetchDocumentInfosByIds,
useFetchDocumentThumbnailsByIds,
} from '@/hooks/document-hooks';
import { IRemoveMessageById } from '@/hooks/logic-hooks';
import { IRegenerateMessage, IRemoveMessageById } from '@/hooks/logic-hooks';
import { IMessage } from '@/pages/chat/interface';
import MarkdownContent from '@/pages/chat/markdown-content';
import { getExtension, isImage } from '@/utils/document-util';
Expand All @@ -24,10 +24,11 @@ import styles from './index.less';

const { Text } = Typography;

interface IProps extends IRemoveMessageById {
interface IProps extends IRemoveMessageById, IRegenerateMessage {
item: IMessage;
reference: IReference;
loading?: boolean;
sendLoading?: boolean;
nickname?: string;
avatar?: string;
clickDocumentButton?: (documentId: string, chunk: IChunk) => void;
Expand All @@ -39,9 +40,11 @@ const MessageItem = ({
reference,
loading = false,
avatar = '',
sendLoading = false,
clickDocumentButton,
index,
removeMessageById,
regenerateMessage,
}: IProps) => {
const isAssistant = item.role === MessageType.Assistant;
const isUser = item.role === MessageType.User;
Expand Down Expand Up @@ -73,6 +76,10 @@ const MessageItem = ({
[showModal],
);

const handleRegenerateMessage = useCallback(() => {
regenerateMessage(item);
}, [regenerateMessage, item]);

useEffect(() => {
const ids = item?.doc_ids ?? [];
if (ids.length) {
Expand Down Expand Up @@ -128,6 +135,8 @@ const MessageItem = ({
content={item.content}
messageId={item.id}
removeMessageById={removeMessageById}
regenerateMessage={handleRegenerateMessage}
sendLoading={sendLoading}
></UserGroupButton>
)}

Expand Down
74 changes: 73 additions & 1 deletion web/src/hooks/logic-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Authorization } from '@/constants/authorization';
import { LanguageTranslationMap } from '@/constants/common';
import { Pagination } from '@/interfaces/common';
import { ResponseType } from '@/interfaces/database/base';
import { IAnswer } from '@/interfaces/database/chat';
import { IAnswer, Message } from '@/interfaces/database/chat';
import { IKnowledgeFile } from '@/interfaces/database/knowledge';
import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
import { IClientConversation } from '@/pages/chat/interface';
Expand All @@ -23,6 +23,7 @@ import {
} from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch } from 'umi';
import { v4 as uuid } from 'uuid';
import { useSetModalState, useTranslate } from './common-hooks';
import { useSetDocumentParser } from './document-hooks';
import { useSetPaginationParams } from './route-hook';
Expand Down Expand Up @@ -336,6 +337,77 @@ export const useRemoveMessageById = (
return { removeMessageById };
};

export const useRemoveMessagesAfterCurrentMessage = (
setCurrentConversation: (
callback: (state: IClientConversation) => IClientConversation,
) => void,
) => {
const removeMessagesAfterCurrentMessage = useCallback(
(messageId: string) => {
setCurrentConversation((pre) => {
const index = pre.message?.findIndex((x) => x.id === messageId);
if (index !== -1) {
let nextMessages = pre.message?.slice(0, index + 2) ?? [];
const latestMessage = nextMessages.at(-1);
nextMessages = latestMessage
? [
...nextMessages.slice(0, -1),
{
...latestMessage,
content: '',
reference: undefined,
prompt: undefined,
},
]
: nextMessages;
return {
...pre,
message: nextMessages,
};
}
return pre;
});
},
[setCurrentConversation],
);

return { removeMessagesAfterCurrentMessage };
};

export interface IRegenerateMessage {
regenerateMessage(message: Message): void;
}

export const useRegenerateMessage = ({
removeMessagesAfterCurrentMessage,
sendMessage,
messages,
}: {
removeMessagesAfterCurrentMessage(messageId: string): void;
sendMessage({ message }: { message: Message; messages?: Message[] }): void;
messages: Message[];
}) => {
const regenerateMessage = useCallback(
async (message: Message) => {
if (message.id) {
removeMessagesAfterCurrentMessage(message.id);
const index = messages.findIndex((x) => x.id === message.id);
let nextMessages;
if (index !== -1) {
nextMessages = messages.slice(0, index);
}
sendMessage({
message: { ...message, id: uuid() },
messages: nextMessages,
});
}
},
[removeMessagesAfterCurrentMessage, sendMessage, messages],
);

return { regenerateMessage };
};

// #endregion

/**
Expand Down
2 changes: 2 additions & 0 deletions web/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ The above is the content you need to summarize.`,
parsing: 'Parsing',
uploading: 'Uploading',
uploadFailed: 'Upload failed',
regenerate: 'Regenerate',
read: 'Read content',
},
setting: {
profile: 'Profile',
Expand Down
2 changes: 2 additions & 0 deletions web/src/locales/zh-traditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ export default {
parsing: '解析中',
uploading: '上傳中',
uploadFailed: '上傳失敗',
regenerate: '重新生成',
read: '朗讀內容',
},
setting: {
profile: '概述',
Expand Down
2 changes: 2 additions & 0 deletions web/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ export default {
parsing: '解析中',
uploading: '上传中',
uploadFailed: '上传失败',
regenerate: '重新生成',
read: '朗读内容',
},
setting: {
profile: '概要',
Expand Down
5 changes: 5 additions & 0 deletions web/src/pages/chat/chat-container/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ const ChatContainer = () => {
conversationId,
loading,
removeMessageById,
removeMessagesAfterCurrentMessage,
} = useFetchConversationOnMount();
const {
handleInputChange,
handlePressEnter,
value,
loading: sendLoading,
regenerateMessage,
} = useSendMessage(
conversation,
addNewestConversation,
removeLatestMessage,
addNewestAnswer,
removeMessagesAfterCurrentMessage,
);
const { visible, hideModal, documentId, selectedChunk, clickDocumentButton } =
useClickDrawer();
Expand Down Expand Up @@ -71,6 +74,8 @@ const ChatContainer = () => {
clickDocumentButton={clickDocumentButton}
index={i}
removeMessageById={removeMessageById}
regenerateMessage={regenerateMessage}
sendLoading={sendLoading}
></MessageItem>
);
})}
Expand Down
Loading

0 comments on commit 73d3128

Please sign in to comment.