const express = require('express'); const router = express.Router(); const { callAI } = require('../utils/ai'); const PROMPTS = require('../utils/prompts'); const { authMiddleware } = require('../middleware/auth'); router.post('/generate-soap', authMiddleware, async (req, res) => { try { const { transcript, patientAge, patientGender, model, type, additionalInstructions, physicianMemories } = req.body; if (!transcript || !transcript.trim()) return res.status(400).json({ error: 'Empty input' }); let prompt; switch (type) { case 'subjective': prompt = PROMPTS.soapSubjective; break; case 'full': default: prompt = PROMPTS.soapFull; } if (additionalInstructions) { prompt += `\n\nADDITIONAL INSTRUCTIONS:\n${additionalInstructions}`; } var context = `Patient: ${patientAge || 'Unknown'}, ${patientGender || 'Unknown'}\n\nINPUT:\n${transcript}`; if (physicianMemories) context += '\n\n[PHYSICIAN PREFERENCES & LEARNED PATTERNS — Apply these preferences to your output. These reflect how this physician writes notes, their preferred style, terminology, and corrections from past outputs. Adapt your response accordingly, but user instructions in the current prompt take priority if they conflict.]\n' + physicianMemories + '\n[END PREFERENCES]'; const result = await callAI([ { role: 'system', content: prompt }, { role: 'user', content: context } ], { model }); res.json({ success: true, soap: result.content, model: result.model }); } catch (err) { res.status(500).json({ error: err.message }); } }); module.exports = router;