From 84c17f68f76f485b7bac762e4f1cdd2fe215bb17 Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 24 Dec 2025 18:13:34 -0800 Subject: [PATCH] Restore context fetching with parallel requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fetch activity + dashboard context in parallel - Context embedded in question for better AI responses 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app/(tabs)/chat.tsx | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/app/(tabs)/chat.tsx b/app/(tabs)/chat.tsx index f26fe4a..a236578 100644 --- a/app/(tabs)/chat.tsx +++ b/app/(tabs)/chat.tsx @@ -168,22 +168,40 @@ export default function ChatScreen() { } }; - // Send message with full context - optimized for speed + // Send message with full context - fetches context in parallel for speed const sendWithContext = async (question: string): Promise => { const token = await SecureStore.getItemAsync('accessToken'); const userName = await SecureStore.getItemAsync('userName'); if (!token || !userName) throw new Error('Please log in'); + if (!currentBeneficiary?.id) throw new Error('Please select a beneficiary'); - const beneficiaryName = currentBeneficiary?.name || 'the patient'; - const deploymentId = currentBeneficiary?.id?.toString() || ''; + const beneficiaryName = currentBeneficiary.name || 'the patient'; + const deploymentId = currentBeneficiary.id.toString(); - // Skip context fetching for faster response - just send directly - const enhancedQuestion = currentBeneficiary?.id - ? `You are a caring assistant helping monitor ${beneficiaryName}'s wellbeing. Please answer: ${question}` - : `You are a helpful AI assistant. Please answer: ${question}`; + // Fetch both contexts in PARALLEL for speed + const [activityContext, dashboardContext] = await Promise.all([ + getActivityContext(token, userName, deploymentId), + getDashboardContext(token, userName, deploymentId), + ]); - // Call API directly without pre-fetching context + // Use activity context, fallback to dashboard + const context = activityContext || dashboardContext; + + // Build the question with embedded context + let enhancedQuestion: string; + if (context) { + enhancedQuestion = `You are a caring assistant helping monitor ${beneficiaryName}'s wellbeing. + +Here is the current data about ${beneficiaryName}: +${context} + +Based on this data, please answer the following question: ${question}`; + } else { + enhancedQuestion = `You are a caring assistant helping monitor ${beneficiaryName}'s wellbeing. Please answer: ${question}`; + } + + // Call API const requestBody = new URLSearchParams({ function: 'voice_ask', clientId: '001', @@ -191,7 +209,7 @@ export default function ChatScreen() { token: token, question: enhancedQuestion, deployment_id: deploymentId, - context: '', + context: context || '', }).toString(); const response = await fetch(API_URL, {