Skip to content

Commit

Permalink
Merge pull request #147 from nextcloud/enh/noid/switch-to-iappconfig
Browse files Browse the repository at this point in the history
Switch to IAppConfig
  • Loading branch information
julien-nc authored Oct 9, 2024
2 parents e34a385 + 1b1850b commit 67fca47
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 28 deletions.
4 changes: 3 additions & 1 deletion lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use OCA\Assistant\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\Capabilities\IPublicCapability;
use OCP\IAppConfig;
use OCP\IConfig;

class Capabilities implements IPublicCapability {

public function __construct(
private IAppManager $appManager,
private IConfig $config,
private IAppConfig $appConfig,
private ?string $userId,
) {
}
Expand All @@ -34,7 +36,7 @@ public function getCapabilities(): array {
],
];
if ($this->userId !== null) {
$adminAssistantEnabled = $this->config->getAppValue(Application::APP_ID, 'assistant_enabled', '1') === '1';
$adminAssistantEnabled = $this->appConfig->getValueString(Application::APP_ID, 'assistant_enabled', '1') === '1';
$userAssistantEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'assistant_enabled', '1') === '1';
$assistantEnabled = $adminAssistantEnabled && $userAssistantEnabled;
$capability[Application::APP_ID]['enabled'] = $assistantEnabled;
Expand Down
12 changes: 6 additions & 6 deletions lib/Controller/ChattyLLMController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
Expand All @@ -39,7 +39,7 @@ public function __construct(
private IL10N $l10n,
private LoggerInterface $logger,
private ITaskProcessingManager $taskProcessingManager,
private IConfig $config,
private IAppConfig $appConfig,
private IUserManager $userManager,
private ?string $userId,
) {
Expand All @@ -64,7 +64,7 @@ public function newSession(int $timestamp, ?string $title = null): JSONResponse
return new JSONResponse(['error' => $this->l10n->t('User not found')], Http::STATUS_UNAUTHORIZED);
}

$userInstructions = $this->config->getAppValue(
$userInstructions = $this->appConfig->getValueString(
Application::APP_ID,
'chat_user_instructions',
Application::CHAT_USER_INSTRUCTIONS,
Expand Down Expand Up @@ -419,7 +419,7 @@ public function generateTitle(int $sessionId): JSONResponse {
}

try {
$userInstructions = $this->config->getAppValue(
$userInstructions = $this->appConfig->getValueString(
Application::APP_ID,
'chat_user_instructions_title',
Application::CHAT_USER_INSTRUCTIONS_TITLE,
Expand Down Expand Up @@ -465,7 +465,7 @@ public function checkTitleGenerationTask(int $taskId, int $sessionId): JSONRespo
if ($task->getStatus() === Task::STATUS_SUCCESSFUL) {
try {
$taskOutput = trim($task->getOutput()['output'] ?? '');
$userInstructions = $this->config->getAppValue(
$userInstructions = $this->appConfig->getValueString(
Application::APP_ID,
'chat_user_instructions_title',
Application::CHAT_USER_INSTRUCTIONS_TITLE,
Expand Down Expand Up @@ -510,7 +510,7 @@ private function getStichedMessages(int $sessionId): string {
$stichedPrompt = $firstMessage->getContent() . PHP_EOL;
}

$lastNMessages = intval($this->config->getAppValue(Application::APP_ID, 'chat_last_n_messages', '10'));
$lastNMessages = intval($this->appConfig->getValueString(Application::APP_ID, 'chat_last_n_messages', '10'));
$messages = $this->messageMapper->getMessages($sessionId, 0, $lastNMessages);

if ($messages[0]->getRole() === 'system') {
Expand Down
4 changes: 3 additions & 1 deletion lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\DataResponse;
use OCP\IAppConfig;
use OCP\IConfig;

use OCP\IRequest;
Expand All @@ -19,6 +20,7 @@ public function __construct(
string $appName,
IRequest $request,
private IConfig $config,
private IAppConfig $appConfig,
private ?string $userId
) {
parent::__construct($appName, $request);
Expand Down Expand Up @@ -57,7 +59,7 @@ public function getConfigValue(string $key): DataResponse {
*/
public function setAdminConfig(array $values): DataResponse {
foreach ($values as $key => $value) {
$this->config->setAppValue(Application::APP_ID, $key, $value);
$this->appConfig->setValueString(Application::APP_ID, $key, $value);
}
return new DataResponse(1);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
Expand All @@ -25,6 +26,7 @@ class BeforeTemplateRenderedListener implements IEventListener {
public function __construct(
private IUserSession $userSession,
private IConfig $config,
private IAppConfig $appConfig,
private IInitialState $initialStateService,
private IEventDispatcher $eventDispatcher,
private ?string $userId,
Expand All @@ -47,7 +49,7 @@ public function handle(Event $event): void {

$this->eventDispatcher->dispatchTyped(new RenderReferenceEvent());

$adminAssistantEnabled = $this->config->getAppValue(Application::APP_ID, 'assistant_enabled', '1') === '1';
$adminAssistantEnabled = $this->appConfig->getValueString(Application::APP_ID, 'assistant_enabled', '1') === '1';
$userAssistantEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'assistant_enabled', '1') === '1';
$assistantEnabled = $adminAssistantEnabled && $userAssistantEnabled;
$this->initialStateService->provideInitialState('assistant-enabled', $assistantEnabled);
Expand Down
6 changes: 4 additions & 2 deletions lib/Listener/FreePrompt/FreePromptReferenceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\TaskProcessing\IManager as ITaskProcessingManager;
use OCP\TaskProcessing\TaskTypes\TextToText;
Expand All @@ -20,6 +21,7 @@
class FreePromptReferenceListener implements IEventListener {
public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private ?string $userId,
private ITaskProcessingManager $taskProcessingManager,
) {
Expand All @@ -32,8 +34,8 @@ public function handle(Event $event): void {
return;
}

if ($this->config->getAppValue(Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1' &&
$this->config->getUserValue($this->userId, Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1') {
if ($this->appConfig->getValueString(Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1'
&& $this->config->getUserValue($this->userId, Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1') {

// Double check that at least one provider is registered
$availableTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
Expand Down
6 changes: 4 additions & 2 deletions lib/Listener/SpeechToText/SpeechToTextReferenceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\TaskProcessing\IManager as ITaskProcessingManager;
use OCP\TaskProcessing\TaskTypes\AudioToText;
Expand All @@ -38,6 +39,7 @@
class SpeechToTextReferenceListener implements IEventListener {
public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private ?string $userId,
private ITaskProcessingManager $taskProcessingManager,
) {
Expand All @@ -47,8 +49,8 @@ public function handle(Event $event): void {
if (!$event instanceof RenderReferenceEvent) {
return;
}
if ($this->config->getAppValue(Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1' &&
($this->userId === null || $this->config->getUserValue($this->userId, Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1')) {
if ($this->appConfig->getValueString(Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1'
&& ($this->userId === null || $this->config->getUserValue($this->userId, Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1')) {

// Double check that at least one provider is registered
$availableTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
Expand Down
6 changes: 4 additions & 2 deletions lib/Listener/Text2Image/Text2ImageReferenceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\TaskProcessing\IManager as ITaskProcessingManager;
use OCP\TaskProcessing\TaskTypes\TextToImage;
Expand All @@ -20,6 +21,7 @@
class Text2ImageReferenceListener implements IEventListener {
public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private ?string $userId,
private ITaskProcessingManager $taskProcessingManager,
) {
Expand All @@ -31,8 +33,8 @@ public function handle(Event $event): void {
return;
}

if ($this->config->getAppValue(Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1' &&
$this->config->getUserValue($this->userId, Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1') {
if ($this->appConfig->getValueString(Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1'
&& $this->config->getUserValue($this->userId, Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1') {

// Double check that atleast one provider is registered
$availableTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
Expand Down
18 changes: 9 additions & 9 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use OCA\Assistant\AppInfo\Application;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\IAppConfig;
use OCP\Settings\ISettings;
use OCP\TaskProcessing\IManager as ITaskProcessingManager;
use OCP\TaskProcessing\TaskTypes\AudioToText;
Expand All @@ -15,7 +15,7 @@
class Admin implements ISettings {

public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private IInitialState $initialStateService,
private ITaskProcessingManager $taskProcessingManager,
) {
Expand All @@ -33,15 +33,15 @@ public function getForm(): TemplateResponse {
$speechToTextAvailable = array_key_exists(AudioToText::ID, $availableTaskTypes);
$textToImageAvailable = array_key_exists(TextToImage::ID, $availableTaskTypes);

$assistantEnabled = $this->config->getAppValue(Application::APP_ID, 'assistant_enabled', '1') === '1';
$assistantEnabled = $this->appConfig->getValueString(Application::APP_ID, 'assistant_enabled', '1') === '1';

$freePromptPickerEnabled = $this->config->getAppValue(Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1';
$textToImagePickerEnabled = $this->config->getAppValue(Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1';
$freePromptPickerEnabled = $this->appConfig->getValueString(Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1';
$textToImagePickerEnabled = $this->appConfig->getValueString(Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1';

$speechToTextEnabled = $this->config->getAppValue(Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1';
$chattyLLMUserInstructions = $this->config->getAppValue(Application::APP_ID, 'chat_user_instructions', Application::CHAT_USER_INSTRUCTIONS);
$chattyLLMUserInstructionsTitle = $this->config->getAppValue(Application::APP_ID, 'chat_user_instructions_title', Application::CHAT_USER_INSTRUCTIONS_TITLE);
$chattyLLMLastNMessages = (int) $this->config->getAppValue(Application::APP_ID, 'chat_last_n_messages', '10');
$speechToTextEnabled = $this->appConfig->getValueString(Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1';
$chattyLLMUserInstructions = $this->appConfig->getValueString(Application::APP_ID, 'chat_user_instructions', Application::CHAT_USER_INSTRUCTIONS);
$chattyLLMUserInstructionsTitle = $this->appConfig->getValueString(Application::APP_ID, 'chat_user_instructions_title', Application::CHAT_USER_INSTRUCTIONS_TITLE);
$chattyLLMLastNMessages = (int) $this->appConfig->getValueString(Application::APP_ID, 'chat_last_n_messages', '10');

$adminConfig = [
'text_processing_available' => $taskProcessingAvailable,
Expand Down
10 changes: 6 additions & 4 deletions lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OCA\Assistant\AppInfo\Application;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\Settings\ISettings;
use OCP\TaskProcessing\Exception\Exception;
Expand All @@ -17,6 +18,7 @@ class Personal implements ISettings {

public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private IInitialState $initialStateService,
private ?string $userId,
private ITaskProcessingManager $taskProcessingManager,
Expand All @@ -35,16 +37,16 @@ public function getForm(): TemplateResponse {
$speechToTextAvailable = in_array(AudioToText::ID, $availableTaskTypes);
$textToImageAvailable = in_array(TextToImage::ID, $availableTaskTypes);

$assistantAvailable = $taskProcessingAvailable && $this->config->getAppValue(Application::APP_ID, 'assistant_enabled', '1') === '1';
$assistantAvailable = $taskProcessingAvailable && $this->appConfig->getValueString(Application::APP_ID, 'assistant_enabled', '1') === '1';
$assistantEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'assistant_enabled', '1') === '1';

$textToImagePickerAvailable = $textToImageAvailable && $this->config->getAppValue(Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1';
$textToImagePickerAvailable = $textToImageAvailable && $this->appConfig->getValueString(Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1';
$textToImagePickerEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'text_to_image_picker_enabled', '1') === '1';

$freePromptPickerAvailable = $freePromptTaskTypeAvailable && $this->config->getAppValue(Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1';
$freePromptPickerAvailable = $freePromptTaskTypeAvailable && $this->appConfig->getValueString(Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1';
$freePromptPickerEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'free_prompt_picker_enabled', '1') === '1';

$speechToTextPickerAvailable = $speechToTextAvailable && $this->config->getAppValue(Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1';
$speechToTextPickerAvailable = $speechToTextAvailable && $this->appConfig->getValueString(Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1';
$speechToTextPickerEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'speech_to_text_picker_enabled', '1') === '1';

$userConfig = [
Expand Down

0 comments on commit 67fca47

Please sign in to comment.