Skip to content

Commit 8ab30f5

Browse files
authored
[Security assistant] Fix invoke_assistant_* telemetry (elastic#198594)
1 parent 0ad421b commit 8ab30f5

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

x-pack/plugins/elastic_assistant/server/routes/chat/chat_complete_route.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ describe('chatCompleteRoute', () => {
290290
actionTypeId: '.gen-ai',
291291
model: 'gpt-4',
292292
assistantStreamingEnabled: false,
293+
isEnabledKnowledgeBase: false,
293294
});
294295
}),
295296
};

x-pack/plugins/elastic_assistant/server/routes/chat/chat_complete_route.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import { buildResponse } from '../../lib/build_response';
2525
import {
2626
appendAssistantMessageToConversation,
2727
createConversationWithUserInput,
28+
DEFAULT_PLUGIN_NAME,
29+
getIsKnowledgeBaseInstalled,
30+
getPluginNameFromRequest,
2831
langChainExecute,
2932
performChecks,
3033
} from '../helpers';
@@ -63,9 +66,9 @@ export const chatCompleteRoute = (
6366
const assistantResponse = buildResponse(response);
6467
let telemetry;
6568
let actionTypeId;
69+
const ctx = await context.resolve(['core', 'elasticAssistant', 'licensing']);
70+
const logger: Logger = ctx.elasticAssistant.logger;
6671
try {
67-
const ctx = await context.resolve(['core', 'elasticAssistant', 'licensing']);
68-
const logger: Logger = ctx.elasticAssistant.logger;
6972
telemetry = ctx.elasticAssistant.telemetry;
7073
const inference = ctx.elasticAssistant.inference;
7174

@@ -219,13 +222,27 @@ export const chatCompleteRoute = (
219222
});
220223
} catch (err) {
221224
const error = transformError(err as Error);
225+
const pluginName = getPluginNameFromRequest({
226+
request,
227+
defaultPluginName: DEFAULT_PLUGIN_NAME,
228+
logger,
229+
});
230+
const v2KnowledgeBaseEnabled =
231+
ctx.elasticAssistant.getRegisteredFeatures(pluginName).assistantKnowledgeBaseByDefault;
232+
const kbDataClient =
233+
(await ctx.elasticAssistant.getAIAssistantKnowledgeBaseDataClient({
234+
v2KnowledgeBaseEnabled,
235+
})) ?? undefined;
236+
const isKnowledgeBaseInstalled = await getIsKnowledgeBaseInstalled(kbDataClient);
237+
222238
telemetry?.reportEvent(INVOKE_ASSISTANT_ERROR_EVENT.eventType, {
223239
actionTypeId: actionTypeId ?? '',
224240
model: request.body.model,
225241
errorMessage: error.message,
226242
// TODO rm actionTypeId check when llmClass for bedrock streaming is implemented
227243
// tracked here: https://github.com/elastic/security-team/issues/7363
228244
assistantStreamingEnabled: request.body.isStream ?? false,
245+
isEnabledKnowledgeBase: isKnowledgeBaseInstalled,
229246
});
230247
return assistantResponse.error({
231248
body: error.message,

x-pack/plugins/elastic_assistant/server/routes/helpers.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { FindResponse } from '../ai_assistant_data_clients/find';
3535
import { EsPromptsSchema } from '../ai_assistant_data_clients/prompts/types';
3636
import { AIAssistantDataClient } from '../ai_assistant_data_clients';
3737
import { MINIMUM_AI_ASSISTANT_LICENSE } from '../../common/constants';
38-
import { ESQL_DOCS_LOADED_QUERY } from './knowledge_base/constants';
38+
import { SECURITY_LABS_RESOURCE, SECURITY_LABS_LOADED_QUERY } from './knowledge_base/constants';
3939
import { buildResponse, getLlmType } from './utils';
4040
import {
4141
AgentExecutorParams,
@@ -436,15 +436,13 @@ export const langChainExecute = async ({
436436
executorParams
437437
);
438438

439-
const { esqlExists, isModelDeployed } = await getIsKnowledgeBaseEnabled(kbDataClient);
439+
const isKnowledgeBaseInstalled = await getIsKnowledgeBaseInstalled(kbDataClient);
440440

441441
telemetry.reportEvent(INVOKE_ASSISTANT_SUCCESS_EVENT.eventType, {
442442
actionTypeId,
443443
model: request.body.model,
444-
// TODO rm actionTypeId check when llmClass for bedrock streaming is implemented
445-
// tracked here: https://github.com/elastic/security-team/issues/7363
446-
assistantStreamingEnabled: isStream && actionTypeId === '.gen-ai',
447-
isEnabledKnowledgeBase: isModelDeployed && esqlExists,
444+
assistantStreamingEnabled: isStream,
445+
isEnabledKnowledgeBase: isKnowledgeBaseInstalled,
448446
});
449447
return response.ok<StreamResponseWithHeaders['body'] | StaticReturnType['body']>(result);
450448
};
@@ -671,23 +669,20 @@ export const isV2KnowledgeBaseEnabled = ({
671669
* Telemetry function to determine whether knowledge base has been installed
672670
* @param kbDataClient
673671
*/
674-
export const getIsKnowledgeBaseEnabled = async (
672+
export const getIsKnowledgeBaseInstalled = async (
675673
kbDataClient?: AIAssistantKnowledgeBaseDataClient | null
676-
): Promise<{
677-
esqlExists: boolean;
678-
isModelDeployed: boolean;
679-
}> => {
680-
let esqlExists = false;
674+
): Promise<boolean> => {
675+
let securityLabsDocsExist = false;
681676
let isModelDeployed = false;
682677
if (kbDataClient != null) {
683678
try {
684679
isModelDeployed = await kbDataClient.isModelDeployed();
685680
if (isModelDeployed) {
686-
esqlExists =
681+
securityLabsDocsExist =
687682
(
688683
await kbDataClient.getKnowledgeBaseDocumentEntries({
689-
query: ESQL_DOCS_LOADED_QUERY,
690-
required: true,
684+
kbResource: SECURITY_LABS_RESOURCE,
685+
query: SECURITY_LABS_LOADED_QUERY,
691686
})
692687
).length > 0;
693688
}
@@ -696,8 +691,5 @@ export const getIsKnowledgeBaseEnabled = async (
696691
}
697692
}
698693

699-
return {
700-
esqlExists,
701-
isModelDeployed,
702-
};
694+
return isModelDeployed && securityLabsDocsExist;
703695
};

x-pack/plugins/elastic_assistant/server/routes/knowledge_base/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ export const ESQL_DOCS_LOADED_QUERY =
1313
'You can chain processing commands, separated by a pipe character: `|`.';
1414
export const SECURITY_LABS_RESOURCE = 'security_labs';
1515
export const USER_RESOURCE = 'user';
16+
// Query for determining if Security Labs docs have been loaded. Intended for use with Telemetry
17+
export const SECURITY_LABS_LOADED_QUERY = 'What is Elastic Security Labs';

x-pack/plugins/elastic_assistant/server/routes/post_actions_connector_execute.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jest.mock('./helpers', () => {
5151

5252
return {
5353
...original,
54-
getIsKnowledgeBaseEnabled: jest.fn(),
54+
getIsKnowledgeBaseInstalled: jest.fn(),
5555
appendAssistantMessageToConversation: jest.fn(),
5656
langChainExecute: jest.fn(),
5757
getPluginNameFromRequest: jest.fn(),

x-pack/plugins/elastic_assistant/server/routes/post_actions_connector_execute.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { ElasticAssistantRequestHandlerContext, GetElser } from '../types';
2424
import {
2525
appendAssistantMessageToConversation,
2626
DEFAULT_PLUGIN_NAME,
27-
getIsKnowledgeBaseEnabled,
27+
getIsKnowledgeBaseInstalled,
2828
getPluginNameFromRequest,
2929
getSystemPromptFromUserConversation,
3030
langChainExecute,
@@ -170,14 +170,13 @@ export const postActionsConnectorExecuteRoute = (
170170
(await assistantContext.getAIAssistantKnowledgeBaseDataClient({
171171
v2KnowledgeBaseEnabled,
172172
})) ?? undefined;
173-
const isEnabledKnowledgeBase = await getIsKnowledgeBaseEnabled(kbDataClient);
174-
173+
const isKnowledgeBaseInstalled = await getIsKnowledgeBaseInstalled(kbDataClient);
175174
telemetry.reportEvent(INVOKE_ASSISTANT_ERROR_EVENT.eventType, {
176175
actionTypeId: request.body.actionTypeId,
177176
model: request.body.model,
178177
errorMessage: error.message,
179178
assistantStreamingEnabled: request.body.subAction !== 'invokeAI',
180-
isEnabledKnowledgeBase,
179+
isEnabledKnowledgeBase: isKnowledgeBaseInstalled,
181180
});
182181

183182
return resp.error({

0 commit comments

Comments
 (0)