1
0

ArtificialIntelligence.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
  5. *
  6. * @author Marcel Klehr <mklehr@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Settings\Admin;
  25. use OCP\AppFramework\Http\TemplateResponse;
  26. use OCP\AppFramework\Services\IInitialState;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\Settings\IDelegatedSettings;
  30. use OCP\SpeechToText\ISpeechToTextManager;
  31. use OCP\TextProcessing\IManager;
  32. use OCP\TextProcessing\IProvider;
  33. use OCP\TextProcessing\ITaskType;
  34. use OCP\Translation\ITranslationManager;
  35. use Psr\Container\ContainerExceptionInterface;
  36. use Psr\Container\ContainerInterface;
  37. use Psr\Container\NotFoundExceptionInterface;
  38. class ArtificialIntelligence implements IDelegatedSettings {
  39. public function __construct(
  40. private IConfig $config,
  41. private IL10N $l,
  42. private IInitialState $initialState,
  43. private ITranslationManager $translationManager,
  44. private ISpeechToTextManager $sttManager,
  45. private IManager $textProcessingManager,
  46. private ContainerInterface $container,
  47. ) {
  48. }
  49. /**
  50. * @return TemplateResponse
  51. */
  52. public function getForm() {
  53. $translationProviders = [];
  54. $translationPreferences = [];
  55. foreach ($this->translationManager->getProviders() as $provider) {
  56. $translationProviders[] = [
  57. 'class' => $provider::class,
  58. 'name' => $provider->getName(),
  59. ];
  60. $translationPreferences[] = $provider::class;
  61. }
  62. $sttProviders = [];
  63. foreach ($this->sttManager->getProviders() as $provider) {
  64. $sttProviders[] = [
  65. 'class' => $provider::class,
  66. 'name' => $provider->getName(),
  67. ];
  68. }
  69. $textProcessingProviders = [];
  70. /** @var array<class-string<ITaskType>, class-string<IProvider>> $textProcessingSettings */
  71. $textProcessingSettings = [];
  72. foreach ($this->textProcessingManager->getProviders() as $provider) {
  73. $textProcessingProviders[] = [
  74. 'class' => $provider::class,
  75. 'name' => $provider->getName(),
  76. 'taskType' => $provider->getTaskType(),
  77. ];
  78. $textProcessingSettings[$provider->getTaskType()] = $provider::class;
  79. }
  80. $textProcessingTaskTypes = [];
  81. foreach ($textProcessingSettings as $taskTypeClass => $providerClass) {
  82. /** @var ITaskType $taskType */
  83. try {
  84. $taskType = $this->container->get($taskTypeClass);
  85. } catch (NotFoundExceptionInterface $e) {
  86. continue;
  87. } catch (ContainerExceptionInterface $e) {
  88. continue;
  89. }
  90. $textProcessingTaskTypes[] = [
  91. 'class' => $taskTypeClass,
  92. 'name' => $taskType->getName(),
  93. 'description' => $taskType->getDescription(),
  94. ];
  95. }
  96. $this->initialState->provideInitialState('ai-stt-providers', $sttProviders);
  97. $this->initialState->provideInitialState('ai-translation-providers', $translationProviders);
  98. $this->initialState->provideInitialState('ai-text-processing-providers', $textProcessingProviders);
  99. $this->initialState->provideInitialState('ai-text-processing-task-types', $textProcessingTaskTypes);
  100. $settings = [
  101. 'ai.stt_provider' => count($sttProviders) > 0 ? $sttProviders[0]['class'] : null,
  102. 'ai.textprocessing_provider_preferences' => $textProcessingSettings,
  103. 'ai.translation_provider_preferences' => $translationPreferences,
  104. ];
  105. foreach ($settings as $key => $defaultValue) {
  106. $value = $defaultValue;
  107. $json = $this->config->getAppValue('core', $key, '');
  108. if ($json !== '') {
  109. $value = json_decode($json, true);
  110. switch($key) {
  111. case 'ai.textprocessing_provider_preferences':
  112. // fill $value with $defaultValue values
  113. $value = array_merge($defaultValue, $value);
  114. break;
  115. case 'ai.translation_provider_preferences':
  116. $value += array_diff($defaultValue, $value); // Add entries from $defaultValue that are not in $value to the end of $value
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. $settings[$key] = $value;
  123. }
  124. $this->initialState->provideInitialState('ai-settings', $settings);
  125. return new TemplateResponse('settings', 'settings/admin/ai');
  126. }
  127. /**
  128. * @return string the section ID, e.g. 'sharing'
  129. */
  130. public function getSection() {
  131. return 'ai';
  132. }
  133. /**
  134. * @return int whether the form should be rather on the top or bottom of
  135. * the admin section. The forms are arranged in ascending order of the
  136. * priority values. It is required to return a value between 0 and 100.
  137. *
  138. * E.g.: 70
  139. */
  140. public function getPriority() {
  141. return 10;
  142. }
  143. public function getName(): ?string {
  144. return $this->l->t('Artificial Intelligence');
  145. }
  146. public function getAuthorizedAppConfig(): array {
  147. return [
  148. 'core' => ['/ai..*/'],
  149. ];
  150. }
  151. }