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 NavigationPlusselected_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 aselected_elementscontext provider instead
Comments
Comment #3
tim bozeman commented