From a1305c7a70fe5ea902f035cf3adbfe6c7df9786e Mon Sep 17 00:00:00 2001 From: Michael Lander <mlander@elevatedthird.com> Date: Tue, 11 Mar 2025 15:04:14 -0600 Subject: [PATCH 1/4] Added fixes for execute method and cleanup around instantiation with container awareness. --- .../AiFunctionCall/ActionPluginBase.php | 55 ++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/src/Plugin/AiFunctionCall/ActionPluginBase.php b/src/Plugin/AiFunctionCall/ActionPluginBase.php index 69216b4b..f516157f 100644 --- a/src/Plugin/AiFunctionCall/ActionPluginBase.php +++ b/src/Plugin/AiFunctionCall/ActionPluginBase.php @@ -3,6 +3,7 @@ namespace Drupal\ai\Plugin\AiFunctionCall; use Drupal\Component\Plugin\ConfigurableInterface; +use Drupal\Core\Action\ActionManager; use Drupal\Core\Action\ActionPluginCollection; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\ai\Attribute\FunctionCall; @@ -25,32 +26,55 @@ use Symfony\Component\DependencyInjection\ContainerInterface; )] class ActionPluginBase extends FunctionCallBase implements ExecutableFunctionCallInterface { + /** + * The action manager service. + * + * @var \Drupal\Core\Action\ActionManager + */ + protected ActionManager $actionManager; + /** * The action plugin. * - * @var \Drupal\Core\Action\ActionInterface + * @var \Drupal\Core\Action\ActionPluginCollection */ - protected $pluginCollection; + protected ActionPluginCollection $pluginCollection; + /** - * The action plugin manager. + * Constructs a FunctionCall plugin. * - * @var \Drupal\Core\Action\ActionManager + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\ai\Utility\ContextDefinitionNormalizer $context_definition_normalizer + * The context definition normalizer service. */ - protected $actionManager; + public function __construct( + array $configuration, + $plugin_id, + $plugin_definition, + protected ContextDefinitionNormalizer $context_definition_normalizer, + protected ActionManager $action_manager + ) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $context_definition_normalizer); + $this->actionManager = $action_manager; + } /** * Load from dependency injection container. */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): FunctionCallInterface|static { - $instance = new static( + return new static( $configuration, $plugin_id, $plugin_definition, - new ContextDefinitionNormalizer(), + $container->get('ai.context_definition_normalizer'), + $container->get('plugin.manager.action') ); - $instance->actionManager = $container->get('plugin.manager.action'); - return $instance; } /** @@ -62,19 +86,22 @@ class ActionPluginBase extends FunctionCallBase implements ExecutableFunctionCal $params = []; $configuration = $action_plugin->getConfiguration(); // If context keys exist, not in configuration, set to execute. - foreach ($this->getContexts() as $key => $context) { + foreach ($this->getContextValues() as $key => $value) { if (isset($configuration[$key])) { - $configuration[$key] = $context->getContextValue(); + $configuration[$key] = $value; } else { - $params[$key] = $context; + $params[$key] = $value; } } } else { - $params = $this->getContexts(); + $params = $this->getContextValues(); } - + $params = array_values($params); + // @todo: Add access check. + // @todo: Count params? or find something better. + // $this->actionPlugin->access($entity, \Drupal::currentUser()); $action_plugin->execute(...$params); } -- GitLab From 818420894636e4f7bfe87b64d5007c9d8b427994 Mon Sep 17 00:00:00 2001 From: Michael Lander <mlander@elevatedthird.com> Date: Tue, 11 Mar 2025 15:09:54 -0600 Subject: [PATCH 2/4] Added missing setConfiguration. --- src/Plugin/AiFunctionCall/ActionPluginBase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Plugin/AiFunctionCall/ActionPluginBase.php b/src/Plugin/AiFunctionCall/ActionPluginBase.php index f516157f..e1da43e9 100644 --- a/src/Plugin/AiFunctionCall/ActionPluginBase.php +++ b/src/Plugin/AiFunctionCall/ActionPluginBase.php @@ -94,6 +94,7 @@ class ActionPluginBase extends FunctionCallBase implements ExecutableFunctionCal $params[$key] = $value; } } + $action_plugin->setConfiguration($configuration); } else { $params = $this->getContextValues(); -- GitLab From f4292dc3ecf3a8eae2dbd02a3a95f162383683b7 Mon Sep 17 00:00:00 2001 From: Michael Lander <mlander@elevatedthird.com> Date: Wed, 12 Mar 2025 08:45:37 -0600 Subject: [PATCH 3/4] Fixed issue with property that may be null. --- src/Plugin/AiFunctionCall/ActionPluginBase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/AiFunctionCall/ActionPluginBase.php b/src/Plugin/AiFunctionCall/ActionPluginBase.php index e1da43e9..1486d3d0 100644 --- a/src/Plugin/AiFunctionCall/ActionPluginBase.php +++ b/src/Plugin/AiFunctionCall/ActionPluginBase.php @@ -38,7 +38,7 @@ class ActionPluginBase extends FunctionCallBase implements ExecutableFunctionCal * * @var \Drupal\Core\Action\ActionPluginCollection */ - protected ActionPluginCollection $pluginCollection; + protected $pluginCollection; /** -- GitLab From 568687c941ac78fa717102f7acd711cdc2da8356 Mon Sep 17 00:00:00 2001 From: Michael Lander <mlander@elevatedthird.com> Date: Wed, 12 Mar 2025 10:54:08 -0600 Subject: [PATCH 4/4] phpcs fixes. --- src/Plugin/AiFunctionCall/ActionPluginBase.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Plugin/AiFunctionCall/ActionPluginBase.php b/src/Plugin/AiFunctionCall/ActionPluginBase.php index 1486d3d0..bb287600 100644 --- a/src/Plugin/AiFunctionCall/ActionPluginBase.php +++ b/src/Plugin/AiFunctionCall/ActionPluginBase.php @@ -40,7 +40,6 @@ class ActionPluginBase extends FunctionCallBase implements ExecutableFunctionCal */ protected $pluginCollection; - /** * Constructs a FunctionCall plugin. * @@ -52,13 +51,15 @@ class ActionPluginBase extends FunctionCallBase implements ExecutableFunctionCal * The plugin implementation definition. * @param \Drupal\ai\Utility\ContextDefinitionNormalizer $context_definition_normalizer * The context definition normalizer service. + * @param \Drupal\Core\Action\ActionManager $action_manager + * The action manager service. */ public function __construct( array $configuration, - $plugin_id, - $plugin_definition, + $plugin_id, + $plugin_definition, protected ContextDefinitionNormalizer $context_definition_normalizer, - protected ActionManager $action_manager + protected ActionManager $action_manager, ) { parent::__construct($configuration, $plugin_id, $plugin_definition, $context_definition_normalizer); $this->actionManager = $action_manager; @@ -100,8 +101,8 @@ class ActionPluginBase extends FunctionCallBase implements ExecutableFunctionCal $params = $this->getContextValues(); } $params = array_values($params); - // @todo: Add access check. - // @todo: Count params? or find something better. + // @todo Add access check. + // @todo Count params? or find something better. // $this->actionPlugin->access($entity, \Drupal::currentUser()); $action_plugin->execute(...$params); } -- GitLab