diff --git a/appinfo/info.xml b/appinfo/info.xml index f1c013a7..8d70b9ae 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -34,7 +34,7 @@ include text processing providers to: * Get an answer from a free prompt * Reformulate (OpenAi/LocalAi only) ]]> - 1.0.4 + 1.0.5 agpl Julien Veyssier TpAssistant diff --git a/lib/Command/CleanupAssistantTasks.php b/lib/Command/CleanupAssistantTasks.php index f3b05d8f..566d34cb 100644 --- a/lib/Command/CleanupAssistantTasks.php +++ b/lib/Command/CleanupAssistantTasks.php @@ -8,14 +8,14 @@ use Exception; use OC\Core\Command\Base; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CleanupAssistantTasks extends Base { public function __construct( - private TaskMapper $taskMapper, + private MetaTaskMapper $metaTaskMapper, ) { parent::__construct(); } @@ -42,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Cleanning up assistant tasks older than ' . $maxAge . ' seconds.'); try { - $cleanedUp = $this->taskMapper->cleanupOldTasks($maxAge); + $cleanedUp = $this->metaTaskMapper->cleanupOldMetaTasks($maxAge); } catch (Exception $e) { $output->writeln('Error: ' . $e->getMessage()); return 1; diff --git a/lib/Controller/AssistantController.php b/lib/Controller/AssistantController.php index d2feced7..c92daa37 100644 --- a/lib/Controller/AssistantController.php +++ b/lib/Controller/AssistantController.php @@ -3,7 +3,6 @@ namespace OCA\TpAssistant\Controller; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\TaskMapper; use OCA\TpAssistant\Service\AssistantService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -17,12 +16,11 @@ class AssistantController extends Controller { public function __construct( - string $appName, - IRequest $request, + string $appName, + IRequest $request, private AssistantService $assistantService, - private IInitialState $initialStateService, - private ?string $userId, - private TaskMapper $taskMapper, + private IInitialState $initialStateService, + private ?string $userId, ) { parent::__construct($appName, $request); } @@ -34,7 +32,7 @@ public function __construct( #[NoAdminRequired] #[NoCSRFRequired] public function getTextProcessingTaskResultPage(int $taskId): TemplateResponse { - + if ($this->userId !== null) { $task = $this->assistantService->getTextProcessingTask($this->userId, $taskId); if ($task !== null) { @@ -143,7 +141,7 @@ public function parseTextFromFile(string $filePath): DataResponse { if ($this->userId === null) { return new DataResponse('Unknow user', Http::STATUS_BAD_REQUEST); } - + try { $text = $this->assistantService->parseTextFromFile($filePath, $this->userId); } catch (\Exception | \Throwable $e) { diff --git a/lib/Controller/SpeechToTextController.php b/lib/Controller/SpeechToTextController.php index 14b63878..79deb1ae 100644 --- a/lib/Controller/SpeechToTextController.php +++ b/lib/Controller/SpeechToTextController.php @@ -25,8 +25,8 @@ use Exception; use InvalidArgumentException; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\Task; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTask; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCA\TpAssistant\Service\SpeechToText\SpeechToTextService; use OCP\AppFramework\Controller; use OCP\AppFramework\Db\DoesNotExistException; @@ -48,14 +48,14 @@ class SpeechToTextController extends Controller { public function __construct( - string $appName, - IRequest $request, + string $appName, + IRequest $request, private SpeechToTextService $service, - private LoggerInterface $logger, - private IL10N $l10n, - private IInitialState $initialState, - private ?string $userId, - private TaskMapper $taskMapper, + private LoggerInterface $logger, + private IL10N $l10n, + private IInitialState $initialState, + private ?string $userId, + private MetaTaskMapper $metaTaskMapper, ) { parent::__construct($appName, $request); } @@ -101,17 +101,17 @@ public function getTranscript(int $id): DataResponse { * Internal function to get transcription assistant tasks based on the assistant meta task id * * @param integer $id - * @return Task + * @return MetaTask */ - private function internalGetTask(int $id): Task { + private function internalGetTask(int $id): MetaTask { try { - $task = $this->taskMapper->getTaskOfUser($id, $this->userId); - - if($task->getCategory() !== Application::TASK_CATEGORY_SPEECH_TO_TEXT) { + $metaTask = $this->metaTaskMapper->getMetaTaskOfUser($id, $this->userId); + + if($metaTask->getCategory() !== Application::TASK_CATEGORY_SPEECH_TO_TEXT) { throw new Exception('Task is not a speech to text task.', Http::STATUS_BAD_REQUEST); } - return $task; + return $metaTask; } catch (MultipleObjectsReturnedException $e) { $this->logger->error('Multiple tasks found for one id: ' . $e->getMessage(), ['app' => Application::APP_ID]); throw new Exception($this->l10n->t('Multiple tasks found'), Http::STATUS_BAD_REQUEST); diff --git a/lib/Cron/CleanupAssistantTasks.php b/lib/Cron/CleanupAssistantTasks.php index 47321d1d..288cdb7b 100644 --- a/lib/Cron/CleanupAssistantTasks.php +++ b/lib/Cron/CleanupAssistantTasks.php @@ -7,7 +7,7 @@ namespace OCA\TpAssistant\Cron; use Exception; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; use Psr\Log\LoggerInterface; @@ -15,9 +15,9 @@ class CleanupAssistantTasks extends TimedJob { public function __construct( - ITimeFactory $time, + ITimeFactory $time, private LoggerInterface $logger, - private TaskMapper $taskMapper, + private MetaTaskMapper $metaTaskMapper, ) { parent::__construct($time); $this->setInterval(60 * 60 * 24); @@ -27,7 +27,7 @@ protected function run($argument): void { $this->logger->debug('Run cleanup job for assistant tasks'); try { - $this->taskMapper->cleanupOldTasks(); + $this->metaTaskMapper->cleanupOldMetaTasks(); } catch (\OCP\Db\Exception | RuntimeException | Exception $e) { $this->logger->debug('Cleanup job for assistant tasks failed: ' . $e->getMessage()); } diff --git a/lib/Db/Task.php b/lib/Db/MetaTask.php similarity index 67% rename from lib/Db/Task.php rename to lib/Db/MetaTask.php index 17b103ac..30d3592c 100644 --- a/lib/Db/Task.php +++ b/lib/Db/MetaTask.php @@ -9,28 +9,28 @@ use OCP\AppFramework\Db\Entity; /** - * @method string getUserId() - * @method void setUserId(string $userId) - * @method string getOutput() - * @method void setOutput(string $value) - * @method string getAppId() - * @method void setAppId(string $appId) - * @method int getOcpTaskId() - * @method void setOcpTaskId(int $value) - * @method int getTimestamp() - * @method void setTimestamp(int $timestamp) - * @method string getTaskType() - * @method void setTaskType(string $taskType) - * @method void setStatus(int $status) - * @method int getStatus() - * @method void setCategory(int $category) - * @method int getCategory() - * @method string getInputs() - * @method void setInputs(string $inputs) - * @method string getIndentifer() - * @method void setIndentifer(string $indentifer) + * @method \string getUserId() + * @method \void setUserId(string $userId) + * @method \string getOutput() + * @method \void setOutput(string $value) + * @method \string getAppId() + * @method \void setAppId(string $appId) + * @method \int getOcpTaskId() + * @method \void setOcpTaskId(int $value) + * @method \int getTimestamp() + * @method \void setTimestamp(int $timestamp) + * @method \string getTaskType() + * @method \void setTaskType(string $taskType) + * @method \void setStatus(int $status) + * @method \int getStatus() + * @method \void setCategory(int $category) + * @method \int getCategory() + * @method \string getInputs() + * @method \void setInputs(string $inputs) + * @method \string getIdentifier() + * @method \void setIdentifier(string $identifier) */ -class Task extends Entity implements \JsonSerializable { +class MetaTask extends Entity implements \JsonSerializable { /** @var string */ protected $userId; /** @var string */ @@ -50,7 +50,7 @@ class Task extends Entity implements \JsonSerializable { /** @var int */ protected $category; /** @var string */ - protected $indentifer; + protected $identifier; public function __construct() { $this->addType('user_id', 'string'); @@ -62,7 +62,7 @@ public function __construct() { $this->addType('task_type', 'string'); $this->addType('status', 'integer'); $this->addType('category', 'integer'); - $this->addType('indentifer', 'string'); + $this->addType('identifier', 'string'); } #[\ReturnTypeWillChange] @@ -78,7 +78,7 @@ public function jsonSerialize() { 'timestamp' => $this->timestamp, 'status' => $this->status, 'category' => $this->category, - 'indentifer' => $this->indentifer, + 'identifier' => $this->identifier, ]; } @@ -95,7 +95,7 @@ public function jsonSerializeCc() { 'timestamp' => $this->timestamp, 'status' => $this->status, 'category' => $this->category, - 'indentifer' => $this->indentifer, + 'identifier' => $this->identifier, ]; } diff --git a/lib/Db/TaskMapper.php b/lib/Db/MetaTaskMapper.php similarity index 84% rename from lib/Db/TaskMapper.php rename to lib/Db/MetaTaskMapper.php index 966f7a5e..d846db57 100644 --- a/lib/Db/TaskMapper.php +++ b/lib/Db/MetaTaskMapper.php @@ -17,21 +17,21 @@ use OCP\IDBConnection; /** - * @extends QBMapper + * @extends QBMapper */ -class TaskMapper extends QBMapper { +class MetaTaskMapper extends QBMapper { public function __construct(IDBConnection $db) { - parent::__construct($db, 'assistant_text_tasks', Task::class); + parent::__construct($db, 'assistant_meta_tasks', MetaTask::class); } /** * @param int $id - * @return Task + * @return MetaTask * @throws DoesNotExistException * @throws Exception * @throws MultipleObjectsReturnedException */ - public function getTask(int $id): Task { + public function getMetaTask(int $id): MetaTask { $qb = $this->db->getQueryBuilder(); $qb->select('*') @@ -46,12 +46,12 @@ public function getTask(int $id): Task { /** * @param int $ocpTaskId * @param int $category - * @return Task + * @return MetaTask * @throws DoesNotExistException * @throws Exception * @throws MultipleObjectsReturnedException */ - public function getTaskByOcpTaskIdAndCategory(int $ocpTaskId, int $category): Task { + public function getMetaTaskByOcpTaskIdAndCategory(int $ocpTaskId, int $category): MetaTask { $qb = $this->db->getQueryBuilder(); $qb->select('*') @@ -80,10 +80,10 @@ public function getTaskByOcpTaskIdAndCategory(int $ocpTaskId, int $category): Ta /** * @param int $ocpTaskId * @param int $category - * @return array + * @return array * @throws Exception */ - public function getTasksByOcpTaskIdAndCategory(int $ocpTaskId, int $category): array { + public function getMetaTasksByOcpTaskIdAndCategory(int $ocpTaskId, int $category): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') @@ -115,12 +115,12 @@ public function getTasksByOcpTaskIdAndCategory(int $ocpTaskId, int $category): a /** * @param int $id * @param string $userId - * @return Task + * @return MetaTask * @throws DoesNotExistException * @throws Exception * @throws MultipleObjectsReturnedException */ - public function getTaskOfUser(int $id, string $userId): Task { + public function getMetaTaskOfUser(int $id, string $userId): MetaTask { $qb = $this->db->getQueryBuilder(); $qb->select('*') @@ -139,7 +139,7 @@ public function getTaskOfUser(int $id, string $userId): Task { * @return array * @throws Exception */ - public function getTasksOfUser(string $userId): array { + public function getMetaTasksOfUser(string $userId): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') @@ -162,18 +162,18 @@ public function getTasksOfUser(string $userId): array { * @param int $status * @param int $category * @param string $identifier - * @return Task + * @return MetaTask * @throws Exception */ - public function createTask( + public function createMetaTask( string $userId, array $inputs, ?string $output, ?int $timestamp = null, ?int $ocpTaskId = null, ?string $taskType = null, ?string $appId = null, int $status = 0, int $category = 0, string $identifier = '' - ): Task { + ): MetaTask { if ($timestamp === null) { $timestamp = (new DateTime())->getTimestamp(); } - $task = new Task(); + $task = new MetaTask(); $task->setUserId($userId); $task->setInputs(json_encode($inputs)); $task->setTimestamp($timestamp); @@ -183,7 +183,7 @@ public function createTask( $task->setAppId($appId); $task->setStatus($status); $task->setCategory($category); - $task->setIndentifer($identifier); + $task->setIdentifier($identifier); return $this->insert($task); } @@ -192,7 +192,7 @@ public function createTask( * @return void * @throws Exception */ - public function deleteUserTasks(string $userId): void { + public function deleteUserMetaTasks(string $userId): void { $qb = $this->db->getQueryBuilder(); $qb->delete($this->getTableName()) ->where( @@ -210,7 +210,7 @@ public function deleteUserTasks(string $userId): void { * @throws Exception * @throws \RuntimeException */ - public function cleanupOldTasks(int $olderThanSeconds = Application::DEFAULT_ASSISTANT_TASK_IDLE_TIME): int { + public function cleanupOldMetaTasks(int $olderThanSeconds = Application::DEFAULT_ASSISTANT_TASK_IDLE_TIME): int { $qb = $this->db->getQueryBuilder(); $qb->delete($this->getTableName()) ->where( diff --git a/lib/Listener/SpeechToText/SpeechToTextResultListener.php b/lib/Listener/SpeechToText/SpeechToTextResultListener.php index 4d75a88a..8c76e9a5 100644 --- a/lib/Listener/SpeechToText/SpeechToTextResultListener.php +++ b/lib/Listener/SpeechToText/SpeechToTextResultListener.php @@ -23,11 +23,10 @@ namespace OCA\TpAssistant\Listener\SpeechToText; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCA\TpAssistant\Service\AssistantService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\IURLGenerator; use OCP\SpeechToText\Events\AbstractTranscriptionEvent; use OCP\SpeechToText\Events\TranscriptionFailedEvent; use OCP\SpeechToText\Events\TranscriptionSuccessfulEvent; @@ -38,10 +37,9 @@ */ class SpeechToTextResultListener implements IEventListener { public function __construct( - private LoggerInterface $logger, - private TaskMapper $taskMapper, + private LoggerInterface $logger, + private MetaTaskMapper $metaTaskMapper, private AssistantService $assistantService, - private IURLGenerator $urlGenerator, ) { } @@ -54,28 +52,28 @@ public function handle(Event $event): void { $transcript = $event->getTranscript(); $file = $event->getFile(); - $tasks = $this->taskMapper->getTasksByOcpTaskIdAndCategory($file->getId(), Application::TASK_CATEGORY_SPEECH_TO_TEXT); + $metaTasks = $this->metaTaskMapper->getMetaTasksByOcpTaskIdAndCategory($file->getId(), Application::TASK_CATEGORY_SPEECH_TO_TEXT); // Find a matching etag: $etag = $file->getEtag(); $assistantTask = null; - foreach ($tasks as $task) { - $taskEtag = $task->getInputsAsArray()['eTag']; - if ($taskEtag === $etag) { - $assistantTask = $task; + foreach ($metaTasks as $metaTask) { + $metaTaskEtag = $metaTask->getInputsAsArray()['eTag']; + if ($metaTaskEtag === $etag) { + $assistantTask = $metaTask; break; } } if ($assistantTask === null) { - $this->logger->error('No assistant task found for speech to text result out of ' . count($tasks) . ' tasks for file ' . $file->getId() . ' with etag ' . $etag); + $this->logger->error('No assistant task found for speech to text result out of ' . count($metaTasks) . ' tasks for file ' . $file->getId() . ' with etag ' . $etag); return; } // Update the meta task with the output and new status $assistantTask->setOutput($transcript); $assistantTask->setStatus(Application::STT_TASK_SUCCESSFUL); - $assistantTask = $this->taskMapper->update($assistantTask); + $assistantTask = $this->metaTaskMapper->update($assistantTask); try { $this->assistantService->sendNotification($assistantTask, null, null, $transcript); @@ -87,15 +85,15 @@ public function handle(Event $event): void { if ($event instanceof TranscriptionFailedEvent) { $this->logger->error('Transcript generation failed: ' . $event->getErrorMessage()); - $tasks = $this->taskMapper->getTasksByOcpTaskIdAndCategory($file->getId(), Application::TASK_CATEGORY_SPEECH_TO_TEXT); + $metaTasks = $this->metaTaskMapper->getMetaTasksByOcpTaskIdAndCategory($file->getId(), Application::TASK_CATEGORY_SPEECH_TO_TEXT); // Find a matching etag: $etag = $file->getEtag(); $assistantTask = null; - foreach ($tasks as $task) { - $taskEtag = $task->getInputsAsArray()['eTag']; - if ($taskEtag === $etag) { - $assistantTask = $task; + foreach ($metaTasks as $metaTask) { + $metaTaskEtag = $metaTask->getInputsAsArray()['eTag']; + if ($metaTaskEtag === $etag) { + $assistantTask = $metaTask; break; } } @@ -107,7 +105,7 @@ public function handle(Event $event): void { // Update the meta task with the new status $assistantTask->setStatus(Application::STT_TASK_FAILED); - $assistantTask = $this->taskMapper->update($assistantTask); + $assistantTask = $this->metaTaskMapper->update($assistantTask); try { $this->assistantService->sendNotification($assistantTask); diff --git a/lib/Listener/TaskFailedListener.php b/lib/Listener/TaskFailedListener.php index 05458ee4..058d76e6 100644 --- a/lib/Listener/TaskFailedListener.php +++ b/lib/Listener/TaskFailedListener.php @@ -3,7 +3,7 @@ namespace OCA\TpAssistant\Listener; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent; use OCA\TpAssistant\Service\AssistantService; use OCP\AppFramework\Db\DoesNotExistException; @@ -20,7 +20,7 @@ class TaskFailedListener implements IEventListener { public function __construct( private AssistantService $assistantService, private IEventDispatcher $eventDispatcher, - private TaskMapper $taskMapper, + private MetaTaskMapper $metaTaskMapper, ) { } @@ -51,7 +51,7 @@ public function handle(Event $event): void { } try { - $assistantTask = $this->taskMapper->getTaskByOcpTaskIdAndCategory($task->getId(), Application::TASK_CATEGORY_TEXT_GEN); + $assistantTask = $this->metaTaskMapper->getMetaTaskByOcpTaskIdAndCategory($task->getId(), Application::TASK_CATEGORY_TEXT_GEN); } catch (DoesNotExistException $e) { // Not an assistant task return; @@ -60,7 +60,7 @@ public function handle(Event $event): void { // Update task status and output: $assistantTask->setStatus($task->getStatus()); $assistantTask->setOutput($task->getOutput()); - $assistantTask = $this->taskMapper->update($assistantTask); + $assistantTask = $this->metaTaskMapper->update($assistantTask); $this->assistantService->sendNotification($assistantTask, $notificationTarget, $notificationActionLabel); } diff --git a/lib/Listener/TaskSuccessfulListener.php b/lib/Listener/TaskSuccessfulListener.php index 7bd026b4..0bf72bc3 100644 --- a/lib/Listener/TaskSuccessfulListener.php +++ b/lib/Listener/TaskSuccessfulListener.php @@ -3,7 +3,7 @@ namespace OCA\TpAssistant\Listener; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent; use OCA\TpAssistant\Service\AssistantService; use OCP\AppFramework\Db\DoesNotExistException; @@ -20,7 +20,7 @@ class TaskSuccessfulListener implements IEventListener { public function __construct( private AssistantService $assistantService, private IEventDispatcher $eventDispatcher, - private TaskMapper $taskMapper, + private MetaTaskMapper $metaTaskMapper, ) { } @@ -51,7 +51,7 @@ public function handle(Event $event): void { } try { - $assistantTask = $this->taskMapper->getTaskByOcpTaskIdAndCategory($task->getId(), Application::TASK_CATEGORY_TEXT_GEN); + $assistantTask = $this->metaTaskMapper->getMetaTaskByOcpTaskIdAndCategory($task->getId(), Application::TASK_CATEGORY_TEXT_GEN); } catch (DoesNotExistException $e) { // Not an assistant task return; @@ -60,7 +60,7 @@ public function handle(Event $event): void { // Update task status and output: $assistantTask->setStatus($task->getStatus()); $assistantTask->setOutput($task->getOutput()); - $assistantTask = $this->taskMapper->update($assistantTask); + $assistantTask = $this->metaTaskMapper->update($assistantTask); $this->assistantService->sendNotification($assistantTask, $notificationTarget, $notificationActionLabel); } diff --git a/lib/Listener/Text2Image/Text2ImageResultListener.php b/lib/Listener/Text2Image/Text2ImageResultListener.php index 0af98ae2..2d42f7ab 100644 --- a/lib/Listener/Text2Image/Text2ImageResultListener.php +++ b/lib/Listener/Text2Image/Text2ImageResultListener.php @@ -3,7 +3,7 @@ namespace OCA\TpAssistant\Listener\Text2Image; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCA\TpAssistant\Db\Text2Image\ImageGenerationMapper; use OCA\TpAssistant\Service\AssistantService; use OCA\TpAssistant\Service\Text2Image\Text2ImageHelperService; @@ -24,10 +24,10 @@ class Text2ImageResultListener implements IEventListener { public function __construct( private Text2ImageHelperService $text2ImageService, - private ImageGenerationMapper $imageGenerationMapper, - private LoggerInterface $logger, - private AssistantService $assistantService, - private TaskMapper $taskMapper, + private ImageGenerationMapper $imageGenerationMapper, + private LoggerInterface $logger, + private AssistantService $assistantService, + private MetaTaskMapper $metaTaskMapper, ) { } @@ -48,7 +48,7 @@ public function handle(Event $event): void { return; } - $assistantTask = $this->taskMapper->getTaskByOcpTaskIdAndCategory($event->getTask()->getId(), Application::TASK_CATEGORY_TEXT_TO_IMAGE); + $assistantTask = $this->metaTaskMapper->getMetaTaskByOcpTaskIdAndCategory($event->getTask()->getId(), Application::TASK_CATEGORY_TEXT_TO_IMAGE); if ($event instanceof TaskSuccessfulEvent) { $this->logger->debug('TextToImageEvent succeeded'); @@ -58,7 +58,7 @@ public function handle(Event $event): void { $this->text2ImageService->storeImages($images, $imageGenId); $assistantTask->setStatus(Task::STATUS_SUCCESSFUL); - $assistantTask = $this->taskMapper->update($assistantTask); + $assistantTask = $this->metaTaskMapper->update($assistantTask); } if ($event instanceof TaskFailedEvent) { @@ -67,7 +67,7 @@ public function handle(Event $event): void { // Update the assistant meta task status: $assistantTask->setStatus(Task::STATUS_FAILED); - $assistantTask = $this->taskMapper->update($assistantTask); + $assistantTask = $this->metaTaskMapper->update($assistantTask); $this->assistantService->sendNotification($assistantTask); } diff --git a/lib/Migration/Version010005Date20240115122933.php b/lib/Migration/Version010004Date20240131182344.php similarity index 78% rename from lib/Migration/Version010005Date20240115122933.php rename to lib/Migration/Version010004Date20240131182344.php index a2c0b4b3..515ad7c1 100644 --- a/lib/Migration/Version010005Date20240115122933.php +++ b/lib/Migration/Version010004Date20240131182344.php @@ -13,7 +13,7 @@ use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; -class Version010005Date20240115122933 extends SimpleMigrationStep { +class Version010004Date20240131182344 extends SimpleMigrationStep { /** * @param IOutput $output * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` @@ -43,9 +43,17 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $schema->dropTable('assistant_stt_transcripts'); } - if (!$schema->hasTable('assistant_text_tasks')) { + if ($schema->hasTable('assistant_text_tasks')) { $schemaChanged = true; - $table = $schema->createTable('assistant_text_tasks'); + $table = $schema->getTable('assistant_text_tasks'); + $table->dropIndex('assistant_t_tasks_uid'); + $table->dropIndex('assistant_t_task_id_category'); + $schema->dropTable('assistant_text_tasks'); + } + + if (!$schema->hasTable('assistant_meta_tasks')) { + $schemaChanged = true; + $table = $schema->createTable('assistant_meta_tasks'); $table->addColumn('id', Types::BIGINT, [ 'autoincrement' => true, 'notnull' => true, @@ -56,6 +64,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt ]); $table->addColumn('app_id', Types::STRING, [ 'notnull' => true, + 'length' => 32, ]); $table->addColumn('inputs', Types::TEXT, [ 'notnull' => false, @@ -72,6 +81,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt ]); $table->addColumn('task_type', Types::STRING, [ 'notnull' => false, + 'length' => 255, ]); $table->addColumn('status', Types::INTEGER, [ 'notnull' => true, @@ -80,12 +90,13 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table->addColumn('category', Types::INTEGER, [ 'notnull' => false, ]); - $table->addColumn('indentifer', Types::STRING, [ + $table->addColumn('identifier', Types::STRING, [ 'notnull' => false, + 'length' => 255, ]); $table->setPrimaryKey(['id']); - $table->addIndex(['user_id'], 'assistant_t_tasks_uid'); - $table->addIndex(['ocp_task_id','category'], 'assistant_t_task_id_category'); + $table->addIndex(['user_id'], 'assistant_meta_task_uid'); + $table->addIndex(['ocp_task_id','category'], 'assistant_meta_task_id_cat'); } return $schemaChanged ? $schema : null; diff --git a/lib/Service/AssistantService.php b/lib/Service/AssistantService.php index 44397825..35c36309 100644 --- a/lib/Service/AssistantService.php +++ b/lib/Service/AssistantService.php @@ -6,12 +6,13 @@ use DateTime; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\Task; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTask; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCA\TpAssistant\Db\Text2Image\ImageGenerationMapper; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\Common\Exception\NotFoundException; +use OCP\DB\Exception; use OCP\Files\File; use OCP\Files\GenericFileException; use OCP\Files\IRootFolder; @@ -32,26 +33,26 @@ class AssistantService { public function __construct( - private INotificationManager $notificationManager, + private INotificationManager $notificationManager, private ITextProcessingManager $textProcessingManager, - private TaskMapper $taskMapper, - private ImageGenerationMapper $imageGenerationMapper, - private LoggerInterface $logger, - private IRootFolder $storage, - private IURLGenerator $url, + private MetaTaskMapper $metaTaskMapper, + private ImageGenerationMapper $imageGenerationMapper, + private LoggerInterface $logger, + private IRootFolder $storage, + private IURLGenerator $url, ) { } /** * Send a success or failure task result notification * - * @param Task $task + * @param MetaTask $task * @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 - * @throws \InvalidArgumentException */ - public function sendNotification(Task $task, ?string $customTarget = null, ?string $actionLabel = null, ?string $resultPreview = null): void { + public function sendNotification(MetaTask $task, ?string $customTarget = null, ?string $actionLabel = null, ?string $resultPreview = null): void { $manager = $this->notificationManager; $notification = $manager->createNotification(); @@ -106,14 +107,14 @@ public function sendNotification(Task $task, ?string $customTarget = null, ?stri $manager->notify($notification); } - private function getDefaultTarget(Task $task): string { + private function getDefaultTarget(MetaTask $task): string { $category = $task->getCategory(); if ($category === Application::TASK_CATEGORY_TEXT_GEN) { return $this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getTextProcessingTaskResultPage', ['taskId' => $task->getId()]); } elseif ($category === Application::TASK_CATEGORY_SPEECH_TO_TEXT) { return $this->url->linkToRouteAbsolute(Application::APP_ID . '.SpeechToText.getResultPage', ['id' => $task->getId()]); } elseif ($category === Application::TASK_CATEGORY_TEXT_TO_IMAGE) { - $imageGeneration = $this->imageGenerationMapper->getImageGenerationOfImageGenId($task->getIndentifer()); + $imageGeneration = $this->imageGenerationMapper->getImageGenerationOfImageGenId($task->getIdentifier()); return $this->url->linkToRouteAbsolute( Application::APP_ID . '.Text2Image.showGenerationPage', [ @@ -168,48 +169,48 @@ private function sanitizeInputs(string $type, array $inputs): array { /** * @param string $userId * @param int $taskId - * @return Task + * @return MetaTask|null */ - public function getTextProcessingTask(string $userId, int $taskId): ?Task { + public function getTextProcessingTask(string $userId, int $taskId): ?MetaTask { try { - $task = $this->taskMapper->getTask($taskId); + $metaTask = $this->metaTaskMapper->getMetaTask($taskId); } catch (DoesNotExistException | MultipleObjectsReturnedException | \OCP\Db\Exception $e) { return null; } - if ($task->getUserId() !== $userId) { + if ($metaTask->getUserId() !== $userId) { return null; } // Check if the task status is up-to-date (if not, update status and output) try { - $ocpTask = $this->textProcessingManager->getTask($task->getOcpTaskId()); + $ocpTask = $this->textProcessingManager->getTask($metaTask->getOcpTaskId()); - if($ocpTask->getStatus() !== $task->getStatus()) { - $task->setStatus($ocpTask->getStatus()); - $task->setOutput($ocpTask->getOutput()); - $task = $this->taskMapper->update($task); + if($ocpTask->getStatus() !== $metaTask->getStatus()) { + $metaTask->setStatus($ocpTask->getStatus()); + $metaTask->setOutput($ocpTask->getOutput()); + $metaTask = $this->metaTaskMapper->update($metaTask); } } catch (NotFoundException $e) { // Ocp task not found, so we can't update the status - $this->logger->debug('OCP task not found for assistant task ' . $task->getId() . '. Could not update status.'); + $this->logger->debug('OCP task not found for assistant task ' . $metaTask->getId() . '. Could not update status.'); } catch (\InvalidArgumentException | \OCP\Db\Exception | RuntimeException $e) { // Something else went wrong, so we can't update the status - $this->logger->warning('Unknown error while trying to retreive an updated status for assistant task: ' . $task->getId() . '.', ['exception' => $e]); + $this->logger->warning('Unknown error while trying to retreive an updated status for assistant task: ' . $metaTask->getId() . '.', ['exception' => $e]); } - return $task; + return $metaTask; } /** * @param string $type - * @param array $input + * @param array $inputs * @param string $appId * @param string $userId * @param string $identifier - * @return Task + * @return MetaTask * @throws PreConditionNotMetException - * @throws \Exception + * @throws Exception */ - public function runTextProcessingTask(string $type, array $inputs, string $appId, string $userId, string $identifier): Task { + public function runTextProcessingTask(string $type, array $inputs, string $appId, string $userId, string $identifier): MetaTask { $inputs = $this->sanitizeInputs($type, $inputs); switch ($type) { case 'copywriter': @@ -229,22 +230,23 @@ public function runTextProcessingTask(string $type, array $inputs, string $appId } } - $assistantTask = $this->taskMapper->createTask($userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, $appId, $task->getStatus(), Application::TASK_CATEGORY_TEXT_GEN, $identifier); - - return $assistantTask; + return $this->metaTaskMapper->createMetaTask( + $userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, + $appId, $task->getStatus(), Application::TASK_CATEGORY_TEXT_GEN, $identifier + ); } /** * @param string $type - * @param array $input + * @param array $inputs * @param string $appId * @param string $userId * @param string $identifier - * @return Task + * @return MetaTask + * @throws Exception * @throws PreConditionNotMetException - * @throws \Exception */ - public function scheduleTextProcessingTask(string $type, array $inputs, string $appId, string $userId, string $identifier): Task { + public function scheduleTextProcessingTask(string $type, array $inputs, string $appId, string $userId, string $identifier): MetaTask { $inputs = $this->sanitizeInputs($type, $inputs); switch ($type) { case 'copywriter': @@ -264,9 +266,10 @@ public function scheduleTextProcessingTask(string $type, array $inputs, string $ } } - $assistantTask = $this->taskMapper->createTask($userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, $appId, $task->getStatus(), Application::TASK_CATEGORY_TEXT_GEN, $identifier); - - return $assistantTask; + return $this->metaTaskMapper->createMetaTask( + $userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, + $appId, $task->getStatus(), Application::TASK_CATEGORY_TEXT_GEN, $identifier + ); } /** @@ -275,12 +278,12 @@ public function scheduleTextProcessingTask(string $type, array $inputs, string $ * @param string $appId * @param string $userId * @param string $identifier - * @return Task + * @return MetaTask * @throws PreConditionNotMetException * @throws \OCP\Db\Exception * @throws \Exception */ - public function runOrScheduleTextProcessingTask(string $type, array $inputs, string $appId, string $userId, string $identifier): Task { + public function runOrScheduleTextProcessingTask(string $type, array $inputs, string $appId, string $userId, string $identifier): MetaTask { $inputs = $this->sanitizeInputs($type, $inputs); switch ($type) { case 'copywriter': @@ -300,9 +303,10 @@ public function runOrScheduleTextProcessingTask(string $type, array $inputs, str } } - $assistantTask = $this->taskMapper->createTask($userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, $appId, $task->getStatus(), Application::TASK_CATEGORY_TEXT_GEN, $identifier); - - return $assistantTask; + return $this->metaTaskMapper->createMetaTask( + $userId, $inputs, $task->getOutput(), time(), $task->getId(), $type, + $appId, $task->getStatus(), Application::TASK_CATEGORY_TEXT_GEN, $identifier + ); } /** @@ -381,6 +385,7 @@ public function parseTextFromFile(string $filePath, string $userId): string { /** * Parse text from doc/docx/odt/rtf file * @param string $filePath + * @param string $mimeType * @return string * @throws \Exception */ diff --git a/lib/Service/FreePrompt/FreePromptService.php b/lib/Service/FreePrompt/FreePromptService.php index 7aeadc04..2f7e97aa 100644 --- a/lib/Service/FreePrompt/FreePromptService.php +++ b/lib/Service/FreePrompt/FreePromptService.php @@ -8,10 +8,9 @@ use Exception; use OCA\TpAssistant\AppInfo\Application; use OCA\TpAssistant\Db\FreePrompt\PromptMapper; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCP\AppFramework\Http; use OCP\DB\Exception as DBException; -use OCP\IConfig; use OCP\IL10N; use OCP\PreConditionNotMetException; use OCP\TextProcessing\Exception\TaskFailureException; @@ -22,12 +21,11 @@ class FreePromptService { public function __construct( - private IConfig $config, private LoggerInterface $logger, - private IManager $textProcessingManager, - private PromptMapper $promptMapper, - private IL10N $l10n, - private TaskMapper $taskMapper, + private IManager $textProcessingManager, + private PromptMapper $promptMapper, + private IL10N $l10n, + private MetaTaskMapper $metaTaskMapper, ) { } @@ -43,7 +41,7 @@ public function processPrompt(string $prompt, string $userId): string { $this->logger->warning('FreePromptTaskType not available'); throw new Exception($this->l10n->t('FreePromptTaskType not available'), Http::STATUS_INTERNAL_SERVER_ERROR); } - + // Generate a unique id for this generation while (true) { $genId = bin2hex(random_bytes(32)); @@ -66,7 +64,7 @@ public function processPrompt(string $prompt, string $userId): string { } // Create an assistant task for the free prompt task: - $this->taskMapper->createTask( + $this->metaTaskMapper->createMetaTask( $userId, ['prompt' => $prompt], $promptTask->getOutput(), @@ -84,7 +82,7 @@ public function processPrompt(string $prompt, string $userId): string { // Save prompt to database $this->promptMapper->createPrompt($userId, $prompt); - + return $genId; } @@ -101,7 +99,7 @@ public function getPromptHistory(string $userId): array { throw new Exception($this->l10n->t('Failed to get prompt history'), Http::STATUS_INTERNAL_SERVER_ERROR); } } - + /** * @param string $genId * @param string $userId @@ -110,12 +108,12 @@ public function getPromptHistory(string $userId): array { */ public function getOutputs(string $genId, string $userId): array { $tasks = $this->textProcessingManager->getUserTasksByApp($userId, Application::APP_ID, $genId); - + if (count($tasks) === 0) { $this->logger->warning('No tasks found for gen id: ' . $genId); throw new Exception($this->l10n->t('Generation not found'), Http::STATUS_BAD_REQUEST); } - + $outputs = []; /** @var Task $task */ foreach ($tasks as $task) { diff --git a/lib/Service/SpeechToText/SpeechToTextService.php b/lib/Service/SpeechToText/SpeechToTextService.php index 1e92e28a..67215331 100644 --- a/lib/Service/SpeechToText/SpeechToTextService.php +++ b/lib/Service/SpeechToText/SpeechToTextService.php @@ -25,30 +25,24 @@ use DateTime; use InvalidArgumentException; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\IConfig; -use OCP\IURLGenerator; -use OCP\Notification\IManager as INotifyManager; use OCP\PreConditionNotMetException; use OCP\SpeechToText\ISpeechToTextManager; -use Psr\Log\LoggerInterface; use RuntimeException; class SpeechToTextService { public function __construct( private ISpeechToTextManager $manager, - private IRootFolder $rootFolder, - private INotifyManager $notificationManager, - private IURLGenerator $urlGenerator, - private LoggerInterface $logger, - private IConfig $config, - private TaskMapper $taskMapper, + private IRootFolder $rootFolder, + private IConfig $config, + private MetaTaskMapper $metaTaskMapper, ) { } @@ -71,8 +65,8 @@ public function transcribeFile(string $path, ?string $userId): void { $audioFile = $userFolder->get($path); $this->manager->scheduleFileTranscription($audioFile, $userId, Application::APP_ID); - - $this->taskMapper->createTask( + + $this->metaTaskMapper->createMetaTask( $userId, ['fileId' => $audioFile->getId(), 'eTag' => $audioFile->getEtag()], '', @@ -98,10 +92,10 @@ public function transcribeAudio(string $tempFileLocation, ?string $userId): void } $audioFile = $this->getFileObject($userId, $tempFileLocation); - + $this->manager->scheduleFileTranscription($audioFile, $userId, Application::APP_ID); - $this->taskMapper->createTask( + $this->metaTaskMapper->createMetaTask( $userId, ['fileId' => $audioFile->getId(), 'eTag' => $audioFile->getEtag()], '', diff --git a/lib/Service/Text2Image/Text2ImageHelperService.php b/lib/Service/Text2Image/Text2ImageHelperService.php index faedb333..eb7840e2 100644 --- a/lib/Service/Text2Image/Text2ImageHelperService.php +++ b/lib/Service/Text2Image/Text2ImageHelperService.php @@ -10,7 +10,7 @@ use GdImage; use OCA\TpAssistant\AppInfo\Application; -use OCA\TpAssistant\Db\TaskMapper; +use OCA\TpAssistant\Db\MetaTaskMapper; use OCA\TpAssistant\Db\Text2Image\ImageFileName; use OCA\TpAssistant\Db\Text2Image\ImageFileNameMapper; use OCA\TpAssistant\Db\Text2Image\ImageGeneration; @@ -43,17 +43,17 @@ class Text2ImageHelperService { private ?ISimpleFolder $imageDataFolder = null; public function __construct( - private LoggerInterface $logger, - private IManager $textToImageManager, - private PromptMapper $promptMapper, + private LoggerInterface $logger, + private IManager $textToImageManager, + private PromptMapper $promptMapper, private ImageGenerationMapper $imageGenerationMapper, - private ImageFileNameMapper $imageFileNameMapper, + private ImageFileNameMapper $imageFileNameMapper, private StaleGenerationMapper $staleGenerationMapper, - private IAppData $appData, - private IURLGenerator $urlGenerator, - private IL10N $l10n, - private AssistantService $assistantService, - private TaskMapper $taskMapper, + private IAppData $appData, + private IURLGenerator $urlGenerator, + private IL10N $l10n, + private AssistantService $assistantService, + private MetaTaskMapper $metaTaskMapper, ) { } @@ -105,7 +105,7 @@ public function processPrompt(string $prompt, int $nResults, bool $displayPrompt // Create an assistant meta task for the image generation task: // TODO check if we should create a task if userId is null - $this->taskMapper->createTask( + $this->metaTaskMapper->createMetaTask( $userId, ['prompt' => $prompt], $imageGenId, @@ -563,14 +563,14 @@ public function notifyWhenReady(string $imageGenId, string $userId): void { if ($task->getStatus() === Task::STATUS_SUCCESSFUL || $task->getStatus() === Task::STATUS_FAILED) { // Get the assistant task try { - $assistantTask = $this->taskMapper->getTaskByOcpTaskIdAndCategory($task->getId(), Application::TASK_CATEGORY_TEXT_TO_IMAGE); + $assistantTask = $this->metaTaskMapper->getMetaTaskByOcpTaskIdAndCategory($task->getId(), Application::TASK_CATEGORY_TEXT_TO_IMAGE); } catch (Exception | DoesNotExistException | MultipleObjectsReturnedException $e) { $this->logger->debug('Assistant meta task for the given generation id does not exist or could not be retrieved: ' . $e->getMessage(), ['app' => Application::APP_ID]); return; } $assistantTask->setStatus($task->getStatus()); // No need to update the output since it's already set - $assistantTask = $this->taskMapper->update($assistantTask); + $assistantTask = $this->metaTaskMapper->update($assistantTask); $this->assistantService->sendNotification($assistantTask); }