Task.php 4.6 KB

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