ArtificialIntelligence.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Settings\Admin;
  8. use OCP\AppFramework\Http\TemplateResponse;
  9. use OCP\AppFramework\Services\IInitialState;
  10. use OCP\IConfig;
  11. use OCP\IL10N;
  12. use OCP\Settings\IDelegatedSettings;
  13. use OCP\SpeechToText\ISpeechToTextManager;
  14. use OCP\SpeechToText\ISpeechToTextProviderWithId;
  15. use OCP\TextProcessing\IManager;
  16. use OCP\TextProcessing\IProvider;
  17. use OCP\TextProcessing\IProviderWithId;
  18. use OCP\TextProcessing\ITaskType;
  19. use OCP\Translation\ITranslationManager;
  20. use OCP\Translation\ITranslationProviderWithId;
  21. use Psr\Container\ContainerExceptionInterface;
  22. use Psr\Container\ContainerInterface;
  23. use Psr\Container\NotFoundExceptionInterface;
  24. class ArtificialIntelligence implements IDelegatedSettings {
  25. public function __construct(
  26. private IConfig $config,
  27. private IL10N $l,
  28. private IInitialState $initialState,
  29. private ITranslationManager $translationManager,
  30. private ISpeechToTextManager $sttManager,
  31. private IManager $textProcessingManager,
  32. private ContainerInterface $container,
  33. private \OCP\TextToImage\IManager $text2imageManager,
  34. private \OCP\TaskProcessing\IManager $taskProcessingManager,
  35. ) {
  36. }
  37. /**
  38. * @return TemplateResponse
  39. */
  40. public function getForm() {
  41. $translationProviders = [];
  42. $translationPreferences = [];
  43. foreach ($this->translationManager->getProviders() as $provider) {
  44. $translationProviders[] = [
  45. 'class' => $provider instanceof ITranslationProviderWithId ? $provider->getId() : $provider::class,
  46. 'name' => $provider->getName(),
  47. ];
  48. $translationPreferences[] = $provider instanceof ITranslationProviderWithId ? $provider->getId() : $provider::class;
  49. }
  50. $sttProviders = [];
  51. foreach ($this->sttManager->getProviders() as $provider) {
  52. $sttProviders[] = [
  53. 'class' => $provider instanceof ISpeechToTextProviderWithId ? $provider->getId() : $provider::class,
  54. 'name' => $provider->getName(),
  55. ];
  56. }
  57. $textProcessingProviders = [];
  58. /** @var array<class-string<ITaskType>, string|class-string<IProvider>> $textProcessingSettings */
  59. $textProcessingSettings = [];
  60. foreach ($this->textProcessingManager->getProviders() as $provider) {
  61. $textProcessingProviders[] = [
  62. 'class' => $provider instanceof IProviderWithId ? $provider->getId() : $provider::class,
  63. 'name' => $provider->getName(),
  64. 'taskType' => $provider->getTaskType(),
  65. ];
  66. if (!isset($textProcessingSettings[$provider->getTaskType()])) {
  67. $textProcessingSettings[$provider->getTaskType()] = $provider instanceof IProviderWithId ? $provider->getId() : $provider::class;
  68. }
  69. }
  70. $textProcessingTaskTypes = [];
  71. foreach ($textProcessingSettings as $taskTypeClass => $providerClass) {
  72. /** @var ITaskType $taskType */
  73. try {
  74. $taskType = $this->container->get($taskTypeClass);
  75. } catch (NotFoundExceptionInterface $e) {
  76. continue;
  77. } catch (ContainerExceptionInterface $e) {
  78. continue;
  79. }
  80. $textProcessingTaskTypes[] = [
  81. 'class' => $taskTypeClass,
  82. 'name' => $taskType->getName(),
  83. 'description' => $taskType->getDescription(),
  84. ];
  85. }
  86. $text2imageProviders = [];
  87. foreach ($this->text2imageManager->getProviders() as $provider) {
  88. $text2imageProviders[] = [
  89. 'id' => $provider->getId(),
  90. 'name' => $provider->getName(),
  91. ];
  92. }
  93. $taskProcessingProviders = [];
  94. /** @var array<class-string<ITaskType>, string|class-string<IProvider>> $taskProcessingSettings */
  95. $taskProcessingSettings = [];
  96. foreach ($this->taskProcessingManager->getProviders() as $provider) {
  97. $taskProcessingProviders[] = [
  98. 'id' => $provider->getId(),
  99. 'name' => $provider->getName(),
  100. 'taskType' => $provider->getTaskTypeId(),
  101. ];
  102. if (!isset($taskProcessingSettings[$provider->getTaskTypeId()])) {
  103. $taskProcessingSettings[$provider->getTaskTypeId()] = $provider->getId();
  104. }
  105. }
  106. $taskProcessingTaskTypes = [];
  107. foreach ($this->taskProcessingManager->getAvailableTaskTypes() as $taskTypeId => $taskTypeDefinition) {
  108. $taskProcessingTaskTypes[] = [
  109. 'id' => $taskTypeId,
  110. 'name' => $taskTypeDefinition['name'],
  111. 'description' => $taskTypeDefinition['description'],
  112. ];
  113. }
  114. $this->initialState->provideInitialState('ai-stt-providers', $sttProviders);
  115. $this->initialState->provideInitialState('ai-translation-providers', $translationProviders);
  116. $this->initialState->provideInitialState('ai-text-processing-providers', $textProcessingProviders);
  117. $this->initialState->provideInitialState('ai-text-processing-task-types', $textProcessingTaskTypes);
  118. $this->initialState->provideInitialState('ai-text2image-providers', $text2imageProviders);
  119. $this->initialState->provideInitialState('ai-task-processing-providers', $taskProcessingProviders);
  120. $this->initialState->provideInitialState('ai-task-processing-task-types', $taskProcessingTaskTypes);
  121. $settings = [
  122. 'ai.stt_provider' => count($sttProviders) > 0 ? $sttProviders[0]['class'] : null,
  123. 'ai.translation_provider_preferences' => $translationPreferences,
  124. 'ai.textprocessing_provider_preferences' => $textProcessingSettings,
  125. 'ai.text2image_provider' => count($text2imageProviders) > 0 ? $text2imageProviders[0]['id'] : null,
  126. 'ai.taskprocessing_provider_preferences' => $taskProcessingSettings,
  127. ];
  128. foreach ($settings as $key => $defaultValue) {
  129. $value = $defaultValue;
  130. $json = $this->config->getAppValue('core', $key, '');
  131. if ($json !== '') {
  132. $value = json_decode($json, true);
  133. switch ($key) {
  134. case 'ai.taskprocessing_provider_preferences':
  135. case 'ai.textprocessing_provider_preferences':
  136. // fill $value with $defaultValue values
  137. $value = array_merge($defaultValue, $value);
  138. break;
  139. case 'ai.translation_provider_preferences':
  140. // Only show entries from $value (saved pref list) that are in $defaultValue (enabled providers)
  141. // and add all providers that are enabled but not in the pref list
  142. if (!is_array($defaultValue)) {
  143. break;
  144. }
  145. $value = array_values(array_unique(array_merge(array_intersect($value, $defaultValue), $defaultValue), SORT_STRING));
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. $settings[$key] = $value;
  152. }
  153. $this->initialState->provideInitialState('ai-settings', $settings);
  154. return new TemplateResponse('settings', 'settings/admin/ai');
  155. }
  156. /**
  157. * @return string the section ID, e.g. 'sharing'
  158. */
  159. public function getSection() {
  160. return 'ai';
  161. }
  162. /**
  163. * @return int whether the form should be rather on the top or bottom of
  164. * the admin section. The forms are arranged in ascending order of the
  165. * priority values. It is required to return a value between 0 and 100.
  166. *
  167. * E.g.: 70
  168. */
  169. public function getPriority() {
  170. return 10;
  171. }
  172. public function getName(): ?string {
  173. return $this->l->t('Artificial Intelligence');
  174. }
  175. public function getAuthorizedAppConfig(): array {
  176. return [
  177. 'core' => ['/ai..*/'],
  178. ];
  179. }
  180. }