Skip to content

Commit 1214180

Browse files
KDKHDkibanamachine
andauthored
[Security Solution] [AI assistant] Make Security AI assistant tools available even when the chat completion endpoint is called from outside of security. (#226206)
## Summary Summarize your PR. If it involves visual changes include a screenshot or gif. This PR makes AI assistant tools registered by the security solution available when the assistant is invoked by other plugins. This is part of a larger effort to make the Security AI assistant globally accessible. ### How to test Code only review is fine. ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [X] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [X] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [X] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [X] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [X] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [X] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [X] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [X] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ... --------- Co-authored-by: kibanamachine <[email protected]>
1 parent 10a540b commit 1214180

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)