2024-02-26 18:38:54 +08:00
|
|
|
import { ReactComponent as AssistantIcon } from '@/assets/svg/assistant.svg';
|
|
|
|
|
import { MessageType } from '@/constants/chat';
|
|
|
|
|
import { useOneNamespaceEffectsLoading } from '@/hooks/storeHooks';
|
|
|
|
|
import { useSelectUserInfo } from '@/hooks/userSettingHook';
|
|
|
|
|
import { IReference, Message } from '@/interfaces/database/chat';
|
2024-02-28 16:28:33 +08:00
|
|
|
import { Avatar, Button, Flex, Input, List, Popover, Space } from 'antd';
|
2024-02-22 17:14:25 +08:00
|
|
|
import classNames from 'classnames';
|
2024-02-26 18:38:54 +08:00
|
|
|
import { ChangeEventHandler, useCallback, useMemo, useState } from 'react';
|
|
|
|
|
import reactStringReplace from 'react-string-replace';
|
2024-02-27 19:05:50 +08:00
|
|
|
import {
|
2024-02-28 16:28:33 +08:00
|
|
|
useFetchConversationOnMount,
|
2024-02-27 19:05:50 +08:00
|
|
|
useGetFileIcon,
|
|
|
|
|
useSendMessage,
|
|
|
|
|
} from '../hooks';
|
2024-02-26 18:38:54 +08:00
|
|
|
|
2024-02-27 19:05:50 +08:00
|
|
|
import Image from '@/components/image';
|
|
|
|
|
import NewDocumentLink from '@/components/new-document-link';
|
2024-02-28 16:28:33 +08:00
|
|
|
import { useSelectFileThumbnails } from '@/hooks/knowledgeHook';
|
2024-02-26 18:38:54 +08:00
|
|
|
import { InfoCircleOutlined } from '@ant-design/icons';
|
|
|
|
|
import Markdown from 'react-markdown';
|
|
|
|
|
import { visitParents } from 'unist-util-visit-parents';
|
2024-02-19 19:16:23 +08:00
|
|
|
import styles from './index.less';
|
|
|
|
|
|
2024-02-27 19:05:50 +08:00
|
|
|
const reg = /(#{2}\d+\${2})/g;
|
|
|
|
|
|
|
|
|
|
const getChunkIndex = (match: string) => Number(match.slice(2, 3));
|
|
|
|
|
|
2024-02-26 18:38:54 +08:00
|
|
|
const rehypeWrapReference = () => {
|
|
|
|
|
return function wrapTextTransform(tree: any) {
|
|
|
|
|
visitParents(tree, 'text', (node, ancestors) => {
|
|
|
|
|
if (ancestors.at(-1).tagName !== 'custom-typography') {
|
|
|
|
|
node.type = 'element';
|
|
|
|
|
node.tagName = 'custom-typography';
|
|
|
|
|
node.properties = {};
|
|
|
|
|
node.children = [{ type: 'text', value: node.value }];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-27 19:05:50 +08:00
|
|
|
const MessageItem = ({
|
|
|
|
|
item,
|
|
|
|
|
reference,
|
|
|
|
|
}: {
|
|
|
|
|
item: Message;
|
|
|
|
|
reference: IReference;
|
|
|
|
|
}) => {
|
2024-02-26 18:38:54 +08:00
|
|
|
const userInfo = useSelectUserInfo();
|
2024-02-28 16:28:33 +08:00
|
|
|
const fileThumbnails = useSelectFileThumbnails();
|
2024-02-26 18:38:54 +08:00
|
|
|
|
2024-02-27 19:05:50 +08:00
|
|
|
const isAssistant = item.role === MessageType.Assistant;
|
|
|
|
|
|
|
|
|
|
const getPopoverContent = useCallback(
|
|
|
|
|
(chunkIndex: number) => {
|
|
|
|
|
const chunks = reference?.chunks ?? [];
|
|
|
|
|
const chunkItem = chunks[chunkIndex];
|
|
|
|
|
const document = reference?.doc_aggs.find(
|
|
|
|
|
(x) => x?.doc_id === chunkItem?.doc_id,
|
|
|
|
|
);
|
|
|
|
|
const documentId = document?.doc_id;
|
|
|
|
|
return (
|
|
|
|
|
<Flex
|
|
|
|
|
key={chunkItem?.chunk_id}
|
|
|
|
|
gap={10}
|
|
|
|
|
className={styles.referencePopoverWrapper}
|
|
|
|
|
>
|
2024-02-28 16:28:33 +08:00
|
|
|
<Popover
|
|
|
|
|
placement="topRight"
|
|
|
|
|
content={
|
|
|
|
|
<Image
|
|
|
|
|
id={chunkItem?.img_id}
|
|
|
|
|
className={styles.referenceImagePreview}
|
|
|
|
|
></Image>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<Image
|
|
|
|
|
id={chunkItem?.img_id}
|
|
|
|
|
className={styles.referenceChunkImage}
|
|
|
|
|
></Image>
|
|
|
|
|
</Popover>
|
2024-02-27 19:05:50 +08:00
|
|
|
<Space direction={'vertical'}>
|
|
|
|
|
<div>{chunkItem?.content_with_weight}</div>
|
|
|
|
|
{documentId && (
|
2024-02-28 16:28:33 +08:00
|
|
|
<Flex gap={'middle'}>
|
|
|
|
|
<img src={fileThumbnails[documentId]} alt="" />
|
|
|
|
|
<NewDocumentLink documentId={documentId}>
|
|
|
|
|
{document?.doc_name}
|
|
|
|
|
</NewDocumentLink>
|
|
|
|
|
</Flex>
|
2024-02-27 19:05:50 +08:00
|
|
|
)}
|
|
|
|
|
</Space>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-02-28 16:28:33 +08:00
|
|
|
[reference, fileThumbnails],
|
2024-02-26 18:38:54 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const renderReference = useCallback(
|
|
|
|
|
(text: string) => {
|
2024-02-27 19:05:50 +08:00
|
|
|
return reactStringReplace(text, reg, (match, i) => {
|
|
|
|
|
const chunkIndex = getChunkIndex(match);
|
2024-02-26 18:38:54 +08:00
|
|
|
return (
|
2024-02-27 19:05:50 +08:00
|
|
|
<Popover content={getPopoverContent(chunkIndex)}>
|
2024-02-26 18:38:54 +08:00
|
|
|
<InfoCircleOutlined key={i} className={styles.referenceIcon} />
|
|
|
|
|
</Popover>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-02-27 19:05:50 +08:00
|
|
|
[getPopoverContent],
|
2024-02-26 18:38:54 +08:00
|
|
|
);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
2024-02-27 19:05:50 +08:00
|
|
|
const referenceDocumentList = useMemo(() => {
|
|
|
|
|
return reference?.doc_aggs ?? [];
|
|
|
|
|
}, [reference?.doc_aggs]);
|
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={classNames(styles.messageItem, {
|
|
|
|
|
[styles.messageItemLeft]: item.role === MessageType.Assistant,
|
|
|
|
|
[styles.messageItemRight]: item.role === MessageType.User,
|
|
|
|
|
})}
|
|
|
|
|
>
|
2024-02-26 18:38:54 +08:00
|
|
|
<section
|
|
|
|
|
className={classNames(styles.messageItemSection, {
|
|
|
|
|
[styles.messageItemSectionLeft]: item.role === MessageType.Assistant,
|
|
|
|
|
[styles.messageItemSectionRight]: item.role === MessageType.User,
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={classNames(styles.messageItemContent, {
|
|
|
|
|
[styles.messageItemContentReverse]: item.role === MessageType.User,
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
{item.role === MessageType.User ? (
|
|
|
|
|
userInfo.avatar ?? (
|
|
|
|
|
<Avatar
|
|
|
|
|
size={40}
|
|
|
|
|
src={
|
|
|
|
|
userInfo.avatar ??
|
|
|
|
|
'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
) : (
|
|
|
|
|
<AssistantIcon></AssistantIcon>
|
|
|
|
|
)}
|
|
|
|
|
<Flex vertical gap={8} flex={1}>
|
2024-02-27 19:05:50 +08:00
|
|
|
<b>{isAssistant ? 'Resume Assistant' : 'You'}</b>
|
2024-02-26 18:38:54 +08:00
|
|
|
<div className={styles.messageText}>
|
|
|
|
|
<Markdown
|
|
|
|
|
rehypePlugins={[rehypeWrapReference]}
|
|
|
|
|
components={
|
|
|
|
|
{
|
|
|
|
|
'custom-typography': ({ children }: { children: string }) =>
|
|
|
|
|
renderReference(children),
|
|
|
|
|
} as any
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{item.content}
|
|
|
|
|
</Markdown>
|
|
|
|
|
</div>
|
2024-02-27 19:05:50 +08:00
|
|
|
{isAssistant && referenceDocumentList.length > 0 && (
|
|
|
|
|
<List
|
|
|
|
|
bordered
|
|
|
|
|
dataSource={referenceDocumentList}
|
|
|
|
|
renderItem={(item) => (
|
|
|
|
|
<List.Item>
|
2024-02-28 16:28:33 +08:00
|
|
|
{/* <SvgIcon name={getFileIcon(item.doc_name)}></SvgIcon> */}
|
|
|
|
|
<Flex gap={'middle'}>
|
|
|
|
|
<img src={fileThumbnails[item.doc_id]}></img>
|
|
|
|
|
<NewDocumentLink documentId={item.doc_id}>
|
|
|
|
|
{item.doc_name}
|
|
|
|
|
</NewDocumentLink>
|
|
|
|
|
</Flex>
|
2024-02-27 19:05:50 +08:00
|
|
|
</List.Item>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-02-26 18:38:54 +08:00
|
|
|
</Flex>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
2024-02-22 17:14:25 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-19 19:16:23 +08:00
|
|
|
const ChatContainer = () => {
|
|
|
|
|
const [value, setValue] = useState('');
|
2024-02-29 14:26:59 +08:00
|
|
|
const {
|
|
|
|
|
ref,
|
|
|
|
|
currentConversation: conversation,
|
|
|
|
|
addNewestConversation,
|
|
|
|
|
} = useFetchConversationOnMount();
|
2024-02-22 17:14:25 +08:00
|
|
|
const { sendMessage } = useSendMessage();
|
2024-02-29 14:26:59 +08:00
|
|
|
|
2024-02-26 18:38:54 +08:00
|
|
|
const loading = useOneNamespaceEffectsLoading('chatModel', [
|
|
|
|
|
'completeConversation',
|
|
|
|
|
]);
|
2024-02-27 19:05:50 +08:00
|
|
|
useGetFileIcon();
|
2024-02-19 19:16:23 +08:00
|
|
|
|
|
|
|
|
const handlePressEnter = () => {
|
2024-02-29 14:26:59 +08:00
|
|
|
if (!loading) {
|
|
|
|
|
setValue('');
|
|
|
|
|
addNewestConversation(value);
|
|
|
|
|
sendMessage(value);
|
|
|
|
|
}
|
2024-02-19 19:16:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
|
|
|
setValue(e.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Flex flex={1} className={styles.chatContainer} vertical>
|
2024-02-26 18:38:54 +08:00
|
|
|
<Flex flex={1} vertical className={styles.messageContainer}>
|
|
|
|
|
<div>
|
2024-02-27 19:05:50 +08:00
|
|
|
{conversation?.message?.map((message) => {
|
|
|
|
|
const assistantMessages = conversation?.message
|
|
|
|
|
?.filter((x) => x.role === MessageType.Assistant)
|
|
|
|
|
.slice(1);
|
|
|
|
|
const referenceIndex = assistantMessages.findIndex(
|
|
|
|
|
(x) => x.id === message.id,
|
|
|
|
|
);
|
|
|
|
|
const reference = conversation.reference[referenceIndex];
|
|
|
|
|
return (
|
|
|
|
|
<MessageItem
|
|
|
|
|
key={message.id}
|
|
|
|
|
item={message}
|
|
|
|
|
reference={reference}
|
|
|
|
|
></MessageItem>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-02-26 18:38:54 +08:00
|
|
|
</div>
|
2024-02-27 19:05:50 +08:00
|
|
|
<div ref={ref} />
|
2024-02-22 17:14:25 +08:00
|
|
|
</Flex>
|
2024-02-19 19:16:23 +08:00
|
|
|
<Input
|
|
|
|
|
size="large"
|
|
|
|
|
placeholder="Message Resume Assistant..."
|
|
|
|
|
value={value}
|
|
|
|
|
suffix={
|
2024-02-26 18:38:54 +08:00
|
|
|
<Button type="primary" onClick={handlePressEnter} loading={loading}>
|
2024-02-19 19:16:23 +08:00
|
|
|
Send
|
|
|
|
|
</Button>
|
|
|
|
|
}
|
|
|
|
|
onPressEnter={handlePressEnter}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
/>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ChatContainer;
|