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] Fix editing prompt from contextual insights #206673

Merged
merged 6 commits into from
Jan 16, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
EuiText,
EuiTextArea,
EuiCallOut,
EuiLoadingSpinner,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { cloneDeep, isArray, isEmpty, last, once } from 'lodash';
Expand Down Expand Up @@ -288,16 +289,47 @@ export function Insight({
[service]
);

const getPromptToEdit = () => {
const clonedMessages = cloneDeep(messages.messages);
const lastUserPrompt = getLastMessageOfType(clonedMessages, MessageRole.User)?.message.content;

if (!lastUserPrompt) {
return '';
}

try {
const { instructions = '' } = JSON.parse(lastUserPrompt);
return instructions.trim();
} catch (e) {
return '';
}
};

const onEditPrompt = (newPrompt: string) => {
const clonedMessages = cloneDeep(messages.messages);
const userMessage = getLastMessageOfType(clonedMessages, MessageRole.User);
if (!userMessage) return false;

userMessage.message.content = newPrompt;
setIsPromptUpdated(true);
setMessages({ messages: clonedMessages, status: FETCH_STATUS.SUCCESS });
setEditingPrompt(false);
return true;
try {
const parsedContent = JSON.parse(userMessage.message.content || '');

if (!parsedContent.instructions) {
return false;
}

// Assign the updated instructions
parsedContent.instructions = newPrompt;
userMessage.message.content = JSON.stringify(parsedContent);

setIsPromptUpdated(true);
setMessages({ messages: clonedMessages, status: FETCH_STATUS.SUCCESS });
setEditingPrompt(false);
return true;
} catch (e) {
// eslint-disable-next-line no-console
console.error('Failed to edit prompt:', e);
return false;
}
};

const handleCancel = () => {
Expand Down Expand Up @@ -372,15 +404,15 @@ export function Insight({
</>
);
} else if (isEditingPrompt) {
children = (
<PromptEdit
initialPrompt={
getLastMessageOfType(messages.messages, MessageRole.User)?.message.content || ''
}
onSend={onEditPrompt}
onCancel={handleCancel}
/>
);
const promptToEdit = getPromptToEdit();

if (messages.status === FETCH_STATUS.SUCCESS && promptToEdit) {
children = (
<PromptEdit initialPrompt={promptToEdit} onSend={onEditPrompt} onCancel={handleCancel} />
);
} else {
children = <EuiLoadingSpinner size="m" />;
}
} else if (!connectors.loading && !connectors.connectors?.length) {
children = (
<MissingCredentialsCallout connectorsManagementHref={getConnectorsManagementHref(http!)} />
Expand Down
Loading