Skip to content

Commit

Permalink
[8.12] Tweaks to prompt editor (elastic#174030) (elastic#175664)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.12`:
- [Tweaks to prompt editor
(elastic#174030)](elastic#174030)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Coen
Warmer","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-01-08T15:59:07Z","message":"Tweaks
to prompt editor (elastic#174030)\n\n## Summary\r\n\r\nThis fixes the following
edge case when using the chat interface:\r\n\r\n* When the main editor
has a value in the text area, and the user edits\r\nan existing message,
and presses `<enter>` on the keyboard, the value\r\nthat was in the main
editor is appended as a new message, instead of\r\nediting the existing
message.\r\n\r\nIt also does a bit of cleanup (moving of
ChatPromptEditor components to\r\n`/components/prompt_editor`, and
renaming to `PromptEditor`.)\r\n\r\n### Additional fixes\r\n* [Don't
stick to bottom when changing to edit mode, re-stick to bottom\r\nwhen
done\r\nediting](https://github.com/elastic/kibana/pull/174030/commits/e8a01c1d4ddd449fdf85f0fef11de3d7cc9b2637)\r\n\r\n*
[Autofocus function popover list search box
upon\r\nopening](https://github.com/elastic/kibana/pull/174030/commits/2329d1c0a791716bf192b5e567c49336853edbd4)\r\n\r\n*
[Remove focus trap as it wasn't doing
anything\r\nanymore](https://github.com/elastic/kibana/pull/174030/commits/7fcb4e0b775a768c07b79c9c362f030c8f6036cb)\r\n\r\n*
[Move constants used when creating monaco model inside function
scope\r\nto avoid sharing of model between multiple
editor\r\ninstances](https://github.com/elastic/kibana/pull/174030/commits/c9cab2c15566a01f21342dcef66964c13574939d)\r\n\r\n*
[Disable submitting function editor when json is
not\r\nvalid](https://github.com/elastic/kibana/pull/174030/commits/2a6f1e1cfb2ee5f917ac4862b68aff02813341be)","sha":"f4265ca731be471481518a24794a3664a46774ff","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport
missing","backport:prev-minor","v8.12.0","v8.13.0"],"number":174030,"url":"https://github.com/elastic/kibana/pull/174030","mergeCommit":{"message":"Tweaks
to prompt editor (elastic#174030)\n\n## Summary\r\n\r\nThis fixes the following
edge case when using the chat interface:\r\n\r\n* When the main editor
has a value in the text area, and the user edits\r\nan existing message,
and presses `<enter>` on the keyboard, the value\r\nthat was in the main
editor is appended as a new message, instead of\r\nediting the existing
message.\r\n\r\nIt also does a bit of cleanup (moving of
ChatPromptEditor components to\r\n`/components/prompt_editor`, and
renaming to `PromptEditor`.)\r\n\r\n### Additional fixes\r\n* [Don't
stick to bottom when changing to edit mode, re-stick to bottom\r\nwhen
done\r\nediting](https://github.com/elastic/kibana/pull/174030/commits/e8a01c1d4ddd449fdf85f0fef11de3d7cc9b2637)\r\n\r\n*
[Autofocus function popover list search box
upon\r\nopening](https://github.com/elastic/kibana/pull/174030/commits/2329d1c0a791716bf192b5e567c49336853edbd4)\r\n\r\n*
[Remove focus trap as it wasn't doing
anything\r\nanymore](https://github.com/elastic/kibana/pull/174030/commits/7fcb4e0b775a768c07b79c9c362f030c8f6036cb)\r\n\r\n*
[Move constants used when creating monaco model inside function
scope\r\nto avoid sharing of model between multiple
editor\r\ninstances](https://github.com/elastic/kibana/pull/174030/commits/c9cab2c15566a01f21342dcef66964c13574939d)\r\n\r\n*
[Disable submitting function editor when json is
not\r\nvalid](https://github.com/elastic/kibana/pull/174030/commits/2a6f1e1cfb2ee5f917ac4862b68aff02813341be)","sha":"f4265ca731be471481518a24794a3664a46774ff"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/174030","number":174030,"mergeCommit":{"message":"Tweaks
to prompt editor (elastic#174030)\n\n## Summary\r\n\r\nThis fixes the following
edge case when using the chat interface:\r\n\r\n* When the main editor
has a value in the text area, and the user edits\r\nan existing message,
and presses `<enter>` on the keyboard, the value\r\nthat was in the main
editor is appended as a new message, instead of\r\nediting the existing
message.\r\n\r\nIt also does a bit of cleanup (moving of
ChatPromptEditor components to\r\n`/components/prompt_editor`, and
renaming to `PromptEditor`.)\r\n\r\n### Additional fixes\r\n* [Don't
stick to bottom when changing to edit mode, re-stick to bottom\r\nwhen
done\r\nediting](https://github.com/elastic/kibana/pull/174030/commits/e8a01c1d4ddd449fdf85f0fef11de3d7cc9b2637)\r\n\r\n*
[Autofocus function popover list search box
upon\r\nopening](https://github.com/elastic/kibana/pull/174030/commits/2329d1c0a791716bf192b5e567c49336853edbd4)\r\n\r\n*
[Remove focus trap as it wasn't doing
anything\r\nanymore](https://github.com/elastic/kibana/pull/174030/commits/7fcb4e0b775a768c07b79c9c362f030c8f6036cb)\r\n\r\n*
[Move constants used when creating monaco model inside function
scope\r\nto avoid sharing of model between multiple
editor\r\ninstances](https://github.com/elastic/kibana/pull/174030/commits/c9cab2c15566a01f21342dcef66964c13574939d)\r\n\r\n*
[Disable submitting function editor when json is
not\r\nvalid](https://github.com/elastic/kibana/pull/174030/commits/2a6f1e1cfb2ee5f917ac4862b68aff02813341be)","sha":"f4265ca731be471481518a24794a3664a46774ff"}}]}]
BACKPORT-->

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
CoenWarmer and kibanamachine authored Jan 26, 2024
1 parent f51584c commit 9419256
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type { UseGenAIConnectorsResult } from '../../hooks/use_genai_connectors'
import type { UseKnowledgeBaseResult } from '../../hooks/use_knowledge_base';
import { type Conversation, type Message, MessageRole } from '../../../common/types';
import { ChatHeader } from './chat_header';
import { ChatPromptEditor } from './chat_prompt_editor';
import { PromptEditor } from '../prompt_editor/prompt_editor';
import { ChatTimeline } from './chat_timeline';
import { Feedback } from '../feedback_buttons';
import { IncorrectLicensePanel } from './incorrect_license_panel';
Expand Down Expand Up @@ -210,7 +210,7 @@ export function ChatBody({
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiPanel hasBorder={false} hasShadow={false} paddingSize="m">
<ChatPromptEditor
<PromptEditor
hidden={connectors.loading || connectors.connectors?.length === 0}
loading={isLoading}
disabled
Expand Down Expand Up @@ -253,6 +253,7 @@ export function ChatBody({
chatState={state}
hasConnector={!!connectors.connectors?.length}
onEdit={(editedMessage, newMessage) => {
setStickToBottom(true);
const indexOf = messages.indexOf(editedMessage);
next(messages.slice(0, indexOf).concat(newMessage));
}}
Expand Down Expand Up @@ -308,14 +309,14 @@ export function ChatBody({
color="subdued"
className={promptEditorContainerClassName}
>
<ChatPromptEditor
<PromptEditor
disabled={!connectors.selectedConnector || !hasCorrectLicense}
hidden={connectors.loading || connectors.connectors?.length === 0}
loading={isLoading}
onChangeHeight={handleChangeHeight}
onSendTelemetry={(eventWithPayload) =>
sendEvent(chatService.analytics, eventWithPayload)
}
onChangeHeight={handleChangeHeight}
onSubmit={(message) => {
setStickToBottom(true);
return next(messages.concat(message));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export function ChatItem({

const handleInlineEditSubmit = (newMessage: Message) => {
handleToggleEdit();

return onEditSubmit(newMessage);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/

import React from 'react';
import { EuiPanel } from '@elastic/eui';
import { noop } from 'lodash';
import { css } from '@emotion/css';
import { EuiPanel } from '@elastic/eui';
import { MessageText } from '../message_panel/message_text';
import { ChatPromptEditor } from './chat_prompt_editor';
import { PromptEditor } from '../prompt_editor/prompt_editor';
import type { Message } from '../../../common';
import type { ChatActionClickHandler } from './types';
import type { TelemetryEventTypeWithPayload } from '../../analytics';
Expand Down Expand Up @@ -59,14 +60,14 @@ export function ChatItemContentInlinePromptEditor({
hasShadow={false}
className={editorContainerClassName}
>
<ChatPromptEditor
<PromptEditor
disabled={false}
hidden={false}
loading={false}
initialFunctionCall={functionCall}
initialContent={content}
initialRole={role}
onChangeHeight={() => {}}
onChangeHeight={noop}
onSubmit={onSubmit}
onSendTelemetry={onSendTelemetry}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ export function ChatTimeline({
key={index}
consolidatedItem={item}
onActionClick={onActionClick}
onEditSubmit={onEdit}
onFeedback={onFeedback}
onRegenerate={onRegenerate}
onEditSubmit={onEdit}
onSendTelemetry={onSendTelemetry}
onStopGenerating={onStopGenerating}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useEffect, useRef, useState } from 'react';
import React, { useEffect, useState } from 'react';
import {
EuiBetaBadge,
EuiButtonIcon,
Expand Down Expand Up @@ -42,8 +42,6 @@ export function FunctionListPopover({
const { getFunctions } = useObservabilityAIAssistantChatService();
const functions = getFunctions();

const filterRef = useRef<HTMLInputElement | null>(null);

const [functionOptions, setFunctionOptions] = useState<
Array<EuiSelectableOption<FunctionListOption>>
>(mapFunctions({ functions, selectedFunctionName }));
Expand All @@ -65,69 +63,19 @@ export function FunctionListPopover({
onSelectFunction(func.label);
};

useEffect(() => {
const keyboardListener = (event: KeyboardEvent) => {
if (event.shiftKey && event.code === 'Digit4') {
setIsFunctionListOpen(true);
}
};

window.addEventListener('keyup', keyboardListener);

return () => {
window.removeEventListener('keyup', keyboardListener);
};
}, []);

useEffect(() => {
if (isFunctionListOpen && filterRef.current) {
filterRef.current.focus();
}
}, [isFunctionListOpen]);

useEffect(() => {
const options = mapFunctions({ functions, selectedFunctionName });
if (options.length !== functionOptions.length) {
setFunctionOptions(options);
}
}, [functionOptions.length, functions, selectedFunctionName]);

const renderFunctionOption = (
option: EuiSelectableOption<FunctionListOption>,
searchValue: string
) => {
return (
<EuiFlexGroup gutterSize="xs" direction="column">
<EuiFlexItem grow={false}>
<EuiFlexGroup gutterSize="xs">
<EuiFlexItem grow={false}>
<EuiText size="s">
<strong>
<EuiHighlight search={searchValue}>{option.label}</EuiHighlight>{' '}
<EuiBetaBadge label="beta" size="s" style={{ verticalAlign: 'middle' }} />
</strong>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false} />
</EuiFlexGroup>
</EuiFlexItem>
<EuiFlexItem>
<EuiText
size="xs"
style={{ textOverflow: 'ellipsis', overflow: 'hidden', marginBottom: 4 }}
>
<EuiHighlight search={searchValue}>{option.searchableLabel || ''}</EuiHighlight>
</EuiText>
</EuiFlexItem>
</EuiFlexGroup>
);
};

return (
<EuiPopover
anchorPosition="downLeft"
button={
<EuiToolTip
key={mode} // this is added to prevent the tooltip from flickering when the mode stays the same
content={
mode === 'prompt'
? i18n.translate(
Expand All @@ -144,6 +92,10 @@ export function FunctionListPopover({
display="block"
>
<EuiButtonIcon
aria-label={i18n.translate(
'xpack.observabilityAiAssistant.functionListPopover.euiButtonIcon.selectAFunctionLabel',
{ defaultMessage: 'Select function' }
)}
data-test-subj="observabilityAiAssistantFunctionListPopoverButton"
disabled={disabled}
iconType={selectedFunctionName ? 'cross' : 'plusInCircle'}
Expand All @@ -154,6 +106,7 @@ export function FunctionListPopover({
}
closePopover={handleClickFunctionList}
css={{ maxWidth: 400 }}
initialFocus="#searchFilterList"
panelPaddingSize="none"
isOpen={isFunctionListOpen}
>
Expand All @@ -173,7 +126,7 @@ export function FunctionListPopover({
searchable
searchProps={{
'data-test-subj': 'searchFiltersList',
inputRef: (node) => (filterRef.current = node),
id: 'searchFilterList',
placeholder: i18n.translate('xpack.observabilityAiAssistant.prompt.functionList.filter', {
defaultMessage: 'Filter',
}),
Expand Down Expand Up @@ -215,3 +168,34 @@ function mapFunctions({
: ('off' as EuiSelectableOptionCheckedType),
}));
}

function renderFunctionOption(
option: EuiSelectableOption<FunctionListOption>,
searchValue: string
) {
return (
<EuiFlexGroup gutterSize="xs" direction="column">
<EuiFlexItem grow={false}>
<EuiFlexGroup gutterSize="xs">
<EuiFlexItem grow={false}>
<EuiText size="s">
<strong>
<EuiHighlight search={searchValue}>{option.label}</EuiHighlight>{' '}
<EuiBetaBadge label="beta" size="s" style={{ verticalAlign: 'middle' }} />
</strong>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false} />
</EuiFlexGroup>
</EuiFlexItem>
<EuiFlexItem>
<EuiText
size="xs"
style={{ textOverflow: 'ellipsis', overflow: 'hidden', marginBottom: 4 }}
>
<EuiHighlight search={searchValue}>{option.searchableLabel || ''}</EuiHighlight>
</EuiText>
</EuiFlexItem>
</EuiFlexGroup>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

import React from 'react';
import { ComponentStory } from '@storybook/react';
import { ChatPromptEditor as Component, ChatPromptEditorProps } from './chat_prompt_editor';
import { PromptEditor as Component, PromptEditorProps } from './prompt_editor';
import { KibanaReactStorybookDecorator } from '../../utils/storybook_decorator';

/*
JSON Schema validation in the ChatPromptEditor compponent does not work
JSON Schema validation in the PromptEditor compponent does not work
when rendering the component from within Storybook.
*/
export default {
component: Component,
title: 'app/Molecules/ChatPromptEditor',
title: 'app/Molecules/PromptEditor',
argTypes: {},
parameters: {
backgrounds: {
Expand All @@ -28,11 +28,11 @@ export default {
decorators: [KibanaReactStorybookDecorator],
};

const Template: ComponentStory<typeof Component> = (props: ChatPromptEditorProps) => {
const Template: ComponentStory<typeof Component> = (props: PromptEditorProps) => {
return <Component {...props} />;
};

const defaultProps = {};

export const ChatPromptEditor = Template.bind({});
ChatPromptEditor.args = defaultProps;
export const PromptEditor = Template.bind({});
PromptEditor.args = defaultProps;
Loading

0 comments on commit 9419256

Please sign in to comment.