|
| 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\Cloudflare; |
| 13 | + |
| 14 | +use Symfony\AI\Chat\Exception\InvalidArgumentException; |
| 15 | +use Symfony\AI\Chat\ManagedStoreInterface; |
| 16 | +use Symfony\AI\Chat\MessageNormalizer; |
| 17 | +use Symfony\AI\Chat\MessageStoreInterface; |
| 18 | +use Symfony\AI\Platform\Message\MessageBag; |
| 19 | +use Symfony\AI\Platform\Message\MessageInterface; |
| 20 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 21 | +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; |
| 22 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 23 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 24 | +use Symfony\Component\Serializer\Serializer; |
| 25 | +use Symfony\Component\Serializer\SerializerInterface; |
| 26 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Guillaume Loulier <[email protected]> |
| 30 | + */ |
| 31 | +final class MessageStore implements ManagedStoreInterface, MessageStoreInterface |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + private readonly HttpClientInterface $httpClient, |
| 35 | + private readonly string $namespace, |
| 36 | + #[\SensitiveParameter] private readonly string $accountId, |
| 37 | + #[\SensitiveParameter] private readonly string $apiKey, |
| 38 | + private readonly SerializerInterface&NormalizerInterface&DenormalizerInterface $serializer = new Serializer([ |
| 39 | + new ArrayDenormalizer(), |
| 40 | + new MessageNormalizer(), |
| 41 | + ], [new JsonEncoder()]), |
| 42 | + private readonly string $endpointUrl = 'https://api.cloudflare.com/client/v4/accounts', |
| 43 | + ) { |
| 44 | + } |
| 45 | + |
| 46 | + public function setup(array $options = []): void |
| 47 | + { |
| 48 | + if ([] !== $options) { |
| 49 | + throw new InvalidArgumentException('No supported options.'); |
| 50 | + } |
| 51 | + |
| 52 | + $namespaces = $this->request('GET', 'storage/kv/namespaces'); |
| 53 | + |
| 54 | + $filteredNamespaces = array_filter( |
| 55 | + $namespaces['result'], |
| 56 | + fn (array $payload): bool => $payload['title'] === $this->namespace, |
| 57 | + ); |
| 58 | + |
| 59 | + if (0 !== \count($filteredNamespaces)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + $this->request('POST', 'storage/kv/namespaces', [ |
| 64 | + 'title' => $this->namespace, |
| 65 | + ]); |
| 66 | + } |
| 67 | + |
| 68 | + public function drop(): void |
| 69 | + { |
| 70 | + $currentNamespaceUuid = $this->retrieveCurrentNamespaceUuid(); |
| 71 | + |
| 72 | + $keys = $this->request('GET', \sprintf('storage/kv/namespaces/%s/keys', $currentNamespaceUuid)); |
| 73 | + |
| 74 | + if ([] === $keys['result']) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + $this->request('POST', \sprintf('storage/kv/namespaces/%s/bulk/delete', $currentNamespaceUuid), array_map( |
| 79 | + static fn (array $payload): string => $payload['name'], |
| 80 | + $keys['result'], |
| 81 | + )); |
| 82 | + } |
| 83 | + |
| 84 | + public function save(MessageBag $messages): void |
| 85 | + { |
| 86 | + $currentNamespaceUuid = $this->retrieveCurrentNamespaceUuid(); |
| 87 | + |
| 88 | + $this->request('PUT', \sprintf('storage/kv/namespaces/%s/bulk', $currentNamespaceUuid), array_map( |
| 89 | + fn (MessageInterface $message): array => [ |
| 90 | + 'key' => $message->getId()->toRfc4122(), |
| 91 | + 'value' => $this->serializer->serialize($message, 'json'), |
| 92 | + ], |
| 93 | + $messages->getMessages(), |
| 94 | + )); |
| 95 | + } |
| 96 | + |
| 97 | + public function load(): MessageBag |
| 98 | + { |
| 99 | + $currentNamespaceUuid = $this->retrieveCurrentNamespaceUuid(); |
| 100 | + |
| 101 | + $keys = $this->request('GET', \sprintf('storage/kv/namespaces/%s/keys', $currentNamespaceUuid)); |
| 102 | + |
| 103 | + $messages = $this->request('POST', \sprintf('storage/kv/namespaces/%s/bulk/get', $currentNamespaceUuid), [ |
| 104 | + 'keys' => array_map( |
| 105 | + static fn (array $payload): string => $payload['name'], |
| 106 | + $keys['result'], |
| 107 | + ), |
| 108 | + ]); |
| 109 | + |
| 110 | + return new MessageBag(...array_map( |
| 111 | + fn (string $message): MessageInterface => $this->serializer->deserialize($message, MessageInterface::class, 'json'), |
| 112 | + $messages['result']['values'], |
| 113 | + )); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param array<string, mixed>|list<array<string, string>> $payload |
| 118 | + * |
| 119 | + * @return array<string, mixed> |
| 120 | + */ |
| 121 | + private function request(string $method, string $endpoint, array $payload = []): array |
| 122 | + { |
| 123 | + $finalOptions = [ |
| 124 | + 'auth_bearer' => $this->apiKey, |
| 125 | + ]; |
| 126 | + |
| 127 | + if ([] !== $payload) { |
| 128 | + $finalOptions['json'] = $payload; |
| 129 | + } |
| 130 | + |
| 131 | + $response = $this->httpClient->request($method, \sprintf('%s/%s/%s', $this->endpointUrl, $this->accountId, $endpoint), $finalOptions); |
| 132 | + |
| 133 | + return $response->toArray(); |
| 134 | + } |
| 135 | + |
| 136 | + private function retrieveCurrentNamespaceUuid(): string |
| 137 | + { |
| 138 | + $namespaces = $this->request('GET', 'storage/kv/namespaces'); |
| 139 | + |
| 140 | + $filteredNamespaces = array_filter( |
| 141 | + $namespaces['result'], |
| 142 | + fn (array $payload): bool => $payload['title'] === $this->namespace, |
| 143 | + ); |
| 144 | + |
| 145 | + if (0 === \count($filteredNamespaces)) { |
| 146 | + throw new InvalidArgumentException('No namespace found.'); |
| 147 | + } |
| 148 | + |
| 149 | + reset($filteredNamespaces); |
| 150 | + |
| 151 | + return $filteredNamespaces[0]['id']; |
| 152 | + } |
| 153 | +} |
0 commit comments