-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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 fix/report_flyout_wordwrap
- Loading branch information
Showing
66 changed files
with
703 additions
and
214 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
118 changes: 118 additions & 0 deletions
118
x-pack/packages/kbn-ai-assistant/src/utils/deserialize_message.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,118 @@ | ||
/* | ||
* 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 { cloneDeep } from 'lodash'; | ||
import { Message, MessageRole } from '@kbn/observability-ai-assistant-plugin/common'; | ||
import { deserializeMessage } from './deserialize_message'; | ||
import { safeJsonParse } from './safe_json_parse'; | ||
|
||
jest.mock('lodash', () => ({ | ||
cloneDeep: jest.fn(), | ||
})); | ||
|
||
jest.mock('./safe_json_parse', () => ({ | ||
safeJsonParse: jest.fn((value) => { | ||
try { | ||
return JSON.parse(value); | ||
} catch { | ||
return value; | ||
} | ||
}), | ||
})); | ||
|
||
describe('deserializeMessage', () => { | ||
const baseMessage: Message = { | ||
'@timestamp': '2024-10-15T00:00:00Z', | ||
message: { | ||
role: MessageRole.User, | ||
content: 'This is a message', | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
(cloneDeep as jest.Mock).mockImplementation((obj) => JSON.parse(JSON.stringify(obj))); | ||
}); | ||
|
||
it('should clone the original message', () => { | ||
const message = { ...baseMessage }; | ||
deserializeMessage(message); | ||
|
||
expect(cloneDeep).toHaveBeenCalledWith(message); | ||
}); | ||
|
||
it('should deserialize function_call.arguments if it is a string', () => { | ||
const messageWithFunctionCall: Message = { | ||
...baseMessage, | ||
message: { | ||
...baseMessage.message, | ||
function_call: { | ||
name: 'testFunction', | ||
arguments: '{"key": "value"}', | ||
trigger: MessageRole.Assistant, | ||
}, | ||
}, | ||
}; | ||
|
||
const result = deserializeMessage(messageWithFunctionCall); | ||
|
||
expect(safeJsonParse).toHaveBeenCalledWith('{"key": "value"}'); | ||
expect(result.message.function_call!.arguments).toEqual({ key: 'value' }); | ||
}); | ||
|
||
it('should deserialize message.content if it is a string', () => { | ||
const messageWithContent: Message = { | ||
...baseMessage, | ||
message: { | ||
...baseMessage.message, | ||
name: 'testMessage', | ||
content: '{"key": "value"}', | ||
}, | ||
}; | ||
|
||
const result = deserializeMessage(messageWithContent); | ||
|
||
expect(safeJsonParse).toHaveBeenCalledWith('{"key": "value"}'); | ||
expect(result.message.content).toEqual({ key: 'value' }); | ||
}); | ||
|
||
it('should deserialize message.data if it is a string', () => { | ||
const messageWithData: Message = { | ||
...baseMessage, | ||
message: { | ||
...baseMessage.message, | ||
name: 'testMessage', | ||
data: '{"key": "value"}', | ||
}, | ||
}; | ||
|
||
const result = deserializeMessage(messageWithData); | ||
|
||
expect(safeJsonParse).toHaveBeenCalledWith('{"key": "value"}'); | ||
expect(result.message.data).toEqual({ key: 'value' }); | ||
}); | ||
|
||
it('should return the copied message as is if no deserialization is needed', () => { | ||
const messageWithoutSerialization: Message = { | ||
...baseMessage, | ||
message: { | ||
...baseMessage.message, | ||
function_call: { | ||
name: 'testFunction', | ||
arguments: '', | ||
trigger: MessageRole.Assistant, | ||
}, | ||
content: '', | ||
}, | ||
}; | ||
|
||
const result = deserializeMessage(messageWithoutSerialization); | ||
|
||
expect(result.message.function_call!.name).toEqual('testFunction'); | ||
expect(result.message.function_call!.arguments).toEqual(''); | ||
expect(result.message.content).toEqual(''); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
x-pack/packages/kbn-ai-assistant/src/utils/deserialize_message.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,35 @@ | ||
/* | ||
* 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 { cloneDeep } from 'lodash'; | ||
import type { Message } from '@kbn/observability-ai-assistant-plugin/common'; | ||
import { safeJsonParse } from './safe_json_parse'; | ||
|
||
export const deserializeMessage = (message: Message): Message => { | ||
const copiedMessage = cloneDeep(message); | ||
|
||
if ( | ||
copiedMessage.message.function_call?.arguments && | ||
typeof copiedMessage.message.function_call?.arguments === 'string' | ||
) { | ||
copiedMessage.message.function_call.arguments = safeJsonParse( | ||
copiedMessage.message.function_call.arguments ?? '{}' | ||
); | ||
} | ||
|
||
if (copiedMessage.message.name) { | ||
if (copiedMessage.message.content && typeof copiedMessage.message.content === 'string') { | ||
copiedMessage.message.content = safeJsonParse(copiedMessage.message.content); | ||
} | ||
|
||
if (copiedMessage.message.data && typeof copiedMessage.message.data === 'string') { | ||
copiedMessage.message.data = safeJsonParse(copiedMessage.message.data); | ||
} | ||
} | ||
|
||
return copiedMessage; | ||
}; |
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/actions/common/routes/connector/apis/execute/index.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,21 @@ | ||
/* | ||
* 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 { | ||
executeConnectorRequestParamsSchema, | ||
executeConnectorRequestBodySchema, | ||
} from './schemas/latest'; | ||
export type { ExecuteConnectorRequestParams, ExecuteConnectorRequestBody } from './types/latest'; | ||
|
||
export { | ||
executeConnectorRequestParamsSchema as executeConnectorRequestParamsSchemaV1, | ||
executeConnectorRequestBodySchema as executeConnectorRequestBodySchemaV1, | ||
} from './schemas/v1'; | ||
export type { | ||
ExecuteConnectorRequestParams as ExecuteConnectorRequestParamsV1, | ||
ExecuteConnectorRequestBody as ExecuteConnectorRequestBodyV1, | ||
} from './types/v1'; |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/actions/common/routes/connector/apis/execute/schemas/latest.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,8 @@ | ||
/* | ||
* 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 './v1'; |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/actions/common/routes/connector/apis/execute/schemas/v1.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,20 @@ | ||
/* | ||
* 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 { schema } from '@kbn/config-schema'; | ||
|
||
export const executeConnectorRequestParamsSchema = schema.object({ | ||
id: schema.string({ | ||
meta: { | ||
description: 'An identifier for the connector.', | ||
}, | ||
}), | ||
}); | ||
|
||
export const executeConnectorRequestBodySchema = schema.object({ | ||
params: schema.recordOf(schema.string(), schema.any()), | ||
}); |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/actions/common/routes/connector/apis/execute/types/latest.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,8 @@ | ||
/* | ||
* 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 './v1'; |
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/actions/common/routes/connector/apis/execute/types/v1.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,12 @@ | ||
/* | ||
* 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 type { TypeOf } from '@kbn/config-schema'; | ||
import { executeConnectorRequestParamsSchemaV1, executeConnectorRequestBodySchemaV1 } from '..'; | ||
|
||
export type ExecuteConnectorRequestParams = TypeOf<typeof executeConnectorRequestParamsSchemaV1>; | ||
export type ExecuteConnectorRequestBody = TypeOf<typeof executeConnectorRequestBodySchemaV1>; |
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
Oops, something went wrong.