Skip to content

Kiến trúc Hệ thống

Tham chiếu Nhanh: frappe-dev-master xây dựng trên 2 trụ cột: Kiến trúc App 7-Layer (tách logic khỏi ORM) và Hệ thống Điều phối AI Agent (phân chia công việc rành mạch theo vòng đời).

1. Kiến trúc 7 Lớp (7-Layer Architecture)

Mô hình phân lớp nghiêm ngặt đảm bảo code testable, maintainable và scalable.

LớpTênVai tròVí dụ
1DocType ControllersSchema JSON + lifecycle hooks (validate, on_submit)my_app/my_module/doctype/my_dt/my_dt.py
2EnginesPure Python logic — side-effect-free, testable 100%my_app/engines/scoring_engine.py
3APIs@frappe.whitelist endpoints, idempotent upsertmy_app/api/permissions.py
4TasksScheduler events (daily/weekly) wrapper quanh Engineshooks.pyscheduler_events
5Setup HooksIdempotent install/migrate hooksafter_install, after_migrate
6TestsUnit tests pure logic, không cần Frappe Servertests/test_scoring_engine.py
7Client JSShared utility namespaces, form/list scriptsmy_app/public/js/my_app.js

Luồng Dữ liệu (Data Flow)

mermaid
flowchart TD
    Client["Client UI / Browser"] --> API["Lớp 3: API Endpoint"]
    Webhook["External Webhook"] --> API
    Task["Lớp 4: Scheduler Tasks"] --> Engine["Lớp 2: Business Logic Engine"]
    API --> Engine
    Engine --> ORM["Lớp 1: DocType Controllers"]
    ORM --> DB[("MariaDB")]

Text fallback: Request từ Client/Webhook → API Endpoint → Engine (Pure Python) → DocType Controller → Database. Scheduler Tasks cũng gọi trực tiếp Engine.

Quy tắc Vàng

  1. KHÔNG đặt business logic nặng trong DocType controller
  2. LUÔN tách thuật toán phức tạp ra engines/ để test độc lập
  3. LUÔN dùng @frappe.whitelist cho API, KHÔNG trả về raw HTML
  4. LUÔN dùng frappe.log_error(), KHÔNG dùng frappe.logger

2. Hệ thống AI Agent

mermaid
flowchart LR
    User["👤 Developer"] --> Orchestrator["🧠 SKILL.md\nĐiều phối"]
    Orchestrator --> Build["🔨 Build Agents"]
    Orchestrator --> Operate["⚙️ Lifecycle Agents"]
    Build --> DA["DocType Architect"]
    Build --> BE["Frappe Backend"]
    Build --> FE["Frappe Frontend"]
    Build --> CFE["Custom Frontend"]
    Build --> ERP["ERPNext Customizer"]
    Operate --> INS["Installer"]
    Operate --> PLN["Planner"]
    Operate --> DBG["Debugger"]
    Operate --> FIX["Fixer"]
    Operate --> PERF["Performance"]
    Operate --> RMT["Remote Ops"]
    Operate --> GH["GitHub Workflow"]

Text fallback: SKILL.md là Orchestrator trung tâm. Nó điều phối 2 nhóm: Build Agents (5 agents cho phát triển) và Lifecycle Agents (7 agents cho vận hành).

Nhóm Build Agents (Phát triển)

AgentFileChức năng
DocType Architectagents/doctype-architect.mdThiết kế schema, relations, workflow states
Frappe Backendagents/frappe-backend.mdPython APIs, controllers, background jobs
Frappe Frontendagents/frappe-frontend.mdClient scripts, dialogs, custom formatters
Custom Frontendagents/frappe-custom-frontend.mdStandalone frontend pages
ERPNext Customizeragents/erpnext-customizer.mdMở rộng ERPNext core an toàn

Nhóm Lifecycle Agents (Vận hành)

AgentFileChức năng
Installeragents/frappe-installer.mdCài đặt bench, site, production
Planneragents/frappe-planner.mdLập kế hoạch feature, ADR
Debuggeragents/frappe-debugger.mdPhân tích lỗi, log investigation
Fixeragents/frappe-fixer.mdSửa lỗi có cấu trúc 6-bước
Performanceagents/frappe-performance.mdQuery optimization, profiling, caching
Remote Opsagents/frappe-remote-ops.mdREST API operations cho remote sites
GitHub Workflowagents/github-workflow.mdGit operations, CI/CD

3. Quyết Định Kiến Trúc (ADR)

Quyết địnhLý do
Tách Engine ra khỏi ControllerDocType controller đòi HTTP Context + DB. Engine thuần Python → test nhanh, CI tự động
Không dùng Raw SQL mặc địnhBypass cơ chế Role Permission → rủi ro SQL Injection. Ưu tiên frappe.get_doc, frappe.db.get_list
Luôn dùng frappe.log_error()frappe.logger không ghi vào Error Log DocType → mất trace
Idempotent Upsert cho APITránh duplicate entries khi retry, đảm bảo safe CI/CD

4. Cấu trúc Thư mục

frappe-dev-master/
├── SKILL.md                  # Bộ não điều phối trung tâm
├── agents/                   # 12 AI Agents chuyên biệt
│   ├── doctype-architect.md
│   ├── frappe-backend.md
│   ├── frappe-frontend.md
│   ├── frappe-installer.md
│   ├── frappe-fixer.md
│   ├── frappe-debugger.md
│   ├── frappe-performance.md
│   ├── frappe-remote-ops.md
│   └── ...
├── commands/                 # 15 CLI Commands
│   ├── frappe-app.md
│   ├── frappe-backend.md
│   ├── frappe-bench.md
│   ├── frappe-install.md
│   ├── frappe-fix.md
│   ├── frappe-remote.md
│   └── ...
├── resources/                # 8+ Tài liệu tham khảo
│   ├── 7-layer-architecture.md
│   ├── bench_commands.md
│   ├── common_pitfalls.md
│   ├── rest-api-patterns.md
│   ├── installation-guide.md
│   └── ...
├── skills/                   # 7 Sub-Skills
│   ├── doctype-patterns/
│   ├── server-scripts/
│   ├── client-scripts/
│   ├── frappe-api/
│   ├── bench-commands/
│   ├── remote-operations/
│   └── web-forms/
└── docs/                     # Tài liệu này

← Trang Chủ · Danh mục Agents →