-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into api-test-for-datafeed-preview
- Loading branch information
Showing
117 changed files
with
1,741 additions
and
725 deletions.
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
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 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 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 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.