@@ -433,39 +433,43 @@ public function scheduleMessageGeneration(?string $userId, int $sessionId, int $
433433 $ fileId = $ audioAttachment ['file_id ' ];
434434 $ taskId = $ this ->scheduleAudioChatTask ($ userId , $ fileId , $ systemPrompt , $ history , $ sessionId , $ lastUserMessage ->getId ());
435435 } else {
436- // for a text chat task, let's only use text in the history
437- $ historyMessages = array_map (static function (Message $ message ) {
438- return json_encode ([
439- 'role ' => $ message ->getRole (),
440- 'content ' => $ message ->getContent (),
441- ]);
442- }, $ history );
443436 if ($ this ->isMultimodalChatAvailable ()) {
444- // for a multimodal chat task, let's use the attachments in the history as we don't know the structure in history
437+ // for a multimodal chat also attachments need to be added to the history
445438 $ assistantService = $ this ->assistantService ;
446- $ attachmentsHistory = array_map (static function (Message $ message ) use ($ userId , $ assistantService ) {
447- // Make sure that the user has access to the output files
448- if ($ message ->getRole () === Message::ROLE_ASSISTANT ) {
449- $ attachments = $ message ->jsonSerialize ()['attachments ' ];
450- $ result = [];
451- foreach ($ attachments as $ attachment ) {
439+ $ historyMessages = array_map (static function (Message $ message ) use ($ userId , $ assistantService ) {
440+ $ attachments = $ message ->jsonSerialize ()['attachments ' ];
441+ // Attachments that were generated need to be saved in the user's files so they are ac
442+ $ content = array_map (static function (array $ attachment ) use ($ userId , $ assistantService , $ message ) {
443+ if ($ message ->getRole () === Message::ROLE_ASSISTANT ) {
452444 $ info = $ assistantService ->saveOutputFile ($ userId , $ message ->getOcpTaskId (), $ attachment ['file_id ' ]);
453- $ result [] = [
454- 'type ' => 'File ' ,
445+ return [
446+ 'type ' => 'file ' ,
455447 'file_id ' => $ info ['fileId ' ],
456448 ];
457449 }
458- return $ result ;
459- }
460- return $ message ->jsonSerialize ()['attachments ' ];
450+ return ['type ' => 'file ' , 'file_id ' => $ attachment ['file_id ' ]];
451+ }, $ attachments );
452+ $ content [] = [
453+ 'type ' => 'text ' ,
454+ 'text ' => $ message ->getContent (),
455+ ];
456+ return json_encode ([
457+ 'role ' => $ message ->getRole (),
458+ 'content ' => $ content ,
459+ ]);
461460 }, $ history );
462- // Just add all the attachments in the history as attachments for the latest message as we can't know the structure in history
463- $ attachmentsHistory = array_merge ($ lastAttachments , ...$ attachmentsHistory );
464- $ attachmentsHistory = array_map (static function (array $ attachment ) {
461+ $ lastAttachments = array_map (static function (array $ attachment ) {
465462 return $ attachment ['file_id ' ];
466- }, $ attachmentsHistory );
467- $ taskId = $ this ->scheduleMultimodalChatTask ($ userId , $ lastUserMessage ->getContent (), $ systemPrompt , $ historyMessages , $ sessionId , $ attachmentsHistory );
463+ }, $ lastAttachments );
464+ $ taskId = $ this ->scheduleMultimodalChatTask ($ userId , $ lastUserMessage ->getContent (), $ systemPrompt , $ historyMessages , $ sessionId , $ lastAttachments );
468465 } else {
466+ // for a text chat task, let's only use text in the history
467+ $ historyMessages = array_map (static function (Message $ message ) {
468+ return json_encode ([
469+ 'role ' => $ message ->getRole (),
470+ 'content ' => $ message ->getContent (),
471+ ]);
472+ }, $ history );
469473 $ taskId = $ this ->scheduleLLMChatTask ($ userId , $ lastUserMessage ->getContent (), $ systemPrompt , $ historyMessages , $ sessionId );
470474 }
471475 }
0 commit comments