Adds AI REST endpoints and JS client#10915
Adds AI REST endpoints and JS client#10915JasonTheAdams wants to merge 17 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
As a note, I wouldn't consider this something critical to get into core. If we don't want this on 7.0 it shouldn't hold up #10881. I'm porting this over for consideration since it does exist in the WP AI Client. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Trac ticket: https://core.trac.wordpress.org/ticket/64591
Merge Proposal: https://make.wordpress.org/core/2026/02/03/proposal-for-merging-wp-ai-client-into-wordpress-7-0
This is a part of #10881. I meant for this to stack on that so the changes in this are clear, but unfortunately I can't stack because both branches are in my forked repo.
Summary
Ports the REST API, JavaScript client, and capabilities system from the WP AI Client plugin into WordPress Core.
This gives WordPress a complete client-side AI API that mirrors the server-side
wp_ai_client_prompt()API already in Core, along with REST endpoints that the JavaScript layer (and any external consumer) can use.What's being ported
REST API (
wp-ai/v1)POST /wp-ai/v1/generate— Sends a prompt to an AI model and returns aGenerativeAiResult. Accepts messages, model config, provider/model selection, model preferences, and request options.POST /wp-ai/v1/is-supported— Checks whether the current prompt configuration is supported by any available model, without actually running the generation.GET /wp-ai/v1/providers— Lists all registered AI providers.GET /wp-ai/v1/providers/{id}— Retrieves a single provider's metadata.GET /wp-ai/v1/providers/{id}/models— Lists models available from a specific provider (requires the provider to be configured with credentials).GET /wp-ai/v1/providers/{id}/models/{modelId}— Retrieves a single model's metadata.The generate/is-supported endpoints wrap
wp_ai_client_prompt()— the same public API that server-side PHP consumers use — so behavior is consistent between PHP and JS.JavaScript Client (
wp-ai-client)A
wp.aiClientglobal (registered as thewp-ai-clientscript handle) that provides:wp.aiClient.prompt()— A fluentPromptBuilderclass mirroring the PHP API:.withText(),.usingModel(),.usingTemperature(),.generateText(),.generateImage(),.isSupported(), etc. All generation methods return Promises that call the REST endpoints.@wordpress/datastore (wp-ai-client/providers-models) with selectors/resolvers forgetProviders(),getProvider(),getProviderModels(),getProviderModel(). Data is fetched lazily via the REST API and cached in the store.Capability,MessageRole,FileType,FinishReason,Modality,ProviderType, etc., matching the PHP SDK enums.GenerativeAiResultwrapper with convenience extraction methods (toText(),toTexts(),toFile(),toImageFile(),toAudioFile(), etc.).Capabilities
Three new meta-capabilities gate access to the REST endpoints:
prompt_aigenerate,is-supportedmanage_optionslist_ai_providersproviders(list & single)manage_optionslist_ai_modelsproviders/{id}/modelsroutesmanage_optionsThese are implemented as
user_has_capfilter callbacks (same pattern asinstall_languages,resume_plugins, etc.) so they can be removed and replaced by plugins or site-specific code.JSON Schema Converter
A small utility (
WP_AI_Client_JSON_Schema_Converter) that converts standard JSON Schemarequiredarrays into the per-propertyrequired: truebooleans that WordPress REST API validation expects. Used to bridge the SDK'sgetJsonSchema()output into valid WP REST args.Open question: capabilities direction
The current approach follows the upstream plugin exactly — three separate meta-capabilities dynamically granted via
user_has_capfilters to anyone withmanage_options. This is simple and removable, but worth discussing:prompt_aidefault to all administrators, or be more restrictive? AI generation can have cost implications (API credits), and the current gate (manage_options) means any admin on a multisite network could generate content. Tying it to a more specific primitive capability or making it opt-in might be more appropriate for Core.list_ai_providersandlist_ai_modelsworth separating fromprompt_ai? In practice, anyone who can prompt also needs to know what's available. Merging them into a single capability (or making listing public to all authenticated users) would simplify the permission model.map_meta_capinstead ofuser_has_cap? Theuser_has_capfilter pattern works but is less discoverable than amap_meta_capmapping. The Abilities API uses primitive capabilities stored in roles, which is the more conventional Core pattern.Test plan
npm run test:php -- --group ai-client— all 326 tests passcomposer lint:errorson new PHP files — 0 errorsnpm run build:dev—ai-client.jsandai-client.min.jsgeneratedGET /wp-json/wp-ai/v1returns route listingrest_forbidden, admin gets throughwp-ai-clientscript is registered withwp-api-fetchandwp-dataas dependenciesUse of AI Tools
This is a compilation of work from the PHP AI Client and WP AI Client repositories, with some changes made in porting to core. Claude Code was used in both the original development of those packages as well as the porting over and creation of the tooling. All code was generated by Claude Code and reviewed by myself and @felixarntz.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.