diff --git a/lib/hooks/image-analyzer-transformer.cjs b/lib/hooks/image-analyzer-transformer.cjs index bea62c96..bd8e7387 100755 --- a/lib/hooks/image-analyzer-transformer.cjs +++ b/lib/hooks/image-analyzer-transformer.cjs @@ -75,7 +75,7 @@ function parseProviderModels(envValue) { const result = {}; envValue.split(',').forEach((pair) => { const [provider, model] = pair.split(':'); - if (provider && model) { + if (provider && model && model.trim()) { result[provider.trim()] = model.trim(); } }); @@ -200,12 +200,21 @@ function analyzeViaCliProxy(base64Data, mediaType, model, timeoutMs) { data += chunk; }); + res.on('error', (err) => { + reject(err); + }); + res.on('end', () => { if (res.statusCode !== 200) { reject(new Error(`CLIProxy returned status ${res.statusCode}: ${data}`)); return; } + if (!data || !data.trim()) { + reject(new Error('Empty response from CLIProxy')); + return; + } + try { const response = JSON.parse(data); const text = response.content?.[0]?.text; @@ -384,8 +393,8 @@ async function processHook() { // Check file size const stats = fs.statSync(filePath); - if (stats.size > MAX_FILE_SIZE_BYTES) { - outputError(filePath, `File too large (${(stats.size / 1024 / 1024).toFixed(2)}MB > ${MAX_FILE_SIZE_MB}MB)`); + if (stats.size >= MAX_FILE_SIZE_BYTES) { + outputError(filePath, `File too large (${(stats.size / 1024 / 1024).toFixed(2)}MB >= ${MAX_FILE_SIZE_MB}MB)`); return; } @@ -401,7 +410,7 @@ async function processHook() { const model = getModelForProvider(); const timeout = parseInt(process.env.CCS_IMAGE_ANALYSIS_TIMEOUT || DEFAULT_TIMEOUT_SEC, 10); - const timeoutMs = timeout * 1000; + const timeoutMs = Math.max(1, Math.min(600, timeout)) * 1000; if (process.env.CCS_DEBUG) { console.error(`[CCS Hook] Analyzing ${path.basename(filePath)} via CLIProxy (${model})`);