Skip to content

Commit

Permalink
TASK: Store metadata for messages in a cache to allow reloading of th…
Browse files Browse the repository at this point in the history
…reads

This is implemented inside the chat controller for simplicity.
  • Loading branch information
mficzel committed Mar 6, 2024
1 parent d62e866 commit b81f3e2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
39 changes: 38 additions & 1 deletion Classes/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sitegeist\Chatterbox\Controller;

use Neos\Cache\Frontend\VariableFrontend;
use Neos\Flow\Mvc\Controller\ActionController;
use Sitegeist\Chatterbox\Domain\MessageRecord;
use Sitegeist\Chatterbox\Domain\OrganizationRepository;
Expand All @@ -20,11 +21,18 @@ class ChatController extends ActionController
*/
protected $viewFormatToObjectNameMap = ['json' => 'Neos\Flow\Mvc\View\JsonView'];

protected ?VariableFrontend $metaDataCache = null;

public function __construct(
private readonly OrganizationRepository $organizationRepository,
) {
}

public function injectMetaDataCache(VariableFrontend $metaDataCache): void
{
$this->metaDataCache = $metaDataCache;
}

public function startAction(string $organizationId, string $assistantId, string $message): string
{
$organization = $this->organizationRepository->findById($organizationId);
Expand All @@ -34,9 +42,14 @@ public function startAction(string $organizationId, string $assistantId, string

$messageResponses = $assistant->readThread($threadId);
$lastMessageKey = array_key_last($messageResponses);
$lastMessageId = $messageResponses[$lastMessageKey]->id;
$lastMessage = $messageResponses[$lastMessageKey];
$metadata = $assistant->getCollectedMetadata();

if ($metadata) {
$this->metaDataCache?->set($this->cacheId($assistantId, $threadId, $lastMessageId), $metadata, [$this->cacheTag($assistantId, $threadId)], 3600);
}

return json_encode(
array_merge(
[
Expand All @@ -55,10 +68,19 @@ public function historyAction(string $organizationId, string $assistantId, strin
$assistant = $organization->assistantDepartment->findAssistantById($assistantId);
$messages = $assistant->readThread($threadId);

$cachedMetadata = $this->metaDataCache ? $this->metaDataCache->getByTag($this->cacheTag($assistantId, $threadId)) : [];

return json_encode(
[
'messages' => array_map(
fn (MessageRecord $message): array => $message->toApiArray(),
function (MessageRecord $message) use ($cachedMetadata, $assistantId, $threadId): array {
return array_merge(
[
'metadata' => $cachedMetadata[$this->cacheId($assistantId, $threadId, $message->id)] ?? null
],
$message->toApiArray()
);
},
$messages
),
],
Expand All @@ -74,9 +96,14 @@ public function postAction(string $organizationId, string $assistantId, string $

$messageResponses = $assistant->readThread($threadId);
$lastMessageKey = array_key_last($messageResponses);
$lastMessageId = $messageResponses[$lastMessageKey]->id;
$lastMessage = $messageResponses[$lastMessageKey];
$metadata = $assistant->getCollectedMetadata();

if ($metadata) {
$this->metaDataCache?->set($this->cacheId($assistantId, $threadId, $lastMessageId), $metadata, [$this->cacheTag($assistantId, $threadId)], 3600);
}

return json_encode(
array_merge(
[
Expand All @@ -87,4 +114,14 @@ public function postAction(string $organizationId, string $assistantId, string $
JSON_THROW_ON_ERROR
);
}

private function cacheTag(string $assistantId, string $threadId): string
{
return 't_' . md5($assistantId . ':' . $threadId);
}

private function cacheId(string $assistantId, string $threadId, string $messageId): string
{
return 'm_' . md5($assistantId . ':' . $threadId . ':' . $messageId);
}
}
6 changes: 6 additions & 0 deletions Configuration/Caches.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sitegeist_Chatterbox_MetaDataCache:
persistent: true
frontend: Neos\Cache\Frontend\VariableFrontend
backend: Neos\Cache\Backend\FileBackend
backendOptions:
defaultLifetime: 86400
9 changes: 9 additions & 0 deletions Configuration/Objects.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Sitegeist\Chatterbox\Controller\ChatController:
properties:
metaDataCache:
object:
factoryObjectName: Neos\Flow\Cache\CacheManager
factoryMethodName: getCache
arguments:
1:
value: Sitegeist_Chatterbox_MetaDataCache

0 comments on commit b81f3e2

Please sign in to comment.