123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- declare(strict_types=1);
- namespace OC\TextProcessing;
- use OC\AppFramework\Bootstrap\Coordinator;
- use OC\TextProcessing\Db\Task as DbTask;
- use OC\TextProcessing\Db\TaskMapper;
- use OCP\AppFramework\Db\DoesNotExistException;
- use OCP\AppFramework\Db\MultipleObjectsReturnedException;
- use OCP\BackgroundJob\IJobList;
- use OCP\Common\Exception\NotFoundException;
- use OCP\DB\Exception;
- use OCP\IConfig;
- use OCP\IServerContainer;
- use OCP\PreConditionNotMetException;
- use OCP\TextProcessing\Exception\TaskFailureException;
- use OCP\TextProcessing\IManager;
- use OCP\TextProcessing\IProvider;
- use OCP\TextProcessing\IProviderWithExpectedRuntime;
- use OCP\TextProcessing\Task;
- use OCP\TextProcessing\Task as OCPTask;
- use Psr\Log\LoggerInterface;
- use RuntimeException;
- use Throwable;
- class Manager implements IManager {
-
- private ?array $providers = null;
- public function __construct(
- private IServerContainer $serverContainer,
- private Coordinator $coordinator,
- private LoggerInterface $logger,
- private IJobList $jobList,
- private TaskMapper $taskMapper,
- private IConfig $config,
- ) {
- }
- public function getProviders(): array {
- $context = $this->coordinator->getRegistrationContext();
- if ($context === null) {
- return [];
- }
- if ($this->providers !== null) {
- return $this->providers;
- }
- $this->providers = [];
- foreach ($context->getTextProcessingProviders() as $providerServiceRegistration) {
- $class = $providerServiceRegistration->getService();
- try {
- $this->providers[$class] = $this->serverContainer->get($class);
- } catch (Throwable $e) {
- $this->logger->error('Failed to load Text processing provider ' . $class, [
- 'exception' => $e,
- ]);
- }
- }
- return $this->providers;
- }
- public function hasProviders(): bool {
- $context = $this->coordinator->getRegistrationContext();
- if ($context === null) {
- return false;
- }
- return count($context->getTextProcessingProviders()) > 0;
- }
-
- public function getAvailableTaskTypes(): array {
- $tasks = [];
- foreach ($this->getProviders() as $provider) {
- $tasks[$provider->getTaskType()] = true;
- }
- return array_keys($tasks);
- }
- public function canHandleTask(OCPTask $task): bool {
- return in_array($task->getType(), $this->getAvailableTaskTypes());
- }
-
- public function runTask(OCPTask $task): string {
- if (!$this->canHandleTask($task)) {
- throw new PreConditionNotMetException('No text processing provider is installed that can handle this task');
- }
- $providers = $this->getPreferredProviders($task);
- foreach ($providers as $provider) {
- try {
- $task->setStatus(OCPTask::STATUS_RUNNING);
- if ($provider instanceof IProviderWithExpectedRuntime) {
- $completionExpectedAt = new \DateTime('now');
- $completionExpectedAt->add(new \DateInterval('PT'.$provider->getExpectedRuntime().'S'));
- $task->setCompletionExpectedAt($completionExpectedAt);
- }
- if ($task->getId() === null) {
- $taskEntity = $this->taskMapper->insert(DbTask::fromPublicTask($task));
- $task->setId($taskEntity->getId());
- } else {
- $this->taskMapper->update(DbTask::fromPublicTask($task));
- }
- $output = $task->visitProvider($provider);
- $task->setOutput($output);
- $task->setStatus(OCPTask::STATUS_SUCCESSFUL);
- $this->taskMapper->update(DbTask::fromPublicTask($task));
- return $output;
- } catch (\Throwable $e) {
- $this->logger->error('LanguageModel call using provider ' . $provider->getName() . ' failed', ['exception' => $e]);
- $task->setStatus(OCPTask::STATUS_FAILED);
- $this->taskMapper->update(DbTask::fromPublicTask($task));
- throw new TaskFailureException('LanguageModel call using provider ' . $provider->getName() . ' failed: ' . $e->getMessage(), 0, $e);
- }
- }
- $task->setStatus(OCPTask::STATUS_FAILED);
- $this->taskMapper->update(DbTask::fromPublicTask($task));
- throw new TaskFailureException('Could not run task');
- }
-
- public function scheduleTask(OCPTask $task): void {
- if (!$this->canHandleTask($task)) {
- throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task');
- }
- $task->setStatus(OCPTask::STATUS_SCHEDULED);
- $providers = $this->getPreferredProviders($task);
- if (count($providers) === 0) {
- throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task');
- }
- [$provider,] = $providers;
- if ($provider instanceof IProviderWithExpectedRuntime) {
- $completionExpectedAt = new \DateTime('now');
- $completionExpectedAt->add(new \DateInterval('PT'.$provider->getExpectedRuntime().'S'));
- $task->setCompletionExpectedAt($completionExpectedAt);
- }
- $taskEntity = DbTask::fromPublicTask($task);
- $this->taskMapper->insert($taskEntity);
- $task->setId($taskEntity->getId());
- $this->jobList->add(TaskBackgroundJob::class, [
- 'taskId' => $task->getId()
- ]);
- }
-
- public function runOrScheduleTask(OCPTask $task): bool {
- if (!$this->canHandleTask($task)) {
- throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task');
- }
- [$provider,] = $this->getPreferredProviders($task);
- $maxExecutionTime = (int) ini_get('max_execution_time');
-
-
- if (!$provider instanceof IProviderWithExpectedRuntime || $provider->getExpectedRuntime() > $maxExecutionTime * 0.8) {
- $this->scheduleTask($task);
- return false;
- }
- $this->runTask($task);
- return true;
- }
-
- public function deleteTask(Task $task): void {
- $taskEntity = DbTask::fromPublicTask($task);
- $this->taskMapper->delete($taskEntity);
- $this->jobList->remove(TaskBackgroundJob::class, [
- 'taskId' => $task->getId()
- ]);
- }
-
- public function getTask(int $id): OCPTask {
- try {
- $taskEntity = $this->taskMapper->find($id);
- return $taskEntity->toPublicTask();
- } catch (DoesNotExistException $e) {
- throw new NotFoundException('Could not find task with the provided id');
- } catch (MultipleObjectsReturnedException $e) {
- throw new RuntimeException('Could not uniquely identify task with given id', 0, $e);
- } catch (Exception $e) {
- throw new RuntimeException('Failure while trying to find task by id: ' . $e->getMessage(), 0, $e);
- }
- }
-
- public function getUserTask(int $id, ?string $userId): OCPTask {
- try {
- $taskEntity = $this->taskMapper->findByIdAndUser($id, $userId);
- return $taskEntity->toPublicTask();
- } catch (DoesNotExistException $e) {
- throw new NotFoundException('Could not find task with the provided id and user id');
- } catch (MultipleObjectsReturnedException $e) {
- throw new RuntimeException('Could not uniquely identify task with given id and user id', 0, $e);
- } catch (Exception $e) {
- throw new RuntimeException('Failure while trying to find task by id and user id: ' . $e->getMessage(), 0, $e);
- }
- }
-
- public function getUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array {
- try {
- $taskEntities = $this->taskMapper->findUserTasksByApp($userId, $appId, $identifier);
- return array_map(static function (DbTask $taskEntity) {
- return $taskEntity->toPublicTask();
- }, $taskEntities);
- } catch (Exception $e) {
- throw new RuntimeException('Failure while trying to find tasks by appId and identifier: ' . $e->getMessage(), 0, $e);
- }
- }
-
- public function getPreferredProviders(OCPTask $task): array {
- $providers = $this->getProviders();
- $json = $this->config->getAppValue('core', 'ai.textprocessing_provider_preferences', '');
- if ($json !== '') {
- $preferences = json_decode($json, true);
- if (isset($preferences[$task->getType()])) {
-
- $provider = current(array_values(array_filter($providers, fn ($provider) => $provider::class === $preferences[$task->getType()])));
- if ($provider !== false) {
- $providers = array_filter($providers, fn ($p) => $p !== $provider);
- array_unshift($providers, $provider);
- }
- }
- }
- $providers = array_values(array_filter($providers, fn (IProvider $provider) => $task->canUseProvider($provider)));
- return $providers;
- }
- }
|