Skip to content

Commit

Permalink
feat: ai message summary
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>

Signed-off-by: Grigory Vodyanov <[email protected]>
  • Loading branch information
SebastianKrupinski committed Dec 18, 2024
1 parent 549a138 commit d90e167
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 51 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The rating depends on the installed text processing backend. See [the rating ove
Learn more about the Nextcloud Ethical AI Rating [in our blog](https://nextcloud.com/blog/nextcloud-ethical-ai-rating/).
]]></description>
<version>4.2.0-alpha.0</version>
<version>4.2.0-alpha.1</version>
<licence>agpl</licence>
<author homepage="https://github.com/ChristophWurst">Christoph Wurst</author>
<author homepage="https://github.com/GretaD">GretaD</author>
Expand Down
5 changes: 5 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
use OCA\Mail\Listener\MoveJunkListener;
use OCA\Mail\Listener\NewMessageClassificationListener;
use OCA\Mail\Listener\NewMessagesNotifier;
use OCA\Mail\Listener\NewMessagesSummarizeListener;
use OCA\Mail\Listener\OauthTokenRefreshListener;
use OCA\Mail\Listener\OptionalIndicesListener;
use OCA\Mail\Listener\OutOfOfficeListener;
use OCA\Mail\Listener\SpamReportListener;
use OCA\Mail\Listener\TaskProcessingListener;
use OCA\Mail\Listener\UserDeletedListener;
use OCA\Mail\Notification\Notifier;
use OCA\Mail\Provider\MailProvider;
Expand All @@ -72,6 +74,7 @@
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\DB\Events\AddMissingIndicesEvent;
use OCP\IServerContainer;
use OCP\TaskProcessing\Events\TaskSuccessfulEvent;
use OCP\User\Events\OutOfOfficeChangedEvent;
use OCP\User\Events\OutOfOfficeClearedEvent;
use OCP\User\Events\OutOfOfficeEndedEvent;
Expand Down Expand Up @@ -133,6 +136,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(NewMessagesSynchronized::class, NewMessageClassificationListener::class);
$context->registerEventListener(NewMessagesSynchronized::class, MessageKnownSinceListener::class);
$context->registerEventListener(NewMessagesSynchronized::class, NewMessagesNotifier::class);
$context->registerEventListener(NewMessagesSynchronized::class, NewMessagesSummarizeListener::class);

Check warning on line 139 in lib/AppInfo/Application.php

View check run for this annotation

Codecov / codecov/patch

lib/AppInfo/Application.php#L139

Added line #L139 was not covered by tests
$context->registerEventListener(SynchronizationEvent::class, AccountSynchronizedThreadUpdaterListener::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(NewMessagesSynchronized::class, FollowUpClassifierListener::class);
Expand All @@ -141,6 +145,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(OutOfOfficeChangedEvent::class, OutOfOfficeListener::class);
$context->registerEventListener(OutOfOfficeClearedEvent::class, OutOfOfficeListener::class);
$context->registerEventListener(OutOfOfficeScheduledEvent::class, OutOfOfficeListener::class);
$context->registerEventListener(TaskSuccessfulEvent::class, TaskProcessingListener::class);

Check warning on line 148 in lib/AppInfo/Application.php

View check run for this annotation

Codecov / codecov/patch

lib/AppInfo/Application.php#L148

Added line #L148 was not covered by tests

$context->registerMiddleWare(ErrorMiddleware::class);
$context->registerMiddleWare(ProvisioningMiddleware::class);
Expand Down
4 changes: 4 additions & 0 deletions lib/Db/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
* @method bool|null getFlagMdnsent()
* @method void setPreviewText(?string $subject)
* @method null|string getPreviewText()
* @method void setSummary(?string $summary)
* @method null|string getSummary()
* @method void setUpdatedAt(int $time)
* @method int getUpdatedAt()
* @method bool isImipMessage()
Expand Down Expand Up @@ -108,6 +110,7 @@ class Message extends Entity implements JsonSerializable {
protected $flagImportant = false;
protected $flagMdnsent;
protected $previewText;
protected $summary;
protected $imipMessage = false;
protected $imipProcessed = false;
protected $imipError = false;
Expand Down Expand Up @@ -325,6 +328,7 @@ static function (Tag $tag) {
'threadRootId' => $this->getThreadRootId(),
'imipMessage' => $this->isImipMessage(),
'previewText' => $this->getPreviewText(),
'summary' => $this->getSummary(),

Check warning on line 331 in lib/Db/Message.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/Message.php#L331

Added line #L331 was not covered by tests
'encrypted' => ($this->isEncrypted() === true),
'mentionsMe' => $this->getMentionsMe(),
];
Expand Down
51 changes: 51 additions & 0 deletions lib/Listener/NewMessagesSummarizeListener.php
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;

Check warning on line 37 in lib/Listener/NewMessagesSummarizeListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/NewMessagesSummarizeListener.php#L37

Added line #L37 was not covered by tests
}

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,
]);

Check warning on line 48 in lib/Listener/NewMessagesSummarizeListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/NewMessagesSummarizeListener.php#L45-L48

Added lines #L45 - L48 were not covered by tests
}
}
}
93 changes: 93 additions & 0 deletions lib/Listener/TaskProcessingListener.php
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(

Check warning on line 25 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L25

Added line #L25 was not covered by tests
private LoggerInterface $logger,
private MessageMapper $messageMapper,
) {
}

Check warning on line 29 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L29

Added line #L29 was not covered by tests

public function handle(Event $event): void {

Check warning on line 31 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L31

Added line #L31 was not covered by tests

if (!($event instanceof TaskSuccessfulEvent)) {
return;

Check warning on line 34 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L33-L34

Added lines #L33 - L34 were not covered by tests
}

$task = $event->getTask();

Check warning on line 37 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L37

Added line #L37 was not covered by tests

if ($task->getAppId() !== Application::APP_ID) {
return;

Check warning on line 40 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L39-L40

Added lines #L39 - L40 were not covered by tests
}

if ($task->getTaskTypeId() !== TextToTextSummary::ID) {
return;

Check warning on line 44 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L43-L44

Added lines #L43 - L44 were not covered by tests
}

if ($task->getCustomId() !== null) {
[$type, $id] = explode(':', $task->getCustomId());

Check warning on line 48 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L47-L48

Added lines #L47 - L48 were not covered by tests
} else {
$this->logger->info('Error handling task processing event custom id missing', ['taskCustomId' => $task->getCustomId()]);
return;

Check warning on line 51 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L50-L51

Added lines #L50 - L51 were not covered by tests
}
if ($type === null || $id === null) {
$this->logger->info('Error handling task processing event custom id is invalid', ['taskCustomId' => $task->getCustomId()]);
return;

Check warning on line 55 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L53-L55

Added lines #L53 - L55 were not covered by tests
}
if ($task->getUserId() !== null) {
$userId = $task->getUserId();

Check warning on line 58 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L57-L58

Added lines #L57 - L58 were not covered by tests
} else {
$this->logger->info('Error handling task processing event user id missing');
return;

Check warning on line 61 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L60-L61

Added lines #L60 - L61 were not covered by tests
}
if ($task->getOutput() !== null) {
$output = $task->getOutput();
if (isset($output['output']) && is_string($output['output'])) {
$summary = $output['output'];

Check warning on line 66 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L63-L66

Added lines #L63 - L66 were not covered by tests
} else {
$this->logger->info('Error handling task processing event output is invalid', ['taskOutput' => $output]);
return;

Check warning on line 69 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L68-L69

Added lines #L68 - L69 were not covered by tests
}
} else {
$this->logger->info('Error handling task processing event output missing');
return;

Check warning on line 73 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L72-L73

Added lines #L72 - L73 were not covered by tests
}

if ($type === 'message') {
$this->handleMessageSummary($userId, (int)$id, $summary);

Check warning on line 77 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L76-L77

Added lines #L76 - L77 were not covered by tests
}

}

private function handleMessageSummary(string $userId, int $id, string $summary): void {
$messages = $this->messageMapper->findByIds($userId, [$id], '');

Check warning on line 83 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L82-L83

Added lines #L82 - L83 were not covered by tests

if (count($messages) !== 1) {
return;

Check warning on line 86 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L85-L86

Added lines #L85 - L86 were not covered by tests
}

$message = $messages[0];
$message->setSummary(substr($summary, 0, 1024));
$this->messageMapper->update($message);

Check warning on line 91 in lib/Listener/TaskProcessingListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/TaskProcessingListener.php#L89-L91

Added lines #L89 - L91 were not covered by tests
}
}
38 changes: 38 additions & 0 deletions lib/Migration/Version4100Date20241209000000.php
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();

Check warning on line 27 in lib/Migration/Version4100Date20241209000000.php

View check run for this annotation

Codecov / codecov/patch

lib/Migration/Version4100Date20241209000000.php#L26-L27

Added lines #L26 - L27 were not covered by tests

$outboxTable = $schema->getTable('mail_messages');
if (!$outboxTable->hasColumn('summary')) {
$outboxTable->addColumn('summary', Types::STRING, [
'length' => 1024,
'notnull' => false,
]);

Check warning on line 34 in lib/Migration/Version4100Date20241209000000.php

View check run for this annotation

Codecov / codecov/patch

lib/Migration/Version4100Date20241209000000.php#L29-L34

Added lines #L29 - L34 were not covered by tests
}
return $schema;

Check warning on line 36 in lib/Migration/Version4100Date20241209000000.php

View check run for this annotation

Codecov / codecov/patch

lib/Migration/Version4100Date20241209000000.php#L36

Added line #L36 was not covered by tests
}
}
95 changes: 75 additions & 20 deletions lib/Service/AiIntegrations/AiIntegrationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,102 @@
use OCA\Mail\Model\EventData;
use OCA\Mail\Model\IMAPMessage;
use OCP\IConfig;
use OCP\TaskProcessing\Exception\Exception as TaskProcessingException;
use OCP\TaskProcessing\IManager as TaskProcessingManager;
use OCP\TaskProcessing\Task as TaskProcessingTask;
use OCP\TaskProcessing\TaskTypes\TextToTextSummary;
use OCP\TextProcessing\FreePromptTaskType;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\SummaryTaskType;
use OCP\TextProcessing\Task;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

use function array_map;
use function implode;
use function in_array;
use function json_decode;

class AiIntegrationsService {

/** @var ContainerInterface */
private ContainerInterface $container;

/** @var Cache */
private Cache $cache;

/** @var IMAPClientFactory */
private IMAPClientFactory $clientFactory;

/** @var IMailManager */
private IMailManager $mailManager;

private IConfig $config;

private const EVENT_DATA_PROMPT_PREAMBLE = <<<PROMPT
I am scheduling an event based on an email thread and need an event title and agenda. Provide the result as JSON with keys for "title" and "agenda". For example ```{ "title": "Project kick-off meeting", "agenda": "* Introduction\\n* Project goals\\n* Next steps" }```.
The email contents are:
PROMPT;

public function __construct(ContainerInterface $container, Cache $cache, IMAPClientFactory $clientFactory, IMailManager $mailManager, IConfig $config) {
$this->container = $container;
$this->cache = $cache;
$this->clientFactory = $clientFactory;
$this->mailManager = $mailManager;
$this->config = $config;
public function __construct(
private ContainerInterface $container,
private LoggerInterface $logger,
private IConfig $config,
private Cache $cache,
private IMAPClientFactory $clientFactory,
private IMailManager $mailManager,
private TaskProcessingManager $taskProcessingManager,
) {
}

/**
* generates summary for each message
*
* @param Account $account
* @param array<Message> $messages
*
* @return void
*/
public function summarizeMessages(Account $account, array $messages): void {
try {
$this->taskProcessingManager->getPreferredProvider(TextToTextSummary::ID);
} catch (TaskProcessingException $e) {
$this->logger->info('No text summary provider available');
return;
}

$client = $this->clientFactory->getClient($account);
try {
foreach ($messages as $entry) {

if (mb_strlen((string)$entry->getSummary()) !== 0) {
continue;
}
// retrieve full message from server
$userId = $account->getUserId();
$mailboxId = $entry->getMailboxId();
$messageLocalId = $entry->getId();
$messageRemoteId = $entry->getUid();
$mailbox = $this->mailManager->getMailbox($userId, $mailboxId);
$message = $this->mailManager->getImapMessage(
$client,
$account,
$mailbox,
$messageRemoteId,
true
);
$messageBody = $message->getPlainBody();
// construct prompt and task
$prompt = "You are tasked with formulating a helpful summary of a email message. \r\n" .
"The summary should be less than 1024 characters. \r\n" .
"Here is the ***E-MAIL*** for which you must generate a helpful summary: \r\n" .
"***START_OF_E-MAIL***\r\n$messageBody\r\n***END_OF_E-MAIL***\r\n";
$task = new TaskProcessingTask(
TextToTextSummary::ID,
[
'max_tokens' => 1024,
'input' => $prompt,
],
Application::APP_ID,
$userId,
'message:' . (string)$messageLocalId
);
$this->taskProcessingManager->scheduleTask($task);
}
} finally {
$client->logout();
}
}

/**
* @param Account $account
* @param string $threadId
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d90e167

Please sign in to comment.