diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index d31f951d..ad3a9146 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -55,6 +55,7 @@ class Application extends App implements IBootstrap { public const CHAT_USER_INSTRUCTIONS = 'This is a conversation in a specific language between the user and you, Nextcloud Assistant. You are a kind, polite and helpful AI that helps the user to the best of its abilities. If you do not understand something, you will ask for clarification. Detect the language that the user is using. Make sure to use the same language in your response. Do not mention the language explicitly.'; public const CHAT_USER_INSTRUCTIONS_TITLE = 'Above is a chat session in a specific language between the user and you, Nextcloud Assistant. Generate a suitable title summarizing the conversation in the same language. Output only the title in plain text, nothing else.'; + public const MAX_TEXT_INPUT_LENGTH = 64_000; private IAppConfig $appConfig; private IManager $taskProcessingManager; diff --git a/lib/Controller/ChattyLLMController.php b/lib/Controller/ChattyLLMController.php index 2af5b57f..d359312a 100644 --- a/lib/Controller/ChattyLLMController.php +++ b/lib/Controller/ChattyLLMController.php @@ -292,6 +292,9 @@ public function newMessage( if ($this->userId === null) { return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED); } + if (strlen($content) > Application::MAX_TEXT_INPUT_LENGTH) { + return new JSONResponse(['error' => $this->l10n->t('The new message is too long')], Http::STATUS_BAD_REQUEST); + } try { $sessionExists = $this->sessionMapper->exists($this->userId, $sessionId); diff --git a/lib/Service/AssistantService.php b/lib/Service/AssistantService.php index 1998549f..cf4395e9 100644 --- a/lib/Service/AssistantService.php +++ b/lib/Service/AssistantService.php @@ -244,6 +244,11 @@ public function getAvailableTaskTypes(): array { 'id' => 'core:inputList', 'priority' => 0, 'inputShape' => [ + 'textList' => new ShapeDescriptor( + 'Input text list', + 'plop', + EShapeType::ListOfTexts, + ), 'fileList' => new ShapeDescriptor( 'Input file list', 'plop', diff --git a/src/components/AssistantTextProcessingForm.vue b/src/components/AssistantTextProcessingForm.vue index 765c3dcc..62375af7 100644 --- a/src/components/AssistantTextProcessingForm.vue +++ b/src/components/AssistantTextProcessingForm.vue @@ -152,7 +152,7 @@ import TaskList from './TaskList.vue' import TaskTypeSelect from './TaskTypeSelect.vue' import TranslateForm from './Translate/TranslateForm.vue' -import { SHAPE_TYPE_NAMES } from '../constants.js' +import { SHAPE_TYPE_NAMES, MAX_TEXT_INPUT_LENGTH } from '../constants.js' import axios from '@nextcloud/axios' import { generateOcsUrl, generateUrl } from '@nextcloud/router' @@ -330,7 +330,11 @@ export default { } const fieldType = taskType.inputShape[k].type const value = this.myInputs[k] - return ([SHAPE_TYPE_NAMES.Text, SHAPE_TYPE_NAMES.Enum].includes(fieldType) && typeof value === 'string' && !!value?.trim()) + return ([SHAPE_TYPE_NAMES.Text, SHAPE_TYPE_NAMES.Enum].includes(fieldType) + && typeof value === 'string' + && !!value?.trim() + // check that the input text is not too long for text fields + && (fieldType === SHAPE_TYPE_NAMES.Enum || value.trim().length <= MAX_TEXT_INPUT_LENGTH)) || ([ SHAPE_TYPE_NAMES.Number, SHAPE_TYPE_NAMES.File, @@ -338,7 +342,11 @@ export default { SHAPE_TYPE_NAMES.Audio, SHAPE_TYPE_NAMES.Video, ].includes(fieldType) && typeof value === 'number') - || (fieldType === SHAPE_TYPE_NAMES.ListOfTexts && typeof value === 'object' && !!value && value.every(v => typeof v === 'string')) + || (fieldType === SHAPE_TYPE_NAMES.ListOfTexts && typeof value === 'object' && !!value && value.every(v => { + return typeof v === 'string' + && !!v?.trim() + && v.trim().length <= MAX_TEXT_INPUT_LENGTH + })) || (fieldType === SHAPE_TYPE_NAMES.ListOfNumbers && typeof value === 'object' && !!value && value.every(v => typeof v === 'number')) || ([ SHAPE_TYPE_NAMES.ListOfFiles, diff --git a/src/components/ChattyLLM/ChattyLLMInputForm.vue b/src/components/ChattyLLM/ChattyLLMInputForm.vue index 592ac86f..3dbcc2b3 100644 --- a/src/components/ChattyLLM/ChattyLLMInputForm.vue +++ b/src/components/ChattyLLM/ChattyLLMInputForm.vue @@ -128,6 +128,10 @@

{{ t('assistant', 'Output shown here is generated by AI. Make sure to always double-check.') }}

+

+ {{ t('assistant', 'Messages should not be longer than {maxLength} characters (currently {length}).', { maxLength: 64_000, length: chatContent.length }) }} +

+ @submit="onSubmitText" />
+ @click="onSubmitText"> @@ -50,6 +50,7 @@ import { generateOcsUrl } from '@nextcloud/router' import axios from '@nextcloud/axios' import { showError } from '@nextcloud/dialogs' import { loadState } from '@nextcloud/initial-state' +import { MAX_TEXT_INPUT_LENGTH } from '../../constants.js' /* maxlength calculation (just a rough estimate): @@ -112,6 +113,9 @@ export default { disabled() { return this.loading.llmGeneration || this.loading.olderMessages || this.loading.initialMessages || this.loading.titleGeneration || this.loading.newHumanMessage || this.loading.newSession }, + chatContentTooLong() { + return this.chatContent.length > MAX_TEXT_INPUT_LENGTH + }, }, mounted() { @@ -139,6 +143,11 @@ export default { console.error(error) }) }, + onSubmitText(e) { + if (!this.chatContentTooLong) { + this.$emit('submit', e) + } + }, }, } diff --git a/src/components/fields/ListOfTextsField.vue b/src/components/fields/ListOfTextsField.vue index 096c0d50..aadace2b 100644 --- a/src/components/fields/ListOfTextsField.vue +++ b/src/components/fields/ListOfTextsField.vue @@ -137,7 +137,7 @@ export default { &--item { display: flex; gap: 4px; - align-items: center; + align-items: end; .text-input { flex-grow: 1; } diff --git a/src/components/fields/TextInput.vue b/src/components/fields/TextInput.vue index fb8bb980..c88045ee 100644 --- a/src/components/fields/TextInput.vue +++ b/src/components/fields/TextInput.vue @@ -6,6 +6,8 @@
getFilePickerBuilder(t('assistant', 'Choose a text file')) .setMimeTypeFilter(VALID_TEXT_MIME_TYPES) @@ -123,6 +126,7 @@ export default { data() { return { copied: false, + maxLength: MAX_TEXT_INPUT_LENGTH, } }, @@ -136,6 +140,12 @@ export default { hasValue() { return this.formattedValue !== '' }, + limitLabel() { + const length = this.value?.length ?? 0 + return length > this.maxLength + ? t('assistant', 'Warning: The input text exceeds the maximum length of {limit} characters (currently {length}).', { length, limit: this.maxLength }) + : undefined + }, }, watch: { diff --git a/src/constants.js b/src/constants.js index b562c064..07d4c6aa 100644 --- a/src/constants.js +++ b/src/constants.js @@ -3,6 +3,8 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ +export const MAX_TEXT_INPUT_LENGTH = 64_000 + export const TASK_STATUS_INT = { cancelled: 5, failed: 4,