import { handleInfo } from '../controllers/info.controller.js';
import { handleDefinitions } from '../controllers/definitions.controller.js';
import { handleSearch } from '../controllers/search.controller.js';
import { handleExecuteStream } from '../controllers/stream.controller.js';
import { handleResume } from '../controllers/resume.controller.js';
import { readBody } from '../utils/index.js';
export async function handleRoute(req, res, context, logger) {
    const clientId = req.headers['x-client-id'];
    const url = new URL(req.url ?? '/', `http://${req.headers.host}`);
    if (url.pathname === '/api/info' && req.method === 'GET') {
        logger.debug('Serving API info');
        await handleInfo(req, res);
        return true;
    }
    if (url.pathname === '/api/definitions' && req.method === 'GET') {
        logger.debug('Serving API definitions');
        await handleDefinitions(req, res, context.aggregator, url);
        return true;
    }
    if (url.pathname === '/api/search' && req.method === 'POST') {
        logger.debug('Searching API functions');
        const body = await readBody(req);
        await handleSearch(req, res, context.searchEngine, body);
        return true;
    }
    if (url.pathname.match(/^\/api\/resume\/[^/]+$/) && req.method === 'POST') {
        const executionId = url.pathname.split('/').pop();
        const body = await readBody(req);
        await handleResume(req, res, { executor: context.executor, stateManager: context.stateManager }, executionId, body, logger);
        return true;
    }
    if (url.pathname === '/api/execute' && req.method === 'POST') {
        const body = await readBody(req);
        await handleExecuteStream(req, res, {
            validator: context.validator,
            executor: context.executor,
            stateManager: context.stateManager,
            auditConfig: context.auditConfig,
            defaultTimeout: context.defaultTimeout,
            defaultMemoryLimit: context.defaultMemoryLimit,
            defaultLLMCallLimit: context.defaultLLMCallLimit,
        }, body, clientId, logger);
        return true;
    }
    return false;
}
//# sourceMappingURL=index.js.map