Added an custom form type for model selection with autocompletition

This commit is contained in:
Jan Böhmer 2026-04-26 15:19:52 +02:00
parent 67cb6fb8a2
commit 9d389309fc
5 changed files with 252 additions and 1 deletions

View file

@ -0,0 +1,62 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Form\Settings;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* An text input with autocomplete for AI models from the given platform.
* The platform is determined by the value of another form field, which is specified by the "platform_selector" option. This allows to filter the available models based on the selected platform.
*/
final class AiModelsType extends AbstractType
{
public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
{
}
public function getParent(): string
{
return TextType::class;
}
public function configureOptions(OptionsResolver $resolver): void
{
//The target label of the platform select, which is used to filter the models for the selected platform.
$resolver->setRequired('platform_selector');
$resolver->setAllowedTypes('platform_selector', 'string');
}
public function finishView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['attr']['data-url-template'] = $this->urlGenerator->generate('typeahead_ai_models', ['platform' => '__PLATFORM__']);
$view->vars['attr']['data-controller'] = 'elements--ai-model-autocomplete';
$view->vars['attr']['data-platform-selector'] = $options['platform_selector'];
}
}

View file

@ -28,8 +28,13 @@ use App\Services\AI\AIPlatforms;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Allow to choose an AI platform from the enabled platforms in the system. This is used in the settings to choose the default platform for AI features.
*/
final class AiPlatformChoiceType extends AbstractType
{
public function __construct(private readonly AIPlatformRegistry $platformRegistry)
@ -49,6 +54,12 @@ final class AiPlatformChoiceType extends AbstractType
'class' => AIPlatforms::class,
'choices' => $choices,
'required' => false,
'platform_selector_label' => null
]);
}
public function finishView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['attr']['data-platform-selector-label'] = $options['platform_selector_label'] ?? $view->vars['id'].'_label';
}
}