Skip to content

Conversation

@Ref34t
Copy link

@Ref34t Ref34t commented Sep 5, 2025

WordPress REST API Endpoints for AI Client

This PR implements comprehensive REST API endpoints that expose all PromptBuilder capabilities through WordPress's native REST API architecture and part of Project MVP Road #1.

Overview

Adds a unified REST controller that creates individual endpoints for each PromptBuilder terminate method, enabling WordPress plugins to integrate AI functionality through familiar REST API patterns with manual registration for distributed ecosystem compatibility.

Key Features

  • 10 REST endpoints mapping 1:1 to PromptBuilder terminate methods
  • Manual registration only via REST_Route_Registrar::register_rest_routes('namespace')
  • Custom namespace support for multi-plugin compatibility
  • No auto-registration - prevents conflicts when multiple plugins bundle this library
  • Full parameter support for all PromptBuilder options
  • WordPress coding standards compliance (PHPCS + PHPStan max level)

Endpoints Added

POST /wp-json/{namespace}/prompt/generate-text
POST /wp-json/{namespace}/prompt/generate-texts
POST /wp-json/{namespace}/prompt/generate-image
POST /wp-json/{namespace}/prompt/generate-images
POST /wp-json/{namespace}/prompt/generate-speech
POST /wp-json/{namespace}/prompt/generate-speeches
POST /wp-json/{namespace}/prompt/generate-result
POST /wp-json/{namespace}/prompt/generate-text-result
POST /wp-json/{namespace}/prompt/generate-image-result
POST /wp-json/{namespace}/prompt/generate-speech-result

Usage

Manual Registration Required

// Plugin developers must manually register routes
add_action( 'rest_api_init', function() {
    REST_Route_Registrar::register_rest_routes( 'my-plugin/v1' );
} );

// Access via REST API
// POST /wp-json/my-plugin/v1/prompt/generate-text
// Body: {"prompt": "Write a story", "max_tokens": 100}

Architecture

  • Prompt_Controller - Unified controller handling all 10 endpoints with WordPress conventions
  • REST_Route_Registrar - Entry point for manual route registration
  • No Auto-Registration - Library provides classes only, plugins must opt-in
  • PHPStan Configuration - WordPress-specific static analysis with phpstan.neon.dist

Distributed Ecosystem Support

This implementation follows WordPress distributed ecosystem patterns:

  • No automatic route registration - prevents namespace conflicts
  • Plugin-controlled namespaces - each plugin chooses their own namespace
  • Library-only approach - provides functionality without imposing registration

Quality Standards

  • PHPCS - WordPress coding standards compliance
  • PHPStan - Maximum level static analysis with WordPress stubs
  • WordPress Patterns - Follows core WordPress REST API conventions

This implementation provides the foundation for WordPress plugins to integrate AI capabilities through manual REST API registration, maintaining compatibility in WordPress's distributed plugin ecosystem.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ref34t Thank you for getting this started!

One early feedback:

POST /wp-json/{namespace}/prompt/generate-text
POST /wp-json/{namespace}/prompt/generate-texts
POST /wp-json/{namespace}/prompt/generate-image
POST /wp-json/{namespace}/prompt/generate-images
POST /wp-json/{namespace}/prompt/generate-speech
POST /wp-json/{namespace}/prompt/generate-speeches
POST /wp-json/{namespace}/prompt/generate-result
POST /wp-json/{namespace}/prompt/generate-text-result
POST /wp-json/{namespace}/prompt/generate-image-result
POST /wp-json/{namespace}/prompt/generate-speech-result

I think we should only have endpoints for the generate-*-result terminate methods.

The other ones are shortcuts to get a specific part of the results, and that's easily something we can handle on the JavaScript API side. No need to have the other shortcut endpoints which are almost duplicates.

*
* @return array<string, array<string, mixed>> Request arguments schema.
*/
protected function get_common_args(): array {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message shouldn't manually define all the arguments. The PHP AI client SDK provides all the definitions (check the getJsonSchema() methods), and we can reuse those. Otherwise it's a lot of duplicate code, and it misses many arguments. No need to have validate_callbacks here either because it would be handled by default.

We need to support two arguments:

  • prompt: I think we can make this a oneOf of:
    • either array of Message DTOs
    • or a single Message DTO
    • or a single MessagePart DTO
    • or a single string
  • config: A single ModelConfig DTO

For all of those we can combine the schemas from the respective DTOs to get this overall args array.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixarntz handled here 1cf0a2a

Comment on lines 88 to 96
register_rest_route(
$this->namespace,
$route,
array(
'methods' => 'POST',
'callback' => array( $this, $callback ),
'args' => $common_args,
'permission_callback' => array( $this, 'check_permission' ),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also register the response schema for the routes here.

Per my previous comment, by only implementing the generate-*-result endpoints, this is easy: We can simply use GenerativeAiResult::getJsonSchema().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixarntz handled here 1cf0a2a

@Ref34t Ref34t requested a review from felixarntz September 11, 2025 07:15
@JasonTheAdams JasonTheAdams mentioned this pull request Oct 8, 2025
6 tasks
@felixarntz felixarntz added this to the 0.2.0 milestone Oct 29, 2025
@felixarntz felixarntz added the [Feature] New feature to highlight in changelogs. label Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] New feature to highlight in changelogs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants