feat: clinic-wide AI (analytics/earnings/inventory + invoice-from-file),

real Live card, persistent chat history

Analytics & earnings:
- Analytics now carries real money computed from invoices (billed/paid/
  outstanding + by-month); new Earnings section on the Analysis page drawn with
  the project's Bklit chart components (shared EarningsChart).

AI agent reaches the whole clinic:
- new read tools getClinicInfo / getAnalytics / listInventory render clinic,
  analytics (with a Bklit earnings chart) and inventory cards in chat
- proposeInvoice turns an uploaded purchase/medication list into an invoice
  (new "invoice" action-preview kind → createInvoice); invoices/appointments
  auto-create/link a patient (ensurePatient) so they hit the Patients page

Live card:
- plots real data — patients checked in today — via GET /api/analytics/live
  (polled); value pill clamped and margins widened so nothing spills the card

Persistent AI chat history (Claude-style):
- ai_chat_threads + ai_chat_messages (migration 0016); per-user, org-scoped
  thread CRUD under /api/chat/threads
- chat panel owns a thread id, loads /?thread=<id>, and auto-saves after each
  exchange; sidebar lists past chats (open/delete), "New chat" starts fresh

Verified with backend typecheck + frontend tsc + next build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Khalid Abdi
2026-06-14 22:25:15 +03:00
parent b946dc9226
commit 3aa699aefe
27 changed files with 4274 additions and 61 deletions
+11
View File
@@ -1,6 +1,9 @@
// A single bar/point in a time-series chart (e.g. one month or one weekday).
export type TrendPoint = { label: string; count: number };
// One month of billing, in currency units (computed from invoices).
export type EarningsPoint = { label: string; billed: number; paid: number };
// Server-computed clinic analytics returned by GET /api/analytics. All figures
// are aggregates over the active clinic's real data (no fabricated financials).
export type Analytics = {
@@ -23,6 +26,14 @@ export type Analytics = {
open: number;
done: number;
};
// Real money, computed from invoices: billed = sum of line items; paid =
// invoices marked paid; outstanding = draft + sent. `void` is excluded.
earnings: {
totalBilled: number;
totalPaid: number;
totalOutstanding: number;
byMonth: EarningsPoint[];
};
// Time-series for charts: new patients over the last 6 months, and
// appointments per day across the current week.
trends: {