Reporting AI writes with a plugin¶
A module that writes values with an AI tells AI Disclosure about them in one
file: a plugin of the type AiDisclosureSource. AI Disclosure then works out
whether the writes are direct or were shown to an editor, and applies or
suggests the grade. The module that provides the plugin needs no hard
dependency on ai_disclosure: the plugin is only found when AI Disclosure is
installed.
A plugin¶
Put the class in src/Plugin/AiDisclosureSource of your module:
namespace Drupal\my_module\Plugin\AiDisclosureSource;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\ai_disclosure\Attribute\AiDisclosureSource;
use Drupal\ai_disclosure\Plugin\AiDisclosureSource\AiDisclosureSourceBase;
use Drupal\ai_disclosure\Write\AiGenerationContext;
use Drupal\ai_disclosure\Write\AiWrite;
use Drupal\ai_disclosure\Write\MatchMode;
use Drupal\ai_disclosure\Write\WriteTarget;
use Symfony\Component\DependencyInjection\ContainerInterface;
#[AiDisclosureSource(
id: 'my_module',
label: new TranslatableMarkup('My module'),
description: new TranslatableMarkup('Alt text that My module writes.'),
)]
class MyModuleSource extends AiDisclosureSourceBase {
protected LanguageManagerInterface $languageManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->languageManager = $container->get('language_manager');
return $instance;
}
public function getRequestTags(): array {
return ['my_module:*'];
}
public function resolveWrites(AiGenerationContext $context): array {
$fid = (int) ($context->routeParameters['file'] ?? 0);
if (!$fid || $context->outputText === '') {
return [];
}
return [
new AiWrite(
target: WriteTarget::file($fid),
field: NULL,
langcode: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(),
values: [$context->outputText],
grade: 'ai_metadata',
source: 'my_module',
match: MatchMode::Exact,
),
];
}
}
With ai_disclosure only in suggest in your info.yml or composer.json,
the file stays unused on a site without AI Disclosure. The plugin ID is stored
as the source of every report, so use the machine name of your module.
The base class makes the plugin always available, not routed by tags, and without writes. Override what you need:
| Method | What it does |
|---|---|
getRequestTags() |
the request tags of the generations to route to the plugin. A tag matches exactly, or as a prefix when it ends with :*. Return no tags when your module passes writes to the tracker itself |
isAvailable() |
return FALSE to switch the plugin off, for example when a module it needs is missing |
resolveWrites() |
turns one generation into a list of AiWrite objects |
What the plugin gets¶
resolveWrites() receives an AiGenerationContext, built from the event the
AI module dispatches after a generation: the request tags, the metadata
(including entity_context), the operation type, the generated text (empty
when the output is not text), the raw route parameters of the current request
and the ID of the current user. It does not depend on the classes of the AI
module, and AI Disclosure itself works without that module.
A generation that streams its answer is reported twice: first with an empty text, as the text is not there yet, and again when the stream has ended, with the whole text. Return no writes for an empty text, so that the generation counts once.
What a write says¶
An AiWrite has a target, a field, a language, the generated strings, the grade
to apply, the source and an optional note. The note is a short detail that is
shown next to the field name in the note of the report.
The target is where the value goes:
| Target | Use it for |
|---|---|
WriteTarget::entity($entity) |
an entity object the code holds. If that object is saved, the grade is applied in that save. If it never is, the grade is suggested when the entity is next saved |
WriteTarget::uuid($entityTypeId, $uuid) |
an entity that is known by its UUID |
WriteTarget::bundle($entityTypeId, $bundle) |
an entity that does not exist yet, such as a new content form |
WriteTarget::file($fid) |
the alt text of an image, whatever entity uses the file |
The language must be the one of the translation that is saved later. A
pending write only matches a save in the same language, so a fixed 'en'
never matches on a site that edits in Italian. The current content language
is the usual answer for a request made from an edit form.
When the request does not name the language, pass
LanguageInterface::LANGCODE_NOT_SPECIFIED ('und'). Such a write matches any
translation of the target, and the first save that holds the value uses it up.
The field is the machine name of the field, or NULL for any text field that the
save changed. A suggestion is made when the field changed in the save and holds
the generated value. MatchMode::Exact asks for the same string.
MatchMode::Loose also accepts a stored value that contains the generated one,
or that is a truncation of it. Both strings are also compared as plain text,
without tags, with entities decoded and white space collapsed, so that text an
editor saved as HTML still matches.
For a file or media field use MatchMode::Reference. The values are then the
IDs of the generated files or media items, as strings, and a field matches when
it changed in the save and references one of them. A file or image field is
compared by the file ID, an entity reference to media by the media ID. With no
field the write matches any file, image or media field. When the file is only
created as the value is stored, as AI Automators does, name the field and pass
no values: the IDs are read from that field of the entity target when the
entity is saved, or when the request ends.
Pass onReferenced: TRUE with a field that references other entities, such as
media items, when the grade belongs to them and not to the target. The grade is
applied to, or suggested on, each referenced entity that has an AI Disclosure
field, when the target is saved holding its ID, and the note names the field of
the target. An entity without the field is skipped, and the target needs no AI
Disclosure field of its own.
Pass suggestedGrade when the grade differs for a value that an editor saw
before saving. When several writes meet in one save, the strictest grade wins.
Routing and the tracker¶
A plugin with request tags is called for every AI generation that carries one of them, and each write it returns goes to the tracker. A plugin without tags, or any other code, passes writes itself, with the tracker injected as a service:
public function __construct(
protected readonly AiWriteTrackerInterface $tracker,
) {}
public function onGenerated(AiWrite $write): void {
$this->tracker->track($write);
}
With autowiring, type-hint Drupal\ai_disclosure\Write\AiWriteTrackerInterface.
The service ID is ai_disclosure.write_tracker.
The tracker keeps what no save claimed for a day, per user. It never breaks a
save: a failure is logged on the ai_disclosure channel.
Other modules can alter the definitions with
hook_ai_disclosure_source_info_alter().