@@ -50,10 +50,7 @@ public function getMessages(): array
5050 {
5151 $ messages = $ this ->request ('POST ' , \sprintf ('indexes/%s/documents/fetch ' , $ this ->indexName ));
5252
53- return array_map (
54- fn (array $ message ): MessageInterface => $ this ->convertToMessage ($ message ),
55- $ messages ['results ' ],
56- );
53+ return array_map ($ this ->convertToMessage (...), $ messages ['results ' ]);
5754 }
5855
5956 public function getSystemMessage (): ?SystemMessage
@@ -157,7 +154,7 @@ private function request(string $method, string $endpoint, array $payload = []):
157154 'headers ' => [
158155 'Authorization ' => \sprintf ('Bearer %s ' , $ this ->apiKey ),
159156 ],
160- 'json ' => $ payload ,
157+ 'json ' => [] !== $ payload ? $ payload : new \ stdClass () ,
161158 ]);
162159
163160 return $ result ->toArray ();
@@ -193,13 +190,14 @@ private function convertToMessage(array $payload): MessageInterface
193190 $ type = $ payload ['type ' ];
194191 $ content = $ payload ['content ' ] ?? '' ;
195192 $ contentAsBase64 = $ payload ['contentAsBase64 ' ] ?? [];
196- $ toolsCalls = $ payload ['toolsCalls ' ] ?? [];
197193
198194 return match ($ type ) {
199195 SystemMessage::class => new SystemMessage ($ content ),
200- AssistantMessage::class => new AssistantMessage ($ content , $ toolsCalls ),
196+ AssistantMessage::class => new AssistantMessage ($ content , $ payload [ ' toolsCalls ' ] ?? [] ),
201197 UserMessage::class => new UserMessage (...array_map (
202- static fn (array $ contentAsBase64 ) => $ contentAsBase64 ['type ' ]::fromDataUrl ($ contentAsBase64 ['content ' ]),
198+ static fn (array $ contentAsBase64 ) => \in_array ($ contentAsBase64 ['type ' ], [File::class, Image::class, Audio::class], true )
199+ ? $ contentAsBase64 ['type ' ]::fromDataUrl ($ contentAsBase64 ['content ' ])
200+ : new $ contentAsBase64 ['type ' ]($ contentAsBase64 ['content ' ]),
203201 $ contentAsBase64 ,
204202 )),
205203 default => throw new \LogicException (\sprintf ('Unknown message type "%s". ' , $ type )),
0 commit comments