|
7 | 7 | analyzeVocalBiomarkers, |
8 | 8 | } from "@/lib/gemini"; |
9 | 9 | import { textToSpeech } from "@/lib/elevenlabs"; |
| 10 | +import { sendTelegramNotification } from "@/lib/telegram-api"; |
10 | 11 | import { writeFile, mkdir, access } from "fs/promises"; |
11 | 12 | import path from "path"; |
12 | 13 |
|
@@ -99,7 +100,9 @@ function processAnswerInBackground( |
99 | 100 | function generateSummaryInBackground( |
100 | 101 | sessionId: string, |
101 | 102 | elderlyName: string, |
102 | | - language: string |
| 103 | + language: string, |
| 104 | + elderlyProfileId: string, |
| 105 | + overallScore: number |
103 | 106 | ) { |
104 | 107 | (async () => { |
105 | 108 | try { |
@@ -150,14 +153,65 @@ function generateSummaryInBackground( |
150 | 153 |
|
151 | 154 | const [report, vocalAnalysis] = await Promise.all([summaryPromise, vocalPromise]); |
152 | 155 |
|
| 156 | + const severity = report.severity; |
| 157 | + |
153 | 158 | await prisma.assessmentSession.update({ |
154 | 159 | where: { id: sessionId }, |
155 | 160 | data: { |
156 | 161 | summary: report.summary, |
157 | | - severity: report.severity, |
| 162 | + severity, |
158 | 163 | ...(vocalAnalysis ? { vocalAnalysis: vocalAnalysis as object } : {}), |
159 | 164 | }, |
160 | 165 | }); |
| 166 | + |
| 167 | + // Send detailed assessment report via Telegram |
| 168 | + try { |
| 169 | + const scorePct = Math.round(overallScore * 100); |
| 170 | + const severityIcon = severity === "GREEN" ? "\u2705" : severity === "YELLOW" ? "\u26A0\uFE0F" : "\u274C"; |
| 171 | + const totalCorrect = allAnswers.filter((a: { result: string | null }) => a.result === "CORRECT").length; |
| 172 | + const totalWrong = allAnswers.filter((a: { result: string | null }) => a.result === "WRONG").length; |
| 173 | + const totalUnclear = allAnswers.filter((a: { result: string | null }) => a.result !== "CORRECT" && a.result !== "WRONG").length; |
| 174 | + |
| 175 | + let msg = `${severityIcon} *${elderlyName}'s Assessment Report*\n`; |
| 176 | + msg += `${todayStr}\n\n`; |
| 177 | + msg += `*Score:* ${scorePct}% (${totalCorrect}/${allAnswers.length} correct)\n`; |
| 178 | + msg += `*Status:* ${severity}\n\n`; |
| 179 | + |
| 180 | + if (report.summary) { |
| 181 | + msg += `*Summary:* ${report.summary}\n\n`; |
| 182 | + } |
| 183 | + |
| 184 | + // Question-by-question breakdown |
| 185 | + msg += `*Questions:*\n`; |
| 186 | + for (let i = 0; i < allAnswers.length; i++) { |
| 187 | + const a = allAnswers[i]; |
| 188 | + const icon = a.result === "CORRECT" ? "\u2705" : a.result === "WRONG" ? "\u274C" : "\u2753"; |
| 189 | + msg += `${icon} ${a.questionText}\n`; |
| 190 | + msg += ` Answer: ${a.elderAnswer || "No answer"}\n`; |
| 191 | + if (a.result !== "CORRECT") { |
| 192 | + msg += ` Correct: ${a.correctAnswer}\n`; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // Vocal analysis |
| 197 | + if (vocalAnalysis) { |
| 198 | + const va = vocalAnalysis as { |
| 199 | + parkinsons: { currentProbability: number; futureRisk: number; details: string }; |
| 200 | + depression: { currentState: number; futurePropensity: number; details: string }; |
| 201 | + mood: { todayMood: string; wellnessScore: number; details: string }; |
| 202 | + }; |
| 203 | + msg += `\n*Vocal & Wellness Analysis:*\n`; |
| 204 | + msg += `Mood: ${va.mood.todayMood} | Wellness: ${va.mood.wellnessScore}/100\n`; |
| 205 | + msg += `${va.mood.details}\n\n`; |
| 206 | + msg += `Parkinson's Risk: ${va.parkinsons.currentProbability}% (future: ${va.parkinsons.futureRisk}%)\n`; |
| 207 | + msg += `Depression: ${va.depression.currentState}% (future: ${va.depression.futurePropensity}%)\n`; |
| 208 | + msg += `\n_This analysis is for caregiver awareness only._`; |
| 209 | + } |
| 210 | + |
| 211 | + await sendTelegramNotification(elderlyProfileId, msg); |
| 212 | + } catch (telegramErr) { |
| 213 | + console.error("Telegram report notification failed:", telegramErr); |
| 214 | + } |
161 | 215 | } catch (err) { |
162 | 216 | console.error("Background summary failed:", err); |
163 | 217 | } |
@@ -258,7 +312,7 @@ export async function POST(req: NextRequest) { |
258 | 312 | }, |
259 | 313 | }).catch((err: unknown) => console.error("Session completion failed:", err)); |
260 | 314 |
|
261 | | - generateSummaryInBackground(sessionId, profile.name, profile.language); |
| 315 | + generateSummaryInBackground(sessionId, profile.name, profile.language, profile.id, score); |
262 | 316 |
|
263 | 317 | // Generate TTS summary using same ElevenLabs voice |
264 | 318 | const voiceId = profile.voiceId || undefined; |
|
0 commit comments