Skip to content

Writing an AI node processor

The AI nodes are ordinary FlowDrop node processor plugins. This page shows the conventions they follow, so a new node, here or in your own module, behaves like the rest. For the plugin type itself, see the FlowDrop documentation.

A node needs two things:

  1. a plugin class in src/Plugin/FlowDropNodeProcessor/;
  2. a flowdrop_node_type config entity that points to it.

The plugin

Extend AbstractFlowDropNodeProcessor, inject AiModelService and ai.provider, and implement process(), validateParams(), getParameterSchema() and getOutputSchema(). A condensed version of the pattern in Summarize.php (constructor, create() and the provider call left out):

#[FlowDropNodeProcessor(
  id: "summarize",
  label: new TranslatableMarkup("Summarize"),
  description: "Summarize long text into a shorter version using AI models",
  version: "1.0.0",
)]
final class Summarize extends AbstractFlowDropNodeProcessor {

  public function process(ParameterBagInterface $params): array {
    $model_id = $params->getString('model', '')
      ?: $this->aiModelService->getDefaultModelForOperationType('summarize')
      ?: (array_key_first($this->aiModelService->getAvailableModels('summarize')) ?? '');
    if ($model_id === '') {
      throw new \Exception('No AI models are configured. Please configure at least one AI provider.');
    }
    $model_config = $this->aiModelService->getModel($model_id, 'summarize')
      ?? throw new \Exception("Model {$model_id} not found");

    $provider = $this->aiProviderManager->createInstance($model_config['provider'], []);
    // Build the operation's input object and call the provider here.

    return [
      'summary' => $summary,
      'model' => $model_id,
      'provider' => $model_config['provider'],
    ];
  }

}

Conventions the existing nodes keep:

  • Model resolution. The node's model, then the operation type's default, then the first available model. Throw when none is left.
  • Return model and provider with every result.
  • Return plain arrays and scalars. Normalize provider responses (for example with ->getNormalized()->toArray()) so the output serializes into the workflow state. SerializableOutputTest checks this for the bundled nodes.
  • Load files through SecureFileLoader. Never call file_get_contents() on a path or URL from workflow input.
  • Log and rethrow provider errors on the flowdrop_node_processor channel.

The schema

getParameterSchema() returns a JSON schema. Two patterns to copy:

No provider. When hasProvidersForOperationType() is false, return a schema with a single error_message property whose title explains it. The editor then shows the message instead of an empty form.

Model picker. List getAvailableModels() as enum and options, and set default to the operation type's default model.

'model' => [
  'type' => 'string',
  'title' => 'Model',
  'default' => $default_model,
  'enum' => $model_ids,
  'options' => $model_options,
],

Prompt and guardrail fields

The module registers editor widgets for two schema formats. Any node can use them.

'systemPrompt' => [
  'type' => 'string',
  'format' => 'ai_prompt',
  'x-ai-prompt' => [
    'promptTypes' => ['flowdrop'],
    'allowDirect' => TRUE,
    'allowReference' => TRUE,
  ],
],
'guardrail_set' => [
  'type' => 'string',
  'format' => 'guardrail_set',
],

Resolve an ai_prompt value with AiPromptResolver::resolve() before use.

The node type

Add config/install/flowdrop_node_type.flowdrop_node_type.<module>_<id>.yml. parameters sets which inputs are settings (configurable) and which are ports (connectable); outputs sets which outputs show as ports (exposed).

id: flowdrop_ai_provider_summarize
label: 'Summarize'
description: 'Summarize long text into a shorter version using AI models'
category: ai
icon: 'mdi:text-short'
plugin_version: '1.0.0'
enabled: true
parameters:
  text:
    configurable: false
    connectable: true
    required: true
  model:
    configurable: true
    connectable: false
    required: false
    default: ''
outputs:
  summary:
    exposed: true
  model:
    exposed: false
  provider:
    exposed: false
tags:
  - ai
executor_plugin: 'flowdrop_ai_provider:summarize'

Config in config/install only reaches new installs. For existing sites, add a post update that creates the node type; see flowdrop_ai_provider.post_update.php for examples.

Availability

If the plugin id differs from the Drupal AI operation type, add it to the map in AiOperationAvailability. Otherwise the plugin id is not a known operation type, the node counts as needing no provider, and the editor shows it even on a site where no provider can run it.