Skip to content

Commit d64e700

Browse files
authored
Merge pull request #52120 from nextcloud/backport/52050/stable31
[stable31] fix(taskprocessing): use the event for AppAPI to get list of AI providers
2 parents 6897385 + 9543f98 commit d64e700

File tree

5 files changed

+434
-2
lines changed

5 files changed

+434
-2
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@
815815
'OCP\\Talk\\ITalkBackend' => $baseDir . '/lib/public/Talk/ITalkBackend.php',
816816
'OCP\\TaskProcessing\\EShapeType' => $baseDir . '/lib/public/TaskProcessing/EShapeType.php',
817817
'OCP\\TaskProcessing\\Events\\AbstractTaskProcessingEvent' => $baseDir . '/lib/public/TaskProcessing/Events/AbstractTaskProcessingEvent.php',
818+
'OCP\\TaskProcessing\\Events\\GetTaskProcessingProvidersEvent' => $baseDir . '/lib/public/TaskProcessing/Events/GetTaskProcessingProvidersEvent.php',
818819
'OCP\\TaskProcessing\\Events\\TaskFailedEvent' => $baseDir . '/lib/public/TaskProcessing/Events/TaskFailedEvent.php',
819820
'OCP\\TaskProcessing\\Events\\TaskSuccessfulEvent' => $baseDir . '/lib/public/TaskProcessing/Events/TaskSuccessfulEvent.php',
820821
'OCP\\TaskProcessing\\Exception\\Exception' => $baseDir . '/lib/public/TaskProcessing/Exception/Exception.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
864864
'OCP\\Talk\\ITalkBackend' => __DIR__ . '/../../..' . '/lib/public/Talk/ITalkBackend.php',
865865
'OCP\\TaskProcessing\\EShapeType' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/EShapeType.php',
866866
'OCP\\TaskProcessing\\Events\\AbstractTaskProcessingEvent' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Events/AbstractTaskProcessingEvent.php',
867+
'OCP\\TaskProcessing\\Events\\GetTaskProcessingProvidersEvent' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Events/GetTaskProcessingProvidersEvent.php',
867868
'OCP\\TaskProcessing\\Events\\TaskFailedEvent' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Events/TaskFailedEvent.php',
868869
'OCP\\TaskProcessing\\Events\\TaskSuccessfulEvent' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Events/TaskSuccessfulEvent.php',
869870
'OCP\\TaskProcessing\\Exception\\Exception' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Exception/Exception.php',

lib/private/TaskProcessing/Manager.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCP\SpeechToText\ISpeechToTextProvider;
4242
use OCP\SpeechToText\ISpeechToTextProviderWithId;
4343
use OCP\TaskProcessing\EShapeType;
44+
use OCP\TaskProcessing\Events\GetTaskProcessingProvidersEvent;
4445
use OCP\TaskProcessing\Events\TaskFailedEvent;
4546
use OCP\TaskProcessing\Events\TaskSuccessfulEvent;
4647
use OCP\TaskProcessing\Exception\NotFoundException;
@@ -81,8 +82,13 @@ class Manager implements IManager {
8182
private IAppData $appData;
8283
private ?array $preferences = null;
8384
private ?array $providersById = null;
85+
86+
/** @var ITaskType[]|null */
87+
private ?array $taskTypes = null;
8488
private ICache $distributedCache;
8589

90+
private ?GetTaskProcessingProvidersEvent $eventResult = null;
91+
8692
public function __construct(
8793
private IConfig $config,
8894
private Coordinator $coordinator,
@@ -488,6 +494,20 @@ public function getOptionalOutputShapeEnumValues(): array {
488494
return $newProviders;
489495
}
490496

497+
/**
498+
* Dispatches the event to collect external providers and task types.
499+
* Caches the result within the request.
500+
*/
501+
private function dispatchGetProvidersEvent(): GetTaskProcessingProvidersEvent {
502+
if ($this->eventResult !== null) {
503+
return $this->eventResult;
504+
}
505+
506+
$this->eventResult = new GetTaskProcessingProvidersEvent();
507+
$this->dispatcher->dispatchTyped($this->eventResult);
508+
return $this->eventResult ;
509+
}
510+
491511
/**
492512
* @return IProvider[]
493513
*/
@@ -516,6 +536,16 @@ private function _getProviders(): array {
516536
}
517537
}
518538

539+
$event = $this->dispatchGetProvidersEvent();
540+
$externalProviders = $event->getProviders();
541+
foreach ($externalProviders as $provider) {
542+
if (!isset($providers[$provider->getId()])) {
543+
$providers[$provider->getId()] = $provider;
544+
} else {
545+
$this->logger->info('Skipping external task processing provider with ID ' . $provider->getId() . ' because a local provider with the same ID already exists.');
546+
}
547+
}
548+
519549
$providers += $this->_getTextProcessingProviders() + $this->_getTextToImageProviders() + $this->_getSpeechToTextProviders();
520550

521551
return $providers;
@@ -531,6 +561,10 @@ private function _getTaskTypes(): array {
531561
return [];
532562
}
533563

564+
if ($this->taskTypes !== null) {
565+
return $this->taskTypes;
566+
}
567+
534568
// Default task types
535569
$taskTypes = [
536570
\OCP\TaskProcessing\TaskTypes\TextToText::ID => \OCP\Server::get(\OCP\TaskProcessing\TaskTypes\TextToText::class),
@@ -568,9 +602,19 @@ private function _getTaskTypes(): array {
568602
}
569603
}
570604

605+
$event = $this->dispatchGetProvidersEvent();
606+
$externalTaskTypes = $event->getTaskTypes();
607+
foreach ($externalTaskTypes as $taskType) {
608+
if (isset($taskTypes[$taskType->getId()])) {
609+
$this->logger->warning('External task processing task type is using ID ' . $taskType->getId() . ' which is already used by a locally registered task type (' . get_class($taskTypes[$taskType->getId()]) . ')');
610+
}
611+
$taskTypes[$taskType->getId()] = $taskType;
612+
}
613+
571614
$taskTypes += $this->_getTextProcessingTaskTypes();
572615

573-
return $taskTypes;
616+
$this->taskTypes = $taskTypes;
617+
return $this->taskTypes;
574618
}
575619

576620
/**
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
namespace OCP\TaskProcessing\Events;
8+
9+
use OCP\EventDispatcher\Event;
10+
use OCP\TaskProcessing\IProvider;
11+
use OCP\TaskProcessing\ITaskType;
12+
13+
/**
14+
* Event dispatched by the server to collect Task Processing Providers
15+
* and custom Task Types from listeners (like AppAPI).
16+
*
17+
* Listeners should add their providers and task types using the
18+
* addProvider() and addTaskType() methods.
19+
*
20+
* @since 32.0.0
21+
*/
22+
class GetTaskProcessingProvidersEvent extends Event {
23+
/** @var IProvider[] */
24+
private array $providers = [];
25+
26+
/** @var ITaskType[] */
27+
private array $taskTypes = [];
28+
29+
/**
30+
* Add a Task Processing Provider.
31+
*
32+
* @param IProvider $provider The provider instance to add.
33+
* @since 32.0.0
34+
*/
35+
public function addProvider(IProvider $provider): void {
36+
$this->providers[] = $provider;
37+
}
38+
39+
/**
40+
* Get all collected Task Processing Providers.
41+
*
42+
* @return IProvider[]
43+
* @since 32.0.0
44+
*/
45+
public function getProviders(): array {
46+
return $this->providers;
47+
}
48+
49+
/**
50+
* Add a custom Task Processing Task Type.
51+
*
52+
* @param ITaskType $taskType The task type instance to add.
53+
* @since 32.0.0
54+
*/
55+
public function addTaskType(ITaskType $taskType): void {
56+
$this->taskTypes[] = $taskType;
57+
}
58+
59+
/**
60+
* Get all collected custom Task Processing Task Types.
61+
*
62+
* @return ITaskType[]
63+
* @since 32.0.0
64+
*/
65+
public function getTaskTypes(): array {
66+
return $this->taskTypes;
67+
}
68+
}

0 commit comments

Comments
 (0)