ai_agents_test-1.0.0-alpha1/src/Form/PrepareAgentTestGroupRunForm.php

src/Form/PrepareAgentTestGroupRunForm.php
<?php

declare(strict_types=1);

namespace Drupal\ai_agents_test\Form;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ai\AiProviderPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form controller for preparing the group run.
 */
final class PrepareAgentTestGroupRunForm extends FormBase {

  /**
   * Constructs a new PrepareAgentTestGroupRunForm object.
   */
  public function __construct(
    protected AiProviderPluginManager $aiProvider,
    protected EntityTypeManagerInterface $entityTypeManager,
  ) {
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('ai.provider'),
      $container->get('entity_type.manager'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'ai_agents_test_group_run_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    // Get the group ID from the query string.
    $group_id = $this->getRequest()->query->get('group_id');
    // Add so you can pick the provider/model.
    $models = $this->aiProvider->getSimpleProviderModelOptions('chat');
    // Get the default provider.
    $defaults = $this->aiProvider->getSimpleDefaultProviderOptions('chat_with_tools');

    // Get all the test groups.
    $test_groups = $this->entityTypeManager->getStorage('ai_agents_test_group')->loadMultiple();
    $test_groups_options = [];
    foreach ($test_groups as $test_group) {
      $test_groups_options[$test_group->id()] = $test_group->label();
    }

    // Agent Test fieldset.
    $form['agent_test'] = [
      '#type' => 'details',
      '#title' => $this->t('Agent Test'),
      '#open' => TRUE,
    ];

    $form['agent_test']['test_group'] = [
      '#type' => 'select',
      '#title' => $this->t('Test group'),
      '#options' => $test_groups_options,
      '#description' => $this->t('Select the test group you want to run.'),
      '#required' => TRUE,
      '#default_value' => $group_id,
    ];

    $form['agent_test']['model'] = [
      '#type' => 'select',
      '#title' => $this->t('Model'),
      '#options' => $models,
      '#description' => $this->t('Select the model you want to use.'),
      '#required' => TRUE,
      '#default_value' => $defaults,
    ];

    // Agent Evaluation fieldset.
    $form['agent_evaluation'] = [
      '#type' => 'details',
      '#title' => $this->t('Agent Evaluation'),
      '#open' => TRUE,
    ];

    $form['agent_evaluation']['use_same_model'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Use the default model for evaluating the test results.'),
      '#description' => $this->t('Uncheck to select a different model for conducting the evaluation of the test run. See <a href="/admin/config/ai/settings">AI Settings</a> » Chat with Tools/Function Calling.'),
      '#default_value' => TRUE,
      '#ajax' => [
        'callback' => '::evaluationModelCallback',
        'wrapper' => 'evaluation-model-wrapper',
        'event' => 'change',
      ],
    ];

    // Container for the evaluation model dropdown.
    $form['agent_evaluation']['evaluation_model_wrapper'] = [
      '#type' => 'container',
      '#attributes' => ['id' => 'evaluation-model-wrapper'],
    ];

    // Show evaluation model dropdown only when checkbox is unchecked.
    $use_same_model = $form_state->getValue('use_same_model', TRUE);
    if (!$use_same_model) {
      $form['agent_evaluation']['evaluation_model_wrapper']['evaluation_model'] = [
        '#type' => 'select',
        '#title' => $this->t('Evaluation Model'),
        '#options' => $models,
        '#description' => $this->t('Select the model you want to use for evaluation.'),
        '#required' => TRUE,
        '#default_value' => $defaults,
      ];
    }

    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Start a new test run'),
    ];
    return $form;
  }

  /**
   * Ajax callback for evaluation model visibility.
   */
  public function evaluationModelCallback(array &$form, FormStateInterface $form_state) {
    return $form['agent_evaluation']['evaluation_model_wrapper'];
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $parts = explode('__', $form_state->getValue('model'));

    // Build query parameters.
    $query = [
      'provider_id' => $parts[0],
      'model_id' => $parts[1],
    ];

    // Handle evaluation model parameters.
    $use_same_model = $form_state->getValue('use_same_model');
    if (!$use_same_model) {
      // User has unchecked - use the specifically selected evaluation model.
      $eval_parts = explode('__', $form_state->getValue('evaluation_model'));
      $query['eval_provider_id'] = $eval_parts[0];
      $query['eval_model_id'] = $eval_parts[1];
    }
    else {
      // User has checked - use the site's default model for chat_with_tools.
      $defaults = $this->aiProvider->getSimpleDefaultProviderOptions('chat_with_tools');
      $default_parts = explode('__', $defaults);
      $query['eval_provider_id'] = $default_parts[0];
      $query['eval_model_id'] = $default_parts[1];
    }

    // We redirect to the test group runner.
    $form_state->setRedirect('ai_agents_test.run_test_group', [
      'group_id' => $form_state->getValue('test_group'),
    ], [
      'query' => $query,
    ]);
  }

}

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

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