Add patient personalization: profile saved once, reused every chat turn

This commit is contained in:
2026-08-25 11:55:13 +07:00
parent 501c226ecd
commit 1ddd878431
12 changed files with 548 additions and 6 deletions
+26 -1
View File
@@ -1,4 +1,9 @@
import type { AuthUser, LoginRequest } from "@duoc-thu/shared-types";
import type {
AuthUser,
LoginRequest,
PatientProfile,
PatientProfileUpdate,
} from "@duoc-thu/shared-types";
export class AuthError extends Error {
constructor(readonly status: number) {
@@ -34,3 +39,23 @@ export async function me(): Promise<AuthUser | null> {
if (!response.ok) throw new AuthError(response.status);
return (await response.json()) as AuthUser;
}
/** Returns `null` on 401, same "not logged in is not an error" rule as `me`. */
export async function getPatientProfile(): Promise<PatientProfile | null> {
const response = await fetch("/api/patient-profile");
if (response.status === 401) return null;
if (!response.ok) throw new AuthError(response.status);
return (await response.json()) as PatientProfile;
}
export async function savePatientProfile(
update: PatientProfileUpdate
): Promise<PatientProfile> {
const response = await fetch("/api/patient-profile", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(update),
});
if (!response.ok) throw new AuthError(response.status);
return (await response.json()) as PatientProfile;
}
+14
View File
@@ -23,3 +23,17 @@ export interface RegisterRequest {
username: string;
password: string;
}
/** Per-user clinical context, set once from the account profile page and
* reused across chat turns so a logged-in doctor isn't re-asked age/weight
* on every dosing question. All fields optional/nullable — an anonymous
* user or a logged-in user with no saved profile is entirely unaffected. */
export interface PatientProfile {
ageText: string | null;
weightKg: number | null;
renalFunction: string | null;
hepaticFunction: string | null;
knownAllergies: string | null;
}
export type PatientProfileUpdate = Partial<PatientProfile>;