Skip to content

Commit

Permalink
feat: Create a conversation before uploading files in it infiniflow#1880
Browse files Browse the repository at this point in the history
  • Loading branch information
cike8899 committed Aug 22, 2024
1 parent 35e880c commit 2690ef7
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 21 deletions.
99 changes: 80 additions & 19 deletions web/src/components/message-input/index.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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';
Expand Down Expand Up @@ -64,9 +70,10 @@ interface IProps {
onPressEnter(documentIds: string[]): void;
onInputChange: ChangeEventHandler<HTMLInputElement>;
conversationId: string;
uploadUrl?: string;
uploadMethod?: string;
isShared?: boolean;
showUploadIcon?: boolean;
createConversationBeforeUploadDocument?(message: string): Promise<any>;
}

const getBase64 = (file: FileType): Promise<string> =>
Expand All @@ -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<UploadFile[]>([]);

Expand All @@ -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 () => {
Expand Down Expand Up @@ -150,6 +195,16 @@ const MessageInput = ({
setDocumentIds(ids);
}, [fileList, setDocumentIds]);

useEffect(() => {
if (
conversationIdRef.current &&
conversationId !== conversationIdRef.current
) {
setFileList([]);
}
conversationIdRef.current = conversationId;
}, [conversationId, setFileList]);

return (
<Flex gap={20} vertical className={styles.messageInputWrapper}>
<Input
Expand All @@ -160,18 +215,22 @@ const MessageInput = ({
className={classNames({ [styles.inputWrapper]: fileList.length === 0 })}
suffix={
<Space>
{conversationId && showUploadIcon && (
{showUploadIcon && (
<Upload
action={uploadUrl}
fileList={fileList}
// action={uploadUrl}
// fileList={fileList}
onPreview={handlePreview}
onChange={handleChange}
multiple
headers={{ [Authorization]: getAuthorization() }}
data={{ conversation_id: conversationId }}
method="post"
multiple={false}
// headers={{ [Authorization]: getAuthorization() }}
// data={{ conversation_id: conversationId }}
// method="post"
onRemove={handleRemove}
showUploadList={false}
beforeUpload={(file, fileList) => {
console.log('🚀 ~ beforeUpload:', fileList);
return false;
}}
>
<Button
type={'text'}
Expand Down Expand Up @@ -209,8 +268,10 @@ const MessageInput = ({
dataSource={fileList}
className={styles.listWrapper}
renderItem={(item) => {
const fileExtension = getExtension(item.name);
const id = getFileId(item);
const documentInfo = getDocumentInfoById(id);
const fileExtension = getExtension(documentInfo?.name ?? '');
const fileName = item.originFileObj?.name ?? '';

return (
<List.Item>
Expand All @@ -228,14 +289,14 @@ const MessageInput = ({
// width={30}
></InfoCircleOutlined>
) : (
<FileIcon id={id} name={item.name}></FileIcon>
<FileIcon id={id} name={fileName}></FileIcon>
)}
<Flex vertical style={{ width: '90%' }}>
<Text
ellipsis={{ tooltip: item.name }}
ellipsis={{ tooltip: fileName }}
className={styles.nameText}
>
<b> {item.name}</b>
<b> {fileName}</b>
</Text>
{isUploadError(item) ? (
t('uploadFailed')
Expand Down Expand Up @@ -275,4 +336,4 @@ const MessageInput = ({
);
};

export default MessageInput;
export default memo(MessageInput);
17 changes: 17 additions & 0 deletions web/src/hooks/chat-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ export const useUpdateConversation = () => {
return updateConversation;
};

export const useUpdateNextConversation = () => {
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['updateConversation'],
mutationFn: async (params: Record<string, any>) => {
const { data } = await chatService.setConversation(params);

return data;
},
});

return { data, loading, updateConversation: mutateAsync };
};

export const useSetDialog = () => {
const dispatch = useDispatch();

Expand Down
32 changes: 32 additions & 0 deletions web/src/hooks/document-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IDocumentInfo } from '@/interfaces/database/document';
import { IChunk, IKnowledgeFile } from '@/interfaces/database/knowledge';
import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
import chatService from '@/services/chat-service';
import kbService from '@/services/knowledge-service';
import { api_host } from '@/utils/api';
import { buildChunkHighlights } from '@/utils/document-util';
Expand Down Expand Up @@ -333,3 +334,34 @@ export const useDeleteDocument = () => {

return { data, loading, deleteDocument: mutateAsync };
};

export const useUploadAndParseDocument = (uploadMethod: string) => {
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['uploadAndParseDocument'],
mutationFn: async ({
conversationId,
fileList,
}: {
conversationId: string;
fileList: UploadFile[];
}) => {
const formData = new FormData();
formData.append('conversation_id', conversationId);
fileList.forEach((file: UploadFile) => {
formData.append('file', file as any);
});
if (uploadMethod === 'upload_and_parse') {
const data = await kbService.upload_and_parse(formData);
return data?.data;
}
const data = await chatService.uploadAndParseExternal(formData);
return data?.data;
},
});

return { data, loading, uploadAndParseDocument: mutateAsync };
};
9 changes: 8 additions & 1 deletion web/src/pages/chat/chat-container/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MessageType } from '@/constants/chat';
import { Drawer, Flex, Spin } from 'antd';
import {
useClickDrawer,
useCreateConversationBeforeUploadDocument,
useFetchConversationOnMount,
useGetFileIcon,
useGetSendButtonDisabled,
Expand All @@ -24,6 +25,7 @@ const ChatContainer = () => {
addNewestConversation,
removeLatestMessage,
addNewestAnswer,
conversationId,
} = useFetchConversationOnMount();
const {
handleInputChange,
Expand All @@ -43,6 +45,8 @@ const ChatContainer = () => {
useGetFileIcon();
const loading = useSelectConversationLoading();
const { data: userInfo } = useFetchUserInfo();
const { createConversationBeforeUploadDocument } =
useCreateConversationBeforeUploadDocument();

return (
<>
Expand Down Expand Up @@ -78,7 +82,10 @@ const ChatContainer = () => {
value={value}
onInputChange={handleInputChange}
onPressEnter={handlePressEnter}
conversationId={conversation.id}
conversationId={conversationId}
createConversationBeforeUploadDocument={
createConversationBeforeUploadDocument
}
></MessageInput>
</Flex>
<Drawer
Expand Down
25 changes: 25 additions & 0 deletions web/src/pages/chat/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ export const useFetchConversationOnMount = () => {
ref,
removeLatestMessage,
addNewestAnswer,
conversationId,
};
};

Expand Down Expand Up @@ -769,4 +770,28 @@ export const useGetSendButtonDisabled = () => {
export const useSendButtonDisabled = (value: string) => {
return trim(value) === '';
};

export const useCreateConversationBeforeUploadDocument = () => {
const { setConversation } = useSetConversation();
const { dialogId } = useGetChatSearchParams();

const { handleClickConversation } = useClickConversationCard();

const createConversationBeforeUploadDocument = useCallback(
async (message: string) => {
const data = await setConversation(message);
if (data.retcode === 0) {
const id = data.data.id;
handleClickConversation(id);
}
return data;
},
[setConversation, handleClickConversation],
);

return {
createConversationBeforeUploadDocument,
dialogId,
};
};
//#endregion
2 changes: 1 addition & 1 deletion web/src/pages/chat/share/large.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const ChatContainer = () => {
onInputChange={handleInputChange}
onPressEnter={handlePressEnter}
sendLoading={sendLoading}
uploadUrl="/v1/api/document/upload_and_parse"
uploadMethod="external_upload_and_parse"
showUploadIcon={from === SharedFrom.Chat}
></MessageInput>
</Flex>
Expand Down
5 changes: 5 additions & 0 deletions web/src/services/chat-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
createExternalConversation,
getExternalConversation,
completeExternalConversation,
uploadAndParseExternal,
} = api;

const methods = {
Expand Down Expand Up @@ -86,6 +87,10 @@ const methods = {
url: completeExternalConversation,
method: 'post',
},
uploadAndParseExternal: {
url: uploadAndParseExternal,
method: 'post',
},
} as const;

const chatService = registerServer<keyof typeof methods>(methods, request);
Expand Down
5 changes: 5 additions & 0 deletions web/src/services/knowledge-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
web_crawl,
knowledge_graph,
document_infos,
upload_and_parse,
} = api;

const methods = {
Expand Down Expand Up @@ -136,6 +137,10 @@ const methods = {
url: document_delete,
method: 'delete',
},
upload_and_parse: {
url: upload_and_parse,
method: 'post',
},
};

const kbService = registerServer<keyof typeof methods>(methods, request);
Expand Down
2 changes: 2 additions & 0 deletions web/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default {
document_upload: `${api_host}/document/upload`,
web_crawl: `${api_host}/document/web_crawl`,
document_infos: `${api_host}/document/infos`,
upload_and_parse: `${api_host}/document/upload_and_parse`,

// chat
setDialog: `${api_host}/dialog/set`,
Expand All @@ -70,6 +71,7 @@ export default {
createExternalConversation: `${api_host}/api/new_conversation`,
getExternalConversation: `${api_host}/api/conversation`,
completeExternalConversation: `${api_host}/api/completion`,
uploadAndParseExternal: `${api_host}/api/document/upload_and_parse`,

// file manager
listFile: `${api_host}/file/list`,
Expand Down

0 comments on commit 2690ef7

Please sign in to comment.