-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10465 from nextcloud/feat/ai-summery
feat: ai message summary
- Loading branch information
Showing
10 changed files
with
467 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Listener; | ||
|
||
use OCA\Mail\Contracts\IMailManager; | ||
use OCA\Mail\Events\NewMessagesSynchronized; | ||
use OCA\Mail\Exception\ServiceException; | ||
use OCA\Mail\IMAP\IMAPClientFactory; | ||
use OCA\Mail\Service\AiIntegrations\AiIntegrationsService; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @template-implements IEventListener<Event> | ||
*/ | ||
class NewMessagesSummarizeListener implements IEventListener { | ||
|
||
public function __construct( | ||
private LoggerInterface $logger, | ||
private IMAPClientFactory $imapFactory, | ||
private AiIntegrationsService $aiService, | ||
private IMailManager $mailManager, | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
|
||
if (!($event instanceof NewMessagesSynchronized)) { | ||
return; | ||
} | ||
|
||
try { | ||
$this->aiService->summarizeMessages( | ||
$event->getAccount(), | ||
$event->getMessages(), | ||
); | ||
} catch (ServiceException $e) { | ||
$this->logger->error('Could not initiate a message summarize task(s): ' . $e->getMessage(), [ | ||
'exception' => $e, | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Listener; | ||
|
||
use OCA\Mail\AppInfo\Application; | ||
use OCA\Mail\Db\MessageMapper; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\TaskProcessing\Events\TaskSuccessfulEvent; | ||
use OCP\TaskProcessing\TaskTypes\TextToTextSummary; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @template-implements IEventListener<Event> | ||
*/ | ||
class TaskProcessingListener implements IEventListener { | ||
|
||
public function __construct( | ||
private LoggerInterface $logger, | ||
private MessageMapper $messageMapper, | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
|
||
if (!($event instanceof TaskSuccessfulEvent)) { | ||
return; | ||
} | ||
|
||
$task = $event->getTask(); | ||
|
||
if ($task->getAppId() !== Application::APP_ID) { | ||
return; | ||
} | ||
|
||
if ($task->getTaskTypeId() !== TextToTextSummary::ID) { | ||
return; | ||
} | ||
|
||
if ($task->getCustomId() !== null) { | ||
[$type, $id] = explode(':', $task->getCustomId()); | ||
} else { | ||
$this->logger->info('Error handling task processing event custom id missing', ['taskCustomId' => $task->getCustomId()]); | ||
return; | ||
} | ||
if ($type === null || $id === null) { | ||
$this->logger->info('Error handling task processing event custom id is invalid', ['taskCustomId' => $task->getCustomId()]); | ||
return; | ||
} | ||
if ($task->getUserId() !== null) { | ||
$userId = $task->getUserId(); | ||
} else { | ||
$this->logger->info('Error handling task processing event user id missing'); | ||
return; | ||
} | ||
if ($task->getOutput() !== null) { | ||
$output = $task->getOutput(); | ||
if (isset($output['output']) && is_string($output['output'])) { | ||
$summary = $output['output']; | ||
} else { | ||
$this->logger->info('Error handling task processing event output is invalid', ['taskOutput' => $output]); | ||
return; | ||
} | ||
} else { | ||
$this->logger->info('Error handling task processing event output missing'); | ||
return; | ||
} | ||
|
||
if ($type === 'message') { | ||
$this->handleMessageSummary($userId, (int)$id, $summary); | ||
} | ||
|
||
} | ||
|
||
private function handleMessageSummary(string $userId, int $id, string $summary): void { | ||
$messages = $this->messageMapper->findByIds($userId, [$id], ''); | ||
|
||
if (count($messages) !== 1) { | ||
return; | ||
} | ||
|
||
$message = $messages[0]; | ||
$message->setSummary(substr($summary, 0, 1024)); | ||
$this->messageMapper->update($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\DB\Types; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version4100Date20241209000000 extends SimpleMigrationStep { | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
$schema = $schemaClosure(); | ||
|
||
$outboxTable = $schema->getTable('mail_messages'); | ||
if (!$outboxTable->hasColumn('summary')) { | ||
$outboxTable->addColumn('summary', Types::STRING, [ | ||
'length' => 1024, | ||
'notnull' => false, | ||
]); | ||
} | ||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "nextcloud-mail", | ||
"description": "Nextcloud Mail", | ||
"version": "4.2.0-alpha.0", | ||
"version": "4.2.0-alpha1", | ||
"author": "Christoph Wurst <[email protected]>", | ||
"license": "agpl", | ||
"private": true, | ||
|
Oops, something went wrong.