From dc02c606b793cdff815e9461f64f71067d00b95a Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Sat, 11 May 2024 07:02:28 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=A7=AF=E5=88=86=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extension.json | 4 +- i18n/zh-hans.json | 5 +- includes/Api/AIToolbox/ApiCheckAccess.php | 36 +---------- includes/Api/AIToolboxBot/ApiReportUsage.php | 66 ++++++++------------ includes/Api/AIToolboxBot/ApiUserInfo.php | 11 ++-- sql/mysql/aitoolbox_usage.sql | 2 + 6 files changed, 41 insertions(+), 83 deletions(-) diff --git a/extension.json b/extension.json index 0bd70da..f9630b9 100644 --- a/extension.json +++ b/extension.json @@ -98,8 +98,8 @@ "IsekaiAIToolboxToken": { "value": "" }, - "IsekaiAIToolboxUserPoints": { - "value": null + "IsekaiAIToolboxPointType": { + "value": "" }, "IsekaiAIToolboxUserAvatar": { "value": { diff --git a/i18n/zh-hans.json b/i18n/zh-hans.json index 1957964..74e23d7 100644 --- a/i18n/zh-hans.json +++ b/i18n/zh-hans.json @@ -25,8 +25,6 @@ "apihelp-aitoolbox+checkaccess-summary": "检测是否拥有权限,并获取使用开销", "apihelp-aitoolbox+checkaccess-param-ataction": "在AI工具箱中的操作", - "apihelp-aitoolbox+checkaccess-param-tokens": "提问Token数", - "apihelp-aitoolbox+checkaccess-param-extractlines": "从文档中提取的信息行数上限", "apihelp-aitoolbox+createtoken-summary": "创建访问AI工具箱所需的Token", @@ -38,8 +36,9 @@ "apihelp-aitoolboxbot+reportusage-summary": "报告用户使用AI工具箱的情况。需要 aitoolboxbot 权限。", "apihelp-aitoolboxbot+reportusage-param-userid": "用户ID", "apihelp-aitoolboxbot+reportusage-param-useraction": "在AI工具箱中的操作", + "apihelp-aitoolboxbot+reportusage-param-robotid": "机器人ID", + "apihelp-aitoolboxbot+reportusage-param-pointusage": "使用的积分数", "apihelp-aitoolboxbot+reportusage-param-tokens": "使用的Token数", - "apihelp-aitoolboxbot+reportusage-param-extractlines": "从文档中提取的信息行数", "apihelp-aitoolboxbot+reportusage-param-step": "操作的阶段", "apihelp-aitoolboxbot+reportusage-param-error": "报告错误信息", "apihelp-aitoolboxbot+reportusage-param-transactionid": "当step为end或cancel时需要事务ID", diff --git a/includes/Api/AIToolbox/ApiCheckAccess.php b/includes/Api/AIToolbox/ApiCheckAccess.php index f71b8ca..fe2df01 100644 --- a/includes/Api/AIToolbox/ApiCheckAccess.php +++ b/includes/Api/AIToolbox/ApiCheckAccess.php @@ -23,8 +23,6 @@ class ApiCheckAccess extends ApiBase { public function execute() { $userAction = strtolower($this->getParameter('ataction')); - $tokens = $this->getParameter('tokens'); - $extractLimit = $this->getParameter('extractlines'); $user = $this->getUser(); @@ -40,8 +38,6 @@ class ApiCheckAccess extends ApiBase { 'chatcomplete' => 'chatcomplete', ]; - $permissionManager = $this->services->getPermissionManager(); - $permissionKey = $permissionMap[$userAction] ?? null; if ($permissionKey && !$user->isAllowed($permissionKey)) { $this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission'); @@ -56,28 +52,8 @@ class ApiCheckAccess extends ApiBase { $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); + $result->addValue(['aitoolbox', $this->getModuleName()], 'nocost', $noCost); return true; } @@ -92,16 +68,6 @@ class ApiCheckAccess extends ApiBase { 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, - ], ]; } diff --git a/includes/Api/AIToolboxBot/ApiReportUsage.php b/includes/Api/AIToolboxBot/ApiReportUsage.php index 506355e..6f76a3b 100644 --- a/includes/Api/AIToolboxBot/ApiReportUsage.php +++ b/includes/Api/AIToolboxBot/ApiReportUsage.php @@ -60,28 +60,14 @@ class ApiReportUsage extends ApiBase { $result = $this->getResult(); if ($step) { switch ($step) { - case 'check': - $this->requireParameter(['userid', 'useraction']); - - $userAction = $this->getParameter('useraction'); - $tokens = $this->getParameter('tokens'); - $extractLines = $this->getParameter('extractlines'); - - $pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLines); - list($pointType, $pointCost) = $pointCostData; - - $result = $this->getResult(); - - $result->addValue(['aitoolboxbot', $this->getModuleName()], 'pointtype', $pointType); - $result->addValue(['aitoolboxbot', $this->getModuleName()], 'pointcost', $pointCost); - break; case 'start': $this->requireParameter(['userid', 'useraction']); $userId = $this->getParameter('userid'); $userAction = $this->getParameter('useraction'); + $botId = $this->getParameter('botid'); $tokens = $this->getParameter('tokens'); - $extractLines = $this->getParameter('extractlines'); + $pointUsage = $this->getParameter('pointusage'); $user = $this->services->getUserFactory()->newFromId($userId); @@ -110,29 +96,29 @@ class ApiReportUsage extends ApiBase { $transactionid = bin2hex(random_bytes(8)); + $pointType = MediaWikiServices::getInstance()->getMainConfig()->get('IsekaiAIToolboxPointType'); + $data = [ 'userid' => $userId, 'useraction' => $userAction, + 'pointtype' => $pointType, + 'pointusage' => $pointUsage, + 'botid' => $botId, 'tokens' => $tokens, 'step' => $step, ]; - $pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLines); - if ($pointCostData && !$noCost) { - list($pointType, $pointCost) = $pointCostData; - - $data['pointtype'] = $pointType; - + if (!$noCost) { /** @var \Isekai\UserPoints\Service\IsekaiUserPointsFactory */ $pointFactory = $this->services->getService('IsekaiUserPoints'); $pointService = $pointFactory->newFromUserId($userId, $pointType); - if (!$pointService->hasEnoughPoints($pointCost)) { // User doesn't have enough points + if (!$pointService->hasEnoughPoints($pointUsage)) { // User doesn't have enough points $this->addError('apierror-isekai-ai-toolbox-noenoughpoints', 'noenoughpoints'); return false; } - $pointTransactionId = $pointService->startConsumePointsTransaction($pointCost); + $pointTransactionId = $pointService->startConsumePointsTransaction($pointUsage); $data['pointtransaction'] = $pointTransactionId; } @@ -144,8 +130,6 @@ class ApiReportUsage extends ApiBase { $result->addValue(['aitoolboxbot', $this->getModuleName()], 'success', 1); $result->addValue(['aitoolboxbot', $this->getModuleName()], 'transactionid', $transactionid); - $result->addValue(['aitoolboxbot', $this->getModuleName()], 'pointtype', $pointType); - $result->addValue(['aitoolboxbot', $this->getModuleName()], 'pointcost', $pointCost); break; case 'end': $transactionId = $this->getParameter('transactionid'); @@ -178,6 +162,7 @@ class ApiReportUsage extends ApiBase { [ 'user_id' => $transactionData['userid'], 'action' => $transactionData['useraction'], + 'bot_id' => $transactionData['botid'], 'tokens' => $transactionData['tokens'], 'real_tokens' => $realTokens, 'timestamp' => $dbw->timestamp(), @@ -227,6 +212,7 @@ class ApiReportUsage extends ApiBase { [ 'user_id' => $transactionData['userid'], 'action' => $transactionData['useraction'], + 'bot_id' => $transactionData['botid'], 'tokens' => $transactionData['tokens'], 'timestamp' => $dbw->timestamp(), 'success' => 0, @@ -251,8 +237,9 @@ class ApiReportUsage extends ApiBase { $userId = $this->getParameter('userid'); $userAction = strtolower($this->getParameter('useraction')); + $botId = $this->getParameter('botid'); $realTokens = $tokens = $this->getParameter('tokens'); - $extractLines = $this->getParameter('extractlines'); + $pointUsage = $this->getParameter('pointusage'); $user = $this->services->getUserFactory()->newFromId($userId); @@ -274,23 +261,21 @@ class ApiReportUsage extends ApiBase { $noCost = true; } - $pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLines); - $isSuccess = true; + + $pointType = MediaWikiServices::getInstance()->getMainConfig()->get('IsekaiAIToolboxPointType'); - if ($pointCostData && !$noCost) { - list($pointType, $pointCost) = $pointCostData; - + if (!$noCost) { /** @var \Isekai\UserPoints\Service\IsekaiUserPointsFactory */ $pointFactory = $this->services->getService('IsekaiUserPoints'); $pointService = $pointFactory->newFromUserId($userId, $pointType); - if (!$pointService->hasEnoughPoints($pointCost)) { // User doesn't have enough points + if (!$pointService->hasEnoughPoints($pointUsage)) { // User doesn't have enough points $this->addError('apierror-isekai-ai-toolbox-notenoughpoints', 'notenoughpoints'); return false; } - $ret = $pointService->consumePoints($pointCost, true); + $ret = $pointService->consumePoints($pointUsage, true); if (!$ret) { $this->addWarning('apiwarn-isekai-ai-toolbox-pointtransactionfailed', 'pointtransactionfailed'); $isSuccess = false; @@ -305,6 +290,7 @@ class ApiReportUsage extends ApiBase { [ 'user_id' => $userId, 'action' => $userAction, + 'bot_id' => $botId, 'tokens' => $tokens, 'real_tokens' => $realTokens, 'timestamp' => $dbw->timestamp(), @@ -315,8 +301,6 @@ class ApiReportUsage extends ApiBase { ); $result->addValue(['aitoolboxbot', $this->getModuleName()], 'success', $isSuccess ? 1 : 0); - $result->addValue(['aitoolboxbot', $this->getModuleName()], 'pointtype', $pointType); - $result->addValue(['aitoolboxbot', $this->getModuleName()], 'pointcost', $pointCost); } return true; @@ -337,14 +321,19 @@ class ApiReportUsage extends ApiBase { ParamValidator::PARAM_DEFAULT => null, ParamValidator::PARAM_REQUIRED => false, ], + 'botid' => [ + ParamValidator::PARAM_TYPE => 'string', + ParamValidator::PARAM_DEFAULT => null, + ParamValidator::PARAM_REQUIRED => false, + ], 'tokens' => [ ParamValidator::PARAM_TYPE => 'integer', ParamValidator::PARAM_DEFAULT => 100, ParamValidator::PARAM_REQUIRED => false, ], - 'extractlines' => [ + 'pointusage' => [ ParamValidator::PARAM_TYPE => 'integer', - ParamValidator::PARAM_DEFAULT => 5, + ParamValidator::PARAM_DEFAULT => 0, ParamValidator::PARAM_REQUIRED => false, ], 'error' => [ @@ -354,7 +343,6 @@ class ApiReportUsage extends ApiBase { ], 'step' => [ ParamValidator::PARAM_TYPE => [ - 'check', 'start', 'end', 'cancel', diff --git a/includes/Api/AIToolboxBot/ApiUserInfo.php b/includes/Api/AIToolboxBot/ApiUserInfo.php index 3ec1212..4a6d8f1 100644 --- a/includes/Api/AIToolboxBot/ApiUserInfo.php +++ b/includes/Api/AIToolboxBot/ApiUserInfo.php @@ -71,13 +71,12 @@ class ApiUserInfo extends ApiBase { 'realname' => $user->getRealName(), // 'nickname' => $userOptionsLookup->getOption($user, 'nickname'), 'email' => $user->getEmail(), + 'nocost' => false ]; // Get user points from IsekaiUserPoints - $pointConfig = $this->config->get('IsekaiAIToolboxUserPoints'); - if (is_array($pointConfig) && isset($pointConfig[self::DEFAULT_CC_ACTION])) { - $pointType = $pointConfig[self::DEFAULT_CC_ACTION]['pointType']; - + $pointType = $this->config->get('IsekaiAIToolboxPointType'); + if ($pointType) { /** @var \Isekai\UserPoints\Service\IsekaiUserPointsFactory */ $pointFactory = $this->services->getService('IsekaiUserPoints'); $pointService = $pointFactory->newFromUserId($userId, $pointType); @@ -87,6 +86,10 @@ class ApiUserInfo extends ApiBase { } } + if ($user->isAllowed('aitoolbox-unlimited')) { + $userInfo['nocost'] = true; + } + // Get user avatar $avatarTpl = $this->config->get('IsekaiAIToolboxUserAvatar'); $avatarUrl = null; diff --git a/sql/mysql/aitoolbox_usage.sql b/sql/mysql/aitoolbox_usage.sql index 0be9ce7..83aa559 100644 --- a/sql/mysql/aitoolbox_usage.sql +++ b/sql/mysql/aitoolbox_usage.sql @@ -2,6 +2,7 @@ CREATE TABLE /*_*/aitoolbox_usage ( `id` int UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, `user_id` int UNSIGNED NOT NULL, `action` varchar(50) NOT NULL, + `bot_id` varchar(50) NULL, `tokens` int UNSIGNED NOT NULL, `real_tokens` int UNSIGNED NOT NULL, `timestamp` bigint UNSIGNED NOT NULL, @@ -11,6 +12,7 @@ CREATE TABLE /*_*/aitoolbox_usage ( ALTER TABLE /*_*/aitoolbox_usage ADD KEY `user_id` (`user_id`), + ADD KEY `bot_id` (`bot_id`), ADD KEY `timestamp` (`timestamp`), ADD KEY `success` (`success`), ADD KEY `action` (`action`),