⚠️ This is test/experimental code for API integration testing. Do not use in production. Includes: - WellNuo API integration (dashboard, patient context) - Playwright tests for API verification - WebView component for dashboard embedding - API documentation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
131 lines
4.0 KiB
JavaScript
131 lines
4.0 KiB
JavaScript
// Test chat functionality with patient context
|
|
const https = require('https');
|
|
|
|
const OPENAI_API_KEY = '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';
|
|
const CONTEXT_URL = 'https://wellnuo.smartlaunchhub.com/api/patient/context';
|
|
|
|
async function fetchPatientContext() {
|
|
return new Promise((resolve, reject) => {
|
|
https.get(CONTEXT_URL, (res) => {
|
|
let data = '';
|
|
res.on('data', chunk => data += chunk);
|
|
res.on('end', () => {
|
|
try {
|
|
resolve(JSON.parse(data));
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
});
|
|
}).on('error', reject);
|
|
});
|
|
}
|
|
|
|
async function askAboutPatient(context, question) {
|
|
// Build system prompt from context
|
|
const systemPrompt = `You are Julia, a caring AI health assistant for WellNuo family care system.
|
|
|
|
Current patient information:
|
|
- Name: ${context.patient.name}
|
|
- Age: ${context.patient.age}
|
|
- Relationship: ${context.patient.relationship}
|
|
- Location: ${context.current_status.location}
|
|
- Activity: ${context.current_status.estimated_activity}
|
|
|
|
Recent sleep data:
|
|
- Last night: ${context.sleep_analysis?.last_night?.total_hours || 'N/A'} hours
|
|
- Quality: ${context.sleep_analysis?.last_night?.quality_score || 'N/A'}/100
|
|
|
|
Today's activity:
|
|
- Active minutes: ${context.activity_patterns?.today?.total_active_minutes || 'N/A'}
|
|
- Rooms visited: ${context.activity_patterns?.today?.rooms_visited?.join(', ') || 'N/A'}
|
|
|
|
Environment (${context.current_status.location}):
|
|
- Temperature: ${context.environment?.living_room?.temperature_f || 'N/A'}°F
|
|
- Humidity: ${context.environment?.living_room?.humidity_percent || 'N/A'}%
|
|
- Air quality: ${context.environment?.living_room?.air_quality_status || 'N/A'}
|
|
|
|
Be warm, concise, and reassuring. Answer questions about the patient's wellbeing.`;
|
|
|
|
const requestData = JSON.stringify({
|
|
model: 'gpt-4o-mini',
|
|
messages: [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: question }
|
|
],
|
|
max_tokens: 300
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const req = https.request({
|
|
hostname: 'api.openai.com',
|
|
path: '/v1/chat/completions',
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${OPENAI_API_KEY}`,
|
|
'Content-Length': Buffer.byteLength(requestData)
|
|
}
|
|
}, (res) => {
|
|
let data = '';
|
|
res.on('data', chunk => data += chunk);
|
|
res.on('end', () => {
|
|
try {
|
|
const response = JSON.parse(data);
|
|
if (response.error) {
|
|
reject(new Error(response.error.message));
|
|
} else {
|
|
resolve(response.choices[0].message.content);
|
|
}
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', reject);
|
|
req.write(requestData);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
console.log('=== TESTING WELLNUO CHAT ===\n');
|
|
|
|
// Step 1: Fetch patient context
|
|
console.log('1. Fetching patient context...');
|
|
let context;
|
|
try {
|
|
context = await fetchPatientContext();
|
|
console.log(` Patient: ${context.patient.name}, ${context.patient.age} years old`);
|
|
console.log(` Location: ${context.current_status.location}`);
|
|
console.log(` Activity: ${context.current_status.estimated_activity}`);
|
|
console.log(' Context API: OK\n');
|
|
} catch (e) {
|
|
console.log(` ERROR: ${e.message}\n`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Step 2: Ask questions about patient
|
|
const questions = [
|
|
'How is my father doing today?',
|
|
'Did he sleep well last night?',
|
|
'Where is he right now?'
|
|
];
|
|
|
|
console.log('2. Testing AI chat responses:\n');
|
|
|
|
for (const question of questions) {
|
|
console.log(`Q: "${question}"`);
|
|
try {
|
|
const answer = await askAboutPatient(context, question);
|
|
console.log(`A: ${answer}\n`);
|
|
} catch (e) {
|
|
console.log(`ERROR: ${e.message}\n`);
|
|
}
|
|
}
|
|
|
|
console.log('=== CHAT TEST COMPLETE ===');
|
|
}
|
|
|
|
main().catch(console.error);
|