File size: 7,779 Bytes
aa396c5 6a6f6eb 058cd84 aa396c5 8e222fd 6a6f6eb 8e222fd 058cd84 aa396c5 8e222fd 6a6f6eb aa396c5 452020d 8e222fd aa396c5 8e222fd aa396c5 6a6f6eb aa396c5 8e222fd 6a6f6eb 8e222fd 6a6f6eb 8e222fd 6a6f6eb aa396c5 8e222fd aa396c5 8e222fd aa396c5 8e222fd aa396c5 058cd84 8e222fd 058cd84 aa396c5 8e222fd aa396c5 8e222fd 6a6f6eb 8e222fd aa396c5 058cd84 452020d 6a6f6eb 058cd84 aa396c5 8e222fd 452020d aa396c5 058cd84 452020d aa396c5 8e222fd aa396c5 8e222fd 058cd84 452020d aa396c5 452020d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
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';
import { Avatar, Button, Flex, Input, List, Popover, Space } from 'antd';
import classNames from 'classnames';
import { ChangeEventHandler, useCallback, useMemo, useState } from 'react';
import reactStringReplace from 'react-string-replace';
import {
useFetchConversationOnMount,
useGetFileIcon,
useScrollToBottom,
useSendMessage,
} from '../hooks';
import { IClientConversation } from '../interface';
import Image from '@/components/image';
import NewDocumentLink from '@/components/new-document-link';
import { useSelectFileThumbnails } from '@/hooks/knowledgeHook';
import { InfoCircleOutlined } from '@ant-design/icons';
import Markdown from 'react-markdown';
import { visitParents } from 'unist-util-visit-parents';
import styles from './index.less';
const reg = /(#{2}\d+\${2})/g;
const getChunkIndex = (match: string) => Number(match.slice(2, 3));
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 }];
}
});
};
};
const MessageItem = ({
item,
reference,
}: {
item: Message;
reference: IReference;
}) => {
const userInfo = useSelectUserInfo();
const fileThumbnails = useSelectFileThumbnails();
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}
>
<Popover
placement="topRight"
content={
<Image
id={chunkItem?.img_id}
className={styles.referenceImagePreview}
></Image>
}
>
<Image
id={chunkItem?.img_id}
className={styles.referenceChunkImage}
></Image>
</Popover>
<Space direction={'vertical'}>
<div>{chunkItem?.content_with_weight}</div>
{documentId && (
<Flex gap={'middle'}>
<img src={fileThumbnails[documentId]} alt="" />
<NewDocumentLink documentId={documentId}>
{document?.doc_name}
</NewDocumentLink>
</Flex>
)}
</Space>
</Flex>
);
},
[reference, fileThumbnails],
);
const renderReference = useCallback(
(text: string) => {
return reactStringReplace(text, reg, (match, i) => {
const chunkIndex = getChunkIndex(match);
return (
<Popover content={getPopoverContent(chunkIndex)}>
<InfoCircleOutlined key={i} className={styles.referenceIcon} />
</Popover>
);
});
},
[getPopoverContent],
);
const referenceDocumentList = useMemo(() => {
return reference?.doc_aggs ?? [];
}, [reference?.doc_aggs]);
return (
<div
className={classNames(styles.messageItem, {
[styles.messageItemLeft]: item.role === MessageType.Assistant,
[styles.messageItemRight]: item.role === MessageType.User,
})}
>
<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}>
<b>{isAssistant ? 'Resume Assistant' : 'You'}</b>
<div className={styles.messageText}>
<Markdown
rehypePlugins={[rehypeWrapReference]}
components={
{
'custom-typography': ({ children }: { children: string }) =>
renderReference(children),
} as any
}
>
{item.content}
</Markdown>
</div>
{isAssistant && referenceDocumentList.length > 0 && (
<List
bordered
dataSource={referenceDocumentList}
renderItem={(item) => (
<List.Item>
{/* <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>
</List.Item>
)}
/>
)}
</Flex>
</div>
</section>
</div>
);
};
const ChatContainer = () => {
const [value, setValue] = useState('');
const conversation: IClientConversation = useFetchConversationOnMount();
const { sendMessage } = useSendMessage();
const loading = useOneNamespaceEffectsLoading('chatModel', [
'completeConversation',
]);
const ref = useScrollToBottom();
useGetFileIcon();
const handlePressEnter = () => {
setValue('');
sendMessage(value);
};
const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {
setValue(e.target.value);
};
return (
<Flex flex={1} className={styles.chatContainer} vertical>
<Flex flex={1} vertical className={styles.messageContainer}>
<div>
{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>
);
})}
</div>
<div ref={ref} />
</Flex>
<Input
size="large"
placeholder="Message Resume Assistant..."
value={value}
suffix={
<Button type="primary" onClick={handlePressEnter} loading={loading}>
Send
</Button>
}
onPressEnter={handlePressEnter}
onChange={handleInputChange}
/>
</Flex>
);
};
export default ChatContainer;
|