Skip to content
Merged
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
63 changes: 12 additions & 51 deletions src/AI_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,13 @@ class AI_Command extends WP_CLI_Command {
public function generate( $args, $assoc_args ) {
list( $type, $prompt ) = $args;

// @phpstan-ignore function.notFound
if ( ! wp_supports_ai() ) {
WP_CLI::error( 'AI features are not supported in this environment.' );
}

try {
// @phpstan-ignore function.notFound
$builder = wp_ai_client_prompt( $prompt );

if ( is_wp_error( $builder ) ) {
WP_CLI::error( $builder->get_error_message() );
}

if ( isset( $assoc_args['provider'] ) ) {
$builder = $builder->using_provider( $assoc_args['provider'] );
}
Expand Down Expand Up @@ -231,7 +225,6 @@ public function generate( $args, $assoc_args ) {
* @return void
*/
public function is_supported( $args, $assoc_args ) {
// @phpstan-ignore function.notFound
if ( wp_supports_ai() ) {
WP_CLI::halt( 0 );
} else {
Expand Down Expand Up @@ -271,19 +264,13 @@ public function check( $args, $assoc_args ) {
list( $prompt ) = $args;
$type = $assoc_args['type'] ?? 'text';

// @phpstan-ignore function.notFound
if ( ! wp_supports_ai() ) {
WP_CLI::error( 'AI features are not supported in this environment.' );
}

try {
// @phpstan-ignore function.notFound
$builder = wp_ai_client_prompt( $prompt );

if ( is_wp_error( $builder ) ) {
WP_CLI::error( $builder->get_error_message() );
}

if ( 'text' === $type ) {
$supported = $builder->is_supported_for_text_generation();
if ( $supported ) {
Expand Down Expand Up @@ -342,13 +329,8 @@ public function check( $args, $assoc_args ) {
*/
public function status( $args, $assoc_args ) {
try {
// @phpstan-ignore function.notFound
$builder = wp_ai_client_prompt();

if ( is_wp_error( $builder ) ) {
WP_CLI::error( $builder->get_error_message() );
}

// Check each capability
$capabilities = array(
array(
Expand Down Expand Up @@ -391,21 +373,17 @@ public function status( $args, $assoc_args ) {
/**
* Generates text from the prompt builder.
*
* @param \WordPress\AI_Client\Builders\Prompt_Builder $builder The prompt builder.
* @param array{format: string} $assoc_args Associative arguments.
* @param \WP_AI_Client_Prompt_Builder $builder The prompt builder.
* @param array{format: string} $assoc_args Associative arguments.
* @return void
*
* @phpstan-ignore class.notFound
*/
private function generate_text( $builder, $assoc_args ) {
$format = $assoc_args['format'] ?? 'text';

// @phpstan-ignore class.notFound
if ( ! $builder->is_supported_for_text_generation() ) {
WP_CLI::error( 'Text generation is not supported. Make sure AI provider credentials are configured.' );
}

// @phpstan-ignore class.notFound
$text = $builder->generate_text_result();

if ( is_wp_error( $text ) ) {
Expand All @@ -429,11 +407,8 @@ private function generate_text( $builder, $assoc_args ) {
"Summary:\nModel used: %s (%s)\nToken usage:\nInput tokens: %s\nOutput tokens: %s\nTotal: %s\n",
$text->getModelMetadata()->getName(),
$text->getProviderMetadata()->getName(),
// @phpstan-ignore class.notFound
$token_usage[ TokenUsage::KEY_PROMPT_TOKENS ],
// @phpstan-ignore class.notFound
$token_usage[ TokenUsage::KEY_COMPLETION_TOKENS ],
// @phpstan-ignore class.notFound
$token_usage[ TokenUsage::KEY_TOTAL_TOKENS ]
),
'ai'
Expand All @@ -443,14 +418,11 @@ private function generate_text( $builder, $assoc_args ) {
/**
* Generates an image from the prompt builder.
*
* @param \WordPress\AI_Client\Builders\Prompt_Builder $builder The prompt builder.
* @param \WP_AI_Client_Prompt_Builder $builder The prompt builder.
* @param array{'destination-file': string, stdout: bool} $assoc_args Associative arguments.
* @return void
*
* @phpstan-ignore class.notFound
*/
private function generate_image( $builder, $assoc_args ) {
// @phpstan-ignore class.notFound
if ( ! $builder->is_supported_for_image_generation() ) {
WP_CLI::error( 'Image generation is not supported. Make sure AI provider credentials are configured.' );
}
Expand All @@ -468,7 +440,6 @@ private function generate_image( $builder, $assoc_args ) {
}
}

// @phpstan-ignore class.notFound
$image_file = $builder->generate_image();

if ( is_wp_error( $image_file ) ) {
Expand Down Expand Up @@ -528,11 +499,9 @@ private function generate_image( $builder, $assoc_args ) {
*
* Accepts a local file path, a URL (http/https), a data URI, or a WordPress attachment ID.
*
* @param \WordPress\AI_Client\Builders\Prompt_Builder $builder The prompt builder.
* @param string $image_input Local file path, URL, data URI, or attachment ID.
* @return mixed The updated prompt builder.
*
* @phpstan-ignore class.notFound
* @param \WP_AI_Client_Prompt_Builder $builder The prompt builder.
* @param string $image_input Local file path, URL, data URI, or attachment ID.
* @return \WP_AI_Client_Prompt_Builder The updated prompt builder.
*/
private function with_image_input( $builder, $image_input ) {
// Attachment ID (positive integer string).
Expand All @@ -546,7 +515,6 @@ private function with_image_input( $builder, $image_input ) {
0 === strpos( $image_input, 'http://' ) ||
0 === strpos( $image_input, 'https://' )
) {
// @phpstan-ignore class.notFound
return $builder->with_file( $image_input );
}

Expand All @@ -557,11 +525,9 @@ private function with_image_input( $builder, $image_input ) {
/**
* Adds a WordPress attachment as image input to the prompt builder.
*
* @param \WordPress\AI_Client\Builders\Prompt_Builder $builder The prompt builder.
* @param int $attachment_id The attachment ID.
* @return mixed The updated prompt builder.
*
* @phpstan-ignore class.notFound
* @param \WP_AI_Client_Prompt_Builder $builder The prompt builder.
* @param int $attachment_id The attachment ID.
* @return \WP_AI_Client_Prompt_Builder The updated prompt builder.
*/
private function with_attachment_input( $builder, $attachment_id ) {
$attachment = get_post( $attachment_id );
Expand Down Expand Up @@ -595,14 +561,12 @@ private function with_attachment_input( $builder, $attachment_id ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$data_uri = 'data:' . $mime_type . ';base64,' . base64_encode( $contents );

// @phpstan-ignore class.notFound
return $builder->with_file( $data_uri );
}

// Fall back to the attachment URL.
$image_src = wp_get_attachment_image_src( $attachment_id, 'full' );
if ( $image_src && ! empty( $image_src[0] ) ) {
// @phpstan-ignore class.notFound
return $builder->with_file( $image_src[0] );
}

Expand All @@ -612,11 +576,9 @@ private function with_attachment_input( $builder, $attachment_id ) {
/**
* Adds a local image file as input to the prompt builder.
*
* @param \WordPress\AI_Client\Builders\Prompt_Builder $builder The prompt builder.
* @param string $file_path The local file path.
* @return mixed The updated prompt builder.
*
* @phpstan-ignore class.notFound
* @param \WP_AI_Client_Prompt_Builder $builder The prompt builder.
* @param string $file_path The local file path.
* @return \WP_AI_Client_Prompt_Builder The updated prompt builder.
*/
private function with_local_file_input( $builder, $file_path ) {
if ( ! file_exists( $file_path ) ) {
Expand All @@ -638,7 +600,6 @@ private function with_local_file_input( $builder, $file_path ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$data_uri = 'data:' . $mime_type . ';base64,' . base64_encode( $contents );

// @phpstan-ignore class.notFound
return $builder->with_file( $data_uri );
}
}
2 changes: 0 additions & 2 deletions src/Connectors_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class Connectors_Command extends WP_CLI_Command {
* @return void
*/
public function list_( $args, $assoc_args ) {
// @phpstan-ignore function.notFound
$connectors = wp_get_connectors();

$items = array();
Expand Down Expand Up @@ -159,7 +158,6 @@ static function ( array $item ) use ( $status_filter ) {
public function get( $args, $assoc_args ) {
list( $connector_id ) = $args;

// @phpstan-ignore function.notFound
$connector = wp_get_connector( $connector_id );

if ( ! $connector ) {
Expand Down