Problem/Motivation

AI Plus injects contextual data (selected elements, view mode) into every DeepChat request so the server-side AI agent knows what the user is looking at. The current approach has each feature wrap DeepChat's requestInterceptor directly, chaining interceptors by capturing the previous one in a closure.

This creates problems:

  • Ordering fragility — if features attach in a different order (race condition, lazy-loaded libraries), the chain breaks or silently drops context
  • No way to remove a provider — once an interceptor is wrapped, it cannot be unwrapped without rebuilding the chain
  • Duplication — each feature repeats the same boilerplate: find deepchat, capture existing interceptor, wrap it, merge into request.body.contexts

Proposed resolution

Introduce Drupal.AiPlusContext, a centralized context manager. Features register named providers instead of wrapping the interceptor directly:

Drupal.AiPlusContext.register('view_mode', () => {
  return editPlugin.getMainEntityInfo().viewMode;
});

The manager installs a single requestInterceptor that collects all providers on every request and merges their values into request.body.contexts. Providers returning null or undefined are skipped.

Included providers:

  • view_mode — reads the active Layout Builder view mode from NavigationPlus
  • selected_elements — migrated from the selection manager's direct interceptor wrapping

Other modules can register additional providers via Drupal.AiPlusContext.register(name, callback).

API changes

  • New JS API: Drupal.AiPlusContext.register(name, callback), Drupal.AiPlusContext.unregister(name), Drupal.AiPlusContext.collect()
  • Selection manager: no longer installs its own requestInterceptor; registers a selected_elements context provider instead

Comments

tim bozeman created an issue. See original summary.

  • tim bozeman committed f0e4ff65 on 1.0.x
    task: #3591601 Add centralized request context manager
    
    By: tim bozeman
    
tim bozeman’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.