Skip to content

Commit

Permalink
Add Caching to Thread summaries
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <[email protected]>
  • Loading branch information
hamza221 committed Aug 10, 2023
1 parent abf1d94 commit 4e1f8cf
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/ThreadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
*/

namespace OCA\Mail\Service;
namespace OCA\Mail\Service\AiIntegrations;

use OCA\Mail\Exception\ServiceException;
use OCP\TextProcessing\IManager;
Expand All @@ -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
Expand All @@ -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');
}
Expand Down
79 changes: 79 additions & 0 deletions lib/Service/AiIntegrations/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/**
* @copyright 2017 Hamza Mahjoubi<[email protected]>
*
* @author 2017 Hamza Mahjoubi <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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);
}


}
2 changes: 1 addition & 1 deletion lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Controller/ThreadControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4e1f8cf

Please sign in to comment.