-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Prevent exception when constructing WP_AI_Client_Prompt_Builder when invalid timeout is provided by filter
#11596
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
Changes from 8 commits
438011b
7eb2c19
8006183
63542e4
ed8d8d6
9925c25
f01e50d
a14916b
0068422
8e4c3df
49ea5bb
226ac98
6a03377
3d6cf35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -190,14 +190,32 @@ public function __construct( ProviderRegistry $registry, $prompt = null ) { | |||||||||||||||||||||||||||||||||||||||
| $this->error = $this->exception_to_wp_error( $e ); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $default_timeout = 30.0; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||
| * Filters the default request timeout in seconds for AI Client HTTP requests. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * @since 7.0.0 | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * @param int $default_timeout The default timeout in seconds. | ||||||||||||||||||||||||||||||||||||||||
| * @param float|null $default_timeout The default timeout in seconds, or null to disable the timeout. | ||||||||||||||||||||||||||||||||||||||||
| * If not null, must be greater than or equal to zero. | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| $default_timeout = (int) apply_filters( 'wp_ai_client_default_request_timeout', 30 ); | ||||||||||||||||||||||||||||||||||||||||
| $timeout = apply_filters( 'wp_ai_client_default_request_timeout', $default_timeout ); | ||||||||||||||||||||||||||||||||||||||||
| if ( is_numeric( $timeout ) && (float) $timeout >= 0.0 ) { | ||||||||||||||||||||||||||||||||||||||||
| $default_timeout = (float) $timeout; | ||||||||||||||||||||||||||||||||||||||||
|
westonruter marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||
| } elseif ( null === $timeout ) { | ||||||||||||||||||||||||||||||||||||||||
| $default_timeout = null; | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| _doing_it_wrong( | ||||||||||||||||||||||||||||||||||||||||
| __METHOD__, | ||||||||||||||||||||||||||||||||||||||||
| sprintf( | ||||||||||||||||||||||||||||||||||||||||
| /* translators: %s: wp_ai_client_default_request_timeout */ | ||||||||||||||||||||||||||||||||||||||||
| __( 'The %s filter must return a non-negative number or null.' ), | ||||||||||||||||||||||||||||||||||||||||
| '<code>wp_ai_client_default_request_timeout</code>' | ||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||
| '7.0.0' | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if there is a reason to support a separate We definitely don't need the extra handholding for < 0 validation, since that's handled by setTimeout directly.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed
We do as otherwise an exception is thrown. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick gut check: are we sure throwing an exception is bad DX? The only way this value can be a bad number is if someone sets it wrong in code, so an early and obvious failure before the code gets committed. 🤔 (This was recently raised via community feedback in the context of the abilities api. Tl;dr it logs instead of erroring if e.g. someone sets a bad category on an ability. Arguably an exception would be better than making them check their debug.log files 🤷)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the failure to set a time limit is a low-severity issue, I think logging is good here. Also, the timeout is variable by user role, it seems less ideal for them to experience a fatal error if a developer who set up the timeouts forgot to test their specific configuration. |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| $this->builder->usingRequestOptions( | ||||||||||||||||||||||||||||||||||||||||
| RequestOptions::fromArray( | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.