/** * Shared Supabase Client * * Provides a configured Supabase client for use across Motia steps. * Uses service role key for full database access. */ import { createClient, type SupabaseClient } from '@supabase/supabase-js'; // Database types (auto-generated from schema) // Generic JSON value type for Supabase JSON columns export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }; // Motia framework types for step handlers export interface MotiaRequest { params?: Record; query?: Record; body?: unknown; headers?: Record; } export interface MotiaContext { emit: (event: string, data?: unknown) => void; } export interface ActiveBot { bot_id: string; bot_type: 'screenshare' | 'voice-agent' | 'tool-forwarder'; pid: number; session_id: string; room_url: string; deck_url: string; status: 'spawning' | 'running' | 'stopping' | 'stopped' | 'error'; spawned_at: string; stopped_at: string | null; error_message: string | null; log_file_path: string | null; created_at: string; updated_at: string; } export interface ConversationSession { id: string; session_type: string; status: string; environment: string; // Daily.co fields (legacy system) daily_room_url: string | null; daily_room_name: string | null; daily_room_config: JsonValue | null; daily_recording_id: string | null; daily_recording_url: string | null; daily_recording_started_at: string | null; daily_recording_duration_seconds: number | null; // Tavus fields (legacy system) tavus_conversation_id: string | null; tavus_replica_id: string | null; // LiveKit fields (NEW) livekit_room_id: string | null; livekit_room_name: string | null; livekit_recording_url: string | null; voice_agent_session_id: string | null; // Discovery phase discovery_started_at: string | null; discovery_ended_at: string | null; discovery_duration_seconds: number | null; discovery_transcript: JsonValue | null; discovery_insights: JsonValue | null; discovery_data: JsonValue | null; // Pipecat session pipecat_session_id: string | null; // Presentation phase presentation_started_at: string | null; presentation_ended_at: string | null; presentation_duration_seconds: number | null; presentation_transcript: JsonValue | null; presentation_slide_history: JsonValue | null; // Slidev deck slidev_url: string | null; slidev_markdown: string | null; slidev_generated_at: string | null; slide_customization: JsonValue | null; // Customer info customer_name: string | null; customer_email: string | null; // Error tracking error_message: string | null; error_code: string | null; error_timestamp: string | null; error_context: JsonValue | null; // Attribution source: string | null; utm_source: string | null; utm_medium: string | null; utm_campaign: string | null; referrer_url: string | null; // Phase 2 prep client_id: string | null; persona_id: string | null; // Timestamps created_at: string; updated_at: string; } export interface SlidevDeck { id: string; deck_id: string; session_id: string | null; template_id: string; generation_data: JsonValue | null; deck_url: string | null; html_content: string | null; created_at: string; } export interface SlideControlCommand { id: string; deck_id: string; command: string; slide_id: string | null; timestamp: string; } export interface Database { public: { Tables: { active_bots: { Row: ActiveBot; Insert: Omit & { bot_id?: string; spawned_at?: string; stopped_at?: string | null; error_message?: string | null; }; Update: Partial>; }; conversation_sessions: { Row: ConversationSession; Insert: Omit & { id?: string; created_at?: string; updated_at?: string; }; Update: Partial>; }; slidev_decks: { Row: SlidevDeck; Insert: Omit & { id?: string; created_at?: string; }; Update: Partial>; }; slide_control_commands: { Row: SlideControlCommand; Insert: Omit & { id?: string; timestamp?: string; }; Update: Partial>; }; }; }; } /** * Create a Supabase client with service role access */ export function createSupabaseClient(): SupabaseClient { const supabaseUrl = process.env.SUPABASE_URL; const supabaseKey = process.env.SUPABASE_SERVICE_KEY; if (!supabaseUrl || !supabaseKey) { throw new Error( 'Missing Supabase credentials. Set SUPABASE_URL and SUPABASE_SERVICE_KEY environment variables.' ); } return createClient(supabaseUrl, supabaseKey, { auth: { autoRefreshToken: false, persistSession: false, }, }); } /** * Helper: Check if a process is alive */ export function isProcessAlive(pid: number): boolean { try { // Send signal 0 to check if process exists (doesn't actually send a signal) process.kill(pid, 0); return true; } catch (_e) { return false; } } /** * Helper: Kill a process with SIGTERM (graceful) then SIGKILL (force) after timeout */ export function killProcess(pid: number, timeoutMs = 10000): void { try { // Send SIGTERM for graceful shutdown process.kill(pid, 'SIGTERM'); // Force kill after timeout setTimeout(() => { try { // Check if still alive process.kill(pid, 0); // Still alive, force kill console.log(`[killProcess] Force killing PID ${pid} after timeout`); process.kill(pid, 'SIGKILL'); } catch { // Already dead, nothing to do } }, timeoutMs); } catch (error: unknown) { if (error && typeof error === 'object' && 'code' in error && error.code === 'ESRCH') { // Process doesn't exist console.log(`[killProcess] Process ${pid} not found (already dead)`); } else { throw error; } } }