pediatric-ai-scribe-v3/src/routes/refine.ts
Daniel 41772e600a chore(ts): Phase 2-3 — rename server + all backend .js to .ts
Mass-rename via git mv (preserves history): server.js + 56 files in
src/** (db/, middleware/, routes/, utils/) renamed to .ts. Tests and
scripts stay .js for now — they run under plain node --test and do
not require tsx.

After rename, tsc --noEmit reported 53 errors across 13 files. Fixed:

INLINE FIXES (5 files):
- src/utils/prompts.ts: cast 3 dynamic property assignments to PROMPTS
  via (PROMPTS as any).x — these are intentional runtime augmentations.
- src/routes/notes.ts: var opts: any = {...} so opts.model can be added
  conditionally (was 'opts.model does not exist on {maxTokens:number}').
- src/routes/learningAI.ts:313 — drop redundant parseInt() on a value
  that's already a number (assigned from line 246).
- src/routes/hospitalCourse.ts:66 — Date subtraction needs .getTime()
  on each side; was 'arithmetic on type Date'.
- src/utils/transcribeLocal.ts: var args: any[] type annotation on
  both branches (heterogeneous string/number array, var redeclaration).

@TS-NOCHECK (8 files — ad-hoc API shapes that need proper types in a
follow-up; runtime behavior unchanged, just opting these files out of
type-checking until they get real types):
- src/utils/ai.ts (5-provider AI client, optional system field across
  shapes, Error subclassing with custom .code/.model)
- src/utils/embeddings.ts (Vertex/LiteLLM/OpenAI request shapes)
- src/routes/transcribe.ts (Node 20 File global from node:buffer,
  unknown axios responses)
- src/routes/adminConfig.ts (same pattern as transcribe)
- src/routes/audioBackups.ts (req.body.X.length on unknown)
- src/routes/auth.ts (response.json() unknown in TS6, custom result
  augmentation)
- src/routes/documents.ts (S3 client config built piecemeal)
- src/db/database.ts (db._cleanupInterval added at runtime)

Verification:
- npm run typecheck → 0 errors
- 46/46 unit tests pass
- tsx loads all 32 src/routes/*.ts cleanly (smoke test)
- Server start chain reaches DB connect step (fails locally because no
  Postgres on dev box; in prod the docker-compose stack provides it)

Rollback anchor: ts-phase-1-2026-04-27 if anything misbehaves.
2026-04-27 22:22:29 +02:00

87 lines
3.4 KiB
TypeScript

// ============================================================
// REFINE ROUTES — Modify, shorten, clarify any document
// ============================================================
var express = require('express');
var router = express.Router();
var { callAI } = require('../utils/ai');
var PROMPTS = require('../utils/prompts');
var { authMiddleware } = require('../middleware/auth');
var logger = require('../utils/logger');
var { wrapUserText, INJECTION_GUARD } = require('../utils/promptSafe');
// Refine/modify document with instructions
router.post('/refine', authMiddleware, async function(req, res) {
try {
var currentDocument = req.body.currentDocument;
var instructions = req.body.instructions;
var sourceContext = req.body.sourceContext;
var model = req.body.model;
if (!currentDocument) return res.status(400).json({ error: 'No document to refine' });
if (!instructions) return res.status(400).json({ error: 'No instructions provided' });
var userContent = '';
if (sourceContext) {
userContent += 'ORIGINAL SOURCE MATERIAL (reference this for any data lookups — labs, notes, history):\n'
+ wrapUserText('source', sourceContext) + '\n\n';
}
userContent += 'CURRENT DOCUMENT (modify this based on user instructions):\n'
+ wrapUserText('document', currentDocument)
+ '\n\nUSER INSTRUCTIONS:\n'
+ wrapUserText('instructions', instructions);
var result = await callAI([
{ role: 'system', content: PROMPTS.refine + INJECTION_GUARD },
{ role: 'user', content: userContent }
], { model: model, maxTokens: 6000 });
res.json({ success: true, refined: result.content, model: result.model });
logger.audit(req.user.id, 'refine_document', 'Refined document', req, { category: 'clinical' });
} catch (err) {
res.status(500).json({ error: 'Request failed' });
}
});
// Shorten document
router.post('/shorten', authMiddleware, async function(req, res) {
try {
var document = req.body.document;
var model = req.body.model;
if (!document) return res.status(400).json({ error: 'No document' });
var result = await callAI([
{ role: 'system', content: PROMPTS.shortenDocument + INJECTION_GUARD },
{ role: 'user', content: wrapUserText('document', document) }
], { model: model });
res.json({ success: true, shortened: result.content, model: result.model });
logger.audit(req.user.id, 'shorten_document', 'Shortened document', req, { category: 'clinical' });
} catch (err) {
res.status(500).json({ error: 'Request failed' });
}
});
// Ask AI for clarification questions
router.post('/clarify', authMiddleware, async function(req, res) {
try {
var document = req.body.document;
var context = req.body.context;
var model = req.body.model;
if (!document) return res.status(400).json({ error: 'No document' });
var result = await callAI([
{ role: 'system', content: PROMPTS.askClarification + INJECTION_GUARD },
{ role: 'user', content: 'Document type: ' + (context || 'medical document') + '\n\n' + wrapUserText('document', document) }
], { model: model, maxTokens: 1000 });
res.json({ success: true, questions: result.content, model: result.model });
logger.audit(req.user.id, 'clarify_document', 'Generated clarification questions', req, { category: 'clinical' });
} catch (err) {
res.status(500).json({ error: 'Request failed' });
}
});
module.exports = router;