690519:0819 Prepare refactor AI [skip ci]

This commit is contained in:
Nattanin
2026-05-19 08:19:40 +07:00
parent 7e6ad0d346
commit 3e25097470
25 changed files with 1307 additions and 59916 deletions
+421
View File
@@ -0,0 +1,421 @@
🧠 🎯 เป้าหมาย: AI DMS (Document Management System อัจฉริยะ)
> จาก “เก็บเอกสาร” → กลายเป็น “ผู้ช่วยควบคุมเอกสาร”
---
🏗️ 1. Architecture ภาพรวม
[User]
(Web UI / Chat UI)
AI Agent Layer ← (หัวใจ)
| RAG Engine (ค้นหาเอกสาร)
| Workflow Engine (RFA logic)
| Tool Layer (DB / File / API)
[Database + File Storage]
---
🔥 2. Core Components (ต้องมี)
2.1 AI Agent (สมอง)
ใช้แนวคิดจาก:
LangChain / OpenClaw / CrewAI
💡 ทำอะไร:
เข้าใจคำถาม user
ตัดสินใจว่าจะ:
query DB
search document
run workflow
---
2.2 RAG System (ค้นหาเอกสาร)
💡 ใช้:
vector DB (เช่น Qdrant / Chroma)
💡 ทำอะไร:
search PDF / Drawing / Spec
ตอบคำถามจาก content
👉 use case:
“Drawing A-101 revision ล่าสุดคืออะไร”
“RFA นี้เกี่ยวข้องกับ drawing ไหน”
---
2.3 Tool Layer (สำคัญมาก)
AI จะไม่เก่งถ้าไม่มี tools
tools ที่ต้องมี:
get_rfa(id)
get_drawings_by_rfa(rfa_id)
get_latest_revision(drawing_code)
search_documents(query)
get_transmittal_history()
👉 ตรงนี้ = bridge ระหว่าง AI กับ DB จริงของคุณ
---
2.4 Workflow Engine (logic ธุรกิจ)
💡 ไม่ให้ AI มั่ว
เช่น:
RFA status flow
approval logic
revision control
👉 AI = “ช่วยคิด”
👉 workflow = “ของจริง”
---
2.5 File Storage
เก็บไฟล์จริง (PDF, DWG)
version control
naming convention
👉 ตรงกับ backlog คุณข้อ 9 พอดี
---
⚙️ 3. Database Design (ต่อยอดของเดิมคุณ)
คุณมีอยู่แล้ว:
RFAs
Drawings
Revisions
👉 เพิ่ม:
🧩 ai_embeddings
document_id
chunk_text
vector
🧩 ai_logs
user_query
agent_decision
tool_used
🧩 ai_tasks (optional)
background job
document indexing
---
🤖 4. AI Use Cases (ของจริงที่ควรทำ)
🔎 1. Smart Search
> “ขอ drawing structural ล่าสุดของ zone B”
AI:
เข้าใจ intent
query DB + RAG
---
📄 2. Document QA
> “สรุป spec นี้”
AI:
อ่าน PDF
summarize
---
🔗 3. Relationship Mapping
> “RFA นี้เกี่ยวกับ drawing อะไร”
AI:
join:
rfa → drawing → revision
---
📊 4. Timeline Analysis
> “RFA นี้ delay เพราะอะไร”
AI:
วิเคราะห์ revision timeline
---
🧠 5. Auto Classification
upload file → AI tag:
type
discipline
revision
---
⚠️ 6. Alert / Assistant
“Drawing นี้ outdated”
“RFA ใกล้ deadline”
---
🧩 5. UI Design (สำคัญมาก)
5.1 Hybrid UI
Table (DataTables)
Chat (AI assistant)
[ Table RFAs ] | [ AI Chat ]
| > RFA ล่าสุดคืออะไร
| > Drawing ไหนยังไม่ approve
👉 best of both worlds
---
5.2 Drawing Page (โคตรสำคัญ)
แสดง:
revision history
related RFAs
AI summary
---
⚡ 6. Tech Stack (แนะนำตรงๆ)
Backend
PHP (ของเดิมคุณ)
Python (AI service)
AI Layer
LangChain หรือ custom agent
Ollama (local) หรือ OpenAI
Vector DB
Qdrant (แนะนำ)
Frontend
DataTables (ตาม backlog)
Chat UI
---
🔥 7. Flow การทำงานจริง
📥 Upload Drawing
1. user upload PDF
2. system:
extract text (OCR)
chunk
embed → vector DB
3. AI tag metadata
---
🔎 Query
user:
> “drawing ล่าสุดของ contract นี้”
AI:
1. เข้าใจ intent
2. call tool:
get_drawings(contract_id)
3. filter revision ล่าสุด
4. ตอบ
---
🧠 8. Insight สำคัญ (อย่าพลาด)
❌ อย่าทำแบบนี้
ให้ AI query DB ตรงๆ
ให้ AI ตัดสินใจ workflow
✅ ต้องทำแบบนี้
AI → call function (tool)
system → validate logic
---
🚀 9. Roadmap (ทำจริง)
Phase 1 (24 สัปดาห์)
AI chat + query DB
basic tools
Phase 2
RAG (search document)
embedding
Phase 3
automation
alert
classification
---
🎯 10. ถ้าจะทำให้ “โหดจริง”
👉 ultimate version:
AI = Document Controller
auto check:
revision mismatch
missing approval
outdated drawing
---
💡 สรุปสั้น
AI DMS =
> “AI + Workflow + Document + Database”
ไม่ใช่แค่ chatbot
---
จากเนื้อหาข้างบน เทียบกับ repo ของผม ขอสรุป แบบละเฮียกทีละข้อ
+189
View File
@@ -0,0 +1,189 @@
จากการวิเคราะห์ repo `lcbp3` อย่างละเอียด เทียบกับ blueprint AI DMS ของคุณแบบข้อต่อข้อ:
---
## 🔍 สรุปเทียบ Blueprint AI DMS กับ Repo `lcbp3`
---
### 🏗️ 1. Architecture ภาพรวม
| Blueprint | Repo `lcbp3` | สถานะ |
|---|---|---|
| Web UI / Chat UI | ✅ Next.js 16.2.0 + shadcn/ui + `RagChatWidget.tsx` | **มีแล้ว** |
| AI Agent Layer | ✅ `AiService` + `AiRagService` + `AiQueueService` — ใช้ **n8n + Ollama** pipeline ตาม ADR-023 | **มีแล้ว** (คนละ approach) |
| RAG Engine | ✅ `AiRagService` + `AiQdrantService` (Qdrant vector DB, project-isolated) | **มีแล้ว** |
| Workflow Engine | ✅ ADR-021 Unified Workflow Engine (DSL-based สำหรับ Correspondences, RFAs, Circulations) | **มีแล้ว** |
| Tool Layer | ⚠️ มี AI services (embedding, OCR, Ollama, Qdrant, migration) แต่ไม่มี tool functions ที่ blueprint ระบุ (เช่น `get_rfa(id)`, `get_drawings_by_rfa(rfa_id)`) ใน AI module | **บางส่วน** |
| DB + File Storage | ✅ MariaDB 11.8 + Two-Phase File Storage (Multer + ClamAV) | **มีแล้ว** |
**💡 ความเห็น:** โครงสร้างพื้นฐานตรงตาม blueprint ~85% แต่ AI Agent Layer ใช้ n8n+Ollama แทน LangChain/CrewAI ที่ blueprint แนะนำ ซึ่งเป็น architectural decision ที่ **ถูกต้องแล้ว** สำหรับบริบทของคุณ (local deployment บน QNAP, เน้น privacy, no external API) — ADR-023 รวม ADR-017, 017B, 018, 020, 022 เข้าด้วยกันเป็น Unified AI Architecture
---
### 🔥 2. Core Components (ต้องมี)
#### 2.1 AI Agent (สมอง)
| Blueprint แนะนำ | Repo จริง | หมายเหตุ |
|---|---|---|
| LangChain / OpenClaw / CrewAI | **n8n + Ollama** | ADR-023 เลือก n8n workflow orchestration + Ollama local LLM inference — pragmatic choice สำหรับ QNAP NAS ที่มี RAM 32GB |
**💡 ความเห็น:** n8n เป็น low-code workflow automation ที่ stable กว่า LangChain สำหรับ production use case นี้ และ Ollama รองรับการรัน local LLM บน CPU ได้ดี จุดที่ยังขาดคือ **agentic decision-making** — ตอนนี้ AI ยังไม่สามารถ "ตัดสินใจว่าจะ query DB / search document / run workflow" ได้เอง ต้องต่อยอดจาก n8n workflow + AI services ที่มีอยู่
#### 2.2 RAG System (ค้นหาเอกสาร)
| Blueprint | Repo | สถานะ |
|---|---|---|
| Vector DB (Qdrant/Chroma) | ✅ **Qdrant**`AiQdrantService` ใช้ `@qdrant/js-client-rest`, collection `lcbp3_vectors`, 768-dim vectors, Cosine distance, **project-isolated** payload filter | **ครบ** |
| Search PDF/Drawing/Spec | ✅ `AiRagService` — query ผ่าน Ollama + Qdrant, BullMQ-backed async pipeline, Redis caching, TTL 5 นาที, FR-009 (1 active job per user) | **ครบ** |
| Embedding | ✅ `embedding.service.ts` + Ollama embed model (`nomic-embed-text`) | **มีแล้ว** |
| Chunking | ⚠️ มี `ai-ingest.service.ts` สำหรับ document ingestion แต่ไม่เห็น chunking strategy ชัดเจน | **ต้องตรวจสอบ** |
| Use case: "Drawing A-101 revision ล่าสุด" | ❌ ยังไม่เห็น integration ระหว่าง RAG search ↔ drawing module โดยตรง | **ยังไม่มี** |
#### 2.3 Tool Layer
| Blueprint Tool | Repo | สถานะ |
|---|---|---|
| `get_rfa(id)` | ❌ ไม่มีใน AI module (มีใน RFA module แต่ AI เรียกตรงไม่ได้) | **ต้องพัฒนา** |
| `get_drawings_by_rfa(rfa_id)` | ❌ ไม่มี cross-module tool | **ต้องพัฒนา** |
| `get_latest_revision(drawing_code)` | ❌ ไม่มีใน AI module | **ต้องพัฒนา** |
| `search_documents(query)` | ✅ มีผ่าน RAG + Elasticsearch | **มีแล้ว** |
| `get_transmittal_history()` | ❌ ไม่มี | **ต้องพัฒนา** |
**💡 Insight:** ตรงนี้คือ **gap สำคัญที่สุด** — blueprint บอกว่า tool layer คือ "bridge ระหว่าง AI กับ DB จริง" แต่ repo ตอนนี้ AI module แยกขาดจาก business modules (RFA, Drawing, Transmittal) ตาม ADR-018 AI Boundary Policy (ต่อมาถูกรวมเข้า ADR-023) ที่กำหนดว่า **"Ollama Isolation มี No Direct DB/Storage Access"** — นี่คือ security boundary ที่ดี แต่ต้องสร้าง tool functions เป็น bridge จริงๆ
#### 2.4 Workflow Engine
| Blueprint | Repo | สถานะ |
|---|---|---|
| RFA status flow | ✅ `workflow-engine` module + `rfas` components | **มีแล้ว** |
| Approval logic | ✅ ADR-021 Integrated Context Workflow | **มีแล้ว** |
| Revision control | ✅ Drawing module มี revision tracking | **มีแล้ว** |
#### 2.5 File Storage
| Blueprint | Repo | สถานะ |
|---|---|---|
| เก็บไฟล์จริง (PDF, DWG) | ✅ Two-Phase File Storage, Multer + ClamAV virus scanning | **มีแล้ว** |
| Version control | ✅ Schema v1.8.0 รองรับ | **มีแล้ว** |
| Naming convention | ✅ Document Numbering (ADR-002, Double-lock mechanism) | **มีแล้ว** |
---
### ⚙️ 3. Database Design
| Blueprint Suggestion | Repo | สถานะ |
|---|---|---|
| `ai_embeddings` (document_id, chunk_text, vector) | ✅ Qdrant เก็บ vectors แยกจาก MariaDB, collection `lcbp3_vectors` พร้อม payload (project_public_id, public_id) | **มีแล้ว** (คนละ implementation) |
| `ai_logs` (user_query, agent_decision, tool_used) | ✅ `ai-audit-log.entity.ts` + `AuditLog` entity — บันทึก AI jobs, callbacks, results | **มีแล้ว** |
| `ai_tasks` (background job, indexing) | ✅ BullMQ queues (`ai-realtime`, `ai-batch`) + `MigrationLog` entity | **มีแล้ว** |
| RFAs, Drawings, Revisions | ✅ มีครบทุก module | **มีแล้ว** |
---
### 🤖 4. AI Use Cases (ของจริง)
| Use Case | สถานะใน Repo |
|---|---|
| **1. Smart Search** — "ขอ drawing structural ล่าสุดของ zone B" | ⚠️ RAG search มีแล้ว + Elasticsearch แต่ยังไม่รวม intent understanding + cross-module query |
| **2. Document QA** — "สรุป spec นี้" | ⚠️ `AiRagService` รองรับ Q&A บน document content ผ่าน Ollama — มีพื้นฐานแล้ว |
| **3. Relationship Mapping** — "RFA นี้เกี่ยวกับ drawing อะไร" | ❌ ยังไม่มี cross-module relationship query |
| **4. Timeline Analysis** — "RFA นี้ delay เพราะอะไร" | ❌ ยังไม่มี |
| **5. Auto Classification** — upload file → AI tag | ✅ `ai-ingest.service.ts` + metadata extraction + AI Suggestion (`POST /ai/suggest`) |
| **6. Alert / Assistant** — "Drawing นี้ outdated", "RFA ใกล้ deadline" | ⚠️ มี `reminder` module + BullMQ queues แต่ยังไม่ integrated กับ AI |
---
### 🧩 5. UI Design
| Blueprint | Repo | สถานะ |
|---|---|---|
| Hybrid UI (Table + Chat) | ✅ DataTables-style + `RagChatWidget.tsx` + `rag-search-bar.tsx` + `rag-result-card.tsx` | **มีแล้ว** |
| Drawing Page (revision history, related RFAs, AI summary) | ✅ Drawing module + revision tracking + AI components (`document-comparison-view.tsx`) | **มีแล้ว** |
| AI Status Banner | ✅ `AiStatusBanner.tsx` | **มีแล้ว** |
---
### ⚡ 6. Tech Stack
| Blueprint แนะนำ | Repo จริง | หมายเหตุ |
|---|---|---|
| **PHP** (ของเดิมคุณ) | **NestJS 11 (TypeScript)** | คุณเปลี่ยน stack ไปแล้ว — repo เป็น TypeScript 87.5% |
| **Python** (AI service) | **TypeScript + n8n + Ollama** | ไม่มี Python service แยก — AI logic อยู่ใน NestJS module + n8n workflows |
| **LangChain** | **n8n + custom services** | Architectural decision ที่ pragmatic — n8n เสถียรกว่า LangChain สำหรับ production |
| **Qdrant** | ✅ `@qdrant/js-client-rest` | ตรงตาม blueprint |
| **Ollama** | ✅ Ollama บน QNAP NAS | ตรงตาม blueprint |
| **DataTables + Chat UI** | ✅ shadcn/ui + RagChatWidget | ตรงตาม blueprint |
---
### 🔥 7. Flow การทำงานจริง
**📥 Upload Drawing Flow:**
```
User upload → Multer + ClamAV scan → Two-Phase File Storage →
ai-ingest.service (extract text, OCR, embed) → Qdrant upsert → AI tag metadata
```
✅ มีครบตาม blueprint
**🔎 Query Flow:**
```
User: "drawing ล่าสุดของ contract นี้" →
RagChatWidget → AiRagService → Qdrant search → Ollama generate answer →
แสดงผลพร้อม citations
```
⚠️ มี RAG search แต่ยังขาด tool call ไปยัง drawing/contract modules
---
### 🧠 8. Insight สำคัญ — สิ่งที่ทำถูกแล้วและที่ต้องระวัง
| หลักการ | Repo | คะแนน |
|---|---|---|
| ❌ อย่าให้ AI query DB ตรงๆ | ✅ ADR-023: Ollama Isolation, No Direct DB Access | **ดีมาก** |
| ❌ อย่าให้ AI ตัดสินใจ workflow | ✅ Workflow Engine (ADR-021) แยกจาก AI | **ดีมาก** |
| ✅ AI → call function (tool) | ⚠️ ยังไม่มี tool function layer เชื่อม AI ↔ business modules | **ต้องพัฒนา** |
| ✅ System → validate logic | ✅ Validation services, RBAC guards, Business Logic Guards (37 edge cases) | **ดีมาก** |
---
### 🚀 9. Roadmap — สถานะเทียบ Blueprint
| Blueprint Phase | Repo Status |
|---|---|
| **Phase 1:** AI chat + query DB + basic tools | ✅ v1.9.0 — `RagChatWidget` + RAG search + AI Suggestion endpoints |
| **Phase 2:** RAG + embedding | ✅ v1.9.2 — Qdrant collection, embedding service, RAG pipeline |
| **Phase 3:** Automation, alert, classification | 🔄 กำลังพัฒนา — AI Suggest, reminder module, n8n workflows |
---
### 🎯 10. Ultimate Version — "AI = Document Controller"
| ความสามารถ | สถานะ |
|---|---|
| Auto check revision mismatch | ❌ ยังไม่มี |
| Missing approval detection | ❌ ยังไม่มี |
| Outdated drawing alert | ❌ ยังไม่มี |
---
## 💡 สรุปภาพรวม
**repo `lcbp3` v1.9.2 ของคุณทำไปแล้ว ~70-80% ของ blueprint AI DMS**
### ✅ สิ่งที่มีแล้ว (แข็งแรงมาก):
- **RAG System** — Qdrant + Ollama + BullMQ pipeline, project-isolated, production-ready
- **AI Chat UI** — `RagChatWidget` + search bar + result cards
- **Workflow Engine** — DSL-based, รองรับ RFA, Correspondence, Circulation
- **File Storage** — Two-phase + virus scanning + version control
- **AI Boundary** — Security isolation ที่ดี (Ollama ไม่แตะ DB โดยตรง)
- **Infrastructure** — Docker Compose hardened, Blue-Green Deploy, 0 vulnerabilities
- **Documentation** — 10/10 Gaps Closed, 23 ADRs, specs ครอบคลุมทุกมิติ
### ❌ สิ่งที่ยังขาด (ตาม blueprint):
1. **Tool Layer** — ยังไม่มี tool functions เชื่อม AI กับ business modules (`get_rfa`, `get_drawings_by_rfa`, `get_latest_revision` ฯลฯ) — **นี่คือ gap ใหญ่สุด**
2. **Agentic Decision-Making** — AI ยังตอบได้เฉพาะ RAG search ยังตัดสินใจเองไม่ได้ว่าจะเรียก tool ไหน
3. **Cross-Module Relationship Mapping** — ยังไม่มี RFA ↔ Drawing ↔ Revision linkage ใน AI context
4. **Auto Document Controller** — ยังไม่ถึงขั้น AI ตรวจสอบ revision mismatch, missing approval อัตโนมัติ
### 🎯 สิ่งที่ควรทำต่อ (Priority):
1. **สร้าง Tool Functions** ใน AI module ที่เป็น bridge ไปยัง RFA, Drawing, Transmittal modules — โดยคง security boundary ไว้
2. **เพิ่ม Intent Routing** — ให้ AI รู้ว่าคำถามแบบไหนควรเรียก tool ไหน (ต่อยอดจาก n8n workflow ที่มี)
3. **Cross-Module Context** — inject RFA-Drawing-Revision relationships เข้า RAG context
4. **Alert Engine** — ต่อยอด reminder module + AI classification เพื่อแจ้งเตือนอัจฉริยะ
+294
View File
@@ -0,0 +1,294 @@
# 🔍 เปรียบเทียบ: AI DMS Architecture Proposal vs. Repo จริง (lcbp3)
> จากแผน "อยากได้" → เทียบกับ "ที่มีอยู่จริง" แบบละเฮียกทีละข้อ 🎯
---
## 🏗️ 1. Architecture ภาพรวม
| ส่วนที่เสนอ | สถานะใน Repo | Gap / ข้อสังเกต |
|------------|-------------|---------------|
| **User → Web UI / Chat UI** | ✅ มี Next.js 16 + App Router | ✅ ตรงตามแผน แต่ยังไม่มี "Chat UI" แบบ Dedicated (มีแต่ Table + Form) |
| **AI Agent Layer** | 🟡 มี `.agents/` + `AGENTS.md` + n8n | ⚠️ เป็น "Agent Toolkit" สำหรับ Dev AI ช่วยเขียนโค้ด **ไม่ใช่** AI Agent ที่รันใน Production สำหรับ User |
| **RAG Engine** | ✅ มี ADR-022 (Superseded) → ADR-023 | ✅ มีสถาปัตยกรรม RAG แล้ว แต่ยังเป็น "แผน" ในเอกสาร ต้องดูการ Implement จริง |
| **Workflow Engine** | ✅ มี `workflow-engine/` module + ADR-001/021 | ✅ ตรงตามแผน 100% — DSL-based workflow + Integrated Context |
| **Tool Layer** | 🟡 มี Services ใน NestJS modules | ⚠️ Tools ยังเป็น "Internal API" ไม่ใช่ "Function Calling Interface" สำหรับ AI โดยตรง |
| **Database + Storage** | ✅ MariaDB 11.8 + Two-Phase File Storage | ✅ ตรงตามแผน + มี ClamAV + Redis Locking |
👉 **สรุป**: โครงสร้างพื้นฐาน (Infrastructure) พร้อมมาก แต่ "AI Agent Layer" ที่ User คุยด้วยยังต้องพัฒนาเพิ่ม
---
## 🔥 2. Core Components (ต้องมี)
### 2.1 AI Agent (สมอง)
| ที่เสนอ | ที่มีอยู่ | Gap |
|--------|----------|-----|
| LangChain / CrewAI | 🟡 ใช้ n8n + Ollama (ADR-023) | ⚠️ n8n เป็น Workflow Orchestrator **ไม่ใช่** Agent Framework แบบ LangChain |
| เข้าใจคำถาม → ตัดสินใจ → Call Tool | 🟡 มี "AI Boundary" (ADR-023) | ⚠️ ยังไม่มี "Intent Recognition Layer" ที่แยก Query → Tool Routing ชัดเจน |
💡 **แนะนำ**: ถ้าอยากได้ Agent แบบ "ตัดสินใจเอง" อาจต้องเพิ่ม `ai-agent/` module ที่ใช้ LangGraph หรือ Custom Agent Loop
---
### 2.2 RAG System (ค้นหาเอกสาร)
| ที่เสนอ | ที่มีอยู่ | Gap |
|--------|----------|-----|
| Vector DB: Qdrant / Chroma | 🟡 มี ADR-023A ระบุใช้ `Qdrant` | ✅ มีในสถาปัตยกรรม แต่ต้องตรวจสอบว่า Deploy แล้วหรือยัง |
| Search PDF / Drawing / Spec | 🟡 มี Elasticsearch 9.3.4 | ⚠️ Elasticsearch เป็น **Keyword Search** ไม่ใช่ **Vector Search** — ต้องแยกกัน |
| Use case: "Drawing A-101 revision ล่าสุด" | 🟡 มี `drawing/` module + revision tracking | ✅ Data Model พร้อม แต่ต้องเพิ่ม "Natural Language → Query Translation" |
💡 **จุดสำคัญ**: ต้องแยกให้ชัดระหว่าง:
- `Elasticsearch` → Full-text search (keyword)
- `Qdrant` → Vector search (semantic/RAG)
---
### 2.3 Tool Layer (สำคัญมาก) ⭐
| Tool ที่เสนอ | ที่มีอยู่ (NestJS Service) | Gap |
|-------------|--------------------------|-----|
| `get_rfa(id)` | ✅ `RfaService.findOne()` | ✅ พร้อม แต่ต้องสร้าง "AI Tool Wrapper" |
| `get_drawings_by_rfa(rfa_id)` | 🟡 มี Relation ใน Schema | ⚠️ ต้องเขียน Service Method แยกสำหรับ AI Call |
| `get_latest_revision(drawing_code)` | ✅ มี `DrawingService.getLatestRevision()` | ✅ พร้อม |
| `search_documents(query)` | ✅ มี `SearchModule` (Elasticsearch) | ⚠️ ต้องเพิ่ม "Vector Search" endpoint สำหรับ RAG |
| `get_transmittal_history()` | ✅ มี `TransmittalService` | ✅ พร้อม |
💡 **สิ่งที่ต้องทำเพิ่ม**: สร้าง `AiToolsService` ที่:
```typescript
// ตัวอย่าง: AI Tool Wrapper
@Injectable()
export class AiToolsService {
@Tool({ name: 'get_rfa', description: 'Get RFA by ID' })
async getRfa(id: string) {
return this.rfaService.findOne({ publicId: id });
}
// ...อื่นๆ
}
```
---
### 2.4 Workflow Engine (logic ธุรกิจ)
| ที่เสนอ | ที่มีอยู่ | Gap |
|--------|----------|-----|
| RFA status flow | ✅ มี `workflow-engine/` + DSL | ✅ ตรงตามแผน 100% |
| Approval logic | ✅ มี CASL Guards + State Machine | ✅ พร้อม |
| Revision control | ✅ มี `revisions` table + locking | ✅ พร้อม |
| "AI = ช่วยคิด, Workflow = ของจริง" | ✅ มี ADR-023 "AI Boundary" | ✅ ตรงตามหลักการ |
👉 **จุดแข็ง**: ส่วนนี้ทำไว้ดีมากแล้ว — AI ไม่สามารถ "มั่ว" Workflow ได้ เพราะต้องผ่าน DSL Engine
---
### 2.5 File Storage
| ที่เสนอ | ที่มีอยู่ | Gap |
|--------|----------|-----|
| เก็บไฟล์จริง (PDF, DWG) | ✅ Two-Phase Storage + ClamAV | ✅ ตรงตามแผน |
| Version control | ✅ มี `revisions` table + naming convention | ✅ พร้อม |
| Naming convention | ✅ มี `document-numbering/` module (ADR-002) | ✅ พร้อม |
---
## ⚙️ 3. Database Design
| Table ที่เสนอ | ที่มีอยู่ | Gap |
|--------------|----------|-----|
| `ai_embeddings` | 🟡 มีใน ADR-023A (Qdrant) | ⚠️ ต้องสร้าง Collection ใน Qdrant + Sync Job |
| `ai_logs` | 🟡 มี `monitoring/` module | ⚠️ ต้องเพิ่ม `ai_audit_logs` table สำหรับ tracking AI decisions |
| `ai_tasks` | 🟡 มี BullMQ (ADR-008) | ✅ สามารถใช้ `ai-batch` queue สำหรับ background jobs |
💡 **แนะนำ**: สร้าง migration script สำหรับ:
```sql
-- ตัวอย่าง: ai_audit_logs table
CREATE TABLE ai_audit_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
user_public_id BINARY(16),
query_text TEXT,
agent_decision JSON,
tools_used JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
---
## 🤖 4. AI Use Cases (ของจริงที่ควรทำ)
| Use Case | สถานะใน Repo | ความยาก |
|----------|-------------|---------|
| 🔎 Smart Search | 🟡 มี Elasticsearch + ADR-023 RAG | ⭐⭐ ต้องเพิ่ม NL→Query translator |
| 📄 Document QA | 🟡 มี OCR + Text extraction ในแผน | ⭐⭐⭐ ต้องเพิ่ม PDF parsing + chunking pipeline |
| 🔗 Relationship Mapping | ✅ มี Relations ใน Schema + Services | ⭐⭐ ต้องเพิ่ม "Graph Query" layer สำหรับ AI |
| 📊 Timeline Analysis | 🟡 มี `revisions.created_at` | ⭐⭐⭐ ต้องเพิ่ม "Temporal Query" logic |
| 🧠 Auto Classification | 🟡 มี ADR-017B (Superseded) | ⭐⭐⭐⭐ ต้องฝึก Model หรือใช้ Prompt Engineering |
| ⚠️ Alert / Assistant | 🟡 มี `notification/` module | ⭐⭐ ต้องเพิ่ม "Rule Engine" สำหรับ trigger alerts |
---
## 🧩 5. UI Design
| ส่วนที่เสนอ | ที่มีอยู่ | Gap |
|------------|----------|-----|
| Hybrid UI: Table + Chat | 🟡 มี DataTables + Forms | ⚠️ ยังไม่มี "Chat Panel" แบบ Side-by-side |
| Drawing Page: revision + RFAs + AI summary | ✅ มี `drawing/` module + relations | ⚠️ ต้องเพิ่ม "AI Summary" component ที่เรียก RAG |
💡 **Quick Win**: เพิ่ม Chat Panel แบบง่ายๆ ใน `app/(dashboard)/drawings/[id]/page.tsx`:
```tsx
// ตัวอย่าง: Chat Panel ใน Drawing Page
<aside className="w-80 border-l">
<AiChatPanel
context={{ type: 'drawing', id: drawing.publicId }}
tools={['get_related_rfas', 'get_revision_history']}
/>
</aside>
```
---
## ⚡ 6. Tech Stack
| ส่วนที่เสนอ | ที่มีอยู่ | ตรงกัน? |
|------------|----------|---------|
| Backend: PHP → Python | ❌ ใช้ NestJS (TypeScript) ทั้งระบบ | ⚠️ ไม่ตรง — แต่ TypeScript + NestJS ดีกว่า PHP สำหรับ AI Integration |
| AI Layer: LangChain / Ollama | 🟡 ใช้ Ollama + n8n (ADR-023) | ✅ ตรงบางส่วน — ขาด LangChain/LlamaIndex |
| Vector DB: Qdrant | ✅ มีใน ADR-023A | ✅ ตรง |
| Frontend: DataTables + Chat UI | 🟡 มี DataTables (shadcn/ui) | ⚠️ ขาด Chat UI component |
💡 **ข้อดีของ Stack ปัจจุบัน**:
- Full TypeScript → Type-safe AI Tool definitions
- NestJS Modules → Easy to add `ai-agent/` module
- BullMQ → Ready for async AI jobs
---
## 🔥 7. Flow การทำงานจริง
### 📥 Upload Drawing
| ขั้นตอนที่เสนอ | ที่มีอยู่ | Gap |
|--------------|----------|-----|
| 1. User upload PDF | ✅ มี Two-Phase Upload | ✅ พร้อม |
| 2. Extract text (OCR) | 🟡 มีแผนใน ADR-023A | ⚠️ ต้องเพิ่ม OCR service (Tesseract / PaddleOCR) |
| 3. Chunk + Embed → Vector DB | 🟡 มี Qdrant ในแผน | ⚠️ ต้องเขียน `DocumentIndexingService` |
| 4. AI tag metadata | 🟡 มี ADR-017B (Superseded) | ⚠️ ต้อง implement classification pipeline |
### 🔎 Query
| ขั้นตอนที่เสนอ | ที่มีอยู่ | Gap |
|--------------|----------|-----|
| 1. เข้าใจ intent | 🟡 ไม่มี NLU layer | ⭐⭐⭐ ต้องเพิ่ม Intent Classifier |
| 2. Call tool: `get_drawings()` | ✅ มี `DrawingService` | ✅ พร้อม (แต่ต้อง wrap เป็น AI Tool) |
| 3. Filter revision ล่าสุด | ✅ มี `getLatestRevision()` | ✅ พร้อม |
| 4. ตอบ | 🟡 มี API Response | ⚠️ ต้องเพิ่ม "Natural Language Generation" layer |
---
## 🧠 8. Insight สำคัญ (อย่าพลาด)
| ข้อควรระวัง | สถานะใน Repo | ผ่าน? |
|------------|-------------|-------|
| ❌ อย่าให้ AI query DB ตรงๆ | ✅ มี ADR-023 "AI Boundary" | ✅ ผ่าน |
| ❌ อย่าให้ AI ตัดสินใจ workflow | ✅ มี Workflow Engine แยก | ✅ ผ่าน |
| ✅ AI → call function (tool) | 🟡 มี Services แต่ไม่มี Tool Interface | ⚠️ ต้องเพิ่ม `@Tool()` decorator pattern |
| ✅ System → validate logic | ✅ มี CASL Guards + Validation | ✅ ผ่าน |
👉 **คุณทำถูกแล้ว**: การแยก "AI Intelligence" ออกจาก "Business Logic" เป็นหัวใจของระบบที่ปลอดภัย
---
## 🚀 9. Roadmap (ทำจริง)
### Phase 1 (24 สัปดาห์) — AI Chat + Query DB
| Task | สถานะ | ความยาก |
|------|--------|---------|
| สร้าง `AiAgentModule` ใน NestJS | ❌ ยังไม่มี | ⭐⭐ |
| สร้าง `AiToolsService` wrapper | ❌ ยังไม่มี | ⭐⭐ |
| เพิ่ม Chat UI component ใน Frontend | ❌ ยังไม่มี | ⭐⭐ |
| ทดสอบ: "RFA ล่าสุดคืออะไร" | ❌ ยังไม่มี | ⭐⭐ |
### Phase 2 — RAG + Embedding
| Task | สถานะ | ความยาก |
|------|--------|---------|
| Deploy Qdrant ใน Docker | 🟡 มีในแผน | ⭐⭐ |
| สร้าง `DocumentIndexingService` (OCR + Chunk + Embed) | ❌ ยังไม่มี | ⭐⭐⭐ |
| สร้าง `QdrantService` สำหรับ vector search | ❌ ยังไม่มี | ⭐⭐ |
| ทดสอบ: "สรุป spec นี้" | ❌ ยังไม่มี | ⭐⭐⭐ |
### Phase 3 — Automation + Alert
| Task | สถานะ | ความยาก |
|------|--------|---------|
| สร้าง `AiAlertService` สำหรับ rule-based alerts | ❌ ยังไม่มี | ⭐⭐⭐ |
| สร้าง `ClassificationPipeline` สำหรับ auto-tagging | ❌ ยังไม่มี | ⭐⭐⭐⭐ |
| Integrateกับ Notification Module | ✅ มี `notification/` module | ⭐⭐ |
---
## 🎯 10. ถ้าจะทำให้ "โหดจริง"
### Ultimate Version: AI = Document Controller
| ฟีเจอร์ | สิ่งที่ต้องเพิ่ม | ความยาก |
|--------|----------------|---------|
| 🔍 Auto check: revision mismatch | สร้าง `RevisionConsistencyChecker` | ⭐⭐⭐ |
| 🔍 Auto check: missing approval | สร้าง `ApprovalGapDetector` | ⭐⭐⭐ |
| 🔍 Auto check: outdated drawing | สร้าง `DrawingFreshnessMonitor` | ⭐⭐⭐ |
| 🤖 Proactive alert: "Drawing นี้ outdated" | Integrateกับ `notification/` module | ⭐⭐ |
---
## 💡 สรุปสั้นๆ (แบบเฮียก)
### ✅ สิ่งที่ "มีแล้ว" และ "ทำดีแล้ว"
1. **สถาปัตยกรรมพื้นฐาน** — NestJS + Next.js + MariaDB + Redis + Elasticsearch → Solid
2. **Workflow Engine** — DSL-based + ADR-001/021 → ตรงตามแผน 100%
3. **Security & RBAC** — CASL + 4-Level + JWT → พร้อมสำหรับ Enterprise
4. **File Storage** — Two-Phase + ClamAV + Versioning → ดีมาก
5. **AI Boundary** — ADR-023 กำหนดขอบเขตชัดเจน → ป้องกัน AI มั่ว
### ⚠️ สิ่งที่ "ต้องเพิ่ม" เพื่อให้เป็น AI DMS จริง
1. **AI Agent Runtime** — สร้าง `ai-agent/` module ที่รันใน Production (ไม่ใช่แค่ `.agents/` สำหรับ Dev)
2. **Tool Calling Interface** — Wrap Services เป็น `@Tool()` decorators สำหรับ AI Call
3. **Vector Search Pipeline** — Deploy Qdrant + สร้าง `DocumentIndexingService`
4. **Chat UI Component** — เพิ่ม Side Panel Chat ใน Drawing/RFA Pages
5. **Intent Recognition** — เพิ่ม NL→Query translator สำหรับ Smart Search
### 🎯 Quick Win (ทำใน 1 สัปดาห์)
```bash
# 1. สร้าง AI Agent Module
nest g module ai-agent
nest g service ai-agent/ai-tools
nest g controller ai-agent/chat
# 2. เพิ่ม Tool Wrapper
# src/ai-agent/ai-tools.service.ts
@Injectable()
export class AiToolsService {
@Tool({ name: 'get_rfa', description: 'Get RFA by publicId' })
async getRfa(@Arg('publicId') publicId: string) {
return this.rfaService.findOne({ where: { publicId } });
}
}
# 3. เพิ่ม Chat Endpoint
# src/ai-agent/chat.controller.ts
@Post('chat')
async chat(@Body() dto: ChatDto) {
// 1. Parse intent
// 2. Route to tools
// 3. Generate response
}
```
---
> 🎯 **Bottom Line**: Repo คุณ "พร้อม 80%" สำหรับ AI DMS — ขาดแค่ "AI Runtime Layer" ที่เชื่อม Agent → Tools → UI
> ถ้าเพิ่ม `ai-agent/` module + Tool Interface + Chat UI → จะได้ระบบที่ "โหดจริง" ตามที่วางแผนไว้ 🔥