-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Security AI Assistant] Persist prompts #187040
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
Merged
YulNaumenko
merged 32 commits into
elastic:main
from
YulNaumenko:security-ai-assistant-prompt-store
Jul 3, 2024
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
f80b152
[Security AI Assistant] Persist prompts
YulNaumenko 4883dd8
Merge remote-tracking branch 'upstream/main' into security-ai-assista…
YulNaumenko 5019292
fixed merge
YulNaumenko 492aa25
fixed type checks
YulNaumenko 5ef4eb1
fixed create new
YulNaumenko bb2ce24
fixed tests
YulNaumenko 3aec593
fixed tests
YulNaumenko 45c33e8
fixed delete
YulNaumenko cb8d7fc
fixed types
YulNaumenko 2c6c32c
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine a71e190
fixed prompts update
YulNaumenko 875c129
Merge branch 'security-ai-assistant-prompt-store' of github.com:YulNa…
YulNaumenko 9ae6ee8
added default conversations
YulNaumenko 883d9a9
fixed unused labels
YulNaumenko a2a7fb6
fixed update on save
YulNaumenko e43b4d5
fixed delete
YulNaumenko 17d7d81
fixed due to comments
YulNaumenko 81534c6
-
YulNaumenko 4c993d8
fixed tests
YulNaumenko 09c97f0
fixed default prompt
YulNaumenko 23b1b85
Merge branch 'main' into security-ai-assistant-prompt-store
YulNaumenko 75af95f
fixed test and stack management page
YulNaumenko 750d30f
Merge branch 'security-ai-assistant-prompt-store' of github.com:YulNa…
YulNaumenko e26b0c5
fixed test
YulNaumenko a282e2d
Merge branch 'main' into security-ai-assistant-prompt-store
YulNaumenko 9be7b85
Merge branch 'main' into security-ai-assistant-prompt-store
YulNaumenko d3cdac3
Merge branch 'main' into security-ai-assistant-prompt-store
patrykkopycinski 8af1bc3
Merge branch 'main' into security-ai-assistant-prompt-store
YulNaumenko 8abfbd8
Merge branch 'main' into security-ai-assistant-prompt-store
YulNaumenko e87383e
fix Error fetching prompts toast
patrykkopycinski 3f700ad
Merge branch 'security-ai-assistant-prompt-store' of github.com:YulNa…
patrykkopycinski 05d97d4
added license check
YulNaumenko 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
137 changes: 137 additions & 0 deletions
137
x-pack/packages/kbn-elastic-assistant/impl/assistant/api/prompts/bulk_update_prompts.test.ts
This file contains hidden or 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,137 @@ | ||
| /* | ||
| * 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 { | ||
| API_VERSIONS, | ||
| ELASTIC_AI_ASSISTANT_PROMPTS_URL_BULK_ACTION, | ||
| } from '@kbn/elastic-assistant-common'; | ||
| import { httpServiceMock } from '@kbn/core-http-browser-mocks'; | ||
| import { IToasts } from '@kbn/core-notifications-browser'; | ||
| import { bulkUpdatePrompts } from './bulk_update_prompts'; | ||
| import { PromptTypeEnum } from '@kbn/elastic-assistant-common/impl/schemas/prompts/bulk_crud_prompts_route.gen'; | ||
|
|
||
| const prompt1 = { | ||
| id: 'field1', | ||
| content: 'Prompt 1', | ||
| name: 'test', | ||
| promptType: PromptTypeEnum.system, | ||
| }; | ||
| const prompt2 = { | ||
| ...prompt1, | ||
| id: 'field2', | ||
| content: 'Prompt 2', | ||
| name: 'test2', | ||
| promptType: PromptTypeEnum.system, | ||
| }; | ||
| const toasts = { | ||
| addError: jest.fn(), | ||
| }; | ||
| describe('bulkUpdatePrompts', () => { | ||
| let httpMock: ReturnType<typeof httpServiceMock.createSetupContract>; | ||
|
|
||
| beforeEach(() => { | ||
| httpMock = httpServiceMock.createSetupContract(); | ||
|
|
||
| jest.clearAllMocks(); | ||
| }); | ||
| it('should send a POST request with the correct parameters and receive a successful response', async () => { | ||
| const promptsActions = { | ||
| create: [], | ||
| update: [], | ||
| delete: { ids: [] }, | ||
| }; | ||
|
|
||
| await bulkUpdatePrompts(httpMock, promptsActions); | ||
|
|
||
| expect(httpMock.fetch).toHaveBeenCalledWith(ELASTIC_AI_ASSISTANT_PROMPTS_URL_BULK_ACTION, { | ||
| method: 'POST', | ||
| version: API_VERSIONS.internal.v1, | ||
| body: JSON.stringify({ | ||
| create: [], | ||
| update: [], | ||
| delete: { ids: [] }, | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it('should transform the prompts dictionary to an array of fields to create', async () => { | ||
| const promptsActions = { | ||
| create: [prompt1, prompt2], | ||
| update: [], | ||
| delete: { ids: [] }, | ||
| }; | ||
|
|
||
| await bulkUpdatePrompts(httpMock, promptsActions); | ||
|
|
||
| expect(httpMock.fetch).toHaveBeenCalledWith(ELASTIC_AI_ASSISTANT_PROMPTS_URL_BULK_ACTION, { | ||
| method: 'POST', | ||
| version: API_VERSIONS.internal.v1, | ||
| body: JSON.stringify({ | ||
| create: [prompt1, prompt2], | ||
| update: [], | ||
| delete: { ids: [] }, | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it('should transform the prompts dictionary to an array of fields to update', async () => { | ||
| const promptsActions = { | ||
| update: [prompt1, prompt2], | ||
| delete: { ids: [] }, | ||
| }; | ||
|
|
||
| await bulkUpdatePrompts(httpMock, promptsActions); | ||
|
|
||
| expect(httpMock.fetch).toHaveBeenCalledWith(ELASTIC_AI_ASSISTANT_PROMPTS_URL_BULK_ACTION, { | ||
| method: 'POST', | ||
| version: API_VERSIONS.internal.v1, | ||
| body: JSON.stringify({ | ||
| update: [prompt1, prompt2], | ||
| delete: { ids: [] }, | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw an error with the correct message when receiving an unsuccessful response', async () => { | ||
| httpMock.fetch.mockResolvedValue({ | ||
| success: false, | ||
| attributes: { | ||
| errors: [ | ||
| { | ||
| statusCode: 400, | ||
| message: 'Error updating prompt', | ||
| prompts: [{ id: prompt1.id, name: prompt1.content }], | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| const promptsActions = { | ||
| create: [], | ||
| update: [prompt1], | ||
| delete: { ids: [] }, | ||
| }; | ||
| await bulkUpdatePrompts(httpMock, promptsActions, toasts as unknown as IToasts); | ||
| expect(toasts.addError.mock.calls[0][0]).toEqual( | ||
| new Error('Error message: Error updating prompt for prompt Prompt 1') | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle cases where result.attributes.errors is undefined', async () => { | ||
| httpMock.fetch.mockResolvedValue({ | ||
| success: false, | ||
| attributes: {}, | ||
| }); | ||
| const promptsActions = { | ||
| create: [], | ||
| update: [], | ||
| delete: { ids: [] }, | ||
| }; | ||
|
|
||
| await bulkUpdatePrompts(httpMock, promptsActions, toasts as unknown as IToasts); | ||
| expect(toasts.addError.mock.calls[0][0]).toEqual(new Error('')); | ||
| }); | ||
| }); |
54 changes: 54 additions & 0 deletions
54
x-pack/packages/kbn-elastic-assistant/impl/assistant/api/prompts/bulk_update_prompts.ts
This file contains hidden or 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,54 @@ | ||
| /* | ||
| * 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'; | ||
| import { HttpSetup, IToasts } from '@kbn/core/public'; | ||
| import { | ||
| ELASTIC_AI_ASSISTANT_PROMPTS_URL_BULK_ACTION, | ||
| API_VERSIONS, | ||
| } from '@kbn/elastic-assistant-common'; | ||
| import { | ||
| PerformBulkActionRequestBody, | ||
| PerformBulkActionResponse, | ||
| } from '@kbn/elastic-assistant-common/impl/schemas/prompts/bulk_crud_prompts_route.gen'; | ||
|
|
||
| export const bulkUpdatePrompts = async ( | ||
| http: HttpSetup, | ||
| prompts: PerformBulkActionRequestBody, | ||
| toasts?: IToasts | ||
| ) => { | ||
| try { | ||
| const result = await http.fetch<PerformBulkActionResponse>( | ||
| ELASTIC_AI_ASSISTANT_PROMPTS_URL_BULK_ACTION, | ||
| { | ||
| method: 'POST', | ||
| version: API_VERSIONS.internal.v1, | ||
| body: JSON.stringify(prompts), | ||
| } | ||
| ); | ||
|
|
||
| if (!result.success) { | ||
| const serverError = result.attributes.errors | ||
| ?.map( | ||
| (e) => | ||
| `${e.status_code ? `Error code: ${e.status_code}. ` : ''}Error message: ${ | ||
| e.message | ||
| } for prompt ${e.prompts.map((c) => c.name).join(',')}` | ||
| ) | ||
| .join(',\n'); | ||
| throw new Error(serverError); | ||
| } | ||
| return result; | ||
| } catch (error) { | ||
| toasts?.addError(error.body && error.body.message ? new Error(error.body.message) : error, { | ||
| title: i18n.translate('xpack.elasticAssistant.prompts.bulkActionspromptsError', { | ||
| defaultMessage: 'Error updating prompts {error}', | ||
| values: { error }, | ||
| }), | ||
| }); | ||
| } | ||
| }; |
9 changes: 9 additions & 0 deletions
9
x-pack/packages/kbn-elastic-assistant/impl/assistant/api/prompts/index.tsx
This file contains hidden or 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,9 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| export * from './bulk_update_prompts'; | ||
| export * from './use_fetch_prompts'; |
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.
should this be enum?
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.
his value will come from the Kibana
applications.currentAppId$, which is dynamic and we should be flexible enough to supporting new apps