mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Feat: Users can chat directly without first creating a conversation. #11768 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -433,7 +433,7 @@ export const useSelectDerivedMessages = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const addNewestQuestion = useCallback(
|
const addNewestQuestion = useCallback(
|
||||||
(message: Message, answer: string = '') => {
|
(message: IMessage, answer: string = '') => {
|
||||||
setDerivedMessages((pre) => {
|
setDerivedMessages((pre) => {
|
||||||
return [
|
return [
|
||||||
...pre,
|
...pre,
|
||||||
@ -446,6 +446,7 @@ export const useSelectDerivedMessages = () => {
|
|||||||
{
|
{
|
||||||
role: MessageType.Assistant,
|
role: MessageType.Assistant,
|
||||||
content: answer,
|
content: answer,
|
||||||
|
conversationId: message.conversationId,
|
||||||
id: buildMessageUuid({ ...message, role: MessageType.Assistant }),
|
id: buildMessageUuid({ ...message, role: MessageType.Assistant }),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@ -16,11 +16,11 @@ import { useGetSharedChatSearchParams } from '@/pages/next-chats/hooks/use-send-
|
|||||||
import { isConversationIdExist } from '@/pages/next-chats/utils';
|
import { isConversationIdExist } from '@/pages/next-chats/utils';
|
||||||
import chatService from '@/services/next-chat-service';
|
import chatService from '@/services/next-chat-service';
|
||||||
import api from '@/utils/api';
|
import api from '@/utils/api';
|
||||||
import { buildMessageListWithUuid, getConversationId } from '@/utils/chat';
|
import { buildMessageListWithUuid, generateConversationId } from '@/utils/chat';
|
||||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
import { useDebounce } from 'ahooks';
|
import { useDebounce } from 'ahooks';
|
||||||
import { has } from 'lodash';
|
import { has } from 'lodash';
|
||||||
import { useCallback, useMemo, useRef } from 'react';
|
import { useCallback, useRef } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useParams, useSearchParams } from 'umi';
|
import { useParams, useSearchParams } from 'umi';
|
||||||
import {
|
import {
|
||||||
@ -36,6 +36,7 @@ export const enum ChatApiAction {
|
|||||||
FetchDialog = 'fetchDialog',
|
FetchDialog = 'fetchDialog',
|
||||||
FetchConversationList = 'fetchConversationList',
|
FetchConversationList = 'fetchConversationList',
|
||||||
FetchConversation = 'fetchConversation',
|
FetchConversation = 'fetchConversation',
|
||||||
|
FetchConversationManually = 'fetchConversationManually',
|
||||||
UpdateConversation = 'updateConversation',
|
UpdateConversation = 'updateConversation',
|
||||||
RemoveConversation = 'removeConversation',
|
RemoveConversation = 'removeConversation',
|
||||||
DeleteMessage = 'deleteMessage',
|
DeleteMessage = 'deleteMessage',
|
||||||
@ -59,29 +60,6 @@ export const useGetChatSearchParams = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useClickDialogCard = () => {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
const [_, setSearchParams] = useSearchParams();
|
|
||||||
|
|
||||||
const newQueryParameters: URLSearchParams = useMemo(() => {
|
|
||||||
return new URLSearchParams();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleClickDialog = useCallback(
|
|
||||||
(dialogId: string) => {
|
|
||||||
newQueryParameters.set(ChatSearchParams.DialogId, dialogId);
|
|
||||||
// newQueryParameters.set(
|
|
||||||
// ChatSearchParams.ConversationId,
|
|
||||||
// EmptyConversationId,
|
|
||||||
// );
|
|
||||||
setSearchParams(newQueryParameters);
|
|
||||||
},
|
|
||||||
[newQueryParameters, setSearchParams],
|
|
||||||
);
|
|
||||||
|
|
||||||
return { handleClickDialog };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useFetchDialogList = () => {
|
export const useFetchDialogList = () => {
|
||||||
const { searchString, handleInputChange } = useHandleSearchChange();
|
const { searchString, handleInputChange } = useHandleSearchChange();
|
||||||
const { pagination, setPagination } = useGetPaginationWithRouter();
|
const { pagination, setPagination } = useGetPaginationWithRouter();
|
||||||
@ -222,28 +200,8 @@ export const useFetchDialog = () => {
|
|||||||
|
|
||||||
//#region Conversation
|
//#region Conversation
|
||||||
|
|
||||||
export const useClickConversationCard = () => {
|
|
||||||
const [currentQueryParameters, setSearchParams] = useSearchParams();
|
|
||||||
const newQueryParameters: URLSearchParams = useMemo(
|
|
||||||
() => new URLSearchParams(currentQueryParameters.toString()),
|
|
||||||
[currentQueryParameters],
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleClickConversation = useCallback(
|
|
||||||
(conversationId: string, isNew: string) => {
|
|
||||||
newQueryParameters.set(ChatSearchParams.ConversationId, conversationId);
|
|
||||||
newQueryParameters.set(ChatSearchParams.isNew, isNew);
|
|
||||||
setSearchParams(newQueryParameters);
|
|
||||||
},
|
|
||||||
[setSearchParams, newQueryParameters],
|
|
||||||
);
|
|
||||||
|
|
||||||
return { handleClickConversation };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useFetchConversationList = () => {
|
export const useFetchConversationList = () => {
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
const { handleClickConversation } = useClickConversationCard();
|
|
||||||
|
|
||||||
const { searchString, handleInputChange } = useHandleSearchStrChange();
|
const { searchString, handleInputChange } = useHandleSearchStrChange();
|
||||||
|
|
||||||
@ -267,13 +225,6 @@ export const useFetchConversationList = () => {
|
|||||||
{ params: { dialog_id: id } },
|
{ params: { dialog_id: id } },
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
if (data.code === 0) {
|
|
||||||
if (data.data.length > 0) {
|
|
||||||
handleClickConversation(data.data[0].id, '');
|
|
||||||
} else {
|
|
||||||
handleClickConversation('', '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return data?.data;
|
return data?.data;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -281,45 +232,33 @@ export const useFetchConversationList = () => {
|
|||||||
return { data, loading, refetch, searchString, handleInputChange };
|
return { data, loading, refetch, searchString, handleInputChange };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useFetchConversation = () => {
|
export function useFetchConversationManually() {
|
||||||
const { isNew, conversationId } = useGetChatSearchParams();
|
|
||||||
const { sharedId } = useGetSharedChatSearchParams();
|
|
||||||
const {
|
const {
|
||||||
data,
|
data,
|
||||||
isFetching: loading,
|
isPending: loading,
|
||||||
refetch,
|
mutateAsync,
|
||||||
} = useQuery<IClientConversation>({
|
} = useMutation<IClientConversation, unknown, string>({
|
||||||
queryKey: [ChatApiAction.FetchConversation, conversationId],
|
mutationKey: [ChatApiAction.FetchConversationManually],
|
||||||
initialData: {} as IClientConversation,
|
mutationFn: async (conversationId) => {
|
||||||
// enabled: isConversationIdExist(conversationId),
|
const { data } = await chatService.getConversation(
|
||||||
gcTime: 0,
|
{
|
||||||
refetchOnWindowFocus: false,
|
params: {
|
||||||
queryFn: async () => {
|
conversationId,
|
||||||
if (
|
|
||||||
isNew !== 'true' &&
|
|
||||||
isConversationIdExist(sharedId || conversationId)
|
|
||||||
) {
|
|
||||||
const { data } = await chatService.getConversation(
|
|
||||||
{
|
|
||||||
params: {
|
|
||||||
conversationId: conversationId || sharedId,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
true,
|
},
|
||||||
);
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
const conversation = data?.data ?? {};
|
const conversation = data?.data ?? {};
|
||||||
|
|
||||||
const messageList = buildMessageListWithUuid(conversation?.message);
|
const messageList = buildMessageListWithUuid(conversation?.message);
|
||||||
|
|
||||||
return { ...conversation, message: messageList };
|
return { ...conversation, message: messageList };
|
||||||
}
|
|
||||||
return { message: [] };
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return { data, loading, refetch };
|
return { data, loading, fetchConversationManually: mutateAsync };
|
||||||
};
|
}
|
||||||
|
|
||||||
export const useUpdateConversation = () => {
|
export const useUpdateConversation = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@ -335,7 +274,7 @@ export const useUpdateConversation = () => {
|
|||||||
...params,
|
...params,
|
||||||
conversation_id: params.conversation_id
|
conversation_id: params.conversation_id
|
||||||
? params.conversation_id
|
? params.conversation_id
|
||||||
: getConversationId(),
|
: generateConversationId(),
|
||||||
});
|
});
|
||||||
if (data.code === 0) {
|
if (data.code === 0) {
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
|
|||||||
@ -187,6 +187,7 @@ export interface IExternalChatInfo {
|
|||||||
export interface IMessage extends Message {
|
export interface IMessage extends Message {
|
||||||
id: string;
|
id: string;
|
||||||
reference?: IReference; // the latest news has reference
|
reference?: IReference; // the latest news has reference
|
||||||
|
conversationId?: string; // To distinguish which conversation the message belongs to
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IClientConversation extends IConversation {
|
export interface IClientConversation extends IConversation {
|
||||||
|
|||||||
@ -15,12 +15,12 @@ import {
|
|||||||
import { MessageType } from '@/constants/chat';
|
import { MessageType } from '@/constants/chat';
|
||||||
import { useScrollToBottom } from '@/hooks/logic-hooks';
|
import { useScrollToBottom } from '@/hooks/logic-hooks';
|
||||||
import {
|
import {
|
||||||
useFetchConversation,
|
|
||||||
useFetchDialog,
|
useFetchDialog,
|
||||||
useGetChatSearchParams,
|
useGetChatSearchParams,
|
||||||
useSetDialog,
|
useSetDialog,
|
||||||
} from '@/hooks/use-chat-request';
|
} from '@/hooks/use-chat-request';
|
||||||
import { useFetchUserInfo } from '@/hooks/use-user-setting-request';
|
import { useFetchUserInfo } from '@/hooks/use-user-setting-request';
|
||||||
|
import { IClientConversation, IMessage } from '@/interfaces/database/chat';
|
||||||
import { buildMessageUuidWithRole } from '@/utils/chat';
|
import { buildMessageUuidWithRole } from '@/utils/chat';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { t } from 'i18next';
|
import { t } from 'i18next';
|
||||||
@ -38,13 +38,14 @@ import { useCreateConversationBeforeUploadDocument } from '../../hooks/use-creat
|
|||||||
import { useSendMessage } from '../../hooks/use-send-chat-message';
|
import { useSendMessage } from '../../hooks/use-send-chat-message';
|
||||||
import { useSendMultipleChatMessage } from '../../hooks/use-send-multiple-message';
|
import { useSendMultipleChatMessage } from '../../hooks/use-send-multiple-message';
|
||||||
import { buildMessageItemReference } from '../../utils';
|
import { buildMessageItemReference } from '../../utils';
|
||||||
import { IMessage } from '../interface';
|
|
||||||
import { useAddChatBox } from '../use-add-box';
|
import { useAddChatBox } from '../use-add-box';
|
||||||
|
import { useSetDefaultModel } from './use-set-default-model';
|
||||||
|
|
||||||
type MultipleChatBoxProps = {
|
type MultipleChatBoxProps = {
|
||||||
controller: AbortController;
|
controller: AbortController;
|
||||||
chatBoxIds: string[];
|
chatBoxIds: string[];
|
||||||
stopOutputMessage(): void;
|
stopOutputMessage(): void;
|
||||||
|
conversation: IClientConversation;
|
||||||
} & Pick<
|
} & Pick<
|
||||||
ReturnType<typeof useAddChatBox>,
|
ReturnType<typeof useAddChatBox>,
|
||||||
'removeChatBox' | 'addChatBox' | 'chatBoxIds'
|
'removeChatBox' | 'addChatBox' | 'chatBoxIds'
|
||||||
@ -55,6 +56,7 @@ type ChatCardProps = {
|
|||||||
idx: number;
|
idx: number;
|
||||||
derivedMessages: IMessage[];
|
derivedMessages: IMessage[];
|
||||||
sendLoading: boolean;
|
sendLoading: boolean;
|
||||||
|
conversation: IClientConversation;
|
||||||
} & Pick<
|
} & Pick<
|
||||||
MultipleChatBoxProps,
|
MultipleChatBoxProps,
|
||||||
'controller' | 'removeChatBox' | 'addChatBox' | 'chatBoxIds'
|
'controller' | 'removeChatBox' | 'addChatBox' | 'chatBoxIds'
|
||||||
@ -72,6 +74,7 @@ const ChatCard = forwardRef(function ChatCard(
|
|||||||
derivedMessages,
|
derivedMessages,
|
||||||
sendLoading,
|
sendLoading,
|
||||||
clickDocumentButton,
|
clickDocumentButton,
|
||||||
|
conversation,
|
||||||
}: ChatCardProps,
|
}: ChatCardProps,
|
||||||
ref,
|
ref,
|
||||||
) {
|
) {
|
||||||
@ -97,7 +100,8 @@ const ChatCard = forwardRef(function ChatCard(
|
|||||||
|
|
||||||
const { data: userInfo } = useFetchUserInfo();
|
const { data: userInfo } = useFetchUserInfo();
|
||||||
const { data: currentDialog } = useFetchDialog();
|
const { data: currentDialog } = useFetchDialog();
|
||||||
const { data: conversation } = useFetchConversation();
|
|
||||||
|
useSetDefaultModel(form);
|
||||||
|
|
||||||
const isLatestChat = idx === chatBoxIds.length - 1;
|
const isLatestChat = idx === chatBoxIds.length - 1;
|
||||||
|
|
||||||
@ -202,6 +206,7 @@ export function MultipleChatBox({
|
|||||||
removeChatBox,
|
removeChatBox,
|
||||||
addChatBox,
|
addChatBox,
|
||||||
stopOutputMessage,
|
stopOutputMessage,
|
||||||
|
conversation,
|
||||||
}: MultipleChatBoxProps) {
|
}: MultipleChatBoxProps) {
|
||||||
const {
|
const {
|
||||||
value,
|
value,
|
||||||
@ -237,6 +242,7 @@ export function MultipleChatBox({
|
|||||||
ref={setFormRef(id)}
|
ref={setFormRef(id)}
|
||||||
sendLoading={sendLoading}
|
sendLoading={sendLoading}
|
||||||
clickDocumentButton={clickDocumentButton}
|
clickDocumentButton={clickDocumentButton}
|
||||||
|
conversation={conversation}
|
||||||
></ChatCard>
|
></ChatCard>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -4,12 +4,13 @@ import PdfSheet from '@/components/pdf-drawer';
|
|||||||
import { useClickDrawer } from '@/components/pdf-drawer/hooks';
|
import { useClickDrawer } from '@/components/pdf-drawer/hooks';
|
||||||
import { MessageType } from '@/constants/chat';
|
import { MessageType } from '@/constants/chat';
|
||||||
import {
|
import {
|
||||||
useFetchConversation,
|
|
||||||
useFetchDialog,
|
useFetchDialog,
|
||||||
useGetChatSearchParams,
|
useGetChatSearchParams,
|
||||||
} from '@/hooks/use-chat-request';
|
} from '@/hooks/use-chat-request';
|
||||||
import { useFetchUserInfo } from '@/hooks/use-user-setting-request';
|
import { useFetchUserInfo } from '@/hooks/use-user-setting-request';
|
||||||
|
import { IClientConversation } from '@/interfaces/database/chat';
|
||||||
import { buildMessageUuidWithRole } from '@/utils/chat';
|
import { buildMessageUuidWithRole } from '@/utils/chat';
|
||||||
|
import { useEffect } from 'react';
|
||||||
import {
|
import {
|
||||||
useGetSendButtonDisabled,
|
useGetSendButtonDisabled,
|
||||||
useSendButtonDisabled,
|
useSendButtonDisabled,
|
||||||
@ -21,9 +22,14 @@ import { buildMessageItemReference } from '../../utils';
|
|||||||
interface IProps {
|
interface IProps {
|
||||||
controller: AbortController;
|
controller: AbortController;
|
||||||
stopOutputMessage(): void;
|
stopOutputMessage(): void;
|
||||||
|
conversation: IClientConversation;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SingleChatBox({ controller, stopOutputMessage }: IProps) {
|
export function SingleChatBox({
|
||||||
|
controller,
|
||||||
|
stopOutputMessage,
|
||||||
|
conversation,
|
||||||
|
}: IProps) {
|
||||||
const {
|
const {
|
||||||
value,
|
value,
|
||||||
scrollRef,
|
scrollRef,
|
||||||
@ -37,18 +43,32 @@ export function SingleChatBox({ controller, stopOutputMessage }: IProps) {
|
|||||||
removeMessageById,
|
removeMessageById,
|
||||||
handleUploadFile,
|
handleUploadFile,
|
||||||
removeFile,
|
removeFile,
|
||||||
|
setDerivedMessages,
|
||||||
} = useSendMessage(controller);
|
} = useSendMessage(controller);
|
||||||
const { data: userInfo } = useFetchUserInfo();
|
const { data: userInfo } = useFetchUserInfo();
|
||||||
const { data: currentDialog } = useFetchDialog();
|
const { data: currentDialog } = useFetchDialog();
|
||||||
const { createConversationBeforeUploadDocument } =
|
const { createConversationBeforeUploadDocument } =
|
||||||
useCreateConversationBeforeUploadDocument();
|
useCreateConversationBeforeUploadDocument();
|
||||||
const { conversationId } = useGetChatSearchParams();
|
const { conversationId } = useGetChatSearchParams();
|
||||||
const { data: conversation } = useFetchConversation();
|
|
||||||
const disabled = useGetSendButtonDisabled();
|
const disabled = useGetSendButtonDisabled();
|
||||||
const sendDisabled = useSendButtonDisabled(value);
|
const sendDisabled = useSendButtonDisabled(value);
|
||||||
const { visible, hideModal, documentId, selectedChunk, clickDocumentButton } =
|
const { visible, hideModal, documentId, selectedChunk, clickDocumentButton } =
|
||||||
useClickDrawer();
|
useClickDrawer();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const messages = conversation?.message;
|
||||||
|
if (Array.isArray(messages)) {
|
||||||
|
setDerivedMessages(messages);
|
||||||
|
}
|
||||||
|
}, [conversation?.message, setDerivedMessages]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Clear the message list after deleting the conversation.
|
||||||
|
if (conversationId === '') {
|
||||||
|
setDerivedMessages([]);
|
||||||
|
}
|
||||||
|
}, [conversationId, setDerivedMessages]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="flex flex-col p-5 h-full">
|
<section className="flex flex-col p-5 h-full">
|
||||||
<div ref={messageContainerRef} className="flex-1 overflow-auto min-h-0">
|
<div ref={messageContainerRef} className="flex-1 overflow-auto min-h-0">
|
||||||
|
|||||||
@ -0,0 +1,18 @@
|
|||||||
|
import { LlmModelType } from '@/constants/knowledge';
|
||||||
|
import { useComposeLlmOptionsByModelTypes } from '@/hooks/use-llm-request';
|
||||||
|
import { useMount } from 'ahooks';
|
||||||
|
import { UseFormReturn } from 'react-hook-form';
|
||||||
|
|
||||||
|
export function useSetDefaultModel(form: UseFormReturn<any>) {
|
||||||
|
const modelOptions = useComposeLlmOptionsByModelTypes([
|
||||||
|
LlmModelType.Chat,
|
||||||
|
LlmModelType.Image2text,
|
||||||
|
]);
|
||||||
|
|
||||||
|
useMount(() => {
|
||||||
|
const firstModel = modelOptions.at(0)?.options.at(0)?.value;
|
||||||
|
if (firstModel) {
|
||||||
|
form.setValue('llm_id', firstModel);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -5,11 +5,15 @@ import {
|
|||||||
DropdownMenuItem,
|
DropdownMenuItem,
|
||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from '@/components/ui/dropdown-menu';
|
} from '@/components/ui/dropdown-menu';
|
||||||
import { useRemoveConversation } from '@/hooks/use-chat-request';
|
import {
|
||||||
|
useGetChatSearchParams,
|
||||||
|
useRemoveConversation,
|
||||||
|
} from '@/hooks/use-chat-request';
|
||||||
import { IConversation } from '@/interfaces/database/chat';
|
import { IConversation } from '@/interfaces/database/chat';
|
||||||
import { Trash2 } from 'lucide-react';
|
import { Trash2 } from 'lucide-react';
|
||||||
import { MouseEventHandler, PropsWithChildren, useCallback } from 'react';
|
import { MouseEventHandler, PropsWithChildren, useCallback } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { useChatUrlParams } from '../hooks/use-chat-url';
|
||||||
|
|
||||||
export function ConversationDropdown({
|
export function ConversationDropdown({
|
||||||
children,
|
children,
|
||||||
@ -20,22 +24,27 @@ export function ConversationDropdown({
|
|||||||
removeTemporaryConversation?: (conversationId: string) => void;
|
removeTemporaryConversation?: (conversationId: string) => void;
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const { setConversationBoth } = useChatUrlParams();
|
||||||
const { removeConversation } = useRemoveConversation();
|
const { removeConversation } = useRemoveConversation();
|
||||||
|
const { isNew } = useGetChatSearchParams();
|
||||||
|
|
||||||
const handleDelete: MouseEventHandler<HTMLDivElement> = useCallback(() => {
|
const handleDelete: MouseEventHandler<HTMLDivElement> =
|
||||||
if (conversation.is_new && removeTemporaryConversation) {
|
useCallback(async () => {
|
||||||
removeTemporaryConversation(conversation.id);
|
if (isNew === 'true' && removeTemporaryConversation) {
|
||||||
removeConversation([]);
|
removeTemporaryConversation(conversation.id);
|
||||||
} else {
|
} else {
|
||||||
removeConversation([conversation.id]);
|
const code = await removeConversation([conversation.id]);
|
||||||
}
|
if (code === 0) {
|
||||||
}, [
|
setConversationBoth('', '');
|
||||||
conversation.id,
|
}
|
||||||
conversation.is_new,
|
}
|
||||||
removeConversation,
|
}, [
|
||||||
removeTemporaryConversation,
|
conversation.id,
|
||||||
]);
|
isNew,
|
||||||
|
removeConversation,
|
||||||
|
removeTemporaryConversation,
|
||||||
|
setConversationBoth,
|
||||||
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
|
|||||||
@ -15,13 +15,17 @@ import { SharedFrom } from '@/constants/chat';
|
|||||||
import { useSetModalState } from '@/hooks/common-hooks';
|
import { useSetModalState } from '@/hooks/common-hooks';
|
||||||
import { useNavigatePage } from '@/hooks/logic-hooks/navigate-hooks';
|
import { useNavigatePage } from '@/hooks/logic-hooks/navigate-hooks';
|
||||||
import {
|
import {
|
||||||
useFetchConversation,
|
useFetchConversationList,
|
||||||
|
useFetchConversationManually,
|
||||||
useFetchDialog,
|
useFetchDialog,
|
||||||
useGetChatSearchParams,
|
useGetChatSearchParams,
|
||||||
} from '@/hooks/use-chat-request';
|
} from '@/hooks/use-chat-request';
|
||||||
|
import { IClientConversation } from '@/interfaces/database/chat';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
|
import { useMount } from 'ahooks';
|
||||||
import { isEmpty } from 'lodash';
|
import { isEmpty } from 'lodash';
|
||||||
import { ArrowUpRight, LogOut, Send } from 'lucide-react';
|
import { ArrowUpRight, LogOut, Send } from 'lucide-react';
|
||||||
|
import { useCallback, useMemo, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useParams } from 'umi';
|
import { useParams } from 'umi';
|
||||||
import { useHandleClickConversationCard } from '../hooks/use-click-card';
|
import { useHandleClickConversationCard } from '../hooks/use-click-card';
|
||||||
@ -37,26 +41,54 @@ export default function Chat() {
|
|||||||
const { navigateToChatList } = useNavigatePage();
|
const { navigateToChatList } = useNavigatePage();
|
||||||
const { data } = useFetchDialog();
|
const { data } = useFetchDialog();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { data: conversation } = useFetchConversation();
|
const [currentConversation, setCurrentConversation] =
|
||||||
|
useState<IClientConversation>({} as IClientConversation);
|
||||||
|
|
||||||
|
const { fetchConversationManually } = useFetchConversationManually();
|
||||||
|
|
||||||
const { handleConversationCardClick, controller, stopOutputMessage } =
|
const { handleConversationCardClick, controller, stopOutputMessage } =
|
||||||
useHandleClickConversationCard();
|
useHandleClickConversationCard();
|
||||||
const { visible: settingVisible, switchVisible: switchSettingVisible } =
|
const { visible: settingVisible, switchVisible: switchSettingVisible } =
|
||||||
useSetModalState(true);
|
useSetModalState(true);
|
||||||
const {
|
|
||||||
removeChatBox,
|
const { isDebugMode, switchDebugMode } = useSwitchDebugMode();
|
||||||
addChatBox,
|
const { removeChatBox, addChatBox, chatBoxIds, hasSingleChatBox } =
|
||||||
chatBoxIds,
|
useAddChatBox(isDebugMode);
|
||||||
hasSingleChatBox,
|
|
||||||
hasThreeChatBox,
|
|
||||||
} = useAddChatBox();
|
|
||||||
|
|
||||||
const { showEmbedModal, hideEmbedModal, embedVisible, beta } =
|
const { showEmbedModal, hideEmbedModal, embedVisible, beta } =
|
||||||
useShowEmbedModal();
|
useShowEmbedModal();
|
||||||
|
|
||||||
const { conversationId, isNew } = useGetChatSearchParams();
|
const { conversationId, isNew } = useGetChatSearchParams();
|
||||||
|
|
||||||
const { isDebugMode, switchDebugMode } = useSwitchDebugMode();
|
const { data: dialogList } = useFetchConversationList();
|
||||||
|
|
||||||
|
const currentConversationName = useMemo(() => {
|
||||||
|
return dialogList.find((x) => x.id === conversationId)?.name;
|
||||||
|
}, [conversationId, dialogList]);
|
||||||
|
|
||||||
|
const fetchConversation: typeof handleConversationCardClick = useCallback(
|
||||||
|
async (conversationId, isNew) => {
|
||||||
|
if (conversationId && !isNew) {
|
||||||
|
const conversation = await fetchConversationManually(conversationId);
|
||||||
|
if (!isEmpty(conversation)) {
|
||||||
|
setCurrentConversation(conversation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[fetchConversationManually],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSessionClick: typeof handleConversationCardClick = useCallback(
|
||||||
|
(conversationId, isNew) => {
|
||||||
|
handleConversationCardClick(conversationId, isNew);
|
||||||
|
fetchConversation(conversationId, isNew);
|
||||||
|
},
|
||||||
|
[fetchConversation, handleConversationCardClick],
|
||||||
|
);
|
||||||
|
|
||||||
|
useMount(() => {
|
||||||
|
fetchConversation(conversationId, isNew === 'true');
|
||||||
|
});
|
||||||
|
|
||||||
if (isDebugMode) {
|
if (isDebugMode) {
|
||||||
return (
|
return (
|
||||||
@ -75,6 +107,7 @@ export default function Chat() {
|
|||||||
removeChatBox={removeChatBox}
|
removeChatBox={removeChatBox}
|
||||||
addChatBox={addChatBox}
|
addChatBox={addChatBox}
|
||||||
stopOutputMessage={stopOutputMessage}
|
stopOutputMessage={stopOutputMessage}
|
||||||
|
conversation={currentConversation}
|
||||||
></MultipleChatBox>
|
></MultipleChatBox>
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
@ -104,7 +137,7 @@ export default function Chat() {
|
|||||||
<div className="flex flex-1 min-h-0 pb-9">
|
<div className="flex flex-1 min-h-0 pb-9">
|
||||||
<Sessions
|
<Sessions
|
||||||
hasSingleChatBox={hasSingleChatBox}
|
hasSingleChatBox={hasSingleChatBox}
|
||||||
handleConversationCardClick={handleConversationCardClick}
|
handleConversationCardClick={handleSessionClick}
|
||||||
switchSettingVisible={switchSettingVisible}
|
switchSettingVisible={switchSettingVisible}
|
||||||
></Sessions>
|
></Sessions>
|
||||||
|
|
||||||
@ -115,16 +148,8 @@ export default function Chat() {
|
|||||||
className={cn('p-5', { 'border-b': hasSingleChatBox })}
|
className={cn('p-5', { 'border-b': hasSingleChatBox })}
|
||||||
>
|
>
|
||||||
<CardTitle className="flex justify-between items-center text-base">
|
<CardTitle className="flex justify-between items-center text-base">
|
||||||
<div className="truncate">{conversation.name}</div>
|
<div className="truncate">{currentConversationName}</div>
|
||||||
<Button
|
<Button variant={'ghost'} onClick={switchDebugMode}>
|
||||||
variant={'ghost'}
|
|
||||||
onClick={switchDebugMode}
|
|
||||||
disabled={
|
|
||||||
hasThreeChatBox ||
|
|
||||||
isEmpty(conversationId) ||
|
|
||||||
isNew === 'true'
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<ArrowUpRight /> {t('chat.multipleModels')}
|
<ArrowUpRight /> {t('chat.multipleModels')}
|
||||||
</Button>
|
</Button>
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
@ -133,6 +158,7 @@ export default function Chat() {
|
|||||||
<SingleChatBox
|
<SingleChatBox
|
||||||
controller={controller}
|
controller={controller}
|
||||||
stopOutputMessage={stopOutputMessage}
|
stopOutputMessage={stopOutputMessage}
|
||||||
|
conversation={currentConversation}
|
||||||
></SingleChatBox>
|
></SingleChatBox>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { useCallback, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
|
|
||||||
export function useAddChatBox() {
|
export function useAddChatBox(isDebugMode: boolean) {
|
||||||
const [ids, setIds] = useState<string[]>([uuid()]);
|
const [ids, setIds] = useState<string[]>([uuid()]);
|
||||||
|
|
||||||
const hasSingleChatBox = ids.length === 1;
|
const hasSingleChatBox = ids.length === 1;
|
||||||
@ -16,6 +16,12 @@ export function useAddChatBox() {
|
|||||||
setIds((prev) => prev.filter((x) => x !== id));
|
setIds((prev) => prev.filter((x) => x !== id));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isDebugMode) {
|
||||||
|
setIds((pre) => pre.slice(0, 1));
|
||||||
|
}
|
||||||
|
}, [isDebugMode]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
chatBoxIds: ids,
|
chatBoxIds: ids,
|
||||||
hasSingleChatBox,
|
hasSingleChatBox,
|
||||||
|
|||||||
@ -1,12 +1,10 @@
|
|||||||
import { useGetChatSearchParams } from '@/hooks/use-chat-request';
|
|
||||||
import { trim } from 'lodash';
|
import { trim } from 'lodash';
|
||||||
import { useParams } from 'umi';
|
import { useParams } from 'umi';
|
||||||
|
|
||||||
export const useGetSendButtonDisabled = () => {
|
export const useGetSendButtonDisabled = () => {
|
||||||
const { conversationId } = useGetChatSearchParams();
|
|
||||||
const { id: dialogId } = useParams();
|
const { id: dialogId } = useParams();
|
||||||
|
|
||||||
return dialogId === '' || conversationId === '';
|
return dialogId === '';
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useSendButtonDisabled = (value: string) => {
|
export const useSendButtonDisabled = (value: string) => {
|
||||||
|
|||||||
97
web/src/pages/next-chats/hooks/use-chat-url.ts
Normal file
97
web/src/pages/next-chats/hooks/use-chat-url.ts
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
import { ChatSearchParams } from '@/constants/chat';
|
||||||
|
import { useGetChatSearchParams } from '@/hooks/use-chat-request';
|
||||||
|
import { IMessage } from '@/interfaces/database/chat';
|
||||||
|
import { generateConversationId } from '@/utils/chat';
|
||||||
|
import { useCallback, useMemo } from 'react';
|
||||||
|
import { useSearchParams } from 'umi';
|
||||||
|
import { useSetConversation } from './use-set-conversation';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Consolidated hook for managing chat URL parameters (conversationId and isNew)
|
||||||
|
* Replaces: useClickConversationCard from use-chat-request.ts and useSetChatRouteParams from use-set-chat-route.ts
|
||||||
|
*/
|
||||||
|
export const useChatUrlParams = () => {
|
||||||
|
const [currentQueryParameters, setSearchParams] = useSearchParams();
|
||||||
|
const newQueryParameters: URLSearchParams = useMemo(
|
||||||
|
() => new URLSearchParams(currentQueryParameters.toString()),
|
||||||
|
[currentQueryParameters],
|
||||||
|
);
|
||||||
|
|
||||||
|
const setConversationId = useCallback(
|
||||||
|
(conversationId: string) => {
|
||||||
|
newQueryParameters.set(ChatSearchParams.ConversationId, conversationId);
|
||||||
|
setSearchParams(newQueryParameters);
|
||||||
|
},
|
||||||
|
[setSearchParams, newQueryParameters],
|
||||||
|
);
|
||||||
|
|
||||||
|
const setIsNew = useCallback(
|
||||||
|
(isNew: string) => {
|
||||||
|
newQueryParameters.set(ChatSearchParams.isNew, isNew);
|
||||||
|
setSearchParams(newQueryParameters);
|
||||||
|
},
|
||||||
|
[setSearchParams, newQueryParameters],
|
||||||
|
);
|
||||||
|
|
||||||
|
const getIsNew = useCallback(() => {
|
||||||
|
return newQueryParameters.get(ChatSearchParams.isNew);
|
||||||
|
}, [newQueryParameters]);
|
||||||
|
|
||||||
|
const setConversationBoth = useCallback(
|
||||||
|
(conversationId: string, isNew: string) => {
|
||||||
|
newQueryParameters.set(ChatSearchParams.ConversationId, conversationId);
|
||||||
|
newQueryParameters.set(ChatSearchParams.isNew, isNew);
|
||||||
|
setSearchParams(newQueryParameters);
|
||||||
|
},
|
||||||
|
[setSearchParams, newQueryParameters],
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
setConversationId,
|
||||||
|
setIsNew,
|
||||||
|
getIsNew,
|
||||||
|
setConversationBoth,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export function useCreateConversationBeforeSendMessage() {
|
||||||
|
const { conversationId, isNew } = useGetChatSearchParams();
|
||||||
|
const { setConversation } = useSetConversation();
|
||||||
|
const { setIsNew, setConversationBoth } = useChatUrlParams();
|
||||||
|
|
||||||
|
// Create conversation if it doesn't exist
|
||||||
|
const createConversationBeforeSendMessage = useCallback(
|
||||||
|
async (value: string) => {
|
||||||
|
let currentMessages: Array<IMessage> = [];
|
||||||
|
const currentConversationId = generateConversationId();
|
||||||
|
if (conversationId === '' || isNew === 'true') {
|
||||||
|
if (conversationId === '') {
|
||||||
|
setConversationBoth(currentConversationId, 'true');
|
||||||
|
}
|
||||||
|
const data = await setConversation(
|
||||||
|
value,
|
||||||
|
true,
|
||||||
|
conversationId || currentConversationId,
|
||||||
|
);
|
||||||
|
if (data.code !== 0) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
setIsNew('');
|
||||||
|
currentMessages = data.data.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetConversationId = conversationId || currentConversationId;
|
||||||
|
|
||||||
|
return {
|
||||||
|
targetConversationId,
|
||||||
|
currentMessages,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
[conversationId, isNew, setConversation, setConversationBoth, setIsNew],
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
createConversationBeforeSendMessage,
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,9 +1,9 @@
|
|||||||
import { useClickConversationCard } from '@/hooks/use-chat-request';
|
|
||||||
import { useCallback, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
|
import { useChatUrlParams } from './use-chat-url';
|
||||||
|
|
||||||
export function useHandleClickConversationCard() {
|
export function useHandleClickConversationCard() {
|
||||||
const [controller, setController] = useState(new AbortController());
|
const [controller, setController] = useState(new AbortController());
|
||||||
const { handleClickConversation } = useClickConversationCard();
|
const { setConversationBoth } = useChatUrlParams();
|
||||||
|
|
||||||
const stopOutputMessage = useCallback(() => {
|
const stopOutputMessage = useCallback(() => {
|
||||||
setController((pre) => {
|
setController((pre) => {
|
||||||
@ -14,10 +14,10 @@ export function useHandleClickConversationCard() {
|
|||||||
|
|
||||||
const handleConversationCardClick = useCallback(
|
const handleConversationCardClick = useCallback(
|
||||||
(conversationId: string, isNew: boolean) => {
|
(conversationId: string, isNew: boolean) => {
|
||||||
handleClickConversation(conversationId, isNew ? 'true' : '');
|
setConversationBoth(conversationId, isNew ? 'true' : '');
|
||||||
stopOutputMessage();
|
stopOutputMessage();
|
||||||
},
|
},
|
||||||
[handleClickConversation, stopOutputMessage],
|
[setConversationBoth, stopOutputMessage],
|
||||||
);
|
);
|
||||||
|
|
||||||
return { controller, handleConversationCardClick, stopOutputMessage };
|
return { controller, handleConversationCardClick, stopOutputMessage };
|
||||||
|
|||||||
@ -1,23 +1,23 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { useParams } from 'umi';
|
import { useParams } from 'umi';
|
||||||
import { useSetChatRouteParams } from './use-set-chat-route';
|
import { useChatUrlParams } from './use-chat-url';
|
||||||
import { useSetConversation } from './use-set-conversation';
|
import { useSetConversation } from './use-set-conversation';
|
||||||
|
|
||||||
export const useCreateConversationBeforeUploadDocument = () => {
|
export const useCreateConversationBeforeUploadDocument = () => {
|
||||||
const { setConversation } = useSetConversation();
|
const { setConversation } = useSetConversation();
|
||||||
const { id: dialogId } = useParams();
|
const { id: dialogId } = useParams();
|
||||||
const { getConversationIsNew } = useSetChatRouteParams();
|
const { getIsNew } = useChatUrlParams();
|
||||||
|
|
||||||
const createConversationBeforeUploadDocument = useCallback(
|
const createConversationBeforeUploadDocument = useCallback(
|
||||||
async (message: string) => {
|
async (message: string) => {
|
||||||
const isNew = getConversationIsNew();
|
const isNew = getIsNew();
|
||||||
if (isNew === 'true') {
|
if (isNew === 'true') {
|
||||||
const data = await setConversation(message, true);
|
const data = await setConversation(message, true);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[setConversation, getConversationIsNew],
|
[setConversation, getIsNew],
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -1,13 +1,14 @@
|
|||||||
import { ChatSearchParams, MessageType } from '@/constants/chat';
|
import { MessageType } from '@/constants/chat';
|
||||||
import { useTranslate } from '@/hooks/common-hooks';
|
import { useTranslate } from '@/hooks/common-hooks';
|
||||||
import {
|
import {
|
||||||
useFetchConversationList,
|
useFetchConversationList,
|
||||||
useFetchDialogList,
|
useFetchDialogList,
|
||||||
} from '@/hooks/use-chat-request';
|
} from '@/hooks/use-chat-request';
|
||||||
import { IConversation } from '@/interfaces/database/chat';
|
import { IConversation } from '@/interfaces/database/chat';
|
||||||
import { getConversationId } from '@/utils/chat';
|
import { generateConversationId } from '@/utils/chat';
|
||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import { useParams, useSearchParams } from 'umi';
|
import { useParams } from 'umi';
|
||||||
|
import { useChatUrlParams } from './use-chat-url';
|
||||||
|
|
||||||
export const useFindPrologueFromDialogList = () => {
|
export const useFindPrologueFromDialogList = () => {
|
||||||
const { id: dialogId } = useParams();
|
const { id: dialogId } = useParams();
|
||||||
@ -20,25 +21,6 @@ export const useFindPrologueFromDialogList = () => {
|
|||||||
return prologue;
|
return prologue;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useSetNewConversationRouteParams = () => {
|
|
||||||
const [currentQueryParameters, setSearchParams] = useSearchParams();
|
|
||||||
const newQueryParameters: URLSearchParams = useMemo(
|
|
||||||
() => new URLSearchParams(currentQueryParameters.toString()),
|
|
||||||
[currentQueryParameters],
|
|
||||||
);
|
|
||||||
|
|
||||||
const setNewConversationRouteParams = useCallback(
|
|
||||||
(conversationId: string, isNew: string) => {
|
|
||||||
newQueryParameters.set(ChatSearchParams.ConversationId, conversationId);
|
|
||||||
newQueryParameters.set(ChatSearchParams.isNew, isNew);
|
|
||||||
setSearchParams(newQueryParameters);
|
|
||||||
},
|
|
||||||
[newQueryParameters, setSearchParams],
|
|
||||||
);
|
|
||||||
|
|
||||||
return { setNewConversationRouteParams };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useSelectDerivedConversationList = () => {
|
export const useSelectDerivedConversationList = () => {
|
||||||
const { t } = useTranslate('chat');
|
const { t } = useTranslate('chat');
|
||||||
|
|
||||||
@ -49,15 +31,16 @@ export const useSelectDerivedConversationList = () => {
|
|||||||
handleInputChange,
|
handleInputChange,
|
||||||
searchString,
|
searchString,
|
||||||
} = useFetchConversationList();
|
} = useFetchConversationList();
|
||||||
|
|
||||||
const { id: dialogId } = useParams();
|
const { id: dialogId } = useParams();
|
||||||
const { setNewConversationRouteParams } = useSetNewConversationRouteParams();
|
|
||||||
const prologue = useFindPrologueFromDialogList();
|
const prologue = useFindPrologueFromDialogList();
|
||||||
|
const { setConversationBoth } = useChatUrlParams();
|
||||||
|
|
||||||
const addTemporaryConversation = useCallback(() => {
|
const addTemporaryConversation = useCallback(() => {
|
||||||
const conversationId = getConversationId();
|
const conversationId = generateConversationId();
|
||||||
setList((pre) => {
|
setList((pre) => {
|
||||||
if (dialogId) {
|
if (dialogId) {
|
||||||
setNewConversationRouteParams(conversationId, 'true');
|
setConversationBoth(conversationId, 'true');
|
||||||
const nextList = [
|
const nextList = [
|
||||||
{
|
{
|
||||||
id: conversationId,
|
id: conversationId,
|
||||||
@ -78,7 +61,7 @@ export const useSelectDerivedConversationList = () => {
|
|||||||
|
|
||||||
return pre;
|
return pre;
|
||||||
});
|
});
|
||||||
}, [conversationList, dialogId, prologue, t, setNewConversationRouteParams]);
|
}, [dialogId, setConversationBoth, t, prologue, conversationList]);
|
||||||
|
|
||||||
const removeTemporaryConversation = useCallback((conversationId: string) => {
|
const removeTemporaryConversation = useCallback((conversationId: string) => {
|
||||||
setList((prevList) => {
|
setList((prevList) => {
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import { FileUploadProps } from '@/components/file-upload';
|
|
||||||
import { MessageType } from '@/constants/chat';
|
import { MessageType } from '@/constants/chat';
|
||||||
import {
|
import {
|
||||||
useHandleMessageInputChange,
|
useHandleMessageInputChange,
|
||||||
@ -6,19 +5,15 @@ import {
|
|||||||
useSelectDerivedMessages,
|
useSelectDerivedMessages,
|
||||||
useSendMessageWithSse,
|
useSendMessageWithSse,
|
||||||
} from '@/hooks/logic-hooks';
|
} from '@/hooks/logic-hooks';
|
||||||
import {
|
import { useGetChatSearchParams } from '@/hooks/use-chat-request';
|
||||||
useFetchConversation,
|
import { IMessage } from '@/interfaces/database/chat';
|
||||||
useGetChatSearchParams,
|
|
||||||
} from '@/hooks/use-chat-request';
|
|
||||||
import { IMessage, Message } from '@/interfaces/database/chat';
|
|
||||||
import api from '@/utils/api';
|
import api from '@/utils/api';
|
||||||
import { trim } from 'lodash';
|
import { trim } from 'lodash';
|
||||||
import { useCallback, useEffect } from 'react';
|
import { useCallback, useEffect } from 'react';
|
||||||
import { useParams } from 'umi';
|
import { useParams } from 'umi';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
|
import { useCreateConversationBeforeSendMessage } from './use-chat-url';
|
||||||
import { useFindPrologueFromDialogList } from './use-select-conversation-list';
|
import { useFindPrologueFromDialogList } from './use-select-conversation-list';
|
||||||
import { useSetChatRouteParams } from './use-set-chat-route';
|
|
||||||
import { useSetConversation } from './use-set-conversation';
|
|
||||||
import { useUploadFile } from './use-upload-file';
|
import { useUploadFile } from './use-upload-file';
|
||||||
|
|
||||||
export const useSelectNextMessages = () => {
|
export const useSelectNextMessages = () => {
|
||||||
@ -33,8 +28,7 @@ export const useSelectNextMessages = () => {
|
|||||||
removeMessageById,
|
removeMessageById,
|
||||||
removeMessagesAfterCurrentMessage,
|
removeMessagesAfterCurrentMessage,
|
||||||
} = useSelectDerivedMessages();
|
} = useSelectDerivedMessages();
|
||||||
const { data: conversation, loading } = useFetchConversation();
|
const { isNew, conversationId } = useGetChatSearchParams();
|
||||||
const { conversationId, isNew } = useGetChatSearchParams();
|
|
||||||
const { id: dialogId } = useParams();
|
const { id: dialogId } = useParams();
|
||||||
const prologue = useFindPrologueFromDialogList();
|
const prologue = useFindPrologueFromDialogList();
|
||||||
|
|
||||||
@ -44,45 +38,31 @@ export const useSelectNextMessages = () => {
|
|||||||
role: MessageType.Assistant,
|
role: MessageType.Assistant,
|
||||||
content: prologue,
|
content: prologue,
|
||||||
id: uuid(),
|
id: uuid(),
|
||||||
|
conversationId: conversationId,
|
||||||
} as IMessage;
|
} as IMessage;
|
||||||
|
|
||||||
setDerivedMessages([nextMessage]);
|
setDerivedMessages([nextMessage]);
|
||||||
}
|
}
|
||||||
}, [dialogId, isNew, prologue, setDerivedMessages]);
|
}, [conversationId, dialogId, isNew, prologue, setDerivedMessages]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
addPrologue();
|
addPrologue();
|
||||||
}, [addPrologue]);
|
}, [addPrologue]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (
|
|
||||||
conversationId &&
|
|
||||||
isNew !== 'true' &&
|
|
||||||
conversation.message?.length > 0
|
|
||||||
) {
|
|
||||||
setDerivedMessages(conversation.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!conversationId) {
|
|
||||||
setDerivedMessages([]);
|
|
||||||
}
|
|
||||||
}, [conversation.message, conversationId, setDerivedMessages, isNew]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
scrollRef,
|
scrollRef,
|
||||||
messageContainerRef,
|
messageContainerRef,
|
||||||
derivedMessages,
|
derivedMessages,
|
||||||
loading,
|
|
||||||
addNewestAnswer,
|
addNewestAnswer,
|
||||||
addNewestQuestion,
|
addNewestQuestion,
|
||||||
removeLatestMessage,
|
removeLatestMessage,
|
||||||
removeMessageById,
|
removeMessageById,
|
||||||
removeMessagesAfterCurrentMessage,
|
removeMessagesAfterCurrentMessage,
|
||||||
|
setDerivedMessages,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useSendMessage = (controller: AbortController) => {
|
export const useSendMessage = (controller: AbortController) => {
|
||||||
const { setConversation } = useSetConversation();
|
|
||||||
const { conversationId, isNew } = useGetChatSearchParams();
|
const { conversationId, isNew } = useGetChatSearchParams();
|
||||||
const { handleInputChange, value, setValue } = useHandleMessageInputChange();
|
const { handleInputChange, value, setValue } = useHandleMessageInputChange();
|
||||||
|
|
||||||
@ -96,31 +76,13 @@ export const useSendMessage = (controller: AbortController) => {
|
|||||||
scrollRef,
|
scrollRef,
|
||||||
messageContainerRef,
|
messageContainerRef,
|
||||||
derivedMessages,
|
derivedMessages,
|
||||||
loading,
|
|
||||||
addNewestAnswer,
|
addNewestAnswer,
|
||||||
addNewestQuestion,
|
addNewestQuestion,
|
||||||
removeLatestMessage,
|
removeLatestMessage,
|
||||||
removeMessageById,
|
removeMessageById,
|
||||||
removeMessagesAfterCurrentMessage,
|
removeMessagesAfterCurrentMessage,
|
||||||
|
setDerivedMessages,
|
||||||
} = useSelectNextMessages();
|
} = useSelectNextMessages();
|
||||||
const { setConversationIsNew, getConversationIsNew } =
|
|
||||||
useSetChatRouteParams();
|
|
||||||
|
|
||||||
const onUploadFile: NonNullable<FileUploadProps['onUpload']> = useCallback(
|
|
||||||
async (files, options) => {
|
|
||||||
const isNew = getConversationIsNew();
|
|
||||||
|
|
||||||
if (isNew === 'true' && Array.isArray(files) && files.length) {
|
|
||||||
const data = await setConversation(files[0].name, true);
|
|
||||||
if (data.code === 0) {
|
|
||||||
handleUploadFile(files, options, data.data?.id);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
handleUploadFile(files, options);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[getConversationIsNew, handleUploadFile, setConversation],
|
|
||||||
);
|
|
||||||
|
|
||||||
const sendMessage = useCallback(
|
const sendMessage = useCallback(
|
||||||
async ({
|
async ({
|
||||||
@ -128,14 +90,19 @@ export const useSendMessage = (controller: AbortController) => {
|
|||||||
currentConversationId,
|
currentConversationId,
|
||||||
messages,
|
messages,
|
||||||
}: {
|
}: {
|
||||||
message: Message;
|
message: IMessage;
|
||||||
currentConversationId?: string;
|
currentConversationId?: string;
|
||||||
messages?: Message[];
|
messages?: IMessage[];
|
||||||
}) => {
|
}) => {
|
||||||
const res = await send(
|
const res = await send(
|
||||||
{
|
{
|
||||||
conversation_id: currentConversationId ?? conversationId,
|
conversation_id: currentConversationId ?? conversationId,
|
||||||
messages: [...(messages ?? derivedMessages ?? []), message],
|
messages: [
|
||||||
|
...(Array.isArray(messages) && messages?.length > 0
|
||||||
|
? messages
|
||||||
|
: derivedMessages ?? []),
|
||||||
|
message,
|
||||||
|
],
|
||||||
},
|
},
|
||||||
controller,
|
controller,
|
||||||
);
|
);
|
||||||
@ -157,44 +124,62 @@ export const useSendMessage = (controller: AbortController) => {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleSendMessage = useCallback(
|
|
||||||
async (message: Message) => {
|
|
||||||
const isNew = getConversationIsNew();
|
|
||||||
if (isNew !== 'true') {
|
|
||||||
sendMessage({ message });
|
|
||||||
} else {
|
|
||||||
const data = await setConversation(
|
|
||||||
message.content,
|
|
||||||
true,
|
|
||||||
conversationId,
|
|
||||||
);
|
|
||||||
if (data.code === 0) {
|
|
||||||
setConversationIsNew('');
|
|
||||||
const id = data.data.id;
|
|
||||||
// currentConversationIdRef.current = id;
|
|
||||||
sendMessage({
|
|
||||||
message,
|
|
||||||
currentConversationId: id,
|
|
||||||
messages: data.data.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[
|
|
||||||
setConversation,
|
|
||||||
sendMessage,
|
|
||||||
setConversationIsNew,
|
|
||||||
getConversationIsNew,
|
|
||||||
conversationId,
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
const { regenerateMessage } = useRegenerateMessage({
|
const { regenerateMessage } = useRegenerateMessage({
|
||||||
removeMessagesAfterCurrentMessage,
|
removeMessagesAfterCurrentMessage,
|
||||||
sendMessage,
|
sendMessage,
|
||||||
messages: derivedMessages,
|
messages: derivedMessages,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { createConversationBeforeSendMessage } =
|
||||||
|
useCreateConversationBeforeSendMessage();
|
||||||
|
|
||||||
|
const handlePressEnter = useCallback(async () => {
|
||||||
|
if (trim(value) === '') return;
|
||||||
|
|
||||||
|
const data = await createConversationBeforeSendMessage(value);
|
||||||
|
|
||||||
|
if (data === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { targetConversationId, currentMessages } = data;
|
||||||
|
|
||||||
|
const id = uuid();
|
||||||
|
|
||||||
|
addNewestQuestion({
|
||||||
|
content: value,
|
||||||
|
files: files,
|
||||||
|
id,
|
||||||
|
role: MessageType.User,
|
||||||
|
conversationId: targetConversationId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
setValue('');
|
||||||
|
sendMessage({
|
||||||
|
currentConversationId: targetConversationId,
|
||||||
|
messages: currentMessages,
|
||||||
|
message: {
|
||||||
|
id,
|
||||||
|
content: value.trim(),
|
||||||
|
role: MessageType.User,
|
||||||
|
files: files,
|
||||||
|
conversationId: targetConversationId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
clearFiles();
|
||||||
|
}, [
|
||||||
|
value,
|
||||||
|
createConversationBeforeSendMessage,
|
||||||
|
addNewestQuestion,
|
||||||
|
files,
|
||||||
|
done,
|
||||||
|
clearFiles,
|
||||||
|
setValue,
|
||||||
|
sendMessage,
|
||||||
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// #1289
|
// #1289
|
||||||
if (answer.answer && conversationId && isNew !== 'true') {
|
if (answer.answer && conversationId && isNew !== 'true') {
|
||||||
@ -202,36 +187,6 @@ export const useSendMessage = (controller: AbortController) => {
|
|||||||
}
|
}
|
||||||
}, [answer, addNewestAnswer, conversationId, isNew]);
|
}, [answer, addNewestAnswer, conversationId, isNew]);
|
||||||
|
|
||||||
const handlePressEnter = useCallback(() => {
|
|
||||||
if (trim(value) === '') return;
|
|
||||||
const id = uuid();
|
|
||||||
|
|
||||||
addNewestQuestion({
|
|
||||||
content: value,
|
|
||||||
files: files,
|
|
||||||
id,
|
|
||||||
role: MessageType.User,
|
|
||||||
});
|
|
||||||
if (done) {
|
|
||||||
setValue('');
|
|
||||||
handleSendMessage({
|
|
||||||
id,
|
|
||||||
content: value.trim(),
|
|
||||||
role: MessageType.User,
|
|
||||||
files: files,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
clearFiles();
|
|
||||||
}, [
|
|
||||||
value,
|
|
||||||
addNewestQuestion,
|
|
||||||
files,
|
|
||||||
done,
|
|
||||||
clearFiles,
|
|
||||||
setValue,
|
|
||||||
handleSendMessage,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
handlePressEnter,
|
handlePressEnter,
|
||||||
handleInputChange,
|
handleInputChange,
|
||||||
@ -239,13 +194,13 @@ export const useSendMessage = (controller: AbortController) => {
|
|||||||
setValue,
|
setValue,
|
||||||
regenerateMessage,
|
regenerateMessage,
|
||||||
sendLoading: !done,
|
sendLoading: !done,
|
||||||
loading,
|
|
||||||
scrollRef,
|
scrollRef,
|
||||||
messageContainerRef,
|
messageContainerRef,
|
||||||
derivedMessages,
|
derivedMessages,
|
||||||
removeMessageById,
|
removeMessageById,
|
||||||
handleUploadFile: onUploadFile,
|
handleUploadFile,
|
||||||
isUploading,
|
isUploading,
|
||||||
removeFile,
|
removeFile,
|
||||||
|
setDerivedMessages,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import { trim } from 'lodash';
|
|||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import { useBuildFormRefs } from './use-build-form-refs';
|
import { useBuildFormRefs } from './use-build-form-refs';
|
||||||
|
import { useCreateConversationBeforeSendMessage } from './use-chat-url';
|
||||||
import { useUploadFile } from './use-upload-file';
|
import { useUploadFile } from './use-upload-file';
|
||||||
|
|
||||||
export function useSendMultipleChatMessage(
|
export function useSendMultipleChatMessage(
|
||||||
@ -29,7 +30,11 @@ export function useSendMultipleChatMessage(
|
|||||||
api.completeConversation,
|
api.completeConversation,
|
||||||
);
|
);
|
||||||
|
|
||||||
const { handleUploadFile, files, clearFiles } = useUploadFile();
|
const { handleUploadFile, isUploading, files, clearFiles, removeFile } =
|
||||||
|
useUploadFile();
|
||||||
|
|
||||||
|
const { createConversationBeforeSendMessage } =
|
||||||
|
useCreateConversationBeforeSendMessage();
|
||||||
|
|
||||||
const { setFormRef, getLLMConfigById, isLLMConfigEmpty } =
|
const { setFormRef, getLLMConfigById, isLLMConfigEmpty } =
|
||||||
useBuildFormRefs(chatBoxIds);
|
useBuildFormRefs(chatBoxIds);
|
||||||
@ -170,10 +175,18 @@ export function useSendMultipleChatMessage(
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handlePressEnter = useCallback(() => {
|
const handlePressEnter = useCallback(async () => {
|
||||||
if (trim(value) === '') return;
|
if (trim(value) === '') return;
|
||||||
const id = uuid();
|
const id = uuid();
|
||||||
|
|
||||||
|
const data = await createConversationBeforeSendMessage(value);
|
||||||
|
|
||||||
|
if (data === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { targetConversationId, currentMessages } = data;
|
||||||
|
|
||||||
chatBoxIds.forEach((chatBoxId) => {
|
chatBoxIds.forEach((chatBoxId) => {
|
||||||
if (!isLLMConfigEmpty(chatBoxId)) {
|
if (!isLLMConfigEmpty(chatBoxId)) {
|
||||||
addNewestQuestion({
|
addNewestQuestion({
|
||||||
@ -182,6 +195,7 @@ export function useSendMultipleChatMessage(
|
|||||||
role: MessageType.User,
|
role: MessageType.User,
|
||||||
chatBoxId,
|
chatBoxId,
|
||||||
files,
|
files,
|
||||||
|
conversationId: targetConversationId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -196,8 +210,11 @@ export function useSendMultipleChatMessage(
|
|||||||
content: value.trim(),
|
content: value.trim(),
|
||||||
role: MessageType.User,
|
role: MessageType.User,
|
||||||
files,
|
files,
|
||||||
|
conversationId: targetConversationId,
|
||||||
},
|
},
|
||||||
chatBoxId,
|
chatBoxId,
|
||||||
|
currentConversationId: targetConversationId,
|
||||||
|
messages: currentMessages,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -205,6 +222,7 @@ export function useSendMultipleChatMessage(
|
|||||||
clearFiles();
|
clearFiles();
|
||||||
}, [
|
}, [
|
||||||
value,
|
value,
|
||||||
|
createConversationBeforeSendMessage,
|
||||||
chatBoxIds,
|
chatBoxIds,
|
||||||
allDone,
|
allDone,
|
||||||
clearFiles,
|
clearFiles,
|
||||||
@ -234,5 +252,7 @@ export function useSendMultipleChatMessage(
|
|||||||
sendLoading: !allDone,
|
sendLoading: !allDone,
|
||||||
setFormRef,
|
setFormRef,
|
||||||
handleUploadFile,
|
handleUploadFile,
|
||||||
|
isUploading,
|
||||||
|
removeFile,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,25 +0,0 @@
|
|||||||
import { ChatSearchParams } from '@/constants/chat';
|
|
||||||
import { useCallback, useMemo } from 'react';
|
|
||||||
import { useSearchParams } from 'umi';
|
|
||||||
|
|
||||||
export const useSetChatRouteParams = () => {
|
|
||||||
const [currentQueryParameters, setSearchParams] = useSearchParams();
|
|
||||||
const newQueryParameters: URLSearchParams = useMemo(
|
|
||||||
() => new URLSearchParams(currentQueryParameters.toString()),
|
|
||||||
[currentQueryParameters],
|
|
||||||
);
|
|
||||||
|
|
||||||
const setConversationIsNew = useCallback(
|
|
||||||
(value: string) => {
|
|
||||||
newQueryParameters.set(ChatSearchParams.isNew, value);
|
|
||||||
setSearchParams(newQueryParameters);
|
|
||||||
},
|
|
||||||
[newQueryParameters, setSearchParams],
|
|
||||||
);
|
|
||||||
|
|
||||||
const getConversationIsNew = useCallback(() => {
|
|
||||||
return newQueryParameters.get(ChatSearchParams.isNew);
|
|
||||||
}, [newQueryParameters]);
|
|
||||||
|
|
||||||
return { setConversationIsNew, getConversationIsNew };
|
|
||||||
};
|
|
||||||
@ -22,6 +22,7 @@ export const useSetConversation = () => {
|
|||||||
{
|
{
|
||||||
role: MessageType.Assistant,
|
role: MessageType.Assistant,
|
||||||
content: message,
|
content: message,
|
||||||
|
conversationId,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,6 +1,12 @@
|
|||||||
import { FileUploadProps } from '@/components/file-upload';
|
import { FileUploadProps } from '@/components/file-upload';
|
||||||
import { useUploadAndParseFile } from '@/hooks/use-chat-request';
|
import {
|
||||||
|
useGetChatSearchParams,
|
||||||
|
useUploadAndParseFile,
|
||||||
|
} from '@/hooks/use-chat-request';
|
||||||
|
import { generateConversationId } from '@/utils/chat';
|
||||||
import { useCallback, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
|
import { useChatUrlParams } from './use-chat-url';
|
||||||
|
import { useSetConversation } from './use-set-conversation';
|
||||||
|
|
||||||
export function useUploadFile() {
|
export function useUploadFile() {
|
||||||
const { uploadAndParseFile, loading, cancel } = useUploadAndParseFile();
|
const { uploadAndParseFile, loading, cancel } = useUploadAndParseFile();
|
||||||
@ -8,6 +14,9 @@ export function useUploadFile() {
|
|||||||
const [fileMap, setFileMap] = useState<Map<File, Record<string, any>>>(
|
const [fileMap, setFileMap] = useState<Map<File, Record<string, any>>>(
|
||||||
new Map(),
|
new Map(),
|
||||||
);
|
);
|
||||||
|
const { setConversation } = useSetConversation();
|
||||||
|
const { conversationId, isNew } = useGetChatSearchParams();
|
||||||
|
const { setIsNew, setConversationBoth } = useChatUrlParams();
|
||||||
|
|
||||||
type FileUploadParameters = Parameters<
|
type FileUploadParameters = Parameters<
|
||||||
NonNullable<FileUploadProps['onUpload']>
|
NonNullable<FileUploadProps['onUpload']>
|
||||||
@ -35,6 +44,44 @@ export function useUploadFile() {
|
|||||||
[uploadAndParseFile],
|
[uploadAndParseFile],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const createConversationBeforeUploadFile: NonNullable<
|
||||||
|
FileUploadProps['onUpload']
|
||||||
|
> = useCallback(
|
||||||
|
async (files, options) => {
|
||||||
|
if (
|
||||||
|
(conversationId === '' || isNew === 'true') &&
|
||||||
|
Array.isArray(files) &&
|
||||||
|
files.length
|
||||||
|
) {
|
||||||
|
const currentConversationId = generateConversationId();
|
||||||
|
|
||||||
|
if (conversationId === '') {
|
||||||
|
setConversationBoth(currentConversationId, 'true');
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await setConversation(
|
||||||
|
files[0].name,
|
||||||
|
true,
|
||||||
|
conversationId || currentConversationId,
|
||||||
|
);
|
||||||
|
if (data.code === 0) {
|
||||||
|
setIsNew('');
|
||||||
|
handleUploadFile(files, options, data.data?.id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
handleUploadFile(files, options);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[
|
||||||
|
conversationId,
|
||||||
|
handleUploadFile,
|
||||||
|
isNew,
|
||||||
|
setConversation,
|
||||||
|
setConversationBoth,
|
||||||
|
setIsNew,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
const clearFiles = useCallback(() => {
|
const clearFiles = useCallback(() => {
|
||||||
setCurrentFiles([]);
|
setCurrentFiles([]);
|
||||||
setFileMap(new Map());
|
setFileMap(new Map());
|
||||||
@ -55,7 +102,7 @@ export function useUploadFile() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
handleUploadFile,
|
handleUploadFile: createConversationBeforeUploadFile,
|
||||||
files: currentFiles,
|
files: currentFiles,
|
||||||
isUploading: loading,
|
isUploading: loading,
|
||||||
removeFile,
|
removeFile,
|
||||||
|
|||||||
@ -26,7 +26,7 @@ export const buildMessageListWithUuid = (messages?: Message[]) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getConversationId = () => {
|
export const generateConversationId = () => {
|
||||||
return uuid().replace(/-/g, '');
|
return uuid().replace(/-/g, '');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user