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

Title Optimization: improve error handling #39340

Merged

Conversation

lhkowalski
Copy link
Contributor

@lhkowalski lhkowalski commented Sep 10, 2024

Proposed changes:

  • Handle suggestion errors and show them using an error notice component (moderation, unclear prompt, service unavailable, etc)
  • Handle quota exceeded errors and show the QuotaExceededMessage component
    • when the plan is free, the QuotaExceededMessage will display the upgrade nudge
    • when the plan is paid, the QuotaExceededMessage will display the fair usage notice
  • Handle JSON parse errors and show a generic error message using an error notice component
  • Keep the "Try again" button only for transient errors

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

Testing instructions:

  • Make sure you are sandboxing the public-api domain
  • Make sure you are running with the beta blocks variation
  • To test the error handling, we are going to simulate errors on the backend using code snippets on the jetpack-ai-query endpoint handler; add the snippet as the first line of the run_query method

Testing the moderation error

  • Use the following snippet on the backend:
return new \WP_Error( 'jetpack_ai_moderation', 'This request has been flagged by our moderation system. Please try to rephrase it and try again.', [ 'status' => 422 ] );
  • On the block editor, add some content, open the AI Assistant sidebar section and click the Improve title for SEO button
  • Confirm you see the proper error message on the UI:
Screenshot 2024-09-11 at 17 31 06

Testing the JSON parsing error

  • This error can happen from time to time when the backend model fails to produce a valid, complete JSON
  • Use the following snippet on the backend (to simulate a suggestion streaming returning an incomplete JSON object; line breaks are relevant, so don't remove it):
		header( 'HTTP/1.1 200 OK' );
		header( 'X-Accel-Buffering: no' );
		header( 'Date: ' . gmdate( 'D, d M Y H:i:s', time() ) . ' GMT' );
		header( 'Content-Type: text/event-stream' );
		header( 'Connection: close' );
		header( 'access-control-allow-origin: *' );
		header( "Cache-Control: no-cache, must-revalidate\n" );
		echo <<<COMPLETION
data: {"id":"chatcmpl-A6LVBpL5q401MjFrd5mY8Siiei3H0","object":"chat.completion.chunk","created":1726075985,"model":"gpt-3.5-turbo-1106","system_fingerprint":"fp_54b3a4486f","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-A6LVBpL5q401MjFrd5mY8Siiei3H0","object":"chat.completion.chunk","created":1726075985,"model":"gpt-3.5-turbo-1106","system_fingerprint":"fp_54b3a4486f","choices":[{"index":0,"delta":{"content":"[\\n"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-A6LVBpL5q401MjFrd5mY8Siiei3H0","object":"chat.completion.chunk","created":1726075985,"model":"gpt-3.5-turbo-1106","system_fingerprint":"fp_54b3a4486f","choices":[{"index":0,"delta":{"content":" "},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-A6LVBpL5q401MjFrd5mY8Siiei3H0","object":"chat.completion.chunk","created":1726075985,"model":"gpt-3.5-turbo-1106","system_fingerprint":"fp_54b3a4486f","choices":[{"index":0,"delta":{"content":" {\\n"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-A6LVBpL5q401MjFrd5mY8Siiei3H0","object":"chat.completion.chunk","created":1726075985,"model":"gpt-3.5-turbo-1106","system_fingerprint":"fp_54b3a4486f","choices":[{"index":0,"delta":{"content":"   "},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-A6LVBpL5q401MjFrd5mY8Siiei3H0","object":"chat.completion.chunk","created":1726075985,"model":"gpt-3.5-turbo-1106","system_fingerprint":"fp_54b3a4486f","choices":[{"index":0,"delta":{"content":" \\""},"logprobs":null,"finish_reason":null}]}

data: [DONE]


COMPLETION;

		return ['body' => '{"usage": {"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30}}' ];
  • Confirm you see the proper error message on the UI:
Screenshot 2024-09-11 at 16 27 51

Testing the quota exceeded error on a free site

  • Use the following snippet on the 0-sandbox.php file:
add_filter( 'jetpack_ai_tier_licensed_quantity', function() { return 0; } );
add_filter( 'jetpack_ai_current_period_requests_count', function() { return 19; } );
add_filter( 'jetpack_ai_all_time_requests_count', function() { return 19; } ); // almost on the limit
  • Go to the block editor, refresh it but do not click on the Improve title for SEO button yet
  • Return to the 0-sandbox.php file and change the snippet to:
add_filter( 'jetpack_ai_tier_licensed_quantity', function() { return 0; } );
add_filter( 'jetpack_ai_current_period_requests_count', function() { return 20; } );
add_filter( 'jetpack_ai_all_time_requests_count', function() { return 20; } );
  • Now, on the block editor, without refreshing, click Improve title for SEO
  • Confirm you see the proper error message on the UI:
Screenshot 2024-09-11 at 17 39 34
  • Confirm that clicking the upgrade link will land you on the proper place (My Jetpack interstitial or checkout page, for Jetpack and Simple sites, in order)

Testing the quota exceeded error on a paid site

  • Use the following snippet on the 0-sandbox.php file:
add_filter( 'jetpack_ai_tier_licensed_quantity', function() { return 1; } );
add_filter( 'jetpack_ai_current_period_requests_count', function() { return 2999; } ); // almost on the limit
add_filter( 'jetpack_ai_all_time_requests_count', function() { return 45000; } );
  • Go to the block editor, refresh it but do not click on the Improve title for SEO button yet
  • Return to the 0-sandbox.php file and change the snippet to:
add_filter( 'jetpack_ai_tier_licensed_quantity', function() { return 1; } );
add_filter( 'jetpack_ai_current_period_requests_count', function() { return 3000; } );
add_filter( 'jetpack_ai_all_time_requests_count', function() { return 45000; } );
  • Now, on the block editor, without refreshing, click Improve title for SEO
  • Confirm you see the proper error message on the UI:
Screenshot 2024-09-11 at 17 33 35

@lhkowalski lhkowalski requested a review from a team September 10, 2024 21:48
@lhkowalski lhkowalski self-assigned this Sep 10, 2024
@lhkowalski lhkowalski added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Extension] AI Assistant Plugin labels Sep 10, 2024
Copy link
Contributor

github-actions bot commented Sep 10, 2024

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the update/jetpack-ai-handle-errors-on-title-improvement-tool branch.

  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack update/jetpack-ai-handle-errors-on-title-improvement-tool
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? Reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen semi-continuously (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for October 1, 2024 (scheduled code freeze on September 30, 2024).

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@lhkowalski lhkowalski marked this pull request as ready for review September 11, 2024 19:38
@@ -58,7 +103,7 @@ export default function TitleOptimization( {
const [ isTitleOptimizationModalVisible, setIsTitleOptimizationModalVisible ] = useState( false );
const [ generating, setGenerating ] = useState( false );
const [ options, setOptions ] = useState( [] );
const [ error, setError ] = useState( false );
const [ error, setError ] = useState< TitleOptimizationError >( null );
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const [ error, setError ] = useState< TitleOptimizationError >( null );
const [ error, setError ] = useState< TitleOptimizationError | null >( null );

}

// Use the provided message, if available, otherwise use the generic error message
const errorMessage = error?.message ? error.message : genericErrorMessage;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const errorMessage = error?.message ? error.message : genericErrorMessage;
const errorMessage = error.message ? error.message : genericErrorMessage;

dhasilva
dhasilva previously approved these changes Sep 12, 2024
Copy link
Contributor

@dhasilva dhasilva left a comment

Choose a reason for hiding this comment

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

LGTM, just left a couple type comments

@lhkowalski lhkowalski merged commit d073875 into trunk Sep 12, 2024
57 checks passed
@lhkowalski lhkowalski deleted the update/jetpack-ai-handle-errors-on-title-improvement-tool branch September 12, 2024 18:10
@github-actions github-actions bot added this to the jetpack/13.9 milestone Sep 12, 2024
gogdzl pushed a commit that referenced this pull request Oct 25, 2024
* Save the error object so we can use it to display the error message

* Move error message to it's own component

* Show the provided error message when the error is related to moderation

* Use an error notice to show the errors

* Show FairUsageNotice when the error is a quota exceeded one

* Changelog

* Use the QuotaExceededMessage component so we handle automatically the free and paid plan quotas

* Support JSON error handling

* Reuse the generic error message

* Change code to trust the message provided by the error object, when available

* Do not show retry button when the error will not go away

* Include type suggestions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Extension] AI Assistant Plugin [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants