Skip to content

Commit 316f667

Browse files
committed
chore: adjust prompt and search commands to new changes
Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
1 parent 864453e commit 316f667

2 files changed

Lines changed: 61 additions & 18 deletions

File tree

lib/Command/Prompt.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace OCA\ContextChat\Command;
99

10-
use OCA\ContextChat\TaskProcessing\ContextChatTaskType;
10+
use OCA\ContextChat\AppInfo\Application;
1111
use OCA\ContextChat\Type\ScopeType;
1212
use OCP\TaskProcessing\IManager;
1313
use OCP\TaskProcessing\Task;
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Console\Output\OutputInterface;
1919

2020
class Prompt extends Command {
21+
private const CONTEXT_CHAT_TASK_TYPE_ID = Application::APP_ID . ':context_chat_search';
2122

2223
public function __construct(
2324
private IManager $taskProcessingManager,
@@ -64,35 +65,62 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6465

6566
if (!empty($contextSources)) {
6667
$contextSources = preg_replace('/\s*,+\s*/', ',', $contextSources);
67-
$contextSourcesArray = array_filter(explode(',', $contextSources), fn ($source) => !empty($source));
68-
$task = new Task(ContextChatTaskType::ID, [
68+
if ($contextSources === null) {
69+
$output->writeln('<error>Regex comma de-duplication returned null</error>');
70+
return 1;
71+
}
72+
if (is_array($contextSources)) {
73+
$contextSources = $contextSources[0];
74+
}
75+
76+
$contextSourcesArray = array_values(array_filter(explode(',', $contextSources), fn ($source) => !empty($source)));
77+
$task = new Task(self::CONTEXT_CHAT_TASK_TYPE_ID, [
6978
'scopeType' => ScopeType::SOURCE,
7079
'scopeList' => $contextSourcesArray,
7180
'scopeListMeta' => '',
7281
'prompt' => $prompt,
7382
], 'context_chat', $userId);
7483
} elseif (!empty($contextProviders)) {
7584
$contextProviders = preg_replace('/\s*,+\s*/', ',', $contextProviders);
76-
$contextProvidersArray = array_filter(explode(',', $contextProviders), fn ($source) => !empty($source));
77-
$task = new Task(ContextChatTaskType::ID, [
85+
if ($contextProviders === null) {
86+
$output->writeln('<error>Regex comma de-duplication returned null</error>');
87+
return 1;
88+
}
89+
if (is_array($contextProviders)) {
90+
$contextProviders = $contextProviders[0];
91+
}
92+
93+
$contextProvidersArray = array_values(array_filter(explode(',', $contextProviders), fn ($source) => !empty($source)));
94+
$task = new Task(self::CONTEXT_CHAT_TASK_TYPE_ID, [
7895
'scopeType' => ScopeType::PROVIDER,
7996
'scopeList' => $contextProvidersArray,
8097
'scopeListMeta' => '',
8198
'prompt' => $prompt,
8299
], 'context_chat', $userId);
83100
} else {
84-
$task = new Task(ContextChatTaskType::ID, [ 'prompt' => $prompt, 'scopeType' => ScopeType::NONE, 'scopeList' => [], 'scopeListMeta' => '' ], 'context_chat', $userId);
101+
$task = new Task(self::CONTEXT_CHAT_TASK_TYPE_ID, [
102+
'prompt' => $prompt,
103+
'scopeType' => ScopeType::NONE,
104+
'scopeList' => [],
105+
'scopeListMeta' => '',
106+
], 'context_chat', $userId);
85107
}
86108

87109
$this->taskProcessingManager->scheduleTask($task);
88-
while (!in_array(($task = $this->taskProcessingManager->getTask($task->getId()))->getStatus(), [Task::STATUS_FAILED, Task::STATUS_SUCCESSFUL], true)) {
89-
sleep(1);
110+
$taskId = $task->getId();
111+
if ($taskId === null) {
112+
$output->writeln('<error>Task schedule failed, taskId is null</error>');
113+
return 1;
114+
}
115+
116+
while (!in_array(($task = $this->taskProcessingManager->getTask($taskId))->getStatus(), [Task::STATUS_FAILED, Task::STATUS_SUCCESSFUL], true)) {
117+
sleep(2);
90118
}
91119
if ($task->getStatus() === Task::STATUS_SUCCESSFUL) {
92120
$output->writeln(var_export($task->getOutput(), true));
93121
return 0;
94122
} else {
95-
$output->writeln($task->getErrorMessage());
123+
$output->writeln('<error>' . ($task->getErrorMessage() ?? '(empty error message)') . '</error>');
96124
return 1;
97125
}
98126
}

lib/Command/Search.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace OCA\ContextChat\Command;
99

10-
use OCA\ContextChat\TaskProcessing\ContextChatSearchTaskType;
10+
use OCA\ContextChat\AppInfo\Application;
1111
use OCA\ContextChat\Type\ScopeType;
1212
use OCP\TaskProcessing\IManager;
1313
use OCP\TaskProcessing\Task;
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Console\Output\OutputInterface;
1919

2020
class Search extends Command {
21+
private const SEARCH_TASK_TYPE_ID = Application::APP_ID . ':context_chat_search';
2122

2223
public function __construct(
2324
private IManager $taskProcessingManager,
@@ -53,31 +54,45 @@ protected function execute(InputInterface $input, OutputInterface $output) {
5354

5455
if (!empty($contextProviders)) {
5556
$contextProviders = preg_replace('/\s*,+\s*/', ',', $contextProviders);
56-
$contextProvidersArray = array_filter(explode(',', $contextProviders), fn ($source) => !empty($source));
57-
$task = new Task(ContextChatSearchTaskType::ID, [
57+
if ($contextProviders === null) {
58+
$output->writeln('<error>Regex comma de-duplication returned null</error>');
59+
return 1;
60+
}
61+
if (is_array($contextProviders)) {
62+
$contextProviders = $contextProviders[0];
63+
}
64+
65+
$contextProvidersArray = array_values(array_filter(explode(',', $contextProviders), fn ($source) => !empty($source)));
66+
$task = new Task(self::SEARCH_TASK_TYPE_ID, [
5867
'prompt' => $prompt,
5968
'scopeType' => ScopeType::PROVIDER,
6069
'scopeList' => $contextProvidersArray,
6170
'scopeListMeta' => '',
62-
], 'context_chat', $userId);
71+
], Application::APP_ID, $userId);
6372
} else {
64-
$task = new Task(ContextChatSearchTaskType::ID, [
73+
$task = new Task(self::SEARCH_TASK_TYPE_ID, [
6574
'prompt' => $prompt,
6675
'scopeType' => ScopeType::NONE,
6776
'scopeList' => [],
6877
'scopeListMeta' => '',
69-
], 'context_chat', $userId);
78+
], Application::APP_ID, $userId);
7079
}
7180

7281
$this->taskProcessingManager->scheduleTask($task);
73-
while (!in_array(($task = $this->taskProcessingManager->getTask($task->getId()))->getStatus(), [Task::STATUS_FAILED, Task::STATUS_SUCCESSFUL], true)) {
74-
sleep(1);
82+
$taskId = $task->getId();
83+
if ($taskId === null) {
84+
$output->writeln('<error>Task schedule failed, taskId is null</error>');
85+
return 1;
86+
}
87+
88+
while (!in_array(($task = $this->taskProcessingManager->getTask($taskId))->getStatus(), [Task::STATUS_FAILED, Task::STATUS_SUCCESSFUL], true)) {
89+
sleep(2);
7590
}
7691
if ($task->getStatus() === Task::STATUS_SUCCESSFUL) {
7792
$output->writeln(var_export($task->getOutput(), true));
7893
return 0;
7994
} else {
80-
$output->writeln('<error>' . $task->getErrorMessage() . '</error>');
95+
$output->writeln('<error>' . ($task->getErrorMessage() ?? '(empty error message)') . '</error>');
8196
return 1;
8297
}
8398
}

0 commit comments

Comments
 (0)