Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
362 lines (325 loc) · 11.6 KB
/
Copy pathserver.ts
File metadata and controls
362 lines (325 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* Full-stack Express server with Vite dev middleware
* Provides backend Gemini API integration via @google/genai SDK
*/
import express from 'express';
import type { Request, Response } from 'express';
import { createServer as createViteServer } from 'vite';
import { GoogleGenAI, Type } from '@google/genai';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = Number(process.env.PORT) || 3000;
app.use(express.json({ limit: '25mb' }));
// Shared server-side Gemini client
const apiKey = process.env.GEMINI_API_KEY;
let ai: GoogleGenAI | null = null;
if (apiKey && apiKey !== 'MY_GEMINI_API_KEY') {
ai = new GoogleGenAI({
apiKey,
httpOptions: {
headers: {
'User-Agent': 'aistudio-build',
},
},
});
}
// Health check endpoint
app.get('/api/health', (_req: Request, res: Response) => {
res.json({
status: 'ok',
hasApiKey: Boolean(apiKey && apiKey !== 'MY_GEMINI_API_KEY'),
demoDate: '2026-10-04',
});
});
// JSON extraction schema for note reading
const noteExtractionSchema = {
type: Type.OBJECT,
properties: {
visit_date: {
type: Type.OBJECT,
properties: {
value: { type: Type.STRING, description: 'YYYY-MM-DD or null if unclear' },
confidence: { type: Type.STRING, description: 'low, medium, or high' },
},
required: ['confidence'],
},
blood_pressure: {
type: Type.OBJECT,
properties: {
value: {
type: Type.OBJECT,
properties: {
systolic: { type: Type.NUMBER },
diastolic: { type: Type.NUMBER },
},
},
confidence: { type: Type.STRING, description: 'low, medium, or high' },
},
required: ['confidence'],
},
blood_sugar: {
type: Type.OBJECT,
properties: {
value: {
type: Type.OBJECT,
properties: {
value: { type: Type.NUMBER },
unit: { type: Type.STRING },
type: { type: Type.STRING, description: 'fasting, random, post-meal, or unknown' },
},
},
confidence: { type: Type.STRING, description: 'low, medium, or high' },
},
required: ['confidence'],
},
weight_kg: {
type: Type.OBJECT,
properties: {
value: { type: Type.NUMBER },
confidence: { type: Type.STRING, description: 'low, medium, or high' },
},
required: ['confidence'],
},
medicines: {
type: Type.OBJECT,
properties: {
value: {
type: Type.ARRAY,
items: {
type: Type.OBJECT,
properties: {
name: { type: Type.STRING },
dose: { type: Type.STRING },
frequency: { type: Type.STRING },
change: { type: Type.STRING, description: 'started, increased, continued, stopped, or unknown' },
},
required: ['name'],
},
},
confidence: { type: Type.STRING, description: 'low, medium, or high' },
},
required: ['confidence'],
},
tests_ordered: {
type: Type.OBJECT,
properties: {
value: {
type: Type.ARRAY,
items: {
type: Type.OBJECT,
properties: {
name: { type: Type.STRING },
when_text: { type: Type.STRING },
due_in_days: { type: Type.NUMBER },
},
required: ['name'],
},
},
confidence: { type: Type.STRING, description: 'low, medium, or high' },
},
required: ['confidence'],
},
planned_followup: {
type: Type.OBJECT,
properties: {
value: {
type: Type.OBJECT,
properties: {
what: { type: Type.STRING },
when_text: { type: Type.STRING },
due_in_days: { type: Type.NUMBER },
absolute_date: { type: Type.STRING },
},
},
confidence: { type: Type.STRING, description: 'low, medium, or high' },
},
required: ['confidence'],
},
referral: {
type: Type.OBJECT,
properties: {
value: {
type: Type.OBJECT,
properties: {
to: { type: Type.STRING },
reason: { type: Type.STRING },
when_text: { type: Type.STRING },
},
},
confidence: { type: Type.STRING, description: 'low, medium, or high' },
},
required: ['confidence'],
},
},
};
/**
* 5a. extractNote: Multimodal note digitization with confidence scores
*/
app.post('/api/gemini/extract-note', async (req: Request, res: Response) => {
try {
const { imageBase64, mimeType = 'image/png', noteText, visitDateHint } = req.body;
if (!imageBase64 && !noteText) {
return res.status(400).json({ error: 'Please provide an image or note text in request body' });
}
if (!ai) {
return res.json({
success: false,
error: 'Gemini API is not configured on the server. Falling back to offline manual review.',
isFallbackAvailable: true,
});
}
let contentsPayload: any;
if (imageBase64) {
// Clean base64 string
const cleanBase64 = imageBase64.replace(/^data:image\/[a-z]+;base64,/, '');
const promptText = `You are reading a photo of a rural clinic patient note (may be handwritten or typed, English/Hindi mix). Extract these fields as JSON only: visit_date, blood_pressure, blood_sugar (with type if mentioned: fasting/random/post-meal/unknown), weight_kg, medicines, tests_ordered, planned_followup (what and when), referral. If a value is unclear or missing, use null. Do not guess. Do not infer diagnoses. Do not add information that is not written. Return a confidence (low/medium/high) for each field. Treat Hindi numerals (१, २, ३...) and Hindi phrases (जैसे '२ हफ्ते बाद' = after 2 weeks, 'सुबह खाली पेट' = fasting sugar) accurately.
${visitDateHint ? `Note: possible reference visit date is around ${visitDateHint}.` : ''}
${noteText ? `Accompanying transcript/note text: """${noteText}"""` : ''}`;
contentsPayload = {
parts: [
{
inlineData: {
data: cleanBase64,
mimeType,
},
},
{
text: promptText,
},
],
};
} else {
// Text-based extraction from typed/pasted note
const textPrompt = `You are reading a rural clinic patient note (typed or transcribed doctor's prescription note, English/Hindi mix). Extract these fields as JSON only: visit_date, blood_pressure, blood_sugar (with type if mentioned: fasting/random/post-meal/unknown), weight_kg, medicines, tests_ordered, planned_followup (what and when), referral. If a value is unclear or missing, use null. Do not guess. Do not infer diagnoses. Do not add information that is not written. Return a confidence (low/medium/high) for each field. Treat Hindi numerals (१, २, ३...) and Hindi phrases (जैसे '२ हफ्ते बाद' = after 2 weeks, 'सुबह खाली पेट' = fasting sugar) accurately.
${visitDateHint ? `Note: reference visit date is around ${visitDateHint}.` : ''}
Prescription Note Content:
"""
${noteText}
"""`;
contentsPayload = textPrompt;
}
let response: any = null;
const modelsToTry = ['gemini-flash-latest', 'gemini-3.1-flash-lite', 'gemini-3.8-flash'];
for (const modelName of modelsToTry) {
try {
response = await ai.models.generateContent({
model: modelName,
contents: contentsPayload,
config: {
responseMimeType: 'application/json',
responseSchema: noteExtractionSchema,
temperature: 0.1,
},
});
if (response) break;
} catch (err: any) {
console.warn(`[Gemini Extract Note] Model ${modelName} unavailable or rate-limited:`, err?.message?.slice(0, 120) || 'API limit');
continue; // Try next model in sequence
}
}
if (!response) {
return res.json({
success: false,
error: 'Gemini free-tier quota reached. Switched to smart clinical fallback extractor.',
isFallbackAvailable: true,
rateLimited: true,
});
}
const responseText = response.text?.trim() || '{}';
const parsedData = JSON.parse(responseText);
res.json({
success: true,
data: parsedData,
source: 'gemini',
});
} catch (error: any) {
console.warn('[Gemini Extract Note notice]:', error?.message?.slice(0, 120) || 'Serving fallback');
res.json({
success: false,
error: 'Gemini rate-limited or quota exhausted. Serving clinical fallback parser.',
isFallbackAvailable: true,
rateLimited: true,
});
}
});
/**
* 5b. explainFlag: Rephrases deterministic clinical signals in natural EN/HI
*/
app.post('/api/gemini/explain-flag', async (req: Request, res: Response) => {
const { flagFacts, language = 'en', templateReason } = req.body;
try {
if (!ai) {
return res.json({
success: false,
explanation: templateReason,
fallbackReason: templateReason,
source: 'rule',
});
}
const langName = language === 'hi' ? 'Hindi' : 'English';
const closingPhrase = language === 'hi' ? 'डॉक्टर द्वारा समीक्षा की सलाह दी जाती है' : 'Doctor review recommended';
const promptText = `Rewrite this clinical decision-support note in plain, simple ${langName} for a traveling doctor, in at most 2 short sentences. Use ONLY the facts provided below. Do not add numbers, causes, diagnoses, or treatment advice. Do not say the patient 'has' any disease. End with '${closingPhrase}'.
Input Facts:
${JSON.stringify(flagFacts, null, 2)}
Original Template:
"${templateReason}"`;
let response: any = null;
const modelsToTry = ['gemini-flash-latest', 'gemini-3.1-flash-lite', 'gemini-3.8-flash'];
for (const modelName of modelsToTry) {
try {
response = await ai.models.generateContent({
model: modelName,
contents: promptText,
config: {
temperature: 0.2,
},
});
if (response) break;
} catch (err: any) {
console.warn(`[Gemini Explain Flag] Model ${modelName} notice:`, err?.message?.slice(0, 120) || 'API limit');
continue;
}
}
const outputText = response?.text?.trim() || templateReason;
res.json({
success: Boolean(response),
explanation: outputText,
language,
source: response ? 'ai' : 'rule',
});
} catch (error: any) {
console.warn('[Gemini Explain Flag notice]:', error?.message?.slice(0, 120) || 'Serving fallback template');
res.json({
success: false,
explanation: templateReason,
fallbackReason: templateReason,
rateLimited: true,
source: 'rule',
});
}
});
// Setup Vite middleware in dev or static files in production
async function startServer() {
const isProd = process.env.NODE_ENV === 'production';
if (!isProd) {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: 'spa',
});
app.use(vite.middlewares);
} else {
app.use(express.static(path.resolve(__dirname, 'dist')));
app.get('*', (_req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
});
}
app.listen(PORT, '0.0.0.0', () => {
console.log(`CareLoop Rural server running on http://0.0.0.0:${PORT}`);
});
}
startServer();