2024-09-06 19:56:17 +08:00
|
|
|
import { useFetchMindMap, useFetchRelatedQuestions } from '@/hooks/chat-hooks';
|
2024-09-05 19:32:55 +08:00
|
|
|
import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
|
|
|
|
|
import { useSendMessageWithSse } from '@/hooks/logic-hooks';
|
2024-09-06 15:42:55 +08:00
|
|
|
import { IAnswer } from '@/interfaces/database/chat';
|
2024-09-05 19:32:55 +08:00
|
|
|
import api from '@/utils/api';
|
2024-09-06 15:42:55 +08:00
|
|
|
import { isEmpty } from 'lodash';
|
|
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
2024-09-05 19:32:55 +08:00
|
|
|
|
|
|
|
|
export const useSendQuestion = (kbIds: string[]) => {
|
|
|
|
|
const { send, answer, done } = useSendMessageWithSse(api.ask);
|
|
|
|
|
const { testChunk, loading } = useTestChunkRetrieval();
|
|
|
|
|
const [sendingLoading, setSendingLoading] = useState(false);
|
2024-09-06 15:42:55 +08:00
|
|
|
const [currentAnswer, setCurrentAnswer] = useState({} as IAnswer);
|
2024-09-06 19:56:17 +08:00
|
|
|
const { fetchRelatedQuestions, data: relatedQuestions } =
|
|
|
|
|
useFetchRelatedQuestions();
|
|
|
|
|
const {
|
|
|
|
|
fetchMindMap,
|
|
|
|
|
data: mindMap,
|
|
|
|
|
loading: mindMapLoading,
|
|
|
|
|
} = useFetchMindMap();
|
2024-09-05 19:32:55 +08:00
|
|
|
|
|
|
|
|
const sendQuestion = useCallback(
|
|
|
|
|
(question: string) => {
|
2024-09-06 15:42:55 +08:00
|
|
|
setCurrentAnswer({} as IAnswer);
|
2024-09-05 19:32:55 +08:00
|
|
|
setSendingLoading(true);
|
|
|
|
|
send({ kb_ids: kbIds, question });
|
|
|
|
|
testChunk({ kb_id: kbIds, highlight: true, question });
|
2024-09-06 19:56:17 +08:00
|
|
|
fetchMindMap({
|
|
|
|
|
question,
|
|
|
|
|
kb_ids: kbIds,
|
|
|
|
|
});
|
|
|
|
|
fetchRelatedQuestions(question);
|
2024-09-05 19:32:55 +08:00
|
|
|
},
|
2024-09-06 19:56:17 +08:00
|
|
|
[send, testChunk, kbIds, fetchRelatedQuestions, fetchMindMap],
|
2024-09-05 19:32:55 +08:00
|
|
|
);
|
|
|
|
|
|
2024-09-06 15:42:55 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isEmpty(answer)) {
|
|
|
|
|
setCurrentAnswer(answer);
|
|
|
|
|
}
|
|
|
|
|
}, [answer]);
|
|
|
|
|
|
2024-09-05 19:32:55 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (done) {
|
|
|
|
|
setSendingLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}, [done]);
|
|
|
|
|
|
2024-09-06 19:56:17 +08:00
|
|
|
return {
|
|
|
|
|
sendQuestion,
|
|
|
|
|
loading,
|
|
|
|
|
sendingLoading,
|
|
|
|
|
answer: currentAnswer,
|
|
|
|
|
relatedQuestions: relatedQuestions?.slice(0, 5) ?? [],
|
|
|
|
|
mindMap,
|
|
|
|
|
mindMapLoading,
|
|
|
|
|
};
|
2024-09-05 19:32:55 +08:00
|
|
|
};
|