Skip to content

Commit 9e466b2

Browse files
Merge pull request #29 from WordPress/enhance/default-http-timeout
2 parents a36cbff + fb18df4 commit 9e466b2

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

includes/Builders/Prompt_Builder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ class Prompt_Builder {
112112
*/
113113
public function __construct( ProviderRegistry $registry, $prompt = null ) {
114114
$this->builder = new PromptBuilder( $registry, $prompt );
115+
116+
/**
117+
* Filters the default request timeout in seconds for AI Client HTTP requests.
118+
*
119+
* @since n.e.x.t
120+
*
121+
* @param int $default_timeout The default timeout in seconds.
122+
*/
123+
$default_timeout = (int) apply_filters( 'wp_ai_client_default_request_timeout', 30 );
124+
125+
$this->builder->usingRequestOptions(
126+
RequestOptions::fromArray(
127+
array(
128+
RequestOptions::KEY_TIMEOUT => $default_timeout,
129+
)
130+
)
131+
);
115132
}
116133

117134
/**

tests/phpunit/tests/Builders/Prompt_Builder_Tests.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use WordPress\AiClient\Providers\DTO\ProviderMetadata;
3030
use WordPress\AiClient\Providers\DTO\ProviderModelsMetadata;
3131
use WordPress\AiClient\Providers\Enums\ProviderTypeEnum;
32+
use WordPress\AiClient\Providers\Http\DTO\RequestOptions;
3233
use WordPress\AiClient\Providers\Models\Contracts\ModelInterface;
3334
use WordPress\AiClient\Providers\Models\DTO\ModelConfig;
3435
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;
@@ -139,6 +140,39 @@ public function test_instantiation_with_prompt(): void {
139140
$this->assertInstanceOf( Prompt_Builder::class, $prompt_builder );
140141
}
141142

143+
/**
144+
* Test that the constructor sets the default request timeout.
145+
*/
146+
public function test_constructor_sets_default_request_timeout(): void {
147+
$builder = new Prompt_Builder( AiClient::defaultRegistry() );
148+
149+
/** @var RequestOptions $request_options */
150+
$request_options = $this->get_wrapped_prompt_builder_property_value( $builder, 'requestOptions' );
151+
152+
$this->assertInstanceOf( RequestOptions::class, $request_options );
153+
$this->assertEquals( 30, $request_options->getTimeout() );
154+
}
155+
156+
/**
157+
* Test that the constructor allows overriding the default request timeout.
158+
*/
159+
public function test_constructor_allows_overriding_request_timeout(): void {
160+
add_filter(
161+
'wp_ai_client_default_request_timeout',
162+
static function () {
163+
return 45;
164+
}
165+
);
166+
167+
$builder = new Prompt_Builder( AiClient::defaultRegistry() );
168+
169+
/** @var RequestOptions $request_options */
170+
$request_options = $this->get_wrapped_prompt_builder_property_value( $builder, 'requestOptions' );
171+
172+
$this->assertInstanceOf( RequestOptions::class, $request_options );
173+
$this->assertEquals( 45, $request_options->getTimeout() );
174+
}
175+
142176
/**
143177
* Test method chaining with fluent methods.
144178
*

0 commit comments

Comments
 (0)