|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\TestCase; |
| 15 | +use Symfony\AI\Platform\Bridge\Anthropic\Claude; |
| 16 | +use Symfony\AI\Platform\Bridge\Anthropic\Contract\AssistantMessageNormalizer; |
| 17 | +use Symfony\AI\Platform\Contract; |
| 18 | +use Symfony\AI\Platform\Message\AssistantMessage; |
| 19 | +use Symfony\AI\Platform\Result\ToolCall; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Oskar Stark <oskarstark@googlemail.com> |
| 23 | + */ |
| 24 | +final class AssistantMessageNormalizerTest extends TestCase |
| 25 | +{ |
| 26 | + private AssistantMessageNormalizer $normalizer; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->normalizer = new AssistantMessageNormalizer(); |
| 31 | + } |
| 32 | + |
| 33 | + public function testSupportsNormalization() |
| 34 | + { |
| 35 | + $model = new Claude(Claude::HAIKU_35); |
| 36 | + $context = [Contract::CONTEXT_MODEL => $model]; |
| 37 | + |
| 38 | + $this->assertTrue($this->normalizer->supportsNormalization(new AssistantMessage('content'), null, $context)); |
| 39 | + $this->assertFalse($this->normalizer->supportsNormalization(new AssistantMessage('content'), null, [])); |
| 40 | + $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass(), null, $context)); |
| 41 | + } |
| 42 | + |
| 43 | + public function testGetSupportedTypes() |
| 44 | + { |
| 45 | + $this->assertSame([AssistantMessage::class => true], $this->normalizer->getSupportedTypes(null)); |
| 46 | + } |
| 47 | + |
| 48 | + public function testNormalizeWithContent() |
| 49 | + { |
| 50 | + $message = new AssistantMessage('Hello, I am an assistant.'); |
| 51 | + |
| 52 | + $this->assertSame([ |
| 53 | + 'role' => 'assistant', |
| 54 | + 'content' => 'Hello, I am an assistant.', |
| 55 | + ], $this->normalizer->normalize($message)); |
| 56 | + } |
| 57 | + |
| 58 | + public function testNormalizeWithNullContentProducesEmptyString() |
| 59 | + { |
| 60 | + $message = new AssistantMessage(null); |
| 61 | + |
| 62 | + $this->assertSame([ |
| 63 | + 'role' => 'assistant', |
| 64 | + 'content' => '', |
| 65 | + ], $this->normalizer->normalize($message)); |
| 66 | + } |
| 67 | + |
| 68 | + public function testNormalizeWithToolCalls() |
| 69 | + { |
| 70 | + $toolCall = new ToolCall('tool-id', 'some_tool', ['param' => 'value']); |
| 71 | + $message = new AssistantMessage(null, [$toolCall]); |
| 72 | + |
| 73 | + $this->assertSame([ |
| 74 | + 'role' => 'assistant', |
| 75 | + 'content' => [ |
| 76 | + [ |
| 77 | + 'type' => 'tool_use', |
| 78 | + 'id' => 'tool-id', |
| 79 | + 'name' => 'some_tool', |
| 80 | + 'input' => ['param' => 'value'], |
| 81 | + ], |
| 82 | + ], |
| 83 | + ], $this->normalizer->normalize($message)); |
| 84 | + } |
| 85 | + |
| 86 | + public function testNormalizeWithToolCallsAndContent() |
| 87 | + { |
| 88 | + $toolCall = new ToolCall('tool-id', 'some_tool', ['param' => 'value']); |
| 89 | + $message = new AssistantMessage('Some text before tool use.', [$toolCall]); |
| 90 | + |
| 91 | + $this->assertSame([ |
| 92 | + 'role' => 'assistant', |
| 93 | + 'content' => [ |
| 94 | + ['type' => 'text', 'text' => 'Some text before tool use.'], |
| 95 | + [ |
| 96 | + 'type' => 'tool_use', |
| 97 | + 'id' => 'tool-id', |
| 98 | + 'name' => 'some_tool', |
| 99 | + 'input' => ['param' => 'value'], |
| 100 | + ], |
| 101 | + ], |
| 102 | + ], $this->normalizer->normalize($message)); |
| 103 | + } |
| 104 | + |
| 105 | + public function testNormalizeWithThinkingContent() |
| 106 | + { |
| 107 | + $message = new AssistantMessage(null, null, 'Let me think about this...', 'sig-abc'); |
| 108 | + |
| 109 | + $this->assertSame([ |
| 110 | + 'role' => 'assistant', |
| 111 | + 'content' => [ |
| 112 | + [ |
| 113 | + 'type' => 'thinking', |
| 114 | + 'thinking' => 'Let me think about this...', |
| 115 | + 'signature' => 'sig-abc', |
| 116 | + ], |
| 117 | + ], |
| 118 | + ], $this->normalizer->normalize($message)); |
| 119 | + } |
| 120 | + |
| 121 | + public function testNormalizeWithThinkingContentAndText() |
| 122 | + { |
| 123 | + $message = new AssistantMessage('The answer is 42.', null, 'Let me think about this...', 'sig-abc'); |
| 124 | + |
| 125 | + $this->assertSame([ |
| 126 | + 'role' => 'assistant', |
| 127 | + 'content' => [ |
| 128 | + [ |
| 129 | + 'type' => 'thinking', |
| 130 | + 'thinking' => 'Let me think about this...', |
| 131 | + 'signature' => 'sig-abc', |
| 132 | + ], |
| 133 | + ['type' => 'text', 'text' => 'The answer is 42.'], |
| 134 | + ], |
| 135 | + ], $this->normalizer->normalize($message)); |
| 136 | + } |
| 137 | +} |
0 commit comments