11import { defineStore } from 'pinia' ;
22import { computed , ref , type ComputedRef , type Ref } from 'vue' ;
33import type { Document , ErrorDetails , ChatMessage , ReformulateResponse } from '@/types' ;
4- import { fetchStream , postAxios } from '@/utils/fetch' ;
4+ import { postAxios } from '@/utils/fetch' ;
55import { getQueryParamValue } from '@/utils/urlsUtils' ;
66import { RELEVANCE_FACTOR } from '@/utils/constants' ;
77import { getFromStorage , saveToStorage , clearFromStorage } from '@/utils/storage' ;
@@ -209,6 +209,7 @@ export const useChatStore = defineStore('chat', () => {
209209 }
210210
211211 async function getNoStreamAnswer ( userMsg : string ) {
212+ chatStatus . value = CHAT_STATUS . FORMULATING_ANSWER ;
212213 const bodyContent = {
213214 sources : sourcesList . value || [ ] ,
214215 history : getMessageHistory . value ,
@@ -218,7 +219,6 @@ export const useChatStore = defineStore('chat', () => {
218219
219220 const respBody = await postAxios ( '/qna/chat/answer' , bodyContent ) ;
220221
221- console . log ( respBody ) ;
222222 chatStatus . value = CHAT_STATUS . FORMULATED_ANSWER ;
223223
224224 chatMessagesList . value . push ( { role : 'assistant' , content : respBody . data } ) ;
@@ -236,106 +236,27 @@ export const useChatStore = defineStore('chat', () => {
236236 setQuestionQueues ( newQuestions ?. data [ 'NEW_QUESTIONS' ] ) ;
237237 }
238238
239- // async function fetchChatAnswer(userMsg: string) {
240- // if (chatStatus.value !== CHAT_STATUS.SEARCHED) return;
241-
242- // try {
243- // const bodyContent = JSON.stringify({
244- // sources: sourcesList.value || [],
245- // history: getMessageHistory.value,
246- // query: userMsg,
247- // ...(storedSubject.value && { subject: storedSubject.value })
248- // });
249-
250- // const respBody = await fetchStream('/qna/stream', {
251- // bodyContent
252- // });
253-
254- // if (!respBody) {
255- // return;
256- // }
257-
258- // const reader = respBody.pipeThrough(new TextDecoderStream()).getReader();
259-
260- // chatMessagesList.value.push({ role: 'assistant', content: '' });
261- // const assistantsAns = chatMessagesList.value.length - 1;
262-
263- // // eslint-disable-next-line no-constant-condition
264- // while (true) {
265- // chatStatus.value = CHAT_STATUS.FORMULATING_ANSWER;
266- // const { value, done } = await reader.read();
267-
268- // if (done) {
269- // const newQuestions: AxiosResponse<{ NEW_QUESTIONS: string[] }> = await postAxios(
270- // '/qna/reformulate/questions',
271- // {
272- // history: getMessageHistory.value,
273- // query: reformulatedQuery.value
274- // }
275- // );
276- // chatStatus.value = CHAT_STATUS.FORMULATED_ANSWER;
277-
278- // setQuestionQueues(newQuestions?.data['NEW_QUESTIONS']);
279-
280- // saveToStorage('chat', chatMessagesList.value);
281- // break;
282- // }
283- // chatMessagesList.value[assistantsAns].content += value;
284- // }
285- // } catch (error) {
286- // console.error(error);
287- // chatStatus.value = CHAT_STATUS.ERROR;
288- // }
289- // }
290-
291- async function fetchRephraseStream ( message : string ) {
292- if ( ! chatStatus . value === CHAT_STATUS . DONE ) return ;
239+ async function fetchRephrase ( ) {
293240 chatStatus . value = CHAT_STATUS . FORMULATING_ANSWER ;
294- try {
295- const lastAssistantsMessage =
296- message || chatMessagesList . value [ chatMessagesList . value . length - 1 ] . content ;
297-
298- const bodyContent = JSON . stringify ( {
299- sources : sourcesList . value ,
300- history : getMessageHistory . value ,
301- query : lastAssistantsMessage ,
302- ...( storedSubject . value && { subject : storedSubject . value } )
303- } ) ;
304-
305- let respBody ;
306- try {
307- respBody = await fetchStream ( '/qna/chat/rephrase_stream' , { bodyContent } ) ;
308- } catch ( error ) {
309- console . error ( error ) ;
310- }
311-
312- if ( ! respBody ) {
313- return ;
314- }
315-
316- const reader = respBody . pipeThrough ( new TextDecoderStream ( ) ) . getReader ( ) ;
241+ // get the content of the message which the role is assistant
242+ const lastAssistantMessage = [ ...chatMessagesList . value ]
243+ . reverse ( )
244+ . find ( ( msg ) => msg . role === 'assistant' ) ?. content ;
317245
318- chatMessagesList . value . push ( { role : 'assistant' , content : '' } ) ;
319- const assistantsAns = chatMessagesList . value . length - 1 ;
246+ const bodyContent = {
247+ sources : sourcesList . value ,
248+ history : getMessageHistory . value ,
249+ query : lastAssistantMessage ,
250+ ...( storedSubject . value && { subject : storedSubject . value } )
251+ } ;
320252
321- // eslint-disable-next-line no-constant-condition
322- while ( true ) {
323- const { value, done } = await reader . read ( ) ;
253+ const respBody = await postAxios ( '/qna/chat/rephrase' , bodyContent ) ;
324254
325- if ( done ) {
326- chatStatus . value = CHAT_STATUS . FORMULATED_ANSWER ;
327- isAnswerStreamed . value = true ;
255+ chatMessagesList . value . push ( { role : 'assistant' , content : respBody . data } ) ;
256+ saveToStorage ( 'chat' , chatMessagesList . value ) ;
328257
329- break ;
330- }
331- chatMessagesList . value [ assistantsAns ] . content += value ;
332- }
333- } catch ( error ) {
334- chatStatus . value = CHAT_STATUS . ERROR ;
335- } finally {
336- chatStatus . value = CHAT_STATUS . FORMULATED_ANSWER ;
337- chatStatus . value = CHAT_STATUS . DONE ;
338- }
258+ chatStatus . value = CHAT_STATUS . FORMULATED_ANSWER ;
259+ chatStatus . value = CHAT_STATUS . DONE ;
339260 }
340261
341262 const noResultsAnswer = ( ) => {
@@ -417,7 +338,7 @@ export const useChatStore = defineStore('chat', () => {
417338 corpora,
418339 reformulatedQuery,
419340 onSendMessage,
420- fetchRephraseStream ,
341+ fetchRephrase ,
421342 clearInput,
422343 setReformulatedQuery,
423344 shouldDisplayScore,
0 commit comments