Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ class WP_AI_Client_Prompt_Builder {
*/
public function __construct( ProviderRegistry $registry, $prompt = null ) {
try {
if ( ! wp_supports_ai() ) {
// The catch block will convert this to a WP_Error.
throw new \RuntimeException( __( 'AI features are not supported in this environment.' ) );
}

$this->builder = new PromptBuilder( $registry, $prompt );
} catch ( Exception $e ) {
$this->builder = new PromptBuilder( $registry );
Expand Down Expand Up @@ -281,6 +276,14 @@ public function using_abilities( ...$abilities ): self {
* @return mixed The result of the method call.
*/
public function __call( string $name, array $arguments ) {
// Check whether AI is supported before proceeding with any method call.
if ( null === $this->error && ! wp_supports_ai() ) {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downside to this approach is that this check runs on every single successful call. We could mitigate it by adding a state property to the class, something which isn't necessary when the ::$error is set in the constructor.

$this->error = new WP_Error(
'ai_not_supported',
__( 'AI features are not supported in this environment.' )
);
}

/*
* If an error occurred in a previous method call, either return the error for terminate methods,
* or return the same instance for other methods to maintain the fluent interface.
Expand Down
Loading