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

aichat: scroll is interruptable #21235

Merged
merged 5 commits into from
Jan 17, 2024
Merged

aichat: scroll is interruptable #21235

merged 5 commits into from
Jan 17, 2024

Conversation

nullhook
Copy link
Contributor

@nullhook nullhook commented Dec 5, 2023

Resolves brave/brave-browser#32970

This PR allows user to interrupts the autoscroll while an answer is being generated. Additionally, it will resume back to autoscroll if the user's scroll is near the bottom.

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run lint, npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

@github-actions github-actions bot added the CI/storybook-url Deploy storybook and provide a unique URL for each build label Dec 5, 2023
@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@nullhook nullhook marked this pull request as ready for review December 6, 2023 21:42
@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Copy link
Member

@petemill petemill left a comment

Choose a reason for hiding this comment

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

It isn't auto-scrolling the user message anymore. So when I press enter on my message, I don't see it until a couple seconds later when the AI response starts being received.

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

return
}

if (scrollPos.current.isAtBottom) {
Copy link
Member

Choose a reason for hiding this comment

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

Why can't we just here check if scrollerElement is at the bottom and not need to have handleScroll and scrollPos?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i did try that initially, but it looks like there's a timing issue. basically, computing isAtBottom in the same call stack is flawed. additionally, it doesn't monitor when the user has scrolled to the bottom, which we need for auto scrolling behavior.

Copy link
Member

Choose a reason for hiding this comment

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

Well, perhaps we can overcome the potential performance issue another way then. I'm seeing handleScroll called on every scroll when it's not neccessary until the content of the "last element" changes.
I'm assuming your issue when you tried this initially is because handleLastElementHeightChange is called immediately when the content of the react properties change but before they are rendered (and definitely before the html layout is done). So couldn't that be fixed with a requestAnimationFrame?

Copy link
Contributor Author

@nullhook nullhook Jan 5, 2024

Choose a reason for hiding this comment

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

i have also tried using rAF to sync with browser's rendering within the same callstack. however, we are still off.

still, this approach doesn't resolve the need to continuously monitor the user's scroll while the generation is occurring. if the user decides to scroll back to the bottom, we need to switch back to the scroll-to-bottom behavior.

i agree that handleScroll doesn't really need to be called on every tick. we can overcome this issue by throttling?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, throttle at the very least. Or useLayoutEffect?

Copy link
Contributor Author

@nullhook nullhook Jan 12, 2024

Choose a reason for hiding this comment

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

actually, we do need to monitor every tick. throttling can be unreliable because if the user is scrolling towards the bottom, it may compute positions at a later time, potentially missing the opportunity to autoscroll.

we could run the scroll handler in rAF and make it tick with the monitor's refresh rate. ex. https://codepen.io/dcorb/pen/pgOKKw - potentially lowers the number of ticks compared to before but not significantly as throttling (depends on how limit).

Copy link
Contributor Author

@nullhook nullhook Jan 12, 2024

Choose a reason for hiding this comment

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

another idea: what if we run or attach the scroll handler only if context.isGenerating? this satisfies the need to avoid unnecessary ticking and computing positions in a separate call stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to only monitor when it's generating

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@@ -85,6 +89,23 @@ function Main() {
}
}

const handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
// Monitor scroll positions only when Assistant is generating
if (!context.isGenerating) return
Copy link
Member

Choose a reason for hiding this comment

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

nit: please don't put return on same line

@nullhook nullhook merged commit ce69a31 into master Jan 17, 2024
18 checks passed
@nullhook nullhook deleted the ai-chat-scroll-issue branch January 17, 2024 16:16
@github-actions github-actions bot added this to the 1.63.x - Nightly milestone Jan 17, 2024
nullhook added a commit that referenced this pull request Jan 18, 2024
@stephendonner
Copy link
Contributor

stephendonner commented Jan 18, 2024

Verified PASSED using

Brave | 1.64.3 Chromium: 121.0.6167.57 (Official Build) nightly (64-bit)
-- | --
Revision | 0ecd8a5470d06ce96921cb7585ecbc0f9878f3cf
OS | Windows 10 Version 22H2 (Build 19045.3930)

Steps:

  1. installed 1.64.3
  2. launched Brave using --env-leo=staging --env-ai-chat.bsg=dev --env-ai-chat-premium.bsg=dev --enable-logging=stderr --v=2
  3. loaded https://constitutioncenter.org/the-constitution/full-text
  4. clicked on Summarize this page
  5. wait for auto-scrolling to start
  6. swipe up and wait
  7. scroll down

Confirmed auto-scrolling is interrupted by mouse/trackpad motion, with the viewport position held until further scrolling

Confirmed after the above that you can resume auto-scrolling once at or near the bottom of the content

2024-01-18_15h57_26.mp4

kjozwiak pushed a commit that referenced this pull request Jan 19, 2024
* aichat: input is growable (#21124)

* aichat: scroll is interruptable (#21235)

* aichat: model maker text shouldnt look like a link (#21220)

* aichat: code formatting (#21342)

* make claude output formatted code (#21599)
kjozwiak pushed a commit that referenced this pull request Jan 20, 2024
* Merge pull request #21134 from brave/cr121

Upgrade from Chromium 120 to Chromium 121.

* Remove the assert for patch_ffmpeg.py (#21184)

* Merge pull request #21539 from brave/ffmpeg-dynamic-alloc

Use dynamic allocation for ffmpeg fft tables on Windows.

* Merge pull request #21585 from brave/issues/35318

Remove dynamic allocation of ffmpeg ff_tx tables.

* Disables a flaky upstream browser test.

* Merge pull request #21584 from brave/fix_new_tab_button_plus_misaligned

Fixed new tab button's plus icon is mis-aligned with horizontal tab

* Merge pull request #21600 from brave/121.0.6167.75_master

Upgrade from Chromium 121.0.6167.57 to Chromium 121.0.6167.75.

* Merge pull request #21628 from brave/maxk-disable-reading-mode

Hides `Open in Reading Mode` context menu item.

* [Uplift 1.62.x] AI chat issues cr121 1.62.x (#21629)

* aichat: input is growable (#21124)

* aichat: scroll is interruptable (#21235)

* aichat: model maker text shouldnt look like a link (#21220)

* aichat: code formatting (#21342)

* make claude output formatted code (#21599)

---------

Co-authored-by: Mikhail <matuchin@brave.com>
Co-authored-by: Aleksey Khoroshilov <5928869+goodov@users.noreply.github.com>
Co-authored-by: Simon Hong <shong@brave.com>
Co-authored-by: taher <8665427+nullhook@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/storybook-url Deploy storybook and provide a unique URL for each build
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto scroll can be distracting
4 participants