-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSearch.php
More file actions
84 lines (75 loc) · 2.51 KB
/
Copy pathSearch.php
File metadata and controls
84 lines (75 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\ContextChat\Command;
use OCA\ContextChat\TaskProcessing\ContextChatSearchTaskType;
use OCA\ContextChat\Type\ScopeType;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\Task;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Search extends Command {
public function __construct(
private IManager $taskProcessingManager,
) {
parent::__construct();
}
protected function configure() {
$this->setName('context_chat:search')
->setDescription('Search with Nextcloud Assistant Context Chat')
->addArgument(
'uid',
InputArgument::REQUIRED,
'The ID of the user to search the documents of'
)
->addArgument(
'prompt',
InputArgument::REQUIRED,
'The prompt'
)
->addOption(
'context-providers',
null,
InputOption::VALUE_REQUIRED,
'Context providers to use (as a comma-separated list without brackets)',
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
$userId = $input->getArgument('uid');
$prompt = $input->getArgument('prompt');
$contextProviders = $input->getOption('context-providers');
if (!empty($contextProviders)) {
$contextProviders = preg_replace('/\s*,+\s*/', ',', $contextProviders);
$contextProvidersArray = array_filter(explode(',', $contextProviders), fn ($source) => !empty($source));
$task = new Task(ContextChatSearchTaskType::ID, [
'prompt' => $prompt,
'scopeType' => ScopeType::PROVIDER,
'scopeList' => $contextProvidersArray,
'scopeListMeta' => '',
], 'context_chat', $userId);
} else {
$task = new Task(ContextChatSearchTaskType::ID, [
'prompt' => $prompt,
'scopeType' => ScopeType::NONE,
'scopeList' => [],
'scopeListMeta' => '',
], 'context_chat', $userId);
}
$this->taskProcessingManager->scheduleTask($task);
while (!in_array(($task = $this->taskProcessingManager->getTask($task->getId()))->getStatus(), [Task::STATUS_FAILED, Task::STATUS_SUCCESSFUL], true)) {
sleep(1);
}
if ($task->getStatus() === Task::STATUS_SUCCESSFUL) {
$output->writeln(var_export($task->getOutput(), true));
return 0;
} else {
$output->writeln('<error>' . $task->getErrorMessage() . '</error>');
return 1;
}
}
}