Skip to content

[RSM] Blog Talks Back > Add reader-chat PHP loader with post context#48144

Draft
kat3samsin wants to merge 30 commits intotrunkfrom
add/jetpack-reader-chat
Draft

[RSM] Blog Talks Back > Add reader-chat PHP loader with post context#48144
kat3samsin wants to merge 30 commits intotrunkfrom
add/jetpack-reader-chat

Conversation

@kat3samsin
Copy link
Copy Markdown
Contributor

Summary

  • class-jetpack-reader-chat.php — CDN loader for reader-chat bundle on blog frontend
  • Enqueues reader-chat.min.js and CSS from widgets.wp.com
  • Passes post context (title, URL, excerpt, author, date, categories, tags) via JetpackReaderChatConfig
  • Gated by jetpack_reader_chat_enabled filter (opt-in)
  • Loaded from modules/blocks.php outside load_block_editor_extensions() for frontend access

Related PRs

Test plan

  • Enable via `add_filter( 'jetpack_reader_chat_enabled', '__return_true' )`
  • Load a singular post — verify script tag, config, and mount div in HTML
  • Verify `currentPost` includes title, URL, excerpt, author, categories
  • Verify no output on non-singular pages

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.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 17, 2026

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the add/jetpack-reader-chat branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/jetpack-reader-chat

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 17, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • 🔴 Add testing instructions.
  • 🔴 Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

## Testing instructions:

* Go to '..'
*

🔴 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:

## Does this pull request change what data or activity we track or use?

My PR adds *x* and *y*.

Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

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:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: May 5, 2026
    • Code freeze: May 4, 2026

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Apr 17, 2026
@jp-launch-control
Copy link
Copy Markdown

jp-launch-control bot commented Apr 17, 2026

Code Coverage Summary

Coverage changed in 3 files.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/ai-assistant-plugin.php 5/20 (25.00%) -2.78% 2 ❤️‍🩹
projects/plugins/jetpack/modules/blocks.php 0/13 (0.00%) 0.00% 2 ❤️‍🩹
projects/plugins/jetpack/extensions/plugins/image-studio/image-studio.php 137/150 (91.33%) -1.02% 1 ❤️‍🩹

3 files are newly checked for coverage.

File Coverage
projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/reader-chat/class-jetpack-reader-chat.php 0/133 (0.00%) 💔
projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/ai-sidebar/class-jetpack-ai-sidebar.php 140/217 (64.52%) 💚
projects/plugins/jetpack/extensions/shared/cdn-locale.php 8/10 (80.00%) 💚

Full summary · PHP report · JS report

If appropriate, add one of these labels to override the failing coverage check: Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage.

@kat3samsin kat3samsin changed the base branch from trunk to add/jetpack-ai-sidebar-cdn April 17, 2026 14:14
@kat3samsin kat3samsin changed the title Add reader-chat PHP loader with post context [RSM] Blog Talks Back > Add reader-chat PHP loader with post context Apr 17, 2026
Base automatically changed from add/jetpack-ai-sidebar-cdn to trunk April 17, 2026 17:28
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Extension] AI Assistant Plugin [Extension] Image Studio [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant