Task.php 4.6 KB

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