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
8 changes: 8 additions & 0 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 <personal@guillaumeloulier.fr>
Expand Down Expand Up @@ -71,6 +74,11 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
default => throw new LogicException(\sprintf('Unknown message type "%s".', $type)),
};

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

$message->withId($existingUuid);

$message->getMetadata()->set([
...$data['metadata'],
'addedAt' => $data['addedAt'],
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
12 changes: 2 additions & 10 deletions src/platform/src/Message/AssistantMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,31 @@

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 <denis.zunke@gmail.com>
*/
final class AssistantMessage implements MessageInterface
{
use IdentifierAwareTrait;
use MetadataAwareTrait;

private readonly AbstractUid&TimeBasedUidInterface $id;

/**
* @param ?ToolCall[] $toolCalls
*/
public function __construct(
private ?string $content = null,
private ?array $toolCalls = null,
) {
$this->id = Uuid::v7();
$this->withId(Uuid::v7());
}

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
33 changes: 33 additions & 0 deletions src/platform/src/Message/IdentifierAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <personal@guillaumeloulier.fr>
*/
trait IdentifierAwareTrait
{
private AbstractUid&TimeBasedUidInterface $id;

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

public function getId(): AbstractUid&TimeBasedUidInterface
{
return $this->id;
}
}
12 changes: 2 additions & 10 deletions src/platform/src/Message/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,27 @@
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 <denis.zunke@gmail.com>
*/
final class SystemMessage implements MessageInterface
{
use IdentifierAwareTrait;
use MetadataAwareTrait;

private readonly AbstractUid&TimeBasedUidInterface $id;

public function __construct(
private readonly string $content,
) {
$this->id = Uuid::v7();
$this->withId(Uuid::v7());
}

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

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

public function getContent(): string
{
return $this->content;
Expand Down
12 changes: 2 additions & 10 deletions src/platform/src/Message/ToolCallMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,28 @@

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 <denis.zunke@gmail.com>
*/
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,
) {
$this->id = Uuid::v7();
$this->withId(Uuid::v7());
}

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

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

public function getToolCall(): ToolCall
{
return $this->toolCall;
Expand Down
12 changes: 2 additions & 10 deletions src/platform/src/Message/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,33 @@
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 <denis.zunke@gmail.com>
*/
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,
) {
$this->content = $content;
$this->id = Uuid::v7();
$this->withId(Uuid::v7());
}

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

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

/**
* @return ContentInterface[]
*/
Expand Down