From 0a97b43beb823cddd18be9d82ce182e8dabaf69a Mon Sep 17 00:00:00 2001 From: marko-kraemer Date: Fri, 25 Apr 2025 22:05:24 +0100 Subject: [PATCH] lil faster --- frontend/src/app/share/[threadId]/page.tsx | 78 +++++++++++++--------- frontend/src/lib/api.ts | 59 ++++++++-------- 2 files changed, 78 insertions(+), 59 deletions(-) diff --git a/frontend/src/app/share/[threadId]/page.tsx b/frontend/src/app/share/[threadId]/page.tsx index 8a24f07a..7c324688 100644 --- a/frontend/src/app/share/[threadId]/page.tsx +++ b/frontend/src/app/share/[threadId]/page.tsx @@ -7,7 +7,7 @@ import { Button } from '@/components/ui/button'; import { ArrowDown, CircleDashed, Info, File, ChevronRight, Play, Pause } from 'lucide-react'; -import { getMessages, getProject, getThread, Project, Message as BaseApiMessageType } from '@/lib/api'; +import { getMessages, getProject, getThread, Project, Message as BaseApiMessageType, getAgentRuns } from '@/lib/api'; import { toast } from 'sonner'; import { Skeleton } from "@/components/ui/skeleton"; import { FileViewerModal } from '@/components/thread/file-viewer-modal'; @@ -600,11 +600,20 @@ export default function ThreadPage({ params }: { params: Promise } try { if (!threadId) throw new Error('Thread ID is required'); - // Fetch the thread to check if it's public - const threadData = await getThread(threadId).catch(err => { - // If it fails with 404, we'll catch it here - throw new Error('Failed to load thread data: ' + err.message); - }); + // Start loading all data in parallel + const [threadData, agentRuns, messagesData] = await Promise.all([ + getThread(threadId).catch(err => { + throw new Error('Failed to load thread data: ' + err.message); + }), + getAgentRuns(threadId).catch(err => { + console.warn('Failed to load agent runs:', err); + return []; + }), + getMessages(threadId).catch(err => { + console.warn('Failed to load messages:', err); + return []; + }) + ]); if (!isMounted) return; @@ -613,37 +622,42 @@ export default function ThreadPage({ params }: { params: Promise } // throw new Error('This thread is not available for public viewing.'); // } - if (threadData?.project_id) { - try { - const projectData = await getProject(threadData.project_id); - if (isMounted && projectData) { - console.log('[SHARE] Project data loaded:', projectData); - - // Set project data - setProject(projectData); - - // Make sure sandbox ID is set correctly - if (typeof projectData.sandbox === 'string') { - setSandboxId(projectData.sandbox); - } else if (projectData.sandbox?.id) { - setSandboxId(projectData.sandbox.id); - } - - setProjectName(projectData.name || ''); + // Load project data if we have a project ID + const projectData = threadData?.project_id ? + await getProject(threadData.project_id).catch(err => { + console.warn('[SHARE] Could not load project data:', err); + return null; + }) : null; + + if (isMounted) { + if (projectData) { + console.log('[SHARE] Project data loaded:', projectData); + + // Set project data + setProject(projectData); + + // Make sure sandbox ID is set correctly + if (typeof projectData.sandbox === 'string') { + setSandboxId(projectData.sandbox); + } else if (projectData.sandbox?.id) { + setSandboxId(projectData.sandbox.id); } - } catch (projectError) { - // Don't throw an error if project can't be loaded - // Just log it and continue with the thread - console.warn('[SHARE] Could not load project data:', projectError); + + setProjectName(projectData.name || ''); + } else { // Set a generic name if we couldn't load the project setProjectName('Shared Conversation'); } - } - // Fetch all messages for the thread - const messagesData = await getMessages(threadId); - if (isMounted) { - // Log raw messages fetched from API + // Set agent run ID if available + if (agentRuns && agentRuns.length > 0) { + const latestRun = agentRuns[0]; + if (latestRun.status === 'running') { + setAgentRunId(latestRun.id); + } + } + + // Process messages data console.log('[SHARE] Raw messages fetched:', messagesData); // Map API message type to UnifiedMessage type diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index e9ef2713..332df618 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -139,34 +139,39 @@ export const getProject = async (projectId: string): Promise => { // If project has a sandbox, ensure it's started if (data.sandbox?.id) { - try { - const { data: { session } } = await supabase.auth.getSession(); - - // For public projects, we don't need authentication - const headers: Record = { - 'Content-Type': 'application/json' - }; - - if (session?.access_token) { - headers['Authorization'] = `Bearer ${session.access_token}`; + // Fire off sandbox activation without blocking + const ensureSandboxActive = async () => { + try { + const { data: { session } } = await supabase.auth.getSession(); + + // For public projects, we don't need authentication + const headers: Record = { + 'Content-Type': 'application/json' + }; + + if (session?.access_token) { + headers['Authorization'] = `Bearer ${session.access_token}`; + } + + console.log(`Ensuring sandbox is active for project ${projectId}...`); + const response = await fetch(`${API_URL}/project/${projectId}/sandbox/ensure-active`, { + method: 'POST', + headers, + }); + + if (!response.ok) { + const errorText = await response.text().catch(() => 'No error details available'); + console.warn(`Failed to ensure sandbox is active: ${response.status} ${response.statusText}`, errorText); + } else { + console.log('Sandbox activation successful'); + } + } catch (sandboxError) { + console.warn('Failed to ensure sandbox is active:', sandboxError); } - - console.log(`Ensuring sandbox is active for project ${projectId}...`); - const response = await fetch(`${API_URL}/project/${projectId}/sandbox/ensure-active`, { - method: 'POST', - headers, - }); - - if (!response.ok) { - const errorText = await response.text().catch(() => 'No error details available'); - console.warn(`Failed to ensure sandbox is active: ${response.status} ${response.statusText}`, errorText); - } else { - console.log('Sandbox activation successful'); - } - } catch (sandboxError) { - console.warn('Failed to ensure sandbox is active:', sandboxError); - // Non-blocking error - continue with the project data - } + }; + + // Start the sandbox activation without awaiting + ensureSandboxActive(); } // Map database fields to our Project type