@@ -500,15 +500,6 @@ public function generateForSession(int $sessionId, int $agencyConfirm = 0): JSON
500
500
do {
501
501
$ lastUserMessage = array_pop ($ history );
502
502
} while ($ lastUserMessage ->getRole () !== 'human ' );
503
- // history is a list of JSON strings
504
- // we ignore audio attachments here because they are supposed to have been transcribed, the content is the transcription
505
- // this makes the history smaller
506
- $ history = array_map (static function (Message $ message ) {
507
- return json_encode ([
508
- 'role ' => $ message ->getRole (),
509
- 'content ' => $ message ->getContent (),
510
- ]);
511
- }, $ history );
512
503
513
504
$ lastAttachments = $ lastUserMessage ->jsonSerialize ()['attachments ' ];
514
505
$ audioAttachment = $ lastAttachments [0 ] ?? null ;
@@ -522,13 +513,22 @@ public function generateForSession(int $sessionId, int $agencyConfirm = 0): JSON
522
513
&& class_exists ('OCP \\TaskProcessing \\TaskTypes \\AudioToAudioChat ' )
523
514
&& isset ($ this ->taskProcessingManager ->getAvailableTaskTypes ()[\OCP \TaskProcessing \TaskTypes \AudioToAudioChat::ID ])
524
515
) {
516
+ // for an audio chat task, let's try to get the remote audio IDs for all the previous audio messages
517
+ $ history = $ this ->getAudioHistory ($ history );
525
518
$ fileId = $ audioAttachment ['file_id ' ];
526
519
try {
527
520
$ taskId = $ this ->scheduleAudioChatTask ($ fileId , $ systemPrompt , $ history , $ sessionId , $ lastUserMessage ->getId ());
528
521
} catch (\Exception $ e ) {
529
522
return new JSONResponse (['error ' => $ e ->getMessage ()], Http::STATUS_BAD_REQUEST );
530
523
}
531
524
} else {
525
+ // for a text chat task, let's only use text in the history
526
+ $ history = array_map (static function (Message $ message ) {
527
+ return json_encode ([
528
+ 'role ' => $ message ->getRole (),
529
+ 'content ' => $ message ->getContent (),
530
+ ]);
531
+ }, $ history );
532
532
try {
533
533
$ taskId = $ this ->scheduleLLMChatTask ($ lastUserMessage ->getContent (), $ systemPrompt , $ history , $ sessionId );
534
534
} catch (\Exception $ e ) {
@@ -540,6 +540,27 @@ public function generateForSession(int $sessionId, int $agencyConfirm = 0): JSON
540
540
return new JSONResponse (['taskId ' => $ taskId ]);
541
541
}
542
542
543
+ private function getAudioHistory (array $ history ): array {
544
+ // history is a list of JSON strings
545
+ // the content is the remote audio ID (or the transcription as fallback)
546
+ return array_map (static function (Message $ message ) {
547
+ $ entry = [
548
+ 'role ' => $ message ->getRole (),
549
+ ];
550
+ $ attachments = $ message ->jsonSerialize ()['attachments ' ];
551
+ if ($ message ->getRole () === 'assistant '
552
+ && count ($ attachments ) > 0
553
+ && $ attachments [0 ]['type ' ] === 'Audio '
554
+ && isset ($ attachments [0 ]['remote_audio_id ' ])
555
+ ) {
556
+ $ entry ['audio ' ] = ['id ' => $ attachments [0 ]['remote_audio_id ' ]];
557
+ } else {
558
+ $ entry ['content ' ] = $ message ->getContent ();
559
+ }
560
+ return json_encode ($ entry );
561
+ }, $ history );
562
+ }
563
+
543
564
/**
544
565
* Regenerate response for a message
545
566
*
0 commit comments