From 34ea675b31ecbe4f37be6359f313c88ae8b9993f Mon Sep 17 00:00:00 2001 From: sharath <29162020+tnfssc@users.noreply.github.com> Date: Wed, 18 Jun 2025 04:40:35 +0000 Subject: [PATCH] fix(api): implement pagination for message retrieval to handle large datasets --- frontend/src/lib/api.ts | 42 ++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 9f312362..91e74c96 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -511,23 +511,39 @@ export const addUserMessage = async ( export const getMessages = async (threadId: string): Promise => { const supabase = createClient(); - const { data, error } = await supabase - .from('messages') - .select('*') - .eq('thread_id', threadId) - .neq('type', 'cost') - .neq('type', 'summary') - .order('created_at', { ascending: true }); + let allMessages: Message[] = []; + let from = 0; + const batchSize = 1000; + let hasMore = true; - if (error) { - console.error('Error fetching messages:', error); - handleApiError(error, { operation: 'load messages', resource: `messages for thread ${threadId}` }); - throw new Error(`Error getting messages: ${error.message}`); + while (hasMore) { + const { data, error } = await supabase + .from('messages') + .select('*') + .eq('thread_id', threadId) + .neq('type', 'cost') + .neq('type', 'summary') + .order('created_at', { ascending: true }) + .range(from, from + batchSize - 1); + + if (error) { + console.error('Error fetching messages:', error); + handleApiError(error, { operation: 'load messages', resource: `messages for thread ${threadId}` }); + throw new Error(`Error getting messages: ${error.message}`); + } + + if (data && data.length > 0) { + allMessages = allMessages.concat(data); + from += batchSize; + hasMore = data.length === batchSize; + } else { + hasMore = false; + } } - console.log('[API] Messages fetched:', data); + console.log('[API] Messages fetched count:', allMessages.length); - return data || []; + return allMessages; }; // Agent APIs