Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ai message summary #10465

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
use OCA\Mail\Listener\MessageKnownSinceListener;
use OCA\Mail\Listener\MoveJunkListener;
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 @@ -71,6 +73,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 @@ -131,6 +134,7 @@
$context->registerEventListener(MessageSentEvent::class, InteractionListener::class);
$context->registerEventListener(NewMessagesSynchronized::class, MessageKnownSinceListener::class);
$context->registerEventListener(NewMessagesSynchronized::class, NewMessagesNotifier::class);
$context->registerEventListener(NewMessagesSynchronized::class, NewMessagesSummarizeListener::class);

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

View check run for this annotation

Codecov / codecov/patch

lib/AppInfo/Application.php#L137

Added line #L137 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 @@ -139,6 +143,7 @@
$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 146 in lib/AppInfo/Application.php

View check run for this annotation

Codecov / codecov/patch

lib/AppInfo/Application.php#L146

Added line #L146 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 @@
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 @@
'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
}
}
99 changes: 79 additions & 20 deletions lib/Service/AiIntegrations/AiIntegrationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,106 @@
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
);
// skip message if it is encrypted
if ($message->isEncrypted()) {
continue;
}
// construct prompt and task
$messageBody = $message->getPlainBody();
$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();
ChristophWurst marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* @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.

2 changes: 1 addition & 1 deletion package.json
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,
Expand Down
Loading
Loading