Skip to content

Commit 23e6807

Browse files
committed
Fix TypeError in AssistantMessageNormalizer when toolCalls is null
1 parent e08c3b1 commit 23e6807

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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\Tests\Bridge\Anthropic\Contract;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\DataProvider;
16+
use PHPUnit\Framework\Attributes\Small;
17+
use PHPUnit\Framework\Attributes\UsesClass;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\AI\Platform\Bridge\Anthropic\Claude;
20+
use Symfony\AI\Platform\Bridge\Anthropic\Contract\AssistantMessageNormalizer;
21+
use Symfony\AI\Platform\Contract;
22+
use Symfony\AI\Platform\Message\AssistantMessage;
23+
use Symfony\AI\Platform\Model;
24+
use Symfony\AI\Platform\Result\ToolCall;
25+
26+
#[Small]
27+
#[CoversClass(AssistantMessageNormalizer::class)]
28+
#[UsesClass(Claude::class)]
29+
#[UsesClass(AssistantMessage::class)]
30+
#[UsesClass(Model::class)]
31+
#[UsesClass(ToolCall::class)]
32+
final class AssistantMessageNormalizerTest extends TestCase
33+
{
34+
public function testSupportsNormalization()
35+
{
36+
$normalizer = new AssistantMessageNormalizer();
37+
38+
$this->assertTrue($normalizer->supportsNormalization(new AssistantMessage('Hello'), context: [
39+
Contract::CONTEXT_MODEL => new Claude(),
40+
]));
41+
$this->assertFalse($normalizer->supportsNormalization('not an assistant message'));
42+
}
43+
44+
public function testGetSupportedTypes()
45+
{
46+
$normalizer = new AssistantMessageNormalizer();
47+
48+
$this->assertSame([AssistantMessage::class => true], $normalizer->getSupportedTypes(null));
49+
}
50+
51+
#[DataProvider('normalizeDataProvider')]
52+
public function testNormalize(AssistantMessage $message, array $expectedOutput)
53+
{
54+
$normalizer = new AssistantMessageNormalizer();
55+
56+
$normalized = $normalizer->normalize($message);
57+
58+
$this->assertEquals($expectedOutput, $normalized);
59+
}
60+
61+
/**
62+
* @return iterable<string, array{AssistantMessage, array{text?: string, functionCall?: array{id: string, name: string, args?: mixed}}[]}>
63+
*/
64+
public static function normalizeDataProvider(): iterable
65+
{
66+
yield 'assistant message' => [
67+
new AssistantMessage('Great to meet you. What would you like to know?'),
68+
[
69+
'role' => 'assistant',
70+
'content' => 'Great to meet you. What would you like to know?',
71+
],
72+
];
73+
yield 'function call' => [
74+
new AssistantMessage(toolCalls: [new ToolCall('id1', 'name1', ['arg1' => '123'])]),
75+
[
76+
'role' => 'assistant',
77+
'content' => [
78+
[
79+
'type' => 'tool_use',
80+
'id' => 'id1',
81+
'name' => 'name1',
82+
'input' => ['arg1' => '123'],
83+
],
84+
],
85+
],
86+
];
87+
yield 'function call without parameters' => [
88+
new AssistantMessage(toolCalls: [new ToolCall('id1', 'name1')]),
89+
[
90+
'role' => 'assistant',
91+
'content' => [
92+
[
93+
'type' => 'tool_use',
94+
'id' => 'id1',
95+
'name' => 'name1',
96+
'input' => new \stdClass(),
97+
],
98+
],
99+
],
100+
];
101+
}
102+
}

0 commit comments

Comments
 (0)