Skip to content

Commit 7f5aae7

Browse files
committed
Handle history for attachments correctly
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
1 parent 9871d80 commit 7f5aae7

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

lib/Listener/ChattyLLMTaskListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public function handle(Event $event): void {
8484
&& $taskTypeId === \OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction::ID;
8585
$isMultimodalChat = class_exists('OCP\\TaskProcessing\\TaskTypes\\MultimodalChatWithTools')
8686
&& $taskTypeId === \OCP\TaskProcessing\TaskTypes\MultimodalChatWithTools::ID;
87+
$isMultimodalAgencyChat = class_exists('OCP\\TaskProcessing\\TaskTypes\\MultimodalContextAgentInteraction')
88+
&& $taskTypeId === \OCP\TaskProcessing\TaskTypes\MultimodalContextAgentInteraction::ID;
8789

8890
$taskOutput = $task->getOutput();
8991

@@ -150,7 +152,7 @@ public function handle(Event $event): void {
150152
$session = $this->sessionMapper->getUserSession($task->getUserId(), $sessionId);
151153

152154
// store the conversation token and the actions if we are using the agency feature
153-
if ($isAgency || $isAgencyAudioChat) {
155+
if ($isAgency || $isAgencyAudioChat || $isMultimodalAgencyChat) {
154156
$conversationToken = ($taskOutput['conversation_token'] ?? null) ?: null;
155157
$pendingActions = ($taskOutput['actions'] ?? null) ?: null;
156158
$session->setAgencyConversationToken($conversationToken);

lib/Service/ChatService.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)