Manager.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. namespace OC\TextProcessing;
  24. use OC\AppFramework\Bootstrap\Coordinator;
  25. use OC\TextProcessing\Db\Task as DbTask;
  26. use OC\TextProcessing\Db\TaskMapper;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\Common\Exception\NotFoundException;
  31. use OCP\DB\Exception;
  32. use OCP\IConfig;
  33. use OCP\IServerContainer;
  34. use OCP\PreConditionNotMetException;
  35. use OCP\TextProcessing\Exception\TaskFailureException;
  36. use OCP\TextProcessing\IManager;
  37. use OCP\TextProcessing\IProvider;
  38. use OCP\TextProcessing\IProviderWithExpectedRuntime;
  39. use OCP\TextProcessing\IProviderWithId;
  40. use OCP\TextProcessing\Task;
  41. use OCP\TextProcessing\Task as OCPTask;
  42. use Psr\Log\LoggerInterface;
  43. use RuntimeException;
  44. use Throwable;
  45. class Manager implements IManager {
  46. /** @var ?IProvider[] */
  47. private ?array $providers = null;
  48. public function __construct(
  49. private IServerContainer $serverContainer,
  50. private Coordinator $coordinator,
  51. private LoggerInterface $logger,
  52. private IJobList $jobList,
  53. private TaskMapper $taskMapper,
  54. private IConfig $config,
  55. ) {
  56. }
  57. public function getProviders(): array {
  58. $context = $this->coordinator->getRegistrationContext();
  59. if ($context === null) {
  60. return [];
  61. }
  62. if ($this->providers !== null) {
  63. return $this->providers;
  64. }
  65. $this->providers = [];
  66. foreach ($context->getTextProcessingProviders() as $providerServiceRegistration) {
  67. $class = $providerServiceRegistration->getService();
  68. try {
  69. $this->providers[$class] = $this->serverContainer->get($class);
  70. } catch (Throwable $e) {
  71. $this->logger->error('Failed to load Text processing provider ' . $class, [
  72. 'exception' => $e,
  73. ]);
  74. }
  75. }
  76. return $this->providers;
  77. }
  78. public function hasProviders(): bool {
  79. $context = $this->coordinator->getRegistrationContext();
  80. if ($context === null) {
  81. return false;
  82. }
  83. return count($context->getTextProcessingProviders()) > 0;
  84. }
  85. /**
  86. * @inheritDoc
  87. */
  88. public function getAvailableTaskTypes(): array {
  89. $tasks = [];
  90. foreach ($this->getProviders() as $provider) {
  91. $tasks[$provider->getTaskType()] = true;
  92. }
  93. return array_keys($tasks);
  94. }
  95. public function canHandleTask(OCPTask $task): bool {
  96. return in_array($task->getType(), $this->getAvailableTaskTypes());
  97. }
  98. /**
  99. * @inheritDoc
  100. */
  101. public function runTask(OCPTask $task): string {
  102. if (!$this->canHandleTask($task)) {
  103. throw new PreConditionNotMetException('No text processing provider is installed that can handle this task');
  104. }
  105. $providers = $this->getPreferredProviders($task);
  106. foreach ($providers as $provider) {
  107. try {
  108. $task->setStatus(OCPTask::STATUS_RUNNING);
  109. if ($provider instanceof IProviderWithExpectedRuntime) {
  110. $completionExpectedAt = new \DateTime('now');
  111. $completionExpectedAt->add(new \DateInterval('PT'.$provider->getExpectedRuntime().'S'));
  112. $task->setCompletionExpectedAt($completionExpectedAt);
  113. }
  114. if ($task->getId() === null) {
  115. $taskEntity = $this->taskMapper->insert(DbTask::fromPublicTask($task));
  116. $task->setId($taskEntity->getId());
  117. } else {
  118. $this->taskMapper->update(DbTask::fromPublicTask($task));
  119. }
  120. $output = $task->visitProvider($provider);
  121. $task->setOutput($output);
  122. $task->setStatus(OCPTask::STATUS_SUCCESSFUL);
  123. $this->taskMapper->update(DbTask::fromPublicTask($task));
  124. return $output;
  125. } catch (\Throwable $e) {
  126. $this->logger->info('LanguageModel call using provider ' . $provider->getName() . ' failed', ['exception' => $e]);
  127. $task->setStatus(OCPTask::STATUS_FAILED);
  128. $this->taskMapper->update(DbTask::fromPublicTask($task));
  129. throw new TaskFailureException('LanguageModel call using provider ' . $provider->getName() . ' failed: ' . $e->getMessage(), 0, $e);
  130. }
  131. }
  132. $task->setStatus(OCPTask::STATUS_FAILED);
  133. $this->taskMapper->update(DbTask::fromPublicTask($task));
  134. throw new TaskFailureException('Could not run task');
  135. }
  136. /**
  137. * @inheritDoc
  138. */
  139. public function scheduleTask(OCPTask $task): void {
  140. if (!$this->canHandleTask($task)) {
  141. throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task');
  142. }
  143. $task->setStatus(OCPTask::STATUS_SCHEDULED);
  144. $providers = $this->getPreferredProviders($task);
  145. if (count($providers) === 0) {
  146. throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task');
  147. }
  148. [$provider,] = $providers;
  149. if ($provider instanceof IProviderWithExpectedRuntime) {
  150. $completionExpectedAt = new \DateTime('now');
  151. $completionExpectedAt->add(new \DateInterval('PT'.$provider->getExpectedRuntime().'S'));
  152. $task->setCompletionExpectedAt($completionExpectedAt);
  153. }
  154. $taskEntity = DbTask::fromPublicTask($task);
  155. $this->taskMapper->insert($taskEntity);
  156. $task->setId($taskEntity->getId());
  157. $this->jobList->add(TaskBackgroundJob::class, [
  158. 'taskId' => $task->getId()
  159. ]);
  160. }
  161. /**
  162. * @inheritDoc
  163. */
  164. public function runOrScheduleTask(OCPTask $task): bool {
  165. if (!$this->canHandleTask($task)) {
  166. throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task');
  167. }
  168. [$provider,] = $this->getPreferredProviders($task);
  169. $maxExecutionTime = (int) ini_get('max_execution_time');
  170. // Offload the task to a background job if the expected runtime of the likely provider is longer than 80% of our max execution time
  171. // or if the provider doesn't provide a getExpectedRuntime() method
  172. if (!$provider instanceof IProviderWithExpectedRuntime || $provider->getExpectedRuntime() > $maxExecutionTime * 0.8) {
  173. $this->scheduleTask($task);
  174. return false;
  175. }
  176. $this->runTask($task);
  177. return true;
  178. }
  179. /**
  180. * @inheritDoc
  181. */
  182. public function deleteTask(Task $task): void {
  183. $taskEntity = DbTask::fromPublicTask($task);
  184. $this->taskMapper->delete($taskEntity);
  185. $this->jobList->remove(TaskBackgroundJob::class, [
  186. 'taskId' => $task->getId()
  187. ]);
  188. }
  189. /**
  190. * Get a task from its id
  191. *
  192. * @param int $id The id of the task
  193. * @return OCPTask
  194. * @throws RuntimeException If the query failed
  195. * @throws NotFoundException If the task could not be found
  196. */
  197. public function getTask(int $id): OCPTask {
  198. try {
  199. $taskEntity = $this->taskMapper->find($id);
  200. return $taskEntity->toPublicTask();
  201. } catch (DoesNotExistException $e) {
  202. throw new NotFoundException('Could not find task with the provided id');
  203. } catch (MultipleObjectsReturnedException $e) {
  204. throw new RuntimeException('Could not uniquely identify task with given id', 0, $e);
  205. } catch (Exception $e) {
  206. throw new RuntimeException('Failure while trying to find task by id: ' . $e->getMessage(), 0, $e);
  207. }
  208. }
  209. /**
  210. * Get a task from its user id and task id
  211. * If userId is null, this can only get a task that was scheduled anonymously
  212. *
  213. * @param int $id The id of the task
  214. * @param string|null $userId The user id that scheduled the task
  215. * @return OCPTask
  216. * @throws RuntimeException If the query failed
  217. * @throws NotFoundException If the task could not be found
  218. */
  219. public function getUserTask(int $id, ?string $userId): OCPTask {
  220. try {
  221. $taskEntity = $this->taskMapper->findByIdAndUser($id, $userId);
  222. return $taskEntity->toPublicTask();
  223. } catch (DoesNotExistException $e) {
  224. throw new NotFoundException('Could not find task with the provided id and user id');
  225. } catch (MultipleObjectsReturnedException $e) {
  226. throw new RuntimeException('Could not uniquely identify task with given id and user id', 0, $e);
  227. } catch (Exception $e) {
  228. throw new RuntimeException('Failure while trying to find task by id and user id: ' . $e->getMessage(), 0, $e);
  229. }
  230. }
  231. /**
  232. * Get a list of tasks scheduled by a specific user for a specific app
  233. * and optionally with a specific identifier.
  234. * This cannot be used to get anonymously scheduled tasks
  235. *
  236. * @param string $userId
  237. * @param string $appId
  238. * @param string|null $identifier
  239. * @return array
  240. */
  241. public function getUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array {
  242. try {
  243. $taskEntities = $this->taskMapper->findUserTasksByApp($userId, $appId, $identifier);
  244. return array_map(static function (DbTask $taskEntity) {
  245. return $taskEntity->toPublicTask();
  246. }, $taskEntities);
  247. } catch (Exception $e) {
  248. throw new RuntimeException('Failure while trying to find tasks by appId and identifier: ' . $e->getMessage(), 0, $e);
  249. }
  250. }
  251. /**
  252. * @param OCPTask $task
  253. * @return IProvider[]
  254. */
  255. public function getPreferredProviders(OCPTask $task): array {
  256. $providers = $this->getProviders();
  257. $json = $this->config->getAppValue('core', 'ai.textprocessing_provider_preferences', '');
  258. if ($json !== '') {
  259. $preferences = json_decode($json, true);
  260. if (isset($preferences[$task->getType()])) {
  261. // If a preference for this task type is set, move the preferred provider to the start
  262. $provider = current(array_values(array_filter($providers, function ($provider) use ($preferences, $task) {
  263. if ($provider instanceof IProviderWithId) {
  264. return $provider->getId() === $preferences[$task->getType()];
  265. }
  266. return $provider::class === $preferences[$task->getType()];
  267. })));
  268. if ($provider !== false) {
  269. $providers = array_filter($providers, fn ($p) => $p !== $provider);
  270. array_unshift($providers, $provider);
  271. }
  272. }
  273. }
  274. return array_values(array_filter($providers, fn (IProvider $provider) => $task->canUseProvider($provider)));
  275. }
  276. }