[RSM] Blog Talks Back > Add reader-chat PHP loader with post context#48144
[RSM] Blog Talks Back > Add reader-chat PHP loader with post context#48144kat3samsin wants to merge 30 commits intotrunkfrom
Conversation
On sandboxes where outbound HTTP to widgets.wp.com and local filesystem access are both unavailable, the asset manifest fetch fails silently. Add a hardcoded dependency list as a fallback so the CDN bundle still loads.
Big Sky has its own chat UI. Don't load AM from CDN separately to avoid duplicate panels. When Big Sky enables AM via the unified-big-sky flag, AM is loaded by jetpack-mu-wpcom instead. Also removes the Big Sky skip from register_provider — with the AM provider merge, both providers coexist and their exports are properly merged.
- Add maybe_enqueue_abilities_script hook to ensure the IIFE bundle loads in the block editor even when Big Sky is present and AM CDN loading is skipped - Skip AM CDN loading when Big Sky is present (has its own UI) - Keep provider registration for AM merge path (Big Sky + AM)
- Await handleUpdateBlockContent() in executeAbility to return resolved value instead of unresolved Promise object. - Add is_array() check on wpcom_get_avatar_url() return to prevent PHP fatal on PHP 8+. - Gate Title_Optimization_Ability::init() behind jetpack_ai_enabled filter to match pattern used by all other AI extensions. - Re-enable I18nCheckPlugin in webpack config, use babel rename plugin for agenttic-ui _n() false positives. - Restore @babel/preset-env and @babel/runtime to trunk versions. - Regenerate pnpm-lock.yaml cleanly from trunk.
Add a filter (default false) to control whether the AI sidebar provider, AM CDN loader, and title optimization ability are registered. This allows gradual rollout and easy per-site toggling.
Cover the main code paths: init() filter gating, maybe_enqueue_am() block editor checks, maybe_enqueue_abilities_script(), register_provider() asset caching and fallback, and full integration flow with filter enabled/disabled.
The canonical source for the AI sidebar provider lives in calypso (@automattic/jetpack-ai-sidebar) and is built to the CDN. Remove the duplicated source files, webpack config, and agenttic npm dependencies from the Jetpack plugin. The PHP loader and ESM wrapper remain.
Move the ESM provider wrapper (jetpack-ai-provider-esm.mjs) from Jetpack plugin files to the CDN build in calypso. This eliminates the dependency on local plugin files for the provider URL, which breaks on Simple sites where plugin files don't exist on disk. - Delete local jetpack-ai-provider-esm.mjs - Add AI_SIDEBAR_PROVIDER_URL constant pointing to CDN - Update register_provider() to use CDN URL without ?ver= param (wrapper reads from window global at import time, always in sync) - Update test assertions to match new URL
class_exists('Big_Sky') is true whenever the plugin is installed,
even when disabled. Check get_option('big_sky_enable') too, matching
the pattern used by Image Studio.
Auto-enable for proxied Automatticians and local dev environments. Regular users remain unaffected. The filter still allows explicit override via add_filter.
The real wpcom/optimize-title ability lives in the wpcom codebase and generates titles via GPT. The Jetpack-side class was a metadata-only stub whose execute callback echoed input back and never generated titles, and the orchestrator discovers abilities from wpcom's own registry, not the site's — so this was dead code.
Both the AI Sidebar CDN loader and the Image Studio CDN loader had an identical copy of this locale-normalization function. Extract it to extensions/shared/cdn-locale.php in a new Automattic\Jetpack\Extensions\Shared namespace and update both callers. Addresses t-wright review comment on PR #47730.
In CIAB (next-admin), Agents Manager is always loaded natively by
jetpack-mu-wpcom. Our CDN loader was still registering the Jetpack AI
provider via the agents_manager_agent_providers filter, causing duplicate
agents to appear in the sidebar.
Bail out of register_provider, maybe_enqueue_am, and
maybe_enqueue_abilities_script when did_action('next_admin_init') is true,
mirroring the existing Big Sky bailout pattern.
Feature is not yet ready for broad internal visibility. Default to false so the sidebar only appears where explicitly enabled via the filter.
- maybe_enqueue_am: combine block editor + AI features guard at top, consistent with maybe_enqueue_abilities_script - register_provider: remove enqueue side effect; asset loading now happens exclusively in maybe_enqueue_abilities_script
- get_current_site: use is_wpcom_simple() to pick the right ID source;
get_current_blog_id() on Simple (where it is the wpcom ID), and
Jetpack_Options::get_option('id') on Atomic/self-hosted
- is_jetpack_disconnected: remove redundant IS_ATOMIC vs is_jetpack_site
branches — both did the same check; simplify to is_wpcom_simple() early
return then a single is_user_connected() call
Remove hardcoded dependency fallback — the list would drift out of sync as Calypso deps change. If neither the local file nor remote fetch returns the asset manifest, skip enqueuing rather than loading with potentially wrong dependencies.
…le; update stale tests register_provider: re-add get_ai_sidebar_asset_data() check removed during Group 1 refactor. Without it, the ESM wrapper's toolProvider Proxy is always truthy even when window.__JetpackAIProvider is never set, causing AM to call getAbilities() on it and get undefined — breaking the provider merge. Tests: update four tests that still documented pre-refactor behavior: - test_register_provider_adds_url: no longer asserts script/style enqueue - test_register_provider_returns_unchanged_when_no_asset_data: count 2→1, remove hardcoded fallback comment - test_sidebar_asset_data_is_cached: call maybe_enqueue_abilities_script instead of register_provider - test_sidebar_asset_data_skips_enqueue_when_fetch_fails: replaces the hardcoded-fallback test; asserts enqueue is skipped on manifest failure
Server-side wp_remote_get to widgets.wp.com doesn't route through the developer's sandbox, so the asset manifest fetch fails on JN/sandbox sites. Return minimal fallback data in dev mode so both the IIFE enqueue and provider registration still work — the browser loads the real bundle from the sandbox.
… type Address PR review feedback: include wp-abilities in the hardcoded dev-mode dependency list, and early-return from is_dev_mode() when wp_parse_url returns a non-string to avoid PHP 8+ stristr warnings.
The Agents Manager variant asset manifest (get_asset_data) also fails on sandbox/JN because server-side fetches don't route through the developer's sandbox. Apply the same fallback pattern used for the AI sidebar IIFE bundle so AM enqueues correctly in dev environments.
- Load Jetpack_Reader_Chat from blocks.php independently of load_block_editor_extensions() so it runs on the frontend - Replace AM_ASSET_BASE_PATH references with string literals (class constants can't use namespace constants) - Add CSS enqueue alongside the JS bundle - Add jetpack_reader_chat_has_ai_features filter for dev overrides
Pass post title, URL, excerpt, author, date, categories, and tags to the frontend via JetpackReaderChatConfig.currentPost so the reader-chat agent knows what page the visitor is viewing.
The dev fallback included wp-abilities which isn't a registered script handle on JN/dev sites, preventing the IIFE bundle from loading and breaking provider registration.
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 🔴 Action required: Please include detailed testing steps, explaining how to test your change, like so: 🔴 Action required: We would recommend that you add a section to the PR description to specify whether this PR includes any changes to data or privacy, like so: Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: The Jetpack plugin has different release cadences depending on the platform:
If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
Code Coverage SummaryCoverage changed in 3 files.
3 files are newly checked for coverage.
Full summary · PHP report · JS report If appropriate, add one of these labels to override the failing coverage check:
Covered by non-unit tests
|
Previously the bundle only enqueued on singular posts. Home, archive, category, and page views got no chat — which is most of the blog. Loads on the frontend everywhere now, skipping admin, feeds, and AJAX. currentPost in JetpackReaderChatConfig is still only populated on singular views (unchanged), so stream views hand the bundle an empty currentPost and the JS shows general-site suggestions instead of post-specific ones.
Summary
class-jetpack-reader-chat.php— CDN loader for reader-chat bundle on blog frontendreader-chat.min.jsand CSS fromwidgets.wp.comJetpackReaderChatConfigjetpack_reader_chat_enabledfilter (opt-in)modules/blocks.phpoutsideload_block_editor_extensions()for frontend accessRelated PRs
Test plan