TextProcessingApiController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\Core\Controller;
  24. use InvalidArgumentException;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\Attribute\AnonRateLimit;
  27. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  28. use OCP\AppFramework\Http\Attribute\PublicPage;
  29. use OCP\AppFramework\Http\Attribute\UserRateLimit;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\Common\Exception\NotFoundException;
  32. use OCP\IL10N;
  33. use OCP\IRequest;
  34. use OCP\TextProcessing\ITaskType;
  35. use OCP\TextProcessing\Task;
  36. use OCP\TextProcessing\IManager;
  37. use OCP\PreConditionNotMetException;
  38. use Psr\Container\ContainerExceptionInterface;
  39. use Psr\Container\ContainerInterface;
  40. use Psr\Container\NotFoundExceptionInterface;
  41. use Psr\Log\LoggerInterface;
  42. class TextProcessingApiController extends \OCP\AppFramework\OCSController {
  43. public function __construct(
  44. string $appName,
  45. IRequest $request,
  46. private IManager $textProcessingManager,
  47. private IL10N $l,
  48. private ?string $userId,
  49. private ContainerInterface $container,
  50. private LoggerInterface $logger,
  51. ) {
  52. parent::__construct($appName, $request);
  53. }
  54. /**
  55. * This endpoint returns all available LanguageModel task types
  56. *
  57. * @return DataResponse
  58. */
  59. #[PublicPage]
  60. public function taskTypes(): DataResponse {
  61. $typeClasses = $this->textProcessingManager->getAvailableTaskTypes();
  62. $types = [];
  63. /** @var string $typeClass */
  64. foreach ($typeClasses as $typeClass) {
  65. try {
  66. /** @var ITaskType $object */
  67. $object = $this->container->get($typeClass);
  68. } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
  69. $this->logger->warning('Could not find ' . $typeClass, ['exception' => $e]);
  70. continue;
  71. }
  72. $types[] = [
  73. 'id' => $typeClass,
  74. 'name' => $object->getName(),
  75. 'description' => $object->getDescription(),
  76. ];
  77. }
  78. return new DataResponse([
  79. 'types' => $types,
  80. ]);
  81. }
  82. /**
  83. * This endpoint allows scheduling a language model task
  84. *
  85. * @param string $input Input text
  86. * @param string $type Type of the task
  87. * @param string $appId ID of the app that will execute the task
  88. * @param string $identifier An arbitrary identifier for the task
  89. *
  90. * @return DataResponse
  91. *
  92. * 200: Task scheduled successfully
  93. * 400: Scheduling task is not possible
  94. * 412: Scheduling task is not possible
  95. */
  96. #[PublicPage]
  97. #[UserRateLimit(limit: 20, period: 120)]
  98. #[AnonRateLimit(limit: 5, period: 120)]
  99. public function schedule(string $input, string $type, string $appId, string $identifier = ''): DataResponse {
  100. try {
  101. $task = new Task($type, $input, $appId, $this->userId, $identifier);
  102. } catch (InvalidArgumentException) {
  103. return new DataResponse(['message' => $this->l->t('Requested task type does not exist')], Http::STATUS_BAD_REQUEST);
  104. }
  105. try {
  106. $this->textProcessingManager->scheduleTask($task);
  107. $json = $task->jsonSerialize();
  108. return new DataResponse([
  109. 'task' => $json,
  110. ]);
  111. } catch (PreConditionNotMetException) {
  112. return new DataResponse(['message' => $this->l->t('Necessary language model provider is not available')], Http::STATUS_PRECONDITION_FAILED);
  113. }
  114. }
  115. /**
  116. * This endpoint allows checking the status and results of a task.
  117. * Tasks are removed 1 week after receiving their last update.
  118. *
  119. * @param int $id The id of the task
  120. *
  121. * @return DataResponse
  122. *
  123. * 200: Task returned
  124. * 404: Task not found
  125. */
  126. #[PublicPage]
  127. public function getTask(int $id): DataResponse {
  128. try {
  129. $task = $this->textProcessingManager->getUserTask($id, $this->userId);
  130. $json = $task->jsonSerialize();
  131. return new DataResponse([
  132. 'task' => $json,
  133. ]);
  134. } catch (NotFoundException $e) {
  135. return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
  136. } catch (\RuntimeException $e) {
  137. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  138. }
  139. }
  140. /**
  141. * This endpoint allows to delete a scheduled task for a user
  142. *
  143. * @param int $id The id of the task
  144. *
  145. * @return DataResponse
  146. *
  147. * 200: Task returned
  148. * 404: Task not found
  149. */
  150. #[NoAdminRequired]
  151. public function deleteTask(int $id): DataResponse {
  152. try {
  153. $task = $this->textProcessingManager->getUserTask($id, $this->userId);
  154. $this->textProcessingManager->deleteTask($task);
  155. $json = $task->jsonSerialize();
  156. return new DataResponse([
  157. 'task' => $json,
  158. ]);
  159. } catch (NotFoundException $e) {
  160. return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
  161. } catch (\RuntimeException $e) {
  162. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  163. }
  164. }
  165. /**
  166. * This endpoint returns a list of tasks of a user that are related
  167. * with a specific appId and optionally with an identifier
  168. *
  169. * @param string $appId
  170. * @param string|null $identifier
  171. * @return DataResponse
  172. *
  173. * 200: Task list returned
  174. */
  175. #[NoAdminRequired]
  176. public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
  177. try {
  178. $tasks = $this->textProcessingManager->getUserTasksByApp($this->userId, $appId, $identifier);
  179. $json = array_map(static function (Task $task) {
  180. return $task->jsonSerialize();
  181. }, $tasks);
  182. return new DataResponse([
  183. 'tasks' => $json,
  184. ]);
  185. } catch (\RuntimeException $e) {
  186. return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
  187. }
  188. }
  189. }