Task.php 4.6 KB

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