2023-06-15 07:22:16 -04:00
|
|
|
<?php
|
2023-06-27 10:58:17 -04:00
|
|
|
|
2023-06-27 10:44:15 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2023-06-27 10:44:15 -04:00
|
|
|
*/
|
2023-06-15 07:22:16 -04:00
|
|
|
|
2023-07-14 09:59:50 -04:00
|
|
|
namespace OCP\TextProcessing;
|
2023-06-15 07:22:16 -04:00
|
|
|
|
2023-06-20 11:30:07 -04:00
|
|
|
/**
|
2023-07-14 09:59:50 -04:00
|
|
|
* This is a text processing task
|
|
|
|
|
* @since 27.1.0
|
2023-11-29 04:55:12 -05:00
|
|
|
* @psalm-template-covariant T of ITaskType
|
2024-07-26 05:20:46 -04:00
|
|
|
* @deprecated 30.0.0
|
2023-06-20 11:30:07 -04:00
|
|
|
*/
|
2023-07-14 09:59:50 -04:00
|
|
|
final class Task implements \JsonSerializable {
|
2023-06-29 11:08:23 -04:00
|
|
|
protected ?int $id = null;
|
|
|
|
|
protected ?string $output = null;
|
2023-11-03 11:22:54 -04:00
|
|
|
private ?\DateTime $completionExpectedAt = null;
|
2023-07-06 06:48:40 -04:00
|
|
|
|
|
|
|
|
/**
|
2023-07-14 09:59:50 -04:00
|
|
|
* @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_*
|
2023-07-06 06:48:40 -04:00
|
|
|
*/
|
2023-07-14 09:59:50 -04:00
|
|
|
protected int $status = self::STATUS_UNKNOWN;
|
2023-06-15 07:22:16 -04:00
|
|
|
|
2023-06-20 11:30:07 -04:00
|
|
|
/**
|
2023-11-29 04:55:12 -05:00
|
|
|
* @psalm-param class-string<T> $type
|
2023-07-14 10:27:06 -04:00
|
|
|
* @param string $type
|
2023-06-20 11:30:07 -04:00
|
|
|
* @param string $input
|
|
|
|
|
* @param string $appId
|
|
|
|
|
* @param string|null $userId
|
2023-07-06 04:24:28 -04:00
|
|
|
* @param string $identifier An arbitrary identifier for this task. max length: 255 chars
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-20 11:30:07 -04:00
|
|
|
*/
|
2023-06-20 08:58:58 -04:00
|
|
|
final public function __construct(
|
2023-07-14 09:59:50 -04:00
|
|
|
protected string $type,
|
2023-06-15 07:22:16 -04:00
|
|
|
protected string $input,
|
|
|
|
|
protected string $appId,
|
|
|
|
|
protected ?string $userId,
|
2023-07-06 04:24:28 -04:00
|
|
|
protected string $identifier = '',
|
2023-06-15 07:22:16 -04:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 11:30:07 -04:00
|
|
|
/**
|
2023-11-29 04:55:12 -05:00
|
|
|
* @psalm-param IProvider<T> $provider
|
2023-11-07 06:02:03 -05:00
|
|
|
* @param IProvider $provider
|
2023-06-20 11:30:07 -04:00
|
|
|
* @return string
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-20 11:30:07 -04:00
|
|
|
*/
|
2023-11-07 06:02:03 -05:00
|
|
|
public function visitProvider(IProvider $provider): string {
|
2023-07-14 09:59:50 -04:00
|
|
|
if ($this->canUseProvider($provider)) {
|
2023-11-07 06:02:03 -05:00
|
|
|
if ($provider instanceof IProviderWithUserId) {
|
2023-11-03 11:22:54 -04:00
|
|
|
$provider->setUserId($this->getUserId());
|
|
|
|
|
}
|
2023-07-14 09:59:50 -04:00
|
|
|
return $provider->process($this->getInput());
|
|
|
|
|
} else {
|
|
|
|
|
throw new \RuntimeException('Task of type ' . $this->getType() . ' cannot visit provider with task type ' . $provider->getTaskType());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-29 04:55:12 -05:00
|
|
|
* @psalm-param IProvider<T> $provider
|
2023-07-14 09:59:50 -04:00
|
|
|
* @param IProvider $provider
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 27.1.0
|
|
|
|
|
*/
|
|
|
|
|
public function canUseProvider(IProvider $provider): bool {
|
|
|
|
|
return $provider->getTaskType() === $this->getType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-29 04:55:12 -05:00
|
|
|
* @psalm-return class-string<T>
|
2023-07-14 09:59:50 -04:00
|
|
|
* @since 27.1.0
|
|
|
|
|
*/
|
|
|
|
|
final public function getType(): string {
|
|
|
|
|
return $this->type;
|
|
|
|
|
}
|
2023-06-16 07:06:47 -04:00
|
|
|
|
2023-06-20 09:45:18 -04:00
|
|
|
/**
|
|
|
|
|
* @return string|null
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-20 09:45:18 -04:00
|
|
|
*/
|
|
|
|
|
final public function getOutput(): ?string {
|
|
|
|
|
return $this->output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|null $output
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-20 09:45:18 -04:00
|
|
|
*/
|
|
|
|
|
final public function setOutput(?string $output): void {
|
|
|
|
|
$this->output = $output;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 07:22:16 -04:00
|
|
|
/**
|
2023-07-14 09:59:50 -04:00
|
|
|
* @psalm-return self::STATUS_*
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-15 07:22:16 -04:00
|
|
|
*/
|
2023-06-20 08:58:58 -04:00
|
|
|
final public function getStatus(): int {
|
2023-06-15 07:22:16 -04:00
|
|
|
return $this->status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-14 09:59:50 -04:00
|
|
|
* @psalm-param self::STATUS_* $status
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-15 07:22:16 -04:00
|
|
|
*/
|
2023-06-20 08:58:58 -04:00
|
|
|
final public function setStatus(int $status): void {
|
2023-06-15 07:22:16 -04:00
|
|
|
$this->status = $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int|null
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-15 07:22:16 -04:00
|
|
|
*/
|
2023-06-20 08:58:58 -04:00
|
|
|
final public function getId(): ?int {
|
2023-06-15 07:22:16 -04:00
|
|
|
return $this->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int|null $id
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-15 07:22:16 -04:00
|
|
|
*/
|
2023-06-20 08:58:58 -04:00
|
|
|
final public function setId(?int $id): void {
|
2023-06-15 07:22:16 -04:00
|
|
|
$this->id = $id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-15 07:22:16 -04:00
|
|
|
*/
|
2023-06-20 08:58:58 -04:00
|
|
|
final public function getInput(): string {
|
2023-06-15 07:22:16 -04:00
|
|
|
return $this->input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-15 07:22:16 -04:00
|
|
|
*/
|
2023-06-20 08:58:58 -04:00
|
|
|
final public function getAppId(): string {
|
2023-06-15 07:22:16 -04:00
|
|
|
return $this->appId;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-06 04:24:28 -04:00
|
|
|
/**
|
|
|
|
|
* @return string
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-07-06 04:24:28 -04:00
|
|
|
*/
|
|
|
|
|
final public function getIdentifier(): string {
|
|
|
|
|
return $this->identifier;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 07:22:16 -04:00
|
|
|
/**
|
|
|
|
|
* @return string|null
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-15 07:22:16 -04:00
|
|
|
*/
|
2023-06-20 08:58:58 -04:00
|
|
|
final public function getUserId(): ?string {
|
2023-06-15 07:22:16 -04:00
|
|
|
return $this->userId;
|
|
|
|
|
}
|
2023-06-16 07:06:47 -04:00
|
|
|
|
2023-06-20 11:30:07 -04:00
|
|
|
/**
|
2023-11-29 04:55:12 -05:00
|
|
|
* @psalm-return array{id: ?int, type: class-string<T>, status: 0|1|2|3|4, userId: ?string, appId: string, input: string, output: ?string, identifier: string, completionExpectedAt: ?int}
|
2023-07-07 07:46:03 -04:00
|
|
|
* @since 27.1.0
|
2023-06-20 11:30:07 -04:00
|
|
|
*/
|
2023-07-14 09:59:50 -04:00
|
|
|
public function jsonSerialize(): array {
|
2023-06-20 08:41:58 -04:00
|
|
|
return [
|
|
|
|
|
'id' => $this->getId(),
|
|
|
|
|
'type' => $this->getType(),
|
|
|
|
|
'status' => $this->getStatus(),
|
|
|
|
|
'userId' => $this->getUserId(),
|
|
|
|
|
'appId' => $this->getAppId(),
|
|
|
|
|
'input' => $this->getInput(),
|
|
|
|
|
'output' => $this->getOutput(),
|
2023-07-06 04:24:28 -04:00
|
|
|
'identifier' => $this->getIdentifier(),
|
2023-11-03 11:22:54 -04:00
|
|
|
'completionExpectedAt' => $this->getCompletionExpectedAt()?->getTimestamp(),
|
2023-06-20 08:41:58 -04:00
|
|
|
];
|
|
|
|
|
}
|
2023-11-03 11:22:54 -04:00
|
|
|
|
2023-11-06 08:53:20 -05:00
|
|
|
/**
|
|
|
|
|
* @param null|\DateTime $completionExpectedAt
|
|
|
|
|
* @return void
|
|
|
|
|
* @since 28.0.0
|
|
|
|
|
*/
|
|
|
|
|
final public function setCompletionExpectedAt(?\DateTime $completionExpectedAt): void {
|
|
|
|
|
$this->completionExpectedAt = $completionExpectedAt;
|
|
|
|
|
}
|
2023-11-03 11:22:54 -04:00
|
|
|
|
2023-11-06 08:53:20 -05:00
|
|
|
/**
|
|
|
|
|
* @return \DateTime|null
|
|
|
|
|
* @since 28.0.0
|
|
|
|
|
*/
|
|
|
|
|
final public function getCompletionExpectedAt(): ?\DateTime {
|
|
|
|
|
return $this->completionExpectedAt;
|
|
|
|
|
}
|
2023-06-15 07:22:16 -04:00
|
|
|
}
|