import { Router } from 'express'; import { SearchServiceError } from '../services/searchService.js'; export function createSearchRouter(searchService, broadcast) { const router = Router(); router.post('/', async (request, response) => { const { question, previousClarification, originalQuestion } = request.body; if (!question) { response.status(400).json({ error: 'Missing required field: question', example: { question: 'What are the latest developments in AI?' }, }); return; } try { const result = await searchService.search(question, previousClarification, originalQuestion); response.json(result); } catch (error) { if (error instanceof SearchServiceError) { broadcast(`❌ Error: ${error.message}`, 'error', error.details); response.status(error.statusCode).json({ error: error.message }); return; } broadcast(`❌ Error: ${error.message}`, 'error', { error: error.stack }); response.status(500).json({ error: error.message }); } }); return router; }