feat: Add MessageInput to the external chat page #1880 (#1963)

### What problem does this PR solve?
feat: Add MessageInput to the external chat page #1880

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu 2024-08-15 16:10:21 +08:00 committed by GitHub
parent 6acc46bc7b
commit d3ff1a30bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 146 additions and 122 deletions

View File

@ -0,0 +1,6 @@
<svg t="1723702162999" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8508"
width="200" height="200">
<path
d="M780.8 166.4V742.4c0 92.928-56.32 268.8-268.8 268.8S243.2 835.328 243.2 742.4V204.8a230.4 230.4 0 0 1 23.296-93.952A179.2 179.2 0 0 1 435.2 12.8a179.2 179.2 0 0 1 168.704 98.048A228.608 228.608 0 0 1 627.2 204.8v499.2A111.872 111.872 0 0 1 512 819.2a111.872 111.872 0 0 1-115.2-115.2V358.4a38.4 38.4 0 0 1 76.8 0v345.6A35.072 35.072 0 0 0 512 742.4a35.328 35.328 0 0 0 38.4-38.912V204.8A109.568 109.568 0 0 0 435.2 89.6a102.4 102.4 0 0 0-100.096 55.552A158.976 158.976 0 0 0 320 204.8v537.6c0 19.456 6.912 192 192 192 189.952 0 192-184.064 192-192V166.4a38.4 38.4 0 0 1 76.8 0"
fill="#030000" p-id="8509"></path>
</svg>

After

Width:  |  Height:  |  Size: 799 B

View File

@ -50,13 +50,8 @@ const ChatOverviewModal = ({
hideModal: hideApiKeyModal, hideModal: hideApiKeyModal,
showModal: showApiKeyModal, showModal: showApiKeyModal,
} = useSetModalState(); } = useSetModalState();
const { const { embedVisible, hideEmbedModal, showEmbedModal, embedToken } =
embedVisible, useShowEmbedModal(id, idKey);
hideEmbedModal,
showEmbedModal,
embedToken,
errorContextHolder,
} = useShowEmbedModal(id, idKey);
const { pickerValue, setPickerValue } = useFetchNextStats(); const { pickerValue, setPickerValue } = useFetchNextStats();
@ -64,7 +59,7 @@ const ChatOverviewModal = ({
return current && current > dayjs().endOf('day'); return current && current > dayjs().endOf('day');
}; };
const { handlePreview, contextHolder } = usePreviewChat(id, idKey); const { handlePreview } = usePreviewChat(id, idKey);
return ( return (
<> <>
@ -138,8 +133,6 @@ const ChatOverviewModal = ({
visible={embedVisible} visible={embedVisible}
hideModal={hideEmbedModal} hideModal={hideEmbedModal}
></EmbedModal> ></EmbedModal>
{contextHolder}
{errorContextHolder}
</Modal> </Modal>
</> </>
); );

View File

@ -63,13 +63,12 @@ export const useSelectChartStatsList = (): ChartStatsType => {
}; };
export const useShowTokenEmptyError = () => { export const useShowTokenEmptyError = () => {
const [messageApi, contextHolder] = message.useMessage();
const { t } = useTranslate('chat'); const { t } = useTranslate('chat');
const showTokenEmptyError = useCallback(() => { const showTokenEmptyError = useCallback(() => {
messageApi.error(t('tokenError')); message.error(t('tokenError'));
}, [messageApi, t]); }, [t]);
return { showTokenEmptyError, contextHolder }; return { showTokenEmptyError };
}; };
const getUrlWithToken = (token: string) => { const getUrlWithToken = (token: string) => {
@ -78,7 +77,7 @@ const getUrlWithToken = (token: string) => {
}; };
const useFetchTokenListBeforeOtherStep = (dialogId: string, idKey: string) => { const useFetchTokenListBeforeOtherStep = (dialogId: string, idKey: string) => {
const { showTokenEmptyError, contextHolder } = useShowTokenEmptyError(); const { showTokenEmptyError } = useShowTokenEmptyError();
const { data: tokenList, refetch } = useFetchTokenList({ [idKey]: dialogId }); const { data: tokenList, refetch } = useFetchTokenList({ [idKey]: dialogId });
@ -98,7 +97,6 @@ const useFetchTokenListBeforeOtherStep = (dialogId: string, idKey: string) => {
return { return {
token, token,
contextHolder,
handleOperate, handleOperate,
}; };
}; };
@ -110,8 +108,10 @@ export const useShowEmbedModal = (dialogId: string, idKey: string) => {
showModal: showEmbedModal, showModal: showEmbedModal,
} = useSetModalState(); } = useSetModalState();
const { handleOperate, token, contextHolder } = const { handleOperate, token } = useFetchTokenListBeforeOtherStep(
useFetchTokenListBeforeOtherStep(dialogId, idKey); dialogId,
idKey,
);
const handleShowEmbedModal = useCallback(async () => { const handleShowEmbedModal = useCallback(async () => {
const succeed = await handleOperate(); const succeed = await handleOperate();
@ -125,15 +125,11 @@ export const useShowEmbedModal = (dialogId: string, idKey: string) => {
hideEmbedModal, hideEmbedModal,
embedVisible, embedVisible,
embedToken: token, embedToken: token,
errorContextHolder: contextHolder,
}; };
}; };
export const usePreviewChat = (dialogId: string, idKey: string) => { export const usePreviewChat = (dialogId: string, idKey: string) => {
const { handleOperate, contextHolder } = useFetchTokenListBeforeOtherStep( const { handleOperate } = useFetchTokenListBeforeOtherStep(dialogId, idKey);
dialogId,
idKey,
);
const open = useCallback((t: string) => { const open = useCallback((t: string) => {
window.open(getUrlWithToken(t), '_blank'); window.open(getUrlWithToken(t), '_blank');
@ -148,6 +144,5 @@ export const usePreviewChat = (dialogId: string, idKey: string) => {
return { return {
handlePreview, handlePreview,
contextHolder,
}; };
}; };

View File

@ -1,11 +1,23 @@
.messageInputWrapper { .messageInputWrapper {
margin-right: 20px; margin-right: 20px;
background-color: #f5f5f8;
border-radius: 8px;
:global(.ant-input-affix-wrapper) {
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.documentCard { .documentCard {
:global(.ant-card-body) { :global(.ant-card-body) {
padding: 10px; padding: 10px;
position: relative; position: relative;
} }
} }
.listWrapper {
padding: 0 10px;
}
.inputWrapper {
border-radius: 8px;
}
.deleteIcon { .deleteIcon {
position: absolute; position: absolute;
right: -4px; right: -4px;

View File

@ -1,14 +1,13 @@
import { Authorization } from '@/constants/authorization'; import { Authorization } from '@/constants/authorization';
import { useTranslate } from '@/hooks/common-hooks'; import { useTranslate } from '@/hooks/common-hooks';
import { useRemoveNextDocument } from '@/hooks/document-hooks'; import {
useFetchDocumentInfosByIds,
useRemoveNextDocument,
} from '@/hooks/document-hooks';
import { getAuthorization } from '@/utils/authorization-util'; import { getAuthorization } from '@/utils/authorization-util';
import { getExtension } from '@/utils/document-util'; import { getExtension } from '@/utils/document-util';
import { import { formatBytes } from '@/utils/file-util';
CloseCircleOutlined, import { CloseCircleOutlined, LoadingOutlined } from '@ant-design/icons';
LoadingOutlined,
PlusOutlined,
UploadOutlined,
} from '@ant-design/icons';
import type { GetProp, UploadFile } from 'antd'; import type { GetProp, UploadFile } from 'antd';
import { import {
Button, Button,
@ -22,10 +21,11 @@ import {
Upload, Upload,
UploadProps, UploadProps,
} from 'antd'; } from 'antd';
import classNames from 'classnames';
import get from 'lodash/get'; import get from 'lodash/get';
import { ChangeEventHandler, useCallback, useState } from 'react'; import { ChangeEventHandler, useCallback, useEffect, useState } from 'react';
import FileIcon from '../file-icon'; import FileIcon from '../file-icon';
import SvgIcon from '../svg-icon';
import styles from './index.less'; import styles from './index.less';
type FileType = Parameters<GetProp<UploadProps, 'beforeUpload'>>[0]; type FileType = Parameters<GetProp<UploadProps, 'beforeUpload'>>[0];
@ -33,6 +33,14 @@ const { Text } = Typography;
const getFileId = (file: UploadFile) => get(file, 'response.data.0'); const getFileId = (file: UploadFile) => get(file, 'response.data.0');
const getFileIds = (fileList: UploadFile[]) => {
const ids = fileList.reduce((pre, cur) => {
return pre.concat(get(cur, 'response.data', []));
}, []);
return ids;
};
interface IProps { interface IProps {
disabled: boolean; disabled: boolean;
value: string; value: string;
@ -41,6 +49,7 @@ interface IProps {
onPressEnter(documentIds: string[]): void; onPressEnter(documentIds: string[]): void;
onInputChange: ChangeEventHandler<HTMLInputElement>; onInputChange: ChangeEventHandler<HTMLInputElement>;
conversationId: string; conversationId: string;
uploadUrl?: string;
} }
const getBase64 = (file: FileType): Promise<string> => const getBase64 = (file: FileType): Promise<string> =>
@ -59,9 +68,11 @@ const MessageInput = ({
sendLoading, sendLoading,
onInputChange, onInputChange,
conversationId, conversationId,
uploadUrl = '/v1/document/upload_and_parse',
}: IProps) => { }: IProps) => {
const { t } = useTranslate('chat'); const { t } = useTranslate('chat');
const { removeDocument } = useRemoveNextDocument(); const { removeDocument } = useRemoveNextDocument();
const { data: documentInfos, setDocumentIds } = useFetchDocumentInfosByIds();
const [fileList, setFileList] = useState<UploadFile[]>([]); const [fileList, setFileList] = useState<UploadFile[]>([]);
@ -78,9 +89,7 @@ const MessageInput = ({
const handlePressEnter = useCallback(async () => { const handlePressEnter = useCallback(async () => {
if (isUploadingFile) return; if (isUploadingFile) return;
const ids = fileList.reduce((pre, cur) => { const ids = getFileIds(fileList);
return pre.concat(get(cur, 'response.data', []));
}, []);
onPressEnter(ids); onPressEnter(ids);
setFileList([]); setFileList([]);
@ -99,13 +108,18 @@ const MessageInput = ({
[removeDocument], [removeDocument],
); );
const uploadButton = ( const getDocumentInfoById = useCallback(
<button style={{ border: 0, background: 'none' }} type="button"> (id: string) => {
<PlusOutlined /> return documentInfos.find((x) => x.id === id);
<div style={{ marginTop: 8 }}>Upload</div> },
</button> [documentInfos],
); );
useEffect(() => {
const ids = getFileIds(fileList);
setDocumentIds(ids);
}, [fileList, setDocumentIds]);
return ( return (
<Flex gap={20} vertical className={styles.messageInputWrapper}> <Flex gap={20} vertical className={styles.messageInputWrapper}>
<Input <Input
@ -113,23 +127,30 @@ const MessageInput = ({
placeholder={t('sendPlaceholder')} placeholder={t('sendPlaceholder')}
value={value} value={value}
disabled={disabled} disabled={disabled}
className={classNames({ [styles.inputWrapper]: fileList.length === 0 })}
suffix={ suffix={
<Space> <Space>
<Upload {conversationId && (
action="/v1/document/upload_and_parse" <Upload
// listType="picture-card" action={uploadUrl}
fileList={fileList} fileList={fileList}
onPreview={handlePreview} onPreview={handlePreview}
onChange={handleChange} onChange={handleChange}
multiple multiple
headers={{ [Authorization]: getAuthorization() }} headers={{ [Authorization]: getAuthorization() }}
data={{ conversation_id: conversationId }} data={{ conversation_id: conversationId }}
method="post" method="post"
onRemove={handleRemove} onRemove={handleRemove}
showUploadList={false} showUploadList={false}
> >
<Button icon={<UploadOutlined />}></Button> <Button
</Upload> type={'text'}
icon={
<SvgIcon name="paper-clip" width={18} height={22}></SvgIcon>
}
></Button>
</Upload>
)}
<Button <Button
type="primary" type="primary"
onClick={handlePressEnter} onClick={handlePressEnter}
@ -143,71 +164,58 @@ const MessageInput = ({
onPressEnter={handlePressEnter} onPressEnter={handlePressEnter}
onChange={onInputChange} onChange={onInputChange}
/> />
{/* <Upload
action="/v1/document/upload_and_parse"
listType="picture-card"
fileList={fileList}
onPreview={handlePreview}
onChange={handleChange}
multiple
headers={{ [Authorization]: getAuthorization() }}
data={{ conversation_id: conversationId }}
method="post"
onRemove={handleRemove}
>
{fileList.length >= 8 ? null : uploadButton}
</Upload> */}
{fileList.length > 0 && ( {fileList.length > 0 && (
<List <List
grid={{ grid={{
gutter: 16, gutter: 16,
xs: 1, xs: 1,
sm: 2, sm: 1,
md: 2, md: 1,
lg: 1, lg: 1,
xl: 2, xl: 2,
xxl: 4, xxl: 4,
}} }}
dataSource={fileList} dataSource={fileList}
className={styles.listWrapper}
renderItem={(item) => { renderItem={(item) => {
const fileExtension = getExtension(item.name); const fileExtension = getExtension(item.name);
const id = getFileId(item);
return ( return (
<List.Item> <List.Item>
<Card className={styles.documentCard}> <Card className={styles.documentCard}>
<> <Flex gap={10} align="center">
<Flex gap={10} align="center"> {item.status === 'uploading' || !item.response ? (
{item.status === 'uploading' || !item.response ? ( <Spin
<Spin indicator={
indicator={ <LoadingOutlined style={{ fontSize: 24 }} spin />
<LoadingOutlined style={{ fontSize: 24 }} spin /> }
} />
/> ) : (
<FileIcon id={id} name={item.name}></FileIcon>
)}
<Flex vertical style={{ width: '90%' }}>
<Text
ellipsis={{ tooltip: item.name }}
className={styles.nameText}
>
<b> {item.name}</b>
</Text>
{item.percent !== 100 ? (
t('uploading')
) : !item.response ? (
t('parsing')
) : ( ) : (
<FileIcon <Space>
id={getFileId(item)} <span>{fileExtension?.toUpperCase()},</span>
name={item.name} <span>
></FileIcon> {formatBytes(getDocumentInfoById(id)?.size ?? 0)}
</span>
</Space>
)} )}
<Flex vertical style={{ width: '90%' }}>
<Text
ellipsis={{ tooltip: item.name }}
className={styles.nameText}
>
<b> {item.name}</b>
</Text>
{item.percent !== 100 ? (
'上传中'
) : !item.response ? (
'解析中'
) : (
<Space>
<span>{fileExtension?.toUpperCase()},</span>
</Space>
)}
</Flex>
</Flex> </Flex>
</> </Flex>
{item.status !== 'uploading' && ( {item.status !== 'uploading' && (
<CloseCircleOutlined <CloseCircleOutlined

View File

@ -422,6 +422,8 @@ The above is the content you need to summarize.`,
extensionTitle: 'Chrome Extension', extensionTitle: 'Chrome Extension',
tokenError: 'Please create API Token first!', tokenError: 'Please create API Token first!',
searching: 'searching...', searching: 'searching...',
parsing: 'Parsing',
uploading: 'Uploading',
}, },
setting: { setting: {
profile: 'Profile', profile: 'Profile',

View File

@ -392,6 +392,8 @@ export default {
extensionTitle: 'Chrome 插件', extensionTitle: 'Chrome 插件',
tokenError: '請先創建 Api Token!', tokenError: '請先創建 Api Token!',
searching: '搜索中', searching: '搜索中',
parsing: '解析中',
uploading: '上傳中',
}, },
setting: { setting: {
profile: '概述', profile: '概述',

View File

@ -409,6 +409,8 @@ export default {
extensionTitle: 'Chrome 插件', extensionTitle: 'Chrome 插件',
tokenError: '请先创建 Api Token!', tokenError: '请先创建 Api Token!',
searching: '搜索中', searching: '搜索中',
parsing: '解析中',
uploading: '上传中',
}, },
setting: { setting: {
profile: '概要', profile: '概要',

View File

@ -1,8 +1,8 @@
import MessageInput from '@/components/message-input';
import MessageItem from '@/components/message-item'; import MessageItem from '@/components/message-item';
import { MessageType } from '@/constants/chat'; import { MessageType } from '@/constants/chat';
import { useTranslate } from '@/hooks/common-hooks';
import { useSendButtonDisabled } from '@/pages/chat/hooks'; import { useSendButtonDisabled } from '@/pages/chat/hooks';
import { Button, Flex, Input, Spin } from 'antd'; import { Flex, Spin } from 'antd';
import { forwardRef } from 'react'; import { forwardRef } from 'react';
import { import {
useCreateSharedConversationOnMount, useCreateSharedConversationOnMount,
@ -10,11 +10,9 @@ import {
useSendSharedMessage, useSendSharedMessage,
} from '../shared-hooks'; } from '../shared-hooks';
import { buildMessageItemReference } from '../utils'; import { buildMessageItemReference } from '../utils';
import styles from './index.less'; import styles from './index.less';
const ChatContainer = () => { const ChatContainer = () => {
const { t } = useTranslate('chat');
const { conversationId } = useCreateSharedConversationOnMount(); const { conversationId } = useCreateSharedConversationOnMount();
const { const {
currentConversation: conversation, currentConversation: conversation,
@ -65,24 +63,17 @@ const ChatContainer = () => {
</div> </div>
<div ref={ref} /> <div ref={ref} />
</Flex> </Flex>
<Input
size="large" <MessageInput
placeholder={t('sendPlaceholder')}
value={value} value={value}
// disabled={disabled} disabled={false}
suffix={ sendDisabled={sendDisabled}
<Button conversationId={conversationId}
type="primary" onInputChange={handleInputChange}
onClick={handlePressEnter}
loading={sendLoading}
disabled={sendDisabled}
>
{t('send')}
</Button>
}
onPressEnter={handlePressEnter} onPressEnter={handlePressEnter}
onChange={handleInputChange} sendLoading={sendLoading}
/> uploadUrl="/v1/api/document/upload_and_parse"
></MessageInput>
</Flex> </Flex>
</> </>
); );

View File

@ -85,3 +85,16 @@ export const downloadFile = ({
downloadElement.click(); downloadElement.click();
document.body.removeChild(downloadElement); document.body.removeChild(downloadElement);
}; };
const Units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
export const formatBytes = (x: string | number) => {
let l = 0,
n = (typeof x === 'string' ? parseInt(x, 10) : x) || 0;
while (n >= 1024 && ++l) {
n = n / 1024;
}
return n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + Units[l];
};