Translate: accept batch items individually so one malformed item cannot poison the rest; log partial parses

This commit is contained in:
martbost
2026-08-21 16:41:12 -05:00
parent af874a6022
commit 9a1d8dbbf7
+8 -4
View File
@@ -404,10 +404,14 @@ async function handleTranslate(req,res){
// parts: [lead, "1", text1, "2", text2, ...]
const arr=[];
for(let k=1;k+1<parts.length;k+=2){ arr[Number(parts[k])-1]=String(parts[k+1]).trim(); }
if(arr.length===miss.length&&arr.every(x=>typeof x==='string'&&x.length)){
miss.forEach((idx,j)=>{ const tr=String(arr[j]).slice(0,600); out[idx]=tr; cache[keyOf(texts[idx])]=tr; });
trDirty=true; done=true;
}
// per-item acceptance: one malformed item must never poison its batch
let got=0;
miss.forEach((idx,j)=>{
const tr=typeof arr[j]==='string'&&arr[j].length?String(arr[j]).slice(0,600):null;
if(tr){ out[idx]=tr; cache[keyOf(texts[idx])]=tr; got++; }
});
if(got){ trDirty=true; done=true; }
if(got<miss.length)console.error(`translate ${tl}: ${got}/${miss.length} items parsed`);
}else{ console.error('translate openrouter status',r.status); }
}catch(e){ console.error('translate error',e.message); }
}