1
0

IManager.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * @return array<array-key,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[][]}>
  41. * @since 30.0.0
  42. */
  43. public function getAvailableTaskTypes(): array;
  44. /**
  45. * @param Task $task The task to run
  46. * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
  47. * @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
  48. * @throws Exception storing the task in the database failed
  49. * @throws UnauthorizedException the user scheduling the task does not have access to the files used in the input
  50. * @since 30.0.0
  51. */
  52. public function scheduleTask(Task $task): void;
  53. /**
  54. * Run the task and return the finished task
  55. *
  56. * @param Task $task The task to run
  57. * @return Task The result task
  58. * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
  59. * @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
  60. * @throws Exception storing the task in the database failed
  61. * @throws UnauthorizedException the user scheduling the task does not have access to the files used in the input
  62. * @since 30.0.0
  63. */
  64. public function runTask(Task $task): Task;
  65. /**
  66. * Process task with a synchronous provider
  67. *
  68. * Prepare task input data and run the process method of the provider
  69. * This should only be used by OC\TaskProcessing\SynchronousBackgroundJob::run() and OCP\TaskProcessing\IManager::runTask()
  70. *
  71. * @param Task $task
  72. * @param ISynchronousProvider $provider
  73. * @return bool True if the task has run successfully
  74. * @throws Exception
  75. * @since 30.0.0
  76. */
  77. public function processTask(Task $task, ISynchronousProvider $provider): bool;
  78. /**
  79. * Delete a task that has been scheduled before
  80. *
  81. * @param Task $task The task to delete
  82. * @throws Exception if deleting the task in the database failed
  83. * @since 30.0.0
  84. */
  85. public function deleteTask(Task $task): void;
  86. /**
  87. * @param int $id The id of the task
  88. * @return Task
  89. * @throws Exception If the query failed
  90. * @throws NotFoundException If the task could not be found
  91. * @since 30.0.0
  92. */
  93. public function getTask(int $id): Task;
  94. /**
  95. * @param int $id The id of the task
  96. * @throws Exception If the query failed
  97. * @throws NotFoundException If the task could not be found
  98. * @since 30.0.0
  99. */
  100. public function cancelTask(int $id): void;
  101. /**
  102. * @param int $id The id of the task
  103. * @param string|null $error
  104. * @param array|null $result
  105. * @param bool $isUsingFileIds
  106. * @throws Exception If the query failed
  107. * @throws NotFoundException If the task could not be found
  108. * @since 30.0.0
  109. */
  110. public function setTaskResult(int $id, ?string $error, ?array $result, bool $isUsingFileIds = false): void;
  111. /**
  112. * @param int $id
  113. * @param float $progress
  114. * @return bool `true` if the task should still be running; `false` if the task has been cancelled in the meantime
  115. * @throws ValidationException
  116. * @throws Exception
  117. * @throws NotFoundException
  118. * @since 30.0.0
  119. */
  120. public function setTaskProgress(int $id, float $progress): bool;
  121. /**
  122. * @param list<string> $taskTypeIds
  123. * @param list<int> $taskIdsToIgnore
  124. * @return Task
  125. * @throws Exception If the query failed
  126. * @throws NotFoundException If no task could not be found
  127. * @since 30.0.0
  128. */
  129. public function getNextScheduledTask(array $taskTypeIds = [], array $taskIdsToIgnore = []): Task;
  130. /**
  131. * @param int $id The id of the task
  132. * @param string|null $userId The user id that scheduled the task
  133. * @return Task
  134. * @throws Exception If the query failed
  135. * @throws NotFoundException If the task could not be found
  136. * @since 30.0.0
  137. */
  138. public function getUserTask(int $id, ?string $userId): Task;
  139. /**
  140. * @param string|null $userId The user id that scheduled the task
  141. * @param string|null $taskTypeId The task type id to filter by
  142. * @param string|null $customId
  143. * @return list<Task>
  144. * @throws Exception If the query failed
  145. * @throws NotFoundException If the task could not be found
  146. * @since 30.0.0
  147. */
  148. public function getUserTasks(?string $userId, ?string $taskTypeId = null, ?string $customId = null): array;
  149. /**
  150. * @param string|null $userId The user id that scheduled the task
  151. * @param string|null $taskTypeId The task type id to filter by
  152. * @param string|null $appId The app ID of the app that submitted the task
  153. * @param string|null $customId The custom task ID
  154. * @param int|null $status The task status
  155. * @param int|null $scheduleAfter Minimum schedule time filter
  156. * @param int|null $endedBefore Maximum ending time filter
  157. * @return list<Task>
  158. * @throws Exception If the query failed
  159. * @throws NotFoundException If the task could not be found
  160. * @since 30.0.0
  161. */
  162. public function getTasks(
  163. ?string $userId, ?string $taskTypeId = null, ?string $appId = null, ?string $customId = null,
  164. ?int $status = null, ?int $scheduleAfter = null, ?int $endedBefore = null
  165. ): array;
  166. /**
  167. * @param string|null $userId
  168. * @param string $appId
  169. * @param string|null $customId
  170. * @return list<Task>
  171. * @throws Exception If the query failed
  172. * @throws \JsonException If parsing the task input and output failed
  173. * @since 30.0.0
  174. */
  175. public function getUserTasksByApp(?string $userId, string $appId, ?string $customId = null): array;
  176. /**
  177. * Prepare the task's input data, so it can be processed by the provider
  178. * ie. this replaces file ids with File objects
  179. *
  180. * @param Task $task
  181. * @return array<array-key, list<numeric|string|File>|numeric|string|File>
  182. * @throws NotPermittedException
  183. * @throws GenericFileException
  184. * @throws LockedException
  185. * @throws ValidationException
  186. * @throws UnauthorizedException
  187. * @since 30.0.0
  188. */
  189. public function prepareInputData(Task $task): array;
  190. /**
  191. * Changes the task status to STATUS_RUNNING and, if successful, returns True.
  192. *
  193. * @param Task $task
  194. * @return bool
  195. * @since 30.0.0
  196. */
  197. public function lockTask(Task $task): bool;
  198. /**
  199. * @param Task $task
  200. * @psalm-param Task::STATUS_* $status
  201. * @param int $status
  202. * @throws \JsonException
  203. * @throws Exception
  204. * @since 30.0.0
  205. */
  206. public function setTaskStatus(Task $task, int $status): void;
  207. }