Replace AI balance with usage counters
This commit is contained in:
@@ -5,7 +5,6 @@ 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;
|
||||
@@ -24,55 +23,6 @@ class AiController extends BaseController
|
||||
);
|
||||
}
|
||||
|
||||
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()) {
|
||||
@@ -132,15 +82,8 @@ class AiController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
$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']);
|
||||
unset($settings['ai_grok_team_id'], $settings['ai_grok_management_key']);
|
||||
|
||||
$extension = (object) [
|
||||
'extension_id' => (int) $row->extension_id,
|
||||
|
||||
Reference in New Issue
Block a user