@@ -323,6 +326,11 @@ private function render_test_runner(): void {
+
+
+
@@ -483,6 +491,116 @@ public function ajax_invoke_ability(): void {
}
}
+ /**
+ * AJAX handler for AI-assisted payload generation.
+ *
+ * @since 1.0.1
+ */
+ public function ajax_generate_payload(): void {
+ // Verify nonce.
+ check_ajax_referer( 'ai_ability_explorer_generate_payload', 'nonce' );
+
+ // Check user capabilities.
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'Insufficient permissions.', 'ai' ),
+ )
+ );
+ }
+
+ // Get parameters.
+ $ability_slug = isset( $_POST['ability'] ) ? sanitize_text_field( wp_unslash( $_POST['ability'] ) ) : '';
+ $command = isset( $_POST['command'] ) ? sanitize_textarea_field( wp_unslash( $_POST['command'] ) ) : '';
+
+ if ( empty( $ability_slug ) ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'Ability slug is required.', 'ai' ),
+ )
+ );
+ }
+
+ if ( empty( $command ) ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'A command is required.', 'ai' ),
+ )
+ );
+ }
+
+ // Get ability to retrieve its input schema.
+ $ability = Ability_Handler::get_ability( $ability_slug );
+
+ if ( ! $ability ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'Ability not found.', 'ai' ),
+ )
+ );
+ }
+
+ if ( ! function_exists( 'wp_ai_client_prompt' ) ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'AI provider is not available.', 'ai' ),
+ )
+ );
+ }
+
+ $schema_json = wp_json_encode( $ability['input_schema'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
+
+ $system_instruction = 'You are a JSON payload generator. Given an input schema and a user command, generate a valid JSON object that satisfies the schema and fulfills the command. Return ONLY valid JSON with no explanation, no markdown, and no code fences.';
+
+ $user_prompt = sprintf(
+ "Input Schema:\n%s\n\nUser Command: %s",
+ $schema_json,
+ $command
+ );
+
+ $prompt_builder = wp_ai_client_prompt( $user_prompt )
+ ->using_system_instruction( $system_instruction );
+
+ if ( ! $prompt_builder->is_supported_for_text_generation() ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'No AI provider available. Please connect one in the plugin settings.', 'ai' ),
+ )
+ );
+ }
+
+ $result = $prompt_builder->generate_text();
+
+ if ( is_wp_error( $result ) ) {
+ wp_send_json_error(
+ array(
+ 'message' => $result->get_error_message(),
+ )
+ );
+ }
+
+ // Strip markdown code fences if the model includes them.
+ $raw = trim( (string) $result );
+ $raw = preg_replace( '/^```(?:json)?\s*/i', '', $raw ) ?? $raw;
+ $raw = preg_replace( '/\s*```$/', '', $raw ) ?? $raw;
+ $raw = trim( $raw );
+
+ $parsed = json_decode( $raw, true );
+ if ( json_last_error() !== JSON_ERROR_NONE ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'The AI returned a response that could not be parsed as JSON. Please try again or rephrase your command.', 'ai' ),
+ )
+ );
+ }
+
+ wp_send_json_success(
+ array(
+ 'payload' => wp_json_encode( $parsed, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
+ )
+ );
+ }
+
/**
* Add contextual help tabs to the screen.
*
@@ -500,7 +618,7 @@ public function add_help_tabs(): void {
'id' => 'abilities-overview',
'title' => __( 'Overview', 'ai' ),
'content' =>
- '
' . esc_html__( 'Abilities are a standardized way for WordPress core, plugins, and themes to expose discrete units of functionality. Each ability has a name, optional input/output schemas, and can be invoked programmatically.', 'ai' ) . '
' .
+ '
' . esc_html__( 'Abilities are a standardized way for WordPress core, plugins, and themes to expose discrete units of functionality. Each ability has a name, optional input/output schemas, and can be invoked programmatically.', 'ai' ) . '
' .
'
' . esc_html__( 'The Abilities Explorer lets you browse every registered ability, inspect its schemas, and test it with custom input right from the admin.', 'ai' ) . '