Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/chat/src/MessageNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\TimeBasedUidInterface;
use Symfony\Component\Uid\Uuid;

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

$message->getMetadata()->set([
/** @var AbstractUid&TimeBasedUidInterface&Uuid $existingUuid */
$existingUuid = Uuid::fromString($data['id']);

$messageWithExistingUuid = $message->withId($existingUuid);

$messageWithExistingUuid->getMetadata()->set([
...$data['metadata'],
'addedAt' => $data['addedAt'],
]);

return $message;
return $messageWithExistingUuid;
}

public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
Expand Down
4 changes: 3 additions & 1 deletion src/chat/tests/MessageNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ public function testItCanNormalize()

public function testItCanDenormalize()
{
$uuid = Uuid::v7()->toRfc4122();
$normalizer = new MessageNormalizer();

$message = $normalizer->denormalize([
'id' => Uuid::v7()->toRfc4122(),
'id' => $uuid,
'type' => UserMessage::class,
'content' => '',
'contentAsBase64' => [
Expand All @@ -71,6 +72,7 @@ public function testItCanDenormalize()
'addedAt' => (new \DateTimeImmutable())->getTimestamp(),
], MessageInterface::class);

$this->assertSame($uuid, $message->getId()->toRfc4122());
$this->assertSame(Role::User, $message->getRole());
$this->assertArrayHasKey('addedAt', $message->getMetadata()->all());
}
Expand Down
10 changes: 1 addition & 9 deletions src/platform/src/Message/AssistantMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@

use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
use Symfony\AI\Platform\Result\ToolCall;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\TimeBasedUidInterface;
use Symfony\Component\Uid\Uuid;

/**
* @author Denis Zunke <[email protected]>
*/
final class AssistantMessage implements MessageInterface
{
use IdentifierAwareTrait;
use MetadataAwareTrait;

private readonly AbstractUid&TimeBasedUidInterface $id;

/**
* @param ?ToolCall[] $toolCalls
*/
Expand All @@ -41,11 +38,6 @@ public function getRole(): Role
return Role::Assistant;
}

public function getId(): AbstractUid&TimeBasedUidInterface
{
return $this->id;
}

public function hasToolCalls(): bool
{
return null !== $this->toolCalls && [] !== $this->toolCalls;
Expand Down
36 changes: 36 additions & 0 deletions src/platform/src/Message/IdentifierAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Message;

use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\TimeBasedUidInterface;

/**
* @author Guillaume Loulier <[email protected]>
*/
trait IdentifierAwareTrait
{
private AbstractUid&TimeBasedUidInterface $id;

public function withId(AbstractUid&TimeBasedUidInterface $id): self
{
$clone = clone $this;
$clone->id = $id;

return $clone;
}

public function getId(): AbstractUid&TimeBasedUidInterface
{
return $this->id;
}
}
2 changes: 2 additions & 0 deletions src/platform/src/Message/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function getRole(): Role;

public function getId(): AbstractUid&TimeBasedUidInterface;

public function withId(AbstractUid&TimeBasedUidInterface $id): self;

/**
* @return string|ContentInterface[]|null
*/
Expand Down
10 changes: 1 addition & 9 deletions src/platform/src/Message/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
namespace Symfony\AI\Platform\Message;

use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\TimeBasedUidInterface;
use Symfony\Component\Uid\Uuid;

/**
* @author Denis Zunke <[email protected]>
*/
final class SystemMessage implements MessageInterface
{
use IdentifierAwareTrait;
use MetadataAwareTrait;

private readonly AbstractUid&TimeBasedUidInterface $id;

public function __construct(
private readonly string $content,
) {
Expand All @@ -36,11 +33,6 @@ public function getRole(): Role
return Role::System;
}

public function getId(): AbstractUid&TimeBasedUidInterface
{
return $this->id;
}

public function getContent(): string
{
return $this->content;
Expand Down
10 changes: 1 addition & 9 deletions src/platform/src/Message/ToolCallMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@

use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
use Symfony\AI\Platform\Result\ToolCall;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\TimeBasedUidInterface;
use Symfony\Component\Uid\Uuid;

/**
* @author Denis Zunke <[email protected]>
*/
final class ToolCallMessage implements MessageInterface
{
use IdentifierAwareTrait;
use MetadataAwareTrait;

private readonly AbstractUid&TimeBasedUidInterface $id;

public function __construct(
private readonly ToolCall $toolCall,
private readonly string $content,
Expand All @@ -38,11 +35,6 @@ public function getRole(): Role
return Role::ToolCall;
}

public function getId(): AbstractUid&TimeBasedUidInterface
{
return $this->id;
}

public function getToolCall(): ToolCall
{
return $this->toolCall;
Expand Down
10 changes: 1 addition & 9 deletions src/platform/src/Message/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@
use Symfony\AI\Platform\Message\Content\ImageUrl;
use Symfony\AI\Platform\Message\Content\Text;
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\TimeBasedUidInterface;
use Symfony\Component\Uid\Uuid;

/**
* @author Denis Zunke <[email protected]>
*/
final class UserMessage implements MessageInterface
{
use IdentifierAwareTrait;
use MetadataAwareTrait;

/**
* @var ContentInterface[]
*/
private readonly array $content;

private readonly AbstractUid&TimeBasedUidInterface $id;

public function __construct(
ContentInterface ...$content,
) {
Expand All @@ -47,11 +44,6 @@ public function getRole(): Role
return Role::User;
}

public function getId(): AbstractUid&TimeBasedUidInterface
{
return $this->id;
}

/**
* @return ContentInterface[]
*/
Expand Down
10 changes: 2 additions & 8 deletions src/platform/tests/ContractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
use Symfony\AI\Platform\Message\Content\Image;
use Symfony\AI\Platform\Message\Content\ImageUrl;
use Symfony\AI\Platform\Message\Content\Text;
use Symfony\AI\Platform\Message\IdentifierAwareTrait;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Message\MessageInterface;
use Symfony\AI\Platform\Message\Role;
use Symfony\AI\Platform\Metadata\MetadataAwareTrait;
use Symfony\AI\Platform\Model;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\TimeBasedUidInterface;
use Symfony\Component\Uid\Uuid;

final class ContractTest extends TestCase
{
Expand Down Expand Up @@ -174,18 +172,14 @@ public static function providePayloadTestCases(): iterable
];

$customSerializableMessage = new class implements MessageInterface, \JsonSerializable {
use IdentifierAwareTrait;
use MetadataAwareTrait;

public function getRole(): Role
{
return Role::User;
}

public function getId(): AbstractUid&TimeBasedUidInterface
{
return Uuid::v7();
}

public function getContent(): array
{
return [new Text('This is a custom serializable message.')];
Expand Down