Skip to content

Commit

Permalink
fix(answerApi): search context trigger new request
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny Gauthier authored and Danny Gauthier committed Aug 19, 2024
1 parent e65b99f commit 24cdd0f
Show file tree
Hide file tree
Showing 6 changed files with 1,570 additions and 67 deletions.
85 changes: 74 additions & 11 deletions packages/headless/src/api/knowledge/stream-answer-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ 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';
import {
initialSearchMappings,
mapFacetRequest,
} from '../../features/search/search-mappings';
import {SearchAppState} from '../../state/search-app-state';
import {
ConfigurationSection,
DebugSection,
GeneratedAnswerSection,
QuerySection,
SearchSection,
} from '../../state/state-sections';
import {getFacets} from '../../utils/facet-utils';
import {GeneratedAnswerCitation} from '../generated-answer/generated-answer-event-payload';
import {SearchRequest} from '../search/search/search-request';
import {answerSlice} from './answer-slice';
Expand All @@ -27,9 +31,7 @@ export type StateNeededByAnswerAPI = {
pipeline: string;
answer: ReturnType<typeof answerApi.reducer>;
} & ConfigurationSection &
QuerySection &
SearchSection &
DebugSection &
Partial<SearchAppState> &
GeneratedAnswerSection;

export interface GeneratedAnswerStream {
Expand Down Expand Up @@ -226,12 +228,47 @@ export const selectAnswerTriggerParams = createSelector(
(q, requestId) => ({q, requestId})
);

const constructAnswerQueryParams = (state: StateNeededByAnswerAPI) => {
let generateFacetParams: Record<string, ReturnType<typeof getFacets>> = {};

const getGeneratedFacetParams = (q: string, state: StateNeededByAnswerAPI) => ({
...generateFacetParams,
[q]: getFacets(state)
?.map((facetRequest) =>
mapFacetRequest(facetRequest, initialSearchMappings())
)
.sort((a, b) =>
a.facetId > b.facetId ? 1 : b.facetId > a.facetId ? -1 : 0
),
});

const getNumberOfResultsWithinIndexLimit = (state: StateNeededByAnswerAPI) => {
if (!state.pagination) {
return undefined;
}

const isOverIndexLimit =
state.pagination.firstResult + state.pagination.numberOfResults >
maximumNumberOfResultsFromIndex;

if (isOverIndexLimit) {
return maximumNumberOfResultsFromIndex - state.pagination.firstResult;
}
return state.pagination.numberOfResults;
};

export const constructAnswerQueryParams = (
state: StateNeededByAnswerAPI,
usage: 'fetch' | 'select'
) => {
const q = selectQuery(state)?.q;
const searchHub = selectSearchHub(state);
const pipeline = selectPipeline(state);
const citationsFieldToInclude = selectFieldsToIncludeInCitation(state) ?? [];

if (q && usage === 'fetch') {
generateFacetParams = getGeneratedFacetParams(q, state);
}

return {
q,
pipelineRuleParameters: {
Expand All @@ -242,13 +279,39 @@ const constructAnswerQueryParams = (state: StateNeededByAnswerAPI) => {
},
...(searchHub?.length && {searchHub}),
...(pipeline?.length && {pipeline}),
...(generateFacetParams[q!]?.length && {
facets: generateFacetParams[q!],
}),
...(state.fields && {fieldsToInclude: state.fields.fieldsToInclude}),
...(state.didYouMean && {
queryCorrection: {
enabled:
state.didYouMean.enableDidYouMean &&
state.didYouMean.queryCorrectionMode === 'next',
options: {
automaticallyCorrect: state.didYouMean.automaticallyCorrectQuery
? ('whenNoResults' as const)
: ('never' as const),
},
},
enableDidYouMean:
state.didYouMean.enableDidYouMean &&
state.didYouMean.queryCorrectionMode === 'legacy',
}),
...(state.pagination && {
numberOfResults: getNumberOfResultsWithinIndexLimit(state),
firstResult: state.pagination.firstResult,
}),
tab: state.configuration.analytics.originLevel2,
};
};

export const fetchAnswer = (state: StateNeededByAnswerAPI) =>
answerApi.endpoints.getAnswer.initiate(constructAnswerQueryParams(state));
answerApi.endpoints.getAnswer.initiate(
constructAnswerQueryParams(state, 'fetch')
);

export const selectAnswer = (state: StateNeededByAnswerAPI) =>
answerApi.endpoints.getAnswer.select(constructAnswerQueryParams(state))(
state
);
answerApi.endpoints.getAnswer.select(
constructAnswerQueryParams(state, 'select')
)(state);
Loading

0 comments on commit 24cdd0f

Please sign in to comment.