Skip to content

Commit

Permalink
fix: separate ProviderConfigService
Browse files Browse the repository at this point in the history
Signed-off-by: Anupam Kumar <[email protected]>
  • Loading branch information
kyteinsky committed Mar 22, 2024
1 parent 5d0e6a2 commit 7fa4308
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 62 deletions.
6 changes: 3 additions & 3 deletions lib/BackgroundJobs/IndexerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down
10 changes: 5 additions & 5 deletions lib/BackgroundJobs/InitialContentImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -57,15 +57,15 @@ 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']
) {
return;
}

$providerObj->triggerInitialImport();
$this->providerService->updateProvider($providerObj->getAppId(), $providerObj->getId(), $argument, true);
$this->providerConfig->updateProvider($providerObj->getAppId(), $providerObj->getId(), $argument, true);
}
}
6 changes: 3 additions & 3 deletions lib/BackgroundJobs/SubmitContentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions lib/Controller/ProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -43,7 +44,7 @@ public function __construct(
*/
#[NoAdminRequired]
public function getDefaultProviderKey(): DataResponse {
$providerKey = $this->providerService->getDefaultProviderKey();
$providerKey = ProviderConfigService::getDefaultProviderKey();
return new DataResponse($providerKey);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Listener/AppDisableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +25,7 @@
*/
class AppDisableListener implements IEventListener {
public function __construct(
private ProviderService $providerService,
private ProviderConfigService $providerConfig,
private LangRopeService $service,
private LoggerInterface $logger,
) {
Expand All @@ -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);

Expand All @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Listener/FileListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
}
Expand Down Expand Up @@ -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]);
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/Public/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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))
]);
}
}
Expand All @@ -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));
}
}
}
2 changes: 1 addition & 1 deletion lib/Service/LangRopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/Service/ProviderConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,23 @@
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class ProviderService extends ProviderConfigService {
class ProviderMetadataService {
public function __construct(
private LoggerInterface $logger,
private IL10N $l10n,
private IAppManager $appManager,
private IUserManager $userManager,
private ProviderConfigService $providerService,
private ProviderConfigService $providerConfig,
private IURLGenerator $urlGenerator,
private ?string $userId,
) {
}

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');
}

/**
* @return list<array{ id: string, label: string, icon: string }>
*/
public function getEnrichedProviders(): array {
$providers = $this->providerService->getProviders();
$providers = $this->providerConfig->getProviders();
$sanitizedProviders = [];

foreach ($providers as $providerKey => $metadata) {
Expand Down
5 changes: 3 additions & 2 deletions lib/Service/ScanService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions lib/TextProcessing/ContextChatProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 7fa4308

Please sign in to comment.