import { existsSync } from 'fs'; import { resolve } from 'path'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; import logger from './logger.js'; import { getConfigDir } from '../config/config-loader.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); type LogData = { data: any; state: string; reason: string; information: string; }; /** * Checks a request has the top-level keys of its request template, and logs * it. `templateName` picks the template (`_req.json`), by default the * preset's own. */ export const validateRequest = async ( request: any, templateName: string = process.env.LLM_NAME ?? 'openai', ) => { // Every entry for this request carries the id sent back with its reply const requestId: string | undefined = typeof request?.id === 'string' ? request.id : undefined; const logRequestBody = ({ data, state, reason, information }: LogData) => { console.log( `New API Request:${new Date().toLocaleString()}${requestId ? ` (${requestId})` : ''}. View it with 'View request log' on the dashboard at 'localhost:${process.env?.SERVER_PORT ?? '8000'}'`, ); logger( data, state, reason, information, ...(requestId ? [requestId] : []), ); }; // Check if validation is disabled if (process.env.VALIDATE_REQUESTS !== 'ON') { return true; } let requestTemplate: { default: JSON[] } | undefined; const templateFileName = `${templateName}_req.json`; // Try the project's templates first (next to the config file). The server // runs from the package folder, so process.cwd() is not the project. const cwdTemplatePath = resolve( getConfigDir(), 'request-templates', templateFileName, ); const srcTemplatePath = resolve( __dirname, '../request-templates', templateFileName, ); try { console.log(`Looking for template: ${templateFileName}`); console.log(`Checking project path: ${cwdTemplatePath}`); console.log(`Checking SRC path: ${srcTemplatePath}`); console.log(`Project template exists: ${existsSync(cwdTemplatePath)}`); // First try the project folder if (existsSync(cwdTemplatePath)) { console.log(`Loading template from project: ${cwdTemplatePath}`); const fs = await import('fs'); const templateData = fs.readFileSync(cwdTemplatePath, 'utf8'); requestTemplate = { default: JSON.parse(templateData) }; } else { console.log(`Loading template from SRC: ${templateFileName}`); // Fallback to src directory // eslint-disable-next-line @typescript-eslint/no-implied-eval requestTemplate = (await import( /* @vite-ignore */ `../request-templates/${templateFileName}`, { with: { type: 'json' }, } )) as { default: JSON[] }; } } catch { console.log('INTERNAL ERROR: No request template found'); console.log(`Checked: ${cwdTemplatePath} and ${srcTemplatePath}`); logRequestBody({ data: request.body, state: 'FAILED', reason: 'MISSING LLM REQUEST TEMPLATE', information: `Add ${templateFileName} in request-templates folder in current directory or src/request-templates`, }); return false; } // Check request has a body and it is an object if (typeof request.body !== 'object' || request.body === null) { console.log('ERROR: missing request body'); logRequestBody({ data: 'Missing or invalid request body', state: 'FAILED', reason: 'INVALID REQUEST', information: 'Missing or Invalid Request Body', }); return false; } // For Fastify, the body is already parsed const data = request.body; const requiredKeys = Object.keys(requestTemplate.default[0]); if (typeof data !== 'object' || data === null) { logRequestBody({ data: request?.body, state: 'FAILED', reason: 'INVALID REQUEST BODY', information: 'data must not be null and must be an object', }); return false; } const requestKeys = Object.keys(data); let missingKeys = false; const missingKeyInformation: string[] = []; for (const key of requiredKeys) { if (!requestKeys.includes(key)) { missingKeys = true; missingKeyInformation.push(key.toUpperCase()); } } if (missingKeys) { logRequestBody({ data, state: 'FAILED', reason: 'INVALID / MISSING KEY(S)', information: `missing keys: ${missingKeyInformation.join(' , ')}`, }); return false; } // Logged once per request, so a failed one isn't also recorded as passed logRequestBody({ data, state: 'PASSED', reason: 'REQUEST STRUCTURE OK', information: 'request structure matches template', }); return true; };