You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637
  1. import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
  2. import { useSendMessageWithSse } from '@/hooks/logic-hooks';
  3. import { IAnswer } from '@/interfaces/database/chat';
  4. import api from '@/utils/api';
  5. import { isEmpty } from 'lodash';
  6. import { useCallback, useEffect, useState } from 'react';
  7. export const useSendQuestion = (kbIds: string[]) => {
  8. const { send, answer, done } = useSendMessageWithSse(api.ask);
  9. const { testChunk, loading } = useTestChunkRetrieval();
  10. const [sendingLoading, setSendingLoading] = useState(false);
  11. const [currentAnswer, setCurrentAnswer] = useState({} as IAnswer);
  12. const sendQuestion = useCallback(
  13. (question: string) => {
  14. setCurrentAnswer({} as IAnswer);
  15. setSendingLoading(true);
  16. send({ kb_ids: kbIds, question });
  17. testChunk({ kb_id: kbIds, highlight: true, question });
  18. },
  19. [send, testChunk, kbIds],
  20. );
  21. useEffect(() => {
  22. if (!isEmpty(answer)) {
  23. setCurrentAnswer(answer);
  24. }
  25. }, [answer]);
  26. useEffect(() => {
  27. if (done) {
  28. setSendingLoading(false);
  29. }
  30. }, [done]);
  31. return { sendQuestion, loading, sendingLoading, answer: currentAnswer };
  32. };