1
0

Task.php 4.6 KB

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