-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security Solution][Detection Engine] adds AI Assistant to rule create form #179091
Merged
vitaliidm
merged 56 commits into
elastic:main
from
vitaliidm:de_on_week/esql-ai-assistant
Jul 1, 2024
Merged
Changes from 43 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
7bfbcbb
[Security Solution][Detection Engine] adds AI Assistant to rule creat…
vitaliidm ac2a20f
adjust assistant for the rest of queries
vitaliidm 8c16a97
tune ES|QL validation error
vitaliidm 4c9e26d
fix formatting
vitaliidm 3626023
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm ee1c0cb
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 5f82c6b
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 496a7d4
remove unused translations
vitaliidm 990d9b3
fix cypress test
vitaliidm 49a7a8d
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 162b268
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm af60f98
show AI assistant only when query is invalid
vitaliidm c99a98b
Merge branch 'de_on_week/esql-ai-assistant' of https://github.com/vit…
vitaliidm 07774cc
WIP assistant v1
vitaliidm 3b6f0f7
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm dc13ca1
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 3874c80
refactoring
vitaliidm 86bf7ac
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 09a8a95
refactoring
vitaliidm bd6eee6
Merge branch 'de_on_week/esql-ai-assistant' of https://github.com/vit…
vitaliidm 02a7351
add tests
vitaliidm 82274c1
add more tests
vitaliidm 0e868aa
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 43f4fb1
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm cf200d1
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 323e1a3
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 469e4d7
fix typings after merge conflicts resolve
vitaliidm 672a58e
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 91739fc
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm c07ed57
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 46b03bc
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 9d23eb1
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm be86744
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 12d16cb
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 6e27b70
Merge branch 'main' into de_on_week/esql-ai-assistant
kibanamachine 70f66c9
Merge branch 'main' into de_on_week/esql-ai-assistant
kibanamachine 29dab89
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm b5d4156
CR feedback
vitaliidm 8801a3a
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 971c2d2
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 6b7f8dd
add Featuire Flag
vitaliidm a8c2433
Merge branch 'de_on_week/esql-ai-assistant' of https://github.com/vit…
vitaliidm 4d94465
enable for CI testing
vitaliidm 6a16b79
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm d4a4a96
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 34ad029
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 338933c
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm be358cb
CR
vitaliidm 0472dac
Merge branch 'de_on_week/esql-ai-assistant' of https://github.com/vit…
vitaliidm 2a86962
redeploy
vitaliidm 4b11f56
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm e33f11e
fix user prompt
vitaliidm 0b205a0
Update experimental_features.ts
vitaliidm 8b981f9
fix jest test
vitaliidm 1d6f7f7
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm 800a1e5
Merge branch 'main' into de_on_week/esql-ai-assistant
vitaliidm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
..._solution/public/detection_engine/rule_creation_ui/components/ai_assistant/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { screen, render } from '@testing-library/react'; | ||
|
||
import { TestProviders } from '../../../../common/mock'; | ||
import { useAssistantAvailability } from '../../../../assistant/use_assistant_availability'; | ||
|
||
import { AiAssistant } from '.'; | ||
|
||
jest.mock('../../../../assistant/use_assistant_availability', () => ({ | ||
useAssistantAvailability: jest.fn(), | ||
})); | ||
|
||
const useAssistantAvailabilityMock = useAssistantAvailability as jest.Mock; | ||
|
||
describe('AiAssistant', () => { | ||
beforeEach(() => { | ||
useAssistantAvailabilityMock.mockReturnValue({ hasAssistantPrivilege: true }); | ||
}); | ||
it('does not render chat component when does not have hasAssistantPrivilege', () => { | ||
useAssistantAvailabilityMock.mockReturnValue({ hasAssistantPrivilege: false }); | ||
|
||
const { container } = render(<AiAssistant getFields={jest.fn()} />, { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
it('renders chat component when has hasAssistantPrivilege', () => { | ||
render(<AiAssistant getFields={jest.fn()} />, { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
expect(screen.getByTestId('newChatLink')).toBeInTheDocument(); | ||
}); | ||
}); |
91 changes: 91 additions & 0 deletions
91
...urity_solution/public/detection_engine/rule_creation_ui/components/ai_assistant/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useCallback } from 'react'; | ||
import { EuiSpacer } from '@elastic/eui'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
|
||
import { NewChat, AssistantAvatar } from '@kbn/elastic-assistant'; | ||
|
||
import { METRIC_TYPE, TELEMETRY_EVENT, track } from '../../../../common/lib/telemetry'; | ||
import { useAssistantAvailability } from '../../../../assistant/use_assistant_availability'; | ||
import * as i18nAssistant from '../../../../detections/pages/detection_engine/rules/translations'; | ||
import type { DefineStepRule } from '../../../../detections/pages/detection_engine/rules/types'; | ||
import type { FormHook, ValidationError } from '../../../../shared_imports'; | ||
|
||
import * as i18n from './translations'; | ||
|
||
interface AiAssistantProps { | ||
getFields: FormHook<DefineStepRule>['getFields']; | ||
} | ||
|
||
const retrieveErrorMessages = (errors: ValidationError[]): string => | ||
errors | ||
.flatMap(({ message, messages }) => [message, ...(Array.isArray(messages) ? messages : [])]) | ||
.join(', '); | ||
|
||
const AiAssistantComponent: React.FC<AiAssistantProps> = ({ getFields }) => { | ||
const { hasAssistantPrivilege, isAssistantEnabled } = useAssistantAvailability(); | ||
|
||
const getPromptContext = useCallback(async () => { | ||
const queryField = getFields().queryBar; | ||
const { query, language } = (queryField.value as DefineStepRule['queryBar']).query; | ||
|
||
if (!query) { | ||
return ''; | ||
} | ||
|
||
if (queryField.errors.length === 0) { | ||
return `No errors in ${language} language query detected. Current query: ${query.trim()}`; | ||
} | ||
|
||
return `${language} language query written for Elastic Security Detection rules: \"${query.trim()}\" | ||
returns validation error on form: \"${retrieveErrorMessages(queryField.errors)}\" | ||
Fix ${language} language query and give an example of it in markdown format that can be copied. | ||
Proposed solution should be valid and must not contain new line symbols (\\n)`; | ||
}, [getFields]); | ||
|
||
const onShowOverlay = useCallback(() => { | ||
track(METRIC_TYPE.COUNT, TELEMETRY_EVENT.OPEN_ASSISTANT_ON_RULE_QUERY_ERROR); | ||
}, []); | ||
|
||
if (!hasAssistantPrivilege) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<EuiSpacer size="s" /> | ||
|
||
<FormattedMessage | ||
id="xpack.securitySolution.detectionEngine.createRule.stepDefineRule.askAssistantHelpText" | ||
defaultMessage="{AiAssistantNewChatLink} to help resolve this error." | ||
values={{ | ||
AiAssistantNewChatLink: ( | ||
<NewChat | ||
asLink={true} | ||
category="detection-rules" | ||
conversationId={i18nAssistant.DETECTION_RULES_CONVERSATION_ID} | ||
description={i18n.ASK_ASSISTANT_DESCRIPTION} | ||
getPromptContext={getPromptContext} | ||
suggestedUserPrompt={i18n.ASK_ASSISTANT_USER_PROMPT} | ||
tooltip={i18n.ASK_ASSISTANT_TOOLTIP} | ||
iconType={null} | ||
onShowOverlay={onShowOverlay} | ||
isAssistantEnabled={isAssistantEnabled} | ||
> | ||
<AssistantAvatar size="xxs" /> {i18n.ASK_ASSISTANT_ERROR_BUTTON} | ||
</NewChat> | ||
), | ||
}} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export const AiAssistant = React.memo(AiAssistantComponent); | ||
AiAssistant.displayName = 'AiAssistant'; |
36 changes: 36 additions & 0 deletions
36
...solution/public/detection_engine/rule_creation_ui/components/ai_assistant/translations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
|
||
export const ASK_ASSISTANT_ERROR_BUTTON = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.createRule.stepDefineRule.askAssistant', | ||
{ | ||
defaultMessage: 'Ask Assistant', | ||
} | ||
); | ||
|
||
export const ASK_ASSISTANT_DESCRIPTION = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.createRule.stepDefineRule.askAssistantDesc', | ||
{ | ||
defaultMessage: 'Rule query error', | ||
} | ||
); | ||
|
||
export const ASK_ASSISTANT_USER_PROMPT = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.createRule.stepDefineRule.askAssistantUserPrompt', | ||
{ | ||
defaultMessage: 'Fix errors in query', | ||
} | ||
); | ||
|
||
export const ASK_ASSISTANT_TOOLTIP = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.createRule.stepDefineRule.askAssistantToolTip', | ||
{ | ||
defaultMessage: 'Fix query or generate new one', | ||
} | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disable before merge. Needed for CI deployment to be testable