ai_agents_test-1.0.0-alpha1/src/Controller/AgentTestController.php
src/Controller/AgentTestController.php
<?php
namespace Drupal\ai_agents_test\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\ai_agents_test\Service\TestRunner;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Yaml;
/**
* Controller for running tests.
*/
class AgentTestController extends ControllerBase {
/**
* Constructs a new AgentTestController object.
*/
public function __construct(
protected TestRunner $testRunner,
protected PrivateTempStoreFactory $tempStore,
) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('ai_agents_test.test_runner'),
$container->get('tempstore.private'),
);
}
/**
* Run a test.
*/
public function runTest($test_id) {
// Load the test entity.
$test = $this->entityTypeManager()->getStorage('ai_agents_test')->load($test_id);
if (!$test) {
throw new \InvalidArgumentException(sprintf('Test with ID %d not found.', $test_id));
}
// Run the test.
$results = $this->testRunner->runTest($test_id);
// Forward to the entity view page.
return new RedirectResponse(Url::fromRoute('entity.ai_agents_test_result.canonical', ['ai_agents_test_result' => $results['entity']->id()])->toString());
}
/**
* Export a test.
*/
public function exportTest($test_id) {
// Load the test entity.
$test = $this->entityTypeManager()->getStorage('ai_agents_test')->load($test_id);
if (!$test) {
throw new \InvalidArgumentException(sprintf('Test with ID %d not found.', $test_id));
}
// Create an YAML export of the test.
$export = [
'label' => $test->label(),
'description' => $test->description->value,
'ai_agent' => $test->ai_agent->entity->id(),
'rules' => $test->rules->value,
'messages' => $test->messages->value,
'config_reset' => $test->config_reset->value,
'tokens' => $test->tokens->value,
'triggering_instructions' => $test->triggering_instructions->value,
];
// Convert the export array to YAML format.
$yaml_dump = Yaml::dump($export, 2, 10, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
// Make a filename from the label.
$filename = preg_replace('/[^a-zA-Z0-9_]/', '_', $test->label());
$filename = strtolower($filename);
$filename = 'test_' . $filename . '.yaml';
// Return a response with the YAML content.
return new Response($yaml_dump, 200, [
'Content-Type' => 'application/x-yaml',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
'Content-Length' => strlen($yaml_dump),
'Content-Transfer-Encoding' => 'binary',
'Cache-Control' => 'no-store, no-cache, must-revalidate',
'Pragma' => 'no-cache',
]);
}
/**
* Duplicate test.
*
* This will take a test and set it in private temp storage and forward to
* the interactive form.
*/
public function duplicateTest($test_id) {
// Load the test entity.
$test = $this->entityTypeManager()->getStorage('ai_agents_test')->load($test_id);
if (!$test) {
throw new \InvalidArgumentException(sprintf('Test with ID %d not found.', $test_id));
}
$storage = [
'label' => $test->label(),
'description' => $test->description->value,
'ai_agent' => $test->ai_agent->entity->id(),
'rules' => $test->rules->value,
'messages' => $test->messages->value,
'tokens' => $test->tokens->value,
];
$this->tempStore->get('ai_agents_test')->set('import', Yaml::dump($storage, 2, 10, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
// Redirect to the interactive import form.
return new RedirectResponse(Url::fromRoute('ai_agents_test.interactive_add_form')->toString());
}
}
