Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function register(IRegistrationContext $context): void {
/** @var TaskProcessingService $taskProcessingService */
$taskProcessingService = $container->get(TaskProcessingService::class);
$taskProcessingService->registerExAppTaskProcessingProviders($context, $container->getServer());
$taskProcessingService->registerExAppTaskProcessingCustomTaskTypes($context);
} catch (NotFoundExceptionInterface|ContainerExceptionInterface) {
}
$context->registerEventListener(NodeCreatedEvent::class, FileEventsListener::class);
Expand Down
5 changes: 3 additions & 2 deletions lib/Controller/TaskProcessingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ public function __construct(
public function registerProvider(
string $name,
string $displayName,
string $taskType
string $taskType,
?array $customTaskType,
): DataResponse {
if (!$this->isSupported()) {
return new DataResponse([], Http::STATUS_NOT_IMPLEMENTED);
}

$provider = $this->taskProcessingService->registerTaskProcessingProvider(
$this->request->getHeader('EX-APP-ID'), $name, $displayName, $taskType,
$this->request->getHeader('EX-APP-ID'), $name, $displayName, $taskType, $customTaskType,
);

if ($provider === null) {
Expand Down
8 changes: 8 additions & 0 deletions lib/Db/TaskProcessing/TaskProcessingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@
* @method string getDisplayName()
* @method void setTaskType(string $taskType)
* @method string getTaskType()
* @method void setCustomTaskType(string|null $customTaskType)
* @method string|null getCustomTaskType()
*/
class TaskProcessingProvider extends Entity implements JsonSerializable {
protected ?string $appId = null;
protected ?string $name = null;
protected ?string $displayName = null;
protected ?string $actionHandler = null;
protected ?string $taskType = null;
protected ?string $customTaskType = null;

public function __construct(array $params = []) {
$this->addType('app_id', 'string');
$this->addType('name', 'string');
$this->addType('display_name', 'string');
$this->addType('task_type', 'string');
$this->addType('custom_task_type', 'string');

if (isset($params['app_id'])) {
$this->setAppId($params['app_id']);
Expand All @@ -46,6 +50,9 @@ public function __construct(array $params = []) {
if (isset($params['task_type'])) {
$this->setTaskType($params['task_type']);
}
if (isset($params['custom_task_type'])) {
$this->setCustomTaskType($params['custom_task_type']);
}
}

public function jsonSerialize(): array {
Expand All @@ -54,6 +61,7 @@ public function jsonSerialize(): array {
'name' => $this->name,
'display_name' => $this->displayName,
'task_type' => $this->taskType,
'custom_task_type' => $this->customTaskType,
];
}
}
38 changes: 38 additions & 0 deletions lib/Migration/Version2800Date20240711080316.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\AppAPI\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version2800Date20240711080316 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('ex_task_processing');
if (!$table->hasColumn('custom_task_type')) {
$table->addColumn('custom_task_type', Types::TEXT, [
'notnull' => false,
]);
}

return $schema;
}
}
67 changes: 66 additions & 1 deletion lib/Service/ProvidersAI/TaskProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IServerContainer;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\IProvider;
use OCP\TaskProcessing\ITaskType;
use OCP\TaskProcessing\ShapeDescriptor;
use Psr\Log\LoggerInterface;

class TaskProcessingService {
Expand Down Expand Up @@ -71,7 +74,8 @@ public function registerTaskProcessingProvider(
string $appId,
string $name,
string $displayName,
string $taskType
string $taskType,
?array $customTaskType,
): ?TaskProcessingProvider {
try {
$taskProcessingProvider = $this->mapper->findByAppidName($appId, $name);
Expand All @@ -84,6 +88,7 @@ public function registerTaskProcessingProvider(
'name' => $name,
'display_name' => $displayName,
'task_type' => $taskType,
'custom_task_type' => json_encode($customTaskType, JSON_THROW_ON_ERROR),
]);

if ($taskProcessingProvider !== null) {
Expand Down Expand Up @@ -186,4 +191,64 @@ public function unregisterExAppTaskProcessingProviders(string $appId): int {
$this->resetCacheEnabled();
return $result;
}

/**
* @param IRegistrationContext $context
*
* @return void
*/
public function registerExAppTaskProcessingCustomTaskTypes(IRegistrationContext $context): void {
$exAppsProviders = $this->getRegisteredTaskProcessingProviders();
foreach ($exAppsProviders as $exAppProvider) {
if ($exAppProvider->getCustomTaskType() === null) {
continue;
}

$className = '\\OCA\\AppAPI\\' . $exAppProvider->getAppId() . '\\' . $exAppProvider->getName() . '\\TaskType';
$taskType = $this->getAnonymousTaskType(json_decode($exAppProvider->getCustomTaskType(), true, 512, JSON_THROW_ON_ERROR));
$context->registerService($className, function () use ($taskType) {
return $taskType;
});
$context->registerTaskProcessingTaskType($className);
}
}

private function getAnonymousTaskType(
array $customTaskType,
): ITaskType {
return new class($customTaskType) implements ITaskType {
public function __construct(
private readonly array $customTaskType,
) {
}

public function getId(): string {
return $this->customTaskType['id'];
}

public function getName(): string {
return $this->customTaskType['name'];
}

public function getDescription(): string {
return $this->customTaskType['description'];
}

public function getInputShape(): array {
return array_map(static fn (array $shape) => new ShapeDescriptor(
$shape['name'],
$shape['description'],
EShapeType::from($shape['type']),
), $this->customTaskType['input_shape']);
}

public function getOutputShape(): array {
return array_map(static fn (array $shape) => new ShapeDescriptor(
$shape['name'],
$shape['description'],
EShapeType::from($shape['type']),
), $this->customTaskType['output_shape']);
}
};
}
}