Skip to content

Commit 3bde1cf

Browse files
committed
feat(audio-chat): store and use potential remote_audio_expires_at
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 397f366 commit 3bde1cf

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

lib/Controller/ChattyLLMController.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ public function generateForSession(int $sessionId, int $agencyConfirm = 0): JSON
571571
private function getAudioHistory(array $history): array {
572572
// history is a list of JSON strings
573573
// the content is the remote audio ID (or the transcription as fallback)
574+
// we only use the audio ID for assistant messages, if we have one and if it's not expired
574575
return array_map(static function (Message $message) {
575576
$entry = [
576577
'role' => $message->getRole(),
@@ -581,10 +582,15 @@ private function getAudioHistory(array $history): array {
581582
&& $attachments[0]['type'] === 'Audio'
582583
&& isset($attachments[0]['remote_audio_id'])
583584
) {
584-
$entry['audio'] = ['id' => $attachments[0]['remote_audio_id']];
585-
} else {
586-
$entry['content'] = $message->getContent();
585+
if (!isset($attachments[0]['remote_audio_expires_at'])
586+
|| time() < $attachments[0]['remote_audio_expires_at']
587+
) {
588+
$entry['audio'] = ['id' => $attachments[0]['remote_audio_id']];
589+
return json_encode($entry);
590+
}
587591
}
592+
593+
$entry['content'] = $message->getContent();
588594
return json_encode($entry);
589595
}, $history);
590596
}

lib/Listener/ChattyLLMTaskListener.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,39 @@ public function handle(Event $event): void {
6666
$isAgencyAudioChat = class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentAudioInteraction')
6767
&& $taskTypeId === \OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction::ID;
6868

69+
$taskOutput = $task->getOutput();
70+
6971
$message = new Message();
7072
$message->setSessionId($sessionId);
7173
$message->setOcpTaskId($task->getId());
7274
$message->setRole('assistant');
7375
$message->setTimestamp(time());
74-
$sources = json_encode($task->getOutput()['sources'] ?? []);
76+
$sources = json_encode($taskOutput['sources'] ?? []);
7577
$message->setSources($sources ?: '[]');
7678
$message->setAttachments('[]');
7779
if ($isAudioChat || $isAgencyAudioChat) {
78-
$outputTranscript = trim($task->getOutput()['output_transcript'] ?? '');
80+
$outputTranscript = trim($taskOutput['output_transcript'] ?? '');
7981
$message->setContent($outputTranscript);
8082
// agency might not return any output but just ask for confirmation
8183
if ($outputTranscript !== '') {
82-
$attachment = ['type' => 'Audio', 'file_id' => $task->getOutput()['output']];
83-
if (isset($task->getOutput()['audio_id'])) {
84-
$attachment['remote_audio_id'] = $task->getOutput()['audio_id'];
84+
$attachment = ['type' => 'Audio', 'file_id' => $taskOutput['output']];
85+
if (isset($taskOutput['audio_id'])) {
86+
$attachment['remote_audio_id'] = $taskOutput['audio_id'];
87+
if (isset($taskOutput['audio_expires_at'])) {
88+
$attachment['remote_audio_expires_at'] = $taskOutput['audio_expires_at'];
89+
}
8590
}
8691
$message->setAttachments(json_encode([$attachment]));
8792
}
8893
// now we have the transcription of the user audio input
8994
if (preg_match('/^chatty-llm:\d+:(\d+)$/', $customId, $matches)) {
9095
$queryMessageId = (int)$matches[1];
9196
$queryMessage = $this->messageMapper->getMessageById($queryMessageId);
92-
$queryMessage->setContent(trim($task->getOutput()['input_transcript'] ?? ''));
97+
$queryMessage->setContent(trim($taskOutput['input_transcript'] ?? ''));
9398
$this->messageMapper->update($queryMessage);
9499
}
95100
} else {
96-
$content = trim($task->getOutput()['output'] ?? '');
101+
$content = trim($taskOutput['output'] ?? '');
97102
$message->setContent($content);
98103
$this->runTtsIfNeeded($sessionId, $message, $taskTypeId, $task->getUserId());
99104
}
@@ -106,8 +111,8 @@ public function handle(Event $event): void {
106111
// store the conversation token and the actions if we are using the agency feature
107112
if ($isAgency || $isAgencyAudioChat) {
108113
$session = $this->sessionMapper->getUserSession($task->getUserId(), $sessionId);
109-
$conversationToken = ($task->getOutput()['conversation_token'] ?? null) ?: null;
110-
$pendingActions = ($task->getOutput()['actions'] ?? null) ?: null;
114+
$conversationToken = ($taskOutput['conversation_token'] ?? null) ?: null;
115+
$pendingActions = ($taskOutput['actions'] ?? null) ?: null;
111116
$session->setAgencyConversationToken($conversationToken);
112117
$session->setAgencyPendingActions($pendingActions);
113118
$this->sessionMapper->update($session);

0 commit comments

Comments
 (0)