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. *