更改扩展名称

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

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

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

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

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

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

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

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

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

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

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

@ -1,19 +1,30 @@
<?php <?php
namespace Isekai\ChatComplete; namespace Isekai\AIToolbox;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use Config; use Config;
class Hooks { 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) { public static function onLoad(\OutputPage $outputPage) {
$user = $outputPage->getUser(); $user = $outputPage->getUser();
$permissionManager = MediaWikiServices::getInstance()->getPermissionManager(); $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
if ($user->isRegistered() && $permissionManager->userHasRight($user, 'chatcomplete')) { 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){ 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() { function createToken() {
var api = new mw.Api(); var api = new mw.Api();
return api.postWithToken('csrf', { return api.postWithToken('csrf', {
action: 'chatcomplete', action: 'aitoolbox',
method: 'createtoken' method: 'createtoken'
}); });
} }
function openChatCompletePage(token) { function openAIToolboxPage(token) {
var urlTemplate = mw.config.get('wgIsekaiChatCompleteFrontendUrl'); var urlTemplate = mw.config.get('wgIsekaiAIToolboxFrontendUrl');
var title = mw.config.get('wgTitle'); var title = mw.config.get('wgTitle');
var url = urlTemplate.replace(/\{title\}/g, encodeURIComponent(title)).replace(/\{token\}/g, token); var url = urlTemplate.replace(/\{title\}/g, encodeURIComponent(title)).replace(/\{token\}/g, token);
window.open(url, '_blank'); window.open(url, '_blank');
} }
function launchChatComplete() { function launchAIToolbox() {
var currentTime = new Date().getTime(); var currentTime = new Date().getTime();
if (currentTime - tokenRefreshTime > 3600000) { if (currentTime - tokenRefreshTime > 3600000) {
mw.notify(mw.msg('isekai-chatcomplete-loading'), { mw.notify(mw.msg('isekai-ai-toolbox-loading'), {
id: "loading-chatcomplete-notify" id: "loading-ai-toolbox-notify"
}); });
createToken().done(function(data) { createToken().done(function(data) {
if (data.chatcomplete && data.chatcomplete.createtoken) { if (data.aitoolbox && data.aitoolbox.createtoken) {
authToken = data.chatcomplete.createtoken.token; authToken = data.aitoolbox.createtoken.token;
tokenRefreshTime = new Date().getTime(); tokenRefreshTime = new Date().getTime();
openChatCompletePage(authToken); openAIToolboxPage(authToken);
$('#loading-chatcomplete-notify').trigger('click'); $('#loading-ai-toolbox-notify').trigger('click');
} }
}); });
} else { } else {
openChatCompletePage(authToken); openAIToolboxPage(authToken);
} }
} }
@ -39,22 +39,22 @@ $(function() {
if (mw.config.get('wgIsArticle')) { if (mw.config.get('wgIsArticle')) {
var menuIcon = new OO.ui.IconWidget({ icon: 'robot' }); var menuIcon = new OO.ui.IconWidget({ icon: 'robot' });
isekai.fab.addButton({ isekai.fab.addButton({
id: 'chatcomplete-launcher', id: 'ai-toolbox-launcher',
label: mw.msg('isekai-chatcomplete-menubutton'), label: mw.msg('isekai-ai-toolbox-menubutton'),
icon: menuIcon.$element[0], icon: menuIcon.$element[0],
priority: 90, priority: 90,
onClick: function() { onClick: function() {
launchChatComplete(); launchAIToolbox();
} }
}); });
var bottomMenuIcon = new OO.ui.IconWidget({ icon: 'robot' }); var bottomMenuIcon = new OO.ui.IconWidget({ icon: 'robot' });
bottomNavBtn = isekai.bottomNav.addButton({ bottomNavBtn = isekai.bottomNav.addButton({
id: 'chatcomplete-launcher', id: 'ai-toolbox-launcher',
label: mw.msg('isekai-chatcomplete-menubutton'), label: mw.msg('isekai-ai-toolbox-menubutton'),
icon: bottomMenuIcon.$element[0], icon: bottomMenuIcon.$element[0],
priority: 90, priority: 90,
onClick: function() { 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