From 4e1f8cf21658f0767fa6b0e6c6a3942e2527f9e6 Mon Sep 17 00:00:00 2001 From: hamza221 Date: Thu, 10 Aug 2023 22:09:51 +0200 Subject: [PATCH] Add Caching to Thread summaries Signed-off-by: hamza221 --- lib/Controller/PageController.php | 2 +- lib/Controller/ThreadController.php | 2 +- .../AiIntegrationsService.php | 21 ++++- lib/Service/AiIntegrations/Cache.php | 79 +++++++++++++++++++ lib/Settings/AdminSettings.php | 2 +- tests/Unit/Controller/PageControllerTest.php | 2 +- .../Unit/Controller/ThreadControllerTest.php | 2 +- 7 files changed, 102 insertions(+), 8 deletions(-) rename lib/Service/{ => AiIntegrations}/AiIntegrationsService.php (81%) create mode 100644 lib/Service/AiIntegrations/Cache.php diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index ba75039e3f..fada1a8cb7 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -33,7 +33,7 @@ use OCA\Mail\Db\SmimeCertificate; use OCA\Mail\Db\TagMapper; use OCA\Mail\Service\AccountService; -use OCA\Mail\Service\AiIntegrationsService; +use OCA\Mail\Service\AiIntegrations\AiIntegrationsService; use OCA\Mail\Service\AliasesService; use OCA\Mail\Service\OutboxService; use OCA\Mail\Service\SmimeService; diff --git a/lib/Controller/ThreadController.php b/lib/Controller/ThreadController.php index 0ffb66cdd8..2c6a3b6d2c 100755 --- a/lib/Controller/ThreadController.php +++ b/lib/Controller/ThreadController.php @@ -28,7 +28,7 @@ use OCA\Mail\Exception\ServiceException; use OCA\Mail\Http\TrapError; use OCA\Mail\Service\AccountService; -use OCA\Mail\Service\AiIntegrationsService; +use OCA\Mail\Service\AiIntegrations\AiIntegrationsService; use OCA\Mail\Service\SnoozeService; use OCP\AppFramework\Controller; use OCP\AppFramework\Db\DoesNotExistException; diff --git a/lib/Service/AiIntegrationsService.php b/lib/Service/AiIntegrations/AiIntegrationsService.php similarity index 81% rename from lib/Service/AiIntegrationsService.php rename to lib/Service/AiIntegrations/AiIntegrationsService.php index ca7f89ee63..1f425ec04b 100644 --- a/lib/Service/AiIntegrationsService.php +++ b/lib/Service/AiIntegrations/AiIntegrationsService.php @@ -21,7 +21,7 @@ * */ -namespace OCA\Mail\Service; +namespace OCA\Mail\Service\AiIntegrations; use OCA\Mail\Exception\ServiceException; use OCP\TextProcessing\IManager; @@ -35,9 +35,13 @@ class AiIntegrationsService { /** @var ContainerInterface */ private ContainerInterface $container; + /** @var Cache */ + private Cache $cache; - public function __construct(ContainerInterface $container) { + + public function __construct(ContainerInterface $container, Cache $cache) { $this->container = $container; + $this->cache = $cache; } /** * @param string $threadId @@ -59,11 +63,22 @@ public function summarizeThread(string $threadId, array $messages, string $curre return $message->getPreviewText(); }, $messages); + $messageIds = array_map(function ($message) { + return $message->getMessageId(); + }, $messages); + $cachedSummary = $this->cache->getSummary($messageIds); + if($cachedSummary) { + return $cachedSummary; + } + $taskPrompt = implode("\n", $messagesBodies); $summaryTask = new Task(SummaryTaskType::class, $taskPrompt, "mail", $currentUserId, $threadId); $manager->runTask($summaryTask); + $summary = $summaryTask->getOutput(); + + $this->cache->addSummary($messageIds, $summary); - return $summaryTask->getOutput(); + return $summary; } else { throw new ServiceException('No language model available for summary'); } diff --git a/lib/Service/AiIntegrations/Cache.php b/lib/Service/AiIntegrations/Cache.php new file mode 100644 index 0000000000..675da59a28 --- /dev/null +++ b/lib/Service/AiIntegrations/Cache.php @@ -0,0 +1,79 @@ + + * + * @author 2017 Hamza Mahjoubi + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Mail\Service\AiIntegrations; + +use OCP\ICache; +use OCP\ICacheFactory; + +class Cache { + // Cache for one week + public const CACHE_TTL = 7 * 24 * 60 * 60; + + /** @var ICache */ + private $cache; + + + public function __construct(ICacheFactory $cacheFactory) { + $this->cache = $cacheFactory->createDistributed('mail.ai'); + } + + /** + * @param array $ids + * @return string + */ + private function buildUrlKey(array $ids): string { + return base64_encode(json_encode($ids)); + } + + + /** + * @param array $ids + * + * @return string|false the summary if cached, false if cached but no value or not cached + */ + public function getSummary(array $ids) { + $cached = $this->cache->get($this->buildUrlKey($ids)); + + if (is_null($cached) || $cached === false) { + return false; + } + + return $cached; + } + + /** + * @param array $ids + * @param string|null $summary + * + * @return void + */ + public function addSummary(array $ids, ?string $summary): void { + $this->cache->set($this->buildUrlKey($ids), $summary === null ? false : $summary, self::CACHE_TTL); + } + + +} diff --git a/lib/Settings/AdminSettings.php b/lib/Settings/AdminSettings.php index 66e2e17951..8e374facbf 100644 --- a/lib/Settings/AdminSettings.php +++ b/lib/Settings/AdminSettings.php @@ -28,7 +28,7 @@ use OCA\Mail\AppInfo\Application; use OCA\Mail\Integration\GoogleIntegration; use OCA\Mail\Integration\MicrosoftIntegration; -use OCA\Mail\Service\AiIntegrationsService; +use OCA\Mail\Service\AiIntegrations\AiIntegrationsService; use OCA\Mail\Service\AntiSpamService; use OCA\Mail\Service\Provisioning\Manager as ProvisioningManager; use OCP\AppFramework\Http\TemplateResponse; diff --git a/tests/Unit/Controller/PageControllerTest.php b/tests/Unit/Controller/PageControllerTest.php index b5ffc24da2..c595fc4d69 100644 --- a/tests/Unit/Controller/PageControllerTest.php +++ b/tests/Unit/Controller/PageControllerTest.php @@ -31,7 +31,7 @@ use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\TagMapper; use OCA\Mail\Service\AccountService; -use OCA\Mail\Service\AiIntegrationsService; +use OCA\Mail\Service\AiIntegrations\AiIntegrationsService; use OCA\Mail\Service\AliasesService; use OCA\Mail\Service\MailManager; use OCA\Mail\Service\OutboxService; diff --git a/tests/Unit/Controller/ThreadControllerTest.php b/tests/Unit/Controller/ThreadControllerTest.php index 56b7f71e62..2b2ea2cfcc 100644 --- a/tests/Unit/Controller/ThreadControllerTest.php +++ b/tests/Unit/Controller/ThreadControllerTest.php @@ -31,7 +31,7 @@ use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\Message; use OCA\Mail\Service\AccountService; -use OCA\Mail\Service\AiIntegrationsService; +use OCA\Mail\Service\AiIntegrations\AiIntegrationsService; use OCA\Mail\Service\SnoozeService; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http;