Spaces:
Runtime error
Runtime error
File size: 19,907 Bytes
fb15d01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | const env = require('../config/env');
const { NEXA_PERSONALITY } = require('../config/personality');
const axios = require('axios');
const https = require('https');
const { downloadProxyToBase64, downloadRelayB64ToBase64, fetchProxyJSON } = require('../utils/telegram_proxy.js');
const { buildProxyChain, postToRelay } = require('../utils/telegram_network');
// IPv4 agent β forces Gemini API calls over IPv4 to avoid Hugging Face routing issues
const ipv4Agent = new https.Agent({ family: 4 });
// ============================================================
// MULTI-KEY POOL β Built at startup, null slots are skipped
// ============================================================
const GEMINI_25_KEYS = [
env.GEMINI_API_KEY_1,
env.GEMINI_API_KEY_2,
env.GEMINI_API_KEY_3,
env.GEMINI_API_KEY_4,
].filter(Boolean);
const GEMINI_20_KEYS = [
env.GEMINI_API_KEY_1,
env.GEMINI_API_KEY_2,
].filter(Boolean);
const GROQ_KEYS = [
env.GROQ_API_KEY_1,
env.GROQ_API_KEY_2,
env.GROQ_API_KEY_3,
env.GROQ_API_KEY_4,
].filter(Boolean);
const CEREBRAS_VISION_KEYS = [
env.CEREBRAS_API_KEY_4, // D
env.CEREBRAS_API_KEY_3, // C
env.CEREBRAS_API_KEY_1, // A
env.CEREBRAS_API_KEY_2, // B
].filter(Boolean);
// ============================================================
// UNIVERSAL IMAGE INTERPRETER β System Prompt
// ============================================================
const VISION_SYSTEM_PROMPT = `
${NEXA_PERSONALITY}
[MODUL PENGLIHATAN β Universal Image Interpreter]
Anda baru saja menerima SEBUAH GAMBAR dari Tuan Faqih melalui Telegram.
TUGAS UTAMA ANDA:
Bertindaklah sebagai mata cerdas N.E.X.A. Analisis gambar ini secara komprehensif, dinamis, dan tanpa batasan kategori kaku. Pahami secara mendalam apa yang ada di dalam gambar, lalu hasilkan deskripsi naratif yang kaya, akurat, dan sangat mendetail.
PANDUAN ANALISIS DINAMIS:
- Jika gambar mengandung TEKS (dokumen, surat, tulisan tangan, screenshot), BACA dan EKSTRAK semua informasi krusialnya ke dalam narasi. Jangan lewatkan nama, angka penting, tanggal, nominal, kontak, atau nomor referensi.
- Jika gambar berupa DATA (tabel, grafik, struk belanja, tiket), EKSTRAK intisari dan nilai-nilai utamanya (misal: nama tempat, total harga, daftar barang, jadwal).
- Jika gambar adalah OBJEK/MOMEN/PRODUK (foto alam, orang, makanan, barang), DESKRIPSIKAN dengan natural, detail, dan gunakan kepribadian N.E.X.A.
- Jadilah PROAKTIF. Pahami *mengapa* gambar ini dikirimkan berdasarkan konteks visualnya.
ATURAN KELUARAN WAJIB:
- Tulis dalam Bahasa Indonesia yang natural dan mengalir.
- SELALU sebut "Tuan Faqih" sebagai subjek (contoh: "Tuan Faqih mengirimkan gambar...").
- Output HANYA boleh berupa SATU paragraf naratif yang kaya informasi. DILARANG KERAS menggunakan format poin-poin (bullet points).
- Jika ada caption dari pengguna, instruksi/konteks caption tersebut HARUS menjadi fokus utama dari arah narasimu, dan sertakan interpretasi maksud Tuan Faqih di bagian akhir.
`;
// ============================================================
// PROXY HELPER
// ============================================================
// Proxy list untuk JSON (getFile API) - Custom Relay cocok untuk ini
function getProxyList(targetUrl) {
return buildProxyChain(targetUrl);
}
// Proxy list untuk BINARY download (gambar/audio)
// Worker v3.0: Custom Relay menggunakan mode b64=true (JSON) agar lolos HF egress firewall
function getBinaryProxyList(targetUrl) {
const proxies = [];
const relayBase = env.NEXA_VERCEL_RELAY_URL || env.TELEGRAM_PROXY_URL;
if (relayBase) {
proxies.push({
name: 'Vercel Relay B64',
url: relayBase.replace(/\?url=$/, '').replace(/\/+$/, ''),
targetUrl,
useB64: true,
});
}
proxies.push({
name: 'AllOrigins',
url: `https://api.allorigins.win/raw?url=${encodeURIComponent(targetUrl)}`,
useB64: false,
});
return proxies;
}
async function fetchJsonWithFailover(targetUrl, opts = {}) {
const timeoutMs = (opts.timeout || 30) * 1000;
const proxies = getProxyList(targetUrl);
for (const proxy of proxies) {
try {
console.log(`[VISION] Getting JSON via: ${proxy.name}...`);
const parsed = await fetchProxyJSON(proxy.url, timeoutMs, 3, proxy.headers);
if (parsed.ok !== undefined) {
console.log(`[VISION] ${proxy.name} JSON fetch succeeded.`);
return parsed;
}
} catch (err) {
console.warn(`[VISION] ${proxy.name} JSON fetch failed: [${err.code || 'NO_CODE'}] ${err.message}`);
}
}
throw new Error('All download paths failed to retrieve valid JSON from Telegram.');
}
// ============================================================
// STEP 1: Download image as base64 via failover
// ============================================================
async function downloadTelegramImageAsBase64(fileId) {
if (!env.TELEGRAM_BOT_TOKEN) throw new Error('TELEGRAM_BOT_TOKEN is missing');
const getFileUrl = `https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/getFile?file_id=${fileId}`;
console.log('[VISION] Step 1: Getting file info...');
const fileData = await fetchJsonWithFailover(getFileUrl);
if (!fileData.ok) throw new Error('Telegram getFile error: ' + JSON.stringify(fileData));
const filePath = fileData.result.file_path;
// Mengunduh biner secara murni menggunakan Native Node.js
const fileUrl = `https://api.telegram.org/file/bot${env.TELEGRAM_BOT_TOKEN}/${filePath}`;
// Gunakan getBinaryProxyList - Custom Relay dilewati karena Cloudflare Worker timeout saat stream file besar
const proxies = getBinaryProxyList(fileUrl);
console.log('[VISION] Step 2: Downloading image binary...');
let base64Data = '';
for (const proxy of proxies) {
try {
console.log(`[VISION] Downloading binary via: ${proxy.name}...`);
let b64;
if (proxy.useB64) {
// Mode B64: Cloudflare Worker encode biner jadi JSON (bypass HF egress firewall)
b64 = await downloadRelayB64ToBase64(proxy.url, proxy.targetUrl, 20 * 1024 * 1024);
} else {
// Mode normal
b64 = await downloadProxyToBase64(proxy.url, 20 * 1024 * 1024);
}
if (b64 && b64.length > 100) {
base64Data = b64;
console.log(`[VISION] Image downloaded via ${proxy.name}. Base64 size:`, base64Data.length, 'chars');
break;
}
} catch (err) {
console.warn(`[VISION] ${proxy.name} binary download failed: [${err.code || 'NO_CODE'}] ${err.message}`);
}
}
if (!base64Data) {
throw new Error('Image download failed across all proxies. Proxy may be timing out or blocked.');
}
const ext = filePath.split('.').pop().toLowerCase();
let mimeType = 'image/jpeg';
if (ext === 'png') mimeType = 'image/png';
if (ext === 'webp') mimeType = 'image/webp';
return { mimeType, data: base64Data };
}
// ============================================================
// GEMINI VISION CALLER β with 503 Smart Retry
// ============================================================
async function callGeminiVision(apiKey, modelName, imageData, caption, retries = 3, systemPromptOverride = '') {
const captionContext = caption
? `\n[CAPTION/INSTRUKSI DARI TUAN FAQIH]: "${caption}"\nGunakan caption ini sebagai petunjuk utama apa yang Tuan Faqih inginkan dari gambar ini.`
: '\n[TIDAK ADA CAPTION]: Tuan Faqih tidak memberikan instruksi teks. Analisis gambar dan interpretasikan konteksnya secara cerdas.';
const finalSystemPrompt = systemPromptOverride || (VISION_SYSTEM_PROMPT + captionContext);
const url = `https://generativelanguage.googleapis.com/v1beta/models/${modelName}:generateContent?key=${apiKey}`;
const payload = {
systemInstruction: { parts: [{ text: finalSystemPrompt }] },
contents: [{
parts: [
{ text: systemPromptOverride ? 'Lakukan ekstraksi metadata sesuai system prompt.' : 'Analisis gambar ini sekarang dan hasilkan teks instruksi lengkap sesuai sistem prompt.' },
{ inlineData: { mimeType: imageData.mimeType, data: imageData.data } }
]
}],
generationConfig: { temperature: systemPromptOverride ? 0.1 : 0.4 } // Lower temperature for JSON extraction
};
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await axios.post(url, payload, {
headers: { 'Content-Type': 'application/json' },
maxBodyLength: Infinity,
maxContentLength: Infinity,
httpsAgent: ipv4Agent,
timeout: 30000
});
if (!response.data.candidates || response.data.candidates.length === 0) {
throw new Error('Gemini API returned no candidates.');
}
return response.data.candidates[0].content.parts[0].text;
} catch (e) {
const status = e.response?.status;
if (status === 503 && attempt < retries) {
const delay = attempt * 2000;
console.warn(`[VISION] 503 attempt ${attempt}/${retries}, retry in ${delay}ms...`);
await new Promise(r => setTimeout(r, delay));
continue;
}
throw e;
}
}
}
// ============================================================
// GROQ VISION CALLER (Llama 3.2 90B Vision) β with 503 Smart Retry
// ============================================================
async function callGroqVision(apiKey, imageData, caption, retries = 3, systemPromptOverride = '') {
const captionContext = caption
? `\nCaption dari pengguna: "${caption}". Gunakan ini sebagai petunjuk utama.`
: '\nTidak ada caption. Analisis gambar secara mandiri.';
const finalSystemPrompt = systemPromptOverride || (VISION_SYSTEM_PROMPT + captionContext);
const requestBody = {
model: 'llama-3.2-90b-vision-preview',
messages: [{
role: 'user',
content: [
{ type: 'text', text: finalSystemPrompt + (systemPromptOverride ? '' : '\n\nAnalisis gambar ini sekarang.') },
{ type: 'image_url', image_url: { url: `data:${imageData.mimeType};base64,${imageData.data}` } }
]
}],
temperature: systemPromptOverride ? 0.1 : 0.4,
max_tokens: 2048
};
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await axios.post('https://api.groq.com/openai/v1/chat/completions', requestBody, {
headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
httpsAgent: ipv4Agent,
maxBodyLength: Infinity,
maxContentLength: Infinity,
timeout: 25000
});
if (!response.data.choices || response.data.choices.length === 0) {
throw new Error('Groq Vision returned no choices.');
}
return response.data.choices[0].message.content;
} catch (e) {
const status = e.response?.status;
if (status === 503 && attempt < retries) {
const delay = attempt * 2000;
console.warn(`[VISION] Groq 503 attempt ${attempt}/${retries}, retry in ${delay}ms...`);
await new Promise(r => setTimeout(r, delay));
continue;
}
throw e;
}
}
}
// ============================================================
// CEREBRAS VISION CALLER (gemma-4-31b) β Ultra-Fast WSE-3 Vision
// ============================================================
async function callCerebrasVision(apiKey, imageData, caption, retries = 2, systemPromptOverride = '') {
if (!apiKey) throw new Error('No Cerebras API key provided');
const captionContext = caption
? `\nCaption dari pengguna: "${caption}". Gunakan ini sebagai petunjuk utama.`
: '\nTidak ada caption. Analisis gambar secara mandiri.';
const finalSystemPrompt = systemPromptOverride || (VISION_SYSTEM_PROMPT + captionContext);
const requestBody = {
model: 'gemma-4-31b',
messages: [
{ role: 'system', content: finalSystemPrompt },
{
role: 'user',
content: [
{ type: 'text', text: 'Analisis gambar ini secara mendetail sesuai instruksi sistem.' },
{ type: 'image_url', image_url: { url: `data:${imageData.mimeType};base64,${imageData.data}` } }
]
}
],
max_tokens: 1200
};
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await axios.post('https://api.cerebras.ai/v1/chat/completions', requestBody, {
headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
timeout: 25000
});
return response.data.choices[0].message.content;
} catch (e) {
if (e.response?.status === 404 || e.response?.status === 429 || e.response?.status === 400) throw e;
if (attempt === retries) throw e;
}
}
}
// ============================================================
// HF VISION CALLER (Qwen2-VL-7B-Instruct) β Final Safety Net
// ============================================================
async function callHuggingFaceVision(imageData, caption, systemPromptOverride = '') {
if (!env.HF_TOKEN) throw new Error('HF_TOKEN not configured');
const captionContext = caption
? `Caption dari pengguna: "${caption}". ` : '';
const finalPrompt = systemPromptOverride
? systemPromptOverride
: `${captionContext}Analisis gambar ini sekarang. ${VISION_SYSTEM_PROMPT.substring(0, 300)}`;
const prompt = finalPrompt;
const response = await axios.post(
'https://api-inference.huggingface.co/models/Qwen/Qwen2-VL-7B-Instruct',
{
inputs: {
image: imageData.data,
question: prompt
}
},
{
headers: {
'Authorization': `Bearer ${env.HF_INFERENCE_TOKEN}`,
'Content-Type': 'application/json'
},
timeout: 60000
}
);
const output = response.data;
if (Array.isArray(output) && output[0]?.generated_text) return output[0].generated_text;
if (typeof output === 'string') return output;
throw new Error('Unexpected HF response format: ' + JSON.stringify(output).substring(0, 200));
}
// ============================================================
// TIER 0: WORKER VISION β The Game Changer
// Worker mendownload gambar DAN memanggil Gemini langsung dari Cloudflare.
// N.E.X.A hanya menerima JSON kecil berisi teks deskripsi.
// TIDAK ADA file biner besar yang perlu diunduh oleh HF container!
// ============================================================
async function callWorkerVision(fileId, caption = '', systemPromptOverride = '') {
const relayBase = env.NEXA_VERCEL_RELAY_URL || env.TELEGRAM_PROXY_URL;
if (!relayBase || !env.TELEGRAM_BOT_TOKEN) {
throw new Error('Vercel relay URL or Bot Token not configured');
}
const getFileUrl = `https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/getFile?file_id=${fileId}`;
const proxies = getProxyList(getFileUrl);
console.log('[VISION-W0] Getting file path via relay...');
let fileData = null;
for (const proxy of proxies) {
try {
fileData = await fetchProxyJSON(proxy.url, 30000, 2, proxy.headers);
if (fileData?.ok) break;
} catch (e) {
console.warn(`[VISION-W0] getFile via ${proxy.name} failed: ${e.message}`);
}
}
if (!fileData?.ok || !fileData.result?.file_path) {
throw new Error(`Telegram getFile error: ${JSON.stringify(fileData).substring(0, 100)}`);
}
const filePath = fileData.result.file_path;
if (GEMINI_25_KEYS.length === 0) throw new Error('No Gemini 2.5 API keys configured');
const geminiKey = GEMINI_25_KEYS[Math.floor(Math.random() * GEMINI_25_KEYS.length)];
const captionContext = caption
? `\nCaption dari pengguna: "${caption}". Gunakan ini sebagai petunjuk utama.`
: '\nTidak ada caption. Analisis gambar secara mandiri.';
const finalSystemPrompt = systemPromptOverride || (VISION_SYSTEM_PROMPT + captionContext);
console.log('[VISION-W0] Requesting Vercel relay to process vision...');
const result = await postToRelay('/api/vision', {
file_path: filePath,
bot_token: env.TELEGRAM_BOT_TOKEN,
gemini_key: geminiKey,
prompt: 'Analisis gambar ini sekarang.',
system_prompt: finalSystemPrompt,
});
if (!result.ok || !result.description) {
throw new Error(`Relay vision failed: ${result.error || 'empty result'}`);
}
console.log('[VISION-W0] Relay vision SUCCESS! Length:', result.description.length);
return result.description;
}
// ============================================================
// MAIN ENTRY POINT β 12-TIER GOD MODE VISION FALLBACK
// Tier 0: Worker Vision (Cloudflare does everything)
// Tier 1-4: Gemini 2.5 Flash (Premium Quality, 4 Keys) + local file
// Tier 5-8: Groq Llama 3.2 90B Vision (Balanced, 4 Keys) + local file
// Tier 9-10: Gemini 2.0 Flash (Generous Quota, 2 Keys) + local file
// Tier 11: Hugging Face Qwen2-VL (Safety Net) + local file
// ============================================================
async function processTelegramImage(fileId, caption = '', systemPromptOverride = '') {
// ============================================================
// TIER 0: Coba Worker Vision DULU (no binary download!)
// ============================================================
try {
const workerResult = await callWorkerVision(fileId, caption, systemPromptOverride);
return workerResult;
} catch (workerErr) {
console.warn(`[VISION-W0] Worker Vision FAILED: ${workerErr.message}. Falling back to local download...`);
}
// ============================================================
// TIER 1-13: Download image ONCE, then try all AI providers
// ============================================================
console.log('[VISION] Downloading image from Telegram...');
const imageData = await downloadTelegramImageAsBase64(fileId);
console.log('[VISION] Image ready. Base64 size:', imageData.data.length, 'chars');
// Build tier list dynamically from available keys
const tiers = [
// Tier 1-4: Cerebras Gemma 4 31B Vision (Ultra-Fast WSE-3, DCAB order)
...CEREBRAS_VISION_KEYS.map((key, i) => ({
name: `Tier${i + 1} (Cerebras Gemma 4 Vision Key ${i + 1})`,
fn: () => callCerebrasVision(key, imageData, caption, 2, systemPromptOverride)
})),
// Tier 5-8: Gemini 3.6 Flash (Premium Quality, 4 Keys)
...GEMINI_25_KEYS.map((key, i) => ({
name: `Tier${CEREBRAS_VISION_KEYS.length + i + 1} (Gemini 3.6 Flash Key ${i + 1})`,
fn: () => callGeminiVision(key, 'gemini-3.6-flash', imageData, caption, 3, systemPromptOverride)
})),
// Tier 9-12: Groq Vision (Balanced, 4 Keys)
...GROQ_KEYS.map((key, i) => ({
name: `Tier${CEREBRAS_VISION_KEYS.length + GEMINI_25_KEYS.length + i + 1} (Groq Vision Key ${i + 1})`,
fn: () => callGroqVision(key, imageData, caption, 3, systemPromptOverride)
})),
// Tier 13: Hugging Face Qwen2-VL (Safety Net β No daily quota)
{
name: `Tier${CEREBRAS_VISION_KEYS.length + GEMINI_25_KEYS.length + GROQ_KEYS.length + 1} (HuggingFace Qwen2-VL)`,
fn: () => callHuggingFaceVision(imageData, caption, systemPromptOverride)
}
];
for (const tier of tiers) {
try {
console.log(`[VISION] Trying ${tier.name}...`);
const result = await tier.fn();
if (!result || result.trim().length < 10) throw new Error('Response too short or empty');
console.log(`[VISION] ${tier.name} SUCCESS. Output length:`, result.length);
return result;
} catch (e) {
const status = e.status || e.response?.status || 'NET';
const apiData = e.response?.data ? JSON.stringify(e.response.data) : '';
const errMsg = e.response?.data?.error?.message || e.message || 'Unknown error';
console.warn(`[VISION] ${tier.name} FAILED (${status}): ${errMsg} ${apiData ? '| ' + apiData : ''}`.substring(0, 500));
}
}
// FALLBACK FINAL β All tiers exhausted, alert user via Telegram
throw new Error('β οΈ VISION DOWN TOTAL: Semua 12 Tier Vision Engine gagal (Worker + 4x Gemini 2.5 + 4x Groq + 2x Gemini 2.0 + HuggingFace).');
}
module.exports = { processTelegramImage };
|