Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(generatedAnswer): map state with answerAPI #4684

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
import {logGeneratedAnswerStreamEnd} from '../../features/generated-answer/generated-answer-analytics-actions';
import {selectFieldsToIncludeInCitation} from '../../features/generated-answer/generated-answer-selectors';
import {
GeneratedAnswerStyle,
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
Loading