|
8 | 8 | use EchoLabs\Prism\Providers\Anthropic\Enums\AnthropicCacheType; |
9 | 9 | use EchoLabs\Prism\Providers\Anthropic\Maps\MessageMap; |
10 | 10 | use EchoLabs\Prism\ValueObjects\Messages\AssistantMessage; |
| 11 | +use EchoLabs\Prism\ValueObjects\Messages\Support\Document; |
11 | 12 | use EchoLabs\Prism\ValueObjects\Messages\Support\Image; |
12 | 13 | use EchoLabs\Prism\ValueObjects\Messages\SystemMessage; |
13 | 14 | use EchoLabs\Prism\ValueObjects\Messages\ToolResultMessage; |
|
73 | 74 | ->toBe('image/png'); |
74 | 75 | }); |
75 | 76 |
|
| 77 | +it('maps user messages with documents from path', function (): void { |
| 78 | + $mappedMessage = MessageMap::map([ |
| 79 | + new UserMessage('Here is the document', [ |
| 80 | + Document::fromPath('tests/Fixtures/test-pdf.pdf'), |
| 81 | + ]), |
| 82 | + ]); |
| 83 | + |
| 84 | + expect(data_get($mappedMessage, '0.content.1.type')) |
| 85 | + ->toBe('document'); |
| 86 | + expect(data_get($mappedMessage, '0.content.1.source.type')) |
| 87 | + ->toBe('base64'); |
| 88 | + expect(data_get($mappedMessage, '0.content.1.source.data')) |
| 89 | + ->toContain(base64_encode(file_get_contents('tests/Fixtures/test-pdf.pdf'))); |
| 90 | + expect(data_get($mappedMessage, '0.content.1.source.media_type')) |
| 91 | + ->toBe('application/pdf'); |
| 92 | +}); |
| 93 | + |
| 94 | +it('maps user messages with documents from base64', function (): void { |
| 95 | + $mappedMessage = MessageMap::map([ |
| 96 | + new UserMessage('Here is the document', [ |
| 97 | + Document::fromBase64(base64_encode(file_get_contents('tests/Fixtures/test-pdf.pdf')), 'application/pdf'), |
| 98 | + ]), |
| 99 | + ]); |
| 100 | + |
| 101 | + expect(data_get($mappedMessage, '0.content.1.type')) |
| 102 | + ->toBe('document'); |
| 103 | + expect(data_get($mappedMessage, '0.content.1.source.type')) |
| 104 | + ->toBe('base64'); |
| 105 | + expect(data_get($mappedMessage, '0.content.1.source.data')) |
| 106 | + ->toContain(base64_encode(file_get_contents('tests/Fixtures/test-pdf.pdf'))); |
| 107 | + expect(data_get($mappedMessage, '0.content.1.source.media_type')) |
| 108 | + ->toBe('application/pdf'); |
| 109 | +}); |
| 110 | + |
76 | 111 | it('does not maps user messages with images from url', function (): void { |
77 | 112 | $this->expectException(InvalidArgumentException::class); |
78 | 113 | MessageMap::map([ |
|
215 | 250 | ]]); |
216 | 251 | }); |
217 | 252 |
|
| 253 | +it('sets the cache type on a UserMessage document if cacheType providerMeta is set on message', function (): void { |
| 254 | + expect(MessageMap::map([ |
| 255 | + (new UserMessage( |
| 256 | + content: 'Who are you?', |
| 257 | + additionalContent: [Document::fromPath('tests/Fixtures/test-pdf.pdf')] |
| 258 | + ))->withProviderMeta(Provider::Anthropic, ['cacheType' => 'ephemeral']), |
| 259 | + ]))->toBe([[ |
| 260 | + 'role' => 'user', |
| 261 | + 'content' => [ |
| 262 | + [ |
| 263 | + 'type' => 'text', |
| 264 | + 'text' => 'Who are you?', |
| 265 | + 'cache_control' => ['type' => 'ephemeral'], |
| 266 | + ], |
| 267 | + [ |
| 268 | + 'type' => 'document', |
| 269 | + 'source' => [ |
| 270 | + 'type' => 'base64', |
| 271 | + 'media_type' => 'application/pdf', |
| 272 | + 'data' => base64_encode(file_get_contents('tests/Fixtures/test-pdf.pdf')), |
| 273 | + ], |
| 274 | + 'cache_control' => ['type' => 'ephemeral'], |
| 275 | + ], |
| 276 | + ], |
| 277 | + ]]); |
| 278 | +}); |
| 279 | + |
218 | 280 | it('sets the cache type on an AssistantMessage if cacheType providerMeta is set on message', function (mixed $cacheType): void { |
219 | 281 | expect(MessageMap::map([ |
220 | 282 | (new AssistantMessage(content: 'Who are you?'))->withProviderMeta(Provider::Anthropic, ['cacheType' => $cacheType]), |
|
0 commit comments