Skip to content

Commit afda737

Browse files
authored
Merge pull request #378 from nextcloud/fix/377/check-ContextAgentInteraction-exists
Check if ContextAgentInteraction exists
2 parents 5c23bd0 + 1b7640c commit afda737

8 files changed

Lines changed: 63 additions & 35 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"nextcloud/ocp": "dev-master",
3535
"nextcloud/openapi-extractor": "^1.0.0",
3636
"phpunit/phpunit": "^9.5",
37-
"psalm/phar": "6.4.0"
37+
"psalm/phar": "6.7"
3838
},
3939
"config": {
4040
"sort-packages": true,

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Controller/ChattyLLMController.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,15 @@ public function generateForSession(int $sessionId, int $agencyConfirm = 0): JSON
500500

501501
$lastAttachments = $lastUserMessage->jsonSerialize()['attachments'];
502502
$audioAttachment = $lastAttachments[0] ?? null;
503+
// see https://github.com/vimeo/psalm/issues/7980
504+
$isContextAgentAudioAvailable = false;
505+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentAudioInteraction')) {
506+
$isContextAgentAudioAvailable = isset($this->taskProcessingManager->getAvailableTaskTypes()[\OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction::ID]);
507+
}
503508
if ($audioAttachment !== null
504509
&& isset($audioAttachment['type'])
505510
&& $audioAttachment['type'] === 'Audio'
506-
&& class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentAudioInteraction')
507-
&& isset($this->taskProcessingManager->getAvailableTaskTypes()[\OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction::ID])
511+
&& $isContextAgentAudioAvailable
508512
) {
509513
// audio agency
510514
$fileId = $audioAttachment['file_id'];
@@ -536,11 +540,14 @@ public function generateForSession(int $sessionId, int $agencyConfirm = 0): JSON
536540

537541
$lastAttachments = $lastUserMessage->jsonSerialize()['attachments'];
538542
$audioAttachment = $lastAttachments[0] ?? null;
543+
$isAudioToAudioAvailable = false;
544+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToAudioChat')) {
545+
$isAudioToAudioAvailable = isset($this->taskProcessingManager->getAvailableTaskTypes()[\OCP\TaskProcessing\TaskTypes\AudioToAudioChat::ID]);
546+
}
539547
if ($audioAttachment !== null
540548
&& isset($audioAttachment['type'])
541549
&& $audioAttachment['type'] === 'Audio'
542-
&& class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToAudioChat')
543-
&& isset($this->taskProcessingManager->getAvailableTaskTypes()[\OCP\TaskProcessing\TaskTypes\AudioToAudioChat::ID])
550+
&& $isAudioToAudioAvailable
544551
) {
545552
// for an audio chat task, let's try to get the remote audio IDs for all the previous audio messages
546553
$history = $this->getAudioHistory($history);
@@ -1006,6 +1013,7 @@ private function scheduleAgencyTask(string $content, int $confirmation, string $
10061013
'confirmation' => $confirmation,
10071014
'conversation_token' => $conversationToken,
10081015
];
1016+
/** @psalm-suppress UndefinedClass */
10091017
$task = new Task(
10101018
\OCP\TaskProcessing\TaskTypes\ContextAgentInteraction::ID,
10111019
$taskInput,
@@ -1027,6 +1035,7 @@ private function scheduleAudioChatTask(
10271035
'system_prompt' => $systemPrompt,
10281036
'history' => $history,
10291037
];
1038+
/** @psalm-suppress UndefinedClass */
10301039
$task = new Task(
10311040
\OCP\TaskProcessing\TaskTypes\AudioToAudioChat::ID,
10321041
$input,
@@ -1048,6 +1057,7 @@ private function scheduleAgencyAudioTask(
10481057
'confirmation' => $confirmation,
10491058
'conversation_token' => $conversationToken,
10501059
];
1060+
/** @psalm-suppress UndefinedClass */
10511061
$task = new Task(
10521062
\OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction::ID,
10531063
$taskInput,

lib/Listener/ChattyLLMTaskListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public function handle(Event $event): void {
105105
} else {
106106
$content = trim($taskOutput['output'] ?? '');
107107
$message->setContent($content);
108+
// the task is not an audio one, but we might still need to Tts the answer
109+
// if it is a response to a ContextAgentInteraction confirmation that was asked about an audio message
108110
$this->runTtsIfNeeded($sessionId, $message, $taskTypeId, $task->getUserId());
109111
}
110112
try {
@@ -135,7 +137,8 @@ public function handle(Event $event): void {
135137
* @return void
136138
*/
137139
private function runTtsIfNeeded(int $sessionId, Message $message, string $taskTypeId, ?string $userId): void {
138-
if ($taskTypeId !== \OCP\TaskProcessing\TaskTypes\ContextAgentInteraction::ID) {
140+
if (!class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentInteraction')
141+
|| $taskTypeId !== \OCP\TaskProcessing\TaskTypes\ContextAgentInteraction::ID) {
139142
return;
140143
}
141144
// is the last non-empty user message an audio one?
@@ -157,6 +160,7 @@ private function runTtsIfNeeded(int $sessionId, Message $message, string $taskTy
157160
*/
158161
private function runTtsTask(Message $message, ?string $userId): void {
159162
try {
163+
/** @psalm-suppress UndefinedClass */
160164
$task = new Task(
161165
\OCP\TaskProcessing\TaskTypes\TextToSpeech::ID,
162166
['input' => $message->getContent()],

lib/Service/AssistantService.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,19 @@ public function cancelNotifyWhenReady(int $taskId, string $userId): void {
214214

215215
public function isAudioChatAvailable(): bool {
216216
$availableTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
217+
$ttsAvailable = false;
218+
// see https://github.com/vimeo/psalm/issues/7980
219+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech')) {
220+
$ttsAvailable = array_key_exists(\OCP\TaskProcessing\TaskTypes\TextToSpeech::ID, $availableTaskTypes);
221+
}
222+
$audioToAudioAvailable = false;
223+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToAudioChat')) {
224+
$audioToAudioAvailable = array_key_exists(\OCP\TaskProcessing\TaskTypes\AudioToAudioChat::ID, $availableTaskTypes);
225+
}
217226
// we have at least the simple audio chat task type and the 3 sub task types available
218-
return class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToAudioChat')
219-
&& array_key_exists(\OCP\TaskProcessing\TaskTypes\AudioToAudioChat::ID, $availableTaskTypes)
227+
return $audioToAudioAvailable
228+
&& $ttsAvailable
220229
&& array_key_exists(AudioToText::ID, $availableTaskTypes)
221-
&& class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech')
222-
&& array_key_exists(\OCP\TaskProcessing\TaskTypes\TextToSpeech::ID, $availableTaskTypes)
223230
&& array_key_exists(TextToTextChat::ID, $availableTaskTypes);
224231
}
225232

@@ -291,21 +298,26 @@ public function getAvailableTaskTypes(): array {
291298
if ($taskTypeArray['isInternal'] ?? false) {
292299
continue;
293300
}
294-
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToTextChatWithTools')
295-
&& $typeId === \OCP\TaskProcessing\TaskTypes\TextToTextChatWithTools::ID) {
296-
continue;
301+
// see https://github.com/vimeo/psalm/issues/7980
302+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToTextChatWithTools')) {
303+
if ($typeId === \OCP\TaskProcessing\TaskTypes\TextToTextChatWithTools::ID) {
304+
continue;
305+
}
297306
}
298-
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentInteraction')
299-
&& $typeId === \OCP\TaskProcessing\TaskTypes\ContextAgentInteraction::ID) {
300-
continue;
307+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentInteraction')) {
308+
if ($typeId === \OCP\TaskProcessing\TaskTypes\ContextAgentInteraction::ID) {
309+
continue;
310+
}
301311
}
302-
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentAudioInteraction')
303-
&& $typeId === \OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction::ID) {
304-
continue;
312+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentAudioInteraction')) {
313+
if ($typeId === \OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction::ID) {
314+
continue;
315+
}
305316
}
306-
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToAudioChat')
307-
&& $typeId === \OCP\TaskProcessing\TaskTypes\AudioToAudioChat::ID) {
308-
continue;
317+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToAudioChat')) {
318+
if ($typeId === \OCP\TaskProcessing\TaskTypes\AudioToAudioChat::ID) {
319+
continue;
320+
}
309321
}
310322
}
311323
if ($typeId === TextToTextChat::ID) {

lib/TaskProcessing/AudioToAudioChatProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function getName(): string {
4141
}
4242

4343
public function getTaskTypeId(): string {
44+
/** @psalm-suppress UndefinedClass */
4445
return AudioToAudioChat::ID;
4546
}
4647

@@ -134,6 +135,8 @@ public function process(?string $userId, array $input, callable $reportProgress)
134135

135136
// text to speech
136137
try {
138+
// this provider is not declared if TextToSpeech does not exist so we know it's fine
139+
/** @psalm-suppress UndefinedClass */
137140
$task = new Task(
138141
TextToSpeech::ID,
139142
['input' => $llmResult],

lib/TaskProcessing/ContextAgentAudioInteractionProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function getName(): string {
4141
}
4242

4343
public function getTaskTypeId(): string {
44+
/** @psalm-suppress UndefinedClass */
4445
return ContextAgentAudioInteraction::ID;
4546
}
4647

@@ -115,6 +116,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
115116

116117
// context agent
117118
try {
119+
/** @psalm-suppress UndefinedClass */
118120
$task = new Task(
119121
ContextAgentInteraction::ID,
120122
[
@@ -134,6 +136,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
134136
if ($agencyTaskOutput['output'] !== '') {
135137
// text to speech
136138
try {
139+
/** @psalm-suppress UndefinedClass */
137140
$task = new Task(
138141
TextToSpeech::ID,
139142
['input' => $agencyTaskOutput['output']],

psalm.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
findUnusedBaselineEntry="true"
1010
findUnusedCode="false"
1111
resolveFromConfigFile="true"
12+
ensureOverrideAttribute="false"
13+
strictBinaryOperands="false"
1214
phpVersion="8.1"
1315
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1416
xmlns="https://getpsalm.org/schema/config"
@@ -35,12 +37,6 @@
3537
<referencedClass name="Symfony\Component\Console\Input\InputInterface" />
3638
<referencedClass name="Symfony\Component\Console\Output\OutputInterface" />
3739
<referencedClass name="OC\User\NoUserException" />
38-
<referencedClass name="OCP\TaskProcessing\EShapeType" />
39-
<referencedClass name="OCP\TaskProcessing\TaskTypes\ContextAgentInteraction" />
40-
<referencedClass name="OCP\TaskProcessing\TaskTypes\TextToTextChatWithTools" />
41-
<referencedClass name="OCP\TaskProcessing\TaskTypes\TextToSpeech" />
42-
<referencedClass name="OCP\TaskProcessing\TaskTypes\AudioToAudioChat" />
43-
<referencedClass name="OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction" />
4440
</errorLevel>
4541
</UndefinedClass>
4642
<UndefinedDocblockClass>

0 commit comments

Comments
 (0)