|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 4 | + * |
| 5 | + * Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics) |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | + |
| 24 | +namespace App\Services\AI; |
| 25 | + |
| 26 | +use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface; |
| 27 | +use Symfony\AI\Platform\PlatformInterface; |
| 28 | +use Symfony\Component\DependencyInjection\Attribute\AutowireIterator; |
| 29 | + |
| 30 | +final readonly class AIPlatformRegistry |
| 31 | +{ |
| 32 | + /** |
| 33 | + * All registered platforms, indexed by their service tag name (e.g. "openrouter", "lmstudio") |
| 34 | + * @var array<string, PlatformInterface> $allPlatforms |
| 35 | + */ |
| 36 | + private array $allPlatforms; |
| 37 | + |
| 38 | + /** |
| 39 | + * All registered platforms, indexed by their AIPlatforms enum value (e.g. AIPlatforms::OPENROUTER->value) |
| 40 | + * @var array<string, PlatformInterface> $enabledPlatforms |
| 41 | + */ |
| 42 | + private array $enabledPlatforms; |
| 43 | + |
| 44 | + public function __construct( |
| 45 | + SettingsManagerInterface $settingsManager, |
| 46 | + |
| 47 | + #[AutowireIterator(tag: 'ai.platform', indexAttribute: 'name')] |
| 48 | + iterable $platforms, |
| 49 | + ) { |
| 50 | + $this->allPlatforms = iterator_to_array($platforms); |
| 51 | + |
| 52 | + //Check which platforms are active based on the settings and store them in $activePlatforms |
| 53 | + $tmp = []; |
| 54 | + foreach (AIPlatforms::cases() as $platform) { |
| 55 | + if (isset($this->allPlatforms[$platform->toServiceTagName()])) { |
| 56 | + //Check if the platform is active by calling its isActive() on the settings class |
| 57 | + $settings = $settingsManager->get($platform->toSettingsClass()); |
| 58 | + if (!$settings->isAIPlatformEnabled()) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $tmp[$platform->value] = $this->allPlatforms[$platform->toServiceTagName()]; |
| 63 | + } |
| 64 | + } |
| 65 | + $this->enabledPlatforms = $tmp; |
| 66 | + } |
| 67 | + |
| 68 | + public function getPlatform(AIPlatforms $platform): PlatformInterface |
| 69 | + { |
| 70 | + if (!isset($this->enabledPlatforms[$platform->value])) { |
| 71 | + throw new \InvalidArgumentException(sprintf('AI platform "%s" is not active or does not exist.', $platform->name)); |
| 72 | + } |
| 73 | + |
| 74 | + return $this->enabledPlatforms[$platform->value]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Check if the given platform is active (i.e. it is registered and its settings are properly configured) |
| 79 | + * @param AIPlatforms $platform |
| 80 | + * @return bool |
| 81 | + */ |
| 82 | + public function isEnabled(AIPlatforms $platform): bool |
| 83 | + { |
| 84 | + return isset($this->enabledPlatforms[$platform->value]); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns an array of all active platforms, indexed by their AIPlatforms enum value (e.g. AIPlatforms::OPENROUTER->value) |
| 89 | + * @return PlatformInterface[] |
| 90 | + */ |
| 91 | + public function getEnabledPlatforms(): array |
| 92 | + { |
| 93 | + return $this->enabledPlatforms; |
| 94 | + } |
| 95 | +} |
0 commit comments