/** * Bot Logs Get Step * * API endpoint to retrieve bot logs from log files. * Since bot stdout/stderr goes to files, we need this to diagnose issues. * * Path: GET /api/bots/screenshare/logs/:bot_id */ import * as path from 'path'; import * as fs from 'fs'; import type { MotiaRequest } from '../src/lib/supabase'; export const config = { name: 'ScreenshareBotLogsGet', type: 'api', path: '/api/bots/screenshare/logs/:bot_id', method: 'GET', description: 'Get screenshare bot logs from log file', emits: [], // No events emitted by this read-only endpoint }; export const handler = async (req: MotiaRequest) => { try { const { bot_id } = req.params; if (!bot_id) { return { status: 400, body: { error: 'Validation failed', details: 'Missing bot_id parameter', }, }; } // Prepare log file path const logDir = path.join(__dirname, '../bots/screenshare-bot/logs'); const logFilePath = path.join(logDir, `bot-${bot_id}.log`); console.log(`[BotLogsGet] Reading log file: ${logFilePath}`); // Check if log file exists if (!fs.existsSync(logFilePath)) { return { status: 404, body: { error: 'Log file not found', details: `No log file exists for bot ${bot_id}`, expected_path: logFilePath, }, }; } // Read log file const logContent = fs.readFileSync(logFilePath, 'utf-8'); const lines = logContent.split('\n'); // Get last N lines (default 200) const tailLines = parseInt(req.query.lines || '200', 10); const recentLines = lines.slice(-tailLines); return { status: 200, headers: { 'Content-Type': 'application/json', }, body: { bot_id, log_file: logFilePath, total_lines: lines.length, returned_lines: recentLines.length, lines: recentLines, content: recentLines.join('\n'), }, }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; console.error('[BotLogsGet] Error:', error); return { status: 500, body: { error: 'Failed to read bot logs', details: errorMessage, }, }; } };