From 7f6eecd1ab626d262752217f4842f0f3d1aa485a Mon Sep 17 00:00:00 2001 From: sharath <29162020+tnfssc@users.noreply.github.com> Date: Sat, 7 Jun 2025 07:07:55 +0000 Subject: [PATCH] fix(api): access toast on LP --- frontend/src/lib/api.ts | 58 ++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index d9d1e5d0..7d6d1781 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -29,6 +29,13 @@ export class BillingError extends Error { } } +export class NoAccessTokenAvailableError extends Error { + constructor(message?: string, options?: { cause?: Error }) { + super(message || 'No access token available', options); + } + name = 'NoAccessTokenAvailableError'; +} + // Type Definitions (moved from potential separate file for clarity) export type Project = { id: string; @@ -541,7 +548,7 @@ export const startAgent = async ( } = await supabase.auth.getSession(); if (!session?.access_token) { - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } // Check if backend URL is configured @@ -633,6 +640,10 @@ export const startAgent = async ( throw error; } + if (error instanceof NoAccessTokenAvailableError) { + throw error; + } + console.error('[API] Failed to start agent:', error); // Handle different error types with appropriate user messages @@ -673,7 +684,7 @@ export const stopAgent = async (agentRunId: string): Promise => { } = await supabase.auth.getSession(); if (!session?.access_token) { - const authError = new Error('No access token available'); + const authError = new NoAccessTokenAvailableError(); handleApiError(authError, { operation: 'stop agent', resource: 'AI assistant' }); throw authError; } @@ -714,7 +725,7 @@ export const getAgentStatus = async (agentRunId: string): Promise => { if (!session?.access_token) { console.error('[API] No access token available for getAgentStatus'); - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } const url = `${API_URL}/agent-run/${agentRunId}`; @@ -771,7 +782,7 @@ export const getAgentRuns = async (threadId: string): Promise => { } = await supabase.auth.getSession(); if (!session?.access_token) { - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } const response = await fetch(`${API_URL}/thread/${threadId}/agent-runs`, { @@ -789,6 +800,10 @@ export const getAgentRuns = async (threadId: string): Promise => { const data = await response.json(); return data.agent_runs || []; } catch (error) { + if (error instanceof NoAccessTokenAvailableError) { + throw error; + } + console.error('Failed to get agent runs:', error); handleApiError(error, { operation: 'load agent runs', resource: 'conversation history' }); throw error; @@ -875,8 +890,9 @@ export const streamAgent = ( } = await supabase.auth.getSession(); if (!session?.access_token) { + const authError = new NoAccessTokenAvailableError(); console.error('[STREAM] No auth token available'); - callbacks.onError(new Error('Authentication required')); + callbacks.onError(authError); callbacks.onClose(); return; } @@ -1398,7 +1414,7 @@ export const initiateAgent = async ( } = await supabase.auth.getSession(); if (!session?.access_token) { - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } if (!API_URL) { @@ -1570,7 +1586,7 @@ export const createCheckoutSession = async ( } = await supabase.auth.getSession(); if (!session?.access_token) { - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } const response = await fetch(`${API_URL}/billing/create-checkout-session`, { @@ -1637,7 +1653,7 @@ export const createPortalSession = async ( } = await supabase.auth.getSession(); if (!session?.access_token) { - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } const response = await fetch(`${API_URL}/billing/create-portal-session`, { @@ -1679,7 +1695,7 @@ export const getSubscription = async (): Promise => { } = await supabase.auth.getSession(); if (!session?.access_token) { - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } const response = await fetch(`${API_URL}/billing/subscription`, { @@ -1703,6 +1719,10 @@ export const getSubscription = async (): Promise => { return response.json(); } catch (error) { + if (error instanceof NoAccessTokenAvailableError) { + throw error; + } + console.error('Failed to get subscription:', error); handleApiError(error, { operation: 'load subscription', resource: 'billing information' }); throw error; @@ -1717,7 +1737,7 @@ export const getAvailableModels = async (): Promise => } = await supabase.auth.getSession(); if (!session?.access_token) { - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } const response = await fetch(`${API_URL}/billing/available-models`, { @@ -1741,6 +1761,10 @@ export const getAvailableModels = async (): Promise => return response.json(); } catch (error) { + if (error instanceof NoAccessTokenAvailableError) { + throw error; + } + console.error('Failed to get available models:', error); handleApiError(error, { operation: 'load available models', resource: 'AI models' }); throw error; @@ -1756,7 +1780,7 @@ export const checkBillingStatus = async (): Promise => { } = await supabase.auth.getSession(); if (!session?.access_token) { - throw new Error('No access token available'); + throw new NoAccessTokenAvailableError(); } const response = await fetch(`${API_URL}/billing/check-status`, { @@ -1780,6 +1804,10 @@ export const checkBillingStatus = async (): Promise => { return response.json(); } catch (error) { + if (error instanceof NoAccessTokenAvailableError) { + throw error; + } + console.error('Failed to check billing status:', error); throw error; } @@ -1799,7 +1827,7 @@ export const transcribeAudio = async (audioFile: File): Promise