Files
html/api/wevia-fast-path.php
2026-04-16 20:30:02 +02:00

79 lines
6.6 KiB
PHP

<?php
function wevia_fast_path($msg) {
$m = mb_strtolower(trim($msg));
$r = null;
// PRIORITY BYPASS
if (preg_match('/reconcil|bilan|dirty|l99|saas|tout.*verif|ethica.*pays|ethica.*valid|ethica.*enrich|ethica.*campag|ethica.*maroc|ethica.*tunisie|ethica.*alger|medecins.*maroc|medecins.*tunisie|medecins.*alger|specialite|gap.*detail|ethica.*count|ethica.*countries|ethica.*repartition|hcp.*count/i', $m)) return null;
if ($m === 'ping' || strpos($m, 'ping') === 0) {
$r = trim(@shell_exec('echo S95:$(timeout 1 ping -c1 -W1 10.1.0.3 2>/dev/null | grep -oP "time=\K[0-9.]+") Groq:$(curl -so/dev/null -w%{http_code} -m2 https://api.groq.com 2>/dev/null)'));
} elseif (strpos($m, 'nonreg') !== false && strpos($m, 'lance') === false && strpos($m, 'run') === false) {
$d = @json_decode(@file_get_contents(__DIR__.'/nonreg-latest.json'),true); $r = $d ? 'NONREG: '.$d['pass'].'/'.$d['total'].' ('.$d['score'].'%)' : 'NONREG: run check';
} elseif ($m === 'aide' || $m === 'help') {
$r = "WEVIA MASTER 375 tools. reconcile dirty l99 ports providers git ethica crons docker nonreg ping aide";
} elseif (strpos($m, 'sovereign') !== false && strpos($m, 'health') !== false) {
$r = trim(@shell_exec('curl -s --max-time 5 http://127.0.0.1:4000/health'));
} elseif (strpos($m, 'diagn') !== false || (strpos($m, 'health') !== false && strpos($m, 'sovereign') === false)) {
$r = trim(@shell_exec('echo "LOAD: $(cat /proc/loadavg)" && echo "RAM: $(free -m | grep Mem)" && echo "DISK: $(df -h / | tail -1)" && echo "FPM: $(pgrep -c php-fpm) workers"'));
} elseif (strpos($m, 'disk') !== false || strpos($m, 'espace') !== false) {
$r = trim(@shell_exec('df -h / | tail -1'));
} elseif (strpos($m, 'reconcil') !== false || strpos($m, 'bilan') !== false || strpos($m, 'multiagent') !== false || strpos($m, 'mobilise') !== false || (strpos($m, 'diagno') !== false && strpos($m, 'complet') !== false) || (strpos($m, 'tout') !== false && strpos($m, 'verif') !== false)) {
$cmds = [];
$cmds[] = 'echo "=GIT=" && cd /var/www/html && echo "Dirty:$(git status -s|wc -l)" && git status -s 2>/dev/null|head -15';
$cmds[] = 'echo "=PORTS=" && ss -tlnp 2>/dev/null|grep -oP ":\K[0-9]+"|sort -un|tr "\n" " "';
$cmds[] = 'echo "" && echo "=SOVEREIGN=" && curl -s -m3 localhost:4000/health 2>/dev/null|head -c 80';
$cmds[] = 'echo "" && echo "=NONREG=" && php8.4 /var/www/html/api/nonreg-quick.php 2>/dev/null|tail -1';
$cmds[] = 'echo "=L99=" && echo "Pages:$(ls /var/www/html/*.html|wc -l) Wiki:$(ls /opt/weval-l99/wiki/*.json 2>/dev/null|wc -l)"';
$cmds[] = 'echo "=CRONS=" && echo "Active:$(crontab -l 2>/dev/null|grep -cv ^#)"';
$r = trim(@shell_exec(implode(' && ', $cmds)));
} elseif (strpos($m, 'dirty') !== false || strpos($m, 'fichiers modif') !== false) {
$r = trim(@shell_exec('cd /var/www/html && echo "DIRTY FILES:" && git status --short 2>/dev/null|head -40'));
} elseif (strpos($m, 'git push') !== false || strpos($m, 'commit') !== false || strpos($m, 'pousse') !== false) {
$r = trim(@shell_exec('cd /var/www/html && git add -A && git commit -m "auto-wevia-master" 2>&1|tail -3 && git push 2>&1|tail -3'));
} elseif (strpos($m, 'git') !== false) {
$r = trim(@shell_exec('cd /var/www/html && echo "DIRTY:$(git status -s|wc -l)" && git log --oneline -3 2>/dev/null'));
} elseif (strpos($m, 'l99') !== false || strpos($m, 'saas') !== false) {
$r = trim(@shell_exec('echo "Pages:$(ls /var/www/html/*.html|wc -l) Wiki:$(ls /opt/weval-l99/wiki/*.json 2>/dev/null|wc -l)" && echo "Last wiki:" && ls -lt /opt/weval-l99/wiki/*.json 2>/dev/null|head -3'));
} elseif (preg_match('/\bport\b/', $m)) {
$r = trim(@shell_exec('ss -tlnp 2>/dev/null|grep LISTEN|head -25'));
} elseif ((strpos($m, 'ethica') !== false || preg_match('/hcp|medecin|combien.*(maroc|tunis|alger)/i', $m))) {
$r = trim(@shell_exec('curl -s -m5 http://127.0.0.1/api/ethica-country-api.php 2>/dev/null') ?: 'Ethica: 141K HCPs');
} elseif (strpos($m, 'provider') !== false || strpos($m, 'cascade') !== false) {
$r = trim(@shell_exec('curl -s -m3 localhost:4000/health 2>/dev/null') ?: 'Sovereign: check');
} elseif (strpos($m, 'cron') !== false) {
$r = trim(@shell_exec('echo "Active:$(crontab -l 2>/dev/null|grep -cv "^#")" && crontab -l 2>/dev/null|grep -v "^#"|head -10'));
} elseif (strpos($m, 'docker') !== false) {
$r = trim(@shell_exec('docker ps --format "{{.Names}}: {{.Status}}" 2>/dev/null|head -10'));
} elseif (strpos($m, 'arena') !== false) {
$r = trim(@shell_exec('echo "Groq:$(timeout 2 curl -so/dev/null -w%{http_code} https://api.groq.com/openai/v1/models 2>/dev/null) Cerebras:$(timeout 2 curl -so/dev/null -w%{http_code} https://api.cerebras.ai/v1/models 2>/dev/null)"'));
} elseif (strpos($m, 'strateg') !== false || strpos($m, 'priorit') !== false) {
$r = "TOP3 Q2: 1.Cloud Scaleway 5POC 240KMAD 2.Ethica 3K/mois 3.Growth 3RDV/sem Pipeline:3.5MMAD";
} elseif (preg_match('/^(bonjour|hello|hi|salut|hey)/i', $m)) {
$r = "WEVIA Master IA souveraine WEVAL. 375 tools, 13 providers, 141K HCPs. Tapez: reconcile dirty l99 ports providers git ethica crons docker nonreg ping aide";
}
// LLM FALLBACK: si aucun intent technique matché et message > 30 chars → sovereign direct
if ($r === null && mb_strlen($m) > 30) {
$ch = curl_init("http://127.0.0.1:4000/v1/chat/completions");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_POSTFIELDS => json_encode([
"messages" => [
["role"=>"system","content"=>"Tu es WEVIA, IA souveraine de WEVAL Consulting Casablanca. Tu aides Yacine avec des réponses directes, concrètes et en français. Tu connais: WEVADS (email marketing), Ethica (141K HCPs pharma), 13 providers IA gratuits, 375 tools, 174 pages."],
["role"=>"user","content"=>$msg]
],
"max_tokens" => 500,
"stream" => false
]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15
]);
$r2 = curl_exec($ch);
curl_close($ch);
$d2 = @json_decode($r2, true);
$txt = $d2["choices"][0]["message"]["content"] ?? null;
if ($txt) return ["provider"=>"sovereign-direct","content"=>$txt,"tool"=>"llm-fallback","model"=>$d2["model"]??"auto"];
}
if ($r === null) return null;
return ['provider'=>'fast-path','content'=>$r,'tool'=>'fast-path'];
}