← back to Enrich Local Hybrid

apply-patch.js

32 lines

const fs = require('fs');
let s = fs.readFileSync('/tmp/enrich-ai-tags.js', 'utf8');
const orig = s;

// 1) require the hybrid module — after the GEMINI_HOST const
const anchor1 = "const GEMINI_HOST    = 'generativelanguage.googleapis.com';";
s = s.replace(anchor1, anchor1 +
  "\n\n// --- HYBRID LOCAL ENRICHMENT (off unless ENRICH_PROVIDER=local|hybrid) ---\n" +
  "const { localAnalyze } = require('./enrich-local');\n" +
  "const ENRICH_PROVIDER = process.env.ENRICH_PROVIDER || 'gemini';");

// 2) dispatcher just before geminiAnalyze definition
const anchor2 = "async function geminiAnalyze(imageUrl, mode, attempt = 0) {";
s = s.replace(anchor2,
  "// analyze(): try local hybrid first (if enabled); on ANY local failure fall back to Gemini.\n" +
  "async function analyze(imageUrl, mode) {\n" +
  "  if (ENRICH_PROVIDER === 'local' || ENRICH_PROVIDER === 'hybrid') {\n" +
  "    try { return await localAnalyze(imageUrl, mode, fetchBuffer); }\n" +
  "    catch (e) { console.log(`    local enrich failed (${e.message}) — Gemini fallback`); }\n" +
  "  }\n" +
  "  return geminiAnalyze(imageUrl, mode);\n" +
  "}\n\n" + anchor2);

// 3) main call site -> analyze()
const anchor3 = "aiData = await geminiAnalyze([row.image_url, ...parseAllImages(row.all_images)], MODE);";
s = s.replace(anchor3, "aiData = await analyze([row.image_url, ...parseAllImages(row.all_images)], MODE);");

if (s === orig) { console.error('PATCH FAILED: no anchors matched'); process.exit(1); }
const hits = [anchor1, "async function analyze", "await analyze("].filter(a => s.includes(a)).length;
fs.writeFileSync('/tmp/enrich-ai-tags.patched.js', s);
console.log(`applied; ${hits}/3 markers present`);