From c739b68b29a4a3d1f75d5a8c9dae341a8a1ec8a5 Mon Sep 17 00:00:00 2001 From: balibabu Date: Thu, 22 Aug 2024 18:01:48 +0800 Subject: [PATCH] feat: Create a conversation before uploading files in it #1880 (#2057) ### What problem does this PR solve? feat: Create a conversation before uploading files in it #1880 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- web/src/components/message-input/index.tsx | 99 +++++++++++++++++---- web/src/hooks/chat-hooks.ts | 17 ++++ web/src/hooks/document-hooks.ts | 32 +++++++ web/src/pages/chat/chat-container/index.tsx | 9 +- web/src/pages/chat/hooks.ts | 25 ++++++ web/src/pages/chat/share/large.tsx | 2 +- web/src/services/chat-service.ts | 5 ++ web/src/services/knowledge-service.ts | 5 ++ web/src/utils/api.ts | 2 + 9 files changed, 175 insertions(+), 21 deletions(-) diff --git a/web/src/components/message-input/index.tsx b/web/src/components/message-input/index.tsx index 1f692a8ed..56e8d6afd 100644 --- a/web/src/components/message-input/index.tsx +++ b/web/src/components/message-input/index.tsx @@ -1,11 +1,10 @@ -import { Authorization } from '@/constants/authorization'; import { useTranslate } from '@/hooks/common-hooks'; import { useDeleteDocument, useFetchDocumentInfosByIds, useRemoveNextDocument, + useUploadAndParseDocument, } from '@/hooks/document-hooks'; -import { getAuthorization } from '@/utils/authorization-util'; import { getExtension } from '@/utils/document-util'; import { formatBytes } from '@/utils/file-util'; import { @@ -28,7 +27,14 @@ import { } from 'antd'; import classNames from 'classnames'; import get from 'lodash/get'; -import { ChangeEventHandler, useCallback, useEffect, useState } from 'react'; +import { + ChangeEventHandler, + memo, + useCallback, + useEffect, + useRef, + useState, +} from 'react'; import FileIcon from '../file-icon'; import SvgIcon from '../svg-icon'; import styles from './index.less'; @@ -64,9 +70,10 @@ interface IProps { onPressEnter(documentIds: string[]): void; onInputChange: ChangeEventHandler; conversationId: string; - uploadUrl?: string; + uploadMethod?: string; isShared?: boolean; showUploadIcon?: boolean; + createConversationBeforeUploadDocument?(message: string): Promise; } const getBase64 = (file: FileType): Promise => @@ -87,12 +94,15 @@ const MessageInput = ({ onInputChange, conversationId, showUploadIcon = true, - uploadUrl = '/v1/document/upload_and_parse', + createConversationBeforeUploadDocument, + uploadMethod = 'upload_and_parse', }: IProps) => { const { t } = useTranslate('chat'); const { removeDocument } = useRemoveNextDocument(); const { deleteDocument } = useDeleteDocument(); const { data: documentInfos, setDocumentIds } = useFetchDocumentInfosByIds(); + const { uploadAndParseDocument } = useUploadAndParseDocument(uploadMethod); + const conversationIdRef = useRef(conversationId); const [fileList, setFileList] = useState([]); @@ -102,9 +112,44 @@ const MessageInput = ({ } }; - const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) => { - setFileList(newFileList); + const handleChange: UploadProps['onChange'] = async ({ + // fileList: newFileList, + file, + }) => { + let nextConversationId: string = conversationId; + if (createConversationBeforeUploadDocument && !conversationId) { + const creatingRet = await createConversationBeforeUploadDocument( + file.name, + ); + if (creatingRet.retcode === 0) { + nextConversationId = creatingRet.data.id; + } + } + setFileList((list) => { + list.push({ + ...file, + status: 'uploading', + originFileObj: file as any, + }); + return [...list]; + }); + const ret = await uploadAndParseDocument({ + conversationId: nextConversationId, + fileList: [file], + }); + setFileList((list) => { + const nextList = list.filter((x) => x.uid !== file.uid); + nextList.push({ + ...file, + originFileObj: file as any, + response: ret, + percent: 100, + status: ret?.retcode === 0 ? 'done' : 'error', + }); + return nextList; + }); }; + const isUploadingFile = fileList.some((x) => x.status === 'uploading'); const handlePressEnter = useCallback(async () => { @@ -150,6 +195,16 @@ const MessageInput = ({ setDocumentIds(ids); }, [fileList, setDocumentIds]); + useEffect(() => { + if ( + conversationIdRef.current && + conversationId !== conversationIdRef.current + ) { + setFileList([]); + } + conversationIdRef.current = conversationId; + }, [conversationId, setFileList]); + return ( - {conversationId && showUploadIcon && ( + {showUploadIcon && ( { + console.log('🚀 ~ beforeUpload:', fileList); + return false; + }} >