Task.php 5.3 KB

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