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
@@ -3,6 +3,7 @@
import { type ReactNode, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { EarningsChart } from "@/components/analysis/earnings-chart";
import { LiveHospitalChart } from "@/components/analysis/live-hospital-chart";
import { Area, AreaChart } from "@/components/charts/area-chart";
import { Bar } from "@/components/charts/bar";
@@ -13,6 +14,7 @@ import { ChartTooltip } from "@/components/charts/tooltip";
import { XAxis } from "@/components/charts/x-axis";
import { Card } from "@/components/ui/card";
import { type Analytics, getAnalytics } from "@/lib/analytics";
import { formatMoney } from "@/lib/invoices";
// Clinic analytics computed on the server from real data (patients,
// appointments, prescriptions, tasks). No fabricated financials — temetro has no
@@ -216,6 +218,37 @@ export function AnalysisView() {
</div>
</section>
<section className="flex flex-col gap-3">
<div>
<h2 className="font-semibold text-lg tracking-tight">
{t("analysis.earnings.title")}
</h2>
<p className="text-muted-foreground text-sm">
{t("analysis.earnings.description")}
</p>
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
<StatCard
label={t("analysis.earnings.billed")}
value={formatMoney(data?.earnings.totalBilled ?? 0)}
/>
<StatCard
label={t("analysis.earnings.paid")}
value={formatMoney(data?.earnings.totalPaid ?? 0)}
/>
<StatCard
label={t("analysis.earnings.outstanding")}
value={formatMoney(data?.earnings.totalOutstanding ?? 0)}
/>
</div>
<Card className="gap-3 p-4">
<span className="text-muted-foreground text-sm">
{t("analysis.earnings.byMonth")}
</span>
<EarningsChart data={data?.earnings.byMonth ?? []} />
</Card>
</section>
<Section
columns={4}
description={t("analysis.appointments.description")}