Skip to content

Commit

Permalink
fix(generatedAnswer): map state with answer api client (#4681)
Browse files Browse the repository at this point in the history
SVCC-4353
https://coveord.atlassian.net/browse/SVCC-4353

## The problem

Upon interacting with a stream response, all headless selectors relying
on the `generatedAnswer` slice after a response fetched by the answerAPI
client aren't working properly because the state of the answerAPI client
is not mapped to the actual `generatedAnswer` slice.
e.g. the custom events

## The proposed fix

The answer API client is now dispatching what is relevant to the
`generatedAnswer` state. The selectors will then return the same value
as the generated answer controller state whenever it gets its answer
from the client getting its answer directly from reveal or from the
answer api.

Co-authored-by: Danny Gauthier <dgauthier@WKS-002169.corp.coveo.com>
  • Loading branch information
dmgauthier and Danny Gauthier authored Nov 18, 2024
1 parent 8fb043f commit 8344a9f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/headless/src/api/knowledge/stream-answer-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import {
EventSourceMessage,
fetchEventSource,
} from '@microsoft/fetch-event-source';
import {createSelector} from '@reduxjs/toolkit';
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 {GeneratedContentFormat} from '../../features/generated-answer/generated-response-format.js';
import {maximumNumberOfResultsFromIndex} from '../../features/pagination/pagination-constants.js';
Expand Down Expand Up @@ -118,7 +124,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 @@ -133,21 +140,27 @@ const updateCacheWithEvent = (
case 'genqa.headerMessageType':
if (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 @@ -170,7 +183,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 8344a9f

Please sign in to comment.