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.
156 lines
5.3 KiB
156 lines
5.3 KiB
|
3 years ago
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
/**
|
||
|
3 years ago
|
* @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
|
||
|
3 years ago
|
*
|
||
|
3 years ago
|
* @author Marcel Klehr <mklehr@gmx.net>
|
||
|
3 years ago
|
*
|
||
|
|
* @license GNU AGPL version 3 or any later version
|
||
|
|
*
|
||
|
|
* This program is free software: you can redistribute it and/or modify
|
||
|
|
* it under the terms of the GNU Affero General Public License as
|
||
|
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
|
* License, or (at your option) any later version.
|
||
|
|
*
|
||
|
|
* This program is distributed in the hope that it will be useful,
|
||
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
|
* GNU Affero General Public License for more details.
|
||
|
|
*
|
||
|
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
*/
|
||
|
|
|
||
|
|
|
||
|
|
namespace OC\Core\Controller;
|
||
|
|
|
||
|
|
use InvalidArgumentException;
|
||
|
|
use OCP\AppFramework\Http;
|
||
|
|
use OCP\AppFramework\Http\DataResponse;
|
||
|
3 years ago
|
use OCP\Common\Exception\NotFoundException;
|
||
|
3 years ago
|
use OCP\IL10N;
|
||
|
|
use OCP\IRequest;
|
||
|
3 years ago
|
use OCP\TextProcessing\ITaskType;
|
||
|
|
use OCP\TextProcessing\Task;
|
||
|
|
use OCP\TextProcessing\IManager;
|
||
|
3 years ago
|
use OCP\PreConditionNotMetException;
|
||
|
3 years ago
|
use Psr\Container\ContainerExceptionInterface;
|
||
|
|
use Psr\Container\ContainerInterface;
|
||
|
|
use Psr\Container\NotFoundExceptionInterface;
|
||
|
|
use Psr\Log\LoggerInterface;
|
||
|
3 years ago
|
|
||
|
3 years ago
|
class TextProcessingApiController extends \OCP\AppFramework\OCSController {
|
||
|
3 years ago
|
public function __construct(
|
||
|
3 years ago
|
string $appName,
|
||
|
|
IRequest $request,
|
||
|
|
private IManager $languageModelManager,
|
||
|
|
private IL10N $l,
|
||
|
|
private ?string $userId,
|
||
|
|
private ContainerInterface $container,
|
||
|
|
private LoggerInterface $logger,
|
||
|
3 years ago
|
) {
|
||
|
|
parent::__construct($appName, $request);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
3 years ago
|
* This endpoint returns all available LanguageModel task types
|
||
|
|
*
|
||
|
3 years ago
|
* @PublicPage
|
||
|
3 years ago
|
* @return DataResponse<Http::STATUS_OK, array{types: list<array{id: string, name: string, description: string}>}, array{}>
|
||
|
3 years ago
|
*
|
||
|
|
* 200: Task types returned
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
public function taskTypes(): DataResponse {
|
||
|
3 years ago
|
$typeClasses = $this->languageModelManager->getAvailableTaskTypes();
|
||
|
|
/** @var list<array{id: string, name: string, description: string}> $types */
|
||
|
|
$types = [];
|
||
|
|
foreach ($typeClasses as $typeClass) {
|
||
|
|
/** @var ITaskType $object */
|
||
|
|
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(),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
return new DataResponse([
|
||
|
3 years ago
|
'types' => $types,
|
||
|
3 years ago
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
3 years ago
|
* This endpoint allows scheduling a language model task
|
||
|
|
*
|
||
|
3 years ago
|
* @PublicPage
|
||
|
|
* @UserRateThrottle(limit=20, period=120)
|
||
|
|
* @AnonRateThrottle(limit=5, period=120)
|
||
|
3 years ago
|
* @param string $input The input for the language model task
|
||
|
|
* @param string $type The task type
|
||
|
|
* @param string $appId The originating app ID
|
||
|
|
* @param string $identifier An identifier to identify this task
|
||
|
3 years ago
|
* @return DataResponse<Http::STATUS_OK, array{task: array{id: ?int, type: string, status: int, userId: ?string, appId: string, input: string, output: ?string, identifier: string}}, array{}>|DataResponse<Http::STATUS_PRECONDITION_FAILED|Http::STATUS_BAD_REQUEST, array{message: string}, array{}>
|
||
|
3 years ago
|
*
|
||
|
|
* 200: Task scheduled
|
||
|
|
* 400: Task type does not exist
|
||
|
|
* 412: Task type not available
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
public function schedule(string $input, string $type, string $appId, string $identifier = ''): DataResponse {
|
||
|
3 years ago
|
try {
|
||
|
3 years ago
|
$task = Task::factory($type, $input, $this->userId, $appId, $identifier);
|
||
|
|
} catch (InvalidArgumentException) {
|
||
|
3 years ago
|
return new DataResponse(['message' => $this->l->t('Requested task type does not exist')], Http::STATUS_BAD_REQUEST);
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
$this->languageModelManager->scheduleTask($task);
|
||
|
|
|
||
|
3 years ago
|
$json = $task->jsonSerialize();
|
||
|
|
|
||
|
3 years ago
|
return new DataResponse([
|
||
|
3 years ago
|
'task' => $json,
|
||
|
3 years ago
|
]);
|
||
|
|
} catch (PreConditionNotMetException) {
|
||
|
|
return new DataResponse(['message' => $this->l->t('Necessary language model provider is not available')], Http::STATUS_PRECONDITION_FAILED);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
3 years ago
|
* This endpoint allows checking the status and results of a task.
|
||
|
|
* Tasks are removed 1 week after receiving their last update.
|
||
|
|
*
|
||
|
3 years ago
|
* @PublicPage
|
||
|
3 years ago
|
* @param int $id The id of the task
|
||
|
3 years ago
|
* @return DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message:string}, array{}>|DataResponse<Http::STATUS_OK, array{task: array{id: ?int, type: string, status: int, userId: ?string, appId: string, input: string, output: ?string, identifier: string}}, array{}>
|
||
|
3 years ago
|
*
|
||
|
|
* 200: Task returned
|
||
|
|
* 404: Task not found
|
||
|
|
* 500: Internal error
|
||
|
3 years ago
|
*/
|
||
|
|
public function getTask(int $id): DataResponse {
|
||
|
|
try {
|
||
|
|
$task = $this->languageModelManager->getTask($id);
|
||
|
|
|
||
|
3 years ago
|
if ($this->userId !== $task->getUserId()) {
|
||
|
|
return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
$json = $task->jsonSerialize();
|
||
|
|
|
||
|
3 years ago
|
return new DataResponse([
|
||
|
3 years ago
|
'task' => $json,
|
||
|
3 years ago
|
]);
|
||
|
3 years ago
|
} catch (NotFoundException $e) {
|
||
|
3 years ago
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|