Skip to content

Commit 9925c25

Browse files
committed
Allow filtered timeout to be null and warn when invalid value provided
1 parent ed8d8d6 commit 9925c25

2 files changed

Lines changed: 78 additions & 8 deletions

File tree

src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,24 @@ public function __construct( ProviderRegistry $registry, $prompt = null ) {
197197
*
198198
* @since 7.0.0
199199
*
200-
* @param float $default_timeout The default timeout in seconds. Must be greater than or equal to zero.
200+
* @param float|null $default_timeout The default timeout in seconds, or null to disable the timeout.
201+
* If not null, must be greater than or equal to zero.
201202
*/
202203
$timeout = apply_filters( 'wp_ai_client_default_request_timeout', $default_timeout );
203-
if ( is_numeric( $timeout ) ) {
204-
$timeout = (float) $timeout;
205-
if ( $timeout >= 0.0 ) {
206-
$default_timeout = $timeout;
207-
}
204+
if ( is_numeric( $timeout ) && (float) $timeout >= 0.0 ) {
205+
$default_timeout = (float) $timeout;
206+
} elseif ( null === $timeout ) {
207+
$default_timeout = null;
208+
} else {
209+
_doing_it_wrong(
210+
__METHOD__,
211+
sprintf(
212+
/* translators: %s: wp_ai_client_default_request_timeout */
213+
__( 'The %s filter must return a non-negative number or null.' ),
214+
'<code>wp_ai_client_default_request_timeout</code>'
215+
),
216+
'7.0.0'
217+
);
208218
}
209219

210220
$this->builder->usingRequestOptions(

tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ public function test_constructor_sets_default_request_timeout() {
192192
}
193193

194194
/**
195-
* Test that the constructor allows overriding the default request timeout.
195+
* Test that the constructor allows overriding the default request timeout with a higher value.
196196
*
197197
* @ticket 64591
198198
*/
199-
public function test_constructor_allows_overriding_request_timeout() {
199+
public function test_constructor_allows_overriding_request_timeout_with_higher_value() {
200200
add_filter(
201201
'wp_ai_client_default_request_timeout',
202202
static function () {
@@ -213,6 +213,66 @@ static function () {
213213
$this->assertEquals( 45, $request_options->getTimeout() );
214214
}
215215

216+
/**
217+
* Test that the constructor allows overriding the default request timeout with null.
218+
*
219+
* @ticket 65094
220+
*/
221+
public function test_constructor_allows_overriding_request_timeout_with_null() {
222+
add_filter( 'wp_ai_client_default_request_timeout', '__return_null' );
223+
224+
$builder = new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry() );
225+
226+
/** @var RequestOptions $request_options */
227+
$request_options = $this->get_wrapped_prompt_builder_property_value( $builder, 'requestOptions' );
228+
229+
$this->assertInstanceOf( RequestOptions::class, $request_options );
230+
$this->assertNull( $request_options->getTimeout() );
231+
}
232+
233+
/**
234+
* Test that the constructor disallows overriding the default request timeout with a invalid value.
235+
*
236+
* @ticket 65094
237+
*
238+
* @expectedIncorrectUsage WP_AI_Client_Prompt_Builder::__construct
239+
*/
240+
public function test_constructor_disallows_overriding_with_negative_request_timeout() {
241+
add_filter(
242+
'wp_ai_client_default_request_timeout',
243+
static function () {
244+
return -1;
245+
}
246+
);
247+
248+
$builder = new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry() );
249+
250+
/** @var RequestOptions $request_options */
251+
$request_options = $this->get_wrapped_prompt_builder_property_value( $builder, 'requestOptions' );
252+
253+
$this->assertInstanceOf( RequestOptions::class, $request_options );
254+
$this->assertEquals( 30, $request_options->getTimeout() );
255+
}
256+
257+
/**
258+
* Test that the constructor disallows overriding the default request timeout with a invalid value.
259+
*
260+
* @ticket 65094
261+
*
262+
* @expectedIncorrectUsage WP_AI_Client_Prompt_Builder::__construct
263+
*/
264+
public function test_constructor_disallows_overriding_with_bad_request_timeout_type() {
265+
add_filter( 'wp_ai_client_default_request_timeout', '__return_empty_array' );
266+
267+
$builder = new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry() );
268+
269+
/** @var RequestOptions $request_options */
270+
$request_options = $this->get_wrapped_prompt_builder_property_value( $builder, 'requestOptions' );
271+
272+
$this->assertInstanceOf( RequestOptions::class, $request_options );
273+
$this->assertEquals( 30, $request_options->getTimeout() );
274+
}
275+
216276
/**
217277
* Test method chaining with fluent methods.
218278
*

0 commit comments

Comments
 (0)