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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ext-json": "*",
"php": ">=7.4",
"wordpress/mcp-adapter": "^0.3.0",
"wordpress/wp-ai-client": "^0.2.0"
"wordpress/wp-ai-client": "dev-trunk"
},
"require-dev": {
"automattic/vipwpcs": "^3.0",
Expand Down
53 changes: 28 additions & 25 deletions composer.lock

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

1 change: 1 addition & 0 deletions includes/Experiment_Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
*
* @param \WordPress\AI\Experiment_Registry $registry The experiment registry instance.
*/
do_action( 'ai_experiments_register_experiments', $this->registry );

Check failure on line 94 in includes/Experiment_Loader.php

View workflow job for this annotation

GitHub Actions / Run Plugin Check

WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

Hook names invoked by a theme/plugin should start with the theme/plugin prefix. Found: "ai_experiments_register_experiments".
}

/**
Expand All @@ -107,6 +107,7 @@
\WordPress\AI\Experiments\Image_Generation\Image_Generation::class,
\WordPress\AI\Experiments\Title_Generation\Title_Generation::class,
\WordPress\AI\Experiments\Excerpt_Generation\Excerpt_Generation::class,
\WordPress\AI\Experiments\AI_Playground\AI_Playground::class,
);

/**
Expand All @@ -119,7 +120,7 @@
*
* @param array $experiment_classes Array of experiment class names or instances.
*/
$items = apply_filters( 'ai_experiments_default_experiment_classes', $experiment_classes );

Check failure on line 123 in includes/Experiment_Loader.php

View workflow job for this annotation

GitHub Actions / Run Plugin Check

WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

Hook names invoked by a theme/plugin should start with the theme/plugin prefix. Found: "ai_experiments_default_experiment_classes".

$experiments = array();
foreach ( $items as $item ) {
Expand Down Expand Up @@ -190,7 +191,7 @@
*
* @param bool $enabled Whether to enable AI experiments.
*/
$experiments_enabled = apply_filters( 'ai_experiments_enabled', true );

Check failure on line 194 in includes/Experiment_Loader.php

View workflow job for this annotation

GitHub Actions / Run Plugin Check

WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

Hook names invoked by a theme/plugin should start with the theme/plugin prefix. Found: "ai_experiments_enabled".

if ( ! $experiments_enabled ) {
$this->initialized = true;
Expand Down
172 changes: 172 additions & 0 deletions includes/Experiments/AI_Playground/AI_Playground.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php
/**
* AI Playground experiment implementation.
*
* @package WordPress\AI
*/

declare( strict_types=1 );

namespace WordPress\AI\Experiments\AI_Playground;

use WordPress\AI\Abstracts\Abstract_Experiment;
use WordPress\AI\Asset_Loader;
use WordPress\AI_Client\Capabilities\Capabilities_Manager;

/**
* AI Playground experiment.
*
* @since n.e.x.t
*/
class AI_Playground extends Abstract_Experiment {

/**
* {@inheritDoc}
*
* @since n.e.x.t
*
* @return array{id: string, label: string, description: string} Experiment metadata.
*/
protected function load_experiment_metadata(): array {
return array(
'id' => 'ai-playground',
'label' => __( 'AI Playground', 'ai' ),
'description' => __( 'Adds a playground UI to explore prompting AI models directly', 'ai' ),
);
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
public function register(): void {
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
add_action( 'admin_menu', array( $this, 'add_playground_screen' ) );
}

/**
* Registers the REST routes for the AI Playground.
*
* @since n.e.x.t
*/
public function register_rest_routes(): void {
$messages_controller = new AI_Playground_Messages_REST_Controller();
$messages_controller->register_routes();
}

/**
* Adds the AI Playground admin screen.
*
* @since n.e.x.t
*/
public function add_playground_screen(): void {
$hook_suffix = add_management_page(
__( 'AI Playground', 'ai' ),
__( 'AI Playground', 'ai' ),
// phpcs:ignore WordPress.WP.Capabilities.Undetermined
Capabilities_Manager::PROMPT_AI_CAPABILITY,
'ai-playground',
array( $this, 'render_playground_screen' )
);

add_action( "load-$hook_suffix", array( $this, 'load_playground_screen' ) );
}

/**
* Loads the AI Playground admin screen.
*
* @since n.e.x.t
*/
public function load_playground_screen(): void {
add_filter(
'admin_body_class',
static function ( $classes ) {
return "$classes remove-screen-spacing";
}
);

add_action(
'admin_notices',
static function () {
remove_all_actions( 'admin_notices' );
},
-9999
);

add_action(
'admin_enqueue_scripts',
function (): void {
// Enqueue the WordPress AI Client.
wp_enqueue_script( 'wp-ai-client' );

// Enqueue the media library.
wp_enqueue_media();

// Enqueue foundational stylesheets for the UI.
wp_enqueue_style(
'ai_wp-interface',
AI_EXPERIMENTS_PLUGIN_URL . 'build/external/wp-interface/style.css',
array( 'wp-components', 'wp-editor' ),
'1.0.0'
);
wp_style_add_data( 'ai_wp-interface', 'rtl', 'replace' );
wp_enqueue_style(
'ai_wp-admin-components',
AI_EXPERIMENTS_PLUGIN_URL . 'build/external/wp-admin-components/style.css',
array( 'wp-components' ),
'1.0.0'
);
wp_style_add_data( 'ai_wp-admin-components', 'rtl', 'replace' );

// Enqueue AI Playground assets.
Asset_Loader::enqueue_script( 'playground', 'experiments/ai-playground' );
Asset_Loader::enqueue_style( 'playground', 'experiments/style-ai-playground' );

// Preload REST API data.
$this->preload_rest_api_data();
}
);
}

/**
* Renders the AI Playground admin screen.
*
* @since n.e.x.t
*/
public function render_playground_screen(): void {
?>
<div id="ai-playground-root" class="wrap">
<?php esc_html_e( 'Loading…', 'ai' ); ?>
</div>
<?php
}

/**
* Preloads REST API data for the AI Playground.
*
* This avoids an extra round-trip when the admin screen is loaded.
*
* @since n.e.x.t
*/
private function preload_rest_api_data(): void {
$preload_paths = array(
'/ai/v1/playground-messages',
);

$preload_data = array_reduce(
$preload_paths,
'rest_preload_api_request',
array()
);

wp_add_inline_script(
'wp-api-fetch',
sprintf(
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
wp_json_encode( $preload_data )
),
'after'
);
}
}
Loading
Loading