|
7 | 7 |
|
8 | 8 | namespace OCA\ContextChat\Command; |
9 | 9 |
|
10 | | -use OCA\ContextChat\TaskProcessing\ContextChatTaskType; |
| 10 | +use OCA\ContextChat\AppInfo\Application; |
11 | 11 | use OCA\ContextChat\Type\ScopeType; |
12 | 12 | use OCP\TaskProcessing\IManager; |
13 | 13 | use OCP\TaskProcessing\Task; |
|
18 | 18 | use Symfony\Component\Console\Output\OutputInterface; |
19 | 19 |
|
20 | 20 | class Prompt extends Command { |
| 21 | + private const CONTEXT_CHAT_TASK_TYPE_ID = Application::APP_ID . ':context_chat_search'; |
21 | 22 |
|
22 | 23 | public function __construct( |
23 | 24 | private IManager $taskProcessingManager, |
@@ -64,35 +65,62 @@ protected function execute(InputInterface $input, OutputInterface $output): int |
64 | 65 |
|
65 | 66 | if (!empty($contextSources)) { |
66 | 67 | $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, [ |
69 | 78 | 'scopeType' => ScopeType::SOURCE, |
70 | 79 | 'scopeList' => $contextSourcesArray, |
71 | 80 | 'scopeListMeta' => '', |
72 | 81 | 'prompt' => $prompt, |
73 | 82 | ], 'context_chat', $userId); |
74 | 83 | } elseif (!empty($contextProviders)) { |
75 | 84 | $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, [ |
78 | 95 | 'scopeType' => ScopeType::PROVIDER, |
79 | 96 | 'scopeList' => $contextProvidersArray, |
80 | 97 | 'scopeListMeta' => '', |
81 | 98 | 'prompt' => $prompt, |
82 | 99 | ], 'context_chat', $userId); |
83 | 100 | } 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); |
85 | 107 | } |
86 | 108 |
|
87 | 109 | $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); |
90 | 118 | } |
91 | 119 | if ($task->getStatus() === Task::STATUS_SUCCESSFUL) { |
92 | 120 | $output->writeln(var_export($task->getOutput(), true)); |
93 | 121 | return 0; |
94 | 122 | } else { |
95 | | - $output->writeln($task->getErrorMessage()); |
| 123 | + $output->writeln('<error>' . ($task->getErrorMessage() ?? '(empty error message)') . '</error>'); |
96 | 124 | return 1; |
97 | 125 | } |
98 | 126 | } |
|
0 commit comments