/** * Session Service * * Handles API calls to create and manage voice demo sessions. */ import type { VoiceSessionConfig, LiveKitTokens, ApiError } from '../types'; /** * Get API base URL from environment variables */ function getApiBaseUrl(): string { return import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000'; } /** * Create a new voice demo session * * Calls POST /api/sessions/voice-demo to create a session and get LiveKit tokens. * * @param config - Session configuration (customerName, customerEmail, deckId, etc.) * @returns LiveKit tokens and session details * @throws Error if the API request fails */ export async function createVoiceSession( config: VoiceSessionConfig = {} ): Promise { const apiBaseUrl = getApiBaseUrl(); const endpoint = `${apiBaseUrl}/api/sessions/voice-demo`; console.log('[SessionService] Creating voice demo session', { endpoint, config, }); try { const response = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(config), }); if (!response.ok) { let errorDetails: ApiError; try { errorDetails = await response.json(); } catch { errorDetails = { error: 'Failed to create session', details: `HTTP ${response.status}: ${response.statusText}`, }; } console.error('[SessionService] Failed to create session', errorDetails); throw new Error(errorDetails.details || errorDetails.error); } const tokens: LiveKitTokens = await response.json(); console.log('[SessionService] Session created successfully', { sessionId: tokens.sessionId, roomName: tokens.roomName, }); return tokens; } catch (error) { console.error('[SessionService] Error creating session', error); if (error instanceof Error) { throw error; } throw new Error('Unknown error creating voice demo session'); } } /** * Get LiveKit URL from environment variables */ export function getLivekitUrl(): string { return import.meta.env.VITE_LIVEKIT_URL || 'wss://buzztrail-xxx.livekit.cloud'; }