diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-07-14 15:59:50 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2023-08-09 10:05:05 +0200 |
commit | cf2c42ae36a3c7280887bd3f15329739f9a6d221 (patch) | |
tree | 3e682483cc8177afd7b9e682fcaa103791f78d2a /lib/private | |
parent | 696a45ddf1d460de7ffa6f252912375efd7e190e (diff) | |
download | nextcloud-server-cf2c42ae36a3c7280887bd3f15329739f9a6d221.tar.gz nextcloud-server-cf2c42ae36a3c7280887bd3f15329739f9a6d221.zip |
Massive refactoring: Turn LanguageModel OCP API into TextProcessing API
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
(cherry picked from commit ffe27ce14ca74b509c8721c9fba7c759498fa471)
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/AppFramework/Bootstrap/RegistrationContext.php | 20 | ||||
-rw-r--r-- | lib/private/Repair/AddRemoveOldTasksBackgroundJob.php | 2 | ||||
-rw-r--r-- | lib/private/Server.php | 4 | ||||
-rw-r--r-- | lib/private/Setup.php | 2 | ||||
-rw-r--r-- | lib/private/TextProcessing/Db/Task.php (renamed from lib/private/LanguageModel/Db/Task.php) | 11 | ||||
-rw-r--r-- | lib/private/TextProcessing/Db/TaskMapper.php (renamed from lib/private/LanguageModel/Db/TaskMapper.php) | 2 | ||||
-rw-r--r-- | lib/private/TextProcessing/Manager.php (renamed from lib/private/LanguageModel/LanguageModelManager.php) | 92 | ||||
-rw-r--r-- | lib/private/TextProcessing/RemoveOldTasksBackgroundJob.php (renamed from lib/private/LanguageModel/RemoveOldTasksBackgroundJob.php) | 4 | ||||
-rw-r--r-- | lib/private/TextProcessing/TaskBackgroundJob.php (renamed from lib/private/LanguageModel/TaskBackgroundJob.php) | 16 |
9 files changed, 61 insertions, 92 deletions
diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index 67e8b390c15..5aea2a7a744 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -33,7 +33,7 @@ use Closure; use OCP\Calendar\Resource\IBackend as IResourceBackend; use OCP\Calendar\Room\IBackend as IRoomBackend; use OCP\Collaboration\Reference\IReferenceProvider; -use OCP\LanguageModel\ILanguageModelProvider; +use OCP\TextProcessing\IProvider as ITextProcessingProvider; use OCP\SpeechToText\ISpeechToTextProvider; use OCP\Talk\ITalkBackend; use OCP\Translation\ITranslationProvider; @@ -116,8 +116,8 @@ class RegistrationContext { /** @var ServiceRegistration<ISpeechToTextProvider>[] */ private $speechToTextProviders = []; - /** @var ServiceRegistration<ILanguageModelProvider>[] */ - private $languageModelProviders = []; + /** @var ServiceRegistration<ITextProcessingProvider>[] */ + private $textProcessingProviders = []; /** @var ServiceRegistration<ICustomTemplateProvider>[] */ private $templateProviders = []; @@ -266,8 +266,8 @@ class RegistrationContext { $providerClass ); } - public function registerLanguageModelProvider(string $providerClass): void { - $this->context->registerLanguageModelProvider( + public function registerTextProcessingProvider(string $providerClass): void { + $this->context->registerTextProcessingProvider( $this->appId, $providerClass ); @@ -439,8 +439,8 @@ class RegistrationContext { $this->speechToTextProviders[] = new ServiceRegistration($appId, $class); } - public function registerLanguageModelProvider(string $appId, string $class): void { - $this->languageModelProviders[] = new ServiceRegistration($appId, $class); + public function registerTextProcessingProvider(string $appId, string $class): void { + $this->textProcessingProviders[] = new ServiceRegistration($appId, $class); } public function registerTemplateProvider(string $appId, string $class): void { @@ -722,10 +722,10 @@ class RegistrationContext { } /** - * @return ServiceRegistration<ILanguageModelProvider>[] + * @return ServiceRegistration<ITextProcessingProvider>[] */ - public function getLanguageModelProviders(): array { - return $this->languageModelProviders; + public function getTextProcessingProviders(): array { + return $this->textProcessingProviders; } /** diff --git a/lib/private/Repair/AddRemoveOldTasksBackgroundJob.php b/lib/private/Repair/AddRemoveOldTasksBackgroundJob.php index 713192b06f9..94ae39f2183 100644 --- a/lib/private/Repair/AddRemoveOldTasksBackgroundJob.php +++ b/lib/private/Repair/AddRemoveOldTasksBackgroundJob.php @@ -25,7 +25,7 @@ declare(strict_types=1); */ namespace OC\Repair; -use OC\LanguageModel\RemoveOldTasksBackgroundJob; +use OC\TextProcessing\RemoveOldTasksBackgroundJob; use OCP\BackgroundJob\IJobList; use OCP\Migration\IOutput; use OCP\Migration\IRepairStep; diff --git a/lib/private/Server.php b/lib/private/Server.php index d1f18a1235f..516dc0b6e8b 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -110,7 +110,6 @@ use OC\IntegrityCheck\Checker; use OC\IntegrityCheck\Helpers\AppLocator; use OC\IntegrityCheck\Helpers\EnvironmentHelper; use OC\IntegrityCheck\Helpers\FileAccessHelper; -use OC\LanguageModel\LanguageModelManager; use OC\LDAP\NullLDAPProviderFactory; use OC\KnownUser\KnownUserService; use OC\Lock\DBLockingProvider; @@ -229,7 +228,6 @@ use OCP\IURLGenerator; use OCP\IUserManager; use OCP\IUserSession; use OCP\L10N\IFactory; -use OCP\LanguageModel\ILanguageModelManager; use OCP\LDAP\ILDAPProvider; use OCP\LDAP\ILDAPProviderFactory; use OCP\Lock\ILockingProvider; @@ -1463,7 +1461,7 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class); - $this->registerAlias(ILanguageModelManager::class, LanguageModelManager::class); + $this->registerAlias(\OCP\TextProcessing\IManager::class, \OC\TextProcessing\Manager::class); $this->connectDispatcher(); } diff --git a/lib/private/Setup.php b/lib/private/Setup.php index a80afee6d86..3c1ac559c87 100644 --- a/lib/private/Setup.php +++ b/lib/private/Setup.php @@ -53,7 +53,7 @@ use Exception; use InvalidArgumentException; use OC\Authentication\Token\PublicKeyTokenProvider; use OC\Authentication\Token\TokenCleanupJob; -use OC\LanguageModel\RemoveOldTasksBackgroundJob; +use OC\TextProcessing\RemoveOldTasksBackgroundJob; use OC\Log\Rotate; use OC\Preview\BackgroundCleanupJob; use OCP\AppFramework\Utility\ITimeFactory; diff --git a/lib/private/LanguageModel/Db/Task.php b/lib/private/TextProcessing/Db/Task.php index 4e46f19e8a9..bc1bbdc13db 100644 --- a/lib/private/LanguageModel/Db/Task.php +++ b/lib/private/TextProcessing/Db/Task.php @@ -23,11 +23,10 @@ declare(strict_types=1); * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -namespace OC\LanguageModel\Db; +namespace OC\TextProcessing\Db; use OCP\AppFramework\Db\Entity; -use OCP\LanguageModel\AbstractLanguageModelTask; -use OCP\LanguageModel\ILanguageModelTask; +use OCP\TextProcessing\Task as OCPTask; /** * @method setType(string $type) @@ -87,7 +86,7 @@ class Task extends Entity { }, self::$fields)); } - public static function fromLanguageModelTask(ILanguageModelTask $task): Task { + public static function fromPublicTask(OCPTask $task): Task { /** @var Task $task */ $task = Task::fromParams([ 'id' => $task->getId(), @@ -103,8 +102,8 @@ class Task extends Entity { return $task; } - public function toLanguageModelTask(): ILanguageModelTask { - $task = AbstractLanguageModelTask::factory($this->getType(), $this->getInput(), $this->getuserId(), $this->getAppId(), $this->getIdentifier()); + public function toPublicTask(): OCPTask { + $task = OCPTask::factory($this->getType(), $this->getInput(), $this->getuserId(), $this->getAppId(), $this->getIdentifier()); $task->setId($this->getId()); $task->setStatus($this->getStatus()); $task->setOutput($this->getOutput()); diff --git a/lib/private/LanguageModel/Db/TaskMapper.php b/lib/private/TextProcessing/Db/TaskMapper.php index 9b93ea1990f..508f3fdf3b8 100644 --- a/lib/private/LanguageModel/Db/TaskMapper.php +++ b/lib/private/TextProcessing/Db/TaskMapper.php @@ -23,7 +23,7 @@ declare(strict_types=1); * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -namespace OC\LanguageModel\Db; +namespace OC\TextProcessing\Db; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; diff --git a/lib/private/LanguageModel/LanguageModelManager.php b/lib/private/TextProcessing/Manager.php index 970d968c883..34e4b2bb4cc 100644 --- a/lib/private/LanguageModel/LanguageModelManager.php +++ b/lib/private/TextProcessing/Manager.php @@ -23,34 +23,27 @@ declare(strict_types=1); * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -namespace OC\LanguageModel; +namespace OC\TextProcessing; use OC\AppFramework\Bootstrap\Coordinator; -use OC\LanguageModel\Db\Task; -use OC\LanguageModel\Db\TaskMapper; +use OC\TextProcessing\Db\Task as DbTask; +use \OCP\TextProcessing\Task as OCPTask; +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\IServerContainer; -use OCP\LanguageModel\FreePromptTask; -use OCP\LanguageModel\HeadlineTask; -use OCP\LanguageModel\IHeadlineProvider; -use OCP\LanguageModel\ILanguageModelManager; -use OCP\LanguageModel\ILanguageModelProvider; -use OCP\LanguageModel\ILanguageModelTask; -use OCP\LanguageModel\ISummaryProvider; -use OCP\LanguageModel\ITopicsProvider; -use OCP\LanguageModel\SummaryTask; -use OCP\LanguageModel\TopicsTask; +use OCP\TextProcessing\IManager; +use OCP\TextProcessing\IProvider; use OCP\PreConditionNotMetException; use Psr\Log\LoggerInterface; use RuntimeException; use Throwable; -class LanguageModelManager implements ILanguageModelManager { - /** @var ?ILanguageModelProvider[] */ +class Manager implements IManager { + /** @var ?IProvider[] */ private ?array $providers = null; public function __construct( @@ -74,12 +67,12 @@ class LanguageModelManager implements ILanguageModelManager { $this->providers = []; - foreach ($context->getLanguageModelProviders() as $providerServiceRegistration) { + 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 LanguageModel provider ' . $class, [ + $this->logger->error('Failed to load Text processing provider ' . $class, [ 'exception' => $e, ]); } @@ -93,78 +86,57 @@ class LanguageModelManager implements ILanguageModelManager { if ($context === null) { return false; } - return count($context->getLanguageModelProviders()) > 0; + return count($context->getTextProcessingProviders()) > 0; } /** * @inheritDoc */ - public function getAvailableTaskClasses(): array { + public function getAvailableTaskTypes(): array { $tasks = []; foreach ($this->getProviders() as $provider) { - $tasks[FreePromptTask::class] = true; - if ($provider instanceof ISummaryProvider) { - $tasks[SummaryTask::class] = true; - } - if ($provider instanceof IHeadlineProvider) { - $tasks[HeadlineTask::class] = true; - } - if ($provider instanceof ITopicsProvider) { - $tasks[TopicsTask::class] = true; - } + $tasks[$provider->getTaskType()] = true; } return array_keys($tasks); } - /** - * @inheritDoc - */ - public function getAvailableTaskTypes(): array { - return array_map(fn ($taskClass) => $taskClass::TYPE, $this->getAvailableTaskClasses()); - } - - public function canHandleTask(ILanguageModelTask $task): bool { - foreach ($this->getAvailableTaskClasses() as $class) { - if ($task instanceof $class) { - return true; - } - } - return false; + public function canHandleTask(OCPTask $task): bool { + return in_array($task->getType(), $this->getAvailableTaskTypes()); } /** * @inheritDoc */ - public function runTask(ILanguageModelTask $task): string { + public function runTask(OCPTask $task): string { if (!$this->canHandleTask($task)) { - throw new PreConditionNotMetException('No LanguageModel provider is installed that can handle this task'); + throw new PreConditionNotMetException('No text processing provider is installed that can handle this task'); } foreach ($this->getProviders() as $provider) { if (!$task->canUseProvider($provider)) { continue; } try { - $task->setStatus(ILanguageModelTask::STATUS_RUNNING); + $task->setStatus(OCPTask::STATUS_RUNNING); if ($task->getId() === null) { - $taskEntity = $this->taskMapper->insert(Task::fromLanguageModelTask($task)); + $taskEntity = $this->taskMapper->insert(DbTask::fromPublicTask($task)); $task->setId($taskEntity->getId()); } else { - $this->taskMapper->update(Task::fromLanguageModelTask($task)); + $this->taskMapper->update(DbTask::fromPublicTask($task)); } $output = $task->visitProvider($provider); $task->setOutput($output); - $task->setStatus(ILanguageModelTask::STATUS_SUCCESSFUL); - $this->taskMapper->update(Task::fromLanguageModelTask($task)); + $task->setStatus(OCPTask::STATUS_SUCCESSFUL); + $this->taskMapper->update(DbTask::fromPublicTask($task)); return $output; } catch (\RuntimeException $e) { $this->logger->info('LanguageModel call using provider ' . $provider->getName() . ' failed', ['exception' => $e]); - $task->setStatus(ILanguageModelTask::STATUS_FAILED); - $this->taskMapper->update(Task::fromLanguageModelTask($task)); + $task->setStatus(OCPTask::STATUS_FAILED); + $this->taskMapper->update(DbTask::fromPublicTask($task)); throw $e; } catch (\Throwable $e) { $this->logger->info('LanguageModel call using provider ' . $provider->getName() . ' failed', ['exception' => $e]); - $task->setStatus(ILanguageModelTask::STATUS_FAILED); - $this->taskMapper->update(Task::fromLanguageModelTask($task)); + $task->setStatus(OCPTask::STATUS_FAILED); + $this->taskMapper->update(DbTask::fromPublicTask($task)); throw new RuntimeException('LanguageModel call using provider ' . $provider->getName() . ' failed: ' . $e->getMessage(), 0, $e); } } @@ -176,12 +148,12 @@ class LanguageModelManager implements ILanguageModelManager { * @inheritDoc * @throws Exception */ - public function scheduleTask(ILanguageModelTask $task): void { + 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(ILanguageModelTask::STATUS_SCHEDULED); - $taskEntity = Task::fromLanguageModelTask($task); + $task->setStatus(OCPTask::STATUS_SCHEDULED); + $taskEntity = DbTask::fromPublicTask($task); $this->taskMapper->insert($taskEntity); $task->setId($taskEntity->getId()); $this->jobList->add(TaskBackgroundJob::class, [ @@ -191,14 +163,14 @@ class LanguageModelManager implements ILanguageModelManager { /** * @param int $id The id of the task - * @return ILanguageModelTask + * @return OCPTask * @throws RuntimeException If the query failed * @throws NotFoundException If the task could not be found */ - public function getTask(int $id): ILanguageModelTask { + public function getTask(int $id): OCPTask { try { $taskEntity = $this->taskMapper->find($id); - return $taskEntity->toLanguageModelTask(); + return $taskEntity->toPublicTask(); } catch (DoesNotExistException $e) { throw new NotFoundException('Could not find task with the provided id'); } catch (MultipleObjectsReturnedException $e) { diff --git a/lib/private/LanguageModel/RemoveOldTasksBackgroundJob.php b/lib/private/TextProcessing/RemoveOldTasksBackgroundJob.php index fa3a716a2c6..89d329acfbb 100644 --- a/lib/private/LanguageModel/RemoveOldTasksBackgroundJob.php +++ b/lib/private/TextProcessing/RemoveOldTasksBackgroundJob.php @@ -24,9 +24,9 @@ declare(strict_types=1); */ -namespace OC\LanguageModel; +namespace OC\TextProcessing; -use OC\LanguageModel\Db\TaskMapper; +use OC\TextProcessing\Db\TaskMapper; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; use OCP\DB\Exception; diff --git a/lib/private/LanguageModel/TaskBackgroundJob.php b/lib/private/TextProcessing/TaskBackgroundJob.php index 5ac37baf332..4c24b3e531f 100644 --- a/lib/private/LanguageModel/TaskBackgroundJob.php +++ b/lib/private/TextProcessing/TaskBackgroundJob.php @@ -24,19 +24,19 @@ declare(strict_types=1); */ -namespace OC\LanguageModel; +namespace OC\TextProcessing; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\QueuedJob; use OCP\EventDispatcher\IEventDispatcher; -use OCP\LanguageModel\Events\TaskFailedEvent; -use OCP\LanguageModel\Events\TaskSuccessfulEvent; -use OCP\LanguageModel\ILanguageModelManager; +use OCP\TextProcessing\Events\TaskFailedEvent; +use OCP\TextProcessing\Events\TaskSuccessfulEvent; +use OCP\TextProcessing\IManager; class TaskBackgroundJob extends QueuedJob { public function __construct( - ITimeFactory $timeFactory, - private ILanguageModelManager $languageModelManager, + ITimeFactory $timeFactory, + private IManager $textProcessingManager, private IEventDispatcher $eventDispatcher, ) { parent::__construct($timeFactory); @@ -51,9 +51,9 @@ class TaskBackgroundJob extends QueuedJob { */ protected function run($argument) { $taskId = $argument['taskId']; - $task = $this->languageModelManager->getTask($taskId); + $task = $this->textProcessingManager->getTask($taskId); try { - $this->languageModelManager->runTask($task); + $this->textProcessingManager->runTask($task); $event = new TaskSuccessfulEvent($task); } catch (\Throwable $e) { $event = new TaskFailedEvent($task, $e->getMessage()); |