1
0

Task.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 OCP\TextProcessing;
  24. /**
  25. * This is a text processing task
  26. * @since 27.1.0
  27. * @psalm-template-covariant T of ITaskType
  28. */
  29. final class Task implements \JsonSerializable {
  30. protected ?int $id = null;
  31. protected ?string $output = null;
  32. private ?\DateTime $completionExpectedAt = null;
  33. /**
  34. * @since 27.1.0
  35. */
  36. public const TYPES = [
  37. FreePromptTaskType::class,
  38. SummaryTaskType::class,
  39. HeadlineTaskType::class,
  40. TopicsTaskType::class,
  41. ];
  42. /**
  43. * @since 27.1.0
  44. */
  45. public const STATUS_FAILED = 4;
  46. /**
  47. * @since 27.1.0
  48. */
  49. public const STATUS_SUCCESSFUL = 3;
  50. /**
  51. * @since 27.1.0
  52. */
  53. public const STATUS_RUNNING = 2;
  54. /**
  55. * @since 27.1.0
  56. */
  57. public const STATUS_SCHEDULED = 1;
  58. /**
  59. * @since 27.1.0
  60. */
  61. public const STATUS_UNKNOWN = 0;
  62. /**
  63. * @psalm-var self::STATUS_*
  64. */
  65. protected int $status = self::STATUS_UNKNOWN;
  66. /**
  67. * @psalm-param class-string<T> $type
  68. * @param string $type
  69. * @param string $input
  70. * @param string $appId
  71. * @param string|null $userId
  72. * @param string $identifier An arbitrary identifier for this task. max length: 255 chars
  73. * @since 27.1.0
  74. */
  75. final public function __construct(
  76. protected string $type,
  77. protected string $input,
  78. protected string $appId,
  79. protected ?string $userId,
  80. protected string $identifier = '',
  81. ) {
  82. }
  83. /**
  84. * @psalm-param IProvider<T> $provider
  85. * @param IProvider $provider
  86. * @return string
  87. * @since 27.1.0
  88. */
  89. public function visitProvider(IProvider $provider): string {
  90. if ($this->canUseProvider($provider)) {
  91. if ($provider instanceof IProviderWithUserId) {
  92. $provider->setUserId($this->getUserId());
  93. }
  94. return $provider->process($this->getInput());
  95. } else {
  96. throw new \RuntimeException('Task of type ' . $this->getType() . ' cannot visit provider with task type ' . $provider->getTaskType());
  97. }
  98. }
  99. /**
  100. * @psalm-param IProvider<T> $provider
  101. * @param IProvider $provider
  102. * @return bool
  103. * @since 27.1.0
  104. */
  105. public function canUseProvider(IProvider $provider): bool {
  106. return $provider->getTaskType() === $this->getType();
  107. }
  108. /**
  109. * @psalm-return class-string<T>
  110. * @since 27.1.0
  111. */
  112. final public function getType(): string {
  113. return $this->type;
  114. }
  115. /**
  116. * @return string|null
  117. * @since 27.1.0
  118. */
  119. final public function getOutput(): ?string {
  120. return $this->output;
  121. }
  122. /**
  123. * @param string|null $output
  124. * @since 27.1.0
  125. */
  126. final public function setOutput(?string $output): void {
  127. $this->output = $output;
  128. }
  129. /**
  130. * @psalm-return self::STATUS_*
  131. * @since 27.1.0
  132. */
  133. final public function getStatus(): int {
  134. return $this->status;
  135. }
  136. /**
  137. * @psalm-param self::STATUS_* $status
  138. * @since 27.1.0
  139. */
  140. final public function setStatus(int $status): void {
  141. $this->status = $status;
  142. }
  143. /**
  144. * @return int|null
  145. * @since 27.1.0
  146. */
  147. final public function getId(): ?int {
  148. return $this->id;
  149. }
  150. /**
  151. * @param int|null $id
  152. * @since 27.1.0
  153. */
  154. final public function setId(?int $id): void {
  155. $this->id = $id;
  156. }
  157. /**
  158. * @return string
  159. * @since 27.1.0
  160. */
  161. final public function getInput(): string {
  162. return $this->input;
  163. }
  164. /**
  165. * @return string
  166. * @since 27.1.0
  167. */
  168. final public function getAppId(): string {
  169. return $this->appId;
  170. }
  171. /**
  172. * @return string
  173. * @since 27.1.0
  174. */
  175. final public function getIdentifier(): string {
  176. return $this->identifier;
  177. }
  178. /**
  179. * @return string|null
  180. * @since 27.1.0
  181. */
  182. final public function getUserId(): ?string {
  183. return $this->userId;
  184. }
  185. /**
  186. * @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}
  187. * @since 27.1.0
  188. */
  189. public function jsonSerialize(): array {
  190. return [
  191. 'id' => $this->getId(),
  192. 'type' => $this->getType(),
  193. 'status' => $this->getStatus(),
  194. 'userId' => $this->getUserId(),
  195. 'appId' => $this->getAppId(),
  196. 'input' => $this->getInput(),
  197. 'output' => $this->getOutput(),
  198. 'identifier' => $this->getIdentifier(),
  199. 'completionExpectedAt' => $this->getCompletionExpectedAt()?->getTimestamp(),
  200. ];
  201. }
  202. /**
  203. * @param null|\DateTime $completionExpectedAt
  204. * @return void
  205. * @since 28.0.0
  206. */
  207. final public function setCompletionExpectedAt(?\DateTime $completionExpectedAt): void {
  208. $this->completionExpectedAt = $completionExpectedAt;
  209. }
  210. /**
  211. * @return \DateTime|null
  212. * @since 28.0.0
  213. */
  214. final public function getCompletionExpectedAt(): ?\DateTime {
  215. return $this->completionExpectedAt;
  216. }
  217. }