registered) { return; } add_filter('http_request_args', [$this, 'filterRequestArgs'], self::PRIORITY, 2); $this->registered = true; } /** * Unregisters the timeout filter. * * @return void * @since 1.1.5 */ public function unregister(): void { if (! $this->registered) { return; } remove_filter('http_request_args', [$this, 'filterRequestArgs'], self::PRIORITY); $this->registered = false; } /** * Raises the timeout for requests to AI provider hosts. * * Never lowers an already higher timeout. * * @param array $args Request arguments. * @param string $url Request URL. * * @return array * @since 1.1.5 */ public function filterRequestArgs(array $args, string $url): array { if (! $this->isProviderUrl($url)) { return $args; } /** * Filters the HTTP timeout (seconds) applied to AI provider requests. * * @param int $timeout Timeout in seconds. * * @since 1.1.5 */ $timeout = (float) apply_filters('agent_mod_ai_http_timeout', Constants::AI_HTTP_TIMEOUT); $current = isset($args['timeout']) ? (float) $args['timeout'] : 0.0; $args['timeout'] = max($current, $timeout); return $args; } /** * Whether the URL targets a known AI provider host. * * @param string $url Request URL. * * @return bool * @since 1.1.5 */ private function isProviderUrl(string $url): bool { $host = wp_parse_url($url, PHP_URL_HOST); if (! is_string($host) || '' === $host) { return false; } /** * Filters the AI provider hosts whose requests get the raised timeout. * * @param string[] $hosts Provider hostnames. * * @since 1.1.5 */ $hosts = (array) apply_filters('agent_mod_ai_http_hosts', [ 'api.openai.com', 'api.anthropic.com', 'generativelanguage.googleapis.com', ]); return in_array(strtolower($host), array_map('strtolower', $hosts), true); } }