Skip to content

Commit d373508

Browse files
committed
refactor(core): improvements on Uuid handling && MessageNormalizer
1 parent 92134c3 commit d373508

File tree

9 files changed

+57
-47
lines changed

9 files changed

+57
-47
lines changed

src/chat/src/MessageNormalizer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2929
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
3030
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
31+
use Symfony\Component\Uid\AbstractUid;
32+
use Symfony\Component\Uid\TimeBasedUidInterface;
33+
use Symfony\Component\Uid\Uuid;
3134

3235
/**
3336
* @author Guillaume Loulier <[email protected]>
@@ -71,12 +74,17 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
7174
default => throw new LogicException(\sprintf('Unknown message type "%s".', $type)),
7275
};
7376

74-
$message->getMetadata()->set([
77+
/** @var AbstractUid&TimeBasedUidInterface&Uuid $existingUuid */
78+
$existingUuid = Uuid::fromString($data['id']);
79+
80+
$messageWithExistingUuid = $message->withId($existingUuid);
81+
82+
$messageWithExistingUuid->getMetadata()->set([
7583
...$data['metadata'],
7684
'addedAt' => $data['addedAt'],
7785
]);
7886

79-
return $message;
87+
return $messageWithExistingUuid;
8088
}
8189

8290
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool

src/chat/tests/MessageNormalizerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ public function testItCanNormalize()
5454

5555
public function testItCanDenormalize()
5656
{
57+
$uuid = Uuid::v7()->toRfc4122();
5758
$normalizer = new MessageNormalizer();
5859

5960
$message = $normalizer->denormalize([
60-
'id' => Uuid::v7()->toRfc4122(),
61+
'id' => $uuid,
6162
'type' => UserMessage::class,
6263
'content' => '',
6364
'contentAsBase64' => [
@@ -71,6 +72,7 @@ public function testItCanDenormalize()
7172
'addedAt' => (new \DateTimeImmutable())->getTimestamp(),
7273
], MessageInterface::class);
7374

75+
$this->assertSame($uuid, $message->getId()->toRfc4122());
7476
$this->assertSame(Role::User, $message->getRole());
7577
$this->assertArrayHasKey('addedAt', $message->getMetadata()->all());
7678
}

src/platform/src/Message/AssistantMessage.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@
1313

1414
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
1515
use Symfony\AI\Platform\Result\ToolCall;
16-
use Symfony\Component\Uid\AbstractUid;
17-
use Symfony\Component\Uid\TimeBasedUidInterface;
1816
use Symfony\Component\Uid\Uuid;
1917

2018
/**
2119
* @author Denis Zunke <[email protected]>
2220
*/
2321
final class AssistantMessage implements MessageInterface
2422
{
23+
use IdentifierAwareTrait;
2524
use MetadataAwareTrait;
2625

27-
private readonly AbstractUid&TimeBasedUidInterface $id;
28-
2926
/**
3027
* @param ?ToolCall[] $toolCalls
3128
*/
@@ -41,11 +38,6 @@ public function getRole(): Role
4138
return Role::Assistant;
4239
}
4340

44-
public function getId(): AbstractUid&TimeBasedUidInterface
45-
{
46-
return $this->id;
47-
}
48-
4941
public function hasToolCalls(): bool
5042
{
5143
return null !== $this->toolCalls && [] !== $this->toolCalls;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Platform\Message;
13+
14+
use Symfony\Component\Uid\AbstractUid;
15+
use Symfony\Component\Uid\TimeBasedUidInterface;
16+
17+
/**
18+
* @author Guillaume Loulier <[email protected]>
19+
*/
20+
trait IdentifierAwareTrait
21+
{
22+
private AbstractUid&TimeBasedUidInterface $id;
23+
24+
public function withId(AbstractUid&TimeBasedUidInterface $id): self
25+
{
26+
$clone = clone $this;
27+
$clone->id = $id;
28+
29+
return $clone;
30+
}
31+
32+
public function getId(): AbstractUid&TimeBasedUidInterface
33+
{
34+
return $this->id;
35+
}
36+
}

src/platform/src/Message/MessageInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function getRole(): Role;
2525

2626
public function getId(): AbstractUid&TimeBasedUidInterface;
2727

28+
public function withId(AbstractUid&TimeBasedUidInterface $id): self;
29+
2830
/**
2931
* @return string|ContentInterface[]|null
3032
*/

src/platform/src/Message/SystemMessage.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@
1212
namespace Symfony\AI\Platform\Message;
1313

1414
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
15-
use Symfony\Component\Uid\AbstractUid;
16-
use Symfony\Component\Uid\TimeBasedUidInterface;
1715
use Symfony\Component\Uid\Uuid;
1816

1917
/**
2018
* @author Denis Zunke <[email protected]>
2119
*/
2220
final class SystemMessage implements MessageInterface
2321
{
22+
use IdentifierAwareTrait;
2423
use MetadataAwareTrait;
2524

26-
private readonly AbstractUid&TimeBasedUidInterface $id;
27-
2825
public function __construct(
2926
private readonly string $content,
3027
) {
@@ -36,11 +33,6 @@ public function getRole(): Role
3633
return Role::System;
3734
}
3835

39-
public function getId(): AbstractUid&TimeBasedUidInterface
40-
{
41-
return $this->id;
42-
}
43-
4436
public function getContent(): string
4537
{
4638
return $this->content;

src/platform/src/Message/ToolCallMessage.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@
1313

1414
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
1515
use Symfony\AI\Platform\Result\ToolCall;
16-
use Symfony\Component\Uid\AbstractUid;
17-
use Symfony\Component\Uid\TimeBasedUidInterface;
1816
use Symfony\Component\Uid\Uuid;
1917

2018
/**
2119
* @author Denis Zunke <[email protected]>
2220
*/
2321
final class ToolCallMessage implements MessageInterface
2422
{
23+
use IdentifierAwareTrait;
2524
use MetadataAwareTrait;
2625

27-
private readonly AbstractUid&TimeBasedUidInterface $id;
28-
2926
public function __construct(
3027
private readonly ToolCall $toolCall,
3128
private readonly string $content,
@@ -38,11 +35,6 @@ public function getRole(): Role
3835
return Role::ToolCall;
3936
}
4037

41-
public function getId(): AbstractUid&TimeBasedUidInterface
42-
{
43-
return $this->id;
44-
}
45-
4638
public function getToolCall(): ToolCall
4739
{
4840
return $this->toolCall;

src/platform/src/Message/UserMessage.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,21 @@
1717
use Symfony\AI\Platform\Message\Content\ImageUrl;
1818
use Symfony\AI\Platform\Message\Content\Text;
1919
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
20-
use Symfony\Component\Uid\AbstractUid;
21-
use Symfony\Component\Uid\TimeBasedUidInterface;
2220
use Symfony\Component\Uid\Uuid;
2321

2422
/**
2523
* @author Denis Zunke <[email protected]>
2624
*/
2725
final class UserMessage implements MessageInterface
2826
{
27+
use IdentifierAwareTrait;
2928
use MetadataAwareTrait;
3029

3130
/**
3231
* @var ContentInterface[]
3332
*/
3433
private readonly array $content;
3534

36-
private readonly AbstractUid&TimeBasedUidInterface $id;
37-
3835
public function __construct(
3936
ContentInterface ...$content,
4037
) {
@@ -47,11 +44,6 @@ public function getRole(): Role
4744
return Role::User;
4845
}
4946

50-
public function getId(): AbstractUid&TimeBasedUidInterface
51-
{
52-
return $this->id;
53-
}
54-
5547
/**
5648
* @return ContentInterface[]
5749
*/

src/platform/tests/ContractTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
use Symfony\AI\Platform\Message\Content\Image;
2424
use Symfony\AI\Platform\Message\Content\ImageUrl;
2525
use Symfony\AI\Platform\Message\Content\Text;
26+
use Symfony\AI\Platform\Message\IdentifierAwareTrait;
2627
use Symfony\AI\Platform\Message\Message;
2728
use Symfony\AI\Platform\Message\MessageBag;
2829
use Symfony\AI\Platform\Message\MessageInterface;
2930
use Symfony\AI\Platform\Message\Role;
3031
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
3132
use Symfony\AI\Platform\Model;
32-
use Symfony\Component\Uid\AbstractUid;
33-
use Symfony\Component\Uid\TimeBasedUidInterface;
34-
use Symfony\Component\Uid\Uuid;
3533

3634
final class ContractTest extends TestCase
3735
{
@@ -174,18 +172,14 @@ public static function providePayloadTestCases(): iterable
174172
];
175173

176174
$customSerializableMessage = new class implements MessageInterface, \JsonSerializable {
175+
use IdentifierAwareTrait;
177176
use MetadataAwareTrait;
178177

179178
public function getRole(): Role
180179
{
181180
return Role::User;
182181
}
183182

184-
public function getId(): AbstractUid&TimeBasedUidInterface
185-
{
186-
return Uuid::v7();
187-
}
188-
189183
public function getContent(): array
190184
{
191185
return [new Text('This is a custom serializable message.')];

0 commit comments

Comments
 (0)