Compare commits

...
2 Commits
Author SHA1 Message Date
Wojtek bb1dc728d1 Diagnose empty AI API key configuration 2026-10-16 12:27:23 +02:00
Wojtek ed2c6b663b Normalize AI provider validation 2026-10-16 12:22:38 +02:00
7 changed files with 28 additions and 6 deletions
+1 -1
View File
@@ -30,6 +30,6 @@ Szkielet komponentu Joomla 6 do gromadzenia pytań testowych.
## Instalacja ## Instalacja
W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.42.5). W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.42.7).
Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**. Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**.
@@ -0,0 +1 @@
-- No database changes required for version 1.42.6.
@@ -0,0 +1 @@
-- No database changes required for version 1.42.7.
@@ -47,9 +47,12 @@ class ResultController extends BaseController
try { try {
$this->getModel('Result')->evaluateWithAi($id, $questionId); $this->getModel('Result')->evaluateWithAi($id, $questionId);
} catch (\Throwable $error) { } catch (\Throwable $error) {
$message = $error->getMessage() === 'AI_NOT_CONFIGURED' $errorMessage = $error->getMessage();
$message = str_starts_with($errorMessage, 'AI_KEY_EMPTY|')
? 'Diagnostyka konfiguracji AI (1.42.7): ' . substr($errorMessage, strlen('AI_KEY_EMPTY|'))
: ($errorMessage === 'AI_NOT_CONFIGURED'
? Text::_('COM_TESTQUESTIONS_AI_NOT_CONFIGURED') ? Text::_('COM_TESTQUESTIONS_AI_NOT_CONFIGURED')
: Text::sprintf('COM_TESTQUESTIONS_AI_ERROR', $error->getMessage()); : Text::sprintf('COM_TESTQUESTIONS_AI_ERROR', $errorMessage));
$type = 'error'; $type = 'error';
} }
@@ -93,6 +93,7 @@ class ResultModel extends BaseDatabaseModel
$params = ComponentHelper::getParams('com_testquestions'); $params = ComponentHelper::getParams('com_testquestions');
$provider = trim((string) $params->get('ai_provider', 'openai')); $provider = trim((string) $params->get('ai_provider', 'openai'));
$apiKey = trim((string) $params->get('ai_api_key', '')); $apiKey = trim((string) $params->get('ai_api_key', ''));
$componentKeyLength = mb_strlen($apiKey);
// Odczytaj zapisane parametry bezpośrednio z bazy. Dzięki temu nie użyjemy // Odczytaj zapisane parametry bezpośrednio z bazy. Dzięki temu nie użyjemy
// nieaktualnej wartości z pamięci ComponentHelper, a maska pola hasła // nieaktualnej wartości z pamięci ComponentHelper, a maska pola hasła
@@ -120,6 +121,17 @@ class ResultModel extends BaseDatabaseModel
} }
} }
if (trim($apiKey) === '') {
$databaseKey = is_array($storedParams) ? trim((string) ($storedParams['ai_api_key'] ?? '')) : '';
$databaseKeyLength = preg_match('/^\*+$/', $databaseKey) ? 0 : mb_strlen($databaseKey);
throw new \RuntimeException(
'AI_KEY_EMPTY|provider=' . ($provider !== '' ? $provider : '(pusty)')
. '|component_chars=' . $componentKeyLength
. '|database_chars=' . $databaseKeyLength
. '|database_params=' . (is_array($storedParams) ? 'json' : 'unreadable')
);
}
$feedback = (new AiService())->evaluate( $feedback = (new AiService())->evaluate(
$provider, $provider,
$apiKey, $apiKey,
@@ -11,14 +11,19 @@ class AiService
public function evaluate(string $provider, string $apiKey, string $question, string $answer, string $pattern): string public function evaluate(string $provider, string $apiKey, string $question, string $answer, string $pattern): string
{ {
$provider = strtolower(trim($provider)); $provider = strtolower(trim($provider));
$provider = preg_replace('/[^a-z]/', '', $provider);
$config = $provider === 'grok' $config = $provider === 'grok'
? ['url' => 'https://api.x.ai/v1/responses', 'model' => 'grok-4.7'] ? ['url' => 'https://api.x.ai/v1/responses', 'model' => 'grok-4.7']
: ['url' => 'https://api.openai.com/v1/responses', 'model' => 'gpt-4.1-mini']; : ['url' => 'https://api.openai.com/v1/responses', 'model' => 'gpt-4.1-mini'];
if (!in_array($provider, ['openai', 'grok'], true) || trim($apiKey) === '') { if (trim($apiKey) === '') {
throw new \RuntimeException('AI_NOT_CONFIGURED'); throw new \RuntimeException('AI_NOT_CONFIGURED');
} }
if (!in_array($provider, ['openai', 'grok'], true)) {
throw new \RuntimeException('Nieprawidłowy dostawca AI: ' . $provider);
}
$input = "Oceń odpowiedź ucznia na pytanie otwarte.\n\n" $input = "Oceń odpowiedź ucznia na pytanie otwarte.\n\n"
. "Pytanie:\n" . $question . "\n\n" . "Pytanie:\n" . $question . "\n\n"
. "Wzorzec odpowiedzi:\n" . $pattern . "\n\n" . "Wzorzec odpowiedzi:\n" . $pattern . "\n\n"
+1 -1
View File
@@ -4,7 +4,7 @@
<creationDate>2026-10-14</creationDate> <creationDate>2026-10-14</creationDate>
<author>Komponent</author> <author>Komponent</author>
<license>GNU General Public License version 2 or later</license> <license>GNU General Public License version 2 or later</license>
<version>1.42.5</version> <version>1.42.7</version>
<description>COM_TESTQUESTIONS_DESCRIPTION</description> <description>COM_TESTQUESTIONS_DESCRIPTION</description>
<element>com_testquestions</element> <element>com_testquestions</element>
<namespace path="src">Komponent\Component\Testquestions</namespace> <namespace path="src">Komponent\Component\Testquestions</namespace>