Skip to content

Commit

Permalink
simplify the last target language management
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Jan 8, 2025
1 parent 51c5552 commit 8d2c550
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
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');
}
}
2 changes: 2 additions & 0 deletions src/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ async function getLastSelectedTaskType() {
}

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

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

Expand Down
71 changes: 27 additions & 44 deletions src/components/AssistantTextProcessingForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,6 @@ export default {
fileId,
})
},
async getLastTargetLanguage() {
const { default: axios } = await import('@nextcloud/axios')
const { generateUrl } = await import('@nextcloud/router')
const req = {
params: {
key: 'last_target_language',
},
}
const url = generateUrl('/apps/assistant/config')
return axios.get(url, req)
},
getTaskTypes() {
this.loadingTaskTypes = true
axios.get(generateOcsUrl('/apps/assistant/api/v1/task-types'))
Expand All @@ -379,37 +366,33 @@ export default {
this.parseTextFileInputs(taskType)
}
this.getLastTargetLanguage()
.then((response) => {
const defaultTargetLanguage = response.data
// add placeholders
taskTypes.forEach(tt => {
if (tt.id === TEXT2TEXT_TASK_TYPE_ID && tt.inputShape.input) {
tt.inputShape.input.placeholder = t('assistant', 'Generate a first draft for a blog post about privacy')
} else if (tt.id === 'context_chat:context_chat' && tt.inputShape.prompt) {
tt.inputShape.prompt.placeholder = t('assistant', 'What is the venue for the team retreat this quarter?')
} else if (tt.id === 'core:text2text:summary' && tt.inputShape.input) {
tt.inputShape.input.placeholder = t('assistant', 'Type or paste the text to summarize')
} else if (tt.id === 'core:text2text:headline' && tt.inputShape.input) {
tt.inputShape.input.placeholder = t('assistant', 'Type or paste the text to generate a headline for')
} else if (tt.id === 'core:text2text:topics' && tt.inputShape.input) {
tt.inputShape.input.placeholder = t('assistant', 'Type or paste the text to extract the topics from')
} else if (tt.id === 'core:text2image' && tt.inputShape.input && tt.inputShape.numberOfImages) {
tt.inputShape.input.placeholder = t('assistant', 'landscape trees forest peaceful')
tt.inputShape.numberOfImages.placeholder = t('assistant', 'a number')
} 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')
// set defaults for translate
} else if (tt.id === 'core:text2text:translate') {
if (defaultTargetLanguage) {
tt.inputShapeDefaults.target_language = defaultTargetLanguage
}
tt.inputShapeDefaults.origin_language = 'detect_language'
}
})
this.taskTypes = taskTypes
})
// add placeholders
taskTypes.forEach(tt => {
if (tt.id === TEXT2TEXT_TASK_TYPE_ID && tt.inputShape.input) {
tt.inputShape.input.placeholder = t('assistant', 'Generate a first draft for a blog post about privacy')
} else if (tt.id === 'context_chat:context_chat' && tt.inputShape.prompt) {
tt.inputShape.prompt.placeholder = t('assistant', 'What is the venue for the team retreat this quarter?')
} else if (tt.id === 'core:text2text:summary' && tt.inputShape.input) {
tt.inputShape.input.placeholder = t('assistant', 'Type or paste the text to summarize')
} else if (tt.id === 'core:text2text:headline' && tt.inputShape.input) {
tt.inputShape.input.placeholder = t('assistant', 'Type or paste the text to generate a headline for')
} else if (tt.id === 'core:text2text:topics' && tt.inputShape.input) {
tt.inputShape.input.placeholder = t('assistant', 'Type or paste the text to extract the topics from')
} else if (tt.id === 'core:text2image' && tt.inputShape.input && tt.inputShape.numberOfImages) {
tt.inputShape.input.placeholder = t('assistant', 'landscape trees forest peaceful')
tt.inputShape.numberOfImages.placeholder = t('assistant', 'a number')
} 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
})
.catch((error) => {
console.error(error)
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 8d2c550

Please sign in to comment.