Skip to content

Commit

Permalink
feat(textprocessing): factorize taskProcessingCompatibleTaskTypes
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Oct 24, 2024
1 parent 54cebc2 commit dcafd36
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions lib/private/TextProcessing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class Manager implements IManager {
/** @var ?IProvider[] */
private ?array $providers = null;

private static array $taskProcessingCompatibleTaskTypes = [
FreePromptTaskType::class => TextToText::ID,
HeadlineTaskType::class => TextToTextHeadline::ID,
SummaryTaskType::class => TextToTextSummary::ID,
TopicsTaskType::class => TextToTextTopics::ID,
];

public function __construct(
private IServerContainer $serverContainer,
private Coordinator $coordinator,
Expand Down Expand Up @@ -84,13 +91,7 @@ public function getProviders(): array {
public function hasProviders(): bool {
// check if task processing equivalent types are available
$taskTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
$taskProcessingCompatibleTaskTypes = [
FreePromptTaskType::class => TextToText::ID,
HeadlineTaskType::class => TextToTextHeadline::ID,
SummaryTaskType::class => TextToTextSummary::ID,
TopicsTaskType::class => TextToTextTopics::ID,
];
foreach ($taskProcessingCompatibleTaskTypes as $textTaskTypeClass => $taskTaskTypeId) {
foreach (self::$taskProcessingCompatibleTaskTypes as $textTaskTypeClass => $taskTaskTypeId) {
if (isset($taskTaskTypes[$taskTaskTypeId])) {
return true;
}
Expand All @@ -114,13 +115,7 @@ public function getAvailableTaskTypes(): array {

// check if task processing equivalent types are available
$taskTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
$taskProcessingCompatibleTaskTypes = [
FreePromptTaskType::class => TextToText::ID,
HeadlineTaskType::class => TextToTextHeadline::ID,
SummaryTaskType::class => TextToTextSummary::ID,
TopicsTaskType::class => TextToTextTopics::ID,
];
foreach ($taskProcessingCompatibleTaskTypes as $textTaskTypeClass => $taskTaskTypeId) {
foreach (self::$taskProcessingCompatibleTaskTypes as $textTaskTypeClass => $taskTaskTypeId) {
if (isset($taskTaskTypes[$taskTaskTypeId])) {
$tasks[$textTaskTypeClass] = true;
}
Expand All @@ -139,15 +134,9 @@ public function canHandleTask(OCPTask $task): bool {
public function runTask(OCPTask $task): string {
// try to run a task processing task if possible
$taskTypeClass = $task->getType();
$taskProcessingCompatibleTaskTypes = [
FreePromptTaskType::class => TextToText::ID,
HeadlineTaskType::class => TextToTextHeadline::ID,
SummaryTaskType::class => TextToTextSummary::ID,
TopicsTaskType::class => TextToTextTopics::ID,
];
if (isset($taskProcessingCompatibleTaskTypes[$taskTypeClass]) && isset($this->taskProcessingManager->getAvailableTaskTypes()[$taskProcessingCompatibleTaskTypes[$taskTypeClass]])) {
if (isset(self::$taskProcessingCompatibleTaskTypes[$taskTypeClass]) && isset($this->taskProcessingManager->getAvailableTaskTypes()[self::$taskProcessingCompatibleTaskTypes[$taskTypeClass]])) {
try {
$taskProcessingTaskTypeId = $taskProcessingCompatibleTaskTypes[$taskTypeClass];
$taskProcessingTaskTypeId = self::$taskProcessingCompatibleTaskTypes[$taskTypeClass];
$taskProcessingTask = new \OCP\TaskProcessing\Task(
$taskProcessingTaskTypeId,
['input' => $task->getInput()],
Expand Down

0 comments on commit dcafd36

Please sign in to comment.