Skip to content

Commit

Permalink
use runOrSchedule when trying to run a sync task
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Dec 6, 2023
1 parent 672044a commit 421b9f3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ include text processing providers to:
<screenshot>https://github.com/nextcloud/assistant/raw/main/img/screenshot2.jpg</screenshot>
<screenshot>https://github.com/nextcloud/assistant/raw/main/img/screenshot3.jpg</screenshot>
<dependencies>
<nextcloud min-version="27.1.0" max-version="29"/>
<nextcloud min-version="28" max-version="29"/>
</dependencies>
<settings>
<admin>OCA\TPAssistant\Settings\Admin</admin>
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
];
19 changes: 19 additions & 0 deletions lib/Controller/AssistantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
}
}
15 changes: 15 additions & 0 deletions lib/Service/AssistantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
36 changes: 32 additions & 4 deletions src/assistant.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
})
Expand Down
7 changes: 7 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const STATUS = {
failed: 4,
successfull: 3,
running: 2,
scheduled: 1,
unknown: 0,
}

0 comments on commit 421b9f3

Please sign in to comment.