/** * ClinicAgent: An agentic orchestrator for Curio. * It uses natural language understanding to map user requests to hospital tools. */ const patientManager = require('./patientManager'); const queueManager = require('./queueManager'); const pharmacyAgent = require('./pharmacyAgent'); const configManager = require('./configManager'); const Anthropic = require('@anthropic-ai/sdk'); const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY, // Defaults to process.env.ANTHROPIC_API_KEY }); const LLM_MODEL = process.env.LLM_MODEL || 'claude-3-haiku-20240307'; class ClinicAgent { constructor() { this.systemPrompt = ` You are 'Curio AI', the assistant for Curio Health. Your goal is to help patients book appointments. Be extremely concise, friendly, and natural. Do not use markdown. To book an appointment, you MUST collect: 1. Department (e.g. Cardiology, Neurology, General) 2. Date (Format as YYYY-MM-DD or today/tomorrow) 3. Time (Format as HH:MM AM/PM) If any of these 3 pieces of information are missing, naturally ask the user for them. Do NOT output JSON until you have ALL THREE pieces of information. If you have all three pieces of information, you MUST reply with ONLY a JSON block like this (no other text): {"action": "BOOK", "department": "Cardiology", "date": "2026-05-26", "time": "10:00"} `; } async processMessage(phone, transcript) { console.log(`[Agent] Processing for ${phone} via Claude`); try { const response = await anthropic.messages.create({ model: LLM_MODEL, max_tokens: 300, temperature: 0.2, system: this.systemPrompt, messages: transcript }); const replyContent = response.content[0].text.trim(); // Try to parse JSON intent try { // Remove potential markdown code blocks if the LLM wrapped it const cleanJson = replyContent.replace(/```json/g, '').replace(/```/g, '').trim(); const intent = JSON.parse(cleanJson); if (intent.action === 'BOOK') { // Extract HH:MM let timeVal = "10:00"; // fallback const timeMatch = intent.time.match(/(\d{1,2}:\d{2})/); if (timeMatch) timeVal = timeMatch[1]; // Parse date let dateVal = new Date().toISOString().split('T')[0]; if (intent.date && intent.date.match(/^\d{4}-\d{2}-\d{2}$/)) { dateVal = intent.date; } // Book the token using Dr. Sharma's Cardiology ("sharma-clinic") // In a real system, we'd map intent.department to tenantId const result = await queueManager.bookOnline(dateVal, timeVal, phone, 'sharma-clinic'); if (result.success) { return { reply: `Awesome! Your appointment for ${intent.department} is booked. Your token is ${result.token}. You should receive a confirmation message shortly.` }; } else { return { reply: `Sorry, ${result.message}` }; } } } catch (e) { // Not JSON, just regular conversational text return { reply: replyContent }; } return { reply: replyContent }; } catch (error) { console.error('[ClinicAgent] LLM Error:', error.message); return { reply: "I'm having trouble connecting to my AI brain right now. Please try again in a moment." }; } } } module.exports = new ClinicAgent();