Files
temetro/frontend/lib/ai-chat-history.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

45 lines
1.4 KiB
TypeScript

import type { TemetroUIMessage } from "@/lib/ai-chat";
import { apiFetch } from "@/lib/api-client";
// Persisted AI-chat threads (Claude-style history). Threads are per-user within
// the active clinic; the thread id is generated on the client.
export type ThreadSummary = { id: string; title: string; updatedAt: string };
type StoredMessage = { role: string; parts: unknown };
export function listThreads(): Promise<ThreadSummary[]> {
return apiFetch<ThreadSummary[]>("/api/chat/threads");
}
export function getThread(
id: string,
): Promise<{ id: string; title: string; messages: StoredMessage[] }> {
return apiFetch(`/api/chat/threads/${id}`);
}
export function saveThread(
id: string,
messages: TemetroUIMessage[],
title: string,
): Promise<{ ok: boolean }> {
return apiFetch(`/api/chat/threads/${id}`, {
method: "PUT",
body: JSON.stringify({
messages: messages.map((m) => ({ role: m.role, parts: m.parts })),
title,
}),
});
}
export function deleteThread(id: string): Promise<void> {
return apiFetch<void>(`/api/chat/threads/${id}`, { method: "DELETE" });
}
// Fired after a thread is saved/deleted so the sidebar history can refresh.
export const THREADS_CHANGED_EVENT = "temetro:threads-changed";
export function notifyThreadsChanged() {
if (typeof window !== "undefined") {
window.dispatchEvent(new CustomEvent(THREADS_CHANGED_EVENT));
}
}