// Active Log Tracker for OCR Processing Layers export interface VllmCall { request: any; response: any; timestamp: string; } export interface ActiveUploadLog { filename: string; vllm_calls: VllmCall[]; ocr_raw?: any; stage_1_output?: any; stage_2_output?: any; frontend_response?: any; pipeline_info?: any; } // Store active logs in global context as a Map keyed by filename // This supports concurrent uploads without race conditions const globalForActiveLog = global as unknown as { activeLogs: Map; }; // Initialise the map once (survives Next.js hot-reloads on the same process) if (!globalForActiveLog.activeLogs) { globalForActiveLog.activeLogs = new Map(); } export function startActiveLog(filename: string) { globalForActiveLog.activeLogs.set(filename, { filename, vllm_calls: [] }); console.log(`[ActiveLog] Started tracking log for ${filename}`); } export function logVllmCall(filename: string, request: any, response: any) { const log = globalForActiveLog.activeLogs.get(filename); if (log) { log.vllm_calls.push({ request, response, timestamp: new Date().toISOString() }); console.log(`[ActiveLog] Logged vLLM call for ${filename} (total calls: ${log.vllm_calls.length})`); } else { console.log(`[ActiveLog] Warning: Attempted to log vLLM call for "${filename}" but no active log session is running.`); } } export function getActiveLog(filename: string): ActiveUploadLog | null { return globalForActiveLog.activeLogs.get(filename) || null; } export function clearActiveLog(filename: string) { globalForActiveLog.activeLogs.delete(filename); console.log(`[ActiveLog] Cleared active log tracking context for ${filename}`); } /** * Log a vLLM call to ALL currently active upload sessions. * Used by the vllm-proxy, which doesn't have per-upload filename context, * since the pipeline-api processes exactly one upload at a time. */ export function logVllmCallToAll(request: any, response: any) { const sessions = globalForActiveLog.activeLogs; if (sessions.size === 0) { console.log("[ActiveLog] Warning: Attempted to log vLLM call but no active log session is running."); return; } for (const [filename, log] of sessions) { log.vllm_calls.push({ request, response, timestamp: new Date().toISOString() }); console.log(`[ActiveLog] Logged vLLM call for ${filename} (total calls: ${log.vllm_calls.length})`); } }