Skip to content

Events

The AI Context module uses two event subscribers to integrate with the AI Agents module.

SystemPromptSubscriber

Service ID: ai_context.system_prompt_subscriber

Class: Drupal\ai_context\EventSubscriber\SystemPromptSubscriber

This is the primary integration point between AI Context and AI Agents. It listens for two events:

AgentStartedExecutionEvent (priority 100)

Captures the runner ID when an agent starts execution. The runner ID is stored temporarily and used later to associate usage records with the correct agent run.

BuildSystemPromptEvent

Fires when the agent's system prompt is being assembled. The subscriber:

  1. Calls AiContextSelector::select() with the current prompt and agent ID
  2. If relevant context is found, appends it to the system prompt with a configurable prefix
  3. Records usage via AiContextUsageTracker if a runner ID is available
  4. Attempts to extract entity information from the route context or event tokens

The context is appended in a fenced section:

[configurable prefix text]
-----------------------------------------------
[rendered context items]
-----------------------------------------------

The prefix text is configurable at /admin/ai/context/settings/general. Default: "The following site-specific context applies to this task. Use it strictly when relevant; do not override user intent."

AiContextAgentToolSubscriber

Service ID: ai_context.agent_tool_subscriber

Class: Drupal\ai_context\EventSubscriber\AiContextAgentToolSubscriber

Listens for tool execution events to track what tools agents use and which entities they modify.

AgentToolFinishedExecutionEvent

Fires when an agent tool completes execution. The subscriber:

  1. Records the tool plugin ID via AiContextUsageTracker::recordToolUsed()
  2. Attempts to extract entity information from the tool's output (e.g., "Entity of type node created with id: 123")
  3. If entity info is found, attaches it to the usage record via AiContextUsageTracker::attachEntity()

This provides visibility into what agents actually do with the context they receive.