File size: 21,957 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
const { GoogleGenerativeAI } = require('@google/generative-ai');
const axios = require('axios');
const env = require('../config/env');

// ============================================================
// NON-BLOCKING ASYNCHRONOUS LOGGING WRAPPERS
// Memastikan pencetakan log ke stdout tidak menahan Event Loop
// saat eksekusi panggilan jaringan AI atau alur routing berlangsung.
// ============================================================
function asyncLog(...args) {
  setImmediate(() => console.log(...args));
}
function asyncWarn(...args) {
  setImmediate(() => console.warn(...args));
}
function asyncError(...args) {
  setImmediate(() => console.error(...args));
}

// ============================================================
// MULTI-KEY AI INITIALIZATION
// ============================================================
const geminiClients = [
  env.GEMINI_API_KEY_1 ? new GoogleGenerativeAI(env.GEMINI_API_KEY_1) : null,
  env.GEMINI_API_KEY_2 ? new GoogleGenerativeAI(env.GEMINI_API_KEY_2) : null,
  env.GEMINI_API_KEY_3 ? new GoogleGenerativeAI(env.GEMINI_API_KEY_3) : null,
  env.GEMINI_API_KEY_4 ? new GoogleGenerativeAI(env.GEMINI_API_KEY_4) : null
];

const groqKeys = [
  env.GROQ_API_KEY_1,
  env.GROQ_API_KEY_2,
  env.GROQ_API_KEY_3,
  env.GROQ_API_KEY_4
];

const cerebrasKeys = [
  env.CEREBRAS_API_KEY_1,
  env.CEREBRAS_API_KEY_2,
  env.CEREBRAS_API_KEY_3,
  env.CEREBRAS_API_KEY_4
];

// ============================================================
// SMART ADAPTIVE CONTEXT ROUTING (SACR) β€” v1.0
// Memilah beban konteks secara otomatis:
//   MODE LIGHT ⚑ : Cerebras Gemma 4 31B di Tier 1 (chat biasa, perintah rutin, ~1.6 detik)
//   MODE HEAVY 🧠 : Google Gemini 3.6 Flash di Tier 1 (analisis, rekap, dokumen, penalaran berat)
// ============================================================

/** Batas panjang karakter (prompt + systemInstruction) untuk trigger MODE HEAVY */
const SACR_HEAVY_CHAR_THRESHOLD = 1000;

/**
 * Kata kunci pemicu MODE HEAVY β€” mencakup tugas analitik, keuangan mendalam,
 * pengelolaan kode, pemrosesan dokumen, riset, dan instruksi multi-langkah.
 */
const SACR_HEAVY_KEYWORDS = [
  // --- Keuangan & Akuntansi Berat ---
  'rekap', 'rekapitulasi', 'laporan bulanan', 'laporan tahunan', 'laporan mingguan',
  'laporan keuangan', 'audit keuangan', 'audit pengeluaran', 'audit pemasukan',
  'analisis keuangan', 'analisis pengeluaran', 'analisis pemasukan', 'analisis transaksi',
  'perbandingan bulan', 'perbandingan keuangan', 'evaluasi keuangan', 'evaluasi budget',
  'budget bulanan', 'alokasi budget', 'rekomendasi hemat', 'rencana keuangan',
  'cashflow', 'cash flow', 'neraca', 'saldo akhir bulan', 'ringkasan keuangan',
  'tren pengeluaran', 'tren pemasukan', 'grafik keuangan', 'kategori terbesar',
  // --- Dokumen & Berkas ---
  'ringkasan dokumen', 'ringkas dokumen', 'baca dokumen', 'baca pdf', 'baca file',
  'analisis dokumen', 'ekstrak dokumen', 'isi dokumen', 'konten file', 'resume dokumen',
  'transkrip', 'transkripsi', 'poin penting', 'kesimpulan dari', 'rangkum',
  'rangkuman panjang', 'summary panjang', 'summary dokumen', 'uraikan',
  // --- Penalaran & Riset Mendalam ---
  'analisis mendalam', 'penjelasan rinci', 'jelaskan secara detail', 'jelaskan panjang',
  'riset', 'penelitian', 'investigasi', 'kajian', 'telaah', 'telaah mendalam',
  'bandingkan', 'perbandingan antara', 'perbedaan antara', 'keunggulan dan kelemahan',
  'pro dan kontra', 'pros and cons', 'kelebihan kekurangan', 'evaluasi mendalam',
  'strategi', 'rekomendasi strategis', 'rencana jangka panjang', 'plan mendalam',
  // --- Kode & Teknis ---
  'refactor', 'refactoring', 'perbaiki kode', 'perbaiki fungsi', 'debug panjang',
  'codebase', 'arsitektur sistem', 'arsitektur kode', 'rancang ulang', 'desain sistem',
  'dokumentasi kode', 'buat dokumentasi', 'review kode', 'code review',
  'optimalkan', 'optimasi sistem', 'migrasi database', 'skema database',
  // --- Kalender & Tugas Kompleks ---
  'jadwal minggu ini', 'jadwal bulan ini', 'agenda bulanan', 'rencana minggu',
  'daftar tugas panjang', 'semua tugas', 'seluruh task', 'list lengkap',
  'prioritaskan semua', 'susun ulang jadwal', 'review minggu', 'weekly review',
  // --- Memori & Profil ---
  'semua yang aku', 'semua yang saya', 'seluruh riwayat', 'semua fakta',
  'profil lengkap', 'rekap percakapan', 'ringkasan obrolan', 'apa saja yang sudah',
  // --- Instruksi Multi-Langkah / Kompleks ---
  'langkah demi langkah', 'step by step', 'panduan lengkap', 'tutorial lengkap',
  'buatkan laporan', 'buat analisis', 'susunkan', 'formulasikan', 'rancangkan',
  'berikan rekomendasi lengkap', 'beri penjelasan komprehensif'
];

/**
 * Evaluasi apakah konteks tergolong HEAVY Mode.
 *
 * @param {string} prompt - Full prompt termasuk konteks memori dan riwayat obrolan
 * @param {string} systemInstruction - System prompt yang dikirim ke model
 * @param {object} [options] - Opsi tambahan termasuk forceHeavy dan userText
 * @returns {boolean} true = HEAVY mode, false = LIGHT mode
 */
function isHeavyContext(prompt, systemInstruction, options = {}) {
  // [SACR] 1. Cek override eksplisit (misal forceHeavy: true dari Cron Job Kategori A)
  if (options && typeof options.forceHeavy === 'boolean') {
    return options.forceHeavy;
  }

  // [SACR] 2. Ambil pesan MURNI Tuan Faqih β€” dikirim eksplisit via options.userText dari AI_Router.
  // Evaluasi threshold karakter & kata kunci HANYA pada teks ini,
  // bukan pada total prompt router (yang berisi 30K+ karakter histori + fakta profil).
  const rawUserChat = (options?.userText || '').trim();

  // Cek 1: Pesan murni > 1.000 karakter β†’ HEAVY
  if (rawUserChat.length > SACR_HEAVY_CHAR_THRESHOLD) return true;

  // Cek 2: Mengandung kata kunci kognitif berat β†’ HEAVY
  if (rawUserChat) {
    return SACR_HEAVY_KEYWORDS.some(kw => rawUserChat.toLowerCase().includes(kw));
  }

  // Tidak ada teks murni (dipanggil tanpa userText, misal dari cron non-forceHeavy) β†’ LIGHT
  return false;
}


/**
 * Execute AI Prompt with Smart Adaptive Context Routing (SACR) + 15-Layer Fallback
 *
 * MODE LIGHT ⚑ (Konteks Normal β€” default):
 *   Tier 1-4  : Cerebras Gemma 4 31B Key 1-4    (The Ultra-Fast WSE-3 Sprinters)
 *   Tier 5-8  : Gemini 3.6 Flash Key 1-4         (The Deep Thinkers & Fast Secondary)
 *   Tier 9-12 : Groq Llama 3.3 70B Versatile Key 1-4 (The Tertiary Fallback)
 *
 * MODE HEAVY 🧠 (Konteks Berat β€” otomatis jika threshold/keyword terpenuhi):
 *   Tier 1-4  : Gemini 3.6 Flash Key 1-4         (1 Juta Token Window, Deep Reasoning)
 *   Tier 5-8  : Cerebras Gemma 4 31B Key 1-4    (The Ultra-Fast WSE-3 Secondary)
 *   Tier 9-12 : Groq Llama 3.3 70B Versatile Key 1-4 (The Tertiary Fallback)
 *
 * Tier 13 : Hugging Face Gemma 4 31B IT          (The Free Safety Net)
 * Tier 14 : Mistral Pixtral 12B                  (The Reliable European Closer β€” 937.5K TPM)
 * Tier 15 : OpenRouter Multi-Model Free          (The Indestructible Last Resort)
 *
 * Trigger HEAVY otomatis:
 *   a) Pesan MURNI Tuan Faqih > 1.000 karakter
 *   b) Mengandung kata kunci: rekap, audit, analisis, dokumen, refactor, dll.
 *
 * Override manual: options.forceHeavy = true | false
 */
const getErrDetails = (e) => {
  const status = e.status || e.response?.status || 'NET';
  const data = e.response?.data ? JSON.stringify(e.response.data) : '';
  return `[${status}] ${e.message} ${data ? '| ' + data : ''}`.substring(0, 500);
};

function validateResponseJson(str, jsonMode) {
  if (!jsonMode) return str;
  if (!str || typeof str !== 'string') throw new Error('Empty response string');
  let cleanStr = str.replace(/```json/gi, '').replace(/```/g, '').trim();

  // Cari bracket pembuka pertama β€” bisa array [ atau object {
  const firstBracket = cleanStr.indexOf('[');
  const firstBrace = cleanStr.indexOf('{');

  // Pilih yang lebih awal muncul di string
  let startChar, endChar;
  if (firstBracket !== -1 && (firstBrace === -1 || firstBracket < firstBrace)) {
    startChar = '[';
    endChar = ']';
  } else if (firstBrace !== -1) {
    startChar = '{';
    endChar = '}';
  } else {
    throw new Error('No JSON bracket found in response');
  }

  const startIdx = cleanStr.indexOf(startChar);
  const endIdx = cleanStr.lastIndexOf(endChar);
  if (startIdx !== -1 && endIdx > startIdx) {
    cleanStr = cleanStr.substring(startIdx, endIdx + 1);
  }

  JSON.parse(cleanStr); // validate β€” throw jika malformed
  return str;
}


// ============================================================
// TOKEN USAGE ACCUMULATOR
// ============================================================
let currentSessionTokenUsage = { input_tokens: 0, output_tokens: 0, total_tokens: 0 };

function resetTokenAccumulator() {
  currentSessionTokenUsage = { input_tokens: 0, output_tokens: 0, total_tokens: 0 };
}

function getAccumulatedTokenUsage() {
  return { ...currentSessionTokenUsage };
}

async function executeWithFallback(prompt, systemInstruction = "", temperature = 0.3, jsonMode = true, options = {}) {
  // --- SMART ADAPTIVE CONTEXT ROUTING (SACR) ---
  // options.forceHeavy = true  β†’ paksa HEAVY mode
  // options.forceHeavy = false β†’ paksa LIGHT mode
  // options.forceHeavy = undefined β†’ deteksi otomatis via isHeavyContext()
  const heavy = options.forceHeavy === true
    ? true
    : options.forceHeavy === false
      ? false
      : isHeavyContext(prompt, systemInstruction, options);

  const sacrMode = heavy ? 'HEAVY 🧠 [Gemini 3.6 Flash Priority]' : 'LIGHT ⚑ [Cerebras Priority]';
  const inputChars = (prompt?.length || 0) + (systemInstruction?.length || 0);
  asyncLog(`[SACR] Mode: ${sacrMode} | Total chars: ${inputChars}`);

  // --- Bangun blok tier Cerebras dan Gemini secara terpisah ---
  const cerebrasBlock = cerebrasKeys
    .filter(Boolean)
    .map((key, i) => ({
      name: `Tier X (Cerebras Key ${i + 1})`,
      fn: () => callCerebras(key, prompt, systemInstruction, temperature, jsonMode)
    }));

  const geminiBlock = geminiClients
    .filter(Boolean)
    .map((client, i) => ({
      name: `Tier X (Gemini 3.6 Key ${i + 1})`,
      fn: () => callGeminiWithRetry(client, 'gemini-3.6-flash', prompt, systemInstruction, temperature, jsonMode)
    }));

  // Primary   = Tier 1-4 (Cerebras jika LIGHT, Gemini 3.6 jika HEAVY)
  // Secondary = Tier 5-8 (Gemini 3.6 jika LIGHT, Cerebras jika HEAVY)
  const primaryBlock   = (heavy ? geminiBlock   : cerebrasBlock);
  const secondaryBlock = (heavy ? cerebrasBlock  : geminiBlock);

  const tiers = [
    // Tier 1-4: Primary AI (Cerebras di LIGHT, Gemini di HEAVY)
    ...primaryBlock.map((t, i) => ({ ...t, name: t.name.replace('Tier X', `Tier ${i + 1}`) })),
    // Tier 5-8: Secondary AI (Gemini di LIGHT, Cerebras di HEAVY) β€” Gemini dipindah ke Tier 5-8
    ...secondaryBlock.map((t, i) => ({ ...t, name: t.name.replace('Tier X', `Tier ${i + 5}`) })),
    // Tier 9-12: Groq Llama 3.3 70B Versatile β€” Groq dipindah ke Tier 9-12
    ...groqKeys.filter(Boolean).map((key, i) => ({
      name: `Tier ${i + 9} (Groq Key ${i + 1})`,
      fn: () => callGroq(key, prompt, systemInstruction, temperature, jsonMode)
    })),
    // Tier 13: Hugging Face Gemma 4 31B
    ...(env.HF_INFERENCE_TOKEN ? [{
      name: 'Tier 13 (Hugging Face Gemma 4 31B)',
      fn: () => callHuggingFaceInference(prompt, systemInstruction, temperature, jsonMode)
    }] : []),
    // Tier 14: Mistral Pixtral 12B
    ...(env.MISTRAL_API_KEY ? [{
      name: 'Tier 14 (Mistral Pixtral 12B)',
      fn: () => callMistral(prompt, systemInstruction, temperature, jsonMode, 'pixtral-12b-2409')
    }] : []),
    // Tier 15: Puter AI Multi-Model Pool (Codestral -> GPT-4o -> Mistral-Large -> Gemma 4 31B)
    ...(env.PUTER_AUTH_TOKEN ? [{
      name: 'Tier 15 (Puter AI Pool - Codestral & GPT-4o)',
      fn: () => callPuter(prompt, systemInstruction, temperature, jsonMode, 'codestral-latest')
    }] : []),
    // Tier 16: OpenRouter Multi-Model Free Pool
    ...(env.OPENROUTER_API_KEY ? [{
      name: 'Tier 16 (OpenRouter)',
      fn: () => callOpenRouter(prompt, systemInstruction, temperature, jsonMode)
    }] : [])
  ];

  for (const tier of tiers) {
    try {
      asyncLog(`[FALLBACK] Trying ${tier.name}...`);
      const rawRes = await tier.fn();
      const validated = validateResponseJson(rawRes, jsonMode);

      // Accumulate token usage (1 token β‰ˆ 3.8 chars in Indonesian/English mix)
      const outputChars = validated?.length || 0;
      const inTok = Math.ceil(inputChars / 3.8);
      const outTok = Math.ceil(outputChars / 3.8);
      currentSessionTokenUsage.input_tokens += inTok;
      currentSessionTokenUsage.output_tokens += outTok;
      currentSessionTokenUsage.total_tokens += (inTok + outTok);

      return validated;
    } catch (e) {
      asyncWarn(`[FALLBACK] ${tier.name} failed:`, getErrDetails(e));
    }
  }

  // Fallback Final
  asyncError('[FALLBACK] ⚠️ All 15 AI layers exhausted. Entering Dumb Mode.');
  return JSON.stringify({
    intent: 'DUMB_MODE',
    extracted_data: null,
    god_mode_trigger: false,
    reply_message: '⚠️ Sistem Otak N.E.X.A (AI Router) mengalami Down Total di semua 15 peladen dunia. Mohon tunggu beberapa saat.'
  });
}

// ----------------------------------------------------
// API WRAPPERS WITH 503 SMART RETRY
// ----------------------------------------------------

async function callGeminiWithRetry(client, modelName, prompt, systemInstruction, temperature, jsonMode = true, retries = 1) {
  const generationConfig = { temperature };
  if (jsonMode) generationConfig.responseMimeType = 'application/json';
  
  const model = client.getGenerativeModel({
    model: modelName,
    systemInstruction: systemInstruction,
    generationConfig,
    // Gemini berjalan di arsitektur Google TPU dengan penalaran mendalam & konteks 1M token.
    // Diberi batas waktu 12.000ms (12 detik) agar cukup waktu untuk memproses beban berat.
    requestOptions: { timeout: 12000 }
  });

  for (let attempt = 1; attempt <= retries; attempt++) {
    try {
      const response = await model.generateContent(prompt);
      return response.response.text();
    } catch (e) {
      if (attempt === retries) throw e;
    }
  }
}

async function callGroq(apiKey, prompt, systemInstruction, temperature, jsonMode = true, retries = 1) {
  const safeSysInst = (jsonMode && !/json/i.test(systemInstruction + prompt))
    ? `${systemInstruction}\nRespond in valid json format.`
    : systemInstruction;

  const requestBody = {
    model: 'llama-3.3-70b-versatile',
    messages: [
      { role: 'system', content: safeSysInst },
      { role: 'user', content: prompt }
    ],
    temperature,
    max_tokens: 1500
  };
  if (jsonMode) requestBody.response_format = { type: 'json_object' };

  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' },
        // Groq berjalan di arsitektur LPU (Language Processing Unit) super cepat.
        // Timeout dibatas 4000ms (4 detik) agar cepat pindah jika terjadi antrean.
        timeout: 4000
      });
      return response.data.choices[0].message.content;
    } catch (e) {
      if (attempt === retries) throw e;
    }
  }
}

async function callCerebras(apiKey, prompt, systemInstruction, temperature, jsonMode = true, retries = 1) {
  if (!apiKey) throw new Error('No Cerebras API key provided');
  const requestBody = {
    model: 'gemma-4-31b',  // Restored: Gemma 4 31B (128k context window, unlike ZAI GLM 4.7's 8k limit)
    messages: [
      { role: 'system', content: systemInstruction },
      { role: 'user', content: prompt }
    ],
    temperature,
    max_tokens: 2500
  };
  // Note: Avoid response_format={type:'json_object'} on gemma-4-31b as Cerebras grammar parser truncates nested arrays

  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' },
        // Cerebras berjalan di arsitektur WSE-3 ultrafast (~1000 token/detik).
        // Timeout 5.000ms (5 detik) memberikan toleransi lebih saat antrean server sedang tinggi.
        timeout: 5000
      });
      return response.data.choices[0].message.content;
    } catch (e) {
      if (attempt === retries) throw e;
    }
  }
}

async function callHuggingFaceInference(prompt, systemInstruction, temperature, jsonMode = true, retries = 2) {
  const token = env.HF_INFERENCE_TOKEN;
  if (!token) throw new Error('No HF_INFERENCE_TOKEN configured');

  const requestBody = {
    model: 'google/gemma-4-31B-it',
    messages: [
      { role: 'system', content: systemInstruction },
      { role: 'user', content: prompt }
    ],
    temperature,
    top_p: 0.9,
    max_tokens: 1500
  };
  if (jsonMode) requestBody.response_format = { type: 'json_object' };

  for (let attempt = 1; attempt <= retries; attempt++) {
    try {
      const response = await axios.post('https://router.huggingface.co/v1/chat/completions', requestBody, {
        headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
        timeout: 8000
      });
      return response.data.choices[0].message.content;
    } catch (e) {
      if (attempt < retries && e.response?.status !== 400 && e.response?.status !== 404) {
        await new Promise(r => setTimeout(r, attempt * 1500));
        continue;
      }
      throw e;
    }
  }
}

async function callMistral(prompt, systemInstruction, temperature, jsonMode = true, modelId = 'pixtral-12b-2409', retries = 3) {
  const requestBody = {
    model: modelId,
    messages: [
      { role: 'system', content: systemInstruction },
      { role: 'user', content: prompt }
    ],
    temperature,
    max_tokens: 1500
  };
  if (jsonMode) requestBody.response_format = { type: 'json_object' };

  for (let attempt = 1; attempt <= retries; attempt++) {
    try {
      const response = await axios.post('https://api.mistral.ai/v1/chat/completions', requestBody, {
        headers: { 'Authorization': `Bearer ${env.MISTRAL_API_KEY}`, 'Content-Type': 'application/json' },
        timeout: 8000
      });
      return response.data.choices[0].message.content;
    } catch (e) {
      if (e.response?.status === 503 && attempt < retries) {
        await new Promise(r => setTimeout(r, attempt * 2000));
        continue;
      }
      throw e;
    }
  }
}

async function callPuter(prompt, systemInstruction, temperature, jsonMode = true, primaryModelId = 'codestral-latest', retries = 1) {
  const token = env.PUTER_AUTH_TOKEN;
  if (!token) throw new Error('No PUTER_AUTH_TOKEN configured');

  // Pool model aktif terverifikasi di Puter AI (diurutkan berdasar kecepatan benchmark real-time)
  const puterModels = [
    primaryModelId,
    'gpt-4o',
    'mistral-large-latest',
    'google/gemma-4-31b-it'
  ].filter((v, i, a) => a.indexOf(v) === i); // deduplicate

  const messages = [];
  if (systemInstruction) {
    messages.push({ role: 'system', content: systemInstruction });
  }
  messages.push({ role: 'user', content: prompt });

  for (const modelId of puterModels) {
    const requestBody = {
      model: modelId,
      messages: messages,
      temperature,
      max_tokens: 1500
    };
    if (jsonMode) requestBody.response_format = { type: 'json_object' };

    for (let attempt = 1; attempt <= retries; attempt++) {
      try {
        const response = await axios.post('https://api.puter.com/puterai/openai/v1/chat/completions', requestBody, {
          headers: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
          },
          timeout: 6000
        });
        return response.data.choices[0].message.content;
      } catch (e) {
        if (attempt < retries) {
          await new Promise(r => setTimeout(r, 1000));
          continue;
        }
        asyncWarn(`[FALLBACK] Puter AI model ${modelId} failed:`, getErrDetails(e));
        break; // Pindah ke model berikutnya dalam pool Puter AI
      }
    }
  }

  throw new Error('All Puter AI pool models exhausted.');
}

async function callOpenRouter(prompt, systemInstruction, temperature, jsonMode = true, retries = 2) {
  const models = [
    'google/gemma-4-31b-it:free',
    'google/gemma-4-26b-a4b-it:free',
    'meta-llama/llama-3.3-70b-instruct:free',
    'qwen/qwen3-next-80b-a3b-instruct:free',
    'liquid/lfm-2.5-1.2b-instruct:free'
  ];

  for (const model of models) {
    const requestBody = {
      model,
      messages: [
        { role: 'system', content: systemInstruction },
        { role: 'user', content: prompt }
      ],
      temperature,
      max_tokens: 1500
    };
    if (jsonMode) requestBody.response_format = { type: 'json_object' };

    for (let attempt = 1; attempt <= retries; attempt++) {
      try {
        const response = await axios.post('https://openrouter.ai/api/v1/chat/completions', requestBody, {
          headers: {
            'Authorization': `Bearer ${env.OPENROUTER_API_KEY}`,
            'Content-Type': 'application/json',
            'HTTP-Referer': 'https://nexa.ai',
            'X-Title': 'NEXA Assistant'
          },
          timeout: 7000
        });
        return response.data.choices[0].message.content;
      } catch (e) {
        if (e.response?.status === 503 && attempt < retries) {
          await new Promise(r => setTimeout(r, attempt * 2000));
          continue;
        }
        asyncWarn(`[FALLBACK] OpenRouter model ${model} failed:`, getErrDetails(e));
        break; // Stop retrying this specific model and jump to the next free model in the list
      }
    }
  }
  throw new Error('All OpenRouter fallback models exhausted.');
}

module.exports = { executeWithFallback, resetTokenAccumulator, getAccumulatedTokenUsage };