Skip to content

Commit

Permalink
replace usage of enum with a class and update gh workflows
Browse files Browse the repository at this point in the history
gh workflows now check run the check on php 8.0 in addition to
8.1 and 8.2.
Also, --no-cache option for psalm

Signed-off-by: Anupam Kumar <[email protected]>
  • Loading branch information
kyteinsky committed Mar 1, 2024
1 parent ca7c6c0 commit a9b4e3b
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-php-cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

strategy:
matrix:
php-versions: [ "8.1", "8.2" ]
php-versions: [ "8.0", "8.1", "8.2" ]

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:

strategy:
matrix:
php-versions: [ "8.1", "8.2" ]
php-versions: [ "8.0", "8.1", "8.2" ]

name: php-lint

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
# do not stop on another job's failure
fail-fast: false
matrix:
php-versions: [ "8.1", "8.2" ]
php-versions: [ '8.0', '8.1', '8.2' ]
databases: ['sqlite']
server-versions: ['master']

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

strategy:
matrix:
php-versions: [ '8.1', '8.2' ]
php-versions: [ '8.0', '8.1', '8.2' ]
server-versions: [ 'dev-master', 'dev-stable28' ]
fail-fast: false

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "^8.1 || ^8.2"
"php": "^8.0 || ^8.1 || ^8.2"
},
"require-dev": {
"nextcloud/coding-standard": "^1.1",
Expand All @@ -32,7 +32,7 @@
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
"cs:check": "php-cs-fixer fix --dry-run --diff",
"cs:fix": "php-cs-fixer fix",
"psalm": "psalm.phar --threads=1",
"psalm": "psalm.phar --threads=1 --no-cache",
"psalm:update-baseline": "psalm.phar --threads=1 --update-baseline",
"psalm:update-baseline:force": "psalm.phar --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
"psalm:clear": "psalm.phar --clear-cache && psalm.phar --clear-global-cache",
Expand All @@ -43,7 +43,7 @@
"optimize-autoloader": true,
"classmap-authoritative": true,
"platform": {
"php": "8.1"
"php": "8.0"
}
},
"autoload": {
Expand Down
194 changes: 101 additions & 93 deletions composer.lock

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions lib/Command/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

namespace OCA\ContextChat\Command;

use OCA\ContextChat\Service\ScopeType;
use OCA\ContextChat\TextProcessing\ContextChatTaskType;
use OCA\ContextChat\TextProcessing\ScopedContextChatTaskType;
use OCA\ContextChat\Type\ScopeType;
use OCP\TextProcessing\FreePromptTaskType;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\Task;
Expand Down Expand Up @@ -55,13 +55,13 @@ protected function configure() {
'context-sources',
null,
InputOption::VALUE_REQUIRED,
'Context sources to use',
'Context sources to use (as a comma-separated list without brackets)',
)
->addOption(
'context-providers',
null,
InputOption::VALUE_REQUIRED,
'Context provider to use',
'Context providers to use (as a comma-separated list without brackets)',
);
}

Expand All @@ -83,15 +83,19 @@ protected function execute(InputInterface $input, OutputInterface $output) {
if ($noContext) {
$task = new Task(FreePromptTaskType::class, $prompt, 'context_chat', $userId);
} elseif (!empty($contextSources)) {
$contextSources = preg_replace('/\s*,+\s*/', ',', $contextSources);
$contextSourcesArray = array_filter(explode(',', $contextSources), fn ($source) => !empty($source));
$task = new Task(ScopedContextChatTaskType::class, json_encode([
'scopeType' => ScopeType::SOURCE,
'scopeList' => explode(',', $contextSources),
'scopeList' => $contextSourcesArray,
'prompt' => $prompt,
]), 'context_chat', $userId);
} elseif (!empty($contextProviders)) {
$contextProviders = preg_replace('/\s*,+\s*/', ',', $contextProviders);
$contextProvidersArray = array_filter(explode(',', $contextProviders), fn ($source) => !empty($source));
$task = new Task(ScopedContextChatTaskType::class, json_encode([
'scopeType' => ScopeType::PROVIDER,
'scopeList' => explode(',', $contextProviders),
'scopeList' => $contextProvidersArray,
'prompt' => $prompt,
]), 'context_chat', $userId);
} else {
Expand Down
9 changes: 2 additions & 7 deletions lib/Service/LangRopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
use Psr\Log\LoggerInterface;
use RuntimeException;

enum ScopeType: string {
case PROVIDER = 'provider';
case SOURCE = 'source';
}

class LangRopeService {
public function __construct(
private LoggerInterface $logger,
Expand Down Expand Up @@ -195,11 +190,11 @@ public function query(string $userId, string $prompt, bool $useContext = true):
/**
* @param string $userId
* @param string $prompt
* @param ScopeType $scopeType
* @param string $scopeType
* @param array<string> $scopeList
* @return array
*/
public function scopedQuery(string $userId, string $prompt, ScopeType $scopeType, array $scopeList): array {
public function scopedQuery(string $userId, string $prompt, string $scopeType, array $scopeList): array {
$params = [
'query' => $prompt,
'userId' => $userId,
Expand Down
11 changes: 6 additions & 5 deletions lib/TextProcessing/ScopedContextChatProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace OCA\ContextChat\TextProcessing;

use OCA\ContextChat\Service\LangRopeService;
use OCA\ContextChat\Service\ScopeType;
use OCA\ContextChat\Type\ScopeType;
use OCP\IL10N;
use OCP\TextProcessing\IProvider;
use OCP\TextProcessing\IProviderWithUserId;
Expand Down Expand Up @@ -63,15 +63,16 @@ public function process(string $prompt): string {
throw new \RuntimeException('Invalid JSON string, expected { "scopeType": string, "scopeList": list[string], "prompt": string }');
}

$scopeTypeEnum = ScopeType::tryFrom($parsedData['scopeType']);
if ($scopeTypeEnum === null) {
throw new \RuntimeException('Invalid scope type: ' . $parsedData['scopeType']);
try {
ScopeType::validate($parsedData['scopeType']);
} catch (\InvalidArgumentException $e) {
throw new \RuntimeException($e->getMessage(), intval($e->getCode()), $e);
}

$response = $this->langRopeService->scopedQuery(
$this->userId,
$parsedData['prompt'],
$scopeTypeEnum,
$parsedData['scopeType'],
$parsedData['scopeList'],
);

Expand Down
28 changes: 28 additions & 0 deletions lib/Type/ScopeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Nextcloud - ContextChat
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Anupam Kumar <[email protected]>
* @copyright Anupam Kumar 2023
*/

declare(strict_types=1);

namespace OCA\ContextChat\Type;

class ScopeType {
public const PROVIDER = 'provider';
public const SOURCE = 'source';

public static function validate(string $scopeType): void {
$relection = new \ReflectionClass(self::class);
if (!in_array($scopeType, $relection->getConstants())) {
throw new \InvalidArgumentException(
"Invalid scope type: {$scopeType}, should be one of: [" . implode(', ', $relection->getConstants()) . ']'
);
}
}
}
4 changes: 2 additions & 2 deletions stubs/appapi-public-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace OCA\AppAPI {
class PublicFunctions {
public function __construct(
private readonly \OCA\AppAPI\Service\ExAppService $exAppService,
private readonly \OCA\AppAPI\Service\AppAPIService $service,
private \OCA\AppAPI\Service\ExAppService $exAppService,
private \OCA\AppAPI\Service\AppAPIService $service,
) {
}

Expand Down
6 changes: 3 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require_once __DIR__ . '/../../../tests/bootstrap.php';
require_once __DIR__ . '/../vendor/autoload.php';

use \OCA\ContextChat\AppInfo\Application;
use \OCP\App\IAppManager;
use \OCP\Server;
use OCA\ContextChat\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\Server;

Server::get(IAppManager::class)->loadApp(Application::APP_ID);
OC_Hook::clear();

0 comments on commit a9b4e3b

Please sign in to comment.