🧩 Module Spec · M5 PHPA · Pharmacy PA · DETAIL · 26 April 2026
← Modules M5 · PHPA DETAIL Sprint 2.1 · 5-18 Jul 2026

M5 — Pharmacy Personal Assistant

Penjaga keselamatan ubat: DDI · allergy cross-ref · dose-by-weight (paeds, eGFR, hepatic) · halal pharma filter · generic substitution · barcode dispense · adverse reaction trigger ke M6.

1. 📌 Overview & Purpose

Goal: Setiap preskripsi sampai kaunter farmasi di-validate AI dlm <3s — DDI, allergy, dose paeds/renal/hepatic, halal status, NPRA stock. Farmasis verify visual + barcode → dispense. Closed-loop verify untuk safety zero compromise.

Per Doc Zam mock (slide 7): Prescription Queue (8 prescriptions visible) · Verification panel · Print Label · Counsel checklist.

HITL gate: AI propose check, pharmacist final approve. Tiada auto-dispense.

2. 👤 User Stories

US-5.1 (Farmasis · Routine RX)

RX masuk dari M4 · saya nak AI dah pre-check DDI/allergy/dose · saya scan barcode dispense + counsel checklist · dah!

US-5.2 (Farmasis · DDI alert)

Pesakit pakai amlodipine, doctor preskripsi clarithromycin (MAJOR DDI · QT prolong) · saya nak AI alert merah, suggest azithromycin alt, hold dispense pending doctor confirm.

US-5.3 (Farmasis · Paeds dose)

Bayi 8kg dpt ubat paracetamol — saya nak AI auto-calc dose (10-15mg/kg) dan flag kalau preskripsi over-dose.

US-5.4 (Pesakit · Halal)

Saya muslim · saya nak AI flag kalau ada gelatin animal-source dlm capsule, suggest halal alternative.

US-5.5 (Klinik · Generic substitution)

RX brand mahal · panel insurance hanya cover generic · AI suggest generic equivalent dgn cost saving · doctor approve once-off.

US-5.6 (Farmasis · Adverse loop)

Pesakit ngadu side effect 2 hari kemudian (M6) · saya terima alert · auto-block re-dispense same drug class for that patient.

3. ✅ Functional Requirements

MUSTFR-5.1: Receive RX from M4 sign-off event · auto-queue dlm Pharmacy panel
MUSTFR-5.2: DDI check (NPRA + Lexicomp data) · severity classification (MINOR/MOD/MAJOR/CONTRAINDICATED) · top-3 alternatives suggested
MUSTFR-5.3: Allergy cross-ref dgn patient registry (M4 + M6 input) · cross-class reaction warn (e.g. penicillin allergy → also avoid cephalosporin first-gen)
MUSTFR-5.4: Dose calculator: paeds by-weight (mg/kg) · renal eGFR-adjusted (Cockcroft-Gault) · hepatic Child-Pugh adjusted · alert >2× standard or <50% standard
MUSTFR-5.5: Halal status filter · NPRA halal index integration · capsule shell · excipients · gelatin source · alcohol
MUSTFR-5.6: Generic substitution suggest · same active ingredient · cost compare · panel coverage match · doctor confirm needed
MUSTFR-5.7: NPRA stock query · alternative if OOS · re-order trigger
MUSTFR-5.8: Barcode scan at dispense (USB scanner integration) · verify drug match RX · prevent wrong-drug error
MUSTFR-5.9: Label printer integration (Zebra ZPL · BPL) · BM/EN dual-language · dose schedule · warnings · prescriber info
MUSTFR-5.10: Counsel checklist (key points dose · timing · with food · red flags) untuk pharmacist konfirm
MUSTFR-5.11: Dispense audit log (who · when · what · barcode hash · counsel completed)
MUSTFR-5.12: Adverse reaction intake hook (M6 trigger) · auto-block re-dispense same drug class
MUSTFR-5.13: HITL pharmacist final approve · auto-block dispense kalau pharmacist tak approve
SHOULDFR-5.14: Inventory tracking · low-stock alert · auto-reorder threshold
SHOULDFR-5.15: Multi-supplier price comparison
SHOULDFR-5.16: Patient pickup notification (WAHA SMS "ubat sedia")

4. ⚙️ Non-Functional Requirements

AspectTargetNotes
DDI check latency<3s p99Cached lookup · NPRA + Lexicomp local
Allergy cross-ref<500msIn-memory patient registry
Dose calc<100msLocal algorithm · no LLM needed
Barcode scan to verify<1sLocal USB · no network
Label print<3sZebra direct · USB / network printer
NPRA DB freshness≤24h delayDaily cron sync
Concurrent RX queue≥20 simultaneousPilot scale
HITL approve round-trip<1s UIPharmacist 1-tap
Token cost / RX~1K tokensMostly rule-based · LLM only for explanation

5. 🗄️ Data Model

TableKey fieldsPurpose
prescriptionsid, encounter_id, doctor_id, patient_id, status (queued · verifying · approved · dispensed · cancelled), justification, items_countRX header
prescription_itemsrx_id, line_no, drug_code (NPRA), drug_name_brand, drug_name_generic, dose_mg, frequency, route, duration_days, halal_status, ai_suggested_substituteRX lines
ddi_alertsrx_id, drug_a, drug_b, severity (MINOR/MOD/MAJOR/CONTRA), description, citation, suggested_alt, pharmacist_actionFR-5.2 audit
allergy_alertsrx_id, drug_class, patient_allergy, severity, cross_class_reactivity, pharmacist_actionFR-5.3 audit
dispense_logrx_id, item_id, dispensed_by_pharmacist, barcode_hash, dispensed_at, counsel_completed, label_urlFR-5.11 closed-loop
drugs_npranpra_code, generic_name, brand_name, halal_status, halal_cert_no, schedule_class (controlled/Rx/OTC), gelatin_source, excipients_jsonNPRA mirror
drug_interactionsdrug_a_code, drug_b_code, severity, description, source (NPRA/Lexicomp/MOH)DDI database
inventorytenant_id, npra_code, qty_on_hand, reorder_threshold, last_received, expiry_dateFR-5.7

6. 🔌 API + MCP

GET    /api/v1/pharmacy/queue                   # ready/verifying/pending
POST   /api/v1/pharmacy/{rx_id}/verify          # AI run all checks
       Returns: { ddi_alerts, allergy_alerts, dose_warnings, halal_flags, generic_suggestions }

POST   /api/v1/pharmacy/{rx_id}/approve         # pharmacist HITL approve
       Body: { line_decisions: [{ line_no, action: "dispense"|"substitute"|"hold"|"cancel", reason }] }

POST   /api/v1/pharmacy/{rx_id}/dispense        # post barcode scan
       Body: { line_no, barcode, quantity }
       Returns: { dispense_id, label_pdf_url }

POST   /api/v1/pharmacy/{rx_id}/print-label     # generate label PDF
POST   /api/v1/pharmacy/{rx_id}/counsel-done    # close encounter

GET    /api/v1/pharmacy/inventory/check/{drug_code}
POST   /api/v1/pharmacy/inventory/reorder

# MCP Tools
ddi_check              Drug-drug interaction · severity + alternatives
allergy_lookup         Patient allergy registry cross-ref
dose_calc              Paeds/eGFR/hepatic adjusted
halal_filter           NPRA halal index check
generic_suggest        Substitution + cost compare
npra_stock             Drug DB lookup
barcode_verify         Match scanned barcode vs RX

7. 🔁 State Machine

┌──────────────────┐
│ RX_RECEIVED      │ from M4 sign-off
└────────┬─────────┘
         ▼
┌──────────────────┐
│ AI_VERIFYING     │ DDI · allergy · dose · halal · stock parallel
└────────┬─────────┘
         ▼
┌──────────────────┐         ┌──────────────────┐
│ ALERTS_RAISED    │────────►│ HITL_REVIEW      │ pharmacist decide
└────────┬─────────┘         └────────┬─────────┘
         │ no alerts                  │
         ▼                            ▼
┌──────────────────┐         ┌──────────────────┐
│ READY_TO_DISPENSE│         │ APPROVED/HOLD/   │
└────────┬─────────┘         │ SUB/CANCEL       │
         │                   └────────┬─────────┘
         ▼                            │ approved
┌──────────────────┐                  │
│ BARCODE_SCAN     │◄─────────────────┘
└────────┬─────────┘
         ▼ match
┌──────────────────┐
│ DISPENSED        │ label print · counsel · log
└────────┬─────────┘
         ▼
┌──────────────────┐
│ COUNSEL_COMPLETE │
└────────┬─────────┘
         ▼
┌──────────────────┐
│ CLOSED           │ → emit event for M6 monitoring
└──────────────────┘

8. 🤖 Agent Specification

M5 mostly rule-based + structured DB lookup. LLM hanya untuk: (a) generate counsel checklist explanation BM/EN, (b) explain DDI severity to patient (kalau pesakit tanya).

  • Model: Llama 8B (rule-based heavy + light explanation)
  • Memory: NPRA drug DB · DDI matrix (in-memory) · patient allergy registry
  • Guardrails: HITL pharmacist final approve · dispense block on CONTRAINDICATED · audit per click

9. 🎨 UI/UX

Per Doc Zam mock slide 7: Prescription Queue + Verification panel.

  • Filament 3 Pharmacy panel · 2-column layout
  • Left: queue list (Ready · Verifying · Pending · Completed)
  • Right: verification detail card · per-line item · alerts color-coded
  • DDI alert: red banner · severity · alternatives card
  • Halal flag: green/red icon · capsule source disclosure
  • Approve/Substitute/Hold/Cancel buttons per-line
  • Barcode scan input · confirmation chime
  • Print Label button · counsel checklist modal
  • Bottom stats: Total Today · Average Time · Pending Approvals

10. ✔️ Acceptance Criteria

  • AC-5.1: 50 RX clinical safety scenarios → 100% MAJOR/CONTRA DDI detected
  • AC-5.2: 30 paeds dose scenarios → 100% under/over-dose flagged
  • AC-5.3: Halal flag accuracy ≥95% pada 100 sample drugs
  • AC-5.4: Barcode mismatch detection 100% (10 swap test)
  • AC-5.5: Generic substitution cost saving ≥20% on average
  • AC-5.6: HITL pharmacist gate enforced — 0 auto-dispense on CONTRA
  • AC-5.7: Label print BM/EN dual format · scannable barcode included
  • AC-5.8: Adverse loop M6 trigger working — block re-dispense within 5 min
  • AC-5.9: Counsel checklist completion required before close encounter
  • AC-5.10: Doc Zam clinical pharmacist review 20 sample RX → ≥18 acceptable

11. 🧪 Test Plan

TierCasesCoverage
UnitDDI matrix lookup · dose calc (paeds/renal/hepatic) · halal filter · barcode hash≥90%
IntegrationRX intake from M4 · all 5 verification checks parallel · HITL gate · M6 adverse trigger100% paths
Clinical safety50 DDI · 30 dose paeds · 20 allergy cross-class · 100 halal · 10 barcode swap0 critical miss
Hardware integrationUSB barcode scanner · Zebra label printer · network printer fallback3 scanner brands tested
Load20 concurrent RX queue · 5 simultaneous label printsp99 <3s
UAT2 weeks pharmacist usage · NPS · time-to-dispenseNPS ≥7 · time savings ≥20%

12. 🔗 Dependencies

  • Hard: M9 (audit + HITL gate) · M4 DRPA (RX source · sign-off event)
  • Soft: M6 ARXL (adverse loop) · M7 ADPA (billing trigger) · M1 PSPA (allergy intake)
  • External: NPRA drug DB · NPRA halal index · Lexicomp DDI (or MIMS Malaysia) · Zebra/BPL label printer SDK · USB barcode scanner driver

13. 🏃 Sprint Allocation

Sprint 2.1 · 5-18 Jul 2026 (1.5 minggu shared with M2)
  • Day 1-2: NPRA DB ingestion · drug + DDI matrix · halal index seed
  • Day 3-4: 5 verification check engines (DDI · allergy · dose · halal · stock)
  • Day 5-6: Filament Pharmacy panel · queue UI · verification card
  • Day 7-8: HITL gate · barcode scan integration · label printer
  • Day 9-10: M6 adverse trigger hook · counsel checklist
  • Day 11: Doc Zam pharmacist review (20 RX scenarios) · iteration
  • Day 12: E2E test · sprint review
Capacity: 2 backend (incl integration heavy) · 1 frontend (Filament) · pharmacist domain SME consult Day 1 + Day 11

14. ⚠️ Module-Specific Risks

RiskLikelihoodImpactMitigation
NPRA DDI database outdatedMed🟠 Wrong alertDaily NPRA sync · Lexicomp backup · alert if >24h stale · pharmacist override allowed
Halal filter false positive (block essential drug)Med🟢 Patient frustrationPatient explicit halal-only opt-in · default warn-not-block · suggest alternative
DDI false positive (block essential RX)Med🟠 Care delaySeverity tier · pharmacist override audited · clinical justification required
Barcode scan spoof / wrong drug missedLow🔴 Patient harmVerify code + name + visual check · counsel checklist requires drug name confirm
Generic substitution cost saved but bioequivalence concernLow🟠 Therapeutic concernNPRA-bioequivalent only · pharmacist flag if narrow therapeutic index drug
Label printer hardware failMed🟢 Workflow stopNetwork printer fallback · PDF generate option · pharmacist hand-write emergency
Inventory desync with physical stockMed🟢 Stock-out surpriseDaily reconciliation · barcode dispense decrements · monthly physical count