From 421b9f346fcd8955acf7d28cc060801e807ed222 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Tue, 5 Dec 2023 12:38:41 +0100 Subject: [PATCH] use runOrSchedule when trying to run a sync task Signed-off-by: Julien Veyssier --- appinfo/info.xml | 2 +- appinfo/routes.php | 1 + lib/Controller/AssistantController.php | 19 ++++++++++++++ lib/Service/AssistantService.php | 15 +++++++++++ src/assistant.js | 36 +++++++++++++++++++++++--- src/constants.js | 7 +++++ 6 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 src/constants.js diff --git a/appinfo/info.xml b/appinfo/info.xml index 52d1ad69..6141ab23 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -48,7 +48,7 @@ include text processing providers to: https://github.com/nextcloud/assistant/raw/main/img/screenshot2.jpg https://github.com/nextcloud/assistant/raw/main/img/screenshot3.jpg - + OCA\TPAssistant\Settings\Admin diff --git a/appinfo/routes.php b/appinfo/routes.php index 5f73bdad..a12d2282 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -8,5 +8,6 @@ ['name' => 'assistant#getTaskResultPage', 'url' => '/t/{taskId}', 'verb' => 'GET'], ['name' => 'assistant#runTask', 'url' => '/run', 'verb' => 'POST'], + ['name' => 'assistant#runOrScheduleTask', 'url' => '/run-or-schedule', 'verb' => 'POST'], ], ]; diff --git a/lib/Controller/AssistantController.php b/lib/Controller/AssistantController.php index 03d1da3b..a088092f 100644 --- a/lib/Controller/AssistantController.php +++ b/lib/Controller/AssistantController.php @@ -69,4 +69,23 @@ public function runTask(string $type, string $input, string $appId, string $iden 'task' => $task->jsonSerialize(), ]); } + + /** + * @param string $input + * @param string $type + * @param string $appId + * @param string $identifier + * @return DataResponse + */ + #[NoAdminRequired] + public function runOrScheduleTask(string $type, string $input, string $appId, string $identifier): DataResponse { + try { + $task = $this->assistantService->runOrScheduleTask($type, $input, $appId, $this->userId, $identifier); + } catch (\Exception | \Throwable $e) { + return new DataResponse($e->getMessage(), Http::STATUS_BAD_REQUEST); + } + return new DataResponse([ + 'task' => $task->jsonSerialize(), + ]); + } } diff --git a/lib/Service/AssistantService.php b/lib/Service/AssistantService.php index 913c6ff8..2f0aa5fe 100644 --- a/lib/Service/AssistantService.php +++ b/lib/Service/AssistantService.php @@ -86,4 +86,19 @@ public function runTask(string $type, string $input, string $appId, ?string $use $this->textProcessingManager->runTask($task); return $task; } + + /** + * @param string $type + * @param string $input + * @param string $appId + * @param string|null $userId + * @param string $identifier + * @return Task + * @throws PreConditionNotMetException + */ + public function runOrScheduleTask(string $type, string $input, string $appId, ?string $userId, string $identifier): Task { + $task = new Task($type, $input, $appId, $userId, $identifier); + $this->textProcessingManager->runOrScheduleTask($task); + return $task; + } } diff --git a/src/assistant.js b/src/assistant.js index 2cc3f093..13a0a68e 100644 --- a/src/assistant.js +++ b/src/assistant.js @@ -1,3 +1,4 @@ +import { STATUS } from './constants.js' import { linkTo } from '@nextcloud/router' import { getRequestToken } from '@nextcloud/auth' __webpack_nonce__ = btoa(getRequestToken()) // eslint-disable-line @@ -68,12 +69,18 @@ export async function openAssistantForm({ appId, identifier = '', taskType = nul view.showSyncTaskRunning = true view.input = data.input view.selectedTaskTypeId = data.taskTypeId - runTask(appId, identifier, data.taskTypeId, data.input) + runOrScheduleTask(appId, identifier, data.taskTypeId, data.input) .then((response) => { - resolve(response.data?.task) - view.output = response.data?.task?.output + const task = response.data?.task + if (task.status === STATUS.successfull) { + view.output = task?.output + } else if (task.status === STATUS.scheduled) { + view.input = task.input + view.showScheduleConfirmation = true + } view.loading = false view.showSyncTaskRunning = false + resolve(task) }) .catch(error => { if (error?.code === 'ERR_CANCELED') { @@ -123,6 +130,21 @@ export async function runTask(appId, identifier, taskType, input) { return axios.post(url, params, { signal: window.assistantAbortController.signal }) } +export async function runOrScheduleTask(appId, identifier, taskType, input) { + window.assistantAbortController = new AbortController() + const { default: axios } = await import(/* webpackChunkName: "axios-lazy" */'@nextcloud/axios') + const { generateUrl } = await import(/* webpackChunkName: "router-gen-lazy" */'@nextcloud/router') + saveLastSelectedTaskType(taskType) + const url = generateUrl('/apps/assistant/run-or-schedule') + const params = { + input, + type: taskType, + appId, + identifier, + } + return axios.post(url, params, { signal: window.assistantAbortController.signal }) +} + /** * Send a request to schedule a task * @@ -261,7 +283,13 @@ export async function openAssistantResult(task) { runTask(task.appId, task.identifier, data.taskTypeId, data.input) .then((response) => { // resolve(response.data?.task) - view.output = response.data?.task?.output + const task = response.data?.task + if (task.status === STATUS.successfull) { + view.output = task?.output + } else if (task.status === STATUS.scheduled) { + view.input = task?.input + view.showScheduleConfirmation = true + } view.loading = false view.showSyncTaskRunning = false }) diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 00000000..a0928539 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,7 @@ +export const STATUS = { + failed: 4, + successfull: 3, + running: 2, + scheduled: 1, + unknown: 0, +}