|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
|
import store from '../store';
|
|
|
|
|
import { fetchAnswerApi, fetchAnswerSteaming } from './conversationApi';
|
|
|
|
|
import { searchEndpoint } from './conversationApi';
|
|
|
|
|
import { Answer, ConversationState, Query, Status } from './conversationModels';
|
|
|
|
|
import { getConversations } from '../preferences/preferenceApi';
|
|
|
|
|
import { setConversations } from '../preferences/preferenceSlice';
|
|
|
|
@ -29,6 +30,7 @@ export const fetchAnswer = createAsyncThunk<Answer, { question: string }>(
|
|
|
|
|
(event) => {
|
|
|
|
|
const data = JSON.parse(event.data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check if the 'end' event has been received
|
|
|
|
|
if (data.type === 'end') {
|
|
|
|
|
// set status to 'idle'
|
|
|
|
@ -40,24 +42,22 @@ export const fetchAnswer = createAsyncThunk<Answer, { question: string }>(
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error('Failed to fetch conversations: ', error);
|
|
|
|
|
});
|
|
|
|
|
} else if (data.type === 'source') {
|
|
|
|
|
// check if data.metadata exists
|
|
|
|
|
let result;
|
|
|
|
|
if (data.metadata && data.metadata.title) {
|
|
|
|
|
const titleParts = data.metadata.title.split('/');
|
|
|
|
|
result = {
|
|
|
|
|
title: titleParts[titleParts.length - 1],
|
|
|
|
|
text: data.doc,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
result = { title: data.doc, text: data.doc };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
searchEndpoint( //search for sources post streaming
|
|
|
|
|
question,
|
|
|
|
|
state.preference.apiKey,
|
|
|
|
|
state.preference.selectedDocs!,
|
|
|
|
|
state.conversation.conversationId,
|
|
|
|
|
state.conversation.queries
|
|
|
|
|
).then(sources => {
|
|
|
|
|
//dispatch streaming sources
|
|
|
|
|
dispatch(
|
|
|
|
|
updateStreamingSource({
|
|
|
|
|
index: state.conversation.queries.length - 1,
|
|
|
|
|
query: { sources: [result] },
|
|
|
|
|
query: { sources },
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
} else if (data.type === 'id') {
|
|
|
|
|
dispatch(
|
|
|
|
|
updateConversationId({
|
|
|
|
@ -165,9 +165,10 @@ export const conversationSlice = createSlice({
|
|
|
|
|
state,
|
|
|
|
|
action: PayloadAction<{ index: number; query: Partial<Query> }>,
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
const { index, query } = action.payload;
|
|
|
|
|
if (!state.queries[index].sources) {
|
|
|
|
|
state.queries[index].sources = [query.sources![0]];
|
|
|
|
|
state.queries[index].sources = query?.sources;
|
|
|
|
|
} else {
|
|
|
|
|
state.queries[index].sources!.push(query.sources![0]);
|
|
|
|
|
}
|
|
|
|
|