Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ToolCallNormalizer extends ModelContractNormalizer
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'arguments' => json_encode($data->getArguments() ?: new \stdClass()),
'arguments' => json_encode($data->getArguments() ?: new \stdClass(), \JSON_UNESCAPED_SLASHES),
'call_id' => $data->getId(),
'name' => $data->getName(),
'type' => 'function_call',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function normalize(mixed $data, ?string $format = null, array $context =
'type' => 'function',
'function' => [
'name' => $data->getName(),
'arguments' => json_encode($data->getArguments() ?: new \stdClass()),
'arguments' => json_encode($data->getArguments() ?: new \stdClass(), \JSON_UNESCAPED_SLASHES),
],
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\Tests\Bridge\OpenResponses\Contract;

use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\OpenResponses\Contract\ToolCallNormalizer;
use Symfony\AI\Platform\Bridge\OpenResponses\ResponsesModel;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Result\ToolCall;

final class ToolCallNormalizerTest extends TestCase
{
private ToolCallNormalizer $normalizer;

protected function setUp(): void
{
$this->normalizer = new ToolCallNormalizer();
}

public function testSupportsNormalization()
{
$context = [Contract::CONTEXT_MODEL => new ResponsesModel('gpt-4o')];

$this->assertTrue($this->normalizer->supportsNormalization(new ToolCall('id', 'function'), null, $context));
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass(), null, $context));
}

public function testNormalize()
{
$toolCall = new ToolCall('call_123', 'get_weather', ['location' => 'Paris']);

$this->assertSame([
'arguments' => '{"location":"Paris"}',
'call_id' => 'call_123',
'name' => 'get_weather',
'type' => 'function_call',
], $this->normalizer->normalize($toolCall));
}

public function testNormalizeKeepsForwardSlashesUnescaped()
{
$toolCall = new ToolCall('call_123', 'query_assets', ['path' => '/de/shop']);

$normalized = $this->normalizer->normalize($toolCall);

$this->assertSame('{"path":"/de/shop"}', $normalized['arguments']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\Tests\Contract\Normalizer\Result;

use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Contract\Normalizer\Result\ToolCallNormalizer;
use Symfony\AI\Platform\Result\ToolCall;

final class ToolCallNormalizerTest extends TestCase
{
private ToolCallNormalizer $normalizer;

protected function setUp(): void
{
$this->normalizer = new ToolCallNormalizer();
}

public function testSupportsNormalization()
{
$this->assertTrue($this->normalizer->supportsNormalization(new ToolCall('id', 'function')));
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}

public function testGetSupportedTypes()
{
$this->assertSame([ToolCall::class => true], $this->normalizer->getSupportedTypes(null));
}

public function testNormalize()
{
$toolCall = new ToolCall('tool_call_123', 'get_weather', ['location' => 'Paris']);

$this->assertSame([
'id' => 'tool_call_123',
'type' => 'function',
'function' => [
'name' => 'get_weather',
'arguments' => '{"location":"Paris"}',
],
], $this->normalizer->normalize($toolCall));
}

public function testNormalizeKeepsForwardSlashesUnescaped()
{
$toolCall = new ToolCall('tool_call_123', 'query_assets', ['path' => '/de/shop']);

$normalized = $this->normalizer->normalize($toolCall);

$this->assertSame('{"path":"/de/shop"}', $normalized['function']['arguments']);
}

public function testNormalizeWithoutArguments()
{
$normalized = $this->normalizer->normalize(new ToolCall('tool_call_123', 'now'));

$this->assertSame('{}', $normalized['function']['arguments']);
}
}
Loading