// Help desk translation (Marty, 2026-10-16: the first real ticket came in Italian). Member messages are checked for language // and get an English translation for Marty; his English replies go back in the member's language with his English underneath. // Same OpenRouter key and model as the chatbot (DATA_DIR/openrouter.key, reasoning off). Any failure returns null and the // desk carries on untranslated: translation never blocks a ticket or a reply. 'use strict'; const fs = require('fs'); const path = require('path'); const https = require('https'); let DATA_DIR = null; const MODEL = process.env.OPENROUTER_MODEL || 'deepseek/deepseek-v4-flash:nitro'; function init(o) { DATA_DIR = o.dataDir; } const orKey = () => { if (process.env.OPENROUTER_API_KEY) return process.env.OPENROUTER_API_KEY.trim(); try { return fs.readFileSync(path.join(DATA_DIR, 'openrouter.key'), 'utf8').trim(); } catch (e) { return ''; } }; function ask(system, user, maxTokens) { return new Promise(resolve => { const k = orKey(); if (!k) return resolve(null); const body = JSON.stringify({ model: MODEL, reasoning: { enabled: false }, max_tokens: maxTokens || 1200, temperature: 0, messages: [{ role: 'system', content: system }, { role: 'user', content: user }] }); const rq = https.request({ hostname: 'openrouter.ai', path: '/api/v1/chat/completions', method: 'POST', timeout: 20000, headers: { Authorization: 'Bearer ' + k, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) } }, res => { let d = ''; res.on('data', c => { d += c; }); res.on('end', () => { // OpenRouter can lead with keepalive whitespace; parse from the first brace const i = d.indexOf('{'); if (i < 0) return resolve(null); try { const j = JSON.parse(d.slice(i)); resolve(String(j.choices && j.choices[0] && j.choices[0].message && j.choices[0].message.content || '').trim() || null); } catch (e) { resolve(null); } }); }); rq.on('error', () => resolve(null)); rq.on('timeout', () => { rq.destroy(); resolve(null); }); rq.end(body); }); } // { lang: 'it', english: '...' } for a non-English text, { lang: 'en' } for English, null when it could not tell async function detect(text) { const t = String(text || '').trim(); if (t.length < 2) return null; const out = await ask('You identify the language of a customer support message and translate it to English. Answer with JSON only: ' + '{"lang":"","english":""}. ' + 'Keep names, numbers, wallet addresses, links and ticket references exactly as written.', t.slice(0, 4000), 1500); if (!out) return null; try { const j = JSON.parse(out.slice(out.indexOf('{'), out.lastIndexOf('}') + 1)); const lang = String(j.lang || '').toLowerCase().slice(0, 5); if (!/^[a-z]{2}(-[a-z]{2})?$/.test(lang)) return null; if (lang === 'en' || lang.startsWith('en-')) return { lang: 'en' }; const english = String(j.english || '').trim(); return english ? { lang, english: english.slice(0, 5000) } : null; } catch (e) { return null; } } // Marty's English reply in the member's language; null on failure async function to(text, lang) { if (!lang || lang === 'en') return null; const out = await ask('Translate this customer support reply from English into the language with ISO 639-1 code "' + lang + '". ' + 'Keep the tone warm and plain. Keep names, numbers, POL amounts, wallet addresses, links, button names in quotes and ticket references exactly as written. ' + 'Answer with the translation only, no notes.', String(text).slice(0, 4000), 1800); return out ? out.slice(0, 5000) : null; } const NAMES = { it: 'Italian', es: 'Spanish', fr: 'French', de: 'German', pt: 'Portuguese', nl: 'Dutch', pl: 'Polish', tr: 'Turkish', ru: 'Russian', uk: 'Ukrainian', ro: 'Romanian', id: 'Indonesian', tl: 'Filipino', vi: 'Vietnamese', th: 'Thai', ar: 'Arabic', hi: 'Hindi', bn: 'Bengali', ur: 'Urdu', zh: 'Chinese', ja: 'Japanese', ko: 'Korean', sw: 'Swahili', yo: 'Yoruba', ha: 'Hausa', el: 'Greek', cs: 'Czech', hu: 'Hungarian', sv: 'Swedish', no: 'Norwegian', da: 'Danish', fi: 'Finnish', he: 'Hebrew', fa: 'Persian' }; const langName = l => NAMES[String(l || '').slice(0, 2)] || String(l || '').toUpperCase(); module.exports = { init, detect, to, langName };