diff --git a/lib/BackgroundJobs/IndexerJob.php b/lib/BackgroundJobs/IndexerJob.php index 2b15e0a..5858dd2 100644 --- a/lib/BackgroundJobs/IndexerJob.php +++ b/lib/BackgroundJobs/IndexerJob.php @@ -9,7 +9,7 @@ use OCA\ContextChat\Db\QueueFile; use OCA\ContextChat\Service\LangRopeService; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; use OCA\ContextChat\Service\QueueService; use OCA\ContextChat\Service\StorageService; use OCA\ContextChat\Type\Source; @@ -128,12 +128,12 @@ protected function index(array $files): void { try { $source = new Source( $userId, - ProviderService::getSourceId($file->getId()), + ProviderConfigService::getSourceId($file->getId()), $file->getPath(), $fileHandle, $file->getMtime(), $file->getMimeType(), - ProviderService::getDefaultProviderKey(), + ProviderConfigService::getDefaultProviderKey(), ); } catch (InvalidPathException|NotFoundException $e) { $this->logger->error('Could not find file ' . $file->getPath(), ['exception' => $e]); diff --git a/lib/BackgroundJobs/InitialContentImportJob.php b/lib/BackgroundJobs/InitialContentImportJob.php index 1d0ada6..b5e955d 100644 --- a/lib/BackgroundJobs/InitialContentImportJob.php +++ b/lib/BackgroundJobs/InitialContentImportJob.php @@ -14,7 +14,7 @@ namespace OCA\ContextChat\BackgroundJobs; use OCA\ContextChat\Public\IContentProvider; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\QueuedJob; @@ -27,7 +27,7 @@ class InitialContentImportJob extends QueuedJob { public function __construct( private IAppManager $appManager, - private ProviderService $providerService, + private ProviderConfigService $providerConfig, private LoggerInterface $logger, private IUserManager $userMan, ITimeFactory $timeFactory, @@ -57,8 +57,8 @@ protected function run($argument): void { return; } - $registeredProviders = $this->providerService->getProviders(); - $identifier = ProviderService::getConfigKey($providerObj->getAppId(), $providerObj->getId()); + $registeredProviders = $this->providerConfig->getProviders(); + $identifier = ProviderConfigService::getConfigKey($providerObj->getAppId(), $providerObj->getId()); if (!isset($registeredProviders[$identifier]) || $registeredProviders[$identifier]['isInitiated'] ) { @@ -66,6 +66,6 @@ protected function run($argument): void { } $providerObj->triggerInitialImport(); - $this->providerService->updateProvider($providerObj->getAppId(), $providerObj->getId(), $argument, true); + $this->providerConfig->updateProvider($providerObj->getAppId(), $providerObj->getId(), $argument, true); } } diff --git a/lib/BackgroundJobs/SubmitContentJob.php b/lib/BackgroundJobs/SubmitContentJob.php index cf2533e..0fef02b 100644 --- a/lib/BackgroundJobs/SubmitContentJob.php +++ b/lib/BackgroundJobs/SubmitContentJob.php @@ -16,7 +16,7 @@ use OCA\ContextChat\Db\QueueContentItem; use OCA\ContextChat\Db\QueueContentItemMapper; use OCA\ContextChat\Service\LangRopeService; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; use OCA\ContextChat\Type\Source; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; @@ -58,8 +58,8 @@ protected function run($argument): void { foreach ($bucketed as $userId => $entities) { $sources = array_map(function (QueueContentItem $item) use ($userId) { - $providerKey = ProviderService::getConfigKey($item->getAppId(), $item->getProviderId()); - $sourceId = ProviderService::getSourceId($item->getItemId(), $providerKey); + $providerKey = ProviderConfigService::getConfigKey($item->getAppId(), $item->getProviderId()); + $sourceId = ProviderConfigService::getSourceId($item->getItemId(), $providerKey); return new Source( $userId, $sourceId, diff --git a/lib/Controller/ProviderController.php b/lib/Controller/ProviderController.php index 2600a9a..3ed6c27 100644 --- a/lib/Controller/ProviderController.php +++ b/lib/Controller/ProviderController.php @@ -22,7 +22,8 @@ namespace OCA\ContextChat\Controller; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; +use OCA\ContextChat\Service\ProviderMetadataService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\DataResponse; @@ -33,7 +34,7 @@ class ProviderController extends Controller { public function __construct( string $appName, IRequest $request, - private ProviderService $providerService, + private ProviderMetadataService $providerService, ) { parent::__construct($appName, $request); } @@ -43,7 +44,7 @@ public function __construct( */ #[NoAdminRequired] public function getDefaultProviderKey(): DataResponse { - $providerKey = $this->providerService->getDefaultProviderKey(); + $providerKey = ProviderConfigService::getDefaultProviderKey(); return new DataResponse($providerKey); } diff --git a/lib/Listener/AppDisableListener.php b/lib/Listener/AppDisableListener.php index 21184d8..e5cdecf 100644 --- a/lib/Listener/AppDisableListener.php +++ b/lib/Listener/AppDisableListener.php @@ -14,7 +14,7 @@ namespace OCA\ContextChat\Listener; use OCA\ContextChat\Service\LangRopeService; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; use OCP\App\Events\AppDisableEvent; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; @@ -25,7 +25,7 @@ */ class AppDisableListener implements IEventListener { public function __construct( - private ProviderService $providerService, + private ProviderConfigService $providerConfig, private LangRopeService $service, private LoggerInterface $logger, ) { @@ -36,7 +36,7 @@ public function handle(Event $event): void { return; } - foreach ($this->providerService->getProviders() as $key => $values) { + foreach ($this->providerConfig->getProviders() as $key => $values) { /** @var string[] */ $identifierValues = explode('__', $key, 2); @@ -51,7 +51,7 @@ public function handle(Event $event): void { continue; } - $this->providerService->removeProvider($appId, $providerId); + $this->providerConfig->removeProvider($appId, $providerId); $this->service->deleteSourcesByProviderForAllUsers($providerId); } } diff --git a/lib/Listener/FileListener.php b/lib/Listener/FileListener.php index 21979ff..4d60ee4 100644 --- a/lib/Listener/FileListener.php +++ b/lib/Listener/FileListener.php @@ -10,7 +10,7 @@ use OCA\ContextChat\AppInfo\Application; use OCA\ContextChat\Db\QueueFile; use OCA\ContextChat\Service\LangRopeService; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; use OCA\ContextChat\Service\QueueService; use OCA\ContextChat\Service\StorageService; use OCP\DB\Exception; @@ -121,7 +121,7 @@ public function handle(Event $event): void { if (!$node instanceof File) { continue; } - $fileRefs[] = ProviderService::getSourceId($node->getId()); + $fileRefs[] = ProviderConfigService::getSourceId($node->getId()); } $this->langRopeService->deleteSources($userId, $fileRefs); @@ -130,7 +130,7 @@ public function handle(Event $event): void { return; } - $fileRef = ProviderService::getSourceId($node->getId()); + $fileRef = ProviderConfigService::getSourceId($node->getId()); foreach ($userIds as $userId) { $this->langRopeService->deleteSources($userId, [$fileRef]); } @@ -195,7 +195,7 @@ public function postDelete(Node $node, bool $recurse = true): void { } foreach ($this->storageService->getUsersForFileId($node->getId()) as $userId) { - $fileRef = ProviderService::getSourceId($node->getId()); + $fileRef = ProviderConfigService::getSourceId($node->getId()); $this->langRopeService->deleteSources($userId, [$fileRef]); } } diff --git a/lib/Public/ContentManager.php b/lib/Public/ContentManager.php index 1debc0e..38c458f 100644 --- a/lib/Public/ContentManager.php +++ b/lib/Public/ContentManager.php @@ -16,7 +16,7 @@ use OCA\ContextChat\Db\QueueContentItem; use OCA\ContextChat\Db\QueueContentItemMapper; use OCA\ContextChat\Service\LangRopeService; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; use OCP\BackgroundJob\IJobList; use OCP\Server; use Psr\Container\ContainerExceptionInterface; @@ -26,7 +26,7 @@ class ContentManager { public function __construct( private IJobList $jobList, - private ProviderService $providerService, + private ProviderConfigService $providerConfig, private LangRopeService $service, private QueueContentItemMapper $mapper, private LoggerInterface $logger, @@ -47,11 +47,11 @@ public function registerContentProvider(string $providerClass): void { return; } - if ($this->providerService->hasProvider($providerObj->getAppId(), $providerObj->getId())) { + if ($this->providerConfig->hasProvider($providerObj->getAppId(), $providerObj->getId())) { return; } - $this->providerService->updateProvider($providerObj->getAppId(), $providerObj->getId(), $providerClass); + $this->providerConfig->updateProvider($providerObj->getAppId(), $providerObj->getId(), $providerClass); if (!$this->jobList->has(InitialContentImportJob::class, $providerClass)) { $this->jobList->add(InitialContentImportJob::class, $providerClass); @@ -99,7 +99,7 @@ public function submitContent(string $appId, array $items): void { public function removeContentForUsers(string $appId, string $providerId, string $itemId, array $users): void { foreach ($users as $userId) { $this->service->deleteSources($userId, [ - ProviderService::getSourceId($itemId, ProviderService::getConfigKey($appId, $providerId)) + ProviderConfigService::getSourceId($itemId, ProviderConfigService::getConfigKey($appId, $providerId)) ]); } } @@ -114,7 +114,7 @@ public function removeContentForUsers(string $appId, string $providerId, string */ public function removeAllContentForUsers(string $appId, string $providerId, array $users): void { foreach ($users as $userId) { - $this->service->deleteSourcesByProvider($userId, ProviderService::getConfigKey($appId, $providerId)); + $this->service->deleteSourcesByProvider($userId, ProviderConfigService::getConfigKey($appId, $providerId)); } } } diff --git a/lib/Service/LangRopeService.php b/lib/Service/LangRopeService.php index 2d6a073..cfa10f5 100644 --- a/lib/Service/LangRopeService.php +++ b/lib/Service/LangRopeService.php @@ -231,7 +231,7 @@ public function getWithPresentableSources(string $llmResponse, string ...$source $output = str_repeat(PHP_EOL, 3) . $this->l10n->t('Sources referenced to generate the above response:') . PHP_EOL; - $emptyFilesSourceId = ProviderService::getSourceId(''); + $emptyFilesSourceId = ProviderConfigService::getSourceId(''); foreach ($sourceRefs as $source) { if (str_starts_with($source, $emptyFilesSourceId) && is_numeric($fileId = substr($source, strlen($emptyFilesSourceId)))) { // use `overwritehost` setting in config.php to overwrite the host diff --git a/lib/Service/ProviderConfigService.php b/lib/Service/ProviderConfigService.php index dc229de..10719d5 100644 --- a/lib/Service/ProviderConfigService.php +++ b/lib/Service/ProviderConfigService.php @@ -23,6 +23,14 @@ public function __construct( ) { } + public static function getSourceId(int | string $nodeId, ?string $providerId = null): string { + return ($providerId ?? self::getDefaultProviderKey()) . ': ' . $nodeId; + } + + public static function getDefaultProviderKey(): string { + return ProviderConfigService::getConfigKey('files', 'default'); + } + public static function getConfigKey(string $appId, string $providerId): string { return $appId . '__' . $providerId; } diff --git a/lib/Service/ProviderService.php b/lib/Service/ProviderService.php deleted file mode 100644 index 7237af6..0000000 --- a/lib/Service/ProviderService.php +++ /dev/null @@ -1,91 +0,0 @@ - - */ - public function getEnrichedProviders(): array { - $providers = $this->providerService->getProviders(); - $sanitizedProviders = []; - - foreach ($providers as $providerKey => $metadata) { - // providerKey ($appId__$providerId) - /** @var string[] */ - $providerValues = explode('__', $providerKey, 2); - - if (count($providerValues) !== 2) { - $this->logger->info("Invalid provider key $providerKey, skipping"); - continue; - } - - [$appId, $providerId] = $providerValues; - - $user = $this->userId === null ? null : $this->userManager->get($this->userId); - if (!$this->appManager->isEnabledForUser($appId, $user)) { - $this->logger->info("App $appId is not enabled for user {$this->userId}, skipping"); - continue; - } - - $appInfo = $this->appManager->getAppInfo($appId); - if ($appInfo === null) { - $this->logger->info("Could not get app info for $appId, skipping"); - continue; - } - - try { - $icon = $this->urlGenerator->imagePath($appId, 'app-dark.svg'); - } catch (\RuntimeException $e) { - $this->logger->info("Could not get app image for $appId"); - $icon = ''; - } - - if (!isset($appInfo['name'])) { - $this->logger->info("App $appId does not have a name, skipping"); - continue; - } - - $sanitizedProviders[] = [ - 'id' => $providerKey, - 'label' => ucfirst($providerId) . ' - ' . $appInfo['name'], - 'icon' => $icon, - ]; - } - return $sanitizedProviders; - } -} diff --git a/lib/Service/ScanService.php b/lib/Service/ScanService.php index 3b0a448..d5c7343 100644 --- a/lib/Service/ScanService.php +++ b/lib/Service/ScanService.php @@ -94,10 +94,11 @@ public function getSourceFromFile(string $userId, array $mimeTypeFilter, File $n return null; } - $providerKey = ProviderService::getDefaultProviderKey(); + $providerKey = ProviderConfigService::getDefaultProviderKey(); + $sourceId = ProviderConfigService::getSourceId($node->getId()); return new Source( $userId, - $providerKey . ': ' . $node->getId(), + $sourceId, $node->getPath(), $fileHandle, $node->getMTime(), diff --git a/lib/TextProcessing/ContextChatProvider.php b/lib/TextProcessing/ContextChatProvider.php index 515950c..7c11c73 100644 --- a/lib/TextProcessing/ContextChatProvider.php +++ b/lib/TextProcessing/ContextChatProvider.php @@ -5,7 +5,7 @@ use OCA\ContextChat\AppInfo\Application; use OCA\ContextChat\Service\LangRopeService; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; use OCA\ContextChat\Service\ScanService; use OCA\ContextChat\Type\ScopeType; use OCP\Files\File; @@ -127,12 +127,12 @@ private function indexFiles(string ...$scopeList): array { $indexedFiles = []; foreach ($scopeList as $scope) { - if (!str_contains($scope, ProviderService::getSourceId(''))) { + if (!str_contains($scope, ProviderConfigService::getSourceId(''))) { $this->logger->warning('Invalid source format, expected "sourceId: itemId"'); continue; } - $nodeId = substr($scope, strlen(ProviderService::getSourceId(''))); + $nodeId = substr($scope, strlen(ProviderConfigService::getSourceId(''))); try { $userFolder = $this->rootFolder->getUserFolder($this->userId); diff --git a/tests/integration/ContentManagerTest.php b/tests/integration/ContentManagerTest.php index 9a30668..7e456be 100644 --- a/tests/integration/ContentManagerTest.php +++ b/tests/integration/ContentManagerTest.php @@ -22,7 +22,7 @@ use OCA\ContextChat\Public\ContentManager; use OCA\ContextChat\Public\IContentProvider; use OCA\ContextChat\Service\LangRopeService; -use OCA\ContextChat\Service\ProviderService; +use OCA\ContextChat\Service\ProviderConfigService; use OCP\BackgroundJob\IJobList; use OCP\Server; use PHPUnit\Framework\MockObject\MockObject; @@ -32,8 +32,8 @@ class ContentManagerTest extends TestCase { /** @var MockObject | QueueContentItemMapper */ private QueueContentItemMapper $mapper; - /** @var MockObject | ProviderService */ - private ProviderService $providerService; + /** @var MockObject | ProviderConfigService */ + private ProviderConfigService $providerConfig; /** @var MockObject | LangRopeService */ private LangRopeService $service; @@ -48,23 +48,23 @@ public function setUp(): void { $this->jobList = Server::get(IJobList::class); $this->logger = Server::get(LoggerInterface::class); $this->mapper = $this->createMock(QueueContentItemMapper::class); - $this->providerService = $this->createMock(ProviderService::class); + $this->providerConfig = $this->createMock(ProviderConfigService::class); $this->service = $this->createMock(LangRopeService::class); - $this->providerService + $this->providerConfig ->method('getProviders') ->willReturn([ - ProviderService::getDefaultProviderKey() => [ + ProviderConfigService::getDefaultProviderKey() => [ 'isInitiated' => true, 'classString' => '', ], - ProviderService::getConfigKey(Application::APP_ID, 'test-provider') => [ + ProviderConfigService::getConfigKey(Application::APP_ID, 'test-provider') => [ 'isInitiated' => false, 'classString' => static::$providerClass, ], ]); - // $this->overwriteService(ProviderConfigService::class, $this->providerConfigService); + // $this->overwriteService(ProviderConfigService::class, $this->providerConfig); // using this app's app id to pass the check that the app is enabled for the user $providerObj = new ContentProvider(Application::APP_ID, 'test-provider', function () { @@ -78,7 +78,7 @@ public function setUp(): void { $this->contentManager = new ContentManager( $this->jobList, - $this->providerService, + $this->providerConfig, $this->service, $this->mapper, $this->logger, @@ -106,13 +106,13 @@ public function testRegisterContentProvider( string $providerId, bool $registrationSuccessful, ): void { - $this->providerService + $this->providerConfig ->expects($registrationSuccessful ? $this->once() : $this->never()) ->method('hasProvider') ->with($appId, $providerId) ->willReturn(false); - $this->providerService + $this->providerConfig ->expects($registrationSuccessful ? $this->once() : $this->never()) ->method('updateProvider') ->with($appId, $providerId, $providerClass); diff --git a/tests/integration/ProviderConfigServiceTest.php b/tests/integration/ProviderConfigServiceTest.php index 7954a11..6d8a851 100644 --- a/tests/integration/ProviderConfigServiceTest.php +++ b/tests/integration/ProviderConfigServiceTest.php @@ -23,11 +23,11 @@ class ProviderConfigServiceTest extends TestCase { /** @var MockObject | IConfig */ private IConfig $config; - private ProviderConfigService $configService; + private ProviderConfigService $providerConfig; public function setUp(): void { $this->config = $this->createMock(IConfig::class); - $this->configService = new ProviderConfigService($this->config); + $this->providerConfig = new ProviderConfigService($this->config); } public function testGetConfigKey(): void { @@ -70,7 +70,7 @@ public function testGetProviders(string $returnVal, array $providers): void { ->with(Application::APP_ID, 'providers') ->willReturn($returnVal); - $this->assertEquals($providers, $this->configService->getProviders()); + $this->assertEquals($providers, $this->providerConfig->getProviders()); } /** @@ -109,7 +109,7 @@ public function testUpdateProvider(string $returnVal, array $providers): void { ->method('setAppValue') ->with(Application::APP_ID, 'providers', $this->logicalOr($this->equalTo(''), $this->equalTo($setProvidersValue))); - $this->configService->updateProvider($appId, $providerId, $providerClass, $isInitiated); + $this->providerConfig->updateProvider($appId, $providerId, $providerClass, $isInitiated); } /** @@ -141,7 +141,7 @@ public function testRemoveProvider(string $returnVal, array $providers): void { $this->equalTo(json_encode($providers)) )); - $this->configService->removeProvider($appId, $providerId); + $this->providerConfig->removeProvider($appId, $providerId); } /** @@ -165,6 +165,6 @@ public function testHasProvider(string $returnVal, array $providers): void { default => true, }; - $this->assertEquals($expected, $this->configService->hasProvider($appId, $providerId)); + $this->assertEquals($expected, $this->providerConfig->hasProvider($appId, $providerId)); } }