getMain(), $method); $this->mParent = $main; $this->services = MediaWikiServices::getInstance(); } public function execute() { $userAction = strtolower($this->getParameter('ataction')); $tokens = $this->getParameter('tokens'); $extractLimit = $this->getParameter('extractlines'); $user = $this->getUser(); if (!$user->isRegistered()) { $this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission'); return false; } $result = $this->getResult(); $permissionMap = [ 'embeddingpage' => 'ccembeddingpage', 'chatcomplete' => 'chatcomplete', ]; $permissionManager = $this->services->getPermissionManager(); $permissionKey = $permissionMap[$userAction] ?? null; if ($permissionKey && !$user->isAllowed($permissionKey)) { $this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission'); $result->addValue(['aitoolbox', $this->getModuleName()], 'available', false); return false; } $noCost = false; if ($userAction === 'chatcomplete' && $user->isAllowed('aitoolbox-unlimited')) { $noCost = true; } $isAvailable = true; $pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLimit); if ($pointCostData && !$noCost) { list($pointType, $pointCost) = $pointCostData; $result->addValue(['aitoolbox', $this->getModuleName()], 'pointtype', $pointType); $result->addValue(['aitoolbox', $this->getModuleName()], 'pointcost', $pointCost); // Check if user have enough points /** @var \Isekai\UserPoints\Service\IsekaiUserPointsFactory */ $pointFactory = $this->services->getService('IsekaiUserPoints'); $pointService = $pointFactory->newFromUser($user, $pointType); if (!$pointService->hasEnoughPoints($pointCost)) { // User doesn't have enough points $this->addError('apierror-isekai-ai-toolbox-notenoughpoints', 'notenoughpoints'); $isAvailable = false; } } else { $result->addValue(['aitoolbox', $this->getModuleName()], 'pointtype', null); $result->addValue(['aitoolbox', $this->getModuleName()], 'pointcost', 0); } $result->addValue(['aitoolbox', $this->getModuleName()], 'available', $isAvailable); return true; } public function getAllowedParams($flags = 0) { return [ 'ataction' => [ ParamValidator::PARAM_TYPE => [ 'embeddingpage', 'chatcomplete', ], ParamValidator::PARAM_DEFAULT => null, ParamValidator::PARAM_REQUIRED => true, ], 'tokens' => [ ParamValidator::PARAM_TYPE => 'integer', ParamValidator::PARAM_DEFAULT => 100, ParamValidator::PARAM_REQUIRED => false, ], 'extractlines' => [ ParamValidator::PARAM_TYPE => 'integer', ParamValidator::PARAM_DEFAULT => 5, ParamValidator::PARAM_REQUIRED => false, ], ]; } public function getCacheMode($params) { return 'private'; } public function getParent() { return $this->mParent; } }