Files
ChatbotAI/app/Actions/Chatbot/AskQuestionAction.php
2026-05-31 00:45:19 +08:00

174 lines
5.5 KiB
PHP

<?php
namespace App\Actions\Chatbot;
use App\Jobs\LogChatInteractionJob;
use App\Models\ChatLog;
use App\Models\ChatSession;
use App\Services\KnowledgeBase\PromptGuardService;
use App\Services\KnowledgeBase\RAGService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use RuntimeException;
use Throwable;
/**
* AskQuestionAction
*
* Tanggungjawab: Koordinasi satu soalan chatbot.
* 1. Urus sesi
* 2. Panggil RAGService
* 3. Dispatch log job (async)
* 4. Return result
*/
class AskQuestionAction
{
public function __construct(
private readonly RAGService $ragService,
private readonly PromptGuardService $promptGuard,
) {}
/**
* @param string $question
* @param ?int $categoryId
* @param Request $request
* @return array{
* answer: string,
* has_answer: bool,
* sources: array[],
* session_token: string,
* chat_log_id: ?int
* }
* @throws RuntimeException
*/
public function execute(
string $question,
?int $categoryId,
Request $request
): array {
// ── Urus sesi ────────────────────────────────────────────────────
$session = $this->resolveSession($request, $categoryId);
// ── Semak input sebelum hantar ke AI ─────────────────────────────
$guard = $this->promptGuard->check($question);
if ($guard['blocked']) {
LogChatInteractionJob::dispatch(
$session->session_token,
auth()->id(),
$categoryId,
$question,
'INPUT_BLOCKED',
[],
[],
'prompt-guard',
null,
0.0,
false,
true,
);
return [
'answer' => 'Maaf, saya tidak dapat memproses permintaan tersebut. Sila kemukakan soalan berkaitan perkhidmatan kami.',
'has_answer' => false,
'sources' => [],
'session_token' => $session->session_token,
];
}
// ── Jawab soalan melalui RAG ──────────────────────────────────────
try {
$result = $this->ragService->ask($question, $categoryId);
} catch (RuntimeException $e) {
$this->logFailedAttempt($session, $question, $categoryId, $e);
throw $e;
}
// ── Log secara async (jangan tangguh response) ────────────────────
LogChatInteractionJob::dispatch(
$session->session_token,
auth()->id(),
$categoryId,
$question,
$result['answer'],
$result['sources'],
$result['context_chunks'],
$result['model_used'],
$result['tokens_used'],
$result['response_time'],
$result['has_answer'],
);
return [
'answer' => $result['answer'],
'has_answer' => $result['has_answer'],
'sources' => $result['sources'],
'session_token' => $session->session_token,
];
}
private function resolveSession(Request $request, ?int $categoryId): ChatSession
{
$token = $request->session()->get('chat_session_token');
if ($token) {
$session = ChatSession::where('session_token', $token)->first();
if ($session) {
return $session;
}
}
// Buat sesi baru
$session = ChatSession::create([
'user_id' => auth()->id(),
'category_id' => $categoryId,
'ip_address' => $request->ip(),
'user_agent' => $request->userAgent(),
]);
$request->session()->put('chat_session_token', $session->session_token);
return $session;
}
private function logFailedAttempt(
ChatSession $session,
string $question,
?int $categoryId,
RuntimeException $exception
): void {
Log::warning('Chatbot request failed before AI response', [
'chat_session_id' => $session->id,
'category_id' => $categoryId,
'question' => mb_substr($question, 0, 200),
'error' => $exception->getMessage(),
'previous' => $exception->getPrevious()?->getMessage(),
]);
try {
ChatLog::create([
'chat_session_id' => $session->id,
'user_id' => auth()->id(),
'category_id' => $categoryId,
'question' => $question,
'answer' => 'Perkhidmatan AI tidak tersedia pada masa ini. Sila cuba sebentar lagi.',
'sources_used' => [],
'context_chunks' => [],
'model_used' => config('ollama.chat_model'),
'tokens_used' => null,
'response_time' => null,
'has_answer' => false,
'is_flagged' => true,
]);
$session->update(['last_activity_at' => now()]);
} catch (Throwable $logException) {
Log::error('Failed to persist failed chatbot request', [
'chat_session_id' => $session->id,
'error' => $logException->getMessage(),
]);
}
}
}