First commit

This commit is contained in:
Saufi
2026-05-18 08:56:23 +08:00
commit fd3d3a4d2b
147 changed files with 22099 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
<?php
namespace App\Actions\Chatbot;
use App\Jobs\LogChatInteractionJob;
use App\Models\ChatSession;
use App\Services\KnowledgeBase\RAGService;
use Illuminate\Http\Request;
use RuntimeException;
/**
* 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
) {}
/**
* @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);
// ── Jawab soalan melalui RAG ──────────────────────────────────────
$result = $this->ragService->ask($question, $categoryId);
// ── 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;
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace App\Actions\Document;
use App\Jobs\ProcessUploadedDocumentJob;
use App\Models\Document;
use App\Models\DocumentVersion;
use App\Services\KnowledgeBase\AuditService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;
/**
* CreateDocumentAction
*
* Tanggungjawab: Simpan dokumen baru + versi pertama + dispatch job.
* Dipanggil oleh DocumentController@store.
*/
class CreateDocumentAction
{
public function __construct(
private readonly AuditService $auditService
) {}
/**
* @param array $data Data dari StoreDocumentRequest
* @param UploadedFile $file Fail PDF yang diupload
* @return Document
* @throws RuntimeException
*/
public function execute(array $data, UploadedFile $file): Document
{
// Semak duplicate (hash yang sama dalam kategori yang sama)
$fileHash = hash_file('sha256', $file->getRealPath());
$existing = DocumentVersion::where('file_hash', $fileHash)->first();
if ($existing) {
throw new RuntimeException(
"Fail ini sudah pernah diupload. Sila semak dokumen: " .
$existing->document->title
);
}
return DB::transaction(function () use ($data, $file, $fileHash) {
// ── Buat rekod dokumen ────────────────────────────────────────
$document = Document::create([
'category_id' => $data['category_id'],
'title' => $data['title'],
'description' => $data['description'] ?? null,
'status' => Document::STATUS_PROCESSING,
'is_active' => false,
'effective_date' => $data['effective_date'] ?? null,
'expiry_date' => $data['expiry_date'] ?? null,
'tags' => $data['tags'] ?? [],
'language' => $data['language'] ?? 'ms',
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
// ── Simpan fail PDF ────────────────────────────────────────────
$storedPath = $this->storePdf($file, $document->id, 1);
// ── Buat rekod versi ──────────────────────────────────────────
$version = DocumentVersion::create([
'document_id' => $document->id,
'version_number' => 1,
'original_filename' => $file->getClientOriginalName(),
'stored_path' => $storedPath,
'mime_type' => $file->getMimeType(),
'file_size' => $file->getSize(),
'file_hash' => $fileHash,
'processing_status' => DocumentVersion::STATUS_PENDING,
'is_current' => true,
'change_notes' => $data['change_notes'] ?? 'Versi pertama.',
'uploaded_by' => auth()->id(),
]);
// ── Audit log ─────────────────────────────────────────────────
$this->auditService->documentUploaded($document, $version);
// ── Dispatch job ke queue ─────────────────────────────────────
ProcessUploadedDocumentJob::dispatch($version->id);
return $document->load('currentVersion');
});
}
private function storePdf(UploadedFile $file, int $documentId, int $versionNumber): string
{
$disk = config('knowledgebase.upload.storage_disk', 'local');
$folder = "documents/{$documentId}/v{$versionNumber}";
$filename = Str::uuid() . '.pdf';
$path = $file->storeAs($folder, $filename, $disk);
if (!$path) {
throw new RuntimeException('Gagal simpan fail PDF ke storage.');
}
return $path;
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace App\Actions\Document;
use App\Jobs\ProcessUploadedDocumentJob;
use App\Models\Document;
use App\Models\DocumentVersion;
use App\Services\KnowledgeBase\AuditService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;
/**
* UploadNewVersionAction
*
* Upload versi baru untuk dokumen yang sedia ada.
* Versi lama TIDAK dipadam kekal dalam storage dan MySQL.
* Chunk versi lama akan di-deactivate (bukan delete) semasa ingestion.
*/
class UploadNewVersionAction
{
public function __construct(
private readonly AuditService $auditService
) {}
public function execute(Document $document, UploadedFile $file, array $data): DocumentVersion
{
$fileHash = hash_file('sha256', $file->getRealPath());
// Semak sama ada hash sama dengan versi semasa
$currentVersion = $document->currentVersion;
if ($currentVersion && $currentVersion->file_hash === $fileHash) {
throw new RuntimeException(
'Fail ini sama dengan versi semasa. Tiada perubahan.'
);
}
return DB::transaction(function () use ($document, $file, $fileHash, $data) {
$newVersionNumber = $document->getLatestVersionNumber() + 1;
// ── Simpan fail baru ──────────────────────────────────────────
$disk = config('knowledgebase.upload.storage_disk', 'local');
$folder = "documents/{$document->id}/v{$newVersionNumber}";
$filename = Str::uuid() . '.pdf';
$path = $file->storeAs($folder, $filename, $disk);
if (!$path) {
throw new RuntimeException('Gagal simpan fail PDF ke storage.');
}
// ── Buat rekod versi baru ─────────────────────────────────────
$version = DocumentVersion::create([
'document_id' => $document->id,
'version_number' => $newVersionNumber,
'original_filename' => $file->getClientOriginalName(),
'stored_path' => $path,
'mime_type' => $file->getMimeType(),
'file_size' => $file->getSize(),
'file_hash' => $fileHash,
'processing_status' => DocumentVersion::STATUS_PENDING,
'is_current' => false, // akan di-set current semasa ingestion
'change_notes' => $data['change_notes'] ?? null,
'uploaded_by' => auth()->id(),
]);
// Kemaskini status dokumen ke processing
$document->update([
'status' => Document::STATUS_PROCESSING,
'updated_by' => auth()->id(),
]);
// ── Audit log ─────────────────────────────────────────────────
$this->auditService->documentUploaded($document, $version);
// ── Dispatch job ──────────────────────────────────────────────
ProcessUploadedDocumentJob::dispatch($version->id);
return $version;
});
}
}