/* eslint-disable @typescript-eslint/naming-convention */ import type { FastifyInstance, FastifyRequest } from 'fastify'; import { validateRequest } from '../../utilities/validate-request.js'; import { buildStaticResponse, handleStreamingResponse, applyResponseDelay, } from '../../utilities/response-helpers.js'; import { buildClaudeStaticResponse } from '../../utilities/build-claude-response.js'; import { handleClaudeStreamingResponse } from '../../utilities/build-claude-streaming-response.js'; import { shouldStream } from '../../utilities/stream-mode.js'; import { decideCall, getReply, isPassthrough, sendPassthrough, } from '../../utilities/call-outcome.js'; import { recordRequest, type RecordedRequest, } from '../../utilities/request-recorder.js'; // Static JSON vs SSE stream is decided per request by shouldStream(): // - claude preset: the request body's `stream` field // - openai preset: the body's boolean `stream` if it has one, else STREAM // - other presets: the STREAM env variable (true = OpenAI-style SSE, default false) /** * Handles the common request processing logic for both GET and POST routes * Applies delay, generates content, and returns appropriate response format * * @param reply - Fastify reply object * @param body - Parsed request body (undefined for GET requests) * @returns Promise */ const handleRequest = async ( request: FastifyRequest, reply: any, record: RecordedRequest, ) => { // A GET has no body: it gets a generated reply (a quick smoke test) const body = request.method === 'POST' ? request.body : undefined; // Apply configured response delay for realistic API simulation await applyResponseDelay(); // Static JSON or an SSE stream (see shouldStream for the rules) const streaming = shouldStream( process.env?.LLM_NAME, body, process.env?.STREAM, ); // A rule with `fail`, or chaos, may fail this call: with an error in // place of a reply, or a stream that fails part-way through const { matched, failure } = decideCall( reply, body, record, streaming, process.env?.LLM_NAME, ); if (failure === true) return reply; const streamFailure = failure; // The reply: a rule's, a generated one, or when recording the provider's const mockReply = await getReply( request, matched, record, process.env?.LLM_NAME, ); if (isPassthrough(mockReply)) { record.streamed = false; return sendPassthrough(reply, mockReply, streaming); } // Route to appropriate response handler if (!streaming) { // === STATIC MODE === // Returns single JSON response matching OpenAI chat.completion format // Uses openai_res.json template with DYNAMIC_CONTENT_HERE replacement // The claude preset adds a unique id, echoed model and token usage const response = process.env?.LLM_NAME === 'claude' ? await buildClaudeStaticResponse(mockReply, body) : await buildStaticResponse(mockReply, body); return reply.send(response); } if (process.env?.LLM_NAME === 'claude') { // === CLAUDE STREAMING MODE === // Anthropic Messages SSE events (message_start ... message_stop) return await handleClaudeStreamingResponse( mockReply, reply, body, streamFailure, ); } // === STREAMING MODE === // Returns OpenAI-style Server-Sent Events stream with chat.completion.chunk events // Streaming format is compatible with OpenAI chat-completions streaming API // Content is split into multiple chunks with proper SSE headers and timing return await handleStreamingResponse(mockReply, reply, streamFailure, body); }; function handler(app: FastifyInstance, pathName: string) { const prefix = process.env?.LLM_URL_ENDPOINT ?? ''; const fullPath = prefix || pathName; // GET route - handles chat completion requests via HTTP GET // Supports both static and streaming modes based on STREAM env variable app.get(`/${fullPath}`, async (request, reply) => { return await handleRequest(request, reply, recordRequest(request)); }); // POST route - handles chat completion requests via HTTP POST // Validates request format against openai_req.json template before processing // Supports both static and streaming modes based on STREAM env variable app.post(`/${fullPath}`, async (request, reply) => { // Validate incoming request against template to ensure API compatibility if (await validateRequest(request)) { return await handleRequest(request, reply, recordRequest(request)); } recordRequest(request, false); // Return detailed error message for invalid requests console.log( `\nREQUEST ERROR: Invalid or missing request format for this LLM Model:${process.env?.LLM_NAME?.toUpperCase()}`, ); return reply .status(400) .type('text/plain') .send( `Invalid or Missing Request For This LLM Model Template: ${process.env?.LLM_NAME?.toUpperCase()} Model:${process.env?.LLM_MODEL ?? 'NO MODEL DEFINED'}. Please ensure your request adheres to the expected format - see localhost:${process.env?.SERVER_PORT ?? '8001'}/ui-request-log for details of missing parameters or formatting issues.`, ); }); } export default handler;