gutenberg_ai_tools-1.0.x-dev/src/Form/GutenbergAISettings.php

src/Form/GutenbergAISettings.php
<?php

namespace Drupal\gutenberg_ai_tools\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class GutenbergAISettings.
 *
 * Provides configuration settings for the Gutenberg AI Plugin.
 */
class GutenbergAISettings extends ConfigFormBase {

  /**
   * The AI provider plugin manager.
   *
   * @var \Drupal\ai\AiProviderPluginManager
   */
  protected $aiProvider;

  /**
   * A request stack symfony instance.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'gutenberg_ai_tools_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['gutenberg_ai_tools.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);

    $instance->aiProvider = $container->get('ai.provider');
    $instance->requestStack = $container->get('request_stack');

    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    $config = $this->config('gutenberg_ai_tools.settings');

    $form['options'] = [
      '#type' => 'markup',
      '#markup' => '<p>Set the AI options for the response sent to Gutenberg AI Block, here you can set what model to use.</p><br />',
    ];

    // Use 'chat' operation type as per AI module documentation.
    $operation_type = 'chat';

    // Check if we have any providers for this operation type.
    if (!$this->aiProvider->hasProvidersForOperationType($operation_type, TRUE)) {
      $form['no_providers'] = [
        '#type' => 'markup',
        '#markup' => $this->t('No AI providers available for chat. Please configure at least one provider 
                              in the <a href="@url">AI module settings</a>.', ['@url' => '/admin/config/ai']),
      ];

      return $form;
    }

    // Get the simple provider/model options as recommended
    // in the documentation.
    $provider_model_options = $this->aiProvider->getSimpleProviderModelOptions($operation_type);

    $ai_provider = $this->aiProvider->getDefaultProviderForOperationType('chat');

    if (empty($provider_model_options)) {
      $form['no_models'] = [
        '#type' => 'markup',
        '#markup' => $this->t('No models available for chat. Please configure at least one provider with chat models in the <a href="@url">AI module settings</a>.', ['@url' => '/admin/config/ai']),
      ];
      return $form;
    }

    $form['ai_provider'] = [
      '#type' => '#markup',
      '#markup' => $this->t('Your current AI Provider is <strong>@AIProvider</strong>, you can set it 
                            up at <strong><a href="@url">@url</a></strong>',
                            [
                              '@url' => $host = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost() . "/admin/config/ai/providers",
                              "@AIProvider" => $ai_provider['provider_id'],
                            ]),
    ];

    $form['provider_model'] = [
      '#type' => 'select',
      '#title' => $this->t('AI Provider and Model'),
      '#options' => $provider_model_options,
      '#default_value' => $config->get('provider_model'),
      '#description' => $this->t('Select which AI provider and model to use.'),
    ];

    $form['chat_configuration'] = [
      '#type' => '#markup',
      '#markup' => $this->t('You can set up the chat configuration (Max Tokens, Temperature, etc.) at <strong><a href="@url">@url</a></strong>', ['@url' => $host = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost() . "/admin/config/ai/explorers/chat_generator"]),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('gutenberg_ai_tools.settings');

    $provider_model = $form_state->getValue('provider_model');
    $ai_provider = $this->aiProvider->getDefaultProviderForOperationType('chat');

    $config->set('provider_model', $provider_model)->save();
    $config->set('ai_provider', $ai_provider)->save();

    parent::submitForm($form, $form_state);
  }

}

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

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