- Add Cloudflare Turnstile to login, register, and password reset forms - Switch AI provider to LiteLLM, transcription to OpenAI Whisper - Change domain to scribe.pedshub.com - Fix PPTX export: add tables, bold/italic, numbered lists, code blocks, blockquotes - Fix announcement banner close button (CSP was blocking inline onclick) - Fix auth middleware: empty Bearer token now falls through to cookie auth - Fix audio backups: only save on transcription failure, stop auto-deleting on success - Soften AI correction injection to prevent model hallucination from correction history - Fix LiteLLM TTS model name handling (no incorrect openai/ prefix) - Expand AI instructions textarea in Learning Hub CMS - Update README for v6 with all features and providers - Add comprehensive docs/: architecture, API reference, database schema, authentication, AI providers, speech, learning hub, configuration, deployment
142 lines
5 KiB
JavaScript
142 lines
5 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { callAI } = require('../utils/ai');
|
|
const PROMPTS = require('../utils/prompts');
|
|
const { authMiddleware } = require('../middleware/auth');
|
|
|
|
// Generate hospital course
|
|
router.post('/generate-hospital-course', authMiddleware, async (req, res) => {
|
|
try {
|
|
const {
|
|
notes, // Array of { date, type, content }
|
|
edNote, // { date, content, labs }
|
|
hAndP, // { date, content }
|
|
labs, // Array of { date, values }
|
|
patientAge, patientGender, pmh,
|
|
setting, // 'floor' | 'picu' | 'nicu' | 'psych'
|
|
los, // length of stay
|
|
model,
|
|
formatPreference, // 'auto' | 'prose' | 'dayByDay' | 'organSystem'
|
|
additionalInstructions,
|
|
physicianMemories
|
|
} = req.body;
|
|
|
|
if (!notes || notes.length === 0) {
|
|
return res.status(400).json({ error: 'No notes provided' });
|
|
}
|
|
|
|
// Determine format
|
|
let prompt;
|
|
let format = formatPreference || 'auto';
|
|
|
|
if (format === 'auto') {
|
|
if (setting === 'picu' || setting === 'nicu') {
|
|
format = 'organSystem';
|
|
} else if (setting === 'psych') {
|
|
format = 'psych';
|
|
} else if (los && parseInt(los) > 3) {
|
|
format = 'dayByDay';
|
|
} else {
|
|
format = 'prose';
|
|
}
|
|
}
|
|
|
|
switch (format) {
|
|
case 'organSystem': prompt = PROMPTS.hospitalCourseICU; break;
|
|
case 'dayByDay': prompt = PROMPTS.hospitalCourseLong; break;
|
|
case 'psych': prompt = PROMPTS.hospitalCoursePsych; break;
|
|
default: prompt = PROMPTS.hospitalCourseShort;
|
|
}
|
|
|
|
// Build the complete clinical data
|
|
let clinicalData = `Patient: ${patientAge || 'Unknown'}, ${patientGender || 'Unknown'}`;
|
|
if (pmh) clinicalData += `\nPMH: ${pmh}`;
|
|
if (setting) clinicalData += `\nSetting: ${setting}`;
|
|
if (los) clinicalData += `\nLength of Stay: ${los} days`;
|
|
clinicalData += `\nFormat: ${format}`;
|
|
|
|
if (edNote) {
|
|
clinicalData += `\n\n=== ED NOTE (${edNote.date || 'date unknown'}) ===\n${edNote.content}`;
|
|
if (edNote.labs) clinicalData += `\nED Labs: ${edNote.labs}`;
|
|
}
|
|
|
|
if (hAndP) {
|
|
clinicalData += `\n\n=== H&P (${hAndP.date || 'date unknown'}) ===\n${hAndP.content}`;
|
|
}
|
|
|
|
// Sort notes by date
|
|
const sortedNotes = [...notes].sort((a, b) => new Date(a.date) - new Date(b.date));
|
|
sortedNotes.forEach((note, i) => {
|
|
clinicalData += `\n\n=== ${(note.type || 'Progress Note').toUpperCase()} - ${note.date || `Note ${i + 1}`} ===\n${note.content}`;
|
|
});
|
|
|
|
if (labs && labs.length > 0) {
|
|
clinicalData += '\n\n=== LABS ===';
|
|
labs.forEach(lab => {
|
|
clinicalData += `\n${lab.date}: ${lab.values}`;
|
|
});
|
|
}
|
|
|
|
if (additionalInstructions) {
|
|
prompt += `\n\nADDITIONAL INSTRUCTIONS FROM PHYSICIAN:\n${additionalInstructions}`;
|
|
}
|
|
|
|
if (physicianMemories) clinicalData += '\n\n[STYLE HINTS (low priority — only apply if relevant to the current note, never copy content from these examples)]\n' + physicianMemories + '\n[END STYLE HINTS]';
|
|
|
|
const result = await callAI([
|
|
{ role: 'system', content: prompt },
|
|
{ role: 'user', content: clinicalData }
|
|
], { model, maxTokens: 6000 });
|
|
|
|
res.json({
|
|
success: true,
|
|
hospitalCourse: result.content,
|
|
format: format,
|
|
model: result.model
|
|
});
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// Ask for clarification/missing info
|
|
router.post('/hospital-course-clarify', authMiddleware, async (req, res) => {
|
|
try {
|
|
const { currentDraft, notes } = req.body;
|
|
|
|
const result = await callAI([
|
|
{ role: 'system', content: PROMPTS.askClarification },
|
|
{ role: 'user', content: `Current draft:\n${currentDraft}\n\nSource notes:\n${JSON.stringify(notes)}` }
|
|
], { model: req.body.model });
|
|
|
|
res.json({ success: true, questions: result.content, model: result.model });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// Add discharge day info
|
|
router.post('/hospital-course-update', authMiddleware, async (req, res) => {
|
|
try {
|
|
const { currentCourse, updates, model, instructions } = req.body;
|
|
if (!currentCourse) return res.status(400).json({ error: 'No current course' });
|
|
|
|
let userMsg = `CURRENT HOSPITAL COURSE:\n${currentCourse}\n\n`;
|
|
if (updates) userMsg += `NEW UPDATES TO ADD:\n${updates}\n\n`;
|
|
if (instructions) userMsg += `INSTRUCTIONS:\n${instructions}`;
|
|
|
|
const result = await callAI([
|
|
{ role: 'system', content: `You are updating a hospital course document with new information.
|
|
Add the new information in the appropriate place. Maintain the same format and style.
|
|
If adding discharge day information, add it at the end.
|
|
${PROMPTS.refine.split('\n').slice(0, -1).join('\n')}` },
|
|
{ role: 'user', content: userMsg }
|
|
], { model, maxTokens: 6000 });
|
|
|
|
res.json({ success: true, hospitalCourse: result.content, model: result.model });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|