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

PCH Smart Linking: Allow API retry when there are upstream errors #2386

Merged
merged 5 commits into from
Apr 17, 2024

Conversation

vaurdan
Copy link
Contributor

@vaurdan vaurdan commented Apr 5, 2024

Description

In some scenarios, when generating Smart Links, the upstream might not be able to generate a valid response. This can happen because of issues communicating with OpenAI, or even invalid responses.

Since most of these issues are transient and can be resolved with a manual retry, this PR aims to implement a automatic retry process, up to three retries, to help overcoming these scenarios. This comes with a time cost though, since there can be up to three additional API requests.

The retries are communicated to the user by changing the label of the block overlay, and by changing the text in the "Adding Smart Links" button. We also give the option to abort, at any time, the retries attempts, in order to unblock the block editor - no pun intended.

Motivation and context

Improve the reliability of the Smart Linking feature, having it automatically handle any actionable failures.

How has this been tested?

Tested locally by editing the provider and simulating three API failures.

Screenshots (if appropriate)

Smart Linking Interface Configuration Settings
Figure 1: The block overlay showing that it is retrying Figure 2: The button is also updated with the number of attempts.
Screen.Recording.2024-04-05.at.13.04.10.mov

Video: Smart Linking retry demonstration.

Summary by CodeRabbit

  • New Features

    • Introduced a new error code ParselyAborted for enhanced error handling.
    • Added a new CSS class for improved visual styling of block overlays.
    • Implemented functionality to retry generating Smart Links with a cancellation option for users.
    • Added the ability to cancel ongoing fetch requests to improve user control over network operations.
    • Enhanced Smart Linking feature with retry logic and user feedback on retry attempts.
  • Refactor

    • Removed unused error codes to streamline error management.
    • Renamed and enhanced functions for better clarity and functionality in Smart Linking.
  • Style

    • Updated styling for the Smart Linking component to improve user interface.
  • Chores

    • Updated internal state management and actions to support new retry functionality in Smart Linking.

@vaurdan vaurdan added this to the 3.15.0 milestone Apr 5, 2024
@vaurdan vaurdan self-assigned this Apr 5, 2024
@vaurdan vaurdan requested a review from a team as a code owner April 5, 2024 12:42
Copy link
Contributor

coderabbitai bot commented Apr 5, 2024

Walkthrough

Walkthrough

The updates focus on enhancing the Smart Linking feature in the content helper tool by introducing a new error code ParselyAborted, improving error handling, adding retry functionality with a cancel option, updating UI components, and refining the user experience. Visual enhancements were also made to the Smart Linking interface to ensure a more intuitive user interaction.

Changes

Files Change Summary
.../content-helper-error.tsx Introduced ParselyAborted error code; removed outdated error references.
.../css/variables.scss Added .wp-parsely-block-overlay CSS class.
.../editor-sidebar/smart-linking/... Enhanced Smart Linking with retry logic, UI updates for retry functionality, and new state management for retries.
.../editor-sidebar/smart-linking/smart-linking.scss Updated styling for Smart Linking component.

Recent Review Details

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between 8752c03 and c0f8db8.
Files ignored due to path filters (2)
  • build/content-helper/editor-sidebar.asset.php is excluded by !build/**
  • build/content-helper/editor-sidebar.js is excluded by !build/**
Files selected for processing (1)
  • src/content-helper/editor-sidebar/smart-linking/provider.ts (3 hunks)
Additional Context Used
Path-based Instructions (1)
src/content-helper/editor-sidebar/smart-linking/provider.ts (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
Additional comments not posted (4)
src/content-helper/editor-sidebar/smart-linking/provider.ts (4)

5-5: Import of __ from @wordpress/i18n is appropriate for internationalization of user-facing strings.


50-50: Addition of abortController static property is well-implemented for managing fetch request cancellations.


57-59: Implementation of cancelRequest correctly handles the single-use nature of AbortController by re-instantiating it after use.


90-99: Modifications in generateSmartLinks to include signal for cancellable fetch operations and handle AbortError are correctly implemented.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

Review Status

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between fefa1d6 and 8752c03.
Files ignored due to path filters (11)
  • build/admin-settings.asset.php is excluded by !build/**
  • build/admin-settings.css is excluded by !build/**
  • build/content-helper/dashboard-widget.asset.php is excluded by !build/**
  • build/content-helper/dashboard-widget.css is excluded by !build/**
  • build/content-helper/dashboard-widget.js is excluded by !build/**
  • build/content-helper/editor-sidebar.asset.php is excluded by !build/**
  • build/content-helper/editor-sidebar.css is excluded by !build/**
  • build/content-helper/editor-sidebar.js is excluded by !build/**
  • build/content-helper/excerpt-generator.asset.php is excluded by !build/**
  • build/content-helper/excerpt-generator.css is excluded by !build/**
  • build/content-helper/excerpt-generator.js is excluded by !build/**
Files selected for processing (7)
  • src/content-helper/common/content-helper-error.tsx (2 hunks)
  • src/content-helper/common/css/variables.scss (1 hunks)
  • src/content-helper/editor-sidebar/smart-linking/component-block-overlay.tsx (5 hunks)
  • src/content-helper/editor-sidebar/smart-linking/component.tsx (17 hunks)
  • src/content-helper/editor-sidebar/smart-linking/provider.ts (2 hunks)
  • src/content-helper/editor-sidebar/smart-linking/smart-linking.scss (2 hunks)
  • src/content-helper/editor-sidebar/smart-linking/store.ts (7 hunks)
Additional Context Used
Path-based Instructions (7)
src/content-helper/common/css/variables.scss (1)

Pattern **/*.{css,scss}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the SCSS code to ensure it is well-structured and adheres to best practices.
  • Convert dimensions greater than or equal to 3px to rem units using the to_rem function.
  • Utilize variables for sizes and colors defined in src/content-helper/common/css/variables.scss instead of hardcoding values."
src/content-helper/editor-sidebar/smart-linking/provider.ts (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/content-helper/editor-sidebar/smart-linking/smart-linking.scss (1)

Pattern **/*.{css,scss}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the SCSS code to ensure it is well-structured and adheres to best practices.
  • Convert dimensions greater than or equal to 3px to rem units using the to_rem function.
  • Utilize variables for sizes and colors defined in src/content-helper/common/css/variables.scss instead of hardcoding values."
src/content-helper/editor-sidebar/smart-linking/component-block-overlay.tsx (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/content-helper/common/content-helper-error.tsx (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/content-helper/editor-sidebar/smart-linking/store.ts (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/content-helper/editor-sidebar/smart-linking/component.tsx (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
Additional comments not posted (12)
src/content-helper/editor-sidebar/smart-linking/provider.ts (2)

44-59: Ensure proper cleanup of AbortController to prevent memory leaks.
Consider adding a destructor or cleanup method to handle the disposal of AbortController instances when the SmartLinkingProvider is no longer in use.


89-99: Validate the error handling for aborted requests to ensure it aligns with the application's overall error management strategy.
Consider centralizing error handling logic if similar patterns are used across different parts of the application.

src/content-helper/editor-sidebar/smart-linking/component-block-overlay.tsx (2)

41-49: Verify the use of useSelect for managing state related to retrying. Ensure that this approach aligns with the overall state management strategy of the application.


133-150: Consider enhancing the user experience by providing feedback or a countdown for the retry attempts.
This could help users understand how many attempts are left and potentially reduce confusion or frustration.

src/content-helper/common/content-helper-error.tsx (1)

24-24: Confirm the removal of OpenAI-related error codes aligns with the application's error handling strategy and that all necessary error scenarios are covered.

src/content-helper/editor-sidebar/smart-linking/store.ts (1)

259-269: Ensure that the retry attempt counter is reset appropriately in scenarios other than toggling isRetrying. Consider edge cases where retries may be initiated multiple times within the same session.

src/content-helper/editor-sidebar/smart-linking/component.tsx (6)

18-18: Ensure the ContentHelperErrorCode import is used correctly throughout the file.


78-78: The MAX_NUMBER_OF_RETRIES constant is well-defined and follows best practices.


116-117: The addition of retrying and retryAttempt states is appropriate for managing retry logic.


644-657: The getGenerateButtonMessage function correctly handles different states to provide user feedback.


681-682: Ensure error messages are displayed correctly in the UI.

Verification successful

The verification process has confirmed that error.Message() is indeed used within various .tsx files in the codebase, including the specific file and context mentioned in the review comment (src/content-helper/editor-sidebar/smart-linking/component.tsx). This indicates that error.Message() is a valid method for displaying error messages in the UI across multiple components.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that error.Message() is a valid method and correctly displays error messages.
rg --type tsx "error\.Message\(\)"

Length of output: 64



Script:

#!/bin/bash
# Re-verify that error.Message() is a valid method and correctly displays error messages by searching in .tsx files without specifying the file type.
rg "error\.Message\(\)" --glob '*.tsx'

Length of output: 567


709-714: The button's onClick handler is correctly set to trigger the generateAndApplySmartLinks function.

@vaurdan
Copy link
Contributor Author

vaurdan commented Apr 5, 2024

@coderabbitai resolve

Copy link
Collaborator

@acicovic acicovic left a comment

Choose a reason for hiding this comment

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

Thanks for working on this, and for introducing the usage of an AbortController. We should definitely use it in other parts of the PCH as well.

@vaurdan vaurdan requested a review from acicovic April 17, 2024 10:26
@vaurdan vaurdan merged commit 9abd202 into develop Apr 17, 2024
36 checks passed
@vaurdan vaurdan deleted the update/smart-linking-retries branch April 17, 2024 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants