Skip to content

Commit 268b5e6

Browse files
kibanamachineKDKHD
andauthored
[9.1] [Security Solution] [AI assistant] Make Security AI assistant tools available even when the chat completion endpoint is called from outside of security. (#226206) (#226444)
# Backport This will backport the following commits from `main` to `9.1`: - [[Security Solution] [AI assistant] Make Security AI assistant tools available even when the chat completion endpoint is called from outside of security. (#226206)](#226206) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Kenneth Kreindler","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-07-03T14:13:35Z","message":"[Security Solution] [AI assistant] Make Security AI assistant tools available even when the chat completion endpoint is called from outside of security. (#226206)\n\n## Summary\n\nSummarize your PR. If it involves visual changes include a screenshot or\ngif.\n\nThis PR makes AI assistant tools registered by the security solution\navailable when the assistant is invoked by other plugins. This is part\nof a larger effort to make the Security AI assistant globally\naccessible.\n\n### How to test\nCode only review is fine. \n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [X] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [X]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [X] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [X] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [X] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [X] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [X] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [X] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: kibanamachine <[email protected]>","sha":"121418052ffe9f877d2dfc66f0b48a33a0c87f19","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Security Generative AI","backport:version","v9.1.0","v8.19.0","v9.2.0"],"title":"[Security Solution] [AI assistant] Make Security AI assistant tools available even when the chat completion endpoint is called from outside of security.","number":226206,"url":"https://github.com/elastic/kibana/pull/226206","mergeCommit":{"message":"[Security Solution] [AI assistant] Make Security AI assistant tools available even when the chat completion endpoint is called from outside of security. (#226206)\n\n## Summary\n\nSummarize your PR. If it involves visual changes include a screenshot or\ngif.\n\nThis PR makes AI assistant tools registered by the security solution\navailable when the assistant is invoked by other plugins. This is part\nof a larger effort to make the Security AI assistant globally\naccessible.\n\n### How to test\nCode only review is fine. \n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [X] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [X]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [X] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [X] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [X] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [X] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [X] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [X] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: kibanamachine <[email protected]>","sha":"121418052ffe9f877d2dfc66f0b48a33a0c87f19"}},"sourceBranch":"main","suggestedTargetBranches":["9.1","8.19"],"targetPullRequestStates":[{"branch":"9.1","label":"v9.1.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/226206","number":226206,"mergeCommit":{"message":"[Security Solution] [AI assistant] Make Security AI assistant tools available even when the chat completion endpoint is called from outside of security. (#226206)\n\n## Summary\n\nSummarize your PR. If it involves visual changes include a screenshot or\ngif.\n\nThis PR makes AI assistant tools registered by the security solution\navailable when the assistant is invoked by other plugins. This is part\nof a larger effort to make the Security AI assistant globally\naccessible.\n\n### How to test\nCode only review is fine. \n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [X] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [X]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [X] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [X] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [X] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [X] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [X] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [X] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: kibanamachine <[email protected]>","sha":"121418052ffe9f877d2dfc66f0b48a33a0c87f19"}}]}] BACKPORT--> Co-authored-by: Kenneth Kreindler <[email protected]>
1 parent 84db917 commit 268b5e6

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

x-pack/solutions/security/plugins/elastic_assistant/server/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export class ElasticAssistantPlugin
194194
getRegisteredFeatures: (pluginName: string) => {
195195
return appContextService.getRegisteredFeatures(pluginName);
196196
},
197-
getRegisteredTools: (pluginName: string) => {
197+
getRegisteredTools: (pluginName: string | string[]) => {
198198
return appContextService.getRegisteredTools(pluginName);
199199
},
200200
};
@@ -220,7 +220,7 @@ export class ElasticAssistantPlugin
220220
getRegisteredFeatures: (pluginName: string) => {
221221
return appContextService.getRegisteredFeatures(pluginName);
222222
},
223-
getRegisteredTools: (pluginName: string) => {
223+
getRegisteredTools: (pluginName: string | string[]) => {
224224
return appContextService.getRegisteredTools(pluginName);
225225
},
226226
registerFeatures: (pluginName: string, features: Partial<AssistantFeatures>) => {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ export const langChainExecute = async ({
293293
const assistantContext = context.elasticAssistant;
294294
// We don't (yet) support invoking these tools interactively
295295
const unsupportedTools = new Set(['attack-discovery', DEFEND_INSIGHTS_ID]);
296+
const pluginNames = Array.from(new Set([pluginName, DEFAULT_PLUGIN_NAME]));
297+
296298
const assistantTools = assistantContext
297-
.getRegisteredTools(pluginName)
299+
.getRegisteredTools(pluginNames)
298300
.filter((tool) => !unsupportedTools.has(tool.id));
299301

300302
// get a scoped esClient for assistant memory

x-pack/solutions/security/plugins/elastic_assistant/server/routes/request_context_factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class RequestContextFactory implements IRequestContextFactory {
107107

108108
getCurrentUser,
109109

110-
getRegisteredTools: (pluginName: string) => {
110+
getRegisteredTools: (pluginName: string | string[]) => {
111111
return appContextService.getRegisteredTools(pluginName);
112112
},
113113

x-pack/solutions/security/plugins/elastic_assistant/server/services/app_context.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ describe('AppContextService', () => {
3535
isSupported: jest.fn(),
3636
getTool: jest.fn(),
3737
};
38+
const toolThree: AssistantTool = {
39+
id: 'tool-three',
40+
name: 'ToolThree',
41+
description: 'Description 3',
42+
sourceRegister: 'Source3',
43+
isSupported: jest.fn(),
44+
getTool: jest.fn(),
45+
};
3846

3947
beforeEach(() => {
4048
appContextService.stop();
@@ -99,6 +107,17 @@ describe('AppContextService', () => {
99107
});
100108
});
101109

110+
it('get tools for multiple plugins', () => {
111+
const pluginName1 = 'pluginName1';
112+
const pluginName2 = 'pluginName2';
113+
114+
appContextService.start(mockAppContext);
115+
appContextService.registerTools(pluginName2, [toolOne, toolThree]);
116+
appContextService.registerTools(pluginName1, [toolOne]);
117+
118+
expect(appContextService.getRegisteredTools([pluginName1, pluginName2]).length).toEqual(2);
119+
});
120+
102121
describe('registering features', () => {
103122
it('should register and get features for a single plugin', () => {
104123
const pluginName = 'pluginName';

x-pack/solutions/security/plugins/elastic_assistant/server/services/app_context.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { AssistantTool } from '../types';
1212
export type PluginName = string;
1313
export type RegisteredToolsStorage = Map<PluginName, Set<AssistantTool>>;
1414
export type RegisteredFeaturesStorage = Map<PluginName, AssistantFeatures>;
15-
export type GetRegisteredTools = (pluginName: string) => AssistantTool[];
15+
export type GetRegisteredTools = (pluginName: string | string[]) => AssistantTool[];
1616
export type GetRegisteredFeatures = (pluginName: string) => AssistantFeatures;
1717
export interface ElasticAssistantAppContext {
1818
logger: Logger;
@@ -67,8 +67,16 @@ class AppContextService {
6767
*
6868
* @param pluginName
6969
*/
70-
public getRegisteredTools(pluginName: string): AssistantTool[] {
71-
const tools = Array.from(this.registeredTools?.get(pluginName) ?? new Set<AssistantTool>());
70+
public getRegisteredTools(pluginName: string | string[]): AssistantTool[] {
71+
const pluginNames = Array.isArray(pluginName) ? pluginName : [pluginName];
72+
73+
const tools = [
74+
...new Set(
75+
pluginNames
76+
.map((name) => this.registeredTools?.get(name) ?? new Set<AssistantTool>())
77+
.flatMap((set) => [...set])
78+
),
79+
];
7280

7381
this.logger?.debug('AppContextService:getRegisteredTools');
7482
this.logger?.debug(`pluginName: ${pluginName}`);

0 commit comments

Comments
 (0)