123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- declare(strict_types=1);
- namespace OC\Core\Controller;
- use InvalidArgumentException;
- use OCA\Core\ResponseDefinitions;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\Attribute\AnonRateLimit;
- use OCP\AppFramework\Http\Attribute\NoAdminRequired;
- use OCP\AppFramework\Http\Attribute\PublicPage;
- use OCP\AppFramework\Http\Attribute\UserRateLimit;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\Common\Exception\NotFoundException;
- use OCP\DB\Exception;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\PreConditionNotMetException;
- use OCP\TextProcessing\Exception\TaskFailureException;
- use OCP\TextProcessing\IManager;
- use OCP\TextProcessing\ITaskType;
- use OCP\TextProcessing\Task;
- use Psr\Container\ContainerExceptionInterface;
- use Psr\Container\ContainerInterface;
- use Psr\Container\NotFoundExceptionInterface;
- use Psr\Log\LoggerInterface;
- class TextProcessingApiController extends \OCP\AppFramework\OCSController {
- public function __construct(
- string $appName,
- IRequest $request,
- private IManager $textProcessingManager,
- private IL10N $l,
- private ?string $userId,
- private ContainerInterface $container,
- private LoggerInterface $logger,
- ) {
- parent::__construct($appName, $request);
- }
-
-
- public function taskTypes(): DataResponse {
- $typeClasses = $this->textProcessingManager->getAvailableTaskTypes();
- $types = [];
-
- foreach ($typeClasses as $typeClass) {
- try {
-
- $object = $this->container->get($typeClass);
- } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
- $this->logger->warning('Could not find ' . $typeClass, ['exception' => $e]);
- continue;
- }
- $types[] = [
- 'id' => $typeClass,
- 'name' => $object->getName(),
- 'description' => $object->getDescription(),
- ];
- }
- return new DataResponse([
- 'types' => $types,
- ]);
- }
-
-
-
-
- public function schedule(string $input, string $type, string $appId, string $identifier = ''): DataResponse {
- try {
- $task = new Task($type, $input, $appId, $this->userId, $identifier);
- } catch (InvalidArgumentException) {
- return new DataResponse(['message' => $this->l->t('Requested task type does not exist')], Http::STATUS_BAD_REQUEST);
- }
- try {
- try {
- $this->textProcessingManager->runOrScheduleTask($task);
- } catch(TaskFailureException) {
-
- }
- $json = $task->jsonSerialize();
- return new DataResponse([
- 'task' => $json,
- ]);
- } catch (PreConditionNotMetException) {
- return new DataResponse(['message' => $this->l->t('Necessary language model provider is not available')], Http::STATUS_PRECONDITION_FAILED);
- } catch (Exception) {
- return new DataResponse(['message' => 'Internal server error'], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
-
-
- public function getTask(int $id): DataResponse {
- try {
- $task = $this->textProcessingManager->getUserTask($id, $this->userId);
- $json = $task->jsonSerialize();
- return new DataResponse([
- 'task' => $json,
- ]);
- } catch (NotFoundException $e) {
- return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
- } catch (\RuntimeException $e) {
- return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
-
-
- public function deleteTask(int $id): DataResponse {
- try {
- $task = $this->textProcessingManager->getUserTask($id, $this->userId);
- $this->textProcessingManager->deleteTask($task);
- $json = $task->jsonSerialize();
- return new DataResponse([
- 'task' => $json,
- ]);
- } catch (NotFoundException $e) {
- return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
- } catch (\RuntimeException $e) {
- return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
-
-
- public function listTasksByApp(string $appId, ?string $identifier = null): DataResponse {
- try {
- $tasks = $this->textProcessingManager->getUserTasksByApp($this->userId, $appId, $identifier);
-
- $json = array_map(static function (Task $task) {
- return $task->jsonSerialize();
- }, $tasks);
- return new DataResponse([
- 'tasks' => $json,
- ]);
- } catch (\RuntimeException $e) {
- return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- }
- }
|