更改扩展名称

main
落雨楓 2 years ago
parent 12611731d7
commit 8b78f4e7d8

@ -1,5 +1,5 @@
{
"name": "hyperzlib/isekai-chat-complete",
"name": "hyperzlib/isekai-ai-toolbox",
"type": "library",
"license": "GPL-2.0-or-later",
"authors": [

@ -1,17 +1,17 @@
{
"name": "Isekai Chat Complete",
"namemsg": "isekai-chatcomplete",
"name": "IsekaiAIToolbox",
"namemsg": "isekai-ai-toolbox",
"author": "Hyperzlib",
"version": "1.0.0",
"url": "https://github.com/Isekai-Project/mediawiki-extension-IsekaiChatComplete",
"descriptionmsg": "isekai-chatcomplete-desc",
"url": "https://github.com/Isekai-Project/mediawiki-extension-IsekaiAIToolbox",
"descriptionmsg": "isekai-ai-toolbox-desc",
"license-name": "GPL-2.0-or-later",
"type": "api",
"requires": {
"MediaWiki": ">= 1.39.0"
},
"MessagesDirs": {
"IsekaiChatComplete": [
"IsekaiAIToolbox": [
"i18n"
]
},
@ -21,50 +21,45 @@
"chatcomplete": true
},
"sysop": {
"unlimitedchatcomplete": true,
"aitoolbox-unlimited": true,
"chatcomplete": true,
"chatcompletebot": true
"aitoolboxbot": true
}
},
"GrantPermissions": {
"chatcomplete": {
"ccembeddingpage": true,
"chatcomplete": true
},
"chatcompletebot": {
"chatcompletebot": true,
"unlimitedchatcomplete": true
"aitoolboxbot": {
"aitoolboxbot": true,
"aitoolbox-unlimited": true
}
},
"AvailableRights": [
"chatcomplete",
"ccembeddingpage",
"unlimitedchatcomplete",
"chatcompletebot"
"aitoolboxbot",
"aitoolbox-unlimited"
],
"GrantPermissionGroups": {
"chatcomplete": "chatcomplete",
"chatcompletebot": "chatcompletebot"
"aitoolboxbot": "aitoolboxbot"
},
"AutoloadNamespaces": {
"Isekai\\ChatComplete\\": "includes"
"Isekai\\AIToolbox\\": "includes"
},
"Hooks": {
"BeforePageDisplay": [
"Isekai\\ChatComplete\\Hooks::onLoad"
"Isekai\\AIToolbox\\Hooks::onLoad"
],
"ResourceLoaderGetConfigVars": [
"Isekai\\ChatComplete\\Hooks::onResourceLoaderGetConfigVars"
"Isekai\\AIToolbox\\Hooks::onResourceLoaderGetConfigVars"
]
},
"APIModules": {
"chatcomplete": "Isekai\\ChatComplete\\Api\\ApiChatComplete",
"chatcompletebot": "Isekai\\ChatComplete\\Api\\ApiChatCompleteBot"
"aitoolbox": "Isekai\\AIToolbox\\Api\\ApiAIToolbox",
"aitoolboxbot": "Isekai\\AIToolbox\\Api\\ApiAIToolboxBot"
},
"ResourceModules": {
"ext.isekai.chatcomplete.launcher": {
"ext.isekai.ai-toolbox.launcher": {
"scripts": [
"ext.isekai.chatcomplete.launcher.js"
"ext.isekai.ai-toolbox.launcher.js"
],
"styles": [],
"dependencies": [
@ -76,32 +71,32 @@
"mobile"
],
"messages": [
"isekai-chatcomplete-menubutton",
"isekai-chatcomplete-loading"
"isekai-ai-toolbox-menubutton",
"isekai-ai-toolbox-loading"
]
}
},
"ResourceFileModulePaths": {
"localBasePath": "modules",
"remoteExtPath": "IsekaiChatComplete/modules"
"remoteExtPath": "IsekaiAIToolbox/modules"
},
"config": {
"IsekaiChatCompleteTokenId": {
"IsekaiAIToolboxTokenId": {
"value": ""
},
"IsekaiChatCompleteToken": {
"IsekaiAIToolboxToken": {
"value": ""
},
"IsekaiChatCompleteUserPoints": {
"IsekaiAIToolboxUserPoints": {
"value": null
},
"IsekaiChatCompleteUserAvatar": {
"IsekaiAIToolboxUserAvatar": {
"value": {
"type": "gravatar"
}
},
"IsekaiChatCompleteFrontendUrl": {
"value": "/chatcomplete/{title}?token={token}"
"IsekaiAIToolboxFrontendUrl": {
"value": "/ai-toolbox/toolbox/{title}?token={token}"
}
},
"load_composer_autoloader": true,

@ -1,53 +1,53 @@
{
"isekai-chatcomplete": "异世界百科 智能写作助手",
"isekai-chatcomplete-desc": "支持询问智能写作助手页面相关的内容。",
"isekai-ai-toolbox": "异世界百科 AI工具箱",
"isekai-ai-toolbox-desc": "使用AI工具箱为Wiki站点添加更多智能服务。",
"isekai-chatcomplete-menubutton": "启动智能助理",
"isekai-chatcomplete-loading": "正在加载智能写作助手",
"isekai-ai-toolbox-menubutton": "启动AI工具箱",
"isekai-ai-toolbox-loading": "正在加载AI工具箱",
"action-chatcomplete": "使用 Chat Complete 功能",
"right-chatcomplete": "使用 Chat Complete 功能",
"action-unlimitedchatcomplete": "无限次数使用 Chat Complete",
"right-unlimitedchatcomplete": "无限次数使用 Chat Complete",
"action-ccembeddingpage": "创建页面的向量索引",
"right-ccembeddingpage": "创建页面的向量索引",
"grant-group-chatcomplete": "使用 Chat Complete",
"grant-chatcomplete": "使用 Chat Complete",
"action-chatcompletebot": "访问 Chat Complete 管理API",
"right-chatcompletebot": "访问 Chat Complete 管理API",
"action-aitoolboxbot": "AI工具箱管理机器人API权限",
"right-aitoolboxbot": "AI工具箱管理机器人API权限",
"grant-group-aitoolboxbot": "AI工具箱管理机器人",
"grant-aitoolboxbot": "AI工具箱管理机器人",
"action-aitoolbox-unlimited": "无限次数使用AI工具箱中的功能",
"right-aitoolbox-unlimited": "无限次数使用AI工具箱中的功能",
"grant-group-chatcompletebot": "Chat Complete 管理API",
"grant-chatcompletebot": "Chat Complete 管理API",
"apihelp-aitoolbox-summary": "AI工具箱相关API",
"apihelp-aitoolbox-param-method": "操作",
"apihelp-chatcomplete-summary": "Chat Complete 相关 API",
"apihelp-chatcomplete-param-method": "操作",
"apihelp-aitoolbox+checkaccess-summary": "检测是否拥有权限,并获取使用开销",
"apihelp-aitoolbox+checkaccess-param-ataction": "在AI工具箱中的操作",
"apihelp-aitoolbox+checkaccess-param-tokens": "提问Token数",
"apihelp-aitoolbox+checkaccess-param-extractlines": "从文档中提取的信息行数上限",
"apihelp-chatcomplete+checkaccess-summary": "检测是否拥有权限,并获取使用开销",
"apihelp-chatcomplete+checkaccess-param-ccaction": "使用的操作",
"apihelp-chatcomplete+checkaccess-param-tokens": "提问 Token 数",
"apihelp-chatcomplete+checkaccess-param-extractlines": "从文档中提取的信息行数上限",
"apihelp-aitoolbox+createtoken-summary": "创建访问AI工具箱所需的Token",
"apihelp-chatcomplete+createtoken-summary": "创建访问 Chat Complete 所需的 Token",
"apihelp-aitoolboxbot-summary": "AI工具箱管理机器人API",
"apihelp-aitoolboxbot-description": "为AI工具箱独立后端提供管理API",
"apihelp-chatcompletebot-summary": "Chat Complete 管理API",
"apihelp-chatcompletebot-description": "为 Chat Complete 独立后端提供管理API",
"apihelp-aitoolboxbot-param-method": "操作",
"apihelp-chatcompletebot-param-method": "操作",
"apihelp-aitoolboxbot+reportusage-summary": "报告用户使用AI工具箱的情况。需要 aitoolboxbot 权限。",
"apihelp-aitoolboxbot+reportusage-param-userid": "用户ID",
"apihelp-aitoolboxbot+reportusage-param-useraction": "在AI工具箱中的操作",
"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",
"apihelp-chatcompletebot+reportusage-summary": "报告用户使用 Chat Complete 的情况。需要 chatcompletebot 权限。",
"apihelp-chatcompletebot+reportusage-param-userid": "用户",
"apihelp-chatcompletebot+reportusage-param-useraction": "使用的操作",
"apihelp-chatcompletebot+reportusage-param-tokens": "使用的Token数",
"apihelp-chatcompletebot+reportusage-param-extractlines": "从文档中提取的信息行数",
"apihelp-chatcompletebot+reportusage-param-step": "操作的阶段",
"apihelp-chatcompletebot+reportusage-param-error": "报告错误信息",
"apihelp-chatcompletebot+reportusage-param-transactionid": "当step为end或cancel时需要事务ID",
"apihelp-aitoolboxbot+userinfo-summary": "为AI工具箱机器人提供Wiki用户信息",
"apihelp-aitoolboxbot+userinfo-param-userid": "用户ID",
"apierror-isekai-chatcomplete-nopermission": "没有使用智能写作助手的权限",
"apierror-isekai-chatcomplete-user-not-exists": "用户不存在",
"apierror-isekai-chatcomplete-noenoughpoints": "积分不足"
"apierror-isekai-ai-toolbox-nopermission": "没有使用智能写作助手的权限",
"apierror-isekai-ai-toolbox-user-not-exists": "用户不存在",
"apierror-isekai-ai-toolbox-noenoughpoints": "积分不足"
}

@ -1,11 +1,11 @@
<?php
namespace Isekai\ChatComplete;
namespace Isekai\AIToolbox;
use MediaWiki\MediaWikiServices;
class ChatCompleteUtils {
class AIToolboxUtils {
public static function getPointCost(string $action = '', int $tokens, int $extractLimit) {
$pointConfig = MediaWikiServices::getInstance()->getMainConfig()->get('IsekaiChatCompleteUserPoints');
$pointConfig = MediaWikiServices::getInstance()->getMainConfig()->get('IsekaiAIToolboxUserPoints');
if (!is_array($pointConfig) && !isset($pointConfig[$action])) {
return false;
}

@ -1,9 +1,9 @@
<?php
namespace Isekai\ChatComplete\Api\ChatComplete;
namespace Isekai\AIToolbox\Api\AIToolbox;
use ApiBase;
use Isekai\ChatComplete\Api\ApiChatComplete;
use Isekai\ChatComplete\ChatCompleteUtils;
use Isekai\AIToolbox\Api\ApiAIToolbox;
use Isekai\AIToolbox\AIToolboxUtils;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
@ -11,10 +11,10 @@ class ApiCheckAccess extends ApiBase {
/** @var MediaWikiServices */
private $services;
/** @var ApiChatComplete */
/** @var ApiAIToolbox */
private $mParent;
public function __construct(ApiChatComplete $main, $method) {
public function __construct(ApiAIToolbox $main, $method) {
parent::__construct($main->getMain(), $method);
$this->mParent = $main;
@ -22,14 +22,14 @@ class ApiCheckAccess extends ApiBase {
}
public function execute() {
$userAction = strtolower($this->getParameter('ccaction'));
$userAction = strtolower($this->getParameter('ataction'));
$tokens = $this->getParameter('tokens');
$extractLimit = $this->getParameter('extractlines');
$user = $this->getUser();
if (!$user->isRegistered()) {
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission');
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
return false;
}
@ -44,24 +44,24 @@ class ApiCheckAccess extends ApiBase {
$permissionKey = $permissionMap[$userAction] ?? null;
if ($permissionKey && !$user->isAllowed($permissionKey)) {
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission');
$result->addValue(['chatcomplete', $this->getModuleName()], 'available', false);
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
$result->addValue(['aitoolbox', $this->getModuleName()], 'available', false);
return false;
}
$noCost = false;
if ($userAction === 'chatcomplete' && $user->isAllowed('unlimitedchatcomplete')) {
if ($userAction === 'chatcomplete' && $user->isAllowed('aitoolbox-unlimited')) {
$noCost = true;
}
$isAvailable = true;
$pointCostData = ChatCompleteUtils::getPointCost($userAction, $tokens, $extractLimit);
$pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLimit);
if ($pointCostData && !$noCost) {
list($pointType, $pointCost) = $pointCostData;
$result->addValue(['chatcomplete', $this->getModuleName()], 'pointtype', $pointType);
$result->addValue(['chatcomplete', $this->getModuleName()], 'pointcost', $pointCost);
$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 */
@ -69,22 +69,22 @@ class ApiCheckAccess extends ApiBase {
$pointService = $pointFactory->newFromUser($user, $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');
$isAvailable = false;
}
} else {
$result->addValue(['chatcomplete', $this->getModuleName()], 'pointtype', null);
$result->addValue(['chatcomplete', $this->getModuleName()], 'pointcost', 0);
$result->addValue(['aitoolbox', $this->getModuleName()], 'pointtype', null);
$result->addValue(['aitoolbox', $this->getModuleName()], 'pointcost', 0);
}
$result->addValue(['chatcomplete', $this->getModuleName()], 'available', $isAvailable);
$result->addValue(['aitoolbox', $this->getModuleName()], 'available', $isAvailable);
return true;
}
public function getAllowedParams($flags = 0) {
return [
'ccaction' => [
'ataction' => [
ParamValidator::PARAM_TYPE => [
'embeddingpage',
'chatcomplete',

@ -1,12 +1,10 @@
<?php
namespace Isekai\ChatComplete\Api\ChatComplete;
namespace Isekai\AIToolbox\Api\AIToolbox;
use ApiBase;
use Config;
use Isekai\ChatComplete\Api\ApiChatComplete;
use Isekai\ChatComplete\Api\ApiChatCompleteBot;
use Isekai\AIToolbox\Api\ApiAIToolbox;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
use Firebase\JWT\JWT;
class ApiCreateToken extends ApiBase {
@ -16,10 +14,10 @@ class ApiCreateToken extends ApiBase {
/** @var Config */
private $config;
/** @var ApiChatComplete */
/** @var ApiAIToolbox */
private $mParent;
public function __construct(ApiChatComplete $main, $method) {
public function __construct(ApiAIToolbox $main, $method) {
parent::__construct($main->getMain(), $method);
$this->mParent = $main;
@ -31,28 +29,28 @@ class ApiCreateToken extends ApiBase {
$user = $this->getUser();
if (!$user->isRegistered()) {
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission');
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
return false;
}
$result = $this->getResult();
$permissionMap = ApiChatComplete::$permissionMap;
$permissionMap = ApiAIToolbox::$permissionMap;
if (!$user->isAllowedAny(...array_values($permissionMap))) {
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission');
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
return false;
}
// Create JWT
$tokenId = $this->config->get('IsekaiChatCompleteTokenId');
$token = $this->config->get('IsekaiChatCompleteToken');
$tokenId = $this->config->get('IsekaiAIToolboxTokenId');
$token = $this->config->get('IsekaiAIToolboxToken');
$currentTime = time();
$expireTime = $currentTime + 86400; // 1 day
$payload = [
'iss' => 'mwchatcomplete',
'iss' => 'mw-ai-toolbox',
'sub' => $user->getId(),
'userName' => $user->getName(),
'iat' => $currentTime,
@ -61,7 +59,7 @@ class ApiCreateToken extends ApiBase {
$jwt = JWT::encode($payload, $token, 'HS256', $tokenId);
$result->addValue(['chatcomplete', $this->getModuleName()], 'token', $jwt);
$result->addValue(['aitoolbox', $this->getModuleName()], 'token', $jwt);
return true;
}

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

@ -1,8 +1,8 @@
<?php
namespace Isekai\ChatComplete\Api\ChatCompleteBot;
namespace Isekai\AIToolbox\Api\AIToolboxBot;
use ApiBase;
use Isekai\ChatComplete\Api\ApiChatCompleteBot;
use Isekai\AIToolbox\Api\ApiAIToolboxBot;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
@ -17,10 +17,10 @@ class ApiUserInfo extends ApiBase {
/** @var Config */
private $config;
/** @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;
@ -52,7 +52,7 @@ class ApiUserInfo extends ApiBase {
public function execute() {
// Check permission
$this->checkUserRightsAny('chatcompletebot');
$this->checkUserRightsAny('aitoolboxbot');
$userFactory = $this->services->getUserFactory();
$userOptionsLookup = $this->services->getUserOptionsLookup();
@ -62,7 +62,7 @@ class ApiUserInfo extends ApiBase {
$user = $userFactory->newFromId($userId);
if (!$user->isRegistered()) {
$this->dieWithError('apierror-chatcomplete-user-not-found', 'user-not-found');
$this->dieWithError('apierror-ai-toolbox-user-not-found', 'user-not-found');
}
$userInfo = [
@ -74,7 +74,7 @@ class ApiUserInfo extends ApiBase {
];
// Get user points from IsekaiUserPoints
$pointConfig = $this->config->get('IsekaiChatCompleteUserPoints');
$pointConfig = $this->config->get('IsekaiAIToolboxUserPoints');
if (is_array($pointConfig) && isset($pointConfig[self::DEFAULT_CC_ACTION])) {
$pointType = $pointConfig[self::DEFAULT_CC_ACTION]['pointType'];
@ -88,7 +88,7 @@ class ApiUserInfo extends ApiBase {
}
// Get user avatar
$avatarTpl = $this->config->get('IsekaiChatCompleteUserAvatar');
$avatarTpl = $this->config->get('IsekaiAIToolboxUserAvatar');
$avatarUrl = null;
if (is_array($avatarTpl) && isset($avatarTpl['type'])) {
if ($avatarTpl['type'] === 'gravatar') {
@ -108,7 +108,7 @@ class ApiUserInfo extends ApiBase {
}
$result = $this->getResult();
$result->addValue(['chatcompletebot'], $this->getModuleName(), $userInfo);
$result->addValue(['aitoolboxbot'], $this->getModuleName(), $userInfo);
}
public function getAllowedParams($flags = 0) {

@ -1,15 +1,15 @@
<?php
namespace Isekai\ChatComplete\Api;
namespace Isekai\AIToolbox\Api;
use ApiBase;
use ApiMain;
use ApiModuleManager;
use Isekai\ChatComplete\Api\ChatComplete\ApiCheckAccess;
use Isekai\ChatComplete\Api\ChatComplete\ApiCreateToken;
use Isekai\AIToolbox\Api\AIToolbox\ApiCheckAccess;
use Isekai\AIToolbox\Api\AIToolbox\ApiCreateToken;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
class ApiChatComplete extends ApiBase {
class ApiAIToolbox extends ApiBase {
public static $permissionMap = [
'embeddingpage' => 'ccembeddingpage',
'chatcomplete' => 'chatcomplete',

@ -1,15 +1,15 @@
<?php
namespace Isekai\ChatComplete\Api;
namespace Isekai\AIToolbox\Api;
use ApiBase;
use ApiMain;
use ApiModuleManager;
use Isekai\ChatComplete\Api\ChatCompleteBot\ApiReportUsage;
use Isekai\ChatComplete\Api\ChatCompleteBot\ApiUserInfo;
use Isekai\AIToolbox\Api\AIToolboxBot\ApiReportUsage;
use Isekai\AIToolbox\Api\AIToolboxBot\ApiUserInfo;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
class ApiChatCompleteBot extends ApiBase {
class ApiAIToolboxBot extends ApiBase {
public static $methodModules = [
'reportusage' => [
'class' => ApiReportUsage::class,

@ -1,19 +1,30 @@
<?php
namespace Isekai\ChatComplete;
namespace Isekai\AIToolbox;
use MediaWiki\MediaWikiServices;
use Config;
class Hooks {
/**
* Implements LoadExtensionSchemaUpdates hook.
*
* @param \DatabaseUpdater $updater
*/
public static function onLoadExtensionSchemaUpdates($updater) {
$dir = dirname(__DIR__) . '/sql/';
$type = $updater->getDB()->getType();
$updater->addExtensionTable('aitoolbox_usage', $dir . $type . '/aitoolbox_usage.sql');
}
public static function onLoad(\OutputPage $outputPage) {
$user = $outputPage->getUser();
$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
if ($user->isRegistered() && $permissionManager->userHasRight($user, 'chatcomplete')) {
$outputPage->addModules(["ext.isekai.chatcomplete.launcher"]);
$outputPage->addModules(["ext.isekai.ai-toolbox.launcher"]);
}
}
public static function onResourceLoaderGetConfigVars(array &$vars, string $skin, Config $config){
$vars['wgIsekaiChatCompleteFrontendUrl'] = $config->get('IsekaiChatCompleteFrontendUrl');
$vars['wgIsekaiAIToolboxFrontendUrl'] = $config->get('IsekaiAIToolboxFrontendUrl');
}
}

@ -4,34 +4,34 @@ var tokenRefreshTime = 0;
function createToken() {
var api = new mw.Api();
return api.postWithToken('csrf', {
action: 'chatcomplete',
action: 'aitoolbox',
method: 'createtoken'
});
}
function openChatCompletePage(token) {
var urlTemplate = mw.config.get('wgIsekaiChatCompleteFrontendUrl');
function openAIToolboxPage(token) {
var urlTemplate = mw.config.get('wgIsekaiAIToolboxFrontendUrl');
var title = mw.config.get('wgTitle');
var url = urlTemplate.replace(/\{title\}/g, encodeURIComponent(title)).replace(/\{token\}/g, token);
window.open(url, '_blank');
}
function launchChatComplete() {
function launchAIToolbox() {
var currentTime = new Date().getTime();
if (currentTime - tokenRefreshTime > 3600000) {
mw.notify(mw.msg('isekai-chatcomplete-loading'), {
id: "loading-chatcomplete-notify"
mw.notify(mw.msg('isekai-ai-toolbox-loading'), {
id: "loading-ai-toolbox-notify"
});
createToken().done(function(data) {
if (data.chatcomplete && data.chatcomplete.createtoken) {
authToken = data.chatcomplete.createtoken.token;
if (data.aitoolbox && data.aitoolbox.createtoken) {
authToken = data.aitoolbox.createtoken.token;
tokenRefreshTime = new Date().getTime();
openChatCompletePage(authToken);
$('#loading-chatcomplete-notify').trigger('click');
openAIToolboxPage(authToken);
$('#loading-ai-toolbox-notify').trigger('click');
}
});
} else {
openChatCompletePage(authToken);
openAIToolboxPage(authToken);
}
}
@ -39,22 +39,22 @@ $(function() {
if (mw.config.get('wgIsArticle')) {
var menuIcon = new OO.ui.IconWidget({ icon: 'robot' });
isekai.fab.addButton({
id: 'chatcomplete-launcher',
label: mw.msg('isekai-chatcomplete-menubutton'),
id: 'ai-toolbox-launcher',
label: mw.msg('isekai-ai-toolbox-menubutton'),
icon: menuIcon.$element[0],
priority: 90,
onClick: function() {
launchChatComplete();
launchAIToolbox();
}
});
var bottomMenuIcon = new OO.ui.IconWidget({ icon: 'robot' });
bottomNavBtn = isekai.bottomNav.addButton({
id: 'chatcomplete-launcher',
label: mw.msg('isekai-chatcomplete-menubutton'),
id: 'ai-toolbox-launcher',
label: mw.msg('isekai-ai-toolbox-menubutton'),
icon: bottomMenuIcon.$element[0],
priority: 90,
onClick: function() {
launchChatComplete();
launchAIToolbox();
}
});
}

@ -0,0 +1,17 @@
CREATE TABLE /*_*/aitoolbox_usage (
`id` int UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`user_id` int UNSIGNED NOT NULL,
`action` varchar(50) NOT NULL,
`tokens` int UNSIGNED NOT NULL,
`real_tokens` int UNSIGNED NOT NULL,
`timestamp` bigint UNSIGNED NOT NULL,
`success` tinyint NOT NULL,
`error` text NOT NULL
) /*$wgDBTableOptions*/;
ALTER TABLE /*_*/aitoolbox_usage
ADD KEY `user_id` (`user_id`),
ADD KEY `timestamp` (`timestamp`),
ADD KEY `success` (`success`),
ADD KEY `action` (`action`),
ADD KEY `real_tokens` (`real_tokens`);
Loading…
Cancel
Save