import sharp from 'sharp'; import { EndpointSpec, ParamSpec, TryItResult } from '@/types'; import { generateMockNeuralResult } from './mockEngine'; const circuitBreakers = new Map(); const CIRCUIT_THRESHOLD = 5; const CIRCUIT_RESET_MS = 30000; export function checkFileSignature(buffer: Buffer): { valid: boolean; mime?: string } { if (buffer.length < 12) return { valid: false }; // JPEG if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) { return { valid: true, mime: 'image/jpeg' }; } // PNG if ( buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4e && buffer[3] === 0x47 && buffer[4] === 0x0d && buffer[5] === 0x0a && buffer[6] === 0x1a && buffer[7] === 0x0a ) { return { valid: true, mime: 'image/png' }; } // WEBP const riff = buffer.toString('ascii', 0, 4); const webp = buffer.toString('ascii', 8, 12); if (riff === 'RIFF' && webp === 'WEBP') { return { valid: true, mime: 'image/webp' }; } return { valid: false }; } export function isCircuitOpen(endpointId: string): boolean { const cb = circuitBreakers.get(endpointId); if (!cb) return false; if (cb.failureCount >= CIRCUIT_THRESHOLD) { if (Date.now() - cb.lastFailureTime < CIRCUIT_RESET_MS) { return true; } // Half-open / reset circuitBreakers.delete(endpointId); } return false; } export function recordEndpointSuccess(endpointId: string) { circuitBreakers.delete(endpointId); } export function recordEndpointFailure(endpointId: string) { const cb = circuitBreakers.get(endpointId) || { failureCount: 0, lastFailureTime: 0 }; cb.failureCount++; cb.lastFailureTime = Date.now(); circuitBreakers.set(endpointId, cb); } export async function executeBackendTryIt( endpoint: EndpointSpec, toolId: string, variantId: string | undefined, imageBuffer: Buffer, filename: string, options: Record = {} ): Promise<{ buffer: Buffer; contentType: string }> { // Check Circuit Breaker if (isCircuitOpen(endpoint.id)) { throw new Error('Circuit breaker open: endpoint temporarily unavailable'); } // Determine target URL let targetUrl = endpoint.url; if (variantId && endpoint.variants && endpoint.variants.length > 0) { const matched = endpoint.variants.find((v) => v.id === variantId); if (matched && matched.url) { targetUrl = matched.url; } } // Use remote production endpoint directly from data.json (or override if explicitly provided via env) const envBackendUrl = process.env.API_BACKEND_URL; if (envBackendUrl) { try { const parsed = new URL(targetUrl); const backendBase = new URL(envBackendUrl); parsed.protocol = backendBase.protocol; parsed.host = backendBase.host; targetUrl = parsed.toString(); } catch { // Keep original targetUrl } } // Enforce MAX 720px dimension on all images sent to the remote server let effectiveBuffer = imageBuffer; try { const meta = await sharp(imageBuffer).metadata(); if (meta.width && meta.height && (meta.width > 720 || meta.height > 720)) { effectiveBuffer = await sharp(imageBuffer) .resize(720, 720, { fit: 'inside', withoutEnlargement: true }) .toBuffer(); } } catch {} // Prepare FormData const formData = new FormData(); const blob = new Blob([new Uint8Array(effectiveBuffer)]); // Find primary file parameter name // Prioritize based on known conventions: 'original_image', 'image0', 'image', 'source', 'source1', 'file' const primaryCandidates = ['original_image', 'image0', 'image', 'source', 'source1', 'file']; const matchedPrimary = endpoint.params.find( (p) => p.kind === 'file' && primaryCandidates.includes(p.name) ) || endpoint.params.find( (p) => p.kind === 'file' && !p.name.toLowerCase().includes('mask') && !p.name.toLowerCase().includes('ref') && !p.name.toLowerCase().includes('target') && !p.name.toLowerCase().includes('bg') && !p.name.toLowerCase().includes('source2') ); const fileParamName = matchedPrimary?.name || 'image0'; formData.append(fileParamName, blob, filename); // If mask buffer provided (Inpainting / Custom Mask / Auto Mask) if (options.maskBuffer && Buffer.isBuffer(options.maskBuffer)) { let effectiveMask = options.maskBuffer; try { const meta = await sharp(effectiveMask).metadata(); if (meta.width && meta.height && (meta.width > 720 || meta.height > 720)) { effectiveMask = await sharp(effectiveMask) .resize(720, 720, { fit: 'inside', withoutEnlargement: true }) .toBuffer(); } } catch {} const maskParam = endpoint.params.find( (p) => p.kind === 'file' && p.name.toLowerCase().includes('mask') ); const maskParamName = maskParam?.name || 'mask_image'; const maskBlob = new Blob([new Uint8Array(effectiveMask)]); formData.append(maskParamName, maskBlob, options.maskFilename || 'mask.png'); } // If secondary image buffer provided (Two-Images: Face Swap, Relighting, Try-on) if (options.secondaryBuffer && Buffer.isBuffer(options.secondaryBuffer)) { let effectiveSec = options.secondaryBuffer; try { const meta = await sharp(effectiveSec).metadata(); if (meta.width && meta.height && (meta.width > 720 || meta.height > 720)) { effectiveSec = await sharp(effectiveSec) .resize(720, 720, { fit: 'inside', withoutEnlargement: true }) .toBuffer(); } } catch {} const secCandidates = ['target', 'bg_image', 'face_segmented_image', 'source2', 'reference_image', 'reference', 'image2']; const matchedSec = endpoint.params.find( (p) => p.kind === 'file' && secCandidates.includes(p.name) ) || endpoint.params.find( (p) => p.kind === 'file' && p.name !== fileParamName && !p.name.toLowerCase().includes('mask') ); const secParamName = matchedSec?.name || 'reference_image'; const secBlob = new Blob([new Uint8Array(effectiveSec)]); formData.append(secParamName, secBlob, options.secondaryFilename || 'reference.jpg'); if (endpoint.params.some((p) => p.name === 'target') && !formData.has('target')) { formData.append('target', secBlob, options.secondaryFilename || 'reference.jpg'); } } // Inject Secret Key (PRD ยง7.11: Secrets never reach browser) const secretParam = endpoint.params.find((p) => p.isSecret || p.name === 'secretKey'); const secretVal = process.env.API_SECRET_DEFAULT || 'abcdef'; if (secretParam) { formData.append(secretParam.name, secretVal); } else { formData.append('secretKey', secretVal); } // Add options & preset mappings (colorId, effectId, style_id, prompt, dress, etc.) for (const [k, v] of Object.entries(options)) { if (k.endsWith('Buffer') || k.endsWith('Filename')) continue; if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') { formData.append(k, String(v)); } } // Ensure effectId / colorId / style_id / artid / dress / expression synchronization if provided in options const selectedEffect = options.style || options.effectId || options.colorId || options.effect_id || options.artId || options.artid || options.dress || options.expression || options.cartoonId; if (selectedEffect) { for (const targetKey of ['colorId', 'effectId', 'effect_id', 'style_id', 'artId', 'artid', 'cartoonId', 'dress', 'expression', 'style', 'option']) { if (endpoint.params.some((p) => p.name === targetKey) && !formData.has(targetKey)) { formData.append(targetKey, String(selectedEffect)); } } } // Populate default values for required / missing parameters from catalog for (const param of endpoint.params) { if (param.kind !== 'file' && !formData.has(param.name)) { if (param.default && param.default !== 'None' && param.default !== 'null') { let val: any = param.default; if (typeof val === 'string') { val = val.replace(/^['"]|['"]$/g, ''); if (val.startsWith('Capped at ')) val = val.replace('Capped at ', ''); if (val === 'Random') val = String(Math.floor(Math.random() * 1000000)); } formData.append(param.name, String(val)); } } } const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), endpoint.limits.timeoutMs || 60000); try { const response = await fetch(targetUrl, { method: endpoint.method === 'MULTI' ? 'POST' : endpoint.method, body: formData, signal: controller.signal, }); clearTimeout(timeoutId); if (!response.ok) { console.warn(`[Backend API] Call failed with status ${response.status} from ${targetUrl}`); if (process.env.STRICT_BACKEND === 'true') { recordEndpointFailure(endpoint.id); } let errMsg = `Backend inference failed (HTTP ${response.status})`; try { const errPayload = await response.json(); errMsg = errPayload.error || errPayload.message || errPayload.detail || errMsg; } catch { try { const textPayload = await response.text(); if (textPayload) errMsg = textPayload.slice(0, 300); } catch {} } throw new Error(typeof errMsg === 'string' ? errMsg : JSON.stringify(errMsg)); } const arrayBuf = await response.arrayBuffer(); const resultBuffer = Buffer.from(arrayBuf); // Validate if the response is actually an image or a JSON payload const sig = checkFileSignature(resultBuffer); let finalBuffer = resultBuffer; let detectedMime = (sig.valid && sig.mime) ? sig.mime : (response.headers.get('content-type') || endpoint.responseType || 'image/jpeg'); if (!sig.valid) { const textPayload = resultBuffer.toString('utf8'); if (textPayload.startsWith('{') || textPayload.startsWith('[')) { try { const jsonResp = JSON.parse(textPayload); // Check if payload contains base64 image data (e.g. Polaroid Effect presets or image_data) if (jsonResp && typeof jsonResp === 'object' && !Array.isArray(jsonResp)) { const candidateKeys = [ options.style, options.variant, options.effectId, 'image', 'image_data', 'result', 'neutral', 'cool', 'warm', 'instax', 'purple' ].filter(Boolean); const allKeys = [...candidateKeys, ...Object.keys(jsonResp)]; for (const key of allKeys) { const val = jsonResp[key]; if (typeof val === 'string' && (val.startsWith('data:image/') || val.length > 500)) { const b64Data = val.includes('base64,') ? val.split('base64,')[1] : val; const decodedBuf = Buffer.from(b64Data, 'base64'); const decodedSig = checkFileSignature(decodedBuf); if (decodedSig.valid) { recordEndpointSuccess(endpoint.id); return { buffer: decodedBuf, contentType: decodedSig.mime || 'image/png' }; } } } if (jsonResp.error || jsonResp.message || jsonResp.status === false || jsonResp.status === 'error' || jsonResp.detail) { const errMsg = jsonResp.error || jsonResp.message || jsonResp.detail || 'Processing failed from model server'; throw new Error(typeof errMsg === 'string' ? errMsg : JSON.stringify(errMsg)); } } } catch (e: any) { if (e.message && !e.message.includes('JSON at position')) { throw e; } } } } const contentType = detectedMime; recordEndpointSuccess(endpoint.id); return { buffer: finalBuffer, contentType, }; } catch (err: any) { clearTimeout(timeoutId); // If the backend returned an explicit error, propagate it if (err.message && !err.message.includes('fetch failed') && !err.message.includes('ECONNREFUSED') && !err.message.includes('abort')) { throw err; } console.warn(`[Backend API] Offline/Unreachable at ${targetUrl}`); if (process.env.STRICT_BACKEND === 'true') { recordEndpointFailure(endpoint.id); } throw new Error(`Local inference server at ${targetUrl} is unreachable. Please ensure the backend is running.`); } }