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.
236 lines
5.1 KiB
236 lines
5.1 KiB
|
3 years ago
|
<?php
|
||
|
3 years ago
|
|
||
|
3 years ago
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
|
||
|
|
*
|
||
|
|
* @author Marcel Klehr <mklehr@gmx.net>
|
||
|
|
*
|
||
|
|
* @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/>.
|
||
|
|
*/
|
||
|
3 years ago
|
|
||
|
3 years ago
|
namespace OCP\TextProcessing;
|
||
|
3 years ago
|
|
||
|
3 years ago
|
/**
|
||
|
3 years ago
|
* This is a text processing task
|
||
|
|
* @since 27.1.0
|
||
|
|
* @template T of ITaskType
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final class Task implements \JsonSerializable {
|
||
|
3 years ago
|
protected ?int $id = null;
|
||
|
|
protected ?string $output = null;
|
||
|
3 years ago
|
|
||
|
|
/**
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
|
*/
|
||
|
|
public const TYPES = [
|
||
|
|
FreePromptTaskType::class,
|
||
|
|
SummaryTaskType::class,
|
||
|
|
HeadlineTaskType::class,
|
||
|
|
TopicsTaskType::class,
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @since 27.1.0
|
||
|
|
*/
|
||
|
|
public const STATUS_FAILED = 4;
|
||
|
|
/**
|
||
|
|
* @since 27.1.0
|
||
|
|
*/
|
||
|
|
public const STATUS_SUCCESSFUL = 3;
|
||
|
|
/**
|
||
|
|
* @since 27.1.0
|
||
|
|
*/
|
||
|
|
public const STATUS_RUNNING = 2;
|
||
|
|
/**
|
||
|
|
* @since 27.1.0
|
||
|
|
*/
|
||
|
|
public const STATUS_SCHEDULED = 1;
|
||
|
|
/**
|
||
|
|
* @since 27.1.0
|
||
|
|
*/
|
||
|
|
public const STATUS_UNKNOWN = 0;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @psalm-var self::STATUS_*
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
protected int $status = self::STATUS_UNKNOWN;
|
||
|
3 years ago
|
|
||
|
3 years ago
|
/**
|
||
|
3 years ago
|
* @param class-string<T> $type
|
||
|
3 years ago
|
* @param string $input
|
||
|
|
* @param string $appId
|
||
|
|
* @param string|null $userId
|
||
|
3 years ago
|
* @param string $identifier An arbitrary identifier for this task. max length: 255 chars
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public function __construct(
|
||
|
3 years ago
|
protected string $type,
|
||
|
3 years ago
|
protected string $input,
|
||
|
|
protected string $appId,
|
||
|
|
protected ?string $userId,
|
||
|
3 years ago
|
protected string $identifier = '',
|
||
|
3 years ago
|
) {
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
/**
|
||
|
3 years ago
|
* @psalm-param IProvider<T> $provider
|
||
|
|
* @param IProvider $provider
|
||
|
3 years ago
|
* @return string
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
public function visitProvider(IProvider $provider): string {
|
||
|
|
if ($this->canUseProvider($provider)) {
|
||
|
|
return $provider->process($this->getInput());
|
||
|
|
} else {
|
||
|
|
throw new \RuntimeException('Task of type ' . $this->getType() . ' cannot visit provider with task type ' . $provider->getTaskType());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @psalm-param IProvider<T> $provider
|
||
|
|
* @param IProvider $provider
|
||
|
|
* @return bool
|
||
|
|
* @since 27.1.0
|
||
|
|
*/
|
||
|
|
public function canUseProvider(IProvider $provider): bool {
|
||
|
|
return $provider->getTaskType() === $this->getType();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return class-string<T>
|
||
|
|
* @since 27.1.0
|
||
|
|
*/
|
||
|
|
final public function getType(): string {
|
||
|
|
return $this->type;
|
||
|
|
}
|
||
|
3 years ago
|
|
||
|
3 years ago
|
/**
|
||
|
|
* @return string|null
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
|
final public function getOutput(): ?string {
|
||
|
|
return $this->output;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param string|null $output
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
|
final public function setOutput(?string $output): void {
|
||
|
|
$this->output = $output;
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
/**
|
||
|
3 years ago
|
* @psalm-return self::STATUS_*
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public function getStatus(): int {
|
||
|
3 years ago
|
return $this->status;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
3 years ago
|
* @psalm-param self::STATUS_* $status
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public function setStatus(int $status): void {
|
||
|
3 years ago
|
$this->status = $status;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return int|null
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public function getId(): ?int {
|
||
|
3 years ago
|
return $this->id;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param int|null $id
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public function setId(?int $id): void {
|
||
|
3 years ago
|
$this->id = $id;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return string
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public function getInput(): string {
|
||
|
3 years ago
|
return $this->input;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return string
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public function getAppId(): string {
|
||
|
3 years ago
|
return $this->appId;
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
/**
|
||
|
|
* @return string
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
|
final public function getIdentifier(): string {
|
||
|
|
return $this->identifier;
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
/**
|
||
|
|
* @return string|null
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public function getUserId(): ?string {
|
||
|
3 years ago
|
return $this->userId;
|
||
|
|
}
|
||
|
3 years ago
|
|
||
|
3 years ago
|
/**
|
||
|
3 years ago
|
* @return array{id: ?string, type: class-string<T>, status: int, userId: ?string, appId: string, input: string, output: ?string, identifier: string}
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
public function jsonSerialize(): array {
|
||
|
3 years ago
|
return [
|
||
|
|
'id' => $this->getId(),
|
||
|
|
'type' => $this->getType(),
|
||
|
|
'status' => $this->getStatus(),
|
||
|
|
'userId' => $this->getUserId(),
|
||
|
|
'appId' => $this->getAppId(),
|
||
|
|
'input' => $this->getInput(),
|
||
|
|
'output' => $this->getOutput(),
|
||
|
3 years ago
|
'identifier' => $this->getIdentifier(),
|
||
|
3 years ago
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param string $type
|
||
|
|
* @param string $input
|
||
|
|
* @param string|null $userId
|
||
|
|
* @param string $appId
|
||
|
3 years ago
|
* @param string $identifier
|
||
|
3 years ago
|
* @return Task
|
||
|
3 years ago
|
* @throws \InvalidArgumentException
|
||
|
3 years ago
|
* @since 27.1.0
|
||
|
3 years ago
|
*/
|
||
|
3 years ago
|
final public static function factory(string $type, string $input, ?string $userId, string $appId, string $identifier = ''): Task {
|
||
|
|
if (!in_array($type, self::TYPES)) {
|
||
|
3 years ago
|
throw new \InvalidArgumentException('Unknown task type');
|
||
|
|
}
|
||
|
3 years ago
|
return new Task($type, $input, $appId, $userId, $identifier);
|
||
|
3 years ago
|
}
|
||
|
3 years ago
|
}
|