You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextProcessingApiController.php 7.2KB

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