IManager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\TextProcessing;
  8. use OCP\Common\Exception\NotFoundException;
  9. use OCP\DB\Exception;
  10. use OCP\PreConditionNotMetException;
  11. use OCP\TextProcessing\Exception\TaskFailureException;
  12. use RuntimeException;
  13. /**
  14. * API surface for apps interacting with and making use of LanguageModel providers
  15. * without known which providers are installed
  16. * @since 27.1.0
  17. */
  18. interface IManager {
  19. /**
  20. * @since 27.1.0
  21. */
  22. public function hasProviders(): bool;
  23. /**
  24. * @return IProvider[]
  25. * @since 27.1.0
  26. */
  27. public function getProviders(): array;
  28. /**
  29. * @return string[]
  30. * @since 27.1.0
  31. */
  32. public function getAvailableTaskTypes(): array;
  33. /**
  34. * @param Task $task The task to run
  35. * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
  36. * @throws TaskFailureException If running the task failed
  37. * @since 27.1.0
  38. */
  39. public function runTask(Task $task): string;
  40. /**
  41. * Will schedule an LLM inference process in the background. The result will become available
  42. * with the \OCP\LanguageModel\Events\TaskSuccessfulEvent
  43. * If inference fails a \OCP\LanguageModel\Events\TaskFailedEvent will be dispatched instead
  44. *
  45. * @param Task $task The task to schedule
  46. * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
  47. * @throws Exception storing the task in the database failed
  48. * @since 27.1.0
  49. */
  50. public function scheduleTask(Task $task) : void;
  51. /**
  52. * If the designated provider for the passed task provides an expected average runtime, we check if the runtime fits into the
  53. * max execution time of this php process and run it synchronously if it does, if it doesn't fit (or the provider doesn't provide that information)
  54. * execution is deferred to a background job
  55. *
  56. * @param Task $task The task to schedule
  57. * @returns bool A boolean indicating whether the task was run synchronously (`true`) or offloaded to a background job (`false`)
  58. * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called
  59. * @throws TaskFailureException If running the task failed
  60. * @throws Exception storing the task in the database failed
  61. * @since 28.0.0
  62. */
  63. public function runOrScheduleTask(Task $task): bool;
  64. /**
  65. * Delete a task that has been scheduled before
  66. *
  67. * @param Task $task The task to delete
  68. * @since 27.1.0
  69. */
  70. public function deleteTask(Task $task): void;
  71. /**
  72. * @param int $id The id of the task
  73. * @return Task
  74. * @throws RuntimeException If the query failed
  75. * @throws NotFoundException If the task could not be found
  76. * @since 27.1.0
  77. */
  78. public function getTask(int $id): Task;
  79. /**
  80. * @param int $id The id of the task
  81. * @param string|null $userId The user id that scheduled the task
  82. * @return Task
  83. * @throws RuntimeException If the query failed
  84. * @throws NotFoundException If the task could not be found
  85. * @since 27.1.0
  86. */
  87. public function getUserTask(int $id, ?string $userId): Task;
  88. /**
  89. * @param string $userId
  90. * @param string $appId
  91. * @param string|null $identifier
  92. * @return array
  93. * @since 27.1.0
  94. */
  95. public function getUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array;
  96. }