Skip to content

Commit bc4c327

Browse files
committed
allow to pass file ID or path as initial input text field value
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 1437648 commit bc4c327

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

lib/Controller/AssistantApiController.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,24 @@ public function getUserTasks(?string $taskTypeId = null): DataResponse {
121121
*
122122
* Parse and extract text content of a file (if the file type is supported)
123123
*
124-
* @param string $filePath Path of the file to parse in the user's storage
124+
* @param string|null $filePath Path of the file to parse in the user's storage
125+
* @param int|null $fileId Id of the file to parse in the user's storage
125126
* @return DataResponse<Http::STATUS_OK, array{parsedText: string}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, string, array{}>
126127
*
127128
* 200: Text parsed from file successfully
128129
* 400: Parsing text from file is not possible
129130
*/
130131
#[NoAdminRequired]
131-
public function parseTextFromFile(string $filePath): DataResponse {
132+
public function parseTextFromFile(?string $filePath = null, ?int $fileId = null): DataResponse {
132133
if ($this->userId === null) {
133134
return new DataResponse('Unknow user', Http::STATUS_BAD_REQUEST);
134135
}
136+
if ($fileId === null && $filePath === null) {
137+
return new DataResponse('Invalid parameters', Http::STATUS_BAD_REQUEST);
138+
}
135139

136140
try {
137-
$text = $this->assistantService->parseTextFromFile($filePath, $this->userId);
141+
$text = $this->assistantService->parseTextFromFile($this->userId, $filePath, $fileId);
138142
} catch (\Exception | \Throwable $e) {
139143
return new DataResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
140144
}

lib/Service/AssistantService.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,14 @@ private function sanitizeInputs(string $type, array $inputs): array {
545545

546546
/**
547547
* Parse text from file (if parsing the file type is supported)
548-
* @param string $filePath
549548
* @param string $userId
549+
* @param string|null $filePath
550+
* @param int|null $fileId
550551
* @return string
551-
* @throws \Exception
552+
* @throws NotPermittedException
553+
* @throws \OCP\Files\NotFoundException
552554
*/
553-
public function parseTextFromFile(string $filePath, string $userId): string {
555+
public function parseTextFromFile(string $userId, ?string $filePath = null, ?int $fileId = null): string {
554556

555557
try {
556558
$userFolder = $this->rootFolder->getUserFolder($userId);
@@ -559,7 +561,11 @@ public function parseTextFromFile(string $filePath, string $userId): string {
559561
}
560562

561563
try {
562-
$file = $userFolder->get($filePath);
564+
if ($filePath !== null) {
565+
$file = $userFolder->get($filePath);
566+
} else {
567+
$file = $userFolder->getFirstNodeById($fileId);
568+
}
563569
} catch (NotFoundException $e) {
564570
throw new \Exception('File not found.');
565571
}

src/components/AssistantTextProcessingForm.vue

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,23 +315,53 @@ export default {
315315
console.debug('[assistant] form\'s myoutputs', this.myOutputs)
316316
},
317317
methods: {
318+
// Parse the file if a fileId is passed as initial value to a text field
319+
parseTextFileInputs(taskType) {
320+
if (taskType === undefined || taskType === null) {
321+
return
322+
}
323+
Object.keys(this.myInputs).forEach(k => {
324+
if (taskType.inputShape[k]?.type === 'Text') {
325+
if (this.myInputs[k]?.fileId || this.myInputs[k]?.filePath) {
326+
const { filePath, fileId } = { fileId: this.myInputs[k]?.fileId, filePath: this.myInputs[k]?.filePath }
327+
this.myInputs[k] = ''
328+
this.parseFile({ fileId, filePath })
329+
.then(response => {
330+
if (response.data?.ocs?.data?.parsedText) {
331+
this.myInputs[k] = response.data?.ocs?.data?.parsedText
332+
}
333+
})
334+
}
335+
}
336+
})
337+
},
338+
parseFile({ filePath, fileId }) {
339+
const url = generateOcsUrl('/apps/assistant/api/v1/parse-file')
340+
return axios.post(url, {
341+
filePath,
342+
fileId,
343+
})
344+
},
318345
getTaskTypes() {
319346
this.loadingTaskTypes = true
320347
axios.get(generateOcsUrl('/apps/assistant/api/v1/task-types'))
321348
.then((response) => {
322-
this.taskTypes = response.data.ocs.data.types
349+
const taskTypes = response.data.ocs.data.types
323350
// check if selected task type is in the list, fallback to text2text
324-
const taskType = this.taskTypes.find(tt => tt.id === this.mySelectedTaskTypeId)
351+
const taskType = taskTypes.find(tt => tt.id === this.mySelectedTaskTypeId)
325352
if (taskType === undefined) {
326-
const text2textType = this.taskTypes.find(tt => tt.id === TEXT2TEXT_TASK_TYPE_ID)
353+
const text2textType = taskTypes.find(tt => tt.id === TEXT2TEXT_TASK_TYPE_ID)
327354
if (text2textType) {
355+
this.parseTextFileInputs(text2textType)
328356
this.mySelectedTaskTypeId = TEXT2TEXT_TASK_TYPE_ID
329357
} else {
330358
this.mySelectedTaskTypeId = null
331359
}
360+
} else {
361+
this.parseTextFileInputs(taskType)
332362
}
333363
// add placeholders
334-
this.taskTypes.forEach(tt => {
364+
taskTypes.forEach(tt => {
335365
if (tt.id === TEXT2TEXT_TASK_TYPE_ID && tt.inputShape.input) {
336366
tt.inputShape.input.placeholder = t('assistant', 'Generate a first draft for a blog post about privacy')
337367
} else if (tt.id === 'context_chat:context_chat' && tt.inputShape.prompt) {
@@ -350,6 +380,7 @@ export default {
350380
tt.inputShape.source_input.placeholder = t('assistant', 'A description of what you need or some original content')
351381
}
352382
})
383+
this.taskTypes = taskTypes
353384
})
354385
.catch((error) => {
355386
console.error(error)

0 commit comments

Comments
 (0)