IManager.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\TaskProcessing;
  8. use OCP\Files\File;
  9. use OCP\Files\GenericFileException;
  10. use OCP\Files\NotPermittedException;
  11. use OCP\Lock\LockedException;
  12. use OCP\TaskProcessing\Exception\Exception;
  13. use OCP\TaskProcessing\Exception\NotFoundException;
  14. use OCP\TaskProcessing\Exception\PreConditionNotMetException;
  15. use OCP\TaskProcessing\Exception\UnauthorizedException;
  16. use OCP\TaskProcessing\Exception\ValidationException;
  17. /**
  18. * API surface for apps interacting with and making use of LanguageModel providers
  19. * without known which providers are installed
  20. * @since 30.0.0
  21. */
  22. interface IManager {
  23. /**
  24. * @since 30.0.0
  25. */
  26. public function hasProviders(): bool;
  27. /**
  28. * @return IProvider[]
  29. * @since 30.0.0
  30. */
  31. public function getProviders(): array;
  32. /**
  33. * @param string $taskTypeId
  34. * @return IProvider
  35. * @throws Exception
  36. * @since 30.0.0
  37. */
  38. public function getPreferredProvider(string $taskTypeId);
  39. /**
  40. * @param bool $showDisabled if false, disabled task types will be filtered
  41. * @return array<string, array{name: string, description: string, inputShape: ShapeDescriptor[], inputShapeEnumValues: ShapeEnumValue[][], inputShapeDefaults: array<array-key, numeric|string>, optionalInputShape: ShapeDescriptor[], optionalInputShapeEnumValues: ShapeEnumValue[][], optionalInputShapeDefaults: array<array-key, numeric|string>, outputShape: ShapeDescriptor[], outputShapeEnumValues: ShapeEnumValue[][], optionalOutputShape: ShapeDescriptor[], optionalOutputShapeEnumValues: ShapeEnumValue[][]}>
  42. * @since 30.0.0
  43. * @since 31.0.0 Added the `showDisabled` argument.
  44. */
  45. public function getAvailableTaskTypes(bool $showDisabled = false): array;
  46. /**
  47. * @param Task $task The task to run
  48. * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
  49. * @throws ValidationException the given task input didn't pass validation against the task type's input shape and/or the providers optional input shape specs
  50. * @throws Exception storing the task in the database failed
  51. * @throws UnauthorizedException the user scheduling the task does not have access to the files used in the input
  52. * @since 30.0.0
  53. */
  54. public function scheduleTask(Task $task): void;
  55. /**
  56. * Run the task and return the finished task
  57. *
  58. * @param Task $task The task to run
  59. * @return Task The result task
  60. * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
  61. * @throws ValidationException the given task input didn't pass validation against the task type's input shape and/or the providers optional input shape specs
  62. * @throws Exception storing the task in the database failed
  63. * @throws UnauthorizedException the user scheduling the task does not have access to the files used in the input
  64. * @since 30.0.0
  65. */
  66. public function runTask(Task $task): Task;
  67. /**
  68. * Process task with a synchronous provider
  69. *
  70. * Prepare task input data and run the process method of the provider
  71. * This should only be used by OC\TaskProcessing\SynchronousBackgroundJob::run() and OCP\TaskProcessing\IManager::runTask()
  72. *
  73. * @param Task $task
  74. * @param ISynchronousProvider $provider
  75. * @return bool True if the task has run successfully
  76. * @throws Exception
  77. * @since 30.0.0
  78. */
  79. public function processTask(Task $task, ISynchronousProvider $provider): bool;
  80. /**
  81. * Delete a task that has been scheduled before
  82. *
  83. * @param Task $task The task to delete
  84. * @throws Exception if deleting the task in the database failed
  85. * @since 30.0.0
  86. */
  87. public function deleteTask(Task $task): void;
  88. /**
  89. * @param int $id The id of the task
  90. * @return Task
  91. * @throws Exception If the query failed
  92. * @throws NotFoundException If the task could not be found
  93. * @since 30.0.0
  94. */
  95. public function getTask(int $id): Task;
  96. /**
  97. * @param int $id The id of the task
  98. * @throws Exception If the query failed
  99. * @throws NotFoundException If the task could not be found
  100. * @since 30.0.0
  101. */
  102. public function cancelTask(int $id): void;
  103. /**
  104. * @param int $id The id of the task
  105. * @param string|null $error
  106. * @param array|null $result
  107. * @param bool $isUsingFileIds
  108. * @throws Exception If the query failed
  109. * @throws NotFoundException If the task could not be found
  110. * @since 30.0.0
  111. */
  112. public function setTaskResult(int $id, ?string $error, ?array $result, bool $isUsingFileIds = false): void;
  113. /**
  114. * @param int $id
  115. * @param float $progress
  116. * @return bool `true` if the task should still be running; `false` if the task has been cancelled in the meantime
  117. * @throws ValidationException
  118. * @throws Exception
  119. * @throws NotFoundException
  120. * @since 30.0.0
  121. */
  122. public function setTaskProgress(int $id, float $progress): bool;
  123. /**
  124. * @param list<string> $taskTypeIds
  125. * @param list<int> $taskIdsToIgnore
  126. * @return Task
  127. * @throws Exception If the query failed
  128. * @throws NotFoundException If no task could not be found
  129. * @since 30.0.0
  130. */
  131. public function getNextScheduledTask(array $taskTypeIds = [], array $taskIdsToIgnore = []): Task;
  132. /**
  133. * @param int $id The id of the task
  134. * @param string|null $userId The user id that scheduled the task
  135. * @return Task
  136. * @throws Exception If the query failed
  137. * @throws NotFoundException If the task could not be found
  138. * @since 30.0.0
  139. */
  140. public function getUserTask(int $id, ?string $userId): Task;
  141. /**
  142. * @param string|null $userId The user id that scheduled the task
  143. * @param string|null $taskTypeId The task type id to filter by
  144. * @param string|null $customId
  145. * @return list<Task>
  146. * @throws Exception If the query failed
  147. * @throws NotFoundException If the task could not be found
  148. * @since 30.0.0
  149. */
  150. public function getUserTasks(?string $userId, ?string $taskTypeId = null, ?string $customId = null): array;
  151. /**
  152. * @param string|null $userId The user id that scheduled the task
  153. * @param string|null $taskTypeId The task type id to filter by
  154. * @param string|null $appId The app ID of the app that submitted the task
  155. * @param string|null $customId The custom task ID
  156. * @param int|null $status The task status
  157. * @param int|null $scheduleAfter Minimum schedule time filter
  158. * @param int|null $endedBefore Maximum ending time filter
  159. * @return list<Task>
  160. * @throws Exception If the query failed
  161. * @throws NotFoundException If the task could not be found
  162. * @since 30.0.0
  163. */
  164. public function getTasks(
  165. ?string $userId, ?string $taskTypeId = null, ?string $appId = null, ?string $customId = null,
  166. ?int $status = null, ?int $scheduleAfter = null, ?int $endedBefore = null,
  167. ): array;
  168. /**
  169. * @param string|null $userId
  170. * @param string $appId
  171. * @param string|null $customId
  172. * @return list<Task>
  173. * @throws Exception If the query failed
  174. * @throws \JsonException If parsing the task input and output failed
  175. * @since 30.0.0
  176. */
  177. public function getUserTasksByApp(?string $userId, string $appId, ?string $customId = null): array;
  178. /**
  179. * Prepare the task's input data, so it can be processed by the provider
  180. * ie. this replaces file ids with File objects
  181. *
  182. * @param Task $task
  183. * @return array<array-key, list<numeric|string|File>|numeric|string|File>
  184. * @throws NotPermittedException
  185. * @throws GenericFileException
  186. * @throws LockedException
  187. * @throws ValidationException
  188. * @throws UnauthorizedException
  189. * @since 30.0.0
  190. */
  191. public function prepareInputData(Task $task): array;
  192. /**
  193. * Changes the task status to STATUS_RUNNING and, if successful, returns True.
  194. *
  195. * @param Task $task
  196. * @return bool
  197. * @since 30.0.0
  198. */
  199. public function lockTask(Task $task): bool;
  200. /**
  201. * @param Task $task
  202. * @psalm-param Task::STATUS_* $status
  203. * @param int $status
  204. * @throws \JsonException
  205. * @throws Exception
  206. * @since 30.0.0
  207. */
  208. public function setTaskStatus(Task $task, int $status): void;
  209. }