ai_agents_test-1.0.0-alpha1/src/Service/TestSyncService.php

src/Service/TestSyncService.php
<?php

namespace Drupal\ai_agents_test\Service;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\ai_agents_test\Entity\AiAgentsTestGroup;
use Symfony\Component\Yaml\Yaml;

/**
 * Service for synchronizing test data.
 */
class TestSyncService {

  /**
   * Constructor for TestSyncService.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
   *   The current user service.
   */
  public function __construct(
    protected EntityTypeManagerInterface $entityTypeManager,
    protected AccountProxyInterface $currentUser,
  ) {

  }

  /**
   * Import one test group.
   *
   * @param string $file_url
   *   The test data file to import.
   *
   * @return \Drupal\ai_agents_test\Entity\AiAgentsTestGroup
   *   The imported test group.
   */
  public function importTestGroup(string $file_url): AiAgentsTestGroup {
    if (!file_exists($file_url)) {
      throw new \InvalidArgumentException('The provided file does not exist: ' . $file_url);
    }
    $test_data = file_get_contents($file_url);
    try {
      $yaml = Yaml::parse($test_data);
    }
    catch (\Exception $e) {
      throw new \InvalidArgumentException('Invalid YAML format: ' . $e->getMessage());
    }
    // First create the tests.
    if (empty($yaml['tests']) || !is_array($yaml['tests'])) {
      throw new \InvalidArgumentException('No tests found in the provided YAML data.');
    }
    $tests = [];
    $testStorage = $this->entityTypeManager->getStorage('ai_agents_test');
    foreach ($yaml['tests'] as $test) {
      // Create the test entity.
      $test_entity = $testStorage->create([
        'label' => $test['label'][0]['value'],
        'status' => $test['status'][0]['value'],
        'description' => $test['description'][0]['value'],
        'uid' => $this->currentUser->id(),
        'created' => $test['created'][0]['value'],
        'changed' => $test['changed'][0]['value'],
        'ai_agent' => $test['ai_agent'][0]['target_id'],
        'messages' => $test['messages'],
        'config_reset' => $test['config_reset'][0]['value'],
        'triggering_instructions' => $test['triggering_instructions'][0]['value'],
        'rules' => $test['rules'],
        'tokens' => $test['tokens'] ?? '',
      ]);
      // Save the test entity.
      $test_entity->save();
      // Add the test entity to the tests array.
      $tests[] = $test_entity;
    }
    // Now create the test group.
    $test_group_storage = $this->entityTypeManager->getStorage('ai_agents_test_group');
    $test_group = $test_group_storage->create([
      'label' => $yaml['label'],
      'description' => $yaml['description'],
      'approval_percentage' => $yaml['approval_percentage'],
      'tests' => $tests,
      'uid' => $this->currentUser->id(),
      'config_reset' => $yaml['config_reset'] ?? FALSE,
    ]);
    // Save the test group entity.
    $test_group->save();
    return $test_group;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc