From b2befbf6773b9d919a10af89575747c5ec9fa51c Mon Sep 17 00:00:00 2001 From: hamza221 Date: Tue, 26 Sep 2023 13:23:53 +0200 Subject: [PATCH] Fix: use message bodies for thread summary Signed-off-by: hamza221 --- lib/Controller/ThreadController.php | 2 ++ .../AiIntegrations/AiIntegrationsService.php | 36 +++++++++++++++---- .../Service/AiIntegrationsServiceTest.php | 28 ++++++++++++--- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/lib/Controller/ThreadController.php b/lib/Controller/ThreadController.php index eb5c3aa209..5c0d7a5c68 100755 --- a/lib/Controller/ThreadController.php +++ b/lib/Controller/ThreadController.php @@ -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, diff --git a/lib/Service/AiIntegrations/AiIntegrationsService.php b/lib/Service/AiIntegrations/AiIntegrationsService.php index ec7f55b429..fbfdadb507 100644 --- a/lib/Service/AiIntegrations/AiIntegrationsService.php +++ b/lib/Service/AiIntegrations/AiIntegrationsService.php @@ -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; @@ -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 @@ -52,17 +64,13 @@ 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); @@ -70,6 +78,22 @@ public function summarizeThread(string $threadId, array $messages, string $curre 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 + ); + $a = $imapMessage->getPlainBody(); + return $a; + }, $messages); + + } finally { + $client->logout(); + } $taskPrompt = implode("\n", $messagesBodies); $summaryTask = new Task(SummaryTaskType::class, $taskPrompt, "mail", $currentUserId, $threadId); diff --git a/tests/Unit/Service/AiIntegrationsServiceTest.php b/tests/Unit/Service/AiIntegrationsServiceTest.php index ef9077daa4..6be18aa024 100644 --- a/tests/Unit/Service/AiIntegrationsServiceTest.php +++ b/tests/Unit/Service/AiIntegrationsServiceTest.php @@ -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; @@ -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(); @@ -64,14 +75,19 @@ 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 @@ -79,12 +95,12 @@ public function testSummarizeThreadNoBackend() { ->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, '', [], ''); } @@ -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') @@ -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')); } } }