Skip to content

Commit d0bc545

Browse files
committed
refactor(core): improvements on Uuid handling && MessageNormalizer
1 parent ff85234 commit d0bc545

File tree

7 files changed

+52
-41
lines changed

7 files changed

+52
-41
lines changed

src/chat/src/MessageNormalizer.php

Lines changed: 8 additions & 0 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,6 +74,11 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
7174
default => throw new LogicException(\sprintf('Unknown message type "%s".', $type)),
7275
};
7376

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

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: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,31 @@
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
*/
3229
public function __construct(
3330
private ?string $content = null,
3431
private ?array $toolCalls = null,
3532
) {
36-
$this->id = Uuid::v7();
33+
$this->withId(Uuid::v7());
3734
}
3835

3936
public function getRole(): Role
4037
{
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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): void
25+
{
26+
$this->id = $id;
27+
}
28+
29+
public function getId(): AbstractUid&TimeBasedUidInterface
30+
{
31+
return $this->id;
32+
}
33+
}

src/platform/src/Message/SystemMessage.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,27 @@
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
) {
31-
$this->id = Uuid::v7();
28+
$this->withId(Uuid::v7());
3229
}
3330

3431
public function getRole(): Role
3532
{
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: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,28 @@
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,
3229
) {
33-
$this->id = Uuid::v7();
30+
$this->withId(Uuid::v7());
3431
}
3532

3633
public function getRole(): Role
3734
{
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: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,33 @@
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
) {
4138
$this->content = $content;
42-
$this->id = Uuid::v7();
39+
$this->withId(Uuid::v7());
4340
}
4441

4542
public function getRole(): Role
4643
{
4744
return Role::User;
4845
}
4946

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

0 commit comments

Comments
 (0)