Skip to content

Commit

Permalink
Merge pull request #167 from nextcloud/feat/set-language-defaults
Browse files Browse the repository at this point in the history
feat: save default target language & set detect_language as origin default
  • Loading branch information
marcelklehr authored Jan 8, 2025
2 parents 43a513e + 8d2c550 commit 1ad93f7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/Controller/AssistantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public function getAssistantStandalonePage(): TemplateResponse {
$this->userId,
''
);
$this->initialStateService->provideInitialState('task', $task->jsonSerialize());
$serializedTask = $task->jsonSerialize();
// otherwise the task id is 0 and the default input shape values are not set
$serializedTask['id'] = null;
$this->initialStateService->provideInitialState('task', $serializedTask);
return new TemplateResponse(Application::APP_ID, 'assistantPage');
}
return new TemplateResponse('', '403', [], TemplateResponse::RENDER_AS_ERROR, Http::STATUS_FORBIDDEN);
Expand Down
4 changes: 4 additions & 0 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function handle(Event $event): void {
$userAssistantEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'assistant_enabled', '1') === '1';
$assistantEnabled = $adminAssistantEnabled && $userAssistantEnabled;
$this->initialStateService->provideInitialState('assistant-enabled', $assistantEnabled);
if ($assistantEnabled) {
$lastTargetLanguage = $this->config->getUserValue($this->userId, Application::APP_ID, 'last_target_language', '');
$this->initialStateService->provideInitialState('last-target-language', $lastTargetLanguage);
}
Util::addScript(Application::APP_ID, Application::APP_ID . '-main');
}
}
18 changes: 18 additions & 0 deletions src/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ export async function scheduleTask(appId, customId, taskType, inputs) {
const { default: axios } = await import('@nextcloud/axios')
const { generateOcsUrl } = await import('@nextcloud/router')
saveLastSelectedTaskType(taskType)
if (taskType === 'core:text2text:translate') {
saveLastTargetLanguage(inputs.target_language)
}
const url = generateOcsUrl('taskprocessing/schedule')
const params = {
input: inputs,
Expand Down Expand Up @@ -271,6 +274,21 @@ async function getLastSelectedTaskType() {
return axios.get(url, req)
}

async function saveLastTargetLanguage(targetLanguage) {
OCA.Assistant.last_target_language = targetLanguage

const { default: axios } = await import('@nextcloud/axios')
const { generateUrl } = await import('@nextcloud/router')

const req = {
values: {
last_target_language: targetLanguage,
},
}
const url = generateUrl('/apps/assistant/config')
return axios.put(url, req)
}

/**
* Check if we want to cancel a notification action click and handle it ourselves
*
Expand Down
7 changes: 7 additions & 0 deletions src/components/AssistantTextProcessingForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ export default {
} else {
this.parseTextFileInputs(taskType)
}
// add placeholders
taskTypes.forEach(tt => {
if (tt.id === TEXT2TEXT_TASK_TYPE_ID && tt.inputShape.input) {
Expand All @@ -383,6 +384,12 @@ export default {
} else if (tt.id === 'core:contextwrite' && tt.inputShape.source_input && tt.inputShape.style_input) {
tt.inputShape.style_input.placeholder = t('assistant', 'Shakespeare or an example of the style')
tt.inputShape.source_input.placeholder = t('assistant', 'A description of what you need or some original content')
} else if (tt.id === 'core:text2text:translate') {
tt.inputShapeDefaults.origin_language = 'detect_language'
const defaultTargetLanguage = OCA.Assistant.last_target_language
if (defaultTargetLanguage) {
tt.inputShapeDefaults.target_language = defaultTargetLanguage
}
}
})
this.taskTypes = taskTypes
Expand Down
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function init() {
subscribe('notifications:action:execute', handleNotification)
if (loadState('assistant', 'assistant-enabled')) {
addAssistantMenuEntry()
OCA.Assistant.last_target_language = loadState('assistant', 'last-target-language')
}
}
}
Expand Down

0 comments on commit 1ad93f7

Please sign in to comment.