Skip to content

Commit

Permalink
Fix: use message bodies for thread summary
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <[email protected]>
  • Loading branch information
hamza221 committed Oct 27, 2023
1 parent 21db406 commit 28ab9ca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/Controller/ThreadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public function summarize(int $id): JSONResponse {
$thread = $this->mailManager->getThread($account, $message->getThreadRootId());
try {
$summary = $this->aiIntergrationsService->summarizeThread(
$account,
$mailbox,
$message->getThreadRootId(),
$thread,
$this->currentUserId,
Expand Down
35 changes: 29 additions & 6 deletions lib/Service/AiIntegrations/AiIntegrationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@

namespace OCA\Mail\Service\AiIntegrations;

use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\SummaryTaskType;
use OCP\TextProcessing\Task;
Expand All @@ -38,10 +42,18 @@ class AiIntegrationsService {
/** @var Cache */
private Cache $cache;

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

public function __construct(ContainerInterface $container, Cache $cache) {
/** @var IMailManager */
private IMailManager $mailManager;


public function __construct(ContainerInterface $container, Cache $cache, IMAPClientFactory $clientFactory, IMailManager $mailManager) {
$this->container = $container;
$this->cache = $cache;
$this->clientFactory = $clientFactory;
$this->mailManager = $mailManager;
}
/**
* @param string $threadId
Expand All @@ -52,24 +64,35 @@ public function __construct(ContainerInterface $container, Cache $cache) {
*
* @throws ServiceException
*/
public function summarizeThread(string $threadId, array $messages, string $currentUserId): null|string {
public function summarizeThread(Account $account, Mailbox $mailbox, $threadId, array $messages, string $currentUserId): null|string {
try {
$manager = $this->container->get(IManager::class);
} catch (\Throwable $e) {
throw new ServiceException('Text processing is not available in your current Nextcloud version', 0, $e);
}
if(in_array(SummaryTaskType::class, $manager->getAvailableTaskTypes(), true)) {
$messagesBodies = array_map(function ($message) {
return $message->getPreviewText();
}, $messages);

$messageIds = array_map(function ($message) {
return $message->getMessageId();
}, $messages);
$cachedSummary = $this->cache->getSummary($messageIds);
if($cachedSummary) {
return $cachedSummary;
}
$client = $this->clientFactory->getClient($account);
try {
$messagesBodies = array_map(function ($message) use ($client, $account, $mailbox) {
$imapMessage = $this->mailManager->getImapMessage(
$client,
$account,
$mailbox,
$message->getUid(), true
);
return $imapMessage->getPlainBody();
}, $messages);

} finally {
$client->logout();
}

$taskPrompt = implode("\n", $messagesBodies);
$summaryTask = new Task(SummaryTaskType::class, $taskPrompt, "mail", $currentUserId, $threadId);
Expand Down
28 changes: 23 additions & 5 deletions tests/Unit/Service/AiIntegrationsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
namespace OCA\Mail\Tests\Unit\Service;

use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\Message;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Service\AiIntegrations\AiIntegrationsService;
use OCA\Mail\Service\AiIntegrations\Cache;
use OCP\TextProcessing\FreePromptTaskType;
Expand All @@ -53,6 +58,12 @@ class AiIntegrationsServiceTest extends TestCase {
/** @var Cache */
private $cache;

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

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


protected function setUp(): void {
parent::setUp();
Expand All @@ -64,27 +75,32 @@ protected function setUp(): void {
}

$this->cache = $this->createMock(Cache::class);

$this->clientFactory = $this->createMock(IMAPClientFactory::class);
$this->mailManager = $this->createMock(IMailManager::class);
$this->aiIntegrationsService = new AiIntegrationsService(
$this->container,
$this->cache
$this->cache,
$this->clientFactory,
$this->mailManager
);
}

public function testSummarizeThreadNoBackend() {
$account = new Account(new MailAccount());
$mailbox = new Mailbox();
if($this->manager !== null) {
$this->container->method('get')->willReturn($this->manager);
$this->manager
->method('getAvailableTaskTypes')
->willReturn([]);
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('No language model available for summary');
$this->aiIntegrationsService->summarizeThread('', [], '');
$this->aiIntegrationsService->summarizeThread($account, $mailbox, '', [], '');
}
$this->container->method('get')->willThrowException(new ServiceException());
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Text processing is not available in your current Nextcloud version');
$this->aiIntegrationsService->summarizeThread('', [], '');
$this->aiIntegrationsService->summarizeThread($account, $mailbox, '', [], '');

}

Expand Down Expand Up @@ -122,6 +138,8 @@ public function testLlmUnavailable() {

public function testCached() {
if($this->manager !== null) {
$account = new Account(new MailAccount());
$mailbox = new Mailbox();
$this->container->method('get')->willReturn($this->manager);
$this->manager
->method('getAvailableTaskTypes')
Expand Down Expand Up @@ -150,7 +168,7 @@ public function testCached() {
->with($messageIds)
->willReturn('this is a cached summary');

$this->assertEquals('this is a cached summary', $this->aiIntegrationsService->summarizeThread('some-thread-root-id-1', $messages, 'admin'));
$this->assertEquals('this is a cached summary', $this->aiIntegrationsService->summarizeThread($account, $mailbox, 'some-thread-root-id-1', $messages, 'admin'));
}
}
}

0 comments on commit 28ab9ca

Please sign in to comment.