Files
temetro/frontend/lib/ai-chat.ts
T
Khalid Abdi 3aa699aefe 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>
2026-06-14 22:25:15 +03:00

85 lines
2.5 KiB
TypeScript

import type { UIMessage } from "ai";
import type { Analytics } from "@/lib/analytics";
import type { Appointment } from "@/lib/appointments";
import type { InventoryItem } from "@/lib/inventory";
import type { Lab, Patient, Trend } from "@/lib/patients";
import type { Prescription } from "@/lib/prescriptions";
import type { Task } from "@/lib/tasks";
// Custom data parts the backend agent streams alongside its text. They carry
// REAL (un-redacted) record data straight to the clinician's screen for rich
// rendering — the model itself only ever sees Veil-redacted tool results.
export type LabCardData = {
fileNumber: string;
name: string;
labs: Lab[];
labTrend: Trend;
};
export type ImportPreviewData = {
// Validated, ready-to-commit records (server re-validates on commit).
valid: unknown[];
invalid: { index: number; errors: string[] }[];
total: number;
};
export type VeilNoticeData = {
provider: string;
level: string;
};
// A Chain-of-Thought step the agent emits as it works (one per tool action).
export type StepData = {
id: string;
label: string;
status: "active" | "complete";
};
// Read-only list cards (display actions).
export type AppointmentListData = { appointments: Appointment[] };
export type TaskListData = { tasks: Task[] };
export type PrescriptionListData = { prescriptions: Prescription[] };
export type InventoryListData = { items: InventoryItem[] };
// Clinic-wide read cards.
export type ClinicCardData = {
name: string;
slug: string | null;
createdAt: string | null;
};
// An add proposed by the agent, awaiting one-click clinician approval. `record`
// is the validated, ready-to-commit input for the matching create endpoint.
export type ActionPreviewKind =
| "appointment"
| "task"
| "prescription"
| "invoice";
export type ActionPreviewData = {
token: string;
kind: ActionPreviewKind;
record: unknown;
issues?: string[];
};
// Maps each data part name → its payload. Part `type` strings are the key
// prefixed with `data-` (e.g. `data-patientCard`), per the AI SDK convention.
export type TemetroDataParts = {
patientCard: Patient;
labCard: LabCardData;
importPreview: ImportPreviewData;
veilNotice: VeilNoticeData;
step: StepData;
appointmentList: AppointmentListData;
taskList: TaskListData;
prescriptionList: PrescriptionListData;
inventoryList: InventoryListData;
clinicCard: ClinicCardData;
analyticsCard: Analytics;
actionPreview: ActionPreviewData;
};
export type TemetroUIMessage = UIMessage<never, TemetroDataParts>;