|
| 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\Doctrine; |
| 13 | + |
| 14 | +use Doctrine\DBAL\Connection; |
| 15 | +use Doctrine\DBAL\Connection as DBALConnection; |
| 16 | +use Doctrine\DBAL\Platforms\OraclePlatform; |
| 17 | +use Doctrine\DBAL\Result; |
| 18 | +use Doctrine\DBAL\Schema\Name\Identifier; |
| 19 | +use Doctrine\DBAL\Schema\Name\UnqualifiedName; |
| 20 | +use Doctrine\DBAL\Schema\PrimaryKeyConstraint; |
| 21 | +use Doctrine\DBAL\Schema\Schema; |
| 22 | +use Doctrine\DBAL\Types\Types; |
| 23 | +use Symfony\AI\Chat\Exception\InvalidArgumentException; |
| 24 | +use Symfony\AI\Chat\ManagedStoreInterface; |
| 25 | +use Symfony\AI\Chat\MessageNormalizer; |
| 26 | +use Symfony\AI\Chat\MessageStoreInterface; |
| 27 | +use Symfony\AI\Platform\Message\MessageBag; |
| 28 | +use Symfony\AI\Platform\Message\MessageInterface; |
| 29 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 30 | +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; |
| 31 | +use Symfony\Component\Serializer\Serializer; |
| 32 | +use Symfony\Component\Serializer\SerializerInterface; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Guillaume Loulier <[email protected]> |
| 36 | + */ |
| 37 | +final class DoctrineDbalMessageStore implements ManagedStoreInterface, MessageStoreInterface |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + private readonly string $tableName, |
| 41 | + private readonly DBALConnection $dbalConnection, |
| 42 | + private readonly SerializerInterface $serializer = new Serializer([ |
| 43 | + new ArrayDenormalizer(), |
| 44 | + new MessageNormalizer(), |
| 45 | + ], [new JsonEncoder()]), |
| 46 | + ) { |
| 47 | + } |
| 48 | + |
| 49 | + public function setup(array $options = []): void |
| 50 | + { |
| 51 | + if ([] !== $options) { |
| 52 | + throw new InvalidArgumentException('No supported options.'); |
| 53 | + } |
| 54 | + |
| 55 | + $schema = $this->dbalConnection->createSchemaManager()->introspectSchema(); |
| 56 | + |
| 57 | + if ($schema->hasTable($this->tableName)) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + $this->addTableToSchema($schema); |
| 62 | + } |
| 63 | + |
| 64 | + public function drop(): void |
| 65 | + { |
| 66 | + $schema = $this->dbalConnection->createSchemaManager()->introspectSchema(); |
| 67 | + |
| 68 | + if (!$schema->hasTable($this->tableName)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $queryBuilder = $this->dbalConnection->createQueryBuilder() |
| 73 | + ->delete($this->tableName); |
| 74 | + |
| 75 | + $this->dbalConnection->transactional(fn (Connection $connection): Result => $connection->executeQuery( |
| 76 | + $queryBuilder->getSQL(), |
| 77 | + )); |
| 78 | + } |
| 79 | + |
| 80 | + public function save(MessageBag $messages): void |
| 81 | + { |
| 82 | + $queryBuilder = $this->dbalConnection->createQueryBuilder() |
| 83 | + ->insert($this->tableName) |
| 84 | + ->values([ |
| 85 | + 'messages' => '?', |
| 86 | + ]); |
| 87 | + |
| 88 | + $this->dbalConnection->transactional(fn (Connection $connection): Result => $connection->executeQuery( |
| 89 | + $queryBuilder->getSQL(), |
| 90 | + [ |
| 91 | + $this->serializer->serialize($messages->getMessages(), 'json'), |
| 92 | + ], |
| 93 | + $queryBuilder->getParameterTypes(), |
| 94 | + )); |
| 95 | + } |
| 96 | + |
| 97 | + public function load(): MessageBag |
| 98 | + { |
| 99 | + $queryBuilder = $this->dbalConnection->createQueryBuilder() |
| 100 | + ->select('messages') |
| 101 | + ->from($this->tableName) |
| 102 | + ; |
| 103 | + |
| 104 | + $result = $this->dbalConnection->transactional(static fn (Connection $connection): Result => $connection->executeQuery( |
| 105 | + $queryBuilder->getSQL(), |
| 106 | + )); |
| 107 | + |
| 108 | + $messages = array_map( |
| 109 | + fn (array $payload): array => $this->serializer->deserialize($payload['messages'], MessageInterface::class.'[]', 'json'), |
| 110 | + $result->fetchAllAssociative(), |
| 111 | + ); |
| 112 | + |
| 113 | + return new MessageBag(...array_merge(...$messages)); |
| 114 | + } |
| 115 | + |
| 116 | + private function addTableToSchema(Schema $schema): void |
| 117 | + { |
| 118 | + $table = $schema->createTable($this->tableName); |
| 119 | + $table->addOption('_symfony_ai_chat_table_name', $this->tableName); |
| 120 | + $idColumn = $table->addColumn('id', Types::BIGINT) |
| 121 | + ->setAutoincrement(true) |
| 122 | + ->setNotnull(true); |
| 123 | + $table->addColumn('messages', Types::TEXT) |
| 124 | + ->setNotnull(true); |
| 125 | + if (class_exists(PrimaryKeyConstraint::class)) { |
| 126 | + $table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [ |
| 127 | + new UnqualifiedName(Identifier::unquoted('id')), |
| 128 | + ], true)); |
| 129 | + } else { |
| 130 | + $table->setPrimaryKey(['id']); |
| 131 | + } |
| 132 | + |
| 133 | + // We need to create a sequence for Oracle and set the id column to get the correct nextval |
| 134 | + if ($this->dbalConnection->getDatabasePlatform() instanceof OraclePlatform) { |
| 135 | + $serverVersion = $this->dbalConnection->executeQuery("SELECT version FROM product_component_version WHERE product LIKE 'Oracle Database%'")->fetchOne(); |
| 136 | + if (version_compare($serverVersion, '12.1.0', '>=')) { |
| 137 | + $idColumn->setAutoincrement(false); // disable the creation of SEQUENCE and TRIGGER |
| 138 | + $idColumn->setDefault($this->tableName.'_seq.nextval'); |
| 139 | + |
| 140 | + $schema->createSequence($this->tableName.'_seq'); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + foreach ($schema->toSql($this->dbalConnection->getDatabasePlatform()) as $sql) { |
| 145 | + $this->dbalConnection->executeQuery($sql); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments