Skip to content

Commit

Permalink
Hide insight agent id in response
Browse files Browse the repository at this point in the history
Signed-off-by: Heng Qian <[email protected]>
  • Loading branch information
qianheng-aws committed Oct 10, 2024
1 parent ae4db73 commit 507f29b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('GeneratePopoverBody', () => {
case SUMMARY_ASSISTANT_API.SUMMARIZE:
value = {
summary: 'Generated summary content',
insightAgentId: 'insightAgentId',
insightAgentIdExists: true,
};
break;

Expand Down Expand Up @@ -168,7 +168,7 @@ describe('GeneratePopoverBody', () => {
case SUMMARY_ASSISTANT_API.SUMMARIZE:
value = {
summary: 'Generated summary content',
insightAgentId: undefined,
insightAgentIdExists: false,
};
break;

Expand Down Expand Up @@ -245,7 +245,7 @@ describe('GeneratePopoverBody', () => {
case SUMMARY_ASSISTANT_API.SUMMARIZE:
value = {
summary: 'Generated summary content',
insightAgentId: 'insightAgentId',
insightAgentIdExists: true,
};
break;

Expand Down Expand Up @@ -293,7 +293,7 @@ describe('GeneratePopoverBody', () => {
case SUMMARY_ASSISTANT_API.SUMMARIZE:
value = {
summary: 'Generated summary content',
insightAgentId: 'insightAgentId',
insightAgentIdExists: true,
};
break;

Expand Down Expand Up @@ -335,7 +335,7 @@ describe('GeneratePopoverBody', () => {
case SUMMARY_ASSISTANT_API.SUMMARIZE:
value = {
summary: 'Generated summary content',
insightAgentId: 'insightAgentId',
insightAgentIdExists: true,
};
break;

Expand Down
7 changes: 2 additions & 5 deletions public/components/incontext_insight/generate_popover_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const GeneratePopoverBody: React.FC<{
await httpSetup
?.post(SUMMARY_ASSISTANT_API.SUMMARIZE, {
body: JSON.stringify({
type: summaryType,
summaryType,
insightType,
question: summarizationQuestion,
context: contextContent,
Expand All @@ -123,11 +123,10 @@ export const GeneratePopoverBody: React.FC<{
const summaryContent = response.summary;
setSummary(summaryContent);
const insightAgentId = response.insightAgentId;
const insightAgentIdExists = !!insightType && !!insightAgentId;
const insightAgentIdExists = !!insightType && response.insightAgentIdExists;
setInsightAvailable(insightAgentIdExists);
if (insightAgentIdExists) {
onGenerateInsightBasedOnSummary(
insightAgentId,
dataSourceQuery,
summaryType,
insightType,
Expand All @@ -152,7 +151,6 @@ export const GeneratePopoverBody: React.FC<{
};

const onGenerateInsightBasedOnSummary = (
insightAgentId: string,
dataSourceQuery: {},
summaryType: string,
insightType: string,
Expand All @@ -164,7 +162,6 @@ export const GeneratePopoverBody: React.FC<{
httpSetup
?.post(SUMMARY_ASSISTANT_API.INSIGHT, {
body: JSON.stringify({
insightAgentId,
summaryType,
insightType,
summary: summaryContent,
Expand Down
42 changes: 29 additions & 13 deletions server/routes/summary_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { schema } from '@osd/config-schema';
import { IRouter } from '../../../../src/core/server';
import { IRouter, OpenSearchClient, RequestHandlerContext } from '../../../../src/core/server';
import { SUMMARY_ASSISTANT_API } from '../../common/constants/llm';
import { getOpenSearchClientTransport } from '../utils/get_opensearch_client_transport';
import { getAgentIdByConfigName, searchAgent } from './get_agent';
Expand All @@ -24,7 +24,7 @@ export function registerSummaryAssistantRoutes(
path: SUMMARY_ASSISTANT_API.SUMMARIZE,
validate: {
body: schema.object({
type: schema.string(),
summaryType: schema.string(),
insightType: schema.maybe(schema.string()),
question: schema.string(),
context: schema.maybe(schema.string()),
Expand Down Expand Up @@ -55,16 +55,14 @@ export function registerSummaryAssistantRoutes(
topNLogPatternData: req.body.topNLogPatternData,
});
let summary;
let insightAgentId;
let insightAgentIdExists = false;
try {
if (req.body.insightType) {
// We have separate agent for os_insight and user_insight. And for user_insight, we can
// only get it by searching on name since it is not stored in agent config.
if (req.body.insightType === 'os_insight') {
insightAgentId = await getAgentIdByConfigName(OS_INSIGHT_AGENT_CONFIG_ID, client);
} else if (req.body.insightType === 'user_insight' && req.body.type === 'alerts') {
insightAgentId = await searchAgent({ name: 'KB_For_Alert_Insight' }, client);
}
insightAgentIdExists = !!(await detectInsightAgentId(
req.body.insightType,
req.body.summaryType,
client
));
}
} catch (e) {
context.assistant_plugin.logger.debug(
Expand All @@ -74,7 +72,7 @@ export function registerSummaryAssistantRoutes(
}
try {
summary = response.body.inference_results[0].output[0].result;
return res.ok({ body: { summary, insightAgentId } });
return res.ok({ body: { summary, insightAgentIdExists } });
} catch (e) {
return res.internalError();
}
Expand All @@ -85,7 +83,6 @@ export function registerSummaryAssistantRoutes(
path: SUMMARY_ASSISTANT_API.INSIGHT,
validate: {
body: schema.object({
insightAgentId: schema.string(),
summaryType: schema.string(),
insightType: schema.string(),
summary: schema.string(),
Expand All @@ -103,7 +100,12 @@ export function registerSummaryAssistantRoutes(
dataSourceId: req.query.dataSourceId,
});
const assistantClient = assistantService.getScopedClient(req, context);
const response = await assistantClient.executeAgent(req.body.insightAgentId, {
const insightAgentId = await detectInsightAgentId(
req.body.insightType,
req.body.summaryType,
client
);
const response = await assistantClient.executeAgent(insightAgentId, {
context: req.body.context,
summary: req.body.summary,
question: req.body.question,
Expand All @@ -117,6 +119,20 @@ export function registerSummaryAssistantRoutes(
);
}

function detectInsightAgentId(
insightType: string,
appType: string,
client: OpenSearchClient['transport']
) {
// We have separate agent for os_insight and user_insight. And for user_insight, we can
// only get it by searching on name since it is not stored in agent config.
if (insightType === 'os_insight') {
return getAgentIdByConfigName(OS_INSIGHT_AGENT_CONFIG_ID, client);
} else if (insightType === 'user_insight' && appType === 'alerts') {
return searchAgent({ name: 'KB_For_Alert_Insight' }, client);
}
}

export function registerData2SummaryRoutes(
router: IRouter,
assistantService: AssistantServiceSetup
Expand Down

0 comments on commit 507f29b

Please sign in to comment.