Skip to content
Draft
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"require-dev": {
"automattic/vipwpcs": "^3.0",
"dealerdirect/phpcodesniffer-composer-installer": "^1.2.1",
"php-stubs/wordpress-stubs": "7.0 as 6.9.4",
"php-stubs/wp-cli-stubs": "^2.12",
"phpcompatibility/phpcompatibility-wp": "^3.0.0-alpha",
"phpstan/extension-installer": "^1.3",
Expand All @@ -21,7 +22,7 @@
"slevomat/coding-standard": "^8.0",
"szepeviktor/phpstan-wordpress": "^2.0.2",
"wp-coding-standards/wpcs": "^3.2.0",
"wp-phpunit/wp-phpunit": "^6.9",
"wp-phpunit/wp-phpunit": "^7.0",
"yoast/phpunit-polyfills": "^4.0"
},
"autoload": {
Expand Down Expand Up @@ -52,7 +53,7 @@
"scripts": {
"format": "phpcbf --standard=phpcs.xml.dist",
"lint": "phpcs --standard=phpcs.xml.dist",
"phpstan": "phpstan analyse --memory-limit=1G",
"phpstan": "phpstan analyse --memory-limit=2G",
"test": "phpunit --strict-coverage"
},
"scripts-descriptions": {
Expand Down
35 changes: 21 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions includes/Abilities/Image/Generate_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ protected function meta(): array {
*
* @param string $prompt The prompt to generate an image from.
* @param string|null $reference_image Optional base64-encoded image to use as a reference for edits.
* @return array{data: string, provider_metadata: array<string, string>, model_metadata: array<string, string>}|\WP_Error The generated image data, or a WP_Error on failure.
* @return array{
* data: string,
* provider_metadata: array<string, string|null>,
* model_metadata: array<string, string>
* }|\WP_Error The generated image data, or a WP_Error on failure.
*/
protected function generate_image( string $prompt, ?string $reference_image = null ) { // phpcs:ignore Generic.NamingConventions.ConstructorName.OldStyle
$prompt_builder = $this->get_prompt_builder( $prompt, $reference_image );
Expand All @@ -190,7 +194,13 @@ protected function generate_image( string $prompt, ?string $reference_image = nu
return $prompt_builder;
}

// Generate the image using the AI client.
/**
* Generate the image using the AI client.
*
* @var \WordPress\AiClient\Results\DTO\GenerativeAiResult $result
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
$result = $prompt_builder->generate_image_result();

if ( is_wp_error( $result ) ) {
Expand Down Expand Up @@ -258,7 +268,9 @@ private function get_prompt_builder( string $prompt, ?string $reference_image =
}

$prompt_builder = wp_ai_client_prompt( $prompt )
/** @phpstan-ignore argument.type (@todo This can be removed when php-ai-client uses FQCN) */
->using_request_options( $request_options )
/** @phpstan-ignore argument.type (@todo This can be removed when php-ai-client uses FQCN) */
->as_output_file_type( FileTypeEnum::inline() );

$prompt_builder = $this->set_provider_model_preference( $prompt_builder, Image_Generation_Feature::class, get_preferred_image_models() );
Expand Down
1 change: 1 addition & 0 deletions includes/Abstracts/Abstract_Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ protected function set_provider_model_preference( \WP_AI_Client_Prompt_Builder $

if ( $provider && $model ) {
$prompt_builder->using_model(
/** @phpstan-ignore argument.type (@todo This can be removed when php-ai-client uses FQCN) */
AiClient::defaultRegistry()->getProviderModel( $provider, $model )
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion includes/Abstracts/Abstract_Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class Abstract_Feature implements Feature {
* Feature identifier.
*
* @since 0.6.0
* @var non-empty-string
* @var non-empty-string&lowercase-string
*/
protected string $id;

Expand Down
21 changes: 18 additions & 3 deletions includes/Admin/Dashboard/AI_Capabilities_Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,29 @@ private function render_provider_capabilities(): void {
try {
$provider_class = $registry->getProviderClassName( $provider_id );

/** @var \WordPress\AiClient\Providers\Contracts\ProviderInterface $provider_class */
$metadata = $provider_class::metadata();
$model_dir = $provider_class::modelMetadataDirectory();
/**
* @var class-string<\WordPress\AiClient\Providers\Contracts\ProviderInterface> $provider_class
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
$metadata = $provider_class::metadata();
$model_dir = $provider_class::modelMetadataDirectory();

/**
* @var \WordPress\AiClient\Providers\Models\DTO\ModelMetadata[] $models
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
$models = $model_dir->listModelMetadata();
$capabilities = array();

foreach ( $models as $model ) {
foreach ( $model->getSupportedCapabilities() as $capability ) {
/**
* @var \WordPress\AiClient\Providers\Models\Enums\CapabilityEnum $capability
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
$capabilities[ $capability->value ] = true;
}
}
Expand Down
3 changes: 3 additions & 0 deletions includes/Asset_Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public static function enqueue_script( string $handle, string $file_name, array
wp_enqueue_script(
self::HANDLE_PREFIX . $handle,
$script_url,
/**
* @phpstan-ignore argument.type (@todo This is a type-error in WordPress 7.0)
*/
$asset_data['dependencies'],
$asset_data['version'],
$args
Expand Down
2 changes: 1 addition & 1 deletion includes/Contracts/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Feature {
*
* @since 0.6.0
*
* @return non-empty-string Feature ID.
* @return non-empty-string&lowercase-string Feature ID.
*/
public static function get_id(): string;

Expand Down
13 changes: 13 additions & 0 deletions includes/Logging/REST/AI_Request_Log_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
* @since 1.0.0
*/
class AI_Request_Log_Controller extends WP_REST_Controller {
/**
* {@inheritDoc}
*
* @var non-falsy-string
*/
protected $namespace;

/**
* {@inheritDoc}
*
* @var non-falsy-string
*/
protected $rest_base;

/**
* Log manager instance.
Expand Down
14 changes: 13 additions & 1 deletion includes/REST/Models_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,21 @@ private function build_requirements( string $capability ): ModelRequirements {
switch ( $capability ) {
case 'text_generation':
return new ModelRequirements(
/** @phpstan-ignore argument.type (@todo This can be removed when php-ai-client uses FQCN) */
array( CapabilityEnum::textGeneration() ),
array()
);

case 'image_generation':
return new ModelRequirements(
/** @phpstan-ignore argument.type (@todo This can be removed when php-ai-client uses FQCN) */
array( CapabilityEnum::imageGeneration() ),
array()
);

case 'vision':
return new ModelRequirements(
/** @phpstan-ignore argument.type (@todo This can be removed when php-ai-client uses FQCN) */
array( CapabilityEnum::textGeneration() ),
array(
new RequiredOption(
Expand Down Expand Up @@ -206,6 +209,11 @@ private function fetch_providers( ModelRequirements $requirements ): array {

foreach ( array_keys( $active_connectors ) as $connector_id ) {
try {
/**
* @var list<\WordPress\AiClient\Providers\Models\DTO\ModelMetadata> $models
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
$models = $registry->findProviderModelsMetadataForSupport( $connector_id, $requirements );

if ( empty( $models ) ) {
Expand All @@ -214,7 +222,11 @@ private function fetch_providers( ModelRequirements $requirements ): array {

$provider_class = $registry->getProviderClassName( $connector_id );

/** @var \WordPress\AiClient\Providers\Contracts\ProviderInterface $provider_class */
/**
* @var class-string<\WordPress\AiClient\Providers\Contracts\ProviderInterface> $provider_class
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
$provider_name = $provider_class::metadata()->getName();

$model_items = array();
Expand Down
4 changes: 3 additions & 1 deletion includes/Services/AI_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public function create_textgen_prompt( ?string $prompt = null, array $options =
if ( ! empty( $options ) ) {
$config_array = $this->map_options_to_config( $options );
if ( ! empty( $config_array ) ) {
$config = ModelConfig::fromArray( $config_array );
$config = ModelConfig::fromArray( $config_array );

/** @phpstan-ignore argument.type (@todo This can be removed when php-ai-client uses FQCN) */
$builder = $builder->using_model_config( $config );
}
}
Expand Down
16 changes: 15 additions & 1 deletion includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,13 +530,27 @@ function has_image_generation_support( bool $reset_cache = false ): bool {
}

try {
/**
* @var class-string<\WordPress\AiClient\Providers\Contracts\ProviderInterface> $provider_class
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
$provider_class = $registry->getProviderClassName( $connector_id );

/** @var \WordPress\AiClient\Providers\Contracts\ProviderInterface $provider_class */
/**
* @var list<\WordPress\AiClient\Providers\Models\DTO\ModelMetadata> $models
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
$models = $provider_class::modelMetadataDirectory()->listModelMetadata();

foreach ( $models as $model ) {
foreach ( $model->getSupportedCapabilities() as $capability ) {
/**
* @var \WordPress\AiClient\Providers\Models\Enums\CapabilityEnum $capability
*
* @phpstan-ignore varTag.type (@todo This can be removed when php-ai-client uses FQCN)
*/
if ( CapabilityEnum::IMAGE_GENERATION === $capability->value ) {
$has_support = true;
break 3;
Expand Down
10 changes: 0 additions & 10 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,10 @@ parameters:
- vendor/phpstan/php-8-stubs/stubs/ext/standard/str_starts_with.php
# Temporary while WP 7.0 beta AI Client migration is in progress.
ignoreErrors:
- '#Function wp_ai_client_prompt not found\.#'
- '#WP_AI_Client_[A-Za-z0-9_]+#'
- '#WordPress\\AiClient\\#'
- '#Function wp_get_connectors not found\.#'
- '#Function wp_is_connector_registered not found\.#'
- '#Function wp_get_connector not found\.#'
- '#class cli\\progress\\Bar#'
- '#Function wp_supports_ai not found\.#'
- '#Function wp_set_script_module_translations not found\.#'
excludePaths:
analyse:
- tests/
- vendor/
- includes/Logging/Logging_Integration.php
- includes/Logging/Logging_Http_Transporter.php
analyseAndScan:
- node_modules (?)
Loading