/** * Screenshare Bot Status Step * * ARCHITECTURE: Event-driven + Supabase * - Queries active_bots table from Supabase * - Validates PID is still alive via OS signal * - Updates status if process died * * API endpoints: * - GET /api/bots/screenshare/status - Get all active bots * - GET /api/bots/screenshare/status/:bot_id - Get specific bot status */ import { createSupabaseClient, isProcessAlive, type MotiaRequest } from '../src/lib/supabase'; export const config = { name: 'ScreenshareBotStatus', type: 'api', path: '/api/bots/screenshare/status/:bot_id?', method: 'GET', description: 'Get status of screenshare bots', emits: [] }; export const handler = async (req: MotiaRequest) => { try { const botId = req.params?.bot_id; const supabase = createSupabaseClient(); // If bot_id provided, return single bot status if (botId) { console.log('[ScreenshareBotStatus] Status request for bot:', botId); // Validate bot_id format (UUID) const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; if (!uuidRegex.test(botId)) { return { status: 400, body: { error: 'Validation failed', details: 'Invalid bot_id format. Must be a valid UUID', }, }; } // Query bot from Supabase (filter by bot_type) const { data: bot, error } = await supabase .from('active_bots') .select('*') .eq('bot_id', botId) .eq('bot_type', 'screenshare') .single(); if (error || !bot) { return { status: 404, body: { error: 'Bot not found', details: `No screenshare bot exists with ID: ${botId}`, }, }; } // Check if process is still alive const isAlive = isProcessAlive(bot.pid); // Update status if process died unexpectedly if (!isAlive && bot.status !== 'stopped' && bot.status !== 'error') { console.log(`[ScreenshareBotStatus] Bot ${botId} process died (PID: ${bot.pid})`); await supabase .from('active_bots') .update({ status: 'error', stopped_at: new Date().toISOString(), error_message: 'Process died unexpectedly', }) .eq('bot_id', botId); bot.status = 'error'; bot.stopped_at = new Date().toISOString(); bot.error_message = 'Process died unexpectedly'; } return { status: 200, body: { bot_id: bot.bot_id, session_id: bot.session_id, room_url: bot.room_url, deck_url: bot.deck_url, status: bot.status, pid: bot.pid, spawned_at: bot.spawned_at, stopped_at: bot.stopped_at, error: bot.error_message, log_file: bot.log_file_path, is_process_alive: isAlive, }, }; } // Otherwise, return all screenshare bots status console.log('[ScreenshareBotStatus] Status request for all screenshare bots'); const { data: bots, error } = await supabase .from('active_bots') .select('*') .eq('bot_type', 'screenshare') .order('spawned_at', { ascending: false }); if (error) { throw new Error(`Failed to query bots: ${error.message}`); } // Check which bots are still alive const botsWithLiveness = bots?.map((bot) => ({ bot_id: bot.bot_id, session_id: bot.session_id, room_url: bot.room_url, deck_url: bot.deck_url, status: bot.status, pid: bot.pid, spawned_at: bot.spawned_at, stopped_at: bot.stopped_at, error: bot.error_message, is_process_alive: isProcessAlive(bot.pid), })) || []; return { status: 200, body: { active_count: botsWithLiveness.length, bots: botsWithLiveness, }, }; } catch (error: unknown) { console.error('[ScreenshareBotStatus] Error:', error); return { status: 500, body: { error: 'Failed to get bot status', details: error.message, }, }; } };