Show remaining API credits in AI settings
This commit is contained in:
@@ -5,6 +5,7 @@ namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Http\HttpFactory;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\Controller\BaseController;
|
||||
use Joomla\CMS\Router\Route;
|
||||
@@ -14,6 +15,65 @@ use Komponent\Component\Testquestions\Administrator\Service\AiService;
|
||||
class AiController extends BaseController
|
||||
{
|
||||
public function save(): void
|
||||
{
|
||||
$this->saveSettings();
|
||||
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=ai', false),
|
||||
Text::_('COM_TESTQUESTIONS_AI_SETTINGS_SAVED')
|
||||
);
|
||||
}
|
||||
|
||||
public function checkbalance(): void
|
||||
{
|
||||
$settings = $this->saveSettings();
|
||||
$teamId = trim((string) ($settings['ai_grok_team_id'] ?? ''));
|
||||
$managementKey = trim((string) ($settings['ai_grok_management_key'] ?? ''));
|
||||
|
||||
if ($teamId === '' || $managementKey === '') {
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=ai', false),
|
||||
Text::_('COM_TESTQUESTIONS_AI_GROK_BALANCE_CONFIG_REQUIRED'),
|
||||
'warning'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = HttpFactory::getHttp()->get(
|
||||
'https://management-api.x.ai/v1/billing/teams/' . rawurlencode($teamId) . '/prepaid/balance',
|
||||
['Authorization' => 'Bearer ' . $managementKey, 'Accept' => 'application/json'],
|
||||
30
|
||||
);
|
||||
} catch (\Throwable $error) {
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=ai', false),
|
||||
Text::_('COM_TESTQUESTIONS_AI_GROK_BALANCE_FAILED'),
|
||||
'error'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = json_decode((string) $response->body, true);
|
||||
if ((int) $response->code < 200 || (int) $response->code >= 300 || !isset($body['total']['val']) || !is_numeric($body['total']['val'])) {
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=ai', false),
|
||||
Text::_('COM_TESTQUESTIONS_AI_GROK_BALANCE_FAILED'),
|
||||
'error'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$balance = abs((float) $body['total']['val']) / 100;
|
||||
$message = Text::sprintf('COM_TESTQUESTIONS_AI_GROK_BALANCE_RESULT', number_format($balance, 2, ',', ' '));
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=ai', false),
|
||||
$message,
|
||||
'message'
|
||||
);
|
||||
}
|
||||
|
||||
private function saveSettings(): array
|
||||
{
|
||||
if (!Session::checkToken()) {
|
||||
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
|
||||
@@ -56,38 +116,41 @@ class AiController extends BaseController
|
||||
$settings = json_decode((string) $row->params, true);
|
||||
$settings = is_array($settings) ? $settings : [];
|
||||
$legacyKey = trim((string) ($settings['ai_api_key'] ?? ''));
|
||||
|
||||
$settings['ai_provider'] = $provider;
|
||||
foreach ($selectedModels as $modelProvider => $model) {
|
||||
$settings['ai_' . $modelProvider . '_model'] = $model;
|
||||
}
|
||||
|
||||
foreach (['openai', 'grok'] as $keyProvider) {
|
||||
$fieldName = 'ai_' . $keyProvider . '_api_key';
|
||||
$apiKey = trim((string) $input->post->get($fieldName, '', 'RAW'));
|
||||
|
||||
if ($apiKey !== '') {
|
||||
$settings[$fieldName] = $apiKey;
|
||||
}
|
||||
|
||||
if ($keyProvider === $provider && empty($settings[$fieldName]) && $legacyKey !== '') {
|
||||
$settings[$fieldName] = $legacyKey;
|
||||
}
|
||||
}
|
||||
|
||||
$teamId = trim((string) $input->post->get('ai_grok_team_id', '', 'STRING'));
|
||||
if ($teamId !== '') {
|
||||
$settings['ai_grok_team_id'] = $teamId;
|
||||
}
|
||||
$managementKey = trim((string) $input->post->get('ai_grok_management_key', '', 'RAW'));
|
||||
if ($managementKey !== '') {
|
||||
$settings['ai_grok_management_key'] = $managementKey;
|
||||
}
|
||||
unset($settings['ai_api_key']);
|
||||
|
||||
$extension = (object) [
|
||||
'extension_id' => (int) $row->extension_id,
|
||||
'params' => json_encode($settings, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
];
|
||||
|
||||
if (!$db->updateObject('#__extensions', $extension, 'extension_id')) {
|
||||
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_AI_SAVE_FAILED'));
|
||||
}
|
||||
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=ai', false),
|
||||
Text::_('COM_TESTQUESTIONS_AI_SETTINGS_SAVED')
|
||||
);
|
||||
return $settings;
|
||||
}
|
||||
|
||||
public function cancel(): void
|
||||
|
||||
Reference in New Issue
Block a user