Skip to content

Commit

Permalink
clarify task statuses, fix stt status check when notifying
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Feb 16, 2024
1 parent d4d1305 commit a421187
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 41 deletions.
16 changes: 12 additions & 4 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\SpeechToText\Events\TranscriptionSuccessfulEvent;
use OCP\TextProcessing\Events\TaskFailedEvent as TextTaskFailedEvent;
use OCP\TextProcessing\Events\TaskSuccessfulEvent as TextTaskSuccessfulEvent;
use OCP\TextProcessing\Task as OCPTextprocessingTask;
use OCP\TextToImage\Events\TaskFailedEvent as TextToImageTaskFailedEvent;
use OCP\TextToImage\Events\TaskSuccessfulEvent as TextToImageTaskSuccessfulEvent;

Expand All @@ -45,11 +46,18 @@ class Application extends App implements IBootstrap {
public const STT_TASK_SUCCESSFUL = 1;
public const STT_TASK_FAILED = -1;

public const STATUS_META_TASK_FAILED = 4;
public const STATUS_META_TASK_SUCCESSFUL = 3;
public const STATUS_META_TASK_RUNNING = 2;
public const STATUS_META_TASK_SCHEDULED = 1;
public const STATUS_META_TASK_UNKNOWN = 0;
public const STATUS_META_TASK_SCHEDULED = 1;
public const STATUS_META_TASK_RUNNING = 2;
public const STATUS_META_TASK_SUCCESSFUL = 3;
public const STATUS_META_TASK_FAILED = 4;
public const TP_STATUS_TO_META_STATUS = [
OCPTextprocessingTask::STATUS_UNKNOWN => self::STATUS_META_TASK_UNKNOWN,
OCPTextprocessingTask::STATUS_SCHEDULED => self::STATUS_META_TASK_SCHEDULED,
OCPTextprocessingTask::STATUS_RUNNING => self::STATUS_META_TASK_RUNNING,
OCPTextprocessingTask::STATUS_SUCCESSFUL => self::STATUS_META_TASK_SUCCESSFUL,
OCPTextprocessingTask::STATUS_FAILED => self::STATUS_META_TASK_FAILED,
];

public const TASK_CATEGORY_TEXT_GEN = 0;
public const TASK_CATEGORY_TEXT_TO_IMAGE = 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/TaskFailedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function handle(Event $event): void {
}

// Update task status and output:
$assistantTask->setStatus($task->getStatus());
$assistantTask->setStatus(Application::TP_STATUS_TO_META_STATUS[$task->getStatus()]);
$assistantTask->setOutput($event->getErrorMessage());
$assistantTask = $this->metaTaskMapper->update($assistantTask);

Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/TaskSuccessfulListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function handle(Event $event): void {
}

// Update task status and output:
$assistantTask->setStatus($task->getStatus());
$assistantTask->setStatus(Application::TP_STATUS_TO_META_STATUS[$task->getStatus()]);
$assistantTask->setOutput($task->getOutput());
$assistantTask = $this->metaTaskMapper->update($assistantTask);

Expand Down
4 changes: 2 additions & 2 deletions lib/Listener/Text2Image/Text2ImageResultListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function handle(Event $event): void {

$this->text2ImageService->storeImages($images, $imageGenId);

$assistantTask->setStatus(Task::STATUS_SUCCESSFUL);
$assistantTask->setStatus(Application::STATUS_META_TASK_SUCCESSFUL);
$assistantTask = $this->metaTaskMapper->update($assistantTask);
}

Expand All @@ -66,7 +66,7 @@ public function handle(Event $event): void {
$this->imageGenerationMapper->setFailed($imageGenId, true);

// Update the assistant meta task status:
$assistantTask->setStatus(Task::STATUS_FAILED);
$assistantTask->setStatus(Application::STATUS_META_TASK_FAILED);
$assistantTask->setOutput($event->getErrorMessage());
$assistantTask = $this->metaTaskMapper->update($assistantTask);

Expand Down
12 changes: 12 additions & 0 deletions lib/Service/AssistantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

require_once __DIR__ . '/../../vendor/autoload.php';

use OC\SpeechToText\TranscriptionJob;
use OCA\TpAssistant\AppInfo\Application;
use OCA\TpAssistant\Db\MetaTask;
use OCA\TpAssistant\Db\MetaTaskMapper;
use OCA\TpAssistant\Service\Text2Image\Text2ImageHelperService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\BackgroundJob\IJobList;
use OCP\Common\Exception\NotFoundException;
use OCP\DB\Exception;
use OCP\Files\File;
Expand All @@ -35,6 +37,7 @@ public function __construct(
private MetaTaskMapper $metaTaskMapper,
private LoggerInterface $logger,
private IRootFolder $storage,
private IJobList $jobList,
private IL10N $l10n,
) {
}
Expand Down Expand Up @@ -163,6 +166,15 @@ private function cancelOcpTaskOfMetaTask(string $userId, MetaTask $metaTask) {
$this->text2ImageHelperService->cancelGeneration($metaTask->getOutput(), $userId);
} elseif ($metaTask->getCategory() === Application::TASK_CATEGORY_SPEECH_TO_TEXT) {
// TODO implement task canceling in stt manager
$jobArguments = [
'fileId' => $metaTask->getOcpTaskId(),
'owner' => $userId,
'userId' => $userId,
'appId' => Application::APP_ID,
];
if ($this->jobList->has(TranscriptionJob::class, $jobArguments)) {
$this->jobList->remove(TranscriptionJob::class, $jobArguments);
}
}
}

Expand Down
43 changes: 11 additions & 32 deletions lib/Service/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,28 @@ public function __construct(
/**
* Send a success or failure task result notification
*
* @param MetaTask $task
* @param MetaTask $metaTask
* @param string|null $customTarget optional notification link target
* @param string|null $actionLabel optional label for the notification action button
* @param string|null $resultPreview
* @return void
*/
public function sendNotification(MetaTask $task, ?string $customTarget = null, ?string $actionLabel = null, ?string $resultPreview = null): void {
public function sendNotification(MetaTask $metaTask, ?string $customTarget = null, ?string $actionLabel = null, ?string $resultPreview = null): void {
$manager = $this->notificationManager;
$notification = $manager->createNotification();

$params = [
'appId' => $task->getAppId(),
'id' => $task->getId(),
'inputs' => $task->getInputsAsArray(),
'target' => $customTarget ?? $this->getDefaultTarget($task),
'appId' => $metaTask->getAppId(),
'id' => $metaTask->getId(),
'inputs' => $metaTask->getInputsAsArray(),
'target' => $customTarget ?? $this->getDefaultTarget($metaTask),
'actionLabel' => $actionLabel,
'result' => $resultPreview,
];
$params['taskTypeClass'] = $task->getTaskType();
$params['taskCategory'] = $task->getCategory();
$params['taskTypeClass'] = $metaTask->getTaskType();
$params['taskCategory'] = $metaTask->getCategory();

switch ($task->getCategory()) {
case Application::TASK_CATEGORY_TEXT_TO_IMAGE:
{
$taskSuccessful = $task->getStatus() === TextToImageTask::STATUS_SUCCESSFUL;
break;
}
case Application::TASK_CATEGORY_TEXT_GEN:
{
$taskSuccessful = $task->getStatus() === TextProcessingTask::STATUS_SUCCESSFUL;
break;
}
case Application::TASK_CATEGORY_SPEECH_TO_TEXT:
{
$taskSuccessful = $task->getStatus() === Application::STT_TASK_SUCCESSFUL;
break;
}
default:
{
$taskSuccessful = false;
break;
}
}
$taskSuccessful = $metaTask->getStatus() === Application::STATUS_META_TASK_SUCCESSFUL;

$subject = $taskSuccessful
? 'success'
Expand All @@ -74,9 +53,9 @@ public function sendNotification(MetaTask $task, ?string $customTarget = null, ?
: 'task-with-custom-target';

$notification->setApp(Application::APP_ID)
->setUser($task->getUserId())
->setUser($metaTask->getUserId())
->setDateTime(new DateTime())
->setObject($objectType, (string) ($task->getId() ?? 0))
->setObject($objectType, (string) ($metaTask->getId() ?? 0))
->setSubject($subject, $params);

$manager->notify($notification);
Expand Down
2 changes: 1 addition & 1 deletion src/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export async function runSttTask(inputs) {
saveLastSelectedTaskType('speech-to-text')
if (inputs.sttMode === 'choose') {
const url = generateUrl('/apps/assistant/stt/transcribeFile')
const params = { path: this.audioFilePath }
const params = { path: inputs.audioFilePath }
return axios.post(url, params)
} else {
const url = generateUrl('/apps/assistant/stt/transcribeAudio')
Expand Down

0 comments on commit a421187

Please sign in to comment.