From 16f2e56dfa26aae83edcf43b4cde6848e9afb7d3 Mon Sep 17 00:00:00 2001 From: Danny Gauthier Date: Tue, 5 Nov 2024 10:04:41 -0500 Subject: [PATCH] feat(answerAPI): insight generate endpoint (#4602) ## Answer API [SVCC-4265](https://coveord.atlassian.net/browse/SVCC-4265) ### Support for the insight specific endpoint The insight panels require a specific generate endpoint because their query pipeline is not exposed and thus it cannot be passed to the generate call from the client. The new endpoint is managing this from under the hood if we pass an InsightConfiguration [SVCC-4265]: https://coveord.atlassian.net/browse/SVCC-4265?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Danny Gauthier Co-authored-by: Frederic Beaudoin --- packages/headless/src/api/knowledge/stream-answer-api.ts | 8 ++++++-- .../api/knowledge/tests/stream-answer-api-state-mock.ts | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/headless/src/api/knowledge/stream-answer-api.ts b/packages/headless/src/api/knowledge/stream-answer-api.ts index a13184555db..9c3f0bd76d0 100644 --- a/packages/headless/src/api/knowledge/stream-answer-api.ts +++ b/packages/headless/src/api/knowledge/stream-answer-api.ts @@ -17,6 +17,7 @@ import {SearchAppState} from '../../state/search-app-state.js'; import { ConfigurationSection, GeneratedAnswerSection, + InsightConfigurationSection, } from '../../state/state-sections.js'; import {getFacets} from '../../utils/facet-utils.js'; import {GeneratedAnswerCitation} from '../generated-answer/generated-answer-event-payload.js'; @@ -30,6 +31,7 @@ export type StateNeededByAnswerAPI = { answer: ReturnType; } & ConfigurationSection & Partial & + Partial & GeneratedAnswerSection; export interface GeneratedAnswerStream { @@ -176,15 +178,17 @@ export const answerApi = answerSlice.injectEndpoints({ * It cannot use the inferred state used by Redux, thus the casting. * https://redux-toolkit.js.org/rtk-query/usage-with-typescript#typing-dispatch-and-getstate */ - const {configuration, generatedAnswer} = + const {configuration, generatedAnswer, insightConfiguration} = getState() as unknown as StateNeededByAnswerAPI; const {organizationId, environment, accessToken} = configuration; const platformEndpoint = getOrganizationEndpoint( organizationId, environment ); + const insightGenerateEndpoint = `${platformEndpoint}/rest/organizations/${organizationId}/insight/v1/configs/${insightConfiguration?.insightId}/answer/${generatedAnswer.answerConfigurationId}/generate`; + const generateEndpoint = `${platformEndpoint}/rest/organizations/${organizationId}/answer/v1/configs/${generatedAnswer.answerConfigurationId}/generate`; await fetchEventSource( - `${platformEndpoint}/rest/organizations/${organizationId}/answer/v1/configs/${generatedAnswer.answerConfigurationId}/generate`, + insightConfiguration ? insightGenerateEndpoint : generateEndpoint, { method: 'POST', body: JSON.stringify(args), diff --git a/packages/headless/src/api/knowledge/tests/stream-answer-api-state-mock.ts b/packages/headless/src/api/knowledge/tests/stream-answer-api-state-mock.ts index a32adee8a2f..386e19bb4df 100644 --- a/packages/headless/src/api/knowledge/tests/stream-answer-api-state-mock.ts +++ b/packages/headless/src/api/knowledge/tests/stream-answer-api-state-mock.ts @@ -31,6 +31,9 @@ export const streamAnswerAPIStateMock: StateNeededByAnswerAPI = { answerConfigurationId: '', }, }, + insightConfiguration: { + insightId: 'insight-id', + }, version: '2.77.0', debug: false, pipeline: '',