Consumer type API
Consumer types are Drupal attribute plugins. They describe how a kind of
thing receives context. AI Context ships the public contract, the
agent type, and the automator type. Other types are implemented
and owned by the consuming project.
This page is the public plugin API. The consumer type manager, route subscriber, and listing forms are internal.
Ownership
Implement the plugin in the consuming project when that project can own the type:
- Preferred: a small integration submodule. Its
info.ymlshould constrain the AI Context version.config/installshould shipai_context.consumer_type_settings.{type}. - Lighter: put the plugin in the main module. There is no version enforcement and no install-time settings default.
The real test is what internals the plugin touches. A type that reads config entities through entity storage and matches on emitted request tags can live in AI Context. A type that reaches into another module's final classes must live with those classes.
Do not add a CKEditor type to AI Context. That stays in the AI CKEditor project.
AI Context ships automator because Automators stay in AI core and
AI core does not take a dependency on CCC. That type is gated on
moduleExists('ai_automators'), enumerates ai_automator config
entities, and matches only ai_automator:id:{id}.
Do not add an assistant type. The generic path excludes
ai_agents and ai_assistant_api so those calls stay on
the agent path. That exclusion (EXCLUDED_REQUEST_TAGS on
the router) is not a setting. Those tags already receive
context on the agent path; making the list configurable
would allow double injection or stealing agent requests.
The same list holds the module's own ai_context and
ai_context_subcontext tags (AiContextConsumerRouter::INTERNAL_REQUEST_TAG
and ::SUBCONTEXT_REQUEST_TAG). Conditional subcontext resolution
dispatches a chat request from inside selection; routing it would
select context for a request that is already inside selection and
recurse without bound. The generic subscriber also refuses to
re-enter while one of its selections is running, so a type that
routes on a broad tag cannot reopen the recursion.
Registering a type
Extend AiContextConsumerTypeBase and use the
AiContextConsumerType attribute. Plugin IDs must not contain a
colon. The colon is reserved for canonical instance IDs
{type}:{instance}.
#[AiContextConsumerType(
id: 'example',
label: new TranslatableMarkup('Example'),
description: new TranslatableMarkup('Example consumers.'),
host_entity_type: 'example_thing',
)]
final class AiContextConsumerTypeExample extends AiContextConsumerTypeBase {
}
A host entity is the entity one consumer instance belongs to,
and the instance ID is that entity's ID. For example, the agent
content_editor is the host entity for the consumer
agent:content_editor. Set host_entity_type when instances
are 1:1 with a host entity. Omit it when instances come from
plugins or configuration.
No definition alter hook
Consumer types have no hook_ai_context_consumer_type_info_alter().
The type plugin manager does not call alterInfo(). A site that
needs to turn a type off uses the enabled kill switch on
ai_context.consumer_type_settings.{type} (Consumer Types listing,
or config). Missing type settings mean the type is enabled.
That is a push gate, not discovery. A disabled type still appears
in listings so stored overrides can be edited. Pull-based tools
keep working. To mark a type that cannot work on this site,
override isAvailable() instead of unsetting the definition.
See Push gates and Hooks.
Instances
getInstances() must be built from the data that defines the
instances (plugin definitions, entities, config). Never hardcode a
list. It returns an AiContextConsumerInstanceCollection.
Read cacheability from CacheableDependencyInterface; there is no
getCacheableMetadata().
Each instance has:
getInstanceId()— the instance ID (content_editor)getConsumerId()— the canonicalagent:content_editorvalue object, fromAiContextConsumerId::fromParts($type, $instanceId)- A label
- An optional description
getLabelRoute($instanceId) may return a route to the owning object's
admin page (the listing name link). For the Automator type, this is the
field configuration edit form when the configured entity type, bundle,
and field exist and the current user can open Field UI. It falls back
to the Automator edit form when the field configuration cannot be
loaded, Field UI is not available, or the current user cannot access
the field form. Return NULL if there is no page.
That is not the CCC consumer editor.
Host entity delete
getConsumerIdForDeletedEntity() maps a deleted host entity to that
type's consumer ID so hook_entity_delete() can drop only that row
from ai_context.consumers. The consumers object itself stays.
AiContextConsumerTypeBase implements the 1:1 case from the
host_entity_type attribute. Agent sets ai_agent. Automator
sets ai_automator. Types with no host entity omit the key and get
NULL. Override the method only when the instance ID is not
the entity ID.
Do not gate on isEnabled() or isAvailable(): a leftover row
can still exist after a kill switch or a missing module. A type
whose create() or mapping throws is skipped and logged so one
broken plugin cannot abort every entity delete. The same skip
omits that type from the consumers listing and from routing.
Check Recent log messages.
A host entity deleted during a module uninstall keeps its row. The type is going away rather than one consumer being removed, so the saved overrides stay and list as Off (type unavailable), or Off (stale) when the type stays installed. Core deletes host entities at several points during an uninstall:
- Config entities deleted by
ConfigManager::uninstall()carryisUninstalling(), and mapping is skipped for them. This includes cascade deletes while a different module is torn down in the same run. - Host entities deleted earlier, while core removes the bundles of
the module's entity types, carry no flag. An automator on a field
of an
automator_chainbundle is one example. Their rows are removed as usual andhook_modules_uninstalled()restores them. - Host entities deleted by a module's own
hook_uninstall(), which runs beforeConfigManager::uninstall(), carry no flag either and are restored the same way.
When stored config repeats a consumer ID, only the first row is restored (see the duplicate-ID rule below and Hooks).
This is generic: any type whose host entities are config entities from an
optional module gets the behavior without implementing anything.
isAvailable() cannot express it, because core clears
core.extension only after the config entities are deleted.
Because an unavailable type enumerates nothing, its leftover rows
have no instance to edit. The consumer editor builds a stand-in
instance for them, but only from a row already saved in
ai_context.consumers, so an arbitrary consumer ID still 404s.
Direct implementations of the interface (without the base class) must add the method. After 1.0.0 this is a public API addition with a base default; see API stability.
Matching provider requests
The consumer router (AiContextConsumerRouter) is an AI Context
service. It is not Drupal's routing system. The router itself is
internal. Callers use routeRequest(). The router does not have
resolveConsumerId(); that method lives on the type plugin.
Types advertise routing tags with getRoutingRequestTags()
(for example ai_ckeditor). Those are AI request tags, not taxonomy
or cache tags. The router then calls resolveConsumerId() on
matching types:
- Return an
AiContextConsumerIdto claim the request - Return
NULLto decline
The router already excludes ai_agents and ai_assistant_api
before it calls plugins. Types do not need to decline those tags.
Unmatched tags (no type advertised them) load no consumer config and no context items. Two types resolving different IDs fail closed as ambiguous.
The Automator type advertises ai_automator and claims a request
only when tags include exactly one ai_automator:id:{id} and that
ai_automator config entity exists. It does not reconstruct the
instance from entity type, bundle, or field name. Those tags are
ambiguous when a field has more than one automator.
AI 1.5.0 or later emits the ID tag from RuleBase::getTags().
On AI 1.4.x, an internal subscriber
(AiContextAutomatorIdTagSubscriber) adds it
when the field properties uniquely identify one automator, or
when a field widget action click maps to one automator on that
field. Multi-value fields post {uuid}_{delta} for values after
0; those names still map.
AiContextAutomatorWidgetOnlySubscriber uses the same click
to skip the other automators on that field when the clicked
automator is allowed to push. Remove both subscribers when CCC
requires an AI version that ships the tag and runs only the
clicked instance on widget clicks.
Request entity and page path
Consumer requests often arrive on an API-proxy route, such as the AI
CKEditor request endpoint. Its Drupal route and HTTP path describe
the endpoint, not the content being edited. Entity Types, Specific
Entities, Taxonomy Terms, and Site Sections need the real target, so
the caller must send it as entity_context request metadata on the
chat input (InputBase::setRequestMetadataValue()):
entity_typeplusidfor a saved entity. The entity is loaded to read its bundle and taxonomy terms.entity_typeplusbundlefor an unsaved entity, for example a node add form. Entity Types matches the bundle; Specific Entities and Taxonomy Terms have nothing to match. A loaded entity that the current user can view wins over the bundle hint when both are present. An entity the user cannot view is treated as missing, so the hint is used.path: the page the visitor is working on, so Site Sections can match its patterns instead of the endpoint path.
Config entities on the proxy route or in agent tokens (an Editor,
a text format) are ignored, so they cannot shadow these hints.
When no path is sent, AI Context falls back to a same-site
Referer on XMLHttpRequest and JSON requests only.
entity_type plus id loads the entity to read its bundle and
taxonomy terms. loadContentEntity() checks view access first, so
the bundle and the terms are read only from an entity the current
user can view. The bundle and path hints are
untrusted client values used only for string matching. They never
load or grant access to an entity on their own. The bundle hint
is stored only for content entity types.
Background jobs and queue workers
A queue worker, a cron hook, or a Drush command can receive automatic
push when it makes a chat call routed to a consumer or runs an agent,
subject to the normal push gates. A job that does neither gets no
automatic push. To retrieve context explicitly, pull it through
ai_context.selection_factory:
$factory = \Drupal::service('ai_context.selection_factory');
$selection = $factory->fromConsumer('my_type:my_instance', $task, 'node', $nid);
$context = $factory->getResultFromSelection($selection)->getRenderedText();
The worker account decides what is selected. Item queries run with
accessCheck(TRUE), so selection sees only what the account running the
job may view. Automated cron runs as the anonymous user, which usually
holds none of the context permissions and therefore selects nothing.
Run the job as an account that holds Use Published AI Context in AI
Features, or switch to it for the call. Do not assume the account that
queued the item is the account that runs it.
Only a saved entity can be passed. fromConsumer() and
AiContextSelectionParams take an entity type and an entity ID, and
getRenderedContext() and getResult() take a loaded entity. The
bundle hint and the page path described in
Request entity and page path
reach selection through an internal resolver that the provider event
fills, so a background job cannot supply them through the public pull
API. For an entity that is not saved yet, save it first. A pull without
entity context can rely on the consumer's subscriptions, subject to the
request-state limitation below.
Entity context persists on the current request. A pull with an entity type and ID stores them on the current request. A later pull without entity arguments does not clear them and can still match scopes for the previous entity. In a queue loop, pass each entity-backed item's own entity. Run work that needs no entity context in a fresh request or separate process. Processing that work first is sufficient only when no entity context is already present. The public pull API has no reset method.
Conditional subcontext can make its own AI calls. When
conditional_subcontext_enabled is on, which is the default, the
resolver asks a provider which children of a selected parent to include,
once per parent, capped by conditional_max_parents (default 3). In a
loop over many queue items those calls add up. The switch is site-wide,
at /admin/config/ai/context/settings/items; there is no per-consumer
override. It is skipped when no provider is configured.
Pull writes no usage record. The two push subscribers create usage
rows, and agent lifecycle subscribers can update them. Pull calls do not
create or update usage records, so pulling context in a background job
does not add it to /admin/config/ai/context/usage. Keep your own log if
the job needs an audit trail.
Pull does not enforce the push gates. Configuration resolution
computes push_enabled, the type kill switch, and isAvailable(), but
their values do not stop a pull. A consumer with push turned off can
still return context when you pull it. Call isPushAllowed() yourself
if the job should honor those settings. See Push gates.
Type settings vs instance overrides
| Store | What it holds |
|---|---|
ai_context.consumer_type_settings.{type} |
Type-wide settings. enabled is the push kill switch. |
ai_context.consumers |
Per-instance overrides (push_enabled, subscriptions, limits, settings) |
Missing type settings mean the type is enabled. Instance
defaults come from getInstanceDefaults(). Scopes still use
defaultConfiguration() for ai_context.scope_settings.{id}.
Leftover type settings after a plugin is gone show as stale rows on
the Consumer Types listing. Removing them deletes that config object
only, not instance rows in ai_context.consumers.
consumer_id is unique within ai_context.consumers. Config validation
rejects a repeated ID: the violation is reported against the later row and
the message names the earlier one, so a full, partial or single config
import with a duplicate fails. If a duplicate reaches stored config anyway,
through a direct write that skips import validation (for example
drush config:set, a recipe config action, custom code or a database
edit), the first matching row is the one that takes effect. The
consumers listing, loadConsumerConfig() and the consumer editor all read
the first match and ignore the rest. Saving any consumer from the editor
drops the later repeats of every duplicated ID, shows a warning naming the
IDs and logs them, so the duplicate is cleared without editing config by
hand. The remove form for a stale consumer deletes every row that carries
its ID, so the consumer stops resolving rather than falling back to a
repeat.
Saving type settings merges keys from submitTypeSettingsForm().
A NULL value clears that key so exported config only carries keys a
site builder set, matching scope settings. Keys omitted from the
array stay in config.
Schema pattern:
- Shared base type for
ai_context.consumer_type_settings.* - A wildcard fallback for unknown types
- Per-type entries that extend the base
- Per-instance
settingsis dynamically typed (ai_context.consumer_settings.{type}, with a*fallback)
The * fallbacks are bare mappings that permit no keys. A type that
stores anything under type settings beyond enabled, or anything at
all under per-instance settings, must ship its own
ai_context.consumer_type_settings.{type} or
ai_context.consumer_settings.{type} schema entry. Otherwise config
validation rejects those keys as unsupported.
The shared consumer editor never edits type-specific instance
settings; it preserves whatever the row already stores. Types that
need a UI for those values own that form.
The Agent type ships settings.loop_aware and
isLoopAware($instanceId). Read the typed bag with
getSettings() on stored or resolved consumer config.
isLoopAware() is the Agent helper over that bag. Loop-aware
and Debug / Explore stay on that type. They are not generic
consumer API.
Selection mode defaults
getInstanceDefaults()['selection_mode'] sets both unsaved
runtime and the Context selection mode radio when the consumer
has no stored selection_mode.
- Omit the key or set
inherit(AiContextSelection::SELECTION_MODE_INHERIT): Site default. The first save writesinherit. Runtime followsai_context.settings:selection_mode. - Set
minimal,relevant, orbroadfor that mode. The first save writes it. - The base sets
inherit. The Automator type setsminimal. - An unrecognized value is treated as inherit.
A stored inherit value follows the site setting. A saved row
that omits the key uses the type default. Do not special-case
plugin IDs in the consumer form.
Push gates
Automatic push runs only when all of these are true:
- The instance
push_enabledflag (or the type default when the key is omitted or null) - The type
isEnabled()kill switch (enabledonai_context.consumer_type_settings.{type}; there is no definition alter hook) - The type
isAvailable()environment gate (for example, an optional module the type integrates with is missing)
Override isAvailable() when the type cannot work on this site.
The base returns TRUE. The Agent type does not override it
because ai_agents is a hard dependency. The Automator type
returns FALSE when ai_automators is not installed.
Override getAvailabilityCacheableMetadata() when
isAvailable() depends on modules or other site state. The
base returns empty metadata. Automator adds
config:core.extension so installing or uninstalling
ai_automators drops a cached empty list and an Unavailable
listing row. Start getInstances() cacheability with
createInstanceCacheability() so those tags travel with the
enumeration. The router also merges this metadata onto
cached instances and the routing map.
isPushAllowed() is TRUE only when all three pass. The generic
subscriber reports unavailable types as type_disabled.
Pull-based tools ignore those three gates.
Invocation result
After the generic subscriber handles a chat request, it writes an
ai_context key on event metadata and on the caller's chat input
debug data (ChatInput::getDebugData(), not request metadata). An
absent key means the request was not handled. The debug-data copy
also marks the input as processed: dispatching the same ChatInput
again (a tool-call inner request) re-publishes a stored pushed
result to the new event instead of appending a second context block.
Any other stored status is not replayed, so the same input is routed
again on a later call.
Compare statuses to AiContextInvocationResult constants
(PUSHED, NO_ITEMS, PUSH_DISABLED,
TYPE_DISABLED, STALE, DECLINED, AMBIGUOUS). The payload
never includes prompt or context content.
item_ids and item_count describe what was delivered, not what
was selected. When the token budget has no room for an item, the
renderer leaves it out, and it is not listed here and not recorded as
usage. An item whose content was only shortened is delivered, because
part of it reached the model. The renderer decides what was delivered. A
TEXT_RENDERED subscriber that changes or removes item text does not
change item_ids or the usage records. If the budget leaves out every
selected item but a subscriber added text, that text is still pushed:
the status is pushed, and item_ids is empty.
NO_ITEMS under an anonymous session means the entity query ran with
accessCheck(TRUE) and every item failed the view access check, not that
routing failed. View access to context items is permission-based, not
per-item: anonymous sessions can only view published items, and only when
the anonymous role has the Use Published AI Context in AI Features
(access published ai context) permission. Granting it exposes every
published context item to anonymous selection — there is no per-item
grant — so review the published catalog before enabling it for a public
chatbot. Scopes narrow what a consumer selects; they are not an access
control.
See API stability.
Naming notes
These names stay as they are:
- Router means
AiContextConsumerRouter(tag → consumer ID), not Drupal routes. Type settings routes are added byAiContextConsumerTypeRouteSubscriber. Router callers userouteRequest(). Type plugins implementresolveConsumerId(). - Type plugins use inherited
getPluginId()(agent). InstancegetInstanceId()is the instance ID (content_editor).getConsumerId()is the canonicalagent:{id}. fromConsumer()accepts a string ID.AiContextConsumerIdis the value object for validation and parts. Build IDs withfromParts()/fromString().SEPARATORis private.isValidType()is@internal.getRenderedContext()/getResult()/fromParameters()/getResultFromSelection():consumerIdmerges canonical config. Pulls without a consumer log asnone. Run a built selection withgetResultFromSelection().- There are two “request context” ideas: scope matching
(
matchesRequestContext(), page/entity) and the provider-call snapshot (AiContextProviderRequestContext). - The invocation-result metadata key is
ai_context.
Convenience APIs
getRenderedContext() and getResult() stay as caller-opt-in
helpers. Their trailing $selectionMode is public and nullable:
NULL uses the site default (relevant). broad is available by
choice. minimal and relevant use the scope index for prefiltering;
only broad walks the full catalog. Automatic injection never widens a
consumer's stored mode; these helpers may. Use
getResultFromSelection() when you already have an
AiContextSelection.
See Services.
Drupal tokens in returned text
The selection factory methods (getRenderedContext(), getResult(),
and getResultFromSelection()) return item text with Drupal tokens
neutralized, the same as every CCC delivery path. This includes the
rendered text, the label and purpose in getSelectedItems(), and the
content snippet in getSelectedItems(TRUE). For example, [user:mail] becomes
[user : mail], so a later token replacement, such as the one AI
Agents runs on its system prompt, does not expand it.
To read item text with its tokens as written, load the context item entity and read its fields.
The Get Relevant tool returns the factory text as its output, and AI Agents
replaces tokens in default information tool output. If you decorate
ai_context.selection_factory, return the text of the inner factory and do
not add item text from other sources. If you replace the factory, break
Drupal tokens in the text that it returns yourself, for example by putting
a space on both sides of the colon, because the CCC neutralizer service is
internal. See
Authoring trust.