Skip to content
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

[Obs AI Assistant] Pass function responses when copying conversation #195635

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion x-pack/packages/kbn-ai-assistant/src/chat/chat_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { SimulatedFunctionCallingCallout } from './simulated_function_calling_ca
import { WelcomeMessage } from './welcome_message';
import { useLicense } from '../hooks/use_license';
import { PromptEditor } from '../prompt_editor/prompt_editor';
import { deserializeMessage } from '../utils/deserialize_message';

const fullHeightClassName = css`
height: 100%;
Expand Down Expand Up @@ -226,9 +227,11 @@ export function ChatBody({
});

const handleCopyConversation = () => {
const deserializedMessages = (conversation.value?.messages ?? messages).map(deserializeMessage);

const content = JSON.stringify({
title: initialTitle,
messages: conversation.value?.messages ?? messages,
messages: deserializedMessages,
});

navigator.clipboard?.writeText(content || '');
Expand Down
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('');
});
});
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;
};