修改积分统计模式

main
落雨楓 8 months ago
parent af11f0ce5d
commit dc02c606b7

@ -98,8 +98,8 @@
"IsekaiAIToolboxToken": {
"value": ""
},
"IsekaiAIToolboxUserPoints": {
"value": null
"IsekaiAIToolboxPointType": {
"value": ""
},
"IsekaiAIToolboxUserAvatar": {
"value": {

@ -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",

@ -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,
],
];
}

@ -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;
if ($pointCostData && !$noCost) {
list($pointType, $pointCost) = $pointCostData;
$pointType = MediaWikiServices::getInstance()->getMainConfig()->get('IsekaiAIToolboxPointType');
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',

@ -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;

@ -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`),

Loading…
Cancel
Save