|
|
|
@ -1,10 +1,10 @@
|
|
|
|
|
<?php
|
|
|
|
|
namespace Isekai\ChatComplete\Api\ChatCompleteBot;
|
|
|
|
|
namespace Isekai\AIToolbox\Api\AIToolboxBot;
|
|
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
|
use Isekai\ChatComplete\Api\ApiChatComplete;
|
|
|
|
|
use Isekai\ChatComplete\Api\ApiChatCompleteBot;
|
|
|
|
|
use Isekai\ChatComplete\ChatCompleteUtils;
|
|
|
|
|
use Isekai\AIToolbox\Api\ApiAIToolbox;
|
|
|
|
|
use Isekai\AIToolbox\Api\ApiAIToolboxBot;
|
|
|
|
|
use Isekai\AIToolbox\AIToolboxUtils;
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
use Message;
|
|
|
|
@ -18,10 +18,10 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
/** @var \WANObjectCache */
|
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
|
|
/** @var ApiChatCompleteBot */
|
|
|
|
|
/** @var ApiAIToolboxBot */
|
|
|
|
|
private $mParent;
|
|
|
|
|
|
|
|
|
|
public function __construct(ApiChatCompleteBot $main, $method) {
|
|
|
|
|
public function __construct(ApiAIToolboxBot $main, $method) {
|
|
|
|
|
parent::__construct($main->getMain(), $method);
|
|
|
|
|
|
|
|
|
|
$this->mParent = $main;
|
|
|
|
@ -53,7 +53,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
// Check permission
|
|
|
|
|
$this->checkUserRightsAny('chatcompletebot');
|
|
|
|
|
$this->checkUserRightsAny('aitoolboxbot');
|
|
|
|
|
|
|
|
|
|
$step = $this->getParameter('step');
|
|
|
|
|
|
|
|
|
@ -67,13 +67,13 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
$tokens = $this->getParameter('tokens');
|
|
|
|
|
$extractLines = $this->getParameter('extractlines');
|
|
|
|
|
|
|
|
|
|
$pointCostData = ChatCompleteUtils::getPointCost($userAction, $tokens, $extractLines);
|
|
|
|
|
$pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLines);
|
|
|
|
|
list($pointType, $pointCost) = $pointCostData;
|
|
|
|
|
|
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'pointtype', $pointType);
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'pointcost', $pointCost);
|
|
|
|
|
$result->addValue(['aitoolboxbot', $this->getModuleName()], 'pointtype', $pointType);
|
|
|
|
|
$result->addValue(['aitoolboxbot', $this->getModuleName()], 'pointcost', $pointCost);
|
|
|
|
|
case 'start':
|
|
|
|
|
$this->requireParameter(['userid', 'useraction']);
|
|
|
|
|
|
|
|
|
@ -85,16 +85,16 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
$user = $this->services->getUserFactory()->newFromId($userId);
|
|
|
|
|
|
|
|
|
|
if (!$user->isRegistered()) {
|
|
|
|
|
$this->addError('apierror-isekai-chatcomplete-user-not-exists', 'user-not-exists');
|
|
|
|
|
$this->addError('apierror-isekai-ai-toolbox-user-not-exists', 'user-not-exists');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$permissionManager = $this->services->getPermissionManager();
|
|
|
|
|
$permissionMap = ApiChatComplete::$permissionMap;
|
|
|
|
|
$permissionMap = ApiAIToolbox::$permissionMap;
|
|
|
|
|
|
|
|
|
|
$permissionKey = $permissionMap[$userAction] ?? null;
|
|
|
|
|
if ($permissionKey && !$user->isAllowedAny($permissionKey)) {
|
|
|
|
|
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission', [
|
|
|
|
|
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission', [
|
|
|
|
|
'permission' => $permissionKey,
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'user_permissions' => $permissionManager->getUserPermissions($this->getUser()),
|
|
|
|
@ -103,7 +103,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$noCost = false;
|
|
|
|
|
if ($user->isAllowedAny('unlimitedchatcomplete')) {
|
|
|
|
|
if ($user->isAllowedAny('aitoolbox-unlimited')) {
|
|
|
|
|
$noCost = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -116,7 +116,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
'step' => $step,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$pointCostData = ChatCompleteUtils::getPointCost($userAction, $tokens, $extractLines);
|
|
|
|
|
$pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLines);
|
|
|
|
|
if ($pointCostData && !$noCost) {
|
|
|
|
|
list($pointType, $pointCost) = $pointCostData;
|
|
|
|
|
|
|
|
|
@ -127,7 +127,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
$pointService = $pointFactory->newFromUserId($userId, $pointType);
|
|
|
|
|
|
|
|
|
|
if (!$pointService->hasEnoughPoints($pointCost)) { // User doesn't have enough points
|
|
|
|
|
$this->addError('apierror-isekai-chatcomplete-noenoughpoints', 'noenoughpoints');
|
|
|
|
|
$this->addError('apierror-isekai-ai-toolbox-noenoughpoints', 'noenoughpoints');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -136,20 +136,20 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->cache->set(
|
|
|
|
|
$this->cache->makeKey('chatcomplete', 'reportusage', 'transaction', $transactionid),
|
|
|
|
|
$this->cache->makeKey('aitoolboxbot', 'reportusage', 'transaction', $transactionid),
|
|
|
|
|
$data,
|
|
|
|
|
self::TRANSACTION_TIMEOUT
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'success', 1);
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'transactionid', $transactionid);
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'pointtype', $pointType);
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'pointcost', $pointCost);
|
|
|
|
|
$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');
|
|
|
|
|
$transactionData = $this->cache->get(
|
|
|
|
|
$this->cache->makeKey('chatcomplete', 'reportusage', 'transaction', $transactionId)
|
|
|
|
|
$this->cache->makeKey('aitoolboxbot', 'reportusage', 'transaction', $transactionId)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$isSuccess = true;
|
|
|
|
@ -164,7 +164,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
|
|
|
|
|
$ret = $pointService->commitConsumePointsTransaction($pointTransactionId);
|
|
|
|
|
if (!$ret) {
|
|
|
|
|
$this->addWarning('apiwarn-isekai-chatcomplete-pointtransactionfailed', 'pointtransactionfailed');
|
|
|
|
|
$this->addWarning('apiwarn-isekai-ai-toolbox-pointtransactionfailed', 'pointtransactionfailed');
|
|
|
|
|
$isSuccess = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -173,7 +173,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
|
|
|
|
|
$dbw = $this->services->getDBLoadBalancer()->getConnection(DB_PRIMARY);
|
|
|
|
|
$dbw->insert(
|
|
|
|
|
'chatcomplete_usage',
|
|
|
|
|
'aitoolbox_usage',
|
|
|
|
|
[
|
|
|
|
|
'user_id' => $transactionData['userid'],
|
|
|
|
|
'action' => $transactionData['useraction'],
|
|
|
|
@ -186,19 +186,19 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->cache->delete(
|
|
|
|
|
$this->cache->makeKey('chatcomplete', 'reportusage', 'transaction', $transactionId)
|
|
|
|
|
$this->cache->makeKey('aitoolboxbot', 'reportusage', 'transaction', $transactionId)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->addWarning('apierror-isekai-chatcomplete-invalidtransaction', 'invalidtransaction');
|
|
|
|
|
$this->addWarning('apierror-isekai-ai-toolbox-invalidtransaction', 'invalidtransaction');
|
|
|
|
|
$isSuccess = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'success', $isSuccess ? 1 : 0);
|
|
|
|
|
$result->addValue(['aitoolboxbot', $this->getModuleName()], 'success', $isSuccess ? 1 : 0);
|
|
|
|
|
break;
|
|
|
|
|
case 'cancel':
|
|
|
|
|
$transactionId = $this->getParameter('transactionid');
|
|
|
|
|
$transactionData = $this->cache->get(
|
|
|
|
|
$this->cache->makeKey('chatcomplete', 'reportusage', 'transaction', $transactionId)
|
|
|
|
|
$this->cache->makeKey('aitoolboxbot', 'reportusage', 'transaction', $transactionId)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$isSuccess = true;
|
|
|
|
@ -213,7 +213,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
|
|
|
|
|
$ret = $pointService->rollbackConsumePointsTransaction($pointTransactionId);
|
|
|
|
|
if (!$ret) {
|
|
|
|
|
$this->addWarning('apiwarn-isekai-chatcomplete-pointtransactionfailed', 'pointtransactionfailed');
|
|
|
|
|
$this->addWarning('apiwarn-isekai-ai-toolbox-pointtransactionfailed', 'pointtransactionfailed');
|
|
|
|
|
$isSuccess = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -222,7 +222,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
|
|
|
|
|
$dbw = $this->services->getDBLoadBalancer()->getConnection(DB_PRIMARY);
|
|
|
|
|
$dbw->insert(
|
|
|
|
|
'chatcomplete_usage',
|
|
|
|
|
'aitoolbox_usage',
|
|
|
|
|
[
|
|
|
|
|
'user_id' => $transactionData['userid'],
|
|
|
|
|
'action' => $transactionData['useraction'],
|
|
|
|
@ -235,14 +235,14 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->cache->delete(
|
|
|
|
|
$this->cache->makeKey('chatcomplete', 'reportusage', 'transaction', $transactionId)
|
|
|
|
|
$this->cache->makeKey('aitoolboxbot', 'reportusage', 'transaction', $transactionId)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->addWarning('apierror-isekai-chatcomplete-invalidtransaction', 'invalidtransaction');
|
|
|
|
|
$this->addWarning('apierror-isekai-ai-toolbox-invalidtransaction', 'invalidtransaction');
|
|
|
|
|
$isSuccess = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'success', $isSuccess ? 1 : 0);
|
|
|
|
|
$result->addValue(['aitoolboxbot', $this->getModuleName()], 'success', $isSuccess ? 1 : 0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
@ -256,24 +256,24 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
$user = $this->services->getUserFactory()->newFromId($userId);
|
|
|
|
|
|
|
|
|
|
if (!$user->isRegistered()) {
|
|
|
|
|
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission');
|
|
|
|
|
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$permissionMap = ApiChatComplete::$permissionMap;
|
|
|
|
|
$permissionMap = ApiAIToolbox::$permissionMap;
|
|
|
|
|
|
|
|
|
|
$permissionKey = $permissionMap[$userAction] ?? null;
|
|
|
|
|
if ($permissionKey && !$user->isAllowed($permissionKey)) {
|
|
|
|
|
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission');
|
|
|
|
|
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$noCost = false;
|
|
|
|
|
if ($userAction === 'chatcomplete' && $user->isAllowed('unlimitedchatcomplete')) {
|
|
|
|
|
if ($userAction === 'chatcomplete' && $user->isAllowed('aitoolbox-unlimited')) {
|
|
|
|
|
$noCost = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pointCostData = ChatCompleteUtils::getPointCost($userAction, $tokens, $extractLines);
|
|
|
|
|
$pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLines);
|
|
|
|
|
|
|
|
|
|
$isSuccess = true;
|
|
|
|
|
|
|
|
|
@ -285,13 +285,13 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
$pointService = $pointFactory->newFromUserId($userId, $pointType);
|
|
|
|
|
|
|
|
|
|
if (!$pointService->hasEnoughPoints($pointCost)) { // User doesn't have enough points
|
|
|
|
|
$this->addError('apierror-isekai-chatcomplete-notenoughpoints', 'notenoughpoints');
|
|
|
|
|
$this->addError('apierror-isekai-ai-toolbox-notenoughpoints', 'notenoughpoints');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ret = $pointService->consumePoints($pointCost, true);
|
|
|
|
|
if (!$ret) {
|
|
|
|
|
$this->addWarning('apiwarn-isekai-chatcomplete-pointtransactionfailed', 'pointtransactionfailed');
|
|
|
|
|
$this->addWarning('apiwarn-isekai-ai-toolbox-pointtransactionfailed', 'pointtransactionfailed');
|
|
|
|
|
$isSuccess = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -300,7 +300,7 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
|
|
|
|
|
$dbw = $this->services->getDBLoadBalancer()->getConnection(DB_PRIMARY);
|
|
|
|
|
$dbw->insert(
|
|
|
|
|
'chatcomplete_usage',
|
|
|
|
|
'aitoolbox_usage',
|
|
|
|
|
[
|
|
|
|
|
'user_id' => $userId,
|
|
|
|
|
'action' => $userAction,
|
|
|
|
@ -313,9 +313,9 @@ class ApiReportUsage extends ApiBase {
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'success', $isSuccess ? 1 : 0);
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'pointtype', $pointType);
|
|
|
|
|
$result->addValue(['chatcompletebot', $this->getModuleName()], 'pointcost', $pointCost);
|
|
|
|
|
$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;
|