Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/wp-includes/ai-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
* @return WP_AI_Client_Prompt_Builder The prompt builder instance.
*/
function wp_ai_client_prompt( $prompt = null ) {
return new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), $prompt );
return new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), $prompt, AiClient::getEventDispatcher() );
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use WordPress\AiClient\Tools\DTO\FunctionDeclaration;
use WordPress\AiClient\Tools\DTO\FunctionResponse;
use WordPress\AiClient\Tools\DTO\WebSearch;
use WordPress\AiClientDependencies\Psr\EventDispatcher\EventDispatcherInterface;

/**
* Fluent builder for constructing AI prompts, returning WP_Error on failure.
Expand Down Expand Up @@ -173,12 +174,13 @@ class WP_AI_Client_Prompt_Builder {
* message array shape, or a list of
* parts or messages for multi-turn
* conversations. Default null.
* @param EventDispatcherInterface|null $event_dispatcher Optional. Event dispatcher for prompt lifecycle events.
*/
public function __construct( ProviderRegistry $registry, $prompt = null ) {
public function __construct( ProviderRegistry $registry, $prompt = null, ?EventDispatcherInterface $event_dispatcher = null ) {
try {
$this->builder = new PromptBuilder( $registry, $prompt );
$this->builder = new PromptBuilder( $registry, $prompt, $event_dispatcher );
} catch ( Exception $e ) {
$this->builder = new PromptBuilder( $registry );
$this->builder = new PromptBuilder( $registry, null, $event_dispatcher );
$this->error = new WP_Error(
'prompt_builder_error',
$e->getMessage(),
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use WordPress\AiClient\Builders\PromptBuilder;
use WordPress\AiClient\Tools\DTO\FunctionDeclaration;
use WordPress\AiClient\Tools\DTO\FunctionResponse;
use WordPress\AiClientDependencies\Psr\EventDispatcher\EventDispatcherInterface;

require_once dirname( __DIR__, 2 ) . '/includes/wp-ai-client-mock-model-creation-trait.php';
require_once dirname( __DIR__, 2 ) . '/includes/wp-ai-client-test-abilities-trait.php';
Expand Down Expand Up @@ -1405,6 +1406,45 @@ public function test_wp_ai_client_prompt_empty_string_returns_wp_error() {
$this->assertSame( 'prompt_builder_error', $result->get_error_code() );
}

/**
* Tests that wp_ai_client_prompt() passes the AI client event dispatcher to the wrapped prompt builder.
*
* @ticket 64938
*/
public function test_wp_ai_client_prompt_passes_event_dispatcher() {
$original_event_dispatcher = AiClient::getEventDispatcher();
$event_dispatcher = new WP_AI_Client_Event_Dispatcher();

AiClient::setEventDispatcher( $event_dispatcher );

try {
$builder = wp_ai_client_prompt( 'Test prompt' );

$this->assertSame(
$event_dispatcher,
$this->get_wrapped_prompt_builder_property_value( $builder, 'eventDispatcher' )
);
} finally {
AiClient::setEventDispatcher( $original_event_dispatcher );
}
}

/**
* Tests that the prompt builder constructor forwards an event dispatcher to the wrapped SDK prompt builder.
*
* @ticket 64938
*/
public function test_constructor_passes_event_dispatcher() {
$event_dispatcher = $this->createMock( EventDispatcherInterface::class );

$builder = new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), 'Test prompt', $event_dispatcher );

$this->assertSame(
$event_dispatcher,
$this->get_wrapped_prompt_builder_property_value( $builder, 'eventDispatcher' )
);
}

/**
* Tests generateResult with text output modality.
*
Expand Down
Loading