|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Chat\Bridge\Pogocache; |
| 13 | + |
| 14 | +use Symfony\AI\Chat\Exception\InvalidArgumentException; |
| 15 | +use Symfony\AI\Chat\Exception\LogicException; |
| 16 | +use Symfony\AI\Chat\ManagedStoreInterface; |
| 17 | +use Symfony\AI\Chat\MessageStoreInterface; |
| 18 | +use Symfony\AI\Platform\Message\AssistantMessage; |
| 19 | +use Symfony\AI\Platform\Message\Content\Audio; |
| 20 | +use Symfony\AI\Platform\Message\Content\ContentInterface; |
| 21 | +use Symfony\AI\Platform\Message\Content\DocumentUrl; |
| 22 | +use Symfony\AI\Platform\Message\Content\File; |
| 23 | +use Symfony\AI\Platform\Message\Content\Image; |
| 24 | +use Symfony\AI\Platform\Message\Content\ImageUrl; |
| 25 | +use Symfony\AI\Platform\Message\Content\Text; |
| 26 | +use Symfony\AI\Platform\Message\MessageBag; |
| 27 | +use Symfony\AI\Platform\Message\MessageInterface; |
| 28 | +use Symfony\AI\Platform\Message\SystemMessage; |
| 29 | +use Symfony\AI\Platform\Message\ToolCallMessage; |
| 30 | +use Symfony\AI\Platform\Message\UserMessage; |
| 31 | +use Symfony\AI\Platform\Result\ToolCall; |
| 32 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Guillaume Loulier <[email protected]> |
| 36 | + */ |
| 37 | +final readonly class MessageStore implements ManagedStoreInterface, MessageStoreInterface |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + private HttpClientInterface $httpClient, |
| 41 | + private string $host, |
| 42 | + #[\SensitiveParameter] private string $password, |
| 43 | + private string $key = '_message_store_pogocache', |
| 44 | + ) { |
| 45 | + } |
| 46 | + |
| 47 | + public function setup(array $options = []): void |
| 48 | + { |
| 49 | + if ([] !== $options) { |
| 50 | + throw new InvalidArgumentException('The Pogocache message store does not support any options.'); |
| 51 | + } |
| 52 | + |
| 53 | + $this->request('PUT', $this->key); |
| 54 | + } |
| 55 | + |
| 56 | + public function drop(): void |
| 57 | + { |
| 58 | + $this->request('PUT', $this->key); |
| 59 | + } |
| 60 | + |
| 61 | + public function save(MessageBag $messages): void |
| 62 | + { |
| 63 | + $messages = $messages->getMessages(); |
| 64 | + |
| 65 | + $this->request('PUT', $this->key, array_map( |
| 66 | + $this->convertToIndexableArray(...), |
| 67 | + $messages, |
| 68 | + )); |
| 69 | + } |
| 70 | + |
| 71 | + public function load(): MessageBag |
| 72 | + { |
| 73 | + $messages = $this->request('GET', $this->key); |
| 74 | + |
| 75 | + return new MessageBag(...array_map( |
| 76 | + $this->convertToMessage(...), |
| 77 | + $messages, |
| 78 | + )); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param array<string, mixed>|list<array<string, mixed>> $payload |
| 83 | + * |
| 84 | + * @return array<string, mixed> |
| 85 | + */ |
| 86 | + private function request(string $method, string $endpoint, array $payload = []): array |
| 87 | + { |
| 88 | + $result = $this->httpClient->request($method, \sprintf('%s/%s?auth=%s', $this->host, $endpoint, $this->password), [ |
| 89 | + 'json' => [] !== $payload ? $payload : new \stdClass(), |
| 90 | + ]); |
| 91 | + |
| 92 | + $payload = $result->getContent(); |
| 93 | + |
| 94 | + if ('GET' === $method && json_validate($payload)) { |
| 95 | + return json_decode($payload, true); |
| 96 | + } |
| 97 | + |
| 98 | + return []; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return array<string, mixed> |
| 103 | + */ |
| 104 | + private function convertToIndexableArray(MessageInterface $message): array |
| 105 | + { |
| 106 | + $toolsCalls = []; |
| 107 | + |
| 108 | + if ($message instanceof AssistantMessage && $message->hasToolCalls()) { |
| 109 | + $toolsCalls = array_map( |
| 110 | + static fn (ToolCall $toolCall): array => $toolCall->jsonSerialize(), |
| 111 | + $message->getToolCalls(), |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + if ($message instanceof ToolCallMessage) { |
| 116 | + $toolsCalls = $message->getToolCall()->jsonSerialize(); |
| 117 | + } |
| 118 | + |
| 119 | + return [ |
| 120 | + 'id' => $message->getId()->toRfc4122(), |
| 121 | + 'type' => $message::class, |
| 122 | + 'content' => ($message instanceof SystemMessage || $message instanceof AssistantMessage || $message instanceof ToolCallMessage) ? $message->getContent() : '', |
| 123 | + 'contentAsBase64' => ($message instanceof UserMessage && [] !== $message->getContent()) ? array_map( |
| 124 | + static fn (ContentInterface $content) => [ |
| 125 | + 'type' => $content::class, |
| 126 | + 'content' => match ($content::class) { |
| 127 | + Text::class => $content->getText(), |
| 128 | + File::class, |
| 129 | + Image::class, |
| 130 | + Audio::class => $content->asBase64(), |
| 131 | + ImageUrl::class, |
| 132 | + DocumentUrl::class => $content->getUrl(), |
| 133 | + default => throw new LogicException(\sprintf('Unknown content type "%s".', $content::class)), |
| 134 | + }, |
| 135 | + ], |
| 136 | + $message->getContent(), |
| 137 | + ) : [], |
| 138 | + 'toolsCalls' => $toolsCalls, |
| 139 | + 'metadata' => $message->getMetadata()->all(), |
| 140 | + ]; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @param array<string, mixed> $payload |
| 145 | + */ |
| 146 | + private function convertToMessage(array $payload): MessageInterface |
| 147 | + { |
| 148 | + $type = $payload['type']; |
| 149 | + $content = $payload['content'] ?? ''; |
| 150 | + $contentAsBase64 = $payload['contentAsBase64'] ?? []; |
| 151 | + |
| 152 | + $message = match ($type) { |
| 153 | + SystemMessage::class => new SystemMessage($content), |
| 154 | + AssistantMessage::class => new AssistantMessage($content, array_map( |
| 155 | + static fn (array $toolsCall): ToolCall => new ToolCall( |
| 156 | + $toolsCall['id'], |
| 157 | + $toolsCall['function']['name'], |
| 158 | + json_decode($toolsCall['function']['arguments'], true) |
| 159 | + ), |
| 160 | + $payload['toolsCalls'], |
| 161 | + )), |
| 162 | + UserMessage::class => new UserMessage(...array_map( |
| 163 | + static fn (array $contentAsBase64): ContentInterface => \in_array($contentAsBase64['type'], [File::class, Image::class, Audio::class], true) |
| 164 | + ? $contentAsBase64['type']::fromDataUrl($contentAsBase64['content']) |
| 165 | + : new $contentAsBase64['type']($contentAsBase64['content']), |
| 166 | + $contentAsBase64, |
| 167 | + )), |
| 168 | + ToolCallMessage::class => new ToolCallMessage( |
| 169 | + new ToolCall( |
| 170 | + $payload['toolsCalls']['id'], |
| 171 | + $payload['toolsCalls']['function']['name'], |
| 172 | + json_decode($payload['toolsCalls']['function']['arguments'], true) |
| 173 | + ), |
| 174 | + $content |
| 175 | + ), |
| 176 | + default => throw new LogicException(\sprintf('Unknown message type "%s".', $type)), |
| 177 | + }; |
| 178 | + |
| 179 | + $message->getMetadata()->set($payload['metadata']); |
| 180 | + |
| 181 | + return $message; |
| 182 | + } |
| 183 | +} |
0 commit comments