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