diff --git a/netlify/functions/classify-crisis.cjs b/netlify/functions/classify-crisis.cjs index 0f3993b..1c8cf83 100644 --- a/netlify/functions/classify-crisis.cjs +++ b/netlify/functions/classify-crisis.cjs @@ -69,25 +69,42 @@ Post to analyze: ${textToAnalyze} """ -Respond with ONLY one word: LOW, MEDIUM, or HIGH. No explanation, no punctuation, nothing else.`; +Respond ONLY with a raw JSON object formatted exactly like this, with no markdown formatting or code blocks: +{ + "riskLevel": "HIGH", + "reason": "Brief explanation of why this risk level was chosen" +}`; const result = await model.generateContent(prompt); - const rawText = result?.response?.text()?.trim().toUpperCase() || 'LOW'; + let rawText = result?.response?.text()?.trim() || ''; + + // Safely strip potential markdown code blocks + if (rawText.startsWith('```')) { + rawText = rawText.replace(/^```(json)?\n?/, '').replace(/\n?```$/, '').trim(); + } + + let parsedResult = { riskLevel: 'LOW', reason: '' }; + try { + parsedResult = JSON.parse(rawText); + } catch (parseError) { + console.error('Failed to parse JSON from AI:', rawText); + } const VALID_LEVELS = ['LOW', 'MEDIUM', 'HIGH']; - const riskLevel = VALID_LEVELS.includes(rawText) ? rawText : 'LOW'; + const riskLevel = VALID_LEVELS.includes(parsedResult.riskLevel?.toUpperCase()) ? parsedResult.riskLevel.toUpperCase() : 'LOW'; + const reason = parsedResult.reason || ''; return { statusCode: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' }, - body: JSON.stringify({ riskLevel }), + body: JSON.stringify({ riskLevel, reason }), }; } catch (error) { console.error('Crisis classification error:', error); return { statusCode: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' }, - body: JSON.stringify({ error: 'Classification failed', riskLevel: 'LOW' }), + body: JSON.stringify({ error: 'Classification failed', riskLevel: 'LOW', reason: '' }), }; } }; \ No newline at end of file diff --git a/src/components/EmergencyModal.tsx b/src/components/EmergencyModal.tsx new file mode 100644 index 0000000..657a12a --- /dev/null +++ b/src/components/EmergencyModal.tsx @@ -0,0 +1,73 @@ +import React from 'react'; +import { X, AlertTriangle } from 'lucide-react'; +import { QUICK_HELPLINES } from '../utils/constants'; + +interface EmergencyModalProps { + isOpen: boolean; + onClose: () => void; +} + +export function EmergencyModal({ isOpen, onClose }: EmergencyModalProps) { + if (!isOpen) return null; + + return ( +
+ We noticed your story mentions signs of urgent distress. Your safety is our top priority. Please do not hesitate to reach out to these free, confidential, and 24/7 helplines right now. +
+ +{helpline.number}
+{helpline.description}
+