-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeminiService.ts
More file actions
291 lines (257 loc) · 11.2 KB
/
Copy pathgeminiService.ts
File metadata and controls
291 lines (257 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { GoogleGenAI, Type } from "@google/genai";
import { AppState, ExerciseEntry, FoodEntry, SleepEntry, UserProfile } from "./types";
const apiKey = process.env.GEMINI_API_KEY || '';
const ai = new GoogleGenAI({ apiKey });
// --- Dietitian Agent ---
export const analyzeFoodImage = async (base64Image: string): Promise<Partial<FoodEntry>> => {
try {
const response = await ai.models.generateContent({
model: 'gemini-3-flash-preview',
contents: {
parts: [
{ inlineData: { mimeType: 'image/jpeg', data: base64Image } },
{ text: "Analyze this food image. Estimate the name, calories, protein (g), carbs (g), and fats (g). Return strictly JSON." }
]
},
config: {
responseMimeType: 'application/json',
responseSchema: {
type: Type.OBJECT,
properties: {
name: { type: Type.STRING },
calories: { type: Type.NUMBER },
protein: { type: Type.NUMBER },
carbs: { type: Type.NUMBER },
fats: { type: Type.NUMBER },
notes: { type: Type.STRING, description: "Brief healthy observation" }
}
}
}
});
if (response.text) {
return JSON.parse(response.text);
}
throw new Error("No data returned");
} catch (error) {
console.error("Dietitian Error:", error);
throw error;
}
};
export const analyzeFoodText = async (description: string): Promise<Partial<FoodEntry>> => {
try {
const response = await ai.models.generateContent({
model: 'gemini-3-flash-preview',
contents: `Analyze this food description: "${description}". Estimate the name, calories, protein (g), carbs (g), and fats (g). Return strictly JSON.`,
config: {
responseMimeType: 'application/json',
responseSchema: {
type: Type.OBJECT,
properties: {
name: { type: Type.STRING },
calories: { type: Type.NUMBER },
protein: { type: Type.NUMBER },
carbs: { type: Type.NUMBER },
fats: { type: Type.NUMBER },
notes: { type: Type.STRING, description: "Brief healthy observation" }
}
}
}
});
if (response.text) {
return JSON.parse(response.text);
}
throw new Error("No data returned");
} catch (error) {
console.error("Dietitian Text Error:", error);
throw error;
}
};
// --- Physical Agent ---
export const generateWorkout = async (profile: UserProfile, mood: string): Promise<string> => {
try {
const context = `User: ${profile.age}yo, ${profile.gender}. History: ${profile.medicalHistory}. Goals: ${profile.goals.join(', ')}`;
const response = await ai.models.generateContent({
model: 'gemini-3-flash-preview',
contents: `Design a quick 20-minute home workout for this user. Context: ${context}. Current Mood: ${mood}. Ensure exercises are safe for their medical history. Format as a clear list.`,
});
return response.text || "Could not generate workout.";
} catch (error) {
console.error("Trainer Error:", error);
return "Error generating workout.";
}
};
export const calculateExerciseStats = async (description: string, duration: number): Promise<Partial<ExerciseEntry>> => {
try {
const response = await ai.models.generateContent({
model: 'gemini-3-flash-preview',
contents: `Calculate estimated calories burned for: "${description}" performed for ${duration} minutes. Return JSON.`,
config: {
responseMimeType: 'application/json',
responseSchema: {
type: Type.OBJECT,
properties: {
caloriesBurned: { type: Type.NUMBER },
intensity: { type: Type.STRING, enum: ['Low', 'Medium', 'High'] }
}
}
}
});
if (response.text) return JSON.parse(response.text);
throw new Error("No data");
} catch (e) {
console.error(e);
return { caloriesBurned: 100, intensity: 'Medium' };
}
};
// --- Sleep Agent ---
export const analyzeDream = async (dreamText: string): Promise<string> => {
try {
const response = await ai.models.generateContent({
model: 'gemini-3-flash-preview',
contents: `Act as a Jungian dream analyst. Briefly interpret this dream: "${dreamText}". Keep it under 100 words.`,
});
return response.text || "No interpretation available.";
} catch (e) {
return "Could not interpret dream.";
}
};
// --- Counseling Agent ---
export const chatWithCounselor = async (history: {role: string, parts: {text: string}[]}[], message: string): Promise<string> => {
try {
const chat = ai.chats.create({
model: 'gemini-3-flash-preview',
history: history,
config: {
systemInstruction: "You are a compassionate, empathetic mental health counselor agent. Keep responses concise, warm, and supportive. Do not give medical prescriptions.",
}
});
const result = await chat.sendMessage({ message });
return result.text || "...";
} catch (e) {
console.error(e);
return "I am having trouble connecting right now. Please try again.";
}
};
export const generateJournalEntry = async (chatHistory: string): Promise<string> => {
try {
const response = await ai.models.generateContent({
model: 'gemini-3-flash-preview',
contents: `Based on this chat history, write a reflective daily journal entry from the user's perspective. Capture the key emotions and thoughts discussed. \n\n Chat: ${chatHistory}`,
});
return response.text || "Could not generate journal.";
} catch (e) {
return "Error generating journal.";
}
};
// --- Medical Agent ---
export const consultMedicalAgent = async (profile: UserProfile, question: string): Promise<string> => {
try {
const response = await ai.models.generateContent({
model: 'gemini-3.1-pro-preview', // Using Pro for specialized medical reasoning as mandated
contents: `
You are an AI Medical Assistant.
User Context:
- Age: ${profile.age}
- Gender: ${profile.gender}
- Medical History: ${profile.medicalHistory}
- Genetic Risks: ${profile.geneticRisks}
- Goals: ${profile.goals.join(', ')}
User Question: ${question}
Provide a safe, informative, and personalized answer based on their history.
DISCLAIMER: Always start by stating you are an AI and this is not professional medical advice.
`
});
return response.text || "Unable to consult at this time.";
} catch (error) {
console.error("Medical Agent Error:", error);
return "Medical agent is currently unavailable.";
}
};
export const analyzeMedicalResult = async (base64Image: string): Promise<string> => {
try {
const response = await ai.models.generateContent({
model: 'gemini-3.1-pro-preview', // Using Pro for complex document analysis
contents: {
parts: [
{ inlineData: { mimeType: 'image/jpeg', data: base64Image } },
{ text: "Analyze this medical test result or document. Provide a concise, factual summary of the KEY FINDINGS suitable for saving to a medical record. Do not use conversational filler (e.g. 'Here is the analysis', 'The image shows'). Start directly with the clinical facts." }
]
}
});
return response.text || "Could not analyze the document.";
} catch (error) {
console.error("Medical Analysis Error:", error);
return "Error analyzing medical document.";
}
};
// --- The Coordinator (Agent Meeting) ---
export const synthesizeDailyReport = async (state: AppState): Promise<string> => {
try {
// Summarize recent history for context
const recentHistory = state.history.slice(0, 3).map(h =>
`- ${h.date}: ${h.mood || 'Neutral'} (${h.caloriesIn - h.caloriesBurned} net kcal)\n Details: ${h.details || 'No details'}`
).join('\n');
const today = new Date().toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
const prompt = `
Act as a Lead Health Coordinator conducting a daily board meeting for DailyHealth AI.
User Profile: ${state.profile.name}, ${state.profile.age}yo ${state.profile.gender}.
Medical Context (Long-term Memory): ${state.profile.medicalHistory}
Genetic Risks: ${state.profile.geneticRisks}
Recent Days Context (Short-term Memory):
${recentHistory || "No previous history recorded."}
Review the data from the specialist agents TODAY:
1. **Dietitian Report**: ${state.foodLog.map(f => `${f.name} (${f.calories}kcal, P:${f.protein}g)`).join(', ') || "No food logged"}
2. **Physical Report**: ${state.exerciseLog.map(e => `${e.type} (${e.durationMinutes}min, ${e.intensity})`).join(', ') || "No exercise logged"}
3. **Sleep Report**: ${state.sleepLog ? `${state.sleepLog.durationHours}hrs, Quality: ${state.sleepLog.quality}` : "No sleep logged"}
4. **Mental Health**: Current Emotion: ${state.currentEmotion || "Not recorded"}.
Task:
Synthesize this information into a cohesive "Daily Health Board Meeting Summary".
CRITICAL: You must include a "Medical Specialist" perspective in the meeting.
Structure the response as follows:
**Meeting Minutes: Daily Health Board Review**
**Date:** ${today}
**Attendees:** Lead Coordinator, Dietitian, Trainer, Sleep Specialist, Counselor, *Medical Specialist*
**1. Executive Summary**
(A brief holistic overview of the day).
**2. Specialist Insights & Medical Cross-Check**
- **Medical Specialist**: Analyze today's logs against the user's Medical History and Genetic Risks. Are there any contraindications? (e.g., High sugar intake vs Diabetes risk, or High Intensity Cardio vs Heart condition). References specific past memory if relevant.
- **Diet & Activity**: How did nutrition fuel movement today?
- **Rest & Recovery**: Is sleep supporting the user's goals?
**3. Strategy for Tomorrow**
- Bullet point 1
- Bullet point 2
- Bullet point 3
`;
const response = await ai.models.generateContent({
model: 'gemini-3.1-pro-preview',
contents: prompt,
});
return response.text || "Unable to generate consensus.";
} catch (error) {
console.error("Synthesis Error:", error);
return "The agents could not meet at this time.";
}
};
export const askCoordinator = async (state: AppState, question: string): Promise<string> => {
try {
const prompt = `
You are the Health Team Coordinator.
User: ${state.profile.name}.
Logs Today:
- Food: ${state.foodLog.length} items (${state.foodLog.reduce((a,b)=>a+b.calories,0)} kcal)
- Exercise: ${state.exerciseLog.length} items
- Sleep: ${state.sleepLog ? state.sleepLog.quality : "Unknown"}
- Medical History: ${state.profile.medicalHistory}
- Current Mood: ${state.currentEmotion}
User Question: "${question}"
Answer as the team representative. Consult the data above. Be helpful, concise, and collaborative.
`;
const response = await ai.models.generateContent({
model: 'gemini-3.1-pro-preview',
contents: prompt
});
return response.text || "I couldn't reach the team right now.";
} catch(e) {
return "The team is currently offline.";
}
};