Shrink translation batches to 20 strings and raise the model token cap - 45-string batches truncated the JSON reply

This commit is contained in:
martbost
2026-08-21 07:34:44 -05:00
parent 944d2beb87
commit 0a1f779305
2 changed files with 3 additions and 3 deletions
+1 -1
View File
@@ -48,7 +48,7 @@
var nodes=collect(root||document.body);
var uniq=[],idx={};
nodes.forEach(function(n){ var t=n.nodeValue.trim(); if(!(t in idx)&&memo[t]==null){ idx[t]=uniq.length; uniq.push(t);} });
var chunks=[]; for(var i=0;i<uniq.length;i+=45)chunks.push(uniq.slice(i,i+45));
var chunks=[]; for(var i=0;i<uniq.length;i+=20)chunks.push(uniq.slice(i,i+20));
var p=Promise.resolve();
chunks.forEach(function(ch){
p=p.then(function(){
+2 -2
View File
@@ -371,7 +371,7 @@ async function handleTranslate(req,res){
if(trLimited(ip))return json(res,429,{error:'Too many translation requests — give it a minute.'});
const b=await bodyJson(req).catch(()=>null);
const tl=b?String(b.tl||''):'';
const texts=b&&Array.isArray(b.texts)?b.texts.slice(0,60).map(t=>String(t).slice(0,300)):null;
const texts=b&&Array.isArray(b.texts)?b.texts.slice(0,25).map(t=>String(t).slice(0,300)):null;
if(!TR_LANGS.has(tl)||!texts||!texts.length)return json(res,400,{error:'Bad request'});
if(texts.reduce((a,t)=>a+t.length,0)>9000)return json(res,400,{error:'Too much text'});
const cache=trLoad();
@@ -387,7 +387,7 @@ async function handleTranslate(req,res){
const r=await fetch('https://openrouter.ai/api/v1/chat/completions',{
method:'POST',signal:ctrl.signal,
headers:{'Authorization':`Bearer ${apiKey}`,'Content-Type':'application/json','HTTP-Referer':'https://rmcircle.team','X-Title':'RM Circle Translate'},
body:JSON.stringify({model:OPENROUTER_MODEL,max_tokens:3000,temperature:0,messages:[
body:JSON.stringify({model:OPENROUTER_MODEL,max_tokens:6000,temperature:0,messages:[
{role:'system',content:`You translate website UI strings from English to ${TR_LANG_NAMES[tl]}. Reply with ONLY a JSON array of the translated strings, same order and length as the input array. Keep these words untranslated wherever they appear: Scintilla, Ascensus, Fabrica, Culmen, Apex, Fastigium, Vertex, Corona, POL, Polygon, RM Circle, MoonPay, MetaMask, Trust Wallet. Keep numbers, #ids, emoji and punctuation intact. Natural, friendly tone.`},
{role:'user',content:JSON.stringify(miss.map(i=>texts[i]))}
]})