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

Tweaks to prompt editor #174030

Merged
merged 14 commits into from
Jan 8, 2024
Merged

Conversation

CoenWarmer
Copy link
Contributor

@CoenWarmer CoenWarmer commented Dec 28, 2023

Summary

This fixes the following edge case when using the chat interface:

  • When the main editor has a value in the text area, and the user edits an existing message, and presses <enter> on the keyboard, the value that was in the main editor is appended as a new message, instead of editing the existing message.

It also does a bit of cleanup (moving of ChatPromptEditor components to /components/prompt_editor, and renaming to PromptEditor.)

Additional fixes

@CoenWarmer CoenWarmer requested a review from a team as a code owner December 28, 2023 16:04
@apmmachine
Copy link
Contributor

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • /oblt-deploy : Deploy a Kibana instance using the Observability test environments.
  • /oblt-deploy-serverless : Deploy a serverless Kibana instance using the Observability test environments.
  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@CoenWarmer CoenWarmer added release_note:skip Skip the PR/issue when compiling release notes v8.12.0 backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) labels Dec 28, 2023
@CoenWarmer CoenWarmer force-pushed the bug/inline-prompt-editing branch from e4dff55 to e963efa Compare December 28, 2023 17:01
@@ -141,6 +141,8 @@ export function ChatBody({
: '100%'};
`;

const [isEditing, setIsEditing] = useState(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this signify? I guess it means "editing a single message"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a dumb question, but if focus can only be on a single input, why don't we use that to constrain submits?

Copy link
Contributor Author

@CoenWarmer CoenWarmer Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dgieselaar This allows this behavior (disabling the main editor when editing an existing message):

Screen.Recording.2024-01-02.at.16.48.59.mov

We could not do that. Wdyt?

@CoenWarmer CoenWarmer requested a review from dgieselaar January 4, 2024 15:03
return () => {
onBlur();
};
}, [onBlur]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this looks a little dangerous in terms of consumers passing a new callback every render?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its in the return of the useEffect though, so it should only run on unmount, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, it just re-creates the unmount hook. I think it's better to use a ref here though, that way we don't call this hook on every render. I'm not too worried about performance, but more so about bugs creeping in and testability.

Copy link
Contributor Author

@CoenWarmer CoenWarmer Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first thought as well, unfortunately Monaco (or at least Kibana's implementation of it) does not have an onBlur.

FWIW it also does not have an onFocus. It only has an editorDidMount prop, which can be used as an alternative for onFocus. There is no corresponding editorWillUnmount though 🤷🏻‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I meant onBlurRef = useRef(props.onBlur); onBlurRef.current = props.onBlur;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out the monaco editor instance has a onDidBlurEditorWidget event.

Implemented with:

  editorRef.current?.onDidBlurEditorWidget(() => {
    onBlur();
  });

@CoenWarmer CoenWarmer requested a review from dgieselaar January 8, 2024 11:19
@CoenWarmer CoenWarmer requested a review from dgieselaar January 8, 2024 11:35
Copy link
Member

@dgieselaar dgieselaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just nits left

Comment on lines +127 to +131
if (innerMessage && !disabled && !invalid && hasFocus) {
if (!event.shiftKey && event.key === keys.ENTER) {
event.preventDefault();
handleSubmit();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a complicated if, can we simplify? (I don't think we need two ifs anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the idea was future proofing, allowing easier additional other keyboard commands. First if checking if it should continue, send the key combination. But if we think no additional kb commands will be added I can consolidate.

return () => {
onBlur();
};
}, [onBlur]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, it just re-creates the unmount hook. I think it's better to use a ref here though, that way we don't call this hook on every render. I'm not too worried about performance, but more so about bugs creeping in and testability.

@CoenWarmer CoenWarmer enabled auto-merge (squash) January 8, 2024 13:31
@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] Jest Tests #5 / EditableMarkdown Cancel button click calls only onChangeEditable
  • [job] [logs] Jest Tests #5 / EditableMarkdown Does not call onSaveContent if no change from current text
  • [job] [logs] Jest Tests #5 / EditableMarkdown draft comment Cancel button click clears session storage
  • [job] [logs] Jest Tests #5 / EditableMarkdown draft comment existing storage key should have session storage value same as draft comment
  • [job] [logs] Jest Tests #5 / EditableMarkdown draft comment Save button click clears session storage
  • [job] [logs] Jest Tests #5 / EditableMarkdown errors Shows error message and save button disabled if current text is empty
  • [job] [logs] Jest Tests #5 / EditableMarkdown errors Shows error message and save button disabled if current text is of empty characters
  • [job] [logs] Jest Tests #5 / EditableMarkdown errors Shows error message and save button disabled if current text is too long
  • [job] [logs] Jest Tests #5 / EditableMarkdown Save button click calls onSaveContent and onChangeEditable when text area value changed

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
observabilityAIAssistant 151.2KB 151.8KB +607.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@CoenWarmer CoenWarmer merged commit f4265ca into elastic:main Jan 8, 2024
23 checks passed
@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
8.12 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 174030

Questions ?

Please refer to the Backport tool documentation

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create automatically backports add the label auto-backport or prevent reminders by adding the backport:skip label.
You can also create backports manually by running node scripts/backport --pr 174030 locally

@kibanamachine kibanamachine added the backport missing Added to PRs automatically when the are determined to be missing a backport. label Jan 9, 2024
delanni pushed a commit to delanni/kibana that referenced this pull request Jan 11, 2024
## Summary

This fixes the following edge case when using the chat interface:

* When the main editor has a value in the text area, and the user edits
an existing message, and presses `<enter>` on the keyboard, the value
that was in the main editor is appended as a new message, instead of
editing the existing message.

It also does a bit of cleanup (moving of ChatPromptEditor components to
`/components/prompt_editor`, and renaming to `PromptEditor`.)

### Additional fixes
* [Don't stick to bottom when changing to edit mode, re-stick to bottom
when done
editing](elastic@e8a01c1)

* [Autofocus function popover list search box upon
opening](elastic@2329d1c)

* [Remove focus trap as it wasn't doing anything
anymore](elastic@7fcb4e0)

* [Move constants used when creating monaco model inside function scope
to avoid sharing of model between multiple editor
instances](elastic@c9cab2c)

* [Disable submitting function editor when json is not
valid](elastic@2a6f1e1)
@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create automatically backports add the label auto-backport or prevent reminders by adding the backport:skip label.
You can also create backports manually by running node scripts/backport --pr 174030 locally

@CoenWarmer
Copy link
Contributor Author

💚 All backports created successfully

Status Branch Result
8.12

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

@sorenlouv
Copy link
Member

💚 All backports created successfully

Status Branch Result
8.12

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

sorenlouv pushed a commit to sorenlouv/kibana that referenced this pull request Jan 26, 2024
## Summary

This fixes the following edge case when using the chat interface:

* When the main editor has a value in the text area, and the user edits
an existing message, and presses `<enter>` on the keyboard, the value
that was in the main editor is appended as a new message, instead of
editing the existing message.

It also does a bit of cleanup (moving of ChatPromptEditor components to
`/components/prompt_editor`, and renaming to `PromptEditor`.)

### Additional fixes
* [Don't stick to bottom when changing to edit mode, re-stick to bottom
when done
editing](elastic@e8a01c1)

* [Autofocus function popover list search box upon
opening](elastic@2329d1c)

* [Remove focus trap as it wasn't doing anything
anymore](elastic@7fcb4e0)

* [Move constants used when creating monaco model inside function scope
to avoid sharing of model between multiple editor
instances](elastic@c9cab2c)

* [Disable submitting function editor when json is not
valid](elastic@2a6f1e1)

(cherry picked from commit f4265ca)

# Conflicts:
#	x-pack/plugins/observability_ai_assistant/public/components/prompt_editor/prompt_editor_function.tsx
@sorenlouv sorenlouv added auto-backport Deprecated - use backport:version if exact versions are needed v8.12.1 and removed v8.12.0 labels Jan 26, 2024
CoenWarmer added a commit to CoenWarmer/kibana that referenced this pull request Jan 26, 2024
## Summary

This fixes the following edge case when using the chat interface:

* When the main editor has a value in the text area, and the user edits
an existing message, and presses `<enter>` on the keyboard, the value
that was in the main editor is appended as a new message, instead of
editing the existing message.

It also does a bit of cleanup (moving of ChatPromptEditor components to
`/components/prompt_editor`, and renaming to `PromptEditor`.)

### Additional fixes
* [Don't stick to bottom when changing to edit mode, re-stick to bottom
when done
editing](elastic@e8a01c1)

* [Autofocus function popover list search box upon
opening](elastic@2329d1c)

* [Remove focus trap as it wasn't doing anything
anymore](elastic@7fcb4e0)

* [Move constants used when creating monaco model inside function scope
to avoid sharing of model between multiple editor
instances](elastic@c9cab2c)

* [Disable submitting function editor when json is not
valid](elastic@2a6f1e1)

(cherry picked from commit f4265ca)

# Conflicts:
#	x-pack/plugins/observability_ai_assistant/public/components/prompt_editor/prompt_editor_function.tsx
@sorenlouv sorenlouv removed backport missing Added to PRs automatically when the are determined to be missing a backport. auto-backport Deprecated - use backport:version if exact versions are needed labels Jan 26, 2024
CoenWarmer added a commit that referenced this pull request Jan 26, 2024
# Backport

This will backport the following commits from `main` to `8.12`:
- [Tweaks to prompt editor
(#174030)](#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 (#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 (#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 (#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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) release_note:skip Skip the PR/issue when compiling release notes v8.12.1 v8.13.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants