ai_agents_test-1.0.0-alpha1/ai_agents_test.module
ai_agents_test.module
<?php
/**
* @file
* Provides an agent test entity type.
*/
use Drupal\ai_agents\Entity\AiAgent;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_entity_operation().
*/
function ai_agents_test_entity_operation(EntityInterface $entity): array {
return $entity instanceof AiAgent ? [
'test' => [
'title' => t('Write/Run Tests'),
'url' => Url::fromRoute('entity.ai_agents_test.collection', ['agent_id' => $entity->id()]),
'weight' => 31,
],
] : [];
}
/**
* Implements hook_theme().
*/
function ai_agents_test_theme(): array {
return [
'ai_agents_test' => ['render element' => 'elements'],
'ai_agents_test_result' => ['render element' => 'elements'],
'ai_agents_test_group' => ['render element' => 'elements'],
'ai_agents_test_group_result' => ['render element' => 'elements'],
];
}
/**
* Prepares variables for agent test templates.
*
* Default template: ai-agents-test.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the agent test information
* and any fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_ai_agents_test(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_user_cancel().
*/
function ai_agents_test_user_cancel($edit, UserInterface $account, $method): void {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish agent tests.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test');
$ai_agents_test_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->accessCheck(FALSE)
->execute();
/** @var \Drupal\ai_agents_test\AgentTestInterface $ai_agents_test */
foreach ($storage->loadMultiple($ai_agents_test_ids) as $ai_agents_test) {
$ai_agents_test->set('status', FALSE)->save();
}
// Unpublish agent test groups.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test_group');
$ai_agents_test_group_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->accessCheck(FALSE)
->execute();
/** @var \Drupal\ai_agents_test\AgentTestGroupInterface $ai_agents_test_group */
foreach ($storage->loadMultiple($ai_agents_test_group_ids) as $ai_agents_test_group) {
$ai_agents_test_group->set('status', FALSE)->save();
}
break;
case 'user_cancel_reassign':
// Anonymize agent tests.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test');
$ai_agents_test_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
/** @var \Drupal\ai_agents_test\AgentTestInterface $ai_agents_test */
foreach ($storage->loadMultiple($ai_agents_test_ids) as $ai_agents_test) {
$ai_agents_test->setOwnerId(0)->save();
}
// Anonymize agent test results.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test_result');
$ai_agents_test_result_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
/** @var \Drupal\ai_agents_test\AgentTestResultInterface $ai_agents_test_result */
foreach ($storage->loadMultiple($ai_agents_test_result_ids) as $ai_agents_test_result) {
$ai_agents_test_result->setOwnerId(0)->save();
}
// Anonymize agent test groups.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test_group');
$ai_agents_test_group_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
/** @var \Drupal\ai_agents_test\AgentTestGroupInterface $ai_agents_test_group */
foreach ($storage->loadMultiple($ai_agents_test_group_ids) as $ai_agents_test_group) {
$ai_agents_test_group->setOwnerId(0)->save();
}
break;
case 'user_cancel_reassign':
// Anonymize agent test group results.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test_group_result');
$ai_agents_test_group_result_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
/** @var \Drupal\ai_agents_test\AgentTestGroupResultInterface $ai_agents_test_group_result */
foreach ($storage->loadMultiple($ai_agents_test_group_result_ids) as $ai_agents_test_group_result) {
$ai_agents_test_group_result->setOwnerId(0)->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function ai_agents_test_user_predelete(UserInterface $account): void {
// Delete agent tests that belong to this account.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test');
$ai_agents_test_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$storage->delete(
$storage->loadMultiple($ai_agents_test_ids)
);
// Delete agent test results that belong to this account.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test_result');
$ai_agents_test_result_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$storage->delete(
$storage->loadMultiple($ai_agents_test_result_ids)
);
// Delete agent test groups that belong to this account.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test_group');
$ai_agents_test_group_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$storage->delete(
$storage->loadMultiple($ai_agents_test_group_ids)
);
// Delete agent test group results that belong to this account.
$storage = \Drupal::entityTypeManager()->getStorage('ai_agents_test_group_result');
$ai_agents_test_group_result_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$storage->delete(
$storage->loadMultiple($ai_agents_test_group_result_ids)
);
}
/**
* Prepares variables for agent test result templates.
*
* Default template: ai-agents-test-result.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the agent test result
* information and any fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_ai_agents_test_result(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$variables['entity'] = $variables['elements']['#ai_agents_test_result'];
$variables['summary_results'] = Yaml::parse($variables['elements']['#ai_agents_test_result']->summary_results->value ?? []);
$variables['detailed_results'] = Yaml::parse($variables['elements']['#ai_agents_test_result']->detailed_results->value ?? []);
$variables['agent_configuration'] = Yaml::parse($variables['elements']['#ai_agents_test_result']->agent_configuration->value ?? []);
$variables['agent_test'] = $variables['elements']['#ai_agents_test_result']->test_id->entity ?? NULL;
$variables['inputs'] = Yaml::parse($variables['agent_test']->messages->value ?? []);
$variables['triggering_instruction'] = $variables['agent_test']->triggering_instructions->value ?? '';
$variables['agent'] = $variables['agent_test']->ai_agent->entity ?? NULL;
$variables['tools_used'] = Yaml::parse($variables['elements']['#ai_agents_test_result']->tools->value ?? []);
$variables['#attached']['library'][] = 'ai_agents_test/results';
$variables['failure_message'] = $variables['entity']->result->value === 'success' ? t('Success') : t('Failure');
$variables['error_message'] = $variables['entity']->error_message->value;
$variables['error_stack'] = $variables['entity']->error_stack->value ?? NULL;
if (!empty($variables['error_message'])) {
$variables['failure_message'] = t('Failure - PHP Error');
}
}
/**
* Prepares variables for agent test group templates.
*
* Default template: ai-agents-test-group.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the agent test group
* information and any fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_ai_agents_test_group(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$variables['entity'] = $variables['elements']['#ai_agents_test_group'];
$tests = [];
foreach ($variables['entity']->tests as $test) {
$tests[] = $test->entity;
}
$variables['tests'] = $tests;
}
/**
* Implements hook_views_pre_render().
*/
function ai_agents_test_preprocess_views_view(&$variables) {
// Add the library to the agent test results view.
if (in_array($variables['view']->id(), [
'ai_agents_test_result',
'ai_agents_test_group_result',
]) && $variables['view']->current_display === 'page_1') {
$variables['#attached']['library'][] = 'ai_agents_test/results';
}
}
/**
* Implements hook_views_pre_render().
*/
function ai_agents_test_group_result_preprocess_views_view(&$variables) {
// Add the library to the agent test results view.
if ($variables['view']->id() === 'ai_agents_test_result' && $variables['view']->current_display === 'page_1') {
$variables['#attached']['library'][] = 'ai_agents_test/results';
}
}
/**
* Prepares variables for agent test group result templates.
*
* Default template: ai-agents-test-group-result.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the agent test group result
* information and any fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_ai_agents_test_group_result(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$variables['#attached']['library'][] = 'ai_agents_test/group_runner';
$variables['entity'] = $variables['elements']['#ai_agents_test_group_result'];
$results = [];
foreach ($variables['entity']->ai_agents_test_results as $result_wrapper) {
$result_wrapper = $result_wrapper->entity;
$results[] = [
'label' => $result_wrapper->test_id->entity->label(),
'description' => $result_wrapper->test_id->entity->description->value,
'result' => $result_wrapper->result->value,
'triggering_instruction' => $result_wrapper->test_id->entity->triggering_instructions->value,
'textual_result' => $result_wrapper->result->value === 'success' ? t('Success') : t('Failure'),
'url' => Url::fromRoute('entity.ai_agents_test_result.canonical', [
'ai_agents_test_result' => $result_wrapper->id(),
]),
];
}
$variables['results'] = $results;
}
