Custom Tokens Bring Page Awareness to Drupal AI Chatbots
A new blog post by Mag. Andreas Mayr describes how to give Drupal AI chatbots real-time awareness of their current page context by extending the Drupal AI module with custom tokens and server-side context reconstruction.
Traditional chatbots often struggle to interpret user queries that rely on the context of the page currently being viewed. To solve this, Mayr introduces a method for extending the AI Agents system using an event subscriber for the BuildSystemPromptEvent, allowing tokens from the existing current-page group to be referenced within system prompts.
A custom token named [current-page:ai-context] is defined and linked to a helper function that dynamically reconstructs the user’s location by parsing the current_route payload sent through the chatbot API. When entity context such as a node or commerce product is successfully resolved, structured metadata like title, type, and canonical URL are returned to guide AI responses.
This enhancement allows the chatbot to prioritize entity-specific answers when applicable, dramatically improving its relevance in context-rich environments such as product catalogs. The full code saamples and architectural notes are available on the Agora Design blog.
