hardening

This commit is contained in:
Saufi
2026-05-31 00:45:19 +08:00
parent d57187bccc
commit e83a5f5bfe
7 changed files with 168 additions and 19 deletions

View File

@@ -5,6 +5,7 @@ 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;
@@ -23,7 +24,8 @@ use Throwable;
class AskQuestionAction
{
public function __construct(
private readonly RAGService $ragService
private readonly RAGService $ragService,
private readonly PromptGuardService $promptGuard,
) {}
/**
@@ -47,6 +49,33 @@ class AskQuestionAction
// ── 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);