Placeholders not available when using advanced (token) mode

Created on 19 May 2025, 5 months ago

Problem/Motivation

The advanced automator type that allows tokens instead of a single base field does not have access to the replacements the automator type plugin may have to offer. For instance the taxonomy plugin offers these replacements which are not readily reproducible via Drupal tokens:

    $tokens['value_options_comma'] = 'A comma separated list of all value options.';
    $tokens['value_options_nl'] = 'A new line separated list of all value options.';
    $tokens['value_options_nl_description'] = 'A new line separated list of all value options, with term descriptions.';

Steps to reproduce

Proposed resolution

Allow use of these replacement patterns for base setup as well as advanced setup.

Remaining tasks

User interface changes

The replacement patterns would appear on the field configuration form for both automator types.

API changes

Data model changes

๐Ÿ› Bug report
Status

Active

Version

1.1

Component

AI Automators

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States jhedstrom Portland, OR

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @jhedstrom
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jhedstrom Portland, OR
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    Added the code to have the "Placeholders available" section for the Advanced mode. Needs to be reviewed.

  • Pipeline finished with Success
    5 months ago
    Total: 194s
    #501647
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Kanchan Bhogade

    Hi
    Tested MR 621 on Drupal 11.x
    The Mr is applied successfully...

    Testing Steps:

    1. Install AI Module
    2. Install the AI Automators module with the Token Module
    3. Enabled the AI Autometers on the entity type's field
    4. Select AI Automatos settings > Automator Input Mode > Advanced Mode (token)
    5. Check for the Placeholder

    Result:
    The Placeholder is available when using advanced (token) mode.

    Attaching screenshots for reference

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jhedstrom Portland, OR

    These now appear on the form to configure the automator, however they are not actually replaced in the plugin (RuleBase::generate()) as this code doesn't fire for the advanced configuration:

      public function generate(ContentEntityInterface $entity, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
        // Generate the real prompt if needed.
        $prompts = [];
        // @phpstan-ignore-next-line
        if (!empty($automatorConfig['mode']) && $automatorConfig['mode'] == 'token' && \Drupal::service('module_handler')->moduleExists('token')) {
          $prompts[] = \Drupal::service('ai_automator.prompt_helper')->renderTokenPrompt($automatorConfig['token'], $entity); /* @phpstan-ignore-line */
        }
        elseif ($this->needsPrompt()) {
          // Run rule.
          foreach ($entity->get($automatorConfig['base_field'])->getValue() as $i => $item) {
            // Get tokens.
            $tokens = $this->generateTokens($entity, $fieldDefinition, $automatorConfig, $i);
            $prompts[] = \Drupal::service('ai_automator.prompt_helper')->renderPrompt($automatorConfig['prompt'], $tokens, $i); /* @phpstan-ignore-line */
          }
        }
        return $prompts;
      }
    
    
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    I think it would be better if Marcus could confirm whether the placeholders not implemented for advanced mode are done on purpose or missed.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jhedstrom Portland, OR

    If they are intentionally left out, are there examples of how to replicate the prompt available at the Base level? I'm not aware of a token that would provide, for example, all the taxonomy terms available to a given field...

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jhedstrom Portland, OR

    I updated the PR and am adding a patch for use with composer here.

  • Pipeline finished with Success
    5 months ago
    #503377
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany marcus_johansson

    It wasn't on purpose to leave them out, it never struck me that you could need them. I'll change this to task and set in to 1.2.x and add it to the roadmap for 1.2.0.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia annmarysruthy

    Tested the feature. Issues I Observed:

    1. 1. The placeholders are available in automator for advanced mode. but what does this placeholders stand for?
      {{ context }} - The cleaned text from the base field.
       {{ raw_context }} - The raw text from the base field. Can include HTML
      { max_amount }} - The max amount of entries to set. If unlimited this value will be empty.

      These explanations are valid only when we have selected a base field. but in advanced mode, there is no base field selected.

    2. Enabled automator for a field, Selected "Advanced Mode", used prompt "write an article on {{ context }}." and saved the setting. But did not get any response.
    3. Enabled automator for a field, Selected "base mode" and selected a field title as "Automator Base Field", then changed the mode to advanced mode and used the same prompt as above "write an article on {{ context }}." and saved the setting. Now I got ai response in the field with an article on title field(selected as base field).
  • Status changed to Needs work about 1 month ago
  • ๐Ÿ‡ง๐Ÿ‡ฌBulgaria valthebald Sofia
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhisekmazumdar India

    Working on the feedback

  • Pipeline finished with Failed
    about 1 month ago
    #588853
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhisekmazumdar India

    I attempted to clean up the direct calling of services in the RuleBase file and quickly realized that it would be a significant task. The RuleBase has been extended by many other classes, each with its own dependency injection for the services included in the RuleBase. I will create an issue for this cleanup since I have completed the current task.

  • Pipeline finished with Failed
    about 1 month ago
    #588881
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhisekmazumdar India

    I feel there is a bug in the logic how the form handle the Automator Base Field value. Event if we select Advanced Mode (Token) the value for Automator Base Field get stored in the config. Which result it making the Placeholders context work in some cases. In theory Placeholders context shouldn't be need for the advance mode.

    I feel the solution here will be to remove the placeholder for the advance mode and manage the value of Automator Base Field addConfigValues() to only save the value when the mode is Base Mode.

    // Only persist base_field when Base mode is selected; otherwise clear.
    $aiConfig->set('base_field', ($formState->getValue('automator_mode') === 'base') ? ($formState->getValue('automator_base_field') ?? '') : '');
    
    

    Advanced Mode is intended to be used only with tokens, that's how I feel.
    Changing this needs review, as it requires input from the maintainer of the module regarding the logical implementation standpoint for this functionality.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany marcus_johansson

    There are some advantages with the contexts the Automator sends - there are for instance in the taxonomy automator type, ways of getting massages lists of taxonomies etc.

    I wonder though, if we are just band-aiding an work around - I think maybe we should postpone this to 2.0 and actually bring the specific context placeholders into tokens and utilize the prompt library we are adding in 1.2.0?

    The thing that is possible in base mode, that might not be utilized much, is that it is rendered via Twig. But we should push for this being possible to use in the prompt library instead, so we have the possibility to for instance show one dynamical value conditional on another or loop items.

    I'll remove beta blocker for now and I think this is a better candidate for full rework in 2.0?

Production build 0.71.5 2024