From 9a3ef46dda449fcb513c20c0e84a102cfdbb0e9a Mon Sep 17 00:00:00 2001 From: Jignesh Bhavani Date: Sat, 1 Aug 2026 18:41:48 +0530 Subject: [PATCH] AI: Return false from support checks when the wrapped builder throws. WP_AI_Client_Prompt_Builder::__call() only special-cased generating methods when converting a caught exception into the error state, so is_supported() and its siblings fell through to the fluent return and handed back the builder instance. That instance is truthy, so a failed support check read as supported. Return false from support check methods instead, matching what they already return when the builder is in a pre-existing error state. --- .../class-wp-ai-client-prompt-builder.php | 3 +++ .../ai-client/wpAiClientPromptBuilder.php | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php b/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php index a64957fe73157..8e2c04ba05be3 100644 --- a/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php +++ b/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php @@ -364,6 +364,9 @@ public function __call( string $name, array $arguments ) { if ( self::is_generating_method( $name ) ) { return $this->error; } + if ( self::is_support_check_method( $name ) ) { + return false; + } return $this; } } diff --git a/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php b/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php index 0669be8cc7bb4..bfec952718396 100644 --- a/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php +++ b/tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php @@ -2648,6 +2648,28 @@ public function test_support_check_methods_return_false_in_error_state() { $this->assertFalse( $prompt_builder->is_supported_for_text_generation(), 'is_supported_for_text_generation should return false when in error state' ); } + /** + * Tests that support check methods return false when the wrapped builder throws. + * + * @ticket 65781 + */ + public function test_support_check_methods_return_false_when_builder_throws() { + $registry = AiClient::defaultRegistry(); + $prompt_builder = new WP_AI_Client_Prompt_Builder( $registry, 'Test text' ); + + /* + * The document modality has no capability to infer, so the wrapped builder + * throws while determining whether the prompt is supported. + */ + $result = $prompt_builder->as_output_modalities( ModalityEnum::document() )->is_supported(); + + $this->assertIsBool( $result, 'is_supported should return a boolean' ); + $this->assertFalse( $result, 'is_supported should return false when the wrapped builder throws' ); + + // The error is still recorded, so a generating method returns it. + $this->assertWPError( $prompt_builder->generate_text(), 'The caught error should still be returned by generating methods' ); + } + /** * Tests that generating methods return WP_Error when in error state. *