Skip to content

Commit

Permalink
fix(generatedAnswer): map state with answerAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny Gauthier authored and Danny Gauthier committed Nov 19, 2024
1 parent de6f91f commit 989434c
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions packages/headless/src/api/knowledge/stream-answer-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import {
EventSourceMessage,
fetchEventSource,
} from '@microsoft/fetch-event-source';
import {createSelector} from '@reduxjs/toolkit';
import {selectFieldsToIncludeInCitation} from '../../features/generated-answer/generated-answer-selectors';
import {createSelector, ThunkDispatch, UnknownAction} from '@reduxjs/toolkit';
import {
setAnswerContentFormat,
updateCitations,
updateMessage,
} from '../../features/generated-answer/generated-answer-actions.js';
import {logGeneratedAnswerStreamEnd} from '../../features/generated-answer/generated-answer-analytics-actions.js';
import {selectFieldsToIncludeInCitation} from '../../features/generated-answer/generated-answer-selectors.js';
import {
GeneratedAnswerStyle,
GeneratedContentFormat,
} from '../../features/generated-answer/generated-response-format';
import {maximumNumberOfResultsFromIndex} from '../../features/pagination/pagination-constants';
import {selectPipeline} from '../../features/pipeline/select-pipeline';
import {selectQuery} from '../../features/query/query-selectors';
import {selectSearchHub} from '../../features/search-hub/search-hub-selectors';
} from '../../features/generated-answer/generated-response-format.js';
import {maximumNumberOfResultsFromIndex} from '../../features/pagination/pagination-constants.js';
import {selectPipeline} from '../../features/pipeline/select-pipeline.js';
import {selectQuery} from '../../features/query/query-selectors.js';
import {selectSearchHub} from '../../features/search-hub/search-hub-selectors.js';
import {
initialSearchMappings,
mapFacetRequest,
Expand Down Expand Up @@ -123,7 +129,8 @@ const handleError = (

const updateCacheWithEvent = (
event: EventSourceMessage,
draft: GeneratedAnswerStream
draft: GeneratedAnswerStream,
dispatch: ThunkDispatch<StateNeededByAnswerAPI, unknown, UnknownAction>
) => {
const message: Required<MessageType> = JSON.parse(event.data);
if (message.finishReason === 'ERROR' && message.errorMessage) {
Expand All @@ -138,21 +145,27 @@ const updateCacheWithEvent = (
case 'genqa.headerMessageType':
if (parsedPayload.answerStyle && parsedPayload.contentFormat) {
handleHeaderMessage(draft, parsedPayload);
dispatch(setAnswerContentFormat(parsedPayload.contentFormat));
}
break;
case 'genqa.messageType':
if (parsedPayload.textDelta) {
handleMessage(draft, parsedPayload);
dispatch(updateMessage({textDelta: parsedPayload.textDelta}));
}
break;
case 'genqa.citationsType':
if (parsedPayload.citations) {
handleCitations(draft, parsedPayload);
dispatch(updateCitations({citations: parsedPayload.citations}));
}
break;
case 'genqa.endOfStreamType':
if (draft.answer?.length || parsedPayload.answerGenerated) {
handleEndOfStream(draft, parsedPayload);
dispatch(
logGeneratedAnswerStreamEnd(parsedPayload.answerGenerated ?? false)
);
}
break;
}
Expand All @@ -176,7 +189,7 @@ export const answerApi = answerSlice.injectEndpoints({
}),
async onCacheEntryAdded(
args,
{getState, cacheDataLoaded, updateCachedData}
{getState, cacheDataLoaded, updateCachedData, dispatch}
) {
await cacheDataLoaded;
/**
Expand Down Expand Up @@ -209,7 +222,7 @@ export const answerApi = answerSlice.injectEndpoints({
},
onmessage: (event) => {
updateCachedData((draft) => {
updateCacheWithEvent(event, draft);
updateCacheWithEvent(event, draft, dispatch);
});
},
onerror: (error) => {
Expand Down

0 comments on commit 989434c

Please sign in to comment.