Filter
uncanny-automator
automator_ai_providers_http_client_timeout
Filters the HTTP client timeout value for AI provider requests, allowing customization of connection duration.
add_filter( 'automator_ai_providers_http_client_timeout', $callback, 10, 1 );
Description
Filters the HTTP client timeout for AI provider requests. Developers can modify this value (defaulting to 120 seconds) to control how long the system waits for a response from an AI service. This is crucial for managing performance and preventing excessively long requests.
Usage
add_filter( 'automator_ai_providers_http_client_timeout', 'your_function_name', 10, 1 );
Parameters
-
$url(mixed) - This parameter represents the HTTP client timeout in seconds.
Return Value
The filtered value.
Examples
/**
* Increase the HTTP client timeout for specific AI provider requests.
*
* This filter allows developers to customize the timeout for HTTP requests made
* to AI providers, potentially for requests that might take longer to process.
*
* @param int $timeout The default timeout in seconds.
* @param array $context An array containing context about the request.
* Includes 'url', 'body', and 'headers'.
*/
add_filter( 'automator_ai_providers_http_client_timeout', function( $timeout, $context ) {
// If the request is for a specific OpenAI endpoint and the body contains
// a large context window, increase the timeout to 300 seconds.
if ( str_contains( $context['url'], 'openai.com/v1/chat/completions' ) && isset( $context['body']['messages'] ) && count( $context['body']['messages'] ) > 50 ) {
return 300;
}
// For all other requests, maintain the default timeout.
return $timeout;
}, 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/lib/ai/adapters/http/integration-http-client.php:173
public function post( string $url, array $body, array $headers ): array {
if ( empty( $url ) ) {
throw new AI_Service_Exception( 'URL cannot be empty.' );
}
// Validate arrays are countable (PHP 7.3+ robustness)
if ( ! is_countable( $body ) || ! is_countable( $headers ) ) {
throw new AI_Service_Exception( 'Body and headers must be arrays.' );
}
$args = array(
'headers' => $headers,
'body' => wp_json_encode( $body ),
'timeout' => apply_filters(
'automator_ai_providers_http_client_timeout',
120,
array(
'url' => $url,
'body' => $body,
'headers' => $headers,
),
),
);
$response = wp_remote_post( $url, $args );
if ( is_wp_error( $response ) ) {
throw new AI_Service_Exception(
sprintf(
'HTTP request failed: %s',
esc_html( $response->get_error_message() ),
),
);
}
$code = (int) wp_remote_retrieve_response_code( $response );
$data = wp_remote_retrieve_body( $response );
$decoded = json_decode( $data, true );
if ( ! in_array( $code, array( 200, 201 ), true ) ) {
throw new AI_Service_Exception(
sprintf(
'Request failed with status code %d. Response: %s',
esc_html( $code ),
esc_html( $data ),
),
);
}
if ( null === $decoded ) {
throw new AI_Service_Exception(
sprintf(
'Invalid JSON response from %s. JSON Error: %s. Response: %s',
esc_url( $url ),
esc_html( json_last_error_msg() ),
esc_html( $data ),
),
);
}
$this->get_api_server_credit_adapter()->reduce_credits();
return $decoded;
}
Internal Usage
Found in src/core/lib/ai/adapters/http/integration-http-client.php:140:
* add_filter('automator_ai_providers_http_client_timeout', function($timeout, $context) {