123 lines
5.9 KiB
PHP
123 lines
5.9 KiB
PHP
<?php
|
|
header("Content-Type: application/json");
|
|
$path = "/var/www/html/wevia.html";
|
|
$c = @file_get_contents($path);
|
|
$orig = strlen($c);
|
|
|
|
if (strpos($c, "AMBRE-V10-MERMAID") !== false) {
|
|
echo json_encode(["already_wired"=>true, "size"=>$orig]);
|
|
exit;
|
|
}
|
|
|
|
// Insert BEFORE V9-PDF-PREMIUM (so mermaid match comes first if both patterns match)
|
|
$anchor = " // === AMBRE-V9-PDF-PREMIUM 2026-04-22";
|
|
$idx = strpos($c, $anchor);
|
|
if ($idx === false) {
|
|
echo json_encode(["error"=>"V9 anchor not found"]);
|
|
exit;
|
|
}
|
|
|
|
// Also make sure the _ambre_gen_pat doesn't catch "mermaid" which we want to route here first
|
|
// Currently _ambre_gen_pat matches mermaid → route to V2-GEN-ROUTER
|
|
// Solution: put V10 BEFORE V2 check (before all routers), by placing it in send() very early
|
|
// Actually just before V9 is OK since that's before V2 in flow
|
|
|
|
$v10 = <<<'JS'
|
|
// === AMBRE-V10-MERMAID 2026-04-22 · Mermaid RAG + inline SVG render + artifact panel ===
|
|
var _mermaid_intent_pat = /(?:mermaid|sch[eé]ma|diagramme|flowchart|sequence\s+diagram|gantt\s+chart)/i;
|
|
if (_mermaid_intent_pat.test(text)) {
|
|
if (typeof showThinking === 'function') showThinking();
|
|
busy = true;
|
|
try{var sb=document.getElementById("sendBtn");if(sb)sb.disabled=true;}catch(e){}
|
|
|
|
var _fetch = (typeof window.__ambreFetch === 'function') ? window.__ambreFetch : fetch;
|
|
_fetch('/api/ambre-tool-mermaid.php', {
|
|
method: 'POST',
|
|
headers: {'Content-Type':'application/json'},
|
|
body: JSON.stringify({topic: text})
|
|
})
|
|
.then(function(r){ return r.text().then(function(t){ try{return JSON.parse(t);}catch(e){return null;} }); })
|
|
.then(function(data){
|
|
if (typeof hideThinking === 'function') hideThinking();
|
|
busy = false;
|
|
try{var sb=document.getElementById("sendBtn");if(sb)sb.disabled=false;}catch(e){}
|
|
try{var mi=document.getElementById("msgInput");if(mi){mi.value="";mi.disabled=false;}}catch(e){}
|
|
|
|
if (!data || !data.ok) {
|
|
addMsg('assistant', '❌ Erreur génération Mermaid. ' + ((data && data.error) || 'Réessayez.'), '0');
|
|
return;
|
|
}
|
|
|
|
var mcode = data.mermaid_code;
|
|
var topic = data.topic || text;
|
|
var src = data.source || 'unknown';
|
|
var kind = data.kind || 'flowchart';
|
|
|
|
// Badges
|
|
var srcBadge = (src === 'kb_reused') ?
|
|
'<span class="nx-badge" style="background:rgba(16,185,129,.15);color:#10b981">♻️ KB Reused (' + (data.use_count || 0) + ' uses)</span>' :
|
|
'<span class="nx-badge" style="background:rgba(99,102,241,.15);color:#6366f1">🧠 LLM Generated</span>';
|
|
|
|
var badges = '<div style="display:flex;gap:6px;flex-wrap:wrap;margin:8px 0">' +
|
|
srcBadge +
|
|
'<span class="nx-badge" style="background:rgba(139,92,246,.15);color:#8b5cf6">' + kind + '</span>' +
|
|
'<span class="nx-badge" style="background:rgba(245,158,11,.15);color:#f59e0b">' + (data.elapsed_ms || 0) + 'ms</span>' +
|
|
'</div>';
|
|
|
|
// Inline render div
|
|
var uniqId = 'mmd-' + Date.now();
|
|
var inlineBlock = '<div style="margin:12px 0;padding:14px;background:#fafafa;border:1px solid #e5e7eb;border-radius:12px">' +
|
|
'<div style="font-weight:600;font-size:13px;color:#6b7280;margin-bottom:10px">📊 ' + topic + '</div>' +
|
|
'<div class="mermaid" id="' + uniqId + '" style="text-align:center">' + mcode + '</div>' +
|
|
'<details style="margin-top:10px"><summary style="cursor:pointer;font-size:11px;color:#94a3b8">📝 Voir le code</summary>' +
|
|
'<pre style="background:#1a1a2e;color:#e6edf3;padding:10px;border-radius:8px;font-size:11px;margin-top:8px;overflow-x:auto">' +
|
|
mcode.replace(/</g,'<').replace(/>/g,'>') + '</pre></details>' +
|
|
'</div>';
|
|
|
|
addMsg('assistant', badges + inlineBlock, (data.elapsed_ms/1000).toFixed(2));
|
|
|
|
// Trigger mermaid render after DOM update
|
|
setTimeout(function(){
|
|
try {
|
|
if (window.mermaid && typeof window.mermaid.run === 'function') {
|
|
window.mermaid.run({ nodes: [document.getElementById(uniqId)] });
|
|
} else if (window.mermaid && typeof window.mermaid.init === 'function') {
|
|
window.mermaid.init(undefined, document.getElementById(uniqId));
|
|
}
|
|
} catch(e) { console.warn('mermaid render fail', e); }
|
|
}, 300);
|
|
|
|
// Also open artifact panel with preview
|
|
if (typeof openPreview === 'function') {
|
|
try {
|
|
var svgWrap = '<div style="padding:20px;background:#fff;height:100%;overflow:auto"><h3 style="margin-bottom:16px">' + topic + '</h3><div class="mermaid">' + mcode + '</div></div>';
|
|
openPreview({type:'html', content: svgWrap});
|
|
} catch(e) {}
|
|
}
|
|
})
|
|
.catch(function(err){
|
|
if (typeof hideThinking === 'function') hideThinking();
|
|
busy = false;
|
|
try{var sb=document.getElementById("sendBtn");if(sb)sb.disabled=false;}catch(e){}
|
|
addMsg('assistant', '❌ Service Mermaid temporairement indisponible.', '0');
|
|
});
|
|
return;
|
|
}
|
|
// === END AMBRE-V10-MERMAID ===
|
|
|
|
JS;
|
|
|
|
$new_c = substr($c, 0, $idx) . $v10 . substr($c, $idx);
|
|
$delta = strlen($new_c) - $orig;
|
|
|
|
$backup = "/opt/wevads/vault/wevia.html.GOLD-" . date("Ymd-His") . "-v10-mermaid";
|
|
@copy($path, $backup);
|
|
$wrote = @file_put_contents($path, $new_c);
|
|
|
|
echo json_encode([
|
|
"delta" => $delta,
|
|
"wrote" => $wrote,
|
|
"backup" => basename($backup),
|
|
"new_size" => strlen($new_c),
|
|
]);
|